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