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_SETCC(SDNode *N, unsigned OpNo);
194 SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
196 void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
198 // Operand Expansion.
199 bool ExpandOperand(SDNode *N, unsigned OperandNo);
200 SDOperand ExpandOperand_TRUNCATE(SDNode *N);
201 SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
202 SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
203 SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
204 SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
205 SDOperand ExpandOperand_SETCC(SDNode *N);
206 SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
208 void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
209 ISD::CondCode &CCCode);
211 } // end anonymous namespace
215 /// run - This is the main entry point for the type legalizer. This does a
216 /// top-down traversal of the dag, legalizing types as it goes.
217 void DAGTypeLegalizer::run() {
218 // Create a dummy node (which is not added to allnodes), that adds a reference
219 // to the root node, preventing it from being deleted, and tracking any
220 // changes of the root.
221 HandleSDNode Dummy(DAG.getRoot());
223 // The root of the dag may dangle to deleted nodes until the type legalizer is
224 // done. Set it to null to avoid confusion.
225 DAG.setRoot(SDOperand());
227 // Walk all nodes in the graph, assigning them a NodeID of 'ReadyToProcess'
228 // (and remembering them) if they are leaves and assigning 'NewNode' if
230 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
231 E = DAG.allnodes_end(); I != E; ++I) {
232 if (I->getNumOperands() == 0) {
233 I->setNodeId(ReadyToProcess);
234 Worklist.push_back(I);
236 I->setNodeId(NewNode);
240 // Now that we have a set of nodes to process, handle them all.
241 while (!Worklist.empty()) {
242 SDNode *N = Worklist.back();
244 assert(N->getNodeId() == ReadyToProcess &&
245 "Node should be ready if on worklist!");
247 // Scan the values produced by the node, checking to see if any result
248 // types are illegal.
250 unsigned NumResults = N->getNumValues();
252 LegalizeAction Action = getTypeAction(N->getValueType(i));
253 if (Action == Promote) {
256 } else if (Action == Expand) {
260 assert(Action == Legal && "Unknown action!");
262 } while (++i < NumResults);
264 // Scan the operand list for the node, handling any nodes with operands that
267 unsigned NumOperands = N->getNumOperands();
268 bool NeedsRevisit = false;
269 for (i = 0; i != NumOperands; ++i) {
270 LegalizeAction Action = getTypeAction(N->getOperand(i).getValueType());
271 if (Action == Promote) {
272 NeedsRevisit = PromoteOperand(N, i);
274 } else if (Action == Expand) {
275 NeedsRevisit = ExpandOperand(N, i);
278 assert(Action == Legal && "Unknown action!");
282 // If the node needs revisiting, don't add all users to the worklist etc.
286 if (i == NumOperands)
287 DEBUG(cerr << "Legally typed node: "; N->dump(&DAG); cerr << "\n");
291 // If we reach here, the node was processed, potentially creating new nodes.
292 // Mark it as processed and add its users to the worklist as appropriate.
293 N->setNodeId(Processed);
295 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
298 int NodeID = User->getNodeId();
299 assert(NodeID != ReadyToProcess && NodeID != Processed &&
300 "Invalid node id for user of unprocessed node!");
302 // This node has two options: it can either be a new node or its Node ID
303 // may be a count of the number of operands it has that are not ready.
305 User->setNodeId(NodeID-1);
307 // If this was the last use it was waiting on, add it to the ready list.
308 if (NodeID-1 == ReadyToProcess)
309 Worklist.push_back(User);
313 // Otherwise, this node is new: this is the first operand of it that
314 // became ready. Its new NodeID is the number of operands it has minus 1
315 // (as this node is now processed).
316 assert(NodeID == NewNode && "Unknown node ID!");
317 User->setNodeId(User->getNumOperands()-1);
319 // If the node only has a single operand, it is now ready.
320 if (User->getNumOperands() == 1)
321 Worklist.push_back(User);
325 // If the root changed (e.g. it was a dead load, update the root).
326 DAG.setRoot(Dummy.getValue());
330 // Remove dead nodes. This is important to do for cleanliness but also before
331 // the checking loop below. Implicit folding by the DAG.getNode operators can
332 // cause unreachable nodes to be around with their flags set to new.
333 DAG.RemoveDeadNodes();
335 // In a debug build, scan all the nodes to make sure we found them all. This
336 // ensures that there are no cycles and that everything got processed.
338 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
339 E = DAG.allnodes_end(); I != E; ++I) {
340 if (I->getNodeId() == Processed)
342 cerr << "Unprocessed node: ";
343 I->dump(&DAG); cerr << "\n";
345 if (I->getNodeId() == NewNode)
346 cerr << "New node not 'noticed'?\n";
347 else if (I->getNodeId() > 0)
348 cerr << "Operand not processed?\n";
349 else if (I->getNodeId() == ReadyToProcess)
350 cerr << "Not added to worklist?\n";
356 /// MarkNewNodes - The specified node is the root of a subtree of potentially
357 /// new nodes. Add the correct NodeId to mark it.
358 void DAGTypeLegalizer::MarkNewNodes(SDNode *N) {
359 // If this was an existing node that is already done, we're done.
360 if (N->getNodeId() != NewNode)
363 // Okay, we know that this node is new. Recursively walk all of its operands
364 // to see if they are new also. The depth of this walk is bounded by the size
365 // of the new tree that was constructed (usually 2-3 nodes), so we don't worry
366 // about revisiting of nodes.
368 // As we walk the operands, keep track of the number of nodes that are
369 // processed. If non-zero, this will become the new nodeid of this node.
370 unsigned NumProcessed = 0;
371 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
372 int OpId = N->getOperand(i).Val->getNodeId();
374 MarkNewNodes(N->getOperand(i).Val);
375 else if (OpId == Processed)
379 N->setNodeId(N->getNumOperands()-NumProcessed);
380 if (N->getNodeId() == ReadyToProcess)
381 Worklist.push_back(N);
384 /// ReplaceLegalValueWith - The specified value with a legal type was legalized
385 /// to the specified other value. If they are different, update the DAG and
386 /// NodeIDs replacing any uses of From to use To instead.
387 void DAGTypeLegalizer::ReplaceLegalValueWith(SDOperand From, SDOperand To) {
388 if (From == To) return;
390 // If expansion produced new nodes, make sure they are properly marked.
391 if (To.Val->getNodeId() == NewNode)
392 MarkNewNodes(To.Val);
394 // Anything that used the old node should now use the new one. Note that this
395 // can potentially cause recursive merging.
396 DAG.ReplaceAllUsesOfValueWith(From, To);
398 // Since we just made an unstructured update to the DAG, which could wreak
399 // general havoc on anything that once used N and now uses Res, walk all users
400 // of the result, updating their flags.
401 for (SDNode::use_iterator I = To.Val->use_begin(), E = To.Val->use_end();
404 // If the node isn't already processed or in the worklist, mark it as new,
405 // then use MarkNewNodes to recompute its ID.
406 int NodeId = User->getNodeId();
407 if (NodeId != ReadyToProcess && NodeId != Processed) {
408 User->setNodeId(NewNode);
414 void DAGTypeLegalizer::SetPromotedOp(SDOperand Op, SDOperand Result) {
415 if (Result.Val->getNodeId() == NewNode)
416 MarkNewNodes(Result.Val);
418 SDOperand &OpEntry = PromotedNodes[Op];
419 assert(OpEntry.Val == 0 && "Node is already promoted!");
423 void DAGTypeLegalizer::GetExpandedOp(SDOperand Op, SDOperand &Lo,
425 std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
426 assert(Entry.first.Val && "Operand isn't expanded");
431 void DAGTypeLegalizer::SetExpandedOp(SDOperand Op, SDOperand Lo,
433 // Remember that this is the result of the node.
434 std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
435 assert(Entry.first.Val == 0 && "Node already expanded");
439 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
440 if (Lo.Val->getNodeId() == NewNode)
441 MarkNewNodes(Lo.Val);
442 if (Hi.Val->getNodeId() == NewNode)
443 MarkNewNodes(Hi.Val);
446 SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op,
447 MVT::ValueType DestVT) {
448 // Create the stack frame object.
449 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
451 // Emit a store to the stack slot.
452 SDOperand Store = DAG.getStore(DAG.getEntryNode(), Op, FIPtr, NULL, 0);
453 // Result is a load from the stack slot.
454 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);
666 SDOperand DAGTypeLegalizer::PromoteResult_SETCC(SDNode *N) {
667 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
668 return DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), N->getOperand(0),
669 N->getOperand(1), N->getOperand(2));
672 SDOperand DAGTypeLegalizer::PromoteResult_LOAD(LoadSDNode *N) {
673 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
674 ISD::LoadExtType ExtType =
675 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
676 SDOperand Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
677 N->getSrcValue(), N->getSrcValueOffset(),
678 N->getLoadedVT(), N->isVolatile(),
681 // Legalized the chain result - switch anything that used the old chain to
683 ReplaceLegalValueWith(SDOperand(N, 1), Res.getValue(1));
687 SDOperand DAGTypeLegalizer::PromoteResult_SimpleIntBinOp(SDNode *N) {
688 // The input may have strange things in the top bits of the registers, but
689 // these operations don't care. They may have weird bits going out, but
690 // that too is okay if they are integer operations.
691 SDOperand LHS = GetPromotedOp(N->getOperand(0));
692 SDOperand RHS = GetPromotedOp(N->getOperand(1));
693 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
696 SDOperand DAGTypeLegalizer::PromoteResult_SHL(SDNode *N) {
697 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
698 GetPromotedOp(N->getOperand(0)), N->getOperand(1));
701 SDOperand DAGTypeLegalizer::PromoteResult_SRA(SDNode *N) {
702 // The input value must be properly sign extended.
703 MVT::ValueType VT = N->getValueType(0);
704 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
705 SDOperand Res = GetPromotedOp(N->getOperand(0));
706 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
707 return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
710 SDOperand DAGTypeLegalizer::PromoteResult_SRL(SDNode *N) {
711 // The input value must be properly zero extended.
712 MVT::ValueType VT = N->getValueType(0);
713 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
714 SDOperand Res = GetPromotedZExtOp(N->getOperand(0));
715 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
718 SDOperand DAGTypeLegalizer::PromoteResult_SELECT(SDNode *N) {
719 SDOperand LHS = GetPromotedOp(N->getOperand(1));
720 SDOperand RHS = GetPromotedOp(N->getOperand(2));
721 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
724 SDOperand DAGTypeLegalizer::PromoteResult_SELECT_CC(SDNode *N) {
725 SDOperand LHS = GetPromotedOp(N->getOperand(2));
726 SDOperand RHS = GetPromotedOp(N->getOperand(3));
727 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
728 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 = cast<VTSDNode>(N->getOperand(1))->getVT();
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 assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
941 if (ExtType == ISD::NON_EXTLOAD) {
942 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
943 isVolatile, Alignment);
944 // Increment the pointer to the other half.
945 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
946 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
947 getIntPtrConstant(IncrementSize));
948 Hi = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
949 isVolatile, std::max(Alignment, IncrementSize));
951 // Build a factor node to remember that this load is independent of the
953 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
956 // Handle endianness of the load.
957 if (!TLI.isLittleEndian())
959 } else if (MVT::getSizeInBits(N->getLoadedVT()) <= MVT::getSizeInBits(NVT)) {
960 MVT::ValueType EVT = N->getLoadedVT();
962 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
963 isVolatile, Alignment);
965 // Remember the chain.
968 if (ExtType == ISD::SEXTLOAD) {
969 // The high part is obtained by SRA'ing all but one of the bits of the
971 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
972 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
973 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
974 } else if (ExtType == ISD::ZEXTLOAD) {
975 // The high part is just a zero.
976 Hi = DAG.getConstant(0, NVT);
978 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
979 // The high part is undefined.
980 Hi = DAG.getNode(ISD::UNDEF, NVT);
982 } else if (TLI.isLittleEndian()) {
983 // Little-endian - low bits are at low addresses.
984 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
985 isVolatile, Alignment);
987 unsigned ExcessBits =
988 MVT::getSizeInBits(N->getLoadedVT()) - MVT::getSizeInBits(NVT);
989 MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
991 // Increment the pointer to the other half.
992 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
993 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
994 getIntPtrConstant(IncrementSize));
995 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
996 SVOffset+IncrementSize, NEVT,
997 isVolatile, std::max(Alignment, IncrementSize));
999 // Build a factor node to remember that this load is independent of the
1001 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1004 // Big-endian - high bits are at low addresses. Favor aligned loads at
1005 // the cost of some bit-fiddling.
1006 MVT::ValueType EVT = N->getLoadedVT();
1007 unsigned EBytes = (MVT::getSizeInBits(EVT) + 7)/8;
1008 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1009 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1011 // Load both the high bits and maybe some of the low bits.
1012 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1013 MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits),
1014 isVolatile, Alignment);
1016 // Increment the pointer to the other half.
1017 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1018 getIntPtrConstant(IncrementSize));
1019 // Load the rest of the low bits.
1020 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1021 SVOffset+IncrementSize, MVT::getIntegerType(ExcessBits),
1022 isVolatile, std::max(Alignment, IncrementSize));
1024 // Build a factor node to remember that this load is independent of the
1026 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1029 if (ExcessBits < MVT::getSizeInBits(NVT)) {
1030 // Transfer low bits from the bottom of Hi to the top of Lo.
1031 Lo = DAG.getNode(ISD::OR, NVT, Lo,
1032 DAG.getNode(ISD::SHL, NVT, Hi,
1033 DAG.getConstant(ExcessBits,
1034 TLI.getShiftAmountTy())));
1035 // Move high bits to the right position in Hi.
1036 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1037 DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
1038 TLI.getShiftAmountTy()));
1042 // Legalized the chain result - switch anything that used the old chain to
1044 ReplaceLegalValueWith(SDOperand(N, 1), Ch);
1047 void DAGTypeLegalizer::ExpandResult_Logical(SDNode *N,
1048 SDOperand &Lo, SDOperand &Hi) {
1049 SDOperand LL, LH, RL, RH;
1050 GetExpandedOp(N->getOperand(0), LL, LH);
1051 GetExpandedOp(N->getOperand(1), RL, RH);
1052 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1053 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1056 void DAGTypeLegalizer::ExpandResult_BSWAP(SDNode *N,
1057 SDOperand &Lo, SDOperand &Hi) {
1058 GetExpandedOp(N->getOperand(0), Hi, Lo); // Note swapped operands.
1059 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1060 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1063 void DAGTypeLegalizer::ExpandResult_SELECT(SDNode *N,
1064 SDOperand &Lo, SDOperand &Hi) {
1065 SDOperand LL, LH, RL, RH;
1066 GetExpandedOp(N->getOperand(1), LL, LH);
1067 GetExpandedOp(N->getOperand(2), RL, RH);
1068 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LL, RL);
1070 assert(N->getOperand(0).getValueType() != MVT::f32 &&
1071 "FIXME: softfp shouldn't use expand!");
1072 Hi = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LH, RH);
1075 void DAGTypeLegalizer::ExpandResult_SELECT_CC(SDNode *N,
1076 SDOperand &Lo, SDOperand &Hi) {
1077 SDOperand LL, LH, RL, RH;
1078 GetExpandedOp(N->getOperand(2), LL, LH);
1079 GetExpandedOp(N->getOperand(3), RL, RH);
1080 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
1081 N->getOperand(1), LL, RL, N->getOperand(4));
1083 assert(N->getOperand(0).getValueType() != MVT::f32 &&
1084 "FIXME: softfp shouldn't use expand!");
1085 Hi = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
1086 N->getOperand(1), LH, RH, N->getOperand(4));
1089 void DAGTypeLegalizer::ExpandResult_ADDSUB(SDNode *N,
1090 SDOperand &Lo, SDOperand &Hi) {
1091 // Expand the subcomponents.
1092 SDOperand LHSL, LHSH, RHSL, RHSH;
1093 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1094 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1095 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1096 SDOperand LoOps[2] = { LHSL, RHSL };
1097 SDOperand HiOps[3] = { LHSH, RHSH };
1099 if (N->getOpcode() == ISD::ADD) {
1100 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1101 HiOps[2] = Lo.getValue(1);
1102 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1104 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1105 HiOps[2] = Lo.getValue(1);
1106 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1110 void DAGTypeLegalizer::ExpandResult_ADDSUBC(SDNode *N,
1111 SDOperand &Lo, SDOperand &Hi) {
1112 // Expand the subcomponents.
1113 SDOperand LHSL, LHSH, RHSL, RHSH;
1114 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1115 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1116 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1117 SDOperand LoOps[2] = { LHSL, RHSL };
1118 SDOperand HiOps[3] = { LHSH, RHSH };
1120 if (N->getOpcode() == ISD::ADDC) {
1121 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1122 HiOps[2] = Lo.getValue(1);
1123 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1125 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1126 HiOps[2] = Lo.getValue(1);
1127 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1130 // Legalized the flag result - switch anything that used the old flag to
1132 ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1135 void DAGTypeLegalizer::ExpandResult_ADDSUBE(SDNode *N,
1136 SDOperand &Lo, SDOperand &Hi) {
1137 // Expand the subcomponents.
1138 SDOperand LHSL, LHSH, RHSL, RHSH;
1139 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1140 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1141 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1142 SDOperand LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1143 SDOperand HiOps[3] = { LHSH, RHSH };
1145 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1146 HiOps[2] = Lo.getValue(1);
1147 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1149 // Legalized the flag result - switch anything that used the old flag to
1151 ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1154 void DAGTypeLegalizer::ExpandResult_MUL(SDNode *N,
1155 SDOperand &Lo, SDOperand &Hi) {
1156 MVT::ValueType VT = N->getValueType(0);
1157 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1159 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1160 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1161 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1162 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1163 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1164 SDOperand LL, LH, RL, RH;
1165 GetExpandedOp(N->getOperand(0), LL, LH);
1166 GetExpandedOp(N->getOperand(1), RL, RH);
1167 unsigned BitSize = MVT::getSizeInBits(NVT);
1168 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1169 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1171 // FIXME: generalize this to handle other bit sizes
1172 if (LHSSB == 32 && RHSSB == 32 &&
1173 DAG.MaskedValueIsZero(N->getOperand(0), 0xFFFFFFFF00000000ULL) &&
1174 DAG.MaskedValueIsZero(N->getOperand(1), 0xFFFFFFFF00000000ULL)) {
1175 // The inputs are both zero-extended.
1177 // We can emit a umul_lohi.
1178 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1179 Hi = SDOperand(Lo.Val, 1);
1183 // We can emit a mulhu+mul.
1184 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1185 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1189 if (LHSSB > BitSize && RHSSB > BitSize) {
1190 // The input values are both sign-extended.
1192 // We can emit a smul_lohi.
1193 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1194 Hi = SDOperand(Lo.Val, 1);
1198 // We can emit a mulhs+mul.
1199 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1200 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1205 // Lo,Hi = umul LHS, RHS.
1206 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1207 DAG.getVTList(NVT, NVT), LL, RL);
1209 Hi = UMulLOHI.getValue(1);
1210 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1211 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1212 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1213 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1220 // If nothing else, we can make a libcall.
1221 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), N,
1222 false/*sign irrelevant*/, Hi);
1227 void DAGTypeLegalizer::ExpandResult_Shift(SDNode *N,
1228 SDOperand &Lo, SDOperand &Hi) {
1229 MVT::ValueType VT = N->getValueType(0);
1231 // If we can emit an efficient shift operation, do so now. Check to see if
1232 // the RHS is a constant.
1233 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1234 return ExpandShiftByConstant(N, CN->getValue(), Lo, Hi);
1236 // If we can determine that the high bit of the shift is zero or one, even if
1237 // the low bits are variable, emit this shift in an optimized form.
1238 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1241 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1243 if (N->getOpcode() == ISD::SHL)
1244 PartsOpc = ISD::SHL_PARTS;
1245 else if (N->getOpcode() == ISD::SRL)
1246 PartsOpc = ISD::SRL_PARTS;
1248 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1249 PartsOpc = ISD::SRA_PARTS;
1252 // Next check to see if the target supports this SHL_PARTS operation or if it
1253 // will custom expand it.
1254 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1255 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1256 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1257 Action == TargetLowering::Custom) {
1258 // Expand the subcomponents.
1259 SDOperand LHSL, LHSH;
1260 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1262 SDOperand Ops[] = { LHSL, LHSH, N->getOperand(1) };
1263 MVT::ValueType VT = LHSL.getValueType();
1264 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1265 Hi = Lo.getValue(1);
1271 // Otherwise, emit a libcall.
1272 unsigned RuntimeCode = ; // SRL -> SRL_I64 etc.
1274 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), N,
1275 false/*lshr is unsigned*/, Hi);
1280 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1281 /// and the shift amount is a constant 'Amt'. Expand the operation.
1282 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1283 SDOperand &Lo, SDOperand &Hi) {
1284 // Expand the incoming operand to be shifted, so that we have its parts
1286 GetExpandedOp(N->getOperand(0), InL, InH);
1288 MVT::ValueType NVT = InL.getValueType();
1289 unsigned VTBits = MVT::getSizeInBits(N->getValueType(0));
1290 unsigned NVTBits = MVT::getSizeInBits(NVT);
1291 MVT::ValueType ShTy = N->getOperand(1).getValueType();
1293 if (N->getOpcode() == ISD::SHL) {
1295 Lo = Hi = DAG.getConstant(0, NVT);
1296 } else if (Amt > NVTBits) {
1297 Lo = DAG.getConstant(0, NVT);
1298 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1299 } else if (Amt == NVTBits) {
1300 Lo = DAG.getConstant(0, NVT);
1303 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1304 Hi = DAG.getNode(ISD::OR, NVT,
1305 DAG.getNode(ISD::SHL, NVT, InH,
1306 DAG.getConstant(Amt, ShTy)),
1307 DAG.getNode(ISD::SRL, NVT, InL,
1308 DAG.getConstant(NVTBits-Amt, ShTy)));
1313 if (N->getOpcode() == ISD::SRL) {
1315 Lo = DAG.getConstant(0, NVT);
1316 Hi = DAG.getConstant(0, NVT);
1317 } else if (Amt > NVTBits) {
1318 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1319 Hi = DAG.getConstant(0, NVT);
1320 } else if (Amt == NVTBits) {
1322 Hi = DAG.getConstant(0, NVT);
1324 Lo = DAG.getNode(ISD::OR, NVT,
1325 DAG.getNode(ISD::SRL, NVT, InL,
1326 DAG.getConstant(Amt, ShTy)),
1327 DAG.getNode(ISD::SHL, NVT, InH,
1328 DAG.getConstant(NVTBits-Amt, ShTy)));
1329 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1334 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1336 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1337 DAG.getConstant(NVTBits-1, ShTy));
1338 } else if (Amt > NVTBits) {
1339 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1340 DAG.getConstant(Amt-NVTBits, ShTy));
1341 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1342 DAG.getConstant(NVTBits-1, ShTy));
1343 } else if (Amt == NVTBits) {
1345 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1346 DAG.getConstant(NVTBits-1, ShTy));
1348 Lo = DAG.getNode(ISD::OR, NVT,
1349 DAG.getNode(ISD::SRL, NVT, InL,
1350 DAG.getConstant(Amt, ShTy)),
1351 DAG.getNode(ISD::SHL, NVT, InH,
1352 DAG.getConstant(NVTBits-Amt, ShTy)));
1353 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1357 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1358 /// this shift based on knowledge of the high bit of the shift amount. If we
1359 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1361 bool DAGTypeLegalizer::
1362 ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
1363 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1364 unsigned NVTBits = MVT::getSizeInBits(NVT);
1365 assert(!(NVTBits & (NVTBits - 1)) &&
1366 "Expanded integer type size not a power of two!");
1368 uint64_t HighBitMask = NVTBits, KnownZero, KnownOne;
1369 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1371 // If we don't know anything about the high bit, exit.
1372 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1375 // Get the incoming operand to be shifted.
1377 GetExpandedOp(N->getOperand(0), InL, InH);
1378 SDOperand Amt = N->getOperand(1);
1380 // If we know that the high bit of the shift amount is one, then we can do
1381 // this as a couple of simple shifts.
1382 if (KnownOne & HighBitMask) {
1383 // Mask out the high bit, which we know is set.
1384 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
1385 DAG.getConstant(NVTBits-1, Amt.getValueType()));
1387 switch (N->getOpcode()) {
1388 default: assert(0 && "Unknown shift");
1390 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1391 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1394 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1395 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1398 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1399 DAG.getConstant(NVTBits-1, Amt.getValueType()));
1400 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1405 // If we know that the high bit of the shift amount is zero, then we can do
1406 // this as a couple of simple shifts.
1407 assert((KnownZero & HighBitMask) && "Bad mask computation above");
1410 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
1411 DAG.getConstant(NVTBits, Amt.getValueType()),
1414 switch (N->getOpcode()) {
1415 default: assert(0 && "Unknown shift");
1416 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1418 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1421 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1422 Hi = DAG.getNode(ISD::OR, NVT,
1423 DAG.getNode(Op1, NVT, InH, Amt),
1424 DAG.getNode(Op2, NVT, InL, Amt2));
1429 //===----------------------------------------------------------------------===//
1430 // Operand Promotion
1431 //===----------------------------------------------------------------------===//
1433 /// PromoteOperand - This method is called when the specified operand of the
1434 /// specified node is found to need promotion. At this point, all of the result
1435 /// types of the node are known to be legal, but other operands of the node may
1436 /// need promotion or expansion as well as the specified one.
1437 bool DAGTypeLegalizer::PromoteOperand(SDNode *N, unsigned OpNo) {
1438 DEBUG(cerr << "Promote node operand: "; N->dump(&DAG); cerr << "\n");
1440 switch (N->getOpcode()) {
1443 cerr << "PromoteOperand Op #" << OpNo << ": ";
1444 N->dump(&DAG); cerr << "\n";
1446 assert(0 && "Do not know how to promote this operator's operand!");
1449 case ISD::ANY_EXTEND: Res = PromoteOperand_ANY_EXTEND(N); break;
1450 case ISD::ZERO_EXTEND: Res = PromoteOperand_ZERO_EXTEND(N); break;
1451 case ISD::SIGN_EXTEND: Res = PromoteOperand_SIGN_EXTEND(N); break;
1452 case ISD::TRUNCATE: Res = PromoteOperand_TRUNCATE(N); break;
1453 case ISD::FP_EXTEND: Res = PromoteOperand_FP_EXTEND(N); break;
1454 case ISD::FP_ROUND: Res = PromoteOperand_FP_ROUND(N); break;
1455 case ISD::SINT_TO_FP:
1456 case ISD::UINT_TO_FP: Res = PromoteOperand_INT_TO_FP(N); break;
1458 case ISD::SELECT: Res = PromoteOperand_SELECT(N, OpNo); break;
1459 case ISD::BRCOND: Res = PromoteOperand_BRCOND(N, OpNo); break;
1460 case ISD::BR_CC: Res = PromoteOperand_BR_CC(N, OpNo); break;
1461 case ISD::SETCC: Res = PromoteOperand_SETCC(N, OpNo); break;
1463 case ISD::STORE: Res = PromoteOperand_STORE(cast<StoreSDNode>(N),
1467 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
1470 // If the result is null, the sub-method took care of registering results etc.
1471 if (!Res.Val) return false;
1472 // If the result is N, the sub-method updated N in place.
1474 // Mark N as new and remark N and its operands. This allows us to correctly
1475 // revisit N if it needs another step of promotion and allows us to visit
1476 // any new operands to N.
1477 N->setNodeId(NewNode);
1482 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1483 "Invalid operand expansion");
1485 ReplaceLegalValueWith(SDOperand(N, 0), Res);
1489 SDOperand DAGTypeLegalizer::PromoteOperand_ANY_EXTEND(SDNode *N) {
1490 SDOperand Op = GetPromotedOp(N->getOperand(0));
1491 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1494 SDOperand DAGTypeLegalizer::PromoteOperand_ZERO_EXTEND(SDNode *N) {
1495 SDOperand Op = GetPromotedOp(N->getOperand(0));
1496 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1497 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
1500 SDOperand DAGTypeLegalizer::PromoteOperand_SIGN_EXTEND(SDNode *N) {
1501 SDOperand Op = GetPromotedOp(N->getOperand(0));
1502 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1503 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
1504 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1507 SDOperand DAGTypeLegalizer::PromoteOperand_TRUNCATE(SDNode *N) {
1508 SDOperand Op = GetPromotedOp(N->getOperand(0));
1509 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
1512 SDOperand DAGTypeLegalizer::PromoteOperand_FP_EXTEND(SDNode *N) {
1513 SDOperand Op = GetPromotedOp(N->getOperand(0));
1514 return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
1517 SDOperand DAGTypeLegalizer::PromoteOperand_FP_ROUND(SDNode *N) {
1518 SDOperand Op = GetPromotedOp(N->getOperand(0));
1519 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op);
1522 SDOperand DAGTypeLegalizer::PromoteOperand_INT_TO_FP(SDNode *N) {
1523 SDOperand In = GetPromotedOp(N->getOperand(0));
1524 MVT::ValueType OpVT = N->getOperand(0).getValueType();
1525 if (N->getOpcode() == ISD::UINT_TO_FP)
1526 In = DAG.getZeroExtendInReg(In, OpVT);
1528 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
1529 In, DAG.getValueType(OpVT));
1531 return DAG.UpdateNodeOperands(SDOperand(N, 0), In);
1534 SDOperand DAGTypeLegalizer::PromoteOperand_SELECT(SDNode *N, unsigned OpNo) {
1535 assert(OpNo == 0 && "Only know how to promote condition");
1536 SDOperand Cond = GetPromotedOp(N->getOperand(0)); // Promote the condition.
1538 // The top bits of the promoted condition are not necessarily zero, ensure
1539 // that the value is properly zero extended.
1540 if (!DAG.MaskedValueIsZero(Cond,
1541 MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1542 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1543 MarkNewNodes(Cond.Val);
1546 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1547 return DAG.UpdateNodeOperands(SDOperand(N, 0), Cond, N->getOperand(1),
1551 SDOperand DAGTypeLegalizer::PromoteOperand_BRCOND(SDNode *N, unsigned OpNo) {
1552 assert(OpNo == 1 && "only know how to promote condition");
1553 SDOperand Cond = GetPromotedOp(N->getOperand(1)); // Promote the condition.
1555 // The top bits of the promoted condition are not necessarily zero, ensure
1556 // that the value is properly zero extended.
1557 if (!DAG.MaskedValueIsZero(Cond,
1558 MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1559 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1560 MarkNewNodes(Cond.Val);
1563 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1564 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0), Cond,
1568 SDOperand DAGTypeLegalizer::PromoteOperand_BR_CC(SDNode *N, unsigned OpNo) {
1569 assert(OpNo == 2 && "Don't know how to promote this operand");
1571 SDOperand LHS = N->getOperand(2);
1572 SDOperand RHS = N->getOperand(3);
1573 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1575 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1577 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
1578 N->getOperand(1), LHS, RHS, N->getOperand(4));
1581 SDOperand DAGTypeLegalizer::PromoteOperand_SETCC(SDNode *N, unsigned OpNo) {
1582 assert(OpNo == 0 && "Don't know how to promote this operand");
1584 SDOperand LHS = N->getOperand(0);
1585 SDOperand RHS = N->getOperand(1);
1586 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1588 // The CC (#2) is always legal.
1589 return DAG.UpdateNodeOperands(SDOperand(N, 0), LHS, RHS, N->getOperand(2));
1592 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1593 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1594 void DAGTypeLegalizer::PromoteSetCCOperands(SDOperand &NewLHS,SDOperand &NewRHS,
1595 ISD::CondCode CCCode) {
1596 MVT::ValueType VT = NewLHS.getValueType();
1598 // Get the promoted values.
1599 NewLHS = GetPromotedOp(NewLHS);
1600 NewRHS = GetPromotedOp(NewRHS);
1602 // If this is an FP compare, the operands have already been extended.
1603 if (!MVT::isInteger(NewLHS.getValueType()))
1606 // Otherwise, we have to insert explicit sign or zero extends. Note
1607 // that we could insert sign extends for ALL conditions, but zero extend
1608 // is cheaper on many machines (an AND instead of two shifts), so prefer
1611 default: assert(0 && "Unknown integer comparison!");
1618 // ALL of these operations will work if we either sign or zero extend
1619 // the operands (including the unsigned comparisons!). Zero extend is
1620 // usually a simpler/cheaper operation, so prefer it.
1621 NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
1622 NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
1628 NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
1629 DAG.getValueType(VT));
1630 NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
1631 DAG.getValueType(VT));
1636 SDOperand DAGTypeLegalizer::PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo){
1637 SDOperand Ch = N->getChain(), Ptr = N->getBasePtr();
1638 int SVOffset = N->getSrcValueOffset();
1639 unsigned Alignment = N->getAlignment();
1640 bool isVolatile = N->isVolatile();
1642 SDOperand Val = GetPromotedOp(N->getValue()); // Get promoted value.
1644 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
1646 // Truncate the value and store the result.
1647 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
1648 SVOffset, N->getStoredVT(),
1649 isVolatile, Alignment);
1653 //===----------------------------------------------------------------------===//
1654 // Operand Expansion
1655 //===----------------------------------------------------------------------===//
1657 /// ExpandOperand - This method is called when the specified operand of the
1658 /// specified node is found to need expansion. At this point, all of the result
1659 /// types of the node are known to be legal, but other operands of the node may
1660 /// need promotion or expansion as well as the specified one.
1661 bool DAGTypeLegalizer::ExpandOperand(SDNode *N, unsigned OpNo) {
1662 DEBUG(cerr << "Expand node operand: "; N->dump(&DAG); cerr << "\n");
1664 switch (N->getOpcode()) {
1667 cerr << "ExpandOperand Op #" << OpNo << ": ";
1668 N->dump(&DAG); cerr << "\n";
1670 assert(0 && "Do not know how to expand this operator's operand!");
1673 case ISD::TRUNCATE: Res = ExpandOperand_TRUNCATE(N); break;
1674 case ISD::BIT_CONVERT: Res = ExpandOperand_BIT_CONVERT(N); break;
1676 case ISD::SINT_TO_FP:
1677 Res = ExpandOperand_SINT_TO_FP(N->getOperand(0), N->getValueType(0));
1679 case ISD::UINT_TO_FP:
1680 Res = ExpandOperand_UINT_TO_FP(N->getOperand(0), N->getValueType(0));
1682 case ISD::EXTRACT_ELEMENT: Res = ExpandOperand_EXTRACT_ELEMENT(N); break;
1683 case ISD::SETCC: Res = ExpandOperand_SETCC(N); break;
1685 case ISD::STORE: Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo); break;
1688 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
1691 // If the result is null, the sub-method took care of registering results etc.
1692 if (!Res.Val) return false;
1693 // If the result is N, the sub-method updated N in place. Check to see if any
1694 // operands are new, and if so, mark them.
1696 // Mark N as new and remark N and its operands. This allows us to correctly
1697 // revisit N if it needs another step of promotion and allows us to visit
1698 // any new operands to N.
1699 N->setNodeId(NewNode);
1704 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1705 "Invalid operand expansion");
1707 ReplaceLegalValueWith(SDOperand(N, 0), Res);
1711 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
1713 GetExpandedOp(N->getOperand(0), InL, InH);
1714 // Just truncate the low part of the source.
1715 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
1718 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
1719 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1722 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source,
1723 MVT::ValueType DestTy) {
1724 // We know the destination is legal, but that the input needs to be expanded.
1725 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1727 // Check to see if the target has a custom way to lower this. If so, use it.
1728 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
1729 default: assert(0 && "This action not implemented for this operation!");
1730 case TargetLowering::Legal:
1731 case TargetLowering::Expand:
1732 break; // This case is handled below.
1733 case TargetLowering::Custom:
1734 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
1736 if (NV.Val) return NV;
1737 break; // The target lowered this.
1741 if (DestTy == MVT::f32)
1742 LC = RTLIB::SINTTOFP_I64_F32;
1744 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1745 LC = RTLIB::SINTTOFP_I64_F64;
1748 assert(0 && "FIXME: no libcalls yet!");
1751 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
1752 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
1753 SDOperand UnusedHiPart;
1754 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
1758 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source,
1759 MVT::ValueType DestTy) {
1760 // We know the destination is legal, but that the input needs to be expanded.
1761 assert(getTypeAction(Source.getValueType()) == Expand &&
1762 "This is not an expansion!");
1763 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1765 // If this is unsigned, and not supported, first perform the conversion to
1766 // signed, then adjust the result if the sign bit is set.
1767 SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
1769 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1770 // incoming integer is set. To handle this, we dynamically test to see if
1771 // it is set, and, if so, add a fudge factor.
1773 GetExpandedOp(Source, Lo, Hi);
1775 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
1776 DAG.getConstant(0, Hi.getValueType()),
1778 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1779 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1780 SignSet, Four, Zero);
1781 uint64_t FF = 0x5f800000ULL;
1782 if (TLI.isLittleEndian()) FF <<= 32;
1783 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
1785 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
1786 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1787 SDOperand FudgeInReg;
1788 if (DestTy == MVT::f32)
1789 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
1790 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
1791 // FIXME: Avoid the extend by construction the right constantpool?
1792 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
1793 CPIdx, NULL, 0, MVT::f32);
1795 assert(0 && "Unexpected conversion");
1797 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
1800 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
1802 GetExpandedOp(N->getOperand(0), Lo, Hi);
1803 return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
1806 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
1807 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1808 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1809 ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1811 // If ExpandSetCCOperands returned a scalar, use it.
1812 if (NewRHS.Val == 0) return NewLHS;
1814 // Otherwise, update N to have the operands specified.
1815 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1816 DAG.getCondCode(CCCode));
1819 /// ExpandSetCCOperands - Expand the operands of a comparison. This code is
1820 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1821 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
1822 ISD::CondCode &CCCode) {
1823 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1824 GetExpandedOp(NewLHS, LHSLo, LHSHi);
1825 GetExpandedOp(NewRHS, RHSLo, RHSHi);
1827 MVT::ValueType VT = NewLHS.getValueType();
1828 if (VT == MVT::f32 || VT == MVT::f64) {
1829 assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1832 if (VT == MVT::ppcf128) {
1833 // FIXME: This generated code sucks. We want to generate
1834 // FCMP crN, hi1, hi2
1836 // FCMP crN, lo1, lo2
1837 // The following can be improved, but not that much.
1838 SDOperand Tmp1, Tmp2, Tmp3;
1839 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1840 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1841 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1842 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1843 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1844 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1845 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1846 NewRHS = SDOperand(); // LHS is the result, not a compare.
1851 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1853 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1854 if (RHSCST->isAllOnesValue()) {
1855 // Equality comparison to -1.
1856 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1861 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1862 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1863 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1864 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1868 // If this is a comparison of the sign bit, just look at the top part.
1870 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1871 if ((CCCode == ISD::SETLT && CST->getValue() == 0) || // X < 0
1872 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1878 // FIXME: This generated code sucks.
1879 ISD::CondCode LowCC;
1881 default: assert(0 && "Unknown integer setcc!");
1883 case ISD::SETULT: LowCC = ISD::SETULT; break;
1885 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1887 case ISD::SETULE: LowCC = ISD::SETULE; break;
1889 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1892 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1893 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1894 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1896 // NOTE: on targets without efficient SELECT of bools, we can always use
1897 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1898 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1899 SDOperand Tmp1, Tmp2;
1900 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1901 false, DagCombineInfo);
1903 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1904 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1905 CCCode, false, DagCombineInfo);
1907 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1908 DAG.getCondCode(CCCode));
1910 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1911 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1912 if ((Tmp1C && Tmp1C->getValue() == 0) ||
1913 (Tmp2C && Tmp2C->getValue() == 0 &&
1914 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1915 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1916 (Tmp2C && Tmp2C->getValue() == 1 &&
1917 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1918 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1919 // low part is known false, returns high part.
1920 // For LE / GE, if high part is known false, ignore the low part.
1921 // For LT / GT, if high part is known true, ignore the low part.
1923 NewRHS = SDOperand();
1927 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1928 ISD::SETEQ, false, DagCombineInfo);
1930 NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1931 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1932 NewLHS, Tmp1, Tmp2);
1933 NewRHS = SDOperand();
1936 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1937 assert(OpNo == 1 && "Can only expand the stored value so far");
1938 assert(!N->isTruncatingStore() && "Can't expand truncstore!");
1940 unsigned IncrementSize = 0;
1943 // If this is a vector type, then we have to calculate the increment as
1944 // the product of the element size in bytes, and the number of elements
1945 // in the high half of the vector.
1946 if (MVT::isVector(N->getValue().getValueType())) {
1947 assert(0 && "Vectors not supported yet");
1949 SDNode *InVal = ST->getValue().Val;
1950 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1951 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
1953 // Figure out if there is a simple type corresponding to this Vector
1954 // type. If so, convert to the vector type.
1955 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1956 if (TLI.isTypeLegal(TVT)) {
1957 // Turn this into a normal store of the vector type.
1958 Tmp3 = LegalizeOp(Node->getOperand(1));
1959 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1960 SVOffset, isVolatile, Alignment);
1961 Result = LegalizeOp(Result);
1963 } else if (NumElems == 1) {
1964 // Turn this into a normal store of the scalar type.
1965 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
1966 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1967 SVOffset, isVolatile, Alignment);
1968 // The scalarized value type may not be legal, e.g. it might require
1969 // promotion or expansion. Relegalize the scalar store.
1970 return LegalizeOp(Result);
1972 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1973 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1977 GetExpandedOp(N->getValue(), Lo, Hi);
1978 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
1980 if (!TLI.isLittleEndian())
1984 SDOperand Chain = N->getChain();
1985 SDOperand Ptr = N->getBasePtr();
1986 int SVOffset = N->getSrcValueOffset();
1987 unsigned Alignment = N->getAlignment();
1988 bool isVolatile = N->isVolatile();
1990 Lo = DAG.getStore(Chain, Lo, Ptr, N->getSrcValue(),
1991 SVOffset, isVolatile, Alignment);
1993 assert(Hi.Val && "FIXME: int <-> float should be handled with promote!");
1995 if (Hi.Val == NULL) {
1996 // Must be int <-> float one-to-one expansion.
2001 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2002 getIntPtrConstant(IncrementSize));
2003 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
2004 Hi = DAG.getStore(Chain, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
2005 isVolatile, std::max(Alignment, IncrementSize));
2006 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2010 //===----------------------------------------------------------------------===//
2012 //===----------------------------------------------------------------------===//
2014 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
2015 /// only uses types natively supported by the target.
2017 /// Note that this is an involved process that may invalidate pointers into
2019 void SelectionDAG::LegalizeTypes() {
2020 DAGTypeLegalizer(*this).run();