blob: d43ac19e3f3fadfa4d391befe2687d6a33492791 [file] [log] [blame]
Chris Lattner5930d3d2005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattner655e7df2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Chengb9d34bd2006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattner655e7df2005-11-16 01:54:32 +000016#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner7c551262006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner7c551262006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Reid Spencer015b4322007-01-12 23:22:14 +000026#include "llvm/Type.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000028#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/Target/TargetMachine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/MathExtras.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000037#include "llvm/ADT/Statistic.h"
Evan Chengb9d34bd2006-08-07 22:28:20 +000038#include <queue>
Evan Cheng54cb1832006-02-05 06:46:41 +000039#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000040using namespace llvm;
41
Chris Lattner1ef9cd42006-12-19 22:59:26 +000042STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
43STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
44
45
Chris Lattner655e7df2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
53 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
61 SDOperand Reg;
62 int FrameIndex;
63 } Base;
64
Evan Cheng11b0a5d2006-09-08 06:48:29 +000065 bool isRIPRel; // RIP relative?
Chris Lattner3f0f71b2005-11-19 02:11:08 +000066 unsigned Scale;
67 SDOperand IndexReg;
68 unsigned Disp;
69 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000070 Constant *CP;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000071 const char *ES;
72 int JT;
Evan Cheng77d86ff2006-02-25 10:09:08 +000073 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Evan Cheng11b0a5d2006-09-08 06:48:29 +000076 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000078 }
79 };
80}
81
82namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000083 //===--------------------------------------------------------------------===//
84 /// ISel - X86 specific code to select X86 machine instructions for
85 /// SelectionDAG operations.
86 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000087 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000088 /// ContainsFPCode - Every instruction we select that uses or defines a FP
89 /// register should set this to true.
90 bool ContainsFPCode;
91
Evan Cheng358b9ed2006-08-29 18:28:33 +000092 /// FastISel - Enable fast(er) instruction selection.
93 ///
94 bool FastISel;
95
Evan Cheng11b0a5d2006-09-08 06:48:29 +000096 /// TM - Keep a reference to X86TargetMachine.
97 ///
98 X86TargetMachine &TM;
99
Chris Lattner655e7df2005-11-16 01:54:32 +0000100 /// X86Lowering - This object fully describes how to lower LLVM code to an
101 /// X86-specific SelectionDAG.
102 X86TargetLowering X86Lowering;
103
104 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
105 /// make the right decision when generating code for different targets.
106 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +0000107
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000108 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
109 /// base register.
Evan Cheng5588de92006-02-18 00:15:05 +0000110 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000111
Chris Lattner655e7df2005-11-16 01:54:32 +0000112 public:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000113 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng2dd2c652006-03-13 23:20:37 +0000114 : SelectionDAGISel(X86Lowering),
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000115 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Cheng691a63d2006-07-27 16:44:36 +0000116 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000117 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000118
Evan Cheng5588de92006-02-18 00:15:05 +0000119 virtual bool runOnFunction(Function &Fn) {
120 // Make sure we re-emit a set of the global base reg if necessary
121 GlobalBaseReg = 0;
122 return SelectionDAGISel::runOnFunction(Fn);
123 }
124
Chris Lattner655e7df2005-11-16 01:54:32 +0000125 virtual const char *getPassName() const {
126 return "X86 DAG->DAG Instruction Selection";
127 }
128
129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
Anton Korobeynikov90910742007-09-25 21:52:30 +0000133 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
134
Dan Gohmanf0bb1282007-07-24 23:00:27 +0000135 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Cheng691a63d2006-07-27 16:44:36 +0000136
Chris Lattner655e7df2005-11-16 01:54:32 +0000137// Include the pieces autogenerated from the target description.
138#include "X86GenDAGISel.inc"
139
140 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000141 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000142
Anton Korobeynikov7522c9d2007-03-28 18:36:33 +0000143 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov0ad22562007-03-28 18:38:33 +0000144 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanccb36112007-08-13 20:03:06 +0000145 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
146 bool isRoot, unsigned Depth);
Evan Cheng6cd09092006-11-08 20:34:28 +0000147 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
150 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
151 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Chengff1a7122006-10-16 06:34:55 +0000152 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng4090dc42006-10-11 21:06:01 +0000153 SDOperand &Index, SDOperand &Disp,
154 SDOperand &InChain, SDOperand &OutChain);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000155 bool TryFoldLoad(SDOperand P, SDOperand N,
156 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000157 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000158 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000159
Chris Lattnerba1ed582006-06-08 18:03:49 +0000160 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
161 /// inline asm expressions.
162 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
163 char ConstraintCode,
164 std::vector<SDOperand> &OutOps,
165 SelectionDAG &DAG);
166
Anton Korobeynikov90910742007-09-25 21:52:30 +0000167 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
168
Evan Cheng67ed58e2005-12-12 21:49:40 +0000169 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
170 SDOperand &Scale, SDOperand &Index,
171 SDOperand &Disp) {
172 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000173 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
174 AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000175 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000176 Index = AM.IndexReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000177 // These are 32-bit even in 64-bit mode since RIP relative offset
178 // is 32-bit.
179 if (AM.GV)
180 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
181 else if (AM.CP)
182 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
183 else if (AM.ES)
184 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
185 else if (AM.JT != -1)
186 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
187 else
188 Disp = getI32Imm(AM.Disp);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000189 }
190
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000191 /// getI8Imm - Return a target constant with the specified value, of type
192 /// i8.
193 inline SDOperand getI8Imm(unsigned Imm) {
194 return CurDAG->getTargetConstant(Imm, MVT::i8);
195 }
196
Chris Lattner655e7df2005-11-16 01:54:32 +0000197 /// getI16Imm - Return a target constant with the specified value, of type
198 /// i16.
199 inline SDOperand getI16Imm(unsigned Imm) {
200 return CurDAG->getTargetConstant(Imm, MVT::i16);
201 }
202
203 /// getI32Imm - Return a target constant with the specified value, of type
204 /// i32.
205 inline SDOperand getI32Imm(unsigned Imm) {
206 return CurDAG->getTargetConstant(Imm, MVT::i32);
207 }
Evan Chengd49cc362006-02-10 22:24:32 +0000208
Evan Cheng5588de92006-02-18 00:15:05 +0000209 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
210 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000211 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000212
Christopher Lambb372aba2007-08-10 21:48:46 +0000213 /// getTruncate - return an SDNode that implements a subreg based truncate
214 /// of the specified operand to the the specified value type.
215 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
216
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000217#ifndef NDEBUG
218 unsigned Indent;
219#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000220 };
221}
222
Evan Cheng61b8b432006-10-10 01:46:56 +0000223static SDNode *findFlagUse(SDNode *N) {
224 unsigned FlagResNo = N->getNumValues()-1;
225 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
226 SDNode *User = *I;
227 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
228 SDOperand Op = User->getOperand(i);
Evan Chenga7956d22006-10-12 19:13:59 +0000229 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Cheng61b8b432006-10-10 01:46:56 +0000230 return User;
231 }
232 }
233 return NULL;
234}
235
Evan Chengb86375c2006-10-14 08:33:25 +0000236static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
237 SDNode *Root, SDNode *Skip, bool &found,
Evan Cheng72bb66a2006-08-08 00:31:00 +0000238 std::set<SDNode *> &Visited) {
239 if (found ||
240 Use->getNodeId() > Def->getNodeId() ||
241 !Visited.insert(Use).second)
242 return;
243
Evan Chengb86375c2006-10-14 08:33:25 +0000244 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Cheng72bb66a2006-08-08 00:31:00 +0000245 SDNode *N = Use->getOperand(i).Val;
Evan Chengb86375c2006-10-14 08:33:25 +0000246 if (N == Skip)
Evan Cheng61b8b432006-10-10 01:46:56 +0000247 continue;
Evan Chengb86375c2006-10-14 08:33:25 +0000248 if (N == Def) {
249 if (Use == ImmedUse)
250 continue; // Immediate use is ok.
251 if (Use == Root) {
252 assert(Use->getOpcode() == ISD::STORE ||
253 Use->getOpcode() == X86ISD::CMP);
254 continue;
255 }
Evan Cheng72bb66a2006-08-08 00:31:00 +0000256 found = true;
257 break;
258 }
Evan Chengb86375c2006-10-14 08:33:25 +0000259 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Cheng72bb66a2006-08-08 00:31:00 +0000260 }
261}
262
Evan Chengb86375c2006-10-14 08:33:25 +0000263/// isNonImmUse - Start searching from Root up the DAG to check is Def can
264/// be reached. Return true if that's the case. However, ignore direct uses
265/// by ImmedUse (which would be U in the example illustrated in
266/// CanBeFoldedBy) and by Root (which can happen in the store case).
267/// FIXME: to be really generic, we should allow direct use by any node
268/// that is being folded. But realisticly since we only fold loads which
269/// have one non-chain use, we only need to watch out for load/op/store
270/// and load/op/cmp case where the root (store / cmp) may reach the load via
271/// its chain operand.
272static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
273 SDNode *Skip = NULL) {
Evan Cheng72bb66a2006-08-08 00:31:00 +0000274 std::set<SDNode *> Visited;
275 bool found = false;
Evan Chengb86375c2006-10-14 08:33:25 +0000276 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Cheng72bb66a2006-08-08 00:31:00 +0000277 return found;
278}
279
280
Dan Gohmanf0bb1282007-07-24 23:00:27 +0000281bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Chengb86375c2006-10-14 08:33:25 +0000282 if (FastISel) return false;
283
Evan Cheng691a63d2006-07-27 16:44:36 +0000284 // If U use can somehow reach N through another path then U can't fold N or
285 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000286 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000287 // a successor of U.
288 //
289 // [ N ]
290 // ^ ^
291 // | |
292 // / \---
293 // / [X]
294 // | ^
295 // [U]--------|
Evan Chengb86375c2006-10-14 08:33:25 +0000296
297 if (isNonImmUse(Root, N, U))
298 return false;
299
300 // If U produces a flag, then it gets (even more) interesting. Since it
301 // would have been "glued" together with its flag use, we need to check if
302 // it might reach N:
303 //
304 // [ N ]
305 // ^ ^
306 // | |
307 // [U] \--
308 // ^ [TF]
309 // | ^
310 // | |
311 // \ /
312 // [FU]
313 //
314 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
315 // NU), then TF is a predecessor of FU and a successor of NU. But since
316 // NU and FU are flagged together, this effectively creates a cycle.
317 bool HasFlagUse = false;
318 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
319 while ((VT == MVT::Flag && !Root->use_empty())) {
320 SDNode *FU = findFlagUse(Root);
321 if (FU == NULL)
322 break;
323 else {
324 Root = FU;
325 HasFlagUse = true;
Evan Cheng61b8b432006-10-10 01:46:56 +0000326 }
Evan Chengb86375c2006-10-14 08:33:25 +0000327 VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng61b8b432006-10-10 01:46:56 +0000328 }
Evan Chengb86375c2006-10-14 08:33:25 +0000329
330 if (HasFlagUse)
331 return !isNonImmUse(Root, N, Root, U);
332 return true;
Evan Cheng691a63d2006-07-27 16:44:36 +0000333}
334
Evan Cheng64a9e282006-08-28 20:10:17 +0000335/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
336/// and move load below the TokenFactor. Replace store's chain operand with
337/// load's chain result.
338static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
339 SDOperand Store, SDOperand TF) {
340 std::vector<SDOperand> Ops;
341 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
342 if (Load.Val == TF.Val->getOperand(i).Val)
343 Ops.push_back(Load.Val->getOperand(0));
344 else
345 Ops.push_back(TF.Val->getOperand(i));
346 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
347 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
348 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
349 Store.getOperand(2), Store.getOperand(3));
350}
351
352/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
353/// selector to pick more load-modify-store instructions. This is a common
354/// case:
355///
356/// [Load chain]
357/// ^
358/// |
359/// [Load]
360/// ^ ^
361/// | |
362/// / \-
363/// / |
364/// [TokenFactor] [Op]
365/// ^ ^
366/// | |
367/// \ /
368/// \ /
369/// [Store]
370///
371/// The fact the store's chain operand != load's chain will prevent the
372/// (store (op (load))) instruction from being selected. We can transform it to:
373///
374/// [Load chain]
375/// ^
376/// |
377/// [TokenFactor]
378/// ^
379/// |
380/// [Load]
381/// ^ ^
382/// | |
383/// | \-
384/// | |
385/// | [Op]
386/// | ^
387/// | |
388/// \ /
389/// \ /
390/// [Store]
391void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
392 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
393 E = DAG.allnodes_end(); I != E; ++I) {
Evan Chengab51cf22006-10-13 21:14:26 +0000394 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng64a9e282006-08-28 20:10:17 +0000395 continue;
396 SDOperand Chain = I->getOperand(0);
397 if (Chain.Val->getOpcode() != ISD::TokenFactor)
398 continue;
399
400 SDOperand N1 = I->getOperand(1);
401 SDOperand N2 = I->getOperand(2);
Evan Cheng2c4e0f12006-09-01 22:52:28 +0000402 if (MVT::isFloatingPoint(N1.getValueType()) ||
403 MVT::isVector(N1.getValueType()) ||
Evan Chengdfb85152006-08-29 18:37:37 +0000404 !N1.hasOneUse())
Evan Cheng64a9e282006-08-28 20:10:17 +0000405 continue;
406
407 bool RModW = false;
408 SDOperand Load;
409 unsigned Opcode = N1.Val->getOpcode();
410 switch (Opcode) {
411 case ISD::ADD:
412 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000413 case ISD::AND:
414 case ISD::OR:
415 case ISD::XOR:
416 case ISD::ADDC:
417 case ISD::ADDE: {
418 SDOperand N10 = N1.getOperand(0);
419 SDOperand N11 = N1.getOperand(1);
Evan Chenge71fe34d2006-10-09 20:57:25 +0000420 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng64a9e282006-08-28 20:10:17 +0000421 RModW = true;
Evan Chenge71fe34d2006-10-09 20:57:25 +0000422 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000423 RModW = true;
424 std::swap(N10, N11);
425 }
426 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000427 (N10.getOperand(1) == N2) &&
428 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000429 if (RModW)
430 Load = N10;
431 break;
432 }
433 case ISD::SUB:
434 case ISD::SHL:
435 case ISD::SRA:
436 case ISD::SRL:
437 case ISD::ROTL:
438 case ISD::ROTR:
439 case ISD::SUBC:
440 case ISD::SUBE:
441 case X86ISD::SHLD:
442 case X86ISD::SHRD: {
443 SDOperand N10 = N1.getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +0000444 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng64a9e282006-08-28 20:10:17 +0000445 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000446 (N10.getOperand(1) == N2) &&
447 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000448 if (RModW)
449 Load = N10;
450 break;
451 }
452 }
453
Evan Chengc07feb142006-08-29 06:44:17 +0000454 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000455 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000456 ++NumLoadMoved;
457 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000458 }
459}
460
Chris Lattner655e7df2005-11-16 01:54:32 +0000461/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
462/// when it has created a SelectionDAG for us to codegen.
463void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
464 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000465 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000466
Evan Cheng358b9ed2006-08-29 18:28:33 +0000467 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000468 InstructionSelectPreprocess(DAG);
469
Chris Lattner655e7df2005-11-16 01:54:32 +0000470 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000471#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +0000472 DOUT << "===== Instruction selection begins:\n";
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000473 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000474#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000475 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000476#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +0000477 DOUT << "===== Instruction selection ends:\n";
Evan Chengd49cc362006-02-10 22:24:32 +0000478#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000479
Chris Lattner655e7df2005-11-16 01:54:32 +0000480 DAG.RemoveDeadNodes();
481
482 // Emit machine code to BB.
483 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000484
485 // If we are emitting FP stack code, scan the basic block to determine if this
486 // block defines any FP values. If so, put an FP_REG_KILL instruction before
487 // the terminator of the block.
Dale Johannesena47f7d72007-08-07 20:29:26 +0000488
Dale Johannesen0241bb52007-09-24 22:52:39 +0000489 // Note that FP stack instructions are used in all modes for long double,
490 // so we always need to do this check.
491 // Also note that it's possible for an FP stack register to be live across
492 // an instruction that produces multiple basic blocks (SSE CMOV) so we
493 // must check all the generated basic blocks.
Dale Johannesena47f7d72007-08-07 20:29:26 +0000494
495 // Scan all of the machine instructions in these MBBs, checking for FP
496 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
497 MachineFunction::iterator MBBI = FirstMBB;
498 do {
Dale Johannesen0241bb52007-09-24 22:52:39 +0000499 bool ContainsFPCode = false;
Dale Johannesena47f7d72007-08-07 20:29:26 +0000500 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
501 !ContainsFPCode && I != E; ++I) {
502 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
503 const TargetRegisterClass *clas;
504 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
505 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
506 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
507 ((clas = RegMap->getRegClass(I->getOperand(0).getReg())) ==
508 X86::RFP32RegisterClass ||
509 clas == X86::RFP64RegisterClass ||
510 clas == X86::RFP80RegisterClass)) {
Chris Lattner7c551262006-01-11 01:15:34 +0000511 ContainsFPCode = true;
512 break;
513 }
514 }
515 }
516 }
Dale Johannesen0241bb52007-09-24 22:52:39 +0000517 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
518 // a copy of the input value in this block. In SSE mode, we only care about
519 // 80-bit values.
520 if (!ContainsFPCode) {
521 // Final check, check LLVM BB's that are successors to the LLVM BB
522 // corresponding to BB for FP PHI nodes.
523 const BasicBlock *LLVMBB = BB->getBasicBlock();
524 const PHINode *PN;
525 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
526 !ContainsFPCode && SI != E; ++SI) {
527 for (BasicBlock::const_iterator II = SI->begin();
528 (PN = dyn_cast<PHINode>(II)); ++II) {
529 if (PN->getType()==Type::X86_FP80Ty ||
530 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
531 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
532 ContainsFPCode = true;
533 break;
534 }
Dale Johannesena47f7d72007-08-07 20:29:26 +0000535 }
536 }
Chris Lattner7c551262006-01-11 01:15:34 +0000537 }
Dale Johannesen0241bb52007-09-24 22:52:39 +0000538 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
539 if (ContainsFPCode) {
540 BuildMI(*MBBI, MBBI->getFirstTerminator(),
541 TM.getInstrInfo()->get(X86::FP_REG_KILL));
542 ++NumFPKill;
543 }
544 } while (&*(MBBI++) != BB);
Chris Lattner655e7df2005-11-16 01:54:32 +0000545}
546
Anton Korobeynikov90910742007-09-25 21:52:30 +0000547/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
548/// the main function.
549void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
550 MachineFrameInfo *MFI) {
551 const TargetInstrInfo *TII = TM.getInstrInfo();
552 if (Subtarget->isTargetCygMing())
553 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
554}
555
556void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
557 // If this is main, emit special code for main.
558 MachineBasicBlock *BB = MF.begin();
559 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
560 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
561}
562
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000563/// MatchAddress - Add the specified node to the specified addressing mode,
564/// returning true if it cannot be done. This just pattern matches for the
565/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000566bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov7522c9d2007-03-28 18:36:33 +0000567 bool isRoot, unsigned Depth) {
Dan Gohmanccb36112007-08-13 20:03:06 +0000568 // Limit recursion.
569 if (Depth > 5)
570 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov7522c9d2007-03-28 18:36:33 +0000571
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000572 // RIP relative addressing: %rip + 32-bit displacement!
573 if (AM.isRIPRel) {
574 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000575 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000576 if (isInt32(AM.Disp + Val)) {
577 AM.Disp += Val;
578 return false;
579 }
580 }
581 return true;
582 }
583
Evan Chengb9d34bd2006-08-07 22:28:20 +0000584 int id = N.Val->getNodeId();
585 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000586
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000587 switch (N.getOpcode()) {
588 default: break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000589 case ISD::Constant: {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000590 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000591 if (isInt32(AM.Disp + Val)) {
592 AM.Disp += Val;
593 return false;
594 }
595 break;
596 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000597
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000598 case X86ISD::Wrapper: {
599 bool is64Bit = Subtarget->is64Bit();
Evan Chengae1cd752006-11-30 21:55:46 +0000600 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000601 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
Evan Chengae1cd752006-11-30 21:55:46 +0000602 break;
Evan Chengdd60ca02006-12-05 19:50:18 +0000603 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
604 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000605 // If value is available in a register both base and index components have
606 // been picked, we can't fit the result available in the register in the
607 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng8c84c7c2006-11-29 23:46:27 +0000608 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Chengdd60ca02006-12-05 19:50:18 +0000609 bool isStatic = TM.getRelocationModel() == Reloc::Static;
610 SDOperand N0 = N.getOperand(0);
Evan Chengce5185b2007-07-26 07:35:15 +0000611 // Mac OS X X86-64 lower 4G address is not available.
Evan Cheng763cdfd2007-08-01 23:45:51 +0000612 bool isAbs32 = !is64Bit ||
613 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
Evan Chengdd60ca02006-12-05 19:50:18 +0000614 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
615 GlobalValue *GV = G->getGlobal();
Evan Chengdd60ca02006-12-05 19:50:18 +0000616 if (isAbs32 || isRoot) {
Evan Cheng582ac4b2006-12-19 21:31:42 +0000617 AM.GV = GV;
Evan Chengdd60ca02006-12-05 19:50:18 +0000618 AM.Disp += G->getOffset();
619 AM.isRIPRel = !isAbs32;
620 return false;
621 }
622 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengce5185b2007-07-26 07:35:15 +0000623 if (isAbs32 || isRoot) {
Evan Cheng9a083a42006-09-12 21:04:05 +0000624 AM.CP = CP->getConstVal();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000625 AM.Align = CP->getAlignment();
626 AM.Disp += CP->getOffset();
Evan Chengca6e0412007-07-26 17:02:45 +0000627 AM.isRIPRel = !isAbs32;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000628 return false;
629 }
Evan Chengdd60ca02006-12-05 19:50:18 +0000630 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengce5185b2007-07-26 07:35:15 +0000631 if (isAbs32 || isRoot) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000632 AM.ES = S->getSymbol();
Evan Chengca6e0412007-07-26 17:02:45 +0000633 AM.isRIPRel = !isAbs32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000634 return false;
Evan Chengdd60ca02006-12-05 19:50:18 +0000635 }
636 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengce5185b2007-07-26 07:35:15 +0000637 if (isAbs32 || isRoot) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000638 AM.JT = J->getIndex();
Evan Chengca6e0412007-07-26 17:02:45 +0000639 AM.isRIPRel = !isAbs32;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000640 return false;
641 }
642 }
643 }
644 break;
Evan Chengae1cd752006-11-30 21:55:46 +0000645 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000646
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000647 case ISD::FrameIndex:
648 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
649 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
650 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
651 return false;
652 }
653 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000654
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000655 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000656 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000657 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
658 unsigned Val = CN->getValue();
659 if (Val == 1 || Val == 2 || Val == 3) {
660 AM.Scale = 1 << Val;
661 SDOperand ShVal = N.Val->getOperand(0);
662
663 // Okay, we know that we have a scale by now. However, if the scaled
664 // value is an add of something and a constant, we can fold the
665 // constant into the disp field here.
666 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
667 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
668 AM.IndexReg = ShVal.Val->getOperand(0);
669 ConstantSDNode *AddVal =
670 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Jeff Cohen7d6f3db2006-11-05 19:31:28 +0000671 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000672 if (isInt32(Disp))
673 AM.Disp = Disp;
674 else
675 AM.IndexReg = ShVal;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000676 } else {
677 AM.IndexReg = ShVal;
678 }
679 return false;
680 }
681 }
682 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000683
Dan Gohmanbf474952007-10-22 20:22:24 +0000684 case ISD::SMUL_LOHI:
685 case ISD::UMUL_LOHI:
686 // A mul_lohi where we need the low part can be folded as a plain multiply.
687 if (N.ResNo != 0) break;
688 // FALL THROUGH
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000689 case ISD::MUL:
690 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000691 if (!Available &&
692 AM.BaseType == X86ISelAddressMode::RegBase &&
693 AM.Base.Reg.Val == 0 &&
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000694 AM.IndexReg.Val == 0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000695 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
696 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
697 AM.Scale = unsigned(CN->getValue())-1;
698
699 SDOperand MulVal = N.Val->getOperand(0);
700 SDOperand Reg;
701
702 // Okay, we know that we have a scale by now. However, if the scaled
703 // value is an add of something and a constant, we can fold the
704 // constant into the disp field here.
705 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
706 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
707 Reg = MulVal.Val->getOperand(0);
708 ConstantSDNode *AddVal =
709 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000710 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
711 if (isInt32(Disp))
712 AM.Disp = Disp;
713 else
714 Reg = N.Val->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000715 } else {
716 Reg = N.Val->getOperand(0);
717 }
718
719 AM.IndexReg = AM.Base.Reg = Reg;
720 return false;
721 }
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000722 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000723 break;
724
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000725 case ISD::ADD:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000726 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000727 X86ISelAddressMode Backup = AM;
Anton Korobeynikov7522c9d2007-03-28 18:36:33 +0000728 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
729 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Chenga86ba852006-02-11 02:05:36 +0000730 return false;
731 AM = Backup;
Anton Korobeynikov7522c9d2007-03-28 18:36:33 +0000732 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
733 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Chenga86ba852006-02-11 02:05:36 +0000734 return false;
735 AM = Backup;
736 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000737 break;
Evan Cheng734e1e22006-05-30 06:59:36 +0000738
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000739 case ISD::OR:
740 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng734e1e22006-05-30 06:59:36 +0000741 if (!Available) {
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000742 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
743 X86ISelAddressMode Backup = AM;
744 // Start with the LHS as an addr mode.
745 if (!MatchAddress(N.getOperand(0), AM, false) &&
746 // Address could not have picked a GV address for the displacement.
747 AM.GV == NULL &&
748 // On x86-64, the resultant disp must fit in 32-bits.
749 isInt32(AM.Disp + CN->getSignExtended()) &&
750 // Check to see if the LHS & C is zero.
Dan Gohman309d3d52007-06-22 14:59:07 +0000751 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000752 AM.Disp += CN->getValue();
Evan Cheng734e1e22006-05-30 06:59:36 +0000753 return false;
754 }
Chris Lattnerfe8c5302007-02-04 20:18:17 +0000755 AM = Backup;
Evan Cheng734e1e22006-05-30 06:59:36 +0000756 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000757 }
758 break;
759 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000760
Dan Gohmanccb36112007-08-13 20:03:06 +0000761 return MatchAddressBase(N, AM, isRoot, Depth);
762}
763
764/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
765/// specified addressing mode without any further recursion.
766bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
767 bool isRoot, unsigned Depth) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000768 // Is the base register already occupied?
769 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
770 // If so, check to see if the scale index register is set.
771 if (AM.IndexReg.Val == 0) {
772 AM.IndexReg = N;
773 AM.Scale = 1;
774 return false;
775 }
776
777 // Otherwise, we cannot select it.
778 return true;
779 }
780
781 // Default, generate it as a register.
782 AM.BaseType = X86ISelAddressMode::RegBase;
783 AM.Base.Reg = N;
784 return false;
785}
786
Evan Chengc9fab312005-12-08 02:01:35 +0000787/// SelectAddr - returns true if it is able pattern match an addressing mode.
788/// It returns the operands which make up the maximal addressing mode it can
789/// match by reference.
Evan Cheng6cd09092006-11-08 20:34:28 +0000790bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
791 SDOperand &Scale, SDOperand &Index,
792 SDOperand &Disp) {
Evan Chengc9fab312005-12-08 02:01:35 +0000793 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000794 if (MatchAddress(N, AM))
795 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000796
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000797 MVT::ValueType VT = N.getValueType();
Evan Chengbc7a0f442006-01-11 06:09:51 +0000798 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000799 if (!AM.Base.Reg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000800 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengc9fab312005-12-08 02:01:35 +0000801 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000802
Evan Chengd19d51f2006-02-05 05:25:07 +0000803 if (!AM.IndexReg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000804 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Chengbc7a0f442006-01-11 06:09:51 +0000805
806 getAddressOperands(AM, Base, Scale, Index, Disp);
807 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000808}
809
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000810/// isZeroNode - Returns true if Elt is a constant zero or a floating point
811/// constant +0.0.
812static inline bool isZeroNode(SDOperand Elt) {
813 return ((isa<ConstantSDNode>(Elt) &&
814 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
815 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000816 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000817}
818
819
Chris Lattner398195e2006-10-07 21:55:32 +0000820/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
821/// match a load whose top elements are either undef or zeros. The load flavor
822/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng6cd09092006-11-08 20:34:28 +0000823bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Chengff1a7122006-10-16 06:34:55 +0000824 SDOperand N, SDOperand &Base,
Evan Cheng4090dc42006-10-11 21:06:01 +0000825 SDOperand &Scale, SDOperand &Index,
826 SDOperand &Disp, SDOperand &InChain,
827 SDOperand &OutChain) {
Chris Lattner398195e2006-10-07 21:55:32 +0000828 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000829 InChain = N.getOperand(0).getValue(1);
Evan Chengff1a7122006-10-16 06:34:55 +0000830 if (ISD::isNON_EXTLoad(InChain.Val) &&
831 InChain.getValue(0).hasOneUse() &&
Evan Chengfb448222006-11-10 21:23:04 +0000832 N.hasOneUse() &&
Evan Cheng6cd09092006-11-08 20:34:28 +0000833 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng4090dc42006-10-11 21:06:01 +0000834 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng6cd09092006-11-08 20:34:28 +0000835 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner398195e2006-10-07 21:55:32 +0000836 return false;
Evan Cheng4090dc42006-10-11 21:06:01 +0000837 OutChain = LD->getChain();
Chris Lattner398195e2006-10-07 21:55:32 +0000838 return true;
839 }
840 }
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000841
842 // Also handle the case where we explicitly require zeros in the top
Chris Lattner398195e2006-10-07 21:55:32 +0000843 // elements. This is a vector shuffle from the zero vector.
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000844 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
845 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
846 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
847 N.getOperand(1).Val->hasOneUse() &&
848 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
849 N.getOperand(1).getOperand(0).hasOneUse()) {
850 // Check to see if the BUILD_VECTOR is building a zero vector.
851 SDOperand BV = N.getOperand(0);
852 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
853 if (!isZeroNode(BV.getOperand(i)) &&
854 BV.getOperand(i).getOpcode() != ISD::UNDEF)
855 return false; // Not a zero/undef vector.
856 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
857 // from the LHS.
858 unsigned VecWidth = BV.getNumOperands();
859 SDOperand ShufMask = N.getOperand(2);
860 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
862 if (C->getValue() == VecWidth) {
863 for (unsigned i = 1; i != VecWidth; ++i) {
864 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
865 // ok.
866 } else {
867 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
868 if (C->getValue() >= VecWidth) return false;
869 }
870 }
871 }
872
873 // Okay, this is a zero extending load. Fold it.
874 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng6cd09092006-11-08 20:34:28 +0000875 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +0000876 return false;
877 OutChain = LD->getChain();
878 InChain = SDOperand(LD, 1);
879 return true;
880 }
881 }
Chris Lattner398195e2006-10-07 21:55:32 +0000882 return false;
883}
884
885
Evan Cheng77d86ff2006-02-25 10:09:08 +0000886/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
887/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng6cd09092006-11-08 20:34:28 +0000888bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
889 SDOperand &Base, SDOperand &Scale,
Evan Cheng77d86ff2006-02-25 10:09:08 +0000890 SDOperand &Index, SDOperand &Disp) {
891 X86ISelAddressMode AM;
892 if (MatchAddress(N, AM))
893 return false;
894
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000895 MVT::ValueType VT = N.getValueType();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000896 unsigned Complexity = 0;
897 if (AM.BaseType == X86ISelAddressMode::RegBase)
898 if (AM.Base.Reg.Val)
899 Complexity = 1;
900 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000901 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000902 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
903 Complexity = 4;
904
905 if (AM.IndexReg.Val)
906 Complexity++;
907 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000908 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000909
Chris Lattner3e1d9172007-03-20 06:08:29 +0000910 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
911 // a simple shift.
912 if (AM.Scale > 1)
Evan Cheng990c3602006-02-28 21:13:57 +0000913 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000914
915 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
916 // to a LEA. This is determined with some expermentation but is by no means
917 // optimal (especially for code size consideration). LEA is nice because of
918 // its three-address nature. Tweak the cost function again when we can run
919 // convertToThreeAddress() at register allocation time.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000920 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
921 // For X86-64, we should always use lea to materialize RIP relative
922 // addresses.
Evan Cheng47e181c2006-12-05 22:03:40 +0000923 if (Subtarget->is64Bit())
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000924 Complexity = 4;
925 else
926 Complexity += 2;
927 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000928
929 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
930 Complexity++;
931
932 if (Complexity > 2) {
933 getAddressOperands(AM, Base, Scale, Index, Disp);
934 return true;
935 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000936 return false;
937}
938
Evan Chengd5f2ba02006-02-06 06:02:33 +0000939bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
940 SDOperand &Base, SDOperand &Scale,
941 SDOperand &Index, SDOperand &Disp) {
Evan Chenge71fe34d2006-10-09 20:57:25 +0000942 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Chengd5f2ba02006-02-06 06:02:33 +0000943 N.hasOneUse() &&
Evan Chengb86375c2006-10-14 08:33:25 +0000944 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng6cd09092006-11-08 20:34:28 +0000945 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng10d27902006-01-06 20:36:21 +0000946 return false;
947}
948
Evan Cheng5588de92006-02-18 00:15:05 +0000949/// getGlobalBaseReg - Output the instructions required to put the
950/// base address to use for accessing globals into a register.
951///
Evan Cheng61413a32006-08-26 05:34:46 +0000952SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000953 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng5588de92006-02-18 00:15:05 +0000954 if (!GlobalBaseReg) {
955 // Insert the set of GlobalBaseReg into the first MBB of the function
956 MachineBasicBlock &FirstMBB = BB->getParent()->front();
957 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
958 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000959 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
960
Evan Cheng20350c42006-11-27 23:37:22 +0000961 const TargetInstrInfo *TII = TM.getInstrInfo();
962 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000963 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
964
965 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
966 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng1281dc32007-01-22 21:34:25 +0000967 if (TM.getRelocationModel() == Reloc::PIC_ &&
968 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000969 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
970 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
971 addReg(PC).
972 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
973 } else {
974 GlobalBaseReg = PC;
975 }
976
Evan Cheng5588de92006-02-18 00:15:05 +0000977 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000978 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000979}
980
Evan Chengf838cfc2006-05-20 01:36:52 +0000981static SDNode *FindCallStartFromCall(SDNode *Node) {
982 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
983 assert(Node->getOperand(0).getValueType() == MVT::Other &&
984 "Node doesn't have a token chain argument!");
985 return FindCallStartFromCall(Node->getOperand(0).Val);
986}
987
Christopher Lambb372aba2007-08-10 21:48:46 +0000988SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
989 SDOperand SRIdx;
990 switch (VT) {
991 case MVT::i8:
992 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
993 // Ensure that the source register has an 8-bit subreg on 32-bit targets
994 if (!Subtarget->is64Bit()) {
995 unsigned Opc;
996 MVT::ValueType VT;
997 switch (N0.getValueType()) {
998 default: assert(0 && "Unknown truncate!");
999 case MVT::i16:
1000 Opc = X86::MOV16to16_;
1001 VT = MVT::i16;
1002 break;
1003 case MVT::i32:
1004 Opc = X86::MOV32to32_;
1005 VT = MVT::i32;
1006 break;
1007 }
Evan Chengf8c23f02007-10-12 07:55:53 +00001008 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1009 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1010 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambb372aba2007-08-10 21:48:46 +00001011 }
1012 break;
1013 case MVT::i16:
1014 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1015 break;
1016 case MVT::i32:
1017 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1018 break;
Evan Chengf8c23f02007-10-12 07:55:53 +00001019 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambb372aba2007-08-10 21:48:46 +00001020 }
Evan Chengf8c23f02007-10-12 07:55:53 +00001021 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambb372aba2007-08-10 21:48:46 +00001022}
1023
1024
Evan Cheng61413a32006-08-26 05:34:46 +00001025SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +00001026 SDNode *Node = N.Val;
1027 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +00001028 unsigned Opc, MOpc;
1029 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +00001030
Evan Chengd49cc362006-02-10 22:24:32 +00001031#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001032 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengd49cc362006-02-10 22:24:32 +00001033 DEBUG(Node->dump(CurDAG));
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001034 DOUT << "\n";
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001035 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001036#endif
1037
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001038 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +00001039#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001040 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengd49cc362006-02-10 22:24:32 +00001041 DEBUG(Node->dump(CurDAG));
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001042 DOUT << "\n";
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001043 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001044#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001045 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001046 }
Evan Cheng2ae799a2006-01-11 22:15:18 +00001047
Evan Cheng10d27902006-01-06 20:36:21 +00001048 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +00001049 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001050 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001051 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001052
Evan Cheng77d86ff2006-02-25 10:09:08 +00001053 case ISD::ADD: {
1054 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1055 // code and is matched first so to prevent it from being turned into
1056 // LEA32r X+c.
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001057 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1058 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng77d86ff2006-02-25 10:09:08 +00001059 SDOperand N0 = N.getOperand(0);
1060 SDOperand N1 = N.getOperand(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001061 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng62cdc3f2006-12-05 04:01:03 +00001062 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng77d86ff2006-02-25 10:09:08 +00001063 N1.getOpcode() == ISD::Constant) {
1064 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1065 SDOperand C(0, 0);
1066 // TODO: handle ExternalSymbolSDNode.
1067 if (GlobalAddressSDNode *G =
1068 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001069 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +00001070 G->getOffset() + Offset);
1071 } else if (ConstantPoolSDNode *CP =
1072 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Cheng9a083a42006-09-12 21:04:05 +00001073 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +00001074 CP->getAlignment(),
1075 CP->getOffset()+Offset);
1076 }
1077
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001078 if (C.Val) {
1079 if (Subtarget->is64Bit()) {
1080 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1081 CurDAG->getRegister(0, PtrVT), C };
1082 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1083 } else
1084 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1085 }
Evan Cheng77d86ff2006-02-25 10:09:08 +00001086 }
1087
1088 // Other cases are handled by auto-generated code.
1089 break;
Evan Cheng1f342c22006-02-23 02:43:52 +00001090 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00001091
Dan Gohmana1603612007-10-08 18:33:35 +00001092 case ISD::SMUL_LOHI:
1093 case ISD::UMUL_LOHI: {
1094 SDOperand N0 = Node->getOperand(0);
1095 SDOperand N1 = Node->getOperand(1);
1096
Dan Gohman51554bf2007-10-09 15:44:37 +00001097 // There are several forms of IMUL that just return the low part and
1098 // don't have fixed-register operands. If we don't need the high part,
1099 // use these instead. They can be selected with the generated ISel code.
Dan Gohmana1603612007-10-08 18:33:35 +00001100 if (NVT != MVT::i8 &&
1101 N.getValue(1).use_empty()) {
1102 N = CurDAG->getNode(ISD::MUL, NVT, N0, N1);
1103 break;
1104 }
1105
1106 bool isSigned = Opcode == ISD::SMUL_LOHI;
1107 if (!isSigned)
Evan Cheng10d27902006-01-06 20:36:21 +00001108 switch (NVT) {
1109 default: assert(0 && "Unsupported VT!");
1110 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1111 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1112 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001113 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +00001114 }
1115 else
1116 switch (NVT) {
1117 default: assert(0 && "Unsupported VT!");
1118 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1119 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1120 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001121 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +00001122 }
1123
1124 unsigned LoReg, HiReg;
1125 switch (NVT) {
1126 default: assert(0 && "Unsupported VT!");
1127 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1128 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1129 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001130 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng10d27902006-01-06 20:36:21 +00001131 }
1132
Evan Cheng10d27902006-01-06 20:36:21 +00001133 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng473c5112007-08-02 05:48:35 +00001134 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohmana1603612007-10-08 18:33:35 +00001135 // multiplty is commmutative
Evan Cheng92e27972006-01-06 23:19:29 +00001136 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +00001137 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng473c5112007-08-02 05:48:35 +00001138 if (foldedLoad)
1139 std::swap(N0, N1);
Evan Cheng92e27972006-01-06 23:19:29 +00001140 }
1141
Evan Cheng2d487222006-08-26 01:05:16 +00001142 AddToISelQueue(N0);
Dan Gohmana1603612007-10-08 18:33:35 +00001143 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1144 N0, SDOperand()).getValue(1);
Evan Cheng10d27902006-01-06 20:36:21 +00001145
1146 if (foldedLoad) {
Dan Gohmana1603612007-10-08 18:33:35 +00001147 AddToISelQueue(N1.getOperand(0));
Evan Cheng2d487222006-08-26 01:05:16 +00001148 AddToISelQueue(Tmp0);
1149 AddToISelQueue(Tmp1);
1150 AddToISelQueue(Tmp2);
1151 AddToISelQueue(Tmp3);
Dan Gohmana1603612007-10-08 18:33:35 +00001152 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +00001153 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +00001154 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +00001155 InFlag = SDOperand(CNode, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00001156 // Update the chain.
1157 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng10d27902006-01-06 20:36:21 +00001158 } else {
Evan Cheng2d487222006-08-26 01:05:16 +00001159 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +00001160 InFlag =
1161 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +00001162 }
1163
Dan Gohmana1603612007-10-08 18:33:35 +00001164 // Copy the low half of the result, if it is needed.
1165 if (!N.getValue(0).use_empty()) {
1166 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1167 LoReg, NVT, InFlag);
1168 InFlag = Result.getValue(2);
1169 ReplaceUses(N.getValue(0), Result);
1170#ifndef NDEBUG
1171 DOUT << std::string(Indent-2, ' ') << "=> ";
1172 DEBUG(Result.Val->dump(CurDAG));
1173 DOUT << "\n";
1174#endif
Evan Chenge32e9232007-08-09 21:59:35 +00001175 }
Dan Gohmana1603612007-10-08 18:33:35 +00001176 // Copy the high half of the result, if it is needed.
1177 if (!N.getValue(1).use_empty()) {
1178 SDOperand Result;
1179 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1180 // Prevent use of AH in a REX instruction by referencing AX instead.
1181 // Shift it down 8 bits.
1182 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1183 X86::AX, MVT::i16, InFlag);
1184 InFlag = Result.getValue(2);
1185 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1186 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1187 // Then truncate it down to i8.
1188 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1189 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1190 MVT::i8, Result, SRIdx), 0);
1191 } else {
1192 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1193 HiReg, NVT, InFlag);
1194 InFlag = Result.getValue(2);
1195 }
1196 ReplaceUses(N.getValue(1), Result);
1197#ifndef NDEBUG
1198 DOUT << std::string(Indent-2, ' ') << "=> ";
1199 DEBUG(Result.Val->dump(CurDAG));
1200 DOUT << "\n";
1201#endif
1202 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001203
Evan Chengd49cc362006-02-10 22:24:32 +00001204#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001205 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001206#endif
Dan Gohmana1603612007-10-08 18:33:35 +00001207
Evan Chengbd1c5a82006-08-11 09:08:15 +00001208 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +00001209 }
Evan Cheng5588de92006-02-18 00:15:05 +00001210
Dan Gohmana1603612007-10-08 18:33:35 +00001211 case ISD::SDIVREM:
1212 case ISD::UDIVREM: {
1213 SDOperand N0 = Node->getOperand(0);
1214 SDOperand N1 = Node->getOperand(1);
1215
1216 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng92e27972006-01-06 23:19:29 +00001217 if (!isSigned)
1218 switch (NVT) {
1219 default: assert(0 && "Unsupported VT!");
1220 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1221 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1222 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001223 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001224 }
1225 else
1226 switch (NVT) {
1227 default: assert(0 && "Unsupported VT!");
1228 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1229 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1230 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001231 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001232 }
1233
1234 unsigned LoReg, HiReg;
1235 unsigned ClrOpcode, SExtOpcode;
1236 switch (NVT) {
1237 default: assert(0 && "Unsupported VT!");
1238 case MVT::i8:
1239 LoReg = X86::AL; HiReg = X86::AH;
Evan Cheng9e8093a2006-11-17 22:10:14 +00001240 ClrOpcode = 0;
Evan Cheng92e27972006-01-06 23:19:29 +00001241 SExtOpcode = X86::CBW;
1242 break;
1243 case MVT::i16:
1244 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001245 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001246 SExtOpcode = X86::CWD;
1247 break;
1248 case MVT::i32:
1249 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001250 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001251 SExtOpcode = X86::CDQ;
1252 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001253 case MVT::i64:
1254 LoReg = X86::RAX; HiReg = X86::RDX;
1255 ClrOpcode = X86::MOV64r0;
1256 SExtOpcode = X86::CQO;
1257 break;
Evan Cheng92e27972006-01-06 23:19:29 +00001258 }
1259
Dan Gohmana1603612007-10-08 18:33:35 +00001260 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1261 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1262
1263 SDOperand InFlag;
Evan Cheng9e8093a2006-11-17 22:10:14 +00001264 if (NVT == MVT::i8 && !isSigned) {
1265 // Special case for div8, just use a move with zero extension to AX to
1266 // clear the upper 8 bits (AH).
1267 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1268 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1269 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1270 AddToISelQueue(N0.getOperand(0));
1271 AddToISelQueue(Tmp0);
1272 AddToISelQueue(Tmp1);
1273 AddToISelQueue(Tmp2);
1274 AddToISelQueue(Tmp3);
1275 Move =
1276 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1277 Ops, 5), 0);
1278 Chain = Move.getValue(1);
1279 ReplaceUses(N0.getValue(1), Chain);
1280 } else {
1281 AddToISelQueue(N0);
1282 Move =
1283 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1284 Chain = CurDAG->getEntryNode();
1285 }
Dan Gohmana1603612007-10-08 18:33:35 +00001286 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng92e27972006-01-06 23:19:29 +00001287 InFlag = Chain.getValue(1);
Evan Cheng9e8093a2006-11-17 22:10:14 +00001288 } else {
1289 AddToISelQueue(N0);
1290 InFlag =
Dan Gohmana1603612007-10-08 18:33:35 +00001291 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1292 LoReg, N0, SDOperand()).getValue(1);
Evan Cheng9e8093a2006-11-17 22:10:14 +00001293 if (isSigned) {
1294 // Sign extend the low part into the high part.
1295 InFlag =
1296 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1297 } else {
1298 // Zero out the high part, effectively zero extending the input.
1299 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohmana1603612007-10-08 18:33:35 +00001300 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1301 ClrNode, InFlag).getValue(1);
Evan Cheng9e8093a2006-11-17 22:10:14 +00001302 }
Evan Cheng92e27972006-01-06 23:19:29 +00001303 }
1304
1305 if (foldedLoad) {
Evan Cheng9e8093a2006-11-17 22:10:14 +00001306 AddToISelQueue(N1.getOperand(0));
Evan Cheng2d487222006-08-26 01:05:16 +00001307 AddToISelQueue(Tmp0);
1308 AddToISelQueue(Tmp1);
1309 AddToISelQueue(Tmp2);
1310 AddToISelQueue(Tmp3);
Evan Cheng9e8093a2006-11-17 22:10:14 +00001311 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +00001312 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +00001313 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +00001314 InFlag = SDOperand(CNode, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00001315 // Update the chain.
1316 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng92e27972006-01-06 23:19:29 +00001317 } else {
Evan Cheng2d487222006-08-26 01:05:16 +00001318 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +00001319 InFlag =
1320 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001321 }
1322
Dan Gohman31599682007-09-25 18:23:27 +00001323 // Copy the division (low) result, if it is needed.
1324 if (!N.getValue(0).use_empty()) {
Dan Gohmana1603612007-10-08 18:33:35 +00001325 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1326 LoReg, NVT, InFlag);
Dan Gohman31599682007-09-25 18:23:27 +00001327 InFlag = Result.getValue(2);
1328 ReplaceUses(N.getValue(0), Result);
1329#ifndef NDEBUG
1330 DOUT << std::string(Indent-2, ' ') << "=> ";
1331 DEBUG(Result.Val->dump(CurDAG));
1332 DOUT << "\n";
1333#endif
Evan Chenge32e9232007-08-09 21:59:35 +00001334 }
Dan Gohman31599682007-09-25 18:23:27 +00001335 // Copy the remainder (high) result, if it is needed.
1336 if (!N.getValue(1).use_empty()) {
1337 SDOperand Result;
1338 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1339 // Prevent use of AH in a REX instruction by referencing AX instead.
1340 // Shift it down 8 bits.
Dan Gohmana1603612007-10-08 18:33:35 +00001341 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1342 X86::AX, MVT::i16, InFlag);
Dan Gohman31599682007-09-25 18:23:27 +00001343 InFlag = Result.getValue(2);
1344 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1345 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1346 // Then truncate it down to i8.
1347 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1348 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1349 MVT::i8, Result, SRIdx), 0);
1350 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00001351 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1352 HiReg, NVT, InFlag);
Dan Gohman31599682007-09-25 18:23:27 +00001353 InFlag = Result.getValue(2);
1354 }
1355 ReplaceUses(N.getValue(1), Result);
1356#ifndef NDEBUG
1357 DOUT << std::string(Indent-2, ' ') << "=> ";
1358 DEBUG(Result.Val->dump(CurDAG));
1359 DOUT << "\n";
1360#endif
1361 }
Evan Chengd49cc362006-02-10 22:24:32 +00001362
1363#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001364 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001365#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001366
1367 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +00001368 }
Christopher Lamb44e79f82007-08-10 22:22:41 +00001369
1370 case ISD::ANY_EXTEND: {
1371 SDOperand N0 = Node->getOperand(0);
1372 AddToISelQueue(N0);
1373 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1374 SDOperand SRIdx;
1375 switch(N0.getValueType()) {
1376 case MVT::i32:
1377 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1378 break;
1379 case MVT::i16:
1380 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1381 break;
1382 case MVT::i8:
1383 if (Subtarget->is64Bit())
1384 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1385 break;
1386 default: assert(0 && "Unknown any_extend!");
1387 }
1388 if (SRIdx.Val) {
Evan Chengf8c23f02007-10-12 07:55:53 +00001389 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1390 NVT, N0, SRIdx);
Christopher Lamb44e79f82007-08-10 22:22:41 +00001391
1392#ifndef NDEBUG
1393 DOUT << std::string(Indent-2, ' ') << "=> ";
1394 DEBUG(ResNode->dump(CurDAG));
1395 DOUT << "\n";
1396 Indent -= 2;
1397#endif
1398 return ResNode;
1399 } // Otherwise let generated ISel handle it.
1400 }
1401 break;
1402 }
Christopher Lambb372aba2007-08-10 21:48:46 +00001403
1404 case ISD::SIGN_EXTEND_INREG: {
1405 SDOperand N0 = Node->getOperand(0);
1406 AddToISelQueue(N0);
Evan Cheng9733bde2006-05-08 08:01:26 +00001407
Christopher Lambb372aba2007-08-10 21:48:46 +00001408 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1409 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendlingb7cabbe2007-11-01 08:51:44 +00001410 unsigned Opc = 0;
Christopher Lamb5fecb802007-07-29 01:24:57 +00001411 switch (NVT) {
Christopher Lamb5fecb802007-07-29 01:24:57 +00001412 case MVT::i16:
Christopher Lambb372aba2007-08-10 21:48:46 +00001413 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1414 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb5fecb802007-07-29 01:24:57 +00001415 break;
1416 case MVT::i32:
Christopher Lambb372aba2007-08-10 21:48:46 +00001417 switch (SVT) {
1418 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1419 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1420 default: assert(0 && "Unknown sign_extend_inreg!");
1421 }
Christopher Lamb5fecb802007-07-29 01:24:57 +00001422 break;
Christopher Lambb372aba2007-08-10 21:48:46 +00001423 case MVT::i64:
1424 switch (SVT) {
1425 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1426 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1427 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1428 default: assert(0 && "Unknown sign_extend_inreg!");
1429 }
1430 break;
1431 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb5fecb802007-07-29 01:24:57 +00001432 }
Christopher Lambb372aba2007-08-10 21:48:46 +00001433
1434 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1435
1436#ifndef NDEBUG
1437 DOUT << std::string(Indent-2, ' ') << "=> ";
1438 DEBUG(TruncOp.Val->dump(CurDAG));
1439 DOUT << "\n";
1440 DOUT << std::string(Indent-2, ' ') << "=> ";
1441 DEBUG(ResNode->dump(CurDAG));
1442 DOUT << "\n";
1443 Indent -= 2;
1444#endif
1445 return ResNode;
1446 break;
1447 }
1448
1449 case ISD::TRUNCATE: {
1450 SDOperand Input = Node->getOperand(0);
1451 AddToISelQueue(Node->getOperand(0));
1452 SDNode *ResNode = getTruncate(Input, NVT);
1453
Evan Cheng9733bde2006-05-08 08:01:26 +00001454#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001455 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng61413a32006-08-26 05:34:46 +00001456 DEBUG(ResNode->dump(CurDAG));
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001457 DOUT << "\n";
Evan Cheng9733bde2006-05-08 08:01:26 +00001458 Indent -= 2;
1459#endif
Christopher Lamb5fecb802007-07-29 01:24:57 +00001460 return ResNode;
Evan Chenga26c4512006-05-20 07:44:28 +00001461 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001462 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001463 }
1464
Evan Cheng61413a32006-08-26 05:34:46 +00001465 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001466
Evan Chengd49cc362006-02-10 22:24:32 +00001467#ifndef NDEBUG
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001468 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng61413a32006-08-26 05:34:46 +00001469 if (ResNode == NULL || ResNode == N.Val)
1470 DEBUG(N.Val->dump(CurDAG));
1471 else
1472 DEBUG(ResNode->dump(CurDAG));
Bill Wendlingc8e81b82006-11-17 07:52:03 +00001473 DOUT << "\n";
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001474 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001475#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001476
1477 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001478}
1479
Chris Lattnerba1ed582006-06-08 18:03:49 +00001480bool X86DAGToDAGISel::
1481SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1482 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1483 SDOperand Op0, Op1, Op2, Op3;
1484 switch (ConstraintCode) {
1485 case 'o': // offsetable ??
1486 case 'v': // not offsetable ??
1487 default: return true;
1488 case 'm': // memory
Evan Cheng6cd09092006-11-08 20:34:28 +00001489 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerba1ed582006-06-08 18:03:49 +00001490 return true;
1491 break;
1492 }
1493
Evan Cheng2d487222006-08-26 01:05:16 +00001494 OutOps.push_back(Op0);
1495 OutOps.push_back(Op1);
1496 OutOps.push_back(Op2);
1497 OutOps.push_back(Op3);
1498 AddToISelQueue(Op0);
1499 AddToISelQueue(Op1);
1500 AddToISelQueue(Op2);
1501 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001502 return false;
1503}
1504
Chris Lattner655e7df2005-11-16 01:54:32 +00001505/// createX86ISelDag - This pass converts a legalized DAG into a
1506/// X86-specific DAG, ready for instruction scheduling.
1507///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001508FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1509 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001510}