blob: 785af30e3a7872e01077118572c6cc28ecdf8afb [file] [log] [blame]
Chris Lattner5930d3d2005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattner655e7df2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb9d34bd2006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattner655e7df2005-11-16 01:54:32 +000016#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner7c551262006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner7c551262006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/MathExtras.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000036#include "llvm/ADT/Statistic.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000037#include <iostream>
Evan Chengb9d34bd2006-08-07 22:28:20 +000038#include <queue>
Evan Cheng54cb1832006-02-05 06:46:41 +000039#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43// Pattern Matcher Implementation
44//===----------------------------------------------------------------------===//
45
46namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000047 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
48 /// SDOperand's instead of register numbers for the leaves of the matched
49 /// tree.
50 struct X86ISelAddressMode {
51 enum {
52 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000053 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000054 } BaseType;
55
56 struct { // This is really a union, discriminated by BaseType!
57 SDOperand Reg;
58 int FrameIndex;
59 } Base;
60
Evan Cheng11b0a5d2006-09-08 06:48:29 +000061 bool isRIPRel; // RIP relative?
Chris Lattner3f0f71b2005-11-19 02:11:08 +000062 unsigned Scale;
63 SDOperand IndexReg;
64 unsigned Disp;
65 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000066 Constant *CP;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000067 const char *ES;
68 int JT;
Evan Cheng77d86ff2006-02-25 10:09:08 +000069 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000070
71 X86ISelAddressMode()
Evan Cheng11b0a5d2006-09-08 06:48:29 +000072 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
73 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000074 }
75 };
76}
77
78namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000079 Statistic<>
80 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
81
Evan Chengc07feb142006-08-29 06:44:17 +000082 Statistic<>
83 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
84
Chris Lattner655e7df2005-11-16 01:54:32 +000085 //===--------------------------------------------------------------------===//
86 /// ISel - X86 specific code to select X86 machine instructions for
87 /// SelectionDAG operations.
88 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000089 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000090 /// ContainsFPCode - Every instruction we select that uses or defines a FP
91 /// register should set this to true.
92 bool ContainsFPCode;
93
Evan Cheng358b9ed2006-08-29 18:28:33 +000094 /// FastISel - Enable fast(er) instruction selection.
95 ///
96 bool FastISel;
97
Evan Cheng11b0a5d2006-09-08 06:48:29 +000098 /// TM - Keep a reference to X86TargetMachine.
99 ///
100 X86TargetMachine &TM;
101
Chris Lattner655e7df2005-11-16 01:54:32 +0000102 /// X86Lowering - This object fully describes how to lower LLVM code to an
103 /// X86-specific SelectionDAG.
104 X86TargetLowering X86Lowering;
105
106 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
107 /// make the right decision when generating code for different targets.
108 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +0000109
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000110 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 /// base register.
Evan Cheng5588de92006-02-18 00:15:05 +0000112 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000113
Chris Lattner655e7df2005-11-16 01:54:32 +0000114 public:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000115 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng2dd2c652006-03-13 23:20:37 +0000116 : SelectionDAGISel(X86Lowering),
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000117 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Cheng691a63d2006-07-27 16:44:36 +0000118 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000119 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000120
Evan Cheng5588de92006-02-18 00:15:05 +0000121 virtual bool runOnFunction(Function &Fn) {
122 // Make sure we re-emit a set of the global base reg if necessary
123 GlobalBaseReg = 0;
124 return SelectionDAGISel::runOnFunction(Fn);
125 }
126
Chris Lattner655e7df2005-11-16 01:54:32 +0000127 virtual const char *getPassName() const {
128 return "X86 DAG->DAG Instruction Selection";
129 }
130
131 /// InstructionSelectBasicBlock - This callback is invoked by
132 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
133 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
134
Evan Chengbc7a0f442006-01-11 06:09:51 +0000135 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136
Evan Chenge2a3f702006-07-28 01:03:48 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000138
Chris Lattner655e7df2005-11-16 01:54:32 +0000139// Include the pieces autogenerated from the target description.
140#include "X86GenDAGISel.inc"
141
142 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000143 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000144
Evan Chenga86ba852006-02-11 02:05:36 +0000145 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000146 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
147 SDOperand &Index, SDOperand &Disp);
148 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
149 SDOperand &Index, SDOperand &Disp);
Chris Lattner398195e2006-10-07 21:55:32 +0000150 bool SelectScalarSSELoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
151 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000152 bool TryFoldLoad(SDOperand P, SDOperand N,
153 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000154 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000155 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000156
Chris Lattnerba1ed582006-06-08 18:03:49 +0000157 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
158 /// inline asm expressions.
159 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
160 char ConstraintCode,
161 std::vector<SDOperand> &OutOps,
162 SelectionDAG &DAG);
163
Evan Chenge8a42362006-06-02 22:38:37 +0000164 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
165
Evan Cheng67ed58e2005-12-12 21:49:40 +0000166 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
167 SDOperand &Scale, SDOperand &Index,
168 SDOperand &Disp) {
169 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000170 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
171 AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000172 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000173 Index = AM.IndexReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000174 // These are 32-bit even in 64-bit mode since RIP relative offset
175 // is 32-bit.
176 if (AM.GV)
177 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
178 else if (AM.CP)
179 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
180 else if (AM.ES)
181 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
182 else if (AM.JT != -1)
183 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
184 else
185 Disp = getI32Imm(AM.Disp);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000186 }
187
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000188 /// getI8Imm - Return a target constant with the specified value, of type
189 /// i8.
190 inline SDOperand getI8Imm(unsigned Imm) {
191 return CurDAG->getTargetConstant(Imm, MVT::i8);
192 }
193
Chris Lattner655e7df2005-11-16 01:54:32 +0000194 /// getI16Imm - Return a target constant with the specified value, of type
195 /// i16.
196 inline SDOperand getI16Imm(unsigned Imm) {
197 return CurDAG->getTargetConstant(Imm, MVT::i16);
198 }
199
200 /// getI32Imm - Return a target constant with the specified value, of type
201 /// i32.
202 inline SDOperand getI32Imm(unsigned Imm) {
203 return CurDAG->getTargetConstant(Imm, MVT::i32);
204 }
Evan Chengd49cc362006-02-10 22:24:32 +0000205
Evan Cheng5588de92006-02-18 00:15:05 +0000206 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
207 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000208 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000209
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000210#ifndef NDEBUG
211 unsigned Indent;
212#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000213 };
214}
215
Evan Cheng72bb66a2006-08-08 00:31:00 +0000216static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
217 std::set<SDNode *> &Visited) {
218 if (found ||
219 Use->getNodeId() > Def->getNodeId() ||
220 !Visited.insert(Use).second)
221 return;
222
223 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
224 SDNode *N = Use->getOperand(i).Val;
225 if (N != Def) {
226 findNonImmUse(N, Def, found, Visited);
227 } else {
228 found = true;
229 break;
230 }
231 }
232}
233
234static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
235 std::set<SDNode *> Visited;
236 bool found = false;
237 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
238 SDNode *N = Use->getOperand(i).Val;
239 if (N != Def) {
240 findNonImmUse(N, Def, found, Visited);
241 if (found) break;
242 }
243 }
244 return found;
245}
246
247
Evan Chenge2a3f702006-07-28 01:03:48 +0000248bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000249 // If U use can somehow reach N through another path then U can't fold N or
250 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000251 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000252 // a successor of U.
253 //
254 // [ N ]
255 // ^ ^
256 // | |
257 // / \---
258 // / [X]
259 // | ^
260 // [U]--------|
Evan Cheng358b9ed2006-08-29 18:28:33 +0000261 return !FastISel && !isNonImmUse(U, N);
Evan Cheng691a63d2006-07-27 16:44:36 +0000262}
263
Evan Cheng64a9e282006-08-28 20:10:17 +0000264/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
265/// and move load below the TokenFactor. Replace store's chain operand with
266/// load's chain result.
267static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
268 SDOperand Store, SDOperand TF) {
269 std::vector<SDOperand> Ops;
270 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
271 if (Load.Val == TF.Val->getOperand(i).Val)
272 Ops.push_back(Load.Val->getOperand(0));
273 else
274 Ops.push_back(TF.Val->getOperand(i));
275 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
276 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
277 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
278 Store.getOperand(2), Store.getOperand(3));
279}
280
281/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
282/// selector to pick more load-modify-store instructions. This is a common
283/// case:
284///
285/// [Load chain]
286/// ^
287/// |
288/// [Load]
289/// ^ ^
290/// | |
291/// / \-
292/// / |
293/// [TokenFactor] [Op]
294/// ^ ^
295/// | |
296/// \ /
297/// \ /
298/// [Store]
299///
300/// The fact the store's chain operand != load's chain will prevent the
301/// (store (op (load))) instruction from being selected. We can transform it to:
302///
303/// [Load chain]
304/// ^
305/// |
306/// [TokenFactor]
307/// ^
308/// |
309/// [Load]
310/// ^ ^
311/// | |
312/// | \-
313/// | |
314/// | [Op]
315/// | ^
316/// | |
317/// \ /
318/// \ /
319/// [Store]
320void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
321 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
322 E = DAG.allnodes_end(); I != E; ++I) {
323 if (I->getOpcode() != ISD::STORE)
324 continue;
325 SDOperand Chain = I->getOperand(0);
326 if (Chain.Val->getOpcode() != ISD::TokenFactor)
327 continue;
328
329 SDOperand N1 = I->getOperand(1);
330 SDOperand N2 = I->getOperand(2);
Evan Cheng2c4e0f12006-09-01 22:52:28 +0000331 if (MVT::isFloatingPoint(N1.getValueType()) ||
332 MVT::isVector(N1.getValueType()) ||
Evan Chengdfb85152006-08-29 18:37:37 +0000333 !N1.hasOneUse())
Evan Cheng64a9e282006-08-28 20:10:17 +0000334 continue;
335
336 bool RModW = false;
337 SDOperand Load;
338 unsigned Opcode = N1.Val->getOpcode();
339 switch (Opcode) {
340 case ISD::ADD:
341 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000342 case ISD::AND:
343 case ISD::OR:
344 case ISD::XOR:
345 case ISD::ADDC:
346 case ISD::ADDE: {
347 SDOperand N10 = N1.getOperand(0);
348 SDOperand N11 = N1.getOperand(1);
349 if (N10.Val->getOpcode() == ISD::LOAD)
350 RModW = true;
351 else if (N11.Val->getOpcode() == ISD::LOAD) {
352 RModW = true;
353 std::swap(N10, N11);
354 }
355 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000356 (N10.getOperand(1) == N2) &&
357 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000358 if (RModW)
359 Load = N10;
360 break;
361 }
362 case ISD::SUB:
363 case ISD::SHL:
364 case ISD::SRA:
365 case ISD::SRL:
366 case ISD::ROTL:
367 case ISD::ROTR:
368 case ISD::SUBC:
369 case ISD::SUBE:
370 case X86ISD::SHLD:
371 case X86ISD::SHRD: {
372 SDOperand N10 = N1.getOperand(0);
373 if (N10.Val->getOpcode() == ISD::LOAD)
374 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000375 (N10.getOperand(1) == N2) &&
376 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000377 if (RModW)
378 Load = N10;
379 break;
380 }
381 }
382
Evan Chengc07feb142006-08-29 06:44:17 +0000383 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000384 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000385 ++NumLoadMoved;
386 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000387 }
388}
389
Chris Lattner655e7df2005-11-16 01:54:32 +0000390/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
391/// when it has created a SelectionDAG for us to codegen.
392void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
393 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000394 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000395
Evan Cheng358b9ed2006-08-29 18:28:33 +0000396 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000397 InstructionSelectPreprocess(DAG);
398
Chris Lattner655e7df2005-11-16 01:54:32 +0000399 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000400#ifndef NDEBUG
401 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000402 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000403#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000404 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000405#ifndef NDEBUG
406 DEBUG(std::cerr << "===== Instruction selection ends:\n");
407#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000408
Chris Lattner655e7df2005-11-16 01:54:32 +0000409 DAG.RemoveDeadNodes();
410
411 // Emit machine code to BB.
412 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000413
414 // If we are emitting FP stack code, scan the basic block to determine if this
415 // block defines any FP values. If so, put an FP_REG_KILL instruction before
416 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000417 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000418 // Note that FP stack instructions *are* used in SSE code when returning
419 // values, but these are not live out of the basic block, so we don't need
420 // an FP_REG_KILL in this case either.
421 bool ContainsFPCode = false;
422
423 // Scan all of the machine instructions in these MBBs, checking for FP
424 // stores.
425 MachineFunction::iterator MBBI = FirstMBB;
426 do {
427 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
428 !ContainsFPCode && I != E; ++I) {
429 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
430 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
431 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
432 RegMap->getRegClass(I->getOperand(0).getReg()) ==
433 X86::RFPRegisterClass) {
434 ContainsFPCode = true;
435 break;
436 }
437 }
438 }
439 } while (!ContainsFPCode && &*(MBBI++) != BB);
440
441 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
442 // a copy of the input value in this block.
443 if (!ContainsFPCode) {
444 // Final check, check LLVM BB's that are successors to the LLVM BB
445 // corresponding to BB for FP PHI nodes.
446 const BasicBlock *LLVMBB = BB->getBasicBlock();
447 const PHINode *PN;
448 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
449 !ContainsFPCode && SI != E; ++SI) {
450 for (BasicBlock::const_iterator II = SI->begin();
451 (PN = dyn_cast<PHINode>(II)); ++II) {
452 if (PN->getType()->isFloatingPoint()) {
453 ContainsFPCode = true;
454 break;
455 }
456 }
457 }
458 }
459
460 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
461 if (ContainsFPCode) {
462 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
463 ++NumFPKill;
464 }
465 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000466}
467
Evan Chengbc7a0f442006-01-11 06:09:51 +0000468/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
469/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000470void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
471 MachineFrameInfo *MFI) {
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +0000472 if (Subtarget->isTargetCygwin())
Evan Chenge8a42362006-06-02 22:38:37 +0000473 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
474
Evan Chengbc7a0f442006-01-11 06:09:51 +0000475 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
476 int CWFrameIdx = MFI->CreateStackObject(2, 2);
477 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
478
479 // Set the high part to be 64-bit precision.
480 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
481 CWFrameIdx, 1).addImm(2);
482
483 // Reload the modified control word now.
484 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
485}
486
487void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
488 // If this is main, emit special code for main.
489 MachineBasicBlock *BB = MF.begin();
490 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
491 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
492}
493
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000494/// MatchAddress - Add the specified node to the specified addressing mode,
495/// returning true if it cannot be done. This just pattern matches for the
496/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000497bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
498 bool isRoot) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000499 // RIP relative addressing: %rip + 32-bit displacement!
500 if (AM.isRIPRel) {
501 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000502 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000503 if (isInt32(AM.Disp + Val)) {
504 AM.Disp += Val;
505 return false;
506 }
507 }
508 return true;
509 }
510
Evan Chengb9d34bd2006-08-07 22:28:20 +0000511 int id = N.Val->getNodeId();
512 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000513
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000514 switch (N.getOpcode()) {
515 default: break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000516 case ISD::Constant: {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000517 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000518 if (isInt32(AM.Disp + Val)) {
519 AM.Disp += Val;
520 return false;
521 }
522 break;
523 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000524
525 case X86ISD::Wrapper:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000526 // If value is available in a register both base and index components have
527 // been picked, we can't fit the result available in the register in the
528 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
529
530 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
531 // model since the displacement field is 32-bit. Ok for small code model.
532
533 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
534 // relative addressing mode.
535 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
536 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
537 bool isRIP = Subtarget->is64Bit();
538 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
539 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
540 break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000541 if (ConstantPoolSDNode *CP =
542 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
543 if (AM.CP == 0) {
Evan Cheng9a083a42006-09-12 21:04:05 +0000544 AM.CP = CP->getConstVal();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000545 AM.Align = CP->getAlignment();
546 AM.Disp += CP->getOffset();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000547 if (isRIP)
548 AM.isRIPRel = true;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000549 return false;
550 }
551 } else if (GlobalAddressSDNode *G =
552 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
553 if (AM.GV == 0) {
554 AM.GV = G->getGlobal();
555 AM.Disp += G->getOffset();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000556 if (isRIP)
557 AM.isRIPRel = true;
558 return false;
559 }
560 } else if (isRoot && isRIP) {
561 if (ExternalSymbolSDNode *S =
562 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
563 AM.ES = S->getSymbol();
564 AM.isRIPRel = true;
565 return false;
566 } else if (JumpTableSDNode *J =
567 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
568 AM.JT = J->getIndex();
569 AM.isRIPRel = true;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000570 return false;
571 }
572 }
573 }
574 break;
575
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000576 case ISD::FrameIndex:
577 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
578 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
579 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
580 return false;
581 }
582 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000583
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000584 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000585 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
587 unsigned Val = CN->getValue();
588 if (Val == 1 || Val == 2 || Val == 3) {
589 AM.Scale = 1 << Val;
590 SDOperand ShVal = N.Val->getOperand(0);
591
592 // Okay, we know that we have a scale by now. However, if the scaled
593 // value is an add of something and a constant, we can fold the
594 // constant into the disp field here.
595 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
596 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
597 AM.IndexReg = ShVal.Val->getOperand(0);
598 ConstantSDNode *AddVal =
599 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000600 uint64_t Disp = AM.Disp + AddVal->getValue() << Val;
601 if (isInt32(Disp))
602 AM.Disp = Disp;
603 else
604 AM.IndexReg = ShVal;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000605 } else {
606 AM.IndexReg = ShVal;
607 }
608 return false;
609 }
610 }
611 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000612
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000613 case ISD::MUL:
614 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000615 if (!Available &&
616 AM.BaseType == X86ISelAddressMode::RegBase &&
617 AM.Base.Reg.Val == 0 &&
618 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000619 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
620 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
621 AM.Scale = unsigned(CN->getValue())-1;
622
623 SDOperand MulVal = N.Val->getOperand(0);
624 SDOperand Reg;
625
626 // Okay, we know that we have a scale by now. However, if the scaled
627 // value is an add of something and a constant, we can fold the
628 // constant into the disp field here.
629 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
630 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
631 Reg = MulVal.Val->getOperand(0);
632 ConstantSDNode *AddVal =
633 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000634 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
635 if (isInt32(Disp))
636 AM.Disp = Disp;
637 else
638 Reg = N.Val->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000639 } else {
640 Reg = N.Val->getOperand(0);
641 }
642
643 AM.IndexReg = AM.Base.Reg = Reg;
644 return false;
645 }
646 break;
647
648 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000649 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000650 X86ISelAddressMode Backup = AM;
651 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
652 !MatchAddress(N.Val->getOperand(1), AM, false))
653 return false;
654 AM = Backup;
655 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
656 !MatchAddress(N.Val->getOperand(0), AM, false))
657 return false;
658 AM = Backup;
659 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000660 break;
661 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000662
663 case ISD::OR: {
664 if (!Available) {
665 X86ISelAddressMode Backup = AM;
666 // Look for (x << c1) | c2 where (c2 < c1)
667 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
668 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
669 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
670 AM.Disp = CN->getValue();
671 return false;
672 }
673 }
674 AM = Backup;
675 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
676 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
677 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
678 AM.Disp = CN->getValue();
679 return false;
680 }
681 }
682 AM = Backup;
683 }
684 break;
685 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000686 }
687
688 // Is the base register already occupied?
689 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
690 // If so, check to see if the scale index register is set.
691 if (AM.IndexReg.Val == 0) {
692 AM.IndexReg = N;
693 AM.Scale = 1;
694 return false;
695 }
696
697 // Otherwise, we cannot select it.
698 return true;
699 }
700
701 // Default, generate it as a register.
702 AM.BaseType = X86ISelAddressMode::RegBase;
703 AM.Base.Reg = N;
704 return false;
705}
706
Evan Chengc9fab312005-12-08 02:01:35 +0000707/// SelectAddr - returns true if it is able pattern match an addressing mode.
708/// It returns the operands which make up the maximal addressing mode it can
709/// match by reference.
710bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
711 SDOperand &Index, SDOperand &Disp) {
712 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000713 if (MatchAddress(N, AM))
714 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000715
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000716 MVT::ValueType VT = N.getValueType();
Evan Chengbc7a0f442006-01-11 06:09:51 +0000717 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000718 if (!AM.Base.Reg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000719 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengc9fab312005-12-08 02:01:35 +0000720 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000721
Evan Chengd19d51f2006-02-05 05:25:07 +0000722 if (!AM.IndexReg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000723 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Chengbc7a0f442006-01-11 06:09:51 +0000724
725 getAddressOperands(AM, Base, Scale, Index, Disp);
726 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000727}
728
Chris Lattner398195e2006-10-07 21:55:32 +0000729/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
730/// match a load whose top elements are either undef or zeros. The load flavor
731/// is derived from the type of N, which is either v4f32 or v2f64.
732bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand N, SDOperand &Base,
733 SDOperand &Scale,
734 SDOperand &Index, SDOperand &Disp) {
735#if 0
736 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
737 if (N.getOperand(0).getOpcode() == ISD::LOAD) {
738 SDOperand LoadAddr = N.getOperand(0).getOperand(0);
739 if (!SelectAddr(LoadAddr, Base, Scale, Index, Disp))
740 return false;
741 return true;
742 }
743 }
744 // TODO: Also handle the case where we explicitly require zeros in the top
745 // elements. This is a vector shuffle from the zero vector.
746#endif
747
748 return false;
749}
750
751
Evan Cheng77d86ff2006-02-25 10:09:08 +0000752/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
753/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000754bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
755 SDOperand &Scale,
756 SDOperand &Index, SDOperand &Disp) {
757 X86ISelAddressMode AM;
758 if (MatchAddress(N, AM))
759 return false;
760
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000761 MVT::ValueType VT = N.getValueType();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000762 unsigned Complexity = 0;
763 if (AM.BaseType == X86ISelAddressMode::RegBase)
764 if (AM.Base.Reg.Val)
765 Complexity = 1;
766 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000767 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000768 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
769 Complexity = 4;
770
771 if (AM.IndexReg.Val)
772 Complexity++;
773 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000774 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000775
Evan Cheng990c3602006-02-28 21:13:57 +0000776 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000777 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000778 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
779 else if (AM.Scale > 1)
780 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000781
782 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
783 // to a LEA. This is determined with some expermentation but is by no means
784 // optimal (especially for code size consideration). LEA is nice because of
785 // its three-address nature. Tweak the cost function again when we can run
786 // convertToThreeAddress() at register allocation time.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000787 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
788 // For X86-64, we should always use lea to materialize RIP relative
789 // addresses.
790 if (Subtarget->is64Bit())
791 Complexity = 4;
792 else
793 Complexity += 2;
794 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000795
796 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
797 Complexity++;
798
799 if (Complexity > 2) {
800 getAddressOperands(AM, Base, Scale, Index, Disp);
801 return true;
802 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000803 return false;
804}
805
Evan Chengd5f2ba02006-02-06 06:02:33 +0000806bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
807 SDOperand &Base, SDOperand &Scale,
808 SDOperand &Index, SDOperand &Disp) {
809 if (N.getOpcode() == ISD::LOAD &&
810 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000811 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000812 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
813 return false;
814}
815
816static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000817 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
818 return (R->getReg() == 0);
819 return false;
820}
821
Evan Cheng5588de92006-02-18 00:15:05 +0000822/// getGlobalBaseReg - Output the instructions required to put the
823/// base address to use for accessing globals into a register.
824///
Evan Cheng61413a32006-08-26 05:34:46 +0000825SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000826 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng5588de92006-02-18 00:15:05 +0000827 if (!GlobalBaseReg) {
828 // Insert the set of GlobalBaseReg into the first MBB of the function
829 MachineBasicBlock &FirstMBB = BB->getParent()->front();
830 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
831 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
832 // FIXME: when we get to LP64, we will need to create the appropriate
833 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000834 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000835 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
836 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
837 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000838 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000839}
840
Evan Chengf838cfc2006-05-20 01:36:52 +0000841static SDNode *FindCallStartFromCall(SDNode *Node) {
842 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
843 assert(Node->getOperand(0).getValueType() == MVT::Other &&
844 "Node doesn't have a token chain argument!");
845 return FindCallStartFromCall(Node->getOperand(0).Val);
846}
847
Evan Cheng61413a32006-08-26 05:34:46 +0000848SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000849 SDNode *Node = N.Val;
850 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000851 unsigned Opc, MOpc;
852 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000853
Evan Chengd49cc362006-02-10 22:24:32 +0000854#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000855 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000856 DEBUG(std::cerr << "Selecting: ");
857 DEBUG(Node->dump(CurDAG));
858 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000859 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000860#endif
861
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000862 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000863#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000864 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000865 DEBUG(std::cerr << "== ");
866 DEBUG(Node->dump(CurDAG));
867 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000868 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000869#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000870 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000871 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000872
Evan Cheng10d27902006-01-06 20:36:21 +0000873 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000874 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000875 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000876 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000877
Evan Cheng77d86ff2006-02-25 10:09:08 +0000878 case ISD::ADD: {
879 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
880 // code and is matched first so to prevent it from being turned into
881 // LEA32r X+c.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000882 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
883 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000884 SDOperand N0 = N.getOperand(0);
885 SDOperand N1 = N.getOperand(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000886 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng77d86ff2006-02-25 10:09:08 +0000887 N0.getOpcode() == X86ISD::Wrapper &&
888 N1.getOpcode() == ISD::Constant) {
889 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
890 SDOperand C(0, 0);
891 // TODO: handle ExternalSymbolSDNode.
892 if (GlobalAddressSDNode *G =
893 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000894 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +0000895 G->getOffset() + Offset);
896 } else if (ConstantPoolSDNode *CP =
897 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Cheng9a083a42006-09-12 21:04:05 +0000898 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +0000899 CP->getAlignment(),
900 CP->getOffset()+Offset);
901 }
902
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000903 if (C.Val) {
904 if (Subtarget->is64Bit()) {
905 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
906 CurDAG->getRegister(0, PtrVT), C };
907 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
908 } else
909 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
910 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000911 }
912
913 // Other cases are handled by auto-generated code.
914 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000915 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000916
Evan Cheng10d27902006-01-06 20:36:21 +0000917 case ISD::MULHU:
918 case ISD::MULHS: {
919 if (Opcode == ISD::MULHU)
920 switch (NVT) {
921 default: assert(0 && "Unsupported VT!");
922 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
923 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
924 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000925 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000926 }
927 else
928 switch (NVT) {
929 default: assert(0 && "Unsupported VT!");
930 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
931 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
932 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000933 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000934 }
935
936 unsigned LoReg, HiReg;
937 switch (NVT) {
938 default: assert(0 && "Unsupported VT!");
939 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
940 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
941 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000942 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000943 }
944
945 SDOperand N0 = Node->getOperand(0);
946 SDOperand N1 = Node->getOperand(1);
947
948 bool foldedLoad = false;
949 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000950 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000951 // MULHU and MULHS are commmutative
952 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000953 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000954 if (foldedLoad) {
955 N0 = Node->getOperand(1);
956 N1 = Node->getOperand(0);
957 }
958 }
959
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000960 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000961 if (foldedLoad) {
962 Chain = N1.getOperand(0);
963 AddToISelQueue(Chain);
964 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000965 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000966
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000967 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000968 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000969 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000970 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000971 InFlag = Chain.getValue(1);
972
973 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000974 AddToISelQueue(Tmp0);
975 AddToISelQueue(Tmp1);
976 AddToISelQueue(Tmp2);
977 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000978 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000979 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000980 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000981 Chain = SDOperand(CNode, 0);
982 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000983 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000984 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000985 InFlag =
986 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000987 }
988
Evan Cheng61413a32006-08-26 05:34:46 +0000989 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000990 ReplaceUses(N.getValue(0), Result);
991 if (foldedLoad)
992 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000993
Evan Chengd49cc362006-02-10 22:24:32 +0000994#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000995 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000996 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000997 DEBUG(Result.Val->dump(CurDAG));
998 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000999 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001000#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001001 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +00001002 }
Evan Cheng5588de92006-02-18 00:15:05 +00001003
Evan Cheng92e27972006-01-06 23:19:29 +00001004 case ISD::SDIV:
1005 case ISD::UDIV:
1006 case ISD::SREM:
1007 case ISD::UREM: {
1008 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1009 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1010 if (!isSigned)
1011 switch (NVT) {
1012 default: assert(0 && "Unsupported VT!");
1013 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1014 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1015 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001016 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001017 }
1018 else
1019 switch (NVT) {
1020 default: assert(0 && "Unsupported VT!");
1021 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1022 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1023 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001024 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001025 }
1026
1027 unsigned LoReg, HiReg;
1028 unsigned ClrOpcode, SExtOpcode;
1029 switch (NVT) {
1030 default: assert(0 && "Unsupported VT!");
1031 case MVT::i8:
1032 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001033 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001034 SExtOpcode = X86::CBW;
1035 break;
1036 case MVT::i16:
1037 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001038 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001039 SExtOpcode = X86::CWD;
1040 break;
1041 case MVT::i32:
1042 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001043 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001044 SExtOpcode = X86::CDQ;
1045 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001046 case MVT::i64:
1047 LoReg = X86::RAX; HiReg = X86::RDX;
1048 ClrOpcode = X86::MOV64r0;
1049 SExtOpcode = X86::CQO;
1050 break;
Evan Cheng92e27972006-01-06 23:19:29 +00001051 }
1052
1053 SDOperand N0 = Node->getOperand(0);
1054 SDOperand N1 = Node->getOperand(1);
1055
1056 bool foldedLoad = false;
1057 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +00001058 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001059 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +00001060 if (foldedLoad) {
1061 Chain = N1.getOperand(0);
1062 AddToISelQueue(Chain);
1063 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001064 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +00001065
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001066 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +00001067 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +00001068 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001069 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +00001070 InFlag = Chain.getValue(1);
1071
1072 if (isSigned) {
1073 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +00001074 InFlag =
1075 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001076 } else {
1077 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +00001078 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001079 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
1080 ClrNode, InFlag);
1081 InFlag = Chain.getValue(1);
1082 }
1083
1084 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +00001085 AddToISelQueue(Tmp0);
1086 AddToISelQueue(Tmp1);
1087 AddToISelQueue(Tmp2);
1088 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +00001089 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +00001090 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +00001091 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +00001092 Chain = SDOperand(CNode, 0);
1093 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +00001094 } else {
Evan Cheng2d487222006-08-26 01:05:16 +00001095 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +00001096 InFlag =
1097 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001098 }
1099
Evan Cheng61413a32006-08-26 05:34:46 +00001100 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
1101 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +00001102 ReplaceUses(N.getValue(0), Result);
1103 if (foldedLoad)
1104 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +00001105
1106#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001107 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001108 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +00001109 DEBUG(Result.Val->dump(CurDAG));
1110 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001111 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001112#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001113
1114 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +00001115 }
Evan Cheng9733bde2006-05-08 08:01:26 +00001116
1117 case ISD::TRUNCATE: {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001118 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng9733bde2006-05-08 08:01:26 +00001119 unsigned Opc2;
1120 MVT::ValueType VT;
1121 switch (Node->getOperand(0).getValueType()) {
1122 default: assert(0 && "Unknown truncate!");
1123 case MVT::i16:
1124 Opc = X86::MOV16to16_;
1125 VT = MVT::i16;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001126 Opc2 = X86::TRUNC_16_to8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001127 break;
1128 case MVT::i32:
1129 Opc = X86::MOV32to32_;
1130 VT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001131 Opc2 = X86::TRUNC_32_to8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001132 break;
1133 }
1134
Evan Cheng2d487222006-08-26 01:05:16 +00001135 AddToISelQueue(Node->getOperand(0));
1136 SDOperand Tmp =
1137 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001138 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001139
1140#ifndef NDEBUG
1141 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001142 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001143 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001144 DEBUG(std::cerr << "\n");
1145 Indent -= 2;
1146#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001147 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001148 }
Evan Chenga26c4512006-05-20 07:44:28 +00001149
1150 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001151 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001152 }
1153
Evan Cheng61413a32006-08-26 05:34:46 +00001154 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001155
Evan Chengd49cc362006-02-10 22:24:32 +00001156#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001157 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001158 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001159 if (ResNode == NULL || ResNode == N.Val)
1160 DEBUG(N.Val->dump(CurDAG));
1161 else
1162 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001163 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001164 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001165#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001166
1167 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001168}
1169
Chris Lattnerba1ed582006-06-08 18:03:49 +00001170bool X86DAGToDAGISel::
1171SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1172 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1173 SDOperand Op0, Op1, Op2, Op3;
1174 switch (ConstraintCode) {
1175 case 'o': // offsetable ??
1176 case 'v': // not offsetable ??
1177 default: return true;
1178 case 'm': // memory
1179 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1180 return true;
1181 break;
1182 }
1183
Evan Cheng2d487222006-08-26 01:05:16 +00001184 OutOps.push_back(Op0);
1185 OutOps.push_back(Op1);
1186 OutOps.push_back(Op2);
1187 OutOps.push_back(Op3);
1188 AddToISelQueue(Op0);
1189 AddToISelQueue(Op1);
1190 AddToISelQueue(Op2);
1191 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001192 return false;
1193}
1194
Chris Lattner655e7df2005-11-16 01:54:32 +00001195/// createX86ISelDag - This pass converts a legalized DAG into a
1196/// X86-specific DAG, ready for instruction scheduling.
1197///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001198FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1199 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001200}