blob: edf25333d2fb58c1e5db3640f273b4fd820bc8fb [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000039#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000040#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000041#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000042#include "llvm/ADT/Statistic.h"
43using namespace llvm;
44
Evan Cheng4d952322009-03-31 01:13:53 +000045#include "llvm/Support/CommandLine.h"
46static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
47
Chris Lattner95b2c7d2006-12-19 22:59:26 +000048STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
49
Chris Lattnerc961eea2005-11-16 01:54:32 +000050//===----------------------------------------------------------------------===//
51// Pattern Matcher Implementation
52//===----------------------------------------------------------------------===//
53
54namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000055 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000056 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000057 /// tree.
58 struct X86ISelAddressMode {
59 enum {
60 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000061 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 } BaseType;
63
64 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066 int FrameIndex;
67 } Base;
68
69 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000071 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000072 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000075 const char *ES;
76 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000077 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000078 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079
80 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000081 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000082 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000083 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000084
85 bool hasSymbolicDisplacement() const {
86 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
87 }
Chris Lattner18c59872009-06-27 04:16:01 +000088
89 bool hasBaseOrIndexReg() const {
90 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
91 }
92
93 /// isRIPRelative - Return true if this addressing mode is already RIP
94 /// relative.
95 bool isRIPRelative() const {
96 if (BaseType != RegBase) return false;
97 if (RegisterSDNode *RegNode =
98 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
99 return RegNode->getReg() == X86::RIP;
100 return false;
101 }
102
103 void setBaseReg(SDValue Reg) {
104 BaseType = RegBase;
105 Base.Reg = Reg;
106 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000107
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000108 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000109 errs() << "X86ISelAddressMode " << this << '\n';
110 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000111 if (Base.Reg.getNode() != 0)
112 Base.Reg.getNode()->dump();
113 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000114 errs() << "nul";
115 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
116 << " Scale" << Scale << '\n'
117 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000118 if (IndexReg.getNode() != 0)
119 IndexReg.getNode()->dump();
120 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000121 errs() << "nul";
122 errs() << " Disp " << Disp << '\n'
123 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000124 if (GV)
125 GV->dump();
126 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000127 errs() << "nul";
128 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000129 if (CP)
130 CP->dump();
131 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000132 errs() << "nul";
133 errs() << '\n'
134 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000135 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000136 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000137 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000138 errs() << "nul";
139 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000140 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000141 };
142}
143
144namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000145 //===--------------------------------------------------------------------===//
146 /// ISel - X86 specific code to select X86 machine instructions for
147 /// SelectionDAG operations.
148 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000149 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000150 /// X86Lowering - This object fully describes how to lower LLVM code to an
151 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000152 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153
154 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
155 /// make the right decision when generating code for different targets.
156 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000157
Evan Chengb7a75a52008-09-26 23:41:32 +0000158 /// OptForSize - If true, selector should try to optimize for code size
159 /// instead of performance.
160 bool OptForSize;
161
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000163 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000164 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000165 X86Lowering(*tm.getTargetLowering()),
166 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000167 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000168
169 virtual const char *getPassName() const {
170 return "X86 DAG->DAG Instruction Selection";
171 }
172
Evan Chengdb8d56b2008-06-30 20:45:06 +0000173 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000174 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000175 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000176
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000177 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
178
Evan Cheng884c70c2008-11-27 00:49:46 +0000179 virtual
180 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000181
Chris Lattnerc961eea2005-11-16 01:54:32 +0000182// Include the pieces autogenerated from the target description.
183#include "X86GenDAGISel.inc"
184
185 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000187 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000188 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000189
Rafael Espindola094fad32009-04-08 21:14:34 +0000190 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
191 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000192 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000193 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
194 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
195 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000196 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000197 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000198 SDValue &Scale, SDValue &Index, SDValue &Disp,
199 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000200 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
201 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000202 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
203 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
205 SDValue N, SDValue &Base, SDValue &Scale,
206 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000207 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue &InChain, SDValue &OutChain);
209 bool TryFoldLoad(SDValue P, SDValue N,
210 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000211 SDValue &Index, SDValue &Disp,
212 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000213 void PreprocessForRMW();
214 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000215
Chris Lattnerc0bad572006-06-08 18:03:49 +0000216 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
217 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000218 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000219 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000220 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000221
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000222 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
223
Dan Gohman475871a2008-07-27 21:46:04 +0000224 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
225 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000226 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000227 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
229 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000230 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000231 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 // These are 32-bit even in 64-bit mode since RIP relative offset
233 // is 32-bit.
234 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000236 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000237 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000239 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000246
247 if (AM.Segment.getNode())
248 Segment = AM.Segment;
249 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000251 }
252
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000253 /// getI8Imm - Return a target constant with the specified value, of type
254 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000255 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000257 }
258
Chris Lattnerc961eea2005-11-16 01:54:32 +0000259 /// getI16Imm - Return a target constant with the specified value, of type
260 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000263 }
264
265 /// getI32Imm - Return a target constant with the specified value, of type
266 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000269 }
Evan Chengf597dc72006-02-10 22:24:32 +0000270
Dan Gohman8b746962008-09-23 18:22:58 +0000271 /// getGlobalBaseReg - Return an SDNode that returns the value of
272 /// the global base register. Output instructions required to
273 /// initialize the global base register, if necessary.
274 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000275 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000276
Dan Gohmanc5534622009-06-03 20:20:00 +0000277 /// getTargetMachine - Return a reference to the TargetMachine, casted
278 /// to the target-specific type.
279 const X86TargetMachine &getTargetMachine() {
280 return static_cast<const X86TargetMachine &>(TM);
281 }
282
283 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
284 /// to the target-specific type.
285 const X86InstrInfo *getInstrInfo() {
286 return getTargetMachine().getInstrInfo();
287 }
288
Evan Cheng23addc02006-02-10 22:46:26 +0000289#ifndef NDEBUG
290 unsigned Indent;
291#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000292 };
293}
294
Evan Chengf4b4c412006-08-08 00:31:00 +0000295
Evan Cheng884c70c2008-11-27 00:49:46 +0000296bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
297 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000298 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000299
Evan Cheng884c70c2008-11-27 00:49:46 +0000300 if (U == Root)
301 switch (U->getOpcode()) {
302 default: break;
303 case ISD::ADD:
304 case ISD::ADDC:
305 case ISD::ADDE:
306 case ISD::AND:
307 case ISD::OR:
308 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000309 SDValue Op1 = U->getOperand(1);
310
Evan Cheng884c70c2008-11-27 00:49:46 +0000311 // If the other operand is a 8-bit immediate we should fold the immediate
312 // instead. This reduces code size.
313 // e.g.
314 // movl 4(%esp), %eax
315 // addl $4, %eax
316 // vs.
317 // movl $4, %eax
318 // addl 4(%esp), %eax
319 // The former is 2 bytes shorter. In case where the increment is 1, then
320 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000321 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000322 if (Imm->getAPIntValue().isSignedIntN(8))
323 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000324
325 // If the other operand is a TLS address, we should fold it instead.
326 // This produces
327 // movl %gs:0, %eax
328 // leal i@NTPOFF(%eax), %eax
329 // instead of
330 // movl $i@NTPOFF, %eax
331 // addl %gs:0, %eax
332 // if the block also has an access to a second TLS address this will save
333 // a load.
334 // FIXME: This is probably also true for non TLS addresses.
335 if (Op1.getOpcode() == X86ISD::Wrapper) {
336 SDValue Val = Op1.getOperand(0);
337 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
338 return false;
339 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000340 }
341 }
342
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000343 // Proceed to 'generic' cycle finder code
344 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000345}
346
Evan Cheng70e674e2006-08-28 20:10:17 +0000347/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
348/// and move load below the TokenFactor. Replace store's chain operand with
349/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000350static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000351 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000352 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000353 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
354 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000355 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000356 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000357 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000358 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
359 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
360 Load.getOperand(1),
361 Load.getOperand(2));
362 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000363 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000364}
365
Evan Chengcd0baf22008-05-23 21:23:16 +0000366/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
367///
Dan Gohman475871a2008-07-27 21:46:04 +0000368static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
369 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000370 if (N.getOpcode() == ISD::BIT_CONVERT)
371 N = N.getOperand(0);
372
373 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
374 if (!LD || LD->isVolatile())
375 return false;
376 if (LD->getAddressingMode() != ISD::UNINDEXED)
377 return false;
378
379 ISD::LoadExtType ExtType = LD->getExtensionType();
380 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
381 return false;
382
383 if (N.hasOneUse() &&
384 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000385 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000386 Load = N;
387 return true;
388 }
389 return false;
390}
391
Evan Chengab6c3bb2008-08-25 21:27:18 +0000392/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
393/// operand and move load below the call's chain operand.
394static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000395 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000397 SDValue Chain = CallSeqStart.getOperand(0);
398 if (Chain.getNode() == Load.getNode())
399 Ops.push_back(Load.getOperand(0));
400 else {
401 assert(Chain.getOpcode() == ISD::TokenFactor &&
402 "Unexpected CallSeqStart chain operand");
403 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
404 if (Chain.getOperand(i).getNode() == Load.getNode())
405 Ops.push_back(Load.getOperand(0));
406 else
407 Ops.push_back(Chain.getOperand(i));
408 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000409 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000411 Ops.clear();
412 Ops.push_back(NewChain);
413 }
414 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
415 Ops.push_back(CallSeqStart.getOperand(i));
416 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000417 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
418 Load.getOperand(1), Load.getOperand(2));
419 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000420 Ops.push_back(SDValue(Load.getNode(), 1));
421 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000422 Ops.push_back(Call.getOperand(i));
423 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
424}
425
426/// isCalleeLoad - Return true if call address is a load and it can be
427/// moved below CALLSEQ_START and the chains leading up to the call.
428/// Return the CALLSEQ_START by reference as a second output.
429static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000430 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000431 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000432 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000433 if (!LD ||
434 LD->isVolatile() ||
435 LD->getAddressingMode() != ISD::UNINDEXED ||
436 LD->getExtensionType() != ISD::NON_EXTLOAD)
437 return false;
438
439 // Now let's find the callseq_start.
440 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
441 if (!Chain.hasOneUse())
442 return false;
443 Chain = Chain.getOperand(0);
444 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000445
446 if (Chain.getOperand(0).getNode() == Callee.getNode())
447 return true;
448 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
449 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
450 return true;
451 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000452}
453
454
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000455/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000456/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000457/// This allows the instruction selector to pick more read-modify-write
458/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000459///
460/// [Load chain]
461/// ^
462/// |
463/// [Load]
464/// ^ ^
465/// | |
466/// / \-
467/// / |
468/// [TokenFactor] [Op]
469/// ^ ^
470/// | |
471/// \ /
472/// \ /
473/// [Store]
474///
475/// The fact the store's chain operand != load's chain will prevent the
476/// (store (op (load))) instruction from being selected. We can transform it to:
477///
478/// [Load chain]
479/// ^
480/// |
481/// [TokenFactor]
482/// ^
483/// |
484/// [Load]
485/// ^ ^
486/// | |
487/// | \-
488/// | |
489/// | [Op]
490/// | ^
491/// | |
492/// \ /
493/// \ /
494/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000495void X86DAGToDAGISel::PreprocessForRMW() {
496 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
497 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000498 if (I->getOpcode() == X86ISD::CALL) {
499 /// Also try moving call address load from outside callseq_start to just
500 /// before the call to allow it to be folded.
501 ///
502 /// [Load chain]
503 /// ^
504 /// |
505 /// [Load]
506 /// ^ ^
507 /// | |
508 /// / \--
509 /// / |
510 ///[CALLSEQ_START] |
511 /// ^ |
512 /// | |
513 /// [LOAD/C2Reg] |
514 /// | |
515 /// \ /
516 /// \ /
517 /// [CALL]
518 SDValue Chain = I->getOperand(0);
519 SDValue Load = I->getOperand(1);
520 if (!isCalleeLoad(Load, Chain))
521 continue;
522 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
523 ++NumLoadMoved;
524 continue;
525 }
526
Evan Cheng8b2794a2006-10-13 21:14:26 +0000527 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000528 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000529 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000530
Gabor Greifba36cb52008-08-28 21:40:38 +0000531 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000532 continue;
533
Dan Gohman475871a2008-07-27 21:46:04 +0000534 SDValue N1 = I->getOperand(1);
535 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000536 if ((N1.getValueType().isFloatingPoint() &&
537 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000538 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000539 continue;
540
541 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000542 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000543 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000544 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000545 case ISD::ADD:
546 case ISD::MUL:
547 case ISD::AND:
548 case ISD::OR:
549 case ISD::XOR:
550 case ISD::ADDC:
551 case ISD::ADDE:
552 case ISD::VECTOR_SHUFFLE: {
553 SDValue N10 = N1.getOperand(0);
554 SDValue N11 = N1.getOperand(1);
555 RModW = isRMWLoad(N10, Chain, N2, Load);
556 if (!RModW)
557 RModW = isRMWLoad(N11, Chain, N2, Load);
558 break;
559 }
560 case ISD::SUB:
561 case ISD::SHL:
562 case ISD::SRA:
563 case ISD::SRL:
564 case ISD::ROTL:
565 case ISD::ROTR:
566 case ISD::SUBC:
567 case ISD::SUBE:
568 case X86ISD::SHLD:
569 case X86ISD::SHRD: {
570 SDValue N10 = N1.getOperand(0);
571 RModW = isRMWLoad(N10, Chain, N2, Load);
572 break;
573 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000574 }
575
Evan Cheng82a35b32006-08-29 06:44:17 +0000576 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000577 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000578 ++NumLoadMoved;
579 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000580 }
581}
582
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000583
584/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
585/// nodes that target the FP stack to be store and load to the stack. This is a
586/// gross hack. We would like to simply mark these as being illegal, but when
587/// we do that, legalize produces these when it expands calls, then expands
588/// these in the same legalize pass. We would like dag combine to be able to
589/// hack on these between the call expansion and the node legalization. As such
590/// this pass basically does "really late" legalization of these inline with the
591/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000592void X86DAGToDAGISel::PreprocessForFPConvert() {
593 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
594 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000595 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
596 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
597 continue;
598
599 // If the source and destination are SSE registers, then this is a legal
600 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000601 EVT SrcVT = N->getOperand(0).getValueType();
602 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000603 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
604 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
605 if (SrcIsSSE && DstIsSSE)
606 continue;
607
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000608 if (!SrcIsSSE && !DstIsSSE) {
609 // If this is an FPStack extension, it is a noop.
610 if (N->getOpcode() == ISD::FP_EXTEND)
611 continue;
612 // If this is a value-preserving FPStack truncation, it is a noop.
613 if (N->getConstantOperandVal(1))
614 continue;
615 }
616
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000617 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
618 // FPStack has extload and truncstore. SSE can fold direct loads into other
619 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000620 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000621 if (N->getOpcode() == ISD::FP_ROUND)
622 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
623 else
624 MemVT = SrcIsSSE ? SrcVT : DstVT;
625
Dan Gohmanf350b272008-08-23 02:25:05 +0000626 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000627 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000628
629 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000630 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000631 N->getOperand(0),
632 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000633 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000634 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000635
636 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
637 // extload we created. This will cause general havok on the dag because
638 // anything below the conversion could be folded into other existing nodes.
639 // To avoid invalidating 'I', back it up to the convert node.
640 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000641 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000642
643 // Now that we did that, the node is dead. Increment the iterator to the
644 // next node to process, then delete N.
645 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000646 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000647 }
648}
649
Chris Lattnerc961eea2005-11-16 01:54:32 +0000650/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
651/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000652void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000653 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000654 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000655
Evan Chengdb8d56b2008-06-30 20:45:06 +0000656 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000657 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000658 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000659
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000660 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000661 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000662
Chris Lattnerc961eea2005-11-16 01:54:32 +0000663 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000664#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000665 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000666 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000667#endif
David Greene8ad4c002008-10-27 21:56:29 +0000668 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000669#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000670 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000671#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000672
Dan Gohmanf350b272008-08-23 02:25:05 +0000673 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000674}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000675
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000676/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
677/// the main function.
678void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
679 MachineFrameInfo *MFI) {
680 const TargetInstrInfo *TII = TM.getInstrInfo();
681 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000682 BuildMI(BB, DebugLoc::getUnknownLoc(),
683 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000684}
685
686void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
687 // If this is main, emit special code for main.
688 MachineBasicBlock *BB = MF.begin();
689 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
690 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
691}
692
Rafael Espindola094fad32009-04-08 21:14:34 +0000693
694bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
695 X86ISelAddressMode &AM) {
696 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
697 SDValue Segment = N.getOperand(0);
698
699 if (AM.Segment.getNode() == 0) {
700 AM.Segment = Segment;
701 return false;
702 }
703
704 return true;
705}
706
707bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
708 // This optimization is valid because the GNU TLS model defines that
709 // gs:0 (or fs:0 on X86-64) contains its own address.
710 // For more information see http://people.redhat.com/drepper/tls.pdf
711
712 SDValue Address = N.getOperand(1);
713 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
714 !MatchSegmentBaseAddress (Address, AM))
715 return false;
716
717 return true;
718}
719
Chris Lattner18c59872009-06-27 04:16:01 +0000720/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
721/// into an addressing mode. These wrap things that will resolve down into a
722/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000723/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000724bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000725 // If the addressing mode already has a symbol as the displacement, we can
726 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000727 if (AM.hasSymbolicDisplacement())
728 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000729
730 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000731 CodeModel::Model M = TM.getCodeModel();
732
Chris Lattner18c59872009-06-27 04:16:01 +0000733 // Handle X86-64 rip-relative addresses. We check this before checking direct
734 // folding because RIP is preferable to non-RIP accesses.
735 if (Subtarget->is64Bit() &&
736 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
737 // they cannot be folded into immediate fields.
738 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000739 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000740 // Base and index reg must be 0 in order to use %rip as base and lowering
741 // must allow RIP.
742 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000743 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
744 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000745 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000746 AM.GV = G->getGlobal();
747 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000748 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000749 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
750 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000751 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000752 AM.CP = CP->getConstVal();
753 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000754 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000755 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000756 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
757 AM.ES = S->getSymbol();
758 AM.SymbolFlags = S->getTargetFlags();
759 } else {
760 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
761 AM.JT = J->getIndex();
762 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000763 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000764
Chris Lattner18c59872009-06-27 04:16:01 +0000765 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000767 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000768 }
769
770 // Handle the case when globals fit in our immediate field: This is true for
771 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
772 // mode, this results in a non-RIP-relative computation.
773 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000774 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000775 TM.getRelocationModel() == Reloc::Static)) {
776 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
777 AM.GV = G->getGlobal();
778 AM.Disp += G->getOffset();
779 AM.SymbolFlags = G->getTargetFlags();
780 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
781 AM.CP = CP->getConstVal();
782 AM.Align = CP->getAlignment();
783 AM.Disp += CP->getOffset();
784 AM.SymbolFlags = CP->getTargetFlags();
785 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
786 AM.ES = S->getSymbol();
787 AM.SymbolFlags = S->getTargetFlags();
788 } else {
789 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
790 AM.JT = J->getIndex();
791 AM.SymbolFlags = J->getTargetFlags();
792 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000793 return false;
794 }
795
796 return true;
797}
798
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000799/// MatchAddress - Add the specified node to the specified addressing mode,
800/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000801/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000802bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
803 if (MatchAddressRecursively(N, AM, 0))
804 return true;
805
806 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
807 // a smaller encoding and avoids a scaled-index.
808 if (AM.Scale == 2 &&
809 AM.BaseType == X86ISelAddressMode::RegBase &&
810 AM.Base.Reg.getNode() == 0) {
811 AM.Base.Reg = AM.IndexReg;
812 AM.Scale = 1;
813 }
814
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000815 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
816 // because it has a smaller encoding.
817 // TODO: Which other code models can use this?
818 if (TM.getCodeModel() == CodeModel::Small &&
819 Subtarget->is64Bit() &&
820 AM.Scale == 1 &&
821 AM.BaseType == X86ISelAddressMode::RegBase &&
822 AM.Base.Reg.getNode() == 0 &&
823 AM.IndexReg.getNode() == 0 &&
824 AM.SymbolFlags == 0 &&
825 AM.hasSymbolicDisplacement())
826 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
827
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000828 return false;
829}
830
831bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
832 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000833 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000834 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000835 DEBUG({
836 errs() << "MatchAddress: ";
837 AM.dump();
838 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000839 // Limit recursion.
840 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000841 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000842
843 CodeModel::Model M = TM.getCodeModel();
844
Chris Lattner18c59872009-06-27 04:16:01 +0000845 // If this is already a %rip relative address, we can only merge immediates
846 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000847 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000848 if (AM.isRIPRelative()) {
849 // FIXME: JumpTable and ExternalSymbol address currently don't like
850 // displacements. It isn't very important, but this should be fixed for
851 // consistency.
852 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000853
Chris Lattner18c59872009-06-27 04:16:01 +0000854 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
855 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000856 if (X86::isOffsetSuitableForCodeModel(Val, M,
857 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000858 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000859 return false;
860 }
861 }
862 return true;
863 }
864
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865 switch (N.getOpcode()) {
866 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000867 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000868 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000869 if (!is64Bit ||
870 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
871 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000872 AM.Disp += Val;
873 return false;
874 }
875 break;
876 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000877
Rafael Espindola094fad32009-04-08 21:14:34 +0000878 case X86ISD::SegmentBaseAddress:
879 if (!MatchSegmentBaseAddress(N, AM))
880 return false;
881 break;
882
Rafael Espindola49a168d2009-04-12 21:55:03 +0000883 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000884 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000885 if (!MatchWrapper(N, AM))
886 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000887 break;
888
Rafael Espindola094fad32009-04-08 21:14:34 +0000889 case ISD::LOAD:
890 if (!MatchLoad(N, AM))
891 return false;
892 break;
893
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000895 if (AM.BaseType == X86ISelAddressMode::RegBase
896 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000897 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
898 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
899 return false;
900 }
901 break;
Evan Chengec693f72005-12-08 02:01:35 +0000902
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000903 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000904 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000905 break;
906
Gabor Greif93c53e52008-08-31 15:37:04 +0000907 if (ConstantSDNode
908 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000909 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000910 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
911 // that the base operand remains free for further matching. If
912 // the base doesn't end up getting used, a post-processing step
913 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000914 if (Val == 1 || Val == 2 || Val == 3) {
915 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000916 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000917
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000918 // Okay, we know that we have a scale by now. However, if the scaled
919 // value is an add of something and a constant, we can fold the
920 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000921 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
922 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
923 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000924 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000926 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000927 if (!is64Bit ||
928 X86::isOffsetSuitableForCodeModel(Disp, M,
929 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000930 AM.Disp = Disp;
931 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000932 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000933 } else {
934 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000935 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000936 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000937 }
938 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000939 }
Evan Chengec693f72005-12-08 02:01:35 +0000940
Dan Gohman83688052007-10-22 20:22:24 +0000941 case ISD::SMUL_LOHI:
942 case ISD::UMUL_LOHI:
943 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000944 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000945 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000946 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000947 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000948 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000949 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000951 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000952 if (ConstantSDNode
953 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000954 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
955 CN->getZExtValue() == 9) {
956 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000957
Gabor Greifba36cb52008-08-28 21:40:38 +0000958 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000960
961 // Okay, we know that we have a scale by now. However, if the scaled
962 // value is an add of something and a constant, we can fold the
963 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000964 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
965 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
966 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000967 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000968 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000969 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000970 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000971 if (!is64Bit ||
972 X86::isOffsetSuitableForCodeModel(Disp, M,
973 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000974 AM.Disp = Disp;
975 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000976 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000977 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000978 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000979 }
980
981 AM.IndexReg = AM.Base.Reg = Reg;
982 return false;
983 }
Chris Lattner62412262007-02-04 20:18:17 +0000984 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000985 break;
986
Dan Gohman3cd90a12009-05-11 18:02:53 +0000987 case ISD::SUB: {
988 // Given A-B, if A can be completely folded into the address and
989 // the index field with the index field unused, use -B as the index.
990 // This is a win if a has multiple parts that can be folded into
991 // the address. Also, this saves a mov if the base register has
992 // other uses, since it avoids a two-address sub instruction, however
993 // it costs an additional mov if the index register has other uses.
994
995 // Test if the LHS of the sub can be folded.
996 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000997 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000998 AM = Backup;
999 break;
1000 }
1001 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001002 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001003 AM = Backup;
1004 break;
1005 }
1006 int Cost = 0;
1007 SDValue RHS = N.getNode()->getOperand(1);
1008 // If the RHS involves a register with multiple uses, this
1009 // transformation incurs an extra mov, due to the neg instruction
1010 // clobbering its operand.
1011 if (!RHS.getNode()->hasOneUse() ||
1012 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1013 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1014 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1015 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001017 ++Cost;
1018 // If the base is a register with multiple uses, this
1019 // transformation may save a mov.
1020 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1021 AM.Base.Reg.getNode() &&
1022 !AM.Base.Reg.getNode()->hasOneUse()) ||
1023 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1024 --Cost;
1025 // If the folded LHS was interesting, this transformation saves
1026 // address arithmetic.
1027 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1028 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1029 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1030 --Cost;
1031 // If it doesn't look like it may be an overall win, don't do it.
1032 if (Cost >= 0) {
1033 AM = Backup;
1034 break;
1035 }
1036
1037 // Ok, the transformation is legal and appears profitable. Go for it.
1038 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1039 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1040 AM.IndexReg = Neg;
1041 AM.Scale = 1;
1042
1043 // Insert the new nodes into the topological ordering.
1044 if (Zero.getNode()->getNodeId() == -1 ||
1045 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1046 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1047 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1048 }
1049 if (Neg.getNode()->getNodeId() == -1 ||
1050 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1051 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1052 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1053 }
1054 return false;
1055 }
1056
Evan Cheng8e278262009-01-17 07:09:27 +00001057 case ISD::ADD: {
1058 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001059 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1060 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001061 return false;
1062 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001063 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1064 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001065 return false;
1066 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001067
1068 // If we couldn't fold both operands into the address at the same time,
1069 // see if we can just put each operand into a register and fold at least
1070 // the add.
1071 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1072 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001073 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001074 AM.Base.Reg = N.getNode()->getOperand(0);
1075 AM.IndexReg = N.getNode()->getOperand(1);
1076 AM.Scale = 1;
1077 return false;
1078 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001079 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001080 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001081
Chris Lattner62412262007-02-04 20:18:17 +00001082 case ISD::OR:
1083 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001084 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1085 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001086 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001087 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001088 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001089 // Address could not have picked a GV address for the displacement.
1090 AM.GV == NULL &&
1091 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001092 (!is64Bit ||
1093 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1094 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001095 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001096 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001097 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001098 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001099 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001100 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001101 }
1102 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001103
1104 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001105 // Perform some heroic transforms on an and of a constant-count shift
1106 // with a constant to enable use of the scaled offset field.
1107
Dan Gohman475871a2008-07-27 21:46:04 +00001108 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001109 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001110
Evan Cheng1314b002007-12-13 00:43:27 +00001111 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001112 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001113
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001114 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001115 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1116 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1117 if (!C1 || !C2) break;
1118
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001119 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1120 // allows us to convert the shift and and into an h-register extract and
1121 // a scaled index.
1122 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1123 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001124 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001125 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001127 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1128 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1129 X, Eight);
1130 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1131 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001133 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1134 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001135
1136 // Insert the new nodes into the topological ordering.
1137 if (Eight.getNode()->getNodeId() == -1 ||
1138 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1139 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1140 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1141 }
1142 if (Mask.getNode()->getNodeId() == -1 ||
1143 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1144 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1145 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1146 }
1147 if (Srl.getNode()->getNodeId() == -1 ||
1148 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1149 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1150 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1151 }
1152 if (And.getNode()->getNodeId() == -1 ||
1153 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1154 CurDAG->RepositionNode(N.getNode(), And.getNode());
1155 And.getNode()->setNodeId(N.getNode()->getNodeId());
1156 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001157 if (ShlCount.getNode()->getNodeId() == -1 ||
1158 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1159 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1160 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1161 }
1162 if (Shl.getNode()->getNodeId() == -1 ||
1163 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1164 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1165 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1166 }
1167 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001168 AM.IndexReg = And;
1169 AM.Scale = (1 << ScaleLog);
1170 return false;
1171 }
1172 }
1173
1174 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1175 // allows us to fold the shift into this addressing mode.
1176 if (Shift.getOpcode() != ISD::SHL) break;
1177
Evan Cheng1314b002007-12-13 00:43:27 +00001178 // Not likely to be profitable if either the AND or SHIFT node has more
1179 // than one use (unless all uses are for address computation). Besides,
1180 // isel mechanism requires their node ids to be reused.
1181 if (!N.hasOneUse() || !Shift.hasOneUse())
1182 break;
1183
1184 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001185 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001186 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1187 break;
1188
1189 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001190 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001191 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001192 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1193 NewANDMask);
1194 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001195 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001196
1197 // Insert the new nodes into the topological ordering.
1198 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1199 CurDAG->RepositionNode(X.getNode(), C1);
1200 C1->setNodeId(X.getNode()->getNodeId());
1201 }
1202 if (NewANDMask.getNode()->getNodeId() == -1 ||
1203 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1204 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1205 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1206 }
1207 if (NewAND.getNode()->getNodeId() == -1 ||
1208 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1209 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1210 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1211 }
1212 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1213 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1214 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1215 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1216 }
1217
Dan Gohman7b8e9642008-10-13 20:52:04 +00001218 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001219
1220 AM.Scale = 1 << ShiftCst;
1221 AM.IndexReg = NewAND;
1222 return false;
1223 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001224 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001225
Rafael Espindola523249f2009-03-31 16:16:57 +00001226 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001227}
1228
1229/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1230/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001231bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001232 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001234 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001235 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001236 AM.IndexReg = N;
1237 AM.Scale = 1;
1238 return false;
1239 }
1240
1241 // Otherwise, we cannot select it.
1242 return true;
1243 }
1244
1245 // Default, generate it as a register.
1246 AM.BaseType = X86ISelAddressMode::RegBase;
1247 AM.Base.Reg = N;
1248 return false;
1249}
1250
Evan Chengec693f72005-12-08 02:01:35 +00001251/// SelectAddr - returns true if it is able pattern match an addressing mode.
1252/// It returns the operands which make up the maximal addressing mode it can
1253/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001254bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1255 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001256 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001257 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001258 bool Done = false;
1259 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1260 unsigned Opcode = N.getOpcode();
1261 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001262 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001263 // If we are able to fold N into addressing mode, then we'll allow it even
1264 // if N has multiple uses. In general, addressing computation is used as
1265 // addresses by all of its uses. But watch out for CopyToReg uses, that
1266 // means the address computation is liveout. It will be computed by a LEA
1267 // so we want to avoid computing the address twice.
1268 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1269 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1270 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001271 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001272 Done = true;
1273 break;
1274 }
1275 }
1276 }
1277 }
1278
1279 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001280 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001281
Owen Andersone50ed302009-08-10 22:56:29 +00001282 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001283 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001285 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001286 }
Evan Cheng8700e142006-01-11 06:09:51 +00001287
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001289 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001290
Rafael Espindola094fad32009-04-08 21:14:34 +00001291 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001292 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001293}
1294
Chris Lattner3a7cd952006-10-07 21:55:32 +00001295/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1296/// match a load whose top elements are either undef or zeros. The load flavor
1297/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001298bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1299 SDValue N, SDValue &Base,
1300 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001301 SDValue &Disp, SDValue &Segment,
1302 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001303 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001304 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001305 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001306 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001307 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001308 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001309 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001310 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001311 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001312 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001313 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001314 return true;
1315 }
1316 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001317
1318 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001319 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001320 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001321 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001322 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 N.getOperand(0).getNode()->hasOneUse() &&
1324 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001325 N.getOperand(0).getOperand(0).hasOneUse()) {
1326 // Okay, this is a zero extending load. Fold it.
1327 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001328 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001329 return false;
1330 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001331 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001332 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001333 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001334 return false;
1335}
1336
1337
Evan Cheng51a9ed92006-02-25 10:09:08 +00001338/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1339/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001340bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1341 SDValue &Base, SDValue &Scale,
1342 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001343 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001344
1345 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1346 // segments.
1347 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001349 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001350 if (MatchAddress(N, AM))
1351 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001352 assert (T == AM.Segment);
1353 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001354
Owen Andersone50ed302009-08-10 22:56:29 +00001355 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001356 unsigned Complexity = 0;
1357 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001358 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001359 Complexity = 1;
1360 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001361 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001362 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1363 Complexity = 4;
1364
Gabor Greifba36cb52008-08-28 21:40:38 +00001365 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001366 Complexity++;
1367 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001368 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001369
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001370 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1371 // a simple shift.
1372 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001373 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001374
1375 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1376 // to a LEA. This is determined with some expermentation but is by no means
1377 // optimal (especially for code size consideration). LEA is nice because of
1378 // its three-address nature. Tweak the cost function again when we can run
1379 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001380 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001381 // For X86-64, we should always use lea to materialize RIP relative
1382 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001383 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001384 Complexity = 4;
1385 else
1386 Complexity += 2;
1387 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001388
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001390 Complexity++;
1391
Chris Lattner25142782009-07-11 22:50:33 +00001392 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001393 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001394 return false;
1395
1396 SDValue Segment;
1397 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1398 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001399}
1400
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001401/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1402bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1403 SDValue &Scale, SDValue &Index,
1404 SDValue &Disp) {
1405 assert(Op.getOpcode() == X86ISD::TLSADDR);
1406 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1407 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1408
1409 X86ISelAddressMode AM;
1410 AM.GV = GA->getGlobal();
1411 AM.Disp += GA->getOffset();
1412 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001413 AM.SymbolFlags = GA->getTargetFlags();
1414
Owen Anderson825b72b2009-08-11 20:47:22 +00001415 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001416 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001418 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001420 }
1421
1422 SDValue Segment;
1423 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1424 return true;
1425}
1426
1427
Dan Gohman475871a2008-07-27 21:46:04 +00001428bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1429 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001430 SDValue &Index, SDValue &Disp,
1431 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001433 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001434 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001435 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001436 return false;
1437}
1438
Dan Gohman8b746962008-09-23 18:22:58 +00001439/// getGlobalBaseReg - Return an SDNode that returns the value of
1440/// the global base register. Output instructions required to
1441/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001442///
Evan Cheng9ade2182006-08-26 05:34:46 +00001443SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001444 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001445 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001446}
1447
Evan Chengb245d922006-05-20 01:36:52 +00001448static SDNode *FindCallStartFromCall(SDNode *Node) {
1449 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001451 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001452 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001453}
1454
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001455SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1456 SDValue Chain = Node->getOperand(0);
1457 SDValue In1 = Node->getOperand(1);
1458 SDValue In2L = Node->getOperand(2);
1459 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001460 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1461 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001462 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001463 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001464 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001465 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001467 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001468}
Christopher Lambc59e5212007-08-10 21:48:46 +00001469
Owen Andersone50ed302009-08-10 22:56:29 +00001470SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001471 if (Node->hasAnyUseOfValue(0))
1472 return 0;
1473
1474 // Optimize common patterns for __sync_add_and_fetch and
1475 // __sync_sub_and_fetch where the result is not used. This allows us
1476 // to use "lock" version of add, sub, inc, dec instructions.
1477 // FIXME: Do not use special instructions but instead add the "lock"
1478 // prefix to the target node somehow. The extra information will then be
1479 // transferred to machine instruction and it denotes the prefix.
1480 SDValue Chain = Node->getOperand(0);
1481 SDValue Ptr = Node->getOperand(1);
1482 SDValue Val = Node->getOperand(2);
1483 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1484 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1485 return 0;
1486
1487 bool isInc = false, isDec = false, isSub = false, isCN = false;
1488 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1489 if (CN) {
1490 isCN = true;
1491 int64_t CNVal = CN->getSExtValue();
1492 if (CNVal == 1)
1493 isInc = true;
1494 else if (CNVal == -1)
1495 isDec = true;
1496 else if (CNVal >= 0)
1497 Val = CurDAG->getTargetConstant(CNVal, NVT);
1498 else {
1499 isSub = true;
1500 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1501 }
1502 } else if (Val.hasOneUse() &&
1503 Val.getOpcode() == ISD::SUB &&
1504 X86::isZeroNode(Val.getOperand(0))) {
1505 isSub = true;
1506 Val = Val.getOperand(1);
1507 }
1508
1509 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001511 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001513 if (isInc)
1514 Opc = X86::LOCK_INC8m;
1515 else if (isDec)
1516 Opc = X86::LOCK_DEC8m;
1517 else if (isSub) {
1518 if (isCN)
1519 Opc = X86::LOCK_SUB8mi;
1520 else
1521 Opc = X86::LOCK_SUB8mr;
1522 } else {
1523 if (isCN)
1524 Opc = X86::LOCK_ADD8mi;
1525 else
1526 Opc = X86::LOCK_ADD8mr;
1527 }
1528 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001530 if (isInc)
1531 Opc = X86::LOCK_INC16m;
1532 else if (isDec)
1533 Opc = X86::LOCK_DEC16m;
1534 else if (isSub) {
1535 if (isCN) {
1536 if (Predicate_i16immSExt8(Val.getNode()))
1537 Opc = X86::LOCK_SUB16mi8;
1538 else
1539 Opc = X86::LOCK_SUB16mi;
1540 } else
1541 Opc = X86::LOCK_SUB16mr;
1542 } else {
1543 if (isCN) {
1544 if (Predicate_i16immSExt8(Val.getNode()))
1545 Opc = X86::LOCK_ADD16mi8;
1546 else
1547 Opc = X86::LOCK_ADD16mi;
1548 } else
1549 Opc = X86::LOCK_ADD16mr;
1550 }
1551 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001553 if (isInc)
1554 Opc = X86::LOCK_INC32m;
1555 else if (isDec)
1556 Opc = X86::LOCK_DEC32m;
1557 else if (isSub) {
1558 if (isCN) {
1559 if (Predicate_i32immSExt8(Val.getNode()))
1560 Opc = X86::LOCK_SUB32mi8;
1561 else
1562 Opc = X86::LOCK_SUB32mi;
1563 } else
1564 Opc = X86::LOCK_SUB32mr;
1565 } else {
1566 if (isCN) {
1567 if (Predicate_i32immSExt8(Val.getNode()))
1568 Opc = X86::LOCK_ADD32mi8;
1569 else
1570 Opc = X86::LOCK_ADD32mi;
1571 } else
1572 Opc = X86::LOCK_ADD32mr;
1573 }
1574 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001576 if (isInc)
1577 Opc = X86::LOCK_INC64m;
1578 else if (isDec)
1579 Opc = X86::LOCK_DEC64m;
1580 else if (isSub) {
1581 Opc = X86::LOCK_SUB64mr;
1582 if (isCN) {
1583 if (Predicate_i64immSExt8(Val.getNode()))
1584 Opc = X86::LOCK_SUB64mi8;
1585 else if (Predicate_i64immSExt32(Val.getNode()))
1586 Opc = X86::LOCK_SUB64mi32;
1587 }
1588 } else {
1589 Opc = X86::LOCK_ADD64mr;
1590 if (isCN) {
1591 if (Predicate_i64immSExt8(Val.getNode()))
1592 Opc = X86::LOCK_ADD64mi8;
1593 else if (Predicate_i64immSExt32(Val.getNode()))
1594 Opc = X86::LOCK_ADD64mi32;
1595 }
1596 }
1597 break;
1598 }
1599
1600 DebugLoc dl = Node->getDebugLoc();
1601 SDValue Undef = SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1602 dl, NVT), 0);
1603 SDValue MemOp = CurDAG->getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
1604 if (isInc || isDec) {
1605 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, MemOp, Chain };
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 7), 0);
Evan Cheng37b73872009-07-30 08:33:02 +00001607 SDValue RetVals[] = { Undef, Ret };
1608 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1609 } else {
1610 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, MemOp, Chain };
Owen Anderson825b72b2009-08-11 20:47:22 +00001611 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 8), 0);
Evan Cheng37b73872009-07-30 08:33:02 +00001612 SDValue RetVals[] = { Undef, Ret };
1613 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1614 }
1615}
1616
Dan Gohman475871a2008-07-27 21:46:04 +00001617SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001619 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001620 unsigned Opc, MOpc;
1621 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001622 DebugLoc dl = Node->getDebugLoc();
1623
Evan Chengf597dc72006-02-10 22:24:32 +00001624#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001625 DEBUG({
1626 errs() << std::string(Indent, ' ') << "Selecting: ";
1627 Node->dump(CurDAG);
1628 errs() << '\n';
1629 });
Evan Cheng23addc02006-02-10 22:46:26 +00001630 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001631#endif
1632
Dan Gohmane8be6c62008-07-17 19:10:17 +00001633 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001634#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001635 DEBUG({
1636 errs() << std::string(Indent-2, ' ') << "== ";
1637 Node->dump(CurDAG);
1638 errs() << '\n';
1639 });
Evan Cheng23addc02006-02-10 22:46:26 +00001640 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001641#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001642 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001643 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001644
Evan Cheng0114e942006-01-06 20:36:21 +00001645 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001646 default: break;
1647 case X86ISD::GlobalBaseReg:
1648 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001649
Dan Gohman72677342009-08-02 16:10:52 +00001650 case X86ISD::ATOMOR64_DAG:
1651 return SelectAtomic64(Node, X86::ATOMOR6432);
1652 case X86ISD::ATOMXOR64_DAG:
1653 return SelectAtomic64(Node, X86::ATOMXOR6432);
1654 case X86ISD::ATOMADD64_DAG:
1655 return SelectAtomic64(Node, X86::ATOMADD6432);
1656 case X86ISD::ATOMSUB64_DAG:
1657 return SelectAtomic64(Node, X86::ATOMSUB6432);
1658 case X86ISD::ATOMNAND64_DAG:
1659 return SelectAtomic64(Node, X86::ATOMNAND6432);
1660 case X86ISD::ATOMAND64_DAG:
1661 return SelectAtomic64(Node, X86::ATOMAND6432);
1662 case X86ISD::ATOMSWAP64_DAG:
1663 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001664
Dan Gohman72677342009-08-02 16:10:52 +00001665 case ISD::ATOMIC_LOAD_ADD: {
1666 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1667 if (RetVal)
1668 return RetVal;
1669 break;
1670 }
1671
1672 case ISD::SMUL_LOHI:
1673 case ISD::UMUL_LOHI: {
1674 SDValue N0 = Node->getOperand(0);
1675 SDValue N1 = Node->getOperand(1);
1676
1677 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001678 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001679 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001680 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001681 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1682 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1683 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1684 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001685 }
Bill Wendling12321672009-08-07 21:33:25 +00001686 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001688 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1690 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1691 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1692 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001693 }
Bill Wendling12321672009-08-07 21:33:25 +00001694 }
Dan Gohman72677342009-08-02 16:10:52 +00001695
1696 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001698 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1700 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1701 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1702 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001703 }
1704
1705 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1706 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001707 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001708 if (!foldedLoad) {
1709 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1710 if (foldedLoad)
1711 std::swap(N0, N1);
1712 }
1713
1714 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1715 N0, SDValue()).getValue(1);
1716
1717 if (foldedLoad) {
1718 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1719 InFlag };
1720 SDNode *CNode =
Owen Anderson825b72b2009-08-11 20:47:22 +00001721 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001722 array_lengthof(Ops));
1723 InFlag = SDValue(CNode, 1);
1724 // Update the chain.
1725 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1726 } else {
1727 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001729 }
1730
1731 // Copy the low half of the result, if it is needed.
1732 if (!N.getValue(0).use_empty()) {
1733 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1734 LoReg, NVT, InFlag);
1735 InFlag = Result.getValue(2);
1736 ReplaceUses(N.getValue(0), Result);
1737#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001738 DEBUG({
1739 errs() << std::string(Indent-2, ' ') << "=> ";
1740 Result.getNode()->dump(CurDAG);
1741 errs() << '\n';
1742 });
Dan Gohman72677342009-08-02 16:10:52 +00001743#endif
1744 }
1745 // Copy the high half of the result, if it is needed.
1746 if (!N.getValue(1).use_empty()) {
1747 SDValue Result;
1748 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1749 // Prevent use of AH in a REX instruction by referencing AX instead.
1750 // Shift it down 8 bits.
1751 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001753 InFlag = Result.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001755 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001757 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001758 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1759 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001760 } else {
1761 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1762 HiReg, NVT, InFlag);
1763 InFlag = Result.getValue(2);
1764 }
1765 ReplaceUses(N.getValue(1), Result);
1766#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001767 DEBUG({
1768 errs() << std::string(Indent-2, ' ') << "=> ";
1769 Result.getNode()->dump(CurDAG);
1770 errs() << '\n';
1771 });
Dan Gohman72677342009-08-02 16:10:52 +00001772#endif
1773 }
1774
1775#ifndef NDEBUG
1776 Indent -= 2;
1777#endif
1778
1779 return NULL;
1780 }
1781
1782 case ISD::SDIVREM:
1783 case ISD::UDIVREM: {
1784 SDValue N0 = Node->getOperand(0);
1785 SDValue N1 = Node->getOperand(1);
1786
1787 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001788 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001790 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1792 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1793 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1794 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001795 }
Bill Wendling12321672009-08-07 21:33:25 +00001796 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001798 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1800 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1801 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1802 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001803 }
Bill Wendling12321672009-08-07 21:33:25 +00001804 }
Dan Gohman72677342009-08-02 16:10:52 +00001805
1806 unsigned LoReg, HiReg;
1807 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001809 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001811 LoReg = X86::AL; HiReg = X86::AH;
1812 ClrOpcode = 0;
1813 SExtOpcode = X86::CBW;
1814 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001816 LoReg = X86::AX; HiReg = X86::DX;
1817 ClrOpcode = X86::MOV16r0;
1818 SExtOpcode = X86::CWD;
1819 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001821 LoReg = X86::EAX; HiReg = X86::EDX;
1822 ClrOpcode = X86::MOV32r0;
1823 SExtOpcode = X86::CDQ;
1824 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001826 LoReg = X86::RAX; HiReg = X86::RDX;
1827 ClrOpcode = ~0U; // NOT USED.
1828 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001829 break;
1830 }
1831
Dan Gohman72677342009-08-02 16:10:52 +00001832 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1833 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1834 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001835
Dan Gohman72677342009-08-02 16:10:52 +00001836 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001838 // Special case for div8, just use a move with zero extension to AX to
1839 // clear the upper 8 bits (AH).
1840 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1841 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1842 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1843 Move =
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
1845 MVT::Other, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001846 array_lengthof(Ops)), 0);
1847 Chain = Move.getValue(1);
1848 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001849 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001850 Move =
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001852 Chain = CurDAG->getEntryNode();
1853 }
1854 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1855 InFlag = Chain.getValue(1);
1856 } else {
1857 InFlag =
1858 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1859 LoReg, N0, SDValue()).getValue(1);
1860 if (isSigned && !signBitIsZero) {
1861 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001862 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001863 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001864 } else {
1865 // Zero out the high part, effectively zero extending the input.
1866 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001867
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 if (NVT.getSimpleVT() == MVT::i64) {
1869 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001870 0);
1871 // We just did a 32-bit clear, insert it into a 64-bit register to
1872 // clear the whole 64-bit reg.
1873 SDValue Undef =
1874 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 dl, MVT::i64), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001876 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001878 ClrNode =
1879 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 MVT::i64, Undef, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001881 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001882 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001883 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001884 }
Dan Gohman72677342009-08-02 16:10:52 +00001885
1886 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1887 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001888 }
Evan Cheng948f3432006-01-06 23:19:29 +00001889 }
Dan Gohman525178c2007-10-08 18:33:35 +00001890
Dan Gohman72677342009-08-02 16:10:52 +00001891 if (foldedLoad) {
1892 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1893 InFlag };
1894 SDNode *CNode =
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001896 array_lengthof(Ops));
1897 InFlag = SDValue(CNode, 1);
1898 // Update the chain.
1899 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1900 } else {
1901 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001903 }
Evan Cheng948f3432006-01-06 23:19:29 +00001904
Dan Gohman72677342009-08-02 16:10:52 +00001905 // Copy the division (low) result, if it is needed.
1906 if (!N.getValue(0).use_empty()) {
1907 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1908 LoReg, NVT, InFlag);
1909 InFlag = Result.getValue(2);
1910 ReplaceUses(N.getValue(0), Result);
1911#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001912 DEBUG({
1913 errs() << std::string(Indent-2, ' ') << "=> ";
1914 Result.getNode()->dump(CurDAG);
1915 errs() << '\n';
1916 });
Dan Gohman72677342009-08-02 16:10:52 +00001917#endif
1918 }
1919 // Copy the remainder (high) result, if it is needed.
1920 if (!N.getValue(1).use_empty()) {
1921 SDValue Result;
1922 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1923 // Prevent use of AH in a REX instruction by referencing AX instead.
1924 // Shift it down 8 bits.
1925 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001927 InFlag = Result.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001929 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001931 0);
1932 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001933 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1934 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001935 } else {
1936 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1937 HiReg, NVT, InFlag);
1938 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001939 }
Dan Gohman72677342009-08-02 16:10:52 +00001940 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001941#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001942 DEBUG({
1943 errs() << std::string(Indent-2, ' ') << "=> ";
1944 Result.getNode()->dump(CurDAG);
1945 errs() << '\n';
1946 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00001947#endif
Dan Gohman72677342009-08-02 16:10:52 +00001948 }
Evan Chengf597dc72006-02-10 22:24:32 +00001949
1950#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00001951 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001952#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001953
Dan Gohman72677342009-08-02 16:10:52 +00001954 return NULL;
1955 }
1956
Dan Gohman6a402dc2009-08-19 18:16:17 +00001957 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001958 SDValue N0 = Node->getOperand(0);
1959 SDValue N1 = Node->getOperand(1);
1960
1961 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1962 // use a smaller encoding.
1963 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1964 N0.getValueType() != MVT::i8 &&
1965 X86::isZeroNode(N1)) {
1966 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1967 if (!C) break;
1968
1969 // For example, convert "testl %eax, $8" to "testb %al, $8"
1970 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0) {
1971 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1972 SDValue Reg = N0.getNode()->getOperand(0);
1973
1974 // On x86-32, only the ABCD registers have 8-bit subregisters.
1975 if (!Subtarget->is64Bit()) {
1976 TargetRegisterClass *TRC = 0;
1977 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1978 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1979 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1980 default: llvm_unreachable("Unsupported TEST operand type!");
1981 }
1982 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
1983 Reg = SDValue(CurDAG->getTargetNode(X86::COPY_TO_REGCLASS, dl,
1984 Reg.getValueType(), Reg, RC), 0);
1985 }
1986
1987 // Extract the l-register.
1988 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1989 MVT::i8, Reg);
1990
1991 // Emit a testb.
1992 return CurDAG->getTargetNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
1993 }
1994
1995 // For example, "testl %eax, $2048" to "testb %ah, $8".
1996 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0) {
1997 // Shift the immediate right by 8 bits.
1998 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1999 MVT::i8);
2000 SDValue Reg = N0.getNode()->getOperand(0);
2001
2002 // Put the value in an ABCD register.
2003 TargetRegisterClass *TRC = 0;
2004 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2005 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2006 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2007 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2008 default: llvm_unreachable("Unsupported TEST operand type!");
2009 }
2010 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
2011 Reg = SDValue(CurDAG->getTargetNode(X86::COPY_TO_REGCLASS, dl,
2012 Reg.getValueType(), Reg, RC), 0);
2013
2014 // Extract the h-register.
2015 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2016 MVT::i8, Reg);
2017
2018 // Emit a testb. No special NOREX tricks are needed since there's
2019 // only one GPR operand!
2020 return CurDAG->getTargetNode(X86::TEST8ri, dl, MVT::i32,
2021 Subreg, ShiftedImm);
2022 }
2023
2024 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2025 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
2026 N0.getValueType() != MVT::i16) {
2027 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2028 SDValue Reg = N0.getNode()->getOperand(0);
2029
2030 // Extract the 16-bit subregister.
2031 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2032 MVT::i16, Reg);
2033
2034 // Emit a testw.
2035 return CurDAG->getTargetNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
2036 }
2037
2038 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2039 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
2040 N0.getValueType() == MVT::i64) {
2041 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2042 SDValue Reg = N0.getNode()->getOperand(0);
2043
2044 // Extract the 32-bit subregister.
2045 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2046 MVT::i32, Reg);
2047
2048 // Emit a testl.
2049 return CurDAG->getTargetNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
2050 }
2051 }
2052 break;
2053 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002054 }
2055
Evan Cheng9ade2182006-08-26 05:34:46 +00002056 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002057
Evan Chengf597dc72006-02-10 22:24:32 +00002058#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002059 DEBUG({
2060 errs() << std::string(Indent-2, ' ') << "=> ";
2061 if (ResNode == NULL || ResNode == N.getNode())
2062 N.getNode()->dump(CurDAG);
2063 else
2064 ResNode->dump(CurDAG);
2065 errs() << '\n';
2066 });
Evan Cheng23addc02006-02-10 22:46:26 +00002067 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002068#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002069
2070 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002071}
2072
Chris Lattnerc0bad572006-06-08 18:03:49 +00002073bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002074SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002075 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002076 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002077 switch (ConstraintCode) {
2078 case 'o': // offsetable ??
2079 case 'v': // not offsetable ??
2080 default: return true;
2081 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002082 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002083 return true;
2084 break;
2085 }
2086
Evan Cheng04699902006-08-26 01:05:16 +00002087 OutOps.push_back(Op0);
2088 OutOps.push_back(Op1);
2089 OutOps.push_back(Op2);
2090 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002091 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002092 return false;
2093}
2094
Chris Lattnerc961eea2005-11-16 01:54:32 +00002095/// createX86ISelDag - This pass converts a legalized DAG into a
2096/// X86-specific DAG, ready for instruction scheduling.
2097///
Bill Wendling98a366d2009-04-29 23:29:43 +00002098FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2099 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002100 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002101}