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