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