1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DAG pattern matching instruction selector for X86,
11 // converting from a legalized dag to a X86 dag.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "x86-isel"
17 #include "X86InstrBuilder.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/FunctionLoweringInfo.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/Statistic.h"
43 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45 //===----------------------------------------------------------------------===//
46 // Pattern Matcher Implementation
47 //===----------------------------------------------------------------------===//
50 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
51 /// SDValue's instead of register numbers for the leaves of the matched
53 struct X86ISelAddressMode {
59 // This is really a union, discriminated by BaseType!
67 const GlobalValue *GV;
69 const BlockAddress *BlockAddr;
72 unsigned Align; // CP alignment.
73 unsigned char SymbolFlags; // X86II::MO_*
76 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0),
77 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
78 SymbolFlags(X86II::MO_NO_FLAG) {
81 bool hasSymbolicDisplacement() const {
82 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
85 bool hasBaseOrIndexReg() const {
86 return IndexReg.getNode() != 0 || Base_Reg.getNode() != 0;
89 /// isRIPRelative - Return true if this addressing mode is already RIP
91 bool isRIPRelative() const {
92 if (BaseType != RegBase) return false;
93 if (RegisterSDNode *RegNode =
94 dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode()))
95 return RegNode->getReg() == X86::RIP;
99 void setBaseReg(SDValue Reg) {
105 dbgs() << "X86ISelAddressMode " << this << '\n';
106 dbgs() << "Base_Reg ";
107 if (Base_Reg.getNode() != 0)
108 Base_Reg.getNode()->dump();
111 dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n'
112 << " Scale" << Scale << '\n'
114 if (IndexReg.getNode() != 0)
115 IndexReg.getNode()->dump();
118 dbgs() << " Disp " << Disp << '\n'
135 dbgs() << " JT" << JT << " Align" << Align << '\n';
141 //===--------------------------------------------------------------------===//
142 /// ISel - X86 specific code to select X86 machine instructions for
143 /// SelectionDAG operations.
145 class X86DAGToDAGISel : public SelectionDAGISel {
146 /// X86Lowering - This object fully describes how to lower LLVM code to an
147 /// X86-specific SelectionDAG.
148 const X86TargetLowering &X86Lowering;
150 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
151 /// make the right decision when generating code for different targets.
152 const X86Subtarget *Subtarget;
154 /// OptForSize - If true, selector should try to optimize for code size
155 /// instead of performance.
159 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
160 : SelectionDAGISel(tm, OptLevel),
161 X86Lowering(*tm.getTargetLowering()),
162 Subtarget(&tm.getSubtarget<X86Subtarget>()),
165 virtual const char *getPassName() const {
166 return "X86 DAG->DAG Instruction Selection";
169 virtual void EmitFunctionEntryCode();
171 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
173 virtual void PreprocessISelDAG();
175 inline bool immSext8(SDNode *N) const {
176 return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue());
179 // i64immSExt32 predicate - True if the 64-bit immediate fits in a 32-bit
180 // sign extended field.
181 inline bool i64immSExt32(SDNode *N) const {
182 uint64_t v = cast<ConstantSDNode>(N)->getZExtValue();
183 return (int64_t)v == (int32_t)v;
186 // Include the pieces autogenerated from the target description.
187 #include "X86GenDAGISel.inc"
190 SDNode *Select(SDNode *N);
191 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
192 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
193 SDNode *SelectAtomicLoadArith(SDNode *Node, EVT NVT);
195 bool FoldOffsetIntoAddress(uint64_t Offset, X86ISelAddressMode &AM);
196 bool MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM);
197 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
198 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
199 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
201 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
202 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
203 SDValue &Scale, SDValue &Index, SDValue &Disp,
205 bool SelectLEAAddr(SDValue N, SDValue &Base,
206 SDValue &Scale, SDValue &Index, SDValue &Disp,
208 bool SelectTLSADDRAddr(SDValue N, SDValue &Base,
209 SDValue &Scale, SDValue &Index, SDValue &Disp,
211 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
212 SDValue &Base, SDValue &Scale,
213 SDValue &Index, SDValue &Disp,
215 SDValue &NodeWithChain);
217 bool TryFoldLoad(SDNode *P, SDValue N,
218 SDValue &Base, SDValue &Scale,
219 SDValue &Index, SDValue &Disp,
222 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
223 /// inline asm expressions.
224 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
226 std::vector<SDValue> &OutOps);
228 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
230 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
231 SDValue &Scale, SDValue &Index,
232 SDValue &Disp, SDValue &Segment) {
233 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
234 CurDAG->getTargetFrameIndex(AM.Base_FrameIndex, TLI.getPointerTy()) :
236 Scale = getI8Imm(AM.Scale);
238 // These are 32-bit even in 64-bit mode since RIP relative offset
241 Disp = CurDAG->getTargetGlobalAddress(AM.GV, DebugLoc(),
245 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
246 AM.Align, AM.Disp, AM.SymbolFlags);
248 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
249 else if (AM.JT != -1)
250 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
251 else if (AM.BlockAddr)
252 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
253 true, AM.SymbolFlags);
255 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
257 if (AM.Segment.getNode())
258 Segment = AM.Segment;
260 Segment = CurDAG->getRegister(0, MVT::i32);
263 /// getI8Imm - Return a target constant with the specified value, of type
265 inline SDValue getI8Imm(unsigned Imm) {
266 return CurDAG->getTargetConstant(Imm, MVT::i8);
269 /// getI32Imm - Return a target constant with the specified value, of type
271 inline SDValue getI32Imm(unsigned Imm) {
272 return CurDAG->getTargetConstant(Imm, MVT::i32);
275 /// getGlobalBaseReg - Return an SDNode that returns the value of
276 /// the global base register. Output instructions required to
277 /// initialize the global base register, if necessary.
279 SDNode *getGlobalBaseReg();
281 /// getTargetMachine - Return a reference to the TargetMachine, casted
282 /// to the target-specific type.
283 const X86TargetMachine &getTargetMachine() {
284 return static_cast<const X86TargetMachine &>(TM);
287 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
288 /// to the target-specific type.
289 const X86InstrInfo *getInstrInfo() {
290 return getTargetMachine().getInstrInfo();
297 X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
298 if (OptLevel == CodeGenOpt::None) return false;
303 if (N.getOpcode() != ISD::LOAD)
306 // If N is a load, do additional profitability checks.
308 switch (U->getOpcode()) {
321 SDValue Op1 = U->getOperand(1);
323 // If the other operand is a 8-bit immediate we should fold the immediate
324 // instead. This reduces code size.
326 // movl 4(%esp), %eax
330 // addl 4(%esp), %eax
331 // The former is 2 bytes shorter. In case where the increment is 1, then
332 // the saving can be 4 bytes (by using incl %eax).
333 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
334 if (Imm->getAPIntValue().isSignedIntN(8))
337 // If the other operand is a TLS address, we should fold it instead.
340 // leal i@NTPOFF(%eax), %eax
342 // movl $i@NTPOFF, %eax
344 // if the block also has an access to a second TLS address this will save
346 // FIXME: This is probably also true for non TLS addresses.
347 if (Op1.getOpcode() == X86ISD::Wrapper) {
348 SDValue Val = Op1.getOperand(0);
349 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
359 /// MoveBelowCallOrigChain - Replace the original chain operand of the call with
360 /// load's chain operand and move load below the call's chain operand.
361 static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
362 SDValue Call, SDValue OrigChain) {
363 SmallVector<SDValue, 8> Ops;
364 SDValue Chain = OrigChain.getOperand(0);
365 if (Chain.getNode() == Load.getNode())
366 Ops.push_back(Load.getOperand(0));
368 assert(Chain.getOpcode() == ISD::TokenFactor &&
369 "Unexpected chain operand");
370 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
371 if (Chain.getOperand(i).getNode() == Load.getNode())
372 Ops.push_back(Load.getOperand(0));
374 Ops.push_back(Chain.getOperand(i));
376 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
377 MVT::Other, &Ops[0], Ops.size());
379 Ops.push_back(NewChain);
381 for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i)
382 Ops.push_back(OrigChain.getOperand(i));
383 CurDAG->UpdateNodeOperands(OrigChain.getNode(), &Ops[0], Ops.size());
384 CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0),
385 Load.getOperand(1), Load.getOperand(2));
387 Ops.push_back(SDValue(Load.getNode(), 1));
388 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
389 Ops.push_back(Call.getOperand(i));
390 CurDAG->UpdateNodeOperands(Call.getNode(), &Ops[0], Ops.size());
393 /// isCalleeLoad - Return true if call address is a load and it can be
394 /// moved below CALLSEQ_START and the chains leading up to the call.
395 /// Return the CALLSEQ_START by reference as a second output.
396 /// In the case of a tail call, there isn't a callseq node between the call
397 /// chain and the load.
398 static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
399 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
401 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
404 LD->getAddressingMode() != ISD::UNINDEXED ||
405 LD->getExtensionType() != ISD::NON_EXTLOAD)
408 // Now let's find the callseq_start.
409 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
410 if (!Chain.hasOneUse())
412 Chain = Chain.getOperand(0);
415 if (!Chain.getNumOperands())
417 if (Chain.getOperand(0).getNode() == Callee.getNode())
419 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
420 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
421 Callee.getValue(1).hasOneUse())
426 void X86DAGToDAGISel::PreprocessISelDAG() {
427 // OptForSize is used in pattern predicates that isel is matching.
428 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
430 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
431 E = CurDAG->allnodes_end(); I != E; ) {
432 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
434 if (OptLevel != CodeGenOpt::None &&
435 (N->getOpcode() == X86ISD::CALL ||
436 N->getOpcode() == X86ISD::TC_RETURN)) {
437 /// Also try moving call address load from outside callseq_start to just
438 /// before the call to allow it to be folded.
456 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
457 SDValue Chain = N->getOperand(0);
458 SDValue Load = N->getOperand(1);
459 if (!isCalleeLoad(Load, Chain, HasCallSeq))
461 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
466 // Lower fpround and fpextend nodes that target the FP stack to be store and
467 // load to the stack. This is a gross hack. We would like to simply mark
468 // these as being illegal, but when we do that, legalize produces these when
469 // it expands calls, then expands these in the same legalize pass. We would
470 // like dag combine to be able to hack on these between the call expansion
471 // and the node legalization. As such this pass basically does "really
472 // late" legalization of these inline with the X86 isel pass.
473 // FIXME: This should only happen when not compiled with -O0.
474 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
477 // If the source and destination are SSE registers, then this is a legal
478 // conversion that should not be lowered.
479 EVT SrcVT = N->getOperand(0).getValueType();
480 EVT DstVT = N->getValueType(0);
481 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
482 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
483 if (SrcIsSSE && DstIsSSE)
486 if (!SrcIsSSE && !DstIsSSE) {
487 // If this is an FPStack extension, it is a noop.
488 if (N->getOpcode() == ISD::FP_EXTEND)
490 // If this is a value-preserving FPStack truncation, it is a noop.
491 if (N->getConstantOperandVal(1))
495 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
496 // FPStack has extload and truncstore. SSE can fold direct loads into other
497 // operations. Based on this, decide what we want to do.
499 if (N->getOpcode() == ISD::FP_ROUND)
500 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
502 MemVT = SrcIsSSE ? SrcVT : DstVT;
504 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
505 DebugLoc dl = N->getDebugLoc();
507 // FIXME: optimize the case where the src/dest is a load or store?
508 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
510 MemTmp, MachinePointerInfo(), MemVT,
512 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
513 MachinePointerInfo(),
514 MemVT, false, false, 0);
516 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
517 // extload we created. This will cause general havok on the dag because
518 // anything below the conversion could be folded into other existing nodes.
519 // To avoid invalidating 'I', back it up to the convert node.
521 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
523 // Now that we did that, the node is dead. Increment the iterator to the
524 // next node to process, then delete N.
526 CurDAG->DeleteNode(N);
531 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
532 /// the main function.
533 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
534 MachineFrameInfo *MFI) {
535 const TargetInstrInfo *TII = TM.getInstrInfo();
536 if (Subtarget->isTargetCygMing()) {
538 Subtarget->is64Bit() ? X86::WINCALL64pcrel32 : X86::CALLpcrel32;
539 BuildMI(BB, DebugLoc(),
540 TII->get(CallOp)).addExternalSymbol("__main");
544 void X86DAGToDAGISel::EmitFunctionEntryCode() {
545 // If this is main, emit special code for main.
546 if (const Function *Fn = MF->getFunction())
547 if (Fn->hasExternalLinkage() && Fn->getName() == "main")
548 EmitSpecialCodeForMain(MF->begin(), MF->getFrameInfo());
551 static bool isDispSafeForFrameIndex(int64_t Val) {
552 // On 64-bit platforms, we can run into an issue where a frame index
553 // includes a displacement that, when added to the explicit displacement,
554 // will overflow the displacement field. Assuming that the frame index
555 // displacement fits into a 31-bit integer (which is only slightly more
556 // aggressive than the current fundamental assumption that it fits into
557 // a 32-bit integer), a 31-bit disp should always be safe.
558 return isInt<31>(Val);
561 bool X86DAGToDAGISel::FoldOffsetIntoAddress(uint64_t Offset,
562 X86ISelAddressMode &AM) {
563 int64_t Val = AM.Disp + Offset;
564 CodeModel::Model M = TM.getCodeModel();
565 if (Subtarget->is64Bit()) {
566 if (!X86::isOffsetSuitableForCodeModel(Val, M,
567 AM.hasSymbolicDisplacement()))
569 // In addition to the checks required for a register base, check that
570 // we do not try to use an unsafe Disp with a frame index.
571 if (AM.BaseType == X86ISelAddressMode::FrameIndexBase &&
572 !isDispSafeForFrameIndex(Val))
580 bool X86DAGToDAGISel::MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
581 SDValue Address = N->getOperand(1);
583 // load gs:0 -> GS segment register.
584 // load fs:0 -> FS segment register.
586 // This optimization is valid because the GNU TLS model defines that
587 // gs:0 (or fs:0 on X86-64) contains its own address.
588 // For more information see http://people.redhat.com/drepper/tls.pdf
589 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
590 if (C->getSExtValue() == 0 && AM.Segment.getNode() == 0 &&
591 Subtarget->isTargetELF())
592 switch (N->getPointerInfo().getAddrSpace()) {
594 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
597 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
604 /// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
605 /// into an addressing mode. These wrap things that will resolve down into a
606 /// symbol reference. If no match is possible, this returns true, otherwise it
608 bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
609 // If the addressing mode already has a symbol as the displacement, we can
610 // never match another symbol.
611 if (AM.hasSymbolicDisplacement())
614 SDValue N0 = N.getOperand(0);
615 CodeModel::Model M = TM.getCodeModel();
617 // Handle X86-64 rip-relative addresses. We check this before checking direct
618 // folding because RIP is preferable to non-RIP accesses.
619 if (Subtarget->is64Bit() &&
620 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
621 // they cannot be folded into immediate fields.
622 // FIXME: This can be improved for kernel and other models?
623 (M == CodeModel::Small || M == CodeModel::Kernel) &&
624 // Base and index reg must be 0 in order to use %rip as base and lowering
626 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
627 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
628 X86ISelAddressMode Backup = AM;
629 AM.GV = G->getGlobal();
630 AM.SymbolFlags = G->getTargetFlags();
631 if (FoldOffsetIntoAddress(G->getOffset(), AM)) {
635 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
636 X86ISelAddressMode Backup = AM;
637 AM.CP = CP->getConstVal();
638 AM.Align = CP->getAlignment();
639 AM.SymbolFlags = CP->getTargetFlags();
640 if (FoldOffsetIntoAddress(CP->getOffset(), AM)) {
644 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
645 AM.ES = S->getSymbol();
646 AM.SymbolFlags = S->getTargetFlags();
647 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
648 AM.JT = J->getIndex();
649 AM.SymbolFlags = J->getTargetFlags();
651 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
652 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
655 if (N.getOpcode() == X86ISD::WrapperRIP)
656 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
660 // Handle the case when globals fit in our immediate field: This is true for
661 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
662 // mode, this results in a non-RIP-relative computation.
663 if (!Subtarget->is64Bit() ||
664 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
665 TM.getRelocationModel() == Reloc::Static)) {
666 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
667 AM.GV = G->getGlobal();
668 AM.Disp += G->getOffset();
669 AM.SymbolFlags = G->getTargetFlags();
670 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
671 AM.CP = CP->getConstVal();
672 AM.Align = CP->getAlignment();
673 AM.Disp += CP->getOffset();
674 AM.SymbolFlags = CP->getTargetFlags();
675 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
676 AM.ES = S->getSymbol();
677 AM.SymbolFlags = S->getTargetFlags();
678 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
679 AM.JT = J->getIndex();
680 AM.SymbolFlags = J->getTargetFlags();
682 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
683 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
691 /// MatchAddress - Add the specified node to the specified addressing mode,
692 /// returning true if it cannot be done. This just pattern matches for the
694 bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
695 if (MatchAddressRecursively(N, AM, 0))
698 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
699 // a smaller encoding and avoids a scaled-index.
701 AM.BaseType == X86ISelAddressMode::RegBase &&
702 AM.Base_Reg.getNode() == 0) {
703 AM.Base_Reg = AM.IndexReg;
707 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
708 // because it has a smaller encoding.
709 // TODO: Which other code models can use this?
710 if (TM.getCodeModel() == CodeModel::Small &&
711 Subtarget->is64Bit() &&
713 AM.BaseType == X86ISelAddressMode::RegBase &&
714 AM.Base_Reg.getNode() == 0 &&
715 AM.IndexReg.getNode() == 0 &&
716 AM.SymbolFlags == X86II::MO_NO_FLAG &&
717 AM.hasSymbolicDisplacement())
718 AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
723 bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
725 DebugLoc dl = N.getDebugLoc();
727 dbgs() << "MatchAddress: ";
732 return MatchAddressBase(N, AM);
734 // If this is already a %rip relative address, we can only merge immediates
735 // into it. Instead of handling this in every case, we handle it here.
736 // RIP relative addressing: %rip + 32-bit displacement!
737 if (AM.isRIPRelative()) {
738 // FIXME: JumpTable and ExternalSymbol address currently don't like
739 // displacements. It isn't very important, but this should be fixed for
741 if (!AM.ES && AM.JT != -1) return true;
743 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N))
744 if (!FoldOffsetIntoAddress(Cst->getSExtValue(), AM))
749 switch (N.getOpcode()) {
751 case ISD::Constant: {
752 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
753 if (!FoldOffsetIntoAddress(Val, AM))
758 case X86ISD::Wrapper:
759 case X86ISD::WrapperRIP:
760 if (!MatchWrapper(N, AM))
765 if (!MatchLoadInAddress(cast<LoadSDNode>(N), AM))
769 case ISD::FrameIndex:
770 if (AM.BaseType == X86ISelAddressMode::RegBase &&
771 AM.Base_Reg.getNode() == 0 &&
772 (!Subtarget->is64Bit() || isDispSafeForFrameIndex(AM.Disp))) {
773 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
774 AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
780 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
784 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
785 unsigned Val = CN->getZExtValue();
786 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
787 // that the base operand remains free for further matching. If
788 // the base doesn't end up getting used, a post-processing step
789 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
790 if (Val == 1 || Val == 2 || Val == 3) {
792 SDValue ShVal = N.getNode()->getOperand(0);
794 // Okay, we know that we have a scale by now. However, if the scaled
795 // value is an add of something and a constant, we can fold the
796 // constant into the disp field here.
797 if (CurDAG->isBaseWithConstantOffset(ShVal)) {
798 AM.IndexReg = ShVal.getNode()->getOperand(0);
799 ConstantSDNode *AddVal =
800 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
801 uint64_t Disp = AddVal->getSExtValue() << Val;
802 if (!FoldOffsetIntoAddress(Disp, AM))
814 // A mul_lohi where we need the low part can be folded as a plain multiply.
815 if (N.getResNo() != 0) break;
818 case X86ISD::MUL_IMM:
819 // X*[3,5,9] -> X+X*[2,4,8]
820 if (AM.BaseType == X86ISelAddressMode::RegBase &&
821 AM.Base_Reg.getNode() == 0 &&
822 AM.IndexReg.getNode() == 0) {
824 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
825 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
826 CN->getZExtValue() == 9) {
827 AM.Scale = unsigned(CN->getZExtValue())-1;
829 SDValue MulVal = N.getNode()->getOperand(0);
832 // Okay, we know that we have a scale by now. However, if the scaled
833 // value is an add of something and a constant, we can fold the
834 // constant into the disp field here.
835 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
836 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
837 Reg = MulVal.getNode()->getOperand(0);
838 ConstantSDNode *AddVal =
839 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
840 uint64_t Disp = AddVal->getSExtValue() * CN->getZExtValue();
841 if (FoldOffsetIntoAddress(Disp, AM))
842 Reg = N.getNode()->getOperand(0);
844 Reg = N.getNode()->getOperand(0);
847 AM.IndexReg = AM.Base_Reg = Reg;
854 // Given A-B, if A can be completely folded into the address and
855 // the index field with the index field unused, use -B as the index.
856 // This is a win if a has multiple parts that can be folded into
857 // the address. Also, this saves a mov if the base register has
858 // other uses, since it avoids a two-address sub instruction, however
859 // it costs an additional mov if the index register has other uses.
861 // Add an artificial use to this node so that we can keep track of
862 // it if it gets CSE'd with a different node.
863 HandleSDNode Handle(N);
865 // Test if the LHS of the sub can be folded.
866 X86ISelAddressMode Backup = AM;
867 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
871 // Test if the index field is free for use.
872 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
878 SDValue RHS = Handle.getValue().getNode()->getOperand(1);
879 // If the RHS involves a register with multiple uses, this
880 // transformation incurs an extra mov, due to the neg instruction
881 // clobbering its operand.
882 if (!RHS.getNode()->hasOneUse() ||
883 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
884 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
885 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
886 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
887 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
889 // If the base is a register with multiple uses, this
890 // transformation may save a mov.
891 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
892 AM.Base_Reg.getNode() &&
893 !AM.Base_Reg.getNode()->hasOneUse()) ||
894 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
896 // If the folded LHS was interesting, this transformation saves
897 // address arithmetic.
898 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
899 ((AM.Disp != 0) && (Backup.Disp == 0)) +
900 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
902 // If it doesn't look like it may be an overall win, don't do it.
908 // Ok, the transformation is legal and appears profitable. Go for it.
909 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
910 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
914 // Insert the new nodes into the topological ordering.
915 if (Zero.getNode()->getNodeId() == -1 ||
916 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
917 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
918 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
920 if (Neg.getNode()->getNodeId() == -1 ||
921 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
922 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
923 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
929 // Add an artificial use to this node so that we can keep track of
930 // it if it gets CSE'd with a different node.
931 HandleSDNode Handle(N);
933 X86ISelAddressMode Backup = AM;
934 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
935 !MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1))
939 // Try again after commuting the operands.
940 if (!MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&&
941 !MatchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1))
945 // If we couldn't fold both operands into the address at the same time,
946 // see if we can just put each operand into a register and fold at least
948 if (AM.BaseType == X86ISelAddressMode::RegBase &&
949 !AM.Base_Reg.getNode() &&
950 !AM.IndexReg.getNode()) {
951 N = Handle.getValue();
952 AM.Base_Reg = N.getOperand(0);
953 AM.IndexReg = N.getOperand(1);
957 N = Handle.getValue();
962 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
963 if (CurDAG->isBaseWithConstantOffset(N)) {
964 X86ISelAddressMode Backup = AM;
965 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
967 // Start with the LHS as an addr mode.
968 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
969 !FoldOffsetIntoAddress(CN->getSExtValue(), AM))
976 // Perform some heroic transforms on an and of a constant-count shift
977 // with a constant to enable use of the scaled offset field.
979 SDValue Shift = N.getOperand(0);
980 if (Shift.getNumOperands() != 2) break;
982 // Scale must not be used already.
983 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
985 SDValue X = Shift.getOperand(0);
986 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
987 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
988 if (!C1 || !C2) break;
990 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
991 // allows us to convert the shift and and into an h-register extract and
993 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
994 unsigned ScaleLog = 8 - C1->getZExtValue();
995 if (ScaleLog > 0 && ScaleLog < 4 &&
996 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
997 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
998 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
999 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1001 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1003 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1004 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1007 // Insert the new nodes into the topological ordering.
1008 if (Eight.getNode()->getNodeId() == -1 ||
1009 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1010 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1011 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1013 if (Mask.getNode()->getNodeId() == -1 ||
1014 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1015 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1016 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1018 if (Srl.getNode()->getNodeId() == -1 ||
1019 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1020 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1021 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1023 if (And.getNode()->getNodeId() == -1 ||
1024 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1025 CurDAG->RepositionNode(N.getNode(), And.getNode());
1026 And.getNode()->setNodeId(N.getNode()->getNodeId());
1028 if (ShlCount.getNode()->getNodeId() == -1 ||
1029 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1030 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1031 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1033 if (Shl.getNode()->getNodeId() == -1 ||
1034 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1035 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1036 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1038 CurDAG->ReplaceAllUsesWith(N, Shl);
1040 AM.Scale = (1 << ScaleLog);
1045 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1046 // allows us to fold the shift into this addressing mode.
1047 if (Shift.getOpcode() != ISD::SHL) break;
1049 // Not likely to be profitable if either the AND or SHIFT node has more
1050 // than one use (unless all uses are for address computation). Besides,
1051 // isel mechanism requires their node ids to be reused.
1052 if (!N.hasOneUse() || !Shift.hasOneUse())
1055 // Verify that the shift amount is something we can fold.
1056 unsigned ShiftCst = C1->getZExtValue();
1057 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1060 // Get the new AND mask, this folds to a constant.
1061 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1062 SDValue(C2, 0), SDValue(C1, 0));
1063 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1065 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1066 NewAND, SDValue(C1, 0));
1068 // Insert the new nodes into the topological ordering.
1069 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1070 CurDAG->RepositionNode(X.getNode(), C1);
1071 C1->setNodeId(X.getNode()->getNodeId());
1073 if (NewANDMask.getNode()->getNodeId() == -1 ||
1074 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1075 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1076 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1078 if (NewAND.getNode()->getNodeId() == -1 ||
1079 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1080 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1081 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1083 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1084 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1085 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1086 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1089 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
1091 AM.Scale = 1 << ShiftCst;
1092 AM.IndexReg = NewAND;
1097 return MatchAddressBase(N, AM);
1100 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1101 /// specified addressing mode without any further recursion.
1102 bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
1103 // Is the base register already occupied?
1104 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) {
1105 // If so, check to see if the scale index register is set.
1106 if (AM.IndexReg.getNode() == 0) {
1112 // Otherwise, we cannot select it.
1116 // Default, generate it as a register.
1117 AM.BaseType = X86ISelAddressMode::RegBase;
1122 /// SelectAddr - returns true if it is able pattern match an addressing mode.
1123 /// It returns the operands which make up the maximal addressing mode it can
1124 /// match by reference.
1126 /// Parent is the parent node of the addr operand that is being matched. It
1127 /// is always a load, store, atomic node, or null. It is only null when
1128 /// checking memory operands for inline asm nodes.
1129 bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
1130 SDValue &Scale, SDValue &Index,
1131 SDValue &Disp, SDValue &Segment) {
1132 X86ISelAddressMode AM;
1135 // This list of opcodes are all the nodes that have an "addr:$ptr" operand
1136 // that are not a MemSDNode, and thus don't have proper addrspace info.
1137 Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme
1138 Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores
1139 Parent->getOpcode() != X86ISD::TLSCALL) { // Fixme
1140 unsigned AddrSpace =
1141 cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace();
1142 // AddrSpace 256 -> GS, 257 -> FS.
1143 if (AddrSpace == 256)
1144 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1145 if (AddrSpace == 257)
1146 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1149 if (MatchAddress(N, AM))
1152 EVT VT = N.getValueType();
1153 if (AM.BaseType == X86ISelAddressMode::RegBase) {
1154 if (!AM.Base_Reg.getNode())
1155 AM.Base_Reg = CurDAG->getRegister(0, VT);
1158 if (!AM.IndexReg.getNode())
1159 AM.IndexReg = CurDAG->getRegister(0, VT);
1161 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1165 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1166 /// match a load whose top elements are either undef or zeros. The load flavor
1167 /// is derived from the type of N, which is either v4f32 or v2f64.
1170 /// PatternChainNode: this is the matched node that has a chain input and
1172 bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
1173 SDValue N, SDValue &Base,
1174 SDValue &Scale, SDValue &Index,
1175 SDValue &Disp, SDValue &Segment,
1176 SDValue &PatternNodeWithChain) {
1177 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1178 PatternNodeWithChain = N.getOperand(0);
1179 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1180 PatternNodeWithChain.hasOneUse() &&
1181 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1182 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
1183 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
1184 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
1190 // Also handle the case where we explicitly require zeros in the top
1191 // elements. This is a vector shuffle from the zero vector.
1192 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
1193 // Check to see if the top elements are all zeros (or bitcast of zeros).
1194 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
1195 N.getOperand(0).getNode()->hasOneUse() &&
1196 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
1197 N.getOperand(0).getOperand(0).hasOneUse() &&
1198 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1199 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
1200 // Okay, this is a zero extending load. Fold it.
1201 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1202 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
1204 PatternNodeWithChain = SDValue(LD, 0);
1211 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1212 /// mode it matches can be cost effectively emitted as an LEA instruction.
1213 bool X86DAGToDAGISel::SelectLEAAddr(SDValue N,
1214 SDValue &Base, SDValue &Scale,
1215 SDValue &Index, SDValue &Disp,
1217 X86ISelAddressMode AM;
1219 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1221 SDValue Copy = AM.Segment;
1222 SDValue T = CurDAG->getRegister(0, MVT::i32);
1224 if (MatchAddress(N, AM))
1226 assert (T == AM.Segment);
1229 EVT VT = N.getValueType();
1230 unsigned Complexity = 0;
1231 if (AM.BaseType == X86ISelAddressMode::RegBase)
1232 if (AM.Base_Reg.getNode())
1235 AM.Base_Reg = CurDAG->getRegister(0, VT);
1236 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1239 if (AM.IndexReg.getNode())
1242 AM.IndexReg = CurDAG->getRegister(0, VT);
1244 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1249 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1250 // to a LEA. This is determined with some expermentation but is by no means
1251 // optimal (especially for code size consideration). LEA is nice because of
1252 // its three-address nature. Tweak the cost function again when we can run
1253 // convertToThreeAddress() at register allocation time.
1254 if (AM.hasSymbolicDisplacement()) {
1255 // For X86-64, we should always use lea to materialize RIP relative
1257 if (Subtarget->is64Bit())
1263 if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode()))
1266 // If it isn't worth using an LEA, reject it.
1267 if (Complexity <= 2)
1270 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1274 /// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1275 bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue N, SDValue &Base,
1276 SDValue &Scale, SDValue &Index,
1277 SDValue &Disp, SDValue &Segment) {
1278 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1279 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1281 X86ISelAddressMode AM;
1282 AM.GV = GA->getGlobal();
1283 AM.Disp += GA->getOffset();
1284 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType());
1285 AM.SymbolFlags = GA->getTargetFlags();
1287 if (N.getValueType() == MVT::i32) {
1289 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1291 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1294 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1299 bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
1300 SDValue &Base, SDValue &Scale,
1301 SDValue &Index, SDValue &Disp,
1303 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1304 !IsProfitableToFold(N, P, P) ||
1305 !IsLegalToFold(N, P, P, OptLevel))
1308 return SelectAddr(N.getNode(),
1309 N.getOperand(1), Base, Scale, Index, Disp, Segment);
1312 /// getGlobalBaseReg - Return an SDNode that returns the value of
1313 /// the global base register. Output instructions required to
1314 /// initialize the global base register, if necessary.
1316 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1317 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
1318 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
1321 SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1322 SDValue Chain = Node->getOperand(0);
1323 SDValue In1 = Node->getOperand(1);
1324 SDValue In2L = Node->getOperand(2);
1325 SDValue In2H = Node->getOperand(3);
1326 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1327 if (!SelectAddr(Node, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1329 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1330 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1331 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1332 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1333 MVT::i32, MVT::i32, MVT::Other, Ops,
1334 array_lengthof(Ops));
1335 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1339 // FIXME: Figure out some way to unify this with the 'or' and other code
1341 SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
1342 if (Node->hasAnyUseOfValue(0))
1345 // Optimize common patterns for __sync_add_and_fetch and
1346 // __sync_sub_and_fetch where the result is not used. This allows us
1347 // to use "lock" version of add, sub, inc, dec instructions.
1348 // FIXME: Do not use special instructions but instead add the "lock"
1349 // prefix to the target node somehow. The extra information will then be
1350 // transferred to machine instruction and it denotes the prefix.
1351 SDValue Chain = Node->getOperand(0);
1352 SDValue Ptr = Node->getOperand(1);
1353 SDValue Val = Node->getOperand(2);
1354 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1355 if (!SelectAddr(Node, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1358 bool isInc = false, isDec = false, isSub = false, isCN = false;
1359 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1360 if (CN && CN->getSExtValue() == (int32_t)CN->getSExtValue()) {
1362 int64_t CNVal = CN->getSExtValue();
1365 else if (CNVal == -1)
1367 else if (CNVal >= 0)
1368 Val = CurDAG->getTargetConstant(CNVal, NVT);
1371 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1373 } else if (Val.hasOneUse() &&
1374 Val.getOpcode() == ISD::SUB &&
1375 X86::isZeroNode(Val.getOperand(0))) {
1377 Val = Val.getOperand(1);
1380 DebugLoc dl = Node->getDebugLoc();
1382 switch (NVT.getSimpleVT().SimpleTy) {
1386 Opc = X86::LOCK_INC8m;
1388 Opc = X86::LOCK_DEC8m;
1391 Opc = X86::LOCK_SUB8mi;
1393 Opc = X86::LOCK_SUB8mr;
1396 Opc = X86::LOCK_ADD8mi;
1398 Opc = X86::LOCK_ADD8mr;
1403 Opc = X86::LOCK_INC16m;
1405 Opc = X86::LOCK_DEC16m;
1408 if (immSext8(Val.getNode()))
1409 Opc = X86::LOCK_SUB16mi8;
1411 Opc = X86::LOCK_SUB16mi;
1413 Opc = X86::LOCK_SUB16mr;
1416 if (immSext8(Val.getNode()))
1417 Opc = X86::LOCK_ADD16mi8;
1419 Opc = X86::LOCK_ADD16mi;
1421 Opc = X86::LOCK_ADD16mr;
1426 Opc = X86::LOCK_INC32m;
1428 Opc = X86::LOCK_DEC32m;
1431 if (immSext8(Val.getNode()))
1432 Opc = X86::LOCK_SUB32mi8;
1434 Opc = X86::LOCK_SUB32mi;
1436 Opc = X86::LOCK_SUB32mr;
1439 if (immSext8(Val.getNode()))
1440 Opc = X86::LOCK_ADD32mi8;
1442 Opc = X86::LOCK_ADD32mi;
1444 Opc = X86::LOCK_ADD32mr;
1449 Opc = X86::LOCK_INC64m;
1451 Opc = X86::LOCK_DEC64m;
1453 Opc = X86::LOCK_SUB64mr;
1455 if (immSext8(Val.getNode()))
1456 Opc = X86::LOCK_SUB64mi8;
1457 else if (i64immSExt32(Val.getNode()))
1458 Opc = X86::LOCK_SUB64mi32;
1461 Opc = X86::LOCK_ADD64mr;
1463 if (immSext8(Val.getNode()))
1464 Opc = X86::LOCK_ADD64mi8;
1465 else if (i64immSExt32(Val.getNode()))
1466 Opc = X86::LOCK_ADD64mi32;
1472 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1474 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1475 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1476 if (isInc || isDec) {
1477 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1478 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1479 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
1480 SDValue RetVals[] = { Undef, Ret };
1481 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1483 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1484 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1485 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
1486 SDValue RetVals[] = { Undef, Ret };
1487 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1513 static const unsigned int AtomicOpcTbl[AtomicOpcEnd][AtomicSzEnd] = {
1537 X86::LOCK_AND64mi32,
1550 X86::LOCK_XOR64mi32,
1555 SDNode *X86DAGToDAGISel::SelectAtomicLoadArith(SDNode *Node, EVT NVT) {
1556 if (Node->hasAnyUseOfValue(0))
1559 // Optimize common patterns for __sync_or_and_fetch and similar arith
1560 // operations where the result is not used. This allows us to use the "lock"
1561 // version of the arithmetic instruction.
1562 // FIXME: Same as for 'add' and 'sub', try to merge those down here.
1563 SDValue Chain = Node->getOperand(0);
1564 SDValue Ptr = Node->getOperand(1);
1565 SDValue Val = Node->getOperand(2);
1566 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1567 if (!SelectAddr(Node, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1570 // Which index into the table.
1572 switch (Node->getOpcode()) {
1573 case ISD::ATOMIC_LOAD_OR:
1576 case ISD::ATOMIC_LOAD_AND:
1579 case ISD::ATOMIC_LOAD_XOR:
1587 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1588 if (CN && (int32_t)CN->getSExtValue() == CN->getSExtValue()) {
1590 Val = CurDAG->getTargetConstant(CN->getSExtValue(), NVT);
1594 switch (NVT.getSimpleVT().SimpleTy) {
1598 Opc = AtomicOpcTbl[Op][ConstantI8];
1600 Opc = AtomicOpcTbl[Op][I8];
1604 if (immSext8(Val.getNode()))
1605 Opc = AtomicOpcTbl[Op][SextConstantI16];
1607 Opc = AtomicOpcTbl[Op][ConstantI16];
1609 Opc = AtomicOpcTbl[Op][I16];
1613 if (immSext8(Val.getNode()))
1614 Opc = AtomicOpcTbl[Op][SextConstantI32];
1616 Opc = AtomicOpcTbl[Op][ConstantI32];
1618 Opc = AtomicOpcTbl[Op][I32];
1621 Opc = AtomicOpcTbl[Op][I64];
1623 if (immSext8(Val.getNode()))
1624 Opc = AtomicOpcTbl[Op][SextConstantI64];
1625 else if (i64immSExt32(Val.getNode()))
1626 Opc = AtomicOpcTbl[Op][ConstantI64];
1631 assert(Opc != 0 && "Invalid arith lock transform!");
1633 DebugLoc dl = Node->getDebugLoc();
1634 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1636 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1637 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1638 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1639 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1640 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
1641 SDValue RetVals[] = { Undef, Ret };
1642 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1645 /// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1646 /// any uses which require the SF or OF bits to be accurate.
1647 static bool HasNoSignedComparisonUses(SDNode *N) {
1648 // Examine each user of the node.
1649 for (SDNode::use_iterator UI = N->use_begin(),
1650 UE = N->use_end(); UI != UE; ++UI) {
1651 // Only examine CopyToReg uses.
1652 if (UI->getOpcode() != ISD::CopyToReg)
1654 // Only examine CopyToReg uses that copy to EFLAGS.
1655 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1658 // Examine each user of the CopyToReg use.
1659 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1660 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1661 // Only examine the Flag result.
1662 if (FlagUI.getUse().getResNo() != 1) continue;
1663 // Anything unusual: assume conservatively.
1664 if (!FlagUI->isMachineOpcode()) return false;
1665 // Examine the opcode of the user.
1666 switch (FlagUI->getMachineOpcode()) {
1667 // These comparisons don't treat the most significant bit specially.
1668 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1669 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1670 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1671 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1672 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1673 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
1674 case X86::CMOVA16rr: case X86::CMOVA16rm:
1675 case X86::CMOVA32rr: case X86::CMOVA32rm:
1676 case X86::CMOVA64rr: case X86::CMOVA64rm:
1677 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1678 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1679 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1680 case X86::CMOVB16rr: case X86::CMOVB16rm:
1681 case X86::CMOVB32rr: case X86::CMOVB32rm:
1682 case X86::CMOVB64rr: case X86::CMOVB64rm:
1683 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1684 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1685 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1686 case X86::CMOVE16rr: case X86::CMOVE16rm:
1687 case X86::CMOVE32rr: case X86::CMOVE32rm:
1688 case X86::CMOVE64rr: case X86::CMOVE64rm:
1689 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1690 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1691 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1692 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1693 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1694 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1695 case X86::CMOVP16rr: case X86::CMOVP16rm:
1696 case X86::CMOVP32rr: case X86::CMOVP32rm:
1697 case X86::CMOVP64rr: case X86::CMOVP64rm:
1699 // Anything else: assume conservatively.
1700 default: return false;
1707 SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
1708 EVT NVT = Node->getValueType(0);
1710 unsigned Opcode = Node->getOpcode();
1711 DebugLoc dl = Node->getDebugLoc();
1713 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
1715 if (Node->isMachineOpcode()) {
1716 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
1717 return NULL; // Already selected.
1722 case X86ISD::GlobalBaseReg:
1723 return getGlobalBaseReg();
1725 case X86ISD::ATOMOR64_DAG:
1726 return SelectAtomic64(Node, X86::ATOMOR6432);
1727 case X86ISD::ATOMXOR64_DAG:
1728 return SelectAtomic64(Node, X86::ATOMXOR6432);
1729 case X86ISD::ATOMADD64_DAG:
1730 return SelectAtomic64(Node, X86::ATOMADD6432);
1731 case X86ISD::ATOMSUB64_DAG:
1732 return SelectAtomic64(Node, X86::ATOMSUB6432);
1733 case X86ISD::ATOMNAND64_DAG:
1734 return SelectAtomic64(Node, X86::ATOMNAND6432);
1735 case X86ISD::ATOMAND64_DAG:
1736 return SelectAtomic64(Node, X86::ATOMAND6432);
1737 case X86ISD::ATOMSWAP64_DAG:
1738 return SelectAtomic64(Node, X86::ATOMSWAP6432);
1740 case ISD::ATOMIC_LOAD_ADD: {
1741 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1746 case ISD::ATOMIC_LOAD_XOR:
1747 case ISD::ATOMIC_LOAD_AND:
1748 case ISD::ATOMIC_LOAD_OR: {
1749 SDNode *RetVal = SelectAtomicLoadArith(Node, NVT);
1757 // For operations of the form (x << C1) op C2, check if we can use a smaller
1758 // encoding for C2 by transforming it into (x op (C2>>C1)) << C1.
1759 SDValue N0 = Node->getOperand(0);
1760 SDValue N1 = Node->getOperand(1);
1762 if (N0->getOpcode() != ISD::SHL || !N0->hasOneUse())
1765 // i8 is unshrinkable, i16 should be promoted to i32.
1766 if (NVT != MVT::i32 && NVT != MVT::i64)
1769 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N1);
1770 ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1771 if (!Cst || !ShlCst)
1774 int64_t Val = Cst->getSExtValue();
1775 uint64_t ShlVal = ShlCst->getZExtValue();
1777 // Make sure that we don't change the operation by removing bits.
1778 // This only matters for OR and XOR, AND is unaffected.
1779 if (Opcode != ISD::AND && ((Val >> ShlVal) << ShlVal) != Val)
1782 unsigned ShlOp, Op = 0;
1785 // Check the minimum bitwidth for the new constant.
1786 // TODO: AND32ri is the same as AND64ri32 with zext imm.
1787 // TODO: MOV32ri+OR64r is cheaper than MOV64ri64+OR64rr
1788 // TODO: Using 16 and 8 bit operations is also possible for or32 & xor32.
1789 if (!isInt<8>(Val) && isInt<8>(Val >> ShlVal))
1791 else if (!isInt<32>(Val) && isInt<32>(Val >> ShlVal))
1794 // Bail if there is no smaller encoding.
1798 switch (NVT.getSimpleVT().SimpleTy) {
1799 default: llvm_unreachable("Unsupported VT!");
1801 assert(CstVT == MVT::i8);
1802 ShlOp = X86::SHL32ri;
1805 case ISD::AND: Op = X86::AND32ri8; break;
1806 case ISD::OR: Op = X86::OR32ri8; break;
1807 case ISD::XOR: Op = X86::XOR32ri8; break;
1811 assert(CstVT == MVT::i8 || CstVT == MVT::i32);
1812 ShlOp = X86::SHL64ri;
1815 case ISD::AND: Op = CstVT==MVT::i8? X86::AND64ri8 : X86::AND64ri32; break;
1816 case ISD::OR: Op = CstVT==MVT::i8? X86::OR64ri8 : X86::OR64ri32; break;
1817 case ISD::XOR: Op = CstVT==MVT::i8? X86::XOR64ri8 : X86::XOR64ri32; break;
1822 // Emit the smaller op and the shift.
1823 SDValue NewCst = CurDAG->getTargetConstant(Val >> ShlVal, CstVT);
1824 SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst);
1825 return CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0),
1829 case X86ISD::UMUL: {
1830 SDValue N0 = Node->getOperand(0);
1831 SDValue N1 = Node->getOperand(1);
1834 switch (NVT.getSimpleVT().SimpleTy) {
1835 default: llvm_unreachable("Unsupported VT!");
1836 case MVT::i8: LoReg = X86::AL; Opc = X86::MUL8r; break;
1837 case MVT::i16: LoReg = X86::AX; Opc = X86::MUL16r; break;
1838 case MVT::i32: LoReg = X86::EAX; Opc = X86::MUL32r; break;
1839 case MVT::i64: LoReg = X86::RAX; Opc = X86::MUL64r; break;
1842 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1843 N0, SDValue()).getValue(1);
1845 SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::i32);
1846 SDValue Ops[] = {N1, InFlag};
1847 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops, 2);
1849 ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0));
1850 ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1));
1851 ReplaceUses(SDValue(Node, 2), SDValue(CNode, 2));
1855 case ISD::SMUL_LOHI:
1856 case ISD::UMUL_LOHI: {
1857 SDValue N0 = Node->getOperand(0);
1858 SDValue N1 = Node->getOperand(1);
1860 bool isSigned = Opcode == ISD::SMUL_LOHI;
1862 switch (NVT.getSimpleVT().SimpleTy) {
1863 default: llvm_unreachable("Unsupported VT!");
1864 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1865 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1866 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1867 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1870 switch (NVT.getSimpleVT().SimpleTy) {
1871 default: llvm_unreachable("Unsupported VT!");
1872 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1873 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1874 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1875 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1879 unsigned LoReg, HiReg;
1880 switch (NVT.getSimpleVT().SimpleTy) {
1881 default: llvm_unreachable("Unsupported VT!");
1882 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1883 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1884 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1885 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1888 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1889 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1890 // Multiply is commmutative.
1892 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1897 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1898 N0, SDValue()).getValue(1);
1901 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1904 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops,
1905 array_lengthof(Ops));
1906 InFlag = SDValue(CNode, 1);
1908 // Update the chain.
1909 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1911 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag);
1912 InFlag = SDValue(CNode, 0);
1915 // Prevent use of AH in a REX instruction by referencing AX instead.
1916 if (HiReg == X86::AH && Subtarget->is64Bit() &&
1917 !SDValue(Node, 1).use_empty()) {
1918 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1919 X86::AX, MVT::i16, InFlag);
1920 InFlag = Result.getValue(2);
1921 // Get the low part if needed. Don't use getCopyFromReg for aliasing
1923 if (!SDValue(Node, 0).use_empty())
1924 ReplaceUses(SDValue(Node, 1),
1925 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
1927 // Shift AX down 8 bits.
1928 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1930 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1931 // Then truncate it down to i8.
1932 ReplaceUses(SDValue(Node, 1),
1933 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
1935 // Copy the low half of the result, if it is needed.
1936 if (!SDValue(Node, 0).use_empty()) {
1937 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1938 LoReg, NVT, InFlag);
1939 InFlag = Result.getValue(2);
1940 ReplaceUses(SDValue(Node, 0), Result);
1941 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
1943 // Copy the high half of the result, if it is needed.
1944 if (!SDValue(Node, 1).use_empty()) {
1945 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1946 HiReg, NVT, InFlag);
1947 InFlag = Result.getValue(2);
1948 ReplaceUses(SDValue(Node, 1), Result);
1949 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
1956 case ISD::UDIVREM: {
1957 SDValue N0 = Node->getOperand(0);
1958 SDValue N1 = Node->getOperand(1);
1960 bool isSigned = Opcode == ISD::SDIVREM;
1962 switch (NVT.getSimpleVT().SimpleTy) {
1963 default: llvm_unreachable("Unsupported VT!");
1964 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1965 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1966 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1967 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1970 switch (NVT.getSimpleVT().SimpleTy) {
1971 default: llvm_unreachable("Unsupported VT!");
1972 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1973 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1974 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1975 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1979 unsigned LoReg, HiReg, ClrReg;
1980 unsigned ClrOpcode, SExtOpcode;
1981 switch (NVT.getSimpleVT().SimpleTy) {
1982 default: llvm_unreachable("Unsupported VT!");
1984 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
1986 SExtOpcode = X86::CBW;
1989 LoReg = X86::AX; HiReg = X86::DX;
1990 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
1991 SExtOpcode = X86::CWD;
1994 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
1995 ClrOpcode = X86::MOV32r0;
1996 SExtOpcode = X86::CDQ;
1999 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
2000 ClrOpcode = X86::MOV64r0;
2001 SExtOpcode = X86::CQO;
2005 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
2006 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
2007 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
2010 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
2011 // Special case for div8, just use a move with zero extension to AX to
2012 // clear the upper 8 bits (AH).
2013 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
2014 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
2015 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
2017 SDValue(CurDAG->getMachineNode(X86::MOVZX32rm8, dl, MVT::i32,
2019 array_lengthof(Ops)), 0);
2020 Chain = Move.getValue(1);
2021 ReplaceUses(N0.getValue(1), Chain);
2024 SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, N0),0);
2025 Chain = CurDAG->getEntryNode();
2027 Chain = CurDAG->getCopyToReg(Chain, dl, X86::EAX, Move, SDValue());
2028 InFlag = Chain.getValue(1);
2031 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
2032 LoReg, N0, SDValue()).getValue(1);
2033 if (isSigned && !signBitIsZero) {
2034 // Sign extend the low part into the high part.
2036 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Glue, InFlag),0);
2038 // Zero out the high part, effectively zero extending the input.
2040 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
2041 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
2042 ClrNode, InFlag).getValue(1);
2047 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2050 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops,
2051 array_lengthof(Ops));
2052 InFlag = SDValue(CNode, 1);
2053 // Update the chain.
2054 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
2057 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag), 0);
2060 // Prevent use of AH in a REX instruction by referencing AX instead.
2061 // Shift it down 8 bits.
2062 if (HiReg == X86::AH && Subtarget->is64Bit() &&
2063 !SDValue(Node, 1).use_empty()) {
2064 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2065 X86::AX, MVT::i16, InFlag);
2066 InFlag = Result.getValue(2);
2068 // If we also need AL (the quotient), get it by extracting a subreg from
2069 // Result. The fast register allocator does not like multiple CopyFromReg
2070 // nodes using aliasing registers.
2071 if (!SDValue(Node, 0).use_empty())
2072 ReplaceUses(SDValue(Node, 0),
2073 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2075 // Shift AX right by 8 bits instead of using AH.
2076 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
2078 CurDAG->getTargetConstant(8, MVT::i8)),
2080 ReplaceUses(SDValue(Node, 1),
2081 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2083 // Copy the division (low) result, if it is needed.
2084 if (!SDValue(Node, 0).use_empty()) {
2085 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2086 LoReg, NVT, InFlag);
2087 InFlag = Result.getValue(2);
2088 ReplaceUses(SDValue(Node, 0), Result);
2089 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
2091 // Copy the remainder (high) result, if it is needed.
2092 if (!SDValue(Node, 1).use_empty()) {
2093 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2094 HiReg, NVT, InFlag);
2095 InFlag = Result.getValue(2);
2096 ReplaceUses(SDValue(Node, 1), Result);
2097 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
2103 SDValue N0 = Node->getOperand(0);
2104 SDValue N1 = Node->getOperand(1);
2106 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2107 // use a smaller encoding.
2108 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
2109 HasNoSignedComparisonUses(Node))
2110 // Look past the truncate if CMP is the only use of it.
2111 N0 = N0.getOperand(0);
2112 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2113 N0.getValueType() != MVT::i8 &&
2114 X86::isZeroNode(N1)) {
2115 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2118 // For example, convert "testl %eax, $8" to "testb %al, $8"
2119 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2120 (!(C->getZExtValue() & 0x80) ||
2121 HasNoSignedComparisonUses(Node))) {
2122 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2123 SDValue Reg = N0.getNode()->getOperand(0);
2125 // On x86-32, only the ABCD registers have 8-bit subregisters.
2126 if (!Subtarget->is64Bit()) {
2127 TargetRegisterClass *TRC = 0;
2128 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2129 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2130 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2131 default: llvm_unreachable("Unsupported TEST operand type!");
2133 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
2134 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2135 Reg.getValueType(), Reg, RC), 0);
2138 // Extract the l-register.
2139 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
2143 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
2146 // For example, "testl %eax, $2048" to "testb %ah, $8".
2147 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2148 (!(C->getZExtValue() & 0x8000) ||
2149 HasNoSignedComparisonUses(Node))) {
2150 // Shift the immediate right by 8 bits.
2151 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2153 SDValue Reg = N0.getNode()->getOperand(0);
2155 // Put the value in an ABCD register.
2156 TargetRegisterClass *TRC = 0;
2157 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2158 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2159 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2160 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2161 default: llvm_unreachable("Unsupported TEST operand type!");
2163 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
2164 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2165 Reg.getValueType(), Reg, RC), 0);
2167 // Extract the h-register.
2168 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl,
2171 // Emit a testb. No special NOREX tricks are needed since there's
2172 // only one GPR operand!
2173 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2174 Subreg, ShiftedImm);
2177 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2178 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
2179 N0.getValueType() != MVT::i16 &&
2180 (!(C->getZExtValue() & 0x8000) ||
2181 HasNoSignedComparisonUses(Node))) {
2182 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2183 SDValue Reg = N0.getNode()->getOperand(0);
2185 // Extract the 16-bit subregister.
2186 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl,
2190 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
2193 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2194 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
2195 N0.getValueType() == MVT::i64 &&
2196 (!(C->getZExtValue() & 0x80000000) ||
2197 HasNoSignedComparisonUses(Node))) {
2198 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2199 SDValue Reg = N0.getNode()->getOperand(0);
2201 // Extract the 32-bit subregister.
2202 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl,
2206 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
2213 SDNode *ResNode = SelectCode(Node);
2215 DEBUG(dbgs() << "=> ";
2216 if (ResNode == NULL || ResNode == Node)
2219 ResNode->dump(CurDAG);
2225 bool X86DAGToDAGISel::
2226 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2227 std::vector<SDValue> &OutOps) {
2228 SDValue Op0, Op1, Op2, Op3, Op4;
2229 switch (ConstraintCode) {
2230 case 'o': // offsetable ??
2231 case 'v': // not offsetable ??
2232 default: return true;
2234 if (!SelectAddr(0, Op, Op0, Op1, Op2, Op3, Op4))
2239 OutOps.push_back(Op0);
2240 OutOps.push_back(Op1);
2241 OutOps.push_back(Op2);
2242 OutOps.push_back(Op3);
2243 OutOps.push_back(Op4);
2247 /// createX86ISelDag - This pass converts a legalized DAG into a
2248 /// X86-specific DAG, ready for instruction scheduling.
2250 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2251 llvm::CodeGenOpt::Level OptLevel) {
2252 return new X86DAGToDAGISel(TM, OptLevel);