blob: 4f73f8b812c787f0085d79d463ec125fc198c1ab [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
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000038#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
68 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000070 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000071 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000072 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000074 const char *ES;
75 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000076 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000077 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078
79 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000080 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0),
82 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000083 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000084
85 bool hasSymbolicDisplacement() const {
86 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
87 }
Chris Lattner18c59872009-06-27 04:16:01 +000088
89 bool hasBaseOrIndexReg() const {
90 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
91 }
92
93 /// isRIPRelative - Return true if this addressing mode is already RIP
94 /// relative.
95 bool isRIPRelative() const {
96 if (BaseType != RegBase) return false;
97 if (RegisterSDNode *RegNode =
98 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
99 return RegNode->getReg() == X86::RIP;
100 return false;
101 }
102
103 void setBaseReg(SDValue Reg) {
104 BaseType = RegBase;
105 Base.Reg = Reg;
106 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000107
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000108 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000109 errs() << "X86ISelAddressMode " << this << '\n';
110 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000111 if (Base.Reg.getNode() != 0)
112 Base.Reg.getNode()->dump();
113 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000114 errs() << "nul";
115 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
116 << " Scale" << Scale << '\n'
117 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000118 if (IndexReg.getNode() != 0)
119 IndexReg.getNode()->dump();
120 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000121 errs() << "nul";
122 errs() << " Disp " << Disp << '\n'
123 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000124 if (GV)
125 GV->dump();
126 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000127 errs() << "nul";
128 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000129 if (CP)
130 CP->dump();
131 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000132 errs() << "nul";
133 errs() << '\n'
134 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000135 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000136 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000137 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000138 errs() << "nul";
139 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000140 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000141 };
142}
143
144namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000145 //===--------------------------------------------------------------------===//
146 /// ISel - X86 specific code to select X86 machine instructions for
147 /// SelectionDAG operations.
148 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000149 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000150 /// X86Lowering - This object fully describes how to lower LLVM code to an
151 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000152 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153
154 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
155 /// make the right decision when generating code for different targets.
156 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000157
Evan Chengb7a75a52008-09-26 23:41:32 +0000158 /// OptForSize - If true, selector should try to optimize for code size
159 /// instead of performance.
160 bool OptForSize;
161
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000163 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000164 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000165 X86Lowering(*tm.getTargetLowering()),
166 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000167 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000168
169 virtual const char *getPassName() const {
170 return "X86 DAG->DAG Instruction Selection";
171 }
172
Evan Chengdb8d56b2008-06-30 20:45:06 +0000173 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000174 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000175 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000176
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000177 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
178
Evan Cheng884c70c2008-11-27 00:49:46 +0000179 virtual
180 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000181
Chris Lattnerc961eea2005-11-16 01:54:32 +0000182// Include the pieces autogenerated from the target description.
183#include "X86GenDAGISel.inc"
184
185 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000187 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000188 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000189
Rafael Espindola094fad32009-04-08 21:14:34 +0000190 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
191 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000192 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000193 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
194 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
195 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000196 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000197 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000198 SDValue &Scale, SDValue &Index, SDValue &Disp,
199 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000200 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
201 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000202 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
203 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
205 SDValue N, SDValue &Base, SDValue &Scale,
206 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000207 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue &InChain, SDValue &OutChain);
209 bool TryFoldLoad(SDValue P, SDValue N,
210 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000211 SDValue &Index, SDValue &Disp,
212 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000213 void PreprocessForRMW();
214 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000215
Chris Lattnerc0bad572006-06-08 18:03:49 +0000216 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
217 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000218 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000219 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000220 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000221
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000222 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
223
Dan Gohman475871a2008-07-27 21:46:04 +0000224 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
225 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000226 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000227 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
229 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000230 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000231 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 // These are 32-bit even in 64-bit mode since RIP relative offset
233 // is 32-bit.
234 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000236 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000237 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000239 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000246
247 if (AM.Segment.getNode())
248 Segment = AM.Segment;
249 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000251 }
252
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000253 /// getI8Imm - Return a target constant with the specified value, of type
254 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000255 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000257 }
258
Chris Lattnerc961eea2005-11-16 01:54:32 +0000259 /// getI16Imm - Return a target constant with the specified value, of type
260 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000263 }
264
265 /// getI32Imm - Return a target constant with the specified value, of type
266 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000269 }
Evan Chengf597dc72006-02-10 22:24:32 +0000270
Dan Gohman8b746962008-09-23 18:22:58 +0000271 /// getGlobalBaseReg - Return an SDNode that returns the value of
272 /// the global base register. Output instructions required to
273 /// initialize the global base register, if necessary.
274 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000275 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000276
Dan Gohmanc5534622009-06-03 20:20:00 +0000277 /// getTargetMachine - Return a reference to the TargetMachine, casted
278 /// to the target-specific type.
279 const X86TargetMachine &getTargetMachine() {
280 return static_cast<const X86TargetMachine &>(TM);
281 }
282
283 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
284 /// to the target-specific type.
285 const X86InstrInfo *getInstrInfo() {
286 return getTargetMachine().getInstrInfo();
287 }
288
Evan Cheng23addc02006-02-10 22:46:26 +0000289#ifndef NDEBUG
290 unsigned Indent;
291#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000292 };
293}
294
Evan Chengf4b4c412006-08-08 00:31:00 +0000295
Evan Cheng884c70c2008-11-27 00:49:46 +0000296bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
297 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000298 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000299
Evan Cheng884c70c2008-11-27 00:49:46 +0000300 if (U == Root)
301 switch (U->getOpcode()) {
302 default: break;
303 case ISD::ADD:
304 case ISD::ADDC:
305 case ISD::ADDE:
306 case ISD::AND:
307 case ISD::OR:
308 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000309 SDValue Op1 = U->getOperand(1);
310
Evan Cheng884c70c2008-11-27 00:49:46 +0000311 // If the other operand is a 8-bit immediate we should fold the immediate
312 // instead. This reduces code size.
313 // e.g.
314 // movl 4(%esp), %eax
315 // addl $4, %eax
316 // vs.
317 // movl $4, %eax
318 // addl 4(%esp), %eax
319 // The former is 2 bytes shorter. In case where the increment is 1, then
320 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000321 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000322 if (Imm->getAPIntValue().isSignedIntN(8))
323 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000324
325 // If the other operand is a TLS address, we should fold it instead.
326 // This produces
327 // movl %gs:0, %eax
328 // leal i@NTPOFF(%eax), %eax
329 // instead of
330 // movl $i@NTPOFF, %eax
331 // addl %gs:0, %eax
332 // if the block also has an access to a second TLS address this will save
333 // a load.
334 // FIXME: This is probably also true for non TLS addresses.
335 if (Op1.getOpcode() == X86ISD::Wrapper) {
336 SDValue Val = Op1.getOperand(0);
337 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
338 return false;
339 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000340 }
341 }
342
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000343 // Proceed to 'generic' cycle finder code
344 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000345}
346
Evan Cheng70e674e2006-08-28 20:10:17 +0000347/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
348/// and move load below the TokenFactor. Replace store's chain operand with
349/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000350static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000351 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000352 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000353 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
354 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000355 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000356 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000357 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000358 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
359 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
360 Load.getOperand(1),
361 Load.getOperand(2));
362 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000363 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000364}
365
Nate Begeman206a3572009-09-16 03:20:46 +0000366/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
367/// chain produced by the load must only be used by the store's chain operand,
368/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000369///
Dan Gohman475871a2008-07-27 21:46:04 +0000370static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
371 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000372 if (N.getOpcode() == ISD::BIT_CONVERT)
373 N = N.getOperand(0);
374
375 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
376 if (!LD || LD->isVolatile())
377 return false;
378 if (LD->getAddressingMode() != ISD::UNINDEXED)
379 return false;
380
381 ISD::LoadExtType ExtType = LD->getExtensionType();
382 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
383 return false;
384
385 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000386 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000387 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000388 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000389 Load = N;
390 return true;
391 }
392 return false;
393}
394
Evan Chengab6c3bb2008-08-25 21:27:18 +0000395/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
396/// operand and move load below the call's chain operand.
397static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000398 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000399 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000400 SDValue Chain = CallSeqStart.getOperand(0);
401 if (Chain.getNode() == Load.getNode())
402 Ops.push_back(Load.getOperand(0));
403 else {
404 assert(Chain.getOpcode() == ISD::TokenFactor &&
405 "Unexpected CallSeqStart chain operand");
406 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
407 if (Chain.getOperand(i).getNode() == Load.getNode())
408 Ops.push_back(Load.getOperand(0));
409 else
410 Ops.push_back(Chain.getOperand(i));
411 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000412 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000414 Ops.clear();
415 Ops.push_back(NewChain);
416 }
417 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
418 Ops.push_back(CallSeqStart.getOperand(i));
419 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000420 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
421 Load.getOperand(1), Load.getOperand(2));
422 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000423 Ops.push_back(SDValue(Load.getNode(), 1));
424 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000425 Ops.push_back(Call.getOperand(i));
426 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
427}
428
429/// isCalleeLoad - Return true if call address is a load and it can be
430/// moved below CALLSEQ_START and the chains leading up to the call.
431/// Return the CALLSEQ_START by reference as a second output.
432static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000433 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000434 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000435 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000436 if (!LD ||
437 LD->isVolatile() ||
438 LD->getAddressingMode() != ISD::UNINDEXED ||
439 LD->getExtensionType() != ISD::NON_EXTLOAD)
440 return false;
441
442 // Now let's find the callseq_start.
443 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
444 if (!Chain.hasOneUse())
445 return false;
446 Chain = Chain.getOperand(0);
447 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000448
449 if (Chain.getOperand(0).getNode() == Callee.getNode())
450 return true;
451 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000452 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
453 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000454 return true;
455 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000456}
457
458
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000459/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000460/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000461/// This allows the instruction selector to pick more read-modify-write
462/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000463///
464/// [Load chain]
465/// ^
466/// |
467/// [Load]
468/// ^ ^
469/// | |
470/// / \-
471/// / |
472/// [TokenFactor] [Op]
473/// ^ ^
474/// | |
475/// \ /
476/// \ /
477/// [Store]
478///
479/// The fact the store's chain operand != load's chain will prevent the
480/// (store (op (load))) instruction from being selected. We can transform it to:
481///
482/// [Load chain]
483/// ^
484/// |
485/// [TokenFactor]
486/// ^
487/// |
488/// [Load]
489/// ^ ^
490/// | |
491/// | \-
492/// | |
493/// | [Op]
494/// | ^
495/// | |
496/// \ /
497/// \ /
498/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000499void X86DAGToDAGISel::PreprocessForRMW() {
500 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
501 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000502 if (I->getOpcode() == X86ISD::CALL) {
503 /// Also try moving call address load from outside callseq_start to just
504 /// before the call to allow it to be folded.
505 ///
506 /// [Load chain]
507 /// ^
508 /// |
509 /// [Load]
510 /// ^ ^
511 /// | |
512 /// / \--
513 /// / |
514 ///[CALLSEQ_START] |
515 /// ^ |
516 /// | |
517 /// [LOAD/C2Reg] |
518 /// | |
519 /// \ /
520 /// \ /
521 /// [CALL]
522 SDValue Chain = I->getOperand(0);
523 SDValue Load = I->getOperand(1);
524 if (!isCalleeLoad(Load, Chain))
525 continue;
526 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
527 ++NumLoadMoved;
528 continue;
529 }
530
Evan Cheng8b2794a2006-10-13 21:14:26 +0000531 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000532 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000533 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000534
Gabor Greifba36cb52008-08-28 21:40:38 +0000535 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000536 continue;
537
Dan Gohman475871a2008-07-27 21:46:04 +0000538 SDValue N1 = I->getOperand(1);
539 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000540 if ((N1.getValueType().isFloatingPoint() &&
541 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000542 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000543 continue;
544
545 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000546 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000547 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000548 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000549 case ISD::ADD:
550 case ISD::MUL:
551 case ISD::AND:
552 case ISD::OR:
553 case ISD::XOR:
554 case ISD::ADDC:
555 case ISD::ADDE:
556 case ISD::VECTOR_SHUFFLE: {
557 SDValue N10 = N1.getOperand(0);
558 SDValue N11 = N1.getOperand(1);
559 RModW = isRMWLoad(N10, Chain, N2, Load);
560 if (!RModW)
561 RModW = isRMWLoad(N11, Chain, N2, Load);
562 break;
563 }
564 case ISD::SUB:
565 case ISD::SHL:
566 case ISD::SRA:
567 case ISD::SRL:
568 case ISD::ROTL:
569 case ISD::ROTR:
570 case ISD::SUBC:
571 case ISD::SUBE:
572 case X86ISD::SHLD:
573 case X86ISD::SHRD: {
574 SDValue N10 = N1.getOperand(0);
575 RModW = isRMWLoad(N10, Chain, N2, Load);
576 break;
577 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000578 }
579
Evan Cheng82a35b32006-08-29 06:44:17 +0000580 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000581 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000582 ++NumLoadMoved;
583 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000584 }
585}
586
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000587
588/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
589/// nodes that target the FP stack to be store and load to the stack. This is a
590/// gross hack. We would like to simply mark these as being illegal, but when
591/// we do that, legalize produces these when it expands calls, then expands
592/// these in the same legalize pass. We would like dag combine to be able to
593/// hack on these between the call expansion and the node legalization. As such
594/// this pass basically does "really late" legalization of these inline with the
595/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000596void X86DAGToDAGISel::PreprocessForFPConvert() {
597 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
598 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000599 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
600 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
601 continue;
602
603 // If the source and destination are SSE registers, then this is a legal
604 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000605 EVT SrcVT = N->getOperand(0).getValueType();
606 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000607 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
608 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
609 if (SrcIsSSE && DstIsSSE)
610 continue;
611
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000612 if (!SrcIsSSE && !DstIsSSE) {
613 // If this is an FPStack extension, it is a noop.
614 if (N->getOpcode() == ISD::FP_EXTEND)
615 continue;
616 // If this is a value-preserving FPStack truncation, it is a noop.
617 if (N->getConstantOperandVal(1))
618 continue;
619 }
620
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000621 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
622 // FPStack has extload and truncstore. SSE can fold direct loads into other
623 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000624 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000625 if (N->getOpcode() == ISD::FP_ROUND)
626 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
627 else
628 MemVT = SrcIsSSE ? SrcVT : DstVT;
629
Dan Gohmanf350b272008-08-23 02:25:05 +0000630 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000631 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000632
633 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000634 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000635 N->getOperand(0),
636 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000637 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000638 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000639
640 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
641 // extload we created. This will cause general havok on the dag because
642 // anything below the conversion could be folded into other existing nodes.
643 // To avoid invalidating 'I', back it up to the convert node.
644 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000645 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000646
647 // Now that we did that, the node is dead. Increment the iterator to the
648 // next node to process, then delete N.
649 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000650 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000651 }
652}
653
Chris Lattnerc961eea2005-11-16 01:54:32 +0000654/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
655/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000656void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000657 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000658 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000659
Evan Chengdb8d56b2008-06-30 20:45:06 +0000660 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000661 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000662 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000663
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000664 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000665 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000666
Chris Lattnerc961eea2005-11-16 01:54:32 +0000667 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000668#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000669 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000670 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000671#endif
David Greene8ad4c002008-10-27 21:56:29 +0000672 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000673#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000674 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000675#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000676
Dan Gohmanf350b272008-08-23 02:25:05 +0000677 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000678}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000679
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000680/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
681/// the main function.
682void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
683 MachineFrameInfo *MFI) {
684 const TargetInstrInfo *TII = TM.getInstrInfo();
685 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000686 BuildMI(BB, DebugLoc::getUnknownLoc(),
687 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000688}
689
690void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
691 // If this is main, emit special code for main.
692 MachineBasicBlock *BB = MF.begin();
693 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
694 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
695}
696
Rafael Espindola094fad32009-04-08 21:14:34 +0000697
698bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
699 X86ISelAddressMode &AM) {
700 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
701 SDValue Segment = N.getOperand(0);
702
703 if (AM.Segment.getNode() == 0) {
704 AM.Segment = Segment;
705 return false;
706 }
707
708 return true;
709}
710
711bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
712 // This optimization is valid because the GNU TLS model defines that
713 // gs:0 (or fs:0 on X86-64) contains its own address.
714 // For more information see http://people.redhat.com/drepper/tls.pdf
715
716 SDValue Address = N.getOperand(1);
717 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
718 !MatchSegmentBaseAddress (Address, AM))
719 return false;
720
721 return true;
722}
723
Chris Lattner18c59872009-06-27 04:16:01 +0000724/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
725/// into an addressing mode. These wrap things that will resolve down into a
726/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000727/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000728bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000729 // If the addressing mode already has a symbol as the displacement, we can
730 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000731 if (AM.hasSymbolicDisplacement())
732 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000733
734 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000735 CodeModel::Model M = TM.getCodeModel();
736
Chris Lattner18c59872009-06-27 04:16:01 +0000737 // Handle X86-64 rip-relative addresses. We check this before checking direct
738 // folding because RIP is preferable to non-RIP accesses.
739 if (Subtarget->is64Bit() &&
740 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
741 // they cannot be folded into immediate fields.
742 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000743 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000744 // Base and index reg must be 0 in order to use %rip as base and lowering
745 // must allow RIP.
746 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000747 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
748 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000749 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000750 AM.GV = G->getGlobal();
751 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000752 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000753 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
754 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000755 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000756 AM.CP = CP->getConstVal();
757 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000758 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000759 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000760 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
761 AM.ES = S->getSymbol();
762 AM.SymbolFlags = S->getTargetFlags();
763 } else {
764 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
765 AM.JT = J->getIndex();
766 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000767 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000768
Chris Lattner18c59872009-06-27 04:16:01 +0000769 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000771 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000772 }
773
774 // Handle the case when globals fit in our immediate field: This is true for
775 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
776 // mode, this results in a non-RIP-relative computation.
777 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000778 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000779 TM.getRelocationModel() == Reloc::Static)) {
780 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
781 AM.GV = G->getGlobal();
782 AM.Disp += G->getOffset();
783 AM.SymbolFlags = G->getTargetFlags();
784 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
785 AM.CP = CP->getConstVal();
786 AM.Align = CP->getAlignment();
787 AM.Disp += CP->getOffset();
788 AM.SymbolFlags = CP->getTargetFlags();
789 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
790 AM.ES = S->getSymbol();
791 AM.SymbolFlags = S->getTargetFlags();
792 } else {
793 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
794 AM.JT = J->getIndex();
795 AM.SymbolFlags = J->getTargetFlags();
796 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000797 return false;
798 }
799
800 return true;
801}
802
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000803/// MatchAddress - Add the specified node to the specified addressing mode,
804/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000805/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000806bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
807 if (MatchAddressRecursively(N, AM, 0))
808 return true;
809
810 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
811 // a smaller encoding and avoids a scaled-index.
812 if (AM.Scale == 2 &&
813 AM.BaseType == X86ISelAddressMode::RegBase &&
814 AM.Base.Reg.getNode() == 0) {
815 AM.Base.Reg = AM.IndexReg;
816 AM.Scale = 1;
817 }
818
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000819 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
820 // because it has a smaller encoding.
821 // TODO: Which other code models can use this?
822 if (TM.getCodeModel() == CodeModel::Small &&
823 Subtarget->is64Bit() &&
824 AM.Scale == 1 &&
825 AM.BaseType == X86ISelAddressMode::RegBase &&
826 AM.Base.Reg.getNode() == 0 &&
827 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000828 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000829 AM.hasSymbolicDisplacement())
830 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
831
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000832 return false;
833}
834
835bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
836 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000837 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000838 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000839 DEBUG({
840 errs() << "MatchAddress: ";
841 AM.dump();
842 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000843 // Limit recursion.
844 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000845 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000846
847 CodeModel::Model M = TM.getCodeModel();
848
Chris Lattner18c59872009-06-27 04:16:01 +0000849 // If this is already a %rip relative address, we can only merge immediates
850 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000851 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000852 if (AM.isRIPRelative()) {
853 // FIXME: JumpTable and ExternalSymbol address currently don't like
854 // displacements. It isn't very important, but this should be fixed for
855 // consistency.
856 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000857
Chris Lattner18c59872009-06-27 04:16:01 +0000858 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
859 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000860 if (X86::isOffsetSuitableForCodeModel(Val, M,
861 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000862 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000863 return false;
864 }
865 }
866 return true;
867 }
868
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869 switch (N.getOpcode()) {
870 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000871 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000872 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000873 if (!is64Bit ||
874 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
875 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000876 AM.Disp += Val;
877 return false;
878 }
879 break;
880 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000881
Rafael Espindola094fad32009-04-08 21:14:34 +0000882 case X86ISD::SegmentBaseAddress:
883 if (!MatchSegmentBaseAddress(N, AM))
884 return false;
885 break;
886
Rafael Espindola49a168d2009-04-12 21:55:03 +0000887 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000888 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000889 if (!MatchWrapper(N, AM))
890 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000891 break;
892
Rafael Espindola094fad32009-04-08 21:14:34 +0000893 case ISD::LOAD:
894 if (!MatchLoad(N, AM))
895 return false;
896 break;
897
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000898 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000899 if (AM.BaseType == X86ISelAddressMode::RegBase
900 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000901 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
902 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
903 return false;
904 }
905 break;
Evan Chengec693f72005-12-08 02:01:35 +0000906
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000907 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000908 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000909 break;
910
Gabor Greif93c53e52008-08-31 15:37:04 +0000911 if (ConstantSDNode
912 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000913 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000914 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
915 // that the base operand remains free for further matching. If
916 // the base doesn't end up getting used, a post-processing step
917 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000918 if (Val == 1 || Val == 2 || Val == 3) {
919 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000920 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000921
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000922 // Okay, we know that we have a scale by now. However, if the scaled
923 // value is an add of something and a constant, we can fold the
924 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
926 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
927 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000928 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000929 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000930 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000931 if (!is64Bit ||
932 X86::isOffsetSuitableForCodeModel(Disp, M,
933 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000934 AM.Disp = Disp;
935 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000936 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000937 } else {
938 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000939 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000940 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000941 }
942 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000943 }
Evan Chengec693f72005-12-08 02:01:35 +0000944
Dan Gohman83688052007-10-22 20:22:24 +0000945 case ISD::SMUL_LOHI:
946 case ISD::UMUL_LOHI:
947 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000948 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000949 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000950 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000951 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000952 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000953 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000954 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000955 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000956 if (ConstantSDNode
957 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000958 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
959 CN->getZExtValue() == 9) {
960 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000961
Gabor Greifba36cb52008-08-28 21:40:38 +0000962 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000963 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000964
965 // Okay, we know that we have a scale by now. However, if the scaled
966 // value is an add of something and a constant, we can fold the
967 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000968 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
969 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
970 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000971 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000972 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000973 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000974 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000975 if (!is64Bit ||
976 X86::isOffsetSuitableForCodeModel(Disp, M,
977 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000978 AM.Disp = Disp;
979 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000981 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000982 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000983 }
984
985 AM.IndexReg = AM.Base.Reg = Reg;
986 return false;
987 }
Chris Lattner62412262007-02-04 20:18:17 +0000988 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000989 break;
990
Dan Gohman3cd90a12009-05-11 18:02:53 +0000991 case ISD::SUB: {
992 // Given A-B, if A can be completely folded into the address and
993 // the index field with the index field unused, use -B as the index.
994 // This is a win if a has multiple parts that can be folded into
995 // the address. Also, this saves a mov if the base register has
996 // other uses, since it avoids a two-address sub instruction, however
997 // it costs an additional mov if the index register has other uses.
998
999 // Test if the LHS of the sub can be folded.
1000 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001001 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001002 AM = Backup;
1003 break;
1004 }
1005 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001006 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001007 AM = Backup;
1008 break;
1009 }
1010 int Cost = 0;
1011 SDValue RHS = N.getNode()->getOperand(1);
1012 // If the RHS involves a register with multiple uses, this
1013 // transformation incurs an extra mov, due to the neg instruction
1014 // clobbering its operand.
1015 if (!RHS.getNode()->hasOneUse() ||
1016 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1017 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1018 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1019 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001021 ++Cost;
1022 // If the base is a register with multiple uses, this
1023 // transformation may save a mov.
1024 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1025 AM.Base.Reg.getNode() &&
1026 !AM.Base.Reg.getNode()->hasOneUse()) ||
1027 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1028 --Cost;
1029 // If the folded LHS was interesting, this transformation saves
1030 // address arithmetic.
1031 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1032 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1033 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1034 --Cost;
1035 // If it doesn't look like it may be an overall win, don't do it.
1036 if (Cost >= 0) {
1037 AM = Backup;
1038 break;
1039 }
1040
1041 // Ok, the transformation is legal and appears profitable. Go for it.
1042 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1043 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1044 AM.IndexReg = Neg;
1045 AM.Scale = 1;
1046
1047 // Insert the new nodes into the topological ordering.
1048 if (Zero.getNode()->getNodeId() == -1 ||
1049 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1050 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1051 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1052 }
1053 if (Neg.getNode()->getNodeId() == -1 ||
1054 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1055 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1056 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1057 }
1058 return false;
1059 }
1060
Evan Cheng8e278262009-01-17 07:09:27 +00001061 case ISD::ADD: {
1062 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001063 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1064 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001065 return false;
1066 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001067 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1068 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001069 return false;
1070 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001071
1072 // If we couldn't fold both operands into the address at the same time,
1073 // see if we can just put each operand into a register and fold at least
1074 // the add.
1075 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1076 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001077 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001078 AM.Base.Reg = N.getNode()->getOperand(0);
1079 AM.IndexReg = N.getNode()->getOperand(1);
1080 AM.Scale = 1;
1081 return false;
1082 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001083 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001084 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001085
Chris Lattner62412262007-02-04 20:18:17 +00001086 case ISD::OR:
1087 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001088 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1089 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001090 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001091 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001092 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001093 // Address could not have picked a GV address for the displacement.
1094 AM.GV == NULL &&
1095 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001096 (!is64Bit ||
1097 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1098 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001099 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001100 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001101 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001102 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001103 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001104 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001105 }
1106 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001107
1108 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001109 // Perform some heroic transforms on an and of a constant-count shift
1110 // with a constant to enable use of the scaled offset field.
1111
Dan Gohman475871a2008-07-27 21:46:04 +00001112 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001113 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001114
Evan Cheng1314b002007-12-13 00:43:27 +00001115 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001116 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001117
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001118 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001119 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1120 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1121 if (!C1 || !C2) break;
1122
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001123 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1124 // allows us to convert the shift and and into an h-register extract and
1125 // a scaled index.
1126 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1127 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001128 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001129 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001131 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1132 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1133 X, Eight);
1134 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1135 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001137 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1138 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001139
1140 // Insert the new nodes into the topological ordering.
1141 if (Eight.getNode()->getNodeId() == -1 ||
1142 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1143 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1144 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1145 }
1146 if (Mask.getNode()->getNodeId() == -1 ||
1147 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1148 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1149 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1150 }
1151 if (Srl.getNode()->getNodeId() == -1 ||
1152 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1153 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1154 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1155 }
1156 if (And.getNode()->getNodeId() == -1 ||
1157 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1158 CurDAG->RepositionNode(N.getNode(), And.getNode());
1159 And.getNode()->setNodeId(N.getNode()->getNodeId());
1160 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001161 if (ShlCount.getNode()->getNodeId() == -1 ||
1162 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1163 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1164 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1165 }
1166 if (Shl.getNode()->getNodeId() == -1 ||
1167 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1168 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1169 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1170 }
1171 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001172 AM.IndexReg = And;
1173 AM.Scale = (1 << ScaleLog);
1174 return false;
1175 }
1176 }
1177
1178 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1179 // allows us to fold the shift into this addressing mode.
1180 if (Shift.getOpcode() != ISD::SHL) break;
1181
Evan Cheng1314b002007-12-13 00:43:27 +00001182 // Not likely to be profitable if either the AND or SHIFT node has more
1183 // than one use (unless all uses are for address computation). Besides,
1184 // isel mechanism requires their node ids to be reused.
1185 if (!N.hasOneUse() || !Shift.hasOneUse())
1186 break;
1187
1188 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001189 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001190 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1191 break;
1192
1193 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001194 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001195 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001196 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1197 NewANDMask);
1198 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001199 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001200
1201 // Insert the new nodes into the topological ordering.
1202 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1203 CurDAG->RepositionNode(X.getNode(), C1);
1204 C1->setNodeId(X.getNode()->getNodeId());
1205 }
1206 if (NewANDMask.getNode()->getNodeId() == -1 ||
1207 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1208 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1209 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1210 }
1211 if (NewAND.getNode()->getNodeId() == -1 ||
1212 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1213 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1214 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1215 }
1216 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1217 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1218 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1219 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1220 }
1221
Dan Gohman7b8e9642008-10-13 20:52:04 +00001222 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001223
1224 AM.Scale = 1 << ShiftCst;
1225 AM.IndexReg = NewAND;
1226 return false;
1227 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001228 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001229
Rafael Espindola523249f2009-03-31 16:16:57 +00001230 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001231}
1232
1233/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1234/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001235bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001236 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001237 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001238 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001239 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001240 AM.IndexReg = N;
1241 AM.Scale = 1;
1242 return false;
1243 }
1244
1245 // Otherwise, we cannot select it.
1246 return true;
1247 }
1248
1249 // Default, generate it as a register.
1250 AM.BaseType = X86ISelAddressMode::RegBase;
1251 AM.Base.Reg = N;
1252 return false;
1253}
1254
Evan Chengec693f72005-12-08 02:01:35 +00001255/// SelectAddr - returns true if it is able pattern match an addressing mode.
1256/// It returns the operands which make up the maximal addressing mode it can
1257/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001258bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1259 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001260 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001261 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001262 bool Done = false;
1263 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1264 unsigned Opcode = N.getOpcode();
1265 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001266 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001267 // If we are able to fold N into addressing mode, then we'll allow it even
1268 // if N has multiple uses. In general, addressing computation is used as
1269 // addresses by all of its uses. But watch out for CopyToReg uses, that
1270 // means the address computation is liveout. It will be computed by a LEA
1271 // so we want to avoid computing the address twice.
1272 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1273 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1274 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001275 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001276 Done = true;
1277 break;
1278 }
1279 }
1280 }
1281 }
1282
1283 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001284 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001285
Owen Andersone50ed302009-08-10 22:56:29 +00001286 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001287 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001289 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001290 }
Evan Cheng8700e142006-01-11 06:09:51 +00001291
Gabor Greifba36cb52008-08-28 21:40:38 +00001292 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001293 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001294
Rafael Espindola094fad32009-04-08 21:14:34 +00001295 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001296 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001297}
1298
Chris Lattner3a7cd952006-10-07 21:55:32 +00001299/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1300/// match a load whose top elements are either undef or zeros. The load flavor
1301/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001302bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1303 SDValue N, SDValue &Base,
1304 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001305 SDValue &Disp, SDValue &Segment,
1306 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001308 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001309 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001310 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001311 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001312 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001313 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001314 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001315 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001316 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001317 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001318 return true;
1319 }
1320 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001321
1322 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001323 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001325 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001326 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001327 N.getOperand(0).getNode()->hasOneUse() &&
1328 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001329 N.getOperand(0).getOperand(0).hasOneUse()) {
1330 // Okay, this is a zero extending load. Fold it.
1331 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001332 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001333 return false;
1334 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001335 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001336 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001337 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001338 return false;
1339}
1340
1341
Evan Cheng51a9ed92006-02-25 10:09:08 +00001342/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1343/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001344bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1345 SDValue &Base, SDValue &Scale,
1346 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001347 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001348
1349 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1350 // segments.
1351 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001353 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001354 if (MatchAddress(N, AM))
1355 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001356 assert (T == AM.Segment);
1357 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001358
Owen Andersone50ed302009-08-10 22:56:29 +00001359 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001360 unsigned Complexity = 0;
1361 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001362 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001363 Complexity = 1;
1364 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001365 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001366 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1367 Complexity = 4;
1368
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001370 Complexity++;
1371 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001372 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001373
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001374 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1375 // a simple shift.
1376 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001377 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001378
1379 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1380 // to a LEA. This is determined with some expermentation but is by no means
1381 // optimal (especially for code size consideration). LEA is nice because of
1382 // its three-address nature. Tweak the cost function again when we can run
1383 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001384 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001385 // For X86-64, we should always use lea to materialize RIP relative
1386 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001387 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001388 Complexity = 4;
1389 else
1390 Complexity += 2;
1391 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001392
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001394 Complexity++;
1395
Chris Lattner25142782009-07-11 22:50:33 +00001396 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001397 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001398 return false;
1399
1400 SDValue Segment;
1401 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1402 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001403}
1404
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001405/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1406bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1407 SDValue &Scale, SDValue &Index,
1408 SDValue &Disp) {
1409 assert(Op.getOpcode() == X86ISD::TLSADDR);
1410 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1411 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1412
1413 X86ISelAddressMode AM;
1414 AM.GV = GA->getGlobal();
1415 AM.Disp += GA->getOffset();
1416 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001417 AM.SymbolFlags = GA->getTargetFlags();
1418
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001420 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001422 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001424 }
1425
1426 SDValue Segment;
1427 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1428 return true;
1429}
1430
1431
Dan Gohman475871a2008-07-27 21:46:04 +00001432bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1433 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001434 SDValue &Index, SDValue &Disp,
1435 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001436 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001437 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001438 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001439 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001440 return false;
1441}
1442
Dan Gohman8b746962008-09-23 18:22:58 +00001443/// getGlobalBaseReg - Return an SDNode that returns the value of
1444/// the global base register. Output instructions required to
1445/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001446///
Evan Cheng9ade2182006-08-26 05:34:46 +00001447SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001448 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001449 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001450}
1451
Evan Chengb245d922006-05-20 01:36:52 +00001452static SDNode *FindCallStartFromCall(SDNode *Node) {
1453 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001455 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001456 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001457}
1458
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001459SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1460 SDValue Chain = Node->getOperand(0);
1461 SDValue In1 = Node->getOperand(1);
1462 SDValue In2L = Node->getOperand(2);
1463 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001464 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1465 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001466 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001467 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1468 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1469 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1470 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1471 MVT::i32, MVT::i32, MVT::Other, Ops,
1472 array_lengthof(Ops));
1473 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1474 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001475}
Christopher Lambc59e5212007-08-10 21:48:46 +00001476
Owen Andersone50ed302009-08-10 22:56:29 +00001477SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001478 if (Node->hasAnyUseOfValue(0))
1479 return 0;
1480
1481 // Optimize common patterns for __sync_add_and_fetch and
1482 // __sync_sub_and_fetch where the result is not used. This allows us
1483 // to use "lock" version of add, sub, inc, dec instructions.
1484 // FIXME: Do not use special instructions but instead add the "lock"
1485 // prefix to the target node somehow. The extra information will then be
1486 // transferred to machine instruction and it denotes the prefix.
1487 SDValue Chain = Node->getOperand(0);
1488 SDValue Ptr = Node->getOperand(1);
1489 SDValue Val = Node->getOperand(2);
1490 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1491 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1492 return 0;
1493
1494 bool isInc = false, isDec = false, isSub = false, isCN = false;
1495 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1496 if (CN) {
1497 isCN = true;
1498 int64_t CNVal = CN->getSExtValue();
1499 if (CNVal == 1)
1500 isInc = true;
1501 else if (CNVal == -1)
1502 isDec = true;
1503 else if (CNVal >= 0)
1504 Val = CurDAG->getTargetConstant(CNVal, NVT);
1505 else {
1506 isSub = true;
1507 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1508 }
1509 } else if (Val.hasOneUse() &&
1510 Val.getOpcode() == ISD::SUB &&
1511 X86::isZeroNode(Val.getOperand(0))) {
1512 isSub = true;
1513 Val = Val.getOperand(1);
1514 }
1515
1516 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001517 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001518 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001520 if (isInc)
1521 Opc = X86::LOCK_INC8m;
1522 else if (isDec)
1523 Opc = X86::LOCK_DEC8m;
1524 else if (isSub) {
1525 if (isCN)
1526 Opc = X86::LOCK_SUB8mi;
1527 else
1528 Opc = X86::LOCK_SUB8mr;
1529 } else {
1530 if (isCN)
1531 Opc = X86::LOCK_ADD8mi;
1532 else
1533 Opc = X86::LOCK_ADD8mr;
1534 }
1535 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001537 if (isInc)
1538 Opc = X86::LOCK_INC16m;
1539 else if (isDec)
1540 Opc = X86::LOCK_DEC16m;
1541 else if (isSub) {
1542 if (isCN) {
1543 if (Predicate_i16immSExt8(Val.getNode()))
1544 Opc = X86::LOCK_SUB16mi8;
1545 else
1546 Opc = X86::LOCK_SUB16mi;
1547 } else
1548 Opc = X86::LOCK_SUB16mr;
1549 } else {
1550 if (isCN) {
1551 if (Predicate_i16immSExt8(Val.getNode()))
1552 Opc = X86::LOCK_ADD16mi8;
1553 else
1554 Opc = X86::LOCK_ADD16mi;
1555 } else
1556 Opc = X86::LOCK_ADD16mr;
1557 }
1558 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001560 if (isInc)
1561 Opc = X86::LOCK_INC32m;
1562 else if (isDec)
1563 Opc = X86::LOCK_DEC32m;
1564 else if (isSub) {
1565 if (isCN) {
1566 if (Predicate_i32immSExt8(Val.getNode()))
1567 Opc = X86::LOCK_SUB32mi8;
1568 else
1569 Opc = X86::LOCK_SUB32mi;
1570 } else
1571 Opc = X86::LOCK_SUB32mr;
1572 } else {
1573 if (isCN) {
1574 if (Predicate_i32immSExt8(Val.getNode()))
1575 Opc = X86::LOCK_ADD32mi8;
1576 else
1577 Opc = X86::LOCK_ADD32mi;
1578 } else
1579 Opc = X86::LOCK_ADD32mr;
1580 }
1581 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001583 if (isInc)
1584 Opc = X86::LOCK_INC64m;
1585 else if (isDec)
1586 Opc = X86::LOCK_DEC64m;
1587 else if (isSub) {
1588 Opc = X86::LOCK_SUB64mr;
1589 if (isCN) {
1590 if (Predicate_i64immSExt8(Val.getNode()))
1591 Opc = X86::LOCK_SUB64mi8;
1592 else if (Predicate_i64immSExt32(Val.getNode()))
1593 Opc = X86::LOCK_SUB64mi32;
1594 }
1595 } else {
1596 Opc = X86::LOCK_ADD64mr;
1597 if (isCN) {
1598 if (Predicate_i64immSExt8(Val.getNode()))
1599 Opc = X86::LOCK_ADD64mi8;
1600 else if (Predicate_i64immSExt32(Val.getNode()))
1601 Opc = X86::LOCK_ADD64mi32;
1602 }
1603 }
1604 break;
1605 }
1606
1607 DebugLoc dl = Node->getDebugLoc();
Dan Gohman602b0c82009-09-25 18:54:59 +00001608 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1609 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001610 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1611 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001612 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001613 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1614 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1615 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001616 SDValue RetVals[] = { Undef, Ret };
1617 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1618 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001619 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1620 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1621 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001622 SDValue RetVals[] = { Undef, Ret };
1623 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1624 }
1625}
1626
Dan Gohman11596ed2009-10-09 20:35:19 +00001627/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1628/// any uses which require the SF or OF bits to be accurate.
1629static bool HasNoSignedComparisonUses(SDNode *N) {
1630 // Examine each user of the node.
1631 for (SDNode::use_iterator UI = N->use_begin(),
1632 UE = N->use_end(); UI != UE; ++UI) {
1633 // Only examine CopyToReg uses.
1634 if (UI->getOpcode() != ISD::CopyToReg)
1635 return false;
1636 // Only examine CopyToReg uses that copy to EFLAGS.
1637 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1638 X86::EFLAGS)
1639 return false;
1640 // Examine each user of the CopyToReg use.
1641 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1642 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1643 // Only examine the Flag result.
1644 if (FlagUI.getUse().getResNo() != 1) continue;
1645 // Anything unusual: assume conservatively.
1646 if (!FlagUI->isMachineOpcode()) return false;
1647 // Examine the opcode of the user.
1648 switch (FlagUI->getMachineOpcode()) {
1649 // These comparisons don't treat the most significant bit specially.
1650 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1651 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1652 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1653 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1654 case X86::JA: case X86::JAE: case X86::JB: case X86::JBE:
1655 case X86::JE: case X86::JNE: case X86::JP: case X86::JNP:
1656 case X86::CMOVA16rr: case X86::CMOVA16rm:
1657 case X86::CMOVA32rr: case X86::CMOVA32rm:
1658 case X86::CMOVA64rr: case X86::CMOVA64rm:
1659 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1660 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1661 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1662 case X86::CMOVB16rr: case X86::CMOVB16rm:
1663 case X86::CMOVB32rr: case X86::CMOVB32rm:
1664 case X86::CMOVB64rr: case X86::CMOVB64rm:
1665 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1666 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1667 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1668 case X86::CMOVE16rr: case X86::CMOVE16rm:
1669 case X86::CMOVE32rr: case X86::CMOVE32rm:
1670 case X86::CMOVE64rr: case X86::CMOVE64rm:
1671 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1672 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1673 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1674 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1675 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1676 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1677 case X86::CMOVP16rr: case X86::CMOVP16rm:
1678 case X86::CMOVP32rr: case X86::CMOVP32rm:
1679 case X86::CMOVP64rr: case X86::CMOVP64rm:
1680 continue;
1681 // Anything else: assume conservatively.
1682 default: return false;
1683 }
1684 }
1685 }
1686 return true;
1687}
1688
Dan Gohman475871a2008-07-27 21:46:04 +00001689SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001690 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001691 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001692 unsigned Opc, MOpc;
1693 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001694 DebugLoc dl = Node->getDebugLoc();
1695
Evan Chengf597dc72006-02-10 22:24:32 +00001696#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001697 DEBUG({
1698 errs() << std::string(Indent, ' ') << "Selecting: ";
1699 Node->dump(CurDAG);
1700 errs() << '\n';
1701 });
Evan Cheng23addc02006-02-10 22:46:26 +00001702 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001703#endif
1704
Dan Gohmane8be6c62008-07-17 19:10:17 +00001705 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001706#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001707 DEBUG({
1708 errs() << std::string(Indent-2, ' ') << "== ";
1709 Node->dump(CurDAG);
1710 errs() << '\n';
1711 });
Evan Cheng23addc02006-02-10 22:46:26 +00001712 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001713#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001714 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001715 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001716
Evan Cheng0114e942006-01-06 20:36:21 +00001717 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001718 default: break;
1719 case X86ISD::GlobalBaseReg:
1720 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001721
Dan Gohman72677342009-08-02 16:10:52 +00001722 case X86ISD::ATOMOR64_DAG:
1723 return SelectAtomic64(Node, X86::ATOMOR6432);
1724 case X86ISD::ATOMXOR64_DAG:
1725 return SelectAtomic64(Node, X86::ATOMXOR6432);
1726 case X86ISD::ATOMADD64_DAG:
1727 return SelectAtomic64(Node, X86::ATOMADD6432);
1728 case X86ISD::ATOMSUB64_DAG:
1729 return SelectAtomic64(Node, X86::ATOMSUB6432);
1730 case X86ISD::ATOMNAND64_DAG:
1731 return SelectAtomic64(Node, X86::ATOMNAND6432);
1732 case X86ISD::ATOMAND64_DAG:
1733 return SelectAtomic64(Node, X86::ATOMAND6432);
1734 case X86ISD::ATOMSWAP64_DAG:
1735 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001736
Dan Gohman72677342009-08-02 16:10:52 +00001737 case ISD::ATOMIC_LOAD_ADD: {
1738 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1739 if (RetVal)
1740 return RetVal;
1741 break;
1742 }
1743
1744 case ISD::SMUL_LOHI:
1745 case ISD::UMUL_LOHI: {
1746 SDValue N0 = Node->getOperand(0);
1747 SDValue N1 = Node->getOperand(1);
1748
1749 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001750 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001752 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1754 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1755 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1756 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001757 }
Bill Wendling12321672009-08-07 21:33:25 +00001758 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001760 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1762 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1763 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1764 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001765 }
Bill Wendling12321672009-08-07 21:33:25 +00001766 }
Dan Gohman72677342009-08-02 16:10:52 +00001767
1768 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001770 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1772 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1773 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1774 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001775 }
1776
1777 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1778 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001779 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001780 if (!foldedLoad) {
1781 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1782 if (foldedLoad)
1783 std::swap(N0, N1);
1784 }
1785
1786 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1787 N0, SDValue()).getValue(1);
1788
1789 if (foldedLoad) {
1790 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1791 InFlag };
1792 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001793 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1794 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001795 InFlag = SDValue(CNode, 1);
1796 // Update the chain.
1797 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1798 } else {
1799 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001800 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001801 }
1802
1803 // Copy the low half of the result, if it is needed.
1804 if (!N.getValue(0).use_empty()) {
1805 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1806 LoReg, NVT, InFlag);
1807 InFlag = Result.getValue(2);
1808 ReplaceUses(N.getValue(0), Result);
1809#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001810 DEBUG({
1811 errs() << std::string(Indent-2, ' ') << "=> ";
1812 Result.getNode()->dump(CurDAG);
1813 errs() << '\n';
1814 });
Dan Gohman72677342009-08-02 16:10:52 +00001815#endif
1816 }
1817 // Copy the high half of the result, if it is needed.
1818 if (!N.getValue(1).use_empty()) {
1819 SDValue Result;
1820 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1821 // Prevent use of AH in a REX instruction by referencing AX instead.
1822 // Shift it down 8 bits.
1823 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001825 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001826 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1827 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001829 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001830 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1831 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001832 } else {
1833 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1834 HiReg, NVT, InFlag);
1835 InFlag = Result.getValue(2);
1836 }
1837 ReplaceUses(N.getValue(1), Result);
1838#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001839 DEBUG({
1840 errs() << std::string(Indent-2, ' ') << "=> ";
1841 Result.getNode()->dump(CurDAG);
1842 errs() << '\n';
1843 });
Dan Gohman72677342009-08-02 16:10:52 +00001844#endif
1845 }
1846
1847#ifndef NDEBUG
1848 Indent -= 2;
1849#endif
1850
1851 return NULL;
1852 }
1853
1854 case ISD::SDIVREM:
1855 case ISD::UDIVREM: {
1856 SDValue N0 = Node->getOperand(0);
1857 SDValue N1 = Node->getOperand(1);
1858
1859 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001860 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001861 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001862 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001863 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1864 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1865 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1866 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001867 }
Bill Wendling12321672009-08-07 21:33:25 +00001868 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001870 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1872 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1873 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1874 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001875 }
Bill Wendling12321672009-08-07 21:33:25 +00001876 }
Dan Gohman72677342009-08-02 16:10:52 +00001877
1878 unsigned LoReg, HiReg;
1879 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001881 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001883 LoReg = X86::AL; HiReg = X86::AH;
1884 ClrOpcode = 0;
1885 SExtOpcode = X86::CBW;
1886 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001888 LoReg = X86::AX; HiReg = X86::DX;
1889 ClrOpcode = X86::MOV16r0;
1890 SExtOpcode = X86::CWD;
1891 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001893 LoReg = X86::EAX; HiReg = X86::EDX;
1894 ClrOpcode = X86::MOV32r0;
1895 SExtOpcode = X86::CDQ;
1896 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001898 LoReg = X86::RAX; HiReg = X86::RDX;
1899 ClrOpcode = ~0U; // NOT USED.
1900 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001901 break;
1902 }
1903
Dan Gohman72677342009-08-02 16:10:52 +00001904 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1905 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1906 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001907
Dan Gohman72677342009-08-02 16:10:52 +00001908 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001910 // Special case for div8, just use a move with zero extension to AX to
1911 // clear the upper 8 bits (AH).
1912 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1913 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1914 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1915 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001916 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1917 MVT::Other, Ops,
1918 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001919 Chain = Move.getValue(1);
1920 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001921 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001922 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001923 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001924 Chain = CurDAG->getEntryNode();
1925 }
1926 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1927 InFlag = Chain.getValue(1);
1928 } else {
1929 InFlag =
1930 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1931 LoReg, N0, SDValue()).getValue(1);
1932 if (isSigned && !signBitIsZero) {
1933 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001934 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001935 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001936 } else {
1937 // Zero out the high part, effectively zero extending the input.
1938 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001939
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 if (NVT.getSimpleVT() == MVT::i64) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001941 ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001942 0);
1943 // We just did a 32-bit clear, insert it into a 64-bit register to
1944 // clear the whole 64-bit reg.
1945 SDValue Undef =
Dan Gohman602b0c82009-09-25 18:54:59 +00001946 SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1947 dl, MVT::i64), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001948 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001950 ClrNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001951 SDValue(CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl,
1952 MVT::i64, Undef, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001953 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001954 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +00001955 ClrNode = SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001956 }
Dan Gohman72677342009-08-02 16:10:52 +00001957
1958 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1959 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001960 }
Evan Cheng948f3432006-01-06 23:19:29 +00001961 }
Dan Gohman525178c2007-10-08 18:33:35 +00001962
Dan Gohman72677342009-08-02 16:10:52 +00001963 if (foldedLoad) {
1964 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1965 InFlag };
1966 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001967 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1968 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001969 InFlag = SDValue(CNode, 1);
1970 // Update the chain.
1971 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1972 } else {
1973 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001974 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001975 }
Evan Cheng948f3432006-01-06 23:19:29 +00001976
Dan Gohman72677342009-08-02 16:10:52 +00001977 // Copy the division (low) result, if it is needed.
1978 if (!N.getValue(0).use_empty()) {
1979 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1980 LoReg, NVT, InFlag);
1981 InFlag = Result.getValue(2);
1982 ReplaceUses(N.getValue(0), Result);
1983#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001984 DEBUG({
1985 errs() << std::string(Indent-2, ' ') << "=> ";
1986 Result.getNode()->dump(CurDAG);
1987 errs() << '\n';
1988 });
Dan Gohman72677342009-08-02 16:10:52 +00001989#endif
1990 }
1991 // Copy the remainder (high) result, if it is needed.
1992 if (!N.getValue(1).use_empty()) {
1993 SDValue Result;
1994 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1995 // Prevent use of AH in a REX instruction by referencing AX instead.
1996 // Shift it down 8 bits.
1997 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001999 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00002000 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00002001 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00002003 0);
2004 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00002005 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2006 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00002007 } else {
2008 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2009 HiReg, NVT, InFlag);
2010 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00002011 }
Dan Gohman72677342009-08-02 16:10:52 +00002012 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002013#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002014 DEBUG({
2015 errs() << std::string(Indent-2, ' ') << "=> ";
2016 Result.getNode()->dump(CurDAG);
2017 errs() << '\n';
2018 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002019#endif
Dan Gohman72677342009-08-02 16:10:52 +00002020 }
Evan Chengf597dc72006-02-10 22:24:32 +00002021
2022#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002023 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002024#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002025
Dan Gohman72677342009-08-02 16:10:52 +00002026 return NULL;
2027 }
2028
Dan Gohman6a402dc2009-08-19 18:16:17 +00002029 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002030 SDValue N0 = Node->getOperand(0);
2031 SDValue N1 = Node->getOperand(1);
2032
2033 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2034 // use a smaller encoding.
2035 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2036 N0.getValueType() != MVT::i8 &&
2037 X86::isZeroNode(N1)) {
2038 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2039 if (!C) break;
2040
2041 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002042 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2043 (!(C->getZExtValue() & 0x80) ||
2044 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002045 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2046 SDValue Reg = N0.getNode()->getOperand(0);
2047
2048 // On x86-32, only the ABCD registers have 8-bit subregisters.
2049 if (!Subtarget->is64Bit()) {
2050 TargetRegisterClass *TRC = 0;
2051 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2052 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2053 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2054 default: llvm_unreachable("Unsupported TEST operand type!");
2055 }
2056 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002057 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2058 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002059 }
2060
2061 // Extract the l-register.
2062 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2063 MVT::i8, Reg);
2064
2065 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002066 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002067 }
2068
2069 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002070 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2071 (!(C->getZExtValue() & 0x8000) ||
2072 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002073 // Shift the immediate right by 8 bits.
2074 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2075 MVT::i8);
2076 SDValue Reg = N0.getNode()->getOperand(0);
2077
2078 // Put the value in an ABCD register.
2079 TargetRegisterClass *TRC = 0;
2080 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2081 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2082 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2083 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2084 default: llvm_unreachable("Unsupported TEST operand type!");
2085 }
2086 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002087 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2088 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002089
2090 // Extract the h-register.
2091 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2092 MVT::i8, Reg);
2093
2094 // Emit a testb. No special NOREX tricks are needed since there's
2095 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002096 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2097 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002098 }
2099
2100 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2101 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002102 N0.getValueType() != MVT::i16 &&
2103 (!(C->getZExtValue() & 0x8000) ||
2104 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002105 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2106 SDValue Reg = N0.getNode()->getOperand(0);
2107
2108 // Extract the 16-bit subregister.
2109 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2110 MVT::i16, Reg);
2111
2112 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002113 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002114 }
2115
2116 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2117 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002118 N0.getValueType() == MVT::i64 &&
2119 (!(C->getZExtValue() & 0x80000000) ||
2120 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002121 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2122 SDValue Reg = N0.getNode()->getOperand(0);
2123
2124 // Extract the 32-bit subregister.
2125 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2126 MVT::i32, Reg);
2127
2128 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002129 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002130 }
2131 }
2132 break;
2133 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002134 }
2135
Evan Cheng9ade2182006-08-26 05:34:46 +00002136 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002137
Evan Chengf597dc72006-02-10 22:24:32 +00002138#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002139 DEBUG({
2140 errs() << std::string(Indent-2, ' ') << "=> ";
2141 if (ResNode == NULL || ResNode == N.getNode())
2142 N.getNode()->dump(CurDAG);
2143 else
2144 ResNode->dump(CurDAG);
2145 errs() << '\n';
2146 });
Evan Cheng23addc02006-02-10 22:46:26 +00002147 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002148#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002149
2150 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002151}
2152
Chris Lattnerc0bad572006-06-08 18:03:49 +00002153bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002154SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002155 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002156 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002157 switch (ConstraintCode) {
2158 case 'o': // offsetable ??
2159 case 'v': // not offsetable ??
2160 default: return true;
2161 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002162 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002163 return true;
2164 break;
2165 }
2166
Evan Cheng04699902006-08-26 01:05:16 +00002167 OutOps.push_back(Op0);
2168 OutOps.push_back(Op1);
2169 OutOps.push_back(Op2);
2170 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002171 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002172 return false;
2173}
2174
Chris Lattnerc961eea2005-11-16 01:54:32 +00002175/// createX86ISelDag - This pass converts a legalized DAG into a
2176/// X86-specific DAG, ready for instruction scheduling.
2177///
Bill Wendling98a366d2009-04-29 23:29:43 +00002178FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2179 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002180 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002181}