1 //===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for X86.
12 //===----------------------------------------------------------------------===//
15 #include "X86InstrBuilder.h"
16 #include "X86RegisterInfo.h"
17 #include "X86Subtarget.h"
18 #include "X86ISelLowering.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Function.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/CodeGen/SSARegMap.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Support/CFG.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/ADT/Statistic.h"
40 //===----------------------------------------------------------------------===//
41 // Pattern Matcher Implementation
42 //===----------------------------------------------------------------------===//
45 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
46 /// SDOperand's instead of register numbers for the leaves of the matched
48 struct X86ISelAddressMode {
54 struct { // This is really a union, discriminated by BaseType!
65 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
73 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
75 //===--------------------------------------------------------------------===//
76 /// ISel - X86 specific code to select X86 machine instructions for
77 /// SelectionDAG operations.
79 class ISel : public SelectionDAGISel {
80 /// ContainsFPCode - Every instruction we select that uses or defines a FP
81 /// register should set this to true.
84 /// X86Lowering - This object fully describes how to lower LLVM code to an
85 /// X86-specific SelectionDAG.
86 X86TargetLowering X86Lowering;
88 /// RegPressureMap - This keeps an approximate count of the number of
89 /// registers required to evaluate each node in the graph.
90 std::map<SDNode*, unsigned> RegPressureMap;
92 /// ExprMap - As shared expressions are codegen'd, we keep track of which
93 /// vreg the value is produced in, so we only emit one copy of each compiled
95 std::map<SDOperand, unsigned> ExprMap;
97 /// TheDAG - The DAG being selected during Select* operations.
100 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
101 /// make the right decision when generating code for different targets.
102 const X86Subtarget *Subtarget;
104 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
105 Subtarget = &TM.getSubtarget<X86Subtarget>();
108 virtual const char *getPassName() const {
109 return "X86 Pattern Instruction Selection";
112 unsigned getRegPressure(SDOperand O) {
113 return RegPressureMap[O.Val];
115 unsigned ComputeRegPressure(SDOperand O);
117 /// InstructionSelectBasicBlock - This callback is invoked by
118 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
119 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
121 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
123 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
124 bool FloatPromoteOk = false);
125 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
126 bool TryToFoldLoadOpStore(SDNode *Node);
127 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
128 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
129 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
130 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
131 MVT::ValueType SVT, unsigned RDest);
132 unsigned SelectExpr(SDOperand N);
134 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
135 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
136 void SelectAddress(SDOperand N, X86AddressMode &AM);
137 bool EmitPotentialTailCall(SDNode *Node);
138 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
139 void Select(SDOperand N);
143 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
144 /// the main function.
145 static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
146 MachineFrameInfo *MFI) {
147 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
148 int CWFrameIdx = MFI->CreateStackObject(2, 2);
149 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
151 // Set the high part to be 64-bit precision.
152 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
153 CWFrameIdx, 1).addImm(2);
155 // Reload the modified control word now.
156 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
159 void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
160 // If this is main, emit special code for main.
161 MachineBasicBlock *BB = MF.begin();
162 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
163 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
167 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
168 /// when it has created a SelectionDAG for us to codegen.
169 void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
170 // While we're doing this, keep track of whether we see any FP code for
171 // FP_REG_KILL insertion.
172 ContainsFPCode = false;
173 MachineFunction *MF = BB->getParent();
175 // Scan the PHI nodes that already are inserted into this basic block. If any
176 // of them is a PHI of a floating point value, we need to insert an
178 SSARegMap *RegMap = MF->getSSARegMap();
179 if (BB != MF->begin())
180 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
182 assert(I->getOpcode() == X86::PHI &&
183 "Isn't just PHI nodes?");
184 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
185 X86::RFPRegisterClass) {
186 ContainsFPCode = true;
191 // Compute the RegPressureMap, which is an approximation for the number of
192 // registers required to compute each node.
193 ComputeRegPressure(DAG.getRoot());
197 // Codegen the basic block.
198 Select(DAG.getRoot());
202 // Finally, look at all of the successors of this block. If any contain a PHI
203 // node of FP type, we need to insert an FP_REG_KILL in this block.
204 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
205 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
206 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
207 I != E && I->getOpcode() == X86::PHI; ++I) {
208 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
209 X86::RFPRegisterClass) {
210 ContainsFPCode = true;
215 // Final check, check LLVM BB's that are successors to the LLVM BB
216 // corresponding to BB for FP PHI nodes.
217 const BasicBlock *LLVMBB = BB->getBasicBlock();
220 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
221 SI != E && !ContainsFPCode; ++SI)
222 for (BasicBlock::const_iterator II = SI->begin();
223 (PN = dyn_cast<PHINode>(II)); ++II)
224 if (PN->getType()->isFloatingPoint()) {
225 ContainsFPCode = true;
229 // Insert FP_REG_KILL instructions into basic blocks that need them. This
230 // only occurs due to the floating point stackifier not being aggressive
231 // enough to handle arbitrary global stackification.
233 // Currently we insert an FP_REG_KILL instruction into each block that uses or
234 // defines a floating point virtual register.
236 // When the global register allocators (like linear scan) finally update live
237 // variable analysis, we can keep floating point values in registers across
238 // basic blocks. This will be a huge win, but we are waiting on the global
239 // allocators before we can do this.
241 if (ContainsFPCode) {
242 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
246 // Clear state used for selection.
248 RegPressureMap.clear();
252 // ComputeRegPressure - Compute the RegPressureMap, which is an approximation
253 // for the number of registers required to compute each node. This is basically
254 // computing a generalized form of the Sethi-Ullman number for each node.
255 unsigned ISel::ComputeRegPressure(SDOperand O) {
257 unsigned &Result = RegPressureMap[N];
258 if (Result) return Result;
260 // FIXME: Should operations like CALL (which clobber lots o regs) have a
261 // higher fixed cost??
263 if (N->getNumOperands() == 0) {
266 unsigned MaxRegUse = 0;
267 unsigned NumExtraMaxRegUsers = 0;
268 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
270 if (N->getOperand(i).getOpcode() == ISD::Constant)
273 Regs = ComputeRegPressure(N->getOperand(i));
274 if (Regs > MaxRegUse) {
276 NumExtraMaxRegUsers = 0;
277 } else if (Regs == MaxRegUse &&
278 N->getOperand(i).getValueType() != MVT::Other) {
279 ++NumExtraMaxRegUsers;
283 if (O.getOpcode() != ISD::TokenFactor)
284 Result = MaxRegUse+NumExtraMaxRegUsers;
286 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
289 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
293 /// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
294 /// The DAG cannot have cycles in it, by definition, so the visited set is not
295 /// needed to prevent infinite loops. The DAG CAN, however, have unbounded
296 /// reuse, so it prevents exponential cases.
298 static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
299 std::set<SDNode*> &Visited) {
300 if (N == Op) return true; // Found it.
301 SDNode *Node = N.Val;
302 if (Node->getNumOperands() == 0 || // Leaf?
303 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
304 if (!Visited.insert(Node).second) return false; // Already visited?
306 // Recurse for the first N-1 operands.
307 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
308 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
311 // Tail recurse for the last operand.
312 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
315 X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
316 X86AddressMode Result;
318 // If we need to emit two register operands, emit the one with the highest
319 // register pressure first.
320 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
321 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
322 bool EmitBaseThenIndex;
323 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
324 std::set<SDNode*> Visited;
325 EmitBaseThenIndex = true;
326 // If Base ends up pointing to Index, we must emit index first. This is
327 // because of the way we fold loads, we may end up doing bad things with
329 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
330 EmitBaseThenIndex = false;
332 std::set<SDNode*> Visited;
333 EmitBaseThenIndex = false;
334 // If Base ends up pointing to Index, we must emit index first. This is
335 // because of the way we fold loads, we may end up doing bad things with
337 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
338 EmitBaseThenIndex = true;
341 if (EmitBaseThenIndex) {
342 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
343 Result.IndexReg = SelectExpr(IAM.IndexReg);
345 Result.IndexReg = SelectExpr(IAM.IndexReg);
346 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
349 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
350 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
351 } else if (IAM.IndexReg.Val) {
352 Result.IndexReg = SelectExpr(IAM.IndexReg);
355 switch (IAM.BaseType) {
356 case X86ISelAddressMode::RegBase:
357 Result.BaseType = X86AddressMode::RegBase;
359 case X86ISelAddressMode::FrameIndexBase:
360 Result.BaseType = X86AddressMode::FrameIndexBase;
361 Result.Base.FrameIndex = IAM.Base.FrameIndex;
364 assert(0 && "Unknown base type!");
367 Result.Scale = IAM.Scale;
368 Result.Disp = IAM.Disp;
373 /// SelectAddress - Pattern match the maximal addressing mode for this node and
374 /// emit all of the leaf registers.
375 void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
376 X86ISelAddressMode IAM;
377 MatchAddress(N, IAM);
378 AM = SelectAddrExprs(IAM);
381 /// MatchAddress - Add the specified node to the specified addressing mode,
382 /// returning true if it cannot be done. This just pattern matches for the
383 /// addressing mode, it does not cause any code to be emitted. For that, use
385 bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
386 switch (N.getOpcode()) {
388 case ISD::FrameIndex:
389 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
390 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
391 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
395 case ISD::GlobalAddress:
397 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
398 // For Darwin, external and weak symbols are indirect, so we want to load
399 // the value at address GV, not the value of GV itself. This means that
400 // the GlobalAddress must be in the base or index register of the address,
401 // not the GV offset field.
402 if (Subtarget->getIndirectExternAndWeakGlobals() &&
403 (GV->hasWeakLinkage() || GV->isExternal())) {
412 AM.Disp += cast<ConstantSDNode>(N)->getValue();
415 // We might have folded the load into this shift, so don't regen the value
417 if (ExprMap.count(N)) break;
419 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
420 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
421 unsigned Val = CN->getValue();
422 if (Val == 1 || Val == 2 || Val == 3) {
424 SDOperand ShVal = N.Val->getOperand(0);
426 // Okay, we know that we have a scale by now. However, if the scaled
427 // value is an add of something and a constant, we can fold the
428 // constant into the disp field here.
429 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
430 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
431 AM.IndexReg = ShVal.Val->getOperand(0);
432 ConstantSDNode *AddVal =
433 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
434 AM.Disp += AddVal->getValue() << Val;
443 // We might have folded the load into this mul, so don't regen the value if
445 if (ExprMap.count(N)) break;
447 // X*[3,5,9] -> X+X*[2,4,8]
448 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
449 AM.Base.Reg.Val == 0)
450 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
451 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
452 AM.Scale = unsigned(CN->getValue())-1;
454 SDOperand MulVal = N.Val->getOperand(0);
457 // Okay, we know that we have a scale by now. However, if the scaled
458 // value is an add of something and a constant, we can fold the
459 // constant into the disp field here.
460 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
461 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
462 Reg = MulVal.Val->getOperand(0);
463 ConstantSDNode *AddVal =
464 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
465 AM.Disp += AddVal->getValue() * CN->getValue();
467 Reg = N.Val->getOperand(0);
470 AM.IndexReg = AM.Base.Reg = Reg;
476 // We might have folded the load into this mul, so don't regen the value if
478 if (ExprMap.count(N)) break;
480 X86ISelAddressMode Backup = AM;
481 if (!MatchAddress(N.Val->getOperand(0), AM) &&
482 !MatchAddress(N.Val->getOperand(1), AM))
485 if (!MatchAddress(N.Val->getOperand(1), AM) &&
486 !MatchAddress(N.Val->getOperand(0), AM))
493 // Is the base register already occupied?
494 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
495 // If so, check to see if the scale index register is set.
496 if (AM.IndexReg.Val == 0) {
502 // Otherwise, we cannot select it.
506 // Default, generate it as a register.
507 AM.BaseType = X86ISelAddressMode::RegBase;
512 /// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
513 /// assuming that the temporary registers are in the 8-bit register class.
517 /// DestReg = logicalop Tmp1, Tmp2
519 static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
520 unsigned SetCC2, unsigned LogicalOp,
522 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
523 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
524 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
525 BuildMI(BB, SetCC1, 0, Tmp1);
526 BuildMI(BB, SetCC2, 0, Tmp2);
527 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
530 /// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
531 /// condition codes match the specified SetCCOpcode. Note that some conditions
532 /// require multiple instructions to generate the correct value.
533 static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
534 ISD::CondCode SetCCOpcode, bool isFP) {
537 switch (SetCCOpcode) {
538 default: assert(0 && "Illegal integer SetCC!");
539 case ISD::SETEQ: Opc = X86::SETEr; break;
540 case ISD::SETGT: Opc = X86::SETGr; break;
541 case ISD::SETGE: Opc = X86::SETGEr; break;
542 case ISD::SETLT: Opc = X86::SETLr; break;
543 case ISD::SETLE: Opc = X86::SETLEr; break;
544 case ISD::SETNE: Opc = X86::SETNEr; break;
545 case ISD::SETULT: Opc = X86::SETBr; break;
546 case ISD::SETUGT: Opc = X86::SETAr; break;
547 case ISD::SETULE: Opc = X86::SETBEr; break;
548 case ISD::SETUGE: Opc = X86::SETAEr; break;
551 // On a floating point condition, the flags are set as follows:
555 // 1 | 0 | 0 | X == Y
556 // 1 | 1 | 1 | unordered
558 switch (SetCCOpcode) {
559 default: assert(0 && "Invalid FP setcc!");
562 Opc = X86::SETEr; // True if ZF = 1
566 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
570 Opc = X86::SETAEr; // True if CF = 0
574 Opc = X86::SETBr; // True if CF = 1
578 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
582 Opc = X86::SETNEr; // True if ZF = 0
585 Opc = X86::SETPr; // True if PF = 1
588 Opc = X86::SETNPr; // True if PF = 0
590 case ISD::SETOEQ: // !PF & ZF
591 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
593 case ISD::SETOLT: // !PF & CF
594 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
596 case ISD::SETOLE: // !PF & (CF || ZF)
597 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
599 case ISD::SETUGT: // PF | (!ZF & !CF)
600 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
602 case ISD::SETUGE: // PF | !CF
603 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
605 case ISD::SETUNE: // PF | !ZF
606 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
610 BuildMI(BB, Opc, 0, DestReg);
614 /// EmitBranchCC - Emit code into BB that arranges for control to transfer to
615 /// the Dest block if the Cond condition is true. If we cannot fold this
616 /// condition into the branch, return true.
618 bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
620 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
621 // B) using two conditional branches instead of one condbr, two setcc's, and
623 if ((Cond.getOpcode() == ISD::OR ||
624 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
625 // And and or set the flags for us, so there is no need to emit a TST of the
626 // result. It is only safe to do this if there is only a single use of the
627 // AND/OR though, otherwise we don't know it will be emitted here.
630 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
634 // Codegen br not C -> JE.
635 if (Cond.getOpcode() == ISD::XOR)
636 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
637 if (NC->isAllOnesValue()) {
639 if (getRegPressure(Chain) > getRegPressure(Cond)) {
641 CondR = SelectExpr(Cond.Val->getOperand(0));
643 CondR = SelectExpr(Cond.Val->getOperand(0));
646 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
647 BuildMI(BB, X86::JE, 1).addMBB(Dest);
651 if (Cond.getOpcode() != ISD::SETCC)
652 return true; // Can only handle simple setcc's so far.
653 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
657 // Handle integer conditions first.
658 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
660 default: assert(0 && "Illegal integer SetCC!");
661 case ISD::SETEQ: Opc = X86::JE; break;
662 case ISD::SETGT: Opc = X86::JG; break;
663 case ISD::SETGE: Opc = X86::JGE; break;
664 case ISD::SETLT: Opc = X86::JL; break;
665 case ISD::SETLE: Opc = X86::JLE; break;
666 case ISD::SETNE: Opc = X86::JNE; break;
667 case ISD::SETULT: Opc = X86::JB; break;
668 case ISD::SETUGT: Opc = X86::JA; break;
669 case ISD::SETULE: Opc = X86::JBE; break;
670 case ISD::SETUGE: Opc = X86::JAE; break;
673 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
674 BuildMI(BB, Opc, 1).addMBB(Dest);
678 unsigned Opc2 = 0; // Second branch if needed.
680 // On a floating point condition, the flags are set as follows:
684 // 1 | 0 | 0 | X == Y
685 // 1 | 1 | 1 | unordered
688 default: assert(0 && "Invalid FP setcc!");
690 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
692 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
694 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
696 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
698 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
700 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
701 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
702 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
703 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
704 Opc = X86::JA; // ZF = 0 & CF = 0
705 Opc2 = X86::JP; // PF = 1
707 case ISD::SETUGE: // PF = 1 | CF = 0
708 Opc = X86::JAE; // CF = 0
709 Opc2 = X86::JP; // PF = 1
711 case ISD::SETUNE: // PF = 1 | ZF = 0
712 Opc = X86::JNE; // ZF = 0
713 Opc2 = X86::JP; // PF = 1
715 case ISD::SETOEQ: // PF = 0 & ZF = 1
718 return true; // FIXME: Emit more efficient code for this branch.
719 case ISD::SETOLT: // PF = 0 & CF = 1
722 return true; // FIXME: Emit more efficient code for this branch.
723 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
726 return true; // FIXME: Emit more efficient code for this branch.
730 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
731 BuildMI(BB, Opc, 1).addMBB(Dest);
733 BuildMI(BB, Opc2, 1).addMBB(Dest);
737 /// EmitSelectCC - Emit code into BB that performs a select operation between
738 /// the two registers RTrue and RFalse, generating a result into RDest.
740 void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
741 MVT::ValueType SVT, unsigned RDest) {
742 unsigned RTrue, RFalse;
744 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
746 } CondCode = NOT_SET;
748 static const unsigned CMOVTAB16[] = {
749 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
750 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
751 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
753 static const unsigned CMOVTAB32[] = {
754 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
755 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
756 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
758 static const unsigned CMOVTABFP[] = {
759 X86::FpCMOVE, X86::FpCMOVNE, /*missing*/0, /*missing*/0,
760 /*missing*/0, /*missing*/ 0, X86::FpCMOVB, X86::FpCMOVBE,
761 X86::FpCMOVA, X86::FpCMOVAE, X86::FpCMOVP, X86::FpCMOVNP
763 static const int SSE_CMOVTAB[] = {
764 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
765 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
766 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
769 if (Cond.getOpcode() == ISD::SETCC) {
770 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
771 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
773 default: assert(0 && "Unknown integer comparison!");
774 case ISD::SETEQ: CondCode = EQ; break;
775 case ISD::SETGT: CondCode = GT; break;
776 case ISD::SETGE: CondCode = GE; break;
777 case ISD::SETLT: CondCode = LT; break;
778 case ISD::SETLE: CondCode = LE; break;
779 case ISD::SETNE: CondCode = NE; break;
780 case ISD::SETULT: CondCode = B; break;
781 case ISD::SETUGT: CondCode = A; break;
782 case ISD::SETULE: CondCode = BE; break;
783 case ISD::SETUGE: CondCode = AE; break;
786 // On a floating point condition, the flags are set as follows:
790 // 1 | 0 | 0 | X == Y
791 // 1 | 1 | 1 | unordered
794 default: assert(0 && "Unknown FP comparison!");
796 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
798 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
800 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
802 case ISD::SETLT: CondCode = B; break; // True if CF = 1
804 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
806 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
807 case ISD::SETUO: CondCode = P; break; // True if PF = 1
808 case ISD::SETO: CondCode = NP; break; // True if PF = 0
809 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
810 case ISD::SETUGE: // PF = 1 | CF = 0
811 case ISD::SETUNE: // PF = 1 | ZF = 0
812 case ISD::SETOEQ: // PF = 0 & ZF = 1
813 case ISD::SETOLT: // PF = 0 & CF = 1
814 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
815 // We cannot emit this comparison as a single cmov.
821 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
822 // code above and one of the results of the select is +0.0, then we can fake
823 // it up through a clever AND with mask. Otherwise, we will fall through to
824 // the code below that will use a PHI node to select the right value.
825 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
826 if (Cond.getOperand(0).getValueType() == SVT &&
827 NOT_SET != CondCode) {
828 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
829 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
830 bool TrueZero = CT && CT->isExactlyValue(0.0);
831 bool FalseZero = CF && CF->isExactlyValue(0.0);
832 if (TrueZero || FalseZero) {
833 SDOperand LHS = Cond.getOperand(0);
834 SDOperand RHS = Cond.getOperand(1);
836 // Select the two halves of the condition
838 if (getRegPressure(LHS) > getRegPressure(RHS)) {
839 RLHS = SelectExpr(LHS);
840 RRHS = SelectExpr(RHS);
842 RRHS = SelectExpr(RHS);
843 RLHS = SelectExpr(LHS);
846 // Emit the comparison and generate a mask from it
847 unsigned MaskReg = MakeReg(SVT);
848 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
849 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
850 .addImm(SSE_CMOVTAB[CondCode]);
853 RFalse = SelectExpr(False);
854 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
855 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
857 RTrue = SelectExpr(True);
858 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
859 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
867 // Select the true and false values for use in both the SSE PHI case, and the
868 // integer or x87 cmov cases below.
869 if (getRegPressure(True) > getRegPressure(False)) {
870 RTrue = SelectExpr(True);
871 RFalse = SelectExpr(False);
873 RFalse = SelectExpr(False);
874 RTrue = SelectExpr(True);
877 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
878 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
879 // node to select between the true and false values.
880 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
881 // FIXME: emit a direct compare and branch rather than setting a cond reg
883 unsigned CondReg = SelectExpr(Cond);
884 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
886 // Create an iterator with which to insert the MBB for copying the false
887 // value and the MBB to hold the PHI instruction for this SetCC.
888 MachineBasicBlock *thisMBB = BB;
889 const BasicBlock *LLVM_BB = BB->getBasicBlock();
890 ilist<MachineBasicBlock>::iterator It = BB;
898 // fallthrough --> copy0MBB
899 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
900 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
901 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
902 MachineFunction *F = BB->getParent();
903 F->getBasicBlockList().insert(It, copy0MBB);
904 F->getBasicBlockList().insert(It, sinkMBB);
905 // Update machine-CFG edges
906 BB->addSuccessor(copy0MBB);
907 BB->addSuccessor(sinkMBB);
911 // # fallthrough to sinkMBB
913 // Update machine-CFG edges
914 BB->addSuccessor(sinkMBB);
917 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
920 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
921 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
926 if (CondCode != NOT_SET) {
928 default: assert(0 && "Cannot select this type!");
929 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
930 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
931 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
935 // Finally, if we weren't able to fold this, just emit the condition and test
937 if (CondCode == NOT_SET || Opc == 0) {
938 // Get the condition into the zero flag.
939 unsigned CondReg = SelectExpr(Cond);
940 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
943 default: assert(0 && "Cannot select this type!");
944 case MVT::i16: Opc = X86::CMOVE16rr; break;
945 case MVT::i32: Opc = X86::CMOVE32rr; break;
946 case MVT::f64: Opc = X86::FpCMOVE; break;
949 // FIXME: CMP R, 0 -> TEST R, R
950 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
951 std::swap(RTrue, RFalse);
953 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
956 void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
958 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
960 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
961 switch (RHS.getValueType()) {
964 case MVT::i8: Opc = X86::CMP8mi; break;
965 case MVT::i16: Opc = X86::CMP16mi; break;
966 case MVT::i32: Opc = X86::CMP32mi; break;
970 EmitFoldedLoad(LHS, AM);
971 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
976 switch (RHS.getValueType()) {
979 case MVT::i8: Opc = X86::CMP8ri; break;
980 case MVT::i16: Opc = X86::CMP16ri; break;
981 case MVT::i32: Opc = X86::CMP32ri; break;
984 unsigned Tmp1 = SelectExpr(LHS);
985 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
988 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
989 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
990 CN->isExactlyValue(-0.0))) {
991 unsigned Reg = SelectExpr(LHS);
992 BuildMI(BB, X86::FpTST, 1).addReg(Reg);
993 BuildMI(BB, X86::FNSTSW8r, 0);
994 BuildMI(BB, X86::SAHF, 1);
1000 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
1001 switch (RHS.getValueType()) {
1004 case MVT::i8: Opc = X86::CMP8mr; break;
1005 case MVT::i16: Opc = X86::CMP16mr; break;
1006 case MVT::i32: Opc = X86::CMP32mr; break;
1010 EmitFoldedLoad(LHS, AM);
1011 unsigned Reg = SelectExpr(RHS);
1012 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1017 switch (LHS.getValueType()) {
1018 default: assert(0 && "Cannot compare this value!");
1020 case MVT::i8: Opc = X86::CMP8rr; break;
1021 case MVT::i16: Opc = X86::CMP16rr; break;
1022 case MVT::i32: Opc = X86::CMP32rr; break;
1023 case MVT::f32: Opc = X86::UCOMISSrr; break;
1024 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FpUCOMIr; break;
1026 unsigned Tmp1, Tmp2;
1027 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1028 Tmp1 = SelectExpr(LHS);
1029 Tmp2 = SelectExpr(RHS);
1031 Tmp2 = SelectExpr(RHS);
1032 Tmp1 = SelectExpr(LHS);
1034 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1037 /// isFoldableLoad - Return true if this is a load instruction that can safely
1038 /// be folded into an operation that uses it.
1039 bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1040 if (Op.getOpcode() == ISD::LOAD) {
1041 // FIXME: currently can't fold constant pool indexes.
1042 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1044 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1045 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
1046 // FIXME: currently can't fold constant pool indexes.
1047 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1053 // If this load has already been emitted, we clearly can't fold it.
1054 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1055 if (ExprMap.count(Op.getValue(1))) return false;
1056 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1057 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
1059 // If there is not just one use of its value, we cannot fold.
1060 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1062 // Finally, we cannot fold the load into the operation if this would induce a
1063 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1064 // operand of the operation we are folding the load into) can possible use the
1065 // chain node defined by the load.
1066 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1067 std::set<SDNode*> Visited;
1068 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1075 /// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1076 /// and compute the address being loaded into AM.
1077 void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1078 SDOperand Chain = Op.getOperand(0);
1079 SDOperand Address = Op.getOperand(1);
1081 if (getRegPressure(Chain) > getRegPressure(Address)) {
1083 SelectAddress(Address, AM);
1085 SelectAddress(Address, AM);
1089 // The chain for this load is now lowered.
1090 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1091 "Load emitted more than once?");
1092 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
1093 assert(0 && "Load emitted more than once!");
1096 // EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1097 // and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1098 // SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1100 bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
1101 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1103 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1104 std::swap(Op1, Op2); // Op1 is the SHL now.
1106 return false; // No match
1109 SDOperand ShlVal = Op1.getOperand(0);
1110 SDOperand ShlAmt = Op1.getOperand(1);
1111 SDOperand ShrVal = Op2.getOperand(0);
1112 SDOperand ShrAmt = Op2.getOperand(1);
1114 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1116 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1117 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1118 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
1119 if (SubCST->getValue() == RegSize) {
1120 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
1121 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
1122 if (ShrVal == ShlVal) {
1123 unsigned Reg, ShAmt;
1124 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1125 Reg = SelectExpr(ShrVal);
1126 ShAmt = SelectExpr(ShrAmt);
1128 ShAmt = SelectExpr(ShrAmt);
1129 Reg = SelectExpr(ShrVal);
1131 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1132 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1133 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1134 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1136 } else if (RegSize != 8) {
1137 unsigned AReg, BReg;
1138 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
1139 BReg = SelectExpr(ShlVal);
1140 AReg = SelectExpr(ShrVal);
1142 AReg = SelectExpr(ShrVal);
1143 BReg = SelectExpr(ShlVal);
1145 unsigned ShAmt = SelectExpr(ShrAmt);
1146 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1147 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1148 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1153 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1154 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1155 if (SubCST->getValue() == RegSize) {
1156 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1157 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1158 if (ShrVal == ShlVal) {
1159 unsigned Reg, ShAmt;
1160 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1161 Reg = SelectExpr(ShrVal);
1162 ShAmt = SelectExpr(ShlAmt);
1164 ShAmt = SelectExpr(ShlAmt);
1165 Reg = SelectExpr(ShrVal);
1167 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1168 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1169 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1170 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1172 } else if (RegSize != 8) {
1173 unsigned AReg, BReg;
1174 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
1175 AReg = SelectExpr(ShlVal);
1176 BReg = SelectExpr(ShrVal);
1178 BReg = SelectExpr(ShrVal);
1179 AReg = SelectExpr(ShlVal);
1181 unsigned ShAmt = SelectExpr(ShlAmt);
1182 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1183 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1184 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1189 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1190 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1191 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1192 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1193 // (A >> 5) | (A << 27) --> ROR A, 5
1194 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1195 if (ShrVal == ShlVal) {
1196 unsigned Reg = SelectExpr(ShrVal);
1197 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1198 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1199 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1201 } else if (RegSize != 8) {
1202 unsigned AReg, BReg;
1203 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
1204 BReg = SelectExpr(ShlVal);
1205 AReg = SelectExpr(ShrVal);
1207 AReg = SelectExpr(ShrVal);
1208 BReg = SelectExpr(ShlVal);
1210 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1211 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1212 .addImm(ShrCst->getValue());
1220 unsigned ISel::SelectExpr(SDOperand N) {
1222 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
1223 SDNode *Node = N.Val;
1226 if (Node->getOpcode() == ISD::CopyFromReg ||
1227 Node->getOpcode() == ISD::Register) {
1228 unsigned Reg = (Node->getOpcode() == ISD::CopyFromReg) ?
1229 cast<RegisterSDNode>(Node->getOperand(1))->getReg() :
1230 cast<RegisterSDNode>(Node)->getReg();
1231 // Just use the specified register as our input if we can.
1232 if (Node->getOpcode() == ISD::Register ||
1233 MRegisterInfo::isVirtualRegister(Reg))
1237 unsigned &Reg = ExprMap[N];
1238 if (Reg) return Reg;
1240 switch (N.getOpcode()) {
1242 Reg = Result = (N.getValueType() != MVT::Other) ?
1243 MakeReg(N.getValueType()) : 1;
1245 case X86ISD::TAILCALL:
1247 // If this is a call instruction, make sure to prepare ALL of the result
1248 // values as well as the chain.
1249 ExprMap[N.getValue(0)] = 1;
1250 if (Node->getNumValues() > 1) {
1251 Result = MakeReg(Node->getValueType(1));
1252 ExprMap[N.getValue(1)] = Result;
1253 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
1254 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1259 case ISD::ADD_PARTS:
1260 case ISD::SUB_PARTS:
1261 case ISD::SHL_PARTS:
1262 case ISD::SRL_PARTS:
1263 case ISD::SRA_PARTS:
1264 Result = MakeReg(Node->getValueType(0));
1265 ExprMap[N.getValue(0)] = Result;
1266 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1267 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1271 switch (N.getOpcode()) {
1274 assert(0 && "Node not handled!\n");
1275 case ISD::FP_EXTEND:
1276 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
1277 Tmp1 = SelectExpr(N.getOperand(0));
1278 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
1281 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
1282 Tmp1 = SelectExpr(N.getOperand(0));
1283 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
1285 case ISD::CopyFromReg:
1286 Select(N.getOperand(0));
1288 Reg = Result = ExprMap[N.getValue(0)] =
1289 MakeReg(N.getValue(0).getValueType());
1291 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1292 switch (Node->getValueType(0)) {
1293 default: assert(0 && "Cannot CopyFromReg this!");
1296 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1299 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
1302 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
1306 case ISD::FrameIndex:
1307 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1308 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1310 case ISD::ConstantPool:
1311 Tmp1 = BB->getParent()->getConstantPool()->
1312 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
1313 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1315 case ISD::ConstantFP:
1317 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
1318 "SSE only supports +0.0");
1319 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
1320 BuildMI(BB, Opc, 0, Result);
1323 ContainsFPCode = true;
1324 Tmp1 = Result; // Intermediate Register
1325 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1326 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1327 Tmp1 = MakeReg(MVT::f64);
1329 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1330 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1331 BuildMI(BB, X86::FpLD0, 0, Tmp1);
1332 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1333 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1334 BuildMI(BB, X86::FpLD1, 0, Tmp1);
1336 assert(0 && "Unexpected constant!");
1338 BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1);
1341 switch (N.getValueType()) {
1342 default: assert(0 && "Cannot use constants of this type!");
1344 case MVT::i8: Opc = X86::MOV8ri; break;
1345 case MVT::i16: Opc = X86::MOV16ri; break;
1346 case MVT::i32: Opc = X86::MOV32ri; break;
1348 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1351 if (Node->getValueType(0) == MVT::f64) {
1352 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1353 BuildMI(BB, X86::FpLD0, 0, Result);
1355 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1358 case ISD::GlobalAddress: {
1359 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1360 // For Darwin, external and weak symbols are indirect, so we want to load
1361 // the value at address GV, not the value of GV itself.
1362 if (Subtarget->getIndirectExternAndWeakGlobals() &&
1363 (GV->hasWeakLinkage() || GV->isExternal())) {
1364 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1365 .addGlobalAddress(GV, false, 0);
1367 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1371 case ISD::ExternalSymbol: {
1372 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1373 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1376 case ISD::ANY_EXTEND: // treat any extend like zext
1377 case ISD::ZERO_EXTEND: {
1378 int DestIs16 = N.getValueType() == MVT::i16;
1379 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1381 // FIXME: This hack is here for zero extension casts from bool to i8. This
1382 // would not be needed if bools were promoted by Legalize.
1383 if (N.getValueType() == MVT::i8) {
1384 Tmp1 = SelectExpr(N.getOperand(0));
1385 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1389 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
1390 static const unsigned Opc[3] = {
1391 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1395 EmitFoldedLoad(N.getOperand(0), AM);
1396 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1401 static const unsigned Opc[3] = {
1402 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1404 Tmp1 = SelectExpr(N.getOperand(0));
1405 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1408 case ISD::SIGN_EXTEND: {
1409 int DestIs16 = N.getValueType() == MVT::i16;
1410 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1412 // FIXME: Legalize should promote bools to i8!
1413 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1414 "Sign extend from bool not implemented!");
1416 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
1417 static const unsigned Opc[3] = {
1418 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1422 EmitFoldedLoad(N.getOperand(0), AM);
1423 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1427 static const unsigned Opc[3] = {
1428 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1430 Tmp1 = SelectExpr(N.getOperand(0));
1431 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1435 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1436 // a move out of AX or AL.
1437 switch (N.getOperand(0).getValueType()) {
1438 default: assert(0 && "Unknown truncate!");
1439 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1440 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1441 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1443 Tmp1 = SelectExpr(N.getOperand(0));
1444 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1446 switch (N.getValueType()) {
1447 default: assert(0 && "Unknown truncate!");
1449 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1450 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1452 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1455 case ISD::SINT_TO_FP: {
1456 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1457 unsigned PromoteOpcode = 0;
1459 // We can handle any sint to fp with the direct sse conversion instructions.
1461 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
1462 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1466 ContainsFPCode = true;
1468 // Spill the integer to memory and reload it from there.
1469 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1470 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1471 MachineFunction *F = BB->getParent();
1472 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1476 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
1477 addFrameReference(BuildMI(BB, X86::FpILD32m, 5, Result), FrameIdx);
1480 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
1481 addFrameReference(BuildMI(BB, X86::FpILD16m, 5, Result), FrameIdx);
1483 default: break; // No promotion required.
1487 case ISD::FP_TO_SINT:
1488 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1490 // If the target supports SSE2 and is performing FP operations in SSE regs
1491 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1492 // CVTSD2SI instructions.
1493 assert(X86ScalarSSE);
1494 if (MVT::f32 == N.getOperand(0).getValueType()) {
1495 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1496 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1497 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1499 assert(0 && "Not an f32 or f64?");
1506 Op0 = N.getOperand(0);
1507 Op1 = N.getOperand(1);
1509 if (isFoldableLoad(Op0, Op1, true)) {
1510 std::swap(Op0, Op1);
1514 if (isFoldableLoad(Op1, Op0, true)) {
1516 switch (N.getValueType()) {
1517 default: assert(0 && "Cannot add this type!");
1519 case MVT::i8: Opc = X86::ADD8rm; break;
1520 case MVT::i16: Opc = X86::ADD16rm; break;
1521 case MVT::i32: Opc = X86::ADD32rm; break;
1522 case MVT::f32: Opc = X86::ADDSSrm; break;
1524 // For F64, handle promoted load operations (from F32) as well!
1526 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1529 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FpADD64m : X86::FpADD32m;
1534 EmitFoldedLoad(Op1, AM);
1535 Tmp1 = SelectExpr(Op0);
1536 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1540 // See if we can codegen this as an LEA to fold operations together.
1541 if (N.getValueType() == MVT::i32) {
1543 X86ISelAddressMode AM;
1544 MatchAddress(N, AM);
1545 ExprMap[N] = Result;
1547 // If this is not just an add, emit the LEA. For a simple add (like
1548 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1549 // leave this as LEA, then peephole it to 'ADD' after two address elim
1551 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1552 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1553 X86AddressMode XAM = SelectAddrExprs(AM);
1554 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1559 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1561 if (CN->getValue() == 1) { // add X, 1 -> inc X
1562 switch (N.getValueType()) {
1563 default: assert(0 && "Cannot integer add this type!");
1564 case MVT::i8: Opc = X86::INC8r; break;
1565 case MVT::i16: Opc = X86::INC16r; break;
1566 case MVT::i32: Opc = X86::INC32r; break;
1568 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1569 switch (N.getValueType()) {
1570 default: assert(0 && "Cannot integer add this type!");
1571 case MVT::i8: Opc = X86::DEC8r; break;
1572 case MVT::i16: Opc = X86::DEC16r; break;
1573 case MVT::i32: Opc = X86::DEC32r; break;
1578 Tmp1 = SelectExpr(Op0);
1579 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1583 switch (N.getValueType()) {
1584 default: assert(0 && "Cannot add this type!");
1585 case MVT::i8: Opc = X86::ADD8ri; break;
1586 case MVT::i16: Opc = X86::ADD16ri; break;
1587 case MVT::i32: Opc = X86::ADD32ri; break;
1590 Tmp1 = SelectExpr(Op0);
1591 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1596 switch (N.getValueType()) {
1597 default: assert(0 && "Cannot add this type!");
1598 case MVT::i8: Opc = X86::ADD8rr; break;
1599 case MVT::i16: Opc = X86::ADD16rr; break;
1600 case MVT::i32: Opc = X86::ADD32rr; break;
1601 case MVT::f32: Opc = X86::ADDSSrr; break;
1602 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
1605 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1606 Tmp1 = SelectExpr(Op0);
1607 Tmp2 = SelectExpr(Op1);
1609 Tmp2 = SelectExpr(Op1);
1610 Tmp1 = SelectExpr(Op0);
1613 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1617 Tmp1 = SelectExpr(Node->getOperand(0));
1619 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1620 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1622 BuildMI(BB, X86::FpSQRT, 1, Result).addReg(Tmp1);
1627 // Once we can spill 16 byte constants into the constant pool, we can
1628 // implement SSE equivalents of FABS and FCHS.
1633 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
1634 Tmp1 = SelectExpr(Node->getOperand(0));
1635 switch (N.getOpcode()) {
1636 default: assert(0 && "Unreachable!");
1637 case ISD::FABS: BuildMI(BB, X86::FpABS, 1, Result).addReg(Tmp1); break;
1638 case ISD::FNEG: BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1); break;
1639 case ISD::FSIN: BuildMI(BB, X86::FpSIN, 1, Result).addReg(Tmp1); break;
1640 case ISD::FCOS: BuildMI(BB, X86::FpCOS, 1, Result).addReg(Tmp1); break;
1645 switch (N.getValueType()) {
1646 default: assert(0 && "Unsupported VT!");
1647 case MVT::i8: Tmp2 = X86::MUL8r; break;
1648 case MVT::i16: Tmp2 = X86::MUL16r; break;
1649 case MVT::i32: Tmp2 = X86::MUL32r; break;
1653 unsigned MovOpc, LowReg, HiReg;
1654 switch (N.getValueType()) {
1655 default: assert(0 && "Unsupported VT!");
1657 MovOpc = X86::MOV8rr;
1663 MovOpc = X86::MOV16rr;
1669 MovOpc = X86::MOV32rr;
1675 if (Node->getOpcode() != ISD::MULHS)
1676 Opc = Tmp2; // Get the MULHU opcode.
1678 Op0 = Node->getOperand(0);
1679 Op1 = Node->getOperand(1);
1680 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1681 Tmp1 = SelectExpr(Op0);
1682 Tmp2 = SelectExpr(Op1);
1684 Tmp2 = SelectExpr(Op1);
1685 Tmp1 = SelectExpr(Op0);
1688 // FIXME: Implement folding of loads into the memory operands here!
1689 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1690 BuildMI(BB, Opc, 1).addReg(Tmp2);
1691 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1702 static const unsigned SUBTab[] = {
1703 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1704 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FpSUB32m, X86::FpSUB64m,
1705 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1707 static const unsigned SSE_SUBTab[] = {
1708 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1709 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1710 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1712 static const unsigned MULTab[] = {
1713 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1714 0, X86::IMUL16rm , X86::IMUL32rm, X86::FpMUL32m, X86::FpMUL64m,
1715 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1717 static const unsigned SSE_MULTab[] = {
1718 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1719 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1720 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1722 static const unsigned ANDTab[] = {
1723 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1724 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1725 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1727 static const unsigned ORTab[] = {
1728 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1729 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1730 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1732 static const unsigned XORTab[] = {
1733 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1734 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1735 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1738 Op0 = Node->getOperand(0);
1739 Op1 = Node->getOperand(1);
1741 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1742 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
1745 if (Node->getOpcode() == ISD::SUB)
1746 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1747 if (CN->isNullValue()) { // 0 - N -> neg N
1748 switch (N.getValueType()) {
1749 default: assert(0 && "Cannot sub this type!");
1751 case MVT::i8: Opc = X86::NEG8r; break;
1752 case MVT::i16: Opc = X86::NEG16r; break;
1753 case MVT::i32: Opc = X86::NEG32r; break;
1755 Tmp1 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1760 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1761 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
1763 switch (N.getValueType()) {
1764 default: assert(0 && "Cannot add this type!");
1765 case MVT::i1: break; // Not supported, don't invert upper bits!
1766 case MVT::i8: Opc = X86::NOT8r; break;
1767 case MVT::i16: Opc = X86::NOT16r; break;
1768 case MVT::i32: Opc = X86::NOT32r; break;
1771 Tmp1 = SelectExpr(Op0);
1772 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1777 // Fold common multiplies into LEA instructions.
1778 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1779 switch ((int)CN->getValue()) {
1784 // Remove N from exprmap so SelectAddress doesn't get confused.
1787 SelectAddress(N, AM);
1788 // Restore it to the map.
1789 ExprMap[N] = Result;
1790 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1795 switch (N.getValueType()) {
1796 default: assert(0 && "Cannot xor this type!");
1798 case MVT::i8: Opc = 0; break;
1799 case MVT::i16: Opc = 1; break;
1800 case MVT::i32: Opc = 2; break;
1802 switch (Node->getOpcode()) {
1803 default: assert(0 && "Unreachable!");
1805 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
1807 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
1808 case ISD::AND: Opc = ANDTab[Opc]; break;
1809 case ISD::OR: Opc = ORTab[Opc]; break;
1810 case ISD::XOR: Opc = XORTab[Opc]; break;
1812 if (Opc) { // Can't fold MUL:i8 R, imm
1813 Tmp1 = SelectExpr(Op0);
1814 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1819 if (isFoldableLoad(Op0, Op1, true))
1820 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
1821 std::swap(Op0, Op1);
1824 // For FP, emit 'reverse' subract, with a memory operand.
1825 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
1826 if (Op0.getOpcode() == ISD::EXTLOAD)
1827 Opc = X86::FpSUBR32m;
1829 Opc = X86::FpSUBR64m;
1832 EmitFoldedLoad(Op0, AM);
1833 Tmp1 = SelectExpr(Op1);
1834 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1839 if (isFoldableLoad(Op1, Op0, true)) {
1841 switch (N.getValueType()) {
1842 default: assert(0 && "Cannot operate on this type!");
1844 case MVT::i8: Opc = 5; break;
1845 case MVT::i16: Opc = 6; break;
1846 case MVT::i32: Opc = 7; break;
1847 case MVT::f32: Opc = 8; break;
1848 // For F64, handle promoted load operations (from F32) as well!
1850 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
1851 "SSE load should have been promoted");
1852 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
1854 switch (Node->getOpcode()) {
1855 default: assert(0 && "Unreachable!");
1857 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
1859 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
1860 case ISD::AND: Opc = ANDTab[Opc]; break;
1861 case ISD::OR: Opc = ORTab[Opc]; break;
1862 case ISD::XOR: Opc = XORTab[Opc]; break;
1866 EmitFoldedLoad(Op1, AM);
1867 Tmp1 = SelectExpr(Op0);
1869 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1871 assert(Node->getOpcode() == ISD::MUL &&
1872 N.getValueType() == MVT::i8 && "Unexpected situation!");
1873 // Must use the MUL instruction, which forces use of AL.
1874 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1875 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1876 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1881 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1882 Tmp1 = SelectExpr(Op0);
1883 Tmp2 = SelectExpr(Op1);
1885 Tmp2 = SelectExpr(Op1);
1886 Tmp1 = SelectExpr(Op0);
1889 switch (N.getValueType()) {
1890 default: assert(0 && "Cannot add this type!");
1892 case MVT::i8: Opc = 10; break;
1893 case MVT::i16: Opc = 11; break;
1894 case MVT::i32: Opc = 12; break;
1895 case MVT::f32: Opc = 13; break;
1896 case MVT::f64: Opc = 14; break;
1898 switch (Node->getOpcode()) {
1899 default: assert(0 && "Unreachable!");
1901 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
1903 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
1904 case ISD::AND: Opc = ANDTab[Opc]; break;
1905 case ISD::OR: Opc = ORTab[Opc]; break;
1906 case ISD::XOR: Opc = XORTab[Opc]; break;
1909 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1911 assert(Node->getOpcode() == ISD::MUL &&
1912 N.getValueType() == MVT::i8 && "Unexpected situation!");
1913 // Must use the MUL instruction, which forces use of AL.
1914 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1915 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1916 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1920 case ISD::ADD_PARTS:
1921 case ISD::SUB_PARTS: {
1922 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1923 "Not an i64 add/sub!");
1924 // Emit all of the operands.
1925 std::vector<unsigned> InVals;
1926 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1927 InVals.push_back(SelectExpr(N.getOperand(i)));
1928 if (N.getOpcode() == ISD::ADD_PARTS) {
1929 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1930 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1932 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1933 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1935 return Result+N.ResNo;
1938 case ISD::SHL_PARTS:
1939 case ISD::SRA_PARTS:
1940 case ISD::SRL_PARTS: {
1941 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1942 "Not an i64 shift!");
1943 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1944 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1945 unsigned TmpReg = MakeReg(MVT::i32);
1946 if (N.getOpcode() == ISD::SRA_PARTS) {
1947 // If this is a SHR of a Long, then we need to do funny sign extension
1948 // stuff. TmpReg gets the value to use as the high-part if we are
1949 // shifting more than 32 bits.
1950 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1952 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1953 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1956 // Initialize CL with the shift amount.
1957 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1958 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1960 unsigned TmpReg2 = MakeReg(MVT::i32);
1961 unsigned TmpReg3 = MakeReg(MVT::i32);
1962 if (N.getOpcode() == ISD::SHL_PARTS) {
1963 // TmpReg2 = shld inHi, inLo
1964 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1966 // TmpReg3 = shl inLo, CL
1967 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
1969 // Set the flags to indicate whether the shift was by more than 32 bits.
1970 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
1972 // DestHi = (>32) ? TmpReg3 : TmpReg2;
1973 BuildMI(BB, X86::CMOVNE32rr, 2,
1974 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1975 // DestLo = (>32) ? TmpReg : TmpReg3;
1976 BuildMI(BB, X86::CMOVNE32rr, 2,
1977 Result).addReg(TmpReg3).addReg(TmpReg);
1979 // TmpReg2 = shrd inLo, inHi
1980 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1982 // TmpReg3 = s[ah]r inHi, CL
1983 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
1984 : X86::SHR32rCL, 1, TmpReg3)
1987 // Set the flags to indicate whether the shift was by more than 32 bits.
1988 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
1990 // DestLo = (>32) ? TmpReg3 : TmpReg2;
1991 BuildMI(BB, X86::CMOVNE32rr, 2,
1992 Result).addReg(TmpReg2).addReg(TmpReg3);
1994 // DestHi = (>32) ? TmpReg : TmpReg3;
1995 BuildMI(BB, X86::CMOVNE32rr, 2,
1996 Result+1).addReg(TmpReg3).addReg(TmpReg);
1998 return Result+N.ResNo;
2002 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2003 N.getValueType(), Result);
2012 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2013 "We don't support this operator!");
2015 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
2016 // We can fold loads into FpDIVs, but not really into any others.
2017 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
2018 // Check for reversed and unreversed DIV.
2019 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2020 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2021 Opc = X86::FpDIVR32m;
2023 Opc = X86::FpDIVR64m;
2025 EmitFoldedLoad(N.getOperand(0), AM);
2026 Tmp1 = SelectExpr(N.getOperand(1));
2027 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2029 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2030 N.getOperand(1).getOpcode() == ISD::LOAD) {
2031 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2032 Opc = X86::FpDIV32m;
2034 Opc = X86::FpDIV64m;
2036 EmitFoldedLoad(N.getOperand(1), AM);
2037 Tmp1 = SelectExpr(N.getOperand(0));
2038 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2044 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2045 Tmp1 = SelectExpr(N.getOperand(0));
2046 Tmp2 = SelectExpr(N.getOperand(1));
2048 Tmp2 = SelectExpr(N.getOperand(1));
2049 Tmp1 = SelectExpr(N.getOperand(0));
2052 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2053 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2054 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2055 switch (N.getValueType()) {
2056 default: assert(0 && "Cannot sdiv this type!");
2058 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2061 MovOpcode = X86::MOV8rr;
2062 ClrOpcode = X86::MOV8ri;
2063 SExtOpcode = X86::CBW;
2066 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2069 MovOpcode = X86::MOV16rr;
2070 ClrOpcode = X86::MOV16ri;
2071 SExtOpcode = X86::CWD;
2074 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
2077 MovOpcode = X86::MOV32rr;
2078 ClrOpcode = X86::MOV32ri;
2079 SExtOpcode = X86::CDQ;
2082 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2085 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2086 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2090 // Set up the low part.
2091 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2094 // Sign extend the low part into the high part.
2095 BuildMI(BB, SExtOpcode, 0);
2097 // Zero out the high part, effectively zero extending the input.
2098 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2101 // Emit the DIV/IDIV instruction.
2102 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2104 // Get the result of the divide or rem.
2105 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2110 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2111 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2112 switch (N.getValueType()) {
2113 default: assert(0 && "Cannot shift this type!");
2114 case MVT::i8: Opc = X86::ADD8rr; break;
2115 case MVT::i16: Opc = X86::ADD16rr; break;
2116 case MVT::i32: Opc = X86::ADD32rr; break;
2118 Tmp1 = SelectExpr(N.getOperand(0));
2119 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2123 switch (N.getValueType()) {
2124 default: assert(0 && "Cannot shift this type!");
2125 case MVT::i8: Opc = X86::SHL8ri; break;
2126 case MVT::i16: Opc = X86::SHL16ri; break;
2127 case MVT::i32: Opc = X86::SHL32ri; break;
2129 Tmp1 = SelectExpr(N.getOperand(0));
2130 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2134 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2135 Tmp1 = SelectExpr(N.getOperand(0));
2136 Tmp2 = SelectExpr(N.getOperand(1));
2138 Tmp2 = SelectExpr(N.getOperand(1));
2139 Tmp1 = SelectExpr(N.getOperand(0));
2142 switch (N.getValueType()) {
2143 default: assert(0 && "Cannot shift this type!");
2144 case MVT::i8 : Opc = X86::SHL8rCL; break;
2145 case MVT::i16: Opc = X86::SHL16rCL; break;
2146 case MVT::i32: Opc = X86::SHL32rCL; break;
2148 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2149 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2152 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2153 switch (N.getValueType()) {
2154 default: assert(0 && "Cannot shift this type!");
2155 case MVT::i8: Opc = X86::SHR8ri; break;
2156 case MVT::i16: Opc = X86::SHR16ri; break;
2157 case MVT::i32: Opc = X86::SHR32ri; break;
2159 Tmp1 = SelectExpr(N.getOperand(0));
2160 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2164 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2165 Tmp1 = SelectExpr(N.getOperand(0));
2166 Tmp2 = SelectExpr(N.getOperand(1));
2168 Tmp2 = SelectExpr(N.getOperand(1));
2169 Tmp1 = SelectExpr(N.getOperand(0));
2172 switch (N.getValueType()) {
2173 default: assert(0 && "Cannot shift this type!");
2174 case MVT::i8 : Opc = X86::SHR8rCL; break;
2175 case MVT::i16: Opc = X86::SHR16rCL; break;
2176 case MVT::i32: Opc = X86::SHR32rCL; break;
2178 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2179 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2182 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2183 switch (N.getValueType()) {
2184 default: assert(0 && "Cannot shift this type!");
2185 case MVT::i8: Opc = X86::SAR8ri; break;
2186 case MVT::i16: Opc = X86::SAR16ri; break;
2187 case MVT::i32: Opc = X86::SAR32ri; break;
2189 Tmp1 = SelectExpr(N.getOperand(0));
2190 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2194 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2195 Tmp1 = SelectExpr(N.getOperand(0));
2196 Tmp2 = SelectExpr(N.getOperand(1));
2198 Tmp2 = SelectExpr(N.getOperand(1));
2199 Tmp1 = SelectExpr(N.getOperand(0));
2202 switch (N.getValueType()) {
2203 default: assert(0 && "Cannot shift this type!");
2204 case MVT::i8 : Opc = X86::SAR8rCL; break;
2205 case MVT::i16: Opc = X86::SAR16rCL; break;
2206 case MVT::i32: Opc = X86::SAR32rCL; break;
2208 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2209 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2213 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
2214 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
2215 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2218 // Make sure we generate both values.
2219 if (Result != 1) { // Generate the token
2220 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2221 assert(0 && "Load already emitted!?");
2223 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2225 switch (Node->getValueType(0)) {
2226 default: assert(0 && "Cannot load this type!");
2228 case MVT::i8: Opc = X86::MOV8rm; break;
2229 case MVT::i16: Opc = X86::MOV16rm; break;
2230 case MVT::i32: Opc = X86::MOV32rm; break;
2231 case MVT::f32: Opc = X86::MOVSSrm; break;
2237 ContainsFPCode = true;
2242 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
2243 unsigned CPIdx = BB->getParent()->getConstantPool()->
2244 getConstantPoolIndex(CP->get());
2245 Select(N.getOperand(0));
2246 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
2250 SDOperand Chain = N.getOperand(0);
2251 SDOperand Address = N.getOperand(1);
2252 if (getRegPressure(Chain) > getRegPressure(Address)) {
2254 SelectAddress(Address, AM);
2256 SelectAddress(Address, AM);
2260 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2264 // Make sure we generate both values.
2265 assert(Result != 1 && N.getValueType() == MVT::f64);
2266 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2267 assert(0 && "Load already emitted!?");
2272 SDOperand Chain = N.getOperand(0);
2273 SDOperand Address = N.getOperand(1);
2274 if (getRegPressure(Chain) > getRegPressure(Address)) {
2276 SelectAddress(Address, AM);
2278 SelectAddress(Address, AM);
2282 addFullAddress(BuildMI(BB, X86::FpILD64m, 4, Result), AM);
2286 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2287 case ISD::ZEXTLOAD: {
2288 // Make sure we generate both values.
2290 ExprMap[N.getValue(1)] = 1; // Generate the token
2292 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2294 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2295 if (Node->getValueType(0) == MVT::f64) {
2296 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
2298 unsigned CPIdx = BB->getParent()->getConstantPool()->
2299 getConstantPoolIndex(CP->get());
2301 addConstantPoolReference(BuildMI(BB, X86::FpLD32m, 4, Result), CPIdx);
2306 if (getRegPressure(Node->getOperand(0)) >
2307 getRegPressure(Node->getOperand(1))) {
2308 Select(Node->getOperand(0)); // chain
2309 SelectAddress(Node->getOperand(1), AM);
2311 SelectAddress(Node->getOperand(1), AM);
2312 Select(Node->getOperand(0)); // chain
2315 switch (Node->getValueType(0)) {
2316 default: assert(0 && "Unknown type to sign extend to.");
2318 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
2320 addFullAddress(BuildMI(BB, X86::FpLD32m, 5, Result), AM);
2323 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
2325 assert(0 && "Bad zero extend!");
2328 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2331 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2336 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
2337 "Bad zero extend!");
2338 addFullAddress(BuildMI(BB, X86::MOVZX16rm8, 5, Result), AM);
2341 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
2342 "Bad zero extend!");
2343 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2348 case ISD::SEXTLOAD: {
2349 // Make sure we generate both values.
2351 ExprMap[N.getValue(1)] = 1; // Generate the token
2353 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2356 if (getRegPressure(Node->getOperand(0)) >
2357 getRegPressure(Node->getOperand(1))) {
2358 Select(Node->getOperand(0)); // chain
2359 SelectAddress(Node->getOperand(1), AM);
2361 SelectAddress(Node->getOperand(1), AM);
2362 Select(Node->getOperand(0)); // chain
2365 switch (Node->getValueType(0)) {
2366 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2367 default: assert(0 && "Unknown type to sign extend to.");
2369 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
2371 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2373 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2376 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2381 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
2382 "Cannot sign extend from bool!");
2383 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2389 case X86ISD::TAILCALL:
2390 case X86ISD::CALL: {
2391 // The chain for this call is now lowered.
2392 ExprMap.insert(std::make_pair(N.getValue(0), 1));
2394 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2395 isa<ExternalSymbolSDNode>(N.getOperand(1));
2396 unsigned Callee = 0;
2398 Select(N.getOperand(0));
2400 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2401 Select(N.getOperand(0));
2402 Callee = SelectExpr(N.getOperand(1));
2404 Callee = SelectExpr(N.getOperand(1));
2405 Select(N.getOperand(0));
2409 // If this call has values to pass in registers, do so now.
2410 if (Node->getNumOperands() > 4) {
2411 // The first value is passed in (a part of) EAX, the second in EDX.
2412 unsigned RegOp1 = SelectExpr(N.getOperand(4));
2414 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
2416 switch (N.getOperand(4).getValueType()) {
2417 default: assert(0 && "Bad thing to pass in regs");
2419 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2420 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2421 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2424 switch (N.getOperand(5).getValueType()) {
2425 default: assert(0 && "Bad thing to pass in regs");
2428 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2431 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2434 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2439 if (GlobalAddressSDNode *GASD =
2440 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2441 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2442 } else if (ExternalSymbolSDNode *ESSDN =
2443 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2444 BuildMI(BB, X86::CALLpcrel32,
2445 1).addExternalSymbol(ESSDN->getSymbol(), true);
2447 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2448 Select(N.getOperand(0));
2449 Tmp1 = SelectExpr(N.getOperand(1));
2451 Tmp1 = SelectExpr(N.getOperand(1));
2452 Select(N.getOperand(0));
2455 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2458 // Get caller stack amount and amount the callee added to the stack pointer.
2459 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2460 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2461 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2463 if (Node->getNumValues() != 1)
2464 switch (Node->getValueType(1)) {
2465 default: assert(0 && "Unknown value type for call result!");
2466 case MVT::Other: return 1;
2469 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2472 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2475 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2476 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2477 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2479 case MVT::f64: // Floating-point return values live in %ST(0)
2481 ContainsFPCode = true;
2482 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2484 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2485 MachineFunction *F = BB->getParent();
2486 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2487 addFrameReference(BuildMI(BB, X86::FpST64m, 5), FrameIdx).addReg(X86::FP0);
2488 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2491 ContainsFPCode = true;
2492 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2496 return Result+N.ResNo-1;
2499 // First, determine that the size of the operand falls within the acceptable
2500 // range for this architecture.
2502 if (Node->getOperand(1).getValueType() != MVT::i16) {
2503 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2507 // Make sure we generate both values.
2508 if (Result != 1) { // Generate the token
2509 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2510 assert(0 && "readport already emitted!?");
2512 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2514 Select(Node->getOperand(0)); // Select the chain.
2516 // If the port is a single-byte constant, use the immediate form.
2517 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2518 if ((Port->getValue() & 255) == Port->getValue()) {
2519 switch (Node->getValueType(0)) {
2521 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2522 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2525 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2526 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2529 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2530 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2536 // Now, move the I/O port address into the DX register and use the IN
2537 // instruction to get the input data.
2539 Tmp1 = SelectExpr(Node->getOperand(1));
2540 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2541 switch (Node->getValueType(0)) {
2543 BuildMI(BB, X86::IN8rr, 0);
2544 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2547 BuildMI(BB, X86::IN16rr, 0);
2548 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2551 BuildMI(BB, X86::IN32rr, 0);
2552 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2555 std::cerr << "Cannot do input on this data type";
2564 /// TryToFoldLoadOpStore - Given a store node, try to fold together a
2565 /// load/op/store instruction. If successful return true.
2566 bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2567 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2568 SDOperand Chain = Node->getOperand(0);
2569 SDOperand StVal = Node->getOperand(1);
2570 SDOperand StPtr = Node->getOperand(2);
2572 // The chain has to be a load, the stored value must be an integer binary
2573 // operation with one use.
2574 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
2575 MVT::isFloatingPoint(StVal.getValueType()))
2578 // Token chain must either be a factor node or the load to fold.
2579 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2584 // Check to see if there is a load from the same pointer that we're storing
2585 // to in either operand of the binop.
2586 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2587 StVal.getOperand(0).getOperand(1) == StPtr)
2588 TheLoad = StVal.getOperand(0);
2589 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2590 StVal.getOperand(1).getOperand(1) == StPtr)
2591 TheLoad = StVal.getOperand(1);
2593 return false; // No matching load operand.
2595 // We can only fold the load if there are no intervening side-effecting
2596 // operations. This means that the store uses the load as its token chain, or
2597 // there are only token factor nodes in between the store and load.
2598 if (Chain != TheLoad.getValue(1)) {
2599 // Okay, the other option is that we have a store referring to (possibly
2600 // nested) token factor nodes. For now, just try peeking through one level
2601 // of token factors to see if this is the case.
2602 bool ChainOk = false;
2603 if (Chain.getOpcode() == ISD::TokenFactor) {
2604 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2605 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2611 if (!ChainOk) return false;
2614 if (TheLoad.getOperand(1) != StPtr)
2617 // Make sure that one of the operands of the binop is the load, and that the
2618 // load folds into the binop.
2619 if (((StVal.getOperand(0) != TheLoad ||
2620 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2621 (StVal.getOperand(1) != TheLoad ||
2622 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2625 // Finally, check to see if this is one of the ops we can handle!
2626 static const unsigned ADDTAB[] = {
2627 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2628 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2630 static const unsigned SUBTAB[] = {
2631 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2632 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2634 static const unsigned ANDTAB[] = {
2635 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2636 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2638 static const unsigned ORTAB[] = {
2639 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2640 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2642 static const unsigned XORTAB[] = {
2643 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2644 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2646 static const unsigned SHLTAB[] = {
2647 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2648 /*Have to put the reg in CL*/0, 0, 0,
2650 static const unsigned SARTAB[] = {
2651 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2652 /*Have to put the reg in CL*/0, 0, 0,
2654 static const unsigned SHRTAB[] = {
2655 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2656 /*Have to put the reg in CL*/0, 0, 0,
2659 const unsigned *TabPtr = 0;
2660 switch (StVal.getOpcode()) {
2662 std::cerr << "CANNOT [mem] op= val: ";
2663 StVal.Val->dump(); std::cerr << "\n";
2671 case ISD::UREM: return false;
2673 case ISD::ADD: TabPtr = ADDTAB; break;
2674 case ISD::SUB: TabPtr = SUBTAB; break;
2675 case ISD::AND: TabPtr = ANDTAB; break;
2676 case ISD:: OR: TabPtr = ORTAB; break;
2677 case ISD::XOR: TabPtr = XORTAB; break;
2678 case ISD::SHL: TabPtr = SHLTAB; break;
2679 case ISD::SRA: TabPtr = SARTAB; break;
2680 case ISD::SRL: TabPtr = SHRTAB; break;
2683 // Handle: [mem] op= CST
2684 SDOperand Op0 = StVal.getOperand(0);
2685 SDOperand Op1 = StVal.getOperand(1);
2687 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2688 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2691 case MVT::i8: Opc = TabPtr[0]; break;
2692 case MVT::i16: Opc = TabPtr[1]; break;
2693 case MVT::i32: Opc = TabPtr[2]; break;
2697 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2698 assert(0 && "Already emitted?");
2702 if (getRegPressure(TheLoad.getOperand(0)) >
2703 getRegPressure(TheLoad.getOperand(1))) {
2704 Select(TheLoad.getOperand(0));
2705 SelectAddress(TheLoad.getOperand(1), AM);
2707 SelectAddress(TheLoad.getOperand(1), AM);
2708 Select(TheLoad.getOperand(0));
2711 if (StVal.getOpcode() == ISD::ADD) {
2712 if (CN->getValue() == 1) {
2713 switch (Op0.getValueType()) {
2716 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2718 case MVT::i16: Opc = TabPtr[1];
2719 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2721 case MVT::i32: Opc = TabPtr[2];
2722 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2725 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2726 switch (Op0.getValueType()) {
2729 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2731 case MVT::i16: Opc = TabPtr[1];
2732 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2734 case MVT::i32: Opc = TabPtr[2];
2735 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2741 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2746 // If we have [mem] = V op [mem], try to turn it into:
2747 // [mem] = [mem] op V.
2748 if (Op1 == TheLoad &&
2749 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
2750 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2751 StVal.getOpcode() != ISD::SRL)
2752 std::swap(Op0, Op1);
2754 if (Op0 != TheLoad) return false;
2756 switch (Op0.getValueType()) {
2757 default: return false;
2759 case MVT::i8: Opc = TabPtr[3]; break;
2760 case MVT::i16: Opc = TabPtr[4]; break;
2761 case MVT::i32: Opc = TabPtr[5]; break;
2764 // Table entry doesn't exist?
2765 if (Opc == 0) return false;
2767 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2768 assert(0 && "Already emitted?");
2770 Select(TheLoad.getOperand(0));
2773 SelectAddress(TheLoad.getOperand(1), AM);
2774 unsigned Reg = SelectExpr(Op1);
2775 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
2779 /// If node is a ret(tailcall) node, emit the specified tail call and return
2780 /// true, otherwise return false.
2782 /// FIXME: This whole thing should be a post-legalize optimization pass which
2783 /// recognizes and transforms the dag. We don't want the selection phase doing
2786 bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2787 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2789 SDOperand Chain = RetNode->getOperand(0);
2791 // If this is a token factor node where one operand is a call, dig into it.
2792 SDOperand TokFactor;
2793 unsigned TokFactorOperand = 0;
2794 if (Chain.getOpcode() == ISD::TokenFactor) {
2795 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2796 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2797 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2798 TokFactorOperand = i;
2800 Chain = Chain.getOperand(i);
2803 if (TokFactor.Val == 0) return false; // No call operand.
2806 // Skip the CALLSEQ_END node if present.
2807 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2808 Chain = Chain.getOperand(0);
2810 // Is a tailcall the last control operation that occurs before the return?
2811 if (Chain.getOpcode() != X86ISD::TAILCALL)
2814 // If we return a value, is it the value produced by the call?
2815 if (RetNode->getNumOperands() > 1) {
2816 // Not returning the ret val of the call?
2817 if (Chain.Val->getNumValues() == 1 ||
2818 RetNode->getOperand(1) != Chain.getValue(1))
2821 if (RetNode->getNumOperands() > 2) {
2822 if (Chain.Val->getNumValues() == 2 ||
2823 RetNode->getOperand(2) != Chain.getValue(2))
2826 assert(RetNode->getNumOperands() <= 3);
2829 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2830 // For FastCC, this will always be > 0.
2831 unsigned CalleeCallArgAmt =
2832 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2834 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2835 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2836 unsigned CalleeCallArgPopAmt =
2837 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2839 // There are several cases we can handle here. First, if the caller and
2840 // callee are both CCC functions, we can tailcall if the callee takes <= the
2841 // number of argument bytes that the caller does.
2842 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2843 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2844 // Check to see if caller arg area size >= callee arg area size.
2845 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2846 //std::cerr << "CCC TAILCALL UNIMP!\n";
2847 // If TokFactor is non-null, emit all operands.
2849 //EmitCCCToCCCTailCall(Chain.Val);
2855 // Second, if both are FastCC functions, we can always perform the tail call.
2856 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2857 // If TokFactor is non-null, emit all operands before the call.
2858 if (TokFactor.Val) {
2859 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2860 if (i != TokFactorOperand)
2861 Select(TokFactor.getOperand(i));
2864 EmitFastCCToFastCCTailCall(Chain.Val);
2868 // We don't support mixed calls, due to issues with alignment. We could in
2869 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2870 // aligned (which depends on the number of arguments to the callee). TODO.
2874 static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2875 SelectionDAG &DAG) {
2876 MVT::ValueType StoreVT;
2877 switch (Chain.getOpcode()) {
2878 default: assert(0 && "Unexpected node!");
2879 case ISD::CALLSEQ_START:
2880 // If we found the start of the call sequence, we're done. We actually
2881 // strip off the CALLSEQ_START node, to avoid generating the
2882 // ADJCALLSTACKDOWN marker for the tail call.
2883 return Chain.getOperand(0);
2884 case ISD::TokenFactor: {
2885 std::vector<SDOperand> Ops;
2886 Ops.reserve(Chain.getNumOperands());
2887 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2888 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2889 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2891 case ISD::STORE: // Normal store
2892 StoreVT = Chain.getOperand(1).getValueType();
2894 case ISD::TRUNCSTORE: // FLOAT store
2895 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
2899 SDOperand OrigDest = Chain.getOperand(2);
2900 unsigned OrigOffset;
2902 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2904 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
2906 // We expect only (ESP+C)
2907 assert(OrigDest.getOpcode() == ISD::ADD &&
2908 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2909 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
2910 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2912 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2915 // Compute the new offset from the incoming ESP value we wish to use.
2916 unsigned NewOffset = OrigOffset + Offset;
2918 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2919 MachineFunction &MF = DAG.getMachineFunction();
2920 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2921 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2923 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2925 if (Chain.getOpcode() == ISD::STORE)
2926 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2928 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2929 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
2930 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
2934 /// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2935 /// fastcc function from a fastcc function, emit the code to emit a 'proper'
2937 void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2938 unsigned CalleeCallArgSize =
2939 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2940 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2942 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2944 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2945 // indexes that are relative to the incoming ESP. If the incoming and
2946 // outgoing arg sizes are the same we will store to [InESP] instead of
2947 // [CurESP] and the ESP referenced will be relative to the incoming function
2949 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2950 SDOperand AdjustedArgStores =
2951 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2953 // Copy the return address of the caller into a virtual register so we don't
2955 SDOperand RetVal(0, 0);
2957 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2958 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2959 RetValAddr, TheDAG->getSrcValue(NULL));
2963 // Codegen all of the argument stores.
2964 Select(AdjustedArgStores);
2967 // Emit a store of the saved ret value to the new location.
2968 MachineFunction &MF = TheDAG->getMachineFunction();
2969 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
2970 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
2971 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
2972 RetVal, RetValAddr));
2975 // Get the destination value.
2976 SDOperand Callee = TailCallNode->getOperand(1);
2977 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
2978 isa<ExternalSymbolSDNode>(Callee);
2979 unsigned CalleeReg = 0;
2981 // If this is not a direct tail call, evaluate the callee's address.
2982 CalleeReg = SelectExpr(Callee);
2985 unsigned RegOp1 = 0;
2986 unsigned RegOp2 = 0;
2988 if (TailCallNode->getNumOperands() > 4) {
2989 // The first value is passed in (a part of) EAX, the second in EDX.
2990 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
2991 if (TailCallNode->getNumOperands() > 5)
2992 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
2994 switch (TailCallNode->getOperand(4).getValueType()) {
2995 default: assert(0 && "Bad thing to pass in regs");
2998 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3002 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3006 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3011 switch (TailCallNode->getOperand(5).getValueType()) {
3012 default: assert(0 && "Bad thing to pass in regs");
3015 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3019 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3023 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3029 // If this is not a direct tail call, put the callee's address into ECX.
3030 // The address has to be evaluated into a non-callee save register that is
3031 // not used for arguments. This means either ECX, as EAX and EDX may be
3032 // used for argument passing. We do this here to make sure that the
3033 // expressions for arguments and callee are all evaluated before the copies
3034 // into physical registers.
3036 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CalleeReg);
3040 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3041 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3043 // TODO: handle jmp [mem]
3045 BuildMI(BB, X86::TAILJMPr, 1).addReg(X86::ECX);
3046 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
3047 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
3049 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3050 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3052 // ADD IMPLICIT USE RegOp1/RegOp2's
3056 void ISel::Select(SDOperand N) {
3057 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
3059 if (!ExprMap.insert(std::make_pair(N, 1)).second)
3060 return; // Already selected.
3062 SDNode *Node = N.Val;
3064 switch (Node->getOpcode()) {
3066 Node->dump(); std::cerr << "\n";
3067 assert(0 && "Node not handled yet!");
3068 case X86ISD::RDTSC_DAG:
3069 Select(Node->getOperand(0)); //Chain
3070 BuildMI(BB, X86::RDTSC, 0);
3073 case ISD::EntryToken: return; // Noop
3074 case ISD::TokenFactor:
3075 if (Node->getNumOperands() == 2) {
3077 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3078 Select(Node->getOperand(OneFirst));
3079 Select(Node->getOperand(!OneFirst));
3081 std::vector<std::pair<unsigned, unsigned> > OpsP;
3082 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3083 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3084 std::sort(OpsP.begin(), OpsP.end());
3085 std::reverse(OpsP.begin(), OpsP.end());
3086 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3087 Select(Node->getOperand(OpsP[i].second));
3090 case ISD::CopyToReg:
3091 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3092 Select(N.getOperand(0));
3093 Tmp1 = SelectExpr(N.getOperand(2));
3095 Tmp1 = SelectExpr(N.getOperand(2));
3096 Select(N.getOperand(0));
3098 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
3101 switch (N.getOperand(2).getValueType()) {
3102 default: assert(0 && "Invalid type for operation!");
3104 case MVT::i8: Opc = X86::MOV8rr; break;
3105 case MVT::i16: Opc = X86::MOV16rr; break;
3106 case MVT::i32: Opc = X86::MOV32rr; break;
3107 case MVT::f32: Opc = X86::MOVSSrr; break;
3113 ContainsFPCode = true;
3117 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3121 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3122 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3123 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3124 if (EmitPotentialTailCall(Node))
3127 switch (N.getNumOperands()) {
3129 assert(0 && "Unknown return instruction!");
3131 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3132 N.getOperand(2).getValueType() == MVT::i32 &&
3133 "Unknown two-register value!");
3134 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3135 Tmp1 = SelectExpr(N.getOperand(1));
3136 Tmp2 = SelectExpr(N.getOperand(2));
3138 Tmp2 = SelectExpr(N.getOperand(2));
3139 Tmp1 = SelectExpr(N.getOperand(1));
3141 Select(N.getOperand(0));
3143 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3144 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
3147 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3148 Select(N.getOperand(0));
3149 Tmp1 = SelectExpr(N.getOperand(1));
3151 Tmp1 = SelectExpr(N.getOperand(1));
3152 Select(N.getOperand(0));
3154 switch (N.getOperand(1).getValueType()) {
3155 default: assert(0 && "All other types should have been promoted!!");
3158 // Spill the value to memory and reload it into top of stack.
3159 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3160 MachineFunction *F = BB->getParent();
3161 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3162 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
3163 addFrameReference(BuildMI(BB, X86::FpLD32m, 4, X86::FP0), FrameIdx);
3164 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
3165 ContainsFPCode = true;
3167 assert(0 && "MVT::f32 only legal with scalar sse fp");
3173 // Spill the value to memory and reload it into top of stack.
3174 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3175 MachineFunction *F = BB->getParent();
3176 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3177 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
3178 addFrameReference(BuildMI(BB, X86::FpLD64m, 4, X86::FP0), FrameIdx);
3179 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
3180 ContainsFPCode = true;
3182 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3186 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3191 Select(N.getOperand(0));
3194 if (X86Lowering.getBytesToPopOnReturn() == 0)
3195 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3197 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
3200 Select(N.getOperand(0));
3201 MachineBasicBlock *Dest =
3202 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3203 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3208 MachineBasicBlock *Dest =
3209 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
3211 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3213 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3214 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3215 Select(N.getOperand(0));
3216 Tmp1 = SelectExpr(N.getOperand(1));
3218 Tmp1 = SelectExpr(N.getOperand(1));
3219 Select(N.getOperand(0));
3221 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3222 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3229 // If this load could be folded into the only using instruction, and if it
3230 // is safe to emit the instruction here, try to do so now.
3231 if (Node->hasNUsesOfValue(1, 0)) {
3232 SDOperand TheVal = N.getValue(0);
3234 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3235 assert(UI != Node->use_end() && "Didn't find use!");
3237 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3238 if (UN->getOperand(i) == TheVal) {
3244 // Only handle unary operators right now.
3245 if (User->getNumOperands() == 1) {
3247 SelectExpr(SDOperand(User, 0));
3258 case X86ISD::TAILCALL:
3263 case ISD::CopyFromReg:
3266 SelectExpr(N.getValue(0));
3269 case X86ISD::FP_TO_INT16_IN_MEM:
3270 case X86ISD::FP_TO_INT32_IN_MEM:
3271 case X86ISD::FP_TO_INT64_IN_MEM: {
3272 assert(N.getOperand(1).getValueType() == MVT::f64);
3274 Select(N.getOperand(0)); // Select the token chain
3277 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3278 ValReg = SelectExpr(N.getOperand(1));
3279 SelectAddress(N.getOperand(2), AM);
3281 SelectAddress(N.getOperand(2), AM);
3282 ValReg = SelectExpr(N.getOperand(1));
3285 // Change the floating point control register to use "round towards zero"
3286 // mode when truncating to an integer value.
3288 MachineFunction *F = BB->getParent();
3289 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3290 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
3292 // Load the old value of the high byte of the control word...
3293 unsigned OldCW = MakeReg(MVT::i16);
3294 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
3296 // Set the high part to be round to zero...
3297 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
3299 // Reload the modified control word now...
3300 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3302 // Restore the memory image of control word to original value
3303 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
3305 // Get the X86 opcode to use.
3306 switch (N.getOpcode()) {
3307 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FpIST16m; break;
3308 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FpIST32m; break;
3309 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FpIST64m; break;
3312 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
3314 // Reload the original control word now.
3315 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3319 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
3321 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
3322 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3323 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3324 && "Unsupported TRUNCSTORE for this target!");
3326 if (StoredTy == MVT::i16) {
3327 // FIXME: This is here just to allow testing. X86 doesn't really have a
3328 // TRUNCSTORE i16 operation, but this is required for targets that do not
3329 // have 16-bit integer registers. We occasionally disable 16-bit integer
3330 // registers to test the promotion code.
3331 Select(N.getOperand(0));
3332 Tmp1 = SelectExpr(N.getOperand(1));
3333 SelectAddress(N.getOperand(2), AM);
3335 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3336 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3340 // Store of constant bool?
3341 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3342 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3343 Select(N.getOperand(0));
3344 SelectAddress(N.getOperand(2), AM);
3346 SelectAddress(N.getOperand(2), AM);
3347 Select(N.getOperand(0));
3349 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3354 default: assert(0 && "Cannot truncstore this type!");
3355 case MVT::i1: Opc = X86::MOV8mr; break;
3357 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
3358 Opc = X86::FpST32m; break;
3361 std::vector<std::pair<unsigned, unsigned> > RP;
3362 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3363 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3364 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3365 std::sort(RP.begin(), RP.end());
3367 Tmp1 = 0; // Silence a warning.
3368 for (unsigned i = 0; i != 3; ++i)
3369 switch (RP[2-i].second) {
3370 default: assert(0 && "Unknown operand number!");
3371 case 0: Select(N.getOperand(0)); break;
3372 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3373 case 2: SelectAddress(N.getOperand(2), AM); break;
3376 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3382 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3384 switch (CN->getValueType(0)) {
3385 default: assert(0 && "Invalid type for operation!");
3387 case MVT::i8: Opc = X86::MOV8mi; break;
3388 case MVT::i16: Opc = X86::MOV16mi; break;
3389 case MVT::i32: Opc = X86::MOV32mi; break;
3392 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3393 Select(N.getOperand(0));
3394 SelectAddress(N.getOperand(2), AM);
3396 SelectAddress(N.getOperand(2), AM);
3397 Select(N.getOperand(0));
3399 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3402 } else if (GlobalAddressSDNode *GA =
3403 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3404 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3406 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3407 Select(N.getOperand(0));
3408 SelectAddress(N.getOperand(2), AM);
3410 SelectAddress(N.getOperand(2), AM);
3411 Select(N.getOperand(0));
3413 GlobalValue *GV = GA->getGlobal();
3414 // For Darwin, external and weak symbols are indirect, so we want to load
3415 // the value at address GV, not the value of GV itself.
3416 if (Subtarget->getIndirectExternAndWeakGlobals() &&
3417 (GV->hasWeakLinkage() || GV->isExternal())) {
3418 Tmp1 = MakeReg(MVT::i32);
3419 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3420 .addGlobalAddress(GV, false, 0);
3421 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3423 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3428 // Check to see if this is a load/op/store combination.
3429 if (TryToFoldLoadOpStore(Node))
3432 switch (N.getOperand(1).getValueType()) {
3433 default: assert(0 && "Cannot store this type!");
3435 case MVT::i8: Opc = X86::MOV8mr; break;
3436 case MVT::i16: Opc = X86::MOV16mr; break;
3437 case MVT::i32: Opc = X86::MOV32mr; break;
3438 case MVT::f32: Opc = X86::MOVSSmr; break;
3439 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FpST64m; break;
3442 std::vector<std::pair<unsigned, unsigned> > RP;
3443 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3444 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3445 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3446 std::sort(RP.begin(), RP.end());
3448 Tmp1 = 0; // Silence a warning.
3449 for (unsigned i = 0; i != 3; ++i)
3450 switch (RP[2-i].second) {
3451 default: assert(0 && "Unknown operand number!");
3452 case 0: Select(N.getOperand(0)); break;
3453 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3454 case 2: SelectAddress(N.getOperand(2), AM); break;
3457 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3460 case ISD::CALLSEQ_START:
3461 Select(N.getOperand(0));
3463 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3464 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3466 case ISD::CALLSEQ_END:
3467 Select(N.getOperand(0));
3470 Select(N.getOperand(0)); // Select the chain.
3472 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3473 if (Align == 0) Align = 1;
3475 // Turn the byte code into # iterations
3478 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3479 unsigned Val = ValC->getValue() & 255;
3481 // If the value is a constant, then we can potentially use larger sets.
3482 switch (Align & 3) {
3483 case 2: // WORD aligned
3484 CountReg = MakeReg(MVT::i32);
3485 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3486 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3488 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3489 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3491 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3492 Opcode = X86::REP_STOSW;
3494 case 0: // DWORD aligned
3495 CountReg = MakeReg(MVT::i32);
3496 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3497 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3499 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3500 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3502 Val = (Val << 8) | Val;
3503 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3504 Opcode = X86::REP_STOSD;
3506 default: // BYTE aligned
3507 CountReg = SelectExpr(Node->getOperand(3));
3508 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3509 Opcode = X86::REP_STOSB;
3513 // If it's not a constant value we are storing, just fall back. We could
3514 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3515 unsigned ValReg = SelectExpr(Node->getOperand(2));
3516 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3517 CountReg = SelectExpr(Node->getOperand(3));
3518 Opcode = X86::REP_STOSB;
3521 // No matter what the alignment is, we put the destination in EDI, and the
3523 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3524 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3525 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3526 BuildMI(BB, Opcode, 0);
3530 Select(N.getOperand(0)); // Select the chain.
3532 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3533 if (Align == 0) Align = 1;
3535 // Turn the byte code into # iterations
3538 switch (Align & 3) {
3539 case 2: // WORD aligned
3540 CountReg = MakeReg(MVT::i32);
3541 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3542 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3544 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3545 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3547 Opcode = X86::REP_MOVSW;
3549 case 0: // DWORD aligned
3550 CountReg = MakeReg(MVT::i32);
3551 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3552 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3554 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3555 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3557 Opcode = X86::REP_MOVSD;
3559 default: // BYTE aligned
3560 CountReg = SelectExpr(Node->getOperand(3));
3561 Opcode = X86::REP_MOVSB;
3565 // No matter what the alignment is, we put the source in ESI, the
3566 // destination in EDI, and the count in ECX.
3567 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3568 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3569 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3570 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3571 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3572 BuildMI(BB, Opcode, 0);
3575 case ISD::WRITEPORT:
3576 if (Node->getOperand(2).getValueType() != MVT::i16) {
3577 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3580 Select(Node->getOperand(0)); // Emit the chain.
3582 Tmp1 = SelectExpr(Node->getOperand(1));
3583 switch (Node->getOperand(1).getValueType()) {
3585 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3586 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3589 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3590 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3593 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3594 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3597 std::cerr << "llvm.writeport: invalid data type for X86 target";
3601 // If the port is a single-byte constant, use the immediate form.
3602 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3603 if ((CN->getValue() & 255) == CN->getValue()) {
3604 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3608 // Otherwise, move the I/O port address into the DX register.
3609 unsigned Reg = SelectExpr(Node->getOperand(2));
3610 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3611 BuildMI(BB, Opc, 0);
3614 assert(0 && "Should not be reached!");
3618 /// createX86ISelPattern - This pass converts an LLVM function
3619 /// into a machine code representation using pattern matching and a machine
3620 /// description file.
3622 FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
3623 return new ISel(TM);