blob: f12b138abbf099318d74f5009450d3fbfd9025a2 [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"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 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 Lattnerf9ce9fb2005-11-19 02:11:08 +000078
79 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000080 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
Rafael Espindola094fad32009-04-08 21:14:34 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000082 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000083
84 bool hasSymbolicDisplacement() const {
85 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
86 }
87
Dale Johannesen50dd1d02008-08-11 23:46:25 +000088 void dump() {
89 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000090 cerr << "Base.Reg ";
91 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
92 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000093 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
94 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000095 cerr << "IndexReg ";
96 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
97 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000098 cerr << " Disp " << Disp << "\n";
99 cerr << "GV "; if (GV) GV->dump();
100 else cerr << "nul";
101 cerr << " CP "; if (CP) CP->dump();
102 else cerr << "nul";
103 cerr << "\n";
104 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
105 cerr << " JT" << JT << " Align" << Align << "\n";
106 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000107 };
108}
109
110namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000111 //===--------------------------------------------------------------------===//
112 /// ISel - X86 specific code to select X86 machine instructions for
113 /// SelectionDAG operations.
114 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000115 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 /// TM - Keep a reference to X86TargetMachine.
117 ///
118 X86TargetMachine &TM;
119
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120 /// X86Lowering - This object fully describes how to lower LLVM code to an
121 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000122 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000123
124 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
125 /// make the right decision when generating code for different targets.
126 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000127
Evan Chengdb8d56b2008-06-30 20:45:06 +0000128 /// CurBB - Current BB being isel'd.
129 ///
130 MachineBasicBlock *CurBB;
131
Evan Chengb7a75a52008-09-26 23:41:32 +0000132 /// OptForSize - If true, selector should try to optimize for code size
133 /// instead of performance.
134 bool OptForSize;
135
Chris Lattnerc961eea2005-11-16 01:54:32 +0000136 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000137 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000138 : SelectionDAGISel(tm, OptLevel),
Dan Gohman38217fe2008-10-03 16:17:33 +0000139 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000140 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000141 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142
143 virtual const char *getPassName() const {
144 return "X86 DAG->DAG Instruction Selection";
145 }
146
Evan Chengdb8d56b2008-06-30 20:45:06 +0000147 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000148 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000149 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000150
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000151 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
152
Evan Cheng884c70c2008-11-27 00:49:46 +0000153 virtual
154 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000155
Chris Lattnerc961eea2005-11-16 01:54:32 +0000156// Include the pieces autogenerated from the target description.
157#include "X86GenDAGISel.inc"
158
159 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000160 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000161 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162
Rafael Espindola094fad32009-04-08 21:14:34 +0000163 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
164 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000165 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000166 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000167 unsigned Depth = 0);
168 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000169 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000170 SDValue &Scale, SDValue &Index, SDValue &Disp,
171 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000172 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
175 SDValue N, SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000177 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue &InChain, SDValue &OutChain);
179 bool TryFoldLoad(SDValue P, SDValue N,
180 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000181 SDValue &Index, SDValue &Disp,
182 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000183 void PreprocessForRMW();
184 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000185
Chris Lattnerc0bad572006-06-08 18:03:49 +0000186 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
187 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000188 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000189 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000190 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000191
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000192 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
193
Dan Gohman475871a2008-07-27 21:46:04 +0000194 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
195 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000196 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000197 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
199 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000200 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000201 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 // These are 32-bit even in 64-bit mode since RIP relative offset
203 // is 32-bit.
204 if (AM.GV)
205 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
206 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000207 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
208 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000210 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 else if (AM.JT != -1)
212 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
213 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000214 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000215
216 if (AM.Segment.getNode())
217 Segment = AM.Segment;
218 else
219 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000220 }
221
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000222 /// getI8Imm - Return a target constant with the specified value, of type
223 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000224 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000225 return CurDAG->getTargetConstant(Imm, MVT::i8);
226 }
227
Chris Lattnerc961eea2005-11-16 01:54:32 +0000228 /// getI16Imm - Return a target constant with the specified value, of type
229 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000230 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000231 return CurDAG->getTargetConstant(Imm, MVT::i16);
232 }
233
234 /// getI32Imm - Return a target constant with the specified value, of type
235 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000236 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000237 return CurDAG->getTargetConstant(Imm, MVT::i32);
238 }
Evan Chengf597dc72006-02-10 22:24:32 +0000239
Dan Gohman8b746962008-09-23 18:22:58 +0000240 /// getGlobalBaseReg - Return an SDNode that returns the value of
241 /// the global base register. Output instructions required to
242 /// initialize the global base register, if necessary.
243 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000244 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000245
Evan Cheng23addc02006-02-10 22:46:26 +0000246#ifndef NDEBUG
247 unsigned Indent;
248#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000249 };
250}
251
Evan Chengf4b4c412006-08-08 00:31:00 +0000252
Evan Cheng884c70c2008-11-27 00:49:46 +0000253bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
254 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000255 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000256
Evan Cheng884c70c2008-11-27 00:49:46 +0000257 if (U == Root)
258 switch (U->getOpcode()) {
259 default: break;
260 case ISD::ADD:
261 case ISD::ADDC:
262 case ISD::ADDE:
263 case ISD::AND:
264 case ISD::OR:
265 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000266 SDValue Op1 = U->getOperand(1);
267
Evan Cheng884c70c2008-11-27 00:49:46 +0000268 // If the other operand is a 8-bit immediate we should fold the immediate
269 // instead. This reduces code size.
270 // e.g.
271 // movl 4(%esp), %eax
272 // addl $4, %eax
273 // vs.
274 // movl $4, %eax
275 // addl 4(%esp), %eax
276 // The former is 2 bytes shorter. In case where the increment is 1, then
277 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000278 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000279 if (Imm->getAPIntValue().isSignedIntN(8))
280 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000281
282 // If the other operand is a TLS address, we should fold it instead.
283 // This produces
284 // movl %gs:0, %eax
285 // leal i@NTPOFF(%eax), %eax
286 // instead of
287 // movl $i@NTPOFF, %eax
288 // addl %gs:0, %eax
289 // if the block also has an access to a second TLS address this will save
290 // a load.
291 // FIXME: This is probably also true for non TLS addresses.
292 if (Op1.getOpcode() == X86ISD::Wrapper) {
293 SDValue Val = Op1.getOperand(0);
294 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
295 return false;
296 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000297 }
298 }
299
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000300 // Proceed to 'generic' cycle finder code
301 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000302}
303
Evan Cheng70e674e2006-08-28 20:10:17 +0000304/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
305/// and move load below the TokenFactor. Replace store's chain operand with
306/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000307static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000308 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000309 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000310 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
311 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000312 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000313 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000314 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000315 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
316 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
317 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
318 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000319}
320
Evan Chengcd0baf22008-05-23 21:23:16 +0000321/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
322///
Dan Gohman475871a2008-07-27 21:46:04 +0000323static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
324 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000325 if (N.getOpcode() == ISD::BIT_CONVERT)
326 N = N.getOperand(0);
327
328 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
329 if (!LD || LD->isVolatile())
330 return false;
331 if (LD->getAddressingMode() != ISD::UNINDEXED)
332 return false;
333
334 ISD::LoadExtType ExtType = LD->getExtensionType();
335 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
336 return false;
337
338 if (N.hasOneUse() &&
339 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000340 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000341 Load = N;
342 return true;
343 }
344 return false;
345}
346
Evan Chengab6c3bb2008-08-25 21:27:18 +0000347/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
348/// operand and move load below the call's chain operand.
349static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000350 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000351 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000352 SDValue Chain = CallSeqStart.getOperand(0);
353 if (Chain.getNode() == Load.getNode())
354 Ops.push_back(Load.getOperand(0));
355 else {
356 assert(Chain.getOpcode() == ISD::TokenFactor &&
357 "Unexpected CallSeqStart chain operand");
358 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
359 if (Chain.getOperand(i).getNode() == Load.getNode())
360 Ops.push_back(Load.getOperand(0));
361 else
362 Ops.push_back(Chain.getOperand(i));
363 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000364 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
365 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000366 Ops.clear();
367 Ops.push_back(NewChain);
368 }
369 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
370 Ops.push_back(CallSeqStart.getOperand(i));
371 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000372 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
373 Load.getOperand(1), Load.getOperand(2));
374 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000375 Ops.push_back(SDValue(Load.getNode(), 1));
376 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000377 Ops.push_back(Call.getOperand(i));
378 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
379}
380
381/// isCalleeLoad - Return true if call address is a load and it can be
382/// moved below CALLSEQ_START and the chains leading up to the call.
383/// Return the CALLSEQ_START by reference as a second output.
384static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000385 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000386 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000387 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000388 if (!LD ||
389 LD->isVolatile() ||
390 LD->getAddressingMode() != ISD::UNINDEXED ||
391 LD->getExtensionType() != ISD::NON_EXTLOAD)
392 return false;
393
394 // Now let's find the callseq_start.
395 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
396 if (!Chain.hasOneUse())
397 return false;
398 Chain = Chain.getOperand(0);
399 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000400
401 if (Chain.getOperand(0).getNode() == Callee.getNode())
402 return true;
403 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
404 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
405 return true;
406 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000407}
408
409
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000410/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000411/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000412/// This allows the instruction selector to pick more read-modify-write
413/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000414///
415/// [Load chain]
416/// ^
417/// |
418/// [Load]
419/// ^ ^
420/// | |
421/// / \-
422/// / |
423/// [TokenFactor] [Op]
424/// ^ ^
425/// | |
426/// \ /
427/// \ /
428/// [Store]
429///
430/// The fact the store's chain operand != load's chain will prevent the
431/// (store (op (load))) instruction from being selected. We can transform it to:
432///
433/// [Load chain]
434/// ^
435/// |
436/// [TokenFactor]
437/// ^
438/// |
439/// [Load]
440/// ^ ^
441/// | |
442/// | \-
443/// | |
444/// | [Op]
445/// | ^
446/// | |
447/// \ /
448/// \ /
449/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000450void X86DAGToDAGISel::PreprocessForRMW() {
451 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
452 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000453 if (I->getOpcode() == X86ISD::CALL) {
454 /// Also try moving call address load from outside callseq_start to just
455 /// before the call to allow it to be folded.
456 ///
457 /// [Load chain]
458 /// ^
459 /// |
460 /// [Load]
461 /// ^ ^
462 /// | |
463 /// / \--
464 /// / |
465 ///[CALLSEQ_START] |
466 /// ^ |
467 /// | |
468 /// [LOAD/C2Reg] |
469 /// | |
470 /// \ /
471 /// \ /
472 /// [CALL]
473 SDValue Chain = I->getOperand(0);
474 SDValue Load = I->getOperand(1);
475 if (!isCalleeLoad(Load, Chain))
476 continue;
477 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
478 ++NumLoadMoved;
479 continue;
480 }
481
Evan Cheng8b2794a2006-10-13 21:14:26 +0000482 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000483 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000485
Gabor Greifba36cb52008-08-28 21:40:38 +0000486 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000487 continue;
488
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue N1 = I->getOperand(1);
490 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000491 if ((N1.getValueType().isFloatingPoint() &&
492 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000493 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000494 continue;
495
496 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000497 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000498 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000499 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000500 case ISD::ADD:
501 case ISD::MUL:
502 case ISD::AND:
503 case ISD::OR:
504 case ISD::XOR:
505 case ISD::ADDC:
506 case ISD::ADDE:
507 case ISD::VECTOR_SHUFFLE: {
508 SDValue N10 = N1.getOperand(0);
509 SDValue N11 = N1.getOperand(1);
510 RModW = isRMWLoad(N10, Chain, N2, Load);
511 if (!RModW)
512 RModW = isRMWLoad(N11, Chain, N2, Load);
513 break;
514 }
515 case ISD::SUB:
516 case ISD::SHL:
517 case ISD::SRA:
518 case ISD::SRL:
519 case ISD::ROTL:
520 case ISD::ROTR:
521 case ISD::SUBC:
522 case ISD::SUBE:
523 case X86ISD::SHLD:
524 case X86ISD::SHRD: {
525 SDValue N10 = N1.getOperand(0);
526 RModW = isRMWLoad(N10, Chain, N2, Load);
527 break;
528 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000529 }
530
Evan Cheng82a35b32006-08-29 06:44:17 +0000531 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000532 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000533 ++NumLoadMoved;
534 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000535 }
536}
537
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000538
539/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
540/// nodes that target the FP stack to be store and load to the stack. This is a
541/// gross hack. We would like to simply mark these as being illegal, but when
542/// we do that, legalize produces these when it expands calls, then expands
543/// these in the same legalize pass. We would like dag combine to be able to
544/// hack on these between the call expansion and the node legalization. As such
545/// this pass basically does "really late" legalization of these inline with the
546/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000547void X86DAGToDAGISel::PreprocessForFPConvert() {
548 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
549 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000550 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
551 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
552 continue;
553
554 // If the source and destination are SSE registers, then this is a legal
555 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000556 MVT SrcVT = N->getOperand(0).getValueType();
557 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000558 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
559 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
560 if (SrcIsSSE && DstIsSSE)
561 continue;
562
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000563 if (!SrcIsSSE && !DstIsSSE) {
564 // If this is an FPStack extension, it is a noop.
565 if (N->getOpcode() == ISD::FP_EXTEND)
566 continue;
567 // If this is a value-preserving FPStack truncation, it is a noop.
568 if (N->getConstantOperandVal(1))
569 continue;
570 }
571
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000572 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
573 // FPStack has extload and truncstore. SSE can fold direct loads into other
574 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000575 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000576 if (N->getOpcode() == ISD::FP_ROUND)
577 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
578 else
579 MemVT = SrcIsSSE ? SrcVT : DstVT;
580
Dan Gohmanf350b272008-08-23 02:25:05 +0000581 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000582 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000583
584 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000585 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000586 N->getOperand(0),
587 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000588 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000589 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000590
591 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
592 // extload we created. This will cause general havok on the dag because
593 // anything below the conversion could be folded into other existing nodes.
594 // To avoid invalidating 'I', back it up to the convert node.
595 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000596 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000597
598 // Now that we did that, the node is dead. Increment the iterator to the
599 // next node to process, then delete N.
600 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000601 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000602 }
603}
604
Chris Lattnerc961eea2005-11-16 01:54:32 +0000605/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
606/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000607void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000608 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000609 const Function *F = CurDAG->getMachineFunction().getFunction();
610 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000611
Evan Chengdb8d56b2008-06-30 20:45:06 +0000612 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000613 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000614 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000615
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000616 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000617 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000618
Chris Lattnerc961eea2005-11-16 01:54:32 +0000619 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000620#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000621 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000622 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000623#endif
David Greene8ad4c002008-10-27 21:56:29 +0000624 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000625#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000626 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000627#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000628
Dan Gohmanf350b272008-08-23 02:25:05 +0000629 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000630}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000631
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000632/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
633/// the main function.
634void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
635 MachineFrameInfo *MFI) {
636 const TargetInstrInfo *TII = TM.getInstrInfo();
637 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000638 BuildMI(BB, DebugLoc::getUnknownLoc(),
639 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000640}
641
642void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
643 // If this is main, emit special code for main.
644 MachineBasicBlock *BB = MF.begin();
645 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
646 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
647}
648
Rafael Espindola094fad32009-04-08 21:14:34 +0000649
650bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
651 X86ISelAddressMode &AM) {
652 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
653 SDValue Segment = N.getOperand(0);
654
655 if (AM.Segment.getNode() == 0) {
656 AM.Segment = Segment;
657 return false;
658 }
659
660 return true;
661}
662
663bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
664 // This optimization is valid because the GNU TLS model defines that
665 // gs:0 (or fs:0 on X86-64) contains its own address.
666 // For more information see http://people.redhat.com/drepper/tls.pdf
667
668 SDValue Address = N.getOperand(1);
669 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
670 !MatchSegmentBaseAddress (Address, AM))
671 return false;
672
673 return true;
674}
675
Rafael Espindola49a168d2009-04-12 21:55:03 +0000676bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
677 bool is64Bit = Subtarget->is64Bit();
678 DOUT << "Wrapper: 64bit " << is64Bit;
679 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Rafael Espindolab2157762009-04-12 23:00:38 +0000680
Rafael Espindola49a168d2009-04-12 21:55:03 +0000681 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Rafael Espindolab2157762009-04-12 23:00:38 +0000682 if (is64Bit && (TM.getCodeModel() != CodeModel::Small))
Rafael Espindola49a168d2009-04-12 21:55:03 +0000683 return true;
Rafael Espindolab2157762009-04-12 23:00:38 +0000684
685 // Base and index reg must be 0 in order to use rip as base.
686 bool canUsePICRel = !AM.Base.Reg.getNode() && !AM.IndexReg.getNode();
687 if (is64Bit && !canUsePICRel && TM.symbolicAddressesAreRIPRel())
688 return true;
689
Rafael Espindola49a168d2009-04-12 21:55:03 +0000690 if (AM.hasSymbolicDisplacement())
691 return true;
692 // If value is available in a register both base and index components have
693 // been picked, we can't fit the result available in the register in the
694 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
695
696 SDValue N0 = N.getOperand(0);
697 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
698 uint64_t Offset = G->getOffset();
699 if (!is64Bit || isInt32(AM.Disp + Offset)) {
700 GlobalValue *GV = G->getGlobal();
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000701 bool isRIPRel = TM.symbolicAddressesAreRIPRel();
702 if (N0.getOpcode() == llvm::ISD::TargetGlobalTLSAddress) {
703 TLSModel::Model model =
704 getTLSModel (GV, TM.getRelocationModel());
705 if (is64Bit && model == TLSModel::InitialExec)
706 isRIPRel = true;
707 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000708 AM.GV = GV;
709 AM.Disp += Offset;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000710 AM.isRIPRel = isRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000711 return false;
712 }
713 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
714 uint64_t Offset = CP->getOffset();
715 if (!is64Bit || isInt32(AM.Disp + Offset)) {
716 AM.CP = CP->getConstVal();
717 AM.Align = CP->getAlignment();
718 AM.Disp += Offset;
719 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
720 return false;
721 }
722 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
723 AM.ES = S->getSymbol();
724 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
725 return false;
726 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
727 AM.JT = J->getIndex();
728 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
729 return false;
730 }
731
732 return true;
733}
734
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000735/// MatchAddress - Add the specified node to the specified addressing mode,
736/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000737/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000738bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000739 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000740 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000741 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000742 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000743 // Limit recursion.
744 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000745 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000746
Evan Cheng25ab6902006-09-08 06:48:29 +0000747 // RIP relative addressing: %rip + 32-bit displacement!
748 if (AM.isRIPRel) {
749 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000750 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000751 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000752 AM.Disp += Val;
753 return false;
754 }
755 }
756 return true;
757 }
758
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000759 switch (N.getOpcode()) {
760 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000761 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000762 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000763 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000764 AM.Disp += Val;
765 return false;
766 }
767 break;
768 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000769
Rafael Espindola094fad32009-04-08 21:14:34 +0000770 case X86ISD::SegmentBaseAddress:
771 if (!MatchSegmentBaseAddress(N, AM))
772 return false;
773 break;
774
Rafael Espindola49a168d2009-04-12 21:55:03 +0000775 case X86ISD::Wrapper:
776 if (!MatchWrapper(N, AM))
777 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000778 break;
779
Rafael Espindola094fad32009-04-08 21:14:34 +0000780 case ISD::LOAD:
781 if (!MatchLoad(N, AM))
782 return false;
783 break;
784
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000785 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000786 if (AM.BaseType == X86ISelAddressMode::RegBase
787 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000788 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
789 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
790 return false;
791 }
792 break;
Evan Chengec693f72005-12-08 02:01:35 +0000793
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000794 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000795 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000796 break;
797
Gabor Greif93c53e52008-08-31 15:37:04 +0000798 if (ConstantSDNode
799 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000800 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000801 if (Val == 1 || Val == 2 || Val == 3) {
802 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000803 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000804
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000805 // Okay, we know that we have a scale by now. However, if the scaled
806 // value is an add of something and a constant, we can fold the
807 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000808 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
809 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
810 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000811 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000812 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000813 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000814 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000815 AM.Disp = Disp;
816 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000817 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000818 } else {
819 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000820 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000821 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000822 }
823 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000824 }
Evan Chengec693f72005-12-08 02:01:35 +0000825
Dan Gohman83688052007-10-22 20:22:24 +0000826 case ISD::SMUL_LOHI:
827 case ISD::UMUL_LOHI:
828 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000829 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000830 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000831 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000832 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000833 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000834 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 AM.Base.Reg.getNode() == 0 &&
836 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000837 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000838 if (ConstantSDNode
839 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000840 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
841 CN->getZExtValue() == 9) {
842 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000843
Gabor Greifba36cb52008-08-28 21:40:38 +0000844 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000846
847 // Okay, we know that we have a scale by now. However, if the scaled
848 // value is an add of something and a constant, we can fold the
849 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000850 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
851 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
852 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000853 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000854 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000855 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000856 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000857 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000858 AM.Disp = Disp;
859 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000860 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000861 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863 }
864
865 AM.IndexReg = AM.Base.Reg = Reg;
866 return false;
867 }
Chris Lattner62412262007-02-04 20:18:17 +0000868 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869 break;
870
Evan Cheng8e278262009-01-17 07:09:27 +0000871 case ISD::ADD: {
872 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +0000873 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
874 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000875 return false;
876 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +0000877 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
878 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000879 return false;
880 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000881
882 // If we couldn't fold both operands into the address at the same time,
883 // see if we can just put each operand into a register and fold at least
884 // the add.
885 if (AM.BaseType == X86ISelAddressMode::RegBase &&
886 !AM.Base.Reg.getNode() &&
887 !AM.IndexReg.getNode() &&
888 !AM.isRIPRel) {
889 AM.Base.Reg = N.getNode()->getOperand(0);
890 AM.IndexReg = N.getNode()->getOperand(1);
891 AM.Scale = 1;
892 return false;
893 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000895 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000896
Chris Lattner62412262007-02-04 20:18:17 +0000897 case ISD::OR:
898 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000899 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
900 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000901 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000902 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +0000903 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000904 // Address could not have picked a GV address for the displacement.
905 AM.GV == NULL &&
906 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000907 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000908 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000909 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000910 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000911 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000912 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000913 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000914 }
915 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000916
917 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000918 // Perform some heroic transforms on an and of a constant-count shift
919 // with a constant to enable use of the scaled offset field.
920
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000922 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000923
Evan Cheng1314b002007-12-13 00:43:27 +0000924 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000926
927 // Not when RIP is used as the base.
928 if (AM.isRIPRel) break;
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000929
930 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000931 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
932 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
933 if (!C1 || !C2) break;
934
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000935 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
936 // allows us to convert the shift and and into an h-register extract and
937 // a scaled index.
938 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
939 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +0000940 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000941 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
942 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
943 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
944 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
945 X, Eight);
946 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
947 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +0000948 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
949 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
950 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000951
952 // Insert the new nodes into the topological ordering.
953 if (Eight.getNode()->getNodeId() == -1 ||
954 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
955 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
956 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
957 }
958 if (Mask.getNode()->getNodeId() == -1 ||
959 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
960 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
961 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
962 }
963 if (Srl.getNode()->getNodeId() == -1 ||
964 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
965 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
966 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
967 }
968 if (And.getNode()->getNodeId() == -1 ||
969 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
970 CurDAG->RepositionNode(N.getNode(), And.getNode());
971 And.getNode()->setNodeId(N.getNode()->getNodeId());
972 }
Dan Gohman62ad1382009-04-14 22:45:05 +0000973 if (ShlCount.getNode()->getNodeId() == -1 ||
974 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
975 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
976 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
977 }
978 if (Shl.getNode()->getNodeId() == -1 ||
979 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
980 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
981 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
982 }
983 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000984 AM.IndexReg = And;
985 AM.Scale = (1 << ScaleLog);
986 return false;
987 }
988 }
989
990 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
991 // allows us to fold the shift into this addressing mode.
992 if (Shift.getOpcode() != ISD::SHL) break;
993
Evan Cheng1314b002007-12-13 00:43:27 +0000994 // Not likely to be profitable if either the AND or SHIFT node has more
995 // than one use (unless all uses are for address computation). Besides,
996 // isel mechanism requires their node ids to be reused.
997 if (!N.hasOneUse() || !Shift.hasOneUse())
998 break;
999
1000 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001001 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001002 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1003 break;
1004
1005 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001006 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001007 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001008 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1009 NewANDMask);
1010 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001011 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001012
1013 // Insert the new nodes into the topological ordering.
1014 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1015 CurDAG->RepositionNode(X.getNode(), C1);
1016 C1->setNodeId(X.getNode()->getNodeId());
1017 }
1018 if (NewANDMask.getNode()->getNodeId() == -1 ||
1019 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1020 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1021 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1022 }
1023 if (NewAND.getNode()->getNodeId() == -1 ||
1024 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1025 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1026 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1027 }
1028 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1029 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1030 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1031 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1032 }
1033
Dan Gohman7b8e9642008-10-13 20:52:04 +00001034 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001035
1036 AM.Scale = 1 << ShiftCst;
1037 AM.IndexReg = NewAND;
1038 return false;
1039 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001040 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001041
Rafael Espindola523249f2009-03-31 16:16:57 +00001042 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001043}
1044
1045/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1046/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001047bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001048 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001049 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001050 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001051 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001052 AM.IndexReg = N;
1053 AM.Scale = 1;
1054 return false;
1055 }
1056
1057 // Otherwise, we cannot select it.
1058 return true;
1059 }
1060
1061 // Default, generate it as a register.
1062 AM.BaseType = X86ISelAddressMode::RegBase;
1063 AM.Base.Reg = N;
1064 return false;
1065}
1066
Evan Chengec693f72005-12-08 02:01:35 +00001067/// SelectAddr - returns true if it is able pattern match an addressing mode.
1068/// It returns the operands which make up the maximal addressing mode it can
1069/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001070bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1071 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001072 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001073 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001074 bool Done = false;
1075 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1076 unsigned Opcode = N.getOpcode();
1077 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1078 Opcode != X86ISD::Wrapper) {
1079 // If we are able to fold N into addressing mode, then we'll allow it even
1080 // if N has multiple uses. In general, addressing computation is used as
1081 // addresses by all of its uses. But watch out for CopyToReg uses, that
1082 // means the address computation is liveout. It will be computed by a LEA
1083 // so we want to avoid computing the address twice.
1084 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1085 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1086 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001087 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001088 Done = true;
1089 break;
1090 }
1091 }
1092 }
1093 }
1094
1095 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001096 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001097
Duncan Sands83ec4b62008-06-06 12:08:01 +00001098 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001099 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001100 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001101 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001102 }
Evan Cheng8700e142006-01-11 06:09:51 +00001103
Gabor Greifba36cb52008-08-28 21:40:38 +00001104 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001105 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001106
Rafael Espindola094fad32009-04-08 21:14:34 +00001107 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001108 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001109}
1110
Chris Lattner3a7cd952006-10-07 21:55:32 +00001111/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1112/// match a load whose top elements are either undef or zeros. The load flavor
1113/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001114bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1115 SDValue N, SDValue &Base,
1116 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001117 SDValue &Disp, SDValue &Segment,
1118 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001119 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001120 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001121 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001122 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001123 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001124 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001125 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001126 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001127 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001128 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001129 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001130 return true;
1131 }
1132 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001133
1134 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001135 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001136 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001137 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001138 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001139 N.getOperand(0).getNode()->hasOneUse() &&
1140 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001141 N.getOperand(0).getOperand(0).hasOneUse()) {
1142 // Okay, this is a zero extending load. Fold it.
1143 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001144 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001145 return false;
1146 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001147 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001148 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001149 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001150 return false;
1151}
1152
1153
Evan Cheng51a9ed92006-02-25 10:09:08 +00001154/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1155/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001156bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1157 SDValue &Base, SDValue &Scale,
1158 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001159 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001160
1161 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1162 // segments.
1163 SDValue Copy = AM.Segment;
1164 SDValue T = CurDAG->getRegister(0, MVT::i32);
1165 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001166 if (MatchAddress(N, AM))
1167 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001168 assert (T == AM.Segment);
1169 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001170
Duncan Sands83ec4b62008-06-06 12:08:01 +00001171 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001172 unsigned Complexity = 0;
1173 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001174 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001175 Complexity = 1;
1176 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001177 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001178 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1179 Complexity = 4;
1180
Gabor Greifba36cb52008-08-28 21:40:38 +00001181 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001182 Complexity++;
1183 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001184 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001185
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001186 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1187 // a simple shift.
1188 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001189 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001190
1191 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1192 // to a LEA. This is determined with some expermentation but is by no means
1193 // optimal (especially for code size consideration). LEA is nice because of
1194 // its three-address nature. Tweak the cost function again when we can run
1195 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001196 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001197 // For X86-64, we should always use lea to materialize RIP relative
1198 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001199 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001200 Complexity = 4;
1201 else
1202 Complexity += 2;
1203 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001204
Gabor Greifba36cb52008-08-28 21:40:38 +00001205 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001206 Complexity++;
1207
1208 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001209 SDValue Segment;
1210 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001211 return true;
1212 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001213 return false;
1214}
1215
Dan Gohman475871a2008-07-27 21:46:04 +00001216bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1217 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001218 SDValue &Index, SDValue &Disp,
1219 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001220 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001221 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001222 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001223 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001224 return false;
1225}
1226
Dan Gohman8b746962008-09-23 18:22:58 +00001227/// getGlobalBaseReg - Return an SDNode that returns the value of
1228/// the global base register. Output instructions required to
1229/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001230///
Evan Cheng9ade2182006-08-26 05:34:46 +00001231SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001232 MachineFunction *MF = CurBB->getParent();
1233 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001234 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001235}
1236
Evan Chengb245d922006-05-20 01:36:52 +00001237static SDNode *FindCallStartFromCall(SDNode *Node) {
1238 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1239 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1240 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001241 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001242}
1243
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001244SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1245 SDValue Chain = Node->getOperand(0);
1246 SDValue In1 = Node->getOperand(1);
1247 SDValue In2L = Node->getOperand(2);
1248 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001249 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1250 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001251 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001252 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001253 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001254 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1255 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001256 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001257}
Christopher Lambc59e5212007-08-10 21:48:46 +00001258
Dan Gohman475871a2008-07-27 21:46:04 +00001259SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001261 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001262 unsigned Opc, MOpc;
1263 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001264 DebugLoc dl = Node->getDebugLoc();
1265
Evan Chengf597dc72006-02-10 22:24:32 +00001266#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001267 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001268 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001269 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001270 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001271#endif
1272
Dan Gohmane8be6c62008-07-17 19:10:17 +00001273 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001274#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001275 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001276 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001277 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001278 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001279#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001280 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001281 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001282
Evan Cheng0114e942006-01-06 20:36:21 +00001283 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001284 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001285 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001286 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001287
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001288 case X86ISD::ATOMOR64_DAG:
1289 return SelectAtomic64(Node, X86::ATOMOR6432);
1290 case X86ISD::ATOMXOR64_DAG:
1291 return SelectAtomic64(Node, X86::ATOMXOR6432);
1292 case X86ISD::ATOMADD64_DAG:
1293 return SelectAtomic64(Node, X86::ATOMADD6432);
1294 case X86ISD::ATOMSUB64_DAG:
1295 return SelectAtomic64(Node, X86::ATOMSUB6432);
1296 case X86ISD::ATOMNAND64_DAG:
1297 return SelectAtomic64(Node, X86::ATOMNAND6432);
1298 case X86ISD::ATOMAND64_DAG:
1299 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001300 case X86ISD::ATOMSWAP64_DAG:
1301 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001302
Dan Gohman525178c2007-10-08 18:33:35 +00001303 case ISD::SMUL_LOHI:
1304 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue N0 = Node->getOperand(0);
1306 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001307
Dan Gohman525178c2007-10-08 18:33:35 +00001308 bool isSigned = Opcode == ISD::SMUL_LOHI;
1309 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001310 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001311 default: assert(0 && "Unsupported VT!");
1312 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1313 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1314 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001315 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001316 }
1317 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001318 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001319 default: assert(0 && "Unsupported VT!");
1320 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1321 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1322 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001323 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001324 }
1325
1326 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001327 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001328 default: assert(0 && "Unsupported VT!");
1329 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1330 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1331 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001332 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001333 }
1334
Rafael Espindola094fad32009-04-08 21:14:34 +00001335 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1336 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001337 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001338 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001339 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001340 if (foldedLoad)
1341 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001342 }
1343
Dale Johannesendd64c412009-02-04 00:33:20 +00001344 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001345 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001346
1347 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001348 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1349 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001350 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001351 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001352 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001353 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001354 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001355 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001356 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001357 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001358 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001359 }
1360
Dan Gohman525178c2007-10-08 18:33:35 +00001361 // Copy the low half of the result, if it is needed.
1362 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001363 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001364 LoReg, NVT, InFlag);
1365 InFlag = Result.getValue(2);
1366 ReplaceUses(N.getValue(0), Result);
1367#ifndef NDEBUG
1368 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001370 DOUT << "\n";
1371#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001372 }
Dan Gohman525178c2007-10-08 18:33:35 +00001373 // Copy the high half of the result, if it is needed.
1374 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001376 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1377 // Prevent use of AH in a REX instruction by referencing AX instead.
1378 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001379 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001380 X86::AX, MVT::i16, InFlag);
1381 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001382 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1383 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001384 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001385 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001386 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001387 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001388 MVT::i8, Result, SRIdx), 0);
1389 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001390 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001391 HiReg, NVT, InFlag);
1392 InFlag = Result.getValue(2);
1393 }
1394 ReplaceUses(N.getValue(1), Result);
1395#ifndef NDEBUG
1396 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001397 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001398 DOUT << "\n";
1399#endif
1400 }
Evan Cheng34167212006-02-09 00:37:58 +00001401
Evan Chengf597dc72006-02-10 22:24:32 +00001402#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001403 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001404#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001405
Evan Cheng64a752f2006-08-11 09:08:15 +00001406 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001407 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001408
Dan Gohman525178c2007-10-08 18:33:35 +00001409 case ISD::SDIVREM:
1410 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue N0 = Node->getOperand(0);
1412 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001413
1414 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001415 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001416 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001417 default: assert(0 && "Unsupported VT!");
1418 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1419 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1420 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001421 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001422 }
1423 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001424 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001425 default: assert(0 && "Unsupported VT!");
1426 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1427 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1428 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001429 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001430 }
1431
1432 unsigned LoReg, HiReg;
1433 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001434 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001435 default: assert(0 && "Unsupported VT!");
1436 case MVT::i8:
1437 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001438 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001439 SExtOpcode = X86::CBW;
1440 break;
1441 case MVT::i16:
1442 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001443 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001444 SExtOpcode = X86::CWD;
1445 break;
1446 case MVT::i32:
1447 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001448 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001449 SExtOpcode = X86::CDQ;
1450 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001451 case MVT::i64:
1452 LoReg = X86::RAX; HiReg = X86::RDX;
1453 ClrOpcode = X86::MOV64r0;
1454 SExtOpcode = X86::CQO;
1455 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001456 }
1457
Rafael Espindola094fad32009-04-08 21:14:34 +00001458 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1459 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001460 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001461
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001463 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001464 // Special case for div8, just use a move with zero extension to AX to
1465 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001466 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1467 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1468 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001469 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001470 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001471 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001472 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001473 Chain = Move.getValue(1);
1474 ReplaceUses(N0.getValue(1), Chain);
1475 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001476 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001477 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001478 Chain = CurDAG->getEntryNode();
1479 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001480 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001481 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001482 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001483 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001484 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001485 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001486 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001487 // Sign extend the low part into the high part.
1488 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001489 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001490 } else {
1491 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001492 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1493 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001494 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001495 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001496 }
Evan Cheng948f3432006-01-06 23:19:29 +00001497 }
1498
1499 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001500 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1501 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001502 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001503 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001504 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001505 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001506 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001507 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001508 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001509 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001510 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001511 }
1512
Dan Gohmana37c9f72007-09-25 18:23:27 +00001513 // Copy the division (low) result, if it is needed.
1514 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001516 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001517 InFlag = Result.getValue(2);
1518 ReplaceUses(N.getValue(0), Result);
1519#ifndef NDEBUG
1520 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001521 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001522 DOUT << "\n";
1523#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001524 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001525 // Copy the remainder (high) result, if it is needed.
1526 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001528 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1529 // Prevent use of AH in a REX instruction by referencing AX instead.
1530 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001531 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001532 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001533 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001534 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1535 Result,
1536 CurDAG->getTargetConstant(8, MVT::i8)),
1537 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001538 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001539 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001540 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001541 MVT::i8, Result, SRIdx), 0);
1542 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001543 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001544 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001545 InFlag = Result.getValue(2);
1546 }
1547 ReplaceUses(N.getValue(1), Result);
1548#ifndef NDEBUG
1549 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001550 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001551 DOUT << "\n";
1552#endif
1553 }
Evan Chengf597dc72006-02-10 22:24:32 +00001554
1555#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001556 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001557#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001558
1559 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001560 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001561
Evan Cheng851bc042008-06-17 02:01:22 +00001562 case ISD::DECLARE: {
1563 // Handle DECLARE nodes here because the second operand may have been
1564 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001565 SDValue Chain = Node->getOperand(0);
1566 SDValue N1 = Node->getOperand(1);
1567 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001568 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001569
1570 // FIXME: We need to handle this for VLAs.
1571 if (!FINode) {
1572 ReplaceUses(N.getValue(0), Chain);
1573 return NULL;
1574 }
1575
Evan Chengfab83872008-06-18 02:48:27 +00001576 if (N2.getOpcode() == ISD::ADD &&
1577 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1578 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001579
1580 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1581 // somehow, just ignore it.
1582 if (N2.getOpcode() != X86ISD::Wrapper) {
1583 ReplaceUses(N.getValue(0), Chain);
1584 return NULL;
1585 }
Evan Chengf2accb52009-01-10 03:33:22 +00001586 GlobalAddressSDNode *GVNode =
1587 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001588 if (GVNode == 0) {
1589 ReplaceUses(N.getValue(0), Chain);
1590 return NULL;
1591 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001592 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1593 TLI.getPointerTy());
1594 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1595 TLI.getPointerTy());
1596 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001597 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001598 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001599 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001600 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001601 }
1602
Evan Cheng9ade2182006-08-26 05:34:46 +00001603 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001604
Evan Chengf597dc72006-02-10 22:24:32 +00001605#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001606 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001607 if (ResNode == NULL || ResNode == N.getNode())
1608 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001609 else
1610 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001611 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001612 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001613#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001614
1615 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001616}
1617
Chris Lattnerc0bad572006-06-08 18:03:49 +00001618bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001619SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001620 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001621 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001622 switch (ConstraintCode) {
1623 case 'o': // offsetable ??
1624 case 'v': // not offsetable ??
1625 default: return true;
1626 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001627 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001628 return true;
1629 break;
1630 }
1631
Evan Cheng04699902006-08-26 01:05:16 +00001632 OutOps.push_back(Op0);
1633 OutOps.push_back(Op1);
1634 OutOps.push_back(Op2);
1635 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001636 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001637 return false;
1638}
1639
Chris Lattnerc961eea2005-11-16 01:54:32 +00001640/// createX86ISelDag - This pass converts a legalized DAG into a
1641/// X86-specific DAG, ready for instruction scheduling.
1642///
Bill Wendling98a366d2009-04-29 23:29:43 +00001643FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1644 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001645 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001646}