blob: 3896797724e9f8b81a1a0d15b0d2a63d172a7e06 [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 Chengf597dc72006-02-10 22:24:32 +000015#define DEBUG_TYPE "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"
33#include "llvm/Support/Debug.h"
Chris Lattner2c79de82006-06-28 23:27:49 +000034#include "llvm/Support/Visibility.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000035#include "llvm/ADT/Statistic.h"
Evan Chengba277312006-07-28 06:05:06 +000036#include <deque>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000037#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000038#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000039using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42// Pattern Matcher Implementation
43//===----------------------------------------------------------------------===//
44
45namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000046 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
47 /// SDOperand's instead of register numbers for the leaves of the matched
48 /// tree.
49 struct X86ISelAddressMode {
50 enum {
51 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000052 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 } BaseType;
54
55 struct { // This is really a union, discriminated by BaseType!
56 SDOperand Reg;
57 int FrameIndex;
58 } Base;
59
60 unsigned Scale;
61 SDOperand IndexReg;
62 unsigned Disp;
63 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000064 Constant *CP;
65 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066
67 X86ISelAddressMode()
Evan Cheng51a9ed92006-02-25 10:09:08 +000068 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
69 CP(0), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000070 }
71 };
72}
73
74namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000075 Statistic<>
76 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
77
78 //===--------------------------------------------------------------------===//
79 /// ISel - X86 specific code to select X86 machine instructions for
80 /// SelectionDAG operations.
81 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000082 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000083 /// ContainsFPCode - Every instruction we select that uses or defines a FP
84 /// register should set this to true.
85 bool ContainsFPCode;
86
87 /// X86Lowering - This object fully describes how to lower LLVM code to an
88 /// X86-specific SelectionDAG.
89 X86TargetLowering X86Lowering;
90
91 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
92 /// make the right decision when generating code for different targets.
93 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +000094
95 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +000096
Chris Lattnerc961eea2005-11-16 01:54:32 +000097 public:
Evan Chengc4c62572006-03-13 23:20:37 +000098 X86DAGToDAGISel(X86TargetMachine &TM)
99 : SelectionDAGISel(X86Lowering),
Evan Chenga8df1b42006-07-27 16:44:36 +0000100 X86Lowering(*TM.getTargetLowering()),
101 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Evan Cheng686c4a12006-08-02 09:18:33 +0000102 DAGSize(0), ReachabilityMatrix(NULL), ReachMatrixRange(NULL) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000103
Evan Cheng7ccced62006-02-18 00:15:05 +0000104 virtual bool runOnFunction(Function &Fn) {
105 // Make sure we re-emit a set of the global base reg if necessary
106 GlobalBaseReg = 0;
107 return SelectionDAGISel::runOnFunction(Fn);
108 }
109
Chris Lattnerc961eea2005-11-16 01:54:32 +0000110 virtual const char *getPassName() const {
111 return "X86 DAG->DAG Instruction Selection";
112 }
113
114 /// InstructionSelectBasicBlock - This callback is invoked by
115 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
116 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
117
Evan Cheng8700e142006-01-11 06:09:51 +0000118 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
119
Evan Chengf2dfafc2006-07-28 01:03:48 +0000120 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000121
Chris Lattnerc961eea2005-11-16 01:54:32 +0000122// Include the pieces autogenerated from the target description.
123#include "X86GenDAGISel.inc"
124
125 private:
Evan Cheng37e18032006-07-28 06:33:41 +0000126 void DetermineReachability(SDNode *f, SDNode *t);
Evan Chenga8df1b42006-07-27 16:44:36 +0000127
Evan Cheng34167212006-02-09 00:37:58 +0000128 void Select(SDOperand &Result, SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000129
Evan Cheng2486af12006-02-11 02:05:36 +0000130 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengec693f72005-12-08 02:01:35 +0000131 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
132 SDOperand &Index, SDOperand &Disp);
133 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
134 SDOperand &Index, SDOperand &Disp);
Evan Cheng5e351682006-02-06 06:02:33 +0000135 bool TryFoldLoad(SDOperand P, SDOperand N,
136 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000137 SDOperand &Index, SDOperand &Disp);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000138 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
139 /// inline asm expressions.
140 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
141 char ConstraintCode,
142 std::vector<SDOperand> &OutOps,
143 SelectionDAG &DAG);
144
Evan Cheng3649b0e2006-06-02 22:38:37 +0000145 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
146
Evan Chenge5280532005-12-12 21:49:40 +0000147 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index,
149 SDOperand &Disp) {
150 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
151 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000152 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000153 Index = AM.IndexReg;
154 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000155 : (AM.CP ?
156 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
157 : getI32Imm(AM.Disp));
Evan Chenge5280532005-12-12 21:49:40 +0000158 }
159
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000160 /// getI8Imm - Return a target constant with the specified value, of type
161 /// i8.
162 inline SDOperand getI8Imm(unsigned Imm) {
163 return CurDAG->getTargetConstant(Imm, MVT::i8);
164 }
165
Chris Lattnerc961eea2005-11-16 01:54:32 +0000166 /// getI16Imm - Return a target constant with the specified value, of type
167 /// i16.
168 inline SDOperand getI16Imm(unsigned Imm) {
169 return CurDAG->getTargetConstant(Imm, MVT::i16);
170 }
171
172 /// getI32Imm - Return a target constant with the specified value, of type
173 /// i32.
174 inline SDOperand getI32Imm(unsigned Imm) {
175 return CurDAG->getTargetConstant(Imm, MVT::i32);
176 }
Evan Chengf597dc72006-02-10 22:24:32 +0000177
Evan Cheng7ccced62006-02-18 00:15:05 +0000178 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
179 /// base register. Return the virtual register that holds this value.
180 SDOperand getGlobalBaseReg();
181
Evan Chenga8df1b42006-07-27 16:44:36 +0000182 /// DAGSize - Number of nodes in the DAG.
183 ///
184 unsigned DAGSize;
185
186 /// TopOrder - Topological ordering of all nodes in the DAG.
187 ///
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000188 std::vector<SDNode*> TopOrder;
Evan Cheng5fa5de82006-07-27 22:10:00 +0000189
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000190 /// ReachabilityMatrix - A N x N matrix representing all pairs reachability
Evan Chenga8df1b42006-07-27 16:44:36 +0000191 /// information. One bit per potential edge.
Evan Cheng686c4a12006-08-02 09:18:33 +0000192 unsigned char *ReachabilityMatrix;
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000193
Evan Cheng686c4a12006-08-02 09:18:33 +0000194 /// ReachMatrixRange - The range of reachability information available for
195 /// the particular source node.
196 unsigned *ReachMatrixRange;
Evan Chenga8df1b42006-07-27 16:44:36 +0000197
198 inline void setReachable(SDNode *f, SDNode *t) {
199 unsigned Idx = f->getNodeId() * DAGSize + t->getNodeId();
Evan Cheng686c4a12006-08-02 09:18:33 +0000200 ReachabilityMatrix[Idx / 8] |= 1 << (Idx % 8);
Evan Chenga8df1b42006-07-27 16:44:36 +0000201 }
202
203 inline bool isReachable(SDNode *f, SDNode *t) {
204 unsigned Idx = f->getNodeId() * DAGSize + t->getNodeId();
Evan Cheng686c4a12006-08-02 09:18:33 +0000205 return ReachabilityMatrix[Idx / 8] & (1 << (Idx % 8));
Evan Chenga8df1b42006-07-27 16:44:36 +0000206 }
207
Evan Cheng2584d932006-07-28 00:49:31 +0000208 /// UnfoldableSet - An boolean array representing nodes which have been
209 /// folded into addressing modes and therefore should not be folded in
210 /// another operation.
Evan Cheng686c4a12006-08-02 09:18:33 +0000211 unsigned char *UnfoldableSet;
Evan Cheng2584d932006-07-28 00:49:31 +0000212
213 inline void setUnfoldable(SDNode *N) {
Evan Cheng686c4a12006-08-02 09:18:33 +0000214 unsigned Id = N->getNodeId();
215 UnfoldableSet[Id / 8] |= 1 << (Id % 8);
Evan Cheng2584d932006-07-28 00:49:31 +0000216 }
217
218 inline bool isUnfoldable(SDNode *N) {
Evan Cheng686c4a12006-08-02 09:18:33 +0000219 unsigned Id = N->getNodeId();
220 return UnfoldableSet[Id / 8] & (1 << (Id % 8));
Evan Cheng2584d932006-07-28 00:49:31 +0000221 }
222
Evan Cheng23addc02006-02-10 22:46:26 +0000223#ifndef NDEBUG
224 unsigned Indent;
225#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000226 };
227}
228
Evan Chengf2dfafc2006-07-28 01:03:48 +0000229bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng2584d932006-07-28 00:49:31 +0000230 // Is it already folded by SelectAddr / SelectLEAAddr?
231 if (isUnfoldable(N))
232 return false;
233
Evan Chenga8df1b42006-07-27 16:44:36 +0000234 // If U use can somehow reach N through another path then U can't fold N or
235 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000236 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000237 // a successor of U.
238 //
239 // [ N ]
240 // ^ ^
241 // | |
242 // / \---
243 // / [X]
244 // | ^
245 // [U]--------|
Evan Cheng37e18032006-07-28 06:33:41 +0000246 DetermineReachability(U, N);
Evan Chenga8df1b42006-07-27 16:44:36 +0000247 assert(isReachable(U, N) && "Attempting to fold a non-operand node?");
248 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I) {
249 SDNode *P = I->Val;
250 if (P != N && isReachable(P, N))
251 return false;
252 }
253 return true;
254}
255
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000256/// DetermineReachability - Determine reachability between all pairs of nodes
Evan Chengba277312006-07-28 06:05:06 +0000257/// between f and t in topological order.
Evan Cheng37e18032006-07-28 06:33:41 +0000258void X86DAGToDAGISel::DetermineReachability(SDNode *f, SDNode *t) {
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000259 unsigned Orderf = f->getNodeId();
260 unsigned Ordert = t->getNodeId();
261 unsigned Range = ReachMatrixRange[Orderf];
Evan Cheng5fa5de82006-07-27 22:10:00 +0000262 if (Range >= Ordert)
263 return;
264 if (Range < Orderf)
265 Range = Orderf;
266
267 for (unsigned i = Range; i < Ordert; ++i) {
Evan Chenga8df1b42006-07-27 16:44:36 +0000268 SDNode *N = TopOrder[i];
269 setReachable(N, N);
270 // If N is a leaf node, there is nothing more to do.
271 if (N->getNumOperands() == 0)
272 continue;
273
Evan Cheng5fa5de82006-07-27 22:10:00 +0000274 for (unsigned i2 = Orderf; ; ++i2) {
Evan Chenga8df1b42006-07-27 16:44:36 +0000275 SDNode *M = TopOrder[i2];
276 if (isReachable(M, N)) {
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000277 // Update reachability from M to N's operands.
Evan Chenga8df1b42006-07-27 16:44:36 +0000278 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E;++I)
279 setReachable(M, I->Val);
280 }
281 if (M == N) break;
282 }
283 }
Evan Cheng5fa5de82006-07-27 22:10:00 +0000284
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000285 ReachMatrixRange[Orderf] = Ordert;
Evan Chenga8df1b42006-07-27 16:44:36 +0000286}
287
Chris Lattnerc961eea2005-11-16 01:54:32 +0000288/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
289/// when it has created a SelectionDAG for us to codegen.
290void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
291 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000292 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000293
Evan Chengdb3cc3d2006-08-01 08:17:22 +0000294 TopOrder = DAG.AssignTopologicalOrder();
295 DAGSize = TopOrder.size();
Evan Cheng686c4a12006-08-02 09:18:33 +0000296 unsigned RMSize = (DAGSize * DAGSize + 7) / 8;
297 ReachabilityMatrix = new unsigned char[RMSize];
298 memset(ReachabilityMatrix, 0, RMSize);
299 ReachMatrixRange = new unsigned[DAGSize];
300 memset(ReachMatrixRange, 0, DAGSize * sizeof(unsigned));
301 unsigned NumBytes = (DAGSize + 7) / 8;
302 UnfoldableSet = new unsigned char[NumBytes];
303 memset(UnfoldableSet, 0, NumBytes);
Evan Cheng63ce5682006-07-28 00:10:59 +0000304
Chris Lattnerc961eea2005-11-16 01:54:32 +0000305 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000306#ifndef NDEBUG
307 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000308 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000309#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000310 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000311#ifndef NDEBUG
312 DEBUG(std::cerr << "===== Instruction selection ends:\n");
313#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000314
Evan Cheng686c4a12006-08-02 09:18:33 +0000315 delete[] ReachabilityMatrix;
316 delete[] ReachMatrixRange;
317 delete[] UnfoldableSet;
318 ReachabilityMatrix = NULL;
319 ReachMatrixRange = NULL;
320 UnfoldableSet = NULL;
Evan Chengfcaa9952005-12-19 22:36:02 +0000321 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000322 HandleMap.clear();
323 ReplaceMap.clear();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000324 DAG.RemoveDeadNodes();
325
326 // Emit machine code to BB.
327 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000328
329 // If we are emitting FP stack code, scan the basic block to determine if this
330 // block defines any FP values. If so, put an FP_REG_KILL instruction before
331 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000332 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000333 // Note that FP stack instructions *are* used in SSE code when returning
334 // values, but these are not live out of the basic block, so we don't need
335 // an FP_REG_KILL in this case either.
336 bool ContainsFPCode = false;
337
338 // Scan all of the machine instructions in these MBBs, checking for FP
339 // stores.
340 MachineFunction::iterator MBBI = FirstMBB;
341 do {
342 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
343 !ContainsFPCode && I != E; ++I) {
344 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
345 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
346 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
347 RegMap->getRegClass(I->getOperand(0).getReg()) ==
348 X86::RFPRegisterClass) {
349 ContainsFPCode = true;
350 break;
351 }
352 }
353 }
354 } while (!ContainsFPCode && &*(MBBI++) != BB);
355
356 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
357 // a copy of the input value in this block.
358 if (!ContainsFPCode) {
359 // Final check, check LLVM BB's that are successors to the LLVM BB
360 // corresponding to BB for FP PHI nodes.
361 const BasicBlock *LLVMBB = BB->getBasicBlock();
362 const PHINode *PN;
363 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
364 !ContainsFPCode && SI != E; ++SI) {
365 for (BasicBlock::const_iterator II = SI->begin();
366 (PN = dyn_cast<PHINode>(II)); ++II) {
367 if (PN->getType()->isFloatingPoint()) {
368 ContainsFPCode = true;
369 break;
370 }
371 }
372 }
373 }
374
375 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
376 if (ContainsFPCode) {
377 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
378 ++NumFPKill;
379 }
380 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000381}
382
Evan Cheng8700e142006-01-11 06:09:51 +0000383/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
384/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000385void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
386 MachineFrameInfo *MFI) {
387 if (Subtarget->TargetType == X86Subtarget::isCygwin)
388 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
389
Evan Cheng8700e142006-01-11 06:09:51 +0000390 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
391 int CWFrameIdx = MFI->CreateStackObject(2, 2);
392 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
393
394 // Set the high part to be 64-bit precision.
395 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
396 CWFrameIdx, 1).addImm(2);
397
398 // Reload the modified control word now.
399 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
400}
401
402void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
403 // If this is main, emit special code for main.
404 MachineBasicBlock *BB = MF.begin();
405 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
406 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
407}
408
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000409/// MatchAddress - Add the specified node to the specified addressing mode,
410/// returning true if it cannot be done. This just pattern matches for the
411/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000412bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
413 bool isRoot) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000414 bool Available = false;
415 // If N has already been selected, reuse the result unless in some very
416 // specific cases.
Evan Cheng2486af12006-02-11 02:05:36 +0000417 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
418 if (CGMI != CodeGenMap.end()) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000419 Available = true;
Evan Cheng2486af12006-02-11 02:05:36 +0000420 }
421
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000422 switch (N.getOpcode()) {
423 default: break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000424 case ISD::Constant:
425 AM.Disp += cast<ConstantSDNode>(N)->getValue();
426 return false;
427
428 case X86ISD::Wrapper:
429 // If both base and index components have been picked, we can't fit
430 // the result available in the register in the addressing mode. Duplicate
431 // GlobalAddress or ConstantPool as displacement.
432 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
433 if (ConstantPoolSDNode *CP =
434 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
435 if (AM.CP == 0) {
436 AM.CP = CP->get();
437 AM.Align = CP->getAlignment();
438 AM.Disp += CP->getOffset();
439 return false;
440 }
441 } else if (GlobalAddressSDNode *G =
442 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
443 if (AM.GV == 0) {
444 AM.GV = G->getGlobal();
445 AM.Disp += G->getOffset();
446 return false;
447 }
448 }
449 }
450 break;
451
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000452 case ISD::FrameIndex:
453 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
454 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
455 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
456 return false;
457 }
458 break;
Evan Chengec693f72005-12-08 02:01:35 +0000459
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000460 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000461 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000462 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
463 unsigned Val = CN->getValue();
464 if (Val == 1 || Val == 2 || Val == 3) {
465 AM.Scale = 1 << Val;
466 SDOperand ShVal = N.Val->getOperand(0);
467
468 // Okay, we know that we have a scale by now. However, if the scaled
469 // value is an add of something and a constant, we can fold the
470 // constant into the disp field here.
471 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
472 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
473 AM.IndexReg = ShVal.Val->getOperand(0);
474 ConstantSDNode *AddVal =
475 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
476 AM.Disp += AddVal->getValue() << Val;
477 } else {
478 AM.IndexReg = ShVal;
479 }
480 return false;
481 }
482 }
483 break;
Evan Chengec693f72005-12-08 02:01:35 +0000484
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000485 case ISD::MUL:
486 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000487 if (!Available &&
488 AM.BaseType == X86ISelAddressMode::RegBase &&
489 AM.Base.Reg.Val == 0 &&
490 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000491 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
492 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
493 AM.Scale = unsigned(CN->getValue())-1;
494
495 SDOperand MulVal = N.Val->getOperand(0);
496 SDOperand Reg;
497
498 // Okay, we know that we have a scale by now. However, if the scaled
499 // value is an add of something and a constant, we can fold the
500 // constant into the disp field here.
501 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
502 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
503 Reg = MulVal.Val->getOperand(0);
504 ConstantSDNode *AddVal =
505 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
506 AM.Disp += AddVal->getValue() * CN->getValue();
507 } else {
508 Reg = N.Val->getOperand(0);
509 }
510
511 AM.IndexReg = AM.Base.Reg = Reg;
512 return false;
513 }
514 break;
515
516 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000517 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000518 X86ISelAddressMode Backup = AM;
519 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
520 !MatchAddress(N.Val->getOperand(1), AM, false))
521 return false;
522 AM = Backup;
523 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
524 !MatchAddress(N.Val->getOperand(0), AM, false))
525 return false;
526 AM = Backup;
527 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000528 break;
529 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000530
531 case ISD::OR: {
532 if (!Available) {
533 X86ISelAddressMode Backup = AM;
534 // Look for (x << c1) | c2 where (c2 < c1)
535 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
536 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
537 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
538 AM.Disp = CN->getValue();
539 return false;
540 }
541 }
542 AM = Backup;
543 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
544 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
545 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
546 AM.Disp = CN->getValue();
547 return false;
548 }
549 }
550 AM = Backup;
551 }
552 break;
553 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000554 }
555
556 // Is the base register already occupied?
557 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
558 // If so, check to see if the scale index register is set.
559 if (AM.IndexReg.Val == 0) {
560 AM.IndexReg = N;
561 AM.Scale = 1;
562 return false;
563 }
564
565 // Otherwise, we cannot select it.
566 return true;
567 }
568
569 // Default, generate it as a register.
570 AM.BaseType = X86ISelAddressMode::RegBase;
571 AM.Base.Reg = N;
572 return false;
573}
574
Evan Chengec693f72005-12-08 02:01:35 +0000575/// SelectAddr - returns true if it is able pattern match an addressing mode.
576/// It returns the operands which make up the maximal addressing mode it can
577/// match by reference.
578bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
579 SDOperand &Index, SDOperand &Disp) {
580 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000581 if (MatchAddress(N, AM))
582 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000583
Evan Cheng8700e142006-01-11 06:09:51 +0000584 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000585 if (!AM.Base.Reg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000586 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000587 }
Evan Cheng8700e142006-01-11 06:09:51 +0000588
Evan Cheng7dd281b2006-02-05 05:25:07 +0000589 if (!AM.IndexReg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000590 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
591
592 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000593
Evan Cheng2584d932006-07-28 00:49:31 +0000594 int Id = Base.Val ? Base.Val->getNodeId() : -1;
595 if (Id != -1)
596 setUnfoldable(Base.Val);
597 Id = Index.Val ? Index.Val->getNodeId() : -1;
598 if (Id != -1)
599 setUnfoldable(Index.Val);
600
Evan Cheng8700e142006-01-11 06:09:51 +0000601 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000602}
603
Evan Cheng51a9ed92006-02-25 10:09:08 +0000604/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
605/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000606bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
607 SDOperand &Scale,
608 SDOperand &Index, SDOperand &Disp) {
609 X86ISelAddressMode AM;
610 if (MatchAddress(N, AM))
611 return false;
612
613 unsigned Complexity = 0;
614 if (AM.BaseType == X86ISelAddressMode::RegBase)
615 if (AM.Base.Reg.Val)
616 Complexity = 1;
617 else
618 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
619 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
620 Complexity = 4;
621
622 if (AM.IndexReg.Val)
623 Complexity++;
624 else
625 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
626
Evan Cheng8c03fe42006-02-28 21:13:57 +0000627 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000628 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000629 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
630 else if (AM.Scale > 1)
631 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000632
633 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
634 // to a LEA. This is determined with some expermentation but is by no means
635 // optimal (especially for code size consideration). LEA is nice because of
636 // its three-address nature. Tweak the cost function again when we can run
637 // convertToThreeAddress() at register allocation time.
638 if (AM.GV || AM.CP)
639 Complexity += 2;
640
641 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
642 Complexity++;
643
644 if (Complexity > 2) {
645 getAddressOperands(AM, Base, Scale, Index, Disp);
646 return true;
647 }
648
Evan Cheng2584d932006-07-28 00:49:31 +0000649 int Id = Base.Val ? Base.Val->getNodeId() : -1;
650 if (Id != -1)
651 setUnfoldable(Base.Val);
652 Id = Index.Val ? Index.Val->getNodeId() : -1;
653 if (Id != -1)
654 setUnfoldable(Index.Val);
655
Evan Cheng51a9ed92006-02-25 10:09:08 +0000656 return false;
657}
658
Evan Cheng5e351682006-02-06 06:02:33 +0000659bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
660 SDOperand &Base, SDOperand &Scale,
661 SDOperand &Index, SDOperand &Disp) {
662 if (N.getOpcode() == ISD::LOAD &&
663 N.hasOneUse() &&
664 !CodeGenMap.count(N.getValue(0)) &&
Evan Chengf2dfafc2006-07-28 01:03:48 +0000665 !CanBeFoldedBy(N.Val, P.Val))
Evan Cheng0114e942006-01-06 20:36:21 +0000666 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
667 return false;
668}
669
670static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000671 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
672 return (R->getReg() == 0);
673 return false;
674}
675
Evan Cheng7ccced62006-02-18 00:15:05 +0000676/// getGlobalBaseReg - Output the instructions required to put the
677/// base address to use for accessing globals into a register.
678///
679SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
680 if (!GlobalBaseReg) {
681 // Insert the set of GlobalBaseReg into the first MBB of the function
682 MachineBasicBlock &FirstMBB = BB->getParent()->front();
683 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
684 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
685 // FIXME: when we get to LP64, we will need to create the appropriate
686 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000687 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000688 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
689 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
690 }
691 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
692}
693
Evan Chengb245d922006-05-20 01:36:52 +0000694static SDNode *FindCallStartFromCall(SDNode *Node) {
695 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
696 assert(Node->getOperand(0).getValueType() == MVT::Other &&
697 "Node doesn't have a token chain argument!");
698 return FindCallStartFromCall(Node->getOperand(0).Val);
699}
700
Evan Cheng34167212006-02-09 00:37:58 +0000701void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000702 SDNode *Node = N.Val;
703 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000704 unsigned Opc, MOpc;
705 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000706
Evan Chengf597dc72006-02-10 22:24:32 +0000707#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000708 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000709 DEBUG(std::cerr << "Selecting: ");
710 DEBUG(Node->dump(CurDAG));
711 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000712 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000713#endif
714
Evan Cheng34167212006-02-09 00:37:58 +0000715 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
716 Result = N;
Evan Chengf597dc72006-02-10 22:24:32 +0000717#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000718 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000719 DEBUG(std::cerr << "== ");
720 DEBUG(Node->dump(CurDAG));
721 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000722 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000723#endif
Evan Cheng34167212006-02-09 00:37:58 +0000724 return; // Already selected.
725 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000726
727 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng34167212006-02-09 00:37:58 +0000728 if (CGMI != CodeGenMap.end()) {
729 Result = CGMI->second;
Evan Chengf597dc72006-02-10 22:24:32 +0000730#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000731 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000732 DEBUG(std::cerr << "== ");
733 DEBUG(Result.Val->dump(CurDAG));
734 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000735 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000736#endif
Evan Cheng34167212006-02-09 00:37:58 +0000737 return;
738 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000739
Evan Cheng0114e942006-01-06 20:36:21 +0000740 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000741 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000742 case X86ISD::GlobalBaseReg:
743 Result = getGlobalBaseReg();
744 return;
745
Evan Cheng51a9ed92006-02-25 10:09:08 +0000746 case ISD::ADD: {
747 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
748 // code and is matched first so to prevent it from being turned into
749 // LEA32r X+c.
750 SDOperand N0 = N.getOperand(0);
751 SDOperand N1 = N.getOperand(1);
752 if (N.Val->getValueType(0) == MVT::i32 &&
753 N0.getOpcode() == X86ISD::Wrapper &&
754 N1.getOpcode() == ISD::Constant) {
755 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
756 SDOperand C(0, 0);
757 // TODO: handle ExternalSymbolSDNode.
758 if (GlobalAddressSDNode *G =
759 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
760 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
761 G->getOffset() + Offset);
762 } else if (ConstantPoolSDNode *CP =
763 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
764 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
765 CP->getAlignment(),
766 CP->getOffset()+Offset);
767 }
768
769 if (C.Val) {
770 if (N.Val->hasOneUse()) {
771 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
772 } else {
773 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
774 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
775 }
776 return;
777 }
778 }
779
780 // Other cases are handled by auto-generated code.
781 break;
Evan Chenga0ea0532006-02-23 02:43:52 +0000782 }
Evan Cheng020d2e82006-02-23 20:41:18 +0000783
Evan Cheng0114e942006-01-06 20:36:21 +0000784 case ISD::MULHU:
785 case ISD::MULHS: {
786 if (Opcode == ISD::MULHU)
787 switch (NVT) {
788 default: assert(0 && "Unsupported VT!");
789 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
790 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
791 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
792 }
793 else
794 switch (NVT) {
795 default: assert(0 && "Unsupported VT!");
796 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
797 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
798 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
799 }
800
801 unsigned LoReg, HiReg;
802 switch (NVT) {
803 default: assert(0 && "Unsupported VT!");
804 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
805 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
806 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
807 }
808
809 SDOperand N0 = Node->getOperand(0);
810 SDOperand N1 = Node->getOperand(1);
811
812 bool foldedLoad = false;
813 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000814 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000815 // MULHU and MULHS are commmutative
816 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +0000817 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000818 if (foldedLoad) {
819 N0 = Node->getOperand(1);
820 N1 = Node->getOperand(0);
821 }
822 }
823
Evan Cheng34167212006-02-09 00:37:58 +0000824 SDOperand Chain;
825 if (foldedLoad)
826 Select(Chain, N1.getOperand(0));
827 else
828 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +0000829
Evan Cheng34167212006-02-09 00:37:58 +0000830 SDOperand InFlag(0, 0);
831 Select(N0, N0);
Evan Cheng0114e942006-01-06 20:36:21 +0000832 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000833 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000834 InFlag = Chain.getValue(1);
835
836 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000837 Select(Tmp0, Tmp0);
838 Select(Tmp1, Tmp1);
839 Select(Tmp2, Tmp2);
840 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000841 SDNode *CNode =
842 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
843 Tmp2, Tmp3, Chain, InFlag);
844 Chain = SDOperand(CNode, 0);
845 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +0000846 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000847 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000848 InFlag =
849 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +0000850 }
851
Evan Cheng34167212006-02-09 00:37:58 +0000852 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000853 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000854 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000855 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000856 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000857 }
Evan Cheng34167212006-02-09 00:37:58 +0000858
Evan Chengf597dc72006-02-10 22:24:32 +0000859#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000860 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000861 DEBUG(std::cerr << "== ");
862 DEBUG(Result.Val->dump(CurDAG));
863 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000864 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000865#endif
Evan Cheng34167212006-02-09 00:37:58 +0000866 return;
Evan Cheng948f3432006-01-06 23:19:29 +0000867 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000868
Evan Cheng948f3432006-01-06 23:19:29 +0000869 case ISD::SDIV:
870 case ISD::UDIV:
871 case ISD::SREM:
872 case ISD::UREM: {
873 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
874 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
875 if (!isSigned)
876 switch (NVT) {
877 default: assert(0 && "Unsupported VT!");
878 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
879 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
880 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
881 }
882 else
883 switch (NVT) {
884 default: assert(0 && "Unsupported VT!");
885 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
886 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
887 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
888 }
889
890 unsigned LoReg, HiReg;
891 unsigned ClrOpcode, SExtOpcode;
892 switch (NVT) {
893 default: assert(0 && "Unsupported VT!");
894 case MVT::i8:
895 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +0000896 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000897 SExtOpcode = X86::CBW;
898 break;
899 case MVT::i16:
900 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +0000901 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000902 SExtOpcode = X86::CWD;
903 break;
904 case MVT::i32:
905 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +0000906 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000907 SExtOpcode = X86::CDQ;
908 break;
909 }
910
911 SDOperand N0 = Node->getOperand(0);
912 SDOperand N1 = Node->getOperand(1);
913
914 bool foldedLoad = false;
915 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000916 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +0000917 SDOperand Chain;
918 if (foldedLoad)
919 Select(Chain, N1.getOperand(0));
920 else
921 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +0000922
Evan Cheng34167212006-02-09 00:37:58 +0000923 SDOperand InFlag(0, 0);
924 Select(N0, N0);
Evan Cheng948f3432006-01-06 23:19:29 +0000925 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000926 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000927 InFlag = Chain.getValue(1);
928
929 if (isSigned) {
930 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000931 InFlag =
932 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000933 } else {
934 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +0000935 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000936 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
937 ClrNode, InFlag);
938 InFlag = Chain.getValue(1);
939 }
940
941 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000942 Select(Tmp0, Tmp0);
943 Select(Tmp1, Tmp1);
944 Select(Tmp2, Tmp2);
945 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000946 SDNode *CNode =
947 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
948 Tmp2, Tmp3, Chain, InFlag);
949 Chain = SDOperand(CNode, 0);
950 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +0000951 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000952 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000953 InFlag =
954 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000955 }
956
Evan Cheng34167212006-02-09 00:37:58 +0000957 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
958 NVT, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000959 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000960 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000961 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000962 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000963 }
Evan Chengf597dc72006-02-10 22:24:32 +0000964
965#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000966 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000967 DEBUG(std::cerr << "== ");
968 DEBUG(Result.Val->dump(CurDAG));
969 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000970 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000971#endif
Evan Cheng34167212006-02-09 00:37:58 +0000972 return;
Evan Cheng0114e942006-01-06 20:36:21 +0000973 }
Evan Cheng403be7e2006-05-08 08:01:26 +0000974
975 case ISD::TRUNCATE: {
976 if (NVT == MVT::i8) {
977 unsigned Opc2;
978 MVT::ValueType VT;
979 switch (Node->getOperand(0).getValueType()) {
980 default: assert(0 && "Unknown truncate!");
981 case MVT::i16:
982 Opc = X86::MOV16to16_;
983 VT = MVT::i16;
Evan Cheng069287d2006-05-16 07:21:53 +0000984 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000985 break;
986 case MVT::i32:
987 Opc = X86::MOV32to32_;
988 VT = MVT::i32;
Evan Cheng069287d2006-05-16 07:21:53 +0000989 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000990 break;
991 }
992
993 SDOperand Tmp0, Tmp1;
994 Select(Tmp0, Node->getOperand(0));
995 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
996 Result = CodeGenMap[N] =
997 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
998
999#ifndef NDEBUG
1000 DEBUG(std::cerr << std::string(Indent-2, ' '));
1001 DEBUG(std::cerr << "== ");
1002 DEBUG(Result.Val->dump(CurDAG));
1003 DEBUG(std::cerr << "\n");
1004 Indent -= 2;
1005#endif
1006 return;
1007 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001008
1009 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001010 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001011 }
1012
Evan Cheng34167212006-02-09 00:37:58 +00001013 SelectCode(Result, N);
Evan Chengf597dc72006-02-10 22:24:32 +00001014#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001015 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +00001016 DEBUG(std::cerr << "=> ");
1017 DEBUG(Result.Val->dump(CurDAG));
1018 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001019 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001020#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +00001021}
1022
Chris Lattnerc0bad572006-06-08 18:03:49 +00001023bool X86DAGToDAGISel::
1024SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1025 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1026 SDOperand Op0, Op1, Op2, Op3;
1027 switch (ConstraintCode) {
1028 case 'o': // offsetable ??
1029 case 'v': // not offsetable ??
1030 default: return true;
1031 case 'm': // memory
1032 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1033 return true;
1034 break;
1035 }
1036
1037 OutOps.resize(4);
1038 Select(OutOps[0], Op0);
1039 Select(OutOps[1], Op1);
1040 Select(OutOps[2], Op2);
1041 Select(OutOps[3], Op3);
1042 return false;
1043}
1044
Chris Lattnerc961eea2005-11-16 01:54:32 +00001045/// createX86ISelDag - This pass converts a legalized DAG into a
1046/// X86-specific DAG, ready for instruction scheduling.
1047///
Evan Chengc4c62572006-03-13 23:20:37 +00001048FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001049 return new X86DAGToDAGISel(TM);
1050}