X86ISelDAGToDAG.cpp revision 2c79de8018bb8c77a245d4ccb740affbf1f52319
1//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the Evan Cheng and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines a DAG pattern matching instruction selector for X86, 11// converting from a legalized dag to a X86 dag. 12// 13//===----------------------------------------------------------------------===// 14 15#define DEBUG_TYPE "isel" 16#include "X86.h" 17#include "X86InstrBuilder.h" 18#include "X86ISelLowering.h" 19#include "X86RegisterInfo.h" 20#include "X86Subtarget.h" 21#include "X86TargetMachine.h" 22#include "llvm/GlobalValue.h" 23#include "llvm/Instructions.h" 24#include "llvm/Intrinsics.h" 25#include "llvm/Support/CFG.h" 26#include "llvm/CodeGen/MachineConstantPool.h" 27#include "llvm/CodeGen/MachineFunction.h" 28#include "llvm/CodeGen/MachineFrameInfo.h" 29#include "llvm/CodeGen/MachineInstrBuilder.h" 30#include "llvm/CodeGen/SSARegMap.h" 31#include "llvm/CodeGen/SelectionDAGISel.h" 32#include "llvm/Target/TargetMachine.h" 33#include "llvm/Support/Debug.h" 34#include "llvm/Support/Visibility.h" 35#include "llvm/ADT/Statistic.h" 36#include <iostream> 37#include <set> 38using namespace llvm; 39 40//===----------------------------------------------------------------------===// 41// Pattern Matcher Implementation 42//===----------------------------------------------------------------------===// 43 44namespace { 45 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses 46 /// SDOperand's instead of register numbers for the leaves of the matched 47 /// tree. 48 struct X86ISelAddressMode { 49 enum { 50 RegBase, 51 FrameIndexBase 52 } BaseType; 53 54 struct { // This is really a union, discriminated by BaseType! 55 SDOperand Reg; 56 int FrameIndex; 57 } Base; 58 59 unsigned Scale; 60 SDOperand IndexReg; 61 unsigned Disp; 62 GlobalValue *GV; 63 Constant *CP; 64 unsigned Align; // CP alignment. 65 66 X86ISelAddressMode() 67 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0), 68 CP(0), Align(0) { 69 } 70 }; 71} 72 73namespace { 74 Statistic<> 75 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); 76 77 //===--------------------------------------------------------------------===// 78 /// ISel - X86 specific code to select X86 machine instructions for 79 /// SelectionDAG operations. 80 /// 81 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { 82 /// ContainsFPCode - Every instruction we select that uses or defines a FP 83 /// register should set this to true. 84 bool ContainsFPCode; 85 86 /// X86Lowering - This object fully describes how to lower LLVM code to an 87 /// X86-specific SelectionDAG. 88 X86TargetLowering X86Lowering; 89 90 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can 91 /// make the right decision when generating code for different targets. 92 const X86Subtarget *Subtarget; 93 94 unsigned GlobalBaseReg; 95 public: 96 X86DAGToDAGISel(X86TargetMachine &TM) 97 : SelectionDAGISel(X86Lowering), 98 X86Lowering(*TM.getTargetLowering()) { 99 Subtarget = &TM.getSubtarget<X86Subtarget>(); 100 } 101 102 virtual bool runOnFunction(Function &Fn) { 103 // Make sure we re-emit a set of the global base reg if necessary 104 GlobalBaseReg = 0; 105 return SelectionDAGISel::runOnFunction(Fn); 106 } 107 108 virtual const char *getPassName() const { 109 return "X86 DAG->DAG Instruction Selection"; 110 } 111 112 /// InstructionSelectBasicBlock - This callback is invoked by 113 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 114 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); 115 116 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); 117 118// Include the pieces autogenerated from the target description. 119#include "X86GenDAGISel.inc" 120 121 private: 122 void Select(SDOperand &Result, SDOperand N); 123 124 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true); 125 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, 126 SDOperand &Index, SDOperand &Disp); 127 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, 128 SDOperand &Index, SDOperand &Disp); 129 bool TryFoldLoad(SDOperand P, SDOperand N, 130 SDOperand &Base, SDOperand &Scale, 131 SDOperand &Index, SDOperand &Disp); 132 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 133 /// inline asm expressions. 134 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, 135 char ConstraintCode, 136 std::vector<SDOperand> &OutOps, 137 SelectionDAG &DAG); 138 139 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); 140 141 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, 142 SDOperand &Scale, SDOperand &Index, 143 SDOperand &Disp) { 144 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? 145 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg; 146 Scale = getI8Imm(AM.Scale); 147 Index = AM.IndexReg; 148 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp) 149 : (AM.CP ? 150 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp) 151 : getI32Imm(AM.Disp)); 152 } 153 154 /// getI8Imm - Return a target constant with the specified value, of type 155 /// i8. 156 inline SDOperand getI8Imm(unsigned Imm) { 157 return CurDAG->getTargetConstant(Imm, MVT::i8); 158 } 159 160 /// getI16Imm - Return a target constant with the specified value, of type 161 /// i16. 162 inline SDOperand getI16Imm(unsigned Imm) { 163 return CurDAG->getTargetConstant(Imm, MVT::i16); 164 } 165 166 /// getI32Imm - Return a target constant with the specified value, of type 167 /// i32. 168 inline SDOperand getI32Imm(unsigned Imm) { 169 return CurDAG->getTargetConstant(Imm, MVT::i32); 170 } 171 172 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC 173 /// base register. Return the virtual register that holds this value. 174 SDOperand getGlobalBaseReg(); 175 176#ifndef NDEBUG 177 unsigned Indent; 178#endif 179 }; 180} 181 182/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel 183/// when it has created a SelectionDAG for us to codegen. 184void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { 185 DEBUG(BB->dump()); 186 MachineFunction::iterator FirstMBB = BB; 187 188 // Codegen the basic block. 189#ifndef NDEBUG 190 DEBUG(std::cerr << "===== Instruction selection begins:\n"); 191 Indent = 0; 192#endif 193 DAG.setRoot(SelectRoot(DAG.getRoot())); 194 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!"); 195#ifndef NDEBUG 196 DEBUG(std::cerr << "===== Instruction selection ends:\n"); 197#endif 198 CodeGenMap.clear(); 199 HandleMap.clear(); 200 ReplaceMap.clear(); 201 DAG.RemoveDeadNodes(); 202 203 // Emit machine code to BB. 204 ScheduleAndEmitDAG(DAG); 205 206 // If we are emitting FP stack code, scan the basic block to determine if this 207 // block defines any FP values. If so, put an FP_REG_KILL instruction before 208 // the terminator of the block. 209 if (!Subtarget->hasSSE2()) { 210 // Note that FP stack instructions *are* used in SSE code when returning 211 // values, but these are not live out of the basic block, so we don't need 212 // an FP_REG_KILL in this case either. 213 bool ContainsFPCode = false; 214 215 // Scan all of the machine instructions in these MBBs, checking for FP 216 // stores. 217 MachineFunction::iterator MBBI = FirstMBB; 218 do { 219 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end(); 220 !ContainsFPCode && I != E; ++I) { 221 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { 222 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && 223 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && 224 RegMap->getRegClass(I->getOperand(0).getReg()) == 225 X86::RFPRegisterClass) { 226 ContainsFPCode = true; 227 break; 228 } 229 } 230 } 231 } while (!ContainsFPCode && &*(MBBI++) != BB); 232 233 // Check PHI nodes in successor blocks. These PHI's will be lowered to have 234 // a copy of the input value in this block. 235 if (!ContainsFPCode) { 236 // Final check, check LLVM BB's that are successors to the LLVM BB 237 // corresponding to BB for FP PHI nodes. 238 const BasicBlock *LLVMBB = BB->getBasicBlock(); 239 const PHINode *PN; 240 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); 241 !ContainsFPCode && SI != E; ++SI) { 242 for (BasicBlock::const_iterator II = SI->begin(); 243 (PN = dyn_cast<PHINode>(II)); ++II) { 244 if (PN->getType()->isFloatingPoint()) { 245 ContainsFPCode = true; 246 break; 247 } 248 } 249 } 250 } 251 252 // Finally, if we found any FP code, emit the FP_REG_KILL instruction. 253 if (ContainsFPCode) { 254 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); 255 ++NumFPKill; 256 } 257 } 258} 259 260/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in 261/// the main function. 262void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, 263 MachineFrameInfo *MFI) { 264 if (Subtarget->TargetType == X86Subtarget::isCygwin) 265 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main"); 266 267 // Switch the FPU to 64-bit precision mode for better compatibility and speed. 268 int CWFrameIdx = MFI->CreateStackObject(2, 2); 269 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); 270 271 // Set the high part to be 64-bit precision. 272 addFrameReference(BuildMI(BB, X86::MOV8mi, 5), 273 CWFrameIdx, 1).addImm(2); 274 275 // Reload the modified control word now. 276 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); 277} 278 279void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { 280 // If this is main, emit special code for main. 281 MachineBasicBlock *BB = MF.begin(); 282 if (Fn.hasExternalLinkage() && Fn.getName() == "main") 283 EmitSpecialCodeForMain(BB, MF.getFrameInfo()); 284} 285 286/// MatchAddress - Add the specified node to the specified addressing mode, 287/// returning true if it cannot be done. This just pattern matches for the 288/// addressing mode 289bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM, 290 bool isRoot) { 291 bool Available = false; 292 // If N has already been selected, reuse the result unless in some very 293 // specific cases. 294 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0)); 295 if (CGMI != CodeGenMap.end()) { 296 Available = true; 297 } 298 299 switch (N.getOpcode()) { 300 default: break; 301 case ISD::Constant: 302 AM.Disp += cast<ConstantSDNode>(N)->getValue(); 303 return false; 304 305 case X86ISD::Wrapper: 306 // If both base and index components have been picked, we can't fit 307 // the result available in the register in the addressing mode. Duplicate 308 // GlobalAddress or ConstantPool as displacement. 309 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) { 310 if (ConstantPoolSDNode *CP = 311 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) { 312 if (AM.CP == 0) { 313 AM.CP = CP->get(); 314 AM.Align = CP->getAlignment(); 315 AM.Disp += CP->getOffset(); 316 return false; 317 } 318 } else if (GlobalAddressSDNode *G = 319 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) { 320 if (AM.GV == 0) { 321 AM.GV = G->getGlobal(); 322 AM.Disp += G->getOffset(); 323 return false; 324 } 325 } 326 } 327 break; 328 329 case ISD::FrameIndex: 330 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { 331 AM.BaseType = X86ISelAddressMode::FrameIndexBase; 332 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); 333 return false; 334 } 335 break; 336 337 case ISD::SHL: 338 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1) 339 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { 340 unsigned Val = CN->getValue(); 341 if (Val == 1 || Val == 2 || Val == 3) { 342 AM.Scale = 1 << Val; 343 SDOperand ShVal = N.Val->getOperand(0); 344 345 // Okay, we know that we have a scale by now. However, if the scaled 346 // value is an add of something and a constant, we can fold the 347 // constant into the disp field here. 348 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && 349 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { 350 AM.IndexReg = ShVal.Val->getOperand(0); 351 ConstantSDNode *AddVal = 352 cast<ConstantSDNode>(ShVal.Val->getOperand(1)); 353 AM.Disp += AddVal->getValue() << Val; 354 } else { 355 AM.IndexReg = ShVal; 356 } 357 return false; 358 } 359 } 360 break; 361 362 case ISD::MUL: 363 // X*[3,5,9] -> X+X*[2,4,8] 364 if (!Available && 365 AM.BaseType == X86ISelAddressMode::RegBase && 366 AM.Base.Reg.Val == 0 && 367 AM.IndexReg.Val == 0) 368 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) 369 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { 370 AM.Scale = unsigned(CN->getValue())-1; 371 372 SDOperand MulVal = N.Val->getOperand(0); 373 SDOperand Reg; 374 375 // Okay, we know that we have a scale by now. However, if the scaled 376 // value is an add of something and a constant, we can fold the 377 // constant into the disp field here. 378 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && 379 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { 380 Reg = MulVal.Val->getOperand(0); 381 ConstantSDNode *AddVal = 382 cast<ConstantSDNode>(MulVal.Val->getOperand(1)); 383 AM.Disp += AddVal->getValue() * CN->getValue(); 384 } else { 385 Reg = N.Val->getOperand(0); 386 } 387 388 AM.IndexReg = AM.Base.Reg = Reg; 389 return false; 390 } 391 break; 392 393 case ISD::ADD: { 394 if (!Available) { 395 X86ISelAddressMode Backup = AM; 396 if (!MatchAddress(N.Val->getOperand(0), AM, false) && 397 !MatchAddress(N.Val->getOperand(1), AM, false)) 398 return false; 399 AM = Backup; 400 if (!MatchAddress(N.Val->getOperand(1), AM, false) && 401 !MatchAddress(N.Val->getOperand(0), AM, false)) 402 return false; 403 AM = Backup; 404 } 405 break; 406 } 407 408 case ISD::OR: { 409 if (!Available) { 410 X86ISelAddressMode Backup = AM; 411 // Look for (x << c1) | c2 where (c2 < c1) 412 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0)); 413 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) { 414 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { 415 AM.Disp = CN->getValue(); 416 return false; 417 } 418 } 419 AM = Backup; 420 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)); 421 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) { 422 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { 423 AM.Disp = CN->getValue(); 424 return false; 425 } 426 } 427 AM = Backup; 428 } 429 break; 430 } 431 } 432 433 // Is the base register already occupied? 434 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { 435 // If so, check to see if the scale index register is set. 436 if (AM.IndexReg.Val == 0) { 437 AM.IndexReg = N; 438 AM.Scale = 1; 439 return false; 440 } 441 442 // Otherwise, we cannot select it. 443 return true; 444 } 445 446 // Default, generate it as a register. 447 AM.BaseType = X86ISelAddressMode::RegBase; 448 AM.Base.Reg = N; 449 return false; 450} 451 452/// SelectAddr - returns true if it is able pattern match an addressing mode. 453/// It returns the operands which make up the maximal addressing mode it can 454/// match by reference. 455bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, 456 SDOperand &Index, SDOperand &Disp) { 457 X86ISelAddressMode AM; 458 if (MatchAddress(N, AM)) 459 return false; 460 461 if (AM.BaseType == X86ISelAddressMode::RegBase) { 462 if (!AM.Base.Reg.Val) 463 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); 464 } 465 466 if (!AM.IndexReg.Val) 467 AM.IndexReg = CurDAG->getRegister(0, MVT::i32); 468 469 getAddressOperands(AM, Base, Scale, Index, Disp); 470 471 return true; 472} 473 474/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing 475/// mode it matches can be cost effectively emitted as an LEA instruction. 476bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, 477 SDOperand &Scale, 478 SDOperand &Index, SDOperand &Disp) { 479 X86ISelAddressMode AM; 480 if (MatchAddress(N, AM)) 481 return false; 482 483 unsigned Complexity = 0; 484 if (AM.BaseType == X86ISelAddressMode::RegBase) 485 if (AM.Base.Reg.Val) 486 Complexity = 1; 487 else 488 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); 489 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) 490 Complexity = 4; 491 492 if (AM.IndexReg.Val) 493 Complexity++; 494 else 495 AM.IndexReg = CurDAG->getRegister(0, MVT::i32); 496 497 if (AM.Scale > 2) 498 Complexity += 2; 499 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg 500 else if (AM.Scale > 1) 501 Complexity++; 502 503 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA 504 // to a LEA. This is determined with some expermentation but is by no means 505 // optimal (especially for code size consideration). LEA is nice because of 506 // its three-address nature. Tweak the cost function again when we can run 507 // convertToThreeAddress() at register allocation time. 508 if (AM.GV || AM.CP) 509 Complexity += 2; 510 511 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val)) 512 Complexity++; 513 514 if (Complexity > 2) { 515 getAddressOperands(AM, Base, Scale, Index, Disp); 516 return true; 517 } 518 519 return false; 520} 521 522bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N, 523 SDOperand &Base, SDOperand &Scale, 524 SDOperand &Index, SDOperand &Disp) { 525 if (N.getOpcode() == ISD::LOAD && 526 N.hasOneUse() && 527 !CodeGenMap.count(N.getValue(0)) && 528 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val))) 529 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp); 530 return false; 531} 532 533static bool isRegister0(SDOperand Op) { 534 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) 535 return (R->getReg() == 0); 536 return false; 537} 538 539/// getGlobalBaseReg - Output the instructions required to put the 540/// base address to use for accessing globals into a register. 541/// 542SDOperand X86DAGToDAGISel::getGlobalBaseReg() { 543 if (!GlobalBaseReg) { 544 // Insert the set of GlobalBaseReg into the first MBB of the function 545 MachineBasicBlock &FirstMBB = BB->getParent()->front(); 546 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 547 SSARegMap *RegMap = BB->getParent()->getSSARegMap(); 548 // FIXME: when we get to LP64, we will need to create the appropriate 549 // type of register here. 550 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass); 551 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0); 552 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg); 553 } 554 return CurDAG->getRegister(GlobalBaseReg, MVT::i32); 555} 556 557static SDNode *FindCallStartFromCall(SDNode *Node) { 558 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; 559 assert(Node->getOperand(0).getValueType() == MVT::Other && 560 "Node doesn't have a token chain argument!"); 561 return FindCallStartFromCall(Node->getOperand(0).Val); 562} 563 564void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) { 565 SDNode *Node = N.Val; 566 MVT::ValueType NVT = Node->getValueType(0); 567 unsigned Opc, MOpc; 568 unsigned Opcode = Node->getOpcode(); 569 570#ifndef NDEBUG 571 DEBUG(std::cerr << std::string(Indent, ' ')); 572 DEBUG(std::cerr << "Selecting: "); 573 DEBUG(Node->dump(CurDAG)); 574 DEBUG(std::cerr << "\n"); 575 Indent += 2; 576#endif 577 578 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) { 579 Result = N; 580#ifndef NDEBUG 581 DEBUG(std::cerr << std::string(Indent-2, ' ')); 582 DEBUG(std::cerr << "== "); 583 DEBUG(Node->dump(CurDAG)); 584 DEBUG(std::cerr << "\n"); 585 Indent -= 2; 586#endif 587 return; // Already selected. 588 } 589 590 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N); 591 if (CGMI != CodeGenMap.end()) { 592 Result = CGMI->second; 593#ifndef NDEBUG 594 DEBUG(std::cerr << std::string(Indent-2, ' ')); 595 DEBUG(std::cerr << "== "); 596 DEBUG(Result.Val->dump(CurDAG)); 597 DEBUG(std::cerr << "\n"); 598 Indent -= 2; 599#endif 600 return; 601 } 602 603 switch (Opcode) { 604 default: break; 605 case X86ISD::GlobalBaseReg: 606 Result = getGlobalBaseReg(); 607 return; 608 609 case ISD::ADD: { 610 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd 611 // code and is matched first so to prevent it from being turned into 612 // LEA32r X+c. 613 SDOperand N0 = N.getOperand(0); 614 SDOperand N1 = N.getOperand(1); 615 if (N.Val->getValueType(0) == MVT::i32 && 616 N0.getOpcode() == X86ISD::Wrapper && 617 N1.getOpcode() == ISD::Constant) { 618 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue(); 619 SDOperand C(0, 0); 620 // TODO: handle ExternalSymbolSDNode. 621 if (GlobalAddressSDNode *G = 622 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { 623 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32, 624 G->getOffset() + Offset); 625 } else if (ConstantPoolSDNode *CP = 626 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { 627 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32, 628 CP->getAlignment(), 629 CP->getOffset()+Offset); 630 } 631 632 if (C.Val) { 633 if (N.Val->hasOneUse()) { 634 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C); 635 } else { 636 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C); 637 Result = CodeGenMap[N] = SDOperand(ResNode, 0); 638 } 639 return; 640 } 641 } 642 643 // Other cases are handled by auto-generated code. 644 break; 645 } 646 647 case ISD::MULHU: 648 case ISD::MULHS: { 649 if (Opcode == ISD::MULHU) 650 switch (NVT) { 651 default: assert(0 && "Unsupported VT!"); 652 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; 653 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; 654 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; 655 } 656 else 657 switch (NVT) { 658 default: assert(0 && "Unsupported VT!"); 659 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; 660 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; 661 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; 662 } 663 664 unsigned LoReg, HiReg; 665 switch (NVT) { 666 default: assert(0 && "Unsupported VT!"); 667 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; 668 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; 669 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; 670 } 671 672 SDOperand N0 = Node->getOperand(0); 673 SDOperand N1 = Node->getOperand(1); 674 675 bool foldedLoad = false; 676 SDOperand Tmp0, Tmp1, Tmp2, Tmp3; 677 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); 678 // MULHU and MULHS are commmutative 679 if (!foldedLoad) { 680 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); 681 if (foldedLoad) { 682 N0 = Node->getOperand(1); 683 N1 = Node->getOperand(0); 684 } 685 } 686 687 SDOperand Chain; 688 if (foldedLoad) 689 Select(Chain, N1.getOperand(0)); 690 else 691 Chain = CurDAG->getEntryNode(); 692 693 SDOperand InFlag(0, 0); 694 Select(N0, N0); 695 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), 696 N0, InFlag); 697 InFlag = Chain.getValue(1); 698 699 if (foldedLoad) { 700 Select(Tmp0, Tmp0); 701 Select(Tmp1, Tmp1); 702 Select(Tmp2, Tmp2); 703 Select(Tmp3, Tmp3); 704 SDNode *CNode = 705 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, 706 Tmp2, Tmp3, Chain, InFlag); 707 Chain = SDOperand(CNode, 0); 708 InFlag = SDOperand(CNode, 1); 709 } else { 710 Select(N1, N1); 711 InFlag = 712 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); 713 } 714 715 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag); 716 CodeGenMap[N.getValue(0)] = Result; 717 if (foldedLoad) { 718 CodeGenMap[N1.getValue(1)] = Result.getValue(1); 719 AddHandleReplacement(N1.Val, 1, Result.Val, 1); 720 } 721 722#ifndef NDEBUG 723 DEBUG(std::cerr << std::string(Indent-2, ' ')); 724 DEBUG(std::cerr << "== "); 725 DEBUG(Result.Val->dump(CurDAG)); 726 DEBUG(std::cerr << "\n"); 727 Indent -= 2; 728#endif 729 return; 730 } 731 732 case ISD::SDIV: 733 case ISD::UDIV: 734 case ISD::SREM: 735 case ISD::UREM: { 736 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM; 737 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV; 738 if (!isSigned) 739 switch (NVT) { 740 default: assert(0 && "Unsupported VT!"); 741 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; 742 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; 743 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; 744 } 745 else 746 switch (NVT) { 747 default: assert(0 && "Unsupported VT!"); 748 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; 749 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; 750 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; 751 } 752 753 unsigned LoReg, HiReg; 754 unsigned ClrOpcode, SExtOpcode; 755 switch (NVT) { 756 default: assert(0 && "Unsupported VT!"); 757 case MVT::i8: 758 LoReg = X86::AL; HiReg = X86::AH; 759 ClrOpcode = X86::MOV8r0; 760 SExtOpcode = X86::CBW; 761 break; 762 case MVT::i16: 763 LoReg = X86::AX; HiReg = X86::DX; 764 ClrOpcode = X86::MOV16r0; 765 SExtOpcode = X86::CWD; 766 break; 767 case MVT::i32: 768 LoReg = X86::EAX; HiReg = X86::EDX; 769 ClrOpcode = X86::MOV32r0; 770 SExtOpcode = X86::CDQ; 771 break; 772 } 773 774 SDOperand N0 = Node->getOperand(0); 775 SDOperand N1 = Node->getOperand(1); 776 777 bool foldedLoad = false; 778 SDOperand Tmp0, Tmp1, Tmp2, Tmp3; 779 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); 780 SDOperand Chain; 781 if (foldedLoad) 782 Select(Chain, N1.getOperand(0)); 783 else 784 Chain = CurDAG->getEntryNode(); 785 786 SDOperand InFlag(0, 0); 787 Select(N0, N0); 788 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), 789 N0, InFlag); 790 InFlag = Chain.getValue(1); 791 792 if (isSigned) { 793 // Sign extend the low part into the high part. 794 InFlag = 795 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); 796 } else { 797 // Zero out the high part, effectively zero extending the input. 798 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0); 799 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT), 800 ClrNode, InFlag); 801 InFlag = Chain.getValue(1); 802 } 803 804 if (foldedLoad) { 805 Select(Tmp0, Tmp0); 806 Select(Tmp1, Tmp1); 807 Select(Tmp2, Tmp2); 808 Select(Tmp3, Tmp3); 809 SDNode *CNode = 810 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, 811 Tmp2, Tmp3, Chain, InFlag); 812 Chain = SDOperand(CNode, 0); 813 InFlag = SDOperand(CNode, 1); 814 } else { 815 Select(N1, N1); 816 InFlag = 817 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); 818 } 819 820 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, 821 NVT, InFlag); 822 CodeGenMap[N.getValue(0)] = Result; 823 if (foldedLoad) { 824 CodeGenMap[N1.getValue(1)] = Result.getValue(1); 825 AddHandleReplacement(N1.Val, 1, Result.Val, 1); 826 } 827 828#ifndef NDEBUG 829 DEBUG(std::cerr << std::string(Indent-2, ' ')); 830 DEBUG(std::cerr << "== "); 831 DEBUG(Result.Val->dump(CurDAG)); 832 DEBUG(std::cerr << "\n"); 833 Indent -= 2; 834#endif 835 return; 836 } 837 838 case ISD::TRUNCATE: { 839 if (NVT == MVT::i8) { 840 unsigned Opc2; 841 MVT::ValueType VT; 842 switch (Node->getOperand(0).getValueType()) { 843 default: assert(0 && "Unknown truncate!"); 844 case MVT::i16: 845 Opc = X86::MOV16to16_; 846 VT = MVT::i16; 847 Opc2 = X86::TRUNC_GR16_GR8; 848 break; 849 case MVT::i32: 850 Opc = X86::MOV32to32_; 851 VT = MVT::i32; 852 Opc2 = X86::TRUNC_GR32_GR8; 853 break; 854 } 855 856 SDOperand Tmp0, Tmp1; 857 Select(Tmp0, Node->getOperand(0)); 858 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0); 859 Result = CodeGenMap[N] = 860 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0); 861 862#ifndef NDEBUG 863 DEBUG(std::cerr << std::string(Indent-2, ' ')); 864 DEBUG(std::cerr << "== "); 865 DEBUG(Result.Val->dump(CurDAG)); 866 DEBUG(std::cerr << "\n"); 867 Indent -= 2; 868#endif 869 return; 870 } 871 872 break; 873 } 874 } 875 876 SelectCode(Result, N); 877#ifndef NDEBUG 878 DEBUG(std::cerr << std::string(Indent-2, ' ')); 879 DEBUG(std::cerr << "=> "); 880 DEBUG(Result.Val->dump(CurDAG)); 881 DEBUG(std::cerr << "\n"); 882 Indent -= 2; 883#endif 884} 885 886bool X86DAGToDAGISel:: 887SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode, 888 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){ 889 SDOperand Op0, Op1, Op2, Op3; 890 switch (ConstraintCode) { 891 case 'o': // offsetable ?? 892 case 'v': // not offsetable ?? 893 default: return true; 894 case 'm': // memory 895 if (!SelectAddr(Op, Op0, Op1, Op2, Op3)) 896 return true; 897 break; 898 } 899 900 OutOps.resize(4); 901 Select(OutOps[0], Op0); 902 Select(OutOps[1], Op1); 903 Select(OutOps[2], Op2); 904 Select(OutOps[3], Op3); 905 return false; 906} 907 908/// createX86ISelDag - This pass converts a legalized DAG into a 909/// X86-specific DAG, ready for instruction scheduling. 910/// 911FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) { 912 return new X86DAGToDAGISel(TM); 913} 914