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