blob: 144776e68c1f23cff0c280a183cd478cdc0d376d [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 Chengba277312006-07-28 06:05:06 +000037#include <deque>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000038#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000039#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000040#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000041using namespace llvm;
42
43//===----------------------------------------------------------------------===//
44// Pattern Matcher Implementation
45//===----------------------------------------------------------------------===//
46
47namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000048 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
49 /// SDOperand's instead of register numbers for the leaves of the matched
50 /// tree.
51 struct X86ISelAddressMode {
52 enum {
53 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000054 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000055 } BaseType;
56
57 struct { // This is really a union, discriminated by BaseType!
58 SDOperand Reg;
59 int FrameIndex;
60 } Base;
61
Evan Cheng25ab6902006-09-08 06:48:29 +000062 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 unsigned Scale;
64 SDOperand IndexReg;
65 unsigned Disp;
66 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000067 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000068 const char *ES;
69 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000071
72 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000073 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
74 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000075 }
76 };
77}
78
79namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000080 Statistic<>
81 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
82
Evan Cheng82a35b32006-08-29 06:44:17 +000083 Statistic<>
84 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
85
Chris Lattnerc961eea2005-11-16 01:54:32 +000086 //===--------------------------------------------------------------------===//
87 /// ISel - X86 specific code to select X86 machine instructions for
88 /// SelectionDAG operations.
89 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000090 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000091 /// ContainsFPCode - Every instruction we select that uses or defines a FP
92 /// register should set this to true.
93 bool ContainsFPCode;
94
Evan Chenge50794a2006-08-29 18:28:33 +000095 /// FastISel - Enable fast(er) instruction selection.
96 ///
97 bool FastISel;
98
Evan Cheng25ab6902006-09-08 06:48:29 +000099 /// TM - Keep a reference to X86TargetMachine.
100 ///
101 X86TargetMachine &TM;
102
Chris Lattnerc961eea2005-11-16 01:54:32 +0000103 /// X86Lowering - This object fully describes how to lower LLVM code to an
104 /// X86-specific SelectionDAG.
105 X86TargetLowering X86Lowering;
106
107 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
108 /// make the right decision when generating code for different targets.
109 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
112 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000113 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000114
Chris Lattnerc961eea2005-11-16 01:54:32 +0000115 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000117 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000118 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000119 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000120 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000121
Evan Cheng7ccced62006-02-18 00:15:05 +0000122 virtual bool runOnFunction(Function &Fn) {
123 // Make sure we re-emit a set of the global base reg if necessary
124 GlobalBaseReg = 0;
125 return SelectionDAGISel::runOnFunction(Fn);
126 }
127
Chris Lattnerc961eea2005-11-16 01:54:32 +0000128 virtual const char *getPassName() const {
129 return "X86 DAG->DAG Instruction Selection";
130 }
131
132 /// InstructionSelectBasicBlock - This callback is invoked by
133 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
134 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
135
Evan Cheng8700e142006-01-11 06:09:51 +0000136 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
137
Evan Chengf2dfafc2006-07-28 01:03:48 +0000138 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000139
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140// Include the pieces autogenerated from the target description.
141#include "X86GenDAGISel.inc"
142
143 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000144 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000145
Evan Cheng2486af12006-02-11 02:05:36 +0000146 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengec693f72005-12-08 02:01:35 +0000147 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
148 SDOperand &Index, SDOperand &Disp);
149 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
150 SDOperand &Index, SDOperand &Disp);
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 Chengf4b4c412006-08-08 00:31:00 +0000215static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
216 std::set<SDNode *> &Visited) {
217 if (found ||
218 Use->getNodeId() > Def->getNodeId() ||
219 !Visited.insert(Use).second)
220 return;
221
222 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
223 SDNode *N = Use->getOperand(i).Val;
224 if (N != Def) {
225 findNonImmUse(N, Def, found, Visited);
226 } else {
227 found = true;
228 break;
229 }
230 }
231}
232
233static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
234 std::set<SDNode *> Visited;
235 bool found = false;
236 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
237 SDNode *N = Use->getOperand(i).Val;
238 if (N != Def) {
239 findNonImmUse(N, Def, found, Visited);
240 if (found) break;
241 }
242 }
243 return found;
244}
245
246
Evan Chengf2dfafc2006-07-28 01:03:48 +0000247bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Chenga8df1b42006-07-27 16:44:36 +0000248 // If U use can somehow reach N through another path then U can't fold N or
249 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000250 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000251 // a successor of U.
252 //
253 // [ N ]
254 // ^ ^
255 // | |
256 // / \---
257 // / [X]
258 // | ^
259 // [U]--------|
Evan Chenge50794a2006-08-29 18:28:33 +0000260 return !FastISel && !isNonImmUse(U, N);
Evan Chenga8df1b42006-07-27 16:44:36 +0000261}
262
Evan Cheng70e674e2006-08-28 20:10:17 +0000263/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
264/// and move load below the TokenFactor. Replace store's chain operand with
265/// load's chain result.
266static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
267 SDOperand Store, SDOperand TF) {
268 std::vector<SDOperand> Ops;
269 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
270 if (Load.Val == TF.Val->getOperand(i).Val)
271 Ops.push_back(Load.Val->getOperand(0));
272 else
273 Ops.push_back(TF.Val->getOperand(i));
274 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
275 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
276 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
277 Store.getOperand(2), Store.getOperand(3));
278}
279
280/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
281/// selector to pick more load-modify-store instructions. This is a common
282/// case:
283///
284/// [Load chain]
285/// ^
286/// |
287/// [Load]
288/// ^ ^
289/// | |
290/// / \-
291/// / |
292/// [TokenFactor] [Op]
293/// ^ ^
294/// | |
295/// \ /
296/// \ /
297/// [Store]
298///
299/// The fact the store's chain operand != load's chain will prevent the
300/// (store (op (load))) instruction from being selected. We can transform it to:
301///
302/// [Load chain]
303/// ^
304/// |
305/// [TokenFactor]
306/// ^
307/// |
308/// [Load]
309/// ^ ^
310/// | |
311/// | \-
312/// | |
313/// | [Op]
314/// | ^
315/// | |
316/// \ /
317/// \ /
318/// [Store]
319void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
320 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
321 E = DAG.allnodes_end(); I != E; ++I) {
322 if (I->getOpcode() != ISD::STORE)
323 continue;
324 SDOperand Chain = I->getOperand(0);
325 if (Chain.Val->getOpcode() != ISD::TokenFactor)
326 continue;
327
328 SDOperand N1 = I->getOperand(1);
329 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000330 if (MVT::isFloatingPoint(N1.getValueType()) ||
331 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000332 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000333 continue;
334
335 bool RModW = false;
336 SDOperand Load;
337 unsigned Opcode = N1.Val->getOpcode();
338 switch (Opcode) {
339 case ISD::ADD:
340 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000341 case ISD::AND:
342 case ISD::OR:
343 case ISD::XOR:
344 case ISD::ADDC:
345 case ISD::ADDE: {
346 SDOperand N10 = N1.getOperand(0);
347 SDOperand N11 = N1.getOperand(1);
348 if (N10.Val->getOpcode() == ISD::LOAD)
349 RModW = true;
350 else if (N11.Val->getOpcode() == ISD::LOAD) {
351 RModW = true;
352 std::swap(N10, N11);
353 }
354 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000355 (N10.getOperand(1) == N2) &&
356 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000357 if (RModW)
358 Load = N10;
359 break;
360 }
361 case ISD::SUB:
362 case ISD::SHL:
363 case ISD::SRA:
364 case ISD::SRL:
365 case ISD::ROTL:
366 case ISD::ROTR:
367 case ISD::SUBC:
368 case ISD::SUBE:
369 case X86ISD::SHLD:
370 case X86ISD::SHRD: {
371 SDOperand N10 = N1.getOperand(0);
372 if (N10.Val->getOpcode() == ISD::LOAD)
373 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000374 (N10.getOperand(1) == N2) &&
375 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000376 if (RModW)
377 Load = N10;
378 break;
379 }
380 }
381
Evan Cheng82a35b32006-08-29 06:44:17 +0000382 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000383 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000384 ++NumLoadMoved;
385 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000386 }
387}
388
Chris Lattnerc961eea2005-11-16 01:54:32 +0000389/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
390/// when it has created a SelectionDAG for us to codegen.
391void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
392 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000393 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000394
Evan Chenge50794a2006-08-29 18:28:33 +0000395 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000396 InstructionSelectPreprocess(DAG);
397
Chris Lattnerc961eea2005-11-16 01:54:32 +0000398 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000399#ifndef NDEBUG
400 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000401 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000402#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000403 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000404#ifndef NDEBUG
405 DEBUG(std::cerr << "===== Instruction selection ends:\n");
406#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000407
Chris Lattnerc961eea2005-11-16 01:54:32 +0000408 DAG.RemoveDeadNodes();
409
410 // Emit machine code to BB.
411 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000412
413 // If we are emitting FP stack code, scan the basic block to determine if this
414 // block defines any FP values. If so, put an FP_REG_KILL instruction before
415 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000416 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000417 // Note that FP stack instructions *are* used in SSE code when returning
418 // values, but these are not live out of the basic block, so we don't need
419 // an FP_REG_KILL in this case either.
420 bool ContainsFPCode = false;
421
422 // Scan all of the machine instructions in these MBBs, checking for FP
423 // stores.
424 MachineFunction::iterator MBBI = FirstMBB;
425 do {
426 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
427 !ContainsFPCode && I != E; ++I) {
428 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
429 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
430 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
431 RegMap->getRegClass(I->getOperand(0).getReg()) ==
432 X86::RFPRegisterClass) {
433 ContainsFPCode = true;
434 break;
435 }
436 }
437 }
438 } while (!ContainsFPCode && &*(MBBI++) != BB);
439
440 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
441 // a copy of the input value in this block.
442 if (!ContainsFPCode) {
443 // Final check, check LLVM BB's that are successors to the LLVM BB
444 // corresponding to BB for FP PHI nodes.
445 const BasicBlock *LLVMBB = BB->getBasicBlock();
446 const PHINode *PN;
447 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
448 !ContainsFPCode && SI != E; ++SI) {
449 for (BasicBlock::const_iterator II = SI->begin();
450 (PN = dyn_cast<PHINode>(II)); ++II) {
451 if (PN->getType()->isFloatingPoint()) {
452 ContainsFPCode = true;
453 break;
454 }
455 }
456 }
457 }
458
459 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
460 if (ContainsFPCode) {
461 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
462 ++NumFPKill;
463 }
464 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000465}
466
Evan Cheng8700e142006-01-11 06:09:51 +0000467/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
468/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000469void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
470 MachineFrameInfo *MFI) {
471 if (Subtarget->TargetType == X86Subtarget::isCygwin)
472 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
473
Evan Cheng8700e142006-01-11 06:09:51 +0000474 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
475 int CWFrameIdx = MFI->CreateStackObject(2, 2);
476 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
477
478 // Set the high part to be 64-bit precision.
479 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
480 CWFrameIdx, 1).addImm(2);
481
482 // Reload the modified control word now.
483 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
484}
485
486void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
487 // If this is main, emit special code for main.
488 MachineBasicBlock *BB = MF.begin();
489 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
490 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
491}
492
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000493/// MatchAddress - Add the specified node to the specified addressing mode,
494/// returning true if it cannot be done. This just pattern matches for the
495/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000496bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
497 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000498 // RIP relative addressing: %rip + 32-bit displacement!
499 if (AM.isRIPRel) {
500 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000501 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000502 if (isInt32(AM.Disp + Val)) {
503 AM.Disp += Val;
504 return false;
505 }
506 }
507 return true;
508 }
509
Evan Cheng2ef88a02006-08-07 22:28:20 +0000510 int id = N.Val->getNodeId();
511 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000512
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000513 switch (N.getOpcode()) {
514 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000515 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000516 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000517 if (isInt32(AM.Disp + Val)) {
518 AM.Disp += Val;
519 return false;
520 }
521 break;
522 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000523
524 case X86ISD::Wrapper:
Evan Cheng25ab6902006-09-08 06:48:29 +0000525 // If value is available in a register both base and index components have
526 // been picked, we can't fit the result available in the register in the
527 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
528
529 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
530 // model since the displacement field is 32-bit. Ok for small code model.
531
532 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
533 // relative addressing mode.
534 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
535 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
536 bool isRIP = Subtarget->is64Bit();
537 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
538 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
539 break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000540 if (ConstantPoolSDNode *CP =
541 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
542 if (AM.CP == 0) {
Evan Chengc356a572006-09-12 21:04:05 +0000543 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000544 AM.Align = CP->getAlignment();
545 AM.Disp += CP->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000546 if (isRIP)
547 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000548 return false;
549 }
550 } else if (GlobalAddressSDNode *G =
551 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
552 if (AM.GV == 0) {
553 AM.GV = G->getGlobal();
554 AM.Disp += G->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000555 if (isRIP)
556 AM.isRIPRel = true;
557 return false;
558 }
559 } else if (isRoot && isRIP) {
560 if (ExternalSymbolSDNode *S =
561 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
562 AM.ES = S->getSymbol();
563 AM.isRIPRel = true;
564 return false;
565 } else if (JumpTableSDNode *J =
566 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
567 AM.JT = J->getIndex();
568 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000569 return false;
570 }
571 }
572 }
573 break;
574
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000575 case ISD::FrameIndex:
576 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
577 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
578 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
579 return false;
580 }
581 break;
Evan Chengec693f72005-12-08 02:01:35 +0000582
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000583 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000584 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000585 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
586 unsigned Val = CN->getValue();
587 if (Val == 1 || Val == 2 || Val == 3) {
588 AM.Scale = 1 << Val;
589 SDOperand ShVal = N.Val->getOperand(0);
590
591 // Okay, we know that we have a scale by now. However, if the scaled
592 // value is an add of something and a constant, we can fold the
593 // constant into the disp field here.
594 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
595 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
596 AM.IndexReg = ShVal.Val->getOperand(0);
597 ConstantSDNode *AddVal =
598 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000599 uint64_t Disp = AM.Disp + AddVal->getValue() << Val;
600 if (isInt32(Disp))
601 AM.Disp = Disp;
602 else
603 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000604 } else {
605 AM.IndexReg = ShVal;
606 }
607 return false;
608 }
609 }
610 break;
Evan Chengec693f72005-12-08 02:01:35 +0000611
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000612 case ISD::MUL:
613 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000614 if (!Available &&
615 AM.BaseType == X86ISelAddressMode::RegBase &&
616 AM.Base.Reg.Val == 0 &&
617 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
619 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
620 AM.Scale = unsigned(CN->getValue())-1;
621
622 SDOperand MulVal = N.Val->getOperand(0);
623 SDOperand Reg;
624
625 // Okay, we know that we have a scale by now. However, if the scaled
626 // value is an add of something and a constant, we can fold the
627 // constant into the disp field here.
628 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
629 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
630 Reg = MulVal.Val->getOperand(0);
631 ConstantSDNode *AddVal =
632 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000633 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
634 if (isInt32(Disp))
635 AM.Disp = Disp;
636 else
637 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000638 } else {
639 Reg = N.Val->getOperand(0);
640 }
641
642 AM.IndexReg = AM.Base.Reg = Reg;
643 return false;
644 }
645 break;
646
647 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000648 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000649 X86ISelAddressMode Backup = AM;
650 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
651 !MatchAddress(N.Val->getOperand(1), AM, false))
652 return false;
653 AM = Backup;
654 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
655 !MatchAddress(N.Val->getOperand(0), AM, false))
656 return false;
657 AM = Backup;
658 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000659 break;
660 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000661
662 case ISD::OR: {
663 if (!Available) {
664 X86ISelAddressMode Backup = AM;
665 // Look for (x << c1) | c2 where (c2 < c1)
666 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
667 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
668 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
669 AM.Disp = CN->getValue();
670 return false;
671 }
672 }
673 AM = Backup;
674 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
675 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
676 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
677 AM.Disp = CN->getValue();
678 return false;
679 }
680 }
681 AM = Backup;
682 }
683 break;
684 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000685 }
686
687 // Is the base register already occupied?
688 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
689 // If so, check to see if the scale index register is set.
690 if (AM.IndexReg.Val == 0) {
691 AM.IndexReg = N;
692 AM.Scale = 1;
693 return false;
694 }
695
696 // Otherwise, we cannot select it.
697 return true;
698 }
699
700 // Default, generate it as a register.
701 AM.BaseType = X86ISelAddressMode::RegBase;
702 AM.Base.Reg = N;
703 return false;
704}
705
Evan Chengec693f72005-12-08 02:01:35 +0000706/// SelectAddr - returns true if it is able pattern match an addressing mode.
707/// It returns the operands which make up the maximal addressing mode it can
708/// match by reference.
709bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
710 SDOperand &Index, SDOperand &Disp) {
711 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000712 if (MatchAddress(N, AM))
713 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000714
Evan Cheng25ab6902006-09-08 06:48:29 +0000715 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000716 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000717 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000718 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000719 }
Evan Cheng8700e142006-01-11 06:09:51 +0000720
Evan Cheng7dd281b2006-02-05 05:25:07 +0000721 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000722 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000723
724 getAddressOperands(AM, Base, Scale, Index, Disp);
725 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000726}
727
Evan Cheng51a9ed92006-02-25 10:09:08 +0000728/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
729/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000730bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
731 SDOperand &Scale,
732 SDOperand &Index, SDOperand &Disp) {
733 X86ISelAddressMode AM;
734 if (MatchAddress(N, AM))
735 return false;
736
Evan Cheng25ab6902006-09-08 06:48:29 +0000737 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000738 unsigned Complexity = 0;
739 if (AM.BaseType == X86ISelAddressMode::RegBase)
740 if (AM.Base.Reg.Val)
741 Complexity = 1;
742 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000743 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000744 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
745 Complexity = 4;
746
747 if (AM.IndexReg.Val)
748 Complexity++;
749 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000750 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000751
Evan Cheng8c03fe42006-02-28 21:13:57 +0000752 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000753 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000754 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
755 else if (AM.Scale > 1)
756 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000757
758 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
759 // to a LEA. This is determined with some expermentation but is by no means
760 // optimal (especially for code size consideration). LEA is nice because of
761 // its three-address nature. Tweak the cost function again when we can run
762 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000763 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
764 // For X86-64, we should always use lea to materialize RIP relative
765 // addresses.
766 if (Subtarget->is64Bit())
767 Complexity = 4;
768 else
769 Complexity += 2;
770 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000771
772 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
773 Complexity++;
774
775 if (Complexity > 2) {
776 getAddressOperands(AM, Base, Scale, Index, Disp);
777 return true;
778 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000779 return false;
780}
781
Evan Cheng5e351682006-02-06 06:02:33 +0000782bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
783 SDOperand &Base, SDOperand &Scale,
784 SDOperand &Index, SDOperand &Disp) {
785 if (N.getOpcode() == ISD::LOAD &&
786 N.hasOneUse() &&
Evan Chengeb8730d2006-08-16 23:59:00 +0000787 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng0114e942006-01-06 20:36:21 +0000788 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
789 return false;
790}
791
792static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000793 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
794 return (R->getReg() == 0);
795 return false;
796}
797
Evan Cheng7ccced62006-02-18 00:15:05 +0000798/// getGlobalBaseReg - Output the instructions required to put the
799/// base address to use for accessing globals into a register.
800///
Evan Cheng9ade2182006-08-26 05:34:46 +0000801SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000802 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000803 if (!GlobalBaseReg) {
804 // Insert the set of GlobalBaseReg into the first MBB of the function
805 MachineBasicBlock &FirstMBB = BB->getParent()->front();
806 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
807 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
808 // FIXME: when we get to LP64, we will need to create the appropriate
809 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000810 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000811 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
812 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
813 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000814 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000815}
816
Evan Chengb245d922006-05-20 01:36:52 +0000817static SDNode *FindCallStartFromCall(SDNode *Node) {
818 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
819 assert(Node->getOperand(0).getValueType() == MVT::Other &&
820 "Node doesn't have a token chain argument!");
821 return FindCallStartFromCall(Node->getOperand(0).Val);
822}
823
Evan Cheng9ade2182006-08-26 05:34:46 +0000824SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000825 SDNode *Node = N.Val;
826 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000827 unsigned Opc, MOpc;
828 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000829
Evan Chengf597dc72006-02-10 22:24:32 +0000830#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000831 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000832 DEBUG(std::cerr << "Selecting: ");
833 DEBUG(Node->dump(CurDAG));
834 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000835 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000836#endif
837
Evan Cheng34167212006-02-09 00:37:58 +0000838 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000839#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000840 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000841 DEBUG(std::cerr << "== ");
842 DEBUG(Node->dump(CurDAG));
843 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000844 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000845#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000846 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000847 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000848
Evan Cheng0114e942006-01-06 20:36:21 +0000849 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000850 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000851 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000852 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000853
Evan Cheng51a9ed92006-02-25 10:09:08 +0000854 case ISD::ADD: {
855 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
856 // code and is matched first so to prevent it from being turned into
857 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000858 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
859 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000860 SDOperand N0 = N.getOperand(0);
861 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000862 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000863 N0.getOpcode() == X86ISD::Wrapper &&
864 N1.getOpcode() == ISD::Constant) {
865 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
866 SDOperand C(0, 0);
867 // TODO: handle ExternalSymbolSDNode.
868 if (GlobalAddressSDNode *G =
869 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000870 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000871 G->getOffset() + Offset);
872 } else if (ConstantPoolSDNode *CP =
873 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +0000874 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000875 CP->getAlignment(),
876 CP->getOffset()+Offset);
877 }
878
Evan Cheng25ab6902006-09-08 06:48:29 +0000879 if (C.Val) {
880 if (Subtarget->is64Bit()) {
881 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
882 CurDAG->getRegister(0, PtrVT), C };
883 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
884 } else
885 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
886 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000887 }
888
889 // Other cases are handled by auto-generated code.
890 break;
Evan Chenga0ea0532006-02-23 02:43:52 +0000891 }
Evan Cheng020d2e82006-02-23 20:41:18 +0000892
Evan Cheng0114e942006-01-06 20:36:21 +0000893 case ISD::MULHU:
894 case ISD::MULHS: {
895 if (Opcode == ISD::MULHU)
896 switch (NVT) {
897 default: assert(0 && "Unsupported VT!");
898 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
899 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
900 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000901 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000902 }
903 else
904 switch (NVT) {
905 default: assert(0 && "Unsupported VT!");
906 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
907 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
908 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000909 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000910 }
911
912 unsigned LoReg, HiReg;
913 switch (NVT) {
914 default: assert(0 && "Unsupported VT!");
915 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
916 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
917 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000918 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000919 }
920
921 SDOperand N0 = Node->getOperand(0);
922 SDOperand N1 = Node->getOperand(1);
923
924 bool foldedLoad = false;
925 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000926 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000927 // MULHU and MULHS are commmutative
928 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +0000929 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000930 if (foldedLoad) {
931 N0 = Node->getOperand(1);
932 N1 = Node->getOperand(0);
933 }
934 }
935
Evan Cheng34167212006-02-09 00:37:58 +0000936 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +0000937 if (foldedLoad) {
938 Chain = N1.getOperand(0);
939 AddToISelQueue(Chain);
940 } else
Evan Cheng34167212006-02-09 00:37:58 +0000941 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +0000942
Evan Cheng34167212006-02-09 00:37:58 +0000943 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +0000944 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +0000945 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000946 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000947 InFlag = Chain.getValue(1);
948
949 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +0000950 AddToISelQueue(Tmp0);
951 AddToISelQueue(Tmp1);
952 AddToISelQueue(Tmp2);
953 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +0000954 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000955 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +0000956 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000957 Chain = SDOperand(CNode, 0);
958 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +0000959 } else {
Evan Cheng04699902006-08-26 01:05:16 +0000960 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000961 InFlag =
962 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +0000963 }
964
Evan Cheng9ade2182006-08-26 05:34:46 +0000965 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000966 ReplaceUses(N.getValue(0), Result);
967 if (foldedLoad)
968 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +0000969
Evan Chengf597dc72006-02-10 22:24:32 +0000970#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000971 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +0000972 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +0000973 DEBUG(Result.Val->dump(CurDAG));
974 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000975 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000976#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000977 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +0000978 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000979
Evan Cheng948f3432006-01-06 23:19:29 +0000980 case ISD::SDIV:
981 case ISD::UDIV:
982 case ISD::SREM:
983 case ISD::UREM: {
984 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
985 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
986 if (!isSigned)
987 switch (NVT) {
988 default: assert(0 && "Unsupported VT!");
989 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
990 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
991 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000992 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +0000993 }
994 else
995 switch (NVT) {
996 default: assert(0 && "Unsupported VT!");
997 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
998 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
999 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001000 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001001 }
1002
1003 unsigned LoReg, HiReg;
1004 unsigned ClrOpcode, SExtOpcode;
1005 switch (NVT) {
1006 default: assert(0 && "Unsupported VT!");
1007 case MVT::i8:
1008 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +00001009 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001010 SExtOpcode = X86::CBW;
1011 break;
1012 case MVT::i16:
1013 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001014 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001015 SExtOpcode = X86::CWD;
1016 break;
1017 case MVT::i32:
1018 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001019 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001020 SExtOpcode = X86::CDQ;
1021 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001022 case MVT::i64:
1023 LoReg = X86::RAX; HiReg = X86::RDX;
1024 ClrOpcode = X86::MOV64r0;
1025 SExtOpcode = X86::CQO;
1026 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001027 }
1028
1029 SDOperand N0 = Node->getOperand(0);
1030 SDOperand N1 = Node->getOperand(1);
1031
1032 bool foldedLoad = false;
1033 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001034 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +00001035 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001036 if (foldedLoad) {
1037 Chain = N1.getOperand(0);
1038 AddToISelQueue(Chain);
1039 } else
Evan Cheng34167212006-02-09 00:37:58 +00001040 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +00001041
Evan Cheng34167212006-02-09 00:37:58 +00001042 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001043 AddToISelQueue(N0);
Evan Cheng948f3432006-01-06 23:19:29 +00001044 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001045 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001046 InFlag = Chain.getValue(1);
1047
1048 if (isSigned) {
1049 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001050 InFlag =
1051 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001052 } else {
1053 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +00001054 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001055 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
1056 ClrNode, InFlag);
1057 InFlag = Chain.getValue(1);
1058 }
1059
1060 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001061 AddToISelQueue(Tmp0);
1062 AddToISelQueue(Tmp1);
1063 AddToISelQueue(Tmp2);
1064 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001065 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001066 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001067 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001068 Chain = SDOperand(CNode, 0);
1069 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001070 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001071 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001072 InFlag =
1073 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001074 }
1075
Evan Cheng9ade2182006-08-26 05:34:46 +00001076 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
1077 NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001078 ReplaceUses(N.getValue(0), Result);
1079 if (foldedLoad)
1080 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001081
1082#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001083 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001084 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +00001085 DEBUG(Result.Val->dump(CurDAG));
1086 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001087 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001088#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001089
1090 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001091 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001092
1093 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001094 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001095 unsigned Opc2;
1096 MVT::ValueType VT;
1097 switch (Node->getOperand(0).getValueType()) {
1098 default: assert(0 && "Unknown truncate!");
1099 case MVT::i16:
1100 Opc = X86::MOV16to16_;
1101 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001102 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001103 break;
1104 case MVT::i32:
1105 Opc = X86::MOV32to32_;
1106 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001107 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001108 break;
1109 }
1110
Evan Cheng04699902006-08-26 01:05:16 +00001111 AddToISelQueue(Node->getOperand(0));
1112 SDOperand Tmp =
1113 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001114 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001115
1116#ifndef NDEBUG
1117 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001118 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001119 DEBUG(ResNode->dump(CurDAG));
Evan Cheng403be7e2006-05-08 08:01:26 +00001120 DEBUG(std::cerr << "\n");
1121 Indent -= 2;
1122#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001123 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001124 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001125
1126 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001127 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001128 }
1129
Evan Cheng9ade2182006-08-26 05:34:46 +00001130 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001131
Evan Chengf597dc72006-02-10 22:24:32 +00001132#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001133 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +00001134 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001135 if (ResNode == NULL || ResNode == N.Val)
1136 DEBUG(N.Val->dump(CurDAG));
1137 else
1138 DEBUG(ResNode->dump(CurDAG));
Evan Chengf597dc72006-02-10 22:24:32 +00001139 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001140 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001141#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001142
1143 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001144}
1145
Chris Lattnerc0bad572006-06-08 18:03:49 +00001146bool X86DAGToDAGISel::
1147SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1148 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1149 SDOperand Op0, Op1, Op2, Op3;
1150 switch (ConstraintCode) {
1151 case 'o': // offsetable ??
1152 case 'v': // not offsetable ??
1153 default: return true;
1154 case 'm': // memory
1155 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1156 return true;
1157 break;
1158 }
1159
Evan Cheng04699902006-08-26 01:05:16 +00001160 OutOps.push_back(Op0);
1161 OutOps.push_back(Op1);
1162 OutOps.push_back(Op2);
1163 OutOps.push_back(Op3);
1164 AddToISelQueue(Op0);
1165 AddToISelQueue(Op1);
1166 AddToISelQueue(Op2);
1167 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001168 return false;
1169}
1170
Chris Lattnerc961eea2005-11-16 01:54:32 +00001171/// createX86ISelDag - This pass converts a legalized DAG into a
1172/// X86-specific DAG, ready for instruction scheduling.
1173///
Evan Chenge50794a2006-08-29 18:28:33 +00001174FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1175 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001176}