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