LegalizeTypes.cpp revision dd1c20d2580236280694a22264fa26b36cb8fbe6
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" 19using namespace llvm; 20 21#ifndef NDEBUG 22static cl::opt<bool> 23ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden, 24 cl::desc("Pop up a window to show dags before legalize types")); 25#else 26static const bool ViewLegalizeTypesDAGs = 0; 27#endif 28 29 30 31/// run - This is the main entry point for the type legalizer. This does a 32/// top-down traversal of the dag, legalizing types as it goes. 33void DAGTypeLegalizer::run() { 34 // Create a dummy node (which is not added to allnodes), that adds a reference 35 // to the root node, preventing it from being deleted, and tracking any 36 // changes of the root. 37 HandleSDNode Dummy(DAG.getRoot()); 38 39 // The root of the dag may dangle to deleted nodes until the type legalizer is 40 // done. Set it to null to avoid confusion. 41 DAG.setRoot(SDOperand()); 42 43 // Walk all nodes in the graph, assigning them a NodeID of 'ReadyToProcess' 44 // (and remembering them) if they are leaves and assigning 'NewNode' if 45 // non-leaves. 46 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 47 E = DAG.allnodes_end(); I != E; ++I) { 48 if (I->getNumOperands() == 0) { 49 I->setNodeId(ReadyToProcess); 50 Worklist.push_back(I); 51 } else { 52 I->setNodeId(NewNode); 53 } 54 } 55 56 // Now that we have a set of nodes to process, handle them all. 57 while (!Worklist.empty()) { 58 SDNode *N = Worklist.back(); 59 Worklist.pop_back(); 60 assert(N->getNodeId() == ReadyToProcess && 61 "Node should be ready if on worklist!"); 62 63 // Scan the values produced by the node, checking to see if any result 64 // types are illegal. 65 unsigned i = 0; 66 unsigned NumResults = N->getNumValues(); 67 do { 68 MVT ResultVT = N->getValueType(i); 69 switch (getTypeAction(ResultVT)) { 70 default: 71 assert(false && "Unknown action!"); 72 case Legal: 73 break; 74 case PromoteInteger: 75 PromoteIntegerResult(N, i); 76 goto NodeDone; 77 case ExpandInteger: 78 ExpandIntegerResult(N, i); 79 goto NodeDone; 80 case SoftenFloat: 81 SoftenFloatResult(N, i); 82 goto NodeDone; 83 case ExpandFloat: 84 ExpandFloatResult(N, i); 85 goto NodeDone; 86 case Scalarize: 87 ScalarizeResult(N, i); 88 goto NodeDone; 89 case Split: 90 SplitResult(N, i); 91 goto NodeDone; 92 } 93 } while (++i < NumResults); 94 95 // Scan the operand list for the node, handling any nodes with operands that 96 // are illegal. 97 { 98 unsigned NumOperands = N->getNumOperands(); 99 bool NeedsRevisit = false; 100 for (i = 0; i != NumOperands; ++i) { 101 MVT OpVT = N->getOperand(i).getValueType(); 102 switch (getTypeAction(OpVT)) { 103 default: 104 assert(false && "Unknown action!"); 105 case Legal: 106 continue; 107 case PromoteInteger: 108 NeedsRevisit = PromoteIntegerOperand(N, i); 109 break; 110 case ExpandInteger: 111 NeedsRevisit = ExpandIntegerOperand(N, i); 112 break; 113 case SoftenFloat: 114 NeedsRevisit = SoftenFloatOperand(N, i); 115 break; 116 case ExpandFloat: 117 NeedsRevisit = ExpandFloatOperand(N, i); 118 break; 119 case Scalarize: 120 NeedsRevisit = ScalarizeOperand(N, i); 121 break; 122 case Split: 123 NeedsRevisit = SplitOperand(N, i); 124 break; 125 } 126 break; 127 } 128 129 // If the node needs revisiting, don't add all users to the worklist etc. 130 if (NeedsRevisit) 131 continue; 132 133 if (i == NumOperands) 134 DEBUG(cerr << "Legally typed node: "; N->dump(&DAG); cerr << "\n"); 135 } 136NodeDone: 137 138 // If we reach here, the node was processed, potentially creating new nodes. 139 // Mark it as processed and add its users to the worklist as appropriate. 140 N->setNodeId(Processed); 141 142 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 143 UI != E; ++UI) { 144 SDNode *User = UI->getUser(); 145 int NodeID = User->getNodeId(); 146 assert(NodeID != ReadyToProcess && NodeID != Processed && 147 "Invalid node id for user of unprocessed node!"); 148 149 // This node has two options: it can either be a new node or its Node ID 150 // may be a count of the number of operands it has that are not ready. 151 if (NodeID > 0) { 152 User->setNodeId(NodeID-1); 153 154 // If this was the last use it was waiting on, add it to the ready list. 155 if (NodeID-1 == ReadyToProcess) 156 Worklist.push_back(User); 157 continue; 158 } 159 160 // Otherwise, this node is new: this is the first operand of it that 161 // became ready. Its new NodeID is the number of operands it has minus 1 162 // (as this node is now processed). 163 assert(NodeID == NewNode && "Unknown node ID!"); 164 User->setNodeId(User->getNumOperands()-1); 165 166 // If the node only has a single operand, it is now ready. 167 if (User->getNumOperands() == 1) 168 Worklist.push_back(User); 169 } 170 } 171 172 // If the root changed (e.g. it was a dead load, update the root). 173 DAG.setRoot(Dummy.getValue()); 174 175 //DAG.viewGraph(); 176 177 // Remove dead nodes. This is important to do for cleanliness but also before 178 // the checking loop below. Implicit folding by the DAG.getNode operators can 179 // cause unreachable nodes to be around with their flags set to new. 180 DAG.RemoveDeadNodes(); 181 182 // In a debug build, scan all the nodes to make sure we found them all. This 183 // ensures that there are no cycles and that everything got processed. 184#ifndef NDEBUG 185 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 186 E = DAG.allnodes_end(); I != E; ++I) { 187 bool Failed = false; 188 189 // Check that all result types are legal. 190 for (unsigned i = 0, NumVals = I->getNumValues(); i < NumVals; ++i) 191 if (!isTypeLegal(I->getValueType(i))) { 192 cerr << "Result type " << i << " illegal!\n"; 193 Failed = true; 194 } 195 196 // Check that all operand types are legal. 197 for (unsigned i = 0, NumOps = I->getNumOperands(); i < NumOps; ++i) 198 if (!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<SDOperand, 8> NewOps; 244 unsigned NumProcessed = 0; 245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 246 SDOperand OrigOp = N->getOperand(i); 247 SDOperand 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(SDOperand(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(SDOperand From, SDOperand 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[SDOperand(From, i)] = SDOperand(To, i); 352 } 353} 354 355 356/// RemapNode - If the specified value was already legalized to another value, 357/// replace it by that value. 358void DAGTypeLegalizer::RemapNode(SDOperand &N) { 359 DenseMap<SDOperand, SDOperand>::iterator I = ReplacedNodes.find(N); 360 if (I != ReplacedNodes.end()) { 361 // Use path compression to speed up future lookups if values get multiply 362 // replaced with other values. 363 RemapNode(I->second); 364 N = I->second; 365 } 366} 367 368/// ExpungeNode - If N has a bogus mapping in ReplacedNodes, eliminate it. 369/// This can occur when a node is deleted then reallocated as a new node - 370/// the mapping in ReplacedNodes applies to the deleted node, not the new 371/// one. 372/// The only map that can have a deleted node as a source is ReplacedNodes. 373/// Other maps can have deleted nodes as targets, but since their looked-up 374/// values are always immediately remapped using RemapNode, resulting in a 375/// not-deleted node, this is harmless as long as ReplacedNodes/RemapNode 376/// always performs correct mappings. In order to keep the mapping correct, 377/// ExpungeNode should be called on any new nodes *before* adding them as 378/// either source or target to ReplacedNodes (which typically means calling 379/// Expunge when a new node is first seen, since it may no longer be marked 380/// NewNode by the time it is added to ReplacedNodes). 381void DAGTypeLegalizer::ExpungeNode(SDNode *N) { 382 if (N->getNodeId() != NewNode) 383 return; 384 385 // If N is not remapped by ReplacedNodes then there is nothing to do. 386 unsigned i, e; 387 for (i = 0, e = N->getNumValues(); i != e; ++i) 388 if (ReplacedNodes.find(SDOperand(N, i)) != ReplacedNodes.end()) 389 break; 390 391 if (i == e) 392 return; 393 394 // Remove N from all maps - this is expensive but rare. 395 396 for (DenseMap<SDOperand, SDOperand>::iterator I = PromotedIntegers.begin(), 397 E = PromotedIntegers.end(); I != E; ++I) { 398 assert(I->first.Val != N); 399 RemapNode(I->second); 400 } 401 402 for (DenseMap<SDOperand, SDOperand>::iterator I = SoftenedFloats.begin(), 403 E = SoftenedFloats.end(); I != E; ++I) { 404 assert(I->first.Val != N); 405 RemapNode(I->second); 406 } 407 408 for (DenseMap<SDOperand, SDOperand>::iterator I = ScalarizedVectors.begin(), 409 E = ScalarizedVectors.end(); I != E; ++I) { 410 assert(I->first.Val != N); 411 RemapNode(I->second); 412 } 413 414 for (DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator 415 I = ExpandedIntegers.begin(), E = ExpandedIntegers.end(); I != E; ++I){ 416 assert(I->first.Val != N); 417 RemapNode(I->second.first); 418 RemapNode(I->second.second); 419 } 420 421 for (DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator 422 I = ExpandedFloats.begin(), E = ExpandedFloats.end(); I != E; ++I) { 423 assert(I->first.Val != N); 424 RemapNode(I->second.first); 425 RemapNode(I->second.second); 426 } 427 428 for (DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator 429 I = SplitVectors.begin(), E = SplitVectors.end(); I != E; ++I) { 430 assert(I->first.Val != N); 431 RemapNode(I->second.first); 432 RemapNode(I->second.second); 433 } 434 435 for (DenseMap<SDOperand, SDOperand>::iterator I = ReplacedNodes.begin(), 436 E = ReplacedNodes.end(); I != E; ++I) 437 RemapNode(I->second); 438 439 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) 440 ReplacedNodes.erase(SDOperand(N, i)); 441} 442 443 444void DAGTypeLegalizer::SetPromotedInteger(SDOperand Op, SDOperand Result) { 445 AnalyzeNewNode(Result.Val); 446 447 SDOperand &OpEntry = PromotedIntegers[Op]; 448 assert(OpEntry.Val == 0 && "Node is already promoted!"); 449 OpEntry = Result; 450} 451 452void DAGTypeLegalizer::SetSoftenedFloat(SDOperand Op, SDOperand Result) { 453 AnalyzeNewNode(Result.Val); 454 455 SDOperand &OpEntry = SoftenedFloats[Op]; 456 assert(OpEntry.Val == 0 && "Node is already converted to integer!"); 457 OpEntry = Result; 458} 459 460void DAGTypeLegalizer::SetScalarizedVector(SDOperand Op, SDOperand Result) { 461 AnalyzeNewNode(Result.Val); 462 463 SDOperand &OpEntry = ScalarizedVectors[Op]; 464 assert(OpEntry.Val == 0 && "Node is already scalarized!"); 465 OpEntry = Result; 466} 467 468void DAGTypeLegalizer::GetExpandedInteger(SDOperand Op, SDOperand &Lo, 469 SDOperand &Hi) { 470 std::pair<SDOperand, SDOperand> &Entry = ExpandedIntegers[Op]; 471 RemapNode(Entry.first); 472 RemapNode(Entry.second); 473 assert(Entry.first.Val && "Operand isn't expanded"); 474 Lo = Entry.first; 475 Hi = Entry.second; 476} 477 478void DAGTypeLegalizer::SetExpandedInteger(SDOperand Op, SDOperand Lo, 479 SDOperand Hi) { 480 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 481 AnalyzeNewNode(Lo.Val); 482 AnalyzeNewNode(Hi.Val); 483 484 // Remember that this is the result of the node. 485 std::pair<SDOperand, SDOperand> &Entry = ExpandedIntegers[Op]; 486 assert(Entry.first.Val == 0 && "Node already expanded"); 487 Entry.first = Lo; 488 Entry.second = Hi; 489} 490 491void DAGTypeLegalizer::GetExpandedFloat(SDOperand Op, SDOperand &Lo, 492 SDOperand &Hi) { 493 std::pair<SDOperand, SDOperand> &Entry = ExpandedFloats[Op]; 494 RemapNode(Entry.first); 495 RemapNode(Entry.second); 496 assert(Entry.first.Val && "Operand isn't expanded"); 497 Lo = Entry.first; 498 Hi = Entry.second; 499} 500 501void DAGTypeLegalizer::SetExpandedFloat(SDOperand Op, SDOperand Lo, 502 SDOperand Hi) { 503 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 504 AnalyzeNewNode(Lo.Val); 505 AnalyzeNewNode(Hi.Val); 506 507 // Remember that this is the result of the node. 508 std::pair<SDOperand, SDOperand> &Entry = ExpandedFloats[Op]; 509 assert(Entry.first.Val == 0 && "Node already expanded"); 510 Entry.first = Lo; 511 Entry.second = Hi; 512} 513 514void DAGTypeLegalizer::GetSplitVector(SDOperand Op, SDOperand &Lo, 515 SDOperand &Hi) { 516 std::pair<SDOperand, SDOperand> &Entry = SplitVectors[Op]; 517 RemapNode(Entry.first); 518 RemapNode(Entry.second); 519 assert(Entry.first.Val && "Operand isn't split"); 520 Lo = Entry.first; 521 Hi = Entry.second; 522} 523 524void DAGTypeLegalizer::SetSplitVector(SDOperand Op, SDOperand Lo, 525 SDOperand Hi) { 526 // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant. 527 AnalyzeNewNode(Lo.Val); 528 AnalyzeNewNode(Hi.Val); 529 530 // Remember that this is the result of the node. 531 std::pair<SDOperand, SDOperand> &Entry = SplitVectors[Op]; 532 assert(Entry.first.Val == 0 && "Node already split"); 533 Entry.first = Lo; 534 Entry.second = Hi; 535} 536 537 538//===----------------------------------------------------------------------===// 539// Utilities. 540//===----------------------------------------------------------------------===// 541 542/// BitConvertToInteger - Convert to an integer of the same size. 543SDOperand DAGTypeLegalizer::BitConvertToInteger(SDOperand Op) { 544 unsigned BitWidth = Op.getValueType().getSizeInBits(); 545 return DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerVT(BitWidth), Op); 546} 547 548SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op, 549 MVT DestVT) { 550 // Create the stack frame object. 551 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT); 552 553 // Emit a store to the stack slot. 554 SDOperand Store = DAG.getStore(DAG.getEntryNode(), Op, FIPtr, NULL, 0); 555 // Result is a load from the stack slot. 556 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); 557} 558 559/// JoinIntegers - Build an integer with low bits Lo and high bits Hi. 560SDOperand DAGTypeLegalizer::JoinIntegers(SDOperand Lo, SDOperand Hi) { 561 MVT LVT = Lo.getValueType(); 562 MVT HVT = Hi.getValueType(); 563 MVT NVT = MVT::getIntegerVT(LVT.getSizeInBits() + HVT.getSizeInBits()); 564 565 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Lo); 566 Hi = DAG.getNode(ISD::ANY_EXTEND, NVT, Hi); 567 Hi = DAG.getNode(ISD::SHL, NVT, Hi, DAG.getConstant(LVT.getSizeInBits(), 568 TLI.getShiftAmountTy())); 569 return DAG.getNode(ISD::OR, NVT, Lo, Hi); 570} 571 572/// SplitInteger - Return the lower LoVT bits of Op in Lo and the upper HiVT 573/// bits in Hi. 574void DAGTypeLegalizer::SplitInteger(SDOperand Op, 575 MVT LoVT, MVT HiVT, 576 SDOperand &Lo, SDOperand &Hi) { 577 assert(LoVT.getSizeInBits() + HiVT.getSizeInBits() == 578 Op.getValueType().getSizeInBits() && "Invalid integer splitting!"); 579 Lo = DAG.getNode(ISD::TRUNCATE, LoVT, Op); 580 Hi = DAG.getNode(ISD::SRL, Op.getValueType(), Op, 581 DAG.getConstant(LoVT.getSizeInBits(), 582 TLI.getShiftAmountTy())); 583 Hi = DAG.getNode(ISD::TRUNCATE, HiVT, Hi); 584} 585 586/// SplitInteger - Return the lower and upper halves of Op's bits in a value type 587/// half the size of Op's. 588void DAGTypeLegalizer::SplitInteger(SDOperand Op, 589 SDOperand &Lo, SDOperand &Hi) { 590 MVT HalfVT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()/2); 591 SplitInteger(Op, HalfVT, HalfVT, Lo, Hi); 592} 593 594/// MakeLibCall - Generate a libcall taking the given operands as arguments and 595/// returning a result of type RetVT. 596SDOperand DAGTypeLegalizer::MakeLibCall(RTLIB::Libcall LC, MVT RetVT, 597 const SDOperand *Ops, unsigned NumOps, 598 bool isSigned) { 599 TargetLowering::ArgListTy Args; 600 Args.reserve(NumOps); 601 602 TargetLowering::ArgListEntry Entry; 603 for (unsigned i = 0; i != NumOps; ++i) { 604 Entry.Node = Ops[i]; 605 Entry.Ty = Entry.Node.getValueType().getTypeForMVT(); 606 Entry.isSExt = isSigned; 607 Entry.isZExt = !isSigned; 608 Args.push_back(Entry); 609 } 610 SDOperand Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 611 TLI.getPointerTy()); 612 613 const Type *RetTy = RetVT.getTypeForMVT(); 614 std::pair<SDOperand,SDOperand> CallInfo = 615 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false, 616 CallingConv::C, false, Callee, Args, DAG); 617 return CallInfo.first; 618} 619 620SDOperand DAGTypeLegalizer::GetVectorElementPointer(SDOperand VecPtr, MVT EltVT, 621 SDOperand Index) { 622 // Make sure the index type is big enough to compute in. 623 if (Index.getValueType().bitsGT(TLI.getPointerTy())) 624 Index = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Index); 625 else 626 Index = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Index); 627 628 // Calculate the element offset and add it to the pointer. 629 unsigned EltSize = EltVT.getSizeInBits() / 8; // FIXME: should be ABI size. 630 631 Index = DAG.getNode(ISD::MUL, Index.getValueType(), Index, 632 DAG.getConstant(EltSize, Index.getValueType())); 633 return DAG.getNode(ISD::ADD, Index.getValueType(), Index, VecPtr); 634} 635 636/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type 637/// which is split into two not necessarily identical pieces. 638void DAGTypeLegalizer::GetSplitDestVTs(MVT InVT, MVT &LoVT, MVT &HiVT) { 639 if (!InVT.isVector()) { 640 LoVT = HiVT = TLI.getTypeToTransformTo(InVT); 641 } else { 642 MVT NewEltVT = InVT.getVectorElementType(); 643 unsigned NumElements = InVT.getVectorNumElements(); 644 if ((NumElements & (NumElements-1)) == 0) { // Simple power of two vector. 645 NumElements >>= 1; 646 LoVT = HiVT = MVT::getVectorVT(NewEltVT, NumElements); 647 } else { // Non-power-of-two vectors. 648 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements); 649 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo; 650 LoVT = MVT::getVectorVT(NewEltVT, NewNumElts_Lo); 651 HiVT = MVT::getVectorVT(NewEltVT, NewNumElts_Hi); 652 } 653 } 654} 655 656 657//===----------------------------------------------------------------------===// 658// Entry Point 659//===----------------------------------------------------------------------===// 660 661/// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that 662/// only uses types natively supported by the target. 663/// 664/// Note that this is an involved process that may invalidate pointers into 665/// the graph. 666void SelectionDAG::LegalizeTypes() { 667 if (ViewLegalizeTypesDAGs) viewGraph(); 668 669 DAGTypeLegalizer(*this).run(); 670} 671