LegalizeTypes.cpp revision 7a30bc4e7c034ecd0a3bcbfb385ac2a129e6583c
1//===-- LegalizeTypes.cpp - Common code for DAG type legalizer ------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the SelectionDAG::LegalizeTypes method. It transforms 11// an arbitrary well-formed SelectionDAG to only consist of legal types. This 12// is common code shared among the LegalizeTypes*.cpp files. 13// 14//===----------------------------------------------------------------------===// 15 16#include "LegalizeTypes.h" 17#include "llvm/CallingConv.h" 18#include "llvm/Support/CommandLine.h" 19#include "llvm/Target/TargetData.h" 20using namespace llvm; 21 22/// run - This is the main entry point for the type legalizer. This does a 23/// top-down traversal of the dag, legalizing types as it goes. 24void DAGTypeLegalizer::run() { 25 // Create a dummy node (which is not added to allnodes), that adds a reference 26 // to the root node, preventing it from being deleted, and tracking any 27 // changes of the root. 28 HandleSDNode Dummy(DAG.getRoot()); 29 30 // The root of the dag may dangle to deleted nodes until the type legalizer is 31 // done. Set it to null to avoid confusion. 32 DAG.setRoot(SDValue()); 33 34 // Walk all nodes in the graph, assigning them a NodeID of 'ReadyToProcess' 35 // (and remembering them) if they are leaves and assigning 'NewNode' if 36 // non-leaves. 37 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 38 E = DAG.allnodes_end(); I != E; ++I) { 39 if (I->getNumOperands() == 0) { 40 I->setNodeId(ReadyToProcess); 41 Worklist.push_back(I); 42 } else { 43 I->setNodeId(NewNode); 44 } 45 } 46 47 // Now that we have a set of nodes to process, handle them all. 48 while (!Worklist.empty()) { 49 SDNode *N = Worklist.back(); 50 Worklist.pop_back(); 51 assert(N->getNodeId() == ReadyToProcess && 52 "Node should be ready if on worklist!"); 53 54 if (IgnoreNodeResults(N)) 55 goto ScanOperands; 56 57 // Scan the values produced by the node, checking to see if any result 58 // types are illegal. 59 for (unsigned i = 0, NumResults = N->getNumValues(); i < NumResults; ++i) { 60 MVT ResultVT = N->getValueType(i); 61 switch (getTypeAction(ResultVT)) { 62 default: 63 assert(false && "Unknown action!"); 64 case Legal: 65 break; 66 case PromoteInteger: 67 PromoteIntegerResult(N, i); 68 goto NodeDone; 69 case ExpandInteger: 70 ExpandIntegerResult(N, i); 71 goto NodeDone; 72 case SoftenFloat: 73 SoftenFloatResult(N, i); 74 goto NodeDone; 75 case ExpandFloat: 76 ExpandFloatResult(N, i); 77 goto NodeDone; 78 case ScalarizeVector: 79 ScalarizeVectorResult(N, i); 80 goto NodeDone; 81 case SplitVector: 82 SplitVectorResult(N, i); 83 goto NodeDone; 84 } 85 } 86 87ScanOperands: 88 // Scan the operand list for the node, handling any nodes with operands that 89 // are illegal. 90 { 91 unsigned NumOperands = N->getNumOperands(); 92 bool NeedsRevisit = false; 93 unsigned i; 94 for (i = 0; i != NumOperands; ++i) { 95 if (IgnoreNodeResults(N->getOperand(i).Val)) 96 continue; 97 98 MVT OpVT = N->getOperand(i).getValueType(); 99 switch (getTypeAction(OpVT)) { 100 default: 101 assert(false && "Unknown action!"); 102 case Legal: 103 continue; 104 case PromoteInteger: 105 NeedsRevisit = PromoteIntegerOperand(N, i); 106 break; 107 case ExpandInteger: 108 NeedsRevisit = ExpandIntegerOperand(N, i); 109 break; 110 case SoftenFloat: 111 NeedsRevisit = SoftenFloatOperand(N, i); 112 break; 113 case ExpandFloat: 114 NeedsRevisit = ExpandFloatOperand(N, i); 115 break; 116 case ScalarizeVector: 117 NeedsRevisit = ScalarizeVectorOperand(N, i); 118 break; 119 case SplitVector: 120 NeedsRevisit = SplitVectorOperand(N, i); 121 break; 122 } 123 break; 124 } 125 126 // If the node needs revisiting, don't add all users to the worklist etc. 127 if (NeedsRevisit) 128 continue; 129 130 if (i == NumOperands) { 131 DEBUG(cerr << "Legally typed node: "; N->dump(&DAG); cerr << "\n"); 132 } 133 } 134NodeDone: 135 136 // If we reach here, the node was processed, potentially creating new nodes. 137 // Mark it as processed and add its users to the worklist as appropriate. 138 N->setNodeId(Processed); 139 140 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 141 UI != E; ++UI) { 142 SDNode *User = *UI; 143 int NodeID = User->getNodeId(); 144 assert(NodeID != ReadyToProcess && NodeID != Processed && 145 "Invalid node id for user of unprocessed node!"); 146 147 // This node has two options: it can either be a new node or its Node ID 148 // may be a count of the number of operands it has that are not ready. 149 if (NodeID > 0) { 150 User->setNodeId(NodeID-1); 151 152 // If this was the last use it was waiting on, add it to the ready list. 153 if (NodeID-1 == ReadyToProcess) 154 Worklist.push_back(User); 155 continue; 156 } 157 158 // Otherwise, this node is new: this is the first operand of it that 159 // became ready. Its new NodeID is the number of operands it has minus 1 160 // (as this node is now processed). 161 assert(NodeID == NewNode && "Unknown node ID!"); 162 User->setNodeId(User->getNumOperands()-1); 163 164 // If the node only has a single operand, it is now ready. 165 if (User->getNumOperands() == 1) 166 Worklist.push_back(User); 167 } 168 } 169 170 // If the root changed (e.g. it was a dead load, update the root). 171 DAG.setRoot(Dummy.getValue()); 172 173 //DAG.viewGraph(); 174 175 // Remove dead nodes. This is important to do for cleanliness but also before 176 // the checking loop below. Implicit folding by the DAG.getNode operators can 177 // cause unreachable nodes to be around with their flags set to new. 178 DAG.RemoveDeadNodes(); 179 180 // In a debug build, scan all the nodes to make sure we found them all. This 181 // ensures that there are no cycles and that everything got processed. 182#ifndef NDEBUG 183 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 184 E = DAG.allnodes_end(); I != E; ++I) { 185 bool Failed = false; 186 187 // Check that all result types are legal. 188 if (!IgnoreNodeResults(I)) 189 for (unsigned i = 0, NumVals = I->getNumValues(); i < NumVals; ++i) 190 if (!isTypeLegal(I->getValueType(i))) { 191 cerr << "Result type " << i << " illegal!\n"; 192 Failed = true; 193 } 194 195 // Check that all operand types are legal. 196 for (unsigned i = 0, NumOps = I->getNumOperands(); i < NumOps; ++i) 197 if (!IgnoreNodeResults(I->getOperand(i).Val) && 198 !isTypeLegal(I->getOperand(i).getValueType())) { 199 cerr << "Operand type " << i << " illegal!\n"; 200 Failed = true; 201 } 202 203 if (I->getNodeId() != Processed) { 204 if (I->getNodeId() == NewNode) 205 cerr << "New node not 'noticed'?\n"; 206 else if (I->getNodeId() > 0) 207 cerr << "Operand not processed?\n"; 208 else if (I->getNodeId() == ReadyToProcess) 209 cerr << "Not added to worklist?\n"; 210 Failed = true; 211 } 212 213 if (Failed) { 214 I->dump(&DAG); cerr << "\n"; 215 abort(); 216 } 217 } 218#endif 219} 220 221/// AnalyzeNewNode - The specified node is the root of a subtree of potentially 222/// new nodes. Correct any processed operands (this may change the node) and 223/// calculate the NodeId. 224void DAGTypeLegalizer::AnalyzeNewNode(SDNode *&N) { 225 // If this was an existing node that is already done, we're done. 226 if (N->getNodeId() != NewNode) 227 return; 228 229 // Remove any stale map entries. 230 ExpungeNode(N); 231 232 // Okay, we know that this node is new. Recursively walk all of its operands 233 // to see if they are new also. The depth of this walk is bounded by the size 234 // of the new tree that was constructed (usually 2-3 nodes), so we don't worry 235 // about revisiting of nodes. 236 // 237 // As we walk the operands, keep track of the number of nodes that are 238 // processed. If non-zero, this will become the new nodeid of this node. 239 // Already processed operands may need to be remapped to the node that 240 // replaced them, which can result in our node changing. Since remapping 241 // is rare, the code tries to minimize overhead in the non-remapping case. 242 243 SmallVector<SDValue, 8> NewOps; 244 unsigned NumProcessed = 0; 245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 246 SDValue OrigOp = N->getOperand(i); 247 SDValue Op = OrigOp; 248 249 if (Op.Val->getNodeId() == Processed) 250 RemapNode(Op); 251 252 if (Op.Val->getNodeId() == NewNode) 253 AnalyzeNewNode(Op.Val); 254 else if (Op.Val->getNodeId() == Processed) 255 ++NumProcessed; 256 257 if (!NewOps.empty()) { 258 // Some previous operand changed. Add this one to the list. 259 NewOps.push_back(Op); 260 } else if (Op != OrigOp) { 261 // This is the first operand to change - add all operands so far. 262 for (unsigned j = 0; j < i; ++j) 263 NewOps.push_back(N->getOperand(j)); 264 NewOps.push_back(Op); 265 } 266 } 267 268 // Some operands changed - update the node. 269 if (!NewOps.empty()) 270 N = DAG.UpdateNodeOperands(SDValue(N, 0), &NewOps[0], NewOps.size()).Val; 271 272 N->setNodeId(N->getNumOperands()-NumProcessed); 273 if (N->getNodeId() == ReadyToProcess) 274 Worklist.push_back(N); 275} 276 277namespace { 278 /// NodeUpdateListener - This class is a DAGUpdateListener that listens for 279 /// updates to nodes and recomputes their ready state. 280 class VISIBILITY_HIDDEN NodeUpdateListener : 281 public SelectionDAG::DAGUpdateListener { 282 DAGTypeLegalizer &DTL; 283 public: 284 explicit NodeUpdateListener(DAGTypeLegalizer &dtl) : DTL(dtl) {} 285 286 virtual void NodeDeleted(SDNode *N, SDNode *E) { 287 assert(N->getNodeId() != DAGTypeLegalizer::Processed && 288 N->getNodeId() != DAGTypeLegalizer::ReadyToProcess && 289 "RAUW deleted processed node!"); 290 // It is possible, though rare, for the deleted node N to occur as a 291 // target in a map, so note the replacement N -> E in ReplacedNodes. 292 assert(E && "Node not replaced?"); 293 DTL.NoteDeletion(N, E); 294 } 295 296 virtual void NodeUpdated(SDNode *N) { 297 // Node updates can mean pretty much anything. It is possible that an 298 // operand was set to something already processed (f.e.) in which case 299 // this node could become ready. Recompute its flags. 300 assert(N->getNodeId() != DAGTypeLegalizer::Processed && 301 N->getNodeId() != DAGTypeLegalizer::ReadyToProcess && 302 "RAUW updated processed node!"); 303 DTL.ReanalyzeNode(N); 304 } 305 }; 306} 307 308 309/// ReplaceValueWith - The specified value was legalized to the specified other 310/// value. If they are different, update the DAG and NodeIDs replacing any uses 311/// of From to use To instead. 312void DAGTypeLegalizer::ReplaceValueWith(SDValue From, SDValue To) { 313 if (From == To) return; 314 315 // If expansion produced new nodes, make sure they are properly marked. 316 ExpungeNode(From.Val); 317 AnalyzeNewNode(To.Val); // Expunges To. 318 319 // Anything that used the old node should now use the new one. Note that this 320 // can potentially cause recursive merging. 321 NodeUpdateListener NUL(*this); 322 DAG.ReplaceAllUsesOfValueWith(From, To, &NUL); 323 324 // The old node may still be present in a map like ExpandedIntegers or 325 // PromotedIntegers. Inform maps about the replacement. 326 ReplacedNodes[From] = To; 327} 328 329/// ReplaceNodeWith - Replace uses of the 'from' node's results with the 'to' 330/// node's results. The from and to node must define identical result types. 331void DAGTypeLegalizer::ReplaceNodeWith(SDNode *From, SDNode *To) { 332 if (From == To) return; 333 334 // If expansion produced new nodes, make sure they are properly marked. 335 ExpungeNode(From); 336 AnalyzeNewNode(To); // Expunges To. 337 338 assert(From->getNumValues() == To->getNumValues() && 339 "Node results don't match"); 340 341 // Anything that used the old node should now use the new one. Note that this 342 // can potentially cause recursive merging. 343 NodeUpdateListener NUL(*this); 344 DAG.ReplaceAllUsesWith(From, To, &NUL); 345 346 // The old node may still be present in a map like ExpandedIntegers or 347 // PromotedIntegers. Inform maps about the replacement. 348 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) { 349 assert(From->getValueType(i) == To->getValueType(i) && 350 "Node results don't match"); 351 ReplacedNodes[SDValue(From, i)] = SDValue(To, i); 352 } 353} 354 355/// RemapNode - If the specified value was already legalized to another value, 356/// replace it by that value. 357void DAGTypeLegalizer::RemapNode(SDValue &N) { 358 DenseMap<SDValue, SDValue>::iterator I = ReplacedNodes.find(N); 359 if (I != ReplacedNodes.end()) { 360 // Use path compression to speed up future lookups if values get multiply 361 // replaced with other values. 362 RemapNode(I->second); 363 N = I->second; 364 } 365} 366 367/// ExpungeNode - If N has a bogus mapping in ReplacedNodes, eliminate it. 368/// This can occur when a node is deleted then reallocated as a new node - 369/// the mapping in ReplacedNodes applies to the deleted node, not the new 370/// one. 371/// The only map that can have a deleted node as a source is ReplacedNodes. 372/// Other maps can have deleted nodes as targets, but since their looked-up 373/// values are always immediately remapped using RemapNode, resulting in a 374/// not-deleted node, this is harmless as long as ReplacedNodes/RemapNode 375/// always performs correct mappings. In order to keep the mapping correct, 376/// ExpungeNode should be called on any new nodes *before* adding them as 377/// either source or target to ReplacedNodes (which typically means calling 378/// Expunge when a new node is first seen, since it may no longer be marked 379/// NewNode by the time it is added to ReplacedNodes). 380void DAGTypeLegalizer::ExpungeNode(SDNode *N) { 381 if (N->getNodeId() != NewNode) 382 return; 383 384 // If N is not remapped by ReplacedNodes then there is nothing to do. 385 unsigned i, e; 386 for (i = 0, e = N->getNumValues(); i != e; ++i) 387 if (ReplacedNodes.find(SDValue(N, i)) != ReplacedNodes.end()) 388 break; 389 390 if (i == e) 391 return; 392 393 // Remove N from all maps - this is expensive but rare. 394 395 for (DenseMap<SDValue, SDValue>::iterator I = PromotedIntegers.begin(), 396 E = PromotedIntegers.end(); I != E; ++I) { 397 assert(I->first.Val != N); 398 RemapNode(I->second); 399 } 400 401 for (DenseMap<SDValue, SDValue>::iterator I = SoftenedFloats.begin(), 402 E = SoftenedFloats.end(); I != E; ++I) { 403 assert(I->first.Val != N); 404 RemapNode(I->second); 405 } 406 407 for (DenseMap<SDValue, SDValue>::iterator I = ScalarizedVectors.begin(), 408 E = ScalarizedVectors.end(); I != E; ++I) { 409 assert(I->first.Val != N); 410 RemapNode(I->second); 411 } 412 413 for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator 414 I = ExpandedIntegers.begin(), E = ExpandedIntegers.end(); I != E; ++I){ 415 assert(I->first.Val != N); 416 RemapNode(I->second.first); 417 RemapNode(I->second.second); 418 } 419 420 for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator 421 I = ExpandedFloats.begin(), E = ExpandedFloats.end(); I != E; ++I) { 422 assert(I->first.Val != N); 423 RemapNode(I->second.first); 424 RemapNode(I->second.second); 425 } 426 427 for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator 428 I = SplitVectors.begin(), E = SplitVectors.end(); I != E; ++I) { 429 assert(I->first.Val != N); 430 RemapNode(I->second.first); 431 RemapNode(I->second.second); 432 } 433 434 for (DenseMap<SDValue, SDValue>::iterator I = ReplacedNodes.begin(), 435 E = ReplacedNodes.end(); I != E; ++I) 436 RemapNode(I->second); 437 438 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) 439 ReplacedNodes.erase(SDValue(N, i)); 440} 441 442void DAGTypeLegalizer::SetPromotedInteger(SDValue Op, SDValue Result) { 443 AnalyzeNewNode(Result.Val); 444 445 SDValue &OpEntry = PromotedIntegers[Op]; 446 assert(OpEntry.Val == 0 && "Node is already promoted!"); 447 OpEntry = Result; 448} 449 450void DAGTypeLegalizer::SetSoftenedFloat(SDValue Op, SDValue Result) { 451 AnalyzeNewNode(Result.Val); 452 453 SDValue &OpEntry = SoftenedFloats[Op]; 454 assert(OpEntry.Val == 0 && "Node is already converted to integer!"); 455 OpEntry = Result; 456} 457 458void DAGTypeLegalizer::SetScalarizedVector(SDValue Op, SDValue Result) { 459 AnalyzeNewNode(Result.Val); 460 461 SDValue &OpEntry = ScalarizedVectors[Op]; 462 assert(OpEntry.Val == 0 && "Node is already scalarized!"); 463 OpEntry = Result; 464} 465 466void DAGTypeLegalizer::GetExpandedInteger(SDValue Op, SDValue &Lo, 467 SDValue &Hi) { 468 std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[Op]; 469 RemapNode(Entry.first); 470 RemapNode(Entry.second); 471 assert(Entry.first.Val && "Operand isn't expanded"); 472 Lo = Entry.first; 473 Hi = Entry.second; 474} 475 476void DAGTypeLegalizer::SetExpandedInteger(SDValue Op, SDValue Lo, 477 SDValue Hi) { 478 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 479 AnalyzeNewNode(Lo.Val); 480 AnalyzeNewNode(Hi.Val); 481 482 // Remember that this is the result of the node. 483 std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[Op]; 484 assert(Entry.first.Val == 0 && "Node already expanded"); 485 Entry.first = Lo; 486 Entry.second = Hi; 487} 488 489void DAGTypeLegalizer::GetExpandedFloat(SDValue Op, SDValue &Lo, 490 SDValue &Hi) { 491 std::pair<SDValue, SDValue> &Entry = ExpandedFloats[Op]; 492 RemapNode(Entry.first); 493 RemapNode(Entry.second); 494 assert(Entry.first.Val && "Operand isn't expanded"); 495 Lo = Entry.first; 496 Hi = Entry.second; 497} 498 499void DAGTypeLegalizer::SetExpandedFloat(SDValue Op, SDValue Lo, 500 SDValue Hi) { 501 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 502 AnalyzeNewNode(Lo.Val); 503 AnalyzeNewNode(Hi.Val); 504 505 // Remember that this is the result of the node. 506 std::pair<SDValue, SDValue> &Entry = ExpandedFloats[Op]; 507 assert(Entry.first.Val == 0 && "Node already expanded"); 508 Entry.first = Lo; 509 Entry.second = Hi; 510} 511 512void DAGTypeLegalizer::GetSplitVector(SDValue Op, SDValue &Lo, 513 SDValue &Hi) { 514 std::pair<SDValue, SDValue> &Entry = SplitVectors[Op]; 515 RemapNode(Entry.first); 516 RemapNode(Entry.second); 517 assert(Entry.first.Val && "Operand isn't split"); 518 Lo = Entry.first; 519 Hi = Entry.second; 520} 521 522void DAGTypeLegalizer::SetSplitVector(SDValue Op, SDValue Lo, 523 SDValue Hi) { 524 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 525 AnalyzeNewNode(Lo.Val); 526 AnalyzeNewNode(Hi.Val); 527 528 // Remember that this is the result of the node. 529 std::pair<SDValue, SDValue> &Entry = SplitVectors[Op]; 530 assert(Entry.first.Val == 0 && "Node already split"); 531 Entry.first = Lo; 532 Entry.second = Hi; 533} 534 535 536//===----------------------------------------------------------------------===// 537// Utilities. 538//===----------------------------------------------------------------------===// 539 540/// BitConvertToInteger - Convert to an integer of the same size. 541SDValue DAGTypeLegalizer::BitConvertToInteger(SDValue Op) { 542 unsigned BitWidth = Op.getValueType().getSizeInBits(); 543 return DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerVT(BitWidth), Op); 544} 545 546SDValue DAGTypeLegalizer::CreateStackStoreLoad(SDValue Op, 547 MVT DestVT) { 548 // Create the stack frame object. Make sure it is aligned for both 549 // the source and destination types. 550 unsigned SrcAlign = 551 TLI.getTargetData()->getPrefTypeAlignment(Op.getValueType().getTypeForMVT()); 552 SDValue FIPtr = DAG.CreateStackTemporary(DestVT, SrcAlign); 553 554 // Emit a store to the stack slot. 555 SDValue Store = DAG.getStore(DAG.getEntryNode(), Op, FIPtr, NULL, 0); 556 // Result is a load from the stack slot. 557 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); 558} 559 560/// JoinIntegers - Build an integer with low bits Lo and high bits Hi. 561SDValue DAGTypeLegalizer::JoinIntegers(SDValue Lo, SDValue Hi) { 562 MVT LVT = Lo.getValueType(); 563 MVT HVT = Hi.getValueType(); 564 MVT NVT = MVT::getIntegerVT(LVT.getSizeInBits() + HVT.getSizeInBits()); 565 566 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Lo); 567 Hi = DAG.getNode(ISD::ANY_EXTEND, NVT, Hi); 568 Hi = DAG.getNode(ISD::SHL, NVT, Hi, DAG.getConstant(LVT.getSizeInBits(), 569 TLI.getShiftAmountTy())); 570 return DAG.getNode(ISD::OR, NVT, Lo, Hi); 571} 572 573/// SplitInteger - Return the lower LoVT bits of Op in Lo and the upper HiVT 574/// bits in Hi. 575void DAGTypeLegalizer::SplitInteger(SDValue Op, 576 MVT LoVT, MVT HiVT, 577 SDValue &Lo, SDValue &Hi) { 578 assert(LoVT.getSizeInBits() + HiVT.getSizeInBits() == 579 Op.getValueType().getSizeInBits() && "Invalid integer splitting!"); 580 Lo = DAG.getNode(ISD::TRUNCATE, LoVT, Op); 581 Hi = DAG.getNode(ISD::SRL, Op.getValueType(), Op, 582 DAG.getConstant(LoVT.getSizeInBits(), 583 TLI.getShiftAmountTy())); 584 Hi = DAG.getNode(ISD::TRUNCATE, HiVT, Hi); 585} 586 587/// SplitInteger - Return the lower and upper halves of Op's bits in a value type 588/// half the size of Op's. 589void DAGTypeLegalizer::SplitInteger(SDValue Op, 590 SDValue &Lo, SDValue &Hi) { 591 MVT HalfVT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()/2); 592 SplitInteger(Op, HalfVT, HalfVT, Lo, Hi); 593} 594 595/// MakeLibCall - Generate a libcall taking the given operands as arguments and 596/// returning a result of type RetVT. 597SDValue DAGTypeLegalizer::MakeLibCall(RTLIB::Libcall LC, MVT RetVT, 598 const SDValue *Ops, unsigned NumOps, 599 bool isSigned) { 600 TargetLowering::ArgListTy Args; 601 Args.reserve(NumOps); 602 603 TargetLowering::ArgListEntry Entry; 604 for (unsigned i = 0; i != NumOps; ++i) { 605 Entry.Node = Ops[i]; 606 Entry.Ty = Entry.Node.getValueType().getTypeForMVT(); 607 Entry.isSExt = isSigned; 608 Entry.isZExt = !isSigned; 609 Args.push_back(Entry); 610 } 611 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 612 TLI.getPointerTy()); 613 614 const Type *RetTy = RetVT.getTypeForMVT(); 615 std::pair<SDValue,SDValue> CallInfo = 616 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false, 617 CallingConv::C, false, Callee, Args, DAG); 618 return CallInfo.first; 619} 620 621SDValue DAGTypeLegalizer::GetVectorElementPointer(SDValue VecPtr, MVT EltVT, 622 SDValue Index) { 623 // Make sure the index type is big enough to compute in. 624 if (Index.getValueType().bitsGT(TLI.getPointerTy())) 625 Index = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Index); 626 else 627 Index = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Index); 628 629 // Calculate the element offset and add it to the pointer. 630 unsigned EltSize = EltVT.getSizeInBits() / 8; // FIXME: should be ABI size. 631 632 Index = DAG.getNode(ISD::MUL, Index.getValueType(), Index, 633 DAG.getConstant(EltSize, Index.getValueType())); 634 return DAG.getNode(ISD::ADD, Index.getValueType(), Index, VecPtr); 635} 636 637/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type 638/// which is split into two not necessarily identical pieces. 639void DAGTypeLegalizer::GetSplitDestVTs(MVT InVT, MVT &LoVT, MVT &HiVT) { 640 if (!InVT.isVector()) { 641 LoVT = HiVT = TLI.getTypeToTransformTo(InVT); 642 } else { 643 MVT NewEltVT = InVT.getVectorElementType(); 644 unsigned NumElements = InVT.getVectorNumElements(); 645 if ((NumElements & (NumElements-1)) == 0) { // Simple power of two vector. 646 NumElements >>= 1; 647 LoVT = HiVT = MVT::getVectorVT(NewEltVT, NumElements); 648 } else { // Non-power-of-two vectors. 649 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements); 650 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo; 651 LoVT = MVT::getVectorVT(NewEltVT, NewNumElts_Lo); 652 HiVT = MVT::getVectorVT(NewEltVT, NewNumElts_Hi); 653 } 654 } 655} 656 657 658//===----------------------------------------------------------------------===// 659// Entry Point 660//===----------------------------------------------------------------------===// 661 662/// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that 663/// only uses types natively supported by the target. 664/// 665/// Note that this is an involved process that may invalidate pointers into 666/// the graph. 667void SelectionDAG::LegalizeTypes() { 668 DAGTypeLegalizer(*this).run(); 669} 670