blob: 6a3577aaaf18dc0a7c49307a58b7c8289f9877c7 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Daniel Dunbar5da58852009-11-10 18:24:37 +000015// Force NDEBUG on in any optimized build on Darwin.
16//
17// FIXME: This is a huge hack, to work around ridiculously awful compile times
18// on this file with gcc-4.2 on Darwin, in Release mode.
Daniel Dunbar253e9b22009-11-11 00:28:38 +000019#if (!defined(__llvm__) && defined(__APPLE__) && \
20 defined(__OPTIMIZE__) && !defined(NDEBUG))
Daniel Dunbar5da58852009-11-10 18:24:37 +000021#define NDEBUG
22#endif
23
Evan Cheng2ef88a02006-08-07 22:28:20 +000024#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000025#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000026#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000027#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000028#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000030#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000031#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000032#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000033#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000034#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000035#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000036#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000037#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000038#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000039#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000040#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000042#include "llvm/CodeGen/SelectionDAGISel.h"
43#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000045#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000046#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000047#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000048#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000049#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000050#include "llvm/ADT/Statistic.h"
51using namespace llvm;
52
Evan Cheng4d952322009-03-31 01:13:53 +000053#include "llvm/Support/CommandLine.h"
54static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
55
Chris Lattner95b2c7d2006-12-19 22:59:26 +000056STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
57
Chris Lattnerc961eea2005-11-16 01:54:32 +000058//===----------------------------------------------------------------------===//
59// Pattern Matcher Implementation
60//===----------------------------------------------------------------------===//
61
62namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000064 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 /// tree.
66 struct X86ISelAddressMode {
67 enum {
68 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000069 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000070 } BaseType;
71
72 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000073 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 int FrameIndex;
75 } Base;
76
77 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000078 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000079 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000080 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000082 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000083 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000084 const char *ES;
85 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000086 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000087 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000088
89 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000090 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000091 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000092 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000093 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000094
95 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000096 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000097 }
Chris Lattner18c59872009-06-27 04:16:01 +000098
99 bool hasBaseOrIndexReg() const {
100 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
101 }
102
103 /// isRIPRelative - Return true if this addressing mode is already RIP
104 /// relative.
105 bool isRIPRelative() const {
106 if (BaseType != RegBase) return false;
107 if (RegisterSDNode *RegNode =
108 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
109 return RegNode->getReg() == X86::RIP;
110 return false;
111 }
112
113 void setBaseReg(SDValue Reg) {
114 BaseType = RegBase;
115 Base.Reg = Reg;
116 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000117
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000118 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000119 errs() << "X86ISelAddressMode " << this << '\n';
120 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000121 if (Base.Reg.getNode() != 0)
122 Base.Reg.getNode()->dump();
123 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000124 errs() << "nul";
125 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
126 << " Scale" << Scale << '\n'
127 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000128 if (IndexReg.getNode() != 0)
129 IndexReg.getNode()->dump();
130 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000131 errs() << "nul";
132 errs() << " Disp " << Disp << '\n'
133 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000134 if (GV)
135 GV->dump();
136 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000137 errs() << "nul";
138 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000139 if (CP)
140 CP->dump();
141 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000142 errs() << "nul";
143 errs() << '\n'
144 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000145 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000146 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000147 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000148 errs() << "nul";
149 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000150 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000151 };
152}
153
154namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000155 //===--------------------------------------------------------------------===//
156 /// ISel - X86 specific code to select X86 machine instructions for
157 /// SelectionDAG operations.
158 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000159 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160 /// X86Lowering - This object fully describes how to lower LLVM code to an
161 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000162 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163
164 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
165 /// make the right decision when generating code for different targets.
166 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000167
Evan Chengb7a75a52008-09-26 23:41:32 +0000168 /// OptForSize - If true, selector should try to optimize for code size
169 /// instead of performance.
170 bool OptForSize;
171
Chris Lattnerc961eea2005-11-16 01:54:32 +0000172 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000173 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000174 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000175 X86Lowering(*tm.getTargetLowering()),
176 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000177 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000178
179 virtual const char *getPassName() const {
180 return "X86 DAG->DAG Instruction Selection";
181 }
182
Evan Chengdb8d56b2008-06-30 20:45:06 +0000183 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000184 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000185 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000186
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000187 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
188
Evan Cheng884c70c2008-11-27 00:49:46 +0000189 virtual
190 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000191
Chris Lattnerc961eea2005-11-16 01:54:32 +0000192// Include the pieces autogenerated from the target description.
193#include "X86GenDAGISel.inc"
194
195 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000197 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000198 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000199
Rafael Espindola094fad32009-04-08 21:14:34 +0000200 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
201 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000202 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000203 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
204 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
205 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000206 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000208 SDValue &Scale, SDValue &Index, SDValue &Disp,
209 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000210 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
211 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000212 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
213 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000214 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
215 SDValue N, SDValue &Base, SDValue &Scale,
216 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000217 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue &InChain, SDValue &OutChain);
219 bool TryFoldLoad(SDValue P, SDValue N,
220 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000221 SDValue &Index, SDValue &Disp,
222 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000223 void PreprocessForRMW();
224 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000225
Chris Lattnerc0bad572006-06-08 18:03:49 +0000226 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
227 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000228 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000229 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000230 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000231
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000232 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
233
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
235 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000236 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000237 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
239 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000240 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000241 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 // These are 32-bit even in 64-bit mode since RIP relative offset
243 // is 32-bit.
244 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000246 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000249 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000252 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000254 else if (AM.BlockAddr)
255 Disp = CurDAG->getBlockAddress(AM.BlockAddr, DebugLoc()/*MVT::i32*/,
256 true /*AM.SymbolFlags*/);
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000259
260 if (AM.Segment.getNode())
261 Segment = AM.Segment;
262 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000264 }
265
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000266 /// getI8Imm - Return a target constant with the specified value, of type
267 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000268 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000270 }
271
Chris Lattnerc961eea2005-11-16 01:54:32 +0000272 /// getI16Imm - Return a target constant with the specified value, of type
273 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000274 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000276 }
277
278 /// getI32Imm - Return a target constant with the specified value, of type
279 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000280 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000282 }
Evan Chengf597dc72006-02-10 22:24:32 +0000283
Dan Gohman8b746962008-09-23 18:22:58 +0000284 /// getGlobalBaseReg - Return an SDNode that returns the value of
285 /// the global base register. Output instructions required to
286 /// initialize the global base register, if necessary.
287 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000288 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000289
Dan Gohmanc5534622009-06-03 20:20:00 +0000290 /// getTargetMachine - Return a reference to the TargetMachine, casted
291 /// to the target-specific type.
292 const X86TargetMachine &getTargetMachine() {
293 return static_cast<const X86TargetMachine &>(TM);
294 }
295
296 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
297 /// to the target-specific type.
298 const X86InstrInfo *getInstrInfo() {
299 return getTargetMachine().getInstrInfo();
300 }
301
Evan Cheng23addc02006-02-10 22:46:26 +0000302#ifndef NDEBUG
303 unsigned Indent;
304#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000305 };
306}
307
Evan Chengf4b4c412006-08-08 00:31:00 +0000308
Evan Cheng884c70c2008-11-27 00:49:46 +0000309bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
310 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000311 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000312
Evan Cheng884c70c2008-11-27 00:49:46 +0000313 if (U == Root)
314 switch (U->getOpcode()) {
315 default: break;
316 case ISD::ADD:
317 case ISD::ADDC:
318 case ISD::ADDE:
319 case ISD::AND:
320 case ISD::OR:
321 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000322 SDValue Op1 = U->getOperand(1);
323
Evan Cheng884c70c2008-11-27 00:49:46 +0000324 // If the other operand is a 8-bit immediate we should fold the immediate
325 // instead. This reduces code size.
326 // e.g.
327 // movl 4(%esp), %eax
328 // addl $4, %eax
329 // vs.
330 // movl $4, %eax
331 // addl 4(%esp), %eax
332 // The former is 2 bytes shorter. In case where the increment is 1, then
333 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000334 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000335 if (Imm->getAPIntValue().isSignedIntN(8))
336 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000337
338 // If the other operand is a TLS address, we should fold it instead.
339 // This produces
340 // movl %gs:0, %eax
341 // leal i@NTPOFF(%eax), %eax
342 // instead of
343 // movl $i@NTPOFF, %eax
344 // addl %gs:0, %eax
345 // if the block also has an access to a second TLS address this will save
346 // a load.
347 // FIXME: This is probably also true for non TLS addresses.
348 if (Op1.getOpcode() == X86ISD::Wrapper) {
349 SDValue Val = Op1.getOperand(0);
350 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
351 return false;
352 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000353 }
354 }
355
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000356 // Proceed to 'generic' cycle finder code
357 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000358}
359
Evan Cheng70e674e2006-08-28 20:10:17 +0000360/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
361/// and move load below the TokenFactor. Replace store's chain operand with
362/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000363static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000364 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000365 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000366 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
367 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000368 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000369 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000370 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000371 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
372 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
373 Load.getOperand(1),
374 Load.getOperand(2));
375 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000376 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000377}
378
Nate Begeman206a3572009-09-16 03:20:46 +0000379/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
380/// chain produced by the load must only be used by the store's chain operand,
381/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000382///
Dan Gohman475871a2008-07-27 21:46:04 +0000383static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
384 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000385 if (N.getOpcode() == ISD::BIT_CONVERT)
386 N = N.getOperand(0);
387
388 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
389 if (!LD || LD->isVolatile())
390 return false;
391 if (LD->getAddressingMode() != ISD::UNINDEXED)
392 return false;
393
394 ISD::LoadExtType ExtType = LD->getExtensionType();
395 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
396 return false;
397
398 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000399 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000400 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000401 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000402 Load = N;
403 return true;
404 }
405 return false;
406}
407
Evan Chengab6c3bb2008-08-25 21:27:18 +0000408/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
409/// operand and move load below the call's chain operand.
410static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000411 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000412 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000413 SDValue Chain = CallSeqStart.getOperand(0);
414 if (Chain.getNode() == Load.getNode())
415 Ops.push_back(Load.getOperand(0));
416 else {
417 assert(Chain.getOpcode() == ISD::TokenFactor &&
418 "Unexpected CallSeqStart chain operand");
419 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
420 if (Chain.getOperand(i).getNode() == Load.getNode())
421 Ops.push_back(Load.getOperand(0));
422 else
423 Ops.push_back(Chain.getOperand(i));
424 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000425 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000427 Ops.clear();
428 Ops.push_back(NewChain);
429 }
430 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
431 Ops.push_back(CallSeqStart.getOperand(i));
432 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000433 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
434 Load.getOperand(1), Load.getOperand(2));
435 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000436 Ops.push_back(SDValue(Load.getNode(), 1));
437 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000438 Ops.push_back(Call.getOperand(i));
439 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
440}
441
442/// isCalleeLoad - Return true if call address is a load and it can be
443/// moved below CALLSEQ_START and the chains leading up to the call.
444/// Return the CALLSEQ_START by reference as a second output.
445static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000446 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000447 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000448 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000449 if (!LD ||
450 LD->isVolatile() ||
451 LD->getAddressingMode() != ISD::UNINDEXED ||
452 LD->getExtensionType() != ISD::NON_EXTLOAD)
453 return false;
454
455 // Now let's find the callseq_start.
456 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
457 if (!Chain.hasOneUse())
458 return false;
459 Chain = Chain.getOperand(0);
460 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000461
462 if (Chain.getOperand(0).getNode() == Callee.getNode())
463 return true;
464 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000465 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
466 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000467 return true;
468 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000469}
470
471
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000472/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000473/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000474/// This allows the instruction selector to pick more read-modify-write
475/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000476///
477/// [Load chain]
478/// ^
479/// |
480/// [Load]
481/// ^ ^
482/// | |
483/// / \-
484/// / |
485/// [TokenFactor] [Op]
486/// ^ ^
487/// | |
488/// \ /
489/// \ /
490/// [Store]
491///
492/// The fact the store's chain operand != load's chain will prevent the
493/// (store (op (load))) instruction from being selected. We can transform it to:
494///
495/// [Load chain]
496/// ^
497/// |
498/// [TokenFactor]
499/// ^
500/// |
501/// [Load]
502/// ^ ^
503/// | |
504/// | \-
505/// | |
506/// | [Op]
507/// | ^
508/// | |
509/// \ /
510/// \ /
511/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000512void X86DAGToDAGISel::PreprocessForRMW() {
513 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
514 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000515 if (I->getOpcode() == X86ISD::CALL) {
516 /// Also try moving call address load from outside callseq_start to just
517 /// before the call to allow it to be folded.
518 ///
519 /// [Load chain]
520 /// ^
521 /// |
522 /// [Load]
523 /// ^ ^
524 /// | |
525 /// / \--
526 /// / |
527 ///[CALLSEQ_START] |
528 /// ^ |
529 /// | |
530 /// [LOAD/C2Reg] |
531 /// | |
532 /// \ /
533 /// \ /
534 /// [CALL]
535 SDValue Chain = I->getOperand(0);
536 SDValue Load = I->getOperand(1);
537 if (!isCalleeLoad(Load, Chain))
538 continue;
539 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
540 ++NumLoadMoved;
541 continue;
542 }
543
Evan Cheng8b2794a2006-10-13 21:14:26 +0000544 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000545 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000546 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000547
Gabor Greifba36cb52008-08-28 21:40:38 +0000548 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000549 continue;
550
Dan Gohman475871a2008-07-27 21:46:04 +0000551 SDValue N1 = I->getOperand(1);
552 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000553 if ((N1.getValueType().isFloatingPoint() &&
554 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000555 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000556 continue;
557
558 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000560 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000561 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000562 case ISD::ADD:
563 case ISD::MUL:
564 case ISD::AND:
565 case ISD::OR:
566 case ISD::XOR:
567 case ISD::ADDC:
568 case ISD::ADDE:
569 case ISD::VECTOR_SHUFFLE: {
570 SDValue N10 = N1.getOperand(0);
571 SDValue N11 = N1.getOperand(1);
572 RModW = isRMWLoad(N10, Chain, N2, Load);
573 if (!RModW)
574 RModW = isRMWLoad(N11, Chain, N2, Load);
575 break;
576 }
577 case ISD::SUB:
578 case ISD::SHL:
579 case ISD::SRA:
580 case ISD::SRL:
581 case ISD::ROTL:
582 case ISD::ROTR:
583 case ISD::SUBC:
584 case ISD::SUBE:
585 case X86ISD::SHLD:
586 case X86ISD::SHRD: {
587 SDValue N10 = N1.getOperand(0);
588 RModW = isRMWLoad(N10, Chain, N2, Load);
589 break;
590 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000591 }
592
Evan Cheng82a35b32006-08-29 06:44:17 +0000593 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000594 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000595 ++NumLoadMoved;
596 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000597 }
598}
599
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000600
601/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
602/// nodes that target the FP stack to be store and load to the stack. This is a
603/// gross hack. We would like to simply mark these as being illegal, but when
604/// we do that, legalize produces these when it expands calls, then expands
605/// these in the same legalize pass. We would like dag combine to be able to
606/// hack on these between the call expansion and the node legalization. As such
607/// this pass basically does "really late" legalization of these inline with the
608/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000609void X86DAGToDAGISel::PreprocessForFPConvert() {
610 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
611 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000612 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
613 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
614 continue;
615
616 // If the source and destination are SSE registers, then this is a legal
617 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000618 EVT SrcVT = N->getOperand(0).getValueType();
619 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000620 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
621 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
622 if (SrcIsSSE && DstIsSSE)
623 continue;
624
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000625 if (!SrcIsSSE && !DstIsSSE) {
626 // If this is an FPStack extension, it is a noop.
627 if (N->getOpcode() == ISD::FP_EXTEND)
628 continue;
629 // If this is a value-preserving FPStack truncation, it is a noop.
630 if (N->getConstantOperandVal(1))
631 continue;
632 }
633
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000634 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
635 // FPStack has extload and truncstore. SSE can fold direct loads into other
636 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000637 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000638 if (N->getOpcode() == ISD::FP_ROUND)
639 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
640 else
641 MemVT = SrcIsSSE ? SrcVT : DstVT;
642
Dan Gohmanf350b272008-08-23 02:25:05 +0000643 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000644 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000645
646 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000647 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000648 N->getOperand(0),
649 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000650 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000651 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000652
653 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
654 // extload we created. This will cause general havok on the dag because
655 // anything below the conversion could be folded into other existing nodes.
656 // To avoid invalidating 'I', back it up to the convert node.
657 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000658 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000659
660 // Now that we did that, the node is dead. Increment the iterator to the
661 // next node to process, then delete N.
662 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000663 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000664 }
665}
666
Chris Lattnerc961eea2005-11-16 01:54:32 +0000667/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
668/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000669void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000670 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000671 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000672
Bill Wendling98a366d2009-04-29 23:29:43 +0000673 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000674 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000675
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000676 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000677 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000678
Chris Lattnerc961eea2005-11-16 01:54:32 +0000679 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000680#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000681 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000682 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000683#endif
David Greene8ad4c002008-10-27 21:56:29 +0000684 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000685#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000686 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000687#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000688
Dan Gohmanf350b272008-08-23 02:25:05 +0000689 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000690}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000691
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000692/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
693/// the main function.
694void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
695 MachineFrameInfo *MFI) {
696 const TargetInstrInfo *TII = TM.getInstrInfo();
697 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000698 BuildMI(BB, DebugLoc::getUnknownLoc(),
699 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000700}
701
702void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
703 // If this is main, emit special code for main.
704 MachineBasicBlock *BB = MF.begin();
705 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
706 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
707}
708
Rafael Espindola094fad32009-04-08 21:14:34 +0000709
710bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
711 X86ISelAddressMode &AM) {
712 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
713 SDValue Segment = N.getOperand(0);
714
715 if (AM.Segment.getNode() == 0) {
716 AM.Segment = Segment;
717 return false;
718 }
719
720 return true;
721}
722
723bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
724 // This optimization is valid because the GNU TLS model defines that
725 // gs:0 (or fs:0 on X86-64) contains its own address.
726 // For more information see http://people.redhat.com/drepper/tls.pdf
727
728 SDValue Address = N.getOperand(1);
729 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
730 !MatchSegmentBaseAddress (Address, AM))
731 return false;
732
733 return true;
734}
735
Chris Lattner18c59872009-06-27 04:16:01 +0000736/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
737/// into an addressing mode. These wrap things that will resolve down into a
738/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000739/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000740bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000741 // If the addressing mode already has a symbol as the displacement, we can
742 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000743 if (AM.hasSymbolicDisplacement())
744 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000745
746 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000747 CodeModel::Model M = TM.getCodeModel();
748
Chris Lattner18c59872009-06-27 04:16:01 +0000749 // Handle X86-64 rip-relative addresses. We check this before checking direct
750 // folding because RIP is preferable to non-RIP accesses.
751 if (Subtarget->is64Bit() &&
752 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
753 // they cannot be folded into immediate fields.
754 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000755 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000756 // Base and index reg must be 0 in order to use %rip as base and lowering
757 // must allow RIP.
758 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000759 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
760 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000761 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000762 AM.GV = G->getGlobal();
763 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000764 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000765 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
766 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000767 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000768 AM.CP = CP->getConstVal();
769 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000770 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000771 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000772 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
773 AM.ES = S->getSymbol();
774 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000775 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000776 AM.JT = J->getIndex();
777 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000778 } else {
779 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
780 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000781 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000782
Chris Lattner18c59872009-06-27 04:16:01 +0000783 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000785 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000786 }
787
788 // Handle the case when globals fit in our immediate field: This is true for
789 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
790 // mode, this results in a non-RIP-relative computation.
791 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000792 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000793 TM.getRelocationModel() == Reloc::Static)) {
794 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
795 AM.GV = G->getGlobal();
796 AM.Disp += G->getOffset();
797 AM.SymbolFlags = G->getTargetFlags();
798 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
799 AM.CP = CP->getConstVal();
800 AM.Align = CP->getAlignment();
801 AM.Disp += CP->getOffset();
802 AM.SymbolFlags = CP->getTargetFlags();
803 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
804 AM.ES = S->getSymbol();
805 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000806 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000807 AM.JT = J->getIndex();
808 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000809 } else {
810 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
811 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000812 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000813 return false;
814 }
815
816 return true;
817}
818
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000819/// MatchAddress - Add the specified node to the specified addressing mode,
820/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000821/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000822bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
823 if (MatchAddressRecursively(N, AM, 0))
824 return true;
825
826 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
827 // a smaller encoding and avoids a scaled-index.
828 if (AM.Scale == 2 &&
829 AM.BaseType == X86ISelAddressMode::RegBase &&
830 AM.Base.Reg.getNode() == 0) {
831 AM.Base.Reg = AM.IndexReg;
832 AM.Scale = 1;
833 }
834
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000835 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
836 // because it has a smaller encoding.
837 // TODO: Which other code models can use this?
838 if (TM.getCodeModel() == CodeModel::Small &&
839 Subtarget->is64Bit() &&
840 AM.Scale == 1 &&
841 AM.BaseType == X86ISelAddressMode::RegBase &&
842 AM.Base.Reg.getNode() == 0 &&
843 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000844 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000845 AM.hasSymbolicDisplacement())
846 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
847
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000848 return false;
849}
850
851bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
852 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000853 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000854 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000855 DEBUG({
856 errs() << "MatchAddress: ";
857 AM.dump();
858 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000859 // Limit recursion.
860 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000861 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000862
863 CodeModel::Model M = TM.getCodeModel();
864
Chris Lattner18c59872009-06-27 04:16:01 +0000865 // If this is already a %rip relative address, we can only merge immediates
866 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000867 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000868 if (AM.isRIPRelative()) {
869 // FIXME: JumpTable and ExternalSymbol address currently don't like
870 // displacements. It isn't very important, but this should be fixed for
871 // consistency.
872 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000873
Chris Lattner18c59872009-06-27 04:16:01 +0000874 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
875 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000876 if (X86::isOffsetSuitableForCodeModel(Val, M,
877 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000878 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000879 return false;
880 }
881 }
882 return true;
883 }
884
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000885 switch (N.getOpcode()) {
886 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000887 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000888 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000889 if (!is64Bit ||
890 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
891 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000892 AM.Disp += Val;
893 return false;
894 }
895 break;
896 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000897
Rafael Espindola094fad32009-04-08 21:14:34 +0000898 case X86ISD::SegmentBaseAddress:
899 if (!MatchSegmentBaseAddress(N, AM))
900 return false;
901 break;
902
Rafael Espindola49a168d2009-04-12 21:55:03 +0000903 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000904 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000905 if (!MatchWrapper(N, AM))
906 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000907 break;
908
Rafael Espindola094fad32009-04-08 21:14:34 +0000909 case ISD::LOAD:
910 if (!MatchLoad(N, AM))
911 return false;
912 break;
913
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000915 if (AM.BaseType == X86ISelAddressMode::RegBase
916 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000917 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
918 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
919 return false;
920 }
921 break;
Evan Chengec693f72005-12-08 02:01:35 +0000922
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000923 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000924 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000925 break;
926
Gabor Greif93c53e52008-08-31 15:37:04 +0000927 if (ConstantSDNode
928 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000929 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000930 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
931 // that the base operand remains free for further matching. If
932 // the base doesn't end up getting used, a post-processing step
933 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000934 if (Val == 1 || Val == 2 || Val == 3) {
935 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000936 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000937
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000938 // Okay, we know that we have a scale by now. However, if the scaled
939 // value is an add of something and a constant, we can fold the
940 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
942 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
943 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000944 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000945 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000946 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000947 if (!is64Bit ||
948 X86::isOffsetSuitableForCodeModel(Disp, M,
949 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000950 AM.Disp = Disp;
951 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000952 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000953 } else {
954 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000955 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000956 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000957 }
958 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000959 }
Evan Chengec693f72005-12-08 02:01:35 +0000960
Dan Gohman83688052007-10-22 20:22:24 +0000961 case ISD::SMUL_LOHI:
962 case ISD::UMUL_LOHI:
963 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000964 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000965 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000966 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000967 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000968 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000969 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000970 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000971 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000972 if (ConstantSDNode
973 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000974 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
975 CN->getZExtValue() == 9) {
976 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000977
Gabor Greifba36cb52008-08-28 21:40:38 +0000978 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000980
981 // Okay, we know that we have a scale by now. However, if the scaled
982 // value is an add of something and a constant, we can fold the
983 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000984 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
985 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
986 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000987 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000988 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000989 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000990 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000991 if (!is64Bit ||
992 X86::isOffsetSuitableForCodeModel(Disp, M,
993 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000994 AM.Disp = Disp;
995 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000996 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000997 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000998 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000999 }
1000
1001 AM.IndexReg = AM.Base.Reg = Reg;
1002 return false;
1003 }
Chris Lattner62412262007-02-04 20:18:17 +00001004 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001005 break;
1006
Dan Gohman3cd90a12009-05-11 18:02:53 +00001007 case ISD::SUB: {
1008 // Given A-B, if A can be completely folded into the address and
1009 // the index field with the index field unused, use -B as the index.
1010 // This is a win if a has multiple parts that can be folded into
1011 // the address. Also, this saves a mov if the base register has
1012 // other uses, since it avoids a two-address sub instruction, however
1013 // it costs an additional mov if the index register has other uses.
1014
1015 // Test if the LHS of the sub can be folded.
1016 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001017 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001018 AM = Backup;
1019 break;
1020 }
1021 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001022 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001023 AM = Backup;
1024 break;
1025 }
1026 int Cost = 0;
1027 SDValue RHS = N.getNode()->getOperand(1);
1028 // If the RHS involves a register with multiple uses, this
1029 // transformation incurs an extra mov, due to the neg instruction
1030 // clobbering its operand.
1031 if (!RHS.getNode()->hasOneUse() ||
1032 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1033 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1034 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1035 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001037 ++Cost;
1038 // If the base is a register with multiple uses, this
1039 // transformation may save a mov.
1040 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1041 AM.Base.Reg.getNode() &&
1042 !AM.Base.Reg.getNode()->hasOneUse()) ||
1043 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1044 --Cost;
1045 // If the folded LHS was interesting, this transformation saves
1046 // address arithmetic.
1047 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1048 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1049 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1050 --Cost;
1051 // If it doesn't look like it may be an overall win, don't do it.
1052 if (Cost >= 0) {
1053 AM = Backup;
1054 break;
1055 }
1056
1057 // Ok, the transformation is legal and appears profitable. Go for it.
1058 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1059 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1060 AM.IndexReg = Neg;
1061 AM.Scale = 1;
1062
1063 // Insert the new nodes into the topological ordering.
1064 if (Zero.getNode()->getNodeId() == -1 ||
1065 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1066 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1067 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1068 }
1069 if (Neg.getNode()->getNodeId() == -1 ||
1070 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1071 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1072 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1073 }
1074 return false;
1075 }
1076
Evan Cheng8e278262009-01-17 07:09:27 +00001077 case ISD::ADD: {
1078 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001079 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1080 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001081 return false;
1082 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001083 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1084 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001085 return false;
1086 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001087
1088 // If we couldn't fold both operands into the address at the same time,
1089 // see if we can just put each operand into a register and fold at least
1090 // the add.
1091 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1092 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001093 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001094 AM.Base.Reg = N.getNode()->getOperand(0);
1095 AM.IndexReg = N.getNode()->getOperand(1);
1096 AM.Scale = 1;
1097 return false;
1098 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001099 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001100 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001101
Chris Lattner62412262007-02-04 20:18:17 +00001102 case ISD::OR:
1103 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001104 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1105 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001106 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001107 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001108 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001109 // Address could not have picked a GV address for the displacement.
1110 AM.GV == NULL &&
1111 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001112 (!is64Bit ||
1113 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1114 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001115 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001116 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001117 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001118 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001119 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001120 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001121 }
1122 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001123
1124 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001125 // Perform some heroic transforms on an and of a constant-count shift
1126 // with a constant to enable use of the scaled offset field.
1127
Dan Gohman475871a2008-07-27 21:46:04 +00001128 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001129 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001130
Evan Cheng1314b002007-12-13 00:43:27 +00001131 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001132 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001133
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001134 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001135 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1136 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1137 if (!C1 || !C2) break;
1138
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001139 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1140 // allows us to convert the shift and and into an h-register extract and
1141 // a scaled index.
1142 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1143 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001144 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001145 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001146 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001147 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1148 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1149 X, Eight);
1150 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1151 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001153 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1154 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001155
1156 // Insert the new nodes into the topological ordering.
1157 if (Eight.getNode()->getNodeId() == -1 ||
1158 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1159 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1160 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1161 }
1162 if (Mask.getNode()->getNodeId() == -1 ||
1163 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1164 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1165 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1166 }
1167 if (Srl.getNode()->getNodeId() == -1 ||
1168 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1169 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1170 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1171 }
1172 if (And.getNode()->getNodeId() == -1 ||
1173 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1174 CurDAG->RepositionNode(N.getNode(), And.getNode());
1175 And.getNode()->setNodeId(N.getNode()->getNodeId());
1176 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001177 if (ShlCount.getNode()->getNodeId() == -1 ||
1178 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1179 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1180 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1181 }
1182 if (Shl.getNode()->getNodeId() == -1 ||
1183 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1184 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1185 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1186 }
1187 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001188 AM.IndexReg = And;
1189 AM.Scale = (1 << ScaleLog);
1190 return false;
1191 }
1192 }
1193
1194 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1195 // allows us to fold the shift into this addressing mode.
1196 if (Shift.getOpcode() != ISD::SHL) break;
1197
Evan Cheng1314b002007-12-13 00:43:27 +00001198 // Not likely to be profitable if either the AND or SHIFT node has more
1199 // than one use (unless all uses are for address computation). Besides,
1200 // isel mechanism requires their node ids to be reused.
1201 if (!N.hasOneUse() || !Shift.hasOneUse())
1202 break;
1203
1204 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001205 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001206 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1207 break;
1208
1209 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001210 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001211 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001212 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1213 NewANDMask);
1214 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001215 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001216
1217 // Insert the new nodes into the topological ordering.
1218 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1219 CurDAG->RepositionNode(X.getNode(), C1);
1220 C1->setNodeId(X.getNode()->getNodeId());
1221 }
1222 if (NewANDMask.getNode()->getNodeId() == -1 ||
1223 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1224 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1225 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1226 }
1227 if (NewAND.getNode()->getNodeId() == -1 ||
1228 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1229 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1230 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1231 }
1232 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1233 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1234 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1235 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1236 }
1237
Dan Gohman7b8e9642008-10-13 20:52:04 +00001238 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001239
1240 AM.Scale = 1 << ShiftCst;
1241 AM.IndexReg = NewAND;
1242 return false;
1243 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001244 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001245
Rafael Espindola523249f2009-03-31 16:16:57 +00001246 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001247}
1248
1249/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1250/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001251bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001252 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001253 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001254 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001255 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001256 AM.IndexReg = N;
1257 AM.Scale = 1;
1258 return false;
1259 }
1260
1261 // Otherwise, we cannot select it.
1262 return true;
1263 }
1264
1265 // Default, generate it as a register.
1266 AM.BaseType = X86ISelAddressMode::RegBase;
1267 AM.Base.Reg = N;
1268 return false;
1269}
1270
Evan Chengec693f72005-12-08 02:01:35 +00001271/// SelectAddr - returns true if it is able pattern match an addressing mode.
1272/// It returns the operands which make up the maximal addressing mode it can
1273/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001274bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1275 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001276 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001277 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001278 bool Done = false;
1279 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1280 unsigned Opcode = N.getOpcode();
1281 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001282 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001283 // If we are able to fold N into addressing mode, then we'll allow it even
1284 // if N has multiple uses. In general, addressing computation is used as
1285 // addresses by all of its uses. But watch out for CopyToReg uses, that
1286 // means the address computation is liveout. It will be computed by a LEA
1287 // so we want to avoid computing the address twice.
1288 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1289 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1290 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001291 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001292 Done = true;
1293 break;
1294 }
1295 }
1296 }
1297 }
1298
1299 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001300 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001301
Owen Andersone50ed302009-08-10 22:56:29 +00001302 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001303 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001305 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001306 }
Evan Cheng8700e142006-01-11 06:09:51 +00001307
Gabor Greifba36cb52008-08-28 21:40:38 +00001308 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001309 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001310
Rafael Espindola094fad32009-04-08 21:14:34 +00001311 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001312 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001313}
1314
Chris Lattner3a7cd952006-10-07 21:55:32 +00001315/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1316/// match a load whose top elements are either undef or zeros. The load flavor
1317/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001318bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1319 SDValue N, SDValue &Base,
1320 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001321 SDValue &Disp, SDValue &Segment,
1322 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001324 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001325 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001326 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001327 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001328 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001329 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001330 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001331 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001332 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001333 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001334 return true;
1335 }
1336 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001337
1338 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001339 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001340 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001341 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001342 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001343 N.getOperand(0).getNode()->hasOneUse() &&
1344 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001345 N.getOperand(0).getOperand(0).hasOneUse()) {
1346 // Okay, this is a zero extending load. Fold it.
1347 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001348 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001349 return false;
1350 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001351 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001352 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001353 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001354 return false;
1355}
1356
1357
Evan Cheng51a9ed92006-02-25 10:09:08 +00001358/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1359/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001360bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1361 SDValue &Base, SDValue &Scale,
1362 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001363 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001364
1365 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1366 // segments.
1367 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001368 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001369 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001370 if (MatchAddress(N, AM))
1371 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001372 assert (T == AM.Segment);
1373 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001374
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001376 unsigned Complexity = 0;
1377 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001378 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001379 Complexity = 1;
1380 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001381 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001382 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1383 Complexity = 4;
1384
Gabor Greifba36cb52008-08-28 21:40:38 +00001385 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001386 Complexity++;
1387 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001388 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001389
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001390 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1391 // a simple shift.
1392 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001393 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001394
1395 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1396 // to a LEA. This is determined with some expermentation but is by no means
1397 // optimal (especially for code size consideration). LEA is nice because of
1398 // its three-address nature. Tweak the cost function again when we can run
1399 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001400 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001401 // For X86-64, we should always use lea to materialize RIP relative
1402 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001403 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001404 Complexity = 4;
1405 else
1406 Complexity += 2;
1407 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001408
Gabor Greifba36cb52008-08-28 21:40:38 +00001409 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001410 Complexity++;
1411
Chris Lattner25142782009-07-11 22:50:33 +00001412 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001413 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001414 return false;
1415
1416 SDValue Segment;
1417 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1418 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001419}
1420
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001421/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1422bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1423 SDValue &Scale, SDValue &Index,
1424 SDValue &Disp) {
1425 assert(Op.getOpcode() == X86ISD::TLSADDR);
1426 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1427 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1428
1429 X86ISelAddressMode AM;
1430 AM.GV = GA->getGlobal();
1431 AM.Disp += GA->getOffset();
1432 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001433 AM.SymbolFlags = GA->getTargetFlags();
1434
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001436 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001437 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001438 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001439 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001440 }
1441
1442 SDValue Segment;
1443 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1444 return true;
1445}
1446
1447
Dan Gohman475871a2008-07-27 21:46:04 +00001448bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1449 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001450 SDValue &Index, SDValue &Disp,
1451 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001452 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001453 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001454 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001455 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001456 return false;
1457}
1458
Dan Gohman8b746962008-09-23 18:22:58 +00001459/// getGlobalBaseReg - Return an SDNode that returns the value of
1460/// the global base register. Output instructions required to
1461/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001462///
Evan Cheng9ade2182006-08-26 05:34:46 +00001463SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001464 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001466}
1467
Evan Chengb245d922006-05-20 01:36:52 +00001468static SDNode *FindCallStartFromCall(SDNode *Node) {
1469 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001470 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001471 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001472 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001473}
1474
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001475SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1476 SDValue Chain = Node->getOperand(0);
1477 SDValue In1 = Node->getOperand(1);
1478 SDValue In2L = Node->getOperand(2);
1479 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001480 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1481 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001482 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001483 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1484 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1485 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1486 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1487 MVT::i32, MVT::i32, MVT::Other, Ops,
1488 array_lengthof(Ops));
1489 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1490 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001491}
Christopher Lambc59e5212007-08-10 21:48:46 +00001492
Owen Andersone50ed302009-08-10 22:56:29 +00001493SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001494 if (Node->hasAnyUseOfValue(0))
1495 return 0;
1496
1497 // Optimize common patterns for __sync_add_and_fetch and
1498 // __sync_sub_and_fetch where the result is not used. This allows us
1499 // to use "lock" version of add, sub, inc, dec instructions.
1500 // FIXME: Do not use special instructions but instead add the "lock"
1501 // prefix to the target node somehow. The extra information will then be
1502 // transferred to machine instruction and it denotes the prefix.
1503 SDValue Chain = Node->getOperand(0);
1504 SDValue Ptr = Node->getOperand(1);
1505 SDValue Val = Node->getOperand(2);
1506 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1507 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1508 return 0;
1509
1510 bool isInc = false, isDec = false, isSub = false, isCN = false;
1511 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1512 if (CN) {
1513 isCN = true;
1514 int64_t CNVal = CN->getSExtValue();
1515 if (CNVal == 1)
1516 isInc = true;
1517 else if (CNVal == -1)
1518 isDec = true;
1519 else if (CNVal >= 0)
1520 Val = CurDAG->getTargetConstant(CNVal, NVT);
1521 else {
1522 isSub = true;
1523 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1524 }
1525 } else if (Val.hasOneUse() &&
1526 Val.getOpcode() == ISD::SUB &&
1527 X86::isZeroNode(Val.getOperand(0))) {
1528 isSub = true;
1529 Val = Val.getOperand(1);
1530 }
1531
1532 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001534 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001536 if (isInc)
1537 Opc = X86::LOCK_INC8m;
1538 else if (isDec)
1539 Opc = X86::LOCK_DEC8m;
1540 else if (isSub) {
1541 if (isCN)
1542 Opc = X86::LOCK_SUB8mi;
1543 else
1544 Opc = X86::LOCK_SUB8mr;
1545 } else {
1546 if (isCN)
1547 Opc = X86::LOCK_ADD8mi;
1548 else
1549 Opc = X86::LOCK_ADD8mr;
1550 }
1551 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001553 if (isInc)
1554 Opc = X86::LOCK_INC16m;
1555 else if (isDec)
1556 Opc = X86::LOCK_DEC16m;
1557 else if (isSub) {
1558 if (isCN) {
1559 if (Predicate_i16immSExt8(Val.getNode()))
1560 Opc = X86::LOCK_SUB16mi8;
1561 else
1562 Opc = X86::LOCK_SUB16mi;
1563 } else
1564 Opc = X86::LOCK_SUB16mr;
1565 } else {
1566 if (isCN) {
1567 if (Predicate_i16immSExt8(Val.getNode()))
1568 Opc = X86::LOCK_ADD16mi8;
1569 else
1570 Opc = X86::LOCK_ADD16mi;
1571 } else
1572 Opc = X86::LOCK_ADD16mr;
1573 }
1574 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001576 if (isInc)
1577 Opc = X86::LOCK_INC32m;
1578 else if (isDec)
1579 Opc = X86::LOCK_DEC32m;
1580 else if (isSub) {
1581 if (isCN) {
1582 if (Predicate_i32immSExt8(Val.getNode()))
1583 Opc = X86::LOCK_SUB32mi8;
1584 else
1585 Opc = X86::LOCK_SUB32mi;
1586 } else
1587 Opc = X86::LOCK_SUB32mr;
1588 } else {
1589 if (isCN) {
1590 if (Predicate_i32immSExt8(Val.getNode()))
1591 Opc = X86::LOCK_ADD32mi8;
1592 else
1593 Opc = X86::LOCK_ADD32mi;
1594 } else
1595 Opc = X86::LOCK_ADD32mr;
1596 }
1597 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001598 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001599 if (isInc)
1600 Opc = X86::LOCK_INC64m;
1601 else if (isDec)
1602 Opc = X86::LOCK_DEC64m;
1603 else if (isSub) {
1604 Opc = X86::LOCK_SUB64mr;
1605 if (isCN) {
1606 if (Predicate_i64immSExt8(Val.getNode()))
1607 Opc = X86::LOCK_SUB64mi8;
1608 else if (Predicate_i64immSExt32(Val.getNode()))
1609 Opc = X86::LOCK_SUB64mi32;
1610 }
1611 } else {
1612 Opc = X86::LOCK_ADD64mr;
1613 if (isCN) {
1614 if (Predicate_i64immSExt8(Val.getNode()))
1615 Opc = X86::LOCK_ADD64mi8;
1616 else if (Predicate_i64immSExt32(Val.getNode()))
1617 Opc = X86::LOCK_ADD64mi32;
1618 }
1619 }
1620 break;
1621 }
1622
1623 DebugLoc dl = Node->getDebugLoc();
Dan Gohman602b0c82009-09-25 18:54:59 +00001624 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1625 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001626 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1627 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001628 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001629 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1630 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1631 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001632 SDValue RetVals[] = { Undef, Ret };
1633 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1634 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001635 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1636 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1637 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001638 SDValue RetVals[] = { Undef, Ret };
1639 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1640 }
1641}
1642
Dan Gohman11596ed2009-10-09 20:35:19 +00001643/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1644/// any uses which require the SF or OF bits to be accurate.
1645static bool HasNoSignedComparisonUses(SDNode *N) {
1646 // Examine each user of the node.
1647 for (SDNode::use_iterator UI = N->use_begin(),
1648 UE = N->use_end(); UI != UE; ++UI) {
1649 // Only examine CopyToReg uses.
1650 if (UI->getOpcode() != ISD::CopyToReg)
1651 return false;
1652 // Only examine CopyToReg uses that copy to EFLAGS.
1653 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1654 X86::EFLAGS)
1655 return false;
1656 // Examine each user of the CopyToReg use.
1657 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1658 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1659 // Only examine the Flag result.
1660 if (FlagUI.getUse().getResNo() != 1) continue;
1661 // Anything unusual: assume conservatively.
1662 if (!FlagUI->isMachineOpcode()) return false;
1663 // Examine the opcode of the user.
1664 switch (FlagUI->getMachineOpcode()) {
1665 // These comparisons don't treat the most significant bit specially.
1666 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1667 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1668 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1669 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1670 case X86::JA: case X86::JAE: case X86::JB: case X86::JBE:
1671 case X86::JE: case X86::JNE: case X86::JP: case X86::JNP:
1672 case X86::CMOVA16rr: case X86::CMOVA16rm:
1673 case X86::CMOVA32rr: case X86::CMOVA32rm:
1674 case X86::CMOVA64rr: case X86::CMOVA64rm:
1675 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1676 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1677 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1678 case X86::CMOVB16rr: case X86::CMOVB16rm:
1679 case X86::CMOVB32rr: case X86::CMOVB32rm:
1680 case X86::CMOVB64rr: case X86::CMOVB64rm:
1681 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1682 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1683 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1684 case X86::CMOVE16rr: case X86::CMOVE16rm:
1685 case X86::CMOVE32rr: case X86::CMOVE32rm:
1686 case X86::CMOVE64rr: case X86::CMOVE64rm:
1687 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1688 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1689 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1690 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1691 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1692 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1693 case X86::CMOVP16rr: case X86::CMOVP16rm:
1694 case X86::CMOVP32rr: case X86::CMOVP32rm:
1695 case X86::CMOVP64rr: case X86::CMOVP64rm:
1696 continue;
1697 // Anything else: assume conservatively.
1698 default: return false;
1699 }
1700 }
1701 }
1702 return true;
1703}
1704
Dan Gohman475871a2008-07-27 21:46:04 +00001705SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001706 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001707 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001708 unsigned Opc, MOpc;
1709 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001710 DebugLoc dl = Node->getDebugLoc();
1711
Evan Chengf597dc72006-02-10 22:24:32 +00001712#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001713 DEBUG({
1714 errs() << std::string(Indent, ' ') << "Selecting: ";
1715 Node->dump(CurDAG);
1716 errs() << '\n';
1717 });
Evan Cheng23addc02006-02-10 22:46:26 +00001718 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001719#endif
1720
Dan Gohmane8be6c62008-07-17 19:10:17 +00001721 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001722#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001723 DEBUG({
1724 errs() << std::string(Indent-2, ' ') << "== ";
1725 Node->dump(CurDAG);
1726 errs() << '\n';
1727 });
Evan Cheng23addc02006-02-10 22:46:26 +00001728 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001729#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001730 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001731 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001732
Evan Cheng0114e942006-01-06 20:36:21 +00001733 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001734 default: break;
1735 case X86ISD::GlobalBaseReg:
1736 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001737
Dan Gohman72677342009-08-02 16:10:52 +00001738 case X86ISD::ATOMOR64_DAG:
1739 return SelectAtomic64(Node, X86::ATOMOR6432);
1740 case X86ISD::ATOMXOR64_DAG:
1741 return SelectAtomic64(Node, X86::ATOMXOR6432);
1742 case X86ISD::ATOMADD64_DAG:
1743 return SelectAtomic64(Node, X86::ATOMADD6432);
1744 case X86ISD::ATOMSUB64_DAG:
1745 return SelectAtomic64(Node, X86::ATOMSUB6432);
1746 case X86ISD::ATOMNAND64_DAG:
1747 return SelectAtomic64(Node, X86::ATOMNAND6432);
1748 case X86ISD::ATOMAND64_DAG:
1749 return SelectAtomic64(Node, X86::ATOMAND6432);
1750 case X86ISD::ATOMSWAP64_DAG:
1751 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001752
Dan Gohman72677342009-08-02 16:10:52 +00001753 case ISD::ATOMIC_LOAD_ADD: {
1754 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1755 if (RetVal)
1756 return RetVal;
1757 break;
1758 }
1759
1760 case ISD::SMUL_LOHI:
1761 case ISD::UMUL_LOHI: {
1762 SDValue N0 = Node->getOperand(0);
1763 SDValue N1 = Node->getOperand(1);
1764
1765 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001766 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001768 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1770 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1771 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1772 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001773 }
Bill Wendling12321672009-08-07 21:33:25 +00001774 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001776 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001777 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1778 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1779 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1780 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001781 }
Bill Wendling12321672009-08-07 21:33:25 +00001782 }
Dan Gohman72677342009-08-02 16:10:52 +00001783
1784 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001786 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1788 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1789 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1790 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001791 }
1792
1793 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1794 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001795 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001796 if (!foldedLoad) {
1797 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1798 if (foldedLoad)
1799 std::swap(N0, N1);
1800 }
1801
1802 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1803 N0, SDValue()).getValue(1);
1804
1805 if (foldedLoad) {
1806 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1807 InFlag };
1808 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001809 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1810 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001811 InFlag = SDValue(CNode, 1);
1812 // Update the chain.
1813 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1814 } else {
1815 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001816 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001817 }
1818
1819 // Copy the low half of the result, if it is needed.
1820 if (!N.getValue(0).use_empty()) {
1821 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1822 LoReg, NVT, InFlag);
1823 InFlag = Result.getValue(2);
1824 ReplaceUses(N.getValue(0), Result);
1825#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001826 DEBUG({
1827 errs() << std::string(Indent-2, ' ') << "=> ";
1828 Result.getNode()->dump(CurDAG);
1829 errs() << '\n';
1830 });
Dan Gohman72677342009-08-02 16:10:52 +00001831#endif
1832 }
1833 // Copy the high half of the result, if it is needed.
1834 if (!N.getValue(1).use_empty()) {
1835 SDValue Result;
1836 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1837 // Prevent use of AH in a REX instruction by referencing AX instead.
1838 // Shift it down 8 bits.
1839 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001841 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001842 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1843 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001845 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001846 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1847 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001848 } else {
1849 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1850 HiReg, NVT, InFlag);
1851 InFlag = Result.getValue(2);
1852 }
1853 ReplaceUses(N.getValue(1), Result);
1854#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001855 DEBUG({
1856 errs() << std::string(Indent-2, ' ') << "=> ";
1857 Result.getNode()->dump(CurDAG);
1858 errs() << '\n';
1859 });
Dan Gohman72677342009-08-02 16:10:52 +00001860#endif
1861 }
1862
1863#ifndef NDEBUG
1864 Indent -= 2;
1865#endif
1866
1867 return NULL;
1868 }
1869
1870 case ISD::SDIVREM:
1871 case ISD::UDIVREM: {
1872 SDValue N0 = Node->getOperand(0);
1873 SDValue N1 = Node->getOperand(1);
1874
1875 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001876 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001878 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1880 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1881 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1882 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001883 }
Bill Wendling12321672009-08-07 21:33:25 +00001884 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001886 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1888 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1889 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1890 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001891 }
Bill Wendling12321672009-08-07 21:33:25 +00001892 }
Dan Gohman72677342009-08-02 16:10:52 +00001893
1894 unsigned LoReg, HiReg;
1895 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001897 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001899 LoReg = X86::AL; HiReg = X86::AH;
1900 ClrOpcode = 0;
1901 SExtOpcode = X86::CBW;
1902 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001903 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001904 LoReg = X86::AX; HiReg = X86::DX;
1905 ClrOpcode = X86::MOV16r0;
1906 SExtOpcode = X86::CWD;
1907 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001909 LoReg = X86::EAX; HiReg = X86::EDX;
1910 ClrOpcode = X86::MOV32r0;
1911 SExtOpcode = X86::CDQ;
1912 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001914 LoReg = X86::RAX; HiReg = X86::RDX;
1915 ClrOpcode = ~0U; // NOT USED.
1916 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001917 break;
1918 }
1919
Dan Gohman72677342009-08-02 16:10:52 +00001920 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1921 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1922 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001923
Dan Gohman72677342009-08-02 16:10:52 +00001924 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001926 // Special case for div8, just use a move with zero extension to AX to
1927 // clear the upper 8 bits (AH).
1928 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1929 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1930 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1931 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001932 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1933 MVT::Other, Ops,
1934 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001935 Chain = Move.getValue(1);
1936 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001937 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001938 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001939 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001940 Chain = CurDAG->getEntryNode();
1941 }
1942 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1943 InFlag = Chain.getValue(1);
1944 } else {
1945 InFlag =
1946 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1947 LoReg, N0, SDValue()).getValue(1);
1948 if (isSigned && !signBitIsZero) {
1949 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001950 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001951 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001952 } else {
1953 // Zero out the high part, effectively zero extending the input.
1954 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001955
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 if (NVT.getSimpleVT() == MVT::i64) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001957 ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001958 0);
1959 // We just did a 32-bit clear, insert it into a 64-bit register to
1960 // clear the whole 64-bit reg.
Dan Gohman7289ed22009-11-05 23:53:08 +00001961 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i64);
Dan Gohman72677342009-08-02 16:10:52 +00001962 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001963 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001964 ClrNode =
Dan Gohman7289ed22009-11-05 23:53:08 +00001965 SDValue(CurDAG->getMachineNode(TargetInstrInfo::SUBREG_TO_REG, dl,
1966 MVT::i64, Zero, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001967 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001968 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +00001969 ClrNode = SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001970 }
Dan Gohman72677342009-08-02 16:10:52 +00001971
1972 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1973 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001974 }
Evan Cheng948f3432006-01-06 23:19:29 +00001975 }
Dan Gohman525178c2007-10-08 18:33:35 +00001976
Dan Gohman72677342009-08-02 16:10:52 +00001977 if (foldedLoad) {
1978 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1979 InFlag };
1980 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001981 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1982 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001983 InFlag = SDValue(CNode, 1);
1984 // Update the chain.
1985 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1986 } else {
1987 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001988 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001989 }
Evan Cheng948f3432006-01-06 23:19:29 +00001990
Dan Gohman72677342009-08-02 16:10:52 +00001991 // Copy the division (low) result, if it is needed.
1992 if (!N.getValue(0).use_empty()) {
1993 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1994 LoReg, NVT, InFlag);
1995 InFlag = Result.getValue(2);
1996 ReplaceUses(N.getValue(0), Result);
1997#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001998 DEBUG({
1999 errs() << std::string(Indent-2, ' ') << "=> ";
2000 Result.getNode()->dump(CurDAG);
2001 errs() << '\n';
2002 });
Dan Gohman72677342009-08-02 16:10:52 +00002003#endif
2004 }
2005 // Copy the remainder (high) result, if it is needed.
2006 if (!N.getValue(1).use_empty()) {
2007 SDValue Result;
2008 if (HiReg == X86::AH && Subtarget->is64Bit()) {
2009 // Prevent use of AH in a REX instruction by referencing AX instead.
2010 // Shift it down 8 bits.
2011 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002013 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00002014 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00002015 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00002017 0);
2018 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00002019 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2020 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00002021 } else {
2022 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2023 HiReg, NVT, InFlag);
2024 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00002025 }
Dan Gohman72677342009-08-02 16:10:52 +00002026 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002027#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002028 DEBUG({
2029 errs() << std::string(Indent-2, ' ') << "=> ";
2030 Result.getNode()->dump(CurDAG);
2031 errs() << '\n';
2032 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002033#endif
Dan Gohman72677342009-08-02 16:10:52 +00002034 }
Evan Chengf597dc72006-02-10 22:24:32 +00002035
2036#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002037 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002038#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002039
Dan Gohman72677342009-08-02 16:10:52 +00002040 return NULL;
2041 }
2042
Dan Gohman6a402dc2009-08-19 18:16:17 +00002043 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002044 SDValue N0 = Node->getOperand(0);
2045 SDValue N1 = Node->getOperand(1);
2046
2047 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2048 // use a smaller encoding.
2049 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2050 N0.getValueType() != MVT::i8 &&
2051 X86::isZeroNode(N1)) {
2052 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2053 if (!C) break;
2054
2055 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002056 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2057 (!(C->getZExtValue() & 0x80) ||
2058 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002059 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2060 SDValue Reg = N0.getNode()->getOperand(0);
2061
2062 // On x86-32, only the ABCD registers have 8-bit subregisters.
2063 if (!Subtarget->is64Bit()) {
2064 TargetRegisterClass *TRC = 0;
2065 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2066 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2067 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2068 default: llvm_unreachable("Unsupported TEST operand type!");
2069 }
2070 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002071 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2072 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002073 }
2074
2075 // Extract the l-register.
2076 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2077 MVT::i8, Reg);
2078
2079 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002080 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002081 }
2082
2083 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002084 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2085 (!(C->getZExtValue() & 0x8000) ||
2086 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002087 // Shift the immediate right by 8 bits.
2088 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2089 MVT::i8);
2090 SDValue Reg = N0.getNode()->getOperand(0);
2091
2092 // Put the value in an ABCD register.
2093 TargetRegisterClass *TRC = 0;
2094 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2095 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2096 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2097 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2098 default: llvm_unreachable("Unsupported TEST operand type!");
2099 }
2100 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002101 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2102 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002103
2104 // Extract the h-register.
2105 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2106 MVT::i8, Reg);
2107
2108 // Emit a testb. No special NOREX tricks are needed since there's
2109 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002110 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2111 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002112 }
2113
2114 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2115 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002116 N0.getValueType() != MVT::i16 &&
2117 (!(C->getZExtValue() & 0x8000) ||
2118 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002119 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2120 SDValue Reg = N0.getNode()->getOperand(0);
2121
2122 // Extract the 16-bit subregister.
2123 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2124 MVT::i16, Reg);
2125
2126 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002127 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002128 }
2129
2130 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2131 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002132 N0.getValueType() == MVT::i64 &&
2133 (!(C->getZExtValue() & 0x80000000) ||
2134 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002135 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2136 SDValue Reg = N0.getNode()->getOperand(0);
2137
2138 // Extract the 32-bit subregister.
2139 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2140 MVT::i32, Reg);
2141
2142 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002143 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002144 }
2145 }
2146 break;
2147 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002148 }
2149
Evan Cheng9ade2182006-08-26 05:34:46 +00002150 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002151
Evan Chengf597dc72006-02-10 22:24:32 +00002152#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002153 DEBUG({
2154 errs() << std::string(Indent-2, ' ') << "=> ";
2155 if (ResNode == NULL || ResNode == N.getNode())
2156 N.getNode()->dump(CurDAG);
2157 else
2158 ResNode->dump(CurDAG);
2159 errs() << '\n';
2160 });
Evan Cheng23addc02006-02-10 22:46:26 +00002161 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002162#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002163
2164 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002165}
2166
Chris Lattnerc0bad572006-06-08 18:03:49 +00002167bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002168SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002169 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002170 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002171 switch (ConstraintCode) {
2172 case 'o': // offsetable ??
2173 case 'v': // not offsetable ??
2174 default: return true;
2175 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002176 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002177 return true;
2178 break;
2179 }
2180
Evan Cheng04699902006-08-26 01:05:16 +00002181 OutOps.push_back(Op0);
2182 OutOps.push_back(Op1);
2183 OutOps.push_back(Op2);
2184 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002185 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002186 return false;
2187}
2188
Chris Lattnerc961eea2005-11-16 01:54:32 +00002189/// createX86ISelDag - This pass converts a legalized DAG into a
2190/// X86-specific DAG, ready for instruction scheduling.
2191///
Bill Wendling98a366d2009-04-29 23:29:43 +00002192FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2193 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002194 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002195}