LegalizeDAG.cpp revision fa404e8a76abfdafefb8806b35f596d288609496
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/Target/TargetLowering.h" 18#include "llvm/Constants.h" 19#include <iostream> 20using namespace llvm; 21 22static const Type *getTypeFor(MVT::ValueType VT) { 23 switch (VT) { 24 default: assert(0 && "Unknown MVT!"); 25 case MVT::i1: return Type::BoolTy; 26 case MVT::i8: return Type::UByteTy; 27 case MVT::i16: return Type::UShortTy; 28 case MVT::i32: return Type::UIntTy; 29 case MVT::i64: return Type::ULongTy; 30 case MVT::f32: return Type::FloatTy; 31 case MVT::f64: return Type::DoubleTy; 32 } 33} 34 35 36//===----------------------------------------------------------------------===// 37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and 38/// hacks on it until the target machine can handle it. This involves 39/// eliminating value sizes the machine cannot handle (promoting small sizes to 40/// large sizes or splitting up large values into small values) as well as 41/// eliminating operations the machine cannot handle. 42/// 43/// This code also does a small amount of optimization and recognition of idioms 44/// as part of its processing. For example, if a target does not support a 45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 46/// will attempt merge setcc and brc instructions into brcc's. 47/// 48namespace { 49class SelectionDAGLegalize { 50 TargetLowering &TLI; 51 SelectionDAG &DAG; 52 53 /// LegalizeAction - This enum indicates what action we should take for each 54 /// value type the can occur in the program. 55 enum LegalizeAction { 56 Legal, // The target natively supports this value type. 57 Promote, // This should be promoted to the next larger type. 58 Expand, // This integer type should be broken into smaller pieces. 59 }; 60 61 /// TransformToType - For any value types we are promoting or expanding, this 62 /// contains the value type that we are changing to. For Expanded types, this 63 /// contains one step of the expand (e.g. i64 -> i32), even if there are 64 /// multiple steps required (e.g. i64 -> i16) 65 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE]; 66 67 /// ValueTypeActions - This is a bitvector that contains two bits for each 68 /// value type, where the two bits correspond to the LegalizeAction enum. 69 /// This can be queried with "getTypeAction(VT)". 70 unsigned ValueTypeActions; 71 72 /// NeedsAnotherIteration - This is set when we expand a large integer 73 /// operation into smaller integer operations, but the smaller operations are 74 /// not set. This occurs only rarely in practice, for targets that don't have 75 /// 32-bit or larger integer registers. 76 bool NeedsAnotherIteration; 77 78 /// LegalizedNodes - For nodes that are of legal width, and that have more 79 /// than one use, this map indicates what regularized operand to use. This 80 /// allows us to avoid legalizing the same thing more than once. 81 std::map<SDOperand, SDOperand> LegalizedNodes; 82 83 /// ExpandedNodes - For nodes that need to be expanded, and which have more 84 /// than one use, this map indicates which which operands are the expanded 85 /// version of the input. This allows us to avoid expanding the same node 86 /// more than once. 87 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; 88 89 void AddLegalizedOperand(SDOperand From, SDOperand To) { 90 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second; 91 assert(isNew && "Got into the map somehow?"); 92 } 93 94 /// setValueTypeAction - Set the action for a particular value type. This 95 /// assumes an action has not already been set for this value type. 96 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) { 97 ValueTypeActions |= A << (VT*2); 98 if (A == Promote) { 99 MVT::ValueType PromoteTo; 100 if (VT == MVT::f32) 101 PromoteTo = MVT::f64; 102 else { 103 unsigned LargerReg = VT+1; 104 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) { 105 ++LargerReg; 106 assert(MVT::isInteger((MVT::ValueType)LargerReg) && 107 "Nothing to promote to??"); 108 } 109 PromoteTo = (MVT::ValueType)LargerReg; 110 } 111 112 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) && 113 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) && 114 "Can only promote from int->int or fp->fp!"); 115 assert(VT < PromoteTo && "Must promote to a larger type!"); 116 TransformToType[VT] = PromoteTo; 117 } else if (A == Expand) { 118 assert(MVT::isInteger(VT) && VT > MVT::i8 && 119 "Cannot expand this type: target must support SOME integer reg!"); 120 // Expand to the next smaller integer type! 121 TransformToType[VT] = (MVT::ValueType)(VT-1); 122 } 123 } 124 125public: 126 127 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG); 128 129 /// Run - While there is still lowering to do, perform a pass over the DAG. 130 /// Most regularization can be done in a single pass, but targets that require 131 /// large values to be split into registers multiple times (e.g. i64 -> 4x 132 /// i16) require iteration for these values (the first iteration will demote 133 /// to i32, the second will demote to i16). 134 void Run() { 135 do { 136 NeedsAnotherIteration = false; 137 LegalizeDAG(); 138 } while (NeedsAnotherIteration); 139 } 140 141 /// getTypeAction - Return how we should legalize values of this type, either 142 /// it is already legal or we need to expand it into multiple registers of 143 /// smaller integer type, or we need to promote it to a larger type. 144 LegalizeAction getTypeAction(MVT::ValueType VT) const { 145 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); 146 } 147 148 /// isTypeLegal - Return true if this type is legal on this target. 149 /// 150 bool isTypeLegal(MVT::ValueType VT) const { 151 return getTypeAction(VT) == Legal; 152 } 153 154private: 155 void LegalizeDAG(); 156 157 SDOperand LegalizeOp(SDOperand O); 158 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); 159 160 SDOperand getIntPtrConstant(uint64_t Val) { 161 return DAG.getConstant(Val, TLI.getPointerTy()); 162 } 163}; 164} 165 166 167SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli, 168 SelectionDAG &dag) 169 : TLI(tli), DAG(dag), ValueTypeActions(0) { 170 171 assert(MVT::LAST_VALUETYPE <= 16 && 172 "Too many value types for ValueTypeActions to hold!"); 173 174 // Inspect all of the ValueType's possible, deciding how to process them. 175 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg) 176 // If TLI says we are expanding this type, expand it! 177 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1) 178 setValueTypeAction((MVT::ValueType)IntReg, Expand); 179 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg)) 180 // Otherwise, if we don't have native support, we must promote to a 181 // larger type. 182 setValueTypeAction((MVT::ValueType)IntReg, Promote); 183 184 // If the target does not have native support for F32, promote it to F64. 185 if (!TLI.hasNativeSupportFor(MVT::f32)) 186 setValueTypeAction(MVT::f32, Promote); 187} 188 189void SelectionDAGLegalize::LegalizeDAG() { 190 SDOperand OldRoot = DAG.getRoot(); 191 SDOperand NewRoot = LegalizeOp(OldRoot); 192 DAG.setRoot(NewRoot); 193 194 ExpandedNodes.clear(); 195 LegalizedNodes.clear(); 196 197 // Remove dead nodes now. 198 DAG.RemoveDeadNodes(OldRoot.Val); 199} 200 201SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 202 assert(getTypeAction(Op.getValueType()) == Legal && 203 "Caller should expand or promote operands that are not legal!"); 204 205 // If this operation defines any values that cannot be represented in a 206 // register on this target, make sure to expand or promote them. 207 if (Op.Val->getNumValues() > 1) { 208 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i) 209 switch (getTypeAction(Op.Val->getValueType(i))) { 210 case Legal: break; // Nothing to do. 211 case Expand: { 212 SDOperand T1, T2; 213 ExpandOp(Op.getValue(i), T1, T2); 214 assert(LegalizedNodes.count(Op) && 215 "Expansion didn't add legal operands!"); 216 return LegalizedNodes[Op]; 217 } 218 case Promote: 219 // FIXME: Implement promotion! 220 assert(0 && "Promotion not implemented at all yet!"); 221 } 222 } 223 224 // If there is more than one use of this, see if we already legalized it. 225 // There is no use remembering values that only have a single use, as the map 226 // entries will never be reused. 227 if (!Op.Val->hasOneUse()) { 228 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 229 if (I != LegalizedNodes.end()) return I->second; 230 } 231 232 SDOperand Tmp1, Tmp2, Tmp3; 233 234 SDOperand Result = Op; 235 SDNode *Node = Op.Val; 236 237 switch (Node->getOpcode()) { 238 default: 239 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 240 assert(0 && "Do not know how to legalize this operator!"); 241 abort(); 242 case ISD::EntryToken: 243 case ISD::FrameIndex: 244 case ISD::GlobalAddress: 245 case ISD::ExternalSymbol: 246 case ISD::ConstantPool: 247 case ISD::CopyFromReg: // Nothing to do. 248 assert(getTypeAction(Node->getValueType(0)) == Legal && 249 "This must be legal!"); 250 break; 251 case ISD::Constant: 252 // We know we don't need to expand constants here, constants only have one 253 // value and we check that it is fine above. 254 255 // FIXME: Maybe we should handle things like targets that don't support full 256 // 32-bit immediates? 257 break; 258 case ISD::ConstantFP: { 259 // Spill FP immediates to the constant pool if the target cannot directly 260 // codegen them. Targets often have some immediate values that can be 261 // efficiently generated into an FP register without a load. We explicitly 262 // leave these constants as ConstantFP nodes for the target to deal with. 263 264 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 265 266 // Check to see if this FP immediate is already legal. 267 bool isLegal = false; 268 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), 269 E = TLI.legal_fpimm_end(); I != E; ++I) 270 if (CFP->isExactlyValue(*I)) { 271 isLegal = true; 272 break; 273 } 274 275 if (!isLegal) { 276 // Otherwise we need to spill the constant to memory. 277 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); 278 279 bool Extend = false; 280 281 // If a FP immediate is precise when represented as a float, we put it 282 // into the constant pool as a float, even if it's is statically typed 283 // as a double. 284 MVT::ValueType VT = CFP->getValueType(0); 285 bool isDouble = VT == MVT::f64; 286 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : 287 Type::FloatTy, CFP->getValue()); 288 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) { 289 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); 290 VT = MVT::f32; 291 Extend = true; 292 } 293 294 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), 295 TLI.getPointerTy()); 296 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx); 297 298 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result); 299 } 300 break; 301 } 302 case ISD::ADJCALLSTACKDOWN: 303 case ISD::ADJCALLSTACKUP: 304 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 305 // There is no need to legalize the size argument (Operand #1) 306 if (Tmp1 != Node->getOperand(0)) 307 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, 308 Node->getOperand(1)); 309 break; 310 case ISD::DYNAMIC_STACKALLOC: 311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 312 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. 313 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. 314 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 315 Tmp3 != Node->getOperand(2)) 316 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0), 317 Tmp1, Tmp2, Tmp3); 318 319 // Since this op produces two values, make sure to remember that we 320 // legalized both of them. 321 AddLegalizedOperand(SDOperand(Node, 0), Result); 322 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 323 return Result.getValue(Op.ResNo); 324 325 case ISD::CALL: 326 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 327 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 328 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 329 std::vector<MVT::ValueType> RetTyVTs; 330 RetTyVTs.reserve(Node->getNumValues()); 331 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 332 RetTyVTs.push_back(Node->getValueType(i)); 333 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo); 334 } 335 break; 336 337 case ISD::BR: 338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 339 if (Tmp1 != Node->getOperand(0)) 340 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); 341 break; 342 343 case ISD::BRCOND: 344 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 345 // FIXME: booleans might not be legal! 346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 347 // Basic block destination (Op#2) is always legal. 348 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 349 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 350 Node->getOperand(2)); 351 break; 352 353 case ISD::LOAD: 354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 355 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 356 if (Tmp1 != Node->getOperand(0) || 357 Tmp2 != Node->getOperand(1)) 358 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2); 359 else 360 Result = SDOperand(Node, 0); 361 362 // Since loads produce two values, make sure to remember that we legalized 363 // both of them. 364 AddLegalizedOperand(SDOperand(Node, 0), Result); 365 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 366 return Result.getValue(Op.ResNo); 367 368 case ISD::EXTRACT_ELEMENT: 369 // Get both the low and high parts. 370 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 371 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) 372 Result = Tmp2; // 1 -> Hi 373 else 374 Result = Tmp1; // 0 -> Lo 375 break; 376 377 case ISD::CopyToReg: 378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 379 380 switch (getTypeAction(Node->getOperand(1).getValueType())) { 381 case Legal: 382 // Legalize the incoming value (must be legal). 383 Tmp2 = LegalizeOp(Node->getOperand(1)); 384 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 385 Result = DAG.getCopyToReg(Tmp1, Tmp2, 386 cast<CopyRegSDNode>(Node)->getReg()); 387 break; 388 case Expand: { 389 SDOperand Lo, Hi; 390 ExpandOp(Node->getOperand(1), Lo, Hi); 391 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg(); 392 Result = DAG.getCopyToReg(Tmp1, Lo, Reg); 393 Result = DAG.getCopyToReg(Result, Hi, Reg+1); 394 assert(isTypeLegal(Result.getValueType()) && 395 "Cannot expand multiple times yet (i64 -> i16)"); 396 break; 397 } 398 case Promote: 399 assert(0 && "Don't know what it means to promote this!"); 400 abort(); 401 } 402 break; 403 404 case ISD::RET: 405 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 406 switch (Node->getNumOperands()) { 407 case 2: // ret val 408 switch (getTypeAction(Node->getOperand(1).getValueType())) { 409 case Legal: 410 Tmp2 = LegalizeOp(Node->getOperand(1)); 411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 412 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 413 break; 414 case Expand: { 415 SDOperand Lo, Hi; 416 ExpandOp(Node->getOperand(1), Lo, Hi); 417 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); 418 break; 419 } 420 case Promote: 421 assert(0 && "Can't promote return value!"); 422 } 423 break; 424 case 1: // ret void 425 if (Tmp1 != Node->getOperand(0)) 426 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); 427 break; 428 default: { // ret <values> 429 std::vector<SDOperand> NewValues; 430 NewValues.push_back(Tmp1); 431 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) 432 switch (getTypeAction(Node->getOperand(i).getValueType())) { 433 case Legal: 434 NewValues.push_back(LegalizeOp(Node->getOperand(i))); 435 break; 436 case Expand: { 437 SDOperand Lo, Hi; 438 ExpandOp(Node->getOperand(i), Lo, Hi); 439 NewValues.push_back(Lo); 440 NewValues.push_back(Hi); 441 break; 442 } 443 case Promote: 444 assert(0 && "Can't promote return value!"); 445 } 446 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); 447 break; 448 } 449 } 450 break; 451 case ISD::STORE: 452 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 453 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 454 455 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 456 if (ConstantFPSDNode *CFP = 457 dyn_cast<ConstantFPSDNode>(Node->getOperand(1))) { 458 if (CFP->getValueType(0) == MVT::f32) { 459 union { 460 unsigned I; 461 float F; 462 } V; 463 V.F = CFP->getValue(); 464 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 465 DAG.getConstant(V.I, MVT::i32), Tmp2); 466 } else { 467 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); 468 union { 469 uint64_t I; 470 double F; 471 } V; 472 V.F = CFP->getValue(); 473 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 474 DAG.getConstant(V.I, MVT::i64), Tmp2); 475 } 476 Op = Result; 477 Node = Op.Val; 478 } 479 480 switch (getTypeAction(Node->getOperand(1).getValueType())) { 481 case Legal: { 482 SDOperand Val = LegalizeOp(Node->getOperand(1)); 483 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || 484 Tmp2 != Node->getOperand(2)) 485 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2); 486 break; 487 } 488 case Promote: 489 assert(0 && "FIXME: promote for stores not implemented!"); 490 case Expand: 491 SDOperand Lo, Hi; 492 ExpandOp(Node->getOperand(1), Lo, Hi); 493 494 if (!TLI.isLittleEndian()) 495 std::swap(Lo, Hi); 496 497 // FIXME: These two stores are independent of each other! 498 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2); 499 500 unsigned IncrementSize; 501 switch (Lo.getValueType()) { 502 default: assert(0 && "Unknown ValueType to expand to!"); 503 case MVT::i32: IncrementSize = 4; break; 504 case MVT::i16: IncrementSize = 2; break; 505 case MVT::i8: IncrementSize = 1; break; 506 } 507 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, 508 getIntPtrConstant(IncrementSize)); 509 assert(isTypeLegal(Tmp2.getValueType()) && 510 "Pointers must be legal!"); 511 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2); 512 } 513 break; 514 case ISD::SELECT: { 515 // FIXME: BOOLS MAY REQUIRE PROMOTION! 516 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond 517 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal 518 SDOperand Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal 519 520 if (Tmp1 != Node->getOperand(0) || 521 Tmp2 != Node->getOperand(1) || 522 Tmp3 != Node->getOperand(2)) 523 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3); 524 break; 525 } 526 case ISD::SETCC: 527 switch (getTypeAction(Node->getOperand(0).getValueType())) { 528 case Legal: 529 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 530 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 531 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 532 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 533 Tmp1, Tmp2); 534 break; 535 case Promote: 536 assert(0 && "Can't promote setcc operands yet!"); 537 break; 538 case Expand: 539 SDOperand LHSLo, LHSHi, RHSLo, RHSHi; 540 ExpandOp(Node->getOperand(0), LHSLo, LHSHi); 541 ExpandOp(Node->getOperand(1), RHSLo, RHSHi); 542 switch (cast<SetCCSDNode>(Node)->getCondition()) { 543 case ISD::SETEQ: 544 case ISD::SETNE: 545 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); 546 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); 547 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); 548 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1, 549 DAG.getConstant(0, Tmp1.getValueType())); 550 break; 551 default: 552 // FIXME: This generated code sucks. 553 ISD::CondCode LowCC; 554 switch (cast<SetCCSDNode>(Node)->getCondition()) { 555 default: assert(0 && "Unknown integer setcc!"); 556 case ISD::SETLT: 557 case ISD::SETULT: LowCC = ISD::SETULT; break; 558 case ISD::SETGT: 559 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 560 case ISD::SETLE: 561 case ISD::SETULE: LowCC = ISD::SETULE; break; 562 case ISD::SETGE: 563 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 564 } 565 566 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 567 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 568 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 569 570 // NOTE: on targets without efficient SELECT of bools, we can always use 571 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 572 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo); 573 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 574 LHSHi, RHSHi); 575 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi); 576 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2); 577 break; 578 } 579 } 580 break; 581 582 case ISD::ADD: 583 case ISD::SUB: 584 case ISD::MUL: 585 case ISD::UDIV: 586 case ISD::SDIV: 587 case ISD::UREM: 588 case ISD::SREM: 589 case ISD::AND: 590 case ISD::OR: 591 case ISD::XOR: 592 case ISD::SHL: 593 case ISD::SRL: 594 case ISD::SRA: 595 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 596 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 597 if (Tmp1 != Node->getOperand(0) || 598 Tmp2 != Node->getOperand(1)) 599 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); 600 break; 601 case ISD::ZERO_EXTEND: 602 case ISD::SIGN_EXTEND: 603 case ISD::TRUNCATE: 604 case ISD::FP_EXTEND: 605 case ISD::FP_ROUND: 606 case ISD::FP_TO_SINT: 607 case ISD::FP_TO_UINT: 608 case ISD::SINT_TO_FP: 609 case ISD::UINT_TO_FP: 610 611 switch (getTypeAction(Node->getOperand(0).getValueType())) { 612 case Legal: 613 Tmp1 = LegalizeOp(Node->getOperand(0)); 614 if (Tmp1 != Node->getOperand(0)) 615 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 616 break; 617 case Expand: 618 // In the expand case, we must be dealing with a truncate, because 619 // otherwise the result would be larger than the source. 620 assert(Node->getOpcode() == ISD::TRUNCATE && 621 "Shouldn't need to expand other operators here!"); 622 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 623 624 // Since the result is legal, we should just be able to truncate the low 625 // part of the source. 626 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); 627 break; 628 629 default: 630 assert(0 && "Do not know how to promote this yet!"); 631 } 632 break; 633 } 634 635 if (!Op.Val->hasOneUse()) 636 AddLegalizedOperand(Op, Result); 637 638 return Result; 639} 640 641 642/// ExpandOp - Expand the specified SDOperand into its two component pieces 643/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the 644/// LegalizeNodes map is filled in for any results that are not expanded, the 645/// ExpandedNodes map is filled in for any results that are expanded, and the 646/// Lo/Hi values are returned. 647void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ 648 MVT::ValueType VT = Op.getValueType(); 649 MVT::ValueType NVT = TransformToType[VT]; 650 SDNode *Node = Op.Val; 651 assert(getTypeAction(VT) == Expand && "Not an expanded type!"); 652 assert(MVT::isInteger(VT) && "Cannot expand FP values!"); 653 assert(MVT::isInteger(NVT) && NVT < VT && 654 "Cannot expand to FP value or to larger int value!"); 655 656 // If there is more than one use of this, see if we already expanded it. 657 // There is no use remembering values that only have a single use, as the map 658 // entries will never be reused. 659 if (!Node->hasOneUse()) { 660 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 661 = ExpandedNodes.find(Op); 662 if (I != ExpandedNodes.end()) { 663 Lo = I->second.first; 664 Hi = I->second.second; 665 return; 666 } 667 } 668 669 // Expanding to multiple registers needs to perform an optimization step, and 670 // is not careful to avoid operations the target does not support. Make sure 671 // that all generated operations are legalized in the next iteration. 672 NeedsAnotherIteration = true; 673 const char *LibCallName = 0; 674 675 switch (Node->getOpcode()) { 676 default: 677 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 678 assert(0 && "Do not know how to expand this operator!"); 679 abort(); 680 case ISD::Constant: { 681 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); 682 Lo = DAG.getConstant(Cst, NVT); 683 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); 684 break; 685 } 686 687 case ISD::CopyFromReg: { 688 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg(); 689 // Aggregate register values are always in consequtive pairs. 690 Lo = DAG.getCopyFromReg(Reg, NVT); 691 Hi = DAG.getCopyFromReg(Reg+1, NVT); 692 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); 693 break; 694 } 695 696 case ISD::LOAD: { 697 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 698 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 699 Lo = DAG.getLoad(NVT, Ch, Ptr); 700 701 // Increment the pointer to the other half. 702 unsigned IncrementSize; 703 switch (Lo.getValueType()) { 704 default: assert(0 && "Unknown ValueType to expand to!"); 705 case MVT::i32: IncrementSize = 4; break; 706 case MVT::i16: IncrementSize = 2; break; 707 case MVT::i8: IncrementSize = 1; break; 708 } 709 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 710 getIntPtrConstant(IncrementSize)); 711 // FIXME: This load is independent of the first one. 712 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr); 713 714 // Remember that we legalized the chain. 715 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); 716 if (!TLI.isLittleEndian()) 717 std::swap(Lo, Hi); 718 break; 719 } 720 case ISD::CALL: { 721 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 722 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 723 724 assert(Node->getNumValues() == 2 && Op.ResNo == 0 && 725 "Can only expand a call once so far, not i64 -> i16!"); 726 727 std::vector<MVT::ValueType> RetTyVTs; 728 RetTyVTs.reserve(3); 729 RetTyVTs.push_back(NVT); 730 RetTyVTs.push_back(NVT); 731 RetTyVTs.push_back(MVT::Other); 732 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee); 733 Lo = SDOperand(NC, 0); 734 Hi = SDOperand(NC, 1); 735 736 // Insert the new chain mapping. 737 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); 738 break; 739 } 740 case ISD::AND: 741 case ISD::OR: 742 case ISD::XOR: { // Simple logical operators -> two trivial pieces. 743 SDOperand LL, LH, RL, RH; 744 ExpandOp(Node->getOperand(0), LL, LH); 745 ExpandOp(Node->getOperand(1), RL, RH); 746 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); 747 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); 748 break; 749 } 750 case ISD::SELECT: { 751 SDOperand C, LL, LH, RL, RH; 752 // FIXME: BOOLS MAY REQUIRE PROMOTION! 753 C = LegalizeOp(Node->getOperand(0)); 754 ExpandOp(Node->getOperand(1), LL, LH); 755 ExpandOp(Node->getOperand(2), RL, RH); 756 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); 757 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); 758 break; 759 } 760 case ISD::SIGN_EXTEND: { 761 // The low part is just a sign extension of the input (which degenerates to 762 // a copy). 763 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); 764 765 // The high part is obtained by SRA'ing all but one of the bits of the lo 766 // part. 767 unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType()); 768 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8)); 769 break; 770 } 771 case ISD::ZERO_EXTEND: 772 // The low part is just a zero extension of the input (which degenerates to 773 // a copy). 774 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); 775 776 // The high part is just a zero. 777 Hi = DAG.getConstant(0, NVT); 778 break; 779 780 // These operators cannot be expanded directly, emit them as calls to 781 // library functions. 782 case ISD::FP_TO_SINT: 783 if (Node->getOperand(0).getValueType() == MVT::f32) 784 LibCallName = "__fixsfdi"; 785 else 786 LibCallName = "__fixdfdi"; 787 break; 788 case ISD::FP_TO_UINT: 789 if (Node->getOperand(0).getValueType() == MVT::f32) 790 LibCallName = "__fixunssfdi"; 791 else 792 LibCallName = "__fixunsdfdi"; 793 break; 794 795 case ISD::ADD: LibCallName = "__adddi3"; break; 796 case ISD::SUB: LibCallName = "__subdi3"; break; 797 case ISD::MUL: LibCallName = "__muldi3"; break; 798 case ISD::SDIV: LibCallName = "__divdi3"; break; 799 case ISD::UDIV: LibCallName = "__udivdi3"; break; 800 case ISD::SREM: LibCallName = "__moddi3"; break; 801 case ISD::UREM: LibCallName = "__umoddi3"; break; 802 case ISD::SHL: LibCallName = "__lshrdi3"; break; 803 case ISD::SRA: LibCallName = "__ashrdi3"; break; 804 case ISD::SRL: LibCallName = "__ashldi3"; break; 805 } 806 807 // Int2FP -> __floatdisf/__floatdidf 808 809 // If this is to be expanded into a libcall... do so now. 810 if (LibCallName) { 811 TargetLowering::ArgListTy Args; 812 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) 813 Args.push_back(std::make_pair(Node->getOperand(i), 814 getTypeFor(Node->getOperand(i).getValueType()))); 815 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy()); 816 817 // We don't care about token chains for libcalls. We just use the entry 818 // node as our input and ignore the output chain. This allows us to place 819 // calls wherever we need them to satisfy data dependences. 820 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(), 821 getTypeFor(Op.getValueType()), Callee, 822 Args, DAG).first; 823 ExpandOp(Result, Lo, Hi); 824 } 825 826 // Remember in a map if the values will be reused later. 827 if (!Node->hasOneUse()) { 828 bool isNew = ExpandedNodes.insert(std::make_pair(Op, 829 std::make_pair(Lo, Hi))).second; 830 assert(isNew && "Value already expanded?!?"); 831 } 832} 833 834 835// SelectionDAG::Legalize - This is the entry point for the file. 836// 837void SelectionDAG::Legalize(TargetLowering &TLI) { 838 /// run - This is the main entry point to this class. 839 /// 840 SelectionDAGLegalize(TLI, *this).Run(); 841} 842 843