blob: 9f5c588df8cd091c38051b83beacece546521be4 [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
15#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000016#include "X86InstrBuilder.h"
Chris Lattner7c551262006-01-11 01:15:34 +000017#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000018#include "X86Subtarget.h"
19#include "X86ISelLowering.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000020#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000021#include "llvm/Instructions.h"
22#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000024#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/ADT/Statistic.h"
32using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// Pattern Matcher Implementation
36//===----------------------------------------------------------------------===//
37
38namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000039 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
40 /// SDOperand's instead of register numbers for the leaves of the matched
41 /// tree.
42 struct X86ISelAddressMode {
43 enum {
44 RegBase,
45 FrameIndexBase,
Evan Chengc9fab312005-12-08 02:01:35 +000046 ConstantPoolBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000047 } BaseType;
48
49 struct { // This is really a union, discriminated by BaseType!
50 SDOperand Reg;
51 int FrameIndex;
52 } Base;
53
54 unsigned Scale;
55 SDOperand IndexReg;
56 unsigned Disp;
57 GlobalValue *GV;
58
59 X86ISelAddressMode()
Evan Cheng4eb7af92005-11-30 02:51:20 +000060 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000061 }
62 };
63}
64
65namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000066 Statistic<>
67 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
68
69 //===--------------------------------------------------------------------===//
70 /// ISel - X86 specific code to select X86 machine instructions for
71 /// SelectionDAG operations.
72 ///
73 class X86DAGToDAGISel : public SelectionDAGISel {
74 /// ContainsFPCode - Every instruction we select that uses or defines a FP
75 /// register should set this to true.
76 bool ContainsFPCode;
77
78 /// X86Lowering - This object fully describes how to lower LLVM code to an
79 /// X86-specific SelectionDAG.
80 X86TargetLowering X86Lowering;
81
82 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
83 /// make the right decision when generating code for different targets.
84 const X86Subtarget *Subtarget;
85 public:
86 X86DAGToDAGISel(TargetMachine &TM)
87 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
88 Subtarget = &TM.getSubtarget<X86Subtarget>();
89 }
90
91 virtual const char *getPassName() const {
92 return "X86 DAG->DAG Instruction Selection";
93 }
94
95 /// InstructionSelectBasicBlock - This callback is invoked by
96 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
97 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
98
Evan Chengbc7a0f442006-01-11 06:09:51 +000099 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
100
Chris Lattner655e7df2005-11-16 01:54:32 +0000101// Include the pieces autogenerated from the target description.
102#include "X86GenDAGISel.inc"
103
104 private:
105 SDOperand Select(SDOperand N);
106
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000107 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
Evan Chengc9fab312005-12-08 02:01:35 +0000108 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
109 SDOperand &Index, SDOperand &Disp);
110 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
111 SDOperand &Index, SDOperand &Disp);
Evan Cheng10d27902006-01-06 20:36:21 +0000112 bool TryFoldLoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
113 SDOperand &Index, SDOperand &Disp);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000114
Evan Cheng67ed58e2005-12-12 21:49:40 +0000115 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
116 SDOperand &Scale, SDOperand &Index,
117 SDOperand &Disp) {
118 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
119 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000120 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000121 Index = AM.IndexReg;
122 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
123 : getI32Imm(AM.Disp);
124 }
125
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000126 /// getI8Imm - Return a target constant with the specified value, of type
127 /// i8.
128 inline SDOperand getI8Imm(unsigned Imm) {
129 return CurDAG->getTargetConstant(Imm, MVT::i8);
130 }
131
Chris Lattner655e7df2005-11-16 01:54:32 +0000132 /// getI16Imm - Return a target constant with the specified value, of type
133 /// i16.
134 inline SDOperand getI16Imm(unsigned Imm) {
135 return CurDAG->getTargetConstant(Imm, MVT::i16);
136 }
137
138 /// getI32Imm - Return a target constant with the specified value, of type
139 /// i32.
140 inline SDOperand getI32Imm(unsigned Imm) {
141 return CurDAG->getTargetConstant(Imm, MVT::i32);
142 }
143 };
144}
145
146/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
147/// when it has created a SelectionDAG for us to codegen.
148void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
149 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000150 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000151
152 // Codegen the basic block.
153 DAG.setRoot(Select(DAG.getRoot()));
Evan Cheng1d9b6712005-12-19 22:36:02 +0000154 CodeGenMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000155 DAG.RemoveDeadNodes();
156
157 // Emit machine code to BB.
158 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000159
160 // If we are emitting FP stack code, scan the basic block to determine if this
161 // block defines any FP values. If so, put an FP_REG_KILL instruction before
162 // the terminator of the block.
163 if (X86Vector < SSE2) {
164 // Note that FP stack instructions *are* used in SSE code when returning
165 // values, but these are not live out of the basic block, so we don't need
166 // an FP_REG_KILL in this case either.
167 bool ContainsFPCode = false;
168
169 // Scan all of the machine instructions in these MBBs, checking for FP
170 // stores.
171 MachineFunction::iterator MBBI = FirstMBB;
172 do {
173 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
174 !ContainsFPCode && I != E; ++I) {
175 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
176 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
177 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
178 RegMap->getRegClass(I->getOperand(0).getReg()) ==
179 X86::RFPRegisterClass) {
180 ContainsFPCode = true;
181 break;
182 }
183 }
184 }
185 } while (!ContainsFPCode && &*(MBBI++) != BB);
186
187 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
188 // a copy of the input value in this block.
189 if (!ContainsFPCode) {
190 // Final check, check LLVM BB's that are successors to the LLVM BB
191 // corresponding to BB for FP PHI nodes.
192 const BasicBlock *LLVMBB = BB->getBasicBlock();
193 const PHINode *PN;
194 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
195 !ContainsFPCode && SI != E; ++SI) {
196 for (BasicBlock::const_iterator II = SI->begin();
197 (PN = dyn_cast<PHINode>(II)); ++II) {
198 if (PN->getType()->isFloatingPoint()) {
199 ContainsFPCode = true;
200 break;
201 }
202 }
203 }
204 }
205
206 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
207 if (ContainsFPCode) {
208 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
209 ++NumFPKill;
210 }
211 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000212}
213
Evan Chengbc7a0f442006-01-11 06:09:51 +0000214/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
215/// the main function.
216static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
217 MachineFrameInfo *MFI) {
218 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
219 int CWFrameIdx = MFI->CreateStackObject(2, 2);
220 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
221
222 // Set the high part to be 64-bit precision.
223 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
224 CWFrameIdx, 1).addImm(2);
225
226 // Reload the modified control word now.
227 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
228}
229
230void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
231 // If this is main, emit special code for main.
232 MachineBasicBlock *BB = MF.begin();
233 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
234 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
235}
236
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000237/// MatchAddress - Add the specified node to the specified addressing mode,
238/// returning true if it cannot be done. This just pattern matches for the
239/// addressing mode
240bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
241 switch (N.getOpcode()) {
242 default: break;
243 case ISD::FrameIndex:
244 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
245 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
246 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
247 return false;
248 }
249 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000250
251 case ISD::ConstantPool:
252 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
253 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
254 AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
255 AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32);
256 return false;
257 }
258 }
259 break;
260
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000261 case ISD::GlobalAddress:
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000262 case ISD::TargetGlobalAddress:
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000263 if (AM.GV == 0) {
Evan Chenga74ce622005-12-21 02:39:21 +0000264 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Evan Cheng1d712482005-12-17 09:13:43 +0000265 return false;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000266 }
267 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000268
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000269 case ISD::Constant:
270 AM.Disp += cast<ConstantSDNode>(N)->getValue();
271 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000272
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000273 case ISD::SHL:
274 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
275 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
276 unsigned Val = CN->getValue();
277 if (Val == 1 || Val == 2 || Val == 3) {
278 AM.Scale = 1 << Val;
279 SDOperand ShVal = N.Val->getOperand(0);
280
281 // Okay, we know that we have a scale by now. However, if the scaled
282 // value is an add of something and a constant, we can fold the
283 // constant into the disp field here.
284 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
285 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
286 AM.IndexReg = ShVal.Val->getOperand(0);
287 ConstantSDNode *AddVal =
288 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
289 AM.Disp += AddVal->getValue() << Val;
290 } else {
291 AM.IndexReg = ShVal;
292 }
293 return false;
294 }
295 }
296 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000297
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000298 case ISD::MUL:
299 // X*[3,5,9] -> X+X*[2,4,8]
300 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
301 AM.Base.Reg.Val == 0)
302 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
303 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
304 AM.Scale = unsigned(CN->getValue())-1;
305
306 SDOperand MulVal = N.Val->getOperand(0);
307 SDOperand Reg;
308
309 // Okay, we know that we have a scale by now. However, if the scaled
310 // value is an add of something and a constant, we can fold the
311 // constant into the disp field here.
312 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
313 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
314 Reg = MulVal.Val->getOperand(0);
315 ConstantSDNode *AddVal =
316 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
317 AM.Disp += AddVal->getValue() * CN->getValue();
318 } else {
319 Reg = N.Val->getOperand(0);
320 }
321
322 AM.IndexReg = AM.Base.Reg = Reg;
323 return false;
324 }
325 break;
326
327 case ISD::ADD: {
328 X86ISelAddressMode Backup = AM;
329 if (!MatchAddress(N.Val->getOperand(0), AM) &&
330 !MatchAddress(N.Val->getOperand(1), AM))
331 return false;
332 AM = Backup;
333 if (!MatchAddress(N.Val->getOperand(1), AM) &&
334 !MatchAddress(N.Val->getOperand(0), AM))
335 return false;
336 AM = Backup;
337 break;
338 }
339 }
340
341 // Is the base register already occupied?
342 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
343 // If so, check to see if the scale index register is set.
344 if (AM.IndexReg.Val == 0) {
345 AM.IndexReg = N;
346 AM.Scale = 1;
347 return false;
348 }
349
350 // Otherwise, we cannot select it.
351 return true;
352 }
353
354 // Default, generate it as a register.
355 AM.BaseType = X86ISelAddressMode::RegBase;
356 AM.Base.Reg = N;
357 return false;
358}
359
Evan Chengc9fab312005-12-08 02:01:35 +0000360/// SelectAddr - returns true if it is able pattern match an addressing mode.
361/// It returns the operands which make up the maximal addressing mode it can
362/// match by reference.
363bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
364 SDOperand &Index, SDOperand &Disp) {
365 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000366 if (MatchAddress(N, AM))
367 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000368
Evan Chengbc7a0f442006-01-11 06:09:51 +0000369 if (AM.BaseType == X86ISelAddressMode::RegBase) {
370 if (AM.Base.Reg.Val) {
371 if (AM.Base.Reg.getOpcode() != ISD::Register)
372 AM.Base.Reg = Select(AM.Base.Reg);
373 } else {
374 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
375 }
Evan Chengc9fab312005-12-08 02:01:35 +0000376 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000377
378 if (AM.IndexReg.Val)
379 AM.IndexReg = Select(AM.IndexReg);
380 else
381 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
382
383 getAddressOperands(AM, Base, Scale, Index, Disp);
384 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000385}
386
Evan Cheng10d27902006-01-06 20:36:21 +0000387bool X86DAGToDAGISel::TryFoldLoad(SDOperand N, SDOperand &Base,
388 SDOperand &Scale, SDOperand &Index,
389 SDOperand &Disp) {
390 if (N.getOpcode() == ISD::LOAD && N.hasOneUse() &&
Evan Cheng92e27972006-01-06 23:19:29 +0000391 CodeGenMap.count(N.getValue(1)) == 0)
Evan Cheng10d27902006-01-06 20:36:21 +0000392 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
393 return false;
394}
395
396static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000397 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
398 return (R->getReg() == 0);
399 return false;
400}
401
402/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
403/// mode it matches can be cost effectively emitted as an LEA instruction.
404/// For X86, it always is unless it's just a (Reg + const).
Chris Lattner29852a582006-01-11 00:46:55 +0000405bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
406 SDOperand &Scale,
Evan Chengc9fab312005-12-08 02:01:35 +0000407 SDOperand &Index, SDOperand &Disp) {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000408 X86ISelAddressMode AM;
409 if (!MatchAddress(N, AM)) {
410 bool SelectBase = false;
411 bool SelectIndex = false;
412 bool Check = false;
413 if (AM.BaseType == X86ISelAddressMode::RegBase) {
414 if (AM.Base.Reg.Val) {
415 Check = true;
416 SelectBase = true;
Evan Chengc9fab312005-12-08 02:01:35 +0000417 } else {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000418 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000419 }
Evan Chengc9fab312005-12-08 02:01:35 +0000420 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000421
422 if (AM.IndexReg.Val) {
423 SelectIndex = true;
424 } else {
425 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
426 }
427
428 if (Check) {
429 unsigned Complexity = 0;
430 if (AM.Scale > 1)
431 Complexity++;
432 if (SelectIndex)
433 Complexity++;
434 if (AM.GV)
435 Complexity++;
436 else if (AM.Disp > 1)
437 Complexity++;
438 if (Complexity <= 1)
439 return false;
440 }
441
442 if (SelectBase)
443 AM.Base.Reg = Select(AM.Base.Reg);
444 if (SelectIndex)
445 AM.IndexReg = Select(AM.IndexReg);
446
447 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengc9fab312005-12-08 02:01:35 +0000448 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000449 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000450 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000451}
452
Evan Cheng00fcb002005-12-15 01:02:48 +0000453SDOperand X86DAGToDAGISel::Select(SDOperand N) {
454 SDNode *Node = N.Val;
455 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000456 unsigned Opc, MOpc;
457 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000458
Evan Cheng10d27902006-01-06 20:36:21 +0000459 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER)
Evan Cheng00fcb002005-12-15 01:02:48 +0000460 return N; // Already selected.
Chris Lattner655e7df2005-11-16 01:54:32 +0000461
Evan Cheng10d27902006-01-06 20:36:21 +0000462 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000463 default: break;
Evan Cheng10d27902006-01-06 20:36:21 +0000464 case ISD::MULHU:
465 case ISD::MULHS: {
466 if (Opcode == ISD::MULHU)
467 switch (NVT) {
468 default: assert(0 && "Unsupported VT!");
469 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
470 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
471 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
472 }
473 else
474 switch (NVT) {
475 default: assert(0 && "Unsupported VT!");
476 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
477 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
478 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
479 }
480
481 unsigned LoReg, HiReg;
482 switch (NVT) {
483 default: assert(0 && "Unsupported VT!");
484 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
485 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
486 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
487 }
488
489 SDOperand N0 = Node->getOperand(0);
490 SDOperand N1 = Node->getOperand(1);
491
492 bool foldedLoad = false;
493 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
494 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000495 // MULHU and MULHS are commmutative
496 if (!foldedLoad) {
497 foldedLoad = TryFoldLoad(N0, Tmp0, Tmp1, Tmp2, Tmp3);
498 if (foldedLoad) {
499 N0 = Node->getOperand(1);
500 N1 = Node->getOperand(0);
501 }
502 }
503
Evan Cheng10d27902006-01-06 20:36:21 +0000504 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
505 : CurDAG->getEntryNode();
506
507 SDOperand InFlag;
508 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
509 Select(N0), InFlag);
510 InFlag = Chain.getValue(1);
511
512 if (foldedLoad) {
513 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
514 Tmp2, Tmp3, Chain, InFlag);
515 InFlag = Chain.getValue(1);
516 } else {
517 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
518 }
519
520 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
521 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng92e27972006-01-06 23:19:29 +0000522 if (foldedLoad)
523 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
524 return Result;
525 }
526
527 case ISD::SDIV:
528 case ISD::UDIV:
529 case ISD::SREM:
530 case ISD::UREM: {
531 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
532 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
533 if (!isSigned)
534 switch (NVT) {
535 default: assert(0 && "Unsupported VT!");
536 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
537 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
538 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
539 }
540 else
541 switch (NVT) {
542 default: assert(0 && "Unsupported VT!");
543 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
544 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
545 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
546 }
547
548 unsigned LoReg, HiReg;
549 unsigned ClrOpcode, SExtOpcode;
550 switch (NVT) {
551 default: assert(0 && "Unsupported VT!");
552 case MVT::i8:
553 LoReg = X86::AL; HiReg = X86::AH;
554 ClrOpcode = X86::MOV8ri;
555 SExtOpcode = X86::CBW;
556 break;
557 case MVT::i16:
558 LoReg = X86::AX; HiReg = X86::DX;
559 ClrOpcode = X86::MOV16ri;
560 SExtOpcode = X86::CWD;
561 break;
562 case MVT::i32:
563 LoReg = X86::EAX; HiReg = X86::EDX;
564 ClrOpcode = X86::MOV32ri;
565 SExtOpcode = X86::CDQ;
566 break;
567 }
568
569 SDOperand N0 = Node->getOperand(0);
570 SDOperand N1 = Node->getOperand(1);
571
572 bool foldedLoad = false;
573 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
574 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
575 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
576 : CurDAG->getEntryNode();
577
578 SDOperand InFlag;
579 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
580 Select(N0), InFlag);
581 InFlag = Chain.getValue(1);
582
583 if (isSigned) {
584 // Sign extend the low part into the high part.
585 InFlag = CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag);
586 } else {
587 // Zero out the high part, effectively zero extending the input.
588 SDOperand ClrNode =
589 CurDAG->getTargetNode(ClrOpcode, NVT,
590 CurDAG->getTargetConstant(0, NVT));
591 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
592 ClrNode, InFlag);
593 InFlag = Chain.getValue(1);
594 }
595
596 if (foldedLoad) {
597 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
598 Tmp2, Tmp3, Chain, InFlag);
599 InFlag = Chain.getValue(1);
600 } else {
601 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
602 }
603
604 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
605 NVT, InFlag);
606 CodeGenMap[N.getValue(0)] = Result;
607 if (foldedLoad)
608 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
609 return Result;
Evan Cheng10d27902006-01-06 20:36:21 +0000610 }
Evan Cheng4eb7af92005-11-30 02:51:20 +0000611
Evan Chengbc7708c2005-12-17 02:02:50 +0000612 case ISD::TRUNCATE: {
613 unsigned Reg;
614 MVT::ValueType VT;
615 switch (Node->getOperand(0).getValueType()) {
616 default: assert(0 && "Unknown truncate!");
617 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
618 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
619 }
620 SDOperand Tmp0 = Select(Node->getOperand(0));
621 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
622 SDOperand InFlag = SDOperand(0,0);
623 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
624 Reg, Tmp1, InFlag).getValue(1);
625 SDOperand Chain = Result.getValue(0);
626 InFlag = Result.getValue(1);
627
628 switch (NVT) {
629 default: assert(0 && "Unknown truncate!");
630 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
631 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
632 }
633
634 Result = CurDAG->getCopyFromReg(Chain,
635 Reg, VT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000636 if (N.Val->hasOneUse())
637 return CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
638 else
639 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
Evan Chengbc7708c2005-12-17 02:02:50 +0000640 break;
641 }
Evan Cheng73a1ad92006-01-10 20:26:56 +0000642
643 case X86ISD::FP_TO_INT16_IN_MEM:
644 case X86ISD::FP_TO_INT32_IN_MEM:
645 case X86ISD::FP_TO_INT64_IN_MEM: {
646 assert(N.getOperand(1).getValueType() == MVT::f64);
647
648 // Change the floating point control register to use "round towards zero"
649 // mode when truncating to an integer value.
650 MachineFunction &MF = CurDAG->getMachineFunction();
651 int CWFI = MF.getFrameInfo()->CreateStackObject(2, 2);
652 SDOperand CWSlot = CurDAG->getFrameIndex(CWFI, MVT::i32);
653 SDOperand Base, Scale, Index, Disp;
654 (void)SelectAddr(CWSlot, Base, Scale, Index, Disp);
655 SDOperand Chain = N.getOperand(0);
656
657 // Save the control word.
658 Chain = CurDAG->getTargetNode(X86::FNSTCW16m, MVT::Other,
659 Base, Scale, Index, Disp, Chain);
660
661 // Load the old value of the high byte of the control word.
662 SDOperand OldCW =
663 CurDAG->getTargetNode(X86::MOV16rm, MVT::i16, MVT::Other,
664 Base, Scale, Index, Disp, Chain);
665 Chain = OldCW.getValue(1);
666
667 // Set the high part to be round to zero...
668 Chain = CurDAG->getTargetNode(X86::MOV16mi, MVT::Other,
669 Base, Scale, Index, Disp,
670 CurDAG->getConstant(0xC7F, MVT::i16),
671 Chain);
672
673 // Reload the modified control word now...
674 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
675 Base, Scale, Index, Disp, Chain);
676
677 // Restore the memory image of control word to original value
678 Chain = CurDAG->getTargetNode(X86::MOV16mr, MVT::Other,
679 Base, Scale, Index, Disp, OldCW, Chain);
680
681 switch (Opcode) {
682 case X86ISD::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
683 case X86ISD::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
684 case X86ISD::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
685 }
686
687 SDOperand N1 = Select(N.getOperand(1));
688 SDOperand Base2, Scale2, Index2, Disp2;
689 (void)SelectAddr(N.getOperand(2), Base2, Scale2, Index2, Disp2);
690 Chain = CurDAG->getTargetNode(Opc, MVT::Other,
691 Base2, Scale2, Index2, Disp2, N1, Chain);
692
693 // Reload the modified control word now...
694 CodeGenMap[N] =
695 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
696 Base, Scale, Index, Disp, Chain);
697 return Chain;
698 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000699 }
700
Evan Cheng00fcb002005-12-15 01:02:48 +0000701 return SelectCode(N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000702}
703
704/// createX86ISelDag - This pass converts a legalized DAG into a
705/// X86-specific DAG, ready for instruction scheduling.
706///
707FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
708 return new X86DAGToDAGISel(TM);
709}