blob: cf83e681922c15ac7acc535de12c789ddb693481 [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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
45STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46
Chris Lattnerc961eea2005-11-16 01:54:32 +000047//===----------------------------------------------------------------------===//
48// Pattern Matcher Implementation
49//===----------------------------------------------------------------------===//
50
51namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000052 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000053 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// tree.
55 struct X86ISelAddressMode {
56 enum {
57 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000059 } BaseType;
60
61 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000062 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 int FrameIndex;
64 } Base;
65
Evan Chengbe3bf422008-02-07 08:53:49 +000066 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000068 SDValue IndexReg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Disp;
70 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000071 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000072 const char *ES;
73 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000075
76 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000077 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079 }
Dale Johannesen50dd1d02008-08-11 23:46:25 +000080 void dump() {
81 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000082 cerr << "Base.Reg ";
83 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
84 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000085 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
86 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000087 cerr << "IndexReg ";
88 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
89 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000090 cerr << " Disp " << Disp << "\n";
91 cerr << "GV "; if (GV) GV->dump();
92 else cerr << "nul";
93 cerr << " CP "; if (CP) CP->dump();
94 else cerr << "nul";
95 cerr << "\n";
96 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
97 cerr << " JT" << JT << " Align" << Align << "\n";
98 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000099 };
100}
101
102namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000103 //===--------------------------------------------------------------------===//
104 /// ISel - X86 specific code to select X86 machine instructions for
105 /// SelectionDAG operations.
106 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000107 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 /// TM - Keep a reference to X86TargetMachine.
109 ///
110 X86TargetMachine &TM;
111
Chris Lattnerc961eea2005-11-16 01:54:32 +0000112 /// X86Lowering - This object fully describes how to lower LLVM code to an
113 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000114 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000115
116 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
117 /// make the right decision when generating code for different targets.
118 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000119
Evan Chengdb8d56b2008-06-30 20:45:06 +0000120 /// CurBB - Current BB being isel'd.
121 ///
122 MachineBasicBlock *CurBB;
123
Evan Chengb7a75a52008-09-26 23:41:32 +0000124 /// OptForSize - If true, selector should try to optimize for code size
125 /// instead of performance.
126 bool OptForSize;
127
Chris Lattnerc961eea2005-11-16 01:54:32 +0000128 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000129 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000130 : SelectionDAGISel(*tm.getTargetLowering(), fast),
Dan Gohman38217fe2008-10-03 16:17:33 +0000131 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000132 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000133 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000134
135 virtual const char *getPassName() const {
136 return "X86 DAG->DAG Instruction Selection";
137 }
138
Evan Chengdb8d56b2008-06-30 20:45:06 +0000139 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000141 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000142
143 /// InstructionSelectPostProcessing - Post processing of selected and
144 /// scheduled basic blocks.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000145 virtual void InstructionSelectPostProcessing();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000146
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000147 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
148
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000149 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000150
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151// Include the pieces autogenerated from the target description.
152#include "X86GenDAGISel.inc"
153
154 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000156 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000157
Dan Gohman475871a2008-07-27 21:46:04 +0000158 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000159 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000160 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000161 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000162 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
163 SDValue &Scale, SDValue &Index, SDValue &Disp);
164 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
165 SDValue &Scale, SDValue &Index, SDValue &Disp);
166 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
167 SDValue N, SDValue &Base, SDValue &Scale,
168 SDValue &Index, SDValue &Disp,
169 SDValue &InChain, SDValue &OutChain);
170 bool TryFoldLoad(SDValue P, SDValue N,
171 SDValue &Base, SDValue &Scale,
172 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000173 void PreprocessForRMW();
174 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000175
Chris Lattnerc0bad572006-06-08 18:03:49 +0000176 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
177 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000178 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000179 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000180 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000181
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000182 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
183
Dan Gohman475871a2008-07-27 21:46:04 +0000184 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
185 SDValue &Scale, SDValue &Index,
186 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000187 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000188 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
189 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000190 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000191 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 // These are 32-bit even in 64-bit mode since RIP relative offset
193 // is 32-bit.
194 if (AM.GV)
195 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
196 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000197 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
198 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000199 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000200 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 else if (AM.JT != -1)
202 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
203 else
204 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000205 }
206
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000207 /// getI8Imm - Return a target constant with the specified value, of type
208 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000209 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000210 return CurDAG->getTargetConstant(Imm, MVT::i8);
211 }
212
Chris Lattnerc961eea2005-11-16 01:54:32 +0000213 /// getI16Imm - Return a target constant with the specified value, of type
214 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000215 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000216 return CurDAG->getTargetConstant(Imm, MVT::i16);
217 }
218
219 /// getI32Imm - Return a target constant with the specified value, of type
220 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000221 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000222 return CurDAG->getTargetConstant(Imm, MVT::i32);
223 }
Evan Chengf597dc72006-02-10 22:24:32 +0000224
Dan Gohman8b746962008-09-23 18:22:58 +0000225 /// getGlobalBaseReg - Return an SDNode that returns the value of
226 /// the global base register. Output instructions required to
227 /// initialize the global base register, if necessary.
228 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000229 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000230
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000231 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
232 /// truncate of the specified operand to i8. This can be done with tablegen,
233 /// except that this code uses MVT::Flag in a tricky way that happens to
234 /// improve scheduling in some cases.
235 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000236
Evan Cheng23addc02006-02-10 22:46:26 +0000237#ifndef NDEBUG
238 unsigned Indent;
239#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000240 };
241}
242
Gabor Greif93c53e52008-08-31 15:37:04 +0000243/// findFlagUse - Return use of MVT::Flag value produced by the specified
244/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000245///
Evan Chenga275ecb2006-10-10 01:46:56 +0000246static SDNode *findFlagUse(SDNode *N) {
247 unsigned FlagResNo = N->getNumValues()-1;
248 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000249 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000250 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000251 SDValue Op = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +0000252 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000253 return User;
254 }
255 }
256 return NULL;
257}
258
Evan Chengcdda25d2008-04-25 08:22:20 +0000259/// findNonImmUse - Return true by reference in "found" if "Use" is an
260/// non-immediate use of "Def". This function recursively traversing
261/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000262static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman682d5a82008-09-17 01:39:10 +0000263 SDNode *Root, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000264 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000265 if (found ||
Dan Gohmanf06c8352008-09-30 18:30:35 +0000266 Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000267 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000268 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000269
Evan Cheng27e1fe92006-10-14 08:33:25 +0000270 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000271 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000272 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000273 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000274 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000275 assert(N != Root);
Evan Chengf4b4c412006-08-08 00:31:00 +0000276 found = true;
277 break;
278 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000279
280 // Traverse up the operand chain.
Dan Gohman682d5a82008-09-17 01:39:10 +0000281 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000282 }
283}
284
Evan Cheng27e1fe92006-10-14 08:33:25 +0000285/// isNonImmUse - Start searching from Root up the DAG to check is Def can
286/// be reached. Return true if that's the case. However, ignore direct uses
287/// by ImmedUse (which would be U in the example illustrated in
288/// CanBeFoldedBy) and by Root (which can happen in the store case).
289/// FIXME: to be really generic, we should allow direct use by any node
290/// that is being folded. But realisticly since we only fold loads which
291/// have one non-chain use, we only need to watch out for load/op/store
292/// and load/op/cmp case where the root (store / cmp) may reach the load via
293/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000294static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000295 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000296 bool found = false;
Dan Gohman682d5a82008-09-17 01:39:10 +0000297 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000298 return found;
299}
300
301
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000302bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000303 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000304
Dan Gohman682d5a82008-09-17 01:39:10 +0000305 // If Root use can somehow reach N through a path that that doesn't contain
306 // U then folding N would create a cycle. e.g. In the following
307 // diagram, Root can reach N through X. If N is folded into into Root, then
308 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000309 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000310 // [N*] //
311 // ^ ^ //
312 // / \ //
313 // [U*] [X]? //
314 // ^ ^ //
315 // \ / //
316 // \ / //
317 // [Root*] //
318 //
319 // * indicates nodes to be folded together.
320 //
321 // If Root produces a flag, then it gets (even more) interesting. Since it
322 // will be "glued" together with its flag use in the scheduler, we need to
323 // check if it might reach N.
324 //
325 // [N*] //
326 // ^ ^ //
327 // / \ //
328 // [U*] [X]? //
329 // ^ ^ //
330 // \ \ //
331 // \ | //
332 // [Root*] | //
333 // ^ | //
334 // f | //
335 // | / //
336 // [Y] / //
337 // ^ / //
338 // f / //
339 // | / //
340 // [FU] //
341 //
342 // If FU (flag use) indirectly reaches N (the load), and Root folds N
343 // (call it Fold), then X is a predecessor of FU and a successor of
344 // Fold. But since Fold and FU are flagged together, this will create
345 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000346
Duncan Sands83ec4b62008-06-06 12:08:01 +0000347 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000348 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000349 SDNode *FU = findFlagUse(Root);
350 if (FU == NULL)
351 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000352 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000353 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000354 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000355
Dan Gohman682d5a82008-09-17 01:39:10 +0000356 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000357}
358
Evan Cheng70e674e2006-08-28 20:10:17 +0000359/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
360/// and move load below the TokenFactor. Replace store's chain operand with
361/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000362static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000363 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000364 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000365 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
366 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000367 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000368 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000369 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000370 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
371 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
372 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
373 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000374}
375
Evan Chengcd0baf22008-05-23 21:23:16 +0000376/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
377///
Dan Gohman475871a2008-07-27 21:46:04 +0000378static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
379 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000380 if (N.getOpcode() == ISD::BIT_CONVERT)
381 N = N.getOperand(0);
382
383 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
384 if (!LD || LD->isVolatile())
385 return false;
386 if (LD->getAddressingMode() != ISD::UNINDEXED)
387 return false;
388
389 ISD::LoadExtType ExtType = LD->getExtensionType();
390 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
391 return false;
392
393 if (N.hasOneUse() &&
394 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000395 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000396 Load = N;
397 return true;
398 }
399 return false;
400}
401
Evan Chengab6c3bb2008-08-25 21:27:18 +0000402/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
403/// operand and move load below the call's chain operand.
404static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
405 SDValue Call, SDValue Chain) {
406 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000407 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
408 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409 Ops.push_back(Load.getOperand(0));
410 else
411 Ops.push_back(Chain.getOperand(i));
412 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
413 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
414 Load.getOperand(1), Load.getOperand(2));
415 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000416 Ops.push_back(SDValue(Load.getNode(), 1));
417 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 Ops.push_back(Call.getOperand(i));
419 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
420}
421
422/// isCalleeLoad - Return true if call address is a load and it can be
423/// moved below CALLSEQ_START and the chains leading up to the call.
424/// Return the CALLSEQ_START by reference as a second output.
425static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000426 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000427 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000428 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000429 if (!LD ||
430 LD->isVolatile() ||
431 LD->getAddressingMode() != ISD::UNINDEXED ||
432 LD->getExtensionType() != ISD::NON_EXTLOAD)
433 return false;
434
435 // Now let's find the callseq_start.
436 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
437 if (!Chain.hasOneUse())
438 return false;
439 Chain = Chain.getOperand(0);
440 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000441 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Chengab6c3bb2008-08-25 21:27:18 +0000442}
443
444
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000445/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
446/// This is only run if not in -fast mode (aka -O0).
447/// This allows the instruction selector to pick more read-modify-write
448/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000449///
450/// [Load chain]
451/// ^
452/// |
453/// [Load]
454/// ^ ^
455/// | |
456/// / \-
457/// / |
458/// [TokenFactor] [Op]
459/// ^ ^
460/// | |
461/// \ /
462/// \ /
463/// [Store]
464///
465/// The fact the store's chain operand != load's chain will prevent the
466/// (store (op (load))) instruction from being selected. We can transform it to:
467///
468/// [Load chain]
469/// ^
470/// |
471/// [TokenFactor]
472/// ^
473/// |
474/// [Load]
475/// ^ ^
476/// | |
477/// | \-
478/// | |
479/// | [Op]
480/// | ^
481/// | |
482/// \ /
483/// \ /
484/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000485void X86DAGToDAGISel::PreprocessForRMW() {
486 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
487 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000488 if (I->getOpcode() == X86ISD::CALL) {
489 /// Also try moving call address load from outside callseq_start to just
490 /// before the call to allow it to be folded.
491 ///
492 /// [Load chain]
493 /// ^
494 /// |
495 /// [Load]
496 /// ^ ^
497 /// | |
498 /// / \--
499 /// / |
500 ///[CALLSEQ_START] |
501 /// ^ |
502 /// | |
503 /// [LOAD/C2Reg] |
504 /// | |
505 /// \ /
506 /// \ /
507 /// [CALL]
508 SDValue Chain = I->getOperand(0);
509 SDValue Load = I->getOperand(1);
510 if (!isCalleeLoad(Load, Chain))
511 continue;
512 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
513 ++NumLoadMoved;
514 continue;
515 }
516
Evan Cheng8b2794a2006-10-13 21:14:26 +0000517 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000518 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000519 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000520
Gabor Greifba36cb52008-08-28 21:40:38 +0000521 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000522 continue;
523
Dan Gohman475871a2008-07-27 21:46:04 +0000524 SDValue N1 = I->getOperand(1);
525 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000526 if ((N1.getValueType().isFloatingPoint() &&
527 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000528 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000529 continue;
530
531 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000532 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000533 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000534 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000535 case ISD::ADD:
536 case ISD::MUL:
537 case ISD::AND:
538 case ISD::OR:
539 case ISD::XOR:
540 case ISD::ADDC:
541 case ISD::ADDE:
542 case ISD::VECTOR_SHUFFLE: {
543 SDValue N10 = N1.getOperand(0);
544 SDValue N11 = N1.getOperand(1);
545 RModW = isRMWLoad(N10, Chain, N2, Load);
546 if (!RModW)
547 RModW = isRMWLoad(N11, Chain, N2, Load);
548 break;
549 }
550 case ISD::SUB:
551 case ISD::SHL:
552 case ISD::SRA:
553 case ISD::SRL:
554 case ISD::ROTL:
555 case ISD::ROTR:
556 case ISD::SUBC:
557 case ISD::SUBE:
558 case X86ISD::SHLD:
559 case X86ISD::SHRD: {
560 SDValue N10 = N1.getOperand(0);
561 RModW = isRMWLoad(N10, Chain, N2, Load);
562 break;
563 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000564 }
565
Evan Cheng82a35b32006-08-29 06:44:17 +0000566 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000567 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000568 ++NumLoadMoved;
569 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000570 }
571}
572
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000573
574/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
575/// nodes that target the FP stack to be store and load to the stack. This is a
576/// gross hack. We would like to simply mark these as being illegal, but when
577/// we do that, legalize produces these when it expands calls, then expands
578/// these in the same legalize pass. We would like dag combine to be able to
579/// hack on these between the call expansion and the node legalization. As such
580/// this pass basically does "really late" legalization of these inline with the
581/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000582void X86DAGToDAGISel::PreprocessForFPConvert() {
583 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
584 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000585 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
586 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
587 continue;
588
589 // If the source and destination are SSE registers, then this is a legal
590 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000591 MVT SrcVT = N->getOperand(0).getValueType();
592 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000593 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
594 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
595 if (SrcIsSSE && DstIsSSE)
596 continue;
597
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000598 if (!SrcIsSSE && !DstIsSSE) {
599 // If this is an FPStack extension, it is a noop.
600 if (N->getOpcode() == ISD::FP_EXTEND)
601 continue;
602 // If this is a value-preserving FPStack truncation, it is a noop.
603 if (N->getConstantOperandVal(1))
604 continue;
605 }
606
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000607 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
608 // FPStack has extload and truncstore. SSE can fold direct loads into other
609 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000610 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000611 if (N->getOpcode() == ISD::FP_ROUND)
612 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
613 else
614 MemVT = SrcIsSSE ? SrcVT : DstVT;
615
Dan Gohmanf350b272008-08-23 02:25:05 +0000616 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000617
618 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohmanf350b272008-08-23 02:25:05 +0000619 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
620 N->getOperand(0),
621 MemTmp, NULL, 0, MemVT);
622 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
623 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000624
625 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
626 // extload we created. This will cause general havok on the dag because
627 // anything below the conversion could be folded into other existing nodes.
628 // To avoid invalidating 'I', back it up to the convert node.
629 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000630 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000631
632 // Now that we did that, the node is dead. Increment the iterator to the
633 // next node to process, then delete N.
634 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000635 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000636 }
637}
638
Chris Lattnerc961eea2005-11-16 01:54:32 +0000639/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
640/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000641void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000642 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000643 const Function *F = CurDAG->getMachineFunction().getFunction();
644 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000645
Evan Chengdb8d56b2008-06-30 20:45:06 +0000646 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000647 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000648 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000649
650 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000651 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000652
Chris Lattnerc961eea2005-11-16 01:54:32 +0000653 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000654#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000655 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000656 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000657#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000658 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000659#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000660 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000661#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000662
Dan Gohmanf350b272008-08-23 02:25:05 +0000663 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000664}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000665
Dan Gohman462dc7f2008-07-21 20:00:07 +0000666void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000667 // If we are emitting FP stack code, scan the basic block to determine if this
668 // block defines any FP values. If so, put an FP_REG_KILL instruction before
669 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000670
Dale Johannesen48d1e452007-09-24 22:52:39 +0000671 // Note that FP stack instructions are used in all modes for long double,
672 // so we always need to do this check.
673 // Also note that it's possible for an FP stack register to be live across
674 // an instruction that produces multiple basic blocks (SSE CMOV) so we
675 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000676
677 // Scan all of the machine instructions in these MBBs, checking for FP
678 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000679 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000680 MachineFunction::iterator EndMBB = BB; ++EndMBB;
681 for (; MBBI != EndMBB; ++MBBI) {
682 MachineBasicBlock *MBB = MBBI;
683
684 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
685 // before the return.
686 if (!MBB->empty()) {
687 MachineBasicBlock::iterator EndI = MBB->end();
688 --EndI;
689 if (EndI->getDesc().isReturn())
690 continue;
691 }
692
Dale Johannesen48d1e452007-09-24 22:52:39 +0000693 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000694 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000695 !ContainsFPCode && I != E; ++I) {
Dan Gohmand735b802008-10-03 15:45:36 +0000696 if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000697 const TargetRegisterClass *clas;
698 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000699 if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000700 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000701 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000702 X86::RFP32RegisterClass ||
703 clas == X86::RFP64RegisterClass ||
704 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000705 ContainsFPCode = true;
706 break;
707 }
708 }
709 }
710 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000711 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
712 // a copy of the input value in this block. In SSE mode, we only care about
713 // 80-bit values.
714 if (!ContainsFPCode) {
715 // Final check, check LLVM BB's that are successors to the LLVM BB
716 // corresponding to BB for FP PHI nodes.
717 const BasicBlock *LLVMBB = BB->getBasicBlock();
718 const PHINode *PN;
719 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
720 !ContainsFPCode && SI != E; ++SI) {
721 for (BasicBlock::const_iterator II = SI->begin();
722 (PN = dyn_cast<PHINode>(II)); ++II) {
723 if (PN->getType()==Type::X86_FP80Ty ||
724 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
725 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
726 ContainsFPCode = true;
727 break;
728 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000729 }
730 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000731 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000732 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
733 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000734 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000735 TM.getInstrInfo()->get(X86::FP_REG_KILL));
736 ++NumFPKill;
737 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000738 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000739}
740
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000741/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
742/// the main function.
743void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
744 MachineFrameInfo *MFI) {
745 const TargetInstrInfo *TII = TM.getInstrInfo();
746 if (Subtarget->isTargetCygMing())
747 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
748}
749
750void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
751 // If this is main, emit special code for main.
752 MachineBasicBlock *BB = MF.begin();
753 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
754 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
755}
756
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000757/// MatchAddress - Add the specified node to the specified addressing mode,
758/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000759/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000760bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000761 bool isRoot, unsigned Depth) {
Evan Chengda43bcf2008-09-24 00:05:32 +0000762 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000763 // Limit recursion.
764 if (Depth > 5)
765 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000766
Evan Cheng25ab6902006-09-08 06:48:29 +0000767 // RIP relative addressing: %rip + 32-bit displacement!
768 if (AM.isRIPRel) {
769 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000770 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000771 if (isInt32(AM.Disp + Val)) {
772 AM.Disp += Val;
773 return false;
774 }
775 }
776 return true;
777 }
778
Gabor Greifba36cb52008-08-28 21:40:38 +0000779 int id = N.getNode()->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000780 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000781
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000782 switch (N.getOpcode()) {
783 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000784 case ISD::Constant: {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000785 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000786 if (isInt32(AM.Disp + Val)) {
787 AM.Disp += Val;
788 return false;
789 }
790 break;
791 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000792
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000793 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000794DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
795DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
796DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000797 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000798 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000799 // Also, base and index reg must be 0 in order to use rip as base.
800 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000801 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000802 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000803 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
804 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000805 // If value is available in a register both base and index components have
806 // been picked, we can't fit the result available in the register in the
807 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greifba36cb52008-08-28 21:40:38 +0000808 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +0000809 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000810 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
811 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000812 AM.GV = GV;
813 AM.Disp += G->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000814 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000815 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000816 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000817 AM.CP = CP->getConstVal();
818 AM.Align = CP->getAlignment();
819 AM.Disp += CP->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000820 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000821 return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000822 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000823 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000824 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000825 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000826 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000827 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000828 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000829 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000830 }
831 }
832 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000833 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000834
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000835 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000836 if (AM.BaseType == X86ISelAddressMode::RegBase
837 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000838 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
839 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
840 return false;
841 }
842 break;
Evan Chengec693f72005-12-08 02:01:35 +0000843
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844 case ISD::SHL:
Gabor Greif93c53e52008-08-31 15:37:04 +0000845 if (AlreadySelected || AM.IndexReg.getNode() != 0
846 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000847 break;
848
Gabor Greif93c53e52008-08-31 15:37:04 +0000849 if (ConstantSDNode
850 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000851 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000852 if (Val == 1 || Val == 2 || Val == 3) {
853 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000854 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000855
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000856 // Okay, we know that we have a scale by now. However, if the scaled
857 // value is an add of something and a constant, we can fold the
858 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000859 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
860 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
861 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000862 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000863 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000864 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000865 if (isInt32(Disp))
866 AM.Disp = Disp;
867 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000868 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000869 } else {
870 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000871 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000872 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000873 }
874 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000875 }
Evan Chengec693f72005-12-08 02:01:35 +0000876
Dan Gohman83688052007-10-22 20:22:24 +0000877 case ISD::SMUL_LOHI:
878 case ISD::UMUL_LOHI:
879 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000880 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000881 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000882 case ISD::MUL:
883 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000884 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000885 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000886 AM.Base.Reg.getNode() == 0 &&
887 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000888 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000889 if (ConstantSDNode
890 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000891 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
892 CN->getZExtValue() == 9) {
893 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894
Gabor Greifba36cb52008-08-28 21:40:38 +0000895 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000897
898 // Okay, we know that we have a scale by now. However, if the scaled
899 // value is an add of something and a constant, we can fold the
900 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000901 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
902 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
903 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000904 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000905 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000906 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
907 CN->getZExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000908 if (isInt32(Disp))
909 AM.Disp = Disp;
910 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000911 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000912 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914 }
915
916 AM.IndexReg = AM.Base.Reg = Reg;
917 return false;
918 }
Chris Lattner62412262007-02-04 20:18:17 +0000919 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000920 break;
921
Chris Lattner62412262007-02-04 20:18:17 +0000922 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000923 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000924 X86ISelAddressMode Backup = AM;
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
926 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000927 return false;
928 AM = Backup;
Gabor Greifba36cb52008-08-28 21:40:38 +0000929 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
930 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000931 return false;
932 AM = Backup;
933 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000934 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000935
Chris Lattner62412262007-02-04 20:18:17 +0000936 case ISD::OR:
937 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000938 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000939
940 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
941 X86ISelAddressMode Backup = AM;
942 // Start with the LHS as an addr mode.
943 if (!MatchAddress(N.getOperand(0), AM, false) &&
944 // Address could not have picked a GV address for the displacement.
945 AM.GV == NULL &&
946 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman7810bfe2008-09-26 21:54:37 +0000947 isInt32(AM.Disp + CN->getSExtValue()) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000948 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000949 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000950 AM.Disp += CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000951 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000952 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000953 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000954 }
955 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000956
957 case ISD::AND: {
958 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
959 // allows us to fold the shift into this addressing mode.
960 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000961 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000962 if (Shift.getOpcode() != ISD::SHL) break;
963
964 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000965 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000966
967 // Not when RIP is used as the base.
968 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000969
970 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
971 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
972 if (!C1 || !C2) break;
973
974 // Not likely to be profitable if either the AND or SHIFT node has more
975 // than one use (unless all uses are for address computation). Besides,
976 // isel mechanism requires their node ids to be reused.
977 if (!N.hasOneUse() || !Shift.hasOneUse())
978 break;
979
980 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000981 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +0000982 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
983 break;
984
985 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +0000986 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +0000987 SDValue(C2, 0), SDValue(C1, 0));
Dan Gohman475871a2008-07-27 21:46:04 +0000988 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +0000989 Shift.getOperand(0), NewANDMask);
Dan Gohman7b8e9642008-10-13 20:52:04 +0000990 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(),
991 NewAND, SDValue(C1, 0));
Gabor Greifba36cb52008-08-28 21:40:38 +0000992 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
993 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Dan Gohman7b8e9642008-10-13 20:52:04 +0000994 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +0000995
996 AM.Scale = 1 << ShiftCst;
997 AM.IndexReg = NewAND;
998 return false;
999 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001000 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001001
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001002 return MatchAddressBase(N, AM, isRoot, Depth);
1003}
1004
1005/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1006/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001007bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001008 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001009 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001010 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001011 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001012 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001013 AM.IndexReg = N;
1014 AM.Scale = 1;
1015 return false;
1016 }
1017
1018 // Otherwise, we cannot select it.
1019 return true;
1020 }
1021
1022 // Default, generate it as a register.
1023 AM.BaseType = X86ISelAddressMode::RegBase;
1024 AM.Base.Reg = N;
1025 return false;
1026}
1027
Evan Chengec693f72005-12-08 02:01:35 +00001028/// SelectAddr - returns true if it is able pattern match an addressing mode.
1029/// It returns the operands which make up the maximal addressing mode it can
1030/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001031bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1032 SDValue &Scale, SDValue &Index,
1033 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001034 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001035 if (MatchAddress(N, AM))
1036 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001037
Duncan Sands83ec4b62008-06-06 12:08:01 +00001038 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001039 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001040 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001041 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001042 }
Evan Cheng8700e142006-01-11 06:09:51 +00001043
Gabor Greifba36cb52008-08-28 21:40:38 +00001044 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001045 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001046
1047 getAddressOperands(AM, Base, Scale, Index, Disp);
1048 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001049}
1050
Chris Lattner3a7cd952006-10-07 21:55:32 +00001051/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1052/// match a load whose top elements are either undef or zeros. The load flavor
1053/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001054bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1055 SDValue N, SDValue &Base,
1056 SDValue &Scale, SDValue &Index,
1057 SDValue &Disp, SDValue &InChain,
1058 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001059 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001060 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001061 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001062 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001063 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001064 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001065 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001066 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001067 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001068 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001069 return true;
1070 }
1071 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001072
1073 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001074 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001076 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001077 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001078 N.getOperand(0).getNode()->hasOneUse() &&
1079 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001080 N.getOperand(0).getOperand(0).hasOneUse()) {
1081 // Okay, this is a zero extending load. Fold it.
1082 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1083 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1084 return false;
1085 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001086 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001087 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001088 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001089 return false;
1090}
1091
1092
Evan Cheng51a9ed92006-02-25 10:09:08 +00001093/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1094/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001095bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1096 SDValue &Base, SDValue &Scale,
1097 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001098 X86ISelAddressMode AM;
1099 if (MatchAddress(N, AM))
1100 return false;
1101
Duncan Sands83ec4b62008-06-06 12:08:01 +00001102 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001103 unsigned Complexity = 0;
1104 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001105 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001106 Complexity = 1;
1107 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001108 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001109 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1110 Complexity = 4;
1111
Gabor Greifba36cb52008-08-28 21:40:38 +00001112 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001113 Complexity++;
1114 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001115 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001116
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001117 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1118 // a simple shift.
1119 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001120 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001121
1122 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1123 // to a LEA. This is determined with some expermentation but is by no means
1124 // optimal (especially for code size consideration). LEA is nice because of
1125 // its three-address nature. Tweak the cost function again when we can run
1126 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001127 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1128 // For X86-64, we should always use lea to materialize RIP relative
1129 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001130 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001131 Complexity = 4;
1132 else
1133 Complexity += 2;
1134 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001135
Gabor Greifba36cb52008-08-28 21:40:38 +00001136 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001137 Complexity++;
1138
1139 if (Complexity > 2) {
1140 getAddressOperands(AM, Base, Scale, Index, Disp);
1141 return true;
1142 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001143 return false;
1144}
1145
Dan Gohman475871a2008-07-27 21:46:04 +00001146bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1147 SDValue &Base, SDValue &Scale,
1148 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001149 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001150 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001151 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001152 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001153 return false;
1154}
1155
Dan Gohman8b746962008-09-23 18:22:58 +00001156/// getGlobalBaseReg - Return an SDNode that returns the value of
1157/// the global base register. Output instructions required to
1158/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001159///
Evan Cheng9ade2182006-08-26 05:34:46 +00001160SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001161 MachineFunction *MF = CurBB->getParent();
1162 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001163 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001164}
1165
Evan Chengb245d922006-05-20 01:36:52 +00001166static SDNode *FindCallStartFromCall(SDNode *Node) {
1167 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1168 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1169 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001170 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001171}
1172
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001173/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1174/// truncate of the specified operand to i8. This can be done with tablegen,
1175/// except that this code uses MVT::Flag in a tricky way that happens to
1176/// improve scheduling in some cases.
1177SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1178 assert(!Subtarget->is64Bit() &&
1179 "getTruncateTo8Bit is only needed on x86-32!");
1180 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1181
1182 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1183 unsigned Opc;
1184 MVT N0VT = N0.getValueType();
1185 switch (N0VT.getSimpleVT()) {
1186 default: assert(0 && "Unknown truncate!");
1187 case MVT::i16:
1188 Opc = X86::MOV16to16_;
1189 break;
1190 case MVT::i32:
1191 Opc = X86::MOV32to32_;
1192 break;
1193 }
1194
1195 // The use of MVT::Flag here is not strictly accurate, but it helps
1196 // scheduling in some cases.
1197 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1198 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1199 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001200}
1201
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001202SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1203 SDValue Chain = Node->getOperand(0);
1204 SDValue In1 = Node->getOperand(1);
1205 SDValue In2L = Node->getOperand(2);
1206 SDValue In2H = Node->getOperand(3);
1207 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1208 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1209 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001210 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001211 AddToISelQueue(Tmp0);
1212 AddToISelQueue(Tmp1);
1213 AddToISelQueue(Tmp2);
1214 AddToISelQueue(Tmp3);
1215 AddToISelQueue(In2L);
1216 AddToISelQueue(In2H);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001217 // For now, don't select the MemOperand object, we don't know how.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001218 AddToISelQueue(Chain);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001219 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1220 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1221}
Christopher Lambc59e5212007-08-10 21:48:46 +00001222
Dan Gohman475871a2008-07-27 21:46:04 +00001223SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001226 unsigned Opc, MOpc;
1227 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001228
Evan Chengf597dc72006-02-10 22:24:32 +00001229#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001230 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001231 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001232 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001233 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001234#endif
1235
Dan Gohmane8be6c62008-07-17 19:10:17 +00001236 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001237#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001238 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001239 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001240 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001241 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001242#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001243 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001244 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001245
Evan Cheng0114e942006-01-06 20:36:21 +00001246 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001247 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001248 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001249 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001250
Evan Cheng51a9ed92006-02-25 10:09:08 +00001251 case ISD::ADD: {
1252 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1253 // code and is matched first so to prevent it from being turned into
1254 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001255 // In 64-bit small code size mode, use LEA to take advantage of
1256 // RIP-relative addressing.
1257 if (TM.getCodeModel() != CodeModel::Small)
1258 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001259 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001260 SDValue N0 = N.getOperand(0);
1261 SDValue N1 = N.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001262 if (N.getNode()->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001263 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001264 N1.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001265 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001266 SDValue C(0, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001267 // TODO: handle ExternalSymbolSDNode.
Evan Cheng51a9ed92006-02-25 10:09:08 +00001268 if (GlobalAddressSDNode *G =
1269 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001270 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001271 G->getOffset() + Offset);
1272 } else if (ConstantPoolSDNode *CP =
1273 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001274 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001275 CP->getAlignment(),
1276 CP->getOffset()+Offset);
1277 }
1278
Gabor Greifba36cb52008-08-28 21:40:38 +00001279 if (C.getNode()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001280 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001281 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001282 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif93c53e52008-08-31 15:37:04 +00001283 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1284 MVT::i64, Ops, 4);
Evan Cheng25ab6902006-09-08 06:48:29 +00001285 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Evan Cheng25ab6902006-09-08 06:48:29 +00001287 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001288 }
1289
1290 // Other cases are handled by auto-generated code.
1291 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001292 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001293
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001294 case X86ISD::ATOMOR64_DAG:
1295 return SelectAtomic64(Node, X86::ATOMOR6432);
1296 case X86ISD::ATOMXOR64_DAG:
1297 return SelectAtomic64(Node, X86::ATOMXOR6432);
1298 case X86ISD::ATOMADD64_DAG:
1299 return SelectAtomic64(Node, X86::ATOMADD6432);
1300 case X86ISD::ATOMSUB64_DAG:
1301 return SelectAtomic64(Node, X86::ATOMSUB6432);
1302 case X86ISD::ATOMNAND64_DAG:
1303 return SelectAtomic64(Node, X86::ATOMNAND6432);
1304 case X86ISD::ATOMAND64_DAG:
1305 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001306 case X86ISD::ATOMSWAP64_DAG:
1307 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001308
Dan Gohman525178c2007-10-08 18:33:35 +00001309 case ISD::SMUL_LOHI:
1310 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue N0 = Node->getOperand(0);
1312 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001313
Dan Gohman525178c2007-10-08 18:33:35 +00001314 bool isSigned = Opcode == ISD::SMUL_LOHI;
1315 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001316 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001317 default: assert(0 && "Unsupported VT!");
1318 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1319 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1320 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001321 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001322 }
1323 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001324 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001325 default: assert(0 && "Unsupported VT!");
1326 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1327 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1328 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001329 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001330 }
1331
1332 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001333 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001334 default: assert(0 && "Unsupported VT!");
1335 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1336 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1337 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001338 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001339 }
1340
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001342 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001343 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001344 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001345 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001346 if (foldedLoad)
1347 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001348 }
1349
Evan Cheng04699902006-08-26 01:05:16 +00001350 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1352 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001353
1354 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001355 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001356 AddToISelQueue(Tmp0);
1357 AddToISelQueue(Tmp1);
1358 AddToISelQueue(Tmp2);
1359 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001361 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001362 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001363 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001364 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001365 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001366 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001367 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001368 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001370 }
1371
Dan Gohman525178c2007-10-08 18:33:35 +00001372 // Copy the low half of the result, if it is needed.
1373 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001375 LoReg, NVT, InFlag);
1376 InFlag = Result.getValue(2);
1377 ReplaceUses(N.getValue(0), Result);
1378#ifndef NDEBUG
1379 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001380 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001381 DOUT << "\n";
1382#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001383 }
Dan Gohman525178c2007-10-08 18:33:35 +00001384 // Copy the high half of the result, if it is needed.
1385 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001386 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001387 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1388 // Prevent use of AH in a REX instruction by referencing AX instead.
1389 // Shift it down 8 bits.
1390 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1391 X86::AX, MVT::i16, InFlag);
1392 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001393 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001394 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001395 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1397 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001398 MVT::i8, Result, SRIdx), 0);
1399 } else {
1400 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1401 HiReg, NVT, InFlag);
1402 InFlag = Result.getValue(2);
1403 }
1404 ReplaceUses(N.getValue(1), Result);
1405#ifndef NDEBUG
1406 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001407 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001408 DOUT << "\n";
1409#endif
1410 }
Evan Cheng34167212006-02-09 00:37:58 +00001411
Evan Chengf597dc72006-02-10 22:24:32 +00001412#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001413 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001414#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001415
Evan Cheng64a752f2006-08-11 09:08:15 +00001416 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001417 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001418
Dan Gohman525178c2007-10-08 18:33:35 +00001419 case ISD::SDIVREM:
1420 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001421 SDValue N0 = Node->getOperand(0);
1422 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001423
1424 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001425 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001426 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001427 default: assert(0 && "Unsupported VT!");
1428 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1429 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1430 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001431 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001432 }
1433 else
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: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1437 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1438 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001439 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001440 }
1441
1442 unsigned LoReg, HiReg;
1443 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001444 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001445 default: assert(0 && "Unsupported VT!");
1446 case MVT::i8:
1447 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001448 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001449 SExtOpcode = X86::CBW;
1450 break;
1451 case MVT::i16:
1452 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001453 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001454 SExtOpcode = X86::CWD;
1455 break;
1456 case MVT::i32:
1457 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001458 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001459 SExtOpcode = X86::CDQ;
1460 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001461 case MVT::i64:
1462 LoReg = X86::RAX; HiReg = X86::RDX;
1463 ClrOpcode = X86::MOV64r0;
1464 SExtOpcode = X86::CQO;
1465 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001466 }
1467
Dan Gohman475871a2008-07-27 21:46:04 +00001468 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001469 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1470
Dan Gohman475871a2008-07-27 21:46:04 +00001471 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001472 if (NVT == MVT::i8 && !isSigned) {
1473 // Special case for div8, just use a move with zero extension to AX to
1474 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001475 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001476 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001477 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001478 AddToISelQueue(N0.getOperand(0));
1479 AddToISelQueue(Tmp0);
1480 AddToISelQueue(Tmp1);
1481 AddToISelQueue(Tmp2);
1482 AddToISelQueue(Tmp3);
1483 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001485 Ops, 5), 0);
1486 Chain = Move.getValue(1);
1487 ReplaceUses(N0.getValue(1), Chain);
1488 } else {
1489 AddToISelQueue(N0);
1490 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001492 Chain = CurDAG->getEntryNode();
1493 }
Dan Gohman475871a2008-07-27 21:46:04 +00001494 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001495 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001496 } else {
1497 AddToISelQueue(N0);
1498 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001499 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001500 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001501 if (isSigned) {
1502 // Sign extend the low part into the high part.
1503 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001504 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001505 } else {
1506 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001508 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1509 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001510 }
Evan Cheng948f3432006-01-06 23:19:29 +00001511 }
1512
1513 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001514 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001515 AddToISelQueue(Tmp0);
1516 AddToISelQueue(Tmp1);
1517 AddToISelQueue(Tmp2);
1518 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001519 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001520 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001521 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001522 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001523 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001524 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001525 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001526 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001527 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001529 }
1530
Dan Gohmana37c9f72007-09-25 18:23:27 +00001531 // Copy the division (low) result, if it is needed.
1532 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001533 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001534 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001535 InFlag = Result.getValue(2);
1536 ReplaceUses(N.getValue(0), Result);
1537#ifndef NDEBUG
1538 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001539 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001540 DOUT << "\n";
1541#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001542 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001543 // Copy the remainder (high) result, if it is needed.
1544 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001545 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001546 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1547 // Prevent use of AH in a REX instruction by referencing AX instead.
1548 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001549 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1550 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001551 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001552 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001553 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001554 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001555 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1556 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001557 MVT::i8, Result, SRIdx), 0);
1558 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001559 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1560 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001561 InFlag = Result.getValue(2);
1562 }
1563 ReplaceUses(N.getValue(1), Result);
1564#ifndef NDEBUG
1565 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001567 DOUT << "\n";
1568#endif
1569 }
Evan Chengf597dc72006-02-10 22:24:32 +00001570
1571#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001572 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001573#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001574
1575 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001576 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001577
Christopher Lambc59e5212007-08-10 21:48:46 +00001578 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001579 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001580 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1581 SDValue N0 = Node->getOperand(0);
1582 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001583
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001584 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1585 unsigned Opc = 0;
1586 switch (NVT.getSimpleVT()) {
1587 default: assert(0 && "Unknown sign_extend_inreg!");
1588 case MVT::i16:
1589 Opc = X86::MOVSX16rr8;
1590 break;
1591 case MVT::i32:
1592 Opc = X86::MOVSX32rr8;
1593 break;
1594 }
1595
1596 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001597
1598#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001599 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001600 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001601 DOUT << "\n";
1602 DOUT << std::string(Indent-2, ' ') << "=> ";
1603 DEBUG(ResNode->dump(CurDAG));
1604 DOUT << "\n";
1605 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001606#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001607 return ResNode;
1608 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001609 break;
1610 }
1611
1612 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001613 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1614 SDValue Input = Node->getOperand(0);
1615 AddToISelQueue(Node->getOperand(0));
1616 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001617
Evan Cheng403be7e2006-05-08 08:01:26 +00001618#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001619 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001620 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001621 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001622 Indent -= 2;
1623#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001624 return ResNode;
1625 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001626 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001627 }
Evan Cheng851bc042008-06-17 02:01:22 +00001628
1629 case ISD::DECLARE: {
1630 // Handle DECLARE nodes here because the second operand may have been
1631 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue Chain = Node->getOperand(0);
1633 SDValue N1 = Node->getOperand(1);
1634 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001635 if (!isa<FrameIndexSDNode>(N1))
1636 break;
1637 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1638 if (N2.getOpcode() == ISD::ADD &&
1639 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1640 N2 = N2.getOperand(1);
1641 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001642 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001643 GlobalValue *GV =
1644 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001645 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1646 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001647 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001648 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001649 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1650 MVT::Other, Ops, 3);
1651 }
1652 break;
1653 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001654 }
1655
Evan Cheng9ade2182006-08-26 05:34:46 +00001656 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001657
Evan Chengf597dc72006-02-10 22:24:32 +00001658#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001659 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001660 if (ResNode == NULL || ResNode == N.getNode())
1661 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001662 else
1663 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001664 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001665 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001666#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001667
1668 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001669}
1670
Chris Lattnerc0bad572006-06-08 18:03:49 +00001671bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001672SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001673 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001675 switch (ConstraintCode) {
1676 case 'o': // offsetable ??
1677 case 'v': // not offsetable ??
1678 default: return true;
1679 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001680 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001681 return true;
1682 break;
1683 }
1684
Evan Cheng04699902006-08-26 01:05:16 +00001685 OutOps.push_back(Op0);
1686 OutOps.push_back(Op1);
1687 OutOps.push_back(Op2);
1688 OutOps.push_back(Op3);
1689 AddToISelQueue(Op0);
1690 AddToISelQueue(Op1);
1691 AddToISelQueue(Op2);
1692 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001693 return false;
1694}
1695
Chris Lattnerc961eea2005-11-16 01:54:32 +00001696/// createX86ISelDag - This pass converts a legalized DAG into a
1697/// X86-specific DAG, ready for instruction scheduling.
1698///
Evan Chenge50794a2006-08-29 18:28:33 +00001699FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1700 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001701}