1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DAG pattern matching instruction selector for X86,
11 // converting from a legalized dag to a X86 dag.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "isel"
17 #include "X86InstrBuilder.h"
18 #include "X86RegisterInfo.h"
19 #include "X86Subtarget.h"
20 #include "X86ISelLowering.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/SSARegMap.h"
29 #include "llvm/CodeGen/SelectionDAGISel.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/ADT/Statistic.h"
37 //===----------------------------------------------------------------------===//
38 // Pattern Matcher Implementation
39 //===----------------------------------------------------------------------===//
42 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
43 /// SDOperand's instead of register numbers for the leaves of the matched
45 struct X86ISelAddressMode {
51 struct { // This is really a union, discriminated by BaseType!
61 unsigned Align; // CP alignment.
64 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
72 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
74 //===--------------------------------------------------------------------===//
75 /// ISel - X86 specific code to select X86 machine instructions for
76 /// SelectionDAG operations.
78 class X86DAGToDAGISel : public SelectionDAGISel {
79 /// ContainsFPCode - Every instruction we select that uses or defines a FP
80 /// register should set this to true.
83 /// X86Lowering - This object fully describes how to lower LLVM code to an
84 /// X86-specific SelectionDAG.
85 X86TargetLowering X86Lowering;
87 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
88 /// make the right decision when generating code for different targets.
89 const X86Subtarget *Subtarget;
91 unsigned GlobalBaseReg;
93 X86DAGToDAGISel(TargetMachine &TM)
94 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
95 Subtarget = &TM.getSubtarget<X86Subtarget>();
98 virtual bool runOnFunction(Function &Fn) {
99 // Make sure we re-emit a set of the global base reg if necessary
101 return SelectionDAGISel::runOnFunction(Fn);
104 virtual const char *getPassName() const {
105 return "X86 DAG->DAG Instruction Selection";
108 /// InstructionSelectBasicBlock - This callback is invoked by
109 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
110 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
112 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
114 // Include the pieces autogenerated from the target description.
115 #include "X86GenDAGISel.inc"
118 void Select(SDOperand &Result, SDOperand N);
120 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
121 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
122 SDOperand &Index, SDOperand &Disp);
123 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
124 SDOperand &Index, SDOperand &Disp);
125 bool TryFoldLoad(SDOperand P, SDOperand N,
126 SDOperand &Base, SDOperand &Scale,
127 SDOperand &Index, SDOperand &Disp);
129 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
130 SDOperand &Scale, SDOperand &Index,
132 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
133 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
134 Scale = getI8Imm(AM.Scale);
136 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
138 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
139 : getI32Imm(AM.Disp));
142 /// getI8Imm - Return a target constant with the specified value, of type
144 inline SDOperand getI8Imm(unsigned Imm) {
145 return CurDAG->getTargetConstant(Imm, MVT::i8);
148 /// getI16Imm - Return a target constant with the specified value, of type
150 inline SDOperand getI16Imm(unsigned Imm) {
151 return CurDAG->getTargetConstant(Imm, MVT::i16);
154 /// getI32Imm - Return a target constant with the specified value, of type
156 inline SDOperand getI32Imm(unsigned Imm) {
157 return CurDAG->getTargetConstant(Imm, MVT::i32);
160 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
161 /// base register. Return the virtual register that holds this value.
162 SDOperand getGlobalBaseReg();
170 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
171 /// when it has created a SelectionDAG for us to codegen.
172 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
174 MachineFunction::iterator FirstMBB = BB;
176 // Codegen the basic block.
178 DEBUG(std::cerr << "===== Instruction selection begins:\n");
181 DAG.setRoot(SelectRoot(DAG.getRoot()));
183 DEBUG(std::cerr << "===== Instruction selection ends:\n");
186 DAG.RemoveDeadNodes();
188 // Emit machine code to BB.
189 ScheduleAndEmitDAG(DAG);
191 // If we are emitting FP stack code, scan the basic block to determine if this
192 // block defines any FP values. If so, put an FP_REG_KILL instruction before
193 // the terminator of the block.
194 if (!Subtarget->hasSSE2()) {
195 // Note that FP stack instructions *are* used in SSE code when returning
196 // values, but these are not live out of the basic block, so we don't need
197 // an FP_REG_KILL in this case either.
198 bool ContainsFPCode = false;
200 // Scan all of the machine instructions in these MBBs, checking for FP
202 MachineFunction::iterator MBBI = FirstMBB;
204 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
205 !ContainsFPCode && I != E; ++I) {
206 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
207 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
208 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
209 RegMap->getRegClass(I->getOperand(0).getReg()) ==
210 X86::RFPRegisterClass) {
211 ContainsFPCode = true;
216 } while (!ContainsFPCode && &*(MBBI++) != BB);
218 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
219 // a copy of the input value in this block.
220 if (!ContainsFPCode) {
221 // Final check, check LLVM BB's that are successors to the LLVM BB
222 // corresponding to BB for FP PHI nodes.
223 const BasicBlock *LLVMBB = BB->getBasicBlock();
225 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
226 !ContainsFPCode && SI != E; ++SI) {
227 for (BasicBlock::const_iterator II = SI->begin();
228 (PN = dyn_cast<PHINode>(II)); ++II) {
229 if (PN->getType()->isFloatingPoint()) {
230 ContainsFPCode = true;
237 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
238 if (ContainsFPCode) {
239 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
245 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
246 /// the main function.
247 static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
248 MachineFrameInfo *MFI) {
249 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
250 int CWFrameIdx = MFI->CreateStackObject(2, 2);
251 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
253 // Set the high part to be 64-bit precision.
254 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
255 CWFrameIdx, 1).addImm(2);
257 // Reload the modified control word now.
258 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
261 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
262 // If this is main, emit special code for main.
263 MachineBasicBlock *BB = MF.begin();
264 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
265 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
268 /// MatchAddress - Add the specified node to the specified addressing mode,
269 /// returning true if it cannot be done. This just pattern matches for the
271 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
273 bool Available = false;
274 // If N has already been selected, reuse the result unless in some very
276 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
277 if (CGMI != CodeGenMap.end()) {
281 switch (N.getOpcode()) {
284 AM.Disp += cast<ConstantSDNode>(N)->getValue();
287 case X86ISD::Wrapper:
288 // If both base and index components have been picked, we can't fit
289 // the result available in the register in the addressing mode. Duplicate
290 // GlobalAddress or ConstantPool as displacement.
291 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
292 if (ConstantPoolSDNode *CP =
293 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
296 AM.Align = CP->getAlignment();
297 AM.Disp += CP->getOffset();
300 } else if (GlobalAddressSDNode *G =
301 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
303 AM.GV = G->getGlobal();
304 AM.Disp += G->getOffset();
311 case ISD::FrameIndex:
312 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
313 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
314 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
320 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
321 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
322 unsigned Val = CN->getValue();
323 if (Val == 1 || Val == 2 || Val == 3) {
325 SDOperand ShVal = N.Val->getOperand(0);
327 // Okay, we know that we have a scale by now. However, if the scaled
328 // value is an add of something and a constant, we can fold the
329 // constant into the disp field here.
330 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
331 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
332 AM.IndexReg = ShVal.Val->getOperand(0);
333 ConstantSDNode *AddVal =
334 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
335 AM.Disp += AddVal->getValue() << Val;
345 // X*[3,5,9] -> X+X*[2,4,8]
347 AM.BaseType == X86ISelAddressMode::RegBase &&
348 AM.Base.Reg.Val == 0 &&
349 AM.IndexReg.Val == 0)
350 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
351 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
352 AM.Scale = unsigned(CN->getValue())-1;
354 SDOperand MulVal = N.Val->getOperand(0);
357 // Okay, we know that we have a scale by now. However, if the scaled
358 // value is an add of something and a constant, we can fold the
359 // constant into the disp field here.
360 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
361 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
362 Reg = MulVal.Val->getOperand(0);
363 ConstantSDNode *AddVal =
364 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
365 AM.Disp += AddVal->getValue() * CN->getValue();
367 Reg = N.Val->getOperand(0);
370 AM.IndexReg = AM.Base.Reg = Reg;
377 X86ISelAddressMode Backup = AM;
378 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
379 !MatchAddress(N.Val->getOperand(1), AM, false))
382 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
383 !MatchAddress(N.Val->getOperand(0), AM, false))
391 // Is the base register already occupied?
392 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
393 // If so, check to see if the scale index register is set.
394 if (AM.IndexReg.Val == 0) {
400 // Otherwise, we cannot select it.
404 // Default, generate it as a register.
405 AM.BaseType = X86ISelAddressMode::RegBase;
410 /// SelectAddr - returns true if it is able pattern match an addressing mode.
411 /// It returns the operands which make up the maximal addressing mode it can
412 /// match by reference.
413 bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
414 SDOperand &Index, SDOperand &Disp) {
415 X86ISelAddressMode AM;
416 if (MatchAddress(N, AM))
419 if (AM.BaseType == X86ISelAddressMode::RegBase) {
420 if (!AM.Base.Reg.Val)
421 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
424 if (!AM.IndexReg.Val)
425 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
427 getAddressOperands(AM, Base, Scale, Index, Disp);
432 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
433 /// mode it matches can be cost effectively emitted as an LEA instruction.
434 /// For X86, it always is unless it's just a (Reg + const).
435 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
437 SDOperand &Index, SDOperand &Disp) {
438 X86ISelAddressMode AM;
439 if (MatchAddress(N, AM))
442 unsigned Complexity = 0;
443 if (AM.BaseType == X86ISelAddressMode::RegBase)
447 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
448 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
454 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
459 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
460 // to a LEA. This is determined with some expermentation but is by no means
461 // optimal (especially for code size consideration). LEA is nice because of
462 // its three-address nature. Tweak the cost function again when we can run
463 // convertToThreeAddress() at register allocation time.
467 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
470 if (Complexity > 2) {
471 getAddressOperands(AM, Base, Scale, Index, Disp);
478 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
479 SDOperand &Base, SDOperand &Scale,
480 SDOperand &Index, SDOperand &Disp) {
481 if (N.getOpcode() == ISD::LOAD &&
483 !CodeGenMap.count(N.getValue(0)) &&
484 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
485 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
489 static bool isRegister0(SDOperand Op) {
490 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
491 return (R->getReg() == 0);
495 /// getGlobalBaseReg - Output the instructions required to put the
496 /// base address to use for accessing globals into a register.
498 SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
499 if (!GlobalBaseReg) {
500 // Insert the set of GlobalBaseReg into the first MBB of the function
501 MachineBasicBlock &FirstMBB = BB->getParent()->front();
502 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
503 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
504 // FIXME: when we get to LP64, we will need to create the appropriate
505 // type of register here.
506 GlobalBaseReg = RegMap->createVirtualRegister(X86::R32RegisterClass);
507 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
508 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
510 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
513 void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
514 SDNode *Node = N.Val;
515 MVT::ValueType NVT = Node->getValueType(0);
517 unsigned Opcode = Node->getOpcode();
520 DEBUG(std::cerr << std::string(Indent, ' '));
521 DEBUG(std::cerr << "Selecting: ");
522 DEBUG(Node->dump(CurDAG));
523 DEBUG(std::cerr << "\n");
527 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
530 DEBUG(std::cerr << std::string(Indent-2, ' '));
531 DEBUG(std::cerr << "== ");
532 DEBUG(Node->dump(CurDAG));
533 DEBUG(std::cerr << "\n");
536 return; // Already selected.
539 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
540 if (CGMI != CodeGenMap.end()) {
541 Result = CGMI->second;
543 DEBUG(std::cerr << std::string(Indent-2, ' '));
544 DEBUG(std::cerr << "== ");
545 DEBUG(Result.Val->dump(CurDAG));
546 DEBUG(std::cerr << "\n");
554 case X86ISD::GlobalBaseReg:
555 Result = getGlobalBaseReg();
559 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
560 // code and is matched first so to prevent it from being turned into
562 SDOperand N0 = N.getOperand(0);
563 SDOperand N1 = N.getOperand(1);
564 if (N.Val->getValueType(0) == MVT::i32 &&
565 N0.getOpcode() == X86ISD::Wrapper &&
566 N1.getOpcode() == ISD::Constant) {
567 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
569 // TODO: handle ExternalSymbolSDNode.
570 if (GlobalAddressSDNode *G =
571 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
572 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
573 G->getOffset() + Offset);
574 } else if (ConstantPoolSDNode *CP =
575 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
576 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
578 CP->getOffset()+Offset);
582 if (N.Val->hasOneUse()) {
583 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
585 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
586 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
592 // Other cases are handled by auto-generated code.
598 if (Opcode == ISD::MULHU)
600 default: assert(0 && "Unsupported VT!");
601 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
602 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
603 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
607 default: assert(0 && "Unsupported VT!");
608 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
609 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
610 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
613 unsigned LoReg, HiReg;
615 default: assert(0 && "Unsupported VT!");
616 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
617 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
618 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
621 SDOperand N0 = Node->getOperand(0);
622 SDOperand N1 = Node->getOperand(1);
624 bool foldedLoad = false;
625 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
626 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
627 // MULHU and MULHS are commmutative
629 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
631 N0 = Node->getOperand(1);
632 N1 = Node->getOperand(0);
638 Select(Chain, N1.getOperand(0));
640 Chain = CurDAG->getEntryNode();
642 SDOperand InFlag(0, 0);
644 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
646 InFlag = Chain.getValue(1);
654 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
655 Tmp2, Tmp3, Chain, InFlag);
656 Chain = SDOperand(CNode, 0);
657 InFlag = SDOperand(CNode, 1);
661 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
664 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
665 CodeGenMap[N.getValue(0)] = Result;
667 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
668 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
672 DEBUG(std::cerr << std::string(Indent-2, ' '));
673 DEBUG(std::cerr << "== ");
674 DEBUG(Result.Val->dump(CurDAG));
675 DEBUG(std::cerr << "\n");
685 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
686 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
689 default: assert(0 && "Unsupported VT!");
690 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
691 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
692 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
696 default: assert(0 && "Unsupported VT!");
697 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
698 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
699 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
702 unsigned LoReg, HiReg;
703 unsigned ClrOpcode, SExtOpcode;
705 default: assert(0 && "Unsupported VT!");
707 LoReg = X86::AL; HiReg = X86::AH;
708 ClrOpcode = X86::MOV8ri;
709 SExtOpcode = X86::CBW;
712 LoReg = X86::AX; HiReg = X86::DX;
713 ClrOpcode = X86::MOV16ri;
714 SExtOpcode = X86::CWD;
717 LoReg = X86::EAX; HiReg = X86::EDX;
718 ClrOpcode = X86::MOV32ri;
719 SExtOpcode = X86::CDQ;
723 SDOperand N0 = Node->getOperand(0);
724 SDOperand N1 = Node->getOperand(1);
726 bool foldedLoad = false;
727 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
728 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
731 Select(Chain, N1.getOperand(0));
733 Chain = CurDAG->getEntryNode();
735 SDOperand InFlag(0, 0);
737 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
739 InFlag = Chain.getValue(1);
742 // Sign extend the low part into the high part.
744 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
746 // Zero out the high part, effectively zero extending the input.
748 SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT,
749 CurDAG->getTargetConstant(0, NVT)), 0);
750 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
752 InFlag = Chain.getValue(1);
761 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
762 Tmp2, Tmp3, Chain, InFlag);
763 Chain = SDOperand(CNode, 0);
764 InFlag = SDOperand(CNode, 1);
768 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
771 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
773 CodeGenMap[N.getValue(0)] = Result;
775 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
776 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
780 DEBUG(std::cerr << std::string(Indent-2, ' '));
781 DEBUG(std::cerr << "== ");
782 DEBUG(Result.Val->dump(CurDAG));
783 DEBUG(std::cerr << "\n");
789 case ISD::TRUNCATE: {
792 switch (Node->getOperand(0).getValueType()) {
793 default: assert(0 && "Unknown truncate!");
794 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
795 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
797 SDOperand Tmp0, Tmp1;
798 Select(Tmp0, Node->getOperand(0));
799 Select(Tmp1, SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0));
800 SDOperand InFlag = SDOperand(0,0);
801 Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(), Reg, Tmp1, InFlag);
802 SDOperand Chain = Result.getValue(0);
803 InFlag = Result.getValue(1);
806 default: assert(0 && "Unknown truncate!");
807 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
808 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
811 Result = CurDAG->getCopyFromReg(Chain, Reg, VT, InFlag);
812 if (N.Val->hasOneUse())
813 Result = CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
815 Result = CodeGenMap[N] =
816 SDOperand(CurDAG->getTargetNode(Opc, VT, Result), 0);
819 DEBUG(std::cerr << std::string(Indent-2, ' '));
820 DEBUG(std::cerr << "== ");
821 DEBUG(Result.Val->dump(CurDAG));
822 DEBUG(std::cerr << "\n");
829 SelectCode(Result, N);
831 DEBUG(std::cerr << std::string(Indent-2, ' '));
832 DEBUG(std::cerr << "=> ");
833 DEBUG(Result.Val->dump(CurDAG));
834 DEBUG(std::cerr << "\n");
839 /// createX86ISelDag - This pass converts a legalized DAG into a
840 /// X86-specific DAG, ready for instruction scheduling.
842 FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
843 return new X86DAGToDAGISel(TM);