blob: c028376774eba5d7bb18bdf3c24a9ae9428bdaa5 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Daniel Dunbar5da58852009-11-10 18:24:37 +000015// Force NDEBUG on in any optimized build on Darwin.
16//
17// FIXME: This is a huge hack, to work around ridiculously awful compile times
18// on this file with gcc-4.2 on Darwin, in Release mode.
Daniel Dunbar253e9b22009-11-11 00:28:38 +000019#if (!defined(__llvm__) && defined(__APPLE__) && \
20 defined(__OPTIMIZE__) && !defined(NDEBUG))
Daniel Dunbar5da58852009-11-10 18:24:37 +000021#define NDEBUG
22#endif
23
Evan Cheng2ef88a02006-08-07 22:28:20 +000024#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000025#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000026#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000027#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000028#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000030#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000031#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000032#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000033#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000034#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000035#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000036#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000037#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000038#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000039#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000040#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000042#include "llvm/CodeGen/SelectionDAGISel.h"
43#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000045#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000046#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000047#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000048#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000049#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000050#include "llvm/ADT/Statistic.h"
51using namespace llvm;
52
Chris Lattner95b2c7d2006-12-19 22:59:26 +000053STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
54
Chris Lattnerc961eea2005-11-16 01:54:32 +000055//===----------------------------------------------------------------------===//
56// Pattern Matcher Implementation
57//===----------------------------------------------------------------------===//
58
59namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000060 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000061 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 /// tree.
63 struct X86ISelAddressMode {
64 enum {
65 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000066 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 } BaseType;
68
69 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000071 int FrameIndex;
72 } Base;
73
74 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000075 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000076 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000077 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000079 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000080 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000081 const char *ES;
82 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000083 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000084 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000085
86 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000087 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000088 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000089 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000090 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000091
92 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000093 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000094 }
Chris Lattner18c59872009-06-27 04:16:01 +000095
96 bool hasBaseOrIndexReg() const {
97 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
98 }
99
100 /// isRIPRelative - Return true if this addressing mode is already RIP
101 /// relative.
102 bool isRIPRelative() const {
103 if (BaseType != RegBase) return false;
104 if (RegisterSDNode *RegNode =
105 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
106 return RegNode->getReg() == X86::RIP;
107 return false;
108 }
109
110 void setBaseReg(SDValue Reg) {
111 BaseType = RegBase;
112 Base.Reg = Reg;
113 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000114
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000115 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000116 errs() << "X86ISelAddressMode " << this << '\n';
117 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000118 if (Base.Reg.getNode() != 0)
119 Base.Reg.getNode()->dump();
120 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000121 errs() << "nul";
122 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
123 << " Scale" << Scale << '\n'
124 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000125 if (IndexReg.getNode() != 0)
126 IndexReg.getNode()->dump();
127 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000128 errs() << "nul";
129 errs() << " Disp " << Disp << '\n'
130 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000131 if (GV)
132 GV->dump();
133 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000134 errs() << "nul";
135 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000136 if (CP)
137 CP->dump();
138 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000139 errs() << "nul";
140 errs() << '\n'
141 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000142 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000143 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000144 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000145 errs() << "nul";
146 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000147 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000148 };
149}
150
151namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000152 //===--------------------------------------------------------------------===//
153 /// ISel - X86 specific code to select X86 machine instructions for
154 /// SelectionDAG operations.
155 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000156 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000157 /// X86Lowering - This object fully describes how to lower LLVM code to an
158 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000159 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160
161 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
162 /// make the right decision when generating code for different targets.
163 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000164
Evan Chengb7a75a52008-09-26 23:41:32 +0000165 /// OptForSize - If true, selector should try to optimize for code size
166 /// instead of performance.
167 bool OptForSize;
168
Chris Lattnerc961eea2005-11-16 01:54:32 +0000169 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000170 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000171 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000172 X86Lowering(*tm.getTargetLowering()),
173 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000174 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000175
176 virtual const char *getPassName() const {
177 return "X86 DAG->DAG Instruction Selection";
178 }
179
Evan Chengdb8d56b2008-06-30 20:45:06 +0000180 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000181 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000182 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000183
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000184 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
185
Evan Cheng884c70c2008-11-27 00:49:46 +0000186 virtual
187 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000188
Chris Lattnerc961eea2005-11-16 01:54:32 +0000189// Include the pieces autogenerated from the target description.
190#include "X86GenDAGISel.inc"
191
192 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000193 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000194 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000195 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000196
Rafael Espindola094fad32009-04-08 21:14:34 +0000197 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
198 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000199 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000200 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
201 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
202 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000203 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000205 SDValue &Scale, SDValue &Index, SDValue &Disp,
206 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
208 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000209 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
210 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
212 SDValue N, SDValue &Base, SDValue &Scale,
213 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000214 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue &InChain, SDValue &OutChain);
216 bool TryFoldLoad(SDValue P, SDValue N,
217 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000218 SDValue &Index, SDValue &Disp,
219 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000220 void PreprocessForRMW();
221 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000222
Chris Lattnerc0bad572006-06-08 18:03:49 +0000223 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
224 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000226 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000227 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000228
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000229 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
230
Dan Gohman475871a2008-07-27 21:46:04 +0000231 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
232 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000233 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000234 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
236 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000237 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000238 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000239 // These are 32-bit even in 64-bit mode since RIP relative offset
240 // is 32-bit.
241 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000243 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000246 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000251 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000252 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
253 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000256
257 if (AM.Segment.getNode())
258 Segment = AM.Segment;
259 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000261 }
262
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000263 /// getI8Imm - Return a target constant with the specified value, of type
264 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000265 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000267 }
268
Chris Lattnerc961eea2005-11-16 01:54:32 +0000269 /// getI16Imm - Return a target constant with the specified value, of type
270 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000271 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000273 }
274
275 /// getI32Imm - Return a target constant with the specified value, of type
276 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000279 }
Evan Chengf597dc72006-02-10 22:24:32 +0000280
Dan Gohman8b746962008-09-23 18:22:58 +0000281 /// getGlobalBaseReg - Return an SDNode that returns the value of
282 /// the global base register. Output instructions required to
283 /// initialize the global base register, if necessary.
284 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000285 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000286
Dan Gohmanc5534622009-06-03 20:20:00 +0000287 /// getTargetMachine - Return a reference to the TargetMachine, casted
288 /// to the target-specific type.
289 const X86TargetMachine &getTargetMachine() {
290 return static_cast<const X86TargetMachine &>(TM);
291 }
292
293 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
294 /// to the target-specific type.
295 const X86InstrInfo *getInstrInfo() {
296 return getTargetMachine().getInstrInfo();
297 }
298
Evan Cheng23addc02006-02-10 22:46:26 +0000299#ifndef NDEBUG
300 unsigned Indent;
301#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000302 };
303}
304
Evan Chengf4b4c412006-08-08 00:31:00 +0000305
Evan Cheng884c70c2008-11-27 00:49:46 +0000306bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
307 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000308 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000309
Evan Cheng884c70c2008-11-27 00:49:46 +0000310 if (U == Root)
311 switch (U->getOpcode()) {
312 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000313 case X86ISD::ADD:
314 case X86ISD::SUB:
315 case X86ISD::AND:
316 case X86ISD::XOR:
317 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000318 case ISD::ADD:
319 case ISD::ADDC:
320 case ISD::ADDE:
321 case ISD::AND:
322 case ISD::OR:
323 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000324 SDValue Op1 = U->getOperand(1);
325
Evan Cheng884c70c2008-11-27 00:49:46 +0000326 // If the other operand is a 8-bit immediate we should fold the immediate
327 // instead. This reduces code size.
328 // e.g.
329 // movl 4(%esp), %eax
330 // addl $4, %eax
331 // vs.
332 // movl $4, %eax
333 // addl 4(%esp), %eax
334 // The former is 2 bytes shorter. In case where the increment is 1, then
335 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000336 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000337 if (Imm->getAPIntValue().isSignedIntN(8))
338 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000339
340 // If the other operand is a TLS address, we should fold it instead.
341 // This produces
342 // movl %gs:0, %eax
343 // leal i@NTPOFF(%eax), %eax
344 // instead of
345 // movl $i@NTPOFF, %eax
346 // addl %gs:0, %eax
347 // if the block also has an access to a second TLS address this will save
348 // a load.
349 // FIXME: This is probably also true for non TLS addresses.
350 if (Op1.getOpcode() == X86ISD::Wrapper) {
351 SDValue Val = Op1.getOperand(0);
352 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
353 return false;
354 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000355 }
356 }
357
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000358 // Proceed to 'generic' cycle finder code
359 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000360}
361
Evan Cheng70e674e2006-08-28 20:10:17 +0000362/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
363/// and move load below the TokenFactor. Replace store's chain operand with
364/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000365static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000366 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000367 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000368 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
369 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000370 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000371 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000372 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000373 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
374 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
375 Load.getOperand(1),
376 Load.getOperand(2));
377 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000378 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000379}
380
Nate Begeman206a3572009-09-16 03:20:46 +0000381/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
382/// chain produced by the load must only be used by the store's chain operand,
383/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000384///
Dan Gohman475871a2008-07-27 21:46:04 +0000385static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
386 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000387 if (N.getOpcode() == ISD::BIT_CONVERT)
388 N = N.getOperand(0);
389
390 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
391 if (!LD || LD->isVolatile())
392 return false;
393 if (LD->getAddressingMode() != ISD::UNINDEXED)
394 return false;
395
396 ISD::LoadExtType ExtType = LD->getExtensionType();
397 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
398 return false;
399
400 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000401 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000402 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000403 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000404 Load = N;
405 return true;
406 }
407 return false;
408}
409
Evan Chengab6c3bb2008-08-25 21:27:18 +0000410/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
411/// operand and move load below the call's chain operand.
412static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000413 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000414 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000415 SDValue Chain = CallSeqStart.getOperand(0);
416 if (Chain.getNode() == Load.getNode())
417 Ops.push_back(Load.getOperand(0));
418 else {
419 assert(Chain.getOpcode() == ISD::TokenFactor &&
420 "Unexpected CallSeqStart chain operand");
421 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
422 if (Chain.getOperand(i).getNode() == Load.getNode())
423 Ops.push_back(Load.getOperand(0));
424 else
425 Ops.push_back(Chain.getOperand(i));
426 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000427 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000429 Ops.clear();
430 Ops.push_back(NewChain);
431 }
432 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
433 Ops.push_back(CallSeqStart.getOperand(i));
434 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000435 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
436 Load.getOperand(1), Load.getOperand(2));
437 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000438 Ops.push_back(SDValue(Load.getNode(), 1));
439 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000440 Ops.push_back(Call.getOperand(i));
441 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
442}
443
444/// isCalleeLoad - Return true if call address is a load and it can be
445/// moved below CALLSEQ_START and the chains leading up to the call.
446/// Return the CALLSEQ_START by reference as a second output.
447static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000448 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000449 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000450 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000451 if (!LD ||
452 LD->isVolatile() ||
453 LD->getAddressingMode() != ISD::UNINDEXED ||
454 LD->getExtensionType() != ISD::NON_EXTLOAD)
455 return false;
456
457 // Now let's find the callseq_start.
458 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
459 if (!Chain.hasOneUse())
460 return false;
461 Chain = Chain.getOperand(0);
462 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000463
464 if (Chain.getOperand(0).getNode() == Callee.getNode())
465 return true;
466 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000467 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
468 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000469 return true;
470 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000471}
472
473
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000474/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000475/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000476/// This allows the instruction selector to pick more read-modify-write
477/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000478///
479/// [Load chain]
480/// ^
481/// |
482/// [Load]
483/// ^ ^
484/// | |
485/// / \-
486/// / |
487/// [TokenFactor] [Op]
488/// ^ ^
489/// | |
490/// \ /
491/// \ /
492/// [Store]
493///
494/// The fact the store's chain operand != load's chain will prevent the
495/// (store (op (load))) instruction from being selected. We can transform it to:
496///
497/// [Load chain]
498/// ^
499/// |
500/// [TokenFactor]
501/// ^
502/// |
503/// [Load]
504/// ^ ^
505/// | |
506/// | \-
507/// | |
508/// | [Op]
509/// | ^
510/// | |
511/// \ /
512/// \ /
513/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000514void X86DAGToDAGISel::PreprocessForRMW() {
515 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
516 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000517 if (I->getOpcode() == X86ISD::CALL) {
518 /// Also try moving call address load from outside callseq_start to just
519 /// before the call to allow it to be folded.
520 ///
521 /// [Load chain]
522 /// ^
523 /// |
524 /// [Load]
525 /// ^ ^
526 /// | |
527 /// / \--
528 /// / |
529 ///[CALLSEQ_START] |
530 /// ^ |
531 /// | |
532 /// [LOAD/C2Reg] |
533 /// | |
534 /// \ /
535 /// \ /
536 /// [CALL]
537 SDValue Chain = I->getOperand(0);
538 SDValue Load = I->getOperand(1);
539 if (!isCalleeLoad(Load, Chain))
540 continue;
541 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
542 ++NumLoadMoved;
543 continue;
544 }
545
Evan Cheng8b2794a2006-10-13 21:14:26 +0000546 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000547 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000548 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000549
Gabor Greifba36cb52008-08-28 21:40:38 +0000550 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000551 continue;
552
Dan Gohman475871a2008-07-27 21:46:04 +0000553 SDValue N1 = I->getOperand(1);
554 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000555 if ((N1.getValueType().isFloatingPoint() &&
556 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000557 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000558 continue;
559
560 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000561 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000562 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000563 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000564 case ISD::ADD:
565 case ISD::MUL:
566 case ISD::AND:
567 case ISD::OR:
568 case ISD::XOR:
569 case ISD::ADDC:
570 case ISD::ADDE:
571 case ISD::VECTOR_SHUFFLE: {
572 SDValue N10 = N1.getOperand(0);
573 SDValue N11 = N1.getOperand(1);
574 RModW = isRMWLoad(N10, Chain, N2, Load);
575 if (!RModW)
576 RModW = isRMWLoad(N11, Chain, N2, Load);
577 break;
578 }
579 case ISD::SUB:
580 case ISD::SHL:
581 case ISD::SRA:
582 case ISD::SRL:
583 case ISD::ROTL:
584 case ISD::ROTR:
585 case ISD::SUBC:
586 case ISD::SUBE:
587 case X86ISD::SHLD:
588 case X86ISD::SHRD: {
589 SDValue N10 = N1.getOperand(0);
590 RModW = isRMWLoad(N10, Chain, N2, Load);
591 break;
592 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000593 }
594
Evan Cheng82a35b32006-08-29 06:44:17 +0000595 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000596 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000597 ++NumLoadMoved;
598 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000599 }
600}
601
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000602
603/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
604/// nodes that target the FP stack to be store and load to the stack. This is a
605/// gross hack. We would like to simply mark these as being illegal, but when
606/// we do that, legalize produces these when it expands calls, then expands
607/// these in the same legalize pass. We would like dag combine to be able to
608/// hack on these between the call expansion and the node legalization. As such
609/// this pass basically does "really late" legalization of these inline with the
610/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000611void X86DAGToDAGISel::PreprocessForFPConvert() {
612 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
613 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000614 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
615 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
616 continue;
617
618 // If the source and destination are SSE registers, then this is a legal
619 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000620 EVT SrcVT = N->getOperand(0).getValueType();
621 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000622 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
623 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
624 if (SrcIsSSE && DstIsSSE)
625 continue;
626
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000627 if (!SrcIsSSE && !DstIsSSE) {
628 // If this is an FPStack extension, it is a noop.
629 if (N->getOpcode() == ISD::FP_EXTEND)
630 continue;
631 // If this is a value-preserving FPStack truncation, it is a noop.
632 if (N->getConstantOperandVal(1))
633 continue;
634 }
635
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000636 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
637 // FPStack has extload and truncstore. SSE can fold direct loads into other
638 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000639 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000640 if (N->getOpcode() == ISD::FP_ROUND)
641 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
642 else
643 MemVT = SrcIsSSE ? SrcVT : DstVT;
644
Dan Gohmanf350b272008-08-23 02:25:05 +0000645 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000646 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000647
648 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000649 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000650 N->getOperand(0),
651 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000652 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000653 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000654
655 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
656 // extload we created. This will cause general havok on the dag because
657 // anything below the conversion could be folded into other existing nodes.
658 // To avoid invalidating 'I', back it up to the convert node.
659 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000660 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000661
662 // Now that we did that, the node is dead. Increment the iterator to the
663 // next node to process, then delete N.
664 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000665 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000666 }
667}
668
Chris Lattnerc961eea2005-11-16 01:54:32 +0000669/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
670/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000671void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000672 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000673 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000674
Bill Wendling98a366d2009-04-29 23:29:43 +0000675 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000676 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000677
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000678 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000679 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000680
Chris Lattnerc961eea2005-11-16 01:54:32 +0000681 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000682#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000683 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000684 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000685#endif
David Greene8ad4c002008-10-27 21:56:29 +0000686 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000687#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000688 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000689#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000690
Dan Gohmanf350b272008-08-23 02:25:05 +0000691 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000692}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000693
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000694/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
695/// the main function.
696void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
697 MachineFrameInfo *MFI) {
698 const TargetInstrInfo *TII = TM.getInstrInfo();
699 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000700 BuildMI(BB, DebugLoc::getUnknownLoc(),
701 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000702}
703
704void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
705 // If this is main, emit special code for main.
706 MachineBasicBlock *BB = MF.begin();
707 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
708 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
709}
710
Rafael Espindola094fad32009-04-08 21:14:34 +0000711
712bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
713 X86ISelAddressMode &AM) {
714 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
715 SDValue Segment = N.getOperand(0);
716
717 if (AM.Segment.getNode() == 0) {
718 AM.Segment = Segment;
719 return false;
720 }
721
722 return true;
723}
724
725bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
726 // This optimization is valid because the GNU TLS model defines that
727 // gs:0 (or fs:0 on X86-64) contains its own address.
728 // For more information see http://people.redhat.com/drepper/tls.pdf
729
730 SDValue Address = N.getOperand(1);
731 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
732 !MatchSegmentBaseAddress (Address, AM))
733 return false;
734
735 return true;
736}
737
Chris Lattner18c59872009-06-27 04:16:01 +0000738/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
739/// into an addressing mode. These wrap things that will resolve down into a
740/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000741/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000742bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000743 // If the addressing mode already has a symbol as the displacement, we can
744 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000745 if (AM.hasSymbolicDisplacement())
746 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000747
748 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000749 CodeModel::Model M = TM.getCodeModel();
750
Chris Lattner18c59872009-06-27 04:16:01 +0000751 // Handle X86-64 rip-relative addresses. We check this before checking direct
752 // folding because RIP is preferable to non-RIP accesses.
753 if (Subtarget->is64Bit() &&
754 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
755 // they cannot be folded into immediate fields.
756 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000757 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000758 // Base and index reg must be 0 in order to use %rip as base and lowering
759 // must allow RIP.
760 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000761 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
762 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000763 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000764 AM.GV = G->getGlobal();
765 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000766 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000767 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
768 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000769 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000770 AM.CP = CP->getConstVal();
771 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000772 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000773 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000774 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
775 AM.ES = S->getSymbol();
776 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000777 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000778 AM.JT = J->getIndex();
779 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000780 } else {
781 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000782 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000783 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000784
Chris Lattner18c59872009-06-27 04:16:01 +0000785 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000787 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000788 }
789
790 // Handle the case when globals fit in our immediate field: This is true for
791 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
792 // mode, this results in a non-RIP-relative computation.
793 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000794 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000795 TM.getRelocationModel() == Reloc::Static)) {
796 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
797 AM.GV = G->getGlobal();
798 AM.Disp += G->getOffset();
799 AM.SymbolFlags = G->getTargetFlags();
800 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
801 AM.CP = CP->getConstVal();
802 AM.Align = CP->getAlignment();
803 AM.Disp += CP->getOffset();
804 AM.SymbolFlags = CP->getTargetFlags();
805 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
806 AM.ES = S->getSymbol();
807 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000808 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000809 AM.JT = J->getIndex();
810 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000811 } else {
812 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000813 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000814 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000815 return false;
816 }
817
818 return true;
819}
820
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000821/// MatchAddress - Add the specified node to the specified addressing mode,
822/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000823/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000824bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
825 if (MatchAddressRecursively(N, AM, 0))
826 return true;
827
828 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
829 // a smaller encoding and avoids a scaled-index.
830 if (AM.Scale == 2 &&
831 AM.BaseType == X86ISelAddressMode::RegBase &&
832 AM.Base.Reg.getNode() == 0) {
833 AM.Base.Reg = AM.IndexReg;
834 AM.Scale = 1;
835 }
836
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000837 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
838 // because it has a smaller encoding.
839 // TODO: Which other code models can use this?
840 if (TM.getCodeModel() == CodeModel::Small &&
841 Subtarget->is64Bit() &&
842 AM.Scale == 1 &&
843 AM.BaseType == X86ISelAddressMode::RegBase &&
844 AM.Base.Reg.getNode() == 0 &&
845 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000846 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000847 AM.hasSymbolicDisplacement())
848 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
849
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000850 return false;
851}
852
853bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
854 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000855 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000856 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000857 DEBUG({
858 errs() << "MatchAddress: ";
859 AM.dump();
860 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000861 // Limit recursion.
862 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000863 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000864
865 CodeModel::Model M = TM.getCodeModel();
866
Chris Lattner18c59872009-06-27 04:16:01 +0000867 // If this is already a %rip relative address, we can only merge immediates
868 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000869 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000870 if (AM.isRIPRelative()) {
871 // FIXME: JumpTable and ExternalSymbol address currently don't like
872 // displacements. It isn't very important, but this should be fixed for
873 // consistency.
874 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000875
Chris Lattner18c59872009-06-27 04:16:01 +0000876 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
877 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000878 if (X86::isOffsetSuitableForCodeModel(Val, M,
879 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000880 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000881 return false;
882 }
883 }
884 return true;
885 }
886
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000887 switch (N.getOpcode()) {
888 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000889 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000890 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000891 if (!is64Bit ||
892 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
893 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000894 AM.Disp += Val;
895 return false;
896 }
897 break;
898 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000899
Rafael Espindola094fad32009-04-08 21:14:34 +0000900 case X86ISD::SegmentBaseAddress:
901 if (!MatchSegmentBaseAddress(N, AM))
902 return false;
903 break;
904
Rafael Espindola49a168d2009-04-12 21:55:03 +0000905 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000906 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000907 if (!MatchWrapper(N, AM))
908 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000909 break;
910
Rafael Espindola094fad32009-04-08 21:14:34 +0000911 case ISD::LOAD:
912 if (!MatchLoad(N, AM))
913 return false;
914 break;
915
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000917 if (AM.BaseType == X86ISelAddressMode::RegBase
918 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000919 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
920 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
921 return false;
922 }
923 break;
Evan Chengec693f72005-12-08 02:01:35 +0000924
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000925 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000926 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000927 break;
928
Gabor Greif93c53e52008-08-31 15:37:04 +0000929 if (ConstantSDNode
930 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000931 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000932 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
933 // that the base operand remains free for further matching. If
934 // the base doesn't end up getting used, a post-processing step
935 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000936 if (Val == 1 || Val == 2 || Val == 3) {
937 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000938 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000939
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000940 // Okay, we know that we have a scale by now. However, if the scaled
941 // value is an add of something and a constant, we can fold the
942 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000943 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
944 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
945 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000946 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000948 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000949 if (!is64Bit ||
950 X86::isOffsetSuitableForCodeModel(Disp, M,
951 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000952 AM.Disp = Disp;
953 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000954 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000955 } else {
956 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000957 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000958 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000959 }
960 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000961 }
Evan Chengec693f72005-12-08 02:01:35 +0000962
Dan Gohman83688052007-10-22 20:22:24 +0000963 case ISD::SMUL_LOHI:
964 case ISD::UMUL_LOHI:
965 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000966 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000967 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000968 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000969 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000970 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000971 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000972 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000973 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000974 if (ConstantSDNode
975 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000976 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
977 CN->getZExtValue() == 9) {
978 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000979
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000981 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000982
983 // Okay, we know that we have a scale by now. However, if the scaled
984 // value is an add of something and a constant, we can fold the
985 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000986 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
987 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
988 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000989 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000990 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000991 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000992 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000993 if (!is64Bit ||
994 X86::isOffsetSuitableForCodeModel(Disp, M,
995 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000996 AM.Disp = Disp;
997 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000998 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000999 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001000 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001001 }
1002
1003 AM.IndexReg = AM.Base.Reg = Reg;
1004 return false;
1005 }
Chris Lattner62412262007-02-04 20:18:17 +00001006 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001007 break;
1008
Dan Gohman3cd90a12009-05-11 18:02:53 +00001009 case ISD::SUB: {
1010 // Given A-B, if A can be completely folded into the address and
1011 // the index field with the index field unused, use -B as the index.
1012 // This is a win if a has multiple parts that can be folded into
1013 // the address. Also, this saves a mov if the base register has
1014 // other uses, since it avoids a two-address sub instruction, however
1015 // it costs an additional mov if the index register has other uses.
1016
1017 // Test if the LHS of the sub can be folded.
1018 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001019 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001020 AM = Backup;
1021 break;
1022 }
1023 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001024 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001025 AM = Backup;
1026 break;
1027 }
1028 int Cost = 0;
1029 SDValue RHS = N.getNode()->getOperand(1);
1030 // If the RHS involves a register with multiple uses, this
1031 // transformation incurs an extra mov, due to the neg instruction
1032 // clobbering its operand.
1033 if (!RHS.getNode()->hasOneUse() ||
1034 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1035 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1036 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1037 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001039 ++Cost;
1040 // If the base is a register with multiple uses, this
1041 // transformation may save a mov.
1042 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1043 AM.Base.Reg.getNode() &&
1044 !AM.Base.Reg.getNode()->hasOneUse()) ||
1045 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1046 --Cost;
1047 // If the folded LHS was interesting, this transformation saves
1048 // address arithmetic.
1049 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1050 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1051 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1052 --Cost;
1053 // If it doesn't look like it may be an overall win, don't do it.
1054 if (Cost >= 0) {
1055 AM = Backup;
1056 break;
1057 }
1058
1059 // Ok, the transformation is legal and appears profitable. Go for it.
1060 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1061 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1062 AM.IndexReg = Neg;
1063 AM.Scale = 1;
1064
1065 // Insert the new nodes into the topological ordering.
1066 if (Zero.getNode()->getNodeId() == -1 ||
1067 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1068 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1069 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1070 }
1071 if (Neg.getNode()->getNodeId() == -1 ||
1072 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1073 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1074 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1075 }
1076 return false;
1077 }
1078
Evan Cheng8e278262009-01-17 07:09:27 +00001079 case ISD::ADD: {
1080 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001081 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1082 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001083 return false;
1084 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001085 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1086 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001087 return false;
1088 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001089
1090 // If we couldn't fold both operands into the address at the same time,
1091 // see if we can just put each operand into a register and fold at least
1092 // the add.
1093 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1094 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001095 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001096 AM.Base.Reg = N.getNode()->getOperand(0);
1097 AM.IndexReg = N.getNode()->getOperand(1);
1098 AM.Scale = 1;
1099 return false;
1100 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001101 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001102 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001103
Chris Lattner62412262007-02-04 20:18:17 +00001104 case ISD::OR:
1105 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001106 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1107 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001108 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001109 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001110 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001111 // Address could not have picked a GV address for the displacement.
1112 AM.GV == NULL &&
1113 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001114 (!is64Bit ||
1115 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1116 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001117 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001118 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001119 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001120 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001121 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001122 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001123 }
1124 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001125
1126 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001127 // Perform some heroic transforms on an and of a constant-count shift
1128 // with a constant to enable use of the scaled offset field.
1129
Dan Gohman475871a2008-07-27 21:46:04 +00001130 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001131 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001132
Evan Cheng1314b002007-12-13 00:43:27 +00001133 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001134 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001135
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001136 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001137 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1138 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1139 if (!C1 || !C2) break;
1140
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001141 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1142 // allows us to convert the shift and and into an h-register extract and
1143 // a scaled index.
1144 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1145 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001146 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001147 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001149 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1150 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1151 X, Eight);
1152 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1153 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001155 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1156 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001157
1158 // Insert the new nodes into the topological ordering.
1159 if (Eight.getNode()->getNodeId() == -1 ||
1160 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1161 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1162 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1163 }
1164 if (Mask.getNode()->getNodeId() == -1 ||
1165 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1166 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1167 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1168 }
1169 if (Srl.getNode()->getNodeId() == -1 ||
1170 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1171 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1172 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1173 }
1174 if (And.getNode()->getNodeId() == -1 ||
1175 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1176 CurDAG->RepositionNode(N.getNode(), And.getNode());
1177 And.getNode()->setNodeId(N.getNode()->getNodeId());
1178 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001179 if (ShlCount.getNode()->getNodeId() == -1 ||
1180 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1181 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1182 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1183 }
1184 if (Shl.getNode()->getNodeId() == -1 ||
1185 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1186 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1187 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1188 }
1189 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001190 AM.IndexReg = And;
1191 AM.Scale = (1 << ScaleLog);
1192 return false;
1193 }
1194 }
1195
1196 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1197 // allows us to fold the shift into this addressing mode.
1198 if (Shift.getOpcode() != ISD::SHL) break;
1199
Evan Cheng1314b002007-12-13 00:43:27 +00001200 // Not likely to be profitable if either the AND or SHIFT node has more
1201 // than one use (unless all uses are for address computation). Besides,
1202 // isel mechanism requires their node ids to be reused.
1203 if (!N.hasOneUse() || !Shift.hasOneUse())
1204 break;
1205
1206 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001207 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001208 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1209 break;
1210
1211 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001212 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001213 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001214 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1215 NewANDMask);
1216 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001217 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001218
1219 // Insert the new nodes into the topological ordering.
1220 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1221 CurDAG->RepositionNode(X.getNode(), C1);
1222 C1->setNodeId(X.getNode()->getNodeId());
1223 }
1224 if (NewANDMask.getNode()->getNodeId() == -1 ||
1225 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1226 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1227 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1228 }
1229 if (NewAND.getNode()->getNodeId() == -1 ||
1230 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1231 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1232 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1233 }
1234 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1235 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1236 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1237 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1238 }
1239
Dan Gohman7b8e9642008-10-13 20:52:04 +00001240 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001241
1242 AM.Scale = 1 << ShiftCst;
1243 AM.IndexReg = NewAND;
1244 return false;
1245 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001246 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001247
Rafael Espindola523249f2009-03-31 16:16:57 +00001248 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001249}
1250
1251/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1252/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001253bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001254 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001255 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001256 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001257 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001258 AM.IndexReg = N;
1259 AM.Scale = 1;
1260 return false;
1261 }
1262
1263 // Otherwise, we cannot select it.
1264 return true;
1265 }
1266
1267 // Default, generate it as a register.
1268 AM.BaseType = X86ISelAddressMode::RegBase;
1269 AM.Base.Reg = N;
1270 return false;
1271}
1272
Evan Chengec693f72005-12-08 02:01:35 +00001273/// SelectAddr - returns true if it is able pattern match an addressing mode.
1274/// It returns the operands which make up the maximal addressing mode it can
1275/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001276bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1277 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001278 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001279 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001280 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001281 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001282
Owen Andersone50ed302009-08-10 22:56:29 +00001283 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001284 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001285 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001286 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001287 }
Evan Cheng8700e142006-01-11 06:09:51 +00001288
Gabor Greifba36cb52008-08-28 21:40:38 +00001289 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001290 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001291
Rafael Espindola094fad32009-04-08 21:14:34 +00001292 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001293 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001294}
1295
Chris Lattner3a7cd952006-10-07 21:55:32 +00001296/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1297/// match a load whose top elements are either undef or zeros. The load flavor
1298/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001299bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1300 SDValue N, SDValue &Base,
1301 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001302 SDValue &Disp, SDValue &Segment,
1303 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001305 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001306 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001308 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001309 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001310 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001311 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001312 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001313 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001314 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001315 return true;
1316 }
1317 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001318
1319 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001320 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001322 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001323 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 N.getOperand(0).getNode()->hasOneUse() &&
1325 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001326 N.getOperand(0).getOperand(0).hasOneUse()) {
1327 // Okay, this is a zero extending load. Fold it.
1328 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001329 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001330 return false;
1331 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001332 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001333 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001334 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001335 return false;
1336}
1337
1338
Evan Cheng51a9ed92006-02-25 10:09:08 +00001339/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1340/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001341bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1342 SDValue &Base, SDValue &Scale,
1343 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001344 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001345
1346 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1347 // segments.
1348 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001350 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001351 if (MatchAddress(N, AM))
1352 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001353 assert (T == AM.Segment);
1354 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001355
Owen Andersone50ed302009-08-10 22:56:29 +00001356 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001357 unsigned Complexity = 0;
1358 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001359 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001360 Complexity = 1;
1361 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001362 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001363 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1364 Complexity = 4;
1365
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001367 Complexity++;
1368 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001369 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001370
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001371 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1372 // a simple shift.
1373 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001374 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001375
1376 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1377 // to a LEA. This is determined with some expermentation but is by no means
1378 // optimal (especially for code size consideration). LEA is nice because of
1379 // its three-address nature. Tweak the cost function again when we can run
1380 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001381 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001382 // For X86-64, we should always use lea to materialize RIP relative
1383 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001384 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001385 Complexity = 4;
1386 else
1387 Complexity += 2;
1388 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001389
Gabor Greifba36cb52008-08-28 21:40:38 +00001390 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001391 Complexity++;
1392
Chris Lattner25142782009-07-11 22:50:33 +00001393 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001394 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001395 return false;
1396
1397 SDValue Segment;
1398 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1399 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001400}
1401
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001402/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1403bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1404 SDValue &Scale, SDValue &Index,
1405 SDValue &Disp) {
1406 assert(Op.getOpcode() == X86ISD::TLSADDR);
1407 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1408 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1409
1410 X86ISelAddressMode AM;
1411 AM.GV = GA->getGlobal();
1412 AM.Disp += GA->getOffset();
1413 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001414 AM.SymbolFlags = GA->getTargetFlags();
1415
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001417 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001418 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001419 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001421 }
1422
1423 SDValue Segment;
1424 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1425 return true;
1426}
1427
1428
Dan Gohman475871a2008-07-27 21:46:04 +00001429bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1430 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001431 SDValue &Index, SDValue &Disp,
1432 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001433 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001434 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001435 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001436 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001437 return false;
1438}
1439
Dan Gohman8b746962008-09-23 18:22:58 +00001440/// getGlobalBaseReg - Return an SDNode that returns the value of
1441/// the global base register. Output instructions required to
1442/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001443///
Evan Cheng9ade2182006-08-26 05:34:46 +00001444SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001445 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001446 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001447}
1448
Evan Chengb245d922006-05-20 01:36:52 +00001449static SDNode *FindCallStartFromCall(SDNode *Node) {
1450 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001451 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001452 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001453 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001454}
1455
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001456SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1457 SDValue Chain = Node->getOperand(0);
1458 SDValue In1 = Node->getOperand(1);
1459 SDValue In2L = Node->getOperand(2);
1460 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001461 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1462 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001463 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001464 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1465 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1466 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1467 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1468 MVT::i32, MVT::i32, MVT::Other, Ops,
1469 array_lengthof(Ops));
1470 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1471 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001472}
Christopher Lambc59e5212007-08-10 21:48:46 +00001473
Owen Andersone50ed302009-08-10 22:56:29 +00001474SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001475 if (Node->hasAnyUseOfValue(0))
1476 return 0;
1477
1478 // Optimize common patterns for __sync_add_and_fetch and
1479 // __sync_sub_and_fetch where the result is not used. This allows us
1480 // to use "lock" version of add, sub, inc, dec instructions.
1481 // FIXME: Do not use special instructions but instead add the "lock"
1482 // prefix to the target node somehow. The extra information will then be
1483 // transferred to machine instruction and it denotes the prefix.
1484 SDValue Chain = Node->getOperand(0);
1485 SDValue Ptr = Node->getOperand(1);
1486 SDValue Val = Node->getOperand(2);
1487 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1488 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1489 return 0;
1490
1491 bool isInc = false, isDec = false, isSub = false, isCN = false;
1492 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1493 if (CN) {
1494 isCN = true;
1495 int64_t CNVal = CN->getSExtValue();
1496 if (CNVal == 1)
1497 isInc = true;
1498 else if (CNVal == -1)
1499 isDec = true;
1500 else if (CNVal >= 0)
1501 Val = CurDAG->getTargetConstant(CNVal, NVT);
1502 else {
1503 isSub = true;
1504 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1505 }
1506 } else if (Val.hasOneUse() &&
1507 Val.getOpcode() == ISD::SUB &&
1508 X86::isZeroNode(Val.getOperand(0))) {
1509 isSub = true;
1510 Val = Val.getOperand(1);
1511 }
1512
1513 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001514 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001515 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001517 if (isInc)
1518 Opc = X86::LOCK_INC8m;
1519 else if (isDec)
1520 Opc = X86::LOCK_DEC8m;
1521 else if (isSub) {
1522 if (isCN)
1523 Opc = X86::LOCK_SUB8mi;
1524 else
1525 Opc = X86::LOCK_SUB8mr;
1526 } else {
1527 if (isCN)
1528 Opc = X86::LOCK_ADD8mi;
1529 else
1530 Opc = X86::LOCK_ADD8mr;
1531 }
1532 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001534 if (isInc)
1535 Opc = X86::LOCK_INC16m;
1536 else if (isDec)
1537 Opc = X86::LOCK_DEC16m;
1538 else if (isSub) {
1539 if (isCN) {
1540 if (Predicate_i16immSExt8(Val.getNode()))
1541 Opc = X86::LOCK_SUB16mi8;
1542 else
1543 Opc = X86::LOCK_SUB16mi;
1544 } else
1545 Opc = X86::LOCK_SUB16mr;
1546 } else {
1547 if (isCN) {
1548 if (Predicate_i16immSExt8(Val.getNode()))
1549 Opc = X86::LOCK_ADD16mi8;
1550 else
1551 Opc = X86::LOCK_ADD16mi;
1552 } else
1553 Opc = X86::LOCK_ADD16mr;
1554 }
1555 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001557 if (isInc)
1558 Opc = X86::LOCK_INC32m;
1559 else if (isDec)
1560 Opc = X86::LOCK_DEC32m;
1561 else if (isSub) {
1562 if (isCN) {
1563 if (Predicate_i32immSExt8(Val.getNode()))
1564 Opc = X86::LOCK_SUB32mi8;
1565 else
1566 Opc = X86::LOCK_SUB32mi;
1567 } else
1568 Opc = X86::LOCK_SUB32mr;
1569 } else {
1570 if (isCN) {
1571 if (Predicate_i32immSExt8(Val.getNode()))
1572 Opc = X86::LOCK_ADD32mi8;
1573 else
1574 Opc = X86::LOCK_ADD32mi;
1575 } else
1576 Opc = X86::LOCK_ADD32mr;
1577 }
1578 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001580 if (isInc)
1581 Opc = X86::LOCK_INC64m;
1582 else if (isDec)
1583 Opc = X86::LOCK_DEC64m;
1584 else if (isSub) {
1585 Opc = X86::LOCK_SUB64mr;
1586 if (isCN) {
1587 if (Predicate_i64immSExt8(Val.getNode()))
1588 Opc = X86::LOCK_SUB64mi8;
1589 else if (Predicate_i64immSExt32(Val.getNode()))
1590 Opc = X86::LOCK_SUB64mi32;
1591 }
1592 } else {
1593 Opc = X86::LOCK_ADD64mr;
1594 if (isCN) {
1595 if (Predicate_i64immSExt8(Val.getNode()))
1596 Opc = X86::LOCK_ADD64mi8;
1597 else if (Predicate_i64immSExt32(Val.getNode()))
1598 Opc = X86::LOCK_ADD64mi32;
1599 }
1600 }
1601 break;
1602 }
1603
1604 DebugLoc dl = Node->getDebugLoc();
Dan Gohman602b0c82009-09-25 18:54:59 +00001605 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1606 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001607 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1608 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001609 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001610 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1611 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1612 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001613 SDValue RetVals[] = { Undef, Ret };
1614 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1615 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001616 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1617 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1618 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001619 SDValue RetVals[] = { Undef, Ret };
1620 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1621 }
1622}
1623
Dan Gohman11596ed2009-10-09 20:35:19 +00001624/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1625/// any uses which require the SF or OF bits to be accurate.
1626static bool HasNoSignedComparisonUses(SDNode *N) {
1627 // Examine each user of the node.
1628 for (SDNode::use_iterator UI = N->use_begin(),
1629 UE = N->use_end(); UI != UE; ++UI) {
1630 // Only examine CopyToReg uses.
1631 if (UI->getOpcode() != ISD::CopyToReg)
1632 return false;
1633 // Only examine CopyToReg uses that copy to EFLAGS.
1634 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1635 X86::EFLAGS)
1636 return false;
1637 // Examine each user of the CopyToReg use.
1638 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1639 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1640 // Only examine the Flag result.
1641 if (FlagUI.getUse().getResNo() != 1) continue;
1642 // Anything unusual: assume conservatively.
1643 if (!FlagUI->isMachineOpcode()) return false;
1644 // Examine the opcode of the user.
1645 switch (FlagUI->getMachineOpcode()) {
1646 // These comparisons don't treat the most significant bit specially.
1647 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1648 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1649 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1650 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1651 case X86::JA: case X86::JAE: case X86::JB: case X86::JBE:
1652 case X86::JE: case X86::JNE: case X86::JP: case X86::JNP:
1653 case X86::CMOVA16rr: case X86::CMOVA16rm:
1654 case X86::CMOVA32rr: case X86::CMOVA32rm:
1655 case X86::CMOVA64rr: case X86::CMOVA64rm:
1656 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1657 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1658 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1659 case X86::CMOVB16rr: case X86::CMOVB16rm:
1660 case X86::CMOVB32rr: case X86::CMOVB32rm:
1661 case X86::CMOVB64rr: case X86::CMOVB64rm:
1662 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1663 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1664 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1665 case X86::CMOVE16rr: case X86::CMOVE16rm:
1666 case X86::CMOVE32rr: case X86::CMOVE32rm:
1667 case X86::CMOVE64rr: case X86::CMOVE64rm:
1668 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1669 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1670 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1671 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1672 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1673 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1674 case X86::CMOVP16rr: case X86::CMOVP16rm:
1675 case X86::CMOVP32rr: case X86::CMOVP32rm:
1676 case X86::CMOVP64rr: case X86::CMOVP64rm:
1677 continue;
1678 // Anything else: assume conservatively.
1679 default: return false;
1680 }
1681 }
1682 }
1683 return true;
1684}
1685
Dan Gohman475871a2008-07-27 21:46:04 +00001686SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001688 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001689 unsigned Opc, MOpc;
1690 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001691 DebugLoc dl = Node->getDebugLoc();
1692
Evan Chengf597dc72006-02-10 22:24:32 +00001693#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001694 DEBUG({
1695 errs() << std::string(Indent, ' ') << "Selecting: ";
1696 Node->dump(CurDAG);
1697 errs() << '\n';
1698 });
Evan Cheng23addc02006-02-10 22:46:26 +00001699 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001700#endif
1701
Dan Gohmane8be6c62008-07-17 19:10:17 +00001702 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001703#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001704 DEBUG({
1705 errs() << std::string(Indent-2, ' ') << "== ";
1706 Node->dump(CurDAG);
1707 errs() << '\n';
1708 });
Evan Cheng23addc02006-02-10 22:46:26 +00001709 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001710#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001711 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001712 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001713
Evan Cheng0114e942006-01-06 20:36:21 +00001714 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001715 default: break;
1716 case X86ISD::GlobalBaseReg:
1717 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001718
Dan Gohman72677342009-08-02 16:10:52 +00001719 case X86ISD::ATOMOR64_DAG:
1720 return SelectAtomic64(Node, X86::ATOMOR6432);
1721 case X86ISD::ATOMXOR64_DAG:
1722 return SelectAtomic64(Node, X86::ATOMXOR6432);
1723 case X86ISD::ATOMADD64_DAG:
1724 return SelectAtomic64(Node, X86::ATOMADD6432);
1725 case X86ISD::ATOMSUB64_DAG:
1726 return SelectAtomic64(Node, X86::ATOMSUB6432);
1727 case X86ISD::ATOMNAND64_DAG:
1728 return SelectAtomic64(Node, X86::ATOMNAND6432);
1729 case X86ISD::ATOMAND64_DAG:
1730 return SelectAtomic64(Node, X86::ATOMAND6432);
1731 case X86ISD::ATOMSWAP64_DAG:
1732 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001733
Dan Gohman72677342009-08-02 16:10:52 +00001734 case ISD::ATOMIC_LOAD_ADD: {
1735 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1736 if (RetVal)
1737 return RetVal;
1738 break;
1739 }
1740
1741 case ISD::SMUL_LOHI:
1742 case ISD::UMUL_LOHI: {
1743 SDValue N0 = Node->getOperand(0);
1744 SDValue N1 = Node->getOperand(1);
1745
1746 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001747 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001749 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1751 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1752 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1753 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001754 }
Bill Wendling12321672009-08-07 21:33:25 +00001755 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001757 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1759 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1760 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1761 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001762 }
Bill Wendling12321672009-08-07 21:33:25 +00001763 }
Dan Gohman72677342009-08-02 16:10:52 +00001764
1765 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001767 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1769 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1770 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1771 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001772 }
1773
1774 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1775 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001776 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001777 if (!foldedLoad) {
1778 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1779 if (foldedLoad)
1780 std::swap(N0, N1);
1781 }
1782
1783 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1784 N0, SDValue()).getValue(1);
1785
1786 if (foldedLoad) {
1787 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1788 InFlag };
1789 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001790 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1791 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001792 InFlag = SDValue(CNode, 1);
1793 // Update the chain.
1794 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1795 } else {
1796 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001797 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001798 }
1799
1800 // Copy the low half of the result, if it is needed.
1801 if (!N.getValue(0).use_empty()) {
1802 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1803 LoReg, NVT, InFlag);
1804 InFlag = Result.getValue(2);
1805 ReplaceUses(N.getValue(0), Result);
1806#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001807 DEBUG({
1808 errs() << std::string(Indent-2, ' ') << "=> ";
1809 Result.getNode()->dump(CurDAG);
1810 errs() << '\n';
1811 });
Dan Gohman72677342009-08-02 16:10:52 +00001812#endif
1813 }
1814 // Copy the high half of the result, if it is needed.
1815 if (!N.getValue(1).use_empty()) {
1816 SDValue Result;
1817 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1818 // Prevent use of AH in a REX instruction by referencing AX instead.
1819 // Shift it down 8 bits.
1820 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001821 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001822 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001823 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1824 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001826 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001827 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1828 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001829 } else {
1830 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1831 HiReg, NVT, InFlag);
1832 InFlag = Result.getValue(2);
1833 }
1834 ReplaceUses(N.getValue(1), Result);
1835#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001836 DEBUG({
1837 errs() << std::string(Indent-2, ' ') << "=> ";
1838 Result.getNode()->dump(CurDAG);
1839 errs() << '\n';
1840 });
Dan Gohman72677342009-08-02 16:10:52 +00001841#endif
1842 }
1843
1844#ifndef NDEBUG
1845 Indent -= 2;
1846#endif
1847
1848 return NULL;
1849 }
1850
1851 case ISD::SDIVREM:
1852 case ISD::UDIVREM: {
1853 SDValue N0 = Node->getOperand(0);
1854 SDValue N1 = Node->getOperand(1);
1855
1856 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001857 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001859 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1861 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1862 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1863 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001864 }
Bill Wendling12321672009-08-07 21:33:25 +00001865 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001867 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1869 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1870 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1871 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001872 }
Bill Wendling12321672009-08-07 21:33:25 +00001873 }
Dan Gohman72677342009-08-02 16:10:52 +00001874
Chris Lattner9e323832009-12-23 01:45:04 +00001875 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001876 unsigned ClrOpcode, SExtOpcode;
Chris Lattner9e323832009-12-23 01:45:04 +00001877 EVT ClrVT = NVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001879 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001881 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001882 ClrOpcode = 0;
1883 SExtOpcode = X86::CBW;
1884 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001886 LoReg = X86::AX; HiReg = X86::DX;
Chris Lattner9e323832009-12-23 01:45:04 +00001887 ClrOpcode = X86::MOV32r0; ClrReg = X86::EDX; ClrVT = MVT::i32;
Dan Gohman72677342009-08-02 16:10:52 +00001888 SExtOpcode = X86::CWD;
1889 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001891 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001892 ClrOpcode = X86::MOV32r0;
1893 SExtOpcode = X86::CDQ;
1894 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001896 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohman72677342009-08-02 16:10:52 +00001897 ClrOpcode = ~0U; // NOT USED.
1898 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001899 break;
1900 }
1901
Dan Gohman72677342009-08-02 16:10:52 +00001902 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1903 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1904 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001905
Dan Gohman72677342009-08-02 16:10:52 +00001906 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001908 // Special case for div8, just use a move with zero extension to AX to
1909 // clear the upper 8 bits (AH).
1910 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1911 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1912 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1913 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001914 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1915 MVT::Other, Ops,
1916 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001917 Chain = Move.getValue(1);
1918 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001919 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001920 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001921 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001922 Chain = CurDAG->getEntryNode();
1923 }
1924 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1925 InFlag = Chain.getValue(1);
1926 } else {
1927 InFlag =
1928 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1929 LoReg, N0, SDValue()).getValue(1);
1930 if (isSigned && !signBitIsZero) {
1931 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001932 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001933 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001934 } else {
1935 // Zero out the high part, effectively zero extending the input.
1936 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001937
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 if (NVT.getSimpleVT() == MVT::i64) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001939 ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001940 0);
1941 // We just did a 32-bit clear, insert it into a 64-bit register to
1942 // clear the whole 64-bit reg.
Dan Gohman7289ed22009-11-05 23:53:08 +00001943 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i64);
Dan Gohman72677342009-08-02 16:10:52 +00001944 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001946 ClrNode =
Dan Gohman7289ed22009-11-05 23:53:08 +00001947 SDValue(CurDAG->getMachineNode(TargetInstrInfo::SUBREG_TO_REG, dl,
1948 MVT::i64, Zero, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001949 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001950 } else {
Chris Lattner9e323832009-12-23 01:45:04 +00001951 ClrNode = SDValue(CurDAG->getMachineNode(ClrOpcode, dl, ClrVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001952 }
Dan Gohman72677342009-08-02 16:10:52 +00001953
Chris Lattner9e323832009-12-23 01:45:04 +00001954 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001955 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001956 }
Evan Cheng948f3432006-01-06 23:19:29 +00001957 }
Dan Gohman525178c2007-10-08 18:33:35 +00001958
Dan Gohman72677342009-08-02 16:10:52 +00001959 if (foldedLoad) {
1960 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1961 InFlag };
1962 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001963 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1964 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001965 InFlag = SDValue(CNode, 1);
1966 // Update the chain.
1967 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1968 } else {
1969 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001970 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001971 }
Evan Cheng948f3432006-01-06 23:19:29 +00001972
Dan Gohman72677342009-08-02 16:10:52 +00001973 // Copy the division (low) result, if it is needed.
1974 if (!N.getValue(0).use_empty()) {
1975 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1976 LoReg, NVT, InFlag);
1977 InFlag = Result.getValue(2);
1978 ReplaceUses(N.getValue(0), Result);
1979#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001980 DEBUG({
1981 errs() << std::string(Indent-2, ' ') << "=> ";
1982 Result.getNode()->dump(CurDAG);
1983 errs() << '\n';
1984 });
Dan Gohman72677342009-08-02 16:10:52 +00001985#endif
1986 }
1987 // Copy the remainder (high) result, if it is needed.
1988 if (!N.getValue(1).use_empty()) {
1989 SDValue Result;
1990 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1991 // Prevent use of AH in a REX instruction by referencing AX instead.
1992 // Shift it down 8 bits.
1993 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001995 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001996 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001997 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001999 0);
2000 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00002001 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2002 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00002003 } else {
2004 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2005 HiReg, NVT, InFlag);
2006 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00002007 }
Dan Gohman72677342009-08-02 16:10:52 +00002008 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002009#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002010 DEBUG({
2011 errs() << std::string(Indent-2, ' ') << "=> ";
2012 Result.getNode()->dump(CurDAG);
2013 errs() << '\n';
2014 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002015#endif
Dan Gohman72677342009-08-02 16:10:52 +00002016 }
Evan Chengf597dc72006-02-10 22:24:32 +00002017
2018#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002019 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002020#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002021
Dan Gohman72677342009-08-02 16:10:52 +00002022 return NULL;
2023 }
2024
Dan Gohman6a402dc2009-08-19 18:16:17 +00002025 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002026 SDValue N0 = Node->getOperand(0);
2027 SDValue N1 = Node->getOperand(1);
2028
2029 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2030 // use a smaller encoding.
2031 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2032 N0.getValueType() != MVT::i8 &&
2033 X86::isZeroNode(N1)) {
2034 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2035 if (!C) break;
2036
2037 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002038 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2039 (!(C->getZExtValue() & 0x80) ||
2040 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002041 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2042 SDValue Reg = N0.getNode()->getOperand(0);
2043
2044 // On x86-32, only the ABCD registers have 8-bit subregisters.
2045 if (!Subtarget->is64Bit()) {
2046 TargetRegisterClass *TRC = 0;
2047 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2048 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2049 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2050 default: llvm_unreachable("Unsupported TEST operand type!");
2051 }
2052 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002053 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2054 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002055 }
2056
2057 // Extract the l-register.
2058 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2059 MVT::i8, Reg);
2060
2061 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002062 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002063 }
2064
2065 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002066 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2067 (!(C->getZExtValue() & 0x8000) ||
2068 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002069 // Shift the immediate right by 8 bits.
2070 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2071 MVT::i8);
2072 SDValue Reg = N0.getNode()->getOperand(0);
2073
2074 // Put the value in an ABCD register.
2075 TargetRegisterClass *TRC = 0;
2076 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2077 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2078 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2079 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2080 default: llvm_unreachable("Unsupported TEST operand type!");
2081 }
2082 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002083 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2084 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002085
2086 // Extract the h-register.
2087 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2088 MVT::i8, Reg);
2089
2090 // Emit a testb. No special NOREX tricks are needed since there's
2091 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002092 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2093 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002094 }
2095
2096 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2097 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002098 N0.getValueType() != MVT::i16 &&
2099 (!(C->getZExtValue() & 0x8000) ||
2100 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002101 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2102 SDValue Reg = N0.getNode()->getOperand(0);
2103
2104 // Extract the 16-bit subregister.
2105 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2106 MVT::i16, Reg);
2107
2108 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002109 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002110 }
2111
2112 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2113 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002114 N0.getValueType() == MVT::i64 &&
2115 (!(C->getZExtValue() & 0x80000000) ||
2116 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002117 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2118 SDValue Reg = N0.getNode()->getOperand(0);
2119
2120 // Extract the 32-bit subregister.
2121 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2122 MVT::i32, Reg);
2123
2124 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002125 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002126 }
2127 }
2128 break;
2129 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002130 }
2131
Evan Cheng9ade2182006-08-26 05:34:46 +00002132 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002133
Evan Chengf597dc72006-02-10 22:24:32 +00002134#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002135 DEBUG({
2136 errs() << std::string(Indent-2, ' ') << "=> ";
2137 if (ResNode == NULL || ResNode == N.getNode())
2138 N.getNode()->dump(CurDAG);
2139 else
2140 ResNode->dump(CurDAG);
2141 errs() << '\n';
2142 });
Evan Cheng23addc02006-02-10 22:46:26 +00002143 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002144#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002145
2146 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002147}
2148
Chris Lattnerc0bad572006-06-08 18:03:49 +00002149bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002150SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002151 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002152 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002153 switch (ConstraintCode) {
2154 case 'o': // offsetable ??
2155 case 'v': // not offsetable ??
2156 default: return true;
2157 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002158 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002159 return true;
2160 break;
2161 }
2162
Evan Cheng04699902006-08-26 01:05:16 +00002163 OutOps.push_back(Op0);
2164 OutOps.push_back(Op1);
2165 OutOps.push_back(Op2);
2166 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002167 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002168 return false;
2169}
2170
Chris Lattnerc961eea2005-11-16 01:54:32 +00002171/// createX86ISelDag - This pass converts a legalized DAG into a
2172/// X86-specific DAG, ready for instruction scheduling.
2173///
Bill Wendling98a366d2009-04-29 23:29:43 +00002174FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2175 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002176 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002177}