LegalizeDAG.cpp revision c7029805ef35ce9805931067b841e6af11db382e
1//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the SelectionDAG::Legalize method. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/SelectionDAG.h" 15#include "llvm/CodeGen/MachineFunction.h" 16#include "llvm/CodeGen/MachineFrameInfo.h" 17#include "llvm/Support/MathExtras.h" 18#include "llvm/Target/TargetLowering.h" 19#include "llvm/Target/TargetData.h" 20#include "llvm/Target/TargetOptions.h" 21#include "llvm/CallingConv.h" 22#include "llvm/Constants.h" 23#include <iostream> 24#include <set> 25using namespace llvm; 26 27//===----------------------------------------------------------------------===// 28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and 29/// hacks on it until the target machine can handle it. This involves 30/// eliminating value sizes the machine cannot handle (promoting small sizes to 31/// large sizes or splitting up large values into small values) as well as 32/// eliminating operations the machine cannot handle. 33/// 34/// This code also does a small amount of optimization and recognition of idioms 35/// as part of its processing. For example, if a target does not support a 36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 37/// will attempt merge setcc and brc instructions into brcc's. 38/// 39namespace { 40class SelectionDAGLegalize { 41 TargetLowering &TLI; 42 SelectionDAG &DAG; 43 44 // Libcall insertion helpers. 45 46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been 47 /// legalized. We use this to ensure that calls are properly serialized 48 /// against each other, including inserted libcalls. 49 SDOperand LastCALLSEQ_END; 50 51 /// IsLegalizingCall - This member is used *only* for purposes of providing 52 /// helpful assertions that a libcall isn't created while another call is 53 /// being legalized (which could lead to non-serialized call sequences). 54 bool IsLegalizingCall; 55 56 enum LegalizeAction { 57 Legal, // The target natively supports this operation. 58 Promote, // This operation should be executed in a larger type. 59 Expand, // Try to expand this to other ops, otherwise use a libcall. 60 }; 61 62 /// ValueTypeActions - This is a bitvector that contains two bits for each 63 /// value type, where the two bits correspond to the LegalizeAction enum. 64 /// This can be queried with "getTypeAction(VT)". 65 TargetLowering::ValueTypeActionImpl ValueTypeActions; 66 67 /// LegalizedNodes - For nodes that are of legal width, and that have more 68 /// than one use, this map indicates what regularized operand to use. This 69 /// allows us to avoid legalizing the same thing more than once. 70 std::map<SDOperand, SDOperand> LegalizedNodes; 71 72 /// PromotedNodes - For nodes that are below legal width, and that have more 73 /// than one use, this map indicates what promoted value to use. This allows 74 /// us to avoid promoting the same thing more than once. 75 std::map<SDOperand, SDOperand> PromotedNodes; 76 77 /// ExpandedNodes - For nodes that need to be expanded this map indicates 78 /// which which operands are the expanded version of the input. This allows 79 /// us to avoid expanding the same node more than once. 80 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; 81 82 /// SplitNodes - For vector nodes that need to be split, this map indicates 83 /// which which operands are the split version of the input. This allows us 84 /// to avoid splitting the same node more than once. 85 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; 86 87 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to 88 /// concrete packed types, this contains the mapping of ones we have already 89 /// processed to the result. 90 std::map<SDOperand, SDOperand> PackedNodes; 91 92 void AddLegalizedOperand(SDOperand From, SDOperand To) { 93 LegalizedNodes.insert(std::make_pair(From, To)); 94 // If someone requests legalization of the new node, return itself. 95 if (From != To) 96 LegalizedNodes.insert(std::make_pair(To, To)); 97 } 98 void AddPromotedOperand(SDOperand From, SDOperand To) { 99 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; 100 assert(isNew && "Got into the map somehow?"); 101 // If someone requests legalization of the new node, return itself. 102 LegalizedNodes.insert(std::make_pair(To, To)); 103 } 104 105public: 106 107 SelectionDAGLegalize(SelectionDAG &DAG); 108 109 /// getTypeAction - Return how we should legalize values of this type, either 110 /// it is already legal or we need to expand it into multiple registers of 111 /// smaller integer type, or we need to promote it to a larger type. 112 LegalizeAction getTypeAction(MVT::ValueType VT) const { 113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT); 114 } 115 116 /// isTypeLegal - Return true if this type is legal on this target. 117 /// 118 bool isTypeLegal(MVT::ValueType VT) const { 119 return getTypeAction(VT) == Legal; 120 } 121 122 void LegalizeDAG(); 123 124private: 125 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as 126 /// appropriate for its type. 127 void HandleOp(SDOperand Op); 128 129 /// LegalizeOp - We know that the specified value has a legal type. 130 /// Recursively ensure that the operands have legal types, then return the 131 /// result. 132 SDOperand LegalizeOp(SDOperand O); 133 134 /// PromoteOp - Given an operation that produces a value in an invalid type, 135 /// promote it to compute the value into a larger type. The produced value 136 /// will have the correct bits for the low portion of the register, but no 137 /// guarantee is made about the top bits: it may be zero, sign-extended, or 138 /// garbage. 139 SDOperand PromoteOp(SDOperand O); 140 141 /// ExpandOp - Expand the specified SDOperand into its two component pieces 142 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, 143 /// the LegalizeNodes map is filled in for any results that are not expanded, 144 /// the ExpandedNodes map is filled in for any results that are expanded, and 145 /// the Lo/Hi values are returned. This applies to integer types and Vector 146 /// types. 147 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); 148 149 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into 150 /// two smaller values of MVT::Vector type. 151 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); 152 153 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the 154 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When 155 /// this is called, we know that PackedVT is the right type for the result and 156 /// we know that this type is legal for the target. 157 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT); 158 159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest); 160 161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); 162 163 SDOperand ExpandLibCall(const char *Name, SDNode *Node, 164 SDOperand &Hi); 165 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, 166 SDOperand Source); 167 168 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); 169 SDOperand ExpandLegalINT_TO_FP(bool isSigned, 170 SDOperand LegalOp, 171 MVT::ValueType DestVT); 172 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, 173 bool isSigned); 174 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, 175 bool isSigned); 176 177 SDOperand ExpandBSWAP(SDOperand Op); 178 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); 179 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, 180 SDOperand &Lo, SDOperand &Hi); 181 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, 182 SDOperand &Lo, SDOperand &Hi); 183 184 SDOperand getIntPtrConstant(uint64_t Val) { 185 return DAG.getConstant(Val, TLI.getPointerTy()); 186 } 187}; 188} 189 190/// getScalarizedOpcode - Return the scalar opcode that corresponds to the 191/// specified vector opcode. 192static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { 193 switch (VecOp) { 194 default: assert(0 && "Don't know how to scalarize this opcode!"); 195 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; 196 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; 197 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; 198 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV; 199 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV; 200 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0; 201 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0; 202 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0; 203 } 204} 205 206SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) 207 : TLI(dag.getTargetLoweringInfo()), DAG(dag), 208 ValueTypeActions(TLI.getValueTypeActions()) { 209 assert(MVT::LAST_VALUETYPE <= 32 && 210 "Too many value types for ValueTypeActions to hold!"); 211} 212 213/// ComputeTopDownOrdering - Add the specified node to the Order list if it has 214/// not been visited yet and if all of its operands have already been visited. 215static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, 216 std::map<SDNode*, unsigned> &Visited) { 217 if (++Visited[N] != N->getNumOperands()) 218 return; // Haven't visited all operands yet 219 220 Order.push_back(N); 221 222 if (N->hasOneUse()) { // Tail recurse in common case. 223 ComputeTopDownOrdering(*N->use_begin(), Order, Visited); 224 return; 225 } 226 227 // Now that we have N in, add anything that uses it if all of their operands 228 // are now done. 229 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) 230 ComputeTopDownOrdering(*UI, Order, Visited); 231} 232 233 234void SelectionDAGLegalize::LegalizeDAG() { 235 LastCALLSEQ_END = DAG.getEntryNode(); 236 IsLegalizingCall = false; 237 238 // The legalize process is inherently a bottom-up recursive process (users 239 // legalize their uses before themselves). Given infinite stack space, we 240 // could just start legalizing on the root and traverse the whole graph. In 241 // practice however, this causes us to run out of stack space on large basic 242 // blocks. To avoid this problem, compute an ordering of the nodes where each 243 // node is only legalized after all of its operands are legalized. 244 std::map<SDNode*, unsigned> Visited; 245 std::vector<SDNode*> Order; 246 247 // Compute ordering from all of the leaves in the graphs, those (like the 248 // entry node) that have no operands. 249 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 250 E = DAG.allnodes_end(); I != E; ++I) { 251 if (I->getNumOperands() == 0) { 252 Visited[I] = 0 - 1U; 253 ComputeTopDownOrdering(I, Order, Visited); 254 } 255 } 256 257 assert(Order.size() == Visited.size() && 258 Order.size() == 259 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && 260 "Error: DAG is cyclic!"); 261 Visited.clear(); 262 263 for (unsigned i = 0, e = Order.size(); i != e; ++i) 264 HandleOp(SDOperand(Order[i], 0)); 265 266 // Finally, it's possible the root changed. Get the new root. 267 SDOperand OldRoot = DAG.getRoot(); 268 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); 269 DAG.setRoot(LegalizedNodes[OldRoot]); 270 271 ExpandedNodes.clear(); 272 LegalizedNodes.clear(); 273 PromotedNodes.clear(); 274 SplitNodes.clear(); 275 PackedNodes.clear(); 276 277 // Remove dead nodes now. 278 DAG.RemoveDeadNodes(OldRoot.Val); 279} 280 281 282/// FindCallEndFromCallStart - Given a chained node that is part of a call 283/// sequence, find the CALLSEQ_END node that terminates the call sequence. 284static SDNode *FindCallEndFromCallStart(SDNode *Node) { 285 if (Node->getOpcode() == ISD::CALLSEQ_END) 286 return Node; 287 if (Node->use_empty()) 288 return 0; // No CallSeqEnd 289 290 // The chain is usually at the end. 291 SDOperand TheChain(Node, Node->getNumValues()-1); 292 if (TheChain.getValueType() != MVT::Other) { 293 // Sometimes it's at the beginning. 294 TheChain = SDOperand(Node, 0); 295 if (TheChain.getValueType() != MVT::Other) { 296 // Otherwise, hunt for it. 297 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) 298 if (Node->getValueType(i) == MVT::Other) { 299 TheChain = SDOperand(Node, i); 300 break; 301 } 302 303 // Otherwise, we walked into a node without a chain. 304 if (TheChain.getValueType() != MVT::Other) 305 return 0; 306 } 307 } 308 309 for (SDNode::use_iterator UI = Node->use_begin(), 310 E = Node->use_end(); UI != E; ++UI) { 311 312 // Make sure to only follow users of our token chain. 313 SDNode *User = *UI; 314 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 315 if (User->getOperand(i) == TheChain) 316 if (SDNode *Result = FindCallEndFromCallStart(User)) 317 return Result; 318 } 319 return 0; 320} 321 322/// FindCallStartFromCallEnd - Given a chained node that is part of a call 323/// sequence, find the CALLSEQ_START node that initiates the call sequence. 324static SDNode *FindCallStartFromCallEnd(SDNode *Node) { 325 assert(Node && "Didn't find callseq_start for a call??"); 326 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; 327 328 assert(Node->getOperand(0).getValueType() == MVT::Other && 329 "Node doesn't have a token chain argument!"); 330 return FindCallStartFromCallEnd(Node->getOperand(0).Val); 331} 332 333/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to 334/// see if any uses can reach Dest. If no dest operands can get to dest, 335/// legalize them, legalize ourself, and return false, otherwise, return true. 336bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, 337 SDNode *Dest) { 338 if (N == Dest) return true; // N certainly leads to Dest :) 339 340 // If the first result of this node has been already legalized, then it cannot 341 // reach N. 342 switch (getTypeAction(N->getValueType(0))) { 343 case Legal: 344 if (LegalizedNodes.count(SDOperand(N, 0))) return false; 345 break; 346 case Promote: 347 if (PromotedNodes.count(SDOperand(N, 0))) return false; 348 break; 349 case Expand: 350 if (ExpandedNodes.count(SDOperand(N, 0))) return false; 351 break; 352 } 353 354 // Okay, this node has not already been legalized. Check and legalize all 355 // operands. If none lead to Dest, then we can legalize this node. 356 bool OperandsLeadToDest = false; 357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 358 OperandsLeadToDest |= // If an operand leads to Dest, so do we. 359 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest); 360 361 if (OperandsLeadToDest) return true; 362 363 // Okay, this node looks safe, legalize it and return false. 364 switch (getTypeAction(N->getValueType(0))) { 365 case Legal: 366 LegalizeOp(SDOperand(N, 0)); 367 break; 368 case Promote: 369 PromoteOp(SDOperand(N, 0)); 370 break; 371 case Expand: { 372 SDOperand X, Y; 373 ExpandOp(SDOperand(N, 0), X, Y); 374 break; 375 } 376 } 377 return false; 378} 379 380/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as 381/// appropriate for its type. 382void SelectionDAGLegalize::HandleOp(SDOperand Op) { 383 switch (getTypeAction(Op.getValueType())) { 384 default: assert(0 && "Bad type action!"); 385 case Legal: LegalizeOp(Op); break; 386 case Promote: PromoteOp(Op); break; 387 case Expand: 388 if (Op.getValueType() != MVT::Vector) { 389 SDOperand X, Y; 390 ExpandOp(Op, X, Y); 391 } else { 392 SDNode *N = Op.Val; 393 unsigned NumOps = N->getNumOperands(); 394 unsigned NumElements = 395 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue(); 396 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT(); 397 MVT::ValueType PackedVT = getVectorType(EVT, NumElements); 398 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) { 399 // In the common case, this is a legal vector type, convert it to the 400 // packed operation and type now. 401 PackVectorOp(Op, PackedVT); 402 } else if (NumElements == 1) { 403 // Otherwise, if this is a single element vector, convert it to a 404 // scalar operation. 405 PackVectorOp(Op, EVT); 406 } else { 407 // Otherwise, this is a multiple element vector that isn't supported. 408 // Split it in half and legalize both parts. 409 SDOperand X, Y; 410 ExpandOp(Op, X, Y); 411 } 412 } 413 break; 414 } 415} 416 417 418/// LegalizeOp - We know that the specified value has a legal type. 419/// Recursively ensure that the operands have legal types, then return the 420/// result. 421SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 422 assert(isTypeLegal(Op.getValueType()) && 423 "Caller should expand or promote operands that are not legal!"); 424 SDNode *Node = Op.Val; 425 426 // If this operation defines any values that cannot be represented in a 427 // register on this target, make sure to expand or promote them. 428 if (Node->getNumValues() > 1) { 429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 430 if (getTypeAction(Node->getValueType(i)) != Legal) { 431 HandleOp(Op.getValue(i)); 432 assert(LegalizedNodes.count(Op) && 433 "Handling didn't add legal operands!"); 434 return LegalizedNodes[Op]; 435 } 436 } 437 438 // Note that LegalizeOp may be reentered even from single-use nodes, which 439 // means that we always must cache transformed nodes. 440 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 441 if (I != LegalizedNodes.end()) return I->second; 442 443 SDOperand Tmp1, Tmp2, Tmp3, Tmp4; 444 SDOperand Result = Op; 445 bool isCustom = false; 446 447 switch (Node->getOpcode()) { 448 case ISD::FrameIndex: 449 case ISD::EntryToken: 450 case ISD::Register: 451 case ISD::BasicBlock: 452 case ISD::TargetFrameIndex: 453 case ISD::TargetConstant: 454 case ISD::TargetConstantFP: 455 case ISD::TargetConstantVec: 456 case ISD::TargetConstantPool: 457 case ISD::TargetGlobalAddress: 458 case ISD::TargetExternalSymbol: 459 case ISD::VALUETYPE: 460 case ISD::SRCVALUE: 461 case ISD::STRING: 462 case ISD::CONDCODE: 463 // Primitives must all be legal. 464 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && 465 "This must be legal!"); 466 break; 467 default: 468 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 469 // If this is a target node, legalize it by legalizing the operands then 470 // passing it through. 471 std::vector<SDOperand> Ops; 472 bool Changed = false; 473 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 474 Ops.push_back(LegalizeOp(Node->getOperand(i))); 475 Changed = Changed || Node->getOperand(i) != Ops.back(); 476 } 477 if (Changed) 478 if (Node->getNumValues() == 1) 479 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); 480 else { 481 std::vector<MVT::ValueType> VTs(Node->value_begin(), 482 Node->value_end()); 483 Result = DAG.getNode(Node->getOpcode(), VTs, Ops); 484 } 485 486 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 487 AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); 488 return Result.getValue(Op.ResNo); 489 } 490 // Otherwise this is an unhandled builtin node. splat. 491 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 492 assert(0 && "Do not know how to legalize this operator!"); 493 abort(); 494 case ISD::GlobalAddress: 495 case ISD::ExternalSymbol: 496 case ISD::ConstantPool: // Nothing to do. 497 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 498 default: assert(0 && "This action is not supported yet!"); 499 case TargetLowering::Custom: 500 Tmp1 = TLI.LowerOperation(Op, DAG); 501 if (Tmp1.Val) Result = Tmp1; 502 // FALLTHROUGH if the target doesn't want to lower this op after all. 503 case TargetLowering::Legal: 504 break; 505 } 506 break; 507 case ISD::AssertSext: 508 case ISD::AssertZext: 509 Tmp1 = LegalizeOp(Node->getOperand(0)); 510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 511 break; 512 case ISD::MERGE_VALUES: 513 // Legalize eliminates MERGE_VALUES nodes. 514 Result = Node->getOperand(Op.ResNo); 515 break; 516 case ISD::CopyFromReg: 517 Tmp1 = LegalizeOp(Node->getOperand(0)); 518 Result = Op.getValue(0); 519 if (Node->getNumValues() == 2) { 520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 521 } else { 522 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); 523 if (Node->getNumOperands() == 3) { 524 Tmp2 = LegalizeOp(Node->getOperand(2)); 525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); 526 } else { 527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 528 } 529 AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); 530 } 531 // Since CopyFromReg produces two values, make sure to remember that we 532 // legalized both of them. 533 AddLegalizedOperand(Op.getValue(0), Result); 534 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 535 return Result.getValue(Op.ResNo); 536 case ISD::UNDEF: { 537 MVT::ValueType VT = Op.getValueType(); 538 switch (TLI.getOperationAction(ISD::UNDEF, VT)) { 539 default: assert(0 && "This action is not supported yet!"); 540 case TargetLowering::Expand: 541 if (MVT::isInteger(VT)) 542 Result = DAG.getConstant(0, VT); 543 else if (MVT::isFloatingPoint(VT)) 544 Result = DAG.getConstantFP(0, VT); 545 else 546 assert(0 && "Unknown value type!"); 547 break; 548 case TargetLowering::Legal: 549 break; 550 } 551 break; 552 } 553 554 case ISD::LOCATION: 555 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); 556 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. 557 558 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { 559 case TargetLowering::Promote: 560 default: assert(0 && "This action is not supported yet!"); 561 case TargetLowering::Expand: { 562 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); 563 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); 564 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other); 565 566 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) { 567 const std::string &FName = 568 cast<StringSDNode>(Node->getOperand(3))->getValue(); 569 const std::string &DirName = 570 cast<StringSDNode>(Node->getOperand(4))->getValue(); 571 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName); 572 573 std::vector<SDOperand> Ops; 574 Ops.push_back(Tmp1); // chain 575 SDOperand LineOp = Node->getOperand(1); 576 SDOperand ColOp = Node->getOperand(2); 577 578 if (useDEBUG_LOC) { 579 Ops.push_back(LineOp); // line # 580 Ops.push_back(ColOp); // col # 581 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id 582 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops); 583 } else { 584 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); 585 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); 586 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile); 587 Ops.push_back(DAG.getConstant(ID, MVT::i32)); 588 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops); 589 } 590 } else { 591 Result = Tmp1; // chain 592 } 593 break; 594 } 595 case TargetLowering::Legal: 596 if (Tmp1 != Node->getOperand(0) || 597 getTypeAction(Node->getOperand(1).getValueType()) == Promote) { 598 std::vector<SDOperand> Ops; 599 Ops.push_back(Tmp1); 600 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { 601 Ops.push_back(Node->getOperand(1)); // line # must be legal. 602 Ops.push_back(Node->getOperand(2)); // col # must be legal. 603 } else { 604 // Otherwise promote them. 605 Ops.push_back(PromoteOp(Node->getOperand(1))); 606 Ops.push_back(PromoteOp(Node->getOperand(2))); 607 } 608 Ops.push_back(Node->getOperand(3)); // filename must be legal. 609 Ops.push_back(Node->getOperand(4)); // working dir # must be legal. 610 Result = DAG.UpdateNodeOperands(Result, Ops); 611 } 612 break; 613 } 614 break; 615 616 case ISD::DEBUG_LOC: 617 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); 618 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { 619 default: assert(0 && "This action is not supported yet!"); 620 case TargetLowering::Legal: 621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 622 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. 623 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. 624 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. 625 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); 626 break; 627 } 628 break; 629 630 case ISD::DEBUG_LABEL: 631 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!"); 632 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) { 633 default: assert(0 && "This action is not supported yet!"); 634 case TargetLowering::Legal: 635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 636 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. 637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 638 break; 639 } 640 break; 641 642 case ISD::Constant: 643 // We know we don't need to expand constants here, constants only have one 644 // value and we check that it is fine above. 645 646 // FIXME: Maybe we should handle things like targets that don't support full 647 // 32-bit immediates? 648 break; 649 case ISD::ConstantFP: { 650 // Spill FP immediates to the constant pool if the target cannot directly 651 // codegen them. Targets often have some immediate values that can be 652 // efficiently generated into an FP register without a load. We explicitly 653 // leave these constants as ConstantFP nodes for the target to deal with. 654 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 655 656 // Check to see if this FP immediate is already legal. 657 bool isLegal = false; 658 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), 659 E = TLI.legal_fpimm_end(); I != E; ++I) 660 if (CFP->isExactlyValue(*I)) { 661 isLegal = true; 662 break; 663 } 664 665 // If this is a legal constant, turn it into a TargetConstantFP node. 666 if (isLegal) { 667 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); 668 break; 669 } 670 671 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { 672 default: assert(0 && "This action is not supported yet!"); 673 case TargetLowering::Custom: 674 Tmp3 = TLI.LowerOperation(Result, DAG); 675 if (Tmp3.Val) { 676 Result = Tmp3; 677 break; 678 } 679 // FALLTHROUGH 680 case TargetLowering::Expand: 681 // Otherwise we need to spill the constant to memory. 682 bool Extend = false; 683 684 // If a FP immediate is precise when represented as a float and if the 685 // target can do an extending load from float to double, we put it into 686 // the constant pool as a float, even if it's is statically typed as a 687 // double. 688 MVT::ValueType VT = CFP->getValueType(0); 689 bool isDouble = VT == MVT::f64; 690 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : 691 Type::FloatTy, CFP->getValue()); 692 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && 693 // Only do this if the target has a native EXTLOAD instruction from 694 // f32. 695 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) { 696 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); 697 VT = MVT::f32; 698 Extend = true; 699 } 700 701 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); 702 if (Extend) { 703 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), 704 CPIdx, DAG.getSrcValue(NULL), MVT::f32); 705 } else { 706 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, 707 DAG.getSrcValue(NULL)); 708 } 709 } 710 break; 711 } 712 case ISD::ConstantVec: 713 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) { 714 default: assert(0 && "This action is not supported yet!"); 715 case TargetLowering::Custom: 716 Tmp3 = TLI.LowerOperation(Result, DAG); 717 if (Tmp3.Val) { 718 Result = Tmp3; 719 break; 720 } 721 // FALLTHROUGH 722 case TargetLowering::Expand: 723 // We assume that vector constants are not legal, and will be immediately 724 // spilled to the constant pool. 725 // 726 // Create a ConstantPacked, and put it in the constant pool. 727 MVT::ValueType VT = Node->getValueType(0); 728 const Type *OpNTy = 729 MVT::getTypeForValueType(Node->getOperand(0).getValueType()); 730 std::vector<Constant*> CV; 731 if (MVT::isFloatingPoint(VT)) { 732 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 733 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue(); 734 CV.push_back(ConstantFP::get(OpNTy, V)); 735 } 736 } else { 737 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 738 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue(); 739 CV.push_back(ConstantUInt::get(OpNTy, V)); 740 } 741 } 742 Constant *CP = ConstantPacked::get(CV); 743 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); 744 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, 745 DAG.getSrcValue(NULL)); 746 break; 747 } 748 break; 749 case ISD::TokenFactor: 750 if (Node->getNumOperands() == 2) { 751 Tmp1 = LegalizeOp(Node->getOperand(0)); 752 Tmp2 = LegalizeOp(Node->getOperand(1)); 753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 754 } else if (Node->getNumOperands() == 3) { 755 Tmp1 = LegalizeOp(Node->getOperand(0)); 756 Tmp2 = LegalizeOp(Node->getOperand(1)); 757 Tmp3 = LegalizeOp(Node->getOperand(2)); 758 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); 759 } else { 760 std::vector<SDOperand> Ops; 761 // Legalize the operands. 762 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) 763 Ops.push_back(LegalizeOp(Node->getOperand(i))); 764 Result = DAG.UpdateNodeOperands(Result, Ops); 765 } 766 break; 767 768 case ISD::CALLSEQ_START: { 769 SDNode *CallEnd = FindCallEndFromCallStart(Node); 770 771 // Recursively Legalize all of the inputs of the call end that do not lead 772 // to this call start. This ensures that any libcalls that need be inserted 773 // are inserted *before* the CALLSEQ_START. 774 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) 775 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node); 776 777 // Now that we legalized all of the inputs (which may have inserted 778 // libcalls) create the new CALLSEQ_START node. 779 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 780 781 // Merge in the last call, to ensure that this call start after the last 782 // call ended. 783 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); 784 Tmp1 = LegalizeOp(Tmp1); 785 786 // Do not try to legalize the target-specific arguments (#1+). 787 if (Tmp1 != Node->getOperand(0)) { 788 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); 789 Ops[0] = Tmp1; 790 Result = DAG.UpdateNodeOperands(Result, Ops); 791 } 792 793 // Remember that the CALLSEQ_START is legalized. 794 AddLegalizedOperand(Op.getValue(0), Result); 795 if (Node->getNumValues() == 2) // If this has a flag result, remember it. 796 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 797 798 // Now that the callseq_start and all of the non-call nodes above this call 799 // sequence have been legalized, legalize the call itself. During this 800 // process, no libcalls can/will be inserted, guaranteeing that no calls 801 // can overlap. 802 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); 803 SDOperand InCallSEQ = LastCALLSEQ_END; 804 // Note that we are selecting this call! 805 LastCALLSEQ_END = SDOperand(CallEnd, 0); 806 IsLegalizingCall = true; 807 808 // Legalize the call, starting from the CALLSEQ_END. 809 LegalizeOp(LastCALLSEQ_END); 810 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); 811 return Result; 812 } 813 case ISD::CALLSEQ_END: 814 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This 815 // will cause this node to be legalized as well as handling libcalls right. 816 if (LastCALLSEQ_END.Val != Node) { 817 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); 818 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 819 assert(I != LegalizedNodes.end() && 820 "Legalizing the call start should have legalized this node!"); 821 return I->second; 822 } 823 824 // Otherwise, the call start has been legalized and everything is going 825 // according to plan. Just legalize ourselves normally here. 826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 827 // Do not try to legalize the target-specific arguments (#1+), except for 828 // an optional flag input. 829 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ 830 if (Tmp1 != Node->getOperand(0)) { 831 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); 832 Ops[0] = Tmp1; 833 Result = DAG.UpdateNodeOperands(Result, Ops); 834 } 835 } else { 836 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); 837 if (Tmp1 != Node->getOperand(0) || 838 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { 839 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); 840 Ops[0] = Tmp1; 841 Ops.back() = Tmp2; 842 Result = DAG.UpdateNodeOperands(Result, Ops); 843 } 844 } 845 assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); 846 // This finishes up call legalization. 847 IsLegalizingCall = false; 848 849 // If the CALLSEQ_END node has a flag, remember that we legalized it. 850 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 851 if (Node->getNumValues() == 2) 852 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 853 return Result.getValue(Op.ResNo); 854 case ISD::DYNAMIC_STACKALLOC: { 855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. 857 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. 858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); 859 860 Tmp1 = Result.getValue(0); 861 Tmp2 = Result.getValue(1); 862 switch (TLI.getOperationAction(Node->getOpcode(), 863 Node->getValueType(0))) { 864 default: assert(0 && "This action is not supported yet!"); 865 case TargetLowering::Expand: { 866 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); 867 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 868 " not tell us which reg is the stack pointer!"); 869 SDOperand Chain = Tmp1.getOperand(0); 870 SDOperand Size = Tmp2.getOperand(1); 871 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); 872 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value 873 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain 874 Tmp1 = LegalizeOp(Tmp1); 875 Tmp2 = LegalizeOp(Tmp2); 876 break; 877 } 878 case TargetLowering::Custom: 879 Tmp3 = TLI.LowerOperation(Tmp1, DAG); 880 if (Tmp3.Val) { 881 Tmp1 = LegalizeOp(Tmp3); 882 Tmp2 = LegalizeOp(Tmp3.getValue(1)); 883 } 884 break; 885 case TargetLowering::Legal: 886 break; 887 } 888 // Since this op produce two values, make sure to remember that we 889 // legalized both of them. 890 AddLegalizedOperand(SDOperand(Node, 0), Tmp1); 891 AddLegalizedOperand(SDOperand(Node, 1), Tmp2); 892 return Op.ResNo ? Tmp2 : Tmp1; 893 } 894 case ISD::INLINEASM: 895 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain. 896 Tmp2 = Node->getOperand(Node->getNumOperands()-1); 897 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists. 898 Tmp2 = Tmp3 = SDOperand(0, 0); 899 else 900 Tmp3 = LegalizeOp(Tmp2); 901 902 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) { 903 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); 904 Ops[0] = Tmp1; 905 if (Tmp3.Val) Ops.back() = Tmp3; 906 Result = DAG.UpdateNodeOperands(Result, Ops); 907 } 908 909 // INLINE asm returns a chain and flag, make sure to add both to the map. 910 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 911 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 912 return Result.getValue(Op.ResNo); 913 case ISD::BR: 914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 915 // Ensure that libcalls are emitted before a branch. 916 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); 917 Tmp1 = LegalizeOp(Tmp1); 918 LastCALLSEQ_END = DAG.getEntryNode(); 919 920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 921 break; 922 923 case ISD::BRCOND: 924 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 925 // Ensure that libcalls are emitted before a return. 926 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); 927 Tmp1 = LegalizeOp(Tmp1); 928 LastCALLSEQ_END = DAG.getEntryNode(); 929 930 switch (getTypeAction(Node->getOperand(1).getValueType())) { 931 case Expand: assert(0 && "It's impossible to expand bools"); 932 case Legal: 933 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 934 break; 935 case Promote: 936 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 937 break; 938 } 939 940 // Basic block destination (Op#2) is always legal. 941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 942 943 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { 944 default: assert(0 && "This action is not supported yet!"); 945 case TargetLowering::Legal: break; 946 case TargetLowering::Custom: 947 Tmp1 = TLI.LowerOperation(Result, DAG); 948 if (Tmp1.Val) Result = Tmp1; 949 break; 950 case TargetLowering::Expand: 951 // Expand brcond's setcc into its constituent parts and create a BR_CC 952 // Node. 953 if (Tmp2.getOpcode() == ISD::SETCC) { 954 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), 955 Tmp2.getOperand(0), Tmp2.getOperand(1), 956 Node->getOperand(2)); 957 } else { 958 // Make sure the condition is either zero or one. It may have been 959 // promoted from something else. 960 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType()); 961 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1)) 962 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); 963 964 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, 965 DAG.getCondCode(ISD::SETNE), Tmp2, 966 DAG.getConstant(0, Tmp2.getValueType()), 967 Node->getOperand(2)); 968 } 969 break; 970 } 971 break; 972 case ISD::BR_CC: 973 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 974 // Ensure that libcalls are emitted before a branch. 975 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); 976 Tmp1 = LegalizeOp(Tmp1); 977 LastCALLSEQ_END = DAG.getEntryNode(); 978 979 Tmp2 = Node->getOperand(2); // LHS 980 Tmp3 = Node->getOperand(3); // RHS 981 Tmp4 = Node->getOperand(1); // CC 982 983 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); 984 985 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, 986 // the LHS is a legal SETCC itself. In this case, we need to compare 987 // the result against zero to select between true and false values. 988 if (Tmp3.Val == 0) { 989 Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); 990 Tmp4 = DAG.getCondCode(ISD::SETNE); 991 } 992 993 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, 994 Node->getOperand(4)); 995 996 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { 997 default: assert(0 && "Unexpected action for BR_CC!"); 998 case TargetLowering::Legal: break; 999 case TargetLowering::Custom: 1000 Tmp4 = TLI.LowerOperation(Result, DAG); 1001 if (Tmp4.Val) Result = Tmp4; 1002 break; 1003 } 1004 break; 1005 case ISD::LOAD: { 1006 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1007 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1008 1009 MVT::ValueType VT = Node->getValueType(0); 1010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 1011 Tmp2 = Result.getValue(0); 1012 Tmp3 = Result.getValue(1); 1013 1014 switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 1015 default: assert(0 && "This action is not supported yet!"); 1016 case TargetLowering::Legal: break; 1017 case TargetLowering::Custom: 1018 Tmp1 = TLI.LowerOperation(Tmp2, DAG); 1019 if (Tmp1.Val) { 1020 Tmp2 = LegalizeOp(Tmp1); 1021 Tmp3 = LegalizeOp(Tmp1.getValue(1)); 1022 } 1023 break; 1024 } 1025 // Since loads produce two values, make sure to remember that we 1026 // legalized both of them. 1027 AddLegalizedOperand(SDOperand(Node, 0), Tmp2); 1028 AddLegalizedOperand(SDOperand(Node, 1), Tmp3); 1029 return Op.ResNo ? Tmp3 : Tmp2; 1030 } 1031 case ISD::EXTLOAD: 1032 case ISD::SEXTLOAD: 1033 case ISD::ZEXTLOAD: { 1034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1036 1037 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 1038 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { 1039 default: assert(0 && "This action is not supported yet!"); 1040 case TargetLowering::Promote: 1041 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); 1042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), 1043 DAG.getValueType(MVT::i8)); 1044 Tmp1 = Result.getValue(0); 1045 Tmp2 = Result.getValue(1); 1046 break; 1047 case TargetLowering::Custom: 1048 isCustom = true; 1049 // FALLTHROUGH 1050 case TargetLowering::Legal: 1051 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), 1052 Node->getOperand(3)); 1053 Tmp1 = Result.getValue(0); 1054 Tmp2 = Result.getValue(1); 1055 1056 if (isCustom) { 1057 Tmp3 = TLI.LowerOperation(Tmp3, DAG); 1058 if (Tmp3.Val) { 1059 Tmp1 = LegalizeOp(Tmp3); 1060 Tmp2 = LegalizeOp(Tmp3.getValue(1)); 1061 } 1062 } 1063 break; 1064 case TargetLowering::Expand: 1065 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND 1066 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { 1067 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); 1068 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); 1069 Tmp1 = LegalizeOp(Result); // Relegalize new nodes. 1070 Tmp2 = LegalizeOp(Load.getValue(1)); 1071 break; 1072 } 1073 assert(Node->getOpcode() != ISD::EXTLOAD && 1074 "EXTLOAD should always be supported!"); 1075 // Turn the unsupported load into an EXTLOAD followed by an explicit 1076 // zero/sign extend inreg. 1077 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), 1078 Tmp1, Tmp2, Node->getOperand(2), SrcVT); 1079 SDOperand ValRes; 1080 if (Node->getOpcode() == ISD::SEXTLOAD) 1081 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1082 Result, DAG.getValueType(SrcVT)); 1083 else 1084 ValRes = DAG.getZeroExtendInReg(Result, SrcVT); 1085 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. 1086 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. 1087 break; 1088 } 1089 // Since loads produce two values, make sure to remember that we legalized 1090 // both of them. 1091 AddLegalizedOperand(SDOperand(Node, 0), Tmp1); 1092 AddLegalizedOperand(SDOperand(Node, 1), Tmp2); 1093 return Op.ResNo ? Tmp2 : Tmp1; 1094 } 1095 case ISD::EXTRACT_ELEMENT: { 1096 MVT::ValueType OpTy = Node->getOperand(0).getValueType(); 1097 switch (getTypeAction(OpTy)) { 1098 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); 1099 case Legal: 1100 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { 1101 // 1 -> Hi 1102 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), 1103 DAG.getConstant(MVT::getSizeInBits(OpTy)/2, 1104 TLI.getShiftAmountTy())); 1105 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); 1106 } else { 1107 // 0 -> Lo 1108 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), 1109 Node->getOperand(0)); 1110 } 1111 break; 1112 case Expand: 1113 // Get both the low and high parts. 1114 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 1115 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) 1116 Result = Tmp2; // 1 -> Hi 1117 else 1118 Result = Tmp1; // 0 -> Lo 1119 break; 1120 } 1121 break; 1122 } 1123 1124 case ISD::CopyToReg: 1125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1126 1127 assert(isTypeLegal(Node->getOperand(2).getValueType()) && 1128 "Register type must be legal!"); 1129 // Legalize the incoming value (must be a legal type). 1130 Tmp2 = LegalizeOp(Node->getOperand(2)); 1131 if (Node->getNumValues() == 1) { 1132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); 1133 } else { 1134 assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); 1135 if (Node->getNumOperands() == 4) { 1136 Tmp3 = LegalizeOp(Node->getOperand(3)); 1137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, 1138 Tmp3); 1139 } else { 1140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); 1141 } 1142 1143 // Since this produces two values, make sure to remember that we legalized 1144 // both of them. 1145 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 1146 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1147 return Result; 1148 } 1149 break; 1150 1151 case ISD::RET: 1152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1153 1154 // Ensure that libcalls are emitted before a return. 1155 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); 1156 Tmp1 = LegalizeOp(Tmp1); 1157 LastCALLSEQ_END = DAG.getEntryNode(); 1158 1159 switch (Node->getNumOperands()) { 1160 case 2: // ret val 1161 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1162 case Legal: 1163 Tmp2 = LegalizeOp(Node->getOperand(1)); 1164 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1165 break; 1166 case Expand: { 1167 SDOperand Lo, Hi; 1168 ExpandOp(Node->getOperand(1), Lo, Hi); 1169 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); 1170 break; 1171 } 1172 case Promote: 1173 Tmp2 = PromoteOp(Node->getOperand(1)); 1174 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1175 Result = LegalizeOp(Result); 1176 break; 1177 } 1178 break; 1179 case 1: // ret void 1180 Result = DAG.UpdateNodeOperands(Result, Tmp1); 1181 break; 1182 default: { // ret <values> 1183 std::vector<SDOperand> NewValues; 1184 NewValues.push_back(Tmp1); 1185 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) 1186 switch (getTypeAction(Node->getOperand(i).getValueType())) { 1187 case Legal: 1188 NewValues.push_back(LegalizeOp(Node->getOperand(i))); 1189 break; 1190 case Expand: { 1191 SDOperand Lo, Hi; 1192 ExpandOp(Node->getOperand(i), Lo, Hi); 1193 NewValues.push_back(Lo); 1194 NewValues.push_back(Hi); 1195 break; 1196 } 1197 case Promote: 1198 assert(0 && "Can't promote multiple return value yet!"); 1199 } 1200 1201 if (NewValues.size() == Node->getNumOperands()) 1202 Result = DAG.UpdateNodeOperands(Result, NewValues); 1203 else 1204 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); 1205 break; 1206 } 1207 } 1208 1209 if (Result.getOpcode() == ISD::RET) { 1210 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { 1211 default: assert(0 && "This action is not supported yet!"); 1212 case TargetLowering::Legal: break; 1213 case TargetLowering::Custom: 1214 Tmp1 = TLI.LowerOperation(Result, DAG); 1215 if (Tmp1.Val) Result = Tmp1; 1216 break; 1217 } 1218 } 1219 break; 1220 case ISD::STORE: { 1221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1222 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 1223 1224 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 1225 // FIXME: We shouldn't do this for TargetConstantFP's. 1226 // FIXME: move this to the DAG Combiner! 1227 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ 1228 if (CFP->getValueType(0) == MVT::f32) { 1229 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); 1230 } else { 1231 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); 1232 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); 1233 } 1234 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2, 1235 Node->getOperand(3)); 1236 break; 1237 } 1238 1239 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1240 case Legal: { 1241 Tmp3 = LegalizeOp(Node->getOperand(1)); 1242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 1243 Node->getOperand(3)); 1244 1245 MVT::ValueType VT = Tmp3.getValueType(); 1246 switch (TLI.getOperationAction(ISD::STORE, VT)) { 1247 default: assert(0 && "This action is not supported yet!"); 1248 case TargetLowering::Legal: break; 1249 case TargetLowering::Custom: 1250 Tmp1 = TLI.LowerOperation(Result, DAG); 1251 if (Tmp1.Val) Result = Tmp1; 1252 break; 1253 } 1254 break; 1255 } 1256 case Promote: 1257 // Truncate the value and store the result. 1258 Tmp3 = PromoteOp(Node->getOperand(1)); 1259 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, 1260 Node->getOperand(3), 1261 DAG.getValueType(Node->getOperand(1).getValueType())); 1262 break; 1263 1264 case Expand: 1265 unsigned IncrementSize = 0; 1266 SDOperand Lo, Hi; 1267 1268 // If this is a vector type, then we have to calculate the increment as 1269 // the product of the element size in bytes, and the number of elements 1270 // in the high half of the vector. 1271 if (Node->getOperand(1).getValueType() == MVT::Vector) { 1272 SDNode *InVal = Node->getOperand(1).Val; 1273 unsigned NumElems = 1274 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); 1275 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); 1276 1277 // Figure out if there is a Packed type corresponding to this Vector 1278 // type. If so, convert to the packed type. 1279 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); 1280 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { 1281 // Turn this into a normal store of the packed type. 1282 Tmp3 = PackVectorOp(Node->getOperand(1), TVT); 1283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 1284 Node->getOperand(3)); 1285 break; 1286 } else if (NumElems == 1) { 1287 // Turn this into a normal store of the scalar type. 1288 Tmp3 = PackVectorOp(Node->getOperand(1), EVT); 1289 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 1290 Node->getOperand(3)); 1291 break; 1292 } else { 1293 SplitVectorOp(Node->getOperand(1), Lo, Hi); 1294 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; 1295 } 1296 } else { 1297 ExpandOp(Node->getOperand(1), Lo, Hi); 1298 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; 1299 } 1300 1301 if (!TLI.isLittleEndian()) 1302 std::swap(Lo, Hi); 1303 1304 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, 1305 Node->getOperand(3)); 1306 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, 1307 getIntPtrConstant(IncrementSize)); 1308 assert(isTypeLegal(Tmp2.getValueType()) && 1309 "Pointers must be legal!"); 1310 // FIXME: This sets the srcvalue of both halves to be the same, which is 1311 // wrong. 1312 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, 1313 Node->getOperand(3)); 1314 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 1315 break; 1316 } 1317 break; 1318 } 1319 case ISD::PCMARKER: 1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 1322 break; 1323 case ISD::STACKSAVE: 1324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1325 Result = DAG.UpdateNodeOperands(Result, Tmp1); 1326 Tmp1 = Result.getValue(0); 1327 Tmp2 = Result.getValue(1); 1328 1329 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { 1330 default: assert(0 && "This action is not supported yet!"); 1331 case TargetLowering::Legal: break; 1332 case TargetLowering::Custom: 1333 Tmp3 = TLI.LowerOperation(Result, DAG); 1334 if (Tmp3.Val) { 1335 Tmp1 = LegalizeOp(Tmp3); 1336 Tmp2 = LegalizeOp(Tmp3.getValue(1)); 1337 } 1338 break; 1339 case TargetLowering::Expand: 1340 // Expand to CopyFromReg if the target set 1341 // StackPointerRegisterToSaveRestore. 1342 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 1343 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, 1344 Node->getValueType(0)); 1345 Tmp2 = Tmp1.getValue(1); 1346 } else { 1347 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); 1348 Tmp2 = Node->getOperand(0); 1349 } 1350 break; 1351 } 1352 1353 // Since stacksave produce two values, make sure to remember that we 1354 // legalized both of them. 1355 AddLegalizedOperand(SDOperand(Node, 0), Tmp1); 1356 AddLegalizedOperand(SDOperand(Node, 1), Tmp2); 1357 return Op.ResNo ? Tmp2 : Tmp1; 1358 1359 case ISD::STACKRESTORE: 1360 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1363 1364 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { 1365 default: assert(0 && "This action is not supported yet!"); 1366 case TargetLowering::Legal: break; 1367 case TargetLowering::Custom: 1368 Tmp1 = TLI.LowerOperation(Result, DAG); 1369 if (Tmp1.Val) Result = Tmp1; 1370 break; 1371 case TargetLowering::Expand: 1372 // Expand to CopyToReg if the target set 1373 // StackPointerRegisterToSaveRestore. 1374 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 1375 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); 1376 } else { 1377 Result = Tmp1; 1378 } 1379 break; 1380 } 1381 break; 1382 1383 case ISD::READCYCLECOUNTER: 1384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain 1385 Result = DAG.UpdateNodeOperands(Result, Tmp1); 1386 1387 // Since rdcc produce two values, make sure to remember that we legalized 1388 // both of them. 1389 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 1390 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1391 return Result; 1392 1393 case ISD::TRUNCSTORE: { 1394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1395 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 1396 1397 assert(isTypeLegal(Node->getOperand(1).getValueType()) && 1398 "Cannot handle illegal TRUNCSTORE yet!"); 1399 Tmp2 = LegalizeOp(Node->getOperand(1)); 1400 1401 // The only promote case we handle is TRUNCSTORE:i1 X into 1402 // -> TRUNCSTORE:i8 (and X, 1) 1403 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 && 1404 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) == 1405 TargetLowering::Promote) { 1406 // Promote the bool to a mask then store. 1407 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2, 1408 DAG.getConstant(1, Tmp2.getValueType())); 1409 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, 1410 Node->getOperand(3), DAG.getValueType(MVT::i8)); 1411 1412 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 1413 Tmp3 != Node->getOperand(2)) { 1414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, 1415 Node->getOperand(3), Node->getOperand(4)); 1416 } 1417 1418 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT(); 1419 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) { 1420 default: assert(0 && "This action is not supported yet!"); 1421 case TargetLowering::Legal: break; 1422 case TargetLowering::Custom: 1423 Tmp1 = TLI.LowerOperation(Result, DAG); 1424 if (Tmp1.Val) Result = Tmp1; 1425 break; 1426 } 1427 break; 1428 } 1429 case ISD::SELECT: 1430 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1431 case Expand: assert(0 && "It's impossible to expand bools"); 1432 case Legal: 1433 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 1434 break; 1435 case Promote: 1436 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 1437 break; 1438 } 1439 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal 1440 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal 1441 1442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); 1443 1444 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { 1445 default: assert(0 && "This action is not supported yet!"); 1446 case TargetLowering::Legal: break; 1447 case TargetLowering::Custom: { 1448 Tmp1 = TLI.LowerOperation(Result, DAG); 1449 if (Tmp1.Val) Result = Tmp1; 1450 break; 1451 } 1452 case TargetLowering::Expand: 1453 if (Tmp1.getOpcode() == ISD::SETCC) { 1454 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), 1455 Tmp2, Tmp3, 1456 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 1457 } else { 1458 // Make sure the condition is either zero or one. It may have been 1459 // promoted from something else. 1460 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType()); 1461 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1)) 1462 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); 1463 Result = DAG.getSelectCC(Tmp1, 1464 DAG.getConstant(0, Tmp1.getValueType()), 1465 Tmp2, Tmp3, ISD::SETNE); 1466 } 1467 break; 1468 case TargetLowering::Promote: { 1469 MVT::ValueType NVT = 1470 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); 1471 unsigned ExtOp, TruncOp; 1472 if (MVT::isInteger(Tmp2.getValueType())) { 1473 ExtOp = ISD::ANY_EXTEND; 1474 TruncOp = ISD::TRUNCATE; 1475 } else { 1476 ExtOp = ISD::FP_EXTEND; 1477 TruncOp = ISD::FP_ROUND; 1478 } 1479 // Promote each of the values to the new type. 1480 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); 1481 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); 1482 // Perform the larger operation, then round down. 1483 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); 1484 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); 1485 break; 1486 } 1487 } 1488 break; 1489 case ISD::SELECT_CC: { 1490 Tmp1 = Node->getOperand(0); // LHS 1491 Tmp2 = Node->getOperand(1); // RHS 1492 Tmp3 = LegalizeOp(Node->getOperand(2)); // True 1493 Tmp4 = LegalizeOp(Node->getOperand(3)); // False 1494 SDOperand CC = Node->getOperand(4); 1495 1496 LegalizeSetCCOperands(Tmp1, Tmp2, CC); 1497 1498 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, 1499 // the LHS is a legal SETCC itself. In this case, we need to compare 1500 // the result against zero to select between true and false values. 1501 if (Tmp2.Val == 0) { 1502 Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); 1503 CC = DAG.getCondCode(ISD::SETNE); 1504 } 1505 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); 1506 1507 // Everything is legal, see if we should expand this op or something. 1508 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { 1509 default: assert(0 && "This action is not supported yet!"); 1510 case TargetLowering::Legal: break; 1511 case TargetLowering::Custom: 1512 Tmp1 = TLI.LowerOperation(Result, DAG); 1513 if (Tmp1.Val) Result = Tmp1; 1514 break; 1515 } 1516 break; 1517 } 1518 case ISD::SETCC: 1519 Tmp1 = Node->getOperand(0); 1520 Tmp2 = Node->getOperand(1); 1521 Tmp3 = Node->getOperand(2); 1522 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); 1523 1524 // If we had to Expand the SetCC operands into a SELECT node, then it may 1525 // not always be possible to return a true LHS & RHS. In this case, just 1526 // return the value we legalized, returned in the LHS 1527 if (Tmp2.Val == 0) { 1528 Result = Tmp1; 1529 break; 1530 } 1531 1532 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { 1533 default: assert(0 && "Cannot handle this action for SETCC yet!"); 1534 case TargetLowering::Custom: 1535 isCustom = true; 1536 // FALLTHROUGH. 1537 case TargetLowering::Legal: 1538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 1539 if (isCustom) { 1540 Tmp3 = TLI.LowerOperation(Result, DAG); 1541 if (Tmp3.Val) Result = Tmp3; 1542 } 1543 break; 1544 case TargetLowering::Promote: { 1545 // First step, figure out the appropriate operation to use. 1546 // Allow SETCC to not be supported for all legal data types 1547 // Mostly this targets FP 1548 MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); 1549 MVT::ValueType OldVT = NewInTy; 1550 1551 // Scan for the appropriate larger type to use. 1552 while (1) { 1553 NewInTy = (MVT::ValueType)(NewInTy+1); 1554 1555 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && 1556 "Fell off of the edge of the integer world"); 1557 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && 1558 "Fell off of the edge of the floating point world"); 1559 1560 // If the target supports SETCC of this type, use it. 1561 if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) 1562 break; 1563 } 1564 if (MVT::isInteger(NewInTy)) 1565 assert(0 && "Cannot promote Legal Integer SETCC yet"); 1566 else { 1567 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); 1568 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); 1569 } 1570 Tmp1 = LegalizeOp(Tmp1); 1571 Tmp2 = LegalizeOp(Tmp2); 1572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 1573 Result = LegalizeOp(Result); 1574 break; 1575 } 1576 case TargetLowering::Expand: 1577 // Expand a setcc node into a select_cc of the same condition, lhs, and 1578 // rhs that selects between const 1 (true) and const 0 (false). 1579 MVT::ValueType VT = Node->getValueType(0); 1580 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, 1581 DAG.getConstant(1, VT), DAG.getConstant(0, VT), 1582 Node->getOperand(2)); 1583 break; 1584 } 1585 break; 1586 case ISD::MEMSET: 1587 case ISD::MEMCPY: 1588 case ISD::MEMMOVE: { 1589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain 1590 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer 1591 1592 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte 1593 switch (getTypeAction(Node->getOperand(2).getValueType())) { 1594 case Expand: assert(0 && "Cannot expand a byte!"); 1595 case Legal: 1596 Tmp3 = LegalizeOp(Node->getOperand(2)); 1597 break; 1598 case Promote: 1599 Tmp3 = PromoteOp(Node->getOperand(2)); 1600 break; 1601 } 1602 } else { 1603 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, 1604 } 1605 1606 SDOperand Tmp4; 1607 switch (getTypeAction(Node->getOperand(3).getValueType())) { 1608 case Expand: { 1609 // Length is too big, just take the lo-part of the length. 1610 SDOperand HiPart; 1611 ExpandOp(Node->getOperand(3), HiPart, Tmp4); 1612 break; 1613 } 1614 case Legal: 1615 Tmp4 = LegalizeOp(Node->getOperand(3)); 1616 break; 1617 case Promote: 1618 Tmp4 = PromoteOp(Node->getOperand(3)); 1619 break; 1620 } 1621 1622 SDOperand Tmp5; 1623 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint 1624 case Expand: assert(0 && "Cannot expand this yet!"); 1625 case Legal: 1626 Tmp5 = LegalizeOp(Node->getOperand(4)); 1627 break; 1628 case Promote: 1629 Tmp5 = PromoteOp(Node->getOperand(4)); 1630 break; 1631 } 1632 1633 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 1634 default: assert(0 && "This action not implemented for this operation!"); 1635 case TargetLowering::Custom: 1636 isCustom = true; 1637 // FALLTHROUGH 1638 case TargetLowering::Legal: 1639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); 1640 if (isCustom) { 1641 Tmp1 = TLI.LowerOperation(Result, DAG); 1642 if (Tmp1.Val) Result = Tmp1; 1643 } 1644 break; 1645 case TargetLowering::Expand: { 1646 // Otherwise, the target does not support this operation. Lower the 1647 // operation to an explicit libcall as appropriate. 1648 MVT::ValueType IntPtr = TLI.getPointerTy(); 1649 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); 1650 std::vector<std::pair<SDOperand, const Type*> > Args; 1651 1652 const char *FnName = 0; 1653 if (Node->getOpcode() == ISD::MEMSET) { 1654 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 1655 // Extend the (previously legalized) ubyte argument to be an int value 1656 // for the call. 1657 if (Tmp3.getValueType() > MVT::i32) 1658 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); 1659 else 1660 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); 1661 Args.push_back(std::make_pair(Tmp3, Type::IntTy)); 1662 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 1663 1664 FnName = "memset"; 1665 } else if (Node->getOpcode() == ISD::MEMCPY || 1666 Node->getOpcode() == ISD::MEMMOVE) { 1667 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 1668 Args.push_back(std::make_pair(Tmp3, IntPtrTy)); 1669 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 1670 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; 1671 } else { 1672 assert(0 && "Unknown op!"); 1673 } 1674 1675 std::pair<SDOperand,SDOperand> CallResult = 1676 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, 1677 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); 1678 Result = CallResult.second; 1679 break; 1680 } 1681 } 1682 break; 1683 } 1684 1685 case ISD::SHL_PARTS: 1686 case ISD::SRA_PARTS: 1687 case ISD::SRL_PARTS: { 1688 std::vector<SDOperand> Ops; 1689 bool Changed = false; 1690 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1691 Ops.push_back(LegalizeOp(Node->getOperand(i))); 1692 Changed |= Ops.back() != Node->getOperand(i); 1693 } 1694 if (Changed) 1695 Result = DAG.UpdateNodeOperands(Result, Ops); 1696 1697 switch (TLI.getOperationAction(Node->getOpcode(), 1698 Node->getValueType(0))) { 1699 default: assert(0 && "This action is not supported yet!"); 1700 case TargetLowering::Legal: break; 1701 case TargetLowering::Custom: 1702 Tmp1 = TLI.LowerOperation(Result, DAG); 1703 if (Tmp1.Val) { 1704 SDOperand Tmp2, RetVal(0, 0); 1705 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 1706 Tmp2 = LegalizeOp(Tmp1.getValue(i)); 1707 AddLegalizedOperand(SDOperand(Node, i), Tmp2); 1708 if (i == Op.ResNo) 1709 RetVal = Tmp2; 1710 } 1711 assert(RetVal.Val && "Illegal result number"); 1712 return RetVal; 1713 } 1714 break; 1715 } 1716 1717 // Since these produce multiple values, make sure to remember that we 1718 // legalized all of them. 1719 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 1720 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); 1721 return Result.getValue(Op.ResNo); 1722 } 1723 1724 // Binary operators 1725 case ISD::ADD: 1726 case ISD::SUB: 1727 case ISD::MUL: 1728 case ISD::MULHS: 1729 case ISD::MULHU: 1730 case ISD::UDIV: 1731 case ISD::SDIV: 1732 case ISD::AND: 1733 case ISD::OR: 1734 case ISD::XOR: 1735 case ISD::SHL: 1736 case ISD::SRL: 1737 case ISD::SRA: 1738 case ISD::FADD: 1739 case ISD::FSUB: 1740 case ISD::FMUL: 1741 case ISD::FDIV: 1742 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 1743 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1744 case Expand: assert(0 && "Not possible"); 1745 case Legal: 1746 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. 1747 break; 1748 case Promote: 1749 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. 1750 break; 1751 } 1752 1753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1754 1755 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 1756 default: assert(0 && "Operation not supported"); 1757 case TargetLowering::Legal: break; 1758 case TargetLowering::Custom: 1759 Tmp1 = TLI.LowerOperation(Result, DAG); 1760 if (Tmp1.Val) Result = Tmp1; 1761 break; 1762 } 1763 break; 1764 1765 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! 1766 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 1767 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1768 case Expand: assert(0 && "Not possible"); 1769 case Legal: 1770 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. 1771 break; 1772 case Promote: 1773 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. 1774 break; 1775 } 1776 1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1778 1779 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 1780 default: assert(0 && "Operation not supported"); 1781 case TargetLowering::Custom: 1782 Tmp1 = TLI.LowerOperation(Result, DAG); 1783 if (Tmp1.Val) Result = Tmp1; 1784 break; 1785 case TargetLowering::Legal: break; 1786 case TargetLowering::Expand: 1787 // If this target supports fabs/fneg natively, do this efficiently. 1788 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) && 1789 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) { 1790 // Get the sign bit of the RHS. 1791 MVT::ValueType IVT = 1792 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; 1793 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); 1794 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), 1795 SignBit, DAG.getConstant(0, IVT), ISD::SETLT); 1796 // Get the absolute value of the result. 1797 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); 1798 // Select between the nabs and abs value based on the sign bit of 1799 // the input. 1800 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, 1801 DAG.getNode(ISD::FNEG, AbsVal.getValueType(), 1802 AbsVal), 1803 AbsVal); 1804 Result = LegalizeOp(Result); 1805 break; 1806 } 1807 1808 // Otherwise, do bitwise ops! 1809 1810 // copysign -> copysignf/copysign libcall. 1811 const char *FnName; 1812 if (Node->getValueType(0) == MVT::f32) { 1813 FnName = "copysignf"; 1814 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type. 1815 Result = DAG.UpdateNodeOperands(Result, Tmp1, 1816 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2)); 1817 } else { 1818 FnName = "copysign"; 1819 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type. 1820 Result = DAG.UpdateNodeOperands(Result, Tmp1, 1821 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2)); 1822 } 1823 SDOperand Dummy; 1824 Result = ExpandLibCall(FnName, Node, Dummy); 1825 break; 1826 } 1827 break; 1828 1829 case ISD::ADDC: 1830 case ISD::SUBC: 1831 Tmp1 = LegalizeOp(Node->getOperand(0)); 1832 Tmp2 = LegalizeOp(Node->getOperand(1)); 1833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1834 // Since this produces two values, make sure to remember that we legalized 1835 // both of them. 1836 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 1837 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1838 return Result; 1839 1840 case ISD::ADDE: 1841 case ISD::SUBE: 1842 Tmp1 = LegalizeOp(Node->getOperand(0)); 1843 Tmp2 = LegalizeOp(Node->getOperand(1)); 1844 Tmp3 = LegalizeOp(Node->getOperand(2)); 1845 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); 1846 // Since this produces two values, make sure to remember that we legalized 1847 // both of them. 1848 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); 1849 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1850 return Result; 1851 1852 case ISD::BUILD_PAIR: { 1853 MVT::ValueType PairTy = Node->getValueType(0); 1854 // TODO: handle the case where the Lo and Hi operands are not of legal type 1855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo 1856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi 1857 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { 1858 case TargetLowering::Promote: 1859 case TargetLowering::Custom: 1860 assert(0 && "Cannot promote/custom this yet!"); 1861 case TargetLowering::Legal: 1862 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1863 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); 1864 break; 1865 case TargetLowering::Expand: 1866 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); 1867 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); 1868 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, 1869 DAG.getConstant(MVT::getSizeInBits(PairTy)/2, 1870 TLI.getShiftAmountTy())); 1871 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); 1872 break; 1873 } 1874 break; 1875 } 1876 1877 case ISD::UREM: 1878 case ISD::SREM: 1879 case ISD::FREM: 1880 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 1881 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 1882 1883 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 1884 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); 1885 case TargetLowering::Custom: 1886 isCustom = true; 1887 // FALLTHROUGH 1888 case TargetLowering::Legal: 1889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 1890 if (isCustom) { 1891 Tmp1 = TLI.LowerOperation(Result, DAG); 1892 if (Tmp1.Val) Result = Tmp1; 1893 } 1894 break; 1895 case TargetLowering::Expand: 1896 if (MVT::isInteger(Node->getValueType(0))) { 1897 // X % Y -> X-X/Y*Y 1898 MVT::ValueType VT = Node->getValueType(0); 1899 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV; 1900 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); 1901 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); 1902 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); 1903 } else { 1904 // Floating point mod -> fmod libcall. 1905 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; 1906 SDOperand Dummy; 1907 Result = ExpandLibCall(FnName, Node, Dummy); 1908 } 1909 break; 1910 } 1911 break; 1912 case ISD::VAARG: { 1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1914 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1915 1916 MVT::ValueType VT = Node->getValueType(0); 1917 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 1918 default: assert(0 && "This action is not supported yet!"); 1919 case TargetLowering::Custom: 1920 isCustom = true; 1921 // FALLTHROUGH 1922 case TargetLowering::Legal: 1923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 1924 Result = Result.getValue(0); 1925 Tmp1 = Result.getValue(1); 1926 1927 if (isCustom) { 1928 Tmp2 = TLI.LowerOperation(Result, DAG); 1929 if (Tmp2.Val) { 1930 Result = LegalizeOp(Tmp2); 1931 Tmp1 = LegalizeOp(Tmp2.getValue(1)); 1932 } 1933 } 1934 break; 1935 case TargetLowering::Expand: { 1936 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, 1937 Node->getOperand(2)); 1938 // Increment the pointer, VAList, to the next vaarg 1939 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, 1940 DAG.getConstant(MVT::getSizeInBits(VT)/8, 1941 TLI.getPointerTy())); 1942 // Store the incremented VAList to the legalized pointer 1943 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, 1944 Node->getOperand(2)); 1945 // Load the actual argument out of the pointer VAList 1946 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0)); 1947 Tmp1 = LegalizeOp(Result.getValue(1)); 1948 Result = LegalizeOp(Result); 1949 break; 1950 } 1951 } 1952 // Since VAARG produces two values, make sure to remember that we 1953 // legalized both of them. 1954 AddLegalizedOperand(SDOperand(Node, 0), Result); 1955 AddLegalizedOperand(SDOperand(Node, 1), Tmp1); 1956 return Op.ResNo ? Tmp1 : Result; 1957 } 1958 1959 case ISD::VACOPY: 1960 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1961 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. 1962 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. 1963 1964 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { 1965 default: assert(0 && "This action is not supported yet!"); 1966 case TargetLowering::Custom: 1967 isCustom = true; 1968 // FALLTHROUGH 1969 case TargetLowering::Legal: 1970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, 1971 Node->getOperand(3), Node->getOperand(4)); 1972 if (isCustom) { 1973 Tmp1 = TLI.LowerOperation(Result, DAG); 1974 if (Tmp1.Val) Result = Tmp1; 1975 } 1976 break; 1977 case TargetLowering::Expand: 1978 // This defaults to loading a pointer from the input and storing it to the 1979 // output, returning the chain. 1980 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3)); 1981 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2, 1982 Node->getOperand(4)); 1983 break; 1984 } 1985 break; 1986 1987 case ISD::VAEND: 1988 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1989 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1990 1991 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { 1992 default: assert(0 && "This action is not supported yet!"); 1993 case TargetLowering::Custom: 1994 isCustom = true; 1995 // FALLTHROUGH 1996 case TargetLowering::Legal: 1997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 1998 if (isCustom) { 1999 Tmp1 = TLI.LowerOperation(Tmp1, DAG); 2000 if (Tmp1.Val) Result = Tmp1; 2001 } 2002 break; 2003 case TargetLowering::Expand: 2004 Result = Tmp1; // Default to a no-op, return the chain 2005 break; 2006 } 2007 break; 2008 2009 case ISD::VASTART: 2010 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 2011 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 2012 2013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); 2014 2015 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { 2016 default: assert(0 && "This action is not supported yet!"); 2017 case TargetLowering::Legal: break; 2018 case TargetLowering::Custom: 2019 Tmp1 = TLI.LowerOperation(Result, DAG); 2020 if (Tmp1.Val) Result = Tmp1; 2021 break; 2022 } 2023 break; 2024 2025 case ISD::ROTL: 2026 case ISD::ROTR: 2027 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 2028 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 2029 2030 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && 2031 "Cannot handle this yet!"); 2032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); 2033 break; 2034 2035 case ISD::BSWAP: 2036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op 2037 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2038 case TargetLowering::Custom: 2039 assert(0 && "Cannot custom legalize this yet!"); 2040 case TargetLowering::Legal: 2041 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2042 break; 2043 case TargetLowering::Promote: { 2044 MVT::ValueType OVT = Tmp1.getValueType(); 2045 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 2046 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); 2047 2048 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 2049 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); 2050 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, 2051 DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); 2052 break; 2053 } 2054 case TargetLowering::Expand: 2055 Result = ExpandBSWAP(Tmp1); 2056 break; 2057 } 2058 break; 2059 2060 case ISD::CTPOP: 2061 case ISD::CTTZ: 2062 case ISD::CTLZ: 2063 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op 2064 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2065 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); 2066 case TargetLowering::Legal: 2067 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2068 break; 2069 case TargetLowering::Promote: { 2070 MVT::ValueType OVT = Tmp1.getValueType(); 2071 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 2072 2073 // Zero extend the argument. 2074 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 2075 // Perform the larger operation, then subtract if needed. 2076 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2077 switch (Node->getOpcode()) { 2078 case ISD::CTPOP: 2079 Result = Tmp1; 2080 break; 2081 case ISD::CTTZ: 2082 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) 2083 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, 2084 DAG.getConstant(getSizeInBits(NVT), NVT), 2085 ISD::SETEQ); 2086 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, 2087 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); 2088 break; 2089 case ISD::CTLZ: 2090 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 2091 Result = DAG.getNode(ISD::SUB, NVT, Tmp1, 2092 DAG.getConstant(getSizeInBits(NVT) - 2093 getSizeInBits(OVT), NVT)); 2094 break; 2095 } 2096 break; 2097 } 2098 case TargetLowering::Expand: 2099 Result = ExpandBitCount(Node->getOpcode(), Tmp1); 2100 break; 2101 } 2102 break; 2103 2104 // Unary operators 2105 case ISD::FABS: 2106 case ISD::FNEG: 2107 case ISD::FSQRT: 2108 case ISD::FSIN: 2109 case ISD::FCOS: 2110 Tmp1 = LegalizeOp(Node->getOperand(0)); 2111 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2112 case TargetLowering::Promote: 2113 case TargetLowering::Custom: 2114 isCustom = true; 2115 // FALLTHROUGH 2116 case TargetLowering::Legal: 2117 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2118 if (isCustom) { 2119 Tmp1 = TLI.LowerOperation(Result, DAG); 2120 if (Tmp1.Val) Result = Tmp1; 2121 } 2122 break; 2123 case TargetLowering::Expand: 2124 switch (Node->getOpcode()) { 2125 default: assert(0 && "Unreachable!"); 2126 case ISD::FNEG: 2127 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 2128 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); 2129 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); 2130 break; 2131 case ISD::FABS: { 2132 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). 2133 MVT::ValueType VT = Node->getValueType(0); 2134 Tmp2 = DAG.getConstantFP(0.0, VT); 2135 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); 2136 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); 2137 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); 2138 break; 2139 } 2140 case ISD::FSQRT: 2141 case ISD::FSIN: 2142 case ISD::FCOS: { 2143 MVT::ValueType VT = Node->getValueType(0); 2144 const char *FnName = 0; 2145 switch(Node->getOpcode()) { 2146 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; 2147 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; 2148 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; 2149 default: assert(0 && "Unreachable!"); 2150 } 2151 SDOperand Dummy; 2152 Result = ExpandLibCall(FnName, Node, Dummy); 2153 break; 2154 } 2155 } 2156 break; 2157 } 2158 break; 2159 2160 case ISD::BIT_CONVERT: 2161 if (!isTypeLegal(Node->getOperand(0).getValueType())) { 2162 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2163 } else { 2164 switch (TLI.getOperationAction(ISD::BIT_CONVERT, 2165 Node->getOperand(0).getValueType())) { 2166 default: assert(0 && "Unknown operation action!"); 2167 case TargetLowering::Expand: 2168 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2169 break; 2170 case TargetLowering::Legal: 2171 Tmp1 = LegalizeOp(Node->getOperand(0)); 2172 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2173 break; 2174 } 2175 } 2176 break; 2177 // Conversion operators. The source and destination have different types. 2178 case ISD::SINT_TO_FP: 2179 case ISD::UINT_TO_FP: { 2180 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; 2181 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2182 case Legal: 2183 switch (TLI.getOperationAction(Node->getOpcode(), 2184 Node->getOperand(0).getValueType())) { 2185 default: assert(0 && "Unknown operation action!"); 2186 case TargetLowering::Custom: 2187 isCustom = true; 2188 // FALLTHROUGH 2189 case TargetLowering::Legal: 2190 Tmp1 = LegalizeOp(Node->getOperand(0)); 2191 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2192 if (isCustom) { 2193 Tmp1 = TLI.LowerOperation(Result, DAG); 2194 if (Tmp1.Val) Result = Tmp1; 2195 } 2196 break; 2197 case TargetLowering::Expand: 2198 Result = ExpandLegalINT_TO_FP(isSigned, 2199 LegalizeOp(Node->getOperand(0)), 2200 Node->getValueType(0)); 2201 break; 2202 case TargetLowering::Promote: 2203 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), 2204 Node->getValueType(0), 2205 isSigned); 2206 break; 2207 } 2208 break; 2209 case Expand: 2210 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, 2211 Node->getValueType(0), Node->getOperand(0)); 2212 break; 2213 case Promote: 2214 Tmp1 = PromoteOp(Node->getOperand(0)); 2215 if (isSigned) { 2216 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), 2217 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); 2218 } else { 2219 Tmp1 = DAG.getZeroExtendInReg(Tmp1, 2220 Node->getOperand(0).getValueType()); 2221 } 2222 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2223 Result = LegalizeOp(Result); // The 'op' is not necessarily legal! 2224 break; 2225 } 2226 break; 2227 } 2228 case ISD::TRUNCATE: 2229 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2230 case Legal: 2231 Tmp1 = LegalizeOp(Node->getOperand(0)); 2232 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2233 break; 2234 case Expand: 2235 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 2236 2237 // Since the result is legal, we should just be able to truncate the low 2238 // part of the source. 2239 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); 2240 break; 2241 case Promote: 2242 Result = PromoteOp(Node->getOperand(0)); 2243 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); 2244 break; 2245 } 2246 break; 2247 2248 case ISD::FP_TO_SINT: 2249 case ISD::FP_TO_UINT: 2250 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2251 case Legal: 2252 Tmp1 = LegalizeOp(Node->getOperand(0)); 2253 2254 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ 2255 default: assert(0 && "Unknown operation action!"); 2256 case TargetLowering::Custom: 2257 isCustom = true; 2258 // FALLTHROUGH 2259 case TargetLowering::Legal: 2260 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2261 if (isCustom) { 2262 Tmp1 = TLI.LowerOperation(Result, DAG); 2263 if (Tmp1.Val) Result = Tmp1; 2264 } 2265 break; 2266 case TargetLowering::Promote: 2267 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), 2268 Node->getOpcode() == ISD::FP_TO_SINT); 2269 break; 2270 case TargetLowering::Expand: 2271 if (Node->getOpcode() == ISD::FP_TO_UINT) { 2272 SDOperand True, False; 2273 MVT::ValueType VT = Node->getOperand(0).getValueType(); 2274 MVT::ValueType NVT = Node->getValueType(0); 2275 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; 2276 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); 2277 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), 2278 Node->getOperand(0), Tmp2, ISD::SETLT); 2279 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); 2280 False = DAG.getNode(ISD::FP_TO_SINT, NVT, 2281 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), 2282 Tmp2)); 2283 False = DAG.getNode(ISD::XOR, NVT, False, 2284 DAG.getConstant(1ULL << ShiftAmt, NVT)); 2285 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); 2286 break; 2287 } else { 2288 assert(0 && "Do not know how to expand FP_TO_SINT yet!"); 2289 } 2290 break; 2291 } 2292 break; 2293 case Expand: 2294 assert(0 && "Shouldn't need to expand other operators here!"); 2295 case Promote: 2296 Tmp1 = PromoteOp(Node->getOperand(0)); 2297 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); 2298 Result = LegalizeOp(Result); 2299 break; 2300 } 2301 break; 2302 2303 case ISD::ANY_EXTEND: 2304 case ISD::ZERO_EXTEND: 2305 case ISD::SIGN_EXTEND: 2306 case ISD::FP_EXTEND: 2307 case ISD::FP_ROUND: 2308 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2309 case Expand: assert(0 && "Shouldn't need to expand other operators here!"); 2310 case Legal: 2311 Tmp1 = LegalizeOp(Node->getOperand(0)); 2312 Result = DAG.UpdateNodeOperands(Result, Tmp1); 2313 break; 2314 case Promote: 2315 switch (Node->getOpcode()) { 2316 case ISD::ANY_EXTEND: 2317 Tmp1 = PromoteOp(Node->getOperand(0)); 2318 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); 2319 break; 2320 case ISD::ZERO_EXTEND: 2321 Result = PromoteOp(Node->getOperand(0)); 2322 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); 2323 Result = DAG.getZeroExtendInReg(Result, 2324 Node->getOperand(0).getValueType()); 2325 break; 2326 case ISD::SIGN_EXTEND: 2327 Result = PromoteOp(Node->getOperand(0)); 2328 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); 2329 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 2330 Result, 2331 DAG.getValueType(Node->getOperand(0).getValueType())); 2332 break; 2333 case ISD::FP_EXTEND: 2334 Result = PromoteOp(Node->getOperand(0)); 2335 if (Result.getValueType() != Op.getValueType()) 2336 // Dynamically dead while we have only 2 FP types. 2337 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); 2338 break; 2339 case ISD::FP_ROUND: 2340 Result = PromoteOp(Node->getOperand(0)); 2341 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); 2342 break; 2343 } 2344 } 2345 break; 2346 case ISD::FP_ROUND_INREG: 2347 case ISD::SIGN_EXTEND_INREG: { 2348 Tmp1 = LegalizeOp(Node->getOperand(0)); 2349 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2350 2351 // If this operation is not supported, convert it to a shl/shr or load/store 2352 // pair. 2353 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { 2354 default: assert(0 && "This action not supported for this op yet!"); 2355 case TargetLowering::Legal: 2356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); 2357 break; 2358 case TargetLowering::Expand: 2359 // If this is an integer extend and shifts are supported, do that. 2360 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { 2361 // NOTE: we could fall back on load/store here too for targets without 2362 // SAR. However, it is doubtful that any exist. 2363 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - 2364 MVT::getSizeInBits(ExtraVT); 2365 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); 2366 Result = DAG.getNode(ISD::SHL, Node->getValueType(0), 2367 Node->getOperand(0), ShiftCst); 2368 Result = DAG.getNode(ISD::SRA, Node->getValueType(0), 2369 Result, ShiftCst); 2370 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { 2371 // The only way we can lower this is to turn it into a STORETRUNC, 2372 // EXTLOAD pair, targetting a temporary location (a stack slot). 2373 2374 // NOTE: there is a choice here between constantly creating new stack 2375 // slots and always reusing the same one. We currently always create 2376 // new ones, as reuse may inhibit scheduling. 2377 const Type *Ty = MVT::getTypeForValueType(ExtraVT); 2378 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); 2379 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); 2380 MachineFunction &MF = DAG.getMachineFunction(); 2381 int SSFI = 2382 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); 2383 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 2384 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), 2385 Node->getOperand(0), StackSlot, 2386 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); 2387 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), 2388 Result, StackSlot, DAG.getSrcValue(NULL), 2389 ExtraVT); 2390 } else { 2391 assert(0 && "Unknown op"); 2392 } 2393 break; 2394 } 2395 break; 2396 } 2397 } 2398 2399 // Make sure that the generated code is itself legal. 2400 if (Result != Op) 2401 Result = LegalizeOp(Result); 2402 2403 // Note that LegalizeOp may be reentered even from single-use nodes, which 2404 // means that we always must cache transformed nodes. 2405 AddLegalizedOperand(Op, Result); 2406 return Result; 2407} 2408 2409/// PromoteOp - Given an operation that produces a value in an invalid type, 2410/// promote it to compute the value into a larger type. The produced value will 2411/// have the correct bits for the low portion of the register, but no guarantee 2412/// is made about the top bits: it may be zero, sign-extended, or garbage. 2413SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { 2414 MVT::ValueType VT = Op.getValueType(); 2415 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 2416 assert(getTypeAction(VT) == Promote && 2417 "Caller should expand or legalize operands that are not promotable!"); 2418 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && 2419 "Cannot promote to smaller type!"); 2420 2421 SDOperand Tmp1, Tmp2, Tmp3; 2422 SDOperand Result; 2423 SDNode *Node = Op.Val; 2424 2425 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); 2426 if (I != PromotedNodes.end()) return I->second; 2427 2428 switch (Node->getOpcode()) { 2429 case ISD::CopyFromReg: 2430 assert(0 && "CopyFromReg must be legal!"); 2431 default: 2432 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 2433 assert(0 && "Do not know how to promote this operator!"); 2434 abort(); 2435 case ISD::UNDEF: 2436 Result = DAG.getNode(ISD::UNDEF, NVT); 2437 break; 2438 case ISD::Constant: 2439 if (VT != MVT::i1) 2440 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); 2441 else 2442 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); 2443 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); 2444 break; 2445 case ISD::ConstantFP: 2446 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); 2447 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); 2448 break; 2449 2450 case ISD::SETCC: 2451 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); 2452 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), 2453 Node->getOperand(1), Node->getOperand(2)); 2454 break; 2455 2456 case ISD::TRUNCATE: 2457 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2458 case Legal: 2459 Result = LegalizeOp(Node->getOperand(0)); 2460 assert(Result.getValueType() >= NVT && 2461 "This truncation doesn't make sense!"); 2462 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT 2463 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); 2464 break; 2465 case Promote: 2466 // The truncation is not required, because we don't guarantee anything 2467 // about high bits anyway. 2468 Result = PromoteOp(Node->getOperand(0)); 2469 break; 2470 case Expand: 2471 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 2472 // Truncate the low part of the expanded value to the result type 2473 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); 2474 } 2475 break; 2476 case ISD::SIGN_EXTEND: 2477 case ISD::ZERO_EXTEND: 2478 case ISD::ANY_EXTEND: 2479 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2480 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); 2481 case Legal: 2482 // Input is legal? Just do extend all the way to the larger type. 2483 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); 2484 break; 2485 case Promote: 2486 // Promote the reg if it's smaller. 2487 Result = PromoteOp(Node->getOperand(0)); 2488 // The high bits are not guaranteed to be anything. Insert an extend. 2489 if (Node->getOpcode() == ISD::SIGN_EXTEND) 2490 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, 2491 DAG.getValueType(Node->getOperand(0).getValueType())); 2492 else if (Node->getOpcode() == ISD::ZERO_EXTEND) 2493 Result = DAG.getZeroExtendInReg(Result, 2494 Node->getOperand(0).getValueType()); 2495 break; 2496 } 2497 break; 2498 case ISD::BIT_CONVERT: 2499 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2500 Result = PromoteOp(Result); 2501 break; 2502 2503 case ISD::FP_EXTEND: 2504 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); 2505 case ISD::FP_ROUND: 2506 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2507 case Expand: assert(0 && "BUG: Cannot expand FP regs!"); 2508 case Promote: assert(0 && "Unreachable with 2 FP types!"); 2509 case Legal: 2510 // Input is legal? Do an FP_ROUND_INREG. 2511 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), 2512 DAG.getValueType(VT)); 2513 break; 2514 } 2515 break; 2516 2517 case ISD::SINT_TO_FP: 2518 case ISD::UINT_TO_FP: 2519 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2520 case Legal: 2521 // No extra round required here. 2522 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); 2523 break; 2524 2525 case Promote: 2526 Result = PromoteOp(Node->getOperand(0)); 2527 if (Node->getOpcode() == ISD::SINT_TO_FP) 2528 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 2529 Result, 2530 DAG.getValueType(Node->getOperand(0).getValueType())); 2531 else 2532 Result = DAG.getZeroExtendInReg(Result, 2533 Node->getOperand(0).getValueType()); 2534 // No extra round required here. 2535 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 2536 break; 2537 case Expand: 2538 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, 2539 Node->getOperand(0)); 2540 // Round if we cannot tolerate excess precision. 2541 if (NoExcessFPPrecision) 2542 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2543 DAG.getValueType(VT)); 2544 break; 2545 } 2546 break; 2547 2548 case ISD::SIGN_EXTEND_INREG: 2549 Result = PromoteOp(Node->getOperand(0)); 2550 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, 2551 Node->getOperand(1)); 2552 break; 2553 case ISD::FP_TO_SINT: 2554 case ISD::FP_TO_UINT: 2555 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2556 case Legal: 2557 Tmp1 = Node->getOperand(0); 2558 break; 2559 case Promote: 2560 // The input result is prerounded, so we don't have to do anything 2561 // special. 2562 Tmp1 = PromoteOp(Node->getOperand(0)); 2563 break; 2564 case Expand: 2565 assert(0 && "not implemented"); 2566 } 2567 // If we're promoting a UINT to a larger size, check to see if the new node 2568 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since 2569 // we can use that instead. This allows us to generate better code for 2570 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not 2571 // legal, such as PowerPC. 2572 if (Node->getOpcode() == ISD::FP_TO_UINT && 2573 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && 2574 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || 2575 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ 2576 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); 2577 } else { 2578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2579 } 2580 break; 2581 2582 case ISD::FABS: 2583 case ISD::FNEG: 2584 Tmp1 = PromoteOp(Node->getOperand(0)); 2585 assert(Tmp1.getValueType() == NVT); 2586 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2587 // NOTE: we do not have to do any extra rounding here for 2588 // NoExcessFPPrecision, because we know the input will have the appropriate 2589 // precision, and these operations don't modify precision at all. 2590 break; 2591 2592 case ISD::FSQRT: 2593 case ISD::FSIN: 2594 case ISD::FCOS: 2595 Tmp1 = PromoteOp(Node->getOperand(0)); 2596 assert(Tmp1.getValueType() == NVT); 2597 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2598 if (NoExcessFPPrecision) 2599 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2600 DAG.getValueType(VT)); 2601 break; 2602 2603 case ISD::AND: 2604 case ISD::OR: 2605 case ISD::XOR: 2606 case ISD::ADD: 2607 case ISD::SUB: 2608 case ISD::MUL: 2609 // The input may have strange things in the top bits of the registers, but 2610 // these operations don't care. They may have weird bits going out, but 2611 // that too is okay if they are integer operations. 2612 Tmp1 = PromoteOp(Node->getOperand(0)); 2613 Tmp2 = PromoteOp(Node->getOperand(1)); 2614 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 2615 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2616 break; 2617 case ISD::FADD: 2618 case ISD::FSUB: 2619 case ISD::FMUL: 2620 Tmp1 = PromoteOp(Node->getOperand(0)); 2621 Tmp2 = PromoteOp(Node->getOperand(1)); 2622 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 2623 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2624 2625 // Floating point operations will give excess precision that we may not be 2626 // able to tolerate. If we DO allow excess precision, just leave it, 2627 // otherwise excise it. 2628 // FIXME: Why would we need to round FP ops more than integer ones? 2629 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) 2630 if (NoExcessFPPrecision) 2631 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2632 DAG.getValueType(VT)); 2633 break; 2634 2635 case ISD::SDIV: 2636 case ISD::SREM: 2637 // These operators require that their input be sign extended. 2638 Tmp1 = PromoteOp(Node->getOperand(0)); 2639 Tmp2 = PromoteOp(Node->getOperand(1)); 2640 if (MVT::isInteger(NVT)) { 2641 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 2642 DAG.getValueType(VT)); 2643 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, 2644 DAG.getValueType(VT)); 2645 } 2646 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2647 2648 // Perform FP_ROUND: this is probably overly pessimistic. 2649 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 2650 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2651 DAG.getValueType(VT)); 2652 break; 2653 case ISD::FDIV: 2654 case ISD::FREM: 2655 case ISD::FCOPYSIGN: 2656 // These operators require that their input be fp extended. 2657 Tmp1 = PromoteOp(Node->getOperand(0)); 2658 Tmp2 = PromoteOp(Node->getOperand(1)); 2659 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2660 2661 // Perform FP_ROUND: this is probably overly pessimistic. 2662 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) 2663 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2664 DAG.getValueType(VT)); 2665 break; 2666 2667 case ISD::UDIV: 2668 case ISD::UREM: 2669 // These operators require that their input be zero extended. 2670 Tmp1 = PromoteOp(Node->getOperand(0)); 2671 Tmp2 = PromoteOp(Node->getOperand(1)); 2672 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); 2673 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 2674 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); 2675 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2676 break; 2677 2678 case ISD::SHL: 2679 Tmp1 = PromoteOp(Node->getOperand(0)); 2680 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); 2681 break; 2682 case ISD::SRA: 2683 // The input value must be properly sign extended. 2684 Tmp1 = PromoteOp(Node->getOperand(0)); 2685 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 2686 DAG.getValueType(VT)); 2687 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); 2688 break; 2689 case ISD::SRL: 2690 // The input value must be properly zero extended. 2691 Tmp1 = PromoteOp(Node->getOperand(0)); 2692 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 2693 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); 2694 break; 2695 2696 case ISD::VAARG: 2697 Tmp1 = Node->getOperand(0); // Get the chain. 2698 Tmp2 = Node->getOperand(1); // Get the pointer. 2699 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { 2700 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); 2701 Result = TLI.CustomPromoteOperation(Tmp3, DAG); 2702 } else { 2703 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, 2704 Node->getOperand(2)); 2705 // Increment the pointer, VAList, to the next vaarg 2706 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, 2707 DAG.getConstant(MVT::getSizeInBits(VT)/8, 2708 TLI.getPointerTy())); 2709 // Store the incremented VAList to the legalized pointer 2710 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, 2711 Node->getOperand(2)); 2712 // Load the actual argument out of the pointer VAList 2713 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, 2714 DAG.getSrcValue(0), VT); 2715 } 2716 // Remember that we legalized the chain. 2717 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 2718 break; 2719 2720 case ISD::LOAD: 2721 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0), 2722 Node->getOperand(1), Node->getOperand(2), VT); 2723 // Remember that we legalized the chain. 2724 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 2725 break; 2726 case ISD::SEXTLOAD: 2727 case ISD::ZEXTLOAD: 2728 case ISD::EXTLOAD: 2729 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0), 2730 Node->getOperand(1), Node->getOperand(2), 2731 cast<VTSDNode>(Node->getOperand(3))->getVT()); 2732 // Remember that we legalized the chain. 2733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 2734 break; 2735 case ISD::SELECT: 2736 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 2737 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 2738 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); 2739 break; 2740 case ISD::SELECT_CC: 2741 Tmp2 = PromoteOp(Node->getOperand(2)); // True 2742 Tmp3 = PromoteOp(Node->getOperand(3)); // False 2743 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 2744 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); 2745 break; 2746 case ISD::BSWAP: 2747 Tmp1 = Node->getOperand(0); 2748 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 2749 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); 2750 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, 2751 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), 2752 TLI.getShiftAmountTy())); 2753 break; 2754 case ISD::CTPOP: 2755 case ISD::CTTZ: 2756 case ISD::CTLZ: 2757 // Zero extend the argument 2758 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); 2759 // Perform the larger operation, then subtract if needed. 2760 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2761 switch(Node->getOpcode()) { 2762 case ISD::CTPOP: 2763 Result = Tmp1; 2764 break; 2765 case ISD::CTTZ: 2766 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) 2767 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, 2768 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); 2769 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, 2770 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); 2771 break; 2772 case ISD::CTLZ: 2773 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 2774 Result = DAG.getNode(ISD::SUB, NVT, Tmp1, 2775 DAG.getConstant(getSizeInBits(NVT) - 2776 getSizeInBits(VT), NVT)); 2777 break; 2778 } 2779 break; 2780 } 2781 2782 assert(Result.Val && "Didn't set a result!"); 2783 2784 // Make sure the result is itself legal. 2785 Result = LegalizeOp(Result); 2786 2787 // Remember that we promoted this! 2788 AddPromotedOperand(Op, Result); 2789 return Result; 2790} 2791 2792/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC 2793/// with condition CC on the current target. This usually involves legalizing 2794/// or promoting the arguments. In the case where LHS and RHS must be expanded, 2795/// there may be no choice but to create a new SetCC node to represent the 2796/// legalized value of setcc lhs, rhs. In this case, the value is returned in 2797/// LHS, and the SDOperand returned in RHS has a nil SDNode value. 2798void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, 2799 SDOperand &RHS, 2800 SDOperand &CC) { 2801 SDOperand Tmp1, Tmp2, Result; 2802 2803 switch (getTypeAction(LHS.getValueType())) { 2804 case Legal: 2805 Tmp1 = LegalizeOp(LHS); // LHS 2806 Tmp2 = LegalizeOp(RHS); // RHS 2807 break; 2808 case Promote: 2809 Tmp1 = PromoteOp(LHS); // LHS 2810 Tmp2 = PromoteOp(RHS); // RHS 2811 2812 // If this is an FP compare, the operands have already been extended. 2813 if (MVT::isInteger(LHS.getValueType())) { 2814 MVT::ValueType VT = LHS.getValueType(); 2815 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 2816 2817 // Otherwise, we have to insert explicit sign or zero extends. Note 2818 // that we could insert sign extends for ALL conditions, but zero extend 2819 // is cheaper on many machines (an AND instead of two shifts), so prefer 2820 // it. 2821 switch (cast<CondCodeSDNode>(CC)->get()) { 2822 default: assert(0 && "Unknown integer comparison!"); 2823 case ISD::SETEQ: 2824 case ISD::SETNE: 2825 case ISD::SETUGE: 2826 case ISD::SETUGT: 2827 case ISD::SETULE: 2828 case ISD::SETULT: 2829 // ALL of these operations will work if we either sign or zero extend 2830 // the operands (including the unsigned comparisons!). Zero extend is 2831 // usually a simpler/cheaper operation, so prefer it. 2832 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 2833 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); 2834 break; 2835 case ISD::SETGE: 2836 case ISD::SETGT: 2837 case ISD::SETLT: 2838 case ISD::SETLE: 2839 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 2840 DAG.getValueType(VT)); 2841 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, 2842 DAG.getValueType(VT)); 2843 break; 2844 } 2845 } 2846 break; 2847 case Expand: 2848 SDOperand LHSLo, LHSHi, RHSLo, RHSHi; 2849 ExpandOp(LHS, LHSLo, LHSHi); 2850 ExpandOp(RHS, RHSLo, RHSHi); 2851 switch (cast<CondCodeSDNode>(CC)->get()) { 2852 case ISD::SETEQ: 2853 case ISD::SETNE: 2854 if (RHSLo == RHSHi) 2855 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) 2856 if (RHSCST->isAllOnesValue()) { 2857 // Comparison to -1. 2858 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); 2859 Tmp2 = RHSLo; 2860 break; 2861 } 2862 2863 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); 2864 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); 2865 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); 2866 Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); 2867 break; 2868 default: 2869 // If this is a comparison of the sign bit, just look at the top part. 2870 // X > -1, x < 0 2871 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) 2872 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && 2873 CST->getValue() == 0) || // X < 0 2874 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && 2875 CST->isAllOnesValue())) { // X > -1 2876 Tmp1 = LHSHi; 2877 Tmp2 = RHSHi; 2878 break; 2879 } 2880 2881 // FIXME: This generated code sucks. 2882 ISD::CondCode LowCC; 2883 switch (cast<CondCodeSDNode>(CC)->get()) { 2884 default: assert(0 && "Unknown integer setcc!"); 2885 case ISD::SETLT: 2886 case ISD::SETULT: LowCC = ISD::SETULT; break; 2887 case ISD::SETGT: 2888 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 2889 case ISD::SETLE: 2890 case ISD::SETULE: LowCC = ISD::SETULE; break; 2891 case ISD::SETGE: 2892 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 2893 } 2894 2895 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 2896 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 2897 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 2898 2899 // NOTE: on targets without efficient SELECT of bools, we can always use 2900 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 2901 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); 2902 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); 2903 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); 2904 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), 2905 Result, Tmp1, Tmp2)); 2906 Tmp1 = Result; 2907 Tmp2 = SDOperand(); 2908 } 2909 } 2910 LHS = Tmp1; 2911 RHS = Tmp2; 2912} 2913 2914/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. 2915/// The resultant code need not be legal. Note that SrcOp is the input operand 2916/// to the BIT_CONVERT, not the BIT_CONVERT node itself. 2917SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, 2918 SDOperand SrcOp) { 2919 // Create the stack frame object. 2920 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 2921 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8; 2922 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); 2923 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); 2924 2925 // Emit a store to the stack slot. 2926 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), 2927 SrcOp, FIPtr, DAG.getSrcValue(NULL)); 2928 // Result is a load from the stack slot. 2929 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0)); 2930} 2931 2932void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, 2933 SDOperand Op, SDOperand Amt, 2934 SDOperand &Lo, SDOperand &Hi) { 2935 // Expand the subcomponents. 2936 SDOperand LHSL, LHSH; 2937 ExpandOp(Op, LHSL, LHSH); 2938 2939 std::vector<SDOperand> Ops; 2940 Ops.push_back(LHSL); 2941 Ops.push_back(LHSH); 2942 Ops.push_back(Amt); 2943 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); 2944 Lo = DAG.getNode(NodeOp, VTs, Ops); 2945 Hi = Lo.getValue(1); 2946} 2947 2948 2949/// ExpandShift - Try to find a clever way to expand this shift operation out to 2950/// smaller elements. If we can't find a way that is more efficient than a 2951/// libcall on this target, return false. Otherwise, return true with the 2952/// low-parts expanded into Lo and Hi. 2953bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, 2954 SDOperand &Lo, SDOperand &Hi) { 2955 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && 2956 "This is not a shift!"); 2957 2958 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); 2959 SDOperand ShAmt = LegalizeOp(Amt); 2960 MVT::ValueType ShTy = ShAmt.getValueType(); 2961 unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); 2962 unsigned NVTBits = MVT::getSizeInBits(NVT); 2963 2964 // Handle the case when Amt is an immediate. Other cases are currently broken 2965 // and are disabled. 2966 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { 2967 unsigned Cst = CN->getValue(); 2968 // Expand the incoming operand to be shifted, so that we have its parts 2969 SDOperand InL, InH; 2970 ExpandOp(Op, InL, InH); 2971 switch(Opc) { 2972 case ISD::SHL: 2973 if (Cst > VTBits) { 2974 Lo = DAG.getConstant(0, NVT); 2975 Hi = DAG.getConstant(0, NVT); 2976 } else if (Cst > NVTBits) { 2977 Lo = DAG.getConstant(0, NVT); 2978 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); 2979 } else if (Cst == NVTBits) { 2980 Lo = DAG.getConstant(0, NVT); 2981 Hi = InL; 2982 } else { 2983 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); 2984 Hi = DAG.getNode(ISD::OR, NVT, 2985 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), 2986 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); 2987 } 2988 return true; 2989 case ISD::SRL: 2990 if (Cst > VTBits) { 2991 Lo = DAG.getConstant(0, NVT); 2992 Hi = DAG.getConstant(0, NVT); 2993 } else if (Cst > NVTBits) { 2994 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); 2995 Hi = DAG.getConstant(0, NVT); 2996 } else if (Cst == NVTBits) { 2997 Lo = InH; 2998 Hi = DAG.getConstant(0, NVT); 2999 } else { 3000 Lo = DAG.getNode(ISD::OR, NVT, 3001 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 3002 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 3003 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); 3004 } 3005 return true; 3006 case ISD::SRA: 3007 if (Cst > VTBits) { 3008 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, 3009 DAG.getConstant(NVTBits-1, ShTy)); 3010 } else if (Cst > NVTBits) { 3011 Lo = DAG.getNode(ISD::SRA, NVT, InH, 3012 DAG.getConstant(Cst-NVTBits, ShTy)); 3013 Hi = DAG.getNode(ISD::SRA, NVT, InH, 3014 DAG.getConstant(NVTBits-1, ShTy)); 3015 } else if (Cst == NVTBits) { 3016 Lo = InH; 3017 Hi = DAG.getNode(ISD::SRA, NVT, InH, 3018 DAG.getConstant(NVTBits-1, ShTy)); 3019 } else { 3020 Lo = DAG.getNode(ISD::OR, NVT, 3021 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 3022 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 3023 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); 3024 } 3025 return true; 3026 } 3027 } 3028 return false; 3029} 3030 3031 3032// ExpandLibCall - Expand a node into a call to a libcall. If the result value 3033// does not fit into a register, return the lo part and set the hi part to the 3034// by-reg argument. If it does fit into a single register, return the result 3035// and leave the Hi part unset. 3036SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, 3037 SDOperand &Hi) { 3038 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); 3039 // The input chain to this libcall is the entry node of the function. 3040 // Legalizing the call will automatically add the previous call to the 3041 // dependence. 3042 SDOperand InChain = DAG.getEntryNode(); 3043 3044 TargetLowering::ArgListTy Args; 3045 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 3046 MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); 3047 const Type *ArgTy = MVT::getTypeForValueType(ArgVT); 3048 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); 3049 } 3050 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); 3051 3052 // Splice the libcall in wherever FindInputOutputChains tells us to. 3053 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); 3054 std::pair<SDOperand,SDOperand> CallInfo = 3055 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, 3056 Callee, Args, DAG); 3057 3058 // Legalize the call sequence, starting with the chain. This will advance 3059 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that 3060 // was added by LowerCallTo (guaranteeing proper serialization of calls). 3061 LegalizeOp(CallInfo.second); 3062 SDOperand Result; 3063 switch (getTypeAction(CallInfo.first.getValueType())) { 3064 default: assert(0 && "Unknown thing"); 3065 case Legal: 3066 Result = CallInfo.first; 3067 break; 3068 case Expand: 3069 ExpandOp(CallInfo.first, Result, Hi); 3070 break; 3071 } 3072 return Result; 3073} 3074 3075 3076/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the 3077/// destination type is legal. 3078SDOperand SelectionDAGLegalize:: 3079ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { 3080 assert(isTypeLegal(DestTy) && "Destination type is not legal!"); 3081 assert(getTypeAction(Source.getValueType()) == Expand && 3082 "This is not an expansion!"); 3083 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); 3084 3085 if (!isSigned) { 3086 assert(Source.getValueType() == MVT::i64 && 3087 "This only works for 64-bit -> FP"); 3088 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the 3089 // incoming integer is set. To handle this, we dynamically test to see if 3090 // it is set, and, if so, add a fudge factor. 3091 SDOperand Lo, Hi; 3092 ExpandOp(Source, Lo, Hi); 3093 3094 // If this is unsigned, and not supported, first perform the conversion to 3095 // signed, then adjust the result if the sign bit is set. 3096 SDOperand SignedConv = ExpandIntToFP(true, DestTy, 3097 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); 3098 3099 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, 3100 DAG.getConstant(0, Hi.getValueType()), 3101 ISD::SETLT); 3102 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); 3103 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), 3104 SignSet, Four, Zero); 3105 uint64_t FF = 0x5f800000ULL; 3106 if (TLI.isLittleEndian()) FF <<= 32; 3107 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); 3108 3109 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); 3110 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); 3111 SDOperand FudgeInReg; 3112 if (DestTy == MVT::f32) 3113 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, 3114 DAG.getSrcValue(NULL)); 3115 else { 3116 assert(DestTy == MVT::f64 && "Unexpected conversion"); 3117 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), 3118 CPIdx, DAG.getSrcValue(NULL), MVT::f32); 3119 } 3120 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); 3121 } 3122 3123 // Check to see if the target has a custom way to lower this. If so, use it. 3124 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { 3125 default: assert(0 && "This action not implemented for this operation!"); 3126 case TargetLowering::Legal: 3127 case TargetLowering::Expand: 3128 break; // This case is handled below. 3129 case TargetLowering::Custom: { 3130 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, 3131 Source), DAG); 3132 if (NV.Val) 3133 return LegalizeOp(NV); 3134 break; // The target decided this was legal after all 3135 } 3136 } 3137 3138 // Expand the source, then glue it back together for the call. We must expand 3139 // the source in case it is shared (this pass of legalize must traverse it). 3140 SDOperand SrcLo, SrcHi; 3141 ExpandOp(Source, SrcLo, SrcHi); 3142 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); 3143 3144 const char *FnName = 0; 3145 if (DestTy == MVT::f32) 3146 FnName = "__floatdisf"; 3147 else { 3148 assert(DestTy == MVT::f64 && "Unknown fp value type!"); 3149 FnName = "__floatdidf"; 3150 } 3151 3152 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); 3153 SDOperand UnusedHiPart; 3154 return ExpandLibCall(FnName, Source.Val, UnusedHiPart); 3155} 3156 3157/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a 3158/// INT_TO_FP operation of the specified operand when the target requests that 3159/// we expand it. At this point, we know that the result and operand types are 3160/// legal for the target. 3161SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, 3162 SDOperand Op0, 3163 MVT::ValueType DestVT) { 3164 if (Op0.getValueType() == MVT::i32) { 3165 // simple 32-bit [signed|unsigned] integer to float/double expansion 3166 3167 // get the stack frame index of a 8 byte buffer 3168 MachineFunction &MF = DAG.getMachineFunction(); 3169 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); 3170 // get address of 8 byte buffer 3171 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 3172 // word offset constant for Hi/Lo address computation 3173 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); 3174 // set up Hi and Lo (into buffer) address based on endian 3175 SDOperand Hi, Lo; 3176 if (TLI.isLittleEndian()) { 3177 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); 3178 Lo = StackSlot; 3179 } else { 3180 Hi = StackSlot; 3181 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); 3182 } 3183 // if signed map to unsigned space 3184 SDOperand Op0Mapped; 3185 if (isSigned) { 3186 // constant used to invert sign bit (signed to unsigned mapping) 3187 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); 3188 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); 3189 } else { 3190 Op0Mapped = Op0; 3191 } 3192 // store the lo of the constructed double - based on integer input 3193 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), 3194 Op0Mapped, Lo, DAG.getSrcValue(NULL)); 3195 // initial hi portion of constructed double 3196 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); 3197 // store the hi of the constructed double - biased exponent 3198 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1, 3199 InitialHi, Hi, DAG.getSrcValue(NULL)); 3200 // load the constructed double 3201 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, 3202 DAG.getSrcValue(NULL)); 3203 // FP constant to bias correct the final result 3204 SDOperand Bias = DAG.getConstantFP(isSigned ? 3205 BitsToDouble(0x4330000080000000ULL) 3206 : BitsToDouble(0x4330000000000000ULL), 3207 MVT::f64); 3208 // subtract the bias 3209 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); 3210 // final result 3211 SDOperand Result; 3212 // handle final rounding 3213 if (DestVT == MVT::f64) { 3214 // do nothing 3215 Result = Sub; 3216 } else { 3217 // if f32 then cast to f32 3218 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); 3219 } 3220 return Result; 3221 } 3222 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); 3223 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); 3224 3225 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, 3226 DAG.getConstant(0, Op0.getValueType()), 3227 ISD::SETLT); 3228 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); 3229 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), 3230 SignSet, Four, Zero); 3231 3232 // If the sign bit of the integer is set, the large number will be treated 3233 // as a negative number. To counteract this, the dynamic code adds an 3234 // offset depending on the data type. 3235 uint64_t FF; 3236 switch (Op0.getValueType()) { 3237 default: assert(0 && "Unsupported integer type!"); 3238 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 3239 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 3240 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 3241 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 3242 } 3243 if (TLI.isLittleEndian()) FF <<= 32; 3244 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); 3245 3246 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); 3247 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); 3248 SDOperand FudgeInReg; 3249 if (DestVT == MVT::f32) 3250 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, 3251 DAG.getSrcValue(NULL)); 3252 else { 3253 assert(DestVT == MVT::f64 && "Unexpected conversion"); 3254 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, 3255 DAG.getEntryNode(), CPIdx, 3256 DAG.getSrcValue(NULL), MVT::f32)); 3257 } 3258 3259 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); 3260} 3261 3262/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a 3263/// *INT_TO_FP operation of the specified operand when the target requests that 3264/// we promote it. At this point, we know that the result and operand types are 3265/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 3266/// operation that takes a larger input. 3267SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, 3268 MVT::ValueType DestVT, 3269 bool isSigned) { 3270 // First step, figure out the appropriate *INT_TO_FP operation to use. 3271 MVT::ValueType NewInTy = LegalOp.getValueType(); 3272 3273 unsigned OpToUse = 0; 3274 3275 // Scan for the appropriate larger type to use. 3276 while (1) { 3277 NewInTy = (MVT::ValueType)(NewInTy+1); 3278 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); 3279 3280 // If the target supports SINT_TO_FP of this type, use it. 3281 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { 3282 default: break; 3283 case TargetLowering::Legal: 3284 if (!TLI.isTypeLegal(NewInTy)) 3285 break; // Can't use this datatype. 3286 // FALL THROUGH. 3287 case TargetLowering::Custom: 3288 OpToUse = ISD::SINT_TO_FP; 3289 break; 3290 } 3291 if (OpToUse) break; 3292 if (isSigned) continue; 3293 3294 // If the target supports UINT_TO_FP of this type, use it. 3295 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { 3296 default: break; 3297 case TargetLowering::Legal: 3298 if (!TLI.isTypeLegal(NewInTy)) 3299 break; // Can't use this datatype. 3300 // FALL THROUGH. 3301 case TargetLowering::Custom: 3302 OpToUse = ISD::UINT_TO_FP; 3303 break; 3304 } 3305 if (OpToUse) break; 3306 3307 // Otherwise, try a larger type. 3308 } 3309 3310 // Okay, we found the operation and type to use. Zero extend our input to the 3311 // desired type then run the operation on it. 3312 return DAG.getNode(OpToUse, DestVT, 3313 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 3314 NewInTy, LegalOp)); 3315} 3316 3317/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a 3318/// FP_TO_*INT operation of the specified operand when the target requests that 3319/// we promote it. At this point, we know that the result and operand types are 3320/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 3321/// operation that returns a larger result. 3322SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, 3323 MVT::ValueType DestVT, 3324 bool isSigned) { 3325 // First step, figure out the appropriate FP_TO*INT operation to use. 3326 MVT::ValueType NewOutTy = DestVT; 3327 3328 unsigned OpToUse = 0; 3329 3330 // Scan for the appropriate larger type to use. 3331 while (1) { 3332 NewOutTy = (MVT::ValueType)(NewOutTy+1); 3333 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); 3334 3335 // If the target supports FP_TO_SINT returning this type, use it. 3336 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { 3337 default: break; 3338 case TargetLowering::Legal: 3339 if (!TLI.isTypeLegal(NewOutTy)) 3340 break; // Can't use this datatype. 3341 // FALL THROUGH. 3342 case TargetLowering::Custom: 3343 OpToUse = ISD::FP_TO_SINT; 3344 break; 3345 } 3346 if (OpToUse) break; 3347 3348 // If the target supports FP_TO_UINT of this type, use it. 3349 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { 3350 default: break; 3351 case TargetLowering::Legal: 3352 if (!TLI.isTypeLegal(NewOutTy)) 3353 break; // Can't use this datatype. 3354 // FALL THROUGH. 3355 case TargetLowering::Custom: 3356 OpToUse = ISD::FP_TO_UINT; 3357 break; 3358 } 3359 if (OpToUse) break; 3360 3361 // Otherwise, try a larger type. 3362 } 3363 3364 // Okay, we found the operation and type to use. Truncate the result of the 3365 // extended FP_TO_*INT operation to the desired size. 3366 return DAG.getNode(ISD::TRUNCATE, DestVT, 3367 DAG.getNode(OpToUse, NewOutTy, LegalOp)); 3368} 3369 3370/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. 3371/// 3372SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { 3373 MVT::ValueType VT = Op.getValueType(); 3374 MVT::ValueType SHVT = TLI.getShiftAmountTy(); 3375 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; 3376 switch (VT) { 3377 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); 3378 case MVT::i16: 3379 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 3380 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 3381 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); 3382 case MVT::i32: 3383 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); 3384 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 3385 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 3386 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); 3387 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); 3388 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); 3389 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); 3390 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); 3391 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); 3392 case MVT::i64: 3393 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); 3394 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); 3395 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); 3396 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 3397 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 3398 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); 3399 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); 3400 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); 3401 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); 3402 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); 3403 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); 3404 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); 3405 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); 3406 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); 3407 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); 3408 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); 3409 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); 3410 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); 3411 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); 3412 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); 3413 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); 3414 } 3415} 3416 3417/// ExpandBitCount - Expand the specified bitcount instruction into operations. 3418/// 3419SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { 3420 switch (Opc) { 3421 default: assert(0 && "Cannot expand this yet!"); 3422 case ISD::CTPOP: { 3423 static const uint64_t mask[6] = { 3424 0x5555555555555555ULL, 0x3333333333333333ULL, 3425 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, 3426 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL 3427 }; 3428 MVT::ValueType VT = Op.getValueType(); 3429 MVT::ValueType ShVT = TLI.getShiftAmountTy(); 3430 unsigned len = getSizeInBits(VT); 3431 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 3432 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) 3433 SDOperand Tmp2 = DAG.getConstant(mask[i], VT); 3434 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); 3435 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), 3436 DAG.getNode(ISD::AND, VT, 3437 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); 3438 } 3439 return Op; 3440 } 3441 case ISD::CTLZ: { 3442 // for now, we do this: 3443 // x = x | (x >> 1); 3444 // x = x | (x >> 2); 3445 // ... 3446 // x = x | (x >>16); 3447 // x = x | (x >>32); // for 64-bit input 3448 // return popcount(~x); 3449 // 3450 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc 3451 MVT::ValueType VT = Op.getValueType(); 3452 MVT::ValueType ShVT = TLI.getShiftAmountTy(); 3453 unsigned len = getSizeInBits(VT); 3454 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 3455 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); 3456 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); 3457 } 3458 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); 3459 return DAG.getNode(ISD::CTPOP, VT, Op); 3460 } 3461 case ISD::CTTZ: { 3462 // for now, we use: { return popcount(~x & (x - 1)); } 3463 // unless the target has ctlz but not ctpop, in which case we use: 3464 // { return 32 - nlz(~x & (x-1)); } 3465 // see also http://www.hackersdelight.org/HDcode/ntz.cc 3466 MVT::ValueType VT = Op.getValueType(); 3467 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); 3468 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, 3469 DAG.getNode(ISD::XOR, VT, Op, Tmp2), 3470 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); 3471 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. 3472 if (!TLI.isOperationLegal(ISD::CTPOP, VT) && 3473 TLI.isOperationLegal(ISD::CTLZ, VT)) 3474 return DAG.getNode(ISD::SUB, VT, 3475 DAG.getConstant(getSizeInBits(VT), VT), 3476 DAG.getNode(ISD::CTLZ, VT, Tmp3)); 3477 return DAG.getNode(ISD::CTPOP, VT, Tmp3); 3478 } 3479 } 3480} 3481 3482 3483/// ExpandOp - Expand the specified SDOperand into its two component pieces 3484/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the 3485/// LegalizeNodes map is filled in for any results that are not expanded, the 3486/// ExpandedNodes map is filled in for any results that are expanded, and the 3487/// Lo/Hi values are returned. 3488void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ 3489 MVT::ValueType VT = Op.getValueType(); 3490 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 3491 SDNode *Node = Op.Val; 3492 assert(getTypeAction(VT) == Expand && "Not an expanded type!"); 3493 assert((MVT::isInteger(VT) || VT == MVT::Vector) && 3494 "Cannot expand FP values!"); 3495 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) && 3496 "Cannot expand to FP value or to larger int value!"); 3497 3498 // See if we already expanded it. 3499 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 3500 = ExpandedNodes.find(Op); 3501 if (I != ExpandedNodes.end()) { 3502 Lo = I->second.first; 3503 Hi = I->second.second; 3504 return; 3505 } 3506 3507 switch (Node->getOpcode()) { 3508 case ISD::CopyFromReg: 3509 assert(0 && "CopyFromReg must be legal!"); 3510 default: 3511 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 3512 assert(0 && "Do not know how to expand this operator!"); 3513 abort(); 3514 case ISD::UNDEF: 3515 Lo = DAG.getNode(ISD::UNDEF, NVT); 3516 Hi = DAG.getNode(ISD::UNDEF, NVT); 3517 break; 3518 case ISD::Constant: { 3519 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); 3520 Lo = DAG.getConstant(Cst, NVT); 3521 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); 3522 break; 3523 } 3524 case ISD::BUILD_PAIR: 3525 // Return the operands. 3526 Lo = Node->getOperand(0); 3527 Hi = Node->getOperand(1); 3528 break; 3529 3530 case ISD::SIGN_EXTEND_INREG: 3531 ExpandOp(Node->getOperand(0), Lo, Hi); 3532 // Sign extend the lo-part. 3533 Hi = DAG.getNode(ISD::SRA, NVT, Lo, 3534 DAG.getConstant(MVT::getSizeInBits(NVT)-1, 3535 TLI.getShiftAmountTy())); 3536 // sext_inreg the low part if needed. 3537 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); 3538 break; 3539 3540 case ISD::BSWAP: { 3541 ExpandOp(Node->getOperand(0), Lo, Hi); 3542 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); 3543 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); 3544 Lo = TempLo; 3545 break; 3546 } 3547 3548 case ISD::CTPOP: 3549 ExpandOp(Node->getOperand(0), Lo, Hi); 3550 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) 3551 DAG.getNode(ISD::CTPOP, NVT, Lo), 3552 DAG.getNode(ISD::CTPOP, NVT, Hi)); 3553 Hi = DAG.getConstant(0, NVT); 3554 break; 3555 3556 case ISD::CTLZ: { 3557 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) 3558 ExpandOp(Node->getOperand(0), Lo, Hi); 3559 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); 3560 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); 3561 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, 3562 ISD::SETNE); 3563 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); 3564 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); 3565 3566 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); 3567 Hi = DAG.getConstant(0, NVT); 3568 break; 3569 } 3570 3571 case ISD::CTTZ: { 3572 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) 3573 ExpandOp(Node->getOperand(0), Lo, Hi); 3574 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); 3575 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); 3576 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, 3577 ISD::SETNE); 3578 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); 3579 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); 3580 3581 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); 3582 Hi = DAG.getConstant(0, NVT); 3583 break; 3584 } 3585 3586 case ISD::VAARG: { 3587 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 3588 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 3589 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); 3590 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); 3591 3592 // Remember that we legalized the chain. 3593 Hi = LegalizeOp(Hi); 3594 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); 3595 if (!TLI.isLittleEndian()) 3596 std::swap(Lo, Hi); 3597 break; 3598 } 3599 3600 case ISD::LOAD: { 3601 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 3602 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 3603 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); 3604 3605 // Increment the pointer to the other half. 3606 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; 3607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 3608 getIntPtrConstant(IncrementSize)); 3609 // FIXME: This creates a bogus srcvalue! 3610 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); 3611 3612 // Build a factor node to remember that this load is independent of the 3613 // other one. 3614 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 3615 Hi.getValue(1)); 3616 3617 // Remember that we legalized the chain. 3618 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); 3619 if (!TLI.isLittleEndian()) 3620 std::swap(Lo, Hi); 3621 break; 3622 } 3623 case ISD::AND: 3624 case ISD::OR: 3625 case ISD::XOR: { // Simple logical operators -> two trivial pieces. 3626 SDOperand LL, LH, RL, RH; 3627 ExpandOp(Node->getOperand(0), LL, LH); 3628 ExpandOp(Node->getOperand(1), RL, RH); 3629 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); 3630 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); 3631 break; 3632 } 3633 case ISD::SELECT: { 3634 SDOperand LL, LH, RL, RH; 3635 ExpandOp(Node->getOperand(1), LL, LH); 3636 ExpandOp(Node->getOperand(2), RL, RH); 3637 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); 3638 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); 3639 break; 3640 } 3641 case ISD::SELECT_CC: { 3642 SDOperand TL, TH, FL, FH; 3643 ExpandOp(Node->getOperand(2), TL, TH); 3644 ExpandOp(Node->getOperand(3), FL, FH); 3645 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 3646 Node->getOperand(1), TL, FL, Node->getOperand(4)); 3647 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 3648 Node->getOperand(1), TH, FH, Node->getOperand(4)); 3649 break; 3650 } 3651 case ISD::SEXTLOAD: { 3652 SDOperand Chain = Node->getOperand(0); 3653 SDOperand Ptr = Node->getOperand(1); 3654 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3655 3656 if (EVT == NVT) 3657 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3658 else 3659 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3660 EVT); 3661 3662 // Remember that we legalized the chain. 3663 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3664 3665 // The high part is obtained by SRA'ing all but one of the bits of the lo 3666 // part. 3667 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 3668 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, 3669 TLI.getShiftAmountTy())); 3670 break; 3671 } 3672 case ISD::ZEXTLOAD: { 3673 SDOperand Chain = Node->getOperand(0); 3674 SDOperand Ptr = Node->getOperand(1); 3675 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3676 3677 if (EVT == NVT) 3678 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3679 else 3680 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3681 EVT); 3682 3683 // Remember that we legalized the chain. 3684 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3685 3686 // The high part is just a zero. 3687 Hi = DAG.getConstant(0, NVT); 3688 break; 3689 } 3690 case ISD::EXTLOAD: { 3691 SDOperand Chain = Node->getOperand(0); 3692 SDOperand Ptr = Node->getOperand(1); 3693 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3694 3695 if (EVT == NVT) 3696 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3697 else 3698 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3699 EVT); 3700 3701 // Remember that we legalized the chain. 3702 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3703 3704 // The high part is undefined. 3705 Hi = DAG.getNode(ISD::UNDEF, NVT); 3706 break; 3707 } 3708 case ISD::ANY_EXTEND: 3709 // The low part is any extension of the input (which degenerates to a copy). 3710 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); 3711 // The high part is undefined. 3712 Hi = DAG.getNode(ISD::UNDEF, NVT); 3713 break; 3714 case ISD::SIGN_EXTEND: { 3715 // The low part is just a sign extension of the input (which degenerates to 3716 // a copy). 3717 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); 3718 3719 // The high part is obtained by SRA'ing all but one of the bits of the lo 3720 // part. 3721 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 3722 Hi = DAG.getNode(ISD::SRA, NVT, Lo, 3723 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); 3724 break; 3725 } 3726 case ISD::ZERO_EXTEND: 3727 // The low part is just a zero extension of the input (which degenerates to 3728 // a copy). 3729 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); 3730 3731 // The high part is just a zero. 3732 Hi = DAG.getConstant(0, NVT); 3733 break; 3734 3735 case ISD::BIT_CONVERT: { 3736 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0), 3737 Node->getOperand(0)); 3738 ExpandOp(Tmp, Lo, Hi); 3739 break; 3740 } 3741 3742 case ISD::READCYCLECOUNTER: 3743 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == 3744 TargetLowering::Custom && 3745 "Must custom expand ReadCycleCounter"); 3746 Lo = TLI.LowerOperation(Op, DAG); 3747 assert(Lo.Val && "Node must be custom expanded!"); 3748 Hi = Lo.getValue(1); 3749 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. 3750 LegalizeOp(Lo.getValue(2))); 3751 break; 3752 3753 // These operators cannot be expanded directly, emit them as calls to 3754 // library functions. 3755 case ISD::FP_TO_SINT: 3756 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { 3757 SDOperand Op; 3758 switch (getTypeAction(Node->getOperand(0).getValueType())) { 3759 case Expand: assert(0 && "cannot expand FP!"); 3760 case Legal: Op = LegalizeOp(Node->getOperand(0)); break; 3761 case Promote: Op = PromoteOp (Node->getOperand(0)); break; 3762 } 3763 3764 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); 3765 3766 // Now that the custom expander is done, expand the result, which is still 3767 // VT. 3768 if (Op.Val) { 3769 ExpandOp(Op, Lo, Hi); 3770 break; 3771 } 3772 } 3773 3774 if (Node->getOperand(0).getValueType() == MVT::f32) 3775 Lo = ExpandLibCall("__fixsfdi", Node, Hi); 3776 else 3777 Lo = ExpandLibCall("__fixdfdi", Node, Hi); 3778 break; 3779 3780 case ISD::FP_TO_UINT: 3781 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { 3782 SDOperand Op; 3783 switch (getTypeAction(Node->getOperand(0).getValueType())) { 3784 case Expand: assert(0 && "cannot expand FP!"); 3785 case Legal: Op = LegalizeOp(Node->getOperand(0)); break; 3786 case Promote: Op = PromoteOp (Node->getOperand(0)); break; 3787 } 3788 3789 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); 3790 3791 // Now that the custom expander is done, expand the result. 3792 if (Op.Val) { 3793 ExpandOp(Op, Lo, Hi); 3794 break; 3795 } 3796 } 3797 3798 if (Node->getOperand(0).getValueType() == MVT::f32) 3799 Lo = ExpandLibCall("__fixunssfdi", Node, Hi); 3800 else 3801 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); 3802 break; 3803 3804 case ISD::SHL: { 3805 // If the target wants custom lowering, do so. 3806 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 3807 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { 3808 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); 3809 Op = TLI.LowerOperation(Op, DAG); 3810 if (Op.Val) { 3811 // Now that the custom expander is done, expand the result, which is 3812 // still VT. 3813 ExpandOp(Op, Lo, Hi); 3814 break; 3815 } 3816 } 3817 3818 // If we can emit an efficient shift operation, do so now. 3819 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) 3820 break; 3821 3822 // If this target supports SHL_PARTS, use it. 3823 TargetLowering::LegalizeAction Action = 3824 TLI.getOperationAction(ISD::SHL_PARTS, NVT); 3825 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 3826 Action == TargetLowering::Custom) { 3827 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 3828 break; 3829 } 3830 3831 // Otherwise, emit a libcall. 3832 Lo = ExpandLibCall("__ashldi3", Node, Hi); 3833 break; 3834 } 3835 3836 case ISD::SRA: { 3837 // If the target wants custom lowering, do so. 3838 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 3839 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { 3840 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); 3841 Op = TLI.LowerOperation(Op, DAG); 3842 if (Op.Val) { 3843 // Now that the custom expander is done, expand the result, which is 3844 // still VT. 3845 ExpandOp(Op, Lo, Hi); 3846 break; 3847 } 3848 } 3849 3850 // If we can emit an efficient shift operation, do so now. 3851 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) 3852 break; 3853 3854 // If this target supports SRA_PARTS, use it. 3855 TargetLowering::LegalizeAction Action = 3856 TLI.getOperationAction(ISD::SRA_PARTS, NVT); 3857 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 3858 Action == TargetLowering::Custom) { 3859 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 3860 break; 3861 } 3862 3863 // Otherwise, emit a libcall. 3864 Lo = ExpandLibCall("__ashrdi3", Node, Hi); 3865 break; 3866 } 3867 3868 case ISD::SRL: { 3869 // If the target wants custom lowering, do so. 3870 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 3871 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { 3872 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); 3873 Op = TLI.LowerOperation(Op, DAG); 3874 if (Op.Val) { 3875 // Now that the custom expander is done, expand the result, which is 3876 // still VT. 3877 ExpandOp(Op, Lo, Hi); 3878 break; 3879 } 3880 } 3881 3882 // If we can emit an efficient shift operation, do so now. 3883 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) 3884 break; 3885 3886 // If this target supports SRL_PARTS, use it. 3887 TargetLowering::LegalizeAction Action = 3888 TLI.getOperationAction(ISD::SRL_PARTS, NVT); 3889 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 3890 Action == TargetLowering::Custom) { 3891 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 3892 break; 3893 } 3894 3895 // Otherwise, emit a libcall. 3896 Lo = ExpandLibCall("__lshrdi3", Node, Hi); 3897 break; 3898 } 3899 3900 case ISD::ADD: 3901 case ISD::SUB: { 3902 // If the target wants to custom expand this, let them. 3903 if (TLI.getOperationAction(Node->getOpcode(), VT) == 3904 TargetLowering::Custom) { 3905 Op = TLI.LowerOperation(Op, DAG); 3906 if (Op.Val) { 3907 ExpandOp(Op, Lo, Hi); 3908 break; 3909 } 3910 } 3911 3912 // Expand the subcomponents. 3913 SDOperand LHSL, LHSH, RHSL, RHSH; 3914 ExpandOp(Node->getOperand(0), LHSL, LHSH); 3915 ExpandOp(Node->getOperand(1), RHSL, RHSH); 3916 std::vector<MVT::ValueType> VTs; 3917 std::vector<SDOperand> LoOps, HiOps; 3918 VTs.push_back(LHSL.getValueType()); 3919 VTs.push_back(MVT::Flag); 3920 LoOps.push_back(LHSL); 3921 LoOps.push_back(RHSL); 3922 HiOps.push_back(LHSH); 3923 HiOps.push_back(RHSH); 3924 if (Node->getOpcode() == ISD::ADD) { 3925 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps); 3926 HiOps.push_back(Lo.getValue(1)); 3927 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps); 3928 } else { 3929 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps); 3930 HiOps.push_back(Lo.getValue(1)); 3931 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps); 3932 } 3933 break; 3934 } 3935 case ISD::MUL: { 3936 if (TLI.isOperationLegal(ISD::MULHU, NVT)) { 3937 SDOperand LL, LH, RL, RH; 3938 ExpandOp(Node->getOperand(0), LL, LH); 3939 ExpandOp(Node->getOperand(1), RL, RH); 3940 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; 3941 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp 3942 // extended the sign bit of the low half through the upper half, and if so 3943 // emit a MULHS instead of the alternate sequence that is valid for any 3944 // i64 x i64 multiply. 3945 if (TLI.isOperationLegal(ISD::MULHS, NVT) && 3946 // is RH an extension of the sign bit of RL? 3947 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && 3948 RH.getOperand(1).getOpcode() == ISD::Constant && 3949 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && 3950 // is LH an extension of the sign bit of LL? 3951 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && 3952 LH.getOperand(1).getOpcode() == ISD::Constant && 3953 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { 3954 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); 3955 } else { 3956 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); 3957 RH = DAG.getNode(ISD::MUL, NVT, LL, RH); 3958 LH = DAG.getNode(ISD::MUL, NVT, LH, RL); 3959 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); 3960 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); 3961 } 3962 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); 3963 } else { 3964 Lo = ExpandLibCall("__muldi3" , Node, Hi); 3965 } 3966 break; 3967 } 3968 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; 3969 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; 3970 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; 3971 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; 3972 } 3973 3974 // Make sure the resultant values have been legalized themselves, unless this 3975 // is a type that requires multi-step expansion. 3976 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { 3977 Lo = LegalizeOp(Lo); 3978 Hi = LegalizeOp(Hi); 3979 } 3980 3981 // Remember in a map if the values will be reused later. 3982 bool isNew = 3983 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; 3984 assert(isNew && "Value already expanded?!?"); 3985} 3986 3987/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into 3988/// two smaller values of MVT::Vector type. 3989void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, 3990 SDOperand &Hi) { 3991 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); 3992 SDNode *Node = Op.Val; 3993 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); 3994 assert(NumElements > 1 && "Cannot split a single element vector!"); 3995 unsigned NewNumElts = NumElements/2; 3996 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); 3997 SDOperand TypeNode = *(Node->op_end()-1); 3998 3999 // See if we already split it. 4000 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 4001 = SplitNodes.find(Op); 4002 if (I != SplitNodes.end()) { 4003 Lo = I->second.first; 4004 Hi = I->second.second; 4005 return; 4006 } 4007 4008 switch (Node->getOpcode()) { 4009 case ISD::VConstant: { 4010 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts); 4011 LoOps.push_back(NewNumEltsNode); 4012 LoOps.push_back(TypeNode); 4013 Lo = DAG.getNode(ISD::VConstant, MVT::Vector, LoOps); 4014 4015 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2); 4016 HiOps.push_back(NewNumEltsNode); 4017 HiOps.push_back(TypeNode); 4018 Hi = DAG.getNode(ISD::VConstant, MVT::Vector, HiOps); 4019 break; 4020 } 4021 case ISD::VADD: 4022 case ISD::VSUB: 4023 case ISD::VMUL: 4024 case ISD::VSDIV: 4025 case ISD::VUDIV: 4026 case ISD::VAND: 4027 case ISD::VOR: 4028 case ISD::VXOR: { 4029 SDOperand LL, LH, RL, RH; 4030 SplitVectorOp(Node->getOperand(0), LL, LH); 4031 SplitVectorOp(Node->getOperand(1), RL, RH); 4032 4033 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, 4034 NewNumEltsNode, TypeNode); 4035 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, 4036 NewNumEltsNode, TypeNode); 4037 break; 4038 } 4039 case ISD::VLOAD: { 4040 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 4041 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 4042 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); 4043 4044 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); 4045 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; 4046 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 4047 getIntPtrConstant(IncrementSize)); 4048 // FIXME: This creates a bogus srcvalue! 4049 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); 4050 4051 // Build a factor node to remember that this load is independent of the 4052 // other one. 4053 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 4054 Hi.getValue(1)); 4055 4056 // Remember that we legalized the chain. 4057 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); 4058 if (!TLI.isLittleEndian()) 4059 std::swap(Lo, Hi); 4060 break; 4061 } 4062 } 4063 4064 // Remember in a map if the values will be reused later. 4065 bool isNew = 4066 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; 4067 assert(isNew && "Value already expanded?!?"); 4068} 4069 4070 4071/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the 4072/// equivalent operation that returns a scalar (e.g. F32) or packed value 4073/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right 4074/// type for the result. 4075SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, 4076 MVT::ValueType NewVT) { 4077 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); 4078 SDNode *Node = Op.Val; 4079 4080 // See if we already packed it. 4081 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); 4082 if (I != PackedNodes.end()) return I->second; 4083 4084 SDOperand Result; 4085 switch (Node->getOpcode()) { 4086 default: assert(0 && "Unknown vector operation!"); 4087 case ISD::VADD: 4088 case ISD::VSUB: 4089 case ISD::VMUL: 4090 case ISD::VSDIV: 4091 case ISD::VUDIV: 4092 case ISD::VAND: 4093 case ISD::VOR: 4094 case ISD::VXOR: 4095 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), 4096 NewVT, 4097 PackVectorOp(Node->getOperand(0), NewVT), 4098 PackVectorOp(Node->getOperand(1), NewVT)); 4099 break; 4100 case ISD::VLOAD: { 4101 SDOperand Ch = LegalizeOp(Node->getOperand(2)); // Legalize the chain. 4102 SDOperand Ptr = LegalizeOp(Node->getOperand(3)); // Legalize the pointer. 4103 4104 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(4)); 4105 4106 // Remember that we legalized the chain. 4107 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 4108 break; 4109 } 4110 case ISD::VConstant: 4111 if (!MVT::isVector(NewVT)) { 4112 Result = Node->getOperand(0); 4113 } else { 4114 // If type of bisected vector is legal, turn it into a ConstantVec (which 4115 // will be lowered to a ConstantPool or something else). Otherwise, bisect 4116 // the VConstant, and return each half as a new VConstant. 4117 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2); 4118 Result = DAG.getNode(ISD::ConstantVec, NewVT, Ops); 4119 } 4120 break; 4121 } 4122 4123 if (TLI.isTypeLegal(NewVT)) 4124 Result = LegalizeOp(Result); 4125 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; 4126 assert(isNew && "Value already packed?"); 4127 return Result; 4128} 4129 4130 4131// SelectionDAG::Legalize - This is the entry point for the file. 4132// 4133void SelectionDAG::Legalize() { 4134 /// run - This is the main entry point to this class. 4135 /// 4136 SelectionDAGLegalize(*this).LegalizeDAG(); 4137} 4138 4139