blob: a900a69ba71e29d65a14ce03a6d4065e25455de4 [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),
Dan Gohman79b765d2009-08-25 17:47:44 +000082 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0),
83 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
87 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
88 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000110 errs() << "X86ISelAddressMode " << this << '\n';
111 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000112 if (Base.Reg.getNode() != 0)
113 Base.Reg.getNode()->dump();
114 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000115 errs() << "nul";
116 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
117 << " Scale" << Scale << '\n'
118 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000119 if (IndexReg.getNode() != 0)
120 IndexReg.getNode()->dump();
121 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000122 errs() << "nul";
123 errs() << " Disp " << Disp << '\n'
124 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000125 if (GV)
126 GV->dump();
127 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000128 errs() << "nul";
129 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000130 if (CP)
131 CP->dump();
132 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000133 errs() << "nul";
134 errs() << '\n'
135 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000136 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000137 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000138 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000139 errs() << "nul";
140 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000141 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000142 };
143}
144
145namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000146 //===--------------------------------------------------------------------===//
147 /// ISel - X86 specific code to select X86 machine instructions for
148 /// SelectionDAG operations.
149 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000150 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151 /// X86Lowering - This object fully describes how to lower LLVM code to an
152 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000153 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000154
155 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
156 /// make the right decision when generating code for different targets.
157 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000158
Evan Chengb7a75a52008-09-26 23:41:32 +0000159 /// OptForSize - If true, selector should try to optimize for code size
160 /// instead of performance.
161 bool OptForSize;
162
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000164 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000165 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000166 X86Lowering(*tm.getTargetLowering()),
167 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000168 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000169
170 virtual const char *getPassName() const {
171 return "X86 DAG->DAG Instruction Selection";
172 }
173
Evan Chengdb8d56b2008-06-30 20:45:06 +0000174 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000175 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000176 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000177
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000178 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
179
Evan Cheng884c70c2008-11-27 00:49:46 +0000180 virtual
181 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000182
Chris Lattnerc961eea2005-11-16 01:54:32 +0000183// Include the pieces autogenerated from the target description.
184#include "X86GenDAGISel.inc"
185
186 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000188 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000189 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000190
Rafael Espindola094fad32009-04-08 21:14:34 +0000191 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
192 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000193 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000194 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
195 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
196 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000197 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000198 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000199 SDValue &Scale, SDValue &Index, SDValue &Disp,
200 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000201 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
202 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000203 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
204 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
206 SDValue N, SDValue &Base, SDValue &Scale,
207 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000208 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue &InChain, SDValue &OutChain);
210 bool TryFoldLoad(SDValue P, SDValue N,
211 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000212 SDValue &Index, SDValue &Disp,
213 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000214 void PreprocessForRMW();
215 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000216
Chris Lattnerc0bad572006-06-08 18:03:49 +0000217 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
218 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000219 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000220 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000221 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000222
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000223 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
224
Dan Gohman475871a2008-07-27 21:46:04 +0000225 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
226 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000227 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000228 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
230 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000231 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000232 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 // These are 32-bit even in 64-bit mode since RIP relative offset
234 // is 32-bit.
235 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000237 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000240 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000241 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000247
248 if (AM.Segment.getNode())
249 Segment = AM.Segment;
250 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000252 }
253
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000254 /// getI8Imm - Return a target constant with the specified value, of type
255 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000256 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000258 }
259
Chris Lattnerc961eea2005-11-16 01:54:32 +0000260 /// getI16Imm - Return a target constant with the specified value, of type
261 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000262 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000264 }
265
266 /// getI32Imm - Return a target constant with the specified value, of type
267 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000268 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000270 }
Evan Chengf597dc72006-02-10 22:24:32 +0000271
Dan Gohman8b746962008-09-23 18:22:58 +0000272 /// getGlobalBaseReg - Return an SDNode that returns the value of
273 /// the global base register. Output instructions required to
274 /// initialize the global base register, if necessary.
275 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000276 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000277
Dan Gohmanc5534622009-06-03 20:20:00 +0000278 /// getTargetMachine - Return a reference to the TargetMachine, casted
279 /// to the target-specific type.
280 const X86TargetMachine &getTargetMachine() {
281 return static_cast<const X86TargetMachine &>(TM);
282 }
283
284 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
285 /// to the target-specific type.
286 const X86InstrInfo *getInstrInfo() {
287 return getTargetMachine().getInstrInfo();
288 }
289
Evan Cheng23addc02006-02-10 22:46:26 +0000290#ifndef NDEBUG
291 unsigned Indent;
292#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000293 };
294}
295
Evan Chengf4b4c412006-08-08 00:31:00 +0000296
Evan Cheng884c70c2008-11-27 00:49:46 +0000297bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
298 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000299 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000300
Evan Cheng884c70c2008-11-27 00:49:46 +0000301 if (U == Root)
302 switch (U->getOpcode()) {
303 default: break;
304 case ISD::ADD:
305 case ISD::ADDC:
306 case ISD::ADDE:
307 case ISD::AND:
308 case ISD::OR:
309 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000310 SDValue Op1 = U->getOperand(1);
311
Evan Cheng884c70c2008-11-27 00:49:46 +0000312 // If the other operand is a 8-bit immediate we should fold the immediate
313 // instead. This reduces code size.
314 // e.g.
315 // movl 4(%esp), %eax
316 // addl $4, %eax
317 // vs.
318 // movl $4, %eax
319 // addl 4(%esp), %eax
320 // The former is 2 bytes shorter. In case where the increment is 1, then
321 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000322 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000323 if (Imm->getAPIntValue().isSignedIntN(8))
324 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000325
326 // If the other operand is a TLS address, we should fold it instead.
327 // This produces
328 // movl %gs:0, %eax
329 // leal i@NTPOFF(%eax), %eax
330 // instead of
331 // movl $i@NTPOFF, %eax
332 // addl %gs:0, %eax
333 // if the block also has an access to a second TLS address this will save
334 // a load.
335 // FIXME: This is probably also true for non TLS addresses.
336 if (Op1.getOpcode() == X86ISD::Wrapper) {
337 SDValue Val = Op1.getOperand(0);
338 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
339 return false;
340 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000341 }
342 }
343
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000344 // Proceed to 'generic' cycle finder code
345 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000346}
347
Evan Cheng70e674e2006-08-28 20:10:17 +0000348/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
349/// and move load below the TokenFactor. Replace store's chain operand with
350/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000351static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000352 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000353 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000354 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
355 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000356 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000357 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000358 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000359 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
360 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
361 Load.getOperand(1),
362 Load.getOperand(2));
363 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000364 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000365}
366
Evan Chengcd0baf22008-05-23 21:23:16 +0000367/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
368///
Dan Gohman475871a2008-07-27 21:46:04 +0000369static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
370 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000371 if (N.getOpcode() == ISD::BIT_CONVERT)
372 N = N.getOperand(0);
373
374 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
375 if (!LD || LD->isVolatile())
376 return false;
377 if (LD->getAddressingMode() != ISD::UNINDEXED)
378 return false;
379
380 ISD::LoadExtType ExtType = LD->getExtensionType();
381 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
382 return false;
383
384 if (N.hasOneUse() &&
385 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000386 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000387 Load = N;
388 return true;
389 }
390 return false;
391}
392
Evan Chengab6c3bb2008-08-25 21:27:18 +0000393/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
394/// operand and move load below the call's chain operand.
395static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000396 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000397 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000398 SDValue Chain = CallSeqStart.getOperand(0);
399 if (Chain.getNode() == Load.getNode())
400 Ops.push_back(Load.getOperand(0));
401 else {
402 assert(Chain.getOpcode() == ISD::TokenFactor &&
403 "Unexpected CallSeqStart chain operand");
404 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
405 if (Chain.getOperand(i).getNode() == Load.getNode())
406 Ops.push_back(Load.getOperand(0));
407 else
408 Ops.push_back(Chain.getOperand(i));
409 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000410 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000412 Ops.clear();
413 Ops.push_back(NewChain);
414 }
415 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
416 Ops.push_back(CallSeqStart.getOperand(i));
417 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
419 Load.getOperand(1), Load.getOperand(2));
420 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000421 Ops.push_back(SDValue(Load.getNode(), 1));
422 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000423 Ops.push_back(Call.getOperand(i));
424 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
425}
426
427/// isCalleeLoad - Return true if call address is a load and it can be
428/// moved below CALLSEQ_START and the chains leading up to the call.
429/// Return the CALLSEQ_START by reference as a second output.
430static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000431 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000432 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000433 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000434 if (!LD ||
435 LD->isVolatile() ||
436 LD->getAddressingMode() != ISD::UNINDEXED ||
437 LD->getExtensionType() != ISD::NON_EXTLOAD)
438 return false;
439
440 // Now let's find the callseq_start.
441 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
442 if (!Chain.hasOneUse())
443 return false;
444 Chain = Chain.getOperand(0);
445 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000446
447 if (Chain.getOperand(0).getNode() == Callee.getNode())
448 return true;
449 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000450 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
451 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000452 return true;
453 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000454}
455
456
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000457/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000458/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000459/// This allows the instruction selector to pick more read-modify-write
460/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000461///
462/// [Load chain]
463/// ^
464/// |
465/// [Load]
466/// ^ ^
467/// | |
468/// / \-
469/// / |
470/// [TokenFactor] [Op]
471/// ^ ^
472/// | |
473/// \ /
474/// \ /
475/// [Store]
476///
477/// The fact the store's chain operand != load's chain will prevent the
478/// (store (op (load))) instruction from being selected. We can transform it to:
479///
480/// [Load chain]
481/// ^
482/// |
483/// [TokenFactor]
484/// ^
485/// |
486/// [Load]
487/// ^ ^
488/// | |
489/// | \-
490/// | |
491/// | [Op]
492/// | ^
493/// | |
494/// \ /
495/// \ /
496/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000497void X86DAGToDAGISel::PreprocessForRMW() {
498 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
499 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000500 if (I->getOpcode() == X86ISD::CALL) {
501 /// Also try moving call address load from outside callseq_start to just
502 /// before the call to allow it to be folded.
503 ///
504 /// [Load chain]
505 /// ^
506 /// |
507 /// [Load]
508 /// ^ ^
509 /// | |
510 /// / \--
511 /// / |
512 ///[CALLSEQ_START] |
513 /// ^ |
514 /// | |
515 /// [LOAD/C2Reg] |
516 /// | |
517 /// \ /
518 /// \ /
519 /// [CALL]
520 SDValue Chain = I->getOperand(0);
521 SDValue Load = I->getOperand(1);
522 if (!isCalleeLoad(Load, Chain))
523 continue;
524 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
525 ++NumLoadMoved;
526 continue;
527 }
528
Evan Cheng8b2794a2006-10-13 21:14:26 +0000529 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000530 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000531 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000532
Gabor Greifba36cb52008-08-28 21:40:38 +0000533 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000534 continue;
535
Dan Gohman475871a2008-07-27 21:46:04 +0000536 SDValue N1 = I->getOperand(1);
537 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000538 if ((N1.getValueType().isFloatingPoint() &&
539 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000540 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000541 continue;
542
543 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000544 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000545 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000546 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000547 case ISD::ADD:
548 case ISD::MUL:
549 case ISD::AND:
550 case ISD::OR:
551 case ISD::XOR:
552 case ISD::ADDC:
553 case ISD::ADDE:
554 case ISD::VECTOR_SHUFFLE: {
555 SDValue N10 = N1.getOperand(0);
556 SDValue N11 = N1.getOperand(1);
557 RModW = isRMWLoad(N10, Chain, N2, Load);
558 if (!RModW)
559 RModW = isRMWLoad(N11, Chain, N2, Load);
560 break;
561 }
562 case ISD::SUB:
563 case ISD::SHL:
564 case ISD::SRA:
565 case ISD::SRL:
566 case ISD::ROTL:
567 case ISD::ROTR:
568 case ISD::SUBC:
569 case ISD::SUBE:
570 case X86ISD::SHLD:
571 case X86ISD::SHRD: {
572 SDValue N10 = N1.getOperand(0);
573 RModW = isRMWLoad(N10, Chain, N2, Load);
574 break;
575 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000576 }
577
Evan Cheng82a35b32006-08-29 06:44:17 +0000578 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000579 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000580 ++NumLoadMoved;
581 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000582 }
583}
584
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000585
586/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
587/// nodes that target the FP stack to be store and load to the stack. This is a
588/// gross hack. We would like to simply mark these as being illegal, but when
589/// we do that, legalize produces these when it expands calls, then expands
590/// these in the same legalize pass. We would like dag combine to be able to
591/// hack on these between the call expansion and the node legalization. As such
592/// this pass basically does "really late" legalization of these inline with the
593/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000594void X86DAGToDAGISel::PreprocessForFPConvert() {
595 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
596 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000597 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
598 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
599 continue;
600
601 // If the source and destination are SSE registers, then this is a legal
602 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000603 EVT SrcVT = N->getOperand(0).getValueType();
604 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000605 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
606 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
607 if (SrcIsSSE && DstIsSSE)
608 continue;
609
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000610 if (!SrcIsSSE && !DstIsSSE) {
611 // If this is an FPStack extension, it is a noop.
612 if (N->getOpcode() == ISD::FP_EXTEND)
613 continue;
614 // If this is a value-preserving FPStack truncation, it is a noop.
615 if (N->getConstantOperandVal(1))
616 continue;
617 }
618
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000619 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
620 // FPStack has extload and truncstore. SSE can fold direct loads into other
621 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000622 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000623 if (N->getOpcode() == ISD::FP_ROUND)
624 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
625 else
626 MemVT = SrcIsSSE ? SrcVT : DstVT;
627
Dan Gohmanf350b272008-08-23 02:25:05 +0000628 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000629 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000630
631 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000632 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000633 N->getOperand(0),
634 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000635 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000636 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000637
638 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
639 // extload we created. This will cause general havok on the dag because
640 // anything below the conversion could be folded into other existing nodes.
641 // To avoid invalidating 'I', back it up to the convert node.
642 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000643 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000644
645 // Now that we did that, the node is dead. Increment the iterator to the
646 // next node to process, then delete N.
647 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000648 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000649 }
650}
651
Chris Lattnerc961eea2005-11-16 01:54:32 +0000652/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
653/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000654void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000655 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000656 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000657
Evan Chengdb8d56b2008-06-30 20:45:06 +0000658 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000659 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000660 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000661
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000662 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000663 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000664
Chris Lattnerc961eea2005-11-16 01:54:32 +0000665 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000666#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000667 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000668 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000669#endif
David Greene8ad4c002008-10-27 21:56:29 +0000670 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000671#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000672 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000673#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000674
Dan Gohmanf350b272008-08-23 02:25:05 +0000675 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000676}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000677
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000678/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
679/// the main function.
680void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
681 MachineFrameInfo *MFI) {
682 const TargetInstrInfo *TII = TM.getInstrInfo();
683 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000684 BuildMI(BB, DebugLoc::getUnknownLoc(),
685 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000686}
687
688void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
689 // If this is main, emit special code for main.
690 MachineBasicBlock *BB = MF.begin();
691 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
692 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
693}
694
Rafael Espindola094fad32009-04-08 21:14:34 +0000695
696bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
697 X86ISelAddressMode &AM) {
698 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
699 SDValue Segment = N.getOperand(0);
700
701 if (AM.Segment.getNode() == 0) {
702 AM.Segment = Segment;
703 return false;
704 }
705
706 return true;
707}
708
709bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
710 // This optimization is valid because the GNU TLS model defines that
711 // gs:0 (or fs:0 on X86-64) contains its own address.
712 // For more information see http://people.redhat.com/drepper/tls.pdf
713
714 SDValue Address = N.getOperand(1);
715 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
716 !MatchSegmentBaseAddress (Address, AM))
717 return false;
718
719 return true;
720}
721
Chris Lattner18c59872009-06-27 04:16:01 +0000722/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
723/// into an addressing mode. These wrap things that will resolve down into a
724/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000725/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000726bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000727 // If the addressing mode already has a symbol as the displacement, we can
728 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000729 if (AM.hasSymbolicDisplacement())
730 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000731
732 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000733 CodeModel::Model M = TM.getCodeModel();
734
Chris Lattner18c59872009-06-27 04:16:01 +0000735 // Handle X86-64 rip-relative addresses. We check this before checking direct
736 // folding because RIP is preferable to non-RIP accesses.
737 if (Subtarget->is64Bit() &&
738 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
739 // they cannot be folded into immediate fields.
740 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000741 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000742 // Base and index reg must be 0 in order to use %rip as base and lowering
743 // must allow RIP.
744 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000745 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
746 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000747 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000748 AM.GV = G->getGlobal();
749 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000750 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000751 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
752 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000753 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000754 AM.CP = CP->getConstVal();
755 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000756 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000757 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000758 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
759 AM.ES = S->getSymbol();
760 AM.SymbolFlags = S->getTargetFlags();
761 } else {
762 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
763 AM.JT = J->getIndex();
764 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000765 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000766
Chris Lattner18c59872009-06-27 04:16:01 +0000767 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000769 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000770 }
771
772 // Handle the case when globals fit in our immediate field: This is true for
773 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
774 // mode, this results in a non-RIP-relative computation.
775 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000776 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000777 TM.getRelocationModel() == Reloc::Static)) {
778 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
779 AM.GV = G->getGlobal();
780 AM.Disp += G->getOffset();
781 AM.SymbolFlags = G->getTargetFlags();
782 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
783 AM.CP = CP->getConstVal();
784 AM.Align = CP->getAlignment();
785 AM.Disp += CP->getOffset();
786 AM.SymbolFlags = CP->getTargetFlags();
787 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
788 AM.ES = S->getSymbol();
789 AM.SymbolFlags = S->getTargetFlags();
790 } else {
791 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
792 AM.JT = J->getIndex();
793 AM.SymbolFlags = J->getTargetFlags();
794 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000795 return false;
796 }
797
798 return true;
799}
800
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000801/// MatchAddress - Add the specified node to the specified addressing mode,
802/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000803/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000804bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
805 if (MatchAddressRecursively(N, AM, 0))
806 return true;
807
808 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
809 // a smaller encoding and avoids a scaled-index.
810 if (AM.Scale == 2 &&
811 AM.BaseType == X86ISelAddressMode::RegBase &&
812 AM.Base.Reg.getNode() == 0) {
813 AM.Base.Reg = AM.IndexReg;
814 AM.Scale = 1;
815 }
816
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000817 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
818 // because it has a smaller encoding.
819 // TODO: Which other code models can use this?
820 if (TM.getCodeModel() == CodeModel::Small &&
821 Subtarget->is64Bit() &&
822 AM.Scale == 1 &&
823 AM.BaseType == X86ISelAddressMode::RegBase &&
824 AM.Base.Reg.getNode() == 0 &&
825 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000826 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000827 AM.hasSymbolicDisplacement())
828 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
829
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000830 return false;
831}
832
833bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
834 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000835 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000836 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000837 DEBUG({
838 errs() << "MatchAddress: ";
839 AM.dump();
840 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000841 // Limit recursion.
842 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000843 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000844
845 CodeModel::Model M = TM.getCodeModel();
846
Chris Lattner18c59872009-06-27 04:16:01 +0000847 // If this is already a %rip relative address, we can only merge immediates
848 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000849 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000850 if (AM.isRIPRelative()) {
851 // FIXME: JumpTable and ExternalSymbol address currently don't like
852 // displacements. It isn't very important, but this should be fixed for
853 // consistency.
854 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000855
Chris Lattner18c59872009-06-27 04:16:01 +0000856 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
857 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000858 if (X86::isOffsetSuitableForCodeModel(Val, M,
859 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000860 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000861 return false;
862 }
863 }
864 return true;
865 }
866
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000867 switch (N.getOpcode()) {
868 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000869 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000870 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000871 if (!is64Bit ||
872 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
873 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000874 AM.Disp += Val;
875 return false;
876 }
877 break;
878 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000879
Rafael Espindola094fad32009-04-08 21:14:34 +0000880 case X86ISD::SegmentBaseAddress:
881 if (!MatchSegmentBaseAddress(N, AM))
882 return false;
883 break;
884
Rafael Espindola49a168d2009-04-12 21:55:03 +0000885 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000886 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000887 if (!MatchWrapper(N, AM))
888 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000889 break;
890
Rafael Espindola094fad32009-04-08 21:14:34 +0000891 case ISD::LOAD:
892 if (!MatchLoad(N, AM))
893 return false;
894 break;
895
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000896 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000897 if (AM.BaseType == X86ISelAddressMode::RegBase
898 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000899 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
900 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
901 return false;
902 }
903 break;
Evan Chengec693f72005-12-08 02:01:35 +0000904
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000905 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000906 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000907 break;
908
Gabor Greif93c53e52008-08-31 15:37:04 +0000909 if (ConstantSDNode
910 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000911 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000912 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
913 // that the base operand remains free for further matching. If
914 // the base doesn't end up getting used, a post-processing step
915 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000916 if (Val == 1 || Val == 2 || Val == 3) {
917 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000919
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000920 // Okay, we know that we have a scale by now. However, if the scaled
921 // value is an add of something and a constant, we can fold the
922 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
924 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
925 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000926 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000927 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000928 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000929 if (!is64Bit ||
930 X86::isOffsetSuitableForCodeModel(Disp, M,
931 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000932 AM.Disp = Disp;
933 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000934 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000935 } else {
936 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000937 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000938 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000939 }
940 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000941 }
Evan Chengec693f72005-12-08 02:01:35 +0000942
Dan Gohman83688052007-10-22 20:22:24 +0000943 case ISD::SMUL_LOHI:
944 case ISD::UMUL_LOHI:
945 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000946 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000947 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000948 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000949 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000950 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000951 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000952 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000953 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000954 if (ConstantSDNode
955 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000956 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
957 CN->getZExtValue() == 9) {
958 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000959
Gabor Greifba36cb52008-08-28 21:40:38 +0000960 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000961 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000962
963 // Okay, we know that we have a scale by now. However, if the scaled
964 // value is an add of something and a constant, we can fold the
965 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000966 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
967 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
968 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000969 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000970 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000971 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000972 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000973 if (!is64Bit ||
974 X86::isOffsetSuitableForCodeModel(Disp, M,
975 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000976 AM.Disp = Disp;
977 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000978 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000979 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000981 }
982
983 AM.IndexReg = AM.Base.Reg = Reg;
984 return false;
985 }
Chris Lattner62412262007-02-04 20:18:17 +0000986 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000987 break;
988
Dan Gohman3cd90a12009-05-11 18:02:53 +0000989 case ISD::SUB: {
990 // Given A-B, if A can be completely folded into the address and
991 // the index field with the index field unused, use -B as the index.
992 // This is a win if a has multiple parts that can be folded into
993 // the address. Also, this saves a mov if the base register has
994 // other uses, since it avoids a two-address sub instruction, however
995 // it costs an additional mov if the index register has other uses.
996
997 // Test if the LHS of the sub can be folded.
998 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000999 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001000 AM = Backup;
1001 break;
1002 }
1003 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001004 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001005 AM = Backup;
1006 break;
1007 }
1008 int Cost = 0;
1009 SDValue RHS = N.getNode()->getOperand(1);
1010 // If the RHS involves a register with multiple uses, this
1011 // transformation incurs an extra mov, due to the neg instruction
1012 // clobbering its operand.
1013 if (!RHS.getNode()->hasOneUse() ||
1014 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1015 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1016 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1017 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001019 ++Cost;
1020 // If the base is a register with multiple uses, this
1021 // transformation may save a mov.
1022 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1023 AM.Base.Reg.getNode() &&
1024 !AM.Base.Reg.getNode()->hasOneUse()) ||
1025 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1026 --Cost;
1027 // If the folded LHS was interesting, this transformation saves
1028 // address arithmetic.
1029 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1030 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1031 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1032 --Cost;
1033 // If it doesn't look like it may be an overall win, don't do it.
1034 if (Cost >= 0) {
1035 AM = Backup;
1036 break;
1037 }
1038
1039 // Ok, the transformation is legal and appears profitable. Go for it.
1040 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1041 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1042 AM.IndexReg = Neg;
1043 AM.Scale = 1;
1044
1045 // Insert the new nodes into the topological ordering.
1046 if (Zero.getNode()->getNodeId() == -1 ||
1047 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1048 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1049 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1050 }
1051 if (Neg.getNode()->getNodeId() == -1 ||
1052 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1053 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1054 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1055 }
1056 return false;
1057 }
1058
Evan Cheng8e278262009-01-17 07:09:27 +00001059 case ISD::ADD: {
1060 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001061 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1062 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001063 return false;
1064 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001065 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1066 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001067 return false;
1068 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001069
1070 // If we couldn't fold both operands into the address at the same time,
1071 // see if we can just put each operand into a register and fold at least
1072 // the add.
1073 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1074 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001075 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001076 AM.Base.Reg = N.getNode()->getOperand(0);
1077 AM.IndexReg = N.getNode()->getOperand(1);
1078 AM.Scale = 1;
1079 return false;
1080 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001081 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001082 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001083
Chris Lattner62412262007-02-04 20:18:17 +00001084 case ISD::OR:
1085 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001086 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1087 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001088 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001089 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001090 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001091 // Address could not have picked a GV address for the displacement.
1092 AM.GV == NULL &&
1093 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001094 (!is64Bit ||
1095 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1096 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001097 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001098 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001099 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001100 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001101 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001102 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001103 }
1104 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001105
1106 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001107 // Perform some heroic transforms on an and of a constant-count shift
1108 // with a constant to enable use of the scaled offset field.
1109
Dan Gohman475871a2008-07-27 21:46:04 +00001110 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001111 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001112
Evan Cheng1314b002007-12-13 00:43:27 +00001113 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001114 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001115
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001116 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001117 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1118 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1119 if (!C1 || !C2) break;
1120
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001121 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1122 // allows us to convert the shift and and into an h-register extract and
1123 // a scaled index.
1124 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1125 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001126 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001127 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001129 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1130 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1131 X, Eight);
1132 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1133 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001135 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1136 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001137
1138 // Insert the new nodes into the topological ordering.
1139 if (Eight.getNode()->getNodeId() == -1 ||
1140 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1141 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1142 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1143 }
1144 if (Mask.getNode()->getNodeId() == -1 ||
1145 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1146 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1147 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1148 }
1149 if (Srl.getNode()->getNodeId() == -1 ||
1150 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1151 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1152 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1153 }
1154 if (And.getNode()->getNodeId() == -1 ||
1155 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1156 CurDAG->RepositionNode(N.getNode(), And.getNode());
1157 And.getNode()->setNodeId(N.getNode()->getNodeId());
1158 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001159 if (ShlCount.getNode()->getNodeId() == -1 ||
1160 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1161 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1162 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1163 }
1164 if (Shl.getNode()->getNodeId() == -1 ||
1165 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1166 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1167 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1168 }
1169 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001170 AM.IndexReg = And;
1171 AM.Scale = (1 << ScaleLog);
1172 return false;
1173 }
1174 }
1175
1176 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1177 // allows us to fold the shift into this addressing mode.
1178 if (Shift.getOpcode() != ISD::SHL) break;
1179
Evan Cheng1314b002007-12-13 00:43:27 +00001180 // Not likely to be profitable if either the AND or SHIFT node has more
1181 // than one use (unless all uses are for address computation). Besides,
1182 // isel mechanism requires their node ids to be reused.
1183 if (!N.hasOneUse() || !Shift.hasOneUse())
1184 break;
1185
1186 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001187 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001188 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1189 break;
1190
1191 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001192 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001193 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001194 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1195 NewANDMask);
1196 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001197 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001198
1199 // Insert the new nodes into the topological ordering.
1200 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1201 CurDAG->RepositionNode(X.getNode(), C1);
1202 C1->setNodeId(X.getNode()->getNodeId());
1203 }
1204 if (NewANDMask.getNode()->getNodeId() == -1 ||
1205 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1206 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1207 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1208 }
1209 if (NewAND.getNode()->getNodeId() == -1 ||
1210 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1211 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1212 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1213 }
1214 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1215 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1216 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1217 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1218 }
1219
Dan Gohman7b8e9642008-10-13 20:52:04 +00001220 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001221
1222 AM.Scale = 1 << ShiftCst;
1223 AM.IndexReg = NewAND;
1224 return false;
1225 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001226 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001227
Rafael Espindola523249f2009-03-31 16:16:57 +00001228 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001229}
1230
1231/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1232/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001233bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001234 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001236 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001237 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001238 AM.IndexReg = N;
1239 AM.Scale = 1;
1240 return false;
1241 }
1242
1243 // Otherwise, we cannot select it.
1244 return true;
1245 }
1246
1247 // Default, generate it as a register.
1248 AM.BaseType = X86ISelAddressMode::RegBase;
1249 AM.Base.Reg = N;
1250 return false;
1251}
1252
Evan Chengec693f72005-12-08 02:01:35 +00001253/// SelectAddr - returns true if it is able pattern match an addressing mode.
1254/// It returns the operands which make up the maximal addressing mode it can
1255/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001256bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1257 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001258 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001259 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001260 bool Done = false;
1261 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1262 unsigned Opcode = N.getOpcode();
1263 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001264 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001265 // If we are able to fold N into addressing mode, then we'll allow it even
1266 // if N has multiple uses. In general, addressing computation is used as
1267 // addresses by all of its uses. But watch out for CopyToReg uses, that
1268 // means the address computation is liveout. It will be computed by a LEA
1269 // so we want to avoid computing the address twice.
1270 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1271 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1272 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001273 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001274 Done = true;
1275 break;
1276 }
1277 }
1278 }
1279 }
1280
1281 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001282 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001283
Owen Andersone50ed302009-08-10 22:56:29 +00001284 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001285 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001287 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001288 }
Evan Cheng8700e142006-01-11 06:09:51 +00001289
Gabor Greifba36cb52008-08-28 21:40:38 +00001290 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001291 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001292
Rafael Espindola094fad32009-04-08 21:14:34 +00001293 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001294 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001295}
1296
Chris Lattner3a7cd952006-10-07 21:55:32 +00001297/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1298/// match a load whose top elements are either undef or zeros. The load flavor
1299/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001300bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1301 SDValue N, SDValue &Base,
1302 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001303 SDValue &Disp, SDValue &Segment,
1304 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001306 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001307 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001308 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001309 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001310 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001311 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001312 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001313 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001314 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001315 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001316 return true;
1317 }
1318 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001319
1320 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001321 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001323 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001324 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001325 N.getOperand(0).getNode()->hasOneUse() &&
1326 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001327 N.getOperand(0).getOperand(0).hasOneUse()) {
1328 // Okay, this is a zero extending load. Fold it.
1329 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001330 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001331 return false;
1332 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001333 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001334 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001335 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001336 return false;
1337}
1338
1339
Evan Cheng51a9ed92006-02-25 10:09:08 +00001340/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1341/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001342bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1343 SDValue &Base, SDValue &Scale,
1344 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001345 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001346
1347 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1348 // segments.
1349 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001351 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001352 if (MatchAddress(N, AM))
1353 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001354 assert (T == AM.Segment);
1355 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001356
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001358 unsigned Complexity = 0;
1359 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001361 Complexity = 1;
1362 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001363 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001364 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1365 Complexity = 4;
1366
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001368 Complexity++;
1369 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001370 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001371
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001372 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1373 // a simple shift.
1374 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001375 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001376
1377 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1378 // to a LEA. This is determined with some expermentation but is by no means
1379 // optimal (especially for code size consideration). LEA is nice because of
1380 // its three-address nature. Tweak the cost function again when we can run
1381 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001382 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001383 // For X86-64, we should always use lea to materialize RIP relative
1384 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001385 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001386 Complexity = 4;
1387 else
1388 Complexity += 2;
1389 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001390
Gabor Greifba36cb52008-08-28 21:40:38 +00001391 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001392 Complexity++;
1393
Chris Lattner25142782009-07-11 22:50:33 +00001394 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001395 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001396 return false;
1397
1398 SDValue Segment;
1399 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1400 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001401}
1402
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001403/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1404bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1405 SDValue &Scale, SDValue &Index,
1406 SDValue &Disp) {
1407 assert(Op.getOpcode() == X86ISD::TLSADDR);
1408 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1409 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1410
1411 X86ISelAddressMode AM;
1412 AM.GV = GA->getGlobal();
1413 AM.Disp += GA->getOffset();
1414 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001415 AM.SymbolFlags = GA->getTargetFlags();
1416
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001418 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001420 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001422 }
1423
1424 SDValue Segment;
1425 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1426 return true;
1427}
1428
1429
Dan Gohman475871a2008-07-27 21:46:04 +00001430bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1431 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001432 SDValue &Index, SDValue &Disp,
1433 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001434 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001435 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001436 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001437 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001438 return false;
1439}
1440
Dan Gohman8b746962008-09-23 18:22:58 +00001441/// getGlobalBaseReg - Return an SDNode that returns the value of
1442/// the global base register. Output instructions required to
1443/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001444///
Evan Cheng9ade2182006-08-26 05:34:46 +00001445SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001446 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001447 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001448}
1449
Evan Chengb245d922006-05-20 01:36:52 +00001450static SDNode *FindCallStartFromCall(SDNode *Node) {
1451 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001453 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001454 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001455}
1456
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001457SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1458 SDValue Chain = Node->getOperand(0);
1459 SDValue In1 = Node->getOperand(1);
1460 SDValue In2L = Node->getOperand(2);
1461 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001462 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1463 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001464 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001465 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001466 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001467 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001469 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001470}
Christopher Lambc59e5212007-08-10 21:48:46 +00001471
Owen Andersone50ed302009-08-10 22:56:29 +00001472SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001473 if (Node->hasAnyUseOfValue(0))
1474 return 0;
1475
1476 // Optimize common patterns for __sync_add_and_fetch and
1477 // __sync_sub_and_fetch where the result is not used. This allows us
1478 // to use "lock" version of add, sub, inc, dec instructions.
1479 // FIXME: Do not use special instructions but instead add the "lock"
1480 // prefix to the target node somehow. The extra information will then be
1481 // transferred to machine instruction and it denotes the prefix.
1482 SDValue Chain = Node->getOperand(0);
1483 SDValue Ptr = Node->getOperand(1);
1484 SDValue Val = Node->getOperand(2);
1485 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1486 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1487 return 0;
1488
1489 bool isInc = false, isDec = false, isSub = false, isCN = false;
1490 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1491 if (CN) {
1492 isCN = true;
1493 int64_t CNVal = CN->getSExtValue();
1494 if (CNVal == 1)
1495 isInc = true;
1496 else if (CNVal == -1)
1497 isDec = true;
1498 else if (CNVal >= 0)
1499 Val = CurDAG->getTargetConstant(CNVal, NVT);
1500 else {
1501 isSub = true;
1502 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1503 }
1504 } else if (Val.hasOneUse() &&
1505 Val.getOpcode() == ISD::SUB &&
1506 X86::isZeroNode(Val.getOperand(0))) {
1507 isSub = true;
1508 Val = Val.getOperand(1);
1509 }
1510
1511 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001513 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001514 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001515 if (isInc)
1516 Opc = X86::LOCK_INC8m;
1517 else if (isDec)
1518 Opc = X86::LOCK_DEC8m;
1519 else if (isSub) {
1520 if (isCN)
1521 Opc = X86::LOCK_SUB8mi;
1522 else
1523 Opc = X86::LOCK_SUB8mr;
1524 } else {
1525 if (isCN)
1526 Opc = X86::LOCK_ADD8mi;
1527 else
1528 Opc = X86::LOCK_ADD8mr;
1529 }
1530 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001532 if (isInc)
1533 Opc = X86::LOCK_INC16m;
1534 else if (isDec)
1535 Opc = X86::LOCK_DEC16m;
1536 else if (isSub) {
1537 if (isCN) {
1538 if (Predicate_i16immSExt8(Val.getNode()))
1539 Opc = X86::LOCK_SUB16mi8;
1540 else
1541 Opc = X86::LOCK_SUB16mi;
1542 } else
1543 Opc = X86::LOCK_SUB16mr;
1544 } else {
1545 if (isCN) {
1546 if (Predicate_i16immSExt8(Val.getNode()))
1547 Opc = X86::LOCK_ADD16mi8;
1548 else
1549 Opc = X86::LOCK_ADD16mi;
1550 } else
1551 Opc = X86::LOCK_ADD16mr;
1552 }
1553 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001555 if (isInc)
1556 Opc = X86::LOCK_INC32m;
1557 else if (isDec)
1558 Opc = X86::LOCK_DEC32m;
1559 else if (isSub) {
1560 if (isCN) {
1561 if (Predicate_i32immSExt8(Val.getNode()))
1562 Opc = X86::LOCK_SUB32mi8;
1563 else
1564 Opc = X86::LOCK_SUB32mi;
1565 } else
1566 Opc = X86::LOCK_SUB32mr;
1567 } else {
1568 if (isCN) {
1569 if (Predicate_i32immSExt8(Val.getNode()))
1570 Opc = X86::LOCK_ADD32mi8;
1571 else
1572 Opc = X86::LOCK_ADD32mi;
1573 } else
1574 Opc = X86::LOCK_ADD32mr;
1575 }
1576 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001578 if (isInc)
1579 Opc = X86::LOCK_INC64m;
1580 else if (isDec)
1581 Opc = X86::LOCK_DEC64m;
1582 else if (isSub) {
1583 Opc = X86::LOCK_SUB64mr;
1584 if (isCN) {
1585 if (Predicate_i64immSExt8(Val.getNode()))
1586 Opc = X86::LOCK_SUB64mi8;
1587 else if (Predicate_i64immSExt32(Val.getNode()))
1588 Opc = X86::LOCK_SUB64mi32;
1589 }
1590 } else {
1591 Opc = X86::LOCK_ADD64mr;
1592 if (isCN) {
1593 if (Predicate_i64immSExt8(Val.getNode()))
1594 Opc = X86::LOCK_ADD64mi8;
1595 else if (Predicate_i64immSExt32(Val.getNode()))
1596 Opc = X86::LOCK_ADD64mi32;
1597 }
1598 }
1599 break;
1600 }
1601
1602 DebugLoc dl = Node->getDebugLoc();
1603 SDValue Undef = SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1604 dl, NVT), 0);
1605 SDValue MemOp = CurDAG->getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
1606 if (isInc || isDec) {
1607 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, MemOp, Chain };
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 7), 0);
Evan Cheng37b73872009-07-30 08:33:02 +00001609 SDValue RetVals[] = { Undef, Ret };
1610 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1611 } else {
1612 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, MemOp, Chain };
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 8), 0);
Evan Cheng37b73872009-07-30 08:33:02 +00001614 SDValue RetVals[] = { Undef, Ret };
1615 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1616 }
1617}
1618
Dan Gohman475871a2008-07-27 21:46:04 +00001619SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001621 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001622 unsigned Opc, MOpc;
1623 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001624 DebugLoc dl = Node->getDebugLoc();
1625
Evan Chengf597dc72006-02-10 22:24:32 +00001626#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001627 DEBUG({
1628 errs() << std::string(Indent, ' ') << "Selecting: ";
1629 Node->dump(CurDAG);
1630 errs() << '\n';
1631 });
Evan Cheng23addc02006-02-10 22:46:26 +00001632 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001633#endif
1634
Dan Gohmane8be6c62008-07-17 19:10:17 +00001635 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001636#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001637 DEBUG({
1638 errs() << std::string(Indent-2, ' ') << "== ";
1639 Node->dump(CurDAG);
1640 errs() << '\n';
1641 });
Evan Cheng23addc02006-02-10 22:46:26 +00001642 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001643#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001644 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001645 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001646
Evan Cheng0114e942006-01-06 20:36:21 +00001647 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001648 default: break;
1649 case X86ISD::GlobalBaseReg:
1650 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001651
Dan Gohman72677342009-08-02 16:10:52 +00001652 case X86ISD::ATOMOR64_DAG:
1653 return SelectAtomic64(Node, X86::ATOMOR6432);
1654 case X86ISD::ATOMXOR64_DAG:
1655 return SelectAtomic64(Node, X86::ATOMXOR6432);
1656 case X86ISD::ATOMADD64_DAG:
1657 return SelectAtomic64(Node, X86::ATOMADD6432);
1658 case X86ISD::ATOMSUB64_DAG:
1659 return SelectAtomic64(Node, X86::ATOMSUB6432);
1660 case X86ISD::ATOMNAND64_DAG:
1661 return SelectAtomic64(Node, X86::ATOMNAND6432);
1662 case X86ISD::ATOMAND64_DAG:
1663 return SelectAtomic64(Node, X86::ATOMAND6432);
1664 case X86ISD::ATOMSWAP64_DAG:
1665 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001666
Dan Gohman72677342009-08-02 16:10:52 +00001667 case ISD::ATOMIC_LOAD_ADD: {
1668 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1669 if (RetVal)
1670 return RetVal;
1671 break;
1672 }
1673
1674 case ISD::SMUL_LOHI:
1675 case ISD::UMUL_LOHI: {
1676 SDValue N0 = Node->getOperand(0);
1677 SDValue N1 = Node->getOperand(1);
1678
1679 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001680 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001681 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001682 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001683 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1684 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1685 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1686 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001687 }
Bill Wendling12321672009-08-07 21:33:25 +00001688 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001690 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001691 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1692 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1693 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1694 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001695 }
Bill Wendling12321672009-08-07 21:33:25 +00001696 }
Dan Gohman72677342009-08-02 16:10:52 +00001697
1698 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001700 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1702 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1703 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1704 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001705 }
1706
1707 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1708 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001709 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001710 if (!foldedLoad) {
1711 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1712 if (foldedLoad)
1713 std::swap(N0, N1);
1714 }
1715
1716 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1717 N0, SDValue()).getValue(1);
1718
1719 if (foldedLoad) {
1720 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1721 InFlag };
1722 SDNode *CNode =
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001724 array_lengthof(Ops));
1725 InFlag = SDValue(CNode, 1);
1726 // Update the chain.
1727 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1728 } else {
1729 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001731 }
1732
1733 // Copy the low half of the result, if it is needed.
1734 if (!N.getValue(0).use_empty()) {
1735 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1736 LoReg, NVT, InFlag);
1737 InFlag = Result.getValue(2);
1738 ReplaceUses(N.getValue(0), Result);
1739#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001740 DEBUG({
1741 errs() << std::string(Indent-2, ' ') << "=> ";
1742 Result.getNode()->dump(CurDAG);
1743 errs() << '\n';
1744 });
Dan Gohman72677342009-08-02 16:10:52 +00001745#endif
1746 }
1747 // Copy the high half of the result, if it is needed.
1748 if (!N.getValue(1).use_empty()) {
1749 SDValue Result;
1750 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1751 // Prevent use of AH in a REX instruction by referencing AX instead.
1752 // Shift it down 8 bits.
1753 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001755 InFlag = Result.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001757 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001759 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001760 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1761 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001762 } else {
1763 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1764 HiReg, NVT, InFlag);
1765 InFlag = Result.getValue(2);
1766 }
1767 ReplaceUses(N.getValue(1), Result);
1768#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001769 DEBUG({
1770 errs() << std::string(Indent-2, ' ') << "=> ";
1771 Result.getNode()->dump(CurDAG);
1772 errs() << '\n';
1773 });
Dan Gohman72677342009-08-02 16:10:52 +00001774#endif
1775 }
1776
1777#ifndef NDEBUG
1778 Indent -= 2;
1779#endif
1780
1781 return NULL;
1782 }
1783
1784 case ISD::SDIVREM:
1785 case ISD::UDIVREM: {
1786 SDValue N0 = Node->getOperand(0);
1787 SDValue N1 = Node->getOperand(1);
1788
1789 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001790 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001792 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1794 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1795 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1796 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001797 }
Bill Wendling12321672009-08-07 21:33:25 +00001798 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001800 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1802 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1803 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1804 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001805 }
Bill Wendling12321672009-08-07 21:33:25 +00001806 }
Dan Gohman72677342009-08-02 16:10:52 +00001807
1808 unsigned LoReg, HiReg;
1809 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001811 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001813 LoReg = X86::AL; HiReg = X86::AH;
1814 ClrOpcode = 0;
1815 SExtOpcode = X86::CBW;
1816 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001818 LoReg = X86::AX; HiReg = X86::DX;
1819 ClrOpcode = X86::MOV16r0;
1820 SExtOpcode = X86::CWD;
1821 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001823 LoReg = X86::EAX; HiReg = X86::EDX;
1824 ClrOpcode = X86::MOV32r0;
1825 SExtOpcode = X86::CDQ;
1826 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001828 LoReg = X86::RAX; HiReg = X86::RDX;
1829 ClrOpcode = ~0U; // NOT USED.
1830 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001831 break;
1832 }
1833
Dan Gohman72677342009-08-02 16:10:52 +00001834 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1835 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1836 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001837
Dan Gohman72677342009-08-02 16:10:52 +00001838 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001840 // Special case for div8, just use a move with zero extension to AX to
1841 // clear the upper 8 bits (AH).
1842 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1843 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1844 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1845 Move =
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
1847 MVT::Other, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001848 array_lengthof(Ops)), 0);
1849 Chain = Move.getValue(1);
1850 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001851 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001852 Move =
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001854 Chain = CurDAG->getEntryNode();
1855 }
1856 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1857 InFlag = Chain.getValue(1);
1858 } else {
1859 InFlag =
1860 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1861 LoReg, N0, SDValue()).getValue(1);
1862 if (isSigned && !signBitIsZero) {
1863 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001864 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001865 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001866 } else {
1867 // Zero out the high part, effectively zero extending the input.
1868 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001869
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 if (NVT.getSimpleVT() == MVT::i64) {
1871 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001872 0);
1873 // We just did a 32-bit clear, insert it into a 64-bit register to
1874 // clear the whole 64-bit reg.
1875 SDValue Undef =
1876 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 dl, MVT::i64), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001878 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001880 ClrNode =
1881 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 MVT::i64, Undef, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001883 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001884 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001885 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001886 }
Dan Gohman72677342009-08-02 16:10:52 +00001887
1888 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1889 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001890 }
Evan Cheng948f3432006-01-06 23:19:29 +00001891 }
Dan Gohman525178c2007-10-08 18:33:35 +00001892
Dan Gohman72677342009-08-02 16:10:52 +00001893 if (foldedLoad) {
1894 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1895 InFlag };
1896 SDNode *CNode =
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Dan Gohman72677342009-08-02 16:10:52 +00001898 array_lengthof(Ops));
1899 InFlag = SDValue(CNode, 1);
1900 // Update the chain.
1901 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1902 } else {
1903 InFlag =
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001905 }
Evan Cheng948f3432006-01-06 23:19:29 +00001906
Dan Gohman72677342009-08-02 16:10:52 +00001907 // Copy the division (low) result, if it is needed.
1908 if (!N.getValue(0).use_empty()) {
1909 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1910 LoReg, NVT, InFlag);
1911 InFlag = Result.getValue(2);
1912 ReplaceUses(N.getValue(0), Result);
1913#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001914 DEBUG({
1915 errs() << std::string(Indent-2, ' ') << "=> ";
1916 Result.getNode()->dump(CurDAG);
1917 errs() << '\n';
1918 });
Dan Gohman72677342009-08-02 16:10:52 +00001919#endif
1920 }
1921 // Copy the remainder (high) result, if it is needed.
1922 if (!N.getValue(1).use_empty()) {
1923 SDValue Result;
1924 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1925 // Prevent use of AH in a REX instruction by referencing AX instead.
1926 // Shift it down 8 bits.
1927 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001929 InFlag = Result.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001931 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001933 0);
1934 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001935 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1936 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001937 } else {
1938 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1939 HiReg, NVT, InFlag);
1940 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001941 }
Dan Gohman72677342009-08-02 16:10:52 +00001942 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001943#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001944 DEBUG({
1945 errs() << std::string(Indent-2, ' ') << "=> ";
1946 Result.getNode()->dump(CurDAG);
1947 errs() << '\n';
1948 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00001949#endif
Dan Gohman72677342009-08-02 16:10:52 +00001950 }
Evan Chengf597dc72006-02-10 22:24:32 +00001951
1952#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00001953 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001954#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001955
Dan Gohman72677342009-08-02 16:10:52 +00001956 return NULL;
1957 }
1958
Dan Gohman6a402dc2009-08-19 18:16:17 +00001959 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001960 SDValue N0 = Node->getOperand(0);
1961 SDValue N1 = Node->getOperand(1);
1962
1963 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1964 // use a smaller encoding.
1965 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1966 N0.getValueType() != MVT::i8 &&
1967 X86::isZeroNode(N1)) {
1968 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1969 if (!C) break;
1970
1971 // For example, convert "testl %eax, $8" to "testb %al, $8"
1972 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0) {
1973 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1974 SDValue Reg = N0.getNode()->getOperand(0);
1975
1976 // On x86-32, only the ABCD registers have 8-bit subregisters.
1977 if (!Subtarget->is64Bit()) {
1978 TargetRegisterClass *TRC = 0;
1979 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1980 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1981 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1982 default: llvm_unreachable("Unsupported TEST operand type!");
1983 }
1984 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
1985 Reg = SDValue(CurDAG->getTargetNode(X86::COPY_TO_REGCLASS, dl,
1986 Reg.getValueType(), Reg, RC), 0);
1987 }
1988
1989 // Extract the l-register.
1990 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1991 MVT::i8, Reg);
1992
1993 // Emit a testb.
1994 return CurDAG->getTargetNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
1995 }
1996
1997 // For example, "testl %eax, $2048" to "testb %ah, $8".
1998 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0) {
1999 // Shift the immediate right by 8 bits.
2000 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2001 MVT::i8);
2002 SDValue Reg = N0.getNode()->getOperand(0);
2003
2004 // Put the value in an ABCD register.
2005 TargetRegisterClass *TRC = 0;
2006 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2007 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2008 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2009 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2010 default: llvm_unreachable("Unsupported TEST operand type!");
2011 }
2012 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
2013 Reg = SDValue(CurDAG->getTargetNode(X86::COPY_TO_REGCLASS, dl,
2014 Reg.getValueType(), Reg, RC), 0);
2015
2016 // Extract the h-register.
2017 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2018 MVT::i8, Reg);
2019
2020 // Emit a testb. No special NOREX tricks are needed since there's
2021 // only one GPR operand!
2022 return CurDAG->getTargetNode(X86::TEST8ri, dl, MVT::i32,
2023 Subreg, ShiftedImm);
2024 }
2025
2026 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2027 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
2028 N0.getValueType() != MVT::i16) {
2029 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2030 SDValue Reg = N0.getNode()->getOperand(0);
2031
2032 // Extract the 16-bit subregister.
2033 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2034 MVT::i16, Reg);
2035
2036 // Emit a testw.
2037 return CurDAG->getTargetNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
2038 }
2039
2040 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2041 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
2042 N0.getValueType() == MVT::i64) {
2043 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2044 SDValue Reg = N0.getNode()->getOperand(0);
2045
2046 // Extract the 32-bit subregister.
2047 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2048 MVT::i32, Reg);
2049
2050 // Emit a testl.
2051 return CurDAG->getTargetNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
2052 }
2053 }
2054 break;
2055 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002056 }
2057
Evan Cheng9ade2182006-08-26 05:34:46 +00002058 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002059
Evan Chengf597dc72006-02-10 22:24:32 +00002060#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002061 DEBUG({
2062 errs() << std::string(Indent-2, ' ') << "=> ";
2063 if (ResNode == NULL || ResNode == N.getNode())
2064 N.getNode()->dump(CurDAG);
2065 else
2066 ResNode->dump(CurDAG);
2067 errs() << '\n';
2068 });
Evan Cheng23addc02006-02-10 22:46:26 +00002069 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002070#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002071
2072 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002073}
2074
Chris Lattnerc0bad572006-06-08 18:03:49 +00002075bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002076SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002077 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002078 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002079 switch (ConstraintCode) {
2080 case 'o': // offsetable ??
2081 case 'v': // not offsetable ??
2082 default: return true;
2083 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002084 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002085 return true;
2086 break;
2087 }
2088
Evan Cheng04699902006-08-26 01:05:16 +00002089 OutOps.push_back(Op0);
2090 OutOps.push_back(Op1);
2091 OutOps.push_back(Op2);
2092 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002093 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002094 return false;
2095}
2096
Chris Lattnerc961eea2005-11-16 01:54:32 +00002097/// createX86ISelDag - This pass converts a legalized DAG into a
2098/// X86-specific DAG, ready for instruction scheduling.
2099///
Bill Wendling98a366d2009-04-29 23:29:43 +00002100FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2101 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002102 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002103}