LegalizeDAG.cpp revision 5b95ed652fcfe578aa8af4e21318fca989164e21
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/MachineConstantPool.h" 16#include "llvm/CodeGen/MachineFunction.h" 17#include "llvm/CodeGen/MachineFrameInfo.h" 18#include "llvm/Target/TargetLowering.h" 19#include "llvm/Target/TargetData.h" 20#include "llvm/Target/TargetOptions.h" 21#include "llvm/Constants.h" 22#include <iostream> 23using namespace llvm; 24 25//===----------------------------------------------------------------------===// 26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and 27/// hacks on it until the target machine can handle it. This involves 28/// eliminating value sizes the machine cannot handle (promoting small sizes to 29/// large sizes or splitting up large values into small values) as well as 30/// eliminating operations the machine cannot handle. 31/// 32/// This code also does a small amount of optimization and recognition of idioms 33/// as part of its processing. For example, if a target does not support a 34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 35/// will attempt merge setcc and brc instructions into brcc's. 36/// 37namespace { 38class SelectionDAGLegalize { 39 TargetLowering &TLI; 40 SelectionDAG &DAG; 41 42 /// LegalizeAction - This enum indicates what action we should take for each 43 /// value type the can occur in the program. 44 enum LegalizeAction { 45 Legal, // The target natively supports this value type. 46 Promote, // This should be promoted to the next larger type. 47 Expand, // This integer type should be broken into smaller pieces. 48 }; 49 50 /// ValueTypeActions - This is a bitvector that contains two bits for each 51 /// value type, where the two bits correspond to the LegalizeAction enum. 52 /// This can be queried with "getTypeAction(VT)". 53 unsigned ValueTypeActions; 54 55 /// NeedsAnotherIteration - This is set when we expand a large integer 56 /// operation into smaller integer operations, but the smaller operations are 57 /// not set. This occurs only rarely in practice, for targets that don't have 58 /// 32-bit or larger integer registers. 59 bool NeedsAnotherIteration; 60 61 /// LegalizedNodes - For nodes that are of legal width, and that have more 62 /// than one use, this map indicates what regularized operand to use. This 63 /// allows us to avoid legalizing the same thing more than once. 64 std::map<SDOperand, SDOperand> LegalizedNodes; 65 66 /// PromotedNodes - For nodes that are below legal width, and that have more 67 /// than one use, this map indicates what promoted value to use. This allows 68 /// us to avoid promoting the same thing more than once. 69 std::map<SDOperand, SDOperand> PromotedNodes; 70 71 /// ExpandedNodes - For nodes that need to be expanded, and which have more 72 /// than one use, this map indicates which which operands are the expanded 73 /// version of the input. This allows us to avoid expanding the same node 74 /// more than once. 75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; 76 77 void AddLegalizedOperand(SDOperand From, SDOperand To) { 78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second; 79 assert(isNew && "Got into the map somehow?"); 80 } 81 void AddPromotedOperand(SDOperand From, SDOperand To) { 82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; 83 assert(isNew && "Got into the map somehow?"); 84 } 85 86public: 87 88 SelectionDAGLegalize(SelectionDAG &DAG); 89 90 /// Run - While there is still lowering to do, perform a pass over the DAG. 91 /// Most regularization can be done in a single pass, but targets that require 92 /// large values to be split into registers multiple times (e.g. i64 -> 4x 93 /// i16) require iteration for these values (the first iteration will demote 94 /// to i32, the second will demote to i16). 95 void Run() { 96 do { 97 NeedsAnotherIteration = false; 98 LegalizeDAG(); 99 } while (NeedsAnotherIteration); 100 } 101 102 /// getTypeAction - Return how we should legalize values of this type, either 103 /// it is already legal or we need to expand it into multiple registers of 104 /// smaller integer type, or we need to promote it to a larger type. 105 LegalizeAction getTypeAction(MVT::ValueType VT) const { 106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); 107 } 108 109 /// isTypeLegal - Return true if this type is legal on this target. 110 /// 111 bool isTypeLegal(MVT::ValueType VT) const { 112 return getTypeAction(VT) == Legal; 113 } 114 115private: 116 void LegalizeDAG(); 117 118 SDOperand LegalizeOp(SDOperand O); 119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); 120 SDOperand PromoteOp(SDOperand O); 121 122 SDOperand ExpandLibCall(const char *Name, SDNode *Node, 123 SDOperand &Hi); 124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, 125 SDOperand Source); 126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, 127 SDOperand &Lo, SDOperand &Hi); 128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, 129 SDOperand &Lo, SDOperand &Hi); 130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, 131 SDOperand &Lo, SDOperand &Hi); 132 133 SDOperand getIntPtrConstant(uint64_t Val) { 134 return DAG.getConstant(Val, TLI.getPointerTy()); 135 } 136}; 137} 138 139 140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) 141 : TLI(dag.getTargetLoweringInfo()), DAG(dag), 142 ValueTypeActions(TLI.getValueTypeActions()) { 143 assert(MVT::LAST_VALUETYPE <= 16 && 144 "Too many value types for ValueTypeActions to hold!"); 145} 146 147void SelectionDAGLegalize::LegalizeDAG() { 148 SDOperand OldRoot = DAG.getRoot(); 149 SDOperand NewRoot = LegalizeOp(OldRoot); 150 DAG.setRoot(NewRoot); 151 152 ExpandedNodes.clear(); 153 LegalizedNodes.clear(); 154 PromotedNodes.clear(); 155 156 // Remove dead nodes now. 157 DAG.RemoveDeadNodes(OldRoot.Val); 158} 159 160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 161 assert(getTypeAction(Op.getValueType()) == Legal && 162 "Caller should expand or promote operands that are not legal!"); 163 164 // If this operation defines any values that cannot be represented in a 165 // register on this target, make sure to expand or promote them. 166 if (Op.Val->getNumValues() > 1) { 167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i) 168 switch (getTypeAction(Op.Val->getValueType(i))) { 169 case Legal: break; // Nothing to do. 170 case Expand: { 171 SDOperand T1, T2; 172 ExpandOp(Op.getValue(i), T1, T2); 173 assert(LegalizedNodes.count(Op) && 174 "Expansion didn't add legal operands!"); 175 return LegalizedNodes[Op]; 176 } 177 case Promote: 178 PromoteOp(Op.getValue(i)); 179 assert(LegalizedNodes.count(Op) && 180 "Expansion didn't add legal operands!"); 181 return LegalizedNodes[Op]; 182 } 183 } 184 185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 186 if (I != LegalizedNodes.end()) return I->second; 187 188 SDOperand Tmp1, Tmp2, Tmp3; 189 190 SDOperand Result = Op; 191 SDNode *Node = Op.Val; 192 193 switch (Node->getOpcode()) { 194 default: 195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 196 assert(0 && "Do not know how to legalize this operator!"); 197 abort(); 198 case ISD::EntryToken: 199 case ISD::FrameIndex: 200 case ISD::GlobalAddress: 201 case ISD::ExternalSymbol: 202 case ISD::ConstantPool: // Nothing to do. 203 assert(getTypeAction(Node->getValueType(0)) == Legal && 204 "This must be legal!"); 205 break; 206 case ISD::CopyFromReg: 207 Tmp1 = LegalizeOp(Node->getOperand(0)); 208 if (Tmp1 != Node->getOperand(0)) 209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), 210 Node->getValueType(0), Tmp1); 211 else 212 Result = Op.getValue(0); 213 214 // Since CopyFromReg produces two values, make sure to remember that we 215 // legalized both of them. 216 AddLegalizedOperand(Op.getValue(0), Result); 217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 218 return Result.getValue(Op.ResNo); 219 case ISD::ImplicitDef: 220 Tmp1 = LegalizeOp(Node->getOperand(0)); 221 if (Tmp1 != Node->getOperand(0)) 222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg()); 223 break; 224 case ISD::UNDEF: { 225 MVT::ValueType VT = Op.getValueType(); 226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) { 227 default: assert(0 && "This action is not supported yet!"); 228 case TargetLowering::Expand: 229 case TargetLowering::Promote: 230 if (MVT::isInteger(VT)) 231 Result = DAG.getConstant(0, VT); 232 else if (MVT::isFloatingPoint(VT)) 233 Result = DAG.getConstantFP(0, VT); 234 else 235 assert(0 && "Unknown value type!"); 236 break; 237 case TargetLowering::Legal: 238 break; 239 } 240 break; 241 } 242 case ISD::Constant: 243 // We know we don't need to expand constants here, constants only have one 244 // value and we check that it is fine above. 245 246 // FIXME: Maybe we should handle things like targets that don't support full 247 // 32-bit immediates? 248 break; 249 case ISD::ConstantFP: { 250 // Spill FP immediates to the constant pool if the target cannot directly 251 // codegen them. Targets often have some immediate values that can be 252 // efficiently generated into an FP register without a load. We explicitly 253 // leave these constants as ConstantFP nodes for the target to deal with. 254 255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 256 257 // Check to see if this FP immediate is already legal. 258 bool isLegal = false; 259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), 260 E = TLI.legal_fpimm_end(); I != E; ++I) 261 if (CFP->isExactlyValue(*I)) { 262 isLegal = true; 263 break; 264 } 265 266 if (!isLegal) { 267 // Otherwise we need to spill the constant to memory. 268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); 269 270 bool Extend = false; 271 272 // If a FP immediate is precise when represented as a float, we put it 273 // into the constant pool as a float, even if it's is statically typed 274 // as a double. 275 MVT::ValueType VT = CFP->getValueType(0); 276 bool isDouble = VT == MVT::f64; 277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : 278 Type::FloatTy, CFP->getValue()); 279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && 280 // Only do this if the target has a native EXTLOAD instruction from 281 // f32. 282 TLI.getOperationAction(ISD::EXTLOAD, 283 MVT::f32) == TargetLowering::Legal) { 284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); 285 VT = MVT::f32; 286 Extend = true; 287 } 288 289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), 290 TLI.getPointerTy()); 291 if (Extend) { 292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx, 293 MVT::f32); 294 } else { 295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx); 296 } 297 } 298 break; 299 } 300 case ISD::TokenFactor: { 301 std::vector<SDOperand> Ops; 302 bool Changed = false; 303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 304 SDOperand Op = Node->getOperand(i); 305 // Fold single-use TokenFactor nodes into this token factor as we go. 306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { 307 Changed = true; 308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) 309 Ops.push_back(LegalizeOp(Op.getOperand(j))); 310 } else { 311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands 312 Changed |= Ops[i] != Op; 313 } 314 } 315 if (Changed) 316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); 317 break; 318 } 319 320 case ISD::ADJCALLSTACKDOWN: 321 case ISD::ADJCALLSTACKUP: 322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 323 // There is no need to legalize the size argument (Operand #1) 324 if (Tmp1 != Node->getOperand(0)) 325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, 326 Node->getOperand(1)); 327 break; 328 case ISD::DYNAMIC_STACKALLOC: 329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. 331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. 332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 333 Tmp3 != Node->getOperand(2)) 334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0), 335 Tmp1, Tmp2, Tmp3); 336 else 337 Result = Op.getValue(0); 338 339 // Since this op produces two values, make sure to remember that we 340 // legalized both of them. 341 AddLegalizedOperand(SDOperand(Node, 0), Result); 342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 343 return Result.getValue(Op.ResNo); 344 345 case ISD::CALL: { 346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 348 349 bool Changed = false; 350 std::vector<SDOperand> Ops; 351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { 352 Ops.push_back(LegalizeOp(Node->getOperand(i))); 353 Changed |= Ops.back() != Node->getOperand(i); 354 } 355 356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) { 357 std::vector<MVT::ValueType> RetTyVTs; 358 RetTyVTs.reserve(Node->getNumValues()); 359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 360 RetTyVTs.push_back(Node->getValueType(i)); 361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0); 362 } else { 363 Result = Result.getValue(0); 364 } 365 // Since calls produce multiple values, make sure to remember that we 366 // legalized all of them. 367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); 369 return Result.getValue(Op.ResNo); 370 } 371 case ISD::BR: 372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 373 if (Tmp1 != Node->getOperand(0)) 374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); 375 break; 376 377 case ISD::BRCOND: 378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 379 380 switch (getTypeAction(Node->getOperand(1).getValueType())) { 381 case Expand: assert(0 && "It's impossible to expand bools"); 382 case Legal: 383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 384 break; 385 case Promote: 386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 387 break; 388 } 389 // Basic block destination (Op#2) is always legal. 390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 392 Node->getOperand(2)); 393 break; 394 case ISD::BRCONDTWOWAY: 395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 396 switch (getTypeAction(Node->getOperand(1).getValueType())) { 397 case Expand: assert(0 && "It's impossible to expand bools"); 398 case Legal: 399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 400 break; 401 case Promote: 402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 403 break; 404 } 405 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR 406 // pair. 407 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) { 408 case TargetLowering::Promote: 409 default: assert(0 && "This action is not supported yet!"); 410 case TargetLowering::Legal: 411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 412 std::vector<SDOperand> Ops; 413 Ops.push_back(Tmp1); 414 Ops.push_back(Tmp2); 415 Ops.push_back(Node->getOperand(2)); 416 Ops.push_back(Node->getOperand(3)); 417 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops); 418 } 419 break; 420 case TargetLowering::Expand: 421 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 422 Node->getOperand(2)); 423 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3)); 424 break; 425 } 426 break; 427 428 case ISD::LOAD: 429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 431 if (Tmp1 != Node->getOperand(0) || 432 Tmp2 != Node->getOperand(1)) 433 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2); 434 else 435 Result = SDOperand(Node, 0); 436 437 // Since loads produce two values, make sure to remember that we legalized 438 // both of them. 439 AddLegalizedOperand(SDOperand(Node, 0), Result); 440 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 441 return Result.getValue(Op.ResNo); 442 443 case ISD::EXTLOAD: 444 case ISD::SEXTLOAD: 445 case ISD::ZEXTLOAD: { 446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 448 449 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType(); 450 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { 451 case TargetLowering::Promote: 452 default: assert(0 && "This action is not supported yet!"); 453 case TargetLowering::Legal: 454 if (Tmp1 != Node->getOperand(0) || 455 Tmp2 != Node->getOperand(1)) 456 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), 457 Tmp1, Tmp2, SrcVT); 458 else 459 Result = SDOperand(Node, 0); 460 461 // Since loads produce two values, make sure to remember that we legalized 462 // both of them. 463 AddLegalizedOperand(SDOperand(Node, 0), Result); 464 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 465 return Result.getValue(Op.ResNo); 466 break; 467 case TargetLowering::Expand: 468 assert(Node->getOpcode() != ISD::EXTLOAD && 469 "EXTLOAD should always be supported!"); 470 // Turn the unsupported load into an EXTLOAD followed by an explicit 471 // zero/sign extend inreg. 472 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), 473 Tmp1, Tmp2, SrcVT); 474 unsigned ExtOp = Node->getOpcode() == ISD::SEXTLOAD ? 475 ISD::SIGN_EXTEND_INREG : ISD::ZERO_EXTEND_INREG; 476 SDOperand ValRes = DAG.getNode(ExtOp, Result.getValueType(), 477 Result, SrcVT); 478 AddLegalizedOperand(SDOperand(Node, 0), ValRes); 479 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 480 if (Op.ResNo) 481 return Result.getValue(1); 482 return ValRes; 483 } 484 assert(0 && "Unreachable"); 485 } 486 case ISD::EXTRACT_ELEMENT: 487 // Get both the low and high parts. 488 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 489 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) 490 Result = Tmp2; // 1 -> Hi 491 else 492 Result = Tmp1; // 0 -> Lo 493 break; 494 495 case ISD::CopyToReg: 496 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 497 498 switch (getTypeAction(Node->getOperand(1).getValueType())) { 499 case Legal: 500 // Legalize the incoming value (must be legal). 501 Tmp2 = LegalizeOp(Node->getOperand(1)); 502 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 503 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); 504 break; 505 case Promote: 506 Tmp2 = PromoteOp(Node->getOperand(1)); 507 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); 508 break; 509 case Expand: 510 SDOperand Lo, Hi; 511 ExpandOp(Node->getOperand(1), Lo, Hi); 512 unsigned Reg = cast<RegSDNode>(Node)->getReg(); 513 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg); 514 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1); 515 // Note that the copytoreg nodes are independent of each other. 516 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 517 assert(isTypeLegal(Result.getValueType()) && 518 "Cannot expand multiple times yet (i64 -> i16)"); 519 break; 520 } 521 break; 522 523 case ISD::RET: 524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 525 switch (Node->getNumOperands()) { 526 case 2: // ret val 527 switch (getTypeAction(Node->getOperand(1).getValueType())) { 528 case Legal: 529 Tmp2 = LegalizeOp(Node->getOperand(1)); 530 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 531 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 532 break; 533 case Expand: { 534 SDOperand Lo, Hi; 535 ExpandOp(Node->getOperand(1), Lo, Hi); 536 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); 537 break; 538 } 539 case Promote: 540 Tmp2 = PromoteOp(Node->getOperand(1)); 541 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 542 break; 543 } 544 break; 545 case 1: // ret void 546 if (Tmp1 != Node->getOperand(0)) 547 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); 548 break; 549 default: { // ret <values> 550 std::vector<SDOperand> NewValues; 551 NewValues.push_back(Tmp1); 552 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) 553 switch (getTypeAction(Node->getOperand(i).getValueType())) { 554 case Legal: 555 NewValues.push_back(LegalizeOp(Node->getOperand(i))); 556 break; 557 case Expand: { 558 SDOperand Lo, Hi; 559 ExpandOp(Node->getOperand(i), Lo, Hi); 560 NewValues.push_back(Lo); 561 NewValues.push_back(Hi); 562 break; 563 } 564 case Promote: 565 assert(0 && "Can't promote multiple return value yet!"); 566 } 567 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); 568 break; 569 } 570 } 571 break; 572 case ISD::STORE: 573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 574 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 575 576 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 577 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ 578 if (CFP->getValueType(0) == MVT::f32) { 579 union { 580 unsigned I; 581 float F; 582 } V; 583 V.F = CFP->getValue(); 584 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 585 DAG.getConstant(V.I, MVT::i32), Tmp2); 586 } else { 587 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); 588 union { 589 uint64_t I; 590 double F; 591 } V; 592 V.F = CFP->getValue(); 593 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 594 DAG.getConstant(V.I, MVT::i64), Tmp2); 595 } 596 Node = Result.Val; 597 } 598 599 switch (getTypeAction(Node->getOperand(1).getValueType())) { 600 case Legal: { 601 SDOperand Val = LegalizeOp(Node->getOperand(1)); 602 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || 603 Tmp2 != Node->getOperand(2)) 604 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2); 605 break; 606 } 607 case Promote: 608 // Truncate the value and store the result. 609 Tmp3 = PromoteOp(Node->getOperand(1)); 610 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, 611 Node->getOperand(1).getValueType()); 612 break; 613 614 case Expand: 615 SDOperand Lo, Hi; 616 ExpandOp(Node->getOperand(1), Lo, Hi); 617 618 if (!TLI.isLittleEndian()) 619 std::swap(Lo, Hi); 620 621 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2); 622 623 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; 624 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, 625 getIntPtrConstant(IncrementSize)); 626 assert(isTypeLegal(Tmp2.getValueType()) && 627 "Pointers must be legal!"); 628 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2); 629 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 630 break; 631 } 632 break; 633 case ISD::PCMARKER: 634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 635 if (Tmp1 != Node->getOperand(0)) 636 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1)); 637 break; 638 case ISD::TRUNCSTORE: 639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 640 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 641 642 switch (getTypeAction(Node->getOperand(1).getValueType())) { 643 case Legal: 644 Tmp2 = LegalizeOp(Node->getOperand(1)); 645 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 646 Tmp3 != Node->getOperand(2)) 647 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, 648 cast<MVTSDNode>(Node)->getExtraValueType()); 649 break; 650 case Promote: 651 case Expand: 652 assert(0 && "Cannot handle illegal TRUNCSTORE yet!"); 653 } 654 break; 655 case ISD::SELECT: 656 switch (getTypeAction(Node->getOperand(0).getValueType())) { 657 case Expand: assert(0 && "It's impossible to expand bools"); 658 case Legal: 659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 660 break; 661 case Promote: 662 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 663 break; 664 } 665 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal 666 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal 667 668 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) { 669 default: assert(0 && "This action is not supported yet!"); 670 case TargetLowering::Legal: 671 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 672 Tmp3 != Node->getOperand(2)) 673 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), 674 Tmp1, Tmp2, Tmp3); 675 break; 676 case TargetLowering::Promote: { 677 MVT::ValueType NVT = 678 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); 679 unsigned ExtOp, TruncOp; 680 if (MVT::isInteger(Tmp2.getValueType())) { 681 ExtOp = ISD::ZERO_EXTEND; 682 TruncOp = ISD::TRUNCATE; 683 } else { 684 ExtOp = ISD::FP_EXTEND; 685 TruncOp = ISD::FP_ROUND; 686 } 687 // Promote each of the values to the new type. 688 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); 689 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); 690 // Perform the larger operation, then round down. 691 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); 692 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); 693 break; 694 } 695 } 696 break; 697 case ISD::SETCC: 698 switch (getTypeAction(Node->getOperand(0).getValueType())) { 699 case Legal: 700 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 701 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 702 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 703 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 704 Node->getValueType(0), Tmp1, Tmp2); 705 break; 706 case Promote: 707 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS 708 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS 709 710 // If this is an FP compare, the operands have already been extended. 711 if (MVT::isInteger(Node->getOperand(0).getValueType())) { 712 MVT::ValueType VT = Node->getOperand(0).getValueType(); 713 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 714 715 // Otherwise, we have to insert explicit sign or zero extends. Note 716 // that we could insert sign extends for ALL conditions, but zero extend 717 // is cheaper on many machines (an AND instead of two shifts), so prefer 718 // it. 719 switch (cast<SetCCSDNode>(Node)->getCondition()) { 720 default: assert(0 && "Unknown integer comparison!"); 721 case ISD::SETEQ: 722 case ISD::SETNE: 723 case ISD::SETUGE: 724 case ISD::SETUGT: 725 case ISD::SETULE: 726 case ISD::SETULT: 727 // ALL of these operations will work if we either sign or zero extend 728 // the operands (including the unsigned comparisons!). Zero extend is 729 // usually a simpler/cheaper operation, so prefer it. 730 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 731 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); 732 break; 733 case ISD::SETGE: 734 case ISD::SETGT: 735 case ISD::SETLT: 736 case ISD::SETLE: 737 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 738 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); 739 break; 740 } 741 742 } 743 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 744 Node->getValueType(0), Tmp1, Tmp2); 745 break; 746 case Expand: 747 SDOperand LHSLo, LHSHi, RHSLo, RHSHi; 748 ExpandOp(Node->getOperand(0), LHSLo, LHSHi); 749 ExpandOp(Node->getOperand(1), RHSLo, RHSHi); 750 switch (cast<SetCCSDNode>(Node)->getCondition()) { 751 case ISD::SETEQ: 752 case ISD::SETNE: 753 if (RHSLo == RHSHi) 754 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) 755 if (RHSCST->isAllOnesValue()) { 756 // Comparison to -1. 757 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); 758 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 759 Node->getValueType(0), Tmp1, RHSLo); 760 break; 761 } 762 763 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); 764 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); 765 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); 766 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 767 Node->getValueType(0), Tmp1, 768 DAG.getConstant(0, Tmp1.getValueType())); 769 break; 770 default: 771 // If this is a comparison of the sign bit, just look at the top part. 772 // X > -1, x < 0 773 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1))) 774 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT && 775 CST->getValue() == 0) || // X < 0 776 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT && 777 (CST->isAllOnesValue()))) // X > -1 778 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 779 Node->getValueType(0), LHSHi, RHSHi); 780 781 // FIXME: This generated code sucks. 782 ISD::CondCode LowCC; 783 switch (cast<SetCCSDNode>(Node)->getCondition()) { 784 default: assert(0 && "Unknown integer setcc!"); 785 case ISD::SETLT: 786 case ISD::SETULT: LowCC = ISD::SETULT; break; 787 case ISD::SETGT: 788 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 789 case ISD::SETLE: 790 case ISD::SETULE: LowCC = ISD::SETULE; break; 791 case ISD::SETGE: 792 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 793 } 794 795 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 796 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 797 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 798 799 // NOTE: on targets without efficient SELECT of bools, we can always use 800 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 801 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo); 802 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 803 Node->getValueType(0), LHSHi, RHSHi); 804 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi); 805 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(), 806 Result, Tmp1, Tmp2); 807 break; 808 } 809 } 810 break; 811 812 case ISD::MEMSET: 813 case ISD::MEMCPY: 814 case ISD::MEMMOVE: { 815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain 816 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer 817 818 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte 819 switch (getTypeAction(Node->getOperand(2).getValueType())) { 820 case Expand: assert(0 && "Cannot expand a byte!"); 821 case Legal: 822 Tmp3 = LegalizeOp(Node->getOperand(2)); 823 break; 824 case Promote: 825 Tmp3 = PromoteOp(Node->getOperand(2)); 826 break; 827 } 828 } else { 829 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, 830 } 831 832 SDOperand Tmp4; 833 switch (getTypeAction(Node->getOperand(3).getValueType())) { 834 case Expand: assert(0 && "Cannot expand this yet!"); 835 case Legal: 836 Tmp4 = LegalizeOp(Node->getOperand(3)); 837 break; 838 case Promote: 839 Tmp4 = PromoteOp(Node->getOperand(3)); 840 break; 841 } 842 843 SDOperand Tmp5; 844 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint 845 case Expand: assert(0 && "Cannot expand this yet!"); 846 case Legal: 847 Tmp5 = LegalizeOp(Node->getOperand(4)); 848 break; 849 case Promote: 850 Tmp5 = PromoteOp(Node->getOperand(4)); 851 break; 852 } 853 854 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 855 default: assert(0 && "This action not implemented for this operation!"); 856 case TargetLowering::Legal: 857 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 858 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || 859 Tmp5 != Node->getOperand(4)) { 860 std::vector<SDOperand> Ops; 861 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 862 Ops.push_back(Tmp4); Ops.push_back(Tmp5); 863 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); 864 } 865 break; 866 case TargetLowering::Expand: { 867 // Otherwise, the target does not support this operation. Lower the 868 // operation to an explicit libcall as appropriate. 869 MVT::ValueType IntPtr = TLI.getPointerTy(); 870 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); 871 std::vector<std::pair<SDOperand, const Type*> > Args; 872 873 const char *FnName = 0; 874 if (Node->getOpcode() == ISD::MEMSET) { 875 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 876 // Extend the ubyte argument to be an int value for the call. 877 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); 878 Args.push_back(std::make_pair(Tmp3, Type::IntTy)); 879 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 880 881 FnName = "memset"; 882 } else if (Node->getOpcode() == ISD::MEMCPY || 883 Node->getOpcode() == ISD::MEMMOVE) { 884 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 885 Args.push_back(std::make_pair(Tmp3, IntPtrTy)); 886 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 887 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; 888 } else { 889 assert(0 && "Unknown op!"); 890 } 891 std::pair<SDOperand,SDOperand> CallResult = 892 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, 893 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); 894 Result = LegalizeOp(CallResult.second); 895 break; 896 } 897 case TargetLowering::Custom: 898 std::vector<SDOperand> Ops; 899 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 900 Ops.push_back(Tmp4); Ops.push_back(Tmp5); 901 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); 902 Result = TLI.LowerOperation(Result); 903 Result = LegalizeOp(Result); 904 break; 905 } 906 break; 907 } 908 case ISD::ADD_PARTS: 909 case ISD::SUB_PARTS: 910 case ISD::SHL_PARTS: 911 case ISD::SRA_PARTS: 912 case ISD::SRL_PARTS: { 913 std::vector<SDOperand> Ops; 914 bool Changed = false; 915 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 916 Ops.push_back(LegalizeOp(Node->getOperand(i))); 917 Changed |= Ops.back() != Node->getOperand(i); 918 } 919 if (Changed) 920 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); 921 922 // Since these produce multiple values, make sure to remember that we 923 // legalized all of them. 924 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 925 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); 926 return Result.getValue(Op.ResNo); 927 } 928 929 // Binary operators 930 case ISD::ADD: 931 case ISD::SUB: 932 case ISD::MUL: 933 case ISD::MULHS: 934 case ISD::MULHU: 935 case ISD::UDIV: 936 case ISD::SDIV: 937 case ISD::AND: 938 case ISD::OR: 939 case ISD::XOR: 940 case ISD::SHL: 941 case ISD::SRL: 942 case ISD::SRA: 943 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 944 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 945 if (Tmp1 != Node->getOperand(0) || 946 Tmp2 != Node->getOperand(1)) 947 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); 948 break; 949 950 case ISD::UREM: 951 case ISD::SREM: 952 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 953 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 954 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 955 case TargetLowering::Legal: 956 if (Tmp1 != Node->getOperand(0) || 957 Tmp2 != Node->getOperand(1)) 958 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, 959 Tmp2); 960 break; 961 case TargetLowering::Promote: 962 case TargetLowering::Custom: 963 assert(0 && "Cannot promote/custom handle this yet!"); 964 case TargetLowering::Expand: { 965 MVT::ValueType VT = Node->getValueType(0); 966 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; 967 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); 968 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); 969 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); 970 } 971 break; 972 } 973 break; 974 975 // Unary operators 976 case ISD::FABS: 977 case ISD::FNEG: 978 Tmp1 = LegalizeOp(Node->getOperand(0)); 979 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 980 case TargetLowering::Legal: 981 if (Tmp1 != Node->getOperand(0)) 982 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 983 break; 984 case TargetLowering::Promote: 985 case TargetLowering::Custom: 986 assert(0 && "Cannot promote/custom handle this yet!"); 987 case TargetLowering::Expand: 988 if (Node->getOpcode() == ISD::FNEG) { 989 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 990 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); 991 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0), 992 Tmp2, Tmp1)); 993 } else if (Node->getOpcode() == ISD::FABS) { 994 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). 995 MVT::ValueType VT = Node->getValueType(0); 996 Tmp2 = DAG.getConstantFP(0.0, VT); 997 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2); 998 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); 999 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); 1000 Result = LegalizeOp(Result); 1001 } else { 1002 assert(0 && "Unreachable!"); 1003 } 1004 break; 1005 } 1006 break; 1007 1008 // Conversion operators. The source and destination have different types. 1009 case ISD::ZERO_EXTEND: 1010 case ISD::SIGN_EXTEND: 1011 case ISD::TRUNCATE: 1012 case ISD::FP_EXTEND: 1013 case ISD::FP_ROUND: 1014 case ISD::FP_TO_SINT: 1015 case ISD::FP_TO_UINT: 1016 case ISD::SINT_TO_FP: 1017 case ISD::UINT_TO_FP: 1018 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1019 case Legal: 1020 Tmp1 = LegalizeOp(Node->getOperand(0)); 1021 if (Tmp1 != Node->getOperand(0)) 1022 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 1023 break; 1024 case Expand: 1025 if (Node->getOpcode() == ISD::SINT_TO_FP || 1026 Node->getOpcode() == ISD::UINT_TO_FP) { 1027 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, 1028 Node->getValueType(0), Node->getOperand(0)); 1029 Result = LegalizeOp(Result); 1030 break; 1031 } else if (Node->getOpcode() == ISD::TRUNCATE) { 1032 // In the expand case, we must be dealing with a truncate, because 1033 // otherwise the result would be larger than the source. 1034 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 1035 1036 // Since the result is legal, we should just be able to truncate the low 1037 // part of the source. 1038 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); 1039 break; 1040 } 1041 assert(0 && "Shouldn't need to expand other operators here!"); 1042 1043 case Promote: 1044 switch (Node->getOpcode()) { 1045 case ISD::ZERO_EXTEND: 1046 Result = PromoteOp(Node->getOperand(0)); 1047 // NOTE: Any extend would work here... 1048 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); 1049 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(), 1050 Result, Node->getOperand(0).getValueType()); 1051 break; 1052 case ISD::SIGN_EXTEND: 1053 Result = PromoteOp(Node->getOperand(0)); 1054 // NOTE: Any extend would work here... 1055 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); 1056 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1057 Result, Node->getOperand(0).getValueType()); 1058 break; 1059 case ISD::TRUNCATE: 1060 Result = PromoteOp(Node->getOperand(0)); 1061 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); 1062 break; 1063 case ISD::FP_EXTEND: 1064 Result = PromoteOp(Node->getOperand(0)); 1065 if (Result.getValueType() != Op.getValueType()) 1066 // Dynamically dead while we have only 2 FP types. 1067 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); 1068 break; 1069 case ISD::FP_ROUND: 1070 case ISD::FP_TO_SINT: 1071 case ISD::FP_TO_UINT: 1072 Result = PromoteOp(Node->getOperand(0)); 1073 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); 1074 break; 1075 case ISD::SINT_TO_FP: 1076 Result = PromoteOp(Node->getOperand(0)); 1077 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1078 Result, Node->getOperand(0).getValueType()); 1079 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); 1080 break; 1081 case ISD::UINT_TO_FP: 1082 Result = PromoteOp(Node->getOperand(0)); 1083 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), 1084 Result, Node->getOperand(0).getValueType()); 1085 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); 1086 break; 1087 } 1088 } 1089 break; 1090 case ISD::FP_ROUND_INREG: 1091 case ISD::SIGN_EXTEND_INREG: 1092 case ISD::ZERO_EXTEND_INREG: { 1093 Tmp1 = LegalizeOp(Node->getOperand(0)); 1094 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType(); 1095 1096 // If this operation is not supported, convert it to a shl/shr or load/store 1097 // pair. 1098 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { 1099 default: assert(0 && "This action not supported for this op yet!"); 1100 case TargetLowering::Legal: 1101 if (Tmp1 != Node->getOperand(0)) 1102 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, 1103 ExtraVT); 1104 break; 1105 case TargetLowering::Expand: 1106 // If this is an integer extend and shifts are supported, do that. 1107 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) { 1108 // NOTE: we could fall back on load/store here too for targets without 1109 // AND. However, it is doubtful that any exist. 1110 // AND out the appropriate bits. 1111 SDOperand Mask = 1112 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1, 1113 Node->getValueType(0)); 1114 Result = DAG.getNode(ISD::AND, Node->getValueType(0), 1115 Node->getOperand(0), Mask); 1116 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { 1117 // NOTE: we could fall back on load/store here too for targets without 1118 // SAR. However, it is doubtful that any exist. 1119 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - 1120 MVT::getSizeInBits(ExtraVT); 1121 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); 1122 Result = DAG.getNode(ISD::SHL, Node->getValueType(0), 1123 Node->getOperand(0), ShiftCst); 1124 Result = DAG.getNode(ISD::SRA, Node->getValueType(0), 1125 Result, ShiftCst); 1126 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { 1127 // The only way we can lower this is to turn it into a STORETRUNC, 1128 // EXTLOAD pair, targetting a temporary location (a stack slot). 1129 1130 // NOTE: there is a choice here between constantly creating new stack 1131 // slots and always reusing the same one. We currently always create 1132 // new ones, as reuse may inhibit scheduling. 1133 const Type *Ty = MVT::getTypeForValueType(ExtraVT); 1134 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); 1135 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); 1136 MachineFunction &MF = DAG.getMachineFunction(); 1137 int SSFI = 1138 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); 1139 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 1140 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), 1141 Node->getOperand(0), StackSlot, ExtraVT); 1142 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), 1143 Result, StackSlot, ExtraVT); 1144 } else { 1145 assert(0 && "Unknown op"); 1146 } 1147 Result = LegalizeOp(Result); 1148 break; 1149 } 1150 break; 1151 } 1152 } 1153 1154 if (!Op.Val->hasOneUse()) 1155 AddLegalizedOperand(Op, Result); 1156 1157 return Result; 1158} 1159 1160/// PromoteOp - Given an operation that produces a value in an invalid type, 1161/// promote it to compute the value into a larger type. The produced value will 1162/// have the correct bits for the low portion of the register, but no guarantee 1163/// is made about the top bits: it may be zero, sign-extended, or garbage. 1164SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { 1165 MVT::ValueType VT = Op.getValueType(); 1166 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 1167 assert(getTypeAction(VT) == Promote && 1168 "Caller should expand or legalize operands that are not promotable!"); 1169 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && 1170 "Cannot promote to smaller type!"); 1171 1172 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); 1173 if (I != PromotedNodes.end()) return I->second; 1174 1175 SDOperand Tmp1, Tmp2, Tmp3; 1176 1177 SDOperand Result; 1178 SDNode *Node = Op.Val; 1179 1180 // Promotion needs an optimization step to clean up after it, and is not 1181 // careful to avoid operations the target does not support. Make sure that 1182 // all generated operations are legalized in the next iteration. 1183 NeedsAnotherIteration = true; 1184 1185 switch (Node->getOpcode()) { 1186 default: 1187 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 1188 assert(0 && "Do not know how to promote this operator!"); 1189 abort(); 1190 case ISD::UNDEF: 1191 Result = DAG.getNode(ISD::UNDEF, NVT); 1192 break; 1193 case ISD::Constant: 1194 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); 1195 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); 1196 break; 1197 case ISD::ConstantFP: 1198 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); 1199 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); 1200 break; 1201 case ISD::CopyFromReg: 1202 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT, 1203 Node->getOperand(0)); 1204 // Remember that we legalized the chain. 1205 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1206 break; 1207 1208 case ISD::SETCC: 1209 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal && 1210 "SetCC type is not legal??"); 1211 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 1212 TLI.getSetCCResultTy(), Node->getOperand(0), 1213 Node->getOperand(1)); 1214 Result = LegalizeOp(Result); 1215 break; 1216 1217 case ISD::TRUNCATE: 1218 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1219 case Legal: 1220 Result = LegalizeOp(Node->getOperand(0)); 1221 assert(Result.getValueType() >= NVT && 1222 "This truncation doesn't make sense!"); 1223 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT 1224 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); 1225 break; 1226 case Promote: 1227 // The truncation is not required, because we don't guarantee anything 1228 // about high bits anyway. 1229 Result = PromoteOp(Node->getOperand(0)); 1230 break; 1231 case Expand: 1232 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 1233 // Truncate the low part of the expanded value to the result type 1234 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1); 1235 } 1236 break; 1237 case ISD::SIGN_EXTEND: 1238 case ISD::ZERO_EXTEND: 1239 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1240 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); 1241 case Legal: 1242 // Input is legal? Just do extend all the way to the larger type. 1243 Result = LegalizeOp(Node->getOperand(0)); 1244 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 1245 break; 1246 case Promote: 1247 // Promote the reg if it's smaller. 1248 Result = PromoteOp(Node->getOperand(0)); 1249 // The high bits are not guaranteed to be anything. Insert an extend. 1250 if (Node->getOpcode() == ISD::SIGN_EXTEND) 1251 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, 1252 Node->getOperand(0).getValueType()); 1253 else 1254 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, 1255 Node->getOperand(0).getValueType()); 1256 break; 1257 } 1258 break; 1259 1260 case ISD::FP_EXTEND: 1261 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); 1262 case ISD::FP_ROUND: 1263 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1264 case Expand: assert(0 && "BUG: Cannot expand FP regs!"); 1265 case Promote: assert(0 && "Unreachable with 2 FP types!"); 1266 case Legal: 1267 // Input is legal? Do an FP_ROUND_INREG. 1268 Result = LegalizeOp(Node->getOperand(0)); 1269 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1270 break; 1271 } 1272 break; 1273 1274 case ISD::SINT_TO_FP: 1275 case ISD::UINT_TO_FP: 1276 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1277 case Legal: 1278 Result = LegalizeOp(Node->getOperand(0)); 1279 // No extra round required here. 1280 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 1281 break; 1282 1283 case Promote: 1284 Result = PromoteOp(Node->getOperand(0)); 1285 if (Node->getOpcode() == ISD::SINT_TO_FP) 1286 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1287 Result, Node->getOperand(0).getValueType()); 1288 else 1289 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), 1290 Result, Node->getOperand(0).getValueType()); 1291 // No extra round required here. 1292 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 1293 break; 1294 case Expand: 1295 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, 1296 Node->getOperand(0)); 1297 Result = LegalizeOp(Result); 1298 1299 // Round if we cannot tolerate excess precision. 1300 if (NoExcessFPPrecision) 1301 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1302 break; 1303 } 1304 break; 1305 1306 case ISD::FP_TO_SINT: 1307 case ISD::FP_TO_UINT: 1308 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1309 case Legal: 1310 Tmp1 = LegalizeOp(Node->getOperand(0)); 1311 break; 1312 case Promote: 1313 // The input result is prerounded, so we don't have to do anything 1314 // special. 1315 Tmp1 = PromoteOp(Node->getOperand(0)); 1316 break; 1317 case Expand: 1318 assert(0 && "not implemented"); 1319 } 1320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 1321 break; 1322 1323 case ISD::FABS: 1324 case ISD::FNEG: 1325 Tmp1 = PromoteOp(Node->getOperand(0)); 1326 assert(Tmp1.getValueType() == NVT); 1327 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 1328 // NOTE: we do not have to do any extra rounding here for 1329 // NoExcessFPPrecision, because we know the input will have the appropriate 1330 // precision, and these operations don't modify precision at all. 1331 break; 1332 1333 case ISD::AND: 1334 case ISD::OR: 1335 case ISD::XOR: 1336 case ISD::ADD: 1337 case ISD::SUB: 1338 case ISD::MUL: 1339 // The input may have strange things in the top bits of the registers, but 1340 // these operations don't care. They may have wierd bits going out, but 1341 // that too is okay if they are integer operations. 1342 Tmp1 = PromoteOp(Node->getOperand(0)); 1343 Tmp2 = PromoteOp(Node->getOperand(1)); 1344 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 1345 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1346 1347 // However, if this is a floating point operation, they will give excess 1348 // precision that we may not be able to tolerate. If we DO allow excess 1349 // precision, just leave it, otherwise excise it. 1350 // FIXME: Why would we need to round FP ops more than integer ones? 1351 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) 1352 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 1353 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1354 break; 1355 1356 case ISD::SDIV: 1357 case ISD::SREM: 1358 // These operators require that their input be sign extended. 1359 Tmp1 = PromoteOp(Node->getOperand(0)); 1360 Tmp2 = PromoteOp(Node->getOperand(1)); 1361 if (MVT::isInteger(NVT)) { 1362 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 1363 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); 1364 } 1365 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1366 1367 // Perform FP_ROUND: this is probably overly pessimistic. 1368 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 1369 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1370 break; 1371 1372 case ISD::UDIV: 1373 case ISD::UREM: 1374 // These operators require that their input be zero extended. 1375 Tmp1 = PromoteOp(Node->getOperand(0)); 1376 Tmp2 = PromoteOp(Node->getOperand(1)); 1377 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); 1378 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 1379 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); 1380 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1381 break; 1382 1383 case ISD::SHL: 1384 Tmp1 = PromoteOp(Node->getOperand(0)); 1385 Tmp2 = LegalizeOp(Node->getOperand(1)); 1386 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2); 1387 break; 1388 case ISD::SRA: 1389 // The input value must be properly sign extended. 1390 Tmp1 = PromoteOp(Node->getOperand(0)); 1391 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 1392 Tmp2 = LegalizeOp(Node->getOperand(1)); 1393 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2); 1394 break; 1395 case ISD::SRL: 1396 // The input value must be properly zero extended. 1397 Tmp1 = PromoteOp(Node->getOperand(0)); 1398 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 1399 Tmp2 = LegalizeOp(Node->getOperand(1)); 1400 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2); 1401 break; 1402 case ISD::LOAD: 1403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1405 // FIXME: When the DAG combiner exists, change this to use EXTLOAD! 1406 if (MVT::isInteger(NVT)) 1407 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, VT); 1408 else 1409 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT); 1410 1411 // Remember that we legalized the chain. 1412 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1413 break; 1414 case ISD::SELECT: 1415 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1416 case Expand: assert(0 && "It's impossible to expand bools"); 1417 case Legal: 1418 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition. 1419 break; 1420 case Promote: 1421 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 1422 break; 1423 } 1424 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 1425 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 1426 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3); 1427 break; 1428 case ISD::CALL: { 1429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 1431 1432 std::vector<SDOperand> Ops; 1433 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) 1434 Ops.push_back(LegalizeOp(Node->getOperand(i))); 1435 1436 assert(Node->getNumValues() == 2 && Op.ResNo == 0 && 1437 "Can only promote single result calls"); 1438 std::vector<MVT::ValueType> RetTyVTs; 1439 RetTyVTs.reserve(2); 1440 RetTyVTs.push_back(NVT); 1441 RetTyVTs.push_back(MVT::Other); 1442 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops); 1443 Result = SDOperand(NC, 0); 1444 1445 // Insert the new chain mapping. 1446 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1447 break; 1448 } 1449 } 1450 1451 assert(Result.Val && "Didn't set a result!"); 1452 AddPromotedOperand(Op, Result); 1453 return Result; 1454} 1455 1456/// ExpandAddSub - Find a clever way to expand this add operation into 1457/// subcomponents. 1458void SelectionDAGLegalize:: 1459ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, 1460 SDOperand &Lo, SDOperand &Hi) { 1461 // Expand the subcomponents. 1462 SDOperand LHSL, LHSH, RHSL, RHSH; 1463 ExpandOp(LHS, LHSL, LHSH); 1464 ExpandOp(RHS, RHSL, RHSH); 1465 1466 // FIXME: this should be moved to the dag combiner someday. 1467 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS) 1468 if (LHSL.getValueType() == MVT::i32) { 1469 SDOperand LowEl; 1470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL)) 1471 if (C->getValue() == 0) 1472 LowEl = RHSL; 1473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL)) 1474 if (C->getValue() == 0) 1475 LowEl = LHSL; 1476 if (LowEl.Val) { 1477 // Turn this into an add/sub of the high part only. 1478 SDOperand HiEl = 1479 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB, 1480 LowEl.getValueType(), LHSH, RHSH); 1481 Lo = LowEl; 1482 Hi = HiEl; 1483 return; 1484 } 1485 } 1486 1487 std::vector<SDOperand> Ops; 1488 Ops.push_back(LHSL); 1489 Ops.push_back(LHSH); 1490 Ops.push_back(RHSL); 1491 Ops.push_back(RHSH); 1492 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); 1493 Hi = Lo.getValue(1); 1494} 1495 1496void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, 1497 SDOperand Op, SDOperand Amt, 1498 SDOperand &Lo, SDOperand &Hi) { 1499 // Expand the subcomponents. 1500 SDOperand LHSL, LHSH; 1501 ExpandOp(Op, LHSL, LHSH); 1502 1503 std::vector<SDOperand> Ops; 1504 Ops.push_back(LHSL); 1505 Ops.push_back(LHSH); 1506 Ops.push_back(Amt); 1507 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); 1508 Hi = Lo.getValue(1); 1509} 1510 1511 1512/// ExpandShift - Try to find a clever way to expand this shift operation out to 1513/// smaller elements. If we can't find a way that is more efficient than a 1514/// libcall on this target, return false. Otherwise, return true with the 1515/// low-parts expanded into Lo and Hi. 1516bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, 1517 SDOperand &Lo, SDOperand &Hi) { 1518 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && 1519 "This is not a shift!"); 1520 1521 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); 1522 SDOperand ShAmt = LegalizeOp(Amt); 1523 MVT::ValueType ShTy = ShAmt.getValueType(); 1524 unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); 1525 unsigned NVTBits = MVT::getSizeInBits(NVT); 1526 1527 // Handle the case when Amt is an immediate. Other cases are currently broken 1528 // and are disabled. 1529 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { 1530 unsigned Cst = CN->getValue(); 1531 // Expand the incoming operand to be shifted, so that we have its parts 1532 SDOperand InL, InH; 1533 ExpandOp(Op, InL, InH); 1534 switch(Opc) { 1535 case ISD::SHL: 1536 if (Cst > VTBits) { 1537 Lo = DAG.getConstant(0, NVT); 1538 Hi = DAG.getConstant(0, NVT); 1539 } else if (Cst > NVTBits) { 1540 Lo = DAG.getConstant(0, NVT); 1541 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); 1542 } else if (Cst == NVTBits) { 1543 Lo = DAG.getConstant(0, NVT); 1544 Hi = InL; 1545 } else { 1546 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); 1547 Hi = DAG.getNode(ISD::OR, NVT, 1548 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), 1549 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); 1550 } 1551 return true; 1552 case ISD::SRL: 1553 if (Cst > VTBits) { 1554 Lo = DAG.getConstant(0, NVT); 1555 Hi = DAG.getConstant(0, NVT); 1556 } else if (Cst > NVTBits) { 1557 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); 1558 Hi = DAG.getConstant(0, NVT); 1559 } else if (Cst == NVTBits) { 1560 Lo = InH; 1561 Hi = DAG.getConstant(0, NVT); 1562 } else { 1563 Lo = DAG.getNode(ISD::OR, NVT, 1564 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 1565 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 1566 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); 1567 } 1568 return true; 1569 case ISD::SRA: 1570 if (Cst > VTBits) { 1571 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, 1572 DAG.getConstant(NVTBits-1, ShTy)); 1573 } else if (Cst > NVTBits) { 1574 Lo = DAG.getNode(ISD::SRA, NVT, InH, 1575 DAG.getConstant(Cst-NVTBits, ShTy)); 1576 Hi = DAG.getNode(ISD::SRA, NVT, InH, 1577 DAG.getConstant(NVTBits-1, ShTy)); 1578 } else if (Cst == NVTBits) { 1579 Lo = InH; 1580 Hi = DAG.getNode(ISD::SRA, NVT, InH, 1581 DAG.getConstant(NVTBits-1, ShTy)); 1582 } else { 1583 Lo = DAG.getNode(ISD::OR, NVT, 1584 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 1585 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 1586 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); 1587 } 1588 return true; 1589 } 1590 } 1591 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy, 1592 // so disable it for now. Currently targets are handling this via SHL_PARTS 1593 // and friends. 1594 return false; 1595 1596 // If we have an efficient select operation (or if the selects will all fold 1597 // away), lower to some complex code, otherwise just emit the libcall. 1598 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal && 1599 !isa<ConstantSDNode>(Amt)) 1600 return false; 1601 1602 SDOperand InL, InH; 1603 ExpandOp(Op, InL, InH); 1604 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt 1605 DAG.getConstant(NVTBits, ShTy), ShAmt); 1606 1607 // Compare the unmasked shift amount against 32. 1608 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt, 1609 DAG.getConstant(NVTBits, ShTy)); 1610 1611 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) { 1612 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31 1613 DAG.getConstant(NVTBits-1, ShTy)); 1614 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31 1615 DAG.getConstant(NVTBits-1, ShTy)); 1616 } 1617 1618 if (Opc == ISD::SHL) { 1619 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt) 1620 DAG.getNode(ISD::SHL, NVT, InH, ShAmt), 1621 DAG.getNode(ISD::SRL, NVT, InL, NAmt)); 1622 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31 1623 1624 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); 1625 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2); 1626 } else { 1627 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT, 1628 DAG.getSetCC(ISD::SETEQ, 1629 TLI.getSetCCResultTy(), NAmt, 1630 DAG.getConstant(32, ShTy)), 1631 DAG.getConstant(0, NVT), 1632 DAG.getNode(ISD::SHL, NVT, InH, NAmt)); 1633 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt) 1634 HiLoPart, 1635 DAG.getNode(ISD::SRL, NVT, InL, ShAmt)); 1636 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31 1637 1638 SDOperand HiPart; 1639 if (Opc == ISD::SRA) 1640 HiPart = DAG.getNode(ISD::SRA, NVT, InH, 1641 DAG.getConstant(NVTBits-1, ShTy)); 1642 else 1643 HiPart = DAG.getConstant(0, NVT); 1644 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); 1645 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2); 1646 } 1647 return true; 1648} 1649 1650/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest 1651/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than 1652/// Found. 1653static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) { 1654 if (Node->getNodeDepth() <= Found->getNodeDepth()) return; 1655 1656 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later 1657 // than the Found node. Just remember this node and return. 1658 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) { 1659 Found = Node; 1660 return; 1661 } 1662 1663 // Otherwise, scan the operands of Node to see if any of them is a call. 1664 assert(Node->getNumOperands() != 0 && 1665 "All leaves should have depth equal to the entry node!"); 1666 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i) 1667 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found); 1668 1669 // Tail recurse for the last iteration. 1670 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val, 1671 Found); 1672} 1673 1674 1675/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest 1676/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent 1677/// than Found. 1678static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) { 1679 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return; 1680 1681 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier 1682 // than the Found node. Just remember this node and return. 1683 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) { 1684 Found = Node; 1685 return; 1686 } 1687 1688 // Otherwise, scan the operands of Node to see if any of them is a call. 1689 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); 1690 if (UI == E) return; 1691 for (--E; UI != E; ++UI) 1692 FindEarliestAdjCallStackUp(*UI, Found); 1693 1694 // Tail recurse for the last iteration. 1695 FindEarliestAdjCallStackUp(*UI, Found); 1696} 1697 1698/// FindAdjCallStackUp - Given a chained node that is part of a call sequence, 1699/// find the ADJCALLSTACKUP node that terminates the call sequence. 1700static SDNode *FindAdjCallStackUp(SDNode *Node) { 1701 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) 1702 return Node; 1703 if (Node->use_empty()) 1704 return 0; // No adjcallstackup 1705 1706 if (Node->hasOneUse()) // Simple case, only has one user to check. 1707 return FindAdjCallStackUp(*Node->use_begin()); 1708 1709 SDOperand TheChain(Node, Node->getNumValues()-1); 1710 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!"); 1711 1712 for (SDNode::use_iterator UI = Node->use_begin(), 1713 E = Node->use_end(); ; ++UI) { 1714 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!"); 1715 1716 // Make sure to only follow users of our token chain. 1717 SDNode *User = *UI; 1718 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 1719 if (User->getOperand(i) == TheChain) 1720 return FindAdjCallStackUp(User); 1721 } 1722 assert(0 && "Unreachable"); 1723 abort(); 1724} 1725 1726/// FindInputOutputChains - If we are replacing an operation with a call we need 1727/// to find the call that occurs before and the call that occurs after it to 1728/// properly serialize the calls in the block. 1729static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain, 1730 SDOperand Entry) { 1731 SDNode *LatestAdjCallStackDown = Entry.Val; 1732 SDNode *LatestAdjCallStackUp = 0; 1733 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown); 1734 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n"; 1735 1736 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no 1737 // previous call in the function. LatestCallStackDown may in that case be 1738 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP 1739 // unless LatestCallStackDown is an ADJCALLSTACKDOWN. 1740 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN) 1741 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown); 1742 else 1743 LatestAdjCallStackUp = Entry.Val; 1744 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp"); 1745 1746 SDNode *EarliestAdjCallStackUp = 0; 1747 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp); 1748 1749 if (EarliestAdjCallStackUp) { 1750 //std::cerr << "Found node: "; 1751 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n"; 1752 } 1753 1754 return SDOperand(LatestAdjCallStackUp, 0); 1755} 1756 1757 1758 1759// ExpandLibCall - Expand a node into a call to a libcall. If the result value 1760// does not fit into a register, return the lo part and set the hi part to the 1761// by-reg argument. If it does fit into a single register, return the result 1762// and leave the Hi part unset. 1763SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, 1764 SDOperand &Hi) { 1765 SDNode *OutChain; 1766 SDOperand InChain = FindInputOutputChains(Node, OutChain, 1767 DAG.getEntryNode()); 1768 if (InChain.Val == 0) 1769 InChain = DAG.getEntryNode(); 1770 1771 TargetLowering::ArgListTy Args; 1772 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1773 MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); 1774 const Type *ArgTy = MVT::getTypeForValueType(ArgVT); 1775 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); 1776 } 1777 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); 1778 1779 // We don't care about token chains for libcalls. We just use the entry 1780 // node as our input and ignore the output chain. This allows us to place 1781 // calls wherever we need them to satisfy data dependences. 1782 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); 1783 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee, 1784 Args, DAG).first; 1785 switch (getTypeAction(Result.getValueType())) { 1786 default: assert(0 && "Unknown thing"); 1787 case Legal: 1788 return Result; 1789 case Promote: 1790 assert(0 && "Cannot promote this yet!"); 1791 case Expand: 1792 SDOperand Lo; 1793 ExpandOp(Result, Lo, Hi); 1794 return Lo; 1795 } 1796} 1797 1798 1799/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the 1800/// destination type is legal. 1801SDOperand SelectionDAGLegalize:: 1802ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { 1803 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!"); 1804 assert(getTypeAction(Source.getValueType()) == Expand && 1805 "This is not an expansion!"); 1806 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); 1807 1808 SDNode *OutChain; 1809 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain, 1810 DAG.getEntryNode()); 1811 1812 const char *FnName = 0; 1813 if (isSigned) { 1814 if (DestTy == MVT::f32) 1815 FnName = "__floatdisf"; 1816 else { 1817 assert(DestTy == MVT::f64 && "Unknown fp value type!"); 1818 FnName = "__floatdidf"; 1819 } 1820 } else { 1821 // If this is unsigned, and not supported, first perform the conversion to 1822 // signed, then adjust the result if the sign bit is set. 1823 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source); 1824 1825 assert(0 && "Unsigned casts not supported yet!"); 1826 } 1827 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy()); 1828 1829 TargetLowering::ArgListTy Args; 1830 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType()); 1831 Args.push_back(std::make_pair(Source, ArgTy)); 1832 1833 // We don't care about token chains for libcalls. We just use the entry 1834 // node as our input and ignore the output chain. This allows us to place 1835 // calls wherever we need them to satisfy data dependences. 1836 const Type *RetTy = MVT::getTypeForValueType(DestTy); 1837 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first; 1838 1839} 1840 1841 1842 1843/// ExpandOp - Expand the specified SDOperand into its two component pieces 1844/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the 1845/// LegalizeNodes map is filled in for any results that are not expanded, the 1846/// ExpandedNodes map is filled in for any results that are expanded, and the 1847/// Lo/Hi values are returned. 1848void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ 1849 MVT::ValueType VT = Op.getValueType(); 1850 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 1851 SDNode *Node = Op.Val; 1852 assert(getTypeAction(VT) == Expand && "Not an expanded type!"); 1853 assert(MVT::isInteger(VT) && "Cannot expand FP values!"); 1854 assert(MVT::isInteger(NVT) && NVT < VT && 1855 "Cannot expand to FP value or to larger int value!"); 1856 1857 // If there is more than one use of this, see if we already expanded it. 1858 // There is no use remembering values that only have a single use, as the map 1859 // entries will never be reused. 1860 if (!Node->hasOneUse()) { 1861 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 1862 = ExpandedNodes.find(Op); 1863 if (I != ExpandedNodes.end()) { 1864 Lo = I->second.first; 1865 Hi = I->second.second; 1866 return; 1867 } 1868 } 1869 1870 // Expanding to multiple registers needs to perform an optimization step, and 1871 // is not careful to avoid operations the target does not support. Make sure 1872 // that all generated operations are legalized in the next iteration. 1873 NeedsAnotherIteration = true; 1874 1875 switch (Node->getOpcode()) { 1876 default: 1877 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 1878 assert(0 && "Do not know how to expand this operator!"); 1879 abort(); 1880 case ISD::UNDEF: 1881 Lo = DAG.getNode(ISD::UNDEF, NVT); 1882 Hi = DAG.getNode(ISD::UNDEF, NVT); 1883 break; 1884 case ISD::Constant: { 1885 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); 1886 Lo = DAG.getConstant(Cst, NVT); 1887 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); 1888 break; 1889 } 1890 1891 case ISD::CopyFromReg: { 1892 unsigned Reg = cast<RegSDNode>(Node)->getReg(); 1893 // Aggregate register values are always in consequtive pairs. 1894 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0)); 1895 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1)); 1896 1897 // Remember that we legalized the chain. 1898 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); 1899 1900 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); 1901 break; 1902 } 1903 1904 case ISD::BUILD_PAIR: 1905 // Legalize both operands. FIXME: in the future we should handle the case 1906 // where the two elements are not legal. 1907 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); 1908 Lo = LegalizeOp(Node->getOperand(0)); 1909 Hi = LegalizeOp(Node->getOperand(1)); 1910 break; 1911 1912 case ISD::LOAD: { 1913 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1914 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1915 Lo = DAG.getLoad(NVT, Ch, Ptr); 1916 1917 // Increment the pointer to the other half. 1918 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; 1919 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 1920 getIntPtrConstant(IncrementSize)); 1921 Hi = DAG.getLoad(NVT, Ch, Ptr); 1922 1923 // Build a factor node to remember that this load is independent of the 1924 // other one. 1925 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 1926 Hi.getValue(1)); 1927 1928 // Remember that we legalized the chain. 1929 AddLegalizedOperand(Op.getValue(1), TF); 1930 if (!TLI.isLittleEndian()) 1931 std::swap(Lo, Hi); 1932 break; 1933 } 1934 case ISD::CALL: { 1935 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1936 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 1937 1938 bool Changed = false; 1939 std::vector<SDOperand> Ops; 1940 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { 1941 Ops.push_back(LegalizeOp(Node->getOperand(i))); 1942 Changed |= Ops.back() != Node->getOperand(i); 1943 } 1944 1945 assert(Node->getNumValues() == 2 && Op.ResNo == 0 && 1946 "Can only expand a call once so far, not i64 -> i16!"); 1947 1948 std::vector<MVT::ValueType> RetTyVTs; 1949 RetTyVTs.reserve(3); 1950 RetTyVTs.push_back(NVT); 1951 RetTyVTs.push_back(NVT); 1952 RetTyVTs.push_back(MVT::Other); 1953 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops); 1954 Lo = SDOperand(NC, 0); 1955 Hi = SDOperand(NC, 1); 1956 1957 // Insert the new chain mapping. 1958 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); 1959 break; 1960 } 1961 case ISD::AND: 1962 case ISD::OR: 1963 case ISD::XOR: { // Simple logical operators -> two trivial pieces. 1964 SDOperand LL, LH, RL, RH; 1965 ExpandOp(Node->getOperand(0), LL, LH); 1966 ExpandOp(Node->getOperand(1), RL, RH); 1967 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); 1968 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); 1969 break; 1970 } 1971 case ISD::SELECT: { 1972 SDOperand C, LL, LH, RL, RH; 1973 1974 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1975 case Expand: assert(0 && "It's impossible to expand bools"); 1976 case Legal: 1977 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 1978 break; 1979 case Promote: 1980 C = PromoteOp(Node->getOperand(0)); // Promote the condition. 1981 break; 1982 } 1983 ExpandOp(Node->getOperand(1), LL, LH); 1984 ExpandOp(Node->getOperand(2), RL, RH); 1985 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); 1986 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); 1987 break; 1988 } 1989 case ISD::SIGN_EXTEND: { 1990 SDOperand In; 1991 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1992 case Expand: assert(0 && "expand-expand not implemented yet!"); 1993 case Legal: In = LegalizeOp(Node->getOperand(0)); break; 1994 case Promote: 1995 In = PromoteOp(Node->getOperand(0)); 1996 // Emit the appropriate sign_extend_inreg to get the value we want. 1997 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In, 1998 Node->getOperand(0).getValueType()); 1999 break; 2000 } 2001 2002 // The low part is just a sign extension of the input (which degenerates to 2003 // a copy). 2004 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In); 2005 2006 // The high part is obtained by SRA'ing all but one of the bits of the lo 2007 // part. 2008 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 2009 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, 2010 TLI.getShiftAmountTy())); 2011 break; 2012 } 2013 case ISD::ZERO_EXTEND: { 2014 SDOperand In; 2015 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2016 case Expand: assert(0 && "expand-expand not implemented yet!"); 2017 case Legal: In = LegalizeOp(Node->getOperand(0)); break; 2018 case Promote: 2019 In = PromoteOp(Node->getOperand(0)); 2020 // Emit the appropriate zero_extend_inreg to get the value we want. 2021 In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In, 2022 Node->getOperand(0).getValueType()); 2023 break; 2024 } 2025 2026 // The low part is just a zero extension of the input (which degenerates to 2027 // a copy). 2028 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In); 2029 2030 // The high part is just a zero. 2031 Hi = DAG.getConstant(0, NVT); 2032 break; 2033 } 2034 // These operators cannot be expanded directly, emit them as calls to 2035 // library functions. 2036 case ISD::FP_TO_SINT: 2037 if (Node->getOperand(0).getValueType() == MVT::f32) 2038 Lo = ExpandLibCall("__fixsfdi", Node, Hi); 2039 else 2040 Lo = ExpandLibCall("__fixdfdi", Node, Hi); 2041 break; 2042 case ISD::FP_TO_UINT: 2043 if (Node->getOperand(0).getValueType() == MVT::f32) 2044 Lo = ExpandLibCall("__fixunssfdi", Node, Hi); 2045 else 2046 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); 2047 break; 2048 2049 case ISD::SHL: 2050 // If we can emit an efficient shift operation, do so now. 2051 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) 2052 break; 2053 2054 // If this target supports SHL_PARTS, use it. 2055 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) { 2056 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1), 2057 Lo, Hi); 2058 break; 2059 } 2060 2061 // Otherwise, emit a libcall. 2062 Lo = ExpandLibCall("__ashldi3", Node, Hi); 2063 break; 2064 2065 case ISD::SRA: 2066 // If we can emit an efficient shift operation, do so now. 2067 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) 2068 break; 2069 2070 // If this target supports SRA_PARTS, use it. 2071 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) { 2072 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1), 2073 Lo, Hi); 2074 break; 2075 } 2076 2077 // Otherwise, emit a libcall. 2078 Lo = ExpandLibCall("__ashrdi3", Node, Hi); 2079 break; 2080 case ISD::SRL: 2081 // If we can emit an efficient shift operation, do so now. 2082 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) 2083 break; 2084 2085 // If this target supports SRL_PARTS, use it. 2086 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) { 2087 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1), 2088 Lo, Hi); 2089 break; 2090 } 2091 2092 // Otherwise, emit a libcall. 2093 Lo = ExpandLibCall("__lshrdi3", Node, Hi); 2094 break; 2095 2096 case ISD::ADD: 2097 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1), 2098 Lo, Hi); 2099 break; 2100 case ISD::SUB: 2101 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1), 2102 Lo, Hi); 2103 break; 2104 case ISD::MUL: { 2105 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) { 2106 SDOperand LL, LH, RL, RH; 2107 ExpandOp(Node->getOperand(0), LL, LH); 2108 ExpandOp(Node->getOperand(1), RL, RH); 2109 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); 2110 RH = DAG.getNode(ISD::MUL, NVT, LL, RH); 2111 LH = DAG.getNode(ISD::MUL, NVT, LH, RL); 2112 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); 2113 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); 2114 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); 2115 } else { 2116 Lo = ExpandLibCall("__muldi3" , Node, Hi); break; 2117 } 2118 break; 2119 } 2120 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; 2121 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; 2122 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; 2123 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; 2124 } 2125 2126 // Remember in a map if the values will be reused later. 2127 if (!Node->hasOneUse()) { 2128 bool isNew = ExpandedNodes.insert(std::make_pair(Op, 2129 std::make_pair(Lo, Hi))).second; 2130 assert(isNew && "Value already expanded?!?"); 2131 } 2132} 2133 2134 2135// SelectionDAG::Legalize - This is the entry point for the file. 2136// 2137void SelectionDAG::Legalize() { 2138 /// run - This is the main entry point to this class. 2139 /// 2140 SelectionDAGLegalize(*this).Run(); 2141} 2142 2143