blob: 2dfc763850aa16dc0017c24055f13900975bc07d [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"
33#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000035#include "llvm/ADT/Statistic.h"
Evan Cheng2e945382006-07-28 06:05:06 +000036#include <deque>
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
Evan Cheng64a9e282006-08-28 20:10:17 +000042#include "llvm/Support/CommandLine.h"
43static cl::opt<bool> X86ISelPreproc("enable-x86-isel-preprocessing", cl::Hidden,
44 cl::desc("Enable isel preprocessing on X86"));
45
Chris Lattner655e7df2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
53 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
61 SDOperand Reg;
62 int FrameIndex;
63 } Base;
64
65 unsigned Scale;
66 SDOperand IndexReg;
67 unsigned Disp;
68 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000069 Constant *CP;
70 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000071
72 X86ISelAddressMode()
Evan Cheng77d86ff2006-02-25 10:09:08 +000073 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
74 CP(0), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000075 }
76 };
77}
78
79namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000080 Statistic<>
81 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
82
83 //===--------------------------------------------------------------------===//
84 /// ISel - X86 specific code to select X86 machine instructions for
85 /// SelectionDAG operations.
86 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000087 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000088 /// ContainsFPCode - Every instruction we select that uses or defines a FP
89 /// register should set this to true.
90 bool ContainsFPCode;
91
92 /// X86Lowering - This object fully describes how to lower LLVM code to an
93 /// X86-specific SelectionDAG.
94 X86TargetLowering X86Lowering;
95
96 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
97 /// make the right decision when generating code for different targets.
98 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +000099
100 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000101
Chris Lattner655e7df2005-11-16 01:54:32 +0000102 public:
Evan Cheng2dd2c652006-03-13 23:20:37 +0000103 X86DAGToDAGISel(X86TargetMachine &TM)
104 : SelectionDAGISel(X86Lowering),
Evan Cheng691a63d2006-07-27 16:44:36 +0000105 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000106 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000107
Evan Cheng5588de92006-02-18 00:15:05 +0000108 virtual bool runOnFunction(Function &Fn) {
109 // Make sure we re-emit a set of the global base reg if necessary
110 GlobalBaseReg = 0;
111 return SelectionDAGISel::runOnFunction(Fn);
112 }
113
Chris Lattner655e7df2005-11-16 01:54:32 +0000114 virtual const char *getPassName() const {
115 return "X86 DAG->DAG Instruction Selection";
116 }
117
118 /// InstructionSelectBasicBlock - This callback is invoked by
119 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
120 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
121
Evan Chengbc7a0f442006-01-11 06:09:51 +0000122 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
123
Evan Chenge2a3f702006-07-28 01:03:48 +0000124 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000125
Chris Lattner655e7df2005-11-16 01:54:32 +0000126// Include the pieces autogenerated from the target description.
127#include "X86GenDAGISel.inc"
128
129 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000130 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000131
Evan Chenga86ba852006-02-11 02:05:36 +0000132 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000133 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
134 SDOperand &Index, SDOperand &Disp);
135 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
136 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000137 bool TryFoldLoad(SDOperand P, SDOperand N,
138 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000139 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000140 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000141
Chris Lattnerba1ed582006-06-08 18:03:49 +0000142 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
143 /// inline asm expressions.
144 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
145 char ConstraintCode,
146 std::vector<SDOperand> &OutOps,
147 SelectionDAG &DAG);
148
Evan Chenge8a42362006-06-02 22:38:37 +0000149 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
150
Evan Cheng67ed58e2005-12-12 21:49:40 +0000151 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
152 SDOperand &Scale, SDOperand &Index,
153 SDOperand &Disp) {
154 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
155 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000156 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000157 Index = AM.IndexReg;
158 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000159 : (AM.CP ?
160 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
161 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000162 }
163
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000164 /// getI8Imm - Return a target constant with the specified value, of type
165 /// i8.
166 inline SDOperand getI8Imm(unsigned Imm) {
167 return CurDAG->getTargetConstant(Imm, MVT::i8);
168 }
169
Chris Lattner655e7df2005-11-16 01:54:32 +0000170 /// getI16Imm - Return a target constant with the specified value, of type
171 /// i16.
172 inline SDOperand getI16Imm(unsigned Imm) {
173 return CurDAG->getTargetConstant(Imm, MVT::i16);
174 }
175
176 /// getI32Imm - Return a target constant with the specified value, of type
177 /// i32.
178 inline SDOperand getI32Imm(unsigned Imm) {
179 return CurDAG->getTargetConstant(Imm, MVT::i32);
180 }
Evan Chengd49cc362006-02-10 22:24:32 +0000181
Evan Cheng5588de92006-02-18 00:15:05 +0000182 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
183 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000184 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000185
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000186#ifndef NDEBUG
187 unsigned Indent;
188#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000189 };
190}
191
Evan Cheng72bb66a2006-08-08 00:31:00 +0000192static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
193 std::set<SDNode *> &Visited) {
194 if (found ||
195 Use->getNodeId() > Def->getNodeId() ||
196 !Visited.insert(Use).second)
197 return;
198
199 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
200 SDNode *N = Use->getOperand(i).Val;
201 if (N != Def) {
202 findNonImmUse(N, Def, found, Visited);
203 } else {
204 found = true;
205 break;
206 }
207 }
208}
209
210static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
211 std::set<SDNode *> Visited;
212 bool found = false;
213 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
214 SDNode *N = Use->getOperand(i).Val;
215 if (N != Def) {
216 findNonImmUse(N, Def, found, Visited);
217 if (found) break;
218 }
219 }
220 return found;
221}
222
223
Evan Chenge2a3f702006-07-28 01:03:48 +0000224bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000225 // If U use can somehow reach N through another path then U can't fold N or
226 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000227 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000228 // a successor of U.
229 //
230 // [ N ]
231 // ^ ^
232 // | |
233 // / \---
234 // / [X]
235 // | ^
236 // [U]--------|
Evan Cheng72bb66a2006-08-08 00:31:00 +0000237 return !isNonImmUse(U, N);
Evan Cheng691a63d2006-07-27 16:44:36 +0000238}
239
Evan Cheng64a9e282006-08-28 20:10:17 +0000240/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
241/// and move load below the TokenFactor. Replace store's chain operand with
242/// load's chain result.
243static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
244 SDOperand Store, SDOperand TF) {
245 std::vector<SDOperand> Ops;
246 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
247 if (Load.Val == TF.Val->getOperand(i).Val)
248 Ops.push_back(Load.Val->getOperand(0));
249 else
250 Ops.push_back(TF.Val->getOperand(i));
251 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
252 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
253 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
254 Store.getOperand(2), Store.getOperand(3));
255}
256
257/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
258/// selector to pick more load-modify-store instructions. This is a common
259/// case:
260///
261/// [Load chain]
262/// ^
263/// |
264/// [Load]
265/// ^ ^
266/// | |
267/// / \-
268/// / |
269/// [TokenFactor] [Op]
270/// ^ ^
271/// | |
272/// \ /
273/// \ /
274/// [Store]
275///
276/// The fact the store's chain operand != load's chain will prevent the
277/// (store (op (load))) instruction from being selected. We can transform it to:
278///
279/// [Load chain]
280/// ^
281/// |
282/// [TokenFactor]
283/// ^
284/// |
285/// [Load]
286/// ^ ^
287/// | |
288/// | \-
289/// | |
290/// | [Op]
291/// | ^
292/// | |
293/// \ /
294/// \ /
295/// [Store]
296void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
297 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
298 E = DAG.allnodes_end(); I != E; ++I) {
299 if (I->getOpcode() != ISD::STORE)
300 continue;
301 SDOperand Chain = I->getOperand(0);
302 if (Chain.Val->getOpcode() != ISD::TokenFactor)
303 continue;
304
305 SDOperand N1 = I->getOperand(1);
306 SDOperand N2 = I->getOperand(2);
307 if (!N1.hasOneUse())
308 continue;
309
310 bool RModW = false;
311 SDOperand Load;
312 unsigned Opcode = N1.Val->getOpcode();
313 switch (Opcode) {
314 case ISD::ADD:
315 case ISD::MUL:
316 case ISD::FADD:
317 case ISD::FMUL:
318 case ISD::AND:
319 case ISD::OR:
320 case ISD::XOR:
321 case ISD::ADDC:
322 case ISD::ADDE: {
323 SDOperand N10 = N1.getOperand(0);
324 SDOperand N11 = N1.getOperand(1);
325 if (N10.Val->getOpcode() == ISD::LOAD)
326 RModW = true;
327 else if (N11.Val->getOpcode() == ISD::LOAD) {
328 RModW = true;
329 std::swap(N10, N11);
330 }
331 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
332 N10.getOperand(1) == N2;
333 if (RModW)
334 Load = N10;
335 break;
336 }
337 case ISD::SUB:
338 case ISD::SHL:
339 case ISD::SRA:
340 case ISD::SRL:
341 case ISD::ROTL:
342 case ISD::ROTR:
343 case ISD::SUBC:
344 case ISD::SUBE:
345 case X86ISD::SHLD:
346 case X86ISD::SHRD: {
347 SDOperand N10 = N1.getOperand(0);
348 if (N10.Val->getOpcode() == ISD::LOAD)
349 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
350 N10.getOperand(1) == N2;
351 if (RModW)
352 Load = N10;
353 break;
354 }
355 }
356
357 if (RModW)
358 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
359 }
360}
361
Chris Lattner655e7df2005-11-16 01:54:32 +0000362/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
363/// when it has created a SelectionDAG for us to codegen.
364void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
365 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000366 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000367
Evan Cheng64a9e282006-08-28 20:10:17 +0000368 if (X86ISelPreproc)
369 InstructionSelectPreprocess(DAG);
370
Chris Lattner655e7df2005-11-16 01:54:32 +0000371 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000372#ifndef NDEBUG
373 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000374 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000375#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000376 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000377#ifndef NDEBUG
378 DEBUG(std::cerr << "===== Instruction selection ends:\n");
379#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000380
Chris Lattner655e7df2005-11-16 01:54:32 +0000381 DAG.RemoveDeadNodes();
382
383 // Emit machine code to BB.
384 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000385
386 // If we are emitting FP stack code, scan the basic block to determine if this
387 // block defines any FP values. If so, put an FP_REG_KILL instruction before
388 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000389 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000390 // Note that FP stack instructions *are* used in SSE code when returning
391 // values, but these are not live out of the basic block, so we don't need
392 // an FP_REG_KILL in this case either.
393 bool ContainsFPCode = false;
394
395 // Scan all of the machine instructions in these MBBs, checking for FP
396 // stores.
397 MachineFunction::iterator MBBI = FirstMBB;
398 do {
399 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
400 !ContainsFPCode && I != E; ++I) {
401 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
402 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
403 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
404 RegMap->getRegClass(I->getOperand(0).getReg()) ==
405 X86::RFPRegisterClass) {
406 ContainsFPCode = true;
407 break;
408 }
409 }
410 }
411 } while (!ContainsFPCode && &*(MBBI++) != BB);
412
413 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
414 // a copy of the input value in this block.
415 if (!ContainsFPCode) {
416 // Final check, check LLVM BB's that are successors to the LLVM BB
417 // corresponding to BB for FP PHI nodes.
418 const BasicBlock *LLVMBB = BB->getBasicBlock();
419 const PHINode *PN;
420 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
421 !ContainsFPCode && SI != E; ++SI) {
422 for (BasicBlock::const_iterator II = SI->begin();
423 (PN = dyn_cast<PHINode>(II)); ++II) {
424 if (PN->getType()->isFloatingPoint()) {
425 ContainsFPCode = true;
426 break;
427 }
428 }
429 }
430 }
431
432 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
433 if (ContainsFPCode) {
434 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
435 ++NumFPKill;
436 }
437 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000438}
439
Evan Chengbc7a0f442006-01-11 06:09:51 +0000440/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
441/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000442void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
443 MachineFrameInfo *MFI) {
444 if (Subtarget->TargetType == X86Subtarget::isCygwin)
445 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
446
Evan Chengbc7a0f442006-01-11 06:09:51 +0000447 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
448 int CWFrameIdx = MFI->CreateStackObject(2, 2);
449 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
450
451 // Set the high part to be 64-bit precision.
452 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
453 CWFrameIdx, 1).addImm(2);
454
455 // Reload the modified control word now.
456 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
457}
458
459void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
460 // If this is main, emit special code for main.
461 MachineBasicBlock *BB = MF.begin();
462 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
463 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
464}
465
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000466/// MatchAddress - Add the specified node to the specified addressing mode,
467/// returning true if it cannot be done. This just pattern matches for the
468/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000469bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
470 bool isRoot) {
Evan Chengb9d34bd2006-08-07 22:28:20 +0000471 int id = N.Val->getNodeId();
472 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000473
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000474 switch (N.getOpcode()) {
475 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000476 case ISD::Constant:
477 AM.Disp += cast<ConstantSDNode>(N)->getValue();
478 return false;
479
480 case X86ISD::Wrapper:
481 // If both base and index components have been picked, we can't fit
482 // the result available in the register in the addressing mode. Duplicate
483 // GlobalAddress or ConstantPool as displacement.
484 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
485 if (ConstantPoolSDNode *CP =
486 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
487 if (AM.CP == 0) {
488 AM.CP = CP->get();
489 AM.Align = CP->getAlignment();
490 AM.Disp += CP->getOffset();
491 return false;
492 }
493 } else if (GlobalAddressSDNode *G =
494 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
495 if (AM.GV == 0) {
496 AM.GV = G->getGlobal();
497 AM.Disp += G->getOffset();
498 return false;
499 }
500 }
501 }
502 break;
503
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000504 case ISD::FrameIndex:
505 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
506 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
507 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
508 return false;
509 }
510 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000511
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000512 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000513 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000514 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
515 unsigned Val = CN->getValue();
516 if (Val == 1 || Val == 2 || Val == 3) {
517 AM.Scale = 1 << Val;
518 SDOperand ShVal = N.Val->getOperand(0);
519
520 // Okay, we know that we have a scale by now. However, if the scaled
521 // value is an add of something and a constant, we can fold the
522 // constant into the disp field here.
523 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
524 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
525 AM.IndexReg = ShVal.Val->getOperand(0);
526 ConstantSDNode *AddVal =
527 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
528 AM.Disp += AddVal->getValue() << Val;
529 } else {
530 AM.IndexReg = ShVal;
531 }
532 return false;
533 }
534 }
535 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000536
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000537 case ISD::MUL:
538 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000539 if (!Available &&
540 AM.BaseType == X86ISelAddressMode::RegBase &&
541 AM.Base.Reg.Val == 0 &&
542 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000543 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
544 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
545 AM.Scale = unsigned(CN->getValue())-1;
546
547 SDOperand MulVal = N.Val->getOperand(0);
548 SDOperand Reg;
549
550 // Okay, we know that we have a scale by now. However, if the scaled
551 // value is an add of something and a constant, we can fold the
552 // constant into the disp field here.
553 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
554 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
555 Reg = MulVal.Val->getOperand(0);
556 ConstantSDNode *AddVal =
557 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
558 AM.Disp += AddVal->getValue() * CN->getValue();
559 } else {
560 Reg = N.Val->getOperand(0);
561 }
562
563 AM.IndexReg = AM.Base.Reg = Reg;
564 return false;
565 }
566 break;
567
568 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000569 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000570 X86ISelAddressMode Backup = AM;
571 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
572 !MatchAddress(N.Val->getOperand(1), AM, false))
573 return false;
574 AM = Backup;
575 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
576 !MatchAddress(N.Val->getOperand(0), AM, false))
577 return false;
578 AM = Backup;
579 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000580 break;
581 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000582
583 case ISD::OR: {
584 if (!Available) {
585 X86ISelAddressMode Backup = AM;
586 // Look for (x << c1) | c2 where (c2 < c1)
587 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
588 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
589 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
590 AM.Disp = CN->getValue();
591 return false;
592 }
593 }
594 AM = Backup;
595 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
596 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
597 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
598 AM.Disp = CN->getValue();
599 return false;
600 }
601 }
602 AM = Backup;
603 }
604 break;
605 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000606 }
607
608 // Is the base register already occupied?
609 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
610 // If so, check to see if the scale index register is set.
611 if (AM.IndexReg.Val == 0) {
612 AM.IndexReg = N;
613 AM.Scale = 1;
614 return false;
615 }
616
617 // Otherwise, we cannot select it.
618 return true;
619 }
620
621 // Default, generate it as a register.
622 AM.BaseType = X86ISelAddressMode::RegBase;
623 AM.Base.Reg = N;
624 return false;
625}
626
Evan Chengc9fab312005-12-08 02:01:35 +0000627/// SelectAddr - returns true if it is able pattern match an addressing mode.
628/// It returns the operands which make up the maximal addressing mode it can
629/// match by reference.
630bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
631 SDOperand &Index, SDOperand &Disp) {
632 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000633 if (MatchAddress(N, AM))
634 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000635
Evan Chengbc7a0f442006-01-11 06:09:51 +0000636 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000637 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000638 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000639 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000640
Evan Chengd19d51f2006-02-05 05:25:07 +0000641 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000642 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
643
644 getAddressOperands(AM, Base, Scale, Index, Disp);
645 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000646}
647
Evan Cheng77d86ff2006-02-25 10:09:08 +0000648/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
649/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000650bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
651 SDOperand &Scale,
652 SDOperand &Index, SDOperand &Disp) {
653 X86ISelAddressMode AM;
654 if (MatchAddress(N, AM))
655 return false;
656
657 unsigned Complexity = 0;
658 if (AM.BaseType == X86ISelAddressMode::RegBase)
659 if (AM.Base.Reg.Val)
660 Complexity = 1;
661 else
662 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
663 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
664 Complexity = 4;
665
666 if (AM.IndexReg.Val)
667 Complexity++;
668 else
669 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
670
Evan Cheng990c3602006-02-28 21:13:57 +0000671 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000672 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000673 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
674 else if (AM.Scale > 1)
675 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000676
677 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
678 // to a LEA. This is determined with some expermentation but is by no means
679 // optimal (especially for code size consideration). LEA is nice because of
680 // its three-address nature. Tweak the cost function again when we can run
681 // convertToThreeAddress() at register allocation time.
682 if (AM.GV || AM.CP)
683 Complexity += 2;
684
685 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
686 Complexity++;
687
688 if (Complexity > 2) {
689 getAddressOperands(AM, Base, Scale, Index, Disp);
690 return true;
691 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000692 return false;
693}
694
Evan Chengd5f2ba02006-02-06 06:02:33 +0000695bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
696 SDOperand &Base, SDOperand &Scale,
697 SDOperand &Index, SDOperand &Disp) {
698 if (N.getOpcode() == ISD::LOAD &&
699 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000700 P.Val->isOnlyUse(N.Val) &&
701 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000702 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
703 return false;
704}
705
706static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000707 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
708 return (R->getReg() == 0);
709 return false;
710}
711
Evan Cheng5588de92006-02-18 00:15:05 +0000712/// getGlobalBaseReg - Output the instructions required to put the
713/// base address to use for accessing globals into a register.
714///
Evan Cheng61413a32006-08-26 05:34:46 +0000715SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng5588de92006-02-18 00:15:05 +0000716 if (!GlobalBaseReg) {
717 // Insert the set of GlobalBaseReg into the first MBB of the function
718 MachineBasicBlock &FirstMBB = BB->getParent()->front();
719 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
720 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
721 // FIXME: when we get to LP64, we will need to create the appropriate
722 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000723 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000724 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
725 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
726 }
Evan Cheng61413a32006-08-26 05:34:46 +0000727 return CurDAG->getRegister(GlobalBaseReg, MVT::i32).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000728}
729
Evan Chengf838cfc2006-05-20 01:36:52 +0000730static SDNode *FindCallStartFromCall(SDNode *Node) {
731 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
732 assert(Node->getOperand(0).getValueType() == MVT::Other &&
733 "Node doesn't have a token chain argument!");
734 return FindCallStartFromCall(Node->getOperand(0).Val);
735}
736
Evan Cheng61413a32006-08-26 05:34:46 +0000737SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000738 SDNode *Node = N.Val;
739 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000740 unsigned Opc, MOpc;
741 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000742
Evan Chengd49cc362006-02-10 22:24:32 +0000743#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000744 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000745 DEBUG(std::cerr << "Selecting: ");
746 DEBUG(Node->dump(CurDAG));
747 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000748 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000749#endif
750
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000751 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000752#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000753 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000754 DEBUG(std::cerr << "== ");
755 DEBUG(Node->dump(CurDAG));
756 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000757 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000758#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000759 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000760 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000761
Evan Cheng10d27902006-01-06 20:36:21 +0000762 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000763 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000764 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000765 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000766
Evan Cheng77d86ff2006-02-25 10:09:08 +0000767 case ISD::ADD: {
768 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
769 // code and is matched first so to prevent it from being turned into
770 // LEA32r X+c.
771 SDOperand N0 = N.getOperand(0);
772 SDOperand N1 = N.getOperand(1);
773 if (N.Val->getValueType(0) == MVT::i32 &&
774 N0.getOpcode() == X86ISD::Wrapper &&
775 N1.getOpcode() == ISD::Constant) {
776 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
777 SDOperand C(0, 0);
778 // TODO: handle ExternalSymbolSDNode.
779 if (GlobalAddressSDNode *G =
780 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
781 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
782 G->getOffset() + Offset);
783 } else if (ConstantPoolSDNode *CP =
784 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
785 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
786 CP->getAlignment(),
787 CP->getOffset()+Offset);
788 }
789
Evan Cheng2d487222006-08-26 01:05:16 +0000790 if (C.Val)
Evan Cheng34b70ee2006-08-26 08:00:10 +0000791 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000792 }
793
794 // Other cases are handled by auto-generated code.
795 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000796 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000797
Evan Cheng10d27902006-01-06 20:36:21 +0000798 case ISD::MULHU:
799 case ISD::MULHS: {
800 if (Opcode == ISD::MULHU)
801 switch (NVT) {
802 default: assert(0 && "Unsupported VT!");
803 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
804 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
805 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
806 }
807 else
808 switch (NVT) {
809 default: assert(0 && "Unsupported VT!");
810 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
811 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
812 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
813 }
814
815 unsigned LoReg, HiReg;
816 switch (NVT) {
817 default: assert(0 && "Unsupported VT!");
818 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
819 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
820 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
821 }
822
823 SDOperand N0 = Node->getOperand(0);
824 SDOperand N1 = Node->getOperand(1);
825
826 bool foldedLoad = false;
827 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000828 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000829 // MULHU and MULHS are commmutative
830 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000831 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000832 if (foldedLoad) {
833 N0 = Node->getOperand(1);
834 N1 = Node->getOperand(0);
835 }
836 }
837
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000838 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000839 if (foldedLoad) {
840 Chain = N1.getOperand(0);
841 AddToISelQueue(Chain);
842 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000843 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000844
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000845 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000846 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000847 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000848 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000849 InFlag = Chain.getValue(1);
850
851 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000852 AddToISelQueue(Tmp0);
853 AddToISelQueue(Tmp1);
854 AddToISelQueue(Tmp2);
855 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000856 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000857 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000858 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000859 Chain = SDOperand(CNode, 0);
860 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000861 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000862 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000863 InFlag =
864 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000865 }
866
Evan Cheng61413a32006-08-26 05:34:46 +0000867 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000868 ReplaceUses(N.getValue(0), Result);
869 if (foldedLoad)
870 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000871
Evan Chengd49cc362006-02-10 22:24:32 +0000872#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000873 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000874 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000875 DEBUG(Result.Val->dump(CurDAG));
876 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000877 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000878#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000879 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +0000880 }
Evan Cheng5588de92006-02-18 00:15:05 +0000881
Evan Cheng92e27972006-01-06 23:19:29 +0000882 case ISD::SDIV:
883 case ISD::UDIV:
884 case ISD::SREM:
885 case ISD::UREM: {
886 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
887 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
888 if (!isSigned)
889 switch (NVT) {
890 default: assert(0 && "Unsupported VT!");
891 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
892 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
893 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
894 }
895 else
896 switch (NVT) {
897 default: assert(0 && "Unsupported VT!");
898 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
899 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
900 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
901 }
902
903 unsigned LoReg, HiReg;
904 unsigned ClrOpcode, SExtOpcode;
905 switch (NVT) {
906 default: assert(0 && "Unsupported VT!");
907 case MVT::i8:
908 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000909 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000910 SExtOpcode = X86::CBW;
911 break;
912 case MVT::i16:
913 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000914 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000915 SExtOpcode = X86::CWD;
916 break;
917 case MVT::i32:
918 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000919 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000920 SExtOpcode = X86::CDQ;
921 break;
922 }
923
924 SDOperand N0 = Node->getOperand(0);
925 SDOperand N1 = Node->getOperand(1);
926
927 bool foldedLoad = false;
928 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000929 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000930 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000931 if (foldedLoad) {
932 Chain = N1.getOperand(0);
933 AddToISelQueue(Chain);
934 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000935 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000936
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000937 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000938 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000939 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000940 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000941 InFlag = Chain.getValue(1);
942
943 if (isSigned) {
944 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000945 InFlag =
946 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000947 } else {
948 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000949 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000950 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
951 ClrNode, InFlag);
952 InFlag = Chain.getValue(1);
953 }
954
955 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000956 AddToISelQueue(Tmp0);
957 AddToISelQueue(Tmp1);
958 AddToISelQueue(Tmp2);
959 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000960 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000961 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000962 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000963 Chain = SDOperand(CNode, 0);
964 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000965 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000966 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000967 InFlag =
968 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000969 }
970
Evan Cheng61413a32006-08-26 05:34:46 +0000971 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
972 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000973 ReplaceUses(N.getValue(0), Result);
974 if (foldedLoad)
975 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +0000976
977#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000978 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000979 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000980 DEBUG(Result.Val->dump(CurDAG));
981 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000982 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000983#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000984
985 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +0000986 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000987
988 case ISD::TRUNCATE: {
989 if (NVT == MVT::i8) {
990 unsigned Opc2;
991 MVT::ValueType VT;
992 switch (Node->getOperand(0).getValueType()) {
993 default: assert(0 && "Unknown truncate!");
994 case MVT::i16:
995 Opc = X86::MOV16to16_;
996 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +0000997 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000998 break;
999 case MVT::i32:
1000 Opc = X86::MOV32to32_;
1001 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +00001002 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001003 break;
1004 }
1005
Evan Cheng2d487222006-08-26 01:05:16 +00001006 AddToISelQueue(Node->getOperand(0));
1007 SDOperand Tmp =
1008 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001009 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001010
1011#ifndef NDEBUG
1012 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001013 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001014 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001015 DEBUG(std::cerr << "\n");
1016 Indent -= 2;
1017#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001018 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001019 }
Evan Chenga26c4512006-05-20 07:44:28 +00001020
1021 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001022 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001023 }
1024
Evan Cheng61413a32006-08-26 05:34:46 +00001025 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001026
Evan Chengd49cc362006-02-10 22:24:32 +00001027#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001028 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001029 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001030 if (ResNode == NULL || ResNode == N.Val)
1031 DEBUG(N.Val->dump(CurDAG));
1032 else
1033 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001034 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001035 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001036#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001037
1038 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001039}
1040
Chris Lattnerba1ed582006-06-08 18:03:49 +00001041bool X86DAGToDAGISel::
1042SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1043 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1044 SDOperand Op0, Op1, Op2, Op3;
1045 switch (ConstraintCode) {
1046 case 'o': // offsetable ??
1047 case 'v': // not offsetable ??
1048 default: return true;
1049 case 'm': // memory
1050 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1051 return true;
1052 break;
1053 }
1054
Evan Cheng2d487222006-08-26 01:05:16 +00001055 OutOps.push_back(Op0);
1056 OutOps.push_back(Op1);
1057 OutOps.push_back(Op2);
1058 OutOps.push_back(Op3);
1059 AddToISelQueue(Op0);
1060 AddToISelQueue(Op1);
1061 AddToISelQueue(Op2);
1062 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001063 return false;
1064}
1065
Chris Lattner655e7df2005-11-16 01:54:32 +00001066/// createX86ISelDag - This pass converts a legalized DAG into a
1067/// X86-specific DAG, ready for instruction scheduling.
1068///
Evan Cheng2dd2c652006-03-13 23:20:37 +00001069FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattner655e7df2005-11-16 01:54:32 +00001070 return new X86DAGToDAGISel(TM);
1071}