blob: 5c80f2375339dd261356579d0ebc7dcecdd62932 [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//
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 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"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000036#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000037#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000038#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000039using namespace llvm;
40
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
42STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
43
44
Chris Lattnerc961eea2005-11-16 01:54:32 +000045//===----------------------------------------------------------------------===//
46// Pattern Matcher Implementation
47//===----------------------------------------------------------------------===//
48
49namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000050 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
51 /// SDOperand's instead of register numbers for the leaves of the matched
52 /// tree.
53 struct X86ISelAddressMode {
54 enum {
55 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000056 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000057 } BaseType;
58
59 struct { // This is really a union, discriminated by BaseType!
60 SDOperand Reg;
61 int FrameIndex;
62 } Base;
63
Evan Cheng25ab6902006-09-08 06:48:29 +000064 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 unsigned Scale;
66 SDOperand IndexReg;
67 unsigned Disp;
68 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000069 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000070 const char *ES;
71 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000072 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073
74 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000075 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
76 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000077 }
78 };
79}
80
81namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000082 //===--------------------------------------------------------------------===//
83 /// ISel - X86 specific code to select X86 machine instructions for
84 /// SelectionDAG operations.
85 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000086 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000087 /// ContainsFPCode - Every instruction we select that uses or defines a FP
88 /// register should set this to true.
89 bool ContainsFPCode;
90
Evan Chenge50794a2006-08-29 18:28:33 +000091 /// FastISel - Enable fast(er) instruction selection.
92 ///
93 bool FastISel;
94
Evan Cheng25ab6902006-09-08 06:48:29 +000095 /// TM - Keep a reference to X86TargetMachine.
96 ///
97 X86TargetMachine &TM;
98
Chris Lattnerc961eea2005-11-16 01:54:32 +000099 /// X86Lowering - This object fully describes how to lower LLVM code to an
100 /// X86-specific SelectionDAG.
101 X86TargetLowering X86Lowering;
102
103 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
104 /// make the right decision when generating code for different targets.
105 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000106
Evan Cheng25ab6902006-09-08 06:48:29 +0000107 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
108 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000109 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000110
Chris Lattnerc961eea2005-11-16 01:54:32 +0000111 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000112 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000113 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000115 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000116 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000117
Evan Cheng7ccced62006-02-18 00:15:05 +0000118 virtual bool runOnFunction(Function &Fn) {
119 // Make sure we re-emit a set of the global base reg if necessary
120 GlobalBaseReg = 0;
121 return SelectionDAGISel::runOnFunction(Fn);
122 }
123
Chris Lattnerc961eea2005-11-16 01:54:32 +0000124 virtual const char *getPassName() const {
125 return "X86 DAG->DAG Instruction Selection";
126 }
127
128 /// InstructionSelectBasicBlock - This callback is invoked by
129 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
130 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
131
Evan Cheng8700e142006-01-11 06:09:51 +0000132 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
133
Evan Cheng27e1fe92006-10-14 08:33:25 +0000134 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000135
Chris Lattnerc961eea2005-11-16 01:54:32 +0000136// Include the pieces autogenerated from the target description.
137#include "X86GenDAGISel.inc"
138
139 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000140 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000141
Evan Cheng2486af12006-02-11 02:05:36 +0000142 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Cheng0d538262006-11-08 20:34:28 +0000143 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
144 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
145 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
146 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
147 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000148 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000149 SDOperand &Index, SDOperand &Disp,
150 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000151 bool TryFoldLoad(SDOperand P, SDOperand N,
152 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000153 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000154 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000155
Chris Lattnerc0bad572006-06-08 18:03:49 +0000156 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
157 /// inline asm expressions.
158 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
159 char ConstraintCode,
160 std::vector<SDOperand> &OutOps,
161 SelectionDAG &DAG);
162
Evan Cheng3649b0e2006-06-02 22:38:37 +0000163 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
164
Evan Chenge5280532005-12-12 21:49:40 +0000165 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
166 SDOperand &Scale, SDOperand &Index,
167 SDOperand &Disp) {
168 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
170 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000171 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000172 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 // These are 32-bit even in 64-bit mode since RIP relative offset
174 // is 32-bit.
175 if (AM.GV)
176 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
177 else if (AM.CP)
178 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
179 else if (AM.ES)
180 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
181 else if (AM.JT != -1)
182 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
183 else
184 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000185 }
186
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000187 /// getI8Imm - Return a target constant with the specified value, of type
188 /// i8.
189 inline SDOperand getI8Imm(unsigned Imm) {
190 return CurDAG->getTargetConstant(Imm, MVT::i8);
191 }
192
Chris Lattnerc961eea2005-11-16 01:54:32 +0000193 /// getI16Imm - Return a target constant with the specified value, of type
194 /// i16.
195 inline SDOperand getI16Imm(unsigned Imm) {
196 return CurDAG->getTargetConstant(Imm, MVT::i16);
197 }
198
199 /// getI32Imm - Return a target constant with the specified value, of type
200 /// i32.
201 inline SDOperand getI32Imm(unsigned Imm) {
202 return CurDAG->getTargetConstant(Imm, MVT::i32);
203 }
Evan Chengf597dc72006-02-10 22:24:32 +0000204
Evan Cheng7ccced62006-02-18 00:15:05 +0000205 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
206 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000207 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000208
Evan Cheng23addc02006-02-10 22:46:26 +0000209#ifndef NDEBUG
210 unsigned Indent;
211#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000212 };
213}
214
Evan Chenga275ecb2006-10-10 01:46:56 +0000215static SDNode *findFlagUse(SDNode *N) {
216 unsigned FlagResNo = N->getNumValues()-1;
217 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
218 SDNode *User = *I;
219 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
220 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000221 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000222 return User;
223 }
224 }
225 return NULL;
226}
227
Evan Cheng27e1fe92006-10-14 08:33:25 +0000228static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
229 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000230 std::set<SDNode *> &Visited) {
231 if (found ||
232 Use->getNodeId() > Def->getNodeId() ||
233 !Visited.insert(Use).second)
234 return;
235
Evan Cheng27e1fe92006-10-14 08:33:25 +0000236 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000237 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000238 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000239 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000240 if (N == Def) {
241 if (Use == ImmedUse)
242 continue; // Immediate use is ok.
243 if (Use == Root) {
244 assert(Use->getOpcode() == ISD::STORE ||
245 Use->getOpcode() == X86ISD::CMP);
246 continue;
247 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000248 found = true;
249 break;
250 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000251 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000252 }
253}
254
Evan Cheng27e1fe92006-10-14 08:33:25 +0000255/// isNonImmUse - Start searching from Root up the DAG to check is Def can
256/// be reached. Return true if that's the case. However, ignore direct uses
257/// by ImmedUse (which would be U in the example illustrated in
258/// CanBeFoldedBy) and by Root (which can happen in the store case).
259/// FIXME: to be really generic, we should allow direct use by any node
260/// that is being folded. But realisticly since we only fold loads which
261/// have one non-chain use, we only need to watch out for load/op/store
262/// and load/op/cmp case where the root (store / cmp) may reach the load via
263/// its chain operand.
264static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
265 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000266 std::set<SDNode *> Visited;
267 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000268 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000269 return found;
270}
271
272
Evan Cheng27e1fe92006-10-14 08:33:25 +0000273bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) {
274 if (FastISel) return false;
275
Evan Chenga8df1b42006-07-27 16:44:36 +0000276 // If U use can somehow reach N through another path then U can't fold N or
277 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000278 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000279 // a successor of U.
280 //
281 // [ N ]
282 // ^ ^
283 // | |
284 // / \---
285 // / [X]
286 // | ^
287 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000288
289 if (isNonImmUse(Root, N, U))
290 return false;
291
292 // If U produces a flag, then it gets (even more) interesting. Since it
293 // would have been "glued" together with its flag use, we need to check if
294 // it might reach N:
295 //
296 // [ N ]
297 // ^ ^
298 // | |
299 // [U] \--
300 // ^ [TF]
301 // | ^
302 // | |
303 // \ /
304 // [FU]
305 //
306 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
307 // NU), then TF is a predecessor of FU and a successor of NU. But since
308 // NU and FU are flagged together, this effectively creates a cycle.
309 bool HasFlagUse = false;
310 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
311 while ((VT == MVT::Flag && !Root->use_empty())) {
312 SDNode *FU = findFlagUse(Root);
313 if (FU == NULL)
314 break;
315 else {
316 Root = FU;
317 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000318 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000319 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000320 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000321
322 if (HasFlagUse)
323 return !isNonImmUse(Root, N, Root, U);
324 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000325}
326
Evan Cheng70e674e2006-08-28 20:10:17 +0000327/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
328/// and move load below the TokenFactor. Replace store's chain operand with
329/// load's chain result.
330static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
331 SDOperand Store, SDOperand TF) {
332 std::vector<SDOperand> Ops;
333 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
334 if (Load.Val == TF.Val->getOperand(i).Val)
335 Ops.push_back(Load.Val->getOperand(0));
336 else
337 Ops.push_back(TF.Val->getOperand(i));
338 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
339 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
340 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
341 Store.getOperand(2), Store.getOperand(3));
342}
343
344/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
345/// selector to pick more load-modify-store instructions. This is a common
346/// case:
347///
348/// [Load chain]
349/// ^
350/// |
351/// [Load]
352/// ^ ^
353/// | |
354/// / \-
355/// / |
356/// [TokenFactor] [Op]
357/// ^ ^
358/// | |
359/// \ /
360/// \ /
361/// [Store]
362///
363/// The fact the store's chain operand != load's chain will prevent the
364/// (store (op (load))) instruction from being selected. We can transform it to:
365///
366/// [Load chain]
367/// ^
368/// |
369/// [TokenFactor]
370/// ^
371/// |
372/// [Load]
373/// ^ ^
374/// | |
375/// | \-
376/// | |
377/// | [Op]
378/// | ^
379/// | |
380/// \ /
381/// \ /
382/// [Store]
383void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
384 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
385 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000386 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000387 continue;
388 SDOperand Chain = I->getOperand(0);
389 if (Chain.Val->getOpcode() != ISD::TokenFactor)
390 continue;
391
392 SDOperand N1 = I->getOperand(1);
393 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000394 if (MVT::isFloatingPoint(N1.getValueType()) ||
395 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000396 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000397 continue;
398
399 bool RModW = false;
400 SDOperand Load;
401 unsigned Opcode = N1.Val->getOpcode();
402 switch (Opcode) {
403 case ISD::ADD:
404 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000405 case ISD::AND:
406 case ISD::OR:
407 case ISD::XOR:
408 case ISD::ADDC:
409 case ISD::ADDE: {
410 SDOperand N10 = N1.getOperand(0);
411 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000412 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000413 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000414 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000415 RModW = true;
416 std::swap(N10, N11);
417 }
418 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000419 (N10.getOperand(1) == N2) &&
420 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000421 if (RModW)
422 Load = N10;
423 break;
424 }
425 case ISD::SUB:
426 case ISD::SHL:
427 case ISD::SRA:
428 case ISD::SRL:
429 case ISD::ROTL:
430 case ISD::ROTR:
431 case ISD::SUBC:
432 case ISD::SUBE:
433 case X86ISD::SHLD:
434 case X86ISD::SHRD: {
435 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000436 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000437 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000438 (N10.getOperand(1) == N2) &&
439 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000440 if (RModW)
441 Load = N10;
442 break;
443 }
444 }
445
Evan Cheng82a35b32006-08-29 06:44:17 +0000446 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000447 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000448 ++NumLoadMoved;
449 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000450 }
451}
452
Chris Lattnerc961eea2005-11-16 01:54:32 +0000453/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
454/// when it has created a SelectionDAG for us to codegen.
455void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
456 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000457 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000458
Evan Chenge50794a2006-08-29 18:28:33 +0000459 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000460 InstructionSelectPreprocess(DAG);
461
Chris Lattnerc961eea2005-11-16 01:54:32 +0000462 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000463#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000464 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000465 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000466#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000467 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000468#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000469 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000470#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000471
Chris Lattnerc961eea2005-11-16 01:54:32 +0000472 DAG.RemoveDeadNodes();
473
474 // Emit machine code to BB.
475 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000476
477 // If we are emitting FP stack code, scan the basic block to determine if this
478 // block defines any FP values. If so, put an FP_REG_KILL instruction before
479 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000480 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000481 // Note that FP stack instructions *are* used in SSE code when returning
482 // values, but these are not live out of the basic block, so we don't need
483 // an FP_REG_KILL in this case either.
484 bool ContainsFPCode = false;
485
486 // Scan all of the machine instructions in these MBBs, checking for FP
487 // stores.
488 MachineFunction::iterator MBBI = FirstMBB;
489 do {
490 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
491 !ContainsFPCode && I != E; ++I) {
492 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
493 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
494 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
495 RegMap->getRegClass(I->getOperand(0).getReg()) ==
496 X86::RFPRegisterClass) {
497 ContainsFPCode = true;
498 break;
499 }
500 }
501 }
502 } while (!ContainsFPCode && &*(MBBI++) != BB);
503
504 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
505 // a copy of the input value in this block.
506 if (!ContainsFPCode) {
507 // Final check, check LLVM BB's that are successors to the LLVM BB
508 // corresponding to BB for FP PHI nodes.
509 const BasicBlock *LLVMBB = BB->getBasicBlock();
510 const PHINode *PN;
511 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
512 !ContainsFPCode && SI != E; ++SI) {
513 for (BasicBlock::const_iterator II = SI->begin();
514 (PN = dyn_cast<PHINode>(II)); ++II) {
515 if (PN->getType()->isFloatingPoint()) {
516 ContainsFPCode = true;
517 break;
518 }
519 }
520 }
521 }
522
523 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
524 if (ContainsFPCode) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000525 BuildMI(*BB, BB->getFirstTerminator(),
526 TM.getInstrInfo()->get(X86::FP_REG_KILL));
Chris Lattner92cb0af2006-01-11 01:15:34 +0000527 ++NumFPKill;
528 }
529 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000530}
531
Evan Cheng8700e142006-01-11 06:09:51 +0000532/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
533/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000534void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
535 MachineFrameInfo *MFI) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000536 const TargetInstrInfo *TII = TM.getInstrInfo();
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000537 if (Subtarget->isTargetCygMing())
Evan Chengc0f64ff2006-11-27 23:37:22 +0000538 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Evan Cheng3649b0e2006-06-02 22:38:37 +0000539
Evan Cheng8700e142006-01-11 06:09:51 +0000540 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
541 int CWFrameIdx = MFI->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000542 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000543
544 // Set the high part to be 64-bit precision.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000545 addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
Evan Cheng8700e142006-01-11 06:09:51 +0000546 CWFrameIdx, 1).addImm(2);
547
548 // Reload the modified control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000549 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000550}
551
552void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
553 // If this is main, emit special code for main.
554 MachineBasicBlock *BB = MF.begin();
555 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
556 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
557}
558
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000559/// MatchAddress - Add the specified node to the specified addressing mode,
560/// returning true if it cannot be done. This just pattern matches for the
561/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000562bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
563 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000564 // RIP relative addressing: %rip + 32-bit displacement!
565 if (AM.isRIPRel) {
566 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000567 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000568 if (isInt32(AM.Disp + Val)) {
569 AM.Disp += Val;
570 return false;
571 }
572 }
573 return true;
574 }
575
Evan Cheng2ef88a02006-08-07 22:28:20 +0000576 int id = N.Val->getNodeId();
577 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000578
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000579 switch (N.getOpcode()) {
580 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000581 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000582 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000583 if (isInt32(AM.Disp + Val)) {
584 AM.Disp += Val;
585 return false;
586 }
587 break;
588 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000589
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000590 case X86ISD::Wrapper: {
591 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000592 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000593 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
Evan Cheng0085a282006-11-30 21:55:46 +0000594 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000595 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
596 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000597 // If value is available in a register both base and index components have
598 // been picked, we can't fit the result available in the register in the
599 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng49463992006-11-29 23:46:27 +0000600 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000601 bool isStatic = TM.getRelocationModel() == Reloc::Static;
602 SDOperand N0 = N.getOperand(0);
603 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
604 GlobalValue *GV = G->getGlobal();
Evan Chenga70d14b2006-12-19 21:31:42 +0000605 bool isAbs32 = !is64Bit || isStatic;
Evan Cheng28b514392006-12-05 19:50:18 +0000606 if (isAbs32 || isRoot) {
Evan Chenga70d14b2006-12-19 21:31:42 +0000607 AM.GV = GV;
Evan Cheng28b514392006-12-05 19:50:18 +0000608 AM.Disp += G->getOffset();
609 AM.isRIPRel = !isAbs32;
610 return false;
611 }
612 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
613 if (!is64Bit || isStatic || isRoot) {
Evan Chengc356a572006-09-12 21:04:05 +0000614 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000615 AM.Align = CP->getAlignment();
616 AM.Disp += CP->getOffset();
Evan Cheng28b514392006-12-05 19:50:18 +0000617 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000618 return false;
619 }
Evan Cheng28b514392006-12-05 19:50:18 +0000620 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
621 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000622 AM.ES = S->getSymbol();
Evan Cheng28b514392006-12-05 19:50:18 +0000623 AM.isRIPRel = !isStatic;
Evan Cheng25ab6902006-09-08 06:48:29 +0000624 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000625 }
626 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
627 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000628 AM.JT = J->getIndex();
Evan Cheng28b514392006-12-05 19:50:18 +0000629 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000630 return false;
631 }
632 }
633 }
634 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000635 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000636
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000637 case ISD::FrameIndex:
638 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
639 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
640 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
641 return false;
642 }
643 break;
Evan Chengec693f72005-12-08 02:01:35 +0000644
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000645 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000646 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000647 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
648 unsigned Val = CN->getValue();
649 if (Val == 1 || Val == 2 || Val == 3) {
650 AM.Scale = 1 << Val;
651 SDOperand ShVal = N.Val->getOperand(0);
652
653 // Okay, we know that we have a scale by now. However, if the scaled
654 // value is an add of something and a constant, we can fold the
655 // constant into the disp field here.
656 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
657 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
658 AM.IndexReg = ShVal.Val->getOperand(0);
659 ConstantSDNode *AddVal =
660 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000661 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
Evan Cheng25ab6902006-09-08 06:48:29 +0000662 if (isInt32(Disp))
663 AM.Disp = Disp;
664 else
665 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000666 } else {
667 AM.IndexReg = ShVal;
668 }
669 return false;
670 }
671 }
672 break;
Evan Chengec693f72005-12-08 02:01:35 +0000673
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000674 case ISD::MUL:
675 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000676 if (!Available &&
677 AM.BaseType == X86ISelAddressMode::RegBase &&
678 AM.Base.Reg.Val == 0 &&
679 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000680 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
681 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
682 AM.Scale = unsigned(CN->getValue())-1;
683
684 SDOperand MulVal = N.Val->getOperand(0);
685 SDOperand Reg;
686
687 // Okay, we know that we have a scale by now. However, if the scaled
688 // value is an add of something and a constant, we can fold the
689 // constant into the disp field here.
690 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
691 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
692 Reg = MulVal.Val->getOperand(0);
693 ConstantSDNode *AddVal =
694 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000695 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
696 if (isInt32(Disp))
697 AM.Disp = Disp;
698 else
699 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000700 } else {
701 Reg = N.Val->getOperand(0);
702 }
703
704 AM.IndexReg = AM.Base.Reg = Reg;
705 return false;
706 }
707 break;
708
709 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000710 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000711 X86ISelAddressMode Backup = AM;
712 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
713 !MatchAddress(N.Val->getOperand(1), AM, false))
714 return false;
715 AM = Backup;
716 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
717 !MatchAddress(N.Val->getOperand(0), AM, false))
718 return false;
719 AM = Backup;
720 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000721 break;
722 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000723
724 case ISD::OR: {
725 if (!Available) {
726 X86ISelAddressMode Backup = AM;
727 // Look for (x << c1) | c2 where (c2 < c1)
728 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
729 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
730 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
731 AM.Disp = CN->getValue();
732 return false;
733 }
734 }
735 AM = Backup;
736 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
737 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
738 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
739 AM.Disp = CN->getValue();
740 return false;
741 }
742 }
743 AM = Backup;
744 }
745 break;
746 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000747 }
748
749 // Is the base register already occupied?
750 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
751 // If so, check to see if the scale index register is set.
752 if (AM.IndexReg.Val == 0) {
753 AM.IndexReg = N;
754 AM.Scale = 1;
755 return false;
756 }
757
758 // Otherwise, we cannot select it.
759 return true;
760 }
761
762 // Default, generate it as a register.
763 AM.BaseType = X86ISelAddressMode::RegBase;
764 AM.Base.Reg = N;
765 return false;
766}
767
Evan Chengec693f72005-12-08 02:01:35 +0000768/// SelectAddr - returns true if it is able pattern match an addressing mode.
769/// It returns the operands which make up the maximal addressing mode it can
770/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000771bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
772 SDOperand &Scale, SDOperand &Index,
773 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000774 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000775 if (MatchAddress(N, AM))
776 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000777
Evan Cheng25ab6902006-09-08 06:48:29 +0000778 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000779 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000780 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000781 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000782 }
Evan Cheng8700e142006-01-11 06:09:51 +0000783
Evan Cheng7dd281b2006-02-05 05:25:07 +0000784 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000785 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000786
787 getAddressOperands(AM, Base, Scale, Index, Disp);
788 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000789}
790
Chris Lattner4fe4f252006-10-11 22:09:58 +0000791/// isZeroNode - Returns true if Elt is a constant zero or a floating point
792/// constant +0.0.
793static inline bool isZeroNode(SDOperand Elt) {
794 return ((isa<ConstantSDNode>(Elt) &&
795 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
796 (isa<ConstantFPSDNode>(Elt) &&
797 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
798}
799
800
Chris Lattner3a7cd952006-10-07 21:55:32 +0000801/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
802/// match a load whose top elements are either undef or zeros. The load flavor
803/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000804bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000805 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000806 SDOperand &Scale, SDOperand &Index,
807 SDOperand &Disp, SDOperand &InChain,
808 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000809 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000810 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000811 if (ISD::isNON_EXTLoad(InChain.Val) &&
812 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000813 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000814 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000815 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000816 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000817 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000818 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000819 return true;
820 }
821 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000822
823 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000824 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000825 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
826 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
827 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
828 N.getOperand(1).Val->hasOneUse() &&
829 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
830 N.getOperand(1).getOperand(0).hasOneUse()) {
831 // Check to see if the BUILD_VECTOR is building a zero vector.
832 SDOperand BV = N.getOperand(0);
833 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
834 if (!isZeroNode(BV.getOperand(i)) &&
835 BV.getOperand(i).getOpcode() != ISD::UNDEF)
836 return false; // Not a zero/undef vector.
837 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
838 // from the LHS.
839 unsigned VecWidth = BV.getNumOperands();
840 SDOperand ShufMask = N.getOperand(2);
841 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
842 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
843 if (C->getValue() == VecWidth) {
844 for (unsigned i = 1; i != VecWidth; ++i) {
845 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
846 // ok.
847 } else {
848 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
849 if (C->getValue() >= VecWidth) return false;
850 }
851 }
852 }
853
854 // Okay, this is a zero extending load. Fold it.
855 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000856 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000857 return false;
858 OutChain = LD->getChain();
859 InChain = SDOperand(LD, 1);
860 return true;
861 }
862 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000863 return false;
864}
865
866
Evan Cheng51a9ed92006-02-25 10:09:08 +0000867/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
868/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000869bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
870 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000871 SDOperand &Index, SDOperand &Disp) {
872 X86ISelAddressMode AM;
873 if (MatchAddress(N, AM))
874 return false;
875
Evan Cheng25ab6902006-09-08 06:48:29 +0000876 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000877 unsigned Complexity = 0;
878 if (AM.BaseType == X86ISelAddressMode::RegBase)
879 if (AM.Base.Reg.Val)
880 Complexity = 1;
881 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000882 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000883 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
884 Complexity = 4;
885
886 if (AM.IndexReg.Val)
887 Complexity++;
888 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000889 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000890
Evan Cheng8c03fe42006-02-28 21:13:57 +0000891 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000892 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000893 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
894 else if (AM.Scale > 1)
895 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000896
897 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
898 // to a LEA. This is determined with some expermentation but is by no means
899 // optimal (especially for code size consideration). LEA is nice because of
900 // its three-address nature. Tweak the cost function again when we can run
901 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000902 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
903 // For X86-64, we should always use lea to materialize RIP relative
904 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +0000905 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000906 Complexity = 4;
907 else
908 Complexity += 2;
909 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000910
911 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
912 Complexity++;
913
914 if (Complexity > 2) {
915 getAddressOperands(AM, Base, Scale, Index, Disp);
916 return true;
917 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000918 return false;
919}
920
Evan Cheng5e351682006-02-06 06:02:33 +0000921bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
922 SDOperand &Base, SDOperand &Scale,
923 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000924 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000925 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000926 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000927 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000928 return false;
929}
930
Evan Cheng7ccced62006-02-18 00:15:05 +0000931/// getGlobalBaseReg - Output the instructions required to put the
932/// base address to use for accessing globals into a register.
933///
Evan Cheng9ade2182006-08-26 05:34:46 +0000934SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000935 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000936 if (!GlobalBaseReg) {
937 // Insert the set of GlobalBaseReg into the first MBB of the function
938 MachineBasicBlock &FirstMBB = BB->getParent()->front();
939 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
940 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Evan Cheng069287d2006-05-16 07:21:53 +0000941 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000942 const TargetInstrInfo *TII = TM.getInstrInfo();
943 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
944 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), GlobalBaseReg);
Evan Cheng7ccced62006-02-18 00:15:05 +0000945 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000946 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000947}
948
Evan Chengb245d922006-05-20 01:36:52 +0000949static SDNode *FindCallStartFromCall(SDNode *Node) {
950 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
951 assert(Node->getOperand(0).getValueType() == MVT::Other &&
952 "Node doesn't have a token chain argument!");
953 return FindCallStartFromCall(Node->getOperand(0).Val);
954}
955
Evan Cheng9ade2182006-08-26 05:34:46 +0000956SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000957 SDNode *Node = N.Val;
958 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000959 unsigned Opc, MOpc;
960 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000961
Evan Chengf597dc72006-02-10 22:24:32 +0000962#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000963 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +0000964 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000965 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000966 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000967#endif
968
Evan Cheng34167212006-02-09 00:37:58 +0000969 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000970#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000971 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +0000972 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000973 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000974 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000975#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000976 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000977 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000978
Evan Cheng0114e942006-01-06 20:36:21 +0000979 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000980 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000981 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000982 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000983
Evan Cheng51a9ed92006-02-25 10:09:08 +0000984 case ISD::ADD: {
985 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
986 // code and is matched first so to prevent it from being turned into
987 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000988 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
989 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000990 SDOperand N0 = N.getOperand(0);
991 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000992 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000993 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000994 N1.getOpcode() == ISD::Constant) {
995 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
996 SDOperand C(0, 0);
997 // TODO: handle ExternalSymbolSDNode.
998 if (GlobalAddressSDNode *G =
999 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001000 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001001 G->getOffset() + Offset);
1002 } else if (ConstantPoolSDNode *CP =
1003 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001004 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001005 CP->getAlignment(),
1006 CP->getOffset()+Offset);
1007 }
1008
Evan Cheng25ab6902006-09-08 06:48:29 +00001009 if (C.Val) {
1010 if (Subtarget->is64Bit()) {
1011 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1012 CurDAG->getRegister(0, PtrVT), C };
1013 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1014 } else
1015 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1016 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001017 }
1018
1019 // Other cases are handled by auto-generated code.
1020 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001021 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001022
Evan Cheng0114e942006-01-06 20:36:21 +00001023 case ISD::MULHU:
1024 case ISD::MULHS: {
1025 if (Opcode == ISD::MULHU)
1026 switch (NVT) {
1027 default: assert(0 && "Unsupported VT!");
1028 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1029 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1030 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001031 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001032 }
1033 else
1034 switch (NVT) {
1035 default: assert(0 && "Unsupported VT!");
1036 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1037 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1038 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001039 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001040 }
1041
1042 unsigned LoReg, HiReg;
1043 switch (NVT) {
1044 default: assert(0 && "Unsupported VT!");
1045 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1046 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1047 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001048 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001049 }
1050
1051 SDOperand N0 = Node->getOperand(0);
1052 SDOperand N1 = Node->getOperand(1);
1053
1054 bool foldedLoad = false;
1055 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001056 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001057 // MULHU and MULHS are commmutative
1058 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001059 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001060 if (foldedLoad) {
1061 N0 = Node->getOperand(1);
1062 N1 = Node->getOperand(0);
1063 }
1064 }
1065
Evan Cheng34167212006-02-09 00:37:58 +00001066 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001067 if (foldedLoad) {
1068 Chain = N1.getOperand(0);
1069 AddToISelQueue(Chain);
1070 } else
Evan Cheng34167212006-02-09 00:37:58 +00001071 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001072
Evan Cheng34167212006-02-09 00:37:58 +00001073 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001074 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001075 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001076 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001077 InFlag = Chain.getValue(1);
1078
1079 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001080 AddToISelQueue(Tmp0);
1081 AddToISelQueue(Tmp1);
1082 AddToISelQueue(Tmp2);
1083 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001084 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001085 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001086 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001087 Chain = SDOperand(CNode, 0);
1088 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001089 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001090 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001091 InFlag =
1092 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001093 }
1094
Evan Cheng9ade2182006-08-26 05:34:46 +00001095 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001096 ReplaceUses(N.getValue(0), Result);
1097 if (foldedLoad)
1098 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001099
Evan Chengf597dc72006-02-10 22:24:32 +00001100#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001101 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001102 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001103 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001104 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001105#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001106 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001107 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001108
Evan Cheng948f3432006-01-06 23:19:29 +00001109 case ISD::SDIV:
1110 case ISD::UDIV:
1111 case ISD::SREM:
1112 case ISD::UREM: {
1113 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1114 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1115 if (!isSigned)
1116 switch (NVT) {
1117 default: assert(0 && "Unsupported VT!");
1118 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1119 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1120 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001121 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001122 }
1123 else
1124 switch (NVT) {
1125 default: assert(0 && "Unsupported VT!");
1126 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1127 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1128 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001129 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001130 }
1131
1132 unsigned LoReg, HiReg;
1133 unsigned ClrOpcode, SExtOpcode;
1134 switch (NVT) {
1135 default: assert(0 && "Unsupported VT!");
1136 case MVT::i8:
1137 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001138 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001139 SExtOpcode = X86::CBW;
1140 break;
1141 case MVT::i16:
1142 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001143 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001144 SExtOpcode = X86::CWD;
1145 break;
1146 case MVT::i32:
1147 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001148 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001149 SExtOpcode = X86::CDQ;
1150 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001151 case MVT::i64:
1152 LoReg = X86::RAX; HiReg = X86::RDX;
1153 ClrOpcode = X86::MOV64r0;
1154 SExtOpcode = X86::CQO;
1155 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001156 }
1157
1158 SDOperand N0 = Node->getOperand(0);
1159 SDOperand N1 = Node->getOperand(1);
Evan Cheng34167212006-02-09 00:37:58 +00001160 SDOperand InFlag(0, 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001161 if (NVT == MVT::i8 && !isSigned) {
1162 // Special case for div8, just use a move with zero extension to AX to
1163 // clear the upper 8 bits (AH).
1164 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1165 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1166 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1167 AddToISelQueue(N0.getOperand(0));
1168 AddToISelQueue(Tmp0);
1169 AddToISelQueue(Tmp1);
1170 AddToISelQueue(Tmp2);
1171 AddToISelQueue(Tmp3);
1172 Move =
1173 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1174 Ops, 5), 0);
1175 Chain = Move.getValue(1);
1176 ReplaceUses(N0.getValue(1), Chain);
1177 } else {
1178 AddToISelQueue(N0);
1179 Move =
1180 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1181 Chain = CurDAG->getEntryNode();
1182 }
1183 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001184 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001185 } else {
1186 AddToISelQueue(N0);
1187 InFlag =
1188 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1189 InFlag).getValue(1);
1190 if (isSigned) {
1191 // Sign extend the low part into the high part.
1192 InFlag =
1193 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1194 } else {
1195 // Zero out the high part, effectively zero extending the input.
1196 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1197 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1198 InFlag).getValue(1);
1199 }
Evan Cheng948f3432006-01-06 23:19:29 +00001200 }
1201
Evan Chengb1409ce2006-11-17 22:10:14 +00001202 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1203 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001204 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001205 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001206 AddToISelQueue(Tmp0);
1207 AddToISelQueue(Tmp1);
1208 AddToISelQueue(Tmp2);
1209 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001210 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001211 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001212 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001213 Chain = SDOperand(CNode, 0);
1214 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001215 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001216 AddToISelQueue(N1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001217 Chain = CurDAG->getEntryNode();
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001218 InFlag =
1219 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001220 }
1221
Evan Chengb1409ce2006-11-17 22:10:14 +00001222 SDOperand Result =
1223 CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001224 ReplaceUses(N.getValue(0), Result);
1225 if (foldedLoad)
1226 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001227
1228#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001229 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001230 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001231 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001232 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001233#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001234
1235 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001236 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001237
1238 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001239 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001240 unsigned Opc2;
1241 MVT::ValueType VT;
1242 switch (Node->getOperand(0).getValueType()) {
1243 default: assert(0 && "Unknown truncate!");
1244 case MVT::i16:
1245 Opc = X86::MOV16to16_;
1246 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001247 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001248 break;
1249 case MVT::i32:
1250 Opc = X86::MOV32to32_;
1251 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001252 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001253 break;
1254 }
1255
Evan Cheng04699902006-08-26 01:05:16 +00001256 AddToISelQueue(Node->getOperand(0));
1257 SDOperand Tmp =
1258 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001259 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001260
1261#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001262 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001263 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001264 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001265 Indent -= 2;
1266#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001267 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001268 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001269
1270 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001271 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001272 }
1273
Evan Cheng9ade2182006-08-26 05:34:46 +00001274 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001275
Evan Chengf597dc72006-02-10 22:24:32 +00001276#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001277 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001278 if (ResNode == NULL || ResNode == N.Val)
1279 DEBUG(N.Val->dump(CurDAG));
1280 else
1281 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001282 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001283 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001284#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001285
1286 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001287}
1288
Chris Lattnerc0bad572006-06-08 18:03:49 +00001289bool X86DAGToDAGISel::
1290SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1291 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1292 SDOperand Op0, Op1, Op2, Op3;
1293 switch (ConstraintCode) {
1294 case 'o': // offsetable ??
1295 case 'v': // not offsetable ??
1296 default: return true;
1297 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001298 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001299 return true;
1300 break;
1301 }
1302
Evan Cheng04699902006-08-26 01:05:16 +00001303 OutOps.push_back(Op0);
1304 OutOps.push_back(Op1);
1305 OutOps.push_back(Op2);
1306 OutOps.push_back(Op3);
1307 AddToISelQueue(Op0);
1308 AddToISelQueue(Op1);
1309 AddToISelQueue(Op2);
1310 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001311 return false;
1312}
1313
Chris Lattnerc961eea2005-11-16 01:54:32 +00001314/// createX86ISelDag - This pass converts a legalized DAG into a
1315/// X86-specific DAG, ready for instruction scheduling.
1316///
Evan Chenge50794a2006-08-29 18:28:33 +00001317FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1318 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001319}