1 //===-- LegalizeDAGTypes.cpp - Implement SelectionDAG::LegalizeTypes ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SelectionDAG::LegalizeTypes method. It transforms
11 // an arbitrary well-formed SelectionDAG to only consist of legal types.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "legalize-types"
16 #include "llvm/CodeGen/SelectionDAG.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Target/TargetLowering.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
25 //===----------------------------------------------------------------------===//
26 /// DAGTypeLegalizer - This takes an arbitrary SelectionDAG as input and
27 /// hacks on it until the target machine can handle it. This involves
28 /// eliminating value sizes the machine cannot handle (promoting small sizes to
29 /// large sizes or splitting up large values into small values) as well as
30 /// eliminating operations the machine cannot handle.
32 /// This code also does a small amount of optimization and recognition of idioms
33 /// as part of its processing. For example, if a target does not support a
34 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35 /// will attempt merge setcc and brc instructions into brcc's.
38 class VISIBILITY_HIDDEN DAGTypeLegalizer {
42 // NodeIDFlags - This pass uses the NodeID on the SDNodes to hold information
43 // about the state of the node. The enum has all the values.
45 /// ReadyToProcess - All operands have been processed, so this node is ready
49 /// NewNode - This is a new node that was created in the process of
50 /// legalizing some other node.
53 /// Processed - This is a node that has already been processed.
56 // 1+ - This is a node which has this many unlegalized operands.
60 Legal, // The target natively supports this type.
61 Promote, // This type should be executed in a larger type.
62 Expand // This type should be split into two types of half the size.
65 /// ValueTypeActions - This is a bitvector that contains two bits for each
66 /// simple value type, where the two bits correspond to the LegalizeAction
67 /// enum. This can be queried with "getTypeAction(VT)".
68 TargetLowering::ValueTypeActionImpl ValueTypeActions;
70 /// getTypeAction - Return how we should legalize values of this type, either
71 /// it is already legal or we need to expand it into multiple registers of
72 /// smaller integer type, or we need to promote it to a larger type.
73 LegalizeAction getTypeAction(MVT::ValueType VT) const {
74 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
77 /// isTypeLegal - Return true if this type is legal on this target.
79 bool isTypeLegal(MVT::ValueType VT) const {
80 return getTypeAction(VT) == Legal;
83 SDOperand getIntPtrConstant(uint64_t Val) {
84 return DAG.getConstant(Val, TLI.getPointerTy());
87 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use.
89 DenseMap<SDOperand, SDOperand> PromotedNodes;
91 /// ExpandedNodes - For nodes that need to be expanded this map indicates
92 /// which operands are the expanded version of the input.
93 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
95 /// Worklist - This defines a worklist of nodes to process. In order to be
96 /// pushed onto this worklist, all operands of a node must have already been
98 SmallVector<SDNode*, 128> Worklist;
101 DAGTypeLegalizer(SelectionDAG &dag)
102 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
103 ValueTypeActions(TLI.getValueTypeActions()) {
104 assert(MVT::LAST_VALUETYPE <= 32 &&
105 "Too many value types for ValueTypeActions to hold!");
111 void MarkNewNodes(SDNode *N);
113 void ReplaceLegalValueWith(SDOperand From, SDOperand To);
115 SDOperand GetPromotedOp(SDOperand Op) {
116 Op = PromotedNodes[Op];
117 assert(Op.Val && "Operand wasn't promoted?");
120 void SetPromotedOp(SDOperand Op, SDOperand Result);
122 /// GetPromotedZExtOp - Get a promoted operand and zero extend it to the final
124 SDOperand GetPromotedZExtOp(SDOperand Op) {
125 MVT::ValueType OldVT = Op.getValueType();
126 Op = GetPromotedOp(Op);
127 return DAG.getZeroExtendInReg(Op, OldVT);
130 void GetExpandedOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
131 void SetExpandedOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
134 SDOperand CreateStackStoreLoad(SDOperand Op, MVT::ValueType DestVT);
135 SDOperand HandleMemIntrinsic(SDNode *N);
136 void SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
139 void PromoteResult(SDNode *N, unsigned ResNo);
140 SDOperand PromoteResult_UNDEF(SDNode *N);
141 SDOperand PromoteResult_Constant(SDNode *N);
142 SDOperand PromoteResult_TRUNCATE(SDNode *N);
143 SDOperand PromoteResult_INT_EXTEND(SDNode *N);
144 SDOperand PromoteResult_FP_ROUND(SDNode *N);
145 SDOperand PromoteResult_FP_TO_XINT(SDNode *N);
146 SDOperand PromoteResult_SETCC(SDNode *N);
147 SDOperand PromoteResult_LOAD(LoadSDNode *N);
148 SDOperand PromoteResult_SimpleIntBinOp(SDNode *N);
149 SDOperand PromoteResult_SHL(SDNode *N);
150 SDOperand PromoteResult_SRA(SDNode *N);
151 SDOperand PromoteResult_SRL(SDNode *N);
152 SDOperand PromoteResult_SELECT (SDNode *N);
153 SDOperand PromoteResult_SELECT_CC(SDNode *N);
156 void ExpandResult(SDNode *N, unsigned ResNo);
157 void ExpandResult_UNDEF (SDNode *N, SDOperand &Lo, SDOperand &Hi);
158 void ExpandResult_Constant (SDNode *N, SDOperand &Lo, SDOperand &Hi);
159 void ExpandResult_BUILD_PAIR (SDNode *N, SDOperand &Lo, SDOperand &Hi);
160 void ExpandResult_ANY_EXTEND (SDNode *N, SDOperand &Lo, SDOperand &Hi);
161 void ExpandResult_ZERO_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
162 void ExpandResult_SIGN_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
163 void ExpandResult_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
164 void ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi);
165 void ExpandResult_LOAD (LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
167 void ExpandResult_Logical (SDNode *N, SDOperand &Lo, SDOperand &Hi);
168 void ExpandResult_BSWAP (SDNode *N, SDOperand &Lo, SDOperand &Hi);
169 void ExpandResult_ADDSUB (SDNode *N, SDOperand &Lo, SDOperand &Hi);
170 void ExpandResult_ADDSUBC (SDNode *N, SDOperand &Lo, SDOperand &Hi);
171 void ExpandResult_ADDSUBE (SDNode *N, SDOperand &Lo, SDOperand &Hi);
172 void ExpandResult_SELECT (SDNode *N, SDOperand &Lo, SDOperand &Hi);
173 void ExpandResult_SELECT_CC (SDNode *N, SDOperand &Lo, SDOperand &Hi);
174 void ExpandResult_MUL (SDNode *N, SDOperand &Lo, SDOperand &Hi);
175 void ExpandResult_Shift (SDNode *N, SDOperand &Lo, SDOperand &Hi);
177 void ExpandShiftByConstant(SDNode *N, unsigned Amt,
178 SDOperand &Lo, SDOperand &Hi);
179 bool ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi);
181 // Operand Promotion.
182 bool PromoteOperand(SDNode *N, unsigned OperandNo);
183 SDOperand PromoteOperand_ANY_EXTEND(SDNode *N);
184 SDOperand PromoteOperand_ZERO_EXTEND(SDNode *N);
185 SDOperand PromoteOperand_SIGN_EXTEND(SDNode *N);
186 SDOperand PromoteOperand_TRUNCATE(SDNode *N);
187 SDOperand PromoteOperand_FP_EXTEND(SDNode *N);
188 SDOperand PromoteOperand_FP_ROUND(SDNode *N);
189 SDOperand PromoteOperand_INT_TO_FP(SDNode *N);
190 SDOperand PromoteOperand_SELECT(SDNode *N, unsigned OpNo);
191 SDOperand PromoteOperand_BRCOND(SDNode *N, unsigned OpNo);
192 SDOperand PromoteOperand_BR_CC(SDNode *N, unsigned OpNo);
193 SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
195 void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
197 // Operand Expansion.
198 bool ExpandOperand(SDNode *N, unsigned OperandNo);
199 SDOperand ExpandOperand_TRUNCATE(SDNode *N);
200 SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
201 SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
202 SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
203 SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
204 SDOperand ExpandOperand_SETCC(SDNode *N);
205 SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
207 void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
208 ISD::CondCode &CCCode);
210 } // end anonymous namespace
214 /// run - This is the main entry point for the type legalizer. This does a
215 /// top-down traversal of the dag, legalizing types as it goes.
216 void DAGTypeLegalizer::run() {
217 // Create a dummy node (which is not added to allnodes), that adds a reference
218 // to the root node, preventing it from being deleted, and tracking any
219 // changes of the root.
220 HandleSDNode Dummy(DAG.getRoot());
222 // The root of the dag may dangle to deleted nodes until the type legalizer is
223 // done. Set it to null to avoid confusion.
224 DAG.setRoot(SDOperand());
226 // Walk all nodes in the graph, assigning them a NodeID of 'ReadyToProcess'
227 // (and remembering them) if they are leaves and assigning 'NewNode' if
229 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
230 E = DAG.allnodes_end(); I != E; ++I) {
231 if (I->getNumOperands() == 0) {
232 I->setNodeId(ReadyToProcess);
233 Worklist.push_back(I);
235 I->setNodeId(NewNode);
239 // Now that we have a set of nodes to process, handle them all.
240 while (!Worklist.empty()) {
241 SDNode *N = Worklist.back();
243 assert(N->getNodeId() == ReadyToProcess &&
244 "Node should be ready if on worklist!");
246 // Scan the values produced by the node, checking to see if any result
247 // types are illegal.
249 unsigned NumResults = N->getNumValues();
251 LegalizeAction Action = getTypeAction(N->getValueType(i));
252 if (Action == Promote) {
255 } else if (Action == Expand) {
259 assert(Action == Legal && "Unknown action!");
261 } while (++i < NumResults);
263 // Scan the operand list for the node, handling any nodes with operands that
266 unsigned NumOperands = N->getNumOperands();
267 bool NeedsRevisit = false;
268 for (i = 0; i != NumOperands; ++i) {
269 LegalizeAction Action = getTypeAction(N->getOperand(i).getValueType());
270 if (Action == Promote) {
271 NeedsRevisit = PromoteOperand(N, i);
273 } else if (Action == Expand) {
274 NeedsRevisit = ExpandOperand(N, i);
277 assert(Action == Legal && "Unknown action!");
281 // If the node needs revisiting, don't add all users to the worklist etc.
285 if (i == NumOperands)
286 DEBUG(cerr << "Legally typed node: "; N->dump(&DAG); cerr << "\n");
290 // If we reach here, the node was processed, potentially creating new nodes.
291 // Mark it as processed and add its users to the worklist as appropriate.
292 N->setNodeId(Processed);
294 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
297 int NodeID = User->getNodeId();
298 assert(NodeID != ReadyToProcess && NodeID != Processed &&
299 "Invalid node id for user of unprocessed node!");
301 // This node has two options: it can either be a new node or its Node ID
302 // may be a count of the number of operands it has that are not ready.
304 User->setNodeId(NodeID-1);
306 // If this was the last use it was waiting on, add it to the ready list.
307 if (NodeID-1 == ReadyToProcess)
308 Worklist.push_back(User);
312 // Otherwise, this node is new: this is the first operand of it that
313 // became ready. Its new NodeID is the number of operands it has minus 1
314 // (as this node is now processed).
315 assert(NodeID == NewNode && "Unknown node ID!");
316 User->setNodeId(User->getNumOperands()-1);
318 // If the node only has a single operand, it is now ready.
319 if (User->getNumOperands() == 1)
320 Worklist.push_back(User);
324 // If the root changed (e.g. it was a dead load, update the root).
325 DAG.setRoot(Dummy.getValue());
329 // Remove dead nodes. This is important to do for cleanliness but also before
330 // the checking loop below. Implicit folding by the DAG.getNode operators can
331 // cause unreachable nodes to be around with their flags set to new.
332 DAG.RemoveDeadNodes();
334 // In a debug build, scan all the nodes to make sure we found them all. This
335 // ensures that there are no cycles and that everything got processed.
337 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
338 E = DAG.allnodes_end(); I != E; ++I) {
339 if (I->getNodeId() == Processed)
341 cerr << "Unprocessed node: ";
342 I->dump(&DAG); cerr << "\n";
344 if (I->getNodeId() == NewNode)
345 cerr << "New node not 'noticed'?\n";
346 else if (I->getNodeId() > 0)
347 cerr << "Operand not processed?\n";
348 else if (I->getNodeId() == ReadyToProcess)
349 cerr << "Not added to worklist?\n";
355 /// MarkNewNodes - The specified node is the root of a subtree of potentially
356 /// new nodes. Add the correct NodeId to mark it.
357 void DAGTypeLegalizer::MarkNewNodes(SDNode *N) {
358 // If this was an existing node that is already done, we're done.
359 if (N->getNodeId() != NewNode)
362 // Okay, we know that this node is new. Recursively walk all of its operands
363 // to see if they are new also. The depth of this walk is bounded by the size
364 // of the new tree that was constructed (usually 2-3 nodes), so we don't worry
365 // about revisiting of nodes.
367 // As we walk the operands, keep track of the number of nodes that are
368 // processed. If non-zero, this will become the new nodeid of this node.
369 unsigned NumProcessed = 0;
370 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
371 int OpId = N->getOperand(i).Val->getNodeId();
373 MarkNewNodes(N->getOperand(i).Val);
374 else if (OpId == Processed)
378 N->setNodeId(N->getNumOperands()-NumProcessed);
379 if (N->getNodeId() == ReadyToProcess)
380 Worklist.push_back(N);
383 /// ReplaceLegalValueWith - The specified value with a legal type was legalized
384 /// to the specified other value. If they are different, update the DAG and
385 /// NodeIDs replacing any uses of From to use To instead.
386 void DAGTypeLegalizer::ReplaceLegalValueWith(SDOperand From, SDOperand To) {
387 if (From == To) return;
389 // If expansion produced new nodes, make sure they are properly marked.
390 if (To.Val->getNodeId() == NewNode)
391 MarkNewNodes(To.Val);
393 // Anything that used the old node should now use the new one. Note that this
394 // can potentially cause recursive merging.
395 DAG.ReplaceAllUsesOfValueWith(From, To);
397 // Since we just made an unstructured update to the DAG, which could wreak
398 // general havoc on anything that once used N and now uses Res, walk all users
399 // of the result, updating their flags.
400 for (SDNode::use_iterator I = To.Val->use_begin(), E = To.Val->use_end();
403 // If the node isn't already processed or in the worklist, mark it as new,
404 // then use MarkNewNodes to recompute its ID.
405 int NodeId = User->getNodeId();
406 if (NodeId != ReadyToProcess && NodeId != Processed) {
407 User->setNodeId(NewNode);
413 void DAGTypeLegalizer::SetPromotedOp(SDOperand Op, SDOperand Result) {
414 if (Result.Val->getNodeId() == NewNode)
415 MarkNewNodes(Result.Val);
417 SDOperand &OpEntry = PromotedNodes[Op];
418 assert(OpEntry.Val == 0 && "Node is already promoted!");
422 void DAGTypeLegalizer::GetExpandedOp(SDOperand Op, SDOperand &Lo,
424 std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
425 assert(Entry.first.Val && "Operand isn't expanded");
430 void DAGTypeLegalizer::SetExpandedOp(SDOperand Op, SDOperand Lo,
432 // Remember that this is the result of the node.
433 std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
434 assert(Entry.first.Val == 0 && "Node already expanded");
438 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
439 if (Lo.Val->getNodeId() == NewNode)
440 MarkNewNodes(Lo.Val);
441 if (Hi.Val->getNodeId() == NewNode)
442 MarkNewNodes(Hi.Val);
445 SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op,
446 MVT::ValueType DestVT) {
447 // Create the stack frame object.
448 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
450 // Emit a store to the stack slot.
451 SDOperand Store = DAG.getStore(DAG.getEntryNode(), Op, FIPtr, NULL, 0);
452 // Result is a load from the stack slot.
453 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
457 /// HandleMemIntrinsic - This handles memcpy/memset/memmove with invalid
458 /// operands. This promotes or expands the operands as required.
459 SDOperand DAGTypeLegalizer::HandleMemIntrinsic(SDNode *N) {
460 // The chain and pointer [operands #0 and #1] are always valid types.
461 SDOperand Chain = N->getOperand(0);
462 SDOperand Ptr = N->getOperand(1);
463 SDOperand Op2 = N->getOperand(2);
465 // Op #2 is either a value (memset) or a pointer. Promote it if required.
466 switch (getTypeAction(Op2.getValueType())) {
467 default: assert(0 && "Unknown action for pointer/value operand");
469 case Promote: Op2 = GetPromotedOp(Op2); break;
472 // The length could have any action required.
473 SDOperand Length = N->getOperand(3);
474 switch (getTypeAction(Length.getValueType())) {
475 default: assert(0 && "Unknown action for memop operand");
477 case Promote: Length = GetPromotedZExtOp(Length); break;
479 SDOperand Dummy; // discard the high part.
480 GetExpandedOp(Length, Length, Dummy);
484 SDOperand Align = N->getOperand(4);
485 switch (getTypeAction(Align.getValueType())) {
486 default: assert(0 && "Unknown action for memop operand");
488 case Promote: Align = GetPromotedZExtOp(Align); break;
491 SDOperand AlwaysInline = N->getOperand(5);
492 switch (getTypeAction(AlwaysInline.getValueType())) {
493 default: assert(0 && "Unknown action for memop operand");
495 case Promote: AlwaysInline = GetPromotedZExtOp(AlwaysInline); break;
498 SDOperand Ops[] = { Chain, Ptr, Op2, Length, Align, AlwaysInline };
499 return DAG.UpdateNodeOperands(SDOperand(N, 0), Ops, 6);
502 /// SplitOp - Return the lower and upper halves of Op's bits in a value type
503 /// half the size of Op's.
504 void DAGTypeLegalizer::SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi) {
505 unsigned NVTBits = MVT::getSizeInBits(Op.getValueType())/2;
506 assert(MVT::getSizeInBits(Op.getValueType()) == 2*NVTBits &&
507 "Cannot split odd sized integer type");
508 MVT::ValueType NVT = MVT::getIntegerType(NVTBits);
509 Lo = DAG.getNode(ISD::TRUNCATE, NVT, Op);
510 Hi = DAG.getNode(ISD::SRL, Op.getValueType(), Op,
511 DAG.getConstant(NVTBits, TLI.getShiftAmountTy()));
512 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
516 //===----------------------------------------------------------------------===//
518 //===----------------------------------------------------------------------===//
520 /// PromoteResult - This method is called when a result of a node is found to be
521 /// in need of promotion to a larger type. At this point, the node may also
522 /// have invalid operands or may have other results that need expansion, we just
523 /// know that (at least) one result needs promotion.
524 void DAGTypeLegalizer::PromoteResult(SDNode *N, unsigned ResNo) {
525 DEBUG(cerr << "Promote node result: "; N->dump(&DAG); cerr << "\n");
526 SDOperand Result = SDOperand();
528 switch (N->getOpcode()) {
531 cerr << "PromoteResult #" << ResNo << ": ";
532 N->dump(&DAG); cerr << "\n";
534 assert(0 && "Do not know how to promote this operator!");
536 case ISD::UNDEF: Result = PromoteResult_UNDEF(N); break;
537 case ISD::Constant: Result = PromoteResult_Constant(N); break;
539 case ISD::TRUNCATE: Result = PromoteResult_TRUNCATE(N); break;
540 case ISD::SIGN_EXTEND:
541 case ISD::ZERO_EXTEND:
542 case ISD::ANY_EXTEND: Result = PromoteResult_INT_EXTEND(N); break;
543 case ISD::FP_ROUND: Result = PromoteResult_FP_ROUND(N); break;
544 case ISD::FP_TO_SINT:
545 case ISD::FP_TO_UINT: Result = PromoteResult_FP_TO_XINT(N); break;
546 case ISD::SETCC: Result = PromoteResult_SETCC(N); break;
547 case ISD::LOAD: Result = PromoteResult_LOAD(cast<LoadSDNode>(N)); break;
554 case ISD::MUL: Result = PromoteResult_SimpleIntBinOp(N); break;
556 case ISD::SHL: Result = PromoteResult_SHL(N); break;
557 case ISD::SRA: Result = PromoteResult_SRA(N); break;
558 case ISD::SRL: Result = PromoteResult_SRL(N); break;
560 case ISD::SELECT: Result = PromoteResult_SELECT(N); break;
561 case ISD::SELECT_CC: Result = PromoteResult_SELECT_CC(N); break;
565 // If Result is null, the sub-method took care of registering the result.
567 SetPromotedOp(SDOperand(N, ResNo), Result);
570 SDOperand DAGTypeLegalizer::PromoteResult_UNDEF(SDNode *N) {
571 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
574 SDOperand DAGTypeLegalizer::PromoteResult_Constant(SDNode *N) {
575 MVT::ValueType VT = N->getValueType(0);
576 // Zero extend things like i1, sign extend everything else. It shouldn't
577 // matter in theory which one we pick, but this tends to give better code?
578 unsigned Opc = VT != MVT::i1 ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
579 SDOperand Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
581 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
585 SDOperand DAGTypeLegalizer::PromoteResult_TRUNCATE(SDNode *N) {
588 switch (getTypeAction(N->getOperand(0).getValueType())) {
589 default: assert(0 && "Unknown type action!");
592 Res = N->getOperand(0);
595 Res = GetPromotedOp(N->getOperand(0));
599 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
600 assert(MVT::getSizeInBits(Res.getValueType()) >= MVT::getSizeInBits(NVT) &&
601 "Truncation doesn't make sense!");
602 if (Res.getValueType() == NVT)
605 // Truncate to NVT instead of VT
606 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
609 SDOperand DAGTypeLegalizer::PromoteResult_INT_EXTEND(SDNode *N) {
610 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
612 if (getTypeAction(N->getOperand(0).getValueType()) == Promote) {
613 SDOperand Res = GetPromotedOp(N->getOperand(0));
614 assert(MVT::getSizeInBits(Res.getValueType()) <= MVT::getSizeInBits(NVT) &&
615 "Extension doesn't make sense!");
617 // If the result and operand types are the same after promotion, simplify
618 // to an in-register extension.
619 if (NVT == Res.getValueType()) {
620 // The high bits are not guaranteed to be anything. Insert an extend.
621 if (N->getOpcode() == ISD::SIGN_EXTEND)
622 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
623 DAG.getValueType(N->getOperand(0).getValueType()));
624 if (N->getOpcode() == ISD::ZERO_EXTEND)
625 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
626 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
631 // Otherwise, just extend the original operand all the way to the larger type.
632 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
635 SDOperand DAGTypeLegalizer::PromoteResult_FP_ROUND(SDNode *N) {
636 // NOTE: Assumes input is legal.
637 return DAG.getNode(ISD::FP_ROUND_INREG, N->getOperand(0).getValueType(),
638 N->getOperand(0), DAG.getValueType(N->getValueType(0)));
641 SDOperand DAGTypeLegalizer::PromoteResult_FP_TO_XINT(SDNode *N) {
642 SDOperand Op = N->getOperand(0);
643 // If the operand needed to be promoted, do so now.
644 if (getTypeAction(Op.getValueType()) == Promote)
645 // The input result is prerounded, so we don't have to do anything special.
646 Op = GetPromotedOp(Op);
648 unsigned NewOpc = N->getOpcode();
649 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
651 // If we're promoting a UINT to a larger size, check to see if the new node
652 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
653 // we can use that instead. This allows us to generate better code for
654 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
655 // legal, such as PowerPC.
656 if (N->getOpcode() == ISD::FP_TO_UINT) {
657 if (!TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
658 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
659 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom))
660 NewOpc = ISD::FP_TO_SINT;
663 return DAG.getNode(NewOpc, NVT, Op);
667 SDOperand DAGTypeLegalizer::PromoteResult_SETCC(SDNode *N) {
668 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
669 return DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), N->getOperand(0),
670 N->getOperand(1), N->getOperand(2));
673 SDOperand DAGTypeLegalizer::PromoteResult_LOAD(LoadSDNode *N) {
674 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
675 ISD::LoadExtType ExtType =
676 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
677 SDOperand Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
678 N->getSrcValue(), N->getSrcValueOffset(),
679 N->getLoadedVT(), N->isVolatile(),
682 // Legalized the chain result - switch anything that used the old chain to
684 ReplaceLegalValueWith(SDOperand(N, 1), Res.getValue(1));
688 SDOperand DAGTypeLegalizer::PromoteResult_SimpleIntBinOp(SDNode *N) {
689 // The input may have strange things in the top bits of the registers, but
690 // these operations don't care. They may have weird bits going out, but
691 // that too is okay if they are integer operations.
692 SDOperand LHS = GetPromotedOp(N->getOperand(0));
693 SDOperand RHS = GetPromotedOp(N->getOperand(1));
694 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
697 SDOperand DAGTypeLegalizer::PromoteResult_SHL(SDNode *N) {
698 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
699 GetPromotedOp(N->getOperand(0)), N->getOperand(1));
702 SDOperand DAGTypeLegalizer::PromoteResult_SRA(SDNode *N) {
703 // The input value must be properly sign extended.
704 MVT::ValueType VT = N->getValueType(0);
705 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
706 SDOperand Res = GetPromotedOp(N->getOperand(0));
707 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
708 return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
711 SDOperand DAGTypeLegalizer::PromoteResult_SRL(SDNode *N) {
712 // The input value must be properly zero extended.
713 MVT::ValueType VT = N->getValueType(0);
714 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
715 SDOperand Res = GetPromotedZExtOp(N->getOperand(0));
716 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
719 SDOperand DAGTypeLegalizer::PromoteResult_SELECT(SDNode *N) {
720 SDOperand LHS = GetPromotedOp(N->getOperand(1));
721 SDOperand RHS = GetPromotedOp(N->getOperand(2));
722 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
725 SDOperand DAGTypeLegalizer::PromoteResult_SELECT_CC(SDNode *N) {
726 SDOperand LHS = GetPromotedOp(N->getOperand(2));
727 SDOperand RHS = GetPromotedOp(N->getOperand(3));
728 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
729 N->getOperand(1), LHS, RHS, N->getOperand(4));
732 //===----------------------------------------------------------------------===//
734 //===----------------------------------------------------------------------===//
736 /// ExpandResult - This method is called when the specified result of the
737 /// specified node is found to need expansion. At this point, the node may also
738 /// have invalid operands or may have other results that need promotion, we just
739 /// know that (at least) one result needs expansion.
740 void DAGTypeLegalizer::ExpandResult(SDNode *N, unsigned ResNo) {
741 DEBUG(cerr << "Expand node result: "; N->dump(&DAG); cerr << "\n");
743 Lo = Hi = SDOperand();
745 // If this is a single-result node, see if the target wants to custom expand
747 if (N->getNumValues() == 1 &&
748 TLI.getOperationAction(N->getOpcode(),
749 N->getValueType(0)) == TargetLowering::Custom) {
750 // If the target wants to, allow it to lower this itself.
751 std::pair<SDOperand,SDOperand> P = TLI.ExpandOperationResult(N, DAG);
759 switch (N->getOpcode()) {
762 cerr << "ExpandResult #" << ResNo << ": ";
763 N->dump(&DAG); cerr << "\n";
765 assert(0 && "Do not know how to expand this operator!");
768 case ISD::UNDEF: ExpandResult_UNDEF(N, Lo, Hi); break;
769 case ISD::Constant: ExpandResult_Constant(N, Lo, Hi); break;
770 case ISD::BUILD_PAIR: ExpandResult_BUILD_PAIR(N, Lo, Hi); break;
771 case ISD::ANY_EXTEND: ExpandResult_ANY_EXTEND(N, Lo, Hi); break;
772 case ISD::ZERO_EXTEND: ExpandResult_ZERO_EXTEND(N, Lo, Hi); break;
773 case ISD::SIGN_EXTEND: ExpandResult_SIGN_EXTEND(N, Lo, Hi); break;
774 case ISD::BIT_CONVERT: ExpandResult_BIT_CONVERT(N, Lo, Hi); break;
775 case ISD::SIGN_EXTEND_INREG: ExpandResult_SIGN_EXTEND_INREG(N, Lo, Hi); break;
776 case ISD::LOAD: ExpandResult_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
780 case ISD::XOR: ExpandResult_Logical(N, Lo, Hi); break;
781 case ISD::BSWAP: ExpandResult_BSWAP(N, Lo, Hi); break;
783 case ISD::SUB: ExpandResult_ADDSUB(N, Lo, Hi); break;
785 case ISD::SUBC: ExpandResult_ADDSUBC(N, Lo, Hi); break;
787 case ISD::SUBE: ExpandResult_ADDSUBE(N, Lo, Hi); break;
788 case ISD::SELECT: ExpandResult_SELECT(N, Lo, Hi); break;
789 case ISD::SELECT_CC: ExpandResult_SELECT_CC(N, Lo, Hi); break;
790 case ISD::MUL: ExpandResult_MUL(N, Lo, Hi); break;
793 case ISD::SRL: ExpandResult_Shift(N, Lo, Hi); break;
797 // If Lo/Hi is null, the sub-method took care of registering results etc.
799 SetExpandedOp(SDOperand(N, ResNo), Lo, Hi);
802 void DAGTypeLegalizer::ExpandResult_UNDEF(SDNode *N,
803 SDOperand &Lo, SDOperand &Hi) {
804 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
805 Lo = Hi = DAG.getNode(ISD::UNDEF, NVT);
808 void DAGTypeLegalizer::ExpandResult_Constant(SDNode *N,
809 SDOperand &Lo, SDOperand &Hi) {
810 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
811 uint64_t Cst = cast<ConstantSDNode>(N)->getValue();
812 Lo = DAG.getConstant(Cst, NVT);
813 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
816 void DAGTypeLegalizer::ExpandResult_BUILD_PAIR(SDNode *N,
817 SDOperand &Lo, SDOperand &Hi) {
818 // Return the operands.
819 Lo = N->getOperand(0);
820 Hi = N->getOperand(1);
823 void DAGTypeLegalizer::ExpandResult_ANY_EXTEND(SDNode *N,
824 SDOperand &Lo, SDOperand &Hi) {
825 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
826 SDOperand Op = N->getOperand(0);
827 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
828 // The low part is any extension of the input (which degenerates to a copy).
829 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
830 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
832 // For example, extension of an i48 to an i64. The operand type necessarily
833 // promotes to the result type, so will end up being expanded too.
834 assert(getTypeAction(Op.getValueType()) == Promote &&
835 "Don't know how to expand this result!");
836 SDOperand Res = GetPromotedOp(Op);
837 assert(Res.getValueType() == N->getValueType(0) &&
838 "Operand over promoted?");
839 // Split the promoted operand. This will simplify when it is expanded.
840 SplitOp(Res, Lo, Hi);
844 void DAGTypeLegalizer::ExpandResult_ZERO_EXTEND(SDNode *N,
845 SDOperand &Lo, SDOperand &Hi) {
846 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
847 SDOperand Op = N->getOperand(0);
848 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
849 // The low part is zero extension of the input (which degenerates to a copy).
850 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
851 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
853 // For example, extension of an i48 to an i64. The operand type necessarily
854 // promotes to the result type, so will end up being expanded too.
855 assert(getTypeAction(Op.getValueType()) == Promote &&
856 "Don't know how to expand this result!");
857 SDOperand Res = GetPromotedOp(Op);
858 assert(Res.getValueType() == N->getValueType(0) &&
859 "Operand over promoted?");
860 // Split the promoted operand. This will simplify when it is expanded.
861 SplitOp(Res, Lo, Hi);
862 unsigned ExcessBits =
863 MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
864 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerType(ExcessBits));
868 void DAGTypeLegalizer::ExpandResult_SIGN_EXTEND(SDNode *N,
869 SDOperand &Lo, SDOperand &Hi) {
870 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
871 SDOperand Op = N->getOperand(0);
872 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
873 // The low part is sign extension of the input (which degenerates to a copy).
874 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
875 // The high part is obtained by SRA'ing all but one of the bits of low part.
876 unsigned LoSize = MVT::getSizeInBits(NVT);
877 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
878 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
880 // For example, extension of an i48 to an i64. The operand type necessarily
881 // promotes to the result type, so will end up being expanded too.
882 assert(getTypeAction(Op.getValueType()) == Promote &&
883 "Don't know how to expand this result!");
884 SDOperand Res = GetPromotedOp(Op);
885 assert(Res.getValueType() == N->getValueType(0) &&
886 "Operand over promoted?");
887 // Split the promoted operand. This will simplify when it is expanded.
888 SplitOp(Res, Lo, Hi);
889 unsigned ExcessBits =
890 MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
891 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
892 DAG.getValueType(MVT::getIntegerType(ExcessBits)));
896 void DAGTypeLegalizer::ExpandResult_BIT_CONVERT(SDNode *N,
897 SDOperand &Lo, SDOperand &Hi) {
898 // Lower the bit-convert to a store/load from the stack, then expand the load.
899 SDOperand Op = CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
900 ExpandResult_LOAD(cast<LoadSDNode>(Op.Val), Lo, Hi);
903 void DAGTypeLegalizer::
904 ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
905 GetExpandedOp(N->getOperand(0), Lo, Hi);
906 MVT::ValueType EVT = N->getOperand(1).getValueType();
908 if (MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(Lo.getValueType())) {
909 // sext_inreg the low part if needed.
910 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
913 // The high part gets the sign extension from the lo-part. This handles
914 // things like sextinreg V:i64 from i8.
915 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
916 DAG.getConstant(MVT::getSizeInBits(Hi.getValueType())-1,
917 TLI.getShiftAmountTy()));
919 // For example, extension of an i48 to an i64. Leave the low part alone,
920 // sext_inreg the high part.
921 unsigned ExcessBits =
922 MVT::getSizeInBits(EVT) - MVT::getSizeInBits(Lo.getValueType());
923 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
924 DAG.getValueType(MVT::getIntegerType(ExcessBits)));
928 void DAGTypeLegalizer::ExpandResult_LOAD(LoadSDNode *N,
929 SDOperand &Lo, SDOperand &Hi) {
930 MVT::ValueType VT = N->getValueType(0);
931 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
932 SDOperand Ch = N->getChain(); // Legalize the chain.
933 SDOperand Ptr = N->getBasePtr(); // Legalize the pointer.
934 ISD::LoadExtType ExtType = N->getExtensionType();
935 int SVOffset = N->getSrcValueOffset();
936 unsigned Alignment = N->getAlignment();
937 bool isVolatile = N->isVolatile();
939 if (ExtType == ISD::NON_EXTLOAD) {
940 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
941 isVolatile, Alignment);
942 // Increment the pointer to the other half.
943 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
944 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
945 getIntPtrConstant(IncrementSize));
946 Hi = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
947 isVolatile, std::max(Alignment, IncrementSize));
949 // Build a factor node to remember that this load is independent of the
951 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
954 // Handle endianness of the load.
955 if (!TLI.isLittleEndian())
958 MVT::ValueType EVT = N->getLoadedVT();
961 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(),
962 SVOffset, isVolatile, Alignment);
964 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
965 SVOffset, EVT, isVolatile,
967 // Remember the chain.
970 if (ExtType == ISD::SEXTLOAD) {
971 // The high part is obtained by SRA'ing all but one of the bits of the
973 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
974 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
975 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
976 } else if (ExtType == ISD::ZEXTLOAD) {
977 // The high part is just a zero.
978 Hi = DAG.getConstant(0, NVT);
980 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
981 // The high part is undefined.
982 Hi = DAG.getNode(ISD::UNDEF, NVT);
986 // Legalized the chain result - switch anything that used the old chain to
988 ReplaceLegalValueWith(SDOperand(N, 1), Ch);
992 void DAGTypeLegalizer::ExpandResult_Logical(SDNode *N,
993 SDOperand &Lo, SDOperand &Hi) {
994 SDOperand LL, LH, RL, RH;
995 GetExpandedOp(N->getOperand(0), LL, LH);
996 GetExpandedOp(N->getOperand(1), RL, RH);
997 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
998 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1001 void DAGTypeLegalizer::ExpandResult_BSWAP(SDNode *N,
1002 SDOperand &Lo, SDOperand &Hi) {
1003 GetExpandedOp(N->getOperand(0), Hi, Lo); // Note swapped operands.
1004 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1005 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1009 void DAGTypeLegalizer::ExpandResult_SELECT(SDNode *N,
1010 SDOperand &Lo, SDOperand &Hi) {
1011 SDOperand LL, LH, RL, RH;
1012 GetExpandedOp(N->getOperand(1), LL, LH);
1013 GetExpandedOp(N->getOperand(2), RL, RH);
1014 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LL, RL);
1016 assert(N->getOperand(0).getValueType() != MVT::f32 &&
1017 "FIXME: softfp shouldn't use expand!");
1018 Hi = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LH, RH);
1021 void DAGTypeLegalizer::ExpandResult_SELECT_CC(SDNode *N,
1022 SDOperand &Lo, SDOperand &Hi) {
1023 SDOperand LL, LH, RL, RH;
1024 GetExpandedOp(N->getOperand(2), LL, LH);
1025 GetExpandedOp(N->getOperand(3), RL, RH);
1026 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
1027 N->getOperand(1), LL, RL, N->getOperand(4));
1029 assert(N->getOperand(0).getValueType() != MVT::f32 &&
1030 "FIXME: softfp shouldn't use expand!");
1031 Hi = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
1032 N->getOperand(1), LH, RH, N->getOperand(4));
1035 void DAGTypeLegalizer::ExpandResult_ADDSUB(SDNode *N,
1036 SDOperand &Lo, SDOperand &Hi) {
1037 // Expand the subcomponents.
1038 SDOperand LHSL, LHSH, RHSL, RHSH;
1039 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1040 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1041 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1042 SDOperand LoOps[2] = { LHSL, RHSL };
1043 SDOperand HiOps[3] = { LHSH, RHSH };
1045 if (N->getOpcode() == ISD::ADD) {
1046 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1047 HiOps[2] = Lo.getValue(1);
1048 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1050 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1051 HiOps[2] = Lo.getValue(1);
1052 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1056 void DAGTypeLegalizer::ExpandResult_ADDSUBC(SDNode *N,
1057 SDOperand &Lo, SDOperand &Hi) {
1058 // Expand the subcomponents.
1059 SDOperand LHSL, LHSH, RHSL, RHSH;
1060 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1061 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1062 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1063 SDOperand LoOps[2] = { LHSL, RHSL };
1064 SDOperand HiOps[3] = { LHSH, RHSH };
1066 if (N->getOpcode() == ISD::ADDC) {
1067 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1068 HiOps[2] = Lo.getValue(1);
1069 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1071 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1072 HiOps[2] = Lo.getValue(1);
1073 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1076 // Legalized the flag result - switch anything that used the old flag to
1078 ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1081 void DAGTypeLegalizer::ExpandResult_ADDSUBE(SDNode *N,
1082 SDOperand &Lo, SDOperand &Hi) {
1083 // Expand the subcomponents.
1084 SDOperand LHSL, LHSH, RHSL, RHSH;
1085 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1086 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1087 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1088 SDOperand LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1089 SDOperand HiOps[3] = { LHSH, RHSH };
1091 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1092 HiOps[2] = Lo.getValue(1);
1093 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1095 // Legalized the flag result - switch anything that used the old flag to
1097 ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1100 void DAGTypeLegalizer::ExpandResult_MUL(SDNode *N,
1101 SDOperand &Lo, SDOperand &Hi) {
1102 MVT::ValueType VT = N->getValueType(0);
1103 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1105 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1106 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1107 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1108 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1109 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1110 SDOperand LL, LH, RL, RH;
1111 GetExpandedOp(N->getOperand(0), LL, LH);
1112 GetExpandedOp(N->getOperand(1), RL, RH);
1113 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
1114 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1115 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1117 // FIXME: generalize this to handle other bit sizes
1118 if (LHSSB == 32 && RHSSB == 32 &&
1119 DAG.MaskedValueIsZero(N->getOperand(0), 0xFFFFFFFF00000000ULL) &&
1120 DAG.MaskedValueIsZero(N->getOperand(1), 0xFFFFFFFF00000000ULL)) {
1121 // The inputs are both zero-extended.
1123 // We can emit a umul_lohi.
1124 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1125 Hi = SDOperand(Lo.Val, 1);
1129 // We can emit a mulhu+mul.
1130 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1131 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1135 if (LHSSB > BitSize && RHSSB > BitSize) {
1136 // The input values are both sign-extended.
1138 // We can emit a smul_lohi.
1139 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1140 Hi = SDOperand(Lo.Val, 1);
1144 // We can emit a mulhs+mul.
1145 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1146 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1151 // Lo,Hi = umul LHS, RHS.
1152 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1153 DAG.getVTList(NVT, NVT), LL, RL);
1155 Hi = UMulLOHI.getValue(1);
1156 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1157 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1158 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1159 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1166 // If nothing else, we can make a libcall.
1167 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), N,
1168 false/*sign irrelevant*/, Hi);
1173 void DAGTypeLegalizer::ExpandResult_Shift(SDNode *N,
1174 SDOperand &Lo, SDOperand &Hi) {
1175 MVT::ValueType VT = N->getValueType(0);
1177 // If we can emit an efficient shift operation, do so now. Check to see if
1178 // the RHS is a constant.
1179 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1180 return ExpandShiftByConstant(N, CN->getValue(), Lo, Hi);
1182 // If we can determine that the high bit of the shift is zero or one, even if
1183 // the low bits are variable, emit this shift in an optimized form.
1184 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1187 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1189 if (N->getOpcode() == ISD::SHL)
1190 PartsOpc = ISD::SHL_PARTS;
1191 else if (N->getOpcode() == ISD::SRL)
1192 PartsOpc = ISD::SRL_PARTS;
1194 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1195 PartsOpc = ISD::SRA_PARTS;
1198 // Next check to see if the target supports this SHL_PARTS operation or if it
1199 // will custom expand it.
1200 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1201 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1202 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1203 Action == TargetLowering::Custom) {
1204 // Expand the subcomponents.
1205 SDOperand LHSL, LHSH;
1206 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1208 SDOperand Ops[] = { LHSL, LHSH, N->getOperand(1) };
1209 MVT::ValueType VT = LHSL.getValueType();
1210 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1211 Hi = Lo.getValue(1);
1217 // Otherwise, emit a libcall.
1218 unsigned RuntimeCode = ; // SRL -> SRL_I64 etc.
1220 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), N,
1221 false/*lshr is unsigned*/, Hi);
1226 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1227 /// and the shift amount is a constant 'Amt'. Expand the operation.
1228 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1229 SDOperand &Lo, SDOperand &Hi) {
1230 // Expand the incoming operand to be shifted, so that we have its parts
1232 GetExpandedOp(N->getOperand(0), InL, InH);
1234 MVT::ValueType NVT = InL.getValueType();
1235 unsigned VTBits = MVT::getSizeInBits(N->getValueType(0));
1236 unsigned NVTBits = MVT::getSizeInBits(NVT);
1237 MVT::ValueType ShTy = N->getOperand(1).getValueType();
1239 if (N->getOpcode() == ISD::SHL) {
1241 Lo = Hi = DAG.getConstant(0, NVT);
1242 } else if (Amt > NVTBits) {
1243 Lo = DAG.getConstant(0, NVT);
1244 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1245 } else if (Amt == NVTBits) {
1246 Lo = DAG.getConstant(0, NVT);
1249 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1250 Hi = DAG.getNode(ISD::OR, NVT,
1251 DAG.getNode(ISD::SHL, NVT, InH,
1252 DAG.getConstant(Amt, ShTy)),
1253 DAG.getNode(ISD::SRL, NVT, InL,
1254 DAG.getConstant(NVTBits-Amt, ShTy)));
1259 if (N->getOpcode() == ISD::SRL) {
1261 Lo = DAG.getConstant(0, NVT);
1262 Hi = DAG.getConstant(0, NVT);
1263 } else if (Amt > NVTBits) {
1264 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1265 Hi = DAG.getConstant(0, NVT);
1266 } else if (Amt == NVTBits) {
1268 Hi = DAG.getConstant(0, NVT);
1270 Lo = DAG.getNode(ISD::OR, NVT,
1271 DAG.getNode(ISD::SRL, NVT, InL,
1272 DAG.getConstant(Amt, ShTy)),
1273 DAG.getNode(ISD::SHL, NVT, InH,
1274 DAG.getConstant(NVTBits-Amt, ShTy)));
1275 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1280 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1282 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1283 DAG.getConstant(NVTBits-1, ShTy));
1284 } else if (Amt > NVTBits) {
1285 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1286 DAG.getConstant(Amt-NVTBits, ShTy));
1287 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1288 DAG.getConstant(NVTBits-1, ShTy));
1289 } else if (Amt == NVTBits) {
1291 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1292 DAG.getConstant(NVTBits-1, ShTy));
1294 Lo = DAG.getNode(ISD::OR, NVT,
1295 DAG.getNode(ISD::SRL, NVT, InL,
1296 DAG.getConstant(Amt, ShTy)),
1297 DAG.getNode(ISD::SHL, NVT, InH,
1298 DAG.getConstant(NVTBits-Amt, ShTy)));
1299 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1303 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1304 /// this shift based on knowledge of the high bit of the shift amount. If we
1305 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1307 bool DAGTypeLegalizer::
1308 ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
1309 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1310 unsigned NVTBits = MVT::getSizeInBits(NVT);
1312 uint64_t HighBitMask = NVTBits, KnownZero, KnownOne;
1313 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1315 // If we don't know anything about the high bit, exit.
1316 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1319 // Get the incoming operand to be shifted.
1321 GetExpandedOp(N->getOperand(0), InL, InH);
1322 SDOperand Amt = N->getOperand(1);
1324 // If we know that the high bit of the shift amount is one, then we can do
1325 // this as a couple of simple shifts.
1326 if (KnownOne & HighBitMask) {
1327 // Mask out the high bit, which we know is set.
1328 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
1329 DAG.getConstant(NVTBits-1, Amt.getValueType()));
1331 switch (N->getOpcode()) {
1332 default: assert(0 && "Unknown shift");
1334 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1335 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1338 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1339 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1342 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1343 DAG.getConstant(NVTBits-1, Amt.getValueType()));
1344 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1349 // If we know that the high bit of the shift amount is zero, then we can do
1350 // this as a couple of simple shifts.
1351 assert((KnownZero & HighBitMask) && "Bad mask computation above");
1354 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
1355 DAG.getConstant(NVTBits, Amt.getValueType()),
1358 switch (N->getOpcode()) {
1359 default: assert(0 && "Unknown shift");
1360 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1362 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1365 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1366 Hi = DAG.getNode(ISD::OR, NVT,
1367 DAG.getNode(Op1, NVT, InH, Amt),
1368 DAG.getNode(Op2, NVT, InL, Amt2));
1372 //===----------------------------------------------------------------------===//
1373 // Operand Promotion
1374 //===----------------------------------------------------------------------===//
1376 /// PromoteOperand - This method is called when the specified operand of the
1377 /// specified node is found to need promotion. At this point, all of the result
1378 /// types of the node are known to be legal, but other operands of the node may
1379 /// need promotion or expansion as well as the specified one.
1380 bool DAGTypeLegalizer::PromoteOperand(SDNode *N, unsigned OpNo) {
1381 DEBUG(cerr << "Promote node operand: "; N->dump(&DAG); cerr << "\n");
1383 switch (N->getOpcode()) {
1386 cerr << "PromoteOperand Op #" << OpNo << ": ";
1387 N->dump(&DAG); cerr << "\n";
1389 assert(0 && "Do not know how to promote this operator's operand!");
1392 case ISD::ANY_EXTEND: Res = PromoteOperand_ANY_EXTEND(N); break;
1393 case ISD::ZERO_EXTEND: Res = PromoteOperand_ZERO_EXTEND(N); break;
1394 case ISD::SIGN_EXTEND: Res = PromoteOperand_SIGN_EXTEND(N); break;
1395 case ISD::TRUNCATE: Res = PromoteOperand_TRUNCATE(N); break;
1396 case ISD::FP_EXTEND: Res = PromoteOperand_FP_EXTEND(N); break;
1397 case ISD::FP_ROUND: Res = PromoteOperand_FP_ROUND(N); break;
1398 case ISD::SINT_TO_FP:
1399 case ISD::UINT_TO_FP: Res = PromoteOperand_INT_TO_FP(N); break;
1401 case ISD::SELECT: Res = PromoteOperand_SELECT(N, OpNo); break;
1402 case ISD::BRCOND: Res = PromoteOperand_BRCOND(N, OpNo); break;
1403 case ISD::BR_CC: Res = PromoteOperand_BR_CC(N, OpNo); break;
1405 case ISD::STORE: Res = PromoteOperand_STORE(cast<StoreSDNode>(N),
1409 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
1412 // If the result is null, the sub-method took care of registering results etc.
1413 if (!Res.Val) return false;
1414 // If the result is N, the sub-method updated N in place.
1416 // Mark N as new and remark N and its operands. This allows us to correctly
1417 // revisit N if it needs another step of promotion and allows us to visit
1418 // any new operands to N.
1419 N->setNodeId(NewNode);
1424 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1425 "Invalid operand expansion");
1427 ReplaceLegalValueWith(SDOperand(N, 0), Res);
1431 SDOperand DAGTypeLegalizer::PromoteOperand_ANY_EXTEND(SDNode *N) {
1432 SDOperand Op = GetPromotedOp(N->getOperand(0));
1433 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1436 SDOperand DAGTypeLegalizer::PromoteOperand_ZERO_EXTEND(SDNode *N) {
1437 SDOperand Op = GetPromotedOp(N->getOperand(0));
1438 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1439 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
1442 SDOperand DAGTypeLegalizer::PromoteOperand_SIGN_EXTEND(SDNode *N) {
1443 SDOperand Op = GetPromotedOp(N->getOperand(0));
1444 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1445 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
1446 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1449 SDOperand DAGTypeLegalizer::PromoteOperand_TRUNCATE(SDNode *N) {
1450 SDOperand Op = GetPromotedOp(N->getOperand(0));
1451 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
1454 SDOperand DAGTypeLegalizer::PromoteOperand_FP_EXTEND(SDNode *N) {
1455 SDOperand Op = GetPromotedOp(N->getOperand(0));
1456 return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
1459 SDOperand DAGTypeLegalizer::PromoteOperand_FP_ROUND(SDNode *N) {
1460 SDOperand Op = GetPromotedOp(N->getOperand(0));
1461 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op);
1464 SDOperand DAGTypeLegalizer::PromoteOperand_INT_TO_FP(SDNode *N) {
1465 SDOperand In = GetPromotedOp(N->getOperand(0));
1466 MVT::ValueType OpVT = N->getOperand(0).getValueType();
1467 if (N->getOpcode() == ISD::UINT_TO_FP)
1468 In = DAG.getZeroExtendInReg(In, OpVT);
1470 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
1471 In, DAG.getValueType(OpVT));
1473 return DAG.UpdateNodeOperands(SDOperand(N, 0), In);
1477 SDOperand DAGTypeLegalizer::PromoteOperand_SELECT(SDNode *N, unsigned OpNo) {
1478 assert(OpNo == 0 && "Only know how to promote condition");
1479 SDOperand Cond = GetPromotedOp(N->getOperand(0)); // Promote the condition.
1481 // The top bits of the promoted condition are not necessarily zero, ensure
1482 // that the value is properly zero extended.
1483 if (!DAG.MaskedValueIsZero(Cond,
1484 MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1485 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1486 MarkNewNodes(Cond.Val);
1489 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1490 return DAG.UpdateNodeOperands(SDOperand(N, 0), Cond, N->getOperand(1),
1495 SDOperand DAGTypeLegalizer::PromoteOperand_BRCOND(SDNode *N, unsigned OpNo) {
1496 assert(OpNo == 1 && "only know how to promote condition");
1497 SDOperand Cond = GetPromotedOp(N->getOperand(1)); // Promote the condition.
1499 // The top bits of the promoted condition are not necessarily zero, ensure
1500 // that the value is properly zero extended.
1501 if (!DAG.MaskedValueIsZero(Cond,
1502 MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1503 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1504 MarkNewNodes(Cond.Val);
1507 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1508 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0), Cond,
1512 SDOperand DAGTypeLegalizer::PromoteOperand_BR_CC(SDNode *N, unsigned OpNo) {
1513 assert(OpNo == 2 && "Don't know how to promote this operand");
1515 SDOperand LHS = N->getOperand(2);
1516 SDOperand RHS = N->getOperand(3);
1517 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1519 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1521 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
1522 N->getOperand(1), LHS, RHS, N->getOperand(4));
1525 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1526 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1527 void DAGTypeLegalizer::PromoteSetCCOperands(SDOperand &NewLHS,SDOperand &NewRHS,
1528 ISD::CondCode CCCode) {
1529 MVT::ValueType VT = NewLHS.getValueType();
1531 // Get the promoted values.
1532 NewLHS = GetPromotedOp(NewLHS);
1533 NewRHS = GetPromotedOp(NewRHS);
1535 // If this is an FP compare, the operands have already been extended.
1536 if (!MVT::isInteger(NewLHS.getValueType()))
1539 // Otherwise, we have to insert explicit sign or zero extends. Note
1540 // that we could insert sign extends for ALL conditions, but zero extend
1541 // is cheaper on many machines (an AND instead of two shifts), so prefer
1544 default: assert(0 && "Unknown integer comparison!");
1551 // ALL of these operations will work if we either sign or zero extend
1552 // the operands (including the unsigned comparisons!). Zero extend is
1553 // usually a simpler/cheaper operation, so prefer it.
1554 NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
1555 NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
1561 NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
1562 DAG.getValueType(VT));
1563 NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
1564 DAG.getValueType(VT));
1570 SDOperand DAGTypeLegalizer::PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo){
1571 SDOperand Ch = N->getChain(), Ptr = N->getBasePtr();
1572 int SVOffset = N->getSrcValueOffset();
1573 unsigned Alignment = N->getAlignment();
1574 bool isVolatile = N->isVolatile();
1576 SDOperand Val = GetPromotedOp(N->getValue()); // Get promoted value.
1578 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
1580 // Truncate the value and store the result.
1581 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
1582 SVOffset, N->getStoredVT(),
1583 isVolatile, Alignment);
1587 //===----------------------------------------------------------------------===//
1588 // Operand Expansion
1589 //===----------------------------------------------------------------------===//
1591 /// ExpandOperand - This method is called when the specified operand of the
1592 /// specified node is found to need expansion. At this point, all of the result
1593 /// types of the node are known to be legal, but other operands of the node may
1594 /// need promotion or expansion as well as the specified one.
1595 bool DAGTypeLegalizer::ExpandOperand(SDNode *N, unsigned OpNo) {
1596 DEBUG(cerr << "Expand node operand: "; N->dump(&DAG); cerr << "\n");
1598 switch (N->getOpcode()) {
1601 cerr << "ExpandOperand Op #" << OpNo << ": ";
1602 N->dump(&DAG); cerr << "\n";
1604 assert(0 && "Do not know how to expand this operator's operand!");
1607 case ISD::TRUNCATE: Res = ExpandOperand_TRUNCATE(N); break;
1608 case ISD::BIT_CONVERT: Res = ExpandOperand_BIT_CONVERT(N); break;
1610 case ISD::SINT_TO_FP:
1611 Res = ExpandOperand_SINT_TO_FP(N->getOperand(0), N->getValueType(0));
1613 case ISD::UINT_TO_FP:
1614 Res = ExpandOperand_UINT_TO_FP(N->getOperand(0), N->getValueType(0));
1616 case ISD::EXTRACT_ELEMENT: Res = ExpandOperand_EXTRACT_ELEMENT(N); break;
1617 case ISD::SETCC: Res = ExpandOperand_SETCC(N); break;
1619 case ISD::STORE: Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo); break;
1622 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
1625 // If the result is null, the sub-method took care of registering results etc.
1626 if (!Res.Val) return false;
1627 // If the result is N, the sub-method updated N in place. Check to see if any
1628 // operands are new, and if so, mark them.
1630 // Mark N as new and remark N and its operands. This allows us to correctly
1631 // revisit N if it needs another step of promotion and allows us to visit
1632 // any new operands to N.
1633 N->setNodeId(NewNode);
1638 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1639 "Invalid operand expansion");
1641 ReplaceLegalValueWith(SDOperand(N, 0), Res);
1645 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
1647 GetExpandedOp(N->getOperand(0), InL, InH);
1648 // Just truncate the low part of the source.
1649 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
1652 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
1653 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1656 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source,
1657 MVT::ValueType DestTy) {
1658 // We know the destination is legal, but that the input needs to be expanded.
1659 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1661 // Check to see if the target has a custom way to lower this. If so, use it.
1662 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
1663 default: assert(0 && "This action not implemented for this operation!");
1664 case TargetLowering::Legal:
1665 case TargetLowering::Expand:
1666 break; // This case is handled below.
1667 case TargetLowering::Custom:
1668 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
1670 if (NV.Val) return NV;
1671 break; // The target lowered this.
1675 if (DestTy == MVT::f32)
1676 LC = RTLIB::SINTTOFP_I64_F32;
1678 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1679 LC = RTLIB::SINTTOFP_I64_F64;
1682 assert(0 && "FIXME: no libcalls yet!");
1685 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
1686 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
1687 SDOperand UnusedHiPart;
1688 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
1692 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source,
1693 MVT::ValueType DestTy) {
1694 // We know the destination is legal, but that the input needs to be expanded.
1695 assert(getTypeAction(Source.getValueType()) == Expand &&
1696 "This is not an expansion!");
1697 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1699 // If this is unsigned, and not supported, first perform the conversion to
1700 // signed, then adjust the result if the sign bit is set.
1701 SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
1703 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1704 // incoming integer is set. To handle this, we dynamically test to see if
1705 // it is set, and, if so, add a fudge factor.
1707 GetExpandedOp(Source, Lo, Hi);
1709 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
1710 DAG.getConstant(0, Hi.getValueType()),
1712 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1713 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1714 SignSet, Four, Zero);
1715 uint64_t FF = 0x5f800000ULL;
1716 if (TLI.isLittleEndian()) FF <<= 32;
1717 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
1719 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
1720 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1721 SDOperand FudgeInReg;
1722 if (DestTy == MVT::f32)
1723 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
1724 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
1725 // FIXME: Avoid the extend by construction the right constantpool?
1726 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
1727 CPIdx, NULL, 0, MVT::f32);
1729 assert(0 && "Unexpected conversion");
1731 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
1735 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
1737 GetExpandedOp(N->getOperand(0), Lo, Hi);
1738 return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
1741 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
1742 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1743 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1744 ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1746 // If ExpandSetCCOperands returned a scalar, use it.
1747 if (NewRHS.Val == 0) return NewLHS;
1749 // Otherwise, update N to have the operands specified.
1750 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1751 DAG.getCondCode(CCCode));
1754 /// ExpandSetCCOperands - Expand the operands of a comparison. This code is
1755 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1756 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
1757 ISD::CondCode &CCCode) {
1758 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1759 GetExpandedOp(NewLHS, LHSLo, LHSHi);
1760 GetExpandedOp(NewRHS, RHSLo, RHSHi);
1762 MVT::ValueType VT = NewLHS.getValueType();
1763 if (VT == MVT::f32 || VT == MVT::f64) {
1764 assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1767 if (VT == MVT::ppcf128) {
1768 // FIXME: This generated code sucks. We want to generate
1769 // FCMP crN, hi1, hi2
1771 // FCMP crN, lo1, lo2
1772 // The following can be improved, but not that much.
1773 SDOperand Tmp1, Tmp2, Tmp3;
1774 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1775 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1776 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1777 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1778 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1779 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1780 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1781 NewRHS = SDOperand(); // LHS is the result, not a compare.
1786 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1788 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1789 if (RHSCST->isAllOnesValue()) {
1790 // Equality comparison to -1.
1791 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1796 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1797 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1798 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1799 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1803 // If this is a comparison of the sign bit, just look at the top part.
1805 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1806 if ((CCCode == ISD::SETLT && CST->getValue() == 0) || // X < 0
1807 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1813 // FIXME: This generated code sucks.
1814 ISD::CondCode LowCC;
1816 default: assert(0 && "Unknown integer setcc!");
1818 case ISD::SETULT: LowCC = ISD::SETULT; break;
1820 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1822 case ISD::SETULE: LowCC = ISD::SETULE; break;
1824 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1827 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1828 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1829 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1831 // NOTE: on targets without efficient SELECT of bools, we can always use
1832 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1833 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1834 SDOperand Tmp1, Tmp2;
1835 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1836 false, DagCombineInfo);
1838 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1839 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1840 CCCode, false, DagCombineInfo);
1842 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1843 DAG.getCondCode(CCCode));
1845 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1846 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1847 if ((Tmp1C && Tmp1C->getValue() == 0) ||
1848 (Tmp2C && Tmp2C->getValue() == 0 &&
1849 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1850 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1851 (Tmp2C && Tmp2C->getValue() == 1 &&
1852 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1853 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1854 // low part is known false, returns high part.
1855 // For LE / GE, if high part is known false, ignore the low part.
1856 // For LT / GT, if high part is known true, ignore the low part.
1858 NewRHS = SDOperand();
1862 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1863 ISD::SETEQ, false, DagCombineInfo);
1865 NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1866 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1867 NewLHS, Tmp1, Tmp2);
1868 NewRHS = SDOperand();
1872 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1873 assert(OpNo == 1 && "Can only expand the stored value so far");
1874 assert(!N->isTruncatingStore() && "Can't expand truncstore!");
1876 unsigned IncrementSize = 0;
1879 // If this is a vector type, then we have to calculate the increment as
1880 // the product of the element size in bytes, and the number of elements
1881 // in the high half of the vector.
1882 if (MVT::isVector(N->getValue().getValueType())) {
1883 assert(0 && "Vectors not supported yet");
1885 SDNode *InVal = ST->getValue().Val;
1886 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1887 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
1889 // Figure out if there is a simple type corresponding to this Vector
1890 // type. If so, convert to the vector type.
1891 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1892 if (TLI.isTypeLegal(TVT)) {
1893 // Turn this into a normal store of the vector type.
1894 Tmp3 = LegalizeOp(Node->getOperand(1));
1895 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1896 SVOffset, isVolatile, Alignment);
1897 Result = LegalizeOp(Result);
1899 } else if (NumElems == 1) {
1900 // Turn this into a normal store of the scalar type.
1901 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
1902 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1903 SVOffset, isVolatile, Alignment);
1904 // The scalarized value type may not be legal, e.g. it might require
1905 // promotion or expansion. Relegalize the scalar store.
1906 return LegalizeOp(Result);
1908 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1909 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1913 GetExpandedOp(N->getValue(), Lo, Hi);
1914 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
1916 if (!TLI.isLittleEndian())
1920 SDOperand Chain = N->getChain();
1921 SDOperand Ptr = N->getBasePtr();
1922 int SVOffset = N->getSrcValueOffset();
1923 unsigned Alignment = N->getAlignment();
1924 bool isVolatile = N->isVolatile();
1926 Lo = DAG.getStore(Chain, Lo, Ptr, N->getSrcValue(),
1927 SVOffset, isVolatile, Alignment);
1929 assert(Hi.Val && "FIXME: int <-> float should be handled with promote!");
1931 if (Hi.Val == NULL) {
1932 // Must be int <-> float one-to-one expansion.
1937 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1938 getIntPtrConstant(IncrementSize));
1939 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
1940 Hi = DAG.getStore(Chain, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
1941 isVolatile, std::max(Alignment, IncrementSize));
1942 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1945 //===----------------------------------------------------------------------===//
1947 //===----------------------------------------------------------------------===//
1949 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
1950 /// only uses types natively supported by the target.
1952 /// Note that this is an involved process that may invalidate pointers into
1954 void SelectionDAG::LegalizeTypes() {
1955 DAGTypeLegalizer(*this).run();