1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SelectionDAG::Legalize method.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/PseudoSourceValue.h"
20 #include "llvm/Target/TargetFrameInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Target/TargetSubtarget.h"
26 #include "llvm/CallingConv.h"
27 #include "llvm/Constants.h"
28 #include "llvm/DerivedTypes.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/SmallPtrSet.h"
38 //===----------------------------------------------------------------------===//
39 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40 /// hacks on it until the target machine can handle it. This involves
41 /// eliminating value sizes the machine cannot handle (promoting small sizes to
42 /// large sizes or splitting up large values into small values) as well as
43 /// eliminating operations the machine cannot handle.
45 /// This code also does a small amount of optimization and recognition of idioms
46 /// as part of its processing. For example, if a target does not support a
47 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48 /// will attempt merge setcc and brc instructions into brcc's.
51 class VISIBILITY_HIDDEN SelectionDAGLegalize {
55 // Libcall insertion helpers.
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
60 SDValue LastCALLSEQ_END;
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
68 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
70 Expand // Try to expand this to other ops, otherwise use a libcall.
73 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
76 TargetLowering::ValueTypeActionImpl ValueTypeActions;
78 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
81 DenseMap<SDValue, SDValue> LegalizedNodes;
83 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
86 DenseMap<SDValue, SDValue> PromotedNodes;
88 /// ExpandedNodes - For nodes that need to be expanded this map indicates
89 /// which operands are the expanded version of the input. This allows
90 /// us to avoid expanding the same node more than once.
91 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
93 /// SplitNodes - For vector nodes that need to be split, this map indicates
94 /// which operands are the split version of the input. This allows us
95 /// to avoid splitting the same node more than once.
96 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
98 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
100 /// processed to the result.
101 std::map<SDValue, SDValue> ScalarizedNodes;
103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
106 std::map<SDValue, SDValue> WidenNodes;
108 void AddLegalizedOperand(SDValue From, SDValue To) {
109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
112 LegalizedNodes.insert(std::make_pair(To, To));
114 void AddPromotedOperand(SDValue From, SDValue To) {
115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
116 assert(isNew && "Got into the map somehow?");
118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
121 void AddWidenedOperand(SDValue From, SDValue To) {
122 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
123 assert(isNew && "Got into the map somehow?");
125 // If someone requests legalization of the new node, return itself.
126 LegalizedNodes.insert(std::make_pair(To, To));
130 explicit SelectionDAGLegalize(SelectionDAG &DAG);
132 /// getTypeAction - Return how we should legalize values of this type, either
133 /// it is already legal or we need to expand it into multiple registers of
134 /// smaller integer type, or we need to promote it to a larger type.
135 LegalizeAction getTypeAction(MVT VT) const {
136 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
139 /// isTypeLegal - Return true if this type is legal on this target.
141 bool isTypeLegal(MVT VT) const {
142 return getTypeAction(VT) == Legal;
148 /// HandleOp - Legalize, Promote, or Expand the specified operand as
149 /// appropriate for its type.
150 void HandleOp(SDValue Op);
152 /// LegalizeOp - We know that the specified value has a legal type.
153 /// Recursively ensure that the operands have legal types, then return the
155 SDValue LegalizeOp(SDValue O);
157 /// UnrollVectorOp - We know that the given vector has a legal type, however
158 /// the operation it performs is not legal and is an operation that we have
159 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
160 /// operating on each element individually.
161 SDValue UnrollVectorOp(SDValue O);
163 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
164 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
165 /// is necessary to spill the vector being inserted into to memory, perform
166 /// the insert there, and then read the result back.
167 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
170 /// PromoteOp - Given an operation that produces a value in an invalid type,
171 /// promote it to compute the value into a larger type. The produced value
172 /// will have the correct bits for the low portion of the register, but no
173 /// guarantee is made about the top bits: it may be zero, sign-extended, or
175 SDValue PromoteOp(SDValue O);
177 /// ExpandOp - Expand the specified SDValue into its two component pieces
178 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
179 /// the LegalizedNodes map is filled in for any results that are not expanded,
180 /// the ExpandedNodes map is filled in for any results that are expanded, and
181 /// the Lo/Hi values are returned. This applies to integer types and Vector
183 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
185 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
186 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
187 /// for the existing elements but no guarantee is made about the new elements
188 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
189 /// when we have an instruction operating on an illegal vector type and we
190 /// want to widen it to do the computation on a legal wider vector type.
191 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
193 /// SplitVectorOp - Given an operand of vector type, break it down into
194 /// two smaller values.
195 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
197 /// ScalarizeVectorOp - Given an operand of single-element vector type
198 /// (e.g. v1f32), convert it into the equivalent operation that returns a
199 /// scalar (e.g. f32) value.
200 SDValue ScalarizeVectorOp(SDValue O);
202 /// Useful 16 element vector type that is used to pass operands for widening.
203 typedef SmallVector<SDValue, 16> SDValueVector;
205 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
206 /// the LdChain contains a single load and false if it contains a token
207 /// factor for multiple loads. It takes
208 /// Result: location to return the result
209 /// LdChain: location to return the load chain
210 /// Op: load operation to widen
211 /// NVT: widen vector result type we want for the load
212 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
213 SDValue Op, MVT NVT);
215 /// Helper genWidenVectorLoads - Helper function to generate a set of
216 /// loads to load a vector with a resulting wider type. It takes
217 /// LdChain: list of chains for the load we have generated
218 /// Chain: incoming chain for the ld vector
219 /// BasePtr: base pointer to load from
220 /// SV: memory disambiguation source value
221 /// SVOffset: memory disambiugation offset
222 /// Alignment: alignment of the memory
223 /// isVolatile: volatile load
224 /// LdWidth: width of memory that we want to load
225 /// ResType: the wider result result type for the resulting loaded vector
226 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
227 SDValue BasePtr, const Value *SV,
228 int SVOffset, unsigned Alignment,
229 bool isVolatile, unsigned LdWidth,
232 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
233 /// location. It takes
234 /// ST: store node that we want to replace
235 /// Chain: incoming store chain
236 /// BasePtr: base address of where we want to store into
237 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
240 /// Helper genWidenVectorStores - Helper function to generate a set of
241 /// stores to store a widen vector into non widen memory
243 // StChain: list of chains for the stores we have generated
244 // Chain: incoming chain for the ld vector
245 // BasePtr: base pointer to load from
246 // SV: memory disambiguation source value
247 // SVOffset: memory disambiugation offset
248 // Alignment: alignment of the memory
249 // isVolatile: volatile lod
250 // ValOp: value to store
251 // StWidth: width of memory that we want to store
252 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
253 SDValue BasePtr, const Value *SV,
254 int SVOffset, unsigned Alignment,
255 bool isVolatile, SDValue ValOp,
258 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
259 /// specified mask and type. Targets can specify exactly which masks they
260 /// support and the code generator is tasked with not creating illegal masks.
262 /// Note that this will also return true for shuffles that are promoted to a
265 /// If this is a legal shuffle, this method returns the (possibly promoted)
266 /// build_vector Mask. If it's not a legal shuffle, it returns null.
267 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
269 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
270 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
272 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
273 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
274 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
275 LegalizeSetCCOperands(LHS, RHS, CC);
276 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
279 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
281 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
283 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
284 SDValue ExpandBUILD_VECTOR(SDNode *Node);
285 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
286 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
287 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
288 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
289 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
291 SDValue ExpandBSWAP(SDValue Op);
292 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
293 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
295 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
296 SDValue &Lo, SDValue &Hi);
298 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
299 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
303 /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
304 /// specified mask and type. Targets can specify exactly which masks they
305 /// support and the code generator is tasked with not creating illegal masks.
307 /// Note that this will also return true for shuffles that are promoted to a
309 SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
310 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
312 case TargetLowering::Legal:
313 case TargetLowering::Custom:
315 case TargetLowering::Promote: {
316 // If this is promoted to a different type, convert the shuffle mask and
317 // ask if it is legal in the promoted type!
318 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
319 MVT EltVT = NVT.getVectorElementType();
321 // If we changed # elements, change the shuffle mask.
322 unsigned NumEltsGrowth =
323 NVT.getVectorNumElements() / VT.getVectorNumElements();
324 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
325 if (NumEltsGrowth > 1) {
326 // Renumber the elements.
327 SmallVector<SDValue, 8> Ops;
328 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
329 SDValue InOp = Mask.getOperand(i);
330 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
331 if (InOp.getOpcode() == ISD::UNDEF)
332 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
334 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
335 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
339 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
345 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
348 SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
349 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
350 ValueTypeActions(TLI.getValueTypeActions()) {
351 assert(MVT::LAST_VALUETYPE <= 32 &&
352 "Too many value types for ValueTypeActions to hold!");
355 void SelectionDAGLegalize::LegalizeDAG() {
356 LastCALLSEQ_END = DAG.getEntryNode();
357 IsLegalizingCall = false;
359 // The legalize process is inherently a bottom-up recursive process (users
360 // legalize their uses before themselves). Given infinite stack space, we
361 // could just start legalizing on the root and traverse the whole graph. In
362 // practice however, this causes us to run out of stack space on large basic
363 // blocks. To avoid this problem, compute an ordering of the nodes where each
364 // node is only legalized after all of its operands are legalized.
365 DAG.AssignTopologicalOrder();
366 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
367 E = prior(DAG.allnodes_end()); I != next(E); ++I)
368 HandleOp(SDValue(I, 0));
370 // Finally, it's possible the root changed. Get the new root.
371 SDValue OldRoot = DAG.getRoot();
372 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
373 DAG.setRoot(LegalizedNodes[OldRoot]);
375 ExpandedNodes.clear();
376 LegalizedNodes.clear();
377 PromotedNodes.clear();
379 ScalarizedNodes.clear();
382 // Remove dead nodes now.
383 DAG.RemoveDeadNodes();
387 /// FindCallEndFromCallStart - Given a chained node that is part of a call
388 /// sequence, find the CALLSEQ_END node that terminates the call sequence.
389 static SDNode *FindCallEndFromCallStart(SDNode *Node) {
390 if (Node->getOpcode() == ISD::CALLSEQ_END)
392 if (Node->use_empty())
393 return 0; // No CallSeqEnd
395 // The chain is usually at the end.
396 SDValue TheChain(Node, Node->getNumValues()-1);
397 if (TheChain.getValueType() != MVT::Other) {
398 // Sometimes it's at the beginning.
399 TheChain = SDValue(Node, 0);
400 if (TheChain.getValueType() != MVT::Other) {
401 // Otherwise, hunt for it.
402 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
403 if (Node->getValueType(i) == MVT::Other) {
404 TheChain = SDValue(Node, i);
408 // Otherwise, we walked into a node without a chain.
409 if (TheChain.getValueType() != MVT::Other)
414 for (SDNode::use_iterator UI = Node->use_begin(),
415 E = Node->use_end(); UI != E; ++UI) {
417 // Make sure to only follow users of our token chain.
419 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
420 if (User->getOperand(i) == TheChain)
421 if (SDNode *Result = FindCallEndFromCallStart(User))
427 /// FindCallStartFromCallEnd - Given a chained node that is part of a call
428 /// sequence, find the CALLSEQ_START node that initiates the call sequence.
429 static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
430 assert(Node && "Didn't find callseq_start for a call??");
431 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
433 assert(Node->getOperand(0).getValueType() == MVT::Other &&
434 "Node doesn't have a token chain argument!");
435 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
438 /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
439 /// see if any uses can reach Dest. If no dest operands can get to dest,
440 /// legalize them, legalize ourself, and return false, otherwise, return true.
442 /// Keep track of the nodes we fine that actually do lead to Dest in
443 /// NodesLeadingTo. This avoids retraversing them exponential number of times.
445 bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
446 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
447 if (N == Dest) return true; // N certainly leads to Dest :)
449 // If we've already processed this node and it does lead to Dest, there is no
450 // need to reprocess it.
451 if (NodesLeadingTo.count(N)) return true;
453 // If the first result of this node has been already legalized, then it cannot
455 switch (getTypeAction(N->getValueType(0))) {
457 if (LegalizedNodes.count(SDValue(N, 0))) return false;
460 if (PromotedNodes.count(SDValue(N, 0))) return false;
463 if (ExpandedNodes.count(SDValue(N, 0))) return false;
467 // Okay, this node has not already been legalized. Check and legalize all
468 // operands. If none lead to Dest, then we can legalize this node.
469 bool OperandsLeadToDest = false;
470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
471 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
472 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
474 if (OperandsLeadToDest) {
475 NodesLeadingTo.insert(N);
479 // Okay, this node looks safe, legalize it and return false.
480 HandleOp(SDValue(N, 0));
484 /// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
485 /// appropriate for its type.
486 void SelectionDAGLegalize::HandleOp(SDValue Op) {
487 MVT VT = Op.getValueType();
488 switch (getTypeAction(VT)) {
489 default: assert(0 && "Bad type action!");
490 case Legal: (void)LegalizeOp(Op); break;
492 if (!VT.isVector()) {
497 // See if we can widen otherwise use Expand to either scalarize or split
498 MVT WidenVT = TLI.getWidenVectorType(VT);
499 if (WidenVT != MVT::Other) {
500 (void) WidenVectorOp(Op, WidenVT);
503 // else fall thru to expand since we can't widen the vector
506 if (!VT.isVector()) {
507 // If this is an illegal scalar, expand it into its two component
510 if (Op.getOpcode() == ISD::TargetConstant)
511 break; // Allow illegal target nodes.
513 } else if (VT.getVectorNumElements() == 1) {
514 // If this is an illegal single element vector, convert it to a
516 (void)ScalarizeVectorOp(Op);
518 // This is an illegal multiple element vector.
519 // Split it in half and legalize both parts.
521 SplitVectorOp(Op, X, Y);
527 /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
528 /// a load from the constant pool.
529 static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
530 SelectionDAG &DAG, TargetLowering &TLI) {
533 // If a FP immediate is precise when represented as a float and if the
534 // target can do an extending load from float to double, we put it into
535 // the constant pool as a float, even if it's is statically typed as a
536 // double. This shrinks FP constants and canonicalizes them for targets where
537 // an FP extending load is the same cost as a normal load (such as on the x87
538 // fp stack or PPC FP unit).
539 MVT VT = CFP->getValueType(0);
540 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
542 if (VT!=MVT::f64 && VT!=MVT::f32)
543 assert(0 && "Invalid type expansion");
544 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
545 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
550 while (SVT != MVT::f32) {
551 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
552 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
553 // Only do this if the target has a native EXTLOAD instruction from
555 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
556 TLI.ShouldShrinkFPConstant(OrigVT)) {
557 const Type *SType = SVT.getTypeForMVT();
558 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
564 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
565 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
567 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
568 CPIdx, PseudoSourceValue::getConstantPool(),
569 0, VT, false, Alignment);
570 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
571 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
575 /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
578 SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
579 SelectionDAG &DAG, TargetLowering &TLI) {
580 MVT VT = Node->getValueType(0);
581 MVT SrcVT = Node->getOperand(1).getValueType();
582 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
583 "fcopysign expansion only supported for f32 and f64");
584 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
586 // First get the sign bit of second operand.
587 SDValue Mask1 = (SrcVT == MVT::f64)
588 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
589 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
590 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
591 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
592 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
593 // Shift right or sign-extend it if the two operands have different types.
594 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
596 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
597 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
598 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
599 } else if (SizeDiff < 0) {
600 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
601 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
602 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
605 // Clear the sign bit of first operand.
606 SDValue Mask2 = (VT == MVT::f64)
607 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
608 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
609 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
610 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
611 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
613 // Or the value with the sign bit.
614 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
618 /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
620 SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
621 TargetLowering &TLI) {
622 SDValue Chain = ST->getChain();
623 SDValue Ptr = ST->getBasePtr();
624 SDValue Val = ST->getValue();
625 MVT VT = Val.getValueType();
626 int Alignment = ST->getAlignment();
627 int SVOffset = ST->getSrcValueOffset();
628 if (ST->getMemoryVT().isFloatingPoint() ||
629 ST->getMemoryVT().isVector()) {
630 // Expand to a bitconvert of the value to the integer type of the
631 // same size, then a (misaligned) int store.
633 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
635 else if (VT.is64BitVector() || VT==MVT::f64)
637 else if (VT==MVT::f32)
640 assert(0 && "Unaligned store of unsupported type");
642 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
643 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
644 SVOffset, ST->isVolatile(), Alignment);
646 assert(ST->getMemoryVT().isInteger() &&
647 !ST->getMemoryVT().isVector() &&
648 "Unaligned store of unknown type.");
649 // Get the half-size VT
651 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
652 int NumBits = NewStoredVT.getSizeInBits();
653 int IncrementSize = NumBits / 8;
655 // Divide the stored value in two parts.
656 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
658 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
660 // Store the two parts
661 SDValue Store1, Store2;
662 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
663 ST->getSrcValue(), SVOffset, NewStoredVT,
664 ST->isVolatile(), Alignment);
665 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
666 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
667 Alignment = MinAlign(Alignment, IncrementSize);
668 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
669 ST->getSrcValue(), SVOffset + IncrementSize,
670 NewStoredVT, ST->isVolatile(), Alignment);
672 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
675 /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
677 SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
678 TargetLowering &TLI) {
679 int SVOffset = LD->getSrcValueOffset();
680 SDValue Chain = LD->getChain();
681 SDValue Ptr = LD->getBasePtr();
682 MVT VT = LD->getValueType(0);
683 MVT LoadedVT = LD->getMemoryVT();
684 if (VT.isFloatingPoint() || VT.isVector()) {
685 // Expand to a (misaligned) integer load of the same size,
686 // then bitconvert to floating point or vector.
688 if (LoadedVT.is128BitVector() ||
689 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
691 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
693 else if (LoadedVT == MVT::f32)
696 assert(0 && "Unaligned load of unsupported type");
698 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
699 SVOffset, LD->isVolatile(),
701 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
702 if (VT.isFloatingPoint() && LoadedVT != VT)
703 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
705 SDValue Ops[] = { Result, Chain };
706 return DAG.getMergeValues(Ops, 2);
708 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
709 "Unaligned load of unsupported type.");
711 // Compute the new VT that is half the size of the old one. This is an
713 unsigned NumBits = LoadedVT.getSizeInBits();
715 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
718 unsigned Alignment = LD->getAlignment();
719 unsigned IncrementSize = NumBits / 8;
720 ISD::LoadExtType HiExtType = LD->getExtensionType();
722 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
723 if (HiExtType == ISD::NON_EXTLOAD)
724 HiExtType = ISD::ZEXTLOAD;
726 // Load the value in two parts
728 if (TLI.isLittleEndian()) {
729 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
730 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
731 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
732 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
733 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
734 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
735 MinAlign(Alignment, IncrementSize));
737 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
738 NewLoadedVT,LD->isVolatile(), Alignment);
739 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
740 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
741 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
742 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
743 MinAlign(Alignment, IncrementSize));
746 // aggregate the two parts
747 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
748 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
749 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
751 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
754 SDValue Ops[] = { Result, TF };
755 return DAG.getMergeValues(Ops, 2);
758 /// UnrollVectorOp - We know that the given vector has a legal type, however
759 /// the operation it performs is not legal and is an operation that we have
760 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
761 /// operating on each element individually.
762 SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
763 MVT VT = Op.getValueType();
764 assert(isTypeLegal(VT) &&
765 "Caller should expand or promote operands that are not legal!");
766 assert(Op.getNode()->getNumValues() == 1 &&
767 "Can't unroll a vector with multiple results!");
768 unsigned NE = VT.getVectorNumElements();
769 MVT EltVT = VT.getVectorElementType();
771 SmallVector<SDValue, 8> Scalars;
772 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
773 for (unsigned i = 0; i != NE; ++i) {
774 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
775 SDValue Operand = Op.getOperand(j);
776 MVT OperandVT = Operand.getValueType();
777 if (OperandVT.isVector()) {
778 // A vector operand; extract a single element.
779 MVT OperandEltVT = OperandVT.getVectorElementType();
780 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
783 DAG.getConstant(i, MVT::i32));
785 // A scalar operand; just use it as is.
786 Operands[j] = Operand;
789 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
790 &Operands[0], Operands.size()));
793 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
796 /// GetFPLibCall - Return the right libcall for the given floating point type.
797 static RTLIB::Libcall GetFPLibCall(MVT VT,
798 RTLIB::Libcall Call_F32,
799 RTLIB::Libcall Call_F64,
800 RTLIB::Libcall Call_F80,
801 RTLIB::Libcall Call_PPCF128) {
803 VT == MVT::f32 ? Call_F32 :
804 VT == MVT::f64 ? Call_F64 :
805 VT == MVT::f80 ? Call_F80 :
806 VT == MVT::ppcf128 ? Call_PPCF128 :
807 RTLIB::UNKNOWN_LIBCALL;
810 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
811 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
812 /// is necessary to spill the vector being inserted into to memory, perform
813 /// the insert there, and then read the result back.
814 SDValue SelectionDAGLegalize::
815 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
820 // If the target doesn't support this, we have to spill the input vector
821 // to a temporary stack slot, update the element, then reload it. This is
822 // badness. We could also load the value into a vector register (either
823 // with a "move to register" or "extload into register" instruction, then
824 // permute it into place, if the idx is a constant and if the idx is
825 // supported by the target.
826 MVT VT = Tmp1.getValueType();
827 MVT EltVT = VT.getVectorElementType();
828 MVT IdxVT = Tmp3.getValueType();
829 MVT PtrVT = TLI.getPointerTy();
830 SDValue StackPtr = DAG.CreateStackTemporary(VT);
832 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
835 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
836 PseudoSourceValue::getFixedStack(SPFI), 0);
838 // Truncate or zero extend offset to target pointer type.
839 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
840 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
841 // Add the offset to the index.
842 unsigned EltSize = EltVT.getSizeInBits()/8;
843 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
844 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
845 // Store the scalar value.
846 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
847 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
848 // Load the updated vector.
849 return DAG.getLoad(VT, Ch, StackPtr,
850 PseudoSourceValue::getFixedStack(SPFI), 0);
853 /// LegalizeOp - We know that the specified value has a legal type, and
854 /// that its operands are legal. Now ensure that the operation itself
855 /// is legal, recursively ensuring that the operands' operations remain
857 SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
858 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
861 assert(isTypeLegal(Op.getValueType()) &&
862 "Caller should expand or promote operands that are not legal!");
863 SDNode *Node = Op.getNode();
865 // If this operation defines any values that cannot be represented in a
866 // register on this target, make sure to expand or promote them.
867 if (Node->getNumValues() > 1) {
868 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
869 if (getTypeAction(Node->getValueType(i)) != Legal) {
870 HandleOp(Op.getValue(i));
871 assert(LegalizedNodes.count(Op) &&
872 "Handling didn't add legal operands!");
873 return LegalizedNodes[Op];
877 // Note that LegalizeOp may be reentered even from single-use nodes, which
878 // means that we always must cache transformed nodes.
879 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
880 if (I != LegalizedNodes.end()) return I->second;
882 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
884 bool isCustom = false;
886 switch (Node->getOpcode()) {
887 case ISD::FrameIndex:
888 case ISD::EntryToken:
890 case ISD::BasicBlock:
891 case ISD::TargetFrameIndex:
892 case ISD::TargetJumpTable:
893 case ISD::TargetConstant:
894 case ISD::TargetConstantFP:
895 case ISD::TargetConstantPool:
896 case ISD::TargetGlobalAddress:
897 case ISD::TargetGlobalTLSAddress:
898 case ISD::TargetExternalSymbol:
901 case ISD::MEMOPERAND:
904 // Primitives must all be legal.
905 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
906 "This must be legal!");
909 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
910 // If this is a target node, legalize it by legalizing the operands then
911 // passing it through.
912 SmallVector<SDValue, 8> Ops;
913 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
916 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
918 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
919 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
920 return Result.getValue(Op.getResNo());
922 // Otherwise this is an unhandled builtin node. splat.
924 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
926 assert(0 && "Do not know how to legalize this operator!");
928 case ISD::GLOBAL_OFFSET_TABLE:
929 case ISD::GlobalAddress:
930 case ISD::GlobalTLSAddress:
931 case ISD::ExternalSymbol:
932 case ISD::ConstantPool:
933 case ISD::JumpTable: // Nothing to do.
934 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Custom:
937 Tmp1 = TLI.LowerOperation(Op, DAG);
938 if (Tmp1.getNode()) Result = Tmp1;
939 // FALLTHROUGH if the target doesn't want to lower this op after all.
940 case TargetLowering::Legal:
945 case ISD::RETURNADDR:
946 // The only option for these nodes is to custom lower them. If the target
947 // does not custom lower them, then return zero.
948 Tmp1 = TLI.LowerOperation(Op, DAG);
952 Result = DAG.getConstant(0, TLI.getPointerTy());
954 case ISD::FRAME_TO_ARGS_OFFSET: {
955 MVT VT = Node->getValueType(0);
956 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
957 default: assert(0 && "This action is not supported yet!");
958 case TargetLowering::Custom:
959 Result = TLI.LowerOperation(Op, DAG);
960 if (Result.getNode()) break;
962 case TargetLowering::Legal:
963 Result = DAG.getConstant(0, VT);
968 case ISD::EXCEPTIONADDR: {
969 Tmp1 = LegalizeOp(Node->getOperand(0));
970 MVT VT = Node->getValueType(0);
971 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
972 default: assert(0 && "This action is not supported yet!");
973 case TargetLowering::Expand: {
974 unsigned Reg = TLI.getExceptionAddressRegister();
975 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
978 case TargetLowering::Custom:
979 Result = TLI.LowerOperation(Op, DAG);
980 if (Result.getNode()) break;
982 case TargetLowering::Legal: {
983 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
984 Result = DAG.getMergeValues(Ops, 2);
989 if (Result.getNode()->getNumValues() == 1) break;
991 assert(Result.getNode()->getNumValues() == 2 &&
992 "Cannot return more than two values!");
994 // Since we produced two values, make sure to remember that we
995 // legalized both of them.
996 Tmp1 = LegalizeOp(Result);
997 Tmp2 = LegalizeOp(Result.getValue(1));
998 AddLegalizedOperand(Op.getValue(0), Tmp1);
999 AddLegalizedOperand(Op.getValue(1), Tmp2);
1000 return Op.getResNo() ? Tmp2 : Tmp1;
1001 case ISD::EHSELECTION: {
1002 Tmp1 = LegalizeOp(Node->getOperand(0));
1003 Tmp2 = LegalizeOp(Node->getOperand(1));
1004 MVT VT = Node->getValueType(0);
1005 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1006 default: assert(0 && "This action is not supported yet!");
1007 case TargetLowering::Expand: {
1008 unsigned Reg = TLI.getExceptionSelectorRegister();
1009 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
1012 case TargetLowering::Custom:
1013 Result = TLI.LowerOperation(Op, DAG);
1014 if (Result.getNode()) break;
1016 case TargetLowering::Legal: {
1017 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
1018 Result = DAG.getMergeValues(Ops, 2);
1023 if (Result.getNode()->getNumValues() == 1) break;
1025 assert(Result.getNode()->getNumValues() == 2 &&
1026 "Cannot return more than two values!");
1028 // Since we produced two values, make sure to remember that we
1029 // legalized both of them.
1030 Tmp1 = LegalizeOp(Result);
1031 Tmp2 = LegalizeOp(Result.getValue(1));
1032 AddLegalizedOperand(Op.getValue(0), Tmp1);
1033 AddLegalizedOperand(Op.getValue(1), Tmp2);
1034 return Op.getResNo() ? Tmp2 : Tmp1;
1035 case ISD::EH_RETURN: {
1036 MVT VT = Node->getValueType(0);
1037 // The only "good" option for this node is to custom lower it.
1038 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1039 default: assert(0 && "This action is not supported at all!");
1040 case TargetLowering::Custom:
1041 Result = TLI.LowerOperation(Op, DAG);
1042 if (Result.getNode()) break;
1044 case TargetLowering::Legal:
1045 // Target does not know, how to lower this, lower to noop
1046 Result = LegalizeOp(Node->getOperand(0));
1051 case ISD::AssertSext:
1052 case ISD::AssertZext:
1053 Tmp1 = LegalizeOp(Node->getOperand(0));
1054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1056 case ISD::MERGE_VALUES:
1057 // Legalize eliminates MERGE_VALUES nodes.
1058 Result = Node->getOperand(Op.getResNo());
1060 case ISD::CopyFromReg:
1061 Tmp1 = LegalizeOp(Node->getOperand(0));
1062 Result = Op.getValue(0);
1063 if (Node->getNumValues() == 2) {
1064 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1066 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1067 if (Node->getNumOperands() == 3) {
1068 Tmp2 = LegalizeOp(Node->getOperand(2));
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1073 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1075 // Since CopyFromReg produces two values, make sure to remember that we
1076 // legalized both of them.
1077 AddLegalizedOperand(Op.getValue(0), Result);
1078 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1079 return Result.getValue(Op.getResNo());
1081 MVT VT = Op.getValueType();
1082 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Expand:
1086 Result = DAG.getConstant(0, VT);
1087 else if (VT.isFloatingPoint())
1088 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
1091 assert(0 && "Unknown value type!");
1093 case TargetLowering::Legal:
1099 case ISD::INTRINSIC_W_CHAIN:
1100 case ISD::INTRINSIC_WO_CHAIN:
1101 case ISD::INTRINSIC_VOID: {
1102 SmallVector<SDValue, 8> Ops;
1103 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1104 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1105 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1107 // Allow the target to custom lower its intrinsics if it wants to.
1108 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1109 TargetLowering::Custom) {
1110 Tmp3 = TLI.LowerOperation(Result, DAG);
1111 if (Tmp3.getNode()) Result = Tmp3;
1114 if (Result.getNode()->getNumValues() == 1) break;
1116 // Must have return value and chain result.
1117 assert(Result.getNode()->getNumValues() == 2 &&
1118 "Cannot return more than two values!");
1120 // Since loads produce two values, make sure to remember that we
1121 // legalized both of them.
1122 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1123 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1124 return Result.getValue(Op.getResNo());
1127 case ISD::DBG_STOPPOINT:
1128 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
1129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1131 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
1132 case TargetLowering::Promote:
1133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Expand: {
1135 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1136 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1137 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
1139 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
1140 if (MMI && (useDEBUG_LOC || useLABEL)) {
1141 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1142 unsigned SrcFile = MMI->RecordSource(CompileUnit);
1144 unsigned Line = DSP->getLine();
1145 unsigned Col = DSP->getColumn();
1148 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
1149 DAG.getConstant(Col, MVT::i32),
1150 DAG.getConstant(SrcFile, MVT::i32) };
1151 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
1153 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
1154 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
1157 Result = Tmp1; // chain
1161 case TargetLowering::Legal: {
1162 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1163 if (Action == Legal && Tmp1 == Node->getOperand(0))
1166 SmallVector<SDValue, 8> Ops;
1167 Ops.push_back(Tmp1);
1168 if (Action == Legal) {
1169 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1170 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1172 // Otherwise promote them.
1173 Ops.push_back(PromoteOp(Node->getOperand(1)));
1174 Ops.push_back(PromoteOp(Node->getOperand(2)));
1176 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1177 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1178 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1185 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1186 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1187 default: assert(0 && "This action is not supported yet!");
1188 case TargetLowering::Legal:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1194 case TargetLowering::Expand:
1195 Result = LegalizeOp(Node->getOperand(0));
1200 case ISD::DEBUG_LOC:
1201 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1202 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1203 default: assert(0 && "This action is not supported yet!");
1204 case TargetLowering::Legal: {
1205 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1206 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1207 if (Action == Legal && Tmp1 == Node->getOperand(0))
1209 if (Action == Legal) {
1210 Tmp2 = Node->getOperand(1);
1211 Tmp3 = Node->getOperand(2);
1212 Tmp4 = Node->getOperand(3);
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1215 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1216 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1224 case ISD::DBG_LABEL:
1226 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1227 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1228 default: assert(0 && "This action is not supported yet!");
1229 case TargetLowering::Legal:
1230 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1231 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1233 case TargetLowering::Expand:
1234 Result = LegalizeOp(Node->getOperand(0));
1240 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1241 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1242 default: assert(0 && "This action is not supported yet!");
1243 case TargetLowering::Legal:
1244 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1245 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1246 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1247 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1248 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1250 case TargetLowering::Expand:
1252 Result = LegalizeOp(Node->getOperand(0));
1257 case ISD::MEMBARRIER: {
1258 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
1259 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1260 default: assert(0 && "This action is not supported yet!");
1261 case TargetLowering::Legal: {
1263 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1264 for (int x = 1; x < 6; ++x) {
1265 Ops[x] = Node->getOperand(x);
1266 if (!isTypeLegal(Ops[x].getValueType()))
1267 Ops[x] = PromoteOp(Ops[x]);
1269 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1272 case TargetLowering::Expand:
1273 //There is no libgcc call for this op
1274 Result = Node->getOperand(0); // Noop
1280 case ISD::ATOMIC_CMP_SWAP_8:
1281 case ISD::ATOMIC_CMP_SWAP_16:
1282 case ISD::ATOMIC_CMP_SWAP_32:
1283 case ISD::ATOMIC_CMP_SWAP_64: {
1284 unsigned int num_operands = 4;
1285 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
1287 for (unsigned int x = 0; x < num_operands; ++x)
1288 Ops[x] = LegalizeOp(Node->getOperand(x));
1289 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1291 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1292 default: assert(0 && "This action is not supported yet!");
1293 case TargetLowering::Custom:
1294 Result = TLI.LowerOperation(Result, DAG);
1296 case TargetLowering::Legal:
1299 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1300 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1301 return Result.getValue(Op.getResNo());
1303 case ISD::ATOMIC_LOAD_ADD_8:
1304 case ISD::ATOMIC_LOAD_SUB_8:
1305 case ISD::ATOMIC_LOAD_AND_8:
1306 case ISD::ATOMIC_LOAD_OR_8:
1307 case ISD::ATOMIC_LOAD_XOR_8:
1308 case ISD::ATOMIC_LOAD_NAND_8:
1309 case ISD::ATOMIC_LOAD_MIN_8:
1310 case ISD::ATOMIC_LOAD_MAX_8:
1311 case ISD::ATOMIC_LOAD_UMIN_8:
1312 case ISD::ATOMIC_LOAD_UMAX_8:
1313 case ISD::ATOMIC_SWAP_8:
1314 case ISD::ATOMIC_LOAD_ADD_16:
1315 case ISD::ATOMIC_LOAD_SUB_16:
1316 case ISD::ATOMIC_LOAD_AND_16:
1317 case ISD::ATOMIC_LOAD_OR_16:
1318 case ISD::ATOMIC_LOAD_XOR_16:
1319 case ISD::ATOMIC_LOAD_NAND_16:
1320 case ISD::ATOMIC_LOAD_MIN_16:
1321 case ISD::ATOMIC_LOAD_MAX_16:
1322 case ISD::ATOMIC_LOAD_UMIN_16:
1323 case ISD::ATOMIC_LOAD_UMAX_16:
1324 case ISD::ATOMIC_SWAP_16:
1325 case ISD::ATOMIC_LOAD_ADD_32:
1326 case ISD::ATOMIC_LOAD_SUB_32:
1327 case ISD::ATOMIC_LOAD_AND_32:
1328 case ISD::ATOMIC_LOAD_OR_32:
1329 case ISD::ATOMIC_LOAD_XOR_32:
1330 case ISD::ATOMIC_LOAD_NAND_32:
1331 case ISD::ATOMIC_LOAD_MIN_32:
1332 case ISD::ATOMIC_LOAD_MAX_32:
1333 case ISD::ATOMIC_LOAD_UMIN_32:
1334 case ISD::ATOMIC_LOAD_UMAX_32:
1335 case ISD::ATOMIC_SWAP_32:
1336 case ISD::ATOMIC_LOAD_ADD_64:
1337 case ISD::ATOMIC_LOAD_SUB_64:
1338 case ISD::ATOMIC_LOAD_AND_64:
1339 case ISD::ATOMIC_LOAD_OR_64:
1340 case ISD::ATOMIC_LOAD_XOR_64:
1341 case ISD::ATOMIC_LOAD_NAND_64:
1342 case ISD::ATOMIC_LOAD_MIN_64:
1343 case ISD::ATOMIC_LOAD_MAX_64:
1344 case ISD::ATOMIC_LOAD_UMIN_64:
1345 case ISD::ATOMIC_LOAD_UMAX_64:
1346 case ISD::ATOMIC_SWAP_64: {
1347 unsigned int num_operands = 3;
1348 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
1350 for (unsigned int x = 0; x < num_operands; ++x)
1351 Ops[x] = LegalizeOp(Node->getOperand(x));
1352 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1354 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1355 default: assert(0 && "This action is not supported yet!");
1356 case TargetLowering::Custom:
1357 Result = TLI.LowerOperation(Result, DAG);
1359 case TargetLowering::Legal:
1362 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1363 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1364 return Result.getValue(Op.getResNo());
1366 case ISD::Constant: {
1367 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1369 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1371 // We know we don't need to expand constants here, constants only have one
1372 // value and we check that it is fine above.
1374 if (opAction == TargetLowering::Custom) {
1375 Tmp1 = TLI.LowerOperation(Result, DAG);
1381 case ISD::ConstantFP: {
1382 // Spill FP immediates to the constant pool if the target cannot directly
1383 // codegen them. Targets often have some immediate values that can be
1384 // efficiently generated into an FP register without a load. We explicitly
1385 // leave these constants as ConstantFP nodes for the target to deal with.
1386 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1388 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1389 default: assert(0 && "This action is not supported yet!");
1390 case TargetLowering::Legal:
1392 case TargetLowering::Custom:
1393 Tmp3 = TLI.LowerOperation(Result, DAG);
1394 if (Tmp3.getNode()) {
1399 case TargetLowering::Expand: {
1400 // Check to see if this FP immediate is already legal.
1401 bool isLegal = false;
1402 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1403 E = TLI.legal_fpimm_end(); I != E; ++I) {
1404 if (CFP->isExactlyValue(*I)) {
1409 // If this is a legal constant, turn it into a TargetConstantFP node.
1412 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1417 case ISD::TokenFactor:
1418 if (Node->getNumOperands() == 2) {
1419 Tmp1 = LegalizeOp(Node->getOperand(0));
1420 Tmp2 = LegalizeOp(Node->getOperand(1));
1421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1422 } else if (Node->getNumOperands() == 3) {
1423 Tmp1 = LegalizeOp(Node->getOperand(0));
1424 Tmp2 = LegalizeOp(Node->getOperand(1));
1425 Tmp3 = LegalizeOp(Node->getOperand(2));
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1428 SmallVector<SDValue, 8> Ops;
1429 // Legalize the operands.
1430 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1431 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1432 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1436 case ISD::FORMAL_ARGUMENTS:
1438 // The only option for this is to custom lower it.
1439 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1440 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
1441 // A call within a calling sequence must be legalized to something
1442 // other than the normal CALLSEQ_END. Violating this gets Legalize
1443 // into an infinite loop.
1444 assert ((!IsLegalizingCall ||
1445 Node->getOpcode() != ISD::CALL ||
1446 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
1447 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
1449 // The number of incoming and outgoing values should match; unless the final
1450 // outgoing value is a flag.
1451 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1452 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1453 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
1455 "Lowering call/formal_arguments produced unexpected # results!");
1457 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1458 // remember that we legalized all of them, so it doesn't get relegalized.
1459 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1460 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
1462 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1463 if (Op.getResNo() == i)
1465 AddLegalizedOperand(SDValue(Node, i), Tmp1);
1468 case ISD::EXTRACT_SUBREG: {
1469 Tmp1 = LegalizeOp(Node->getOperand(0));
1470 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1471 assert(idx && "Operand must be a constant");
1472 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
1473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1476 case ISD::INSERT_SUBREG: {
1477 Tmp1 = LegalizeOp(Node->getOperand(0));
1478 Tmp2 = LegalizeOp(Node->getOperand(1));
1479 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1480 assert(idx && "Operand must be a constant");
1481 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
1482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1485 case ISD::BUILD_VECTOR:
1486 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1487 default: assert(0 && "This action is not supported yet!");
1488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
1490 if (Tmp3.getNode()) {
1495 case TargetLowering::Expand:
1496 Result = ExpandBUILD_VECTOR(Result.getNode());
1500 case ISD::INSERT_VECTOR_ELT:
1501 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1502 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1504 // The type of the value to insert may not be legal, even though the vector
1505 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1507 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1508 default: assert(0 && "Cannot expand insert element operand");
1509 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1510 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1512 // FIXME: An alternative would be to check to see if the target is not
1513 // going to custom lower this operation, we could bitcast to half elt
1514 // width and perform two inserts at that width, if that is legal.
1515 Tmp2 = Node->getOperand(1);
1518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1520 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1521 Node->getValueType(0))) {
1522 default: assert(0 && "This action is not supported yet!");
1523 case TargetLowering::Legal:
1525 case TargetLowering::Custom:
1526 Tmp4 = TLI.LowerOperation(Result, DAG);
1527 if (Tmp4.getNode()) {
1532 case TargetLowering::Promote:
1533 // Fall thru for vector case
1534 case TargetLowering::Expand: {
1535 // If the insert index is a constant, codegen this as a scalar_to_vector,
1536 // then a shuffle that inserts it into the right position in the vector.
1537 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1538 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1539 // match the element type of the vector being created.
1540 if (Tmp2.getValueType() ==
1541 Op.getValueType().getVectorElementType()) {
1542 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1543 Tmp1.getValueType(), Tmp2);
1545 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1547 MVT::getIntVectorWithNumElements(NumElts);
1548 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
1550 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1551 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1552 // elt 0 of the RHS.
1553 SmallVector<SDValue, 8> ShufOps;
1554 for (unsigned i = 0; i != NumElts; ++i) {
1555 if (i != InsertPos->getZExtValue())
1556 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1558 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1560 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1561 &ShufOps[0], ShufOps.size());
1563 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1564 Tmp1, ScVec, ShufMask);
1565 Result = LegalizeOp(Result);
1569 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
1574 case ISD::SCALAR_TO_VECTOR:
1575 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1576 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1580 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1581 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1582 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1583 Node->getValueType(0))) {
1584 default: assert(0 && "This action is not supported yet!");
1585 case TargetLowering::Legal:
1587 case TargetLowering::Custom:
1588 Tmp3 = TLI.LowerOperation(Result, DAG);
1589 if (Tmp3.getNode()) {
1594 case TargetLowering::Expand:
1595 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1599 case ISD::VECTOR_SHUFFLE:
1600 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1601 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1602 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1604 // Allow targets to custom lower the SHUFFLEs they support.
1605 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1606 default: assert(0 && "Unknown operation action!");
1607 case TargetLowering::Legal:
1608 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1609 "vector shuffle should not be created if not legal!");
1611 case TargetLowering::Custom:
1612 Tmp3 = TLI.LowerOperation(Result, DAG);
1613 if (Tmp3.getNode()) {
1618 case TargetLowering::Expand: {
1619 MVT VT = Node->getValueType(0);
1620 MVT EltVT = VT.getVectorElementType();
1621 MVT PtrVT = TLI.getPointerTy();
1622 SDValue Mask = Node->getOperand(2);
1623 unsigned NumElems = Mask.getNumOperands();
1624 SmallVector<SDValue,8> Ops;
1625 for (unsigned i = 0; i != NumElems; ++i) {
1626 SDValue Arg = Mask.getOperand(i);
1627 if (Arg.getOpcode() == ISD::UNDEF) {
1628 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1630 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1631 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
1633 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1634 DAG.getConstant(Idx, PtrVT)));
1636 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1637 DAG.getConstant(Idx - NumElems, PtrVT)));
1640 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1643 case TargetLowering::Promote: {
1644 // Change base type to a different vector type.
1645 MVT OVT = Node->getValueType(0);
1646 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1648 // Cast the two input vectors.
1649 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1650 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1652 // Convert the shuffle mask to the right # elements.
1653 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1654 assert(Tmp3.getNode() && "Shuffle not legal?");
1655 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1656 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1662 case ISD::EXTRACT_VECTOR_ELT:
1663 Tmp1 = Node->getOperand(0);
1664 Tmp2 = LegalizeOp(Node->getOperand(1));
1665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1666 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1669 case ISD::EXTRACT_SUBVECTOR:
1670 Tmp1 = Node->getOperand(0);
1671 Tmp2 = LegalizeOp(Node->getOperand(1));
1672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1673 Result = ExpandEXTRACT_SUBVECTOR(Result);
1676 case ISD::CONCAT_VECTORS: {
1677 // Use extract/insert/build vector for now. We might try to be
1678 // more clever later.
1679 MVT PtrVT = TLI.getPointerTy();
1680 SmallVector<SDValue, 8> Ops;
1681 unsigned NumOperands = Node->getNumOperands();
1682 for (unsigned i=0; i < NumOperands; ++i) {
1683 SDValue SubOp = Node->getOperand(i);
1684 MVT VVT = SubOp.getNode()->getValueType(0);
1685 MVT EltVT = VVT.getVectorElementType();
1686 unsigned NumSubElem = VVT.getVectorNumElements();
1687 for (unsigned j=0; j < NumSubElem; ++j) {
1688 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1689 DAG.getConstant(j, PtrVT)));
1692 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1693 &Ops[0], Ops.size()));
1696 case ISD::CALLSEQ_START: {
1697 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1699 // Recursively Legalize all of the inputs of the call end that do not lead
1700 // to this call start. This ensures that any libcalls that need be inserted
1701 // are inserted *before* the CALLSEQ_START.
1702 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1703 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1704 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
1708 // Now that we legalized all of the inputs (which may have inserted
1709 // libcalls) create the new CALLSEQ_START node.
1710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1712 // Merge in the last call, to ensure that this call start after the last
1714 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1715 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1716 Tmp1 = LegalizeOp(Tmp1);
1719 // Do not try to legalize the target-specific arguments (#1+).
1720 if (Tmp1 != Node->getOperand(0)) {
1721 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1723 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1726 // Remember that the CALLSEQ_START is legalized.
1727 AddLegalizedOperand(Op.getValue(0), Result);
1728 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1729 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1731 // Now that the callseq_start and all of the non-call nodes above this call
1732 // sequence have been legalized, legalize the call itself. During this
1733 // process, no libcalls can/will be inserted, guaranteeing that no calls
1735 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1736 // Note that we are selecting this call!
1737 LastCALLSEQ_END = SDValue(CallEnd, 0);
1738 IsLegalizingCall = true;
1740 // Legalize the call, starting from the CALLSEQ_END.
1741 LegalizeOp(LastCALLSEQ_END);
1742 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1745 case ISD::CALLSEQ_END:
1746 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1747 // will cause this node to be legalized as well as handling libcalls right.
1748 if (LastCALLSEQ_END.getNode() != Node) {
1749 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1750 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1751 assert(I != LegalizedNodes.end() &&
1752 "Legalizing the call start should have legalized this node!");
1756 // Otherwise, the call start has been legalized and everything is going
1757 // according to plan. Just legalize ourselves normally here.
1758 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1759 // Do not try to legalize the target-specific arguments (#1+), except for
1760 // an optional flag input.
1761 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1762 if (Tmp1 != Node->getOperand(0)) {
1763 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1765 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1768 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1769 if (Tmp1 != Node->getOperand(0) ||
1770 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1771 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1774 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1777 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1778 // This finishes up call legalization.
1779 IsLegalizingCall = false;
1781 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1782 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1783 if (Node->getNumValues() == 2)
1784 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1785 return Result.getValue(Op.getResNo());
1786 case ISD::DYNAMIC_STACKALLOC: {
1787 MVT VT = Node->getValueType(0);
1788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1790 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1793 Tmp1 = Result.getValue(0);
1794 Tmp2 = Result.getValue(1);
1795 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1796 default: assert(0 && "This action is not supported yet!");
1797 case TargetLowering::Expand: {
1798 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1799 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1800 " not tell us which reg is the stack pointer!");
1801 SDValue Chain = Tmp1.getOperand(0);
1803 // Chain the dynamic stack allocation so that it doesn't modify the stack
1804 // pointer when other instructions are using the stack.
1805 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1807 SDValue Size = Tmp2.getOperand(1);
1808 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1809 Chain = SP.getValue(1);
1810 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
1811 unsigned StackAlign =
1812 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1813 if (Align > StackAlign)
1814 SP = DAG.getNode(ISD::AND, VT, SP,
1815 DAG.getConstant(-(uint64_t)Align, VT));
1816 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1817 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1819 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1820 DAG.getIntPtrConstant(0, true), SDValue());
1822 Tmp1 = LegalizeOp(Tmp1);
1823 Tmp2 = LegalizeOp(Tmp2);
1826 case TargetLowering::Custom:
1827 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1828 if (Tmp3.getNode()) {
1829 Tmp1 = LegalizeOp(Tmp3);
1830 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1833 case TargetLowering::Legal:
1836 // Since this op produce two values, make sure to remember that we
1837 // legalized both of them.
1838 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1839 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1840 return Op.getResNo() ? Tmp2 : Tmp1;
1842 case ISD::INLINEASM: {
1843 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1844 bool Changed = false;
1845 // Legalize all of the operands of the inline asm, in case they are nodes
1846 // that need to be expanded or something. Note we skip the asm string and
1847 // all of the TargetConstant flags.
1848 SDValue Op = LegalizeOp(Ops[0]);
1849 Changed = Op != Ops[0];
1852 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1853 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1854 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
1855 for (++i; NumVals; ++i, --NumVals) {
1856 SDValue Op = LegalizeOp(Ops[i]);
1865 Op = LegalizeOp(Ops.back());
1866 Changed |= Op != Ops.back();
1871 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1873 // INLINE asm returns a chain and flag, make sure to add both to the map.
1874 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1875 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1876 return Result.getValue(Op.getResNo());
1879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1880 // Ensure that libcalls are emitted before a branch.
1881 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1882 Tmp1 = LegalizeOp(Tmp1);
1883 LastCALLSEQ_END = DAG.getEntryNode();
1885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1889 // Ensure that libcalls are emitted before a branch.
1890 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1891 Tmp1 = LegalizeOp(Tmp1);
1892 LastCALLSEQ_END = DAG.getEntryNode();
1894 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1895 default: assert(0 && "Indirect target must be legal type (pointer)!");
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1904 // Ensure that libcalls are emitted before a branch.
1905 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1906 Tmp1 = LegalizeOp(Tmp1);
1907 LastCALLSEQ_END = DAG.getEntryNode();
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1912 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1913 default: assert(0 && "This action is not supported yet!");
1914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp1 = TLI.LowerOperation(Result, DAG);
1917 if (Tmp1.getNode()) Result = Tmp1;
1919 case TargetLowering::Expand: {
1920 SDValue Chain = Result.getOperand(0);
1921 SDValue Table = Result.getOperand(1);
1922 SDValue Index = Result.getOperand(2);
1924 MVT PTy = TLI.getPointerTy();
1925 MachineFunction &MF = DAG.getMachineFunction();
1926 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1927 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1928 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1931 switch (EntrySize) {
1932 default: assert(0 && "Size of jump table not supported yet."); break;
1933 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
1934 PseudoSourceValue::getJumpTable(), 0); break;
1935 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
1936 PseudoSourceValue::getJumpTable(), 0); break;
1940 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1941 // For PIC, the sequence is:
1942 // BRIND(load(Jumptable + index) + RelocBase)
1943 // RelocBase can be JumpTable, GOT or some sort of global base.
1944 if (PTy != MVT::i32)
1945 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1946 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1947 TLI.getPICJumpTableRelocBase(Table, DAG));
1949 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1954 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1955 // Ensure that libcalls are emitted before a return.
1956 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1957 Tmp1 = LegalizeOp(Tmp1);
1958 LastCALLSEQ_END = DAG.getEntryNode();
1960 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1961 case Expand: assert(0 && "It's impossible to expand bools");
1963 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1966 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1968 // The top bits of the promoted condition are not necessarily zero, ensure
1969 // that the value is properly zero extended.
1970 unsigned BitWidth = Tmp2.getValueSizeInBits();
1971 if (!DAG.MaskedValueIsZero(Tmp2,
1972 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
1973 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1978 // Basic block destination (Op#2) is always legal.
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1981 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1982 default: assert(0 && "This action is not supported yet!");
1983 case TargetLowering::Legal: break;
1984 case TargetLowering::Custom:
1985 Tmp1 = TLI.LowerOperation(Result, DAG);
1986 if (Tmp1.getNode()) Result = Tmp1;
1988 case TargetLowering::Expand:
1989 // Expand brcond's setcc into its constituent parts and create a BR_CC
1991 if (Tmp2.getOpcode() == ISD::SETCC) {
1992 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1993 Tmp2.getOperand(0), Tmp2.getOperand(1),
1994 Node->getOperand(2));
1996 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1997 DAG.getCondCode(ISD::SETNE), Tmp2,
1998 DAG.getConstant(0, Tmp2.getValueType()),
1999 Node->getOperand(2));
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2006 // Ensure that libcalls are emitted before a branch.
2007 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2008 Tmp1 = LegalizeOp(Tmp1);
2009 Tmp2 = Node->getOperand(2); // LHS
2010 Tmp3 = Node->getOperand(3); // RHS
2011 Tmp4 = Node->getOperand(1); // CC
2013 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
2014 LastCALLSEQ_END = DAG.getEntryNode();
2016 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
2017 // the LHS is a legal SETCC itself. In this case, we need to compare
2018 // the result against zero to select between true and false values.
2019 if (Tmp3.getNode() == 0) {
2020 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2021 Tmp4 = DAG.getCondCode(ISD::SETNE);
2024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2025 Node->getOperand(4));
2027 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2028 default: assert(0 && "Unexpected action for BR_CC!");
2029 case TargetLowering::Legal: break;
2030 case TargetLowering::Custom:
2031 Tmp4 = TLI.LowerOperation(Result, DAG);
2032 if (Tmp4.getNode()) Result = Tmp4;
2037 LoadSDNode *LD = cast<LoadSDNode>(Node);
2038 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2039 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2041 ISD::LoadExtType ExtType = LD->getExtensionType();
2042 if (ExtType == ISD::NON_EXTLOAD) {
2043 MVT VT = Node->getValueType(0);
2044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2045 Tmp3 = Result.getValue(0);
2046 Tmp4 = Result.getValue(1);
2048 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2049 default: assert(0 && "This action is not supported yet!");
2050 case TargetLowering::Legal:
2051 // If this is an unaligned load and the target doesn't support it,
2053 if (!TLI.allowsUnalignedMemoryAccesses()) {
2054 unsigned ABIAlignment = TLI.getTargetData()->
2055 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
2056 if (LD->getAlignment() < ABIAlignment){
2057 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
2059 Tmp3 = Result.getOperand(0);
2060 Tmp4 = Result.getOperand(1);
2061 Tmp3 = LegalizeOp(Tmp3);
2062 Tmp4 = LegalizeOp(Tmp4);
2066 case TargetLowering::Custom:
2067 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
2068 if (Tmp1.getNode()) {
2069 Tmp3 = LegalizeOp(Tmp1);
2070 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2073 case TargetLowering::Promote: {
2074 // Only promote a load of vector type to another.
2075 assert(VT.isVector() && "Cannot promote this load!");
2076 // Change base type to a different vector type.
2077 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
2079 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2080 LD->getSrcValueOffset(),
2081 LD->isVolatile(), LD->getAlignment());
2082 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2083 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2087 // Since loads produce two values, make sure to remember that we
2088 // legalized both of them.
2089 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2090 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
2091 return Op.getResNo() ? Tmp4 : Tmp3;
2093 MVT SrcVT = LD->getMemoryVT();
2094 unsigned SrcWidth = SrcVT.getSizeInBits();
2095 int SVOffset = LD->getSrcValueOffset();
2096 unsigned Alignment = LD->getAlignment();
2097 bool isVolatile = LD->isVolatile();
2099 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
2100 // Some targets pretend to have an i1 loading operation, and actually
2101 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2102 // bits are guaranteed to be zero; it helps the optimizers understand
2103 // that these bits are zero. It is also useful for EXTLOAD, since it
2104 // tells the optimizers that those bits are undefined. It would be
2105 // nice to have an effective generic way of getting these benefits...
2106 // Until such a way is found, don't insist on promoting i1 here.
2107 (SrcVT != MVT::i1 ||
2108 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
2109 // Promote to a byte-sized load if not loading an integral number of
2110 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
2111 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2112 MVT NVT = MVT::getIntegerVT(NewWidth);
2115 // The extra bits are guaranteed to be zero, since we stored them that
2116 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2118 ISD::LoadExtType NewExtType =
2119 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2121 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2122 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2123 NVT, isVolatile, Alignment);
2125 Ch = Result.getValue(1); // The chain.
2127 if (ExtType == ISD::SEXTLOAD)
2128 // Having the top bits zero doesn't help when sign extending.
2129 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2130 Result, DAG.getValueType(SrcVT));
2131 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2132 // All the top bits are guaranteed to be zero - inform the optimizers.
2133 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2134 DAG.getValueType(SrcVT));
2136 Tmp1 = LegalizeOp(Result);
2137 Tmp2 = LegalizeOp(Ch);
2138 } else if (SrcWidth & (SrcWidth - 1)) {
2139 // If not loading a power-of-2 number of bits, expand as two loads.
2140 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
2141 "Unsupported extload!");
2142 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2143 assert(RoundWidth < SrcWidth);
2144 unsigned ExtraWidth = SrcWidth - RoundWidth;
2145 assert(ExtraWidth < RoundWidth);
2146 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2147 "Load size not an integral number of bytes!");
2148 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2149 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
2151 unsigned IncrementSize;
2153 if (TLI.isLittleEndian()) {
2154 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2155 // Load the bottom RoundWidth bits.
2156 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2157 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2160 // Load the remaining ExtraWidth bits.
2161 IncrementSize = RoundWidth / 8;
2162 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2163 DAG.getIntPtrConstant(IncrementSize));
2164 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2165 LD->getSrcValue(), SVOffset + IncrementSize,
2166 ExtraVT, isVolatile,
2167 MinAlign(Alignment, IncrementSize));
2169 // Build a factor node to remember that this load is independent of the
2171 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2174 // Move the top bits to the right place.
2175 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2176 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2178 // Join the hi and lo parts.
2179 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2181 // Big endian - avoid unaligned loads.
2182 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2183 // Load the top RoundWidth bits.
2184 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2185 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2188 // Load the remaining ExtraWidth bits.
2189 IncrementSize = RoundWidth / 8;
2190 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2191 DAG.getIntPtrConstant(IncrementSize));
2192 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2193 LD->getSrcValue(), SVOffset + IncrementSize,
2194 ExtraVT, isVolatile,
2195 MinAlign(Alignment, IncrementSize));
2197 // Build a factor node to remember that this load is independent of the
2199 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2202 // Move the top bits to the right place.
2203 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2204 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2206 // Join the hi and lo parts.
2207 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2210 Tmp1 = LegalizeOp(Result);
2211 Tmp2 = LegalizeOp(Ch);
2213 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
2214 default: assert(0 && "This action is not supported yet!");
2215 case TargetLowering::Custom:
2218 case TargetLowering::Legal:
2219 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2220 Tmp1 = Result.getValue(0);
2221 Tmp2 = Result.getValue(1);
2224 Tmp3 = TLI.LowerOperation(Result, DAG);
2225 if (Tmp3.getNode()) {
2226 Tmp1 = LegalizeOp(Tmp3);
2227 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2230 // If this is an unaligned load and the target doesn't support it,
2232 if (!TLI.allowsUnalignedMemoryAccesses()) {
2233 unsigned ABIAlignment = TLI.getTargetData()->
2234 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
2235 if (LD->getAlignment() < ABIAlignment){
2236 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
2238 Tmp1 = Result.getOperand(0);
2239 Tmp2 = Result.getOperand(1);
2240 Tmp1 = LegalizeOp(Tmp1);
2241 Tmp2 = LegalizeOp(Tmp2);
2246 case TargetLowering::Expand:
2247 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2248 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2249 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2250 LD->getSrcValueOffset(),
2251 LD->isVolatile(), LD->getAlignment());
2252 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2253 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2254 Tmp2 = LegalizeOp(Load.getValue(1));
2257 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2258 // Turn the unsupported load into an EXTLOAD followed by an explicit
2259 // zero/sign extend inreg.
2260 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2261 Tmp1, Tmp2, LD->getSrcValue(),
2262 LD->getSrcValueOffset(), SrcVT,
2263 LD->isVolatile(), LD->getAlignment());
2265 if (ExtType == ISD::SEXTLOAD)
2266 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2267 Result, DAG.getValueType(SrcVT));
2269 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2270 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2271 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
2276 // Since loads produce two values, make sure to remember that we legalized
2278 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2279 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
2280 return Op.getResNo() ? Tmp2 : Tmp1;
2283 case ISD::EXTRACT_ELEMENT: {
2284 MVT OpTy = Node->getOperand(0).getValueType();
2285 switch (getTypeAction(OpTy)) {
2286 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2288 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2290 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2291 DAG.getConstant(OpTy.getSizeInBits()/2,
2292 TLI.getShiftAmountTy()));
2293 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2296 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2297 Node->getOperand(0));
2301 // Get both the low and high parts.
2302 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2303 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
2304 Result = Tmp2; // 1 -> Hi
2306 Result = Tmp1; // 0 -> Lo
2312 case ISD::CopyToReg:
2313 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2315 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2316 "Register type must be legal!");
2317 // Legalize the incoming value (must be a legal type).
2318 Tmp2 = LegalizeOp(Node->getOperand(2));
2319 if (Node->getNumValues() == 1) {
2320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2322 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2323 if (Node->getNumOperands() == 4) {
2324 Tmp3 = LegalizeOp(Node->getOperand(3));
2325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2331 // Since this produces two values, make sure to remember that we legalized
2333 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2334 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
2340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2342 // Ensure that libcalls are emitted before a return.
2343 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2344 Tmp1 = LegalizeOp(Tmp1);
2345 LastCALLSEQ_END = DAG.getEntryNode();
2347 switch (Node->getNumOperands()) {
2349 Tmp2 = Node->getOperand(1);
2350 Tmp3 = Node->getOperand(2); // Signness
2351 switch (getTypeAction(Tmp2.getValueType())) {
2353 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2356 if (!Tmp2.getValueType().isVector()) {
2358 ExpandOp(Tmp2, Lo, Hi);
2360 // Big endian systems want the hi reg first.
2361 if (TLI.isBigEndian())
2365 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2367 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2368 Result = LegalizeOp(Result);
2370 SDNode *InVal = Tmp2.getNode();
2371 int InIx = Tmp2.getResNo();
2372 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2373 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
2375 // Figure out if there is a simple type corresponding to this Vector
2376 // type. If so, convert to the vector type.
2377 MVT TVT = MVT::getVectorVT(EVT, NumElems);
2378 if (TLI.isTypeLegal(TVT)) {
2379 // Turn this into a return of the vector type.
2380 Tmp2 = LegalizeOp(Tmp2);
2381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2382 } else if (NumElems == 1) {
2383 // Turn this into a return of the scalar type.
2384 Tmp2 = ScalarizeVectorOp(Tmp2);
2385 Tmp2 = LegalizeOp(Tmp2);
2386 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2388 // FIXME: Returns of gcc generic vectors smaller than a legal type
2389 // should be returned in integer registers!
2391 // The scalarized value type may not be legal, e.g. it might require
2392 // promotion or expansion. Relegalize the return.
2393 Result = LegalizeOp(Result);
2395 // FIXME: Returns of gcc generic vectors larger than a legal vector
2396 // type should be returned by reference!
2398 SplitVectorOp(Tmp2, Lo, Hi);
2399 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2400 Result = LegalizeOp(Result);
2405 Tmp2 = PromoteOp(Node->getOperand(1));
2406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2407 Result = LegalizeOp(Result);
2412 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2414 default: { // ret <values>
2415 SmallVector<SDValue, 8> NewValues;
2416 NewValues.push_back(Tmp1);
2417 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2418 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2420 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2421 NewValues.push_back(Node->getOperand(i+1));
2425 assert(!Node->getOperand(i).getValueType().isExtended() &&
2426 "FIXME: TODO: implement returning non-legal vector types!");
2427 ExpandOp(Node->getOperand(i), Lo, Hi);
2428 NewValues.push_back(Lo);
2429 NewValues.push_back(Node->getOperand(i+1));
2431 NewValues.push_back(Hi);
2432 NewValues.push_back(Node->getOperand(i+1));
2437 assert(0 && "Can't promote multiple return value yet!");
2440 if (NewValues.size() == Node->getNumOperands())
2441 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2443 Result = DAG.getNode(ISD::RET, MVT::Other,
2444 &NewValues[0], NewValues.size());
2449 if (Result.getOpcode() == ISD::RET) {
2450 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2451 default: assert(0 && "This action is not supported yet!");
2452 case TargetLowering::Legal: break;
2453 case TargetLowering::Custom:
2454 Tmp1 = TLI.LowerOperation(Result, DAG);
2455 if (Tmp1.getNode()) Result = Tmp1;
2461 StoreSDNode *ST = cast<StoreSDNode>(Node);
2462 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2463 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2464 int SVOffset = ST->getSrcValueOffset();
2465 unsigned Alignment = ST->getAlignment();
2466 bool isVolatile = ST->isVolatile();
2468 if (!ST->isTruncatingStore()) {
2469 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2470 // FIXME: We shouldn't do this for TargetConstantFP's.
2471 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2472 // to phase ordering between legalized code and the dag combiner. This
2473 // probably means that we need to integrate dag combiner and legalizer
2475 // We generally can't do this one for long doubles.
2476 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
2477 if (CFP->getValueType(0) == MVT::f32 &&
2478 getTypeAction(MVT::i32) == Legal) {
2479 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2480 bitcastToAPInt().zextOrTrunc(32),
2482 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2483 SVOffset, isVolatile, Alignment);
2485 } else if (CFP->getValueType(0) == MVT::f64) {
2486 // If this target supports 64-bit registers, do a single 64-bit store.
2487 if (getTypeAction(MVT::i64) == Legal) {
2488 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
2489 zextOrTrunc(64), MVT::i64);
2490 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2491 SVOffset, isVolatile, Alignment);
2493 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
2494 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2495 // stores. If the target supports neither 32- nor 64-bits, this
2496 // xform is certainly not worth it.
2497 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
2498 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2499 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
2500 if (TLI.isBigEndian()) std::swap(Lo, Hi);
2502 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2503 SVOffset, isVolatile, Alignment);
2504 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2505 DAG.getIntPtrConstant(4));
2506 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
2507 isVolatile, MinAlign(Alignment, 4U));
2509 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2515 switch (getTypeAction(ST->getMemoryVT())) {
2517 Tmp3 = LegalizeOp(ST->getValue());
2518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2521 MVT VT = Tmp3.getValueType();
2522 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2523 default: assert(0 && "This action is not supported yet!");
2524 case TargetLowering::Legal:
2525 // If this is an unaligned store and the target doesn't support it,
2527 if (!TLI.allowsUnalignedMemoryAccesses()) {
2528 unsigned ABIAlignment = TLI.getTargetData()->
2529 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
2530 if (ST->getAlignment() < ABIAlignment)
2531 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
2535 case TargetLowering::Custom:
2536 Tmp1 = TLI.LowerOperation(Result, DAG);
2537 if (Tmp1.getNode()) Result = Tmp1;
2539 case TargetLowering::Promote:
2540 assert(VT.isVector() && "Unknown legal promote case!");
2541 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2542 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2543 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2544 ST->getSrcValue(), SVOffset, isVolatile,
2551 if (!ST->getMemoryVT().isVector()) {
2552 // Truncate the value and store the result.
2553 Tmp3 = PromoteOp(ST->getValue());
2554 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2555 SVOffset, ST->getMemoryVT(),
2556 isVolatile, Alignment);
2559 // Fall thru to expand for vector
2561 unsigned IncrementSize = 0;
2564 // If this is a vector type, then we have to calculate the increment as
2565 // the product of the element size in bytes, and the number of elements
2566 // in the high half of the vector.
2567 if (ST->getValue().getValueType().isVector()) {
2568 SDNode *InVal = ST->getValue().getNode();
2569 int InIx = ST->getValue().getResNo();
2570 MVT InVT = InVal->getValueType(InIx);
2571 unsigned NumElems = InVT.getVectorNumElements();
2572 MVT EVT = InVT.getVectorElementType();
2574 // Figure out if there is a simple type corresponding to this Vector
2575 // type. If so, convert to the vector type.
2576 MVT TVT = MVT::getVectorVT(EVT, NumElems);
2577 if (TLI.isTypeLegal(TVT)) {
2578 // Turn this into a normal store of the vector type.
2579 Tmp3 = LegalizeOp(ST->getValue());
2580 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2581 SVOffset, isVolatile, Alignment);
2582 Result = LegalizeOp(Result);
2584 } else if (NumElems == 1) {
2585 // Turn this into a normal store of the scalar type.
2586 Tmp3 = ScalarizeVectorOp(ST->getValue());
2587 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2588 SVOffset, isVolatile, Alignment);
2589 // The scalarized value type may not be legal, e.g. it might require
2590 // promotion or expansion. Relegalize the scalar store.
2591 Result = LegalizeOp(Result);
2594 // Check if we have widen this node with another value
2595 std::map<SDValue, SDValue>::iterator I =
2596 WidenNodes.find(ST->getValue());
2597 if (I != WidenNodes.end()) {
2598 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2602 SplitVectorOp(ST->getValue(), Lo, Hi);
2603 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2604 EVT.getSizeInBits()/8;
2608 ExpandOp(ST->getValue(), Lo, Hi);
2609 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
2611 if (Hi.getNode() && TLI.isBigEndian())
2615 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2616 SVOffset, isVolatile, Alignment);
2618 if (Hi.getNode() == NULL) {
2619 // Must be int <-> float one-to-one expansion.
2624 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2625 DAG.getIntPtrConstant(IncrementSize));
2626 assert(isTypeLegal(Tmp2.getValueType()) &&
2627 "Pointers must be legal!");
2628 SVOffset += IncrementSize;
2629 Alignment = MinAlign(Alignment, IncrementSize);
2630 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2631 SVOffset, isVolatile, Alignment);
2632 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2637 switch (getTypeAction(ST->getValue().getValueType())) {
2639 Tmp3 = LegalizeOp(ST->getValue());
2642 if (!ST->getValue().getValueType().isVector()) {
2643 // We can promote the value, the truncstore will still take care of it.
2644 Tmp3 = PromoteOp(ST->getValue());
2647 // Vector case falls through to expand
2649 // Just store the low part. This may become a non-trunc store, so make
2650 // sure to use getTruncStore, not UpdateNodeOperands below.
2651 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2652 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2653 SVOffset, MVT::i8, isVolatile, Alignment);
2656 MVT StVT = ST->getMemoryVT();
2657 unsigned StWidth = StVT.getSizeInBits();
2659 if (StWidth != StVT.getStoreSizeInBits()) {
2660 // Promote to a byte-sized store with upper bits zero if not
2661 // storing an integral number of bytes. For example, promote
2662 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2663 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
2664 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2665 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2666 SVOffset, NVT, isVolatile, Alignment);
2667 } else if (StWidth & (StWidth - 1)) {
2668 // If not storing a power-of-2 number of bits, expand as two stores.
2669 assert(StVT.isExtended() && !StVT.isVector() &&
2670 "Unsupported truncstore!");
2671 unsigned RoundWidth = 1 << Log2_32(StWidth);
2672 assert(RoundWidth < StWidth);
2673 unsigned ExtraWidth = StWidth - RoundWidth;
2674 assert(ExtraWidth < RoundWidth);
2675 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2676 "Store size not an integral number of bytes!");
2677 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2678 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
2680 unsigned IncrementSize;
2682 if (TLI.isLittleEndian()) {
2683 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2684 // Store the bottom RoundWidth bits.
2685 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2687 isVolatile, Alignment);
2689 // Store the remaining ExtraWidth bits.
2690 IncrementSize = RoundWidth / 8;
2691 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2692 DAG.getIntPtrConstant(IncrementSize));
2693 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2694 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2695 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2696 SVOffset + IncrementSize, ExtraVT, isVolatile,
2697 MinAlign(Alignment, IncrementSize));
2699 // Big endian - avoid unaligned stores.
2700 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2701 // Store the top RoundWidth bits.
2702 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2703 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2704 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2705 RoundVT, isVolatile, Alignment);
2707 // Store the remaining ExtraWidth bits.
2708 IncrementSize = RoundWidth / 8;
2709 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2710 DAG.getIntPtrConstant(IncrementSize));
2711 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2712 SVOffset + IncrementSize, ExtraVT, isVolatile,
2713 MinAlign(Alignment, IncrementSize));
2716 // The order of the stores doesn't matter.
2717 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2719 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2720 Tmp2 != ST->getBasePtr())
2721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2724 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2725 default: assert(0 && "This action is not supported yet!");
2726 case TargetLowering::Legal:
2727 // If this is an unaligned store and the target doesn't support it,
2729 if (!TLI.allowsUnalignedMemoryAccesses()) {
2730 unsigned ABIAlignment = TLI.getTargetData()->
2731 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
2732 if (ST->getAlignment() < ABIAlignment)
2733 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
2737 case TargetLowering::Custom:
2738 Result = TLI.LowerOperation(Result, DAG);
2741 // TRUNCSTORE:i16 i32 -> STORE i16
2742 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2743 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2744 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2745 isVolatile, Alignment);
2753 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2756 case ISD::STACKSAVE:
2757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2758 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2759 Tmp1 = Result.getValue(0);
2760 Tmp2 = Result.getValue(1);
2762 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2763 default: assert(0 && "This action is not supported yet!");
2764 case TargetLowering::Legal: break;
2765 case TargetLowering::Custom:
2766 Tmp3 = TLI.LowerOperation(Result, DAG);
2767 if (Tmp3.getNode()) {
2768 Tmp1 = LegalizeOp(Tmp3);
2769 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2772 case TargetLowering::Expand:
2773 // Expand to CopyFromReg if the target set
2774 // StackPointerRegisterToSaveRestore.
2775 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2776 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2777 Node->getValueType(0));
2778 Tmp2 = Tmp1.getValue(1);
2780 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2781 Tmp2 = Node->getOperand(0);
2786 // Since stacksave produce two values, make sure to remember that we
2787 // legalized both of them.
2788 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2789 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
2790 return Op.getResNo() ? Tmp2 : Tmp1;
2792 case ISD::STACKRESTORE:
2793 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2797 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2798 default: assert(0 && "This action is not supported yet!");
2799 case TargetLowering::Legal: break;
2800 case TargetLowering::Custom:
2801 Tmp1 = TLI.LowerOperation(Result, DAG);
2802 if (Tmp1.getNode()) Result = Tmp1;
2804 case TargetLowering::Expand:
2805 // Expand to CopyToReg if the target set
2806 // StackPointerRegisterToSaveRestore.
2807 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2808 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2816 case ISD::READCYCLECOUNTER:
2817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2818 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2819 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2820 Node->getValueType(0))) {
2821 default: assert(0 && "This action is not supported yet!");
2822 case TargetLowering::Legal:
2823 Tmp1 = Result.getValue(0);
2824 Tmp2 = Result.getValue(1);
2826 case TargetLowering::Custom:
2827 Result = TLI.LowerOperation(Result, DAG);
2828 Tmp1 = LegalizeOp(Result.getValue(0));
2829 Tmp2 = LegalizeOp(Result.getValue(1));
2833 // Since rdcc produce two values, make sure to remember that we legalized
2835 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2836 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
2840 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2841 case Expand: assert(0 && "It's impossible to expand bools");
2843 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2846 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
2847 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2848 // Make sure the condition is either zero or one.
2849 unsigned BitWidth = Tmp1.getValueSizeInBits();
2850 if (!DAG.MaskedValueIsZero(Tmp1,
2851 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
2852 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2856 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2857 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2861 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2862 default: assert(0 && "This action is not supported yet!");
2863 case TargetLowering::Legal: break;
2864 case TargetLowering::Custom: {
2865 Tmp1 = TLI.LowerOperation(Result, DAG);
2866 if (Tmp1.getNode()) Result = Tmp1;
2869 case TargetLowering::Expand:
2870 if (Tmp1.getOpcode() == ISD::SETCC) {
2871 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2873 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2875 Result = DAG.getSelectCC(Tmp1,
2876 DAG.getConstant(0, Tmp1.getValueType()),
2877 Tmp2, Tmp3, ISD::SETNE);
2880 case TargetLowering::Promote: {
2882 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2883 unsigned ExtOp, TruncOp;
2884 if (Tmp2.getValueType().isVector()) {
2885 ExtOp = ISD::BIT_CONVERT;
2886 TruncOp = ISD::BIT_CONVERT;
2887 } else if (Tmp2.getValueType().isInteger()) {
2888 ExtOp = ISD::ANY_EXTEND;
2889 TruncOp = ISD::TRUNCATE;
2891 ExtOp = ISD::FP_EXTEND;
2892 TruncOp = ISD::FP_ROUND;
2894 // Promote each of the values to the new type.
2895 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2896 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2897 // Perform the larger operation, then round down.
2898 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2899 if (TruncOp != ISD::FP_ROUND)
2900 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2902 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2903 DAG.getIntPtrConstant(0));
2908 case ISD::SELECT_CC: {
2909 Tmp1 = Node->getOperand(0); // LHS
2910 Tmp2 = Node->getOperand(1); // RHS
2911 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2912 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2913 SDValue CC = Node->getOperand(4);
2915 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
2917 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
2918 // the LHS is a legal SETCC itself. In this case, we need to compare
2919 // the result against zero to select between true and false values.
2920 if (Tmp2.getNode() == 0) {
2921 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2922 CC = DAG.getCondCode(ISD::SETNE);
2924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2926 // Everything is legal, see if we should expand this op or something.
2927 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2928 default: assert(0 && "This action is not supported yet!");
2929 case TargetLowering::Legal: break;
2930 case TargetLowering::Custom:
2931 Tmp1 = TLI.LowerOperation(Result, DAG);
2932 if (Tmp1.getNode()) Result = Tmp1;
2938 Tmp1 = Node->getOperand(0);
2939 Tmp2 = Node->getOperand(1);
2940 Tmp3 = Node->getOperand(2);
2941 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
2943 // If we had to Expand the SetCC operands into a SELECT node, then it may
2944 // not always be possible to return a true LHS & RHS. In this case, just
2945 // return the value we legalized, returned in the LHS
2946 if (Tmp2.getNode() == 0) {
2951 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2952 default: assert(0 && "Cannot handle this action for SETCC yet!");
2953 case TargetLowering::Custom:
2956 case TargetLowering::Legal:
2957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2959 Tmp4 = TLI.LowerOperation(Result, DAG);
2960 if (Tmp4.getNode()) Result = Tmp4;
2963 case TargetLowering::Promote: {
2964 // First step, figure out the appropriate operation to use.
2965 // Allow SETCC to not be supported for all legal data types
2966 // Mostly this targets FP
2967 MVT NewInTy = Node->getOperand(0).getValueType();
2968 MVT OldVT = NewInTy; OldVT = OldVT;
2970 // Scan for the appropriate larger type to use.
2972 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
2974 assert(NewInTy.isInteger() == OldVT.isInteger() &&
2975 "Fell off of the edge of the integer world");
2976 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
2977 "Fell off of the edge of the floating point world");
2979 // If the target supports SETCC of this type, use it.
2980 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2983 if (NewInTy.isInteger())
2984 assert(0 && "Cannot promote Legal Integer SETCC yet");
2986 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2987 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2989 Tmp1 = LegalizeOp(Tmp1);
2990 Tmp2 = LegalizeOp(Tmp2);
2991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2992 Result = LegalizeOp(Result);
2995 case TargetLowering::Expand:
2996 // Expand a setcc node into a select_cc of the same condition, lhs, and
2997 // rhs that selects between const 1 (true) and const 0 (false).
2998 MVT VT = Node->getValueType(0);
2999 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3000 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3006 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3007 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3008 SDValue CC = Node->getOperand(2);
3010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3012 // Everything is legal, see if we should expand this op or something.
3013 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3014 default: assert(0 && "This action is not supported yet!");
3015 case TargetLowering::Legal: break;
3016 case TargetLowering::Custom:
3017 Tmp1 = TLI.LowerOperation(Result, DAG);
3018 if (Tmp1.getNode()) Result = Tmp1;
3024 case ISD::SHL_PARTS:
3025 case ISD::SRA_PARTS:
3026 case ISD::SRL_PARTS: {
3027 SmallVector<SDValue, 8> Ops;
3028 bool Changed = false;
3029 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3030 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3031 Changed |= Ops.back() != Node->getOperand(i);
3034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3036 switch (TLI.getOperationAction(Node->getOpcode(),
3037 Node->getValueType(0))) {
3038 default: assert(0 && "This action is not supported yet!");
3039 case TargetLowering::Legal: break;
3040 case TargetLowering::Custom:
3041 Tmp1 = TLI.LowerOperation(Result, DAG);
3042 if (Tmp1.getNode()) {
3043 SDValue Tmp2, RetVal(0, 0);
3044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3045 Tmp2 = LegalizeOp(Tmp1.getValue(i));
3046 AddLegalizedOperand(SDValue(Node, i), Tmp2);
3047 if (i == Op.getResNo())
3050 assert(RetVal.getNode() && "Illegal result number");
3056 // Since these produce multiple values, make sure to remember that we
3057 // legalized all of them.
3058 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3059 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
3060 return Result.getValue(Op.getResNo());
3082 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3083 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3084 case Expand: assert(0 && "Not possible");
3086 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3089 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3093 if ((Node->getOpcode() == ISD::SHL ||
3094 Node->getOpcode() == ISD::SRL ||
3095 Node->getOpcode() == ISD::SRA) &&
3096 !Node->getValueType(0).isVector()) {
3097 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3098 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3099 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3100 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3105 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3106 default: assert(0 && "BinOp legalize operation not supported");
3107 case TargetLowering::Legal: break;
3108 case TargetLowering::Custom:
3109 Tmp1 = TLI.LowerOperation(Result, DAG);
3110 if (Tmp1.getNode()) {
3114 // Fall through if the custom lower can't deal with the operation
3115 case TargetLowering::Expand: {
3116 MVT VT = Op.getValueType();
3118 // See if multiply or divide can be lowered using two-result operations.
3119 SDVTList VTs = DAG.getVTList(VT, VT);
3120 if (Node->getOpcode() == ISD::MUL) {
3121 // We just need the low half of the multiply; try both the signed
3122 // and unsigned forms. If the target supports both SMUL_LOHI and
3123 // UMUL_LOHI, form a preference by checking which forms of plain
3124 // MULH it supports.
3125 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3126 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3127 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3128 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3129 unsigned OpToUse = 0;
3130 if (HasSMUL_LOHI && !HasMULHS) {
3131 OpToUse = ISD::SMUL_LOHI;
3132 } else if (HasUMUL_LOHI && !HasMULHU) {
3133 OpToUse = ISD::UMUL_LOHI;
3134 } else if (HasSMUL_LOHI) {
3135 OpToUse = ISD::SMUL_LOHI;
3136 } else if (HasUMUL_LOHI) {
3137 OpToUse = ISD::UMUL_LOHI;
3140 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
3144 if (Node->getOpcode() == ISD::MULHS &&
3145 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3146 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3150 if (Node->getOpcode() == ISD::MULHU &&
3151 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3152 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3156 if (Node->getOpcode() == ISD::SDIV &&
3157 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3158 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3162 if (Node->getOpcode() == ISD::UDIV &&
3163 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3164 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3169 // Check to see if we have a libcall for this operator.
3170 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3171 bool isSigned = false;
3172 switch (Node->getOpcode()) {
3175 if (VT == MVT::i32) {
3176 LC = Node->getOpcode() == ISD::UDIV
3177 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
3178 isSigned = Node->getOpcode() == ISD::SDIV;
3183 LC = RTLIB::MUL_I32;
3186 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3187 RTLIB::POW_PPCF128);
3191 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3193 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
3197 assert(Node->getValueType(0).isVector() &&
3198 "Cannot expand this binary operator!");
3199 // Expand the operation into a bunch of nasty scalar code.
3200 Result = LegalizeOp(UnrollVectorOp(Op));
3203 case TargetLowering::Promote: {
3204 switch (Node->getOpcode()) {
3205 default: assert(0 && "Do not know how to promote this BinOp!");
3209 MVT OVT = Node->getValueType(0);
3210 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3211 assert(OVT.isVector() && "Cannot promote this BinOp!");
3212 // Bit convert each of the values to the new type.
3213 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3214 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3216 // Bit convert the result back the original type.
3217 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3225 case ISD::SMUL_LOHI:
3226 case ISD::UMUL_LOHI:
3229 // These nodes will only be produced by target-specific lowering, so
3230 // they shouldn't be here if they aren't legal.
3231 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
3232 "This must be legal!");
3234 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3235 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3239 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3240 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3241 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3242 case Expand: assert(0 && "Not possible");
3244 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3247 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3253 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3254 default: assert(0 && "Operation not supported");
3255 case TargetLowering::Custom:
3256 Tmp1 = TLI.LowerOperation(Result, DAG);
3257 if (Tmp1.getNode()) Result = Tmp1;
3259 case TargetLowering::Legal: break;
3260 case TargetLowering::Expand: {
3261 // If this target supports fabs/fneg natively and select is cheap,
3262 // do this efficiently.
3263 if (!TLI.isSelectExpensive() &&
3264 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3265 TargetLowering::Legal &&
3266 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3267 TargetLowering::Legal) {
3268 // Get the sign bit of the RHS.
3270 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3271 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3272 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
3273 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3274 // Get the absolute value of the result.
3275 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3276 // Select between the nabs and abs value based on the sign bit of
3278 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3279 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3282 Result = LegalizeOp(Result);
3286 // Otherwise, do bitwise ops!
3288 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3289 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3290 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3291 Result = LegalizeOp(Result);
3299 Tmp1 = LegalizeOp(Node->getOperand(0));
3300 Tmp2 = LegalizeOp(Node->getOperand(1));
3301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3302 // Since this produces two values, make sure to remember that we legalized
3304 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3305 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
3310 Tmp1 = LegalizeOp(Node->getOperand(0));
3311 Tmp2 = LegalizeOp(Node->getOperand(1));
3312 Tmp3 = LegalizeOp(Node->getOperand(2));
3313 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3314 // Since this produces two values, make sure to remember that we legalized
3316 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3317 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
3320 case ISD::BUILD_PAIR: {
3321 MVT PairTy = Node->getValueType(0);
3322 // TODO: handle the case where the Lo and Hi operands are not of legal type
3323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3325 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3326 case TargetLowering::Promote:
3327 case TargetLowering::Custom:
3328 assert(0 && "Cannot promote/custom this yet!");
3329 case TargetLowering::Legal:
3330 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3331 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3333 case TargetLowering::Expand:
3334 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3335 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3336 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3337 DAG.getConstant(PairTy.getSizeInBits()/2,
3338 TLI.getShiftAmountTy()));
3339 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3348 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3349 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3351 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3352 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3353 case TargetLowering::Custom:
3356 case TargetLowering::Legal:
3357 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3359 Tmp1 = TLI.LowerOperation(Result, DAG);
3360 if (Tmp1.getNode()) Result = Tmp1;
3363 case TargetLowering::Expand: {
3364 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3365 bool isSigned = DivOpc == ISD::SDIV;
3366 MVT VT = Node->getValueType(0);
3368 // See if remainder can be lowered using two-result operations.
3369 SDVTList VTs = DAG.getVTList(VT, VT);
3370 if (Node->getOpcode() == ISD::SREM &&
3371 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3372 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
3375 if (Node->getOpcode() == ISD::UREM &&
3376 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3377 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
3381 if (VT.isInteger()) {
3382 if (TLI.getOperationAction(DivOpc, VT) ==
3383 TargetLowering::Legal) {
3385 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3386 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3387 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
3388 } else if (VT.isVector()) {
3389 Result = LegalizeOp(UnrollVectorOp(Op));
3391 assert(VT == MVT::i32 &&
3392 "Cannot expand this binary operator!");
3393 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3394 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3396 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
3399 assert(VT.isFloatingPoint() &&
3400 "remainder op must have integer or floating-point type");
3401 if (VT.isVector()) {
3402 Result = LegalizeOp(UnrollVectorOp(Op));
3404 // Floating point mod -> fmod libcall.
3405 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3406 RTLIB::REM_F80, RTLIB::REM_PPCF128);
3408 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
3416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3417 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3419 MVT VT = Node->getValueType(0);
3420 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3421 default: assert(0 && "This action is not supported yet!");
3422 case TargetLowering::Custom:
3425 case TargetLowering::Legal:
3426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3427 Result = Result.getValue(0);
3428 Tmp1 = Result.getValue(1);
3431 Tmp2 = TLI.LowerOperation(Result, DAG);
3432 if (Tmp2.getNode()) {
3433 Result = LegalizeOp(Tmp2);
3434 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3438 case TargetLowering::Expand: {
3439 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3440 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
3441 // Increment the pointer, VAList, to the next vaarg
3442 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3443 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3444 TLI.getPointerTy()));
3445 // Store the incremented VAList to the legalized pointer
3446 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
3447 // Load the actual argument out of the pointer VAList
3448 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3449 Tmp1 = LegalizeOp(Result.getValue(1));
3450 Result = LegalizeOp(Result);
3454 // Since VAARG produces two values, make sure to remember that we
3455 // legalized both of them.
3456 AddLegalizedOperand(SDValue(Node, 0), Result);
3457 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
3458 return Op.getResNo() ? Tmp1 : Result;
3462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3463 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3464 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3466 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3467 default: assert(0 && "This action is not supported yet!");
3468 case TargetLowering::Custom:
3471 case TargetLowering::Legal:
3472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3473 Node->getOperand(3), Node->getOperand(4));
3475 Tmp1 = TLI.LowerOperation(Result, DAG);
3476 if (Tmp1.getNode()) Result = Tmp1;
3479 case TargetLowering::Expand:
3480 // This defaults to loading a pointer from the input and storing it to the
3481 // output, returning the chain.
3482 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3483 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3484 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3485 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
3491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3492 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3494 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3495 default: assert(0 && "This action is not supported yet!");
3496 case TargetLowering::Custom:
3499 case TargetLowering::Legal:
3500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3502 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3503 if (Tmp1.getNode()) Result = Tmp1;
3506 case TargetLowering::Expand:
3507 Result = Tmp1; // Default to a no-op, return the chain
3513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3514 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3518 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3519 default: assert(0 && "This action is not supported yet!");
3520 case TargetLowering::Legal: break;
3521 case TargetLowering::Custom:
3522 Tmp1 = TLI.LowerOperation(Result, DAG);
3523 if (Tmp1.getNode()) Result = Tmp1;
3530 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3531 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3533 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3535 assert(0 && "ROTL/ROTR legalize operation not supported");
3537 case TargetLowering::Legal:
3539 case TargetLowering::Custom:
3540 Tmp1 = TLI.LowerOperation(Result, DAG);
3541 if (Tmp1.getNode()) Result = Tmp1;
3543 case TargetLowering::Promote:
3544 assert(0 && "Do not know how to promote ROTL/ROTR");
3546 case TargetLowering::Expand:
3547 assert(0 && "Do not know how to expand ROTL/ROTR");
3553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3554 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3555 case TargetLowering::Custom:
3556 assert(0 && "Cannot custom legalize this yet!");
3557 case TargetLowering::Legal:
3558 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3560 case TargetLowering::Promote: {
3561 MVT OVT = Tmp1.getValueType();
3562 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3563 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3565 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3566 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3567 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3568 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3571 case TargetLowering::Expand:
3572 Result = ExpandBSWAP(Tmp1);
3580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3581 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3582 case TargetLowering::Custom:
3583 case TargetLowering::Legal:
3584 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3585 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3586 TargetLowering::Custom) {
3587 Tmp1 = TLI.LowerOperation(Result, DAG);
3588 if (Tmp1.getNode()) {
3593 case TargetLowering::Promote: {
3594 MVT OVT = Tmp1.getValueType();
3595 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3597 // Zero extend the argument.
3598 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3599 // Perform the larger operation, then subtract if needed.
3600 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3601 switch (Node->getOpcode()) {
3606 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3607 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
3608 DAG.getConstant(NVT.getSizeInBits(), NVT),
3610 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
3611 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3614 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3615 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3616 DAG.getConstant(NVT.getSizeInBits() -
3617 OVT.getSizeInBits(), NVT));
3622 case TargetLowering::Expand:
3623 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3643 case ISD::FNEARBYINT:
3644 Tmp1 = LegalizeOp(Node->getOperand(0));
3645 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3646 case TargetLowering::Promote:
3647 case TargetLowering::Custom:
3650 case TargetLowering::Legal:
3651 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3653 Tmp1 = TLI.LowerOperation(Result, DAG);
3654 if (Tmp1.getNode()) Result = Tmp1;
3657 case TargetLowering::Expand:
3658 switch (Node->getOpcode()) {
3659 default: assert(0 && "Unreachable!");
3661 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3662 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3663 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3666 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3667 MVT VT = Node->getValueType(0);
3668 Tmp2 = DAG.getConstantFP(0.0, VT);
3669 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
3671 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3672 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3687 case ISD::FNEARBYINT: {
3688 MVT VT = Node->getValueType(0);
3690 // Expand unsupported unary vector operators by unrolling them.
3691 if (VT.isVector()) {
3692 Result = LegalizeOp(UnrollVectorOp(Op));
3696 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3697 switch(Node->getOpcode()) {
3699 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3700 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
3703 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3704 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
3707 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3708 RTLIB::COS_F80, RTLIB::COS_PPCF128);
3711 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3712 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3715 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3716 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3719 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3720 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3723 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3724 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3727 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3728 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3731 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3732 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3735 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3736 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3739 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3740 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3743 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3744 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3746 case ISD::FNEARBYINT:
3747 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3748 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3751 default: assert(0 && "Unreachable!");
3754 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
3762 MVT VT = Node->getValueType(0);
3764 // Expand unsupported unary vector operators by unrolling them.
3765 if (VT.isVector()) {
3766 Result = LegalizeOp(UnrollVectorOp(Op));
3770 // We always lower FPOWI into a libcall. No target support for it yet.
3771 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3772 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
3774 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
3777 case ISD::BIT_CONVERT:
3778 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
3779 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3780 Node->getValueType(0));
3781 } else if (Op.getOperand(0).getValueType().isVector()) {
3782 // The input has to be a vector type, we have to either scalarize it, pack
3783 // it, or convert it based on whether the input vector type is legal.
3784 SDNode *InVal = Node->getOperand(0).getNode();
3785 int InIx = Node->getOperand(0).getResNo();
3786 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3787 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
3789 // Figure out if there is a simple type corresponding to this Vector
3790 // type. If so, convert to the vector type.
3791 MVT TVT = MVT::getVectorVT(EVT, NumElems);
3792 if (TLI.isTypeLegal(TVT)) {
3793 // Turn this into a bit convert of the vector input.
3794 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3795 LegalizeOp(Node->getOperand(0)));
3797 } else if (NumElems == 1) {
3798 // Turn this into a bit convert of the scalar input.
3799 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3800 ScalarizeVectorOp(Node->getOperand(0)));
3803 // FIXME: UNIMP! Store then reload
3804 assert(0 && "Cast from unsupported vector type not implemented yet!");
3807 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3808 Node->getOperand(0).getValueType())) {
3809 default: assert(0 && "Unknown operation action!");
3810 case TargetLowering::Expand:
3811 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3812 Node->getValueType(0));
3814 case TargetLowering::Legal:
3815 Tmp1 = LegalizeOp(Node->getOperand(0));
3816 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3821 case ISD::CONVERT_RNDSAT: {
3822 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3824 default: assert(0 && "Unknown cvt code!");
3835 SDValue DTyOp = Node->getOperand(1);
3836 SDValue STyOp = Node->getOperand(2);
3837 SDValue RndOp = Node->getOperand(3);
3838 SDValue SatOp = Node->getOperand(4);
3839 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3840 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3842 Tmp1 = LegalizeOp(Node->getOperand(0));
3843 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3845 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3846 TargetLowering::Custom) {
3847 Tmp1 = TLI.LowerOperation(Result, DAG);
3848 if (Tmp1.getNode()) Result = Tmp1;
3852 Result = PromoteOp(Node->getOperand(0));
3853 // For FP, make Op1 a i32
3855 Result = DAG.getConvertRndSat(Result.getValueType(), Result,
3856 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3861 } // end switch CvtCode
3864 // Conversion operators. The source and destination have different types.
3865 case ISD::SINT_TO_FP:
3866 case ISD::UINT_TO_FP: {
3867 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3868 Result = LegalizeINT_TO_FP(Result, isSigned,
3869 Node->getValueType(0), Node->getOperand(0));
3873 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3875 Tmp1 = LegalizeOp(Node->getOperand(0));
3876 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3879 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3881 // Since the result is legal, we should just be able to truncate the low
3882 // part of the source.
3883 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3886 Result = PromoteOp(Node->getOperand(0));
3887 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3892 case ISD::FP_TO_SINT:
3893 case ISD::FP_TO_UINT:
3894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3896 Tmp1 = LegalizeOp(Node->getOperand(0));
3898 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3899 default: assert(0 && "Unknown operation action!");
3900 case TargetLowering::Custom:
3903 case TargetLowering::Legal:
3904 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3906 Tmp1 = TLI.LowerOperation(Result, DAG);
3907 if (Tmp1.getNode()) Result = Tmp1;
3910 case TargetLowering::Promote:
3911 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3912 Node->getOpcode() == ISD::FP_TO_SINT);
3914 case TargetLowering::Expand:
3915 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3916 SDValue True, False;
3917 MVT VT = Node->getOperand(0).getValueType();
3918 MVT NVT = Node->getValueType(0);
3919 const uint64_t zero[] = {0, 0};
3920 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3921 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3922 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3923 Tmp2 = DAG.getConstantFP(apf, VT);
3924 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
3925 Node->getOperand(0), Tmp2, ISD::SETLT);
3926 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3927 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3928 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3930 False = DAG.getNode(ISD::XOR, NVT, False,
3931 DAG.getConstant(x, NVT));
3932 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3935 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3941 MVT VT = Op.getValueType();
3942 MVT OVT = Node->getOperand(0).getValueType();
3943 // Convert ppcf128 to i32
3944 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
3945 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3946 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3947 Node->getOperand(0), DAG.getValueType(MVT::f64));
3948 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3949 DAG.getIntPtrConstant(1));
3950 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3952 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3953 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3954 Tmp2 = DAG.getConstantFP(apf, OVT);
3955 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3956 // FIXME: generated code sucks.
3957 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3958 DAG.getNode(ISD::ADD, MVT::i32,
3959 DAG.getNode(ISD::FP_TO_SINT, VT,
3960 DAG.getNode(ISD::FSUB, OVT,
3961 Node->getOperand(0), Tmp2)),
3962 DAG.getConstant(0x80000000, MVT::i32)),
3963 DAG.getNode(ISD::FP_TO_SINT, VT,
3964 Node->getOperand(0)),
3965 DAG.getCondCode(ISD::SETGE));
3969 // Convert f32 / f64 to i32 / i64 / i128.
3970 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3971 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3972 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
3974 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
3978 Tmp1 = PromoteOp(Node->getOperand(0));
3979 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3980 Result = LegalizeOp(Result);
3985 case ISD::FP_EXTEND: {
3986 MVT DstVT = Op.getValueType();
3987 MVT SrcVT = Op.getOperand(0).getValueType();
3988 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3989 // The only other way we can lower this is to turn it into a STORE,
3990 // LOAD pair, targetting a temporary location (a stack slot).
3991 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3994 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3995 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3997 Tmp1 = LegalizeOp(Node->getOperand(0));
3998 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4001 Tmp1 = PromoteOp(Node->getOperand(0));
4002 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4007 case ISD::FP_ROUND: {
4008 MVT DstVT = Op.getValueType();
4009 MVT SrcVT = Op.getOperand(0).getValueType();
4010 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4011 if (SrcVT == MVT::ppcf128) {
4013 ExpandOp(Node->getOperand(0), Lo, Result);
4014 // Round it the rest of the way (e.g. to f32) if needed.
4015 if (DstVT!=MVT::f64)
4016 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
4019 // The only other way we can lower this is to turn it into a STORE,
4020 // LOAD pair, targetting a temporary location (a stack slot).
4021 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4025 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4027 Tmp1 = LegalizeOp(Node->getOperand(0));
4028 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4031 Tmp1 = PromoteOp(Node->getOperand(0));
4032 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4033 Node->getOperand(1));
4038 case ISD::ANY_EXTEND:
4039 case ISD::ZERO_EXTEND:
4040 case ISD::SIGN_EXTEND:
4041 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4042 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4044 Tmp1 = LegalizeOp(Node->getOperand(0));
4045 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4046 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4047 TargetLowering::Custom) {
4048 Tmp1 = TLI.LowerOperation(Result, DAG);
4049 if (Tmp1.getNode()) Result = Tmp1;
4053 switch (Node->getOpcode()) {
4054 case ISD::ANY_EXTEND:
4055 Tmp1 = PromoteOp(Node->getOperand(0));
4056 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4058 case ISD::ZERO_EXTEND:
4059 Result = PromoteOp(Node->getOperand(0));
4060 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4061 Result = DAG.getZeroExtendInReg(Result,
4062 Node->getOperand(0).getValueType());
4064 case ISD::SIGN_EXTEND:
4065 Result = PromoteOp(Node->getOperand(0));
4066 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4067 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4069 DAG.getValueType(Node->getOperand(0).getValueType()));
4074 case ISD::FP_ROUND_INREG:
4075 case ISD::SIGN_EXTEND_INREG: {
4076 Tmp1 = LegalizeOp(Node->getOperand(0));
4077 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
4079 // If this operation is not supported, convert it to a shl/shr or load/store
4081 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4082 default: assert(0 && "This action not supported for this op yet!");
4083 case TargetLowering::Legal:
4084 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4086 case TargetLowering::Expand:
4087 // If this is an integer extend and shifts are supported, do that.
4088 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4089 // NOTE: we could fall back on load/store here too for targets without
4090 // SAR. However, it is doubtful that any exist.
4091 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4092 ExtraVT.getSizeInBits();
4093 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
4094 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4095 Node->getOperand(0), ShiftCst);
4096 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4098 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4099 // The only way we can lower this is to turn it into a TRUNCSTORE,
4100 // EXTLOAD pair, targetting a temporary location (a stack slot).
4102 // NOTE: there is a choice here between constantly creating new stack
4103 // slots and always reusing the same one. We currently always create
4104 // new ones, as reuse may inhibit scheduling.
4105 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4106 Node->getValueType(0));
4108 assert(0 && "Unknown op");
4114 case ISD::TRAMPOLINE: {
4116 for (unsigned i = 0; i != 6; ++i)
4117 Ops[i] = LegalizeOp(Node->getOperand(i));
4118 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4119 // The only option for this node is to custom lower it.
4120 Result = TLI.LowerOperation(Result, DAG);
4121 assert(Result.getNode() && "Should always custom lower!");
4123 // Since trampoline produces two values, make sure to remember that we
4124 // legalized both of them.
4125 Tmp1 = LegalizeOp(Result.getValue(1));
4126 Result = LegalizeOp(Result);
4127 AddLegalizedOperand(SDValue(Node, 0), Result);
4128 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
4129 return Op.getResNo() ? Tmp1 : Result;
4131 case ISD::FLT_ROUNDS_: {
4132 MVT VT = Node->getValueType(0);
4133 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4134 default: assert(0 && "This action not supported for this op yet!");
4135 case TargetLowering::Custom:
4136 Result = TLI.LowerOperation(Op, DAG);
4137 if (Result.getNode()) break;
4139 case TargetLowering::Legal:
4140 // If this operation is not supported, lower it to constant 1
4141 Result = DAG.getConstant(1, VT);
4147 MVT VT = Node->getValueType(0);
4148 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4149 default: assert(0 && "This action not supported for this op yet!");
4150 case TargetLowering::Legal:
4151 Tmp1 = LegalizeOp(Node->getOperand(0));
4152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4154 case TargetLowering::Custom:
4155 Result = TLI.LowerOperation(Op, DAG);
4156 if (Result.getNode()) break;
4158 case TargetLowering::Expand:
4159 // If this operation is not supported, lower it to 'abort()' call
4160 Tmp1 = LegalizeOp(Node->getOperand(0));
4161 TargetLowering::ArgListTy Args;
4162 std::pair<SDValue,SDValue> CallResult =
4163 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4164 false, false, false, false, CallingConv::C, false,
4165 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4167 Result = CallResult.second;
4174 MVT VT = Node->getValueType(0);
4175 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4176 default: assert(0 && "This action not supported for this op yet!");
4177 case TargetLowering::Custom:
4178 Result = TLI.LowerOperation(Op, DAG);
4179 if (Result.getNode()) break;
4181 case TargetLowering::Legal: {
4182 SDValue LHS = LegalizeOp(Node->getOperand(0));
4183 SDValue RHS = LegalizeOp(Node->getOperand(1));
4185 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
4186 MVT OType = Node->getValueType(1);
4188 SDValue Zero = DAG.getConstant(0, OType);
4190 SDValue LHSPos = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4191 SDValue RHSPos = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
4192 SDValue And1 = DAG.getNode(ISD::AND, OType, LHSPos, RHSPos);
4194 And1 = DAG.getNode(ISD::AND, OType, And1,
4195 DAG.getSetCC(OType, Sum, Zero, ISD::SETLT));
4197 SDValue LHSNeg = DAG.getSetCC(OType, LHS, Zero, ISD::SETLT);
4198 SDValue RHSNeg = DAG.getSetCC(OType, RHS, Zero, ISD::SETLT);
4199 SDValue And2 = DAG.getNode(ISD::AND, OType, LHSNeg, RHSNeg);
4201 And2 = DAG.getNode(ISD::AND, OType, And2,
4202 DAG.getSetCC(OType, Sum, Zero, ISD::SETGE));
4204 SDValue Cmp = DAG.getNode(ISD::OR, OType, And1, And2);
4206 MVT ValueVTs[] = { LHS.getValueType(), OType };
4207 SDValue Ops[] = { Sum, Cmp };
4209 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4210 SDNode *RNode = Result.getNode();
4211 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4212 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4220 MVT VT = Node->getValueType(0);
4221 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4222 default: assert(0 && "This action not supported for this op yet!");
4223 case TargetLowering::Custom:
4224 Result = TLI.LowerOperation(Op, DAG);
4225 if (Result.getNode()) break;
4227 case TargetLowering::Legal: {
4228 SDValue LHS = LegalizeOp(Node->getOperand(0));
4229 SDValue RHS = LegalizeOp(Node->getOperand(1));
4231 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
4232 MVT OType = Node->getValueType(1);
4233 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS, ISD::SETULT);
4235 MVT ValueVTs[] = { LHS.getValueType(), OType };
4236 SDValue Ops[] = { Sum, Cmp };
4238 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4239 SDNode *RNode = Result.getNode();
4240 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4241 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4250 assert(Result.getValueType() == Op.getValueType() &&
4251 "Bad legalization!");
4253 // Make sure that the generated code is itself legal.
4255 Result = LegalizeOp(Result);
4257 // Note that LegalizeOp may be reentered even from single-use nodes, which
4258 // means that we always must cache transformed nodes.
4259 AddLegalizedOperand(Op, Result);
4263 /// PromoteOp - Given an operation that produces a value in an invalid type,
4264 /// promote it to compute the value into a larger type. The produced value will
4265 /// have the correct bits for the low portion of the register, but no guarantee
4266 /// is made about the top bits: it may be zero, sign-extended, or garbage.
4267 SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
4268 MVT VT = Op.getValueType();
4269 MVT NVT = TLI.getTypeToTransformTo(VT);
4270 assert(getTypeAction(VT) == Promote &&
4271 "Caller should expand or legalize operands that are not promotable!");
4272 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
4273 "Cannot promote to smaller type!");
4275 SDValue Tmp1, Tmp2, Tmp3;
4277 SDNode *Node = Op.getNode();
4279 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
4280 if (I != PromotedNodes.end()) return I->second;
4282 switch (Node->getOpcode()) {
4283 case ISD::CopyFromReg:
4284 assert(0 && "CopyFromReg must be legal!");
4287 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4289 assert(0 && "Do not know how to promote this operator!");
4292 Result = DAG.getNode(ISD::UNDEF, NVT);
4296 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4298 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4299 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4301 case ISD::ConstantFP:
4302 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4303 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4307 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
4308 && "SetCC type is not legal??");
4309 Result = DAG.getNode(ISD::SETCC,
4310 TLI.getSetCCResultType(Node->getOperand(0)),
4311 Node->getOperand(0), Node->getOperand(1),
4312 Node->getOperand(2));
4316 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4318 Result = LegalizeOp(Node->getOperand(0));
4319 assert(Result.getValueType().bitsGE(NVT) &&
4320 "This truncation doesn't make sense!");
4321 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
4322 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4325 // The truncation is not required, because we don't guarantee anything
4326 // about high bits anyway.
4327 Result = PromoteOp(Node->getOperand(0));
4330 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4331 // Truncate the low part of the expanded value to the result type
4332 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4335 case ISD::SIGN_EXTEND:
4336 case ISD::ZERO_EXTEND:
4337 case ISD::ANY_EXTEND:
4338 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4339 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4341 // Input is legal? Just do extend all the way to the larger type.
4342 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4345 // Promote the reg if it's smaller.
4346 Result = PromoteOp(Node->getOperand(0));
4347 // The high bits are not guaranteed to be anything. Insert an extend.
4348 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4349 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4350 DAG.getValueType(Node->getOperand(0).getValueType()));
4351 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4352 Result = DAG.getZeroExtendInReg(Result,
4353 Node->getOperand(0).getValueType());
4357 case ISD::CONVERT_RNDSAT: {
4358 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4359 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4360 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4361 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4362 "can only promote integers");
4363 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4364 Node->getOperand(1), Node->getOperand(2),
4365 Node->getOperand(3), Node->getOperand(4),
4370 case ISD::BIT_CONVERT:
4371 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4372 Node->getValueType(0));
4373 Result = PromoteOp(Result);
4376 case ISD::FP_EXTEND:
4377 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4380 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4381 case Promote: assert(0 && "Unreachable with 2 FP types!");
4383 if (Node->getConstantOperandVal(1) == 0) {
4384 // Input is legal? Do an FP_ROUND_INREG.
4385 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4386 DAG.getValueType(VT));
4388 // Just remove the truncate, it isn't affecting the value.
4389 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4390 Node->getOperand(1));
4395 case ISD::SINT_TO_FP:
4396 case ISD::UINT_TO_FP:
4397 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4399 // No extra round required here.
4400 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4404 Result = PromoteOp(Node->getOperand(0));
4405 if (Node->getOpcode() == ISD::SINT_TO_FP)
4406 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4408 DAG.getValueType(Node->getOperand(0).getValueType()));
4410 Result = DAG.getZeroExtendInReg(Result,
4411 Node->getOperand(0).getValueType());
4412 // No extra round required here.
4413 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4416 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4417 Node->getOperand(0));
4418 // Round if we cannot tolerate excess precision.
4419 if (NoExcessFPPrecision)
4420 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4421 DAG.getValueType(VT));
4426 case ISD::SIGN_EXTEND_INREG:
4427 Result = PromoteOp(Node->getOperand(0));
4428 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4429 Node->getOperand(1));
4431 case ISD::FP_TO_SINT:
4432 case ISD::FP_TO_UINT:
4433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4436 Tmp1 = Node->getOperand(0);
4439 // The input result is prerounded, so we don't have to do anything
4441 Tmp1 = PromoteOp(Node->getOperand(0));
4444 // If we're promoting a UINT to a larger size, check to see if the new node
4445 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4446 // we can use that instead. This allows us to generate better code for
4447 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4448 // legal, such as PowerPC.
4449 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4450 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4451 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4452 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4453 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4455 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4461 Tmp1 = PromoteOp(Node->getOperand(0));
4462 assert(Tmp1.getValueType() == NVT);
4463 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4464 // NOTE: we do not have to do any extra rounding here for
4465 // NoExcessFPPrecision, because we know the input will have the appropriate
4466 // precision, and these operations don't modify precision at all.
4481 case ISD::FNEARBYINT:
4482 Tmp1 = PromoteOp(Node->getOperand(0));
4483 assert(Tmp1.getValueType() == NVT);
4484 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4485 if (NoExcessFPPrecision)
4486 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4487 DAG.getValueType(VT));
4492 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
4493 // directly as well, which may be better.
4494 Tmp1 = PromoteOp(Node->getOperand(0));
4495 Tmp2 = Node->getOperand(1);
4496 if (Node->getOpcode() == ISD::FPOW)
4497 Tmp2 = PromoteOp(Tmp2);
4498 assert(Tmp1.getValueType() == NVT);
4499 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4500 if (NoExcessFPPrecision)
4501 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4502 DAG.getValueType(VT));
4506 case ISD::ATOMIC_CMP_SWAP_8:
4507 case ISD::ATOMIC_CMP_SWAP_16:
4508 case ISD::ATOMIC_CMP_SWAP_32:
4509 case ISD::ATOMIC_CMP_SWAP_64: {
4510 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
4511 Tmp2 = PromoteOp(Node->getOperand(2));
4512 Tmp3 = PromoteOp(Node->getOperand(3));
4513 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4514 AtomNode->getBasePtr(), Tmp2, Tmp3,
4515 AtomNode->getSrcValue(),
4516 AtomNode->getAlignment());
4517 // Remember that we legalized the chain.
4518 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4521 case ISD::ATOMIC_LOAD_ADD_8:
4522 case ISD::ATOMIC_LOAD_SUB_8:
4523 case ISD::ATOMIC_LOAD_AND_8:
4524 case ISD::ATOMIC_LOAD_OR_8:
4525 case ISD::ATOMIC_LOAD_XOR_8:
4526 case ISD::ATOMIC_LOAD_NAND_8:
4527 case ISD::ATOMIC_LOAD_MIN_8:
4528 case ISD::ATOMIC_LOAD_MAX_8:
4529 case ISD::ATOMIC_LOAD_UMIN_8:
4530 case ISD::ATOMIC_LOAD_UMAX_8:
4531 case ISD::ATOMIC_SWAP_8:
4532 case ISD::ATOMIC_LOAD_ADD_16:
4533 case ISD::ATOMIC_LOAD_SUB_16:
4534 case ISD::ATOMIC_LOAD_AND_16:
4535 case ISD::ATOMIC_LOAD_OR_16:
4536 case ISD::ATOMIC_LOAD_XOR_16:
4537 case ISD::ATOMIC_LOAD_NAND_16:
4538 case ISD::ATOMIC_LOAD_MIN_16:
4539 case ISD::ATOMIC_LOAD_MAX_16:
4540 case ISD::ATOMIC_LOAD_UMIN_16:
4541 case ISD::ATOMIC_LOAD_UMAX_16:
4542 case ISD::ATOMIC_SWAP_16:
4543 case ISD::ATOMIC_LOAD_ADD_32:
4544 case ISD::ATOMIC_LOAD_SUB_32:
4545 case ISD::ATOMIC_LOAD_AND_32:
4546 case ISD::ATOMIC_LOAD_OR_32:
4547 case ISD::ATOMIC_LOAD_XOR_32:
4548 case ISD::ATOMIC_LOAD_NAND_32:
4549 case ISD::ATOMIC_LOAD_MIN_32:
4550 case ISD::ATOMIC_LOAD_MAX_32:
4551 case ISD::ATOMIC_LOAD_UMIN_32:
4552 case ISD::ATOMIC_LOAD_UMAX_32:
4553 case ISD::ATOMIC_SWAP_32:
4554 case ISD::ATOMIC_LOAD_ADD_64:
4555 case ISD::ATOMIC_LOAD_SUB_64:
4556 case ISD::ATOMIC_LOAD_AND_64:
4557 case ISD::ATOMIC_LOAD_OR_64:
4558 case ISD::ATOMIC_LOAD_XOR_64:
4559 case ISD::ATOMIC_LOAD_NAND_64:
4560 case ISD::ATOMIC_LOAD_MIN_64:
4561 case ISD::ATOMIC_LOAD_MAX_64:
4562 case ISD::ATOMIC_LOAD_UMIN_64:
4563 case ISD::ATOMIC_LOAD_UMAX_64:
4564 case ISD::ATOMIC_SWAP_64: {
4565 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
4566 Tmp2 = PromoteOp(Node->getOperand(2));
4567 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4568 AtomNode->getBasePtr(), Tmp2,
4569 AtomNode->getSrcValue(),
4570 AtomNode->getAlignment());
4571 // Remember that we legalized the chain.
4572 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4582 // The input may have strange things in the top bits of the registers, but
4583 // these operations don't care. They may have weird bits going out, but
4584 // that too is okay if they are integer operations.
4585 Tmp1 = PromoteOp(Node->getOperand(0));
4586 Tmp2 = PromoteOp(Node->getOperand(1));
4587 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4588 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4593 Tmp1 = PromoteOp(Node->getOperand(0));
4594 Tmp2 = PromoteOp(Node->getOperand(1));
4595 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4596 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4598 // Floating point operations will give excess precision that we may not be
4599 // able to tolerate. If we DO allow excess precision, just leave it,
4600 // otherwise excise it.
4601 // FIXME: Why would we need to round FP ops more than integer ones?
4602 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4603 if (NoExcessFPPrecision)
4604 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4605 DAG.getValueType(VT));
4610 // These operators require that their input be sign extended.
4611 Tmp1 = PromoteOp(Node->getOperand(0));
4612 Tmp2 = PromoteOp(Node->getOperand(1));
4613 if (NVT.isInteger()) {
4614 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4615 DAG.getValueType(VT));
4616 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4617 DAG.getValueType(VT));
4619 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4621 // Perform FP_ROUND: this is probably overly pessimistic.
4622 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
4623 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4624 DAG.getValueType(VT));
4628 case ISD::FCOPYSIGN:
4629 // These operators require that their input be fp extended.
4630 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4631 case Expand: assert(0 && "not implemented");
4632 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4633 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
4635 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4636 case Expand: assert(0 && "not implemented");
4637 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4638 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
4640 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4642 // Perform FP_ROUND: this is probably overly pessimistic.
4643 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4644 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4645 DAG.getValueType(VT));
4650 // These operators require that their input be zero extended.
4651 Tmp1 = PromoteOp(Node->getOperand(0));
4652 Tmp2 = PromoteOp(Node->getOperand(1));
4653 assert(NVT.isInteger() && "Operators don't apply to FP!");
4654 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4655 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4656 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4660 Tmp1 = PromoteOp(Node->getOperand(0));
4661 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4664 // The input value must be properly sign extended.
4665 Tmp1 = PromoteOp(Node->getOperand(0));
4666 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4667 DAG.getValueType(VT));
4668 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4671 // The input value must be properly zero extended.
4672 Tmp1 = PromoteOp(Node->getOperand(0));
4673 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4674 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4678 Tmp1 = Node->getOperand(0); // Get the chain.
4679 Tmp2 = Node->getOperand(1); // Get the pointer.
4680 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4681 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4682 Result = TLI.LowerOperation(Tmp3, DAG);
4684 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4685 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
4686 // Increment the pointer, VAList, to the next vaarg
4687 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4688 DAG.getConstant(VT.getSizeInBits()/8,
4689 TLI.getPointerTy()));
4690 // Store the incremented VAList to the legalized pointer
4691 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
4692 // Load the actual argument out of the pointer VAList
4693 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4695 // Remember that we legalized the chain.
4696 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4700 LoadSDNode *LD = cast<LoadSDNode>(Node);
4701 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4702 ? ISD::EXTLOAD : LD->getExtensionType();
4703 Result = DAG.getExtLoad(ExtType, NVT,
4704 LD->getChain(), LD->getBasePtr(),
4705 LD->getSrcValue(), LD->getSrcValueOffset(),
4708 LD->getAlignment());
4709 // Remember that we legalized the chain.
4710 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4714 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4715 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4717 MVT VT2 = Tmp2.getValueType();
4718 assert(VT2 == Tmp3.getValueType()
4719 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4720 // Ensure that the resulting node is at least the same size as the operands'
4721 // value types, because we cannot assume that TLI.getSetCCValueType() is
4723 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
4726 case ISD::SELECT_CC:
4727 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4728 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4729 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4730 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4733 Tmp1 = Node->getOperand(0);
4734 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4735 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4736 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4737 DAG.getConstant(NVT.getSizeInBits() -
4739 TLI.getShiftAmountTy()));
4744 // Zero extend the argument
4745 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4746 // Perform the larger operation, then subtract if needed.
4747 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4748 switch(Node->getOpcode()) {
4753 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4754 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
4755 DAG.getConstant(NVT.getSizeInBits(), NVT),
4757 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4758 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
4761 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4762 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4763 DAG.getConstant(NVT.getSizeInBits() -
4764 VT.getSizeInBits(), NVT));
4768 case ISD::EXTRACT_SUBVECTOR:
4769 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4771 case ISD::EXTRACT_VECTOR_ELT:
4772 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4776 assert(Result.getNode() && "Didn't set a result!");
4778 // Make sure the result is itself legal.
4779 Result = LegalizeOp(Result);
4781 // Remember that we promoted this!
4782 AddPromotedOperand(Op, Result);
4786 /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4787 /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4788 /// based on the vector type. The return type of this matches the element type
4789 /// of the vector, which may not be legal for the target.
4790 SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
4791 // We know that operand #0 is the Vec vector. If the index is a constant
4792 // or if the invec is a supported hardware type, we can use it. Otherwise,
4793 // lower to a store then an indexed load.
4794 SDValue Vec = Op.getOperand(0);
4795 SDValue Idx = Op.getOperand(1);
4797 MVT TVT = Vec.getValueType();
4798 unsigned NumElems = TVT.getVectorNumElements();
4800 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4801 default: assert(0 && "This action is not supported yet!");
4802 case TargetLowering::Custom: {
4803 Vec = LegalizeOp(Vec);
4804 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4805 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
4810 case TargetLowering::Legal:
4811 if (isTypeLegal(TVT)) {
4812 Vec = LegalizeOp(Vec);
4813 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4817 case TargetLowering::Promote:
4818 assert(TVT.isVector() && "not vector type");
4819 // fall thru to expand since vectors are by default are promote
4820 case TargetLowering::Expand:
4824 if (NumElems == 1) {
4825 // This must be an access of the only element. Return it.
4826 Op = ScalarizeVectorOp(Vec);
4827 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4828 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
4829 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4831 SplitVectorOp(Vec, Lo, Hi);
4832 if (CIdx->getZExtValue() < NumLoElts) {
4836 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
4837 Idx.getValueType());
4840 // It's now an extract from the appropriate high or low part. Recurse.
4841 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4842 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4844 // Store the value to a temporary stack slot, then LOAD the scalar
4845 // element back out.
4846 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4847 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4849 // Add the offset to the index.
4850 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
4851 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4852 DAG.getConstant(EltSize, Idx.getValueType()));
4854 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
4855 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
4857 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
4859 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4861 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4866 /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4867 /// we assume the operation can be split if it is not already legal.
4868 SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
4869 // We know that operand #0 is the Vec vector. For now we assume the index
4870 // is a constant and that the extracted result is a supported hardware type.
4871 SDValue Vec = Op.getOperand(0);
4872 SDValue Idx = LegalizeOp(Op.getOperand(1));
4874 unsigned NumElems = Vec.getValueType().getVectorNumElements();
4876 if (NumElems == Op.getValueType().getVectorNumElements()) {
4877 // This must be an access of the desired vector length. Return it.
4881 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4883 SplitVectorOp(Vec, Lo, Hi);
4884 if (CIdx->getZExtValue() < NumElems/2) {
4888 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4889 Idx.getValueType());
4892 // It's now an extract from the appropriate high or low part. Recurse.
4893 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4894 return ExpandEXTRACT_SUBVECTOR(Op);
4897 /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4898 /// with condition CC on the current target. This usually involves legalizing
4899 /// or promoting the arguments. In the case where LHS and RHS must be expanded,
4900 /// there may be no choice but to create a new SetCC node to represent the
4901 /// legalized value of setcc lhs, rhs. In this case, the value is returned in
4902 /// LHS, and the SDValue returned in RHS has a nil SDNode value.
4903 void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4906 SDValue Tmp1, Tmp2, Tmp3, Result;
4908 switch (getTypeAction(LHS.getValueType())) {
4910 Tmp1 = LegalizeOp(LHS); // LHS
4911 Tmp2 = LegalizeOp(RHS); // RHS
4914 Tmp1 = PromoteOp(LHS); // LHS
4915 Tmp2 = PromoteOp(RHS); // RHS
4917 // If this is an FP compare, the operands have already been extended.
4918 if (LHS.getValueType().isInteger()) {
4919 MVT VT = LHS.getValueType();
4920 MVT NVT = TLI.getTypeToTransformTo(VT);
4922 // Otherwise, we have to insert explicit sign or zero extends. Note
4923 // that we could insert sign extends for ALL conditions, but zero extend
4924 // is cheaper on many machines (an AND instead of two shifts), so prefer
4926 switch (cast<CondCodeSDNode>(CC)->get()) {
4927 default: assert(0 && "Unknown integer comparison!");
4934 // ALL of these operations will work if we either sign or zero extend
4935 // the operands (including the unsigned comparisons!). Zero extend is
4936 // usually a simpler/cheaper operation, so prefer it.
4937 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4938 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4944 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4945 DAG.getValueType(VT));
4946 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4947 DAG.getValueType(VT));
4948 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4949 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
4955 MVT VT = LHS.getValueType();
4956 if (VT == MVT::f32 || VT == MVT::f64) {
4957 // Expand into one or more soft-fp libcall(s).
4958 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
4959 switch (cast<CondCodeSDNode>(CC)->get()) {
4962 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4966 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4970 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4974 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4978 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4982 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4985 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4988 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4991 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4992 switch (cast<CondCodeSDNode>(CC)->get()) {
4994 // SETONE = SETOLT | SETOGT
4995 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4998 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5001 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5004 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5007 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5010 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5012 default: assert(0 && "Unsupported FP setcc!");
5017 SDValue Ops[2] = { LHS, RHS };
5018 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
5019 false /*sign irrelevant*/, Dummy);
5020 Tmp2 = DAG.getConstant(0, MVT::i32);
5021 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5022 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
5023 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
5025 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
5026 false /*sign irrelevant*/, Dummy);
5027 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
5028 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
5029 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5032 LHS = LegalizeOp(Tmp1);
5037 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5038 ExpandOp(LHS, LHSLo, LHSHi);
5039 ExpandOp(RHS, RHSLo, RHSHi);
5040 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5042 if (VT==MVT::ppcf128) {
5043 // FIXME: This generated code sucks. We want to generate
5044 // FCMPU crN, hi1, hi2
5046 // FCMPU crN, lo1, lo2
5047 // The following can be improved, but not that much.
5048 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5050 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
5051 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5052 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5054 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
5055 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5056 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
5065 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5066 if (RHSCST->isAllOnesValue()) {
5067 // Comparison to -1.
5068 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5073 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5074 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5075 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5076 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5079 // If this is a comparison of the sign bit, just look at the top part.
5081 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5082 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
5083 CST->isNullValue()) || // X < 0
5084 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5085 CST->isAllOnesValue())) { // X > -1
5091 // FIXME: This generated code sucks.
5092 ISD::CondCode LowCC;
5094 default: assert(0 && "Unknown integer setcc!");
5096 case ISD::SETULT: LowCC = ISD::SETULT; break;
5098 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5100 case ISD::SETULE: LowCC = ISD::SETULE; break;
5102 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5105 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5106 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5107 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5109 // NOTE: on targets without efficient SELECT of bools, we can always use
5110 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5111 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
5112 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
5113 LowCC, false, DagCombineInfo);
5114 if (!Tmp1.getNode())
5115 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5116 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5117 CCCode, false, DagCombineInfo);
5118 if (!Tmp2.getNode())
5119 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
5122 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5123 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
5124 if ((Tmp1C && Tmp1C->isNullValue()) ||
5125 (Tmp2C && Tmp2C->isNullValue() &&
5126 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5127 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
5128 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
5129 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5130 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5131 // low part is known false, returns high part.
5132 // For LE / GE, if high part is known false, ignore the low part.
5133 // For LT / GT, if high part is known true, ignore the low part.
5137 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5138 ISD::SETEQ, false, DagCombineInfo);
5139 if (!Result.getNode())
5140 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5142 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5143 Result, Tmp1, Tmp2));
5154 /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5155 /// condition code CC on the current target. This routine assumes LHS and rHS
5156 /// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5157 /// illegal condition code into AND / OR of multiple SETCC values.
5158 void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5159 SDValue &LHS, SDValue &RHS,
5161 MVT OpVT = LHS.getValueType();
5162 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5163 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5164 default: assert(0 && "Unknown condition code action!");
5165 case TargetLowering::Legal:
5168 case TargetLowering::Expand: {
5169 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5172 default: assert(0 && "Don't know how to expand this condition!"); abort();
5173 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5174 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5175 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5176 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5177 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5178 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5179 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5180 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5181 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5182 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5183 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5184 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5185 // FIXME: Implement more expansions.
5188 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5189 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5190 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5198 /// EmitStackConvert - Emit a store/load combination to the stack. This stores
5199 /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5200 /// a load from the stack slot to DestVT, extending it if needed.
5201 /// The resultant code need not be legal.
5202 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5205 // Create the stack frame object.
5206 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5207 SrcOp.getValueType().getTypeForMVT());
5208 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
5210 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
5211 int SPFI = StackPtrFI->getIndex();
5213 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5214 unsigned SlotSize = SlotVT.getSizeInBits();
5215 unsigned DestSize = DestVT.getSizeInBits();
5216 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5217 DestVT.getTypeForMVT());
5219 // Emit a store to the stack slot. Use a truncstore if the input value is
5220 // later than DestVT.
5223 if (SrcSize > SlotSize)
5224 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
5225 PseudoSourceValue::getFixedStack(SPFI), 0,
5226 SlotVT, false, SrcAlign);
5228 assert(SrcSize == SlotSize && "Invalid store");
5229 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
5230 PseudoSourceValue::getFixedStack(SPFI), 0,
5234 // Result is a load from the stack slot.
5235 if (SlotSize == DestSize)
5236 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
5238 assert(SlotSize < DestSize && "Unknown extension!");
5239 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5243 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
5244 // Create a vector sized/aligned stack slot, store the value to element #0,
5245 // then load the whole vector back out.
5246 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
5248 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
5249 int SPFI = StackPtrFI->getIndex();
5251 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
5252 PseudoSourceValue::getFixedStack(SPFI), 0);
5253 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
5254 PseudoSourceValue::getFixedStack(SPFI), 0);
5258 /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5259 /// support the operation, but do support the resultant vector type.
5260 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
5262 // If the only non-undef value is the low element, turn this into a
5263 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5264 unsigned NumElems = Node->getNumOperands();
5265 bool isOnlyLowElement = true;
5266 SDValue SplatValue = Node->getOperand(0);
5268 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
5269 // and use a bitmask instead of a list of elements.
5270 std::map<SDValue, std::vector<unsigned> > Values;
5271 Values[SplatValue].push_back(0);
5272 bool isConstant = true;
5273 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5274 SplatValue.getOpcode() != ISD::UNDEF)
5277 for (unsigned i = 1; i < NumElems; ++i) {
5278 SDValue V = Node->getOperand(i);
5279 Values[V].push_back(i);
5280 if (V.getOpcode() != ISD::UNDEF)
5281 isOnlyLowElement = false;
5282 if (SplatValue != V)
5283 SplatValue = SDValue(0,0);
5285 // If this isn't a constant element or an undef, we can't use a constant
5287 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5288 V.getOpcode() != ISD::UNDEF)
5292 if (isOnlyLowElement) {
5293 // If the low element is an undef too, then this whole things is an undef.
5294 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5295 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5296 // Otherwise, turn this into a scalar_to_vector node.
5297 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5298 Node->getOperand(0));
5301 // If all elements are constants, create a load from the constant pool.
5303 MVT VT = Node->getValueType(0);
5304 std::vector<Constant*> CV;
5305 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5306 if (ConstantFPSDNode *V =
5307 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
5308 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
5309 } else if (ConstantSDNode *V =
5310 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
5311 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
5313 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
5315 Node->getOperand(0).getValueType().getTypeForMVT();
5316 CV.push_back(UndefValue::get(OpNTy));
5319 Constant *CP = ConstantVector::get(CV);
5320 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
5321 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
5322 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
5323 PseudoSourceValue::getConstantPool(), 0,
5327 if (SplatValue.getNode()) { // Splat of one value?
5328 // Build the shuffle constant vector: <0, 0, 0, 0>
5329 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5330 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5331 std::vector<SDValue> ZeroVec(NumElems, Zero);
5332 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5333 &ZeroVec[0], ZeroVec.size());
5335 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5336 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5337 // Get the splatted value into the low element of a vector register.
5339 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5341 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5342 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5343 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5348 // If there are only two unique elements, we may be able to turn this into a
5350 if (Values.size() == 2) {
5351 // Get the two values in deterministic order.
5352 SDValue Val1 = Node->getOperand(1);
5354 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
5355 if (MI->first != Val1)
5358 Val2 = (++MI)->first;
5360 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5361 // vector shuffle has the undef vector on the RHS.
5362 if (Val1.getOpcode() == ISD::UNDEF)
5363 std::swap(Val1, Val2);
5365 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
5366 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5367 MVT MaskEltVT = MaskVT.getVectorElementType();
5368 std::vector<SDValue> MaskVec(NumElems);
5370 // Set elements of the shuffle mask for Val1.
5371 std::vector<unsigned> &Val1Elts = Values[Val1];
5372 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5373 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5375 // Set elements of the shuffle mask for Val2.
5376 std::vector<unsigned> &Val2Elts = Values[Val2];
5377 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5378 if (Val2.getOpcode() != ISD::UNDEF)
5379 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5381 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5383 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5384 &MaskVec[0], MaskVec.size());
5386 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
5387 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5388 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
5389 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5390 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
5391 SDValue Ops[] = { Val1, Val2, ShuffleMask };
5393 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
5394 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
5398 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5399 // aligned object on the stack, store each element into it, then load
5400 // the result as a vector.
5401 MVT VT = Node->getValueType(0);
5402 // Create the stack frame object.
5403 SDValue FIPtr = DAG.CreateStackTemporary(VT);
5405 // Emit a store of each element to the stack slot.
5406 SmallVector<SDValue, 8> Stores;
5407 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
5408 // Store (in the right endianness) the elements to memory.
5409 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5410 // Ignore undef elements.
5411 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5413 unsigned Offset = TypeByteSize*i;
5415 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5416 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5418 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5423 if (!Stores.empty()) // Not all undef elements?
5424 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5425 &Stores[0], Stores.size());
5427 StoreChain = DAG.getEntryNode();
5429 // Result is a load from the stack slot.
5430 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5433 void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5434 SDValue Op, SDValue Amt,
5435 SDValue &Lo, SDValue &Hi) {
5436 // Expand the subcomponents.
5438 ExpandOp(Op, LHSL, LHSH);
5440 SDValue Ops[] = { LHSL, LHSH, Amt };
5441 MVT VT = LHSL.getValueType();
5442 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5443 Hi = Lo.getValue(1);
5447 /// ExpandShift - Try to find a clever way to expand this shift operation out to
5448 /// smaller elements. If we can't find a way that is more efficient than a
5449 /// libcall on this target, return false. Otherwise, return true with the
5450 /// low-parts expanded into Lo and Hi.
5451 bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5452 SDValue &Lo, SDValue &Hi) {
5453 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5454 "This is not a shift!");
5456 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
5457 SDValue ShAmt = LegalizeOp(Amt);
5458 MVT ShTy = ShAmt.getValueType();
5459 unsigned ShBits = ShTy.getSizeInBits();
5460 unsigned VTBits = Op.getValueType().getSizeInBits();
5461 unsigned NVTBits = NVT.getSizeInBits();
5463 // Handle the case when Amt is an immediate.
5464 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
5465 unsigned Cst = CN->getZExtValue();
5466 // Expand the incoming operand to be shifted, so that we have its parts
5468 ExpandOp(Op, InL, InH);
5472 Lo = DAG.getConstant(0, NVT);
5473 Hi = DAG.getConstant(0, NVT);
5474 } else if (Cst > NVTBits) {
5475 Lo = DAG.getConstant(0, NVT);
5476 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5477 } else if (Cst == NVTBits) {
5478 Lo = DAG.getConstant(0, NVT);
5481 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5482 Hi = DAG.getNode(ISD::OR, NVT,
5483 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5484 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5489 Lo = DAG.getConstant(0, NVT);
5490 Hi = DAG.getConstant(0, NVT);
5491 } else if (Cst > NVTBits) {
5492 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5493 Hi = DAG.getConstant(0, NVT);
5494 } else if (Cst == NVTBits) {
5496 Hi = DAG.getConstant(0, NVT);
5498 Lo = DAG.getNode(ISD::OR, NVT,
5499 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5500 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5501 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5506 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5507 DAG.getConstant(NVTBits-1, ShTy));
5508 } else if (Cst > NVTBits) {
5509 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5510 DAG.getConstant(Cst-NVTBits, ShTy));
5511 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5512 DAG.getConstant(NVTBits-1, ShTy));
5513 } else if (Cst == NVTBits) {
5515 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5516 DAG.getConstant(NVTBits-1, ShTy));
5518 Lo = DAG.getNode(ISD::OR, NVT,
5519 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5520 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5521 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5527 // Okay, the shift amount isn't constant. However, if we can tell that it is
5528 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
5529 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5530 APInt KnownZero, KnownOne;
5531 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5533 // If we know that if any of the high bits of the shift amount are one, then
5534 // we can do this as a couple of simple shifts.
5535 if (KnownOne.intersects(Mask)) {
5536 // Mask out the high bit, which we know is set.
5537 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
5538 DAG.getConstant(~Mask, Amt.getValueType()));
5540 // Expand the incoming operand to be shifted, so that we have its parts
5542 ExpandOp(Op, InL, InH);
5545 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5546 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5549 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5550 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5553 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5554 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5555 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5560 // If we know that the high bits of the shift amount are all zero, then we can
5561 // do this as a couple of simple shifts.
5562 if ((KnownZero & Mask) == Mask) {
5564 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5565 DAG.getConstant(NVTBits, Amt.getValueType()),
5568 // Expand the incoming operand to be shifted, so that we have its parts
5570 ExpandOp(Op, InL, InH);
5573 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5574 Hi = DAG.getNode(ISD::OR, NVT,
5575 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5576 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5579 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5580 Lo = DAG.getNode(ISD::OR, NVT,
5581 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5582 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5585 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5586 Lo = DAG.getNode(ISD::OR, NVT,
5587 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5588 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5597 // ExpandLibCall - Expand a node into a call to a libcall. If the result value
5598 // does not fit into a register, return the lo part and set the hi part to the
5599 // by-reg argument. If it does fit into a single register, return the result
5600 // and leave the Hi part unset.
5601 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5602 bool isSigned, SDValue &Hi) {
5603 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5604 // The input chain to this libcall is the entry node of the function.
5605 // Legalizing the call will automatically add the previous call to the
5607 SDValue InChain = DAG.getEntryNode();
5609 TargetLowering::ArgListTy Args;
5610 TargetLowering::ArgListEntry Entry;
5611 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5612 MVT ArgVT = Node->getOperand(i).getValueType();
5613 const Type *ArgTy = ArgVT.getTypeForMVT();
5614 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5615 Entry.isSExt = isSigned;
5616 Entry.isZExt = !isSigned;
5617 Args.push_back(Entry);
5619 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
5620 TLI.getPointerTy());
5622 // Splice the libcall in wherever FindInputOutputChains tells us to.
5623 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
5624 std::pair<SDValue,SDValue> CallInfo =
5625 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5626 CallingConv::C, false, Callee, Args, DAG);
5628 // Legalize the call sequence, starting with the chain. This will advance
5629 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5630 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5631 LegalizeOp(CallInfo.second);
5633 switch (getTypeAction(CallInfo.first.getValueType())) {
5634 default: assert(0 && "Unknown thing");
5636 Result = CallInfo.first;
5639 ExpandOp(CallInfo.first, Result, Hi);
5645 /// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5647 SDValue SelectionDAGLegalize::
5648 LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5649 bool isCustom = false;
5651 switch (getTypeAction(Op.getValueType())) {
5653 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5654 Op.getValueType())) {
5655 default: assert(0 && "Unknown operation action!");
5656 case TargetLowering::Custom:
5659 case TargetLowering::Legal:
5660 Tmp1 = LegalizeOp(Op);
5661 if (Result.getNode())
5662 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5664 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5667 Tmp1 = TLI.LowerOperation(Result, DAG);
5668 if (Tmp1.getNode()) Result = Tmp1;
5671 case TargetLowering::Expand:
5672 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5674 case TargetLowering::Promote:
5675 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5680 Result = ExpandIntToFP(isSigned, DestTy, Op);
5683 Tmp1 = PromoteOp(Op);
5685 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5686 Tmp1, DAG.getValueType(Op.getValueType()));
5688 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5691 if (Result.getNode())
5692 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5694 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5696 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5702 /// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5704 SDValue SelectionDAGLegalize::
5705 ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
5706 MVT SourceVT = Source.getValueType();
5707 bool ExpandSource = getTypeAction(SourceVT) == Expand;
5709 // Expand unsupported int-to-fp vector casts by unrolling them.
5710 if (DestTy.isVector()) {
5712 return LegalizeOp(UnrollVectorOp(Source));
5713 MVT DestEltTy = DestTy.getVectorElementType();
5714 if (DestTy.getVectorNumElements() == 1) {
5715 SDValue Scalar = ScalarizeVectorOp(Source);
5716 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5718 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5721 SplitVectorOp(Source, Lo, Hi);
5722 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5723 DestTy.getVectorNumElements() / 2);
5724 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5725 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
5726 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5730 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5731 if (!isSigned && SourceVT != MVT::i32) {
5732 // The integer value loaded will be incorrectly if the 'sign bit' of the
5733 // incoming integer is set. To handle this, we dynamically test to see if
5734 // it is set, and, if so, add a fudge factor.
5738 ExpandOp(Source, Lo, Hi);
5739 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5741 // The comparison for the sign bit will use the entire operand.
5745 // Check to see if the target has a custom way to lower this. If so, use
5746 // it. (Note we've already expanded the operand in this case.)
5747 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5748 default: assert(0 && "This action not implemented for this operation!");
5749 case TargetLowering::Legal:
5750 case TargetLowering::Expand:
5751 break; // This case is handled below.
5752 case TargetLowering::Custom: {
5753 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5756 return LegalizeOp(NV);
5757 break; // The target decided this was legal after all
5761 // If this is unsigned, and not supported, first perform the conversion to
5762 // signed, then adjust the result if the sign bit is set.
5763 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
5765 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
5766 DAG.getConstant(0, Hi.getValueType()),
5768 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5769 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5770 SignSet, Four, Zero);
5771 uint64_t FF = 0x5f800000ULL;
5772 if (TLI.isLittleEndian()) FF <<= 32;
5773 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5775 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5776 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
5777 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5778 Alignment = std::min(Alignment, 4u);
5780 if (DestTy == MVT::f32)
5781 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
5782 PseudoSourceValue::getConstantPool(), 0,
5784 else if (DestTy.bitsGT(MVT::f32))
5785 // FIXME: Avoid the extend by construction the right constantpool?
5786 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
5788 PseudoSourceValue::getConstantPool(), 0,
5789 MVT::f32, false, Alignment);
5791 assert(0 && "Unexpected conversion");
5793 MVT SCVT = SignedConv.getValueType();
5794 if (SCVT != DestTy) {
5795 // Destination type needs to be expanded as well. The FADD now we are
5796 // constructing will be expanded into a libcall.
5797 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5798 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
5799 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
5800 SignedConv, SignedConv.getValue(1));
5802 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5804 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5807 // Check to see if the target has a custom way to lower this. If so, use it.
5808 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
5809 default: assert(0 && "This action not implemented for this operation!");
5810 case TargetLowering::Legal:
5811 case TargetLowering::Expand:
5812 break; // This case is handled below.
5813 case TargetLowering::Custom: {
5814 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5817 return LegalizeOp(NV);
5818 break; // The target decided this was legal after all
5822 // Expand the source, then glue it back together for the call. We must expand
5823 // the source in case it is shared (this pass of legalize must traverse it).
5825 SDValue SrcLo, SrcHi;
5826 ExpandOp(Source, SrcLo, SrcHi);
5827 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5830 RTLIB::Libcall LC = isSigned ?
5831 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5832 RTLIB::getUINTTOFP(SourceVT, DestTy);
5833 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5835 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5837 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5838 if (Result.getValueType() != DestTy && HiPart.getNode())
5839 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5843 /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5844 /// INT_TO_FP operation of the specified operand when the target requests that
5845 /// we expand it. At this point, we know that the result and operand types are
5846 /// legal for the target.
5847 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5850 if (Op0.getValueType() == MVT::i32) {
5851 // simple 32-bit [signed|unsigned] integer to float/double expansion
5853 // Get the stack frame index of a 8 byte buffer.
5854 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
5856 // word offset constant for Hi/Lo address computation
5857 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5858 // set up Hi and Lo (into buffer) address based on endian
5859 SDValue Hi = StackSlot;
5860 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5861 if (TLI.isLittleEndian())
5864 // if signed map to unsigned space
5867 // constant used to invert sign bit (signed to unsigned mapping)
5868 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5869 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5873 // store the lo of the constructed double - based on integer input
5874 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
5875 Op0Mapped, Lo, NULL, 0);
5876 // initial hi portion of constructed double
5877 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5878 // store the hi of the constructed double - biased exponent
5879 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5880 // load the constructed double
5881 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5882 // FP constant to bias correct the final result
5883 SDValue Bias = DAG.getConstantFP(isSigned ?
5884 BitsToDouble(0x4330000080000000ULL)
5885 : BitsToDouble(0x4330000000000000ULL),
5887 // subtract the bias
5888 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5891 // handle final rounding
5892 if (DestVT == MVT::f64) {
5895 } else if (DestVT.bitsLT(MVT::f64)) {
5896 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5897 DAG.getIntPtrConstant(0));
5898 } else if (DestVT.bitsGT(MVT::f64)) {
5899 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
5903 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5904 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5906 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
5907 DAG.getConstant(0, Op0.getValueType()),
5909 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5910 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5911 SignSet, Four, Zero);
5913 // If the sign bit of the integer is set, the large number will be treated
5914 // as a negative number. To counteract this, the dynamic code adds an
5915 // offset depending on the data type.
5917 switch (Op0.getValueType().getSimpleVT()) {
5918 default: assert(0 && "Unsupported integer type!");
5919 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5920 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5921 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5922 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5924 if (TLI.isLittleEndian()) FF <<= 32;
5925 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5927 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5928 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
5929 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5930 Alignment = std::min(Alignment, 4u);
5932 if (DestVT == MVT::f32)
5933 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
5934 PseudoSourceValue::getConstantPool(), 0,
5938 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5939 DAG.getEntryNode(), CPIdx,
5940 PseudoSourceValue::getConstantPool(), 0,
5941 MVT::f32, false, Alignment));
5944 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5947 /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5948 /// *INT_TO_FP operation of the specified operand when the target requests that
5949 /// we promote it. At this point, we know that the result and operand types are
5950 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5951 /// operation that takes a larger input.
5952 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5955 // First step, figure out the appropriate *INT_TO_FP operation to use.
5956 MVT NewInTy = LegalOp.getValueType();
5958 unsigned OpToUse = 0;
5960 // Scan for the appropriate larger type to use.
5962 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5963 assert(NewInTy.isInteger() && "Ran out of possibilities!");
5965 // If the target supports SINT_TO_FP of this type, use it.
5966 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5968 case TargetLowering::Legal:
5969 if (!TLI.isTypeLegal(NewInTy))
5970 break; // Can't use this datatype.
5972 case TargetLowering::Custom:
5973 OpToUse = ISD::SINT_TO_FP;
5977 if (isSigned) continue;
5979 // If the target supports UINT_TO_FP of this type, use it.
5980 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5982 case TargetLowering::Legal:
5983 if (!TLI.isTypeLegal(NewInTy))
5984 break; // Can't use this datatype.
5986 case TargetLowering::Custom:
5987 OpToUse = ISD::UINT_TO_FP;
5992 // Otherwise, try a larger type.
5995 // Okay, we found the operation and type to use. Zero extend our input to the
5996 // desired type then run the operation on it.
5997 return DAG.getNode(OpToUse, DestVT,
5998 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6002 /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6003 /// FP_TO_*INT operation of the specified operand when the target requests that
6004 /// we promote it. At this point, we know that the result and operand types are
6005 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6006 /// operation that returns a larger result.
6007 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6010 // First step, figure out the appropriate FP_TO*INT operation to use.
6011 MVT NewOutTy = DestVT;
6013 unsigned OpToUse = 0;
6015 // Scan for the appropriate larger type to use.
6017 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6018 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
6020 // If the target supports FP_TO_SINT returning this type, use it.
6021 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6023 case TargetLowering::Legal:
6024 if (!TLI.isTypeLegal(NewOutTy))
6025 break; // Can't use this datatype.
6027 case TargetLowering::Custom:
6028 OpToUse = ISD::FP_TO_SINT;
6033 // If the target supports FP_TO_UINT of this type, use it.
6034 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6036 case TargetLowering::Legal:
6037 if (!TLI.isTypeLegal(NewOutTy))
6038 break; // Can't use this datatype.
6040 case TargetLowering::Custom:
6041 OpToUse = ISD::FP_TO_UINT;
6046 // Otherwise, try a larger type.
6050 // Okay, we found the operation and type to use.
6051 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
6053 // If the operation produces an invalid type, it must be custom lowered. Use
6054 // the target lowering hooks to expand it. Just keep the low part of the
6055 // expanded operation, we know that we're truncating anyway.
6056 if (getTypeAction(NewOutTy) == Expand) {
6057 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
6058 assert(Operation.getNode() && "Didn't return anything");
6061 // Truncate the result of the extended FP_TO_*INT operation to the desired
6063 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
6066 /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6068 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
6069 MVT VT = Op.getValueType();
6070 MVT SHVT = TLI.getShiftAmountTy();
6071 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
6072 switch (VT.getSimpleVT()) {
6073 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6075 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6076 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6077 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6079 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6080 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6081 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6082 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6083 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6084 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6085 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6086 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6087 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6089 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6090 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6091 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6092 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6093 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6094 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6095 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6096 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6097 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6098 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6099 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6100 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6101 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6102 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6103 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6104 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6105 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6106 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6107 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6108 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6109 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6113 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
6115 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
6117 default: assert(0 && "Cannot expand this yet!");
6119 static const uint64_t mask[6] = {
6120 0x5555555555555555ULL, 0x3333333333333333ULL,
6121 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6122 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6124 MVT VT = Op.getValueType();
6125 MVT ShVT = TLI.getShiftAmountTy();
6126 unsigned len = VT.getSizeInBits();
6127 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6128 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
6129 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6130 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
6131 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6132 DAG.getNode(ISD::AND, VT,
6133 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6138 // for now, we do this:
6139 // x = x | (x >> 1);
6140 // x = x | (x >> 2);
6142 // x = x | (x >>16);
6143 // x = x | (x >>32); // for 64-bit input
6144 // return popcount(~x);
6146 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
6147 MVT VT = Op.getValueType();
6148 MVT ShVT = TLI.getShiftAmountTy();
6149 unsigned len = VT.getSizeInBits();
6150 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6151 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
6152 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6154 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6155 return DAG.getNode(ISD::CTPOP, VT, Op);
6158 // for now, we use: { return popcount(~x & (x - 1)); }
6159 // unless the target has ctlz but not ctpop, in which case we use:
6160 // { return 32 - nlz(~x & (x-1)); }
6161 // see also http://www.hackersdelight.org/HDcode/ntz.cc
6162 MVT VT = Op.getValueType();
6163 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6164 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
6165 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6166 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6167 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6168 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6169 TLI.isOperationLegal(ISD::CTLZ, VT))
6170 return DAG.getNode(ISD::SUB, VT,
6171 DAG.getConstant(VT.getSizeInBits(), VT),
6172 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6173 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6178 /// ExpandOp - Expand the specified SDValue into its two component pieces
6179 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
6180 /// LegalizedNodes map is filled in for any results that are not expanded, the
6181 /// ExpandedNodes map is filled in for any results that are expanded, and the
6182 /// Lo/Hi values are returned.
6183 void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
6184 MVT VT = Op.getValueType();
6185 MVT NVT = TLI.getTypeToTransformTo(VT);
6186 SDNode *Node = Op.getNode();
6187 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
6188 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
6189 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
6191 // See if we already expanded it.
6192 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
6193 = ExpandedNodes.find(Op);
6194 if (I != ExpandedNodes.end()) {
6195 Lo = I->second.first;
6196 Hi = I->second.second;
6200 switch (Node->getOpcode()) {
6201 case ISD::CopyFromReg:
6202 assert(0 && "CopyFromReg must be legal!");
6203 case ISD::FP_ROUND_INREG:
6204 if (VT == MVT::ppcf128 &&
6205 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6206 TargetLowering::Custom) {
6207 SDValue SrcLo, SrcHi, Src;
6208 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6209 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
6210 SDValue Result = TLI.LowerOperation(
6211 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
6212 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6213 Lo = Result.getNode()->getOperand(0);
6214 Hi = Result.getNode()->getOperand(1);
6220 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6222 assert(0 && "Do not know how to expand this operator!");
6224 case ISD::EXTRACT_ELEMENT:
6225 ExpandOp(Node->getOperand(0), Lo, Hi);
6226 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
6227 return ExpandOp(Hi, Lo, Hi);
6228 return ExpandOp(Lo, Lo, Hi);
6229 case ISD::EXTRACT_VECTOR_ELT:
6230 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6231 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6232 return ExpandOp(Lo, Lo, Hi);
6234 Lo = DAG.getNode(ISD::UNDEF, NVT);
6235 Hi = DAG.getNode(ISD::UNDEF, NVT);
6237 case ISD::Constant: {
6238 unsigned NVTBits = NVT.getSizeInBits();
6239 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6240 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6241 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
6244 case ISD::ConstantFP: {
6245 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
6246 if (CFP->getValueType(0) == MVT::ppcf128) {
6247 APInt api = CFP->getValueAPF().bitcastToAPInt();
6248 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6250 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6254 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6255 if (getTypeAction(Lo.getValueType()) == Expand)
6256 ExpandOp(Lo, Lo, Hi);
6259 case ISD::BUILD_PAIR:
6260 // Return the operands.
6261 Lo = Node->getOperand(0);
6262 Hi = Node->getOperand(1);
6265 case ISD::MERGE_VALUES:
6266 if (Node->getNumValues() == 1) {
6267 ExpandOp(Op.getOperand(0), Lo, Hi);
6270 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
6271 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
6272 Op.getValue(1).getValueType() == MVT::Other &&
6273 "unhandled MERGE_VALUES");
6274 ExpandOp(Op.getOperand(0), Lo, Hi);
6275 // Remember that we legalized the chain.
6276 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6279 case ISD::SIGN_EXTEND_INREG:
6280 ExpandOp(Node->getOperand(0), Lo, Hi);
6281 // sext_inreg the low part if needed.
6282 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6284 // The high part gets the sign extension from the lo-part. This handles
6285 // things like sextinreg V:i64 from i8.
6286 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6287 DAG.getConstant(NVT.getSizeInBits()-1,
6288 TLI.getShiftAmountTy()));
6292 ExpandOp(Node->getOperand(0), Lo, Hi);
6293 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
6294 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6300 ExpandOp(Node->getOperand(0), Lo, Hi);
6301 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6302 DAG.getNode(ISD::CTPOP, NVT, Lo),
6303 DAG.getNode(ISD::CTPOP, NVT, Hi));
6304 Hi = DAG.getConstant(0, NVT);
6308 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6309 ExpandOp(Node->getOperand(0), Lo, Hi);
6310 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6311 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6312 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
6314 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
6315 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6317 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6318 Hi = DAG.getConstant(0, NVT);
6323 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6324 ExpandOp(Node->getOperand(0), Lo, Hi);
6325 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6326 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6327 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
6329 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
6330 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6332 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6333 Hi = DAG.getConstant(0, NVT);
6338 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6339 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
6340 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6341 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6343 // Remember that we legalized the chain.
6344 Hi = LegalizeOp(Hi);
6345 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
6346 if (TLI.isBigEndian())
6352 LoadSDNode *LD = cast<LoadSDNode>(Node);
6353 SDValue Ch = LD->getChain(); // Legalize the chain.
6354 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
6355 ISD::LoadExtType ExtType = LD->getExtensionType();
6356 const Value *SV = LD->getSrcValue();
6357 int SVOffset = LD->getSrcValueOffset();
6358 unsigned Alignment = LD->getAlignment();
6359 bool isVolatile = LD->isVolatile();
6361 if (ExtType == ISD::NON_EXTLOAD) {
6362 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
6363 isVolatile, Alignment);
6364 if (VT == MVT::f32 || VT == MVT::f64) {
6365 // f32->i32 or f64->i64 one to one expansion.
6366 // Remember that we legalized the chain.
6367 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
6368 // Recursively expand the new load.
6369 if (getTypeAction(NVT) == Expand)
6370 ExpandOp(Lo, Lo, Hi);
6374 // Increment the pointer to the other half.
6375 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
6376 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6377 DAG.getIntPtrConstant(IncrementSize));
6378 SVOffset += IncrementSize;
6379 Alignment = MinAlign(Alignment, IncrementSize);
6380 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
6381 isVolatile, Alignment);
6383 // Build a factor node to remember that this load is independent of the
6385 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6388 // Remember that we legalized the chain.
6389 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6390 if (TLI.isBigEndian())
6393 MVT EVT = LD->getMemoryVT();
6395 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6396 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
6397 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
6398 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
6399 SVOffset, isVolatile, Alignment);
6400 // Remember that we legalized the chain.
6401 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
6402 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6407 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
6408 SVOffset, isVolatile, Alignment);
6410 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
6411 SVOffset, EVT, isVolatile,
6414 // Remember that we legalized the chain.
6415 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
6417 if (ExtType == ISD::SEXTLOAD) {
6418 // The high part is obtained by SRA'ing all but one of the bits of the
6420 unsigned LoSize = Lo.getValueType().getSizeInBits();
6421 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6422 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6423 } else if (ExtType == ISD::ZEXTLOAD) {
6424 // The high part is just a zero.
6425 Hi = DAG.getConstant(0, NVT);
6426 } else /* if (ExtType == ISD::EXTLOAD) */ {
6427 // The high part is undefined.
6428 Hi = DAG.getNode(ISD::UNDEF, NVT);
6435 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
6436 SDValue LL, LH, RL, RH;
6437 ExpandOp(Node->getOperand(0), LL, LH);
6438 ExpandOp(Node->getOperand(1), RL, RH);
6439 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6440 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6444 SDValue LL, LH, RL, RH;
6445 ExpandOp(Node->getOperand(1), LL, LH);
6446 ExpandOp(Node->getOperand(2), RL, RH);
6447 if (getTypeAction(NVT) == Expand)
6448 NVT = TLI.getTypeToExpandTo(NVT);
6449 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6451 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6454 case ISD::SELECT_CC: {
6455 SDValue TL, TH, FL, FH;
6456 ExpandOp(Node->getOperand(2), TL, TH);
6457 ExpandOp(Node->getOperand(3), FL, FH);
6458 if (getTypeAction(NVT) == Expand)
6459 NVT = TLI.getTypeToExpandTo(NVT);
6460 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6461 Node->getOperand(1), TL, FL, Node->getOperand(4));
6463 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6464 Node->getOperand(1), TH, FH, Node->getOperand(4));
6467 case ISD::ANY_EXTEND:
6468 // The low part is any extension of the input (which degenerates to a copy).
6469 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6470 // The high part is undefined.
6471 Hi = DAG.getNode(ISD::UNDEF, NVT);
6473 case ISD::SIGN_EXTEND: {
6474 // The low part is just a sign extension of the input (which degenerates to
6476 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6478 // The high part is obtained by SRA'ing all but one of the bits of the lo
6480 unsigned LoSize = Lo.getValueType().getSizeInBits();
6481 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6482 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6485 case ISD::ZERO_EXTEND:
6486 // The low part is just a zero extension of the input (which degenerates to
6488 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6490 // The high part is just a zero.
6491 Hi = DAG.getConstant(0, NVT);
6494 case ISD::TRUNCATE: {
6495 // The input value must be larger than this value. Expand *it*.
6497 ExpandOp(Node->getOperand(0), NewLo, Hi);
6499 // The low part is now either the right size, or it is closer. If not the
6500 // right size, make an illegal truncate so we recursively expand it.
6501 if (NewLo.getValueType() != Node->getValueType(0))
6502 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6503 ExpandOp(NewLo, Lo, Hi);
6507 case ISD::BIT_CONVERT: {
6509 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6510 // If the target wants to, allow it to lower this itself.
6511 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6512 case Expand: assert(0 && "cannot expand FP!");
6513 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6514 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6516 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6519 // f32 / f64 must be expanded to i32 / i64.
6520 if (VT == MVT::f32 || VT == MVT::f64) {
6521 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6522 if (getTypeAction(NVT) == Expand)
6523 ExpandOp(Lo, Lo, Hi);
6527 // If source operand will be expanded to the same type as VT, i.e.
6528 // i64 <- f64, i32 <- f32, expand the source operand instead.
6529 MVT VT0 = Node->getOperand(0).getValueType();
6530 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6531 ExpandOp(Node->getOperand(0), Lo, Hi);
6535 // Turn this into a load/store pair by default.
6536 if (Tmp.getNode() == 0)
6537 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
6539 ExpandOp(Tmp, Lo, Hi);
6543 case ISD::READCYCLECOUNTER: {
6544 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6545 TargetLowering::Custom &&
6546 "Must custom expand ReadCycleCounter");
6547 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6548 assert(Tmp.getNode() && "Node must be custom expanded!");
6549 ExpandOp(Tmp.getValue(0), Lo, Hi);
6550 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6551 LegalizeOp(Tmp.getValue(1)));
6555 case ISD::ATOMIC_CMP_SWAP_64: {
6556 // This operation does not need a loop.
6557 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6558 assert(Tmp.getNode() && "Node must be custom expanded!");
6559 ExpandOp(Tmp.getValue(0), Lo, Hi);
6560 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6561 LegalizeOp(Tmp.getValue(1)));
6565 case ISD::ATOMIC_LOAD_ADD_64:
6566 case ISD::ATOMIC_LOAD_SUB_64:
6567 case ISD::ATOMIC_LOAD_AND_64:
6568 case ISD::ATOMIC_LOAD_OR_64:
6569 case ISD::ATOMIC_LOAD_XOR_64:
6570 case ISD::ATOMIC_LOAD_NAND_64:
6571 case ISD::ATOMIC_SWAP_64: {
6572 // These operations require a loop to be generated. We can't do that yet,
6573 // so substitute a target-dependent pseudo and expand that later.
6574 SDValue In2Lo, In2Hi, In2;
6575 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6576 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
6577 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6579 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6580 Anode->getSrcValue(), Anode->getAlignment());
6581 SDValue Result = TLI.LowerOperation(Replace, DAG);
6582 ExpandOp(Result.getValue(0), Lo, Hi);
6583 // Remember that we legalized the chain.
6584 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
6588 // These operators cannot be expanded directly, emit them as calls to
6589 // library functions.
6590 case ISD::FP_TO_SINT: {
6591 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6593 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6594 case Expand: assert(0 && "cannot expand FP!");
6595 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6596 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6599 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6601 // Now that the custom expander is done, expand the result, which is still
6604 ExpandOp(Op, Lo, Hi);
6609 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6611 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6612 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
6616 case ISD::FP_TO_UINT: {
6617 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6619 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6620 case Expand: assert(0 && "cannot expand FP!");
6621 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6622 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6625 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6627 // Now that the custom expander is done, expand the result.
6629 ExpandOp(Op, Lo, Hi);
6634 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6636 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6637 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
6642 // If the target wants custom lowering, do so.
6643 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
6644 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6645 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6646 Op = TLI.LowerOperation(Op, DAG);
6648 // Now that the custom expander is done, expand the result, which is
6650 ExpandOp(Op, Lo, Hi);
6655 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6656 // this X << 1 as X+X.
6657 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6658 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6659 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6660 SDValue LoOps[2], HiOps[3];
6661 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6662 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6663 LoOps[1] = LoOps[0];
6664 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6666 HiOps[1] = HiOps[0];
6667 HiOps[2] = Lo.getValue(1);
6668 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6673 // If we can emit an efficient shift operation, do so now.
6674 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6677 // If this target supports SHL_PARTS, use it.
6678 TargetLowering::LegalizeAction Action =
6679 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6680 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6681 Action == TargetLowering::Custom) {
6682 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6686 // Otherwise, emit a libcall.
6687 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
6692 // If the target wants custom lowering, do so.
6693 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
6694 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6695 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6696 Op = TLI.LowerOperation(Op, DAG);
6698 // Now that the custom expander is done, expand the result, which is
6700 ExpandOp(Op, Lo, Hi);
6705 // If we can emit an efficient shift operation, do so now.
6706 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6709 // If this target supports SRA_PARTS, use it.
6710 TargetLowering::LegalizeAction Action =
6711 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6712 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6713 Action == TargetLowering::Custom) {
6714 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6718 // Otherwise, emit a libcall.
6719 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
6724 // If the target wants custom lowering, do so.
6725 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
6726 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6727 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6728 Op = TLI.LowerOperation(Op, DAG);
6730 // Now that the custom expander is done, expand the result, which is
6732 ExpandOp(Op, Lo, Hi);
6737 // If we can emit an efficient shift operation, do so now.
6738 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6741 // If this target supports SRL_PARTS, use it.
6742 TargetLowering::LegalizeAction Action =
6743 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6744 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6745 Action == TargetLowering::Custom) {
6746 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6750 // Otherwise, emit a libcall.
6751 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
6757 // If the target wants to custom expand this, let them.
6758 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6759 TargetLowering::Custom) {
6760 SDValue Result = TLI.LowerOperation(Op, DAG);
6761 if (Result.getNode()) {
6762 ExpandOp(Result, Lo, Hi);
6766 // Expand the subcomponents.
6767 SDValue LHSL, LHSH, RHSL, RHSH;
6768 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6769 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6770 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6771 SDValue LoOps[2], HiOps[3];
6777 //cascaded check to see if any smaller size has a a carry flag.
6778 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6779 bool hasCarry = false;
6780 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6781 MVT AVT = MVT::getIntegerVT(BitSize);
6782 if (TLI.isOperationLegal(OpV, AVT)) {
6789 if (Node->getOpcode() == ISD::ADD) {
6790 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6791 HiOps[2] = Lo.getValue(1);
6792 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6794 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6795 HiOps[2] = Lo.getValue(1);
6796 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6800 if (Node->getOpcode() == ISD::ADD) {
6801 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6802 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
6803 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6804 Lo, LoOps[0], ISD::SETULT);
6805 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6806 DAG.getConstant(1, NVT),
6807 DAG.getConstant(0, NVT));
6808 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6809 Lo, LoOps[1], ISD::SETULT);
6810 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6811 DAG.getConstant(1, NVT),
6813 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6815 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6816 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6817 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6818 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6819 DAG.getConstant(1, NVT),
6820 DAG.getConstant(0, NVT));
6821 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6829 // Expand the subcomponents.
6830 SDValue LHSL, LHSH, RHSL, RHSH;
6831 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6832 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6833 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6834 SDValue LoOps[2] = { LHSL, RHSL };
6835 SDValue HiOps[3] = { LHSH, RHSH };
6837 if (Node->getOpcode() == ISD::ADDC) {
6838 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6839 HiOps[2] = Lo.getValue(1);
6840 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6842 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6843 HiOps[2] = Lo.getValue(1);
6844 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6846 // Remember that we legalized the flag.
6847 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6852 // Expand the subcomponents.
6853 SDValue LHSL, LHSH, RHSL, RHSH;
6854 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6855 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6856 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6857 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6858 SDValue HiOps[3] = { LHSH, RHSH };
6860 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6861 HiOps[2] = Lo.getValue(1);
6862 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6864 // Remember that we legalized the flag.
6865 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6869 // If the target wants to custom expand this, let them.
6870 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6871 SDValue New = TLI.LowerOperation(Op, DAG);
6872 if (New.getNode()) {
6873 ExpandOp(New, Lo, Hi);
6878 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6879 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
6880 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6881 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6882 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
6883 SDValue LL, LH, RL, RH;
6884 ExpandOp(Node->getOperand(0), LL, LH);
6885 ExpandOp(Node->getOperand(1), RL, RH);
6886 unsigned OuterBitSize = Op.getValueSizeInBits();
6887 unsigned InnerBitSize = RH.getValueSizeInBits();
6888 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6889 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6890 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6891 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6892 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
6893 // The inputs are both zero-extended.
6895 // We can emit a umul_lohi.
6896 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6897 Hi = SDValue(Lo.getNode(), 1);
6901 // We can emit a mulhu+mul.
6902 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6903 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6907 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
6908 // The input values are both sign-extended.
6910 // We can emit a smul_lohi.
6911 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6912 Hi = SDValue(Lo.getNode(), 1);
6916 // We can emit a mulhs+mul.
6917 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6918 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6923 // Lo,Hi = umul LHS, RHS.
6924 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6925 DAG.getVTList(NVT, NVT), LL, RL);
6927 Hi = UMulLOHI.getValue(1);
6928 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6929 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6930 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6931 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6935 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6936 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6937 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6938 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6939 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6940 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6945 // If nothing else, we can make a libcall.
6946 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
6950 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
6953 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
6956 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
6959 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
6963 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6966 RTLIB::ADD_PPCF128),
6970 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6973 RTLIB::SUB_PPCF128),
6977 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6980 RTLIB::MUL_PPCF128),
6984 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6987 RTLIB::DIV_PPCF128),
6990 case ISD::FP_EXTEND: {
6991 if (VT == MVT::ppcf128) {
6992 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6993 Node->getOperand(0).getValueType()==MVT::f64);
6994 const uint64_t zero = 0;
6995 if (Node->getOperand(0).getValueType()==MVT::f32)
6996 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6998 Hi = Node->getOperand(0);
6999 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7002 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7003 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7004 Lo = ExpandLibCall(LC, Node, true, Hi);
7007 case ISD::FP_ROUND: {
7008 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7010 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7011 Lo = ExpandLibCall(LC, Node, true, Hi);
7026 case ISD::FNEARBYINT:
7029 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7030 switch(Node->getOpcode()) {
7032 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7033 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
7036 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7037 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
7040 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7041 RTLIB::COS_F80, RTLIB::COS_PPCF128);
7044 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7045 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7048 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7049 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7052 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7053 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7056 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7057 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7060 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7061 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7064 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7065 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7068 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7069 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7072 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7073 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7076 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7077 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7079 case ISD::FNEARBYINT:
7080 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7081 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7084 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7085 RTLIB::POW_PPCF128);
7088 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7089 RTLIB::POWI_PPCF128);
7091 default: assert(0 && "Unreachable!");
7093 Lo = ExpandLibCall(LC, Node, false, Hi);
7097 if (VT == MVT::ppcf128) {
7099 ExpandOp(Node->getOperand(0), Lo, Tmp);
7100 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7101 // lo = hi==fabs(hi) ? lo : -lo;
7102 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7103 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7104 DAG.getCondCode(ISD::SETEQ));
7107 SDValue Mask = (VT == MVT::f64)
7108 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7109 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7110 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7111 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7112 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7113 if (getTypeAction(NVT) == Expand)
7114 ExpandOp(Lo, Lo, Hi);
7118 if (VT == MVT::ppcf128) {
7119 ExpandOp(Node->getOperand(0), Lo, Hi);
7120 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7121 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7124 SDValue Mask = (VT == MVT::f64)
7125 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7126 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7127 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7128 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7129 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7130 if (getTypeAction(NVT) == Expand)
7131 ExpandOp(Lo, Lo, Hi);
7134 case ISD::FCOPYSIGN: {
7135 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7136 if (getTypeAction(NVT) == Expand)
7137 ExpandOp(Lo, Lo, Hi);
7140 case ISD::SINT_TO_FP:
7141 case ISD::UINT_TO_FP: {
7142 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
7143 MVT SrcVT = Node->getOperand(0).getValueType();
7145 // Promote the operand if needed. Do this before checking for
7146 // ppcf128 so conversions of i16 and i8 work.
7147 if (getTypeAction(SrcVT) == Promote) {
7148 SDValue Tmp = PromoteOp(Node->getOperand(0));
7150 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7151 DAG.getValueType(SrcVT))
7152 : DAG.getZeroExtendInReg(Tmp, SrcVT);
7153 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
7154 SrcVT = Node->getOperand(0).getValueType();
7157 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
7158 static const uint64_t zero = 0;
7160 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7161 Node->getOperand(0)));
7162 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7164 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
7165 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7166 Node->getOperand(0)));
7167 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7168 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7169 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
7170 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7171 DAG.getConstant(0, MVT::i32),
7172 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7174 APFloat(APInt(128, 2, TwoE32)),
7177 DAG.getCondCode(ISD::SETLT)),
7182 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7183 // si64->ppcf128 done by libcall, below
7184 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
7185 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7187 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7188 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7189 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7190 DAG.getConstant(0, MVT::i64),
7191 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7193 APFloat(APInt(128, 2, TwoE64)),
7196 DAG.getCondCode(ISD::SETLT)),
7201 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7202 Node->getOperand(0));
7203 if (getTypeAction(Lo.getValueType()) == Expand)
7204 // float to i32 etc. can be 'expanded' to a single node.
7205 ExpandOp(Lo, Lo, Hi);
7210 // Make sure the resultant values have been legalized themselves, unless this
7211 // is a type that requires multi-step expansion.
7212 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7213 Lo = LegalizeOp(Lo);
7215 // Don't legalize the high part if it is expanded to a single node.
7216 Hi = LegalizeOp(Hi);
7219 // Remember in a map if the values will be reused later.
7221 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7222 assert(isNew && "Value already expanded?!?");
7226 /// SplitVectorOp - Given an operand of vector type, break it down into
7227 /// two smaller values, still of vector type.
7228 void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7230 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
7231 SDNode *Node = Op.getNode();
7232 unsigned NumElements = Op.getValueType().getVectorNumElements();
7233 assert(NumElements > 1 && "Cannot split a single element vector!");
7235 MVT NewEltVT = Op.getValueType().getVectorElementType();
7237 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7238 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7240 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7241 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
7243 // See if we already split it.
7244 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
7245 = SplitNodes.find(Op);
7246 if (I != SplitNodes.end()) {
7247 Lo = I->second.first;
7248 Hi = I->second.second;
7252 switch (Node->getOpcode()) {
7257 assert(0 && "Unhandled operation in SplitVectorOp!");
7259 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7260 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7262 case ISD::BUILD_PAIR:
7263 Lo = Node->getOperand(0);
7264 Hi = Node->getOperand(1);
7266 case ISD::INSERT_VECTOR_ELT: {
7267 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7268 SplitVectorOp(Node->getOperand(0), Lo, Hi);
7269 unsigned Index = Idx->getZExtValue();
7270 SDValue ScalarOp = Node->getOperand(1);
7271 if (Index < NewNumElts_Lo)
7272 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7273 DAG.getIntPtrConstant(Index));
7275 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7276 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7279 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
7280 Node->getOperand(1),
7281 Node->getOperand(2));
7282 SplitVectorOp(Tmp, Lo, Hi);
7285 case ISD::VECTOR_SHUFFLE: {
7286 // Build the low part.
7287 SDValue Mask = Node->getOperand(2);
7288 SmallVector<SDValue, 8> Ops;
7289 MVT PtrVT = TLI.getPointerTy();
7291 // Insert all of the elements from the input that are needed. We use
7292 // buildvector of extractelement here because the input vectors will have
7293 // to be legalized, so this makes the code simpler.
7294 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
7295 SDValue IdxNode = Mask.getOperand(i);
7296 if (IdxNode.getOpcode() == ISD::UNDEF) {
7297 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7300 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
7301 SDValue InVec = Node->getOperand(0);
7302 if (Idx >= NumElements) {
7303 InVec = Node->getOperand(1);
7306 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7307 DAG.getConstant(Idx, PtrVT)));
7309 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7312 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
7313 SDValue IdxNode = Mask.getOperand(i);
7314 if (IdxNode.getOpcode() == ISD::UNDEF) {
7315 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7318 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
7319 SDValue InVec = Node->getOperand(0);
7320 if (Idx >= NumElements) {
7321 InVec = Node->getOperand(1);
7324 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7325 DAG.getConstant(Idx, PtrVT)));
7327 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
7330 case ISD::BUILD_VECTOR: {
7331 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7332 Node->op_begin()+NewNumElts_Lo);
7333 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
7335 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
7337 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
7340 case ISD::CONCAT_VECTORS: {
7341 // FIXME: Handle non-power-of-two vectors?
7342 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7343 if (NewNumSubvectors == 1) {
7344 Lo = Node->getOperand(0);
7345 Hi = Node->getOperand(1);
7347 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7348 Node->op_begin()+NewNumSubvectors);
7349 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
7351 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
7353 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
7357 case ISD::EXTRACT_SUBVECTOR: {
7358 SDValue Vec = Op.getOperand(0);
7359 SDValue Idx = Op.getOperand(1);
7360 MVT IdxVT = Idx.getValueType();
7362 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7363 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7365 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7366 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7369 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7370 DAG.getConstant(NewNumElts_Lo, IdxVT));
7371 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7376 SDValue Cond = Node->getOperand(0);
7378 SDValue LL, LH, RL, RH;
7379 SplitVectorOp(Node->getOperand(1), LL, LH);
7380 SplitVectorOp(Node->getOperand(2), RL, RH);
7382 if (Cond.getValueType().isVector()) {
7383 // Handle a vector merge.
7385 SplitVectorOp(Cond, CL, CH);
7386 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7387 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
7389 // Handle a simple select with vector operands.
7390 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7391 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
7395 case ISD::SELECT_CC: {
7396 SDValue CondLHS = Node->getOperand(0);
7397 SDValue CondRHS = Node->getOperand(1);
7398 SDValue CondCode = Node->getOperand(4);
7400 SDValue LL, LH, RL, RH;
7401 SplitVectorOp(Node->getOperand(2), LL, LH);
7402 SplitVectorOp(Node->getOperand(3), RL, RH);
7404 // Handle a simple select with vector operands.
7405 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7407 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7412 SDValue LL, LH, RL, RH;
7413 SplitVectorOp(Node->getOperand(0), LL, LH);
7414 SplitVectorOp(Node->getOperand(1), RL, RH);
7415 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7416 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7435 SDValue LL, LH, RL, RH;
7436 SplitVectorOp(Node->getOperand(0), LL, LH);
7437 SplitVectorOp(Node->getOperand(1), RL, RH);
7439 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7440 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
7446 SplitVectorOp(Node->getOperand(0), L, H);
7448 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7449 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
7465 case ISD::FP_TO_SINT:
7466 case ISD::FP_TO_UINT:
7467 case ISD::SINT_TO_FP:
7468 case ISD::UINT_TO_FP:
7470 case ISD::ANY_EXTEND:
7471 case ISD::SIGN_EXTEND:
7472 case ISD::ZERO_EXTEND:
7473 case ISD::FP_EXTEND: {
7475 SplitVectorOp(Node->getOperand(0), L, H);
7477 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7478 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
7481 case ISD::CONVERT_RNDSAT: {
7482 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7484 SplitVectorOp(Node->getOperand(0), L, H);
7485 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7486 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7487 SDValue STyOpL = DAG.getValueType(L.getValueType());
7488 SDValue STyOpH = DAG.getValueType(H.getValueType());
7490 SDValue RndOp = Node->getOperand(3);
7491 SDValue SatOp = Node->getOperand(4);
7493 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7494 RndOp, SatOp, CvtCode);
7495 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7496 RndOp, SatOp, CvtCode);
7500 LoadSDNode *LD = cast<LoadSDNode>(Node);
7501 SDValue Ch = LD->getChain();
7502 SDValue Ptr = LD->getBasePtr();
7503 ISD::LoadExtType ExtType = LD->getExtensionType();
7504 const Value *SV = LD->getSrcValue();
7505 int SVOffset = LD->getSrcValueOffset();
7506 MVT MemoryVT = LD->getMemoryVT();
7507 unsigned Alignment = LD->getAlignment();
7508 bool isVolatile = LD->isVolatile();
7510 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7511 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7513 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7514 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7515 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7517 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7518 NewVT_Lo, Ch, Ptr, Offset,
7519 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7520 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
7521 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
7522 DAG.getIntPtrConstant(IncrementSize));
7523 SVOffset += IncrementSize;
7524 Alignment = MinAlign(Alignment, IncrementSize);
7525 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7526 NewVT_Hi, Ch, Ptr, Offset,
7527 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
7529 // Build a factor node to remember that this load is independent of the
7531 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
7534 // Remember that we legalized the chain.
7535 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7538 case ISD::BIT_CONVERT: {
7539 // We know the result is a vector. The input may be either a vector or a
7541 SDValue InOp = Node->getOperand(0);
7542 if (!InOp.getValueType().isVector() ||
7543 InOp.getValueType().getVectorNumElements() == 1) {
7544 // The input is a scalar or single-element vector.
7545 // Lower to a store/load so that it can be split.
7546 // FIXME: this could be improved probably.
7547 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7548 Op.getValueType().getTypeForMVT());
7549 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
7550 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
7552 SDValue St = DAG.getStore(DAG.getEntryNode(),
7554 PseudoSourceValue::getFixedStack(FI), 0);
7555 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
7556 PseudoSourceValue::getFixedStack(FI), 0);
7558 // Split the vector and convert each of the pieces now.
7559 SplitVectorOp(InOp, Lo, Hi);
7560 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7561 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
7566 // Remember in a map if the values will be reused later.
7568 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7569 assert(isNew && "Value already split?!?");
7574 /// ScalarizeVectorOp - Given an operand of single-element vector type
7575 /// (e.g. v1f32), convert it into the equivalent operation that returns a
7576 /// scalar (e.g. f32) value.
7577 SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
7578 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
7579 SDNode *Node = Op.getNode();
7580 MVT NewVT = Op.getValueType().getVectorElementType();
7581 assert(Op.getValueType().getVectorNumElements() == 1);
7583 // See if we already scalarized it.
7584 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
7585 if (I != ScalarizedNodes.end()) return I->second;
7588 switch (Node->getOpcode()) {
7591 Node->dump(&DAG); cerr << "\n";
7593 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7610 Result = DAG.getNode(Node->getOpcode(),
7612 ScalarizeVectorOp(Node->getOperand(0)),
7613 ScalarizeVectorOp(Node->getOperand(1)));
7625 case ISD::FP_TO_SINT:
7626 case ISD::FP_TO_UINT:
7627 case ISD::SINT_TO_FP:
7628 case ISD::UINT_TO_FP:
7629 case ISD::SIGN_EXTEND:
7630 case ISD::ZERO_EXTEND:
7631 case ISD::ANY_EXTEND:
7633 case ISD::FP_EXTEND:
7634 Result = DAG.getNode(Node->getOpcode(),
7636 ScalarizeVectorOp(Node->getOperand(0)));
7638 case ISD::CONVERT_RNDSAT: {
7639 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7640 Result = DAG.getConvertRndSat(NewVT, Op0,
7641 DAG.getValueType(NewVT),
7642 DAG.getValueType(Op0.getValueType()),
7643 Node->getOperand(3),
7644 Node->getOperand(4),
7645 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7650 Result = DAG.getNode(Node->getOpcode(),
7652 ScalarizeVectorOp(Node->getOperand(0)),
7653 Node->getOperand(1));
7656 LoadSDNode *LD = cast<LoadSDNode>(Node);
7657 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7658 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
7659 ISD::LoadExtType ExtType = LD->getExtensionType();
7660 const Value *SV = LD->getSrcValue();
7661 int SVOffset = LD->getSrcValueOffset();
7662 MVT MemoryVT = LD->getMemoryVT();
7663 unsigned Alignment = LD->getAlignment();
7664 bool isVolatile = LD->isVolatile();
7666 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7667 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7669 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7670 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7671 MemoryVT.getVectorElementType(),
7672 isVolatile, Alignment);
7674 // Remember that we legalized the chain.
7675 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7678 case ISD::BUILD_VECTOR:
7679 Result = Node->getOperand(0);
7681 case ISD::INSERT_VECTOR_ELT:
7682 // Returning the inserted scalar element.
7683 Result = Node->getOperand(1);
7685 case ISD::CONCAT_VECTORS:
7686 assert(Node->getOperand(0).getValueType() == NewVT &&
7687 "Concat of non-legal vectors not yet supported!");
7688 Result = Node->getOperand(0);
7690 case ISD::VECTOR_SHUFFLE: {
7691 // Figure out if the scalar is the LHS or RHS and return it.
7692 SDValue EltNum = Node->getOperand(2).getOperand(0);
7693 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
7694 Result = ScalarizeVectorOp(Node->getOperand(1));
7696 Result = ScalarizeVectorOp(Node->getOperand(0));
7699 case ISD::EXTRACT_SUBVECTOR:
7700 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
7701 Node->getOperand(1));
7703 case ISD::BIT_CONVERT: {
7704 SDValue Op0 = Op.getOperand(0);
7705 if (Op0.getValueType().getVectorNumElements() == 1)
7706 Op0 = ScalarizeVectorOp(Op0);
7707 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
7711 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7712 ScalarizeVectorOp(Op.getOperand(1)),
7713 ScalarizeVectorOp(Op.getOperand(2)));
7715 case ISD::SELECT_CC:
7716 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7717 Node->getOperand(1),
7718 ScalarizeVectorOp(Op.getOperand(2)),
7719 ScalarizeVectorOp(Op.getOperand(3)),
7720 Node->getOperand(4));
7723 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7724 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
7725 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7727 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7728 DAG.getConstant(-1ULL, NewVT),
7729 DAG.getConstant(0ULL, NewVT));
7734 if (TLI.isTypeLegal(NewVT))
7735 Result = LegalizeOp(Result);
7736 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7737 assert(isNew && "Value already scalarized?");
7743 SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7744 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7745 if (I != WidenNodes.end()) return I->second;
7747 MVT VT = Op.getValueType();
7748 assert(VT.isVector() && "Cannot widen non-vector type!");
7751 SDNode *Node = Op.getNode();
7752 MVT EVT = VT.getVectorElementType();
7754 unsigned NumElts = VT.getVectorNumElements();
7755 unsigned NewNumElts = WidenVT.getVectorNumElements();
7756 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7757 assert(NewNumElts < 17);
7759 // When widen is called, it is assumed that it is more efficient to use a
7760 // wide type. The default action is to widen to operation to a wider legal
7761 // vector type and then do the operation if it is legal by calling LegalizeOp
7762 // again. If there is no vector equivalent, we will unroll the operation, do
7763 // it, and rebuild the vector. If most of the operations are vectorizible to
7764 // the legal type, the resulting code will be more efficient. If this is not
7765 // the case, the resulting code will preform badly as we end up generating
7766 // code to pack/unpack the results. It is the function that calls widen
7767 // that is responsible for seeing this doesn't happen.
7768 switch (Node->getOpcode()) {
7773 assert(0 && "Unexpected operation in WidenVectorOp!");
7775 case ISD::CopyFromReg:
7776 assert(0 && "CopyFromReg doesn't need widening!");
7778 case ISD::ConstantFP:
7779 // To build a vector of these elements, clients should call BuildVector
7780 // and with each element instead of creating a node with a vector type
7781 assert(0 && "Unexpected operation in WidenVectorOp!");
7783 // Variable Arguments with vector types doesn't make any sense to me
7784 assert(0 && "Unexpected operation in WidenVectorOp!");
7787 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7789 case ISD::BUILD_VECTOR: {
7790 // Build a vector with undefined for the new nodes
7791 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7792 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7793 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7795 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7798 case ISD::INSERT_VECTOR_ELT: {
7799 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7800 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7801 Node->getOperand(1), Node->getOperand(2));
7804 case ISD::VECTOR_SHUFFLE: {
7805 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7806 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7807 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7808 // used as permutation array. We build the vector here instead of widening
7809 // because we don't want to legalize and have it turned to something else.
7810 SDValue PermOp = Node->getOperand(2);
7811 SDValueVector NewOps;
7812 MVT PVT = PermOp.getValueType().getVectorElementType();
7813 for (unsigned i = 0; i < NumElts; ++i) {
7814 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7815 NewOps.push_back(PermOp.getOperand(i));
7818 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7819 if (Idx < NumElts) {
7820 NewOps.push_back(PermOp.getOperand(i));
7823 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7824 PermOp.getOperand(i).getValueType()));
7828 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7829 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7832 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7833 MVT::getVectorVT(PVT, NewOps.size()),
7834 &NewOps[0], NewOps.size());
7836 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7840 // If the load widen returns true, we can use a single load for the
7841 // vector. Otherwise, it is returning a token factor for multiple
7844 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7845 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7847 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7851 case ISD::BIT_CONVERT: {
7852 SDValue Tmp1 = Node->getOperand(0);
7853 // Converts between two different types so we need to determine
7854 // the correct widen type for the input operand.
7855 MVT TVT = Tmp1.getValueType();
7856 assert(TVT.isVector() && "can not widen non vector type");
7857 MVT TEVT = TVT.getVectorElementType();
7858 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7859 "can not widen bit bit convert that are not multiple of element type");
7860 MVT TWidenVT = MVT::getVectorVT(TEVT,
7861 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7862 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7863 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7864 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7866 TargetLowering::LegalizeAction action =
7867 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7869 default: assert(0 && "action not supported");
7870 case TargetLowering::Legal:
7872 case TargetLowering::Promote:
7873 // We defer the promotion to when we legalize the op
7875 case TargetLowering::Expand:
7876 // Expand the operation into a bunch of nasty scalar code.
7877 Result = LegalizeOp(UnrollVectorOp(Result));
7883 case ISD::SINT_TO_FP:
7884 case ISD::UINT_TO_FP:
7885 case ISD::FP_TO_SINT:
7886 case ISD::FP_TO_UINT: {
7887 SDValue Tmp1 = Node->getOperand(0);
7888 // Converts between two different types so we need to determine
7889 // the correct widen type for the input operand.
7890 MVT TVT = Tmp1.getValueType();
7891 assert(TVT.isVector() && "can not widen non vector type");
7892 MVT TEVT = TVT.getVectorElementType();
7893 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7894 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7895 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7896 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7898 TargetLowering::LegalizeAction action =
7899 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7901 default: assert(0 && "action not supported");
7902 case TargetLowering::Legal:
7904 case TargetLowering::Promote:
7905 // We defer the promotion to when we legalize the op
7907 case TargetLowering::Expand:
7908 // Expand the operation into a bunch of nasty scalar code.
7909 Result = LegalizeOp(UnrollVectorOp(Result));
7915 case ISD::FP_EXTEND:
7916 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7918 case ISD::SIGN_EXTEND:
7919 case ISD::ZERO_EXTEND:
7920 case ISD::ANY_EXTEND:
7922 case ISD::SIGN_EXTEND_INREG:
7931 // Unary op widening
7933 TargetLowering::LegalizeAction action =
7934 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7936 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7937 assert(Tmp1.getValueType() == WidenVT);
7938 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7940 default: assert(0 && "action not supported");
7941 case TargetLowering::Legal:
7943 case TargetLowering::Promote:
7944 // We defer the promotion to when we legalize the op
7946 case TargetLowering::Expand:
7947 // Expand the operation into a bunch of nasty scalar code.
7948 Result = LegalizeOp(UnrollVectorOp(Result));
7953 case ISD::CONVERT_RNDSAT: {
7954 SDValue RndOp = Node->getOperand(3);
7955 SDValue SatOp = Node->getOperand(4);
7957 TargetLowering::LegalizeAction action =
7958 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7960 SDValue SrcOp = Node->getOperand(0);
7962 // Converts between two different types so we need to determine
7963 // the correct widen type for the input operand.
7964 MVT SVT = SrcOp.getValueType();
7965 assert(SVT.isVector() && "can not widen non vector type");
7966 MVT SEVT = SVT.getVectorElementType();
7967 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7969 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7970 assert(SrcOp.getValueType() == WidenVT);
7971 SDValue DTyOp = DAG.getValueType(WidenVT);
7972 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
7973 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7975 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
7976 RndOp, SatOp, CvtCode);
7978 default: assert(0 && "action not supported");
7979 case TargetLowering::Legal:
7981 case TargetLowering::Promote:
7982 // We defer the promotion to when we legalize the op
7984 case TargetLowering::Expand:
7985 // Expand the operation into a bunch of nasty scalar code.
7986 Result = LegalizeOp(UnrollVectorOp(Result));
8008 case ISD::FCOPYSIGN:
8012 // Binary op widening
8013 TargetLowering::LegalizeAction action =
8014 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8016 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8017 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8018 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8019 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
8021 default: assert(0 && "action not supported");
8022 case TargetLowering::Legal:
8024 case TargetLowering::Promote:
8025 // We defer the promotion to when we legalize the op
8027 case TargetLowering::Expand:
8028 // Expand the operation into a bunch of nasty scalar code by first
8029 // Widening to the right type and then unroll the beast.
8030 Result = LegalizeOp(UnrollVectorOp(Result));
8039 // Binary op with one non vector operand
8040 TargetLowering::LegalizeAction action =
8041 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8043 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8044 assert(Tmp1.getValueType() == WidenVT);
8045 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
8047 default: assert(0 && "action not supported");
8048 case TargetLowering::Legal:
8050 case TargetLowering::Promote:
8051 // We defer the promotion to when we legalize the op
8053 case TargetLowering::Expand:
8054 // Expand the operation into a bunch of nasty scalar code.
8055 Result = LegalizeOp(UnrollVectorOp(Result));
8060 case ISD::EXTRACT_VECTOR_ELT: {
8061 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8062 assert(Tmp1.getValueType() == WidenVT);
8063 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8066 case ISD::CONCAT_VECTORS: {
8067 // We concurrently support only widen on a multiple of the incoming vector.
8068 // We could widen on a multiple of the incoming operand if necessary.
8069 unsigned NumConcat = NewNumElts / NumElts;
8070 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
8071 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
8072 VT.getVectorElementType()));
8073 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
8074 &UnOps[0], UnOps.size());
8075 SmallVector<SDValue, 8> MOps;
8077 for (unsigned i = 1; i != NumConcat; ++i) {
8078 MOps.push_back(UndefVal);
8080 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8081 &MOps[0], MOps.size()));
8084 case ISD::EXTRACT_SUBVECTOR: {
8085 SDValue Tmp1 = Node->getOperand(0);
8086 SDValue Idx = Node->getOperand(1);
8087 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8088 if (CIdx && CIdx->getZExtValue() == 0) {
8089 // Since we are access the start of the vector, the incoming
8090 // vector type might be the proper.
8091 MVT Tmp1VT = Tmp1.getValueType();
8092 if (Tmp1VT == WidenVT)
8095 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8096 if (Tmp1VTNumElts < NewNumElts)
8097 Result = WidenVectorOp(Tmp1, WidenVT);
8099 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8101 } else if (NewNumElts % NumElts == 0) {
8102 // Widen the extracted subvector.
8103 unsigned NumConcat = NewNumElts / NumElts;
8104 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8105 SmallVector<SDValue, 8> MOps;
8107 for (unsigned i = 1; i != NumConcat; ++i) {
8108 MOps.push_back(UndefVal);
8110 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8111 &MOps[0], MOps.size()));
8113 assert(0 && "can not widen extract subvector");
8114 // This could be implemented using insert and build vector but I would
8115 // like to see when this happens.
8121 TargetLowering::LegalizeAction action =
8122 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8124 // Determine new condition widen type and widen
8125 SDValue Cond1 = Node->getOperand(0);
8126 MVT CondVT = Cond1.getValueType();
8127 assert(CondVT.isVector() && "can not widen non vector type");
8128 MVT CondEVT = CondVT.getVectorElementType();
8129 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8130 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8131 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8133 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8134 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8135 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8136 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
8138 default: assert(0 && "action not supported");
8139 case TargetLowering::Legal:
8141 case TargetLowering::Promote:
8142 // We defer the promotion to when we legalize the op
8144 case TargetLowering::Expand:
8145 // Expand the operation into a bunch of nasty scalar code by first
8146 // Widening to the right type and then unroll the beast.
8147 Result = LegalizeOp(UnrollVectorOp(Result));
8153 case ISD::SELECT_CC: {
8154 TargetLowering::LegalizeAction action =
8155 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8157 // Determine new condition widen type and widen
8158 SDValue Cond1 = Node->getOperand(0);
8159 SDValue Cond2 = Node->getOperand(1);
8160 MVT CondVT = Cond1.getValueType();
8161 assert(CondVT.isVector() && "can not widen non vector type");
8162 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8163 MVT CondEVT = CondVT.getVectorElementType();
8164 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8165 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8166 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8167 assert(Cond1.getValueType() == CondWidenVT &&
8168 Cond2.getValueType() == CondWidenVT && "condition not widen");
8170 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8171 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8172 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8173 "operands not widen");
8174 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8175 Tmp2, Node->getOperand(4));
8177 default: assert(0 && "action not supported");
8178 case TargetLowering::Legal:
8180 case TargetLowering::Promote:
8181 // We defer the promotion to when we legalize the op
8183 case TargetLowering::Expand:
8184 // Expand the operation into a bunch of nasty scalar code by first
8185 // Widening to the right type and then unroll the beast.
8186 Result = LegalizeOp(UnrollVectorOp(Result));
8192 // Determine widen for the operand
8193 SDValue Tmp1 = Node->getOperand(0);
8194 MVT TmpVT = Tmp1.getValueType();
8195 assert(TmpVT.isVector() && "can not widen non vector type");
8196 MVT TmpEVT = TmpVT.getVectorElementType();
8197 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8198 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8199 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8200 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8201 Node->getOperand(2));
8204 case ISD::ATOMIC_CMP_SWAP_8:
8205 case ISD::ATOMIC_CMP_SWAP_16:
8206 case ISD::ATOMIC_CMP_SWAP_32:
8207 case ISD::ATOMIC_CMP_SWAP_64:
8208 case ISD::ATOMIC_LOAD_ADD_8:
8209 case ISD::ATOMIC_LOAD_SUB_8:
8210 case ISD::ATOMIC_LOAD_AND_8:
8211 case ISD::ATOMIC_LOAD_OR_8:
8212 case ISD::ATOMIC_LOAD_XOR_8:
8213 case ISD::ATOMIC_LOAD_NAND_8:
8214 case ISD::ATOMIC_LOAD_MIN_8:
8215 case ISD::ATOMIC_LOAD_MAX_8:
8216 case ISD::ATOMIC_LOAD_UMIN_8:
8217 case ISD::ATOMIC_LOAD_UMAX_8:
8218 case ISD::ATOMIC_SWAP_8:
8219 case ISD::ATOMIC_LOAD_ADD_16:
8220 case ISD::ATOMIC_LOAD_SUB_16:
8221 case ISD::ATOMIC_LOAD_AND_16:
8222 case ISD::ATOMIC_LOAD_OR_16:
8223 case ISD::ATOMIC_LOAD_XOR_16:
8224 case ISD::ATOMIC_LOAD_NAND_16:
8225 case ISD::ATOMIC_LOAD_MIN_16:
8226 case ISD::ATOMIC_LOAD_MAX_16:
8227 case ISD::ATOMIC_LOAD_UMIN_16:
8228 case ISD::ATOMIC_LOAD_UMAX_16:
8229 case ISD::ATOMIC_SWAP_16:
8230 case ISD::ATOMIC_LOAD_ADD_32:
8231 case ISD::ATOMIC_LOAD_SUB_32:
8232 case ISD::ATOMIC_LOAD_AND_32:
8233 case ISD::ATOMIC_LOAD_OR_32:
8234 case ISD::ATOMIC_LOAD_XOR_32:
8235 case ISD::ATOMIC_LOAD_NAND_32:
8236 case ISD::ATOMIC_LOAD_MIN_32:
8237 case ISD::ATOMIC_LOAD_MAX_32:
8238 case ISD::ATOMIC_LOAD_UMIN_32:
8239 case ISD::ATOMIC_LOAD_UMAX_32:
8240 case ISD::ATOMIC_SWAP_32:
8241 case ISD::ATOMIC_LOAD_ADD_64:
8242 case ISD::ATOMIC_LOAD_SUB_64:
8243 case ISD::ATOMIC_LOAD_AND_64:
8244 case ISD::ATOMIC_LOAD_OR_64:
8245 case ISD::ATOMIC_LOAD_XOR_64:
8246 case ISD::ATOMIC_LOAD_NAND_64:
8247 case ISD::ATOMIC_LOAD_MIN_64:
8248 case ISD::ATOMIC_LOAD_MAX_64:
8249 case ISD::ATOMIC_LOAD_UMIN_64:
8250 case ISD::ATOMIC_LOAD_UMAX_64:
8251 case ISD::ATOMIC_SWAP_64: {
8252 // For now, we assume that using vectors for these operations don't make
8253 // much sense so we just split it. We return an empty result
8255 SplitVectorOp(Op, X, Y);
8260 } // end switch (Node->getOpcode())
8262 assert(Result.getNode() && "Didn't set a result!");
8264 Result = LegalizeOp(Result);
8266 AddWidenedOperand(Op, Result);
8270 // Utility function to find a legal vector type and its associated element
8271 // type from a preferred width and whose vector type must be the same size
8273 // TLI: Target lowering used to determine legal types
8274 // Width: Preferred width of element type
8275 // VVT: Vector value type whose size we must match.
8276 // Returns VecEVT and EVT - the vector type and its associated element type
8277 static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8278 MVT& EVT, MVT& VecEVT) {
8279 // We start with the preferred width, make it a power of 2 and see if
8280 // we can find a vector type of that width. If not, we reduce it by
8281 // another power of 2. If we have widen the type, a vector of bytes should
8283 assert(TLI.isTypeLegal(VVT));
8284 unsigned EWidth = Width + 1;
8287 EWidth = (1 << Log2_32(EWidth-1));
8288 EVT = MVT::getIntegerVT(EWidth);
8289 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8290 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8291 } while (!TLI.isTypeLegal(VecEVT) ||
8292 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8295 SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8304 // We assume that we have good rules to handle loading power of two loads so
8305 // we break down the operations to power of 2 loads. The strategy is to
8306 // load the largest power of 2 that we can easily transform to a legal vector
8307 // and then insert into that vector, and the cast the result into the legal
8308 // vector that we want. This avoids unnecessary stack converts.
8309 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8310 // the load is nonvolatile, we an use a wider load for the value.
8311 // Find a vector length we can load a large chunk
8314 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8315 EVTWidth = EVT.getSizeInBits();
8317 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8318 isVolatile, Alignment);
8319 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8320 LdChain.push_back(LdOp.getValue(1));
8322 // Check if we can load the element with one instruction
8323 if (LdWidth == EVTWidth) {
8324 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8327 // The vector element order is endianness dependent.
8329 LdWidth -= EVTWidth;
8330 unsigned Offset = 0;
8332 while (LdWidth > 0) {
8333 unsigned Increment = EVTWidth / 8;
8334 Offset += Increment;
8335 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8336 DAG.getIntPtrConstant(Increment));
8338 if (LdWidth < EVTWidth) {
8339 // Our current type we are using is too large, use a smaller size by
8340 // using a smaller power of 2
8341 unsigned oEVTWidth = EVTWidth;
8342 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8343 EVTWidth = EVT.getSizeInBits();
8344 // Readjust position and vector position based on new load type
8345 Idx = Idx * (oEVTWidth/EVTWidth);
8346 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8349 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8350 SVOffset+Offset, isVolatile,
8351 MinAlign(Alignment, Offset));
8352 LdChain.push_back(LdOp.getValue(1));
8353 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8354 DAG.getIntPtrConstant(Idx++));
8356 LdWidth -= EVTWidth;
8359 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8362 bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8366 // TODO: Add support for ConcatVec and the ability to load many vector
8367 // types (e.g., v4i8). This will not work when a vector register
8368 // to memory mapping is strange (e.g., vector elements are not
8369 // stored in some sequential order).
8371 // It must be true that the widen vector type is bigger than where
8372 // we need to load from.
8373 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8374 MVT LdVT = LD->getMemoryVT();
8375 assert(LdVT.isVector() && NVT.isVector());
8376 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8379 SDValue Chain = LD->getChain();
8380 SDValue BasePtr = LD->getBasePtr();
8381 int SVOffset = LD->getSrcValueOffset();
8382 unsigned Alignment = LD->getAlignment();
8383 bool isVolatile = LD->isVolatile();
8384 const Value *SV = LD->getSrcValue();
8385 unsigned int LdWidth = LdVT.getSizeInBits();
8387 // Load value as a large register
8388 SDValueVector LdChain;
8389 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8390 Alignment, isVolatile, LdWidth, NVT);
8392 if (LdChain.size() == 1) {
8397 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8403 void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8412 // Breaks the stores into a series of power of 2 width stores. For any
8413 // width, we convert the vector to the vector of element size that we
8414 // want to store. This avoids requiring a stack convert.
8416 // Find a width of the element type we can store with
8417 MVT VVT = ValOp.getValueType();
8420 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8421 EVTWidth = EVT.getSizeInBits();
8423 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8424 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8425 DAG.getIntPtrConstant(0));
8426 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8427 isVolatile, Alignment);
8428 StChain.push_back(StOp);
8430 // Check if we are done
8431 if (StWidth == EVTWidth) {
8436 StWidth -= EVTWidth;
8437 unsigned Offset = 0;
8439 while (StWidth > 0) {
8440 unsigned Increment = EVTWidth / 8;
8441 Offset += Increment;
8442 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8443 DAG.getIntPtrConstant(Increment));
8445 if (StWidth < EVTWidth) {
8446 // Our current type we are using is too large, use a smaller size by
8447 // using a smaller power of 2
8448 unsigned oEVTWidth = EVTWidth;
8449 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8450 EVTWidth = EVT.getSizeInBits();
8451 // Readjust position and vector position based on new load type
8452 Idx = Idx * (oEVTWidth/EVTWidth);
8453 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8456 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8457 DAG.getIntPtrConstant(Idx++));
8458 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8459 SVOffset + Offset, isVolatile,
8460 MinAlign(Alignment, Offset)));
8461 StWidth -= EVTWidth;
8466 SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8469 // TODO: It might be cleaner if we can use SplitVector and have more legal
8470 // vector types that can be stored into memory (e.g., v4xi8 can
8471 // be stored as a word). This will not work when a vector register
8472 // to memory mapping is strange (e.g., vector elements are not
8473 // stored in some sequential order).
8475 MVT StVT = ST->getMemoryVT();
8476 SDValue ValOp = ST->getValue();
8478 // Check if we have widen this node with another value
8479 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8480 if (I != WidenNodes.end())
8483 MVT VVT = ValOp.getValueType();
8485 // It must be true that we the widen vector type is bigger than where
8486 // we need to store.
8487 assert(StVT.isVector() && VVT.isVector());
8488 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8489 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8492 SDValueVector StChain;
8493 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8494 ST->getSrcValueOffset(), ST->getAlignment(),
8495 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8496 if (StChain.size() == 1)
8499 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8503 // SelectionDAG::Legalize - This is the entry point for the file.
8505 void SelectionDAG::Legalize() {
8506 /// run - This is the main entry point to this class.
8508 SelectionDAGLegalize(*this).LegalizeDAG();