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