LegalizeDAG.cpp revision 1e81b9e5112c40c23ecca9a64b6ab2ce97c8c6d4
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(TargetLowering &TLI, 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 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, 123 SDOperand &Lo, SDOperand &Hi); 124 125 SDOperand getIntPtrConstant(uint64_t Val) { 126 return DAG.getConstant(Val, TLI.getPointerTy()); 127 } 128}; 129} 130 131 132SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli, 133 SelectionDAG &dag) 134 : TLI(tli), DAG(dag), ValueTypeActions(TLI.getValueTypeActions()) { 135 assert(MVT::LAST_VALUETYPE <= 16 && 136 "Too many value types for ValueTypeActions to hold!"); 137} 138 139void SelectionDAGLegalize::LegalizeDAG() { 140 SDOperand OldRoot = DAG.getRoot(); 141 SDOperand NewRoot = LegalizeOp(OldRoot); 142 DAG.setRoot(NewRoot); 143 144 ExpandedNodes.clear(); 145 LegalizedNodes.clear(); 146 PromotedNodes.clear(); 147 148 // Remove dead nodes now. 149 DAG.RemoveDeadNodes(OldRoot.Val); 150} 151 152SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 153 assert(getTypeAction(Op.getValueType()) == Legal && 154 "Caller should expand or promote operands that are not legal!"); 155 156 // If this operation defines any values that cannot be represented in a 157 // register on this target, make sure to expand or promote them. 158 if (Op.Val->getNumValues() > 1) { 159 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i) 160 switch (getTypeAction(Op.Val->getValueType(i))) { 161 case Legal: break; // Nothing to do. 162 case Expand: { 163 SDOperand T1, T2; 164 ExpandOp(Op.getValue(i), T1, T2); 165 assert(LegalizedNodes.count(Op) && 166 "Expansion didn't add legal operands!"); 167 return LegalizedNodes[Op]; 168 } 169 case Promote: 170 PromoteOp(Op.getValue(i)); 171 assert(LegalizedNodes.count(Op) && 172 "Expansion didn't add legal operands!"); 173 return LegalizedNodes[Op]; 174 } 175 } 176 177 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 178 if (I != LegalizedNodes.end()) return I->second; 179 180 SDOperand Tmp1, Tmp2, Tmp3; 181 182 SDOperand Result = Op; 183 SDNode *Node = Op.Val; 184 185 switch (Node->getOpcode()) { 186 default: 187 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 188 assert(0 && "Do not know how to legalize this operator!"); 189 abort(); 190 case ISD::EntryToken: 191 case ISD::FrameIndex: 192 case ISD::GlobalAddress: 193 case ISD::ExternalSymbol: 194 case ISD::ConstantPool: // Nothing to do. 195 assert(getTypeAction(Node->getValueType(0)) == Legal && 196 "This must be legal!"); 197 break; 198 case ISD::CopyFromReg: 199 Tmp1 = LegalizeOp(Node->getOperand(0)); 200 if (Tmp1 != Node->getOperand(0)) 201 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), 202 Node->getValueType(0), Tmp1); 203 break; 204 case ISD::ImplicitDef: 205 Tmp1 = LegalizeOp(Node->getOperand(0)); 206 if (Tmp1 != Node->getOperand(0)) 207 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg()); 208 break; 209 case ISD::Constant: 210 // We know we don't need to expand constants here, constants only have one 211 // value and we check that it is fine above. 212 213 // FIXME: Maybe we should handle things like targets that don't support full 214 // 32-bit immediates? 215 break; 216 case ISD::ConstantFP: { 217 // Spill FP immediates to the constant pool if the target cannot directly 218 // codegen them. Targets often have some immediate values that can be 219 // efficiently generated into an FP register without a load. We explicitly 220 // leave these constants as ConstantFP nodes for the target to deal with. 221 222 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 223 224 // Check to see if this FP immediate is already legal. 225 bool isLegal = false; 226 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), 227 E = TLI.legal_fpimm_end(); I != E; ++I) 228 if (CFP->isExactlyValue(*I)) { 229 isLegal = true; 230 break; 231 } 232 233 if (!isLegal) { 234 // Otherwise we need to spill the constant to memory. 235 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); 236 237 bool Extend = false; 238 239 // If a FP immediate is precise when represented as a float, we put it 240 // into the constant pool as a float, even if it's is statically typed 241 // as a double. 242 MVT::ValueType VT = CFP->getValueType(0); 243 bool isDouble = VT == MVT::f64; 244 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : 245 Type::FloatTy, CFP->getValue()); 246 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) { 247 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); 248 VT = MVT::f32; 249 Extend = true; 250 } 251 252 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), 253 TLI.getPointerTy()); 254 if (Extend) { 255 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx, 256 MVT::f32); 257 } else { 258 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx); 259 } 260 } 261 break; 262 } 263 case ISD::TokenFactor: { 264 std::vector<SDOperand> Ops; 265 bool Changed = false; 266 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 267 SDOperand Op = Node->getOperand(i); 268 // Fold single-use TokenFactor nodes into this token factor as we go. 269 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { 270 Changed = true; 271 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) 272 Ops.push_back(LegalizeOp(Op.getOperand(j))); 273 } else { 274 Ops.push_back(LegalizeOp(Op)); // Legalize the operands 275 Changed |= Ops[i] != Op; 276 } 277 } 278 if (Changed) 279 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); 280 break; 281 } 282 283 case ISD::ADJCALLSTACKDOWN: 284 case ISD::ADJCALLSTACKUP: 285 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 286 // There is no need to legalize the size argument (Operand #1) 287 if (Tmp1 != Node->getOperand(0)) 288 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, 289 Node->getOperand(1)); 290 break; 291 case ISD::DYNAMIC_STACKALLOC: 292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. 294 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. 295 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 296 Tmp3 != Node->getOperand(2)) 297 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0), 298 Tmp1, Tmp2, Tmp3); 299 else 300 Result = Op.getValue(0); 301 302 // Since this op produces two values, make sure to remember that we 303 // legalized both of them. 304 AddLegalizedOperand(SDOperand(Node, 0), Result); 305 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 306 return Result.getValue(Op.ResNo); 307 308 case ISD::CALL: 309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 310 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 311 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 312 std::vector<MVT::ValueType> RetTyVTs; 313 RetTyVTs.reserve(Node->getNumValues()); 314 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 315 RetTyVTs.push_back(Node->getValueType(i)); 316 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0); 317 } else { 318 Result = Result.getValue(0); 319 } 320 // Since calls produce multiple values, make sure to remember that we 321 // legalized all of them. 322 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 323 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); 324 return Result.getValue(Op.ResNo); 325 326 case ISD::BR: 327 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 328 if (Tmp1 != Node->getOperand(0)) 329 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); 330 break; 331 332 case ISD::BRCOND: 333 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 334 335 switch (getTypeAction(Node->getOperand(1).getValueType())) { 336 case Expand: assert(0 && "It's impossible to expand bools"); 337 case Legal: 338 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 339 break; 340 case Promote: 341 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 342 break; 343 } 344 // Basic block destination (Op#2) is always legal. 345 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 346 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 347 Node->getOperand(2)); 348 break; 349 350 case ISD::LOAD: 351 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 352 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 353 if (Tmp1 != Node->getOperand(0) || 354 Tmp2 != Node->getOperand(1)) 355 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2); 356 else 357 Result = SDOperand(Node, 0); 358 359 // Since loads produce two values, make sure to remember that we legalized 360 // both of them. 361 AddLegalizedOperand(SDOperand(Node, 0), Result); 362 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 363 return Result.getValue(Op.ResNo); 364 365 case ISD::EXTLOAD: 366 case ISD::SEXTLOAD: 367 case ISD::ZEXTLOAD: 368 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 369 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 370 if (Tmp1 != Node->getOperand(0) || 371 Tmp2 != Node->getOperand(1)) 372 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2, 373 cast<MVTSDNode>(Node)->getExtraValueType()); 374 else 375 Result = SDOperand(Node, 0); 376 377 // Since loads produce two values, make sure to remember that we legalized 378 // both of them. 379 AddLegalizedOperand(SDOperand(Node, 0), Result); 380 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 381 return Result.getValue(Op.ResNo); 382 383 case ISD::EXTRACT_ELEMENT: 384 // Get both the low and high parts. 385 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 386 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) 387 Result = Tmp2; // 1 -> Hi 388 else 389 Result = Tmp1; // 0 -> Lo 390 break; 391 392 case ISD::CopyToReg: 393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 394 395 switch (getTypeAction(Node->getOperand(1).getValueType())) { 396 case Legal: 397 // Legalize the incoming value (must be legal). 398 Tmp2 = LegalizeOp(Node->getOperand(1)); 399 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 400 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); 401 break; 402 case Promote: 403 Tmp2 = PromoteOp(Node->getOperand(1)); 404 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); 405 break; 406 case Expand: 407 SDOperand Lo, Hi; 408 ExpandOp(Node->getOperand(1), Lo, Hi); 409 unsigned Reg = cast<RegSDNode>(Node)->getReg(); 410 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg); 411 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1); 412 // Note that the copytoreg nodes are independent of each other. 413 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 414 assert(isTypeLegal(Result.getValueType()) && 415 "Cannot expand multiple times yet (i64 -> i16)"); 416 break; 417 } 418 break; 419 420 case ISD::RET: 421 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 422 switch (Node->getNumOperands()) { 423 case 2: // ret val 424 switch (getTypeAction(Node->getOperand(1).getValueType())) { 425 case Legal: 426 Tmp2 = LegalizeOp(Node->getOperand(1)); 427 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 428 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 429 break; 430 case Expand: { 431 SDOperand Lo, Hi; 432 ExpandOp(Node->getOperand(1), Lo, Hi); 433 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); 434 break; 435 } 436 case Promote: 437 Tmp2 = PromoteOp(Node->getOperand(1)); 438 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 439 break; 440 } 441 break; 442 case 1: // ret void 443 if (Tmp1 != Node->getOperand(0)) 444 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); 445 break; 446 default: { // ret <values> 447 std::vector<SDOperand> NewValues; 448 NewValues.push_back(Tmp1); 449 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) 450 switch (getTypeAction(Node->getOperand(i).getValueType())) { 451 case Legal: 452 NewValues.push_back(LegalizeOp(Node->getOperand(i))); 453 break; 454 case Expand: { 455 SDOperand Lo, Hi; 456 ExpandOp(Node->getOperand(i), Lo, Hi); 457 NewValues.push_back(Lo); 458 NewValues.push_back(Hi); 459 break; 460 } 461 case Promote: 462 assert(0 && "Can't promote multiple return value yet!"); 463 } 464 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); 465 break; 466 } 467 } 468 break; 469 case ISD::STORE: 470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 471 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 472 473 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 474 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ 475 if (CFP->getValueType(0) == MVT::f32) { 476 union { 477 unsigned I; 478 float F; 479 } V; 480 V.F = CFP->getValue(); 481 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 482 DAG.getConstant(V.I, MVT::i32), Tmp2); 483 } else { 484 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); 485 union { 486 uint64_t I; 487 double F; 488 } V; 489 V.F = CFP->getValue(); 490 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 491 DAG.getConstant(V.I, MVT::i64), Tmp2); 492 } 493 Op = Result; 494 Node = Op.Val; 495 } 496 497 switch (getTypeAction(Node->getOperand(1).getValueType())) { 498 case Legal: { 499 SDOperand Val = LegalizeOp(Node->getOperand(1)); 500 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || 501 Tmp2 != Node->getOperand(2)) 502 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2); 503 break; 504 } 505 case Promote: 506 // Truncate the value and store the result. 507 Tmp3 = PromoteOp(Node->getOperand(1)); 508 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, 509 Node->getOperand(1).getValueType()); 510 break; 511 512 case Expand: 513 SDOperand Lo, Hi; 514 ExpandOp(Node->getOperand(1), Lo, Hi); 515 516 if (!TLI.isLittleEndian()) 517 std::swap(Lo, Hi); 518 519 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2); 520 521 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; 522 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, 523 getIntPtrConstant(IncrementSize)); 524 assert(isTypeLegal(Tmp2.getValueType()) && 525 "Pointers must be legal!"); 526 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2); 527 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 528 break; 529 } 530 break; 531 case ISD::TRUNCSTORE: 532 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 533 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 534 535 switch (getTypeAction(Node->getOperand(1).getValueType())) { 536 case Legal: 537 Tmp2 = LegalizeOp(Node->getOperand(1)); 538 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 539 Tmp3 != Node->getOperand(2)) 540 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, 541 cast<MVTSDNode>(Node)->getExtraValueType()); 542 break; 543 case Promote: 544 case Expand: 545 assert(0 && "Cannot handle illegal TRUNCSTORE yet!"); 546 } 547 break; 548 case ISD::SELECT: 549 switch (getTypeAction(Node->getOperand(0).getValueType())) { 550 case Expand: assert(0 && "It's impossible to expand bools"); 551 case Legal: 552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 553 break; 554 case Promote: 555 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 556 break; 557 } 558 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal 559 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal 560 561 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) { 562 default: assert(0 && "This action is not supported yet!"); 563 case TargetLowering::Legal: 564 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 565 Tmp3 != Node->getOperand(2)) 566 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), 567 Tmp1, Tmp2, Tmp3); 568 break; 569 case TargetLowering::Promote: { 570 MVT::ValueType NVT = 571 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); 572 unsigned ExtOp, TruncOp; 573 if (MVT::isInteger(Tmp2.getValueType())) { 574 ExtOp = ISD::ZERO_EXTEND; 575 TruncOp = ISD::TRUNCATE; 576 } else { 577 ExtOp = ISD::FP_EXTEND; 578 TruncOp = ISD::FP_ROUND; 579 } 580 // Promote each of the values to the new type. 581 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); 582 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); 583 // Perform the larger operation, then round down. 584 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); 585 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); 586 break; 587 } 588 } 589 break; 590 case ISD::SETCC: 591 switch (getTypeAction(Node->getOperand(0).getValueType())) { 592 case Legal: 593 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 594 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 595 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 596 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 597 Node->getValueType(0), Tmp1, Tmp2); 598 break; 599 case Promote: 600 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS 601 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS 602 603 // If this is an FP compare, the operands have already been extended. 604 if (MVT::isInteger(Node->getOperand(0).getValueType())) { 605 MVT::ValueType VT = Node->getOperand(0).getValueType(); 606 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 607 608 // Otherwise, we have to insert explicit sign or zero extends. Note 609 // that we could insert sign extends for ALL conditions, but zero extend 610 // is cheaper on many machines (an AND instead of two shifts), so prefer 611 // it. 612 switch (cast<SetCCSDNode>(Node)->getCondition()) { 613 default: assert(0 && "Unknown integer comparison!"); 614 case ISD::SETEQ: 615 case ISD::SETNE: 616 case ISD::SETUGE: 617 case ISD::SETUGT: 618 case ISD::SETULE: 619 case ISD::SETULT: 620 // ALL of these operations will work if we either sign or zero extend 621 // the operands (including the unsigned comparisons!). Zero extend is 622 // usually a simpler/cheaper operation, so prefer it. 623 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 624 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); 625 break; 626 case ISD::SETGE: 627 case ISD::SETGT: 628 case ISD::SETLT: 629 case ISD::SETLE: 630 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 631 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); 632 break; 633 } 634 635 } 636 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 637 Node->getValueType(0), Tmp1, Tmp2); 638 break; 639 case Expand: 640 SDOperand LHSLo, LHSHi, RHSLo, RHSHi; 641 ExpandOp(Node->getOperand(0), LHSLo, LHSHi); 642 ExpandOp(Node->getOperand(1), RHSLo, RHSHi); 643 switch (cast<SetCCSDNode>(Node)->getCondition()) { 644 case ISD::SETEQ: 645 case ISD::SETNE: 646 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); 647 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); 648 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); 649 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 650 Node->getValueType(0), Tmp1, 651 DAG.getConstant(0, Tmp1.getValueType())); 652 break; 653 default: 654 // FIXME: This generated code sucks. 655 ISD::CondCode LowCC; 656 switch (cast<SetCCSDNode>(Node)->getCondition()) { 657 default: assert(0 && "Unknown integer setcc!"); 658 case ISD::SETLT: 659 case ISD::SETULT: LowCC = ISD::SETULT; break; 660 case ISD::SETGT: 661 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 662 case ISD::SETLE: 663 case ISD::SETULE: LowCC = ISD::SETULE; break; 664 case ISD::SETGE: 665 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 666 } 667 668 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 669 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 670 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 671 672 // NOTE: on targets without efficient SELECT of bools, we can always use 673 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 674 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo); 675 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 676 Node->getValueType(0), LHSHi, RHSHi); 677 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi); 678 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(), 679 Result, Tmp1, Tmp2); 680 break; 681 } 682 } 683 break; 684 685 case ISD::MEMSET: 686 case ISD::MEMCPY: 687 case ISD::MEMMOVE: { 688 Tmp1 = LegalizeOp(Node->getOperand(0)); 689 Tmp2 = LegalizeOp(Node->getOperand(1)); 690 Tmp3 = LegalizeOp(Node->getOperand(2)); 691 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3)); 692 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4)); 693 694 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 695 default: assert(0 && "This action not implemented for this operation!"); 696 case TargetLowering::Legal: 697 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 698 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || 699 Tmp5 != Node->getOperand(4)) { 700 std::vector<SDOperand> Ops; 701 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 702 Ops.push_back(Tmp4); Ops.push_back(Tmp5); 703 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); 704 } 705 break; 706 case TargetLowering::Expand: { 707 // Otherwise, the target does not support this operation. Lower the 708 // operation to an explicit libcall as appropriate. 709 MVT::ValueType IntPtr = TLI.getPointerTy(); 710 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); 711 std::vector<std::pair<SDOperand, const Type*> > Args; 712 713 const char *FnName = 0; 714 if (Node->getOpcode() == ISD::MEMSET) { 715 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 716 // Extend the ubyte argument to be an int value for the call. 717 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); 718 Args.push_back(std::make_pair(Tmp3, Type::IntTy)); 719 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 720 721 FnName = "memset"; 722 } else if (Node->getOpcode() == ISD::MEMCPY || 723 Node->getOpcode() == ISD::MEMMOVE) { 724 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 725 Args.push_back(std::make_pair(Tmp3, IntPtrTy)); 726 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 727 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; 728 } else { 729 assert(0 && "Unknown op!"); 730 } 731 std::pair<SDOperand,SDOperand> CallResult = 732 TLI.LowerCallTo(Tmp1, Type::VoidTy, 733 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); 734 Result = LegalizeOp(CallResult.second); 735 break; 736 } 737 case TargetLowering::Custom: 738 std::vector<SDOperand> Ops; 739 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 740 Ops.push_back(Tmp4); Ops.push_back(Tmp5); 741 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); 742 Result = TLI.LowerOperation(Result); 743 Result = LegalizeOp(Result); 744 break; 745 } 746 break; 747 } 748 case ISD::ADD: 749 case ISD::SUB: 750 case ISD::MUL: 751 case ISD::UDIV: 752 case ISD::SDIV: 753 case ISD::UREM: 754 case ISD::SREM: 755 case ISD::AND: 756 case ISD::OR: 757 case ISD::XOR: 758 case ISD::SHL: 759 case ISD::SRL: 760 case ISD::SRA: 761 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 762 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 763 if (Tmp1 != Node->getOperand(0) || 764 Tmp2 != Node->getOperand(1)) 765 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); 766 break; 767 case ISD::ZERO_EXTEND: 768 case ISD::SIGN_EXTEND: 769 case ISD::TRUNCATE: 770 case ISD::FP_EXTEND: 771 case ISD::FP_ROUND: 772 case ISD::FP_TO_SINT: 773 case ISD::FP_TO_UINT: 774 case ISD::SINT_TO_FP: 775 case ISD::UINT_TO_FP: 776 switch (getTypeAction(Node->getOperand(0).getValueType())) { 777 case Legal: 778 Tmp1 = LegalizeOp(Node->getOperand(0)); 779 if (Tmp1 != Node->getOperand(0)) 780 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 781 break; 782 case Expand: 783 assert(Node->getOpcode() != ISD::SINT_TO_FP && 784 Node->getOpcode() != ISD::UINT_TO_FP && 785 "Cannot lower Xint_to_fp to a call yet!"); 786 787 // In the expand case, we must be dealing with a truncate, because 788 // otherwise the result would be larger than the source. 789 assert(Node->getOpcode() == ISD::TRUNCATE && 790 "Shouldn't need to expand other operators here!"); 791 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 792 793 // Since the result is legal, we should just be able to truncate the low 794 // part of the source. 795 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); 796 break; 797 798 case Promote: 799 switch (Node->getOpcode()) { 800 case ISD::ZERO_EXTEND: 801 Result = PromoteOp(Node->getOperand(0)); 802 // NOTE: Any extend would work here... 803 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); 804 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(), 805 Result, Node->getOperand(0).getValueType()); 806 break; 807 case ISD::SIGN_EXTEND: 808 Result = PromoteOp(Node->getOperand(0)); 809 // NOTE: Any extend would work here... 810 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); 811 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 812 Result, Node->getOperand(0).getValueType()); 813 break; 814 case ISD::TRUNCATE: 815 Result = PromoteOp(Node->getOperand(0)); 816 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); 817 break; 818 case ISD::FP_EXTEND: 819 Result = PromoteOp(Node->getOperand(0)); 820 if (Result.getValueType() != Op.getValueType()) 821 // Dynamically dead while we have only 2 FP types. 822 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); 823 break; 824 case ISD::FP_ROUND: 825 case ISD::FP_TO_SINT: 826 case ISD::FP_TO_UINT: 827 Result = PromoteOp(Node->getOperand(0)); 828 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); 829 break; 830 case ISD::SINT_TO_FP: 831 Result = PromoteOp(Node->getOperand(0)); 832 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 833 Result, Node->getOperand(0).getValueType()); 834 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); 835 break; 836 case ISD::UINT_TO_FP: 837 Result = PromoteOp(Node->getOperand(0)); 838 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), 839 Result, Node->getOperand(0).getValueType()); 840 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); 841 break; 842 } 843 } 844 break; 845 case ISD::FP_ROUND_INREG: 846 case ISD::SIGN_EXTEND_INREG: 847 case ISD::ZERO_EXTEND_INREG: { 848 Tmp1 = LegalizeOp(Node->getOperand(0)); 849 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType(); 850 851 // If this operation is not supported, convert it to a shl/shr or load/store 852 // pair. 853 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { 854 default: assert(0 && "This action not supported for this op yet!"); 855 case TargetLowering::Legal: 856 if (Tmp1 != Node->getOperand(0)) 857 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, 858 ExtraVT); 859 break; 860 case TargetLowering::Expand: 861 // If this is an integer extend and shifts are supported, do that. 862 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) { 863 // NOTE: we could fall back on load/store here too for targets without 864 // AND. However, it is doubtful that any exist. 865 // AND out the appropriate bits. 866 SDOperand Mask = 867 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1, 868 Node->getValueType(0)); 869 Result = DAG.getNode(ISD::AND, Node->getValueType(0), 870 Node->getOperand(0), Mask); 871 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { 872 // NOTE: we could fall back on load/store here too for targets without 873 // SAR. However, it is doubtful that any exist. 874 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - 875 MVT::getSizeInBits(ExtraVT); 876 SDOperand ShiftCst = DAG.getConstant(BitsDiff, MVT::i8); 877 Result = DAG.getNode(ISD::SHL, Node->getValueType(0), 878 Node->getOperand(0), ShiftCst); 879 Result = DAG.getNode(ISD::SRA, Node->getValueType(0), 880 Result, ShiftCst); 881 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { 882 // The only way we can lower this is to turn it into a STORETRUNC, 883 // EXTLOAD pair, targetting a temporary location (a stack slot). 884 885 // NOTE: there is a choice here between constantly creating new stack 886 // slots and always reusing the same one. We currently always create 887 // new ones, as reuse may inhibit scheduling. 888 const Type *Ty = MVT::getTypeForValueType(ExtraVT); 889 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); 890 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); 891 MachineFunction &MF = DAG.getMachineFunction(); 892 int SSFI = 893 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); 894 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 895 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), 896 Node->getOperand(0), StackSlot, ExtraVT); 897 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), 898 Result, StackSlot, ExtraVT); 899 } else { 900 assert(0 && "Unknown op"); 901 } 902 Result = LegalizeOp(Result); 903 break; 904 } 905 break; 906 } 907 } 908 909 if (!Op.Val->hasOneUse()) 910 AddLegalizedOperand(Op, Result); 911 912 return Result; 913} 914 915/// PromoteOp - Given an operation that produces a value in an invalid type, 916/// promote it to compute the value into a larger type. The produced value will 917/// have the correct bits for the low portion of the register, but no guarantee 918/// is made about the top bits: it may be zero, sign-extended, or garbage. 919SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { 920 MVT::ValueType VT = Op.getValueType(); 921 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 922 assert(getTypeAction(VT) == Promote && 923 "Caller should expand or legalize operands that are not promotable!"); 924 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && 925 "Cannot promote to smaller type!"); 926 927 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); 928 if (I != PromotedNodes.end()) return I->second; 929 930 SDOperand Tmp1, Tmp2, Tmp3; 931 932 SDOperand Result; 933 SDNode *Node = Op.Val; 934 935 // Promotion needs an optimization step to clean up after it, and is not 936 // careful to avoid operations the target does not support. Make sure that 937 // all generated operations are legalized in the next iteration. 938 NeedsAnotherIteration = true; 939 940 switch (Node->getOpcode()) { 941 default: 942 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 943 assert(0 && "Do not know how to promote this operator!"); 944 abort(); 945 case ISD::Constant: 946 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); 947 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); 948 break; 949 case ISD::ConstantFP: 950 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); 951 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); 952 break; 953 case ISD::CopyFromReg: 954 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT, 955 Node->getOperand(0)); 956 // Remember that we legalized the chain. 957 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 958 break; 959 960 case ISD::SETCC: 961 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal && 962 "SetCC type is not legal??"); 963 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), 964 TLI.getSetCCResultTy(), Node->getOperand(0), 965 Node->getOperand(1)); 966 Result = LegalizeOp(Result); 967 break; 968 969 case ISD::TRUNCATE: 970 switch (getTypeAction(Node->getOperand(0).getValueType())) { 971 case Legal: 972 Result = LegalizeOp(Node->getOperand(0)); 973 assert(Result.getValueType() >= NVT && 974 "This truncation doesn't make sense!"); 975 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT 976 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); 977 break; 978 case Expand: 979 assert(0 && "Cannot handle expand yet"); 980 case Promote: 981 assert(0 && "Cannot handle promote-promote yet"); 982 } 983 break; 984 case ISD::SIGN_EXTEND: 985 case ISD::ZERO_EXTEND: 986 switch (getTypeAction(Node->getOperand(0).getValueType())) { 987 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); 988 case Legal: 989 // Input is legal? Just do extend all the way to the larger type. 990 Result = LegalizeOp(Node->getOperand(0)); 991 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 992 break; 993 case Promote: 994 // Promote the reg if it's smaller. 995 Result = PromoteOp(Node->getOperand(0)); 996 // The high bits are not guaranteed to be anything. Insert an extend. 997 if (Node->getOpcode() == ISD::SIGN_EXTEND) 998 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT); 999 else 1000 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT); 1001 break; 1002 } 1003 break; 1004 1005 case ISD::FP_EXTEND: 1006 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); 1007 case ISD::FP_ROUND: 1008 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1009 case Expand: assert(0 && "BUG: Cannot expand FP regs!"); 1010 case Promote: assert(0 && "Unreachable with 2 FP types!"); 1011 case Legal: 1012 // Input is legal? Do an FP_ROUND_INREG. 1013 Result = LegalizeOp(Node->getOperand(0)); 1014 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1015 break; 1016 } 1017 break; 1018 1019 case ISD::SINT_TO_FP: 1020 case ISD::UINT_TO_FP: 1021 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1022 case Legal: 1023 Result = LegalizeOp(Node->getOperand(0)); 1024 break; 1025 1026 case Promote: 1027 Result = PromoteOp(Node->getOperand(0)); 1028 if (Node->getOpcode() == ISD::SINT_TO_FP) 1029 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1030 Result, Node->getOperand(0).getValueType()); 1031 else 1032 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), 1033 Result, Node->getOperand(0).getValueType()); 1034 break; 1035 case Expand: 1036 assert(0 && "Unimplemented"); 1037 } 1038 // No extra round required here. 1039 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 1040 break; 1041 1042 case ISD::FP_TO_SINT: 1043 case ISD::FP_TO_UINT: 1044 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1045 case Legal: 1046 Tmp1 = LegalizeOp(Node->getOperand(0)); 1047 break; 1048 case Promote: 1049 // The input result is prerounded, so we don't have to do anything 1050 // special. 1051 Tmp1 = PromoteOp(Node->getOperand(0)); 1052 break; 1053 case Expand: 1054 assert(0 && "not implemented"); 1055 } 1056 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 1057 break; 1058 1059 case ISD::AND: 1060 case ISD::OR: 1061 case ISD::XOR: 1062 case ISD::ADD: 1063 case ISD::SUB: 1064 case ISD::MUL: 1065 // The input may have strange things in the top bits of the registers, but 1066 // these operations don't care. They may have wierd bits going out, but 1067 // that too is okay if they are integer operations. 1068 Tmp1 = PromoteOp(Node->getOperand(0)); 1069 Tmp2 = PromoteOp(Node->getOperand(1)); 1070 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 1071 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1072 1073 // However, if this is a floating point operation, they will give excess 1074 // precision that we may not be able to tolerate. If we DO allow excess 1075 // precision, just leave it, otherwise excise it. 1076 // FIXME: Why would we need to round FP ops more than integer ones? 1077 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) 1078 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 1079 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1080 break; 1081 1082 case ISD::SDIV: 1083 case ISD::SREM: 1084 // These operators require that their input be sign extended. 1085 Tmp1 = PromoteOp(Node->getOperand(0)); 1086 Tmp2 = PromoteOp(Node->getOperand(1)); 1087 if (MVT::isInteger(NVT)) { 1088 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 1089 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); 1090 } 1091 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1092 1093 // Perform FP_ROUND: this is probably overly pessimistic. 1094 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 1095 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); 1096 break; 1097 1098 case ISD::UDIV: 1099 case ISD::UREM: 1100 // These operators require that their input be zero extended. 1101 Tmp1 = PromoteOp(Node->getOperand(0)); 1102 Tmp2 = PromoteOp(Node->getOperand(1)); 1103 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); 1104 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 1105 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); 1106 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 1107 break; 1108 1109 case ISD::SHL: 1110 Tmp1 = PromoteOp(Node->getOperand(0)); 1111 Tmp2 = LegalizeOp(Node->getOperand(1)); 1112 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2); 1113 break; 1114 case ISD::SRA: 1115 // The input value must be properly sign extended. 1116 Tmp1 = PromoteOp(Node->getOperand(0)); 1117 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); 1118 Tmp2 = LegalizeOp(Node->getOperand(1)); 1119 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2); 1120 break; 1121 case ISD::SRL: 1122 // The input value must be properly zero extended. 1123 Tmp1 = PromoteOp(Node->getOperand(0)); 1124 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); 1125 Tmp2 = LegalizeOp(Node->getOperand(1)); 1126 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2); 1127 break; 1128 case ISD::LOAD: 1129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1130 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1131 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT); 1132 1133 // Remember that we legalized the chain. 1134 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1135 break; 1136 case ISD::SELECT: 1137 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1138 case Expand: assert(0 && "It's impossible to expand bools"); 1139 case Legal: 1140 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition. 1141 break; 1142 case Promote: 1143 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 1144 break; 1145 } 1146 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 1147 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 1148 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3); 1149 break; 1150 case ISD::CALL: { 1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 1153 1154 assert(Node->getNumValues() == 2 && Op.ResNo == 0 && 1155 "Can only promote single result calls"); 1156 std::vector<MVT::ValueType> RetTyVTs; 1157 RetTyVTs.reserve(2); 1158 RetTyVTs.push_back(NVT); 1159 RetTyVTs.push_back(MVT::Other); 1160 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2); 1161 Result = SDOperand(NC, 0); 1162 1163 // Insert the new chain mapping. 1164 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1165 break; 1166 } 1167 } 1168 1169 assert(Result.Val && "Didn't set a result!"); 1170 AddPromotedOperand(Op, Result); 1171 return Result; 1172} 1173 1174/// ExpandShift - Try to find a clever way to expand this shift operation out to 1175/// smaller elements. If we can't find a way that is more efficient than a 1176/// libcall on this target, return false. Otherwise, return true with the 1177/// low-parts expanded into Lo and Hi. 1178bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, 1179 SDOperand &Lo, SDOperand &Hi) { 1180 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && 1181 "This is not a shift!"); 1182 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); 1183 1184 // If we have an efficient select operation (or if the selects will all fold 1185 // away), lower to some complex code, otherwise just emit the libcall. 1186 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal && 1187 !isa<ConstantSDNode>(Amt)) 1188 return false; 1189 1190 SDOperand InL, InH; 1191 ExpandOp(Op, InL, InH); 1192 SDOperand ShAmt = LegalizeOp(Amt); 1193 SDOperand OShAmt = ShAmt; // Unmasked shift amount. 1194 MVT::ValueType ShTy = ShAmt.getValueType(); 1195 1196 unsigned NVTBits = MVT::getSizeInBits(NVT); 1197 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt 1198 DAG.getConstant(NVTBits, ShTy), ShAmt); 1199 1200 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) { 1201 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31 1202 DAG.getConstant(NVTBits-1, ShTy)); 1203 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31 1204 DAG.getConstant(NVTBits-1, ShTy)); 1205 } 1206 1207 if (Opc == ISD::SHL) { 1208 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt) 1209 DAG.getNode(ISD::SHL, NVT, InH, ShAmt), 1210 DAG.getNode(ISD::SRL, NVT, InL, NAmt)); 1211 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt 1212 1213 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), OShAmt, 1214 DAG.getConstant(NVTBits, ShTy)); 1215 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); 1216 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2); 1217 } else { 1218 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt) 1219 DAG.getNode(ISD::SHL, NVT, InH, NAmt), 1220 DAG.getNode(ISD::SRL, NVT, InL, ShAmt)); 1221 bool isSign = Opc == ISD::SRA; 1222 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); 1223 1224 SDOperand HiPart; 1225 if (isSign) 1226 HiPart = DAG.getNode(Opc, NVT, InH, DAG.getConstant(NVTBits-1, ShTy)); 1227 else 1228 HiPart = DAG.getConstant(0, NVT); 1229 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), OShAmt, 1230 DAG.getConstant(NVTBits, ShTy)); 1231 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); 1232 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart,T2); 1233 } 1234 return true; 1235} 1236 1237 1238 1239/// ExpandOp - Expand the specified SDOperand into its two component pieces 1240/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the 1241/// LegalizeNodes map is filled in for any results that are not expanded, the 1242/// ExpandedNodes map is filled in for any results that are expanded, and the 1243/// Lo/Hi values are returned. 1244void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ 1245 MVT::ValueType VT = Op.getValueType(); 1246 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 1247 SDNode *Node = Op.Val; 1248 assert(getTypeAction(VT) == Expand && "Not an expanded type!"); 1249 assert(MVT::isInteger(VT) && "Cannot expand FP values!"); 1250 assert(MVT::isInteger(NVT) && NVT < VT && 1251 "Cannot expand to FP value or to larger int value!"); 1252 1253 // If there is more than one use of this, see if we already expanded it. 1254 // There is no use remembering values that only have a single use, as the map 1255 // entries will never be reused. 1256 if (!Node->hasOneUse()) { 1257 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 1258 = ExpandedNodes.find(Op); 1259 if (I != ExpandedNodes.end()) { 1260 Lo = I->second.first; 1261 Hi = I->second.second; 1262 return; 1263 } 1264 } 1265 1266 // Expanding to multiple registers needs to perform an optimization step, and 1267 // is not careful to avoid operations the target does not support. Make sure 1268 // that all generated operations are legalized in the next iteration. 1269 NeedsAnotherIteration = true; 1270 const char *LibCallName = 0; 1271 1272 switch (Node->getOpcode()) { 1273 default: 1274 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 1275 assert(0 && "Do not know how to expand this operator!"); 1276 abort(); 1277 case ISD::Constant: { 1278 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); 1279 Lo = DAG.getConstant(Cst, NVT); 1280 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); 1281 break; 1282 } 1283 1284 case ISD::CopyFromReg: { 1285 unsigned Reg = cast<RegSDNode>(Node)->getReg(); 1286 // Aggregate register values are always in consequtive pairs. 1287 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0)); 1288 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1)); 1289 1290 // Remember that we legalized the chain. 1291 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); 1292 1293 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); 1294 break; 1295 } 1296 1297 case ISD::LOAD: { 1298 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1299 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1300 Lo = DAG.getLoad(NVT, Ch, Ptr); 1301 1302 // Increment the pointer to the other half. 1303 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; 1304 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 1305 getIntPtrConstant(IncrementSize)); 1306 Hi = DAG.getLoad(NVT, Ch, Ptr); 1307 1308 // Build a factor node to remember that this load is independent of the 1309 // other one. 1310 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 1311 Hi.getValue(1)); 1312 1313 // Remember that we legalized the chain. 1314 AddLegalizedOperand(Op.getValue(1), TF); 1315 if (!TLI.isLittleEndian()) 1316 std::swap(Lo, Hi); 1317 break; 1318 } 1319 case ISD::CALL: { 1320 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1321 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. 1322 1323 assert(Node->getNumValues() == 2 && Op.ResNo == 0 && 1324 "Can only expand a call once so far, not i64 -> i16!"); 1325 1326 std::vector<MVT::ValueType> RetTyVTs; 1327 RetTyVTs.reserve(3); 1328 RetTyVTs.push_back(NVT); 1329 RetTyVTs.push_back(NVT); 1330 RetTyVTs.push_back(MVT::Other); 1331 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee); 1332 Lo = SDOperand(NC, 0); 1333 Hi = SDOperand(NC, 1); 1334 1335 // Insert the new chain mapping. 1336 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); 1337 break; 1338 } 1339 case ISD::AND: 1340 case ISD::OR: 1341 case ISD::XOR: { // Simple logical operators -> two trivial pieces. 1342 SDOperand LL, LH, RL, RH; 1343 ExpandOp(Node->getOperand(0), LL, LH); 1344 ExpandOp(Node->getOperand(1), RL, RH); 1345 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); 1346 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); 1347 break; 1348 } 1349 case ISD::SELECT: { 1350 SDOperand C, LL, LH, RL, RH; 1351 1352 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1353 case Expand: assert(0 && "It's impossible to expand bools"); 1354 case Legal: 1355 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 1356 break; 1357 case Promote: 1358 C = PromoteOp(Node->getOperand(0)); // Promote the condition. 1359 break; 1360 } 1361 ExpandOp(Node->getOperand(1), LL, LH); 1362 ExpandOp(Node->getOperand(2), RL, RH); 1363 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); 1364 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); 1365 break; 1366 } 1367 case ISD::SIGN_EXTEND: { 1368 // The low part is just a sign extension of the input (which degenerates to 1369 // a copy). 1370 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); 1371 1372 // The high part is obtained by SRA'ing all but one of the bits of the lo 1373 // part. 1374 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 1375 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8)); 1376 break; 1377 } 1378 case ISD::ZERO_EXTEND: 1379 // The low part is just a zero extension of the input (which degenerates to 1380 // a copy). 1381 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); 1382 1383 // The high part is just a zero. 1384 Hi = DAG.getConstant(0, NVT); 1385 break; 1386 1387 // These operators cannot be expanded directly, emit them as calls to 1388 // library functions. 1389 case ISD::FP_TO_SINT: 1390 if (Node->getOperand(0).getValueType() == MVT::f32) 1391 LibCallName = "__fixsfdi"; 1392 else 1393 LibCallName = "__fixdfdi"; 1394 break; 1395 case ISD::FP_TO_UINT: 1396 if (Node->getOperand(0).getValueType() == MVT::f32) 1397 LibCallName = "__fixunssfdi"; 1398 else 1399 LibCallName = "__fixunsdfdi"; 1400 break; 1401 1402 case ISD::SHL: 1403 // If we can emit an efficient shift operation, do so now. 1404 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), 1405 Lo, Hi)) 1406 break; 1407 // Otherwise, emit a libcall. 1408 LibCallName = "__ashldi3"; 1409 break; 1410 1411 case ISD::SRA: 1412 // If we can emit an efficient shift operation, do so now. 1413 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), 1414 Lo, Hi)) 1415 break; 1416 // Otherwise, emit a libcall. 1417 LibCallName = "__ashrdi3"; 1418 break; 1419 case ISD::SRL: 1420 // If we can emit an efficient shift operation, do so now. 1421 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), 1422 Lo, Hi)) 1423 break; 1424 // Otherwise, emit a libcall. 1425 LibCallName = "__lshrdi3"; 1426 break; 1427 1428 case ISD::ADD: LibCallName = "__adddi3"; break; 1429 case ISD::SUB: LibCallName = "__subdi3"; break; 1430 case ISD::MUL: LibCallName = "__muldi3"; break; 1431 case ISD::SDIV: LibCallName = "__divdi3"; break; 1432 case ISD::UDIV: LibCallName = "__udivdi3"; break; 1433 case ISD::SREM: LibCallName = "__moddi3"; break; 1434 case ISD::UREM: LibCallName = "__umoddi3"; break; 1435 } 1436 1437 // Int2FP -> __floatdisf/__floatdidf 1438 1439 // If this is to be expanded into a libcall... do so now. 1440 if (LibCallName) { 1441 TargetLowering::ArgListTy Args; 1442 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) 1443 Args.push_back(std::make_pair(Node->getOperand(i), 1444 MVT::getTypeForValueType(Node->getOperand(i).getValueType()))); 1445 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy()); 1446 1447 // We don't care about token chains for libcalls. We just use the entry 1448 // node as our input and ignore the output chain. This allows us to place 1449 // calls wherever we need them to satisfy data dependences. 1450 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(), 1451 MVT::getTypeForValueType(Op.getValueType()), Callee, 1452 Args, DAG).first; 1453 ExpandOp(Result, Lo, Hi); 1454 } 1455 1456 // Remember in a map if the values will be reused later. 1457 if (!Node->hasOneUse()) { 1458 bool isNew = ExpandedNodes.insert(std::make_pair(Op, 1459 std::make_pair(Lo, Hi))).second; 1460 assert(isNew && "Value already expanded?!?"); 1461 } 1462} 1463 1464 1465// SelectionDAG::Legalize - This is the entry point for the file. 1466// 1467void SelectionDAG::Legalize(TargetLowering &TLI) { 1468 /// run - This is the main entry point to this class. 1469 /// 1470 SelectionDAGLegalize(TLI, *this).Run(); 1471} 1472 1473