blob: 7ad797dfdbdf9a5ab749c4df3678d5aa919086c8 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group 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 pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
46
47 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000048 addRegisterClass(MVT::i8, X86::R8RegisterClass);
49 addRegisterClass(MVT::i16, X86::R16RegisterClass);
50 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
52
53 // FIXME: Eliminate these two classes when legalize can handle promotions
54 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000055/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000056
57 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
58 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
59 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
61 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
63 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
64 setOperationAction(ISD::SREM , MVT::f64 , Expand);
65
66 // These should be promoted to a larger select which is supported.
67/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
68 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000069
70 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000071
72 addLegalFPImmediate(+0.0); // FLD0
73 addLegalFPImmediate(+1.0); // FLD1
74 addLegalFPImmediate(-0.0); // FLD0/FCHS
75 addLegalFPImmediate(-1.0); // FLD1/FCHS
76 }
77
78 /// LowerArguments - This hook must be implemented to indicate how we should
79 /// lower the arguments for the specified function, into the specified DAG.
80 virtual std::vector<SDOperand>
81 LowerArguments(Function &F, SelectionDAG &DAG);
82
83 /// LowerCallTo - This hook lowers an abstract call to a function into an
84 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000085 virtual std::pair<SDOperand, SDOperand>
86 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
87 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000088
89 virtual std::pair<SDOperand, SDOperand>
90 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
91
92 virtual std::pair<SDOperand,SDOperand>
93 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
94 const Type *ArgTy, SelectionDAG &DAG);
95
96 virtual std::pair<SDOperand, SDOperand>
97 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
98 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000099 };
100}
101
102
103std::vector<SDOperand>
104X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
105 std::vector<SDOperand> ArgValues;
106
107 // Add DAG nodes to load the arguments... On entry to a function on the X86,
108 // the stack frame looks like this:
109 //
110 // [ESP] -- return address
111 // [ESP + 4] -- first argument (leftmost lexically)
112 // [ESP + 8] -- second argument, if first argument is four bytes in size
113 // ...
114 //
115 MachineFunction &MF = DAG.getMachineFunction();
116 MachineFrameInfo *MFI = MF.getFrameInfo();
117
118 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
119 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
120 MVT::ValueType ObjectVT = getValueType(I->getType());
121 unsigned ArgIncrement = 4;
122 unsigned ObjSize;
123 switch (ObjectVT) {
124 default: assert(0 && "Unhandled argument type!");
125 case MVT::i1:
126 case MVT::i8: ObjSize = 1; break;
127 case MVT::i16: ObjSize = 2; break;
128 case MVT::i32: ObjSize = 4; break;
129 case MVT::i64: ObjSize = ArgIncrement = 8; break;
130 case MVT::f32: ObjSize = 4; break;
131 case MVT::f64: ObjSize = ArgIncrement = 8; break;
132 }
133 // Create the frame index object for this incoming parameter...
134 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
135
136 // Create the SelectionDAG nodes corresponding to a load from this parameter
137 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
138
139 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
140 // dead loads.
141 SDOperand ArgValue;
142 if (!I->use_empty())
143 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
144 else {
145 if (MVT::isInteger(ObjectVT))
146 ArgValue = DAG.getConstant(0, ObjectVT);
147 else
148 ArgValue = DAG.getConstantFP(0, ObjectVT);
149 }
150 ArgValues.push_back(ArgValue);
151
152 ArgOffset += ArgIncrement; // Move on to the next argument...
153 }
154
155 // If the function takes variable number of arguments, make a frame index for
156 // the start of the first vararg value... for expansion of llvm.va_start.
157 if (F.isVarArg())
158 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000159 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000160 return ArgValues;
161}
162
Chris Lattner5188ad72005-01-08 19:28:19 +0000163std::pair<SDOperand, SDOperand>
164X86TargetLowering::LowerCallTo(SDOperand Chain,
165 const Type *RetTy, SDOperand Callee,
166 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000167 // Count how many bytes are to be pushed on the stack.
168 unsigned NumBytes = 0;
169
170 if (Args.empty()) {
171 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000172 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
173 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000174 } else {
175 for (unsigned i = 0, e = Args.size(); i != e; ++i)
176 switch (getValueType(Args[i].second)) {
177 default: assert(0 && "Unknown value type!");
178 case MVT::i1:
179 case MVT::i8:
180 case MVT::i16:
181 case MVT::i32:
182 case MVT::f32:
183 NumBytes += 4;
184 break;
185 case MVT::i64:
186 case MVT::f64:
187 NumBytes += 8;
188 break;
189 }
190
Chris Lattner5188ad72005-01-08 19:28:19 +0000191 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
192 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000193
194 // Arguments go on the stack in reverse order, as specified by the ABI.
195 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000196 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
197 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000198 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
199 unsigned ArgReg;
200 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
201 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
202
203 switch (getValueType(Args[i].second)) {
204 default: assert(0 && "Unexpected ValueType for argument!");
205 case MVT::i1:
206 case MVT::i8:
207 case MVT::i16:
208 // Promote the integer to 32 bits. If the input type is signed use a
209 // sign extend, otherwise use a zero extend.
210 if (Args[i].second->isSigned())
211 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
212 else
213 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
214
215 // FALL THROUGH
216 case MVT::i32:
217 case MVT::f32:
218 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000219 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
220 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000221 ArgOffset += 4;
222 break;
223 case MVT::i64:
224 case MVT::f64:
225 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
227 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000228 ArgOffset += 8;
229 break;
230 }
231 }
232 }
233
234 std::vector<MVT::ValueType> RetVals;
235 MVT::ValueType RetTyVT = getValueType(RetTy);
236 if (RetTyVT != MVT::isVoid)
237 RetVals.push_back(RetTyVT);
238 RetVals.push_back(MVT::Other);
239
Chris Lattner5188ad72005-01-08 19:28:19 +0000240 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000241 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000242 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
243 DAG.getConstant(NumBytes, getPointerTy()));
244 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000245}
246
Chris Lattner14824582005-01-09 00:01:27 +0000247std::pair<SDOperand, SDOperand>
248X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
249 // vastart just returns the address of the VarArgsFrameIndex slot.
250 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
251}
252
253std::pair<SDOperand,SDOperand> X86TargetLowering::
254LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
255 const Type *ArgTy, SelectionDAG &DAG) {
256 MVT::ValueType ArgVT = getValueType(ArgTy);
257 SDOperand Result;
258 if (!isVANext) {
259 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
260 } else {
261 unsigned Amt;
262 if (ArgVT == MVT::i32)
263 Amt = 4;
264 else {
265 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
266 "Other types should have been promoted for varargs!");
267 Amt = 8;
268 }
269 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
270 DAG.getConstant(Amt, VAList.getValueType()));
271 }
272 return std::make_pair(Result, Chain);
273}
274
275
276std::pair<SDOperand, SDOperand> X86TargetLowering::
277LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
278 SelectionDAG &DAG) {
279 SDOperand Result;
280 if (Depth) // Depths > 0 not supported yet!
281 Result = DAG.getConstant(0, getPointerTy());
282 else {
283 if (ReturnAddrIndex == 0) {
284 // Set up a frame object for the return address.
285 MachineFunction &MF = DAG.getMachineFunction();
286 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
287 }
288
289 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
290
291 if (!isFrameAddress)
292 // Just load the return address
293 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
294 else
295 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
296 DAG.getConstant(4, MVT::i32));
297 }
298 return std::make_pair(Result, Chain);
299}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000300
301
Chris Lattner98a8ba02005-01-18 01:06:26 +0000302namespace {
303 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
304 /// SDOperand's instead of register numbers for the leaves of the matched
305 /// tree.
306 struct X86ISelAddressMode {
307 enum {
308 RegBase,
309 FrameIndexBase,
310 } BaseType;
311
312 struct { // This is really a union, discriminated by BaseType!
313 SDOperand Reg;
314 int FrameIndex;
315 } Base;
316
317 unsigned Scale;
318 SDOperand IndexReg;
319 unsigned Disp;
320 GlobalValue *GV;
321
322 X86ISelAddressMode()
323 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
324 }
325 };
326}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000327
328
329namespace {
330 Statistic<>
331 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
332
333 //===--------------------------------------------------------------------===//
334 /// ISel - X86 specific code to select X86 machine instructions for
335 /// SelectionDAG operations.
336 ///
337 class ISel : public SelectionDAGISel {
338 /// ContainsFPCode - Every instruction we select that uses or defines a FP
339 /// register should set this to true.
340 bool ContainsFPCode;
341
342 /// X86Lowering - This object fully describes how to lower LLVM code to an
343 /// X86-specific SelectionDAG.
344 X86TargetLowering X86Lowering;
345
Chris Lattner11333092005-01-11 03:11:44 +0000346 /// RegPressureMap - This keeps an approximate count of the number of
347 /// registers required to evaluate each node in the graph.
348 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000349
350 /// ExprMap - As shared expressions are codegen'd, we keep track of which
351 /// vreg the value is produced in, so we only emit one copy of each compiled
352 /// tree.
353 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000354
355 public:
356 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
357 }
358
Chris Lattner11333092005-01-11 03:11:44 +0000359 unsigned getRegPressure(SDOperand O) {
360 return RegPressureMap[O.Val];
361 }
362 unsigned ComputeRegPressure(SDOperand O);
363
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364 /// InstructionSelectBasicBlock - This callback is invoked by
365 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000366 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000367
Chris Lattner4ff348b2005-01-17 06:26:58 +0000368 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000369 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000370 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000371
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000372 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000373 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000374 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
375 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000376 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000377
378 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
379 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
380 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000381 void Select(SDOperand N);
382 };
383}
384
Chris Lattner7dbcb752005-01-12 04:21:28 +0000385/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
386/// when it has created a SelectionDAG for us to codegen.
387void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
388 // While we're doing this, keep track of whether we see any FP code for
389 // FP_REG_KILL insertion.
390 ContainsFPCode = false;
391
392 // Scan the PHI nodes that already are inserted into this basic block. If any
393 // of them is a PHI of a floating point value, we need to insert an
394 // FP_REG_KILL.
395 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
396 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
397 I != E; ++I) {
398 assert(I->getOpcode() == X86::PHI &&
399 "Isn't just PHI nodes?");
400 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
401 X86::RFPRegisterClass) {
402 ContainsFPCode = true;
403 break;
404 }
405 }
406
407 // Compute the RegPressureMap, which is an approximation for the number of
408 // registers required to compute each node.
409 ComputeRegPressure(DAG.getRoot());
410
411 // Codegen the basic block.
412 Select(DAG.getRoot());
413
414 // Finally, look at all of the successors of this block. If any contain a PHI
415 // node of FP type, we need to insert an FP_REG_KILL in this block.
416 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
417 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
418 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
419 I != E && I->getOpcode() == X86::PHI; ++I) {
420 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
421 X86::RFPRegisterClass) {
422 ContainsFPCode = true;
423 break;
424 }
425 }
426
427 // Insert FP_REG_KILL instructions into basic blocks that need them. This
428 // only occurs due to the floating point stackifier not being aggressive
429 // enough to handle arbitrary global stackification.
430 //
431 // Currently we insert an FP_REG_KILL instruction into each block that uses or
432 // defines a floating point virtual register.
433 //
434 // When the global register allocators (like linear scan) finally update live
435 // variable analysis, we can keep floating point values in registers across
436 // basic blocks. This will be a huge win, but we are waiting on the global
437 // allocators before we can do this.
438 //
439 if (ContainsFPCode && BB->succ_size()) {
440 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
441 ++NumFPKill;
442 }
443
444 // Clear state used for selection.
445 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000446 RegPressureMap.clear();
447}
448
449
Chris Lattner11333092005-01-11 03:11:44 +0000450// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
451// for the number of registers required to compute each node. This is basically
452// computing a generalized form of the Sethi-Ullman number for each node.
453unsigned ISel::ComputeRegPressure(SDOperand O) {
454 SDNode *N = O.Val;
455 unsigned &Result = RegPressureMap[N];
456 if (Result) return Result;
457
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000458 // FIXME: Should operations like CALL (which clobber lots o regs) have a
459 // higher fixed cost??
460
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000461 if (N->getNumOperands() == 0) {
462 Result = 1;
463 } else {
464 unsigned MaxRegUse = 0;
465 unsigned NumExtraMaxRegUsers = 0;
466 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
467 unsigned Regs;
468 if (N->getOperand(i).getOpcode() == ISD::Constant)
469 Regs = 0;
470 else
471 Regs = ComputeRegPressure(N->getOperand(i));
472 if (Regs > MaxRegUse) {
473 MaxRegUse = Regs;
474 NumExtraMaxRegUsers = 0;
475 } else if (Regs == MaxRegUse &&
476 N->getOperand(i).getValueType() != MVT::Other) {
477 ++NumExtraMaxRegUsers;
478 }
Chris Lattner11333092005-01-11 03:11:44 +0000479 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000480
481 if (O.getOpcode() != ISD::TokenFactor)
482 Result = MaxRegUse+NumExtraMaxRegUsers;
483 else
Chris Lattner869e0432005-01-17 23:02:13 +0000484 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000485 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000486
Chris Lattner837caa72005-01-11 23:21:30 +0000487 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000488 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000489}
490
Chris Lattner98a8ba02005-01-18 01:06:26 +0000491X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
492 X86AddressMode Result;
493
494 // If we need to emit two register operands, emit the one with the highest
495 // register pressure first.
496 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
497 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
498 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
499 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
500 Result.IndexReg = SelectExpr(IAM.IndexReg);
501 } else {
502 Result.IndexReg = SelectExpr(IAM.IndexReg);
503 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
504 }
505 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
506 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
507 } else if (IAM.IndexReg.Val) {
508 Result.IndexReg = SelectExpr(IAM.IndexReg);
509 }
510
511 switch (IAM.BaseType) {
512 case X86ISelAddressMode::RegBase:
513 Result.BaseType = X86AddressMode::RegBase;
514 break;
515 case X86ISelAddressMode::FrameIndexBase:
516 Result.BaseType = X86AddressMode::FrameIndexBase;
517 Result.Base.FrameIndex = IAM.Base.FrameIndex;
518 break;
519 default:
520 assert(0 && "Unknown base type!");
521 break;
522 }
523 Result.Scale = IAM.Scale;
524 Result.Disp = IAM.Disp;
525 Result.GV = IAM.GV;
526 return Result;
527}
528
529/// SelectAddress - Pattern match the maximal addressing mode for this node and
530/// emit all of the leaf registers.
531void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
532 X86ISelAddressMode IAM;
533 MatchAddress(N, IAM);
534 AM = SelectAddrExprs(IAM);
535}
536
537/// MatchAddress - Add the specified node to the specified addressing mode,
538/// returning true if it cannot be done. This just pattern matches for the
539/// addressing mode, it does not cause any code to be emitted. For that, use
540/// SelectAddress.
541bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000542 switch (N.getOpcode()) {
543 default: break;
544 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000545 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
546 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000547 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
548 return false;
549 }
550 break;
551 case ISD::GlobalAddress:
552 if (AM.GV == 0) {
553 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
554 return false;
555 }
556 break;
557 case ISD::Constant:
558 AM.Disp += cast<ConstantSDNode>(N)->getValue();
559 return false;
560 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000561 // We might have folded the load into this shift, so don't regen the value
562 // if so.
563 if (ExprMap.count(N)) break;
564
Chris Lattner98a8ba02005-01-18 01:06:26 +0000565 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000566 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
567 unsigned Val = CN->getValue();
568 if (Val == 1 || Val == 2 || Val == 3) {
569 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000570 SDOperand ShVal = N.Val->getOperand(0);
571
572 // Okay, we know that we have a scale by now. However, if the scaled
573 // value is an add of something and a constant, we can fold the
574 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000575 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000576 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000577 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000578 ConstantSDNode *AddVal =
579 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
580 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000581 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000582 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000583 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000584 return false;
585 }
586 }
587 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000588 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000589 // We might have folded the load into this mul, so don't regen the value if
590 // so.
591 if (ExprMap.count(N)) break;
592
Chris Lattner947d5442005-01-11 19:37:02 +0000593 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000594 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
595 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000596 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
597 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
598 AM.Scale = unsigned(CN->getValue())-1;
599
600 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000601 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000602
603 // Okay, we know that we have a scale by now. However, if the scaled
604 // value is an add of something and a constant, we can fold the
605 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000606 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000607 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000608 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000609 ConstantSDNode *AddVal =
610 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
611 AM.Disp += AddVal->getValue() * CN->getValue();
612 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000613 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000614 }
615
616 AM.IndexReg = AM.Base.Reg = Reg;
617 return false;
618 }
619 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000620
621 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000622 // We might have folded the load into this mul, so don't regen the value if
623 // so.
624 if (ExprMap.count(N)) break;
625
Chris Lattner98a8ba02005-01-18 01:06:26 +0000626 X86ISelAddressMode Backup = AM;
627 if (!MatchAddress(N.Val->getOperand(0), AM) &&
628 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000629 return false;
630 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000631 if (!MatchAddress(N.Val->getOperand(1), AM) &&
632 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000633 return false;
634 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000635 break;
636 }
637 }
638
Chris Lattnera95589b2005-01-11 04:40:19 +0000639 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000640 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000641 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000642 if (AM.IndexReg.Val == 0) {
643 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000644 AM.Scale = 1;
645 return false;
646 }
647
648 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000649 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000650 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000651
652 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000653 AM.BaseType = X86ISelAddressMode::RegBase;
654 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000655 return false;
656}
657
658/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
659/// assuming that the temporary registers are in the 8-bit register class.
660///
661/// Tmp1 = setcc1
662/// Tmp2 = setcc2
663/// DestReg = logicalop Tmp1, Tmp2
664///
665static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
666 unsigned SetCC2, unsigned LogicalOp,
667 unsigned DestReg) {
668 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
669 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
670 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
671 BuildMI(BB, SetCC1, 0, Tmp1);
672 BuildMI(BB, SetCC2, 0, Tmp2);
673 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
674}
675
676/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
677/// condition codes match the specified SetCCOpcode. Note that some conditions
678/// require multiple instructions to generate the correct value.
679static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
680 ISD::CondCode SetCCOpcode, bool isFP) {
681 unsigned Opc;
682 if (!isFP) {
683 switch (SetCCOpcode) {
684 default: assert(0 && "Illegal integer SetCC!");
685 case ISD::SETEQ: Opc = X86::SETEr; break;
686 case ISD::SETGT: Opc = X86::SETGr; break;
687 case ISD::SETGE: Opc = X86::SETGEr; break;
688 case ISD::SETLT: Opc = X86::SETLr; break;
689 case ISD::SETLE: Opc = X86::SETLEr; break;
690 case ISD::SETNE: Opc = X86::SETNEr; break;
691 case ISD::SETULT: Opc = X86::SETBr; break;
692 case ISD::SETUGT: Opc = X86::SETAr; break;
693 case ISD::SETULE: Opc = X86::SETBEr; break;
694 case ISD::SETUGE: Opc = X86::SETAEr; break;
695 }
696 } else {
697 // On a floating point condition, the flags are set as follows:
698 // ZF PF CF op
699 // 0 | 0 | 0 | X > Y
700 // 0 | 0 | 1 | X < Y
701 // 1 | 0 | 0 | X == Y
702 // 1 | 1 | 1 | unordered
703 //
704 switch (SetCCOpcode) {
705 default: assert(0 && "Invalid FP setcc!");
706 case ISD::SETUEQ:
707 case ISD::SETEQ:
708 Opc = X86::SETEr; // True if ZF = 1
709 break;
710 case ISD::SETOGT:
711 case ISD::SETGT:
712 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
713 break;
714 case ISD::SETOGE:
715 case ISD::SETGE:
716 Opc = X86::SETAEr; // True if CF = 0
717 break;
718 case ISD::SETULT:
719 case ISD::SETLT:
720 Opc = X86::SETBr; // True if CF = 1
721 break;
722 case ISD::SETULE:
723 case ISD::SETLE:
724 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
725 break;
726 case ISD::SETONE:
727 case ISD::SETNE:
728 Opc = X86::SETNEr; // True if ZF = 0
729 break;
730 case ISD::SETUO:
731 Opc = X86::SETPr; // True if PF = 1
732 break;
733 case ISD::SETO:
734 Opc = X86::SETNPr; // True if PF = 0
735 break;
736 case ISD::SETOEQ: // !PF & ZF
737 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
738 return;
739 case ISD::SETOLT: // !PF & CF
740 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
741 return;
742 case ISD::SETOLE: // !PF & (CF || ZF)
743 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
744 return;
745 case ISD::SETUGT: // PF | (!ZF & !CF)
746 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
747 return;
748 case ISD::SETUGE: // PF | !CF
749 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
750 return;
751 case ISD::SETUNE: // PF | !ZF
752 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
753 return;
754 }
755 }
756 BuildMI(BB, Opc, 0, DestReg);
757}
758
759
760/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
761/// the Dest block if the Cond condition is true. If we cannot fold this
762/// condition into the branch, return true.
763///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000764bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
765 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000766 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
767 // B) using two conditional branches instead of one condbr, two setcc's, and
768 // an or.
769 if ((Cond.getOpcode() == ISD::OR ||
770 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
771 // And and or set the flags for us, so there is no need to emit a TST of the
772 // result. It is only safe to do this if there is only a single use of the
773 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000774 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000775 SelectExpr(Cond);
776 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
777 return false;
778 }
779
780 // Codegen br not C -> JE.
781 if (Cond.getOpcode() == ISD::XOR)
782 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
783 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000784 unsigned CondR;
785 if (getRegPressure(Chain) > getRegPressure(Cond)) {
786 Select(Chain);
787 CondR = SelectExpr(Cond.Val->getOperand(0));
788 } else {
789 CondR = SelectExpr(Cond.Val->getOperand(0));
790 Select(Chain);
791 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000792 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
793 BuildMI(BB, X86::JE, 1).addMBB(Dest);
794 return false;
795 }
796
797 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
798 if (SetCC == 0)
799 return true; // Can only handle simple setcc's so far.
800
801 unsigned Opc;
802
803 // Handle integer conditions first.
804 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
805 switch (SetCC->getCondition()) {
806 default: assert(0 && "Illegal integer SetCC!");
807 case ISD::SETEQ: Opc = X86::JE; break;
808 case ISD::SETGT: Opc = X86::JG; break;
809 case ISD::SETGE: Opc = X86::JGE; break;
810 case ISD::SETLT: Opc = X86::JL; break;
811 case ISD::SETLE: Opc = X86::JLE; break;
812 case ISD::SETNE: Opc = X86::JNE; break;
813 case ISD::SETULT: Opc = X86::JB; break;
814 case ISD::SETUGT: Opc = X86::JA; break;
815 case ISD::SETULE: Opc = X86::JBE; break;
816 case ISD::SETUGE: Opc = X86::JAE; break;
817 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000818 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000819 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000820 BuildMI(BB, Opc, 1).addMBB(Dest);
821 return false;
822 }
823
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000824 unsigned Opc2 = 0; // Second branch if needed.
825
826 // On a floating point condition, the flags are set as follows:
827 // ZF PF CF op
828 // 0 | 0 | 0 | X > Y
829 // 0 | 0 | 1 | X < Y
830 // 1 | 0 | 0 | X == Y
831 // 1 | 1 | 1 | unordered
832 //
833 switch (SetCC->getCondition()) {
834 default: assert(0 && "Invalid FP setcc!");
835 case ISD::SETUEQ:
836 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
837 case ISD::SETOGT:
838 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
839 case ISD::SETOGE:
840 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
841 case ISD::SETULT:
842 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
843 case ISD::SETULE:
844 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
845 case ISD::SETONE:
846 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
847 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
848 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
849 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
850 Opc = X86::JA; // ZF = 0 & CF = 0
851 Opc2 = X86::JP; // PF = 1
852 break;
853 case ISD::SETUGE: // PF = 1 | CF = 0
854 Opc = X86::JAE; // CF = 0
855 Opc2 = X86::JP; // PF = 1
856 break;
857 case ISD::SETUNE: // PF = 1 | ZF = 0
858 Opc = X86::JNE; // ZF = 0
859 Opc2 = X86::JP; // PF = 1
860 break;
861 case ISD::SETOEQ: // PF = 0 & ZF = 1
862 //X86::JNP, X86::JE
863 //X86::AND8rr
864 return true; // FIXME: Emit more efficient code for this branch.
865 case ISD::SETOLT: // PF = 0 & CF = 1
866 //X86::JNP, X86::JB
867 //X86::AND8rr
868 return true; // FIXME: Emit more efficient code for this branch.
869 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
870 //X86::JNP, X86::JBE
871 //X86::AND8rr
872 return true; // FIXME: Emit more efficient code for this branch.
873 }
874
Chris Lattner6c07aee2005-01-11 04:06:27 +0000875 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000876 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000877 BuildMI(BB, Opc, 1).addMBB(Dest);
878 if (Opc2)
879 BuildMI(BB, Opc2, 1).addMBB(Dest);
880 return false;
881}
882
Chris Lattner24aad1b2005-01-10 22:10:13 +0000883/// EmitSelectCC - Emit code into BB that performs a select operation between
884/// the two registers RTrue and RFalse, generating a result into RDest. Return
885/// true if the fold cannot be performed.
886///
887void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
888 unsigned RTrue, unsigned RFalse, unsigned RDest) {
889 enum Condition {
890 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
891 NOT_SET
892 } CondCode = NOT_SET;
893
894 static const unsigned CMOVTAB16[] = {
895 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
896 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
897 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
898 };
899 static const unsigned CMOVTAB32[] = {
900 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
901 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
902 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
903 };
904 static const unsigned CMOVTABFP[] = {
905 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
906 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
907 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
908 };
909
910 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
911 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
912 switch (SetCC->getCondition()) {
913 default: assert(0 && "Unknown integer comparison!");
914 case ISD::SETEQ: CondCode = EQ; break;
915 case ISD::SETGT: CondCode = GT; break;
916 case ISD::SETGE: CondCode = GE; break;
917 case ISD::SETLT: CondCode = LT; break;
918 case ISD::SETLE: CondCode = LE; break;
919 case ISD::SETNE: CondCode = NE; break;
920 case ISD::SETULT: CondCode = B; break;
921 case ISD::SETUGT: CondCode = A; break;
922 case ISD::SETULE: CondCode = BE; break;
923 case ISD::SETUGE: CondCode = AE; break;
924 }
925 } else {
926 // On a floating point condition, the flags are set as follows:
927 // ZF PF CF op
928 // 0 | 0 | 0 | X > Y
929 // 0 | 0 | 1 | X < Y
930 // 1 | 0 | 0 | X == Y
931 // 1 | 1 | 1 | unordered
932 //
933 switch (SetCC->getCondition()) {
934 default: assert(0 && "Unknown FP comparison!");
935 case ISD::SETUEQ:
936 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
937 case ISD::SETOGT:
938 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
939 case ISD::SETOGE:
940 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
941 case ISD::SETULT:
942 case ISD::SETLT: CondCode = B; break; // True if CF = 1
943 case ISD::SETULE:
944 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
945 case ISD::SETONE:
946 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
947 case ISD::SETUO: CondCode = P; break; // True if PF = 1
948 case ISD::SETO: CondCode = NP; break; // True if PF = 0
949 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
950 case ISD::SETUGE: // PF = 1 | CF = 0
951 case ISD::SETUNE: // PF = 1 | ZF = 0
952 case ISD::SETOEQ: // PF = 0 & ZF = 1
953 case ISD::SETOLT: // PF = 0 & CF = 1
954 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
955 // We cannot emit this comparison as a single cmov.
956 break;
957 }
958 }
959 }
960
961 unsigned Opc = 0;
962 if (CondCode != NOT_SET) {
963 switch (SVT) {
964 default: assert(0 && "Cannot select this type!");
965 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
966 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +0000967 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000968 }
969 }
970
971 // Finally, if we weren't able to fold this, just emit the condition and test
972 // it.
973 if (CondCode == NOT_SET || Opc == 0) {
974 // Get the condition into the zero flag.
975 unsigned CondReg = SelectExpr(Cond);
976 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
977
978 switch (SVT) {
979 default: assert(0 && "Cannot select this type!");
980 case MVT::i16: Opc = X86::CMOVE16rr; break;
981 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +0000982 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000983 }
984 } else {
985 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000986 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000987 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000988 }
989 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
990}
991
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000992void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000993 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000994 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
995 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000996 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000997 switch (RHS.getValueType()) {
998 default: break;
999 case MVT::i1:
1000 case MVT::i8: Opc = X86::CMP8mi; break;
1001 case MVT::i16: Opc = X86::CMP16mi; break;
1002 case MVT::i32: Opc = X86::CMP32mi; break;
1003 }
1004 if (Opc) {
1005 X86AddressMode AM;
1006 EmitFoldedLoad(LHS, AM);
1007 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1008 return;
1009 }
1010 }
1011
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001012 switch (RHS.getValueType()) {
1013 default: break;
1014 case MVT::i1:
1015 case MVT::i8: Opc = X86::CMP8ri; break;
1016 case MVT::i16: Opc = X86::CMP16ri; break;
1017 case MVT::i32: Opc = X86::CMP32ri; break;
1018 }
1019 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001020 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001021 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1022 return;
1023 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001024 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1025 if (CN->isExactlyValue(+0.0) ||
1026 CN->isExactlyValue(-0.0)) {
1027 unsigned Reg = SelectExpr(LHS);
1028 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1029 BuildMI(BB, X86::FNSTSW8r, 0);
1030 BuildMI(BB, X86::SAHF, 1);
1031 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001032 }
1033
Chris Lattneref6806c2005-01-12 02:02:48 +00001034 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001035 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001036 switch (RHS.getValueType()) {
1037 default: break;
1038 case MVT::i1:
1039 case MVT::i8: Opc = X86::CMP8mr; break;
1040 case MVT::i16: Opc = X86::CMP16mr; break;
1041 case MVT::i32: Opc = X86::CMP32mr; break;
1042 }
1043 if (Opc) {
1044 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001045 EmitFoldedLoad(LHS, AM);
1046 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001047 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1048 return;
1049 }
1050 }
1051
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001052 switch (LHS.getValueType()) {
1053 default: assert(0 && "Cannot compare this value!");
1054 case MVT::i1:
1055 case MVT::i8: Opc = X86::CMP8rr; break;
1056 case MVT::i16: Opc = X86::CMP16rr; break;
1057 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001058 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001059 }
Chris Lattner11333092005-01-11 03:11:44 +00001060 unsigned Tmp1, Tmp2;
1061 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1062 Tmp1 = SelectExpr(LHS);
1063 Tmp2 = SelectExpr(RHS);
1064 } else {
1065 Tmp2 = SelectExpr(RHS);
1066 Tmp1 = SelectExpr(LHS);
1067 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001068 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1069}
1070
Chris Lattner4ff348b2005-01-17 06:26:58 +00001071/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1072/// The DAG cannot have cycles in it, by definition, so the visited set is not
1073/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1074/// reuse, so it prevents exponential cases.
1075///
1076static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1077 std::set<SDNode*> &Visited) {
1078 if (N == Op) return true; // Found it.
1079 SDNode *Node = N.Val;
1080 if (Node->getNumOperands() == 0) return false; // Leaf?
1081 if (!Visited.insert(Node).second) return false; // Already visited?
1082
1083 // Recurse for the first N-1 operands.
1084 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1085 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1086 return true;
1087
1088 // Tail recurse for the last operand.
1089 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1090}
1091
Chris Lattnera5ade062005-01-11 21:19:59 +00001092/// isFoldableLoad - Return true if this is a load instruction that can safely
1093/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001094bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001095 if (Op.getOpcode() != ISD::LOAD ||
1096 // FIXME: currently can't fold constant pool indexes.
1097 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1098 return false;
1099
1100 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001101 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1102 if (ExprMap.count(Op.getValue(1))) return false;
1103 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001104 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001105
Chris Lattner4ff348b2005-01-17 06:26:58 +00001106 // If there is not just one use of its value, we cannot fold.
1107 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1108
1109 // Finally, we cannot fold the load into the operation if this would induce a
1110 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1111 // operand of the operation we are folding the load into) can possible use the
1112 // chain node defined by the load.
1113 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1114 std::set<SDNode*> Visited;
1115 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1116 return false;
1117 }
1118 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001119}
1120
Chris Lattner4ff348b2005-01-17 06:26:58 +00001121
Chris Lattnera5ade062005-01-11 21:19:59 +00001122/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1123/// and compute the address being loaded into AM.
1124void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1125 SDOperand Chain = Op.getOperand(0);
1126 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001127
Chris Lattnera5ade062005-01-11 21:19:59 +00001128 if (getRegPressure(Chain) > getRegPressure(Address)) {
1129 Select(Chain);
1130 SelectAddress(Address, AM);
1131 } else {
1132 SelectAddress(Address, AM);
1133 Select(Chain);
1134 }
1135
1136 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001137 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1138 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001139 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001140 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001141}
1142
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001143unsigned ISel::SelectExpr(SDOperand N) {
1144 unsigned Result;
1145 unsigned Tmp1, Tmp2, Tmp3;
1146 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001147 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001148 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001149
Chris Lattner7f2afac2005-01-14 22:37:41 +00001150 if (Node->getOpcode() == ISD::CopyFromReg) {
1151 // FIXME: Handle copy from physregs!
1152
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001153 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001154 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001155 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001156
1157 unsigned &Reg = ExprMap[N];
1158 if (Reg) return Reg;
1159
1160 if (N.getOpcode() != ISD::CALL)
1161 Reg = Result = (N.getValueType() != MVT::Other) ?
1162 MakeReg(N.getValueType()) : 1;
1163 else {
1164 // If this is a call instruction, make sure to prepare ALL of the result
1165 // values as well as the chain.
1166 if (Node->getNumValues() == 1)
1167 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001168 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001169 Result = MakeReg(Node->getValueType(0));
1170 ExprMap[N.getValue(0)] = Result;
1171 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1172 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1173 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001174 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001175 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001176
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001177 switch (N.getOpcode()) {
1178 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001179 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001180 assert(0 && "Node not handled!\n");
1181 case ISD::FrameIndex:
1182 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1183 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1184 return Result;
1185 case ISD::ConstantPool:
1186 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1187 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1188 return Result;
1189 case ISD::ConstantFP:
1190 ContainsFPCode = true;
1191 Tmp1 = Result; // Intermediate Register
1192 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1193 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1194 Tmp1 = MakeReg(MVT::f64);
1195
1196 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1197 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1198 BuildMI(BB, X86::FLD0, 0, Tmp1);
1199 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1200 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1201 BuildMI(BB, X86::FLD1, 0, Tmp1);
1202 else
1203 assert(0 && "Unexpected constant!");
1204 if (Tmp1 != Result)
1205 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1206 return Result;
1207 case ISD::Constant:
1208 switch (N.getValueType()) {
1209 default: assert(0 && "Cannot use constants of this type!");
1210 case MVT::i1:
1211 case MVT::i8: Opc = X86::MOV8ri; break;
1212 case MVT::i16: Opc = X86::MOV16ri; break;
1213 case MVT::i32: Opc = X86::MOV32ri; break;
1214 }
1215 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1216 return Result;
1217 case ISD::GlobalAddress: {
1218 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1219 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1220 return Result;
1221 }
1222 case ISD::ExternalSymbol: {
1223 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1224 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1225 return Result;
1226 }
1227 case ISD::FP_EXTEND:
1228 Tmp1 = SelectExpr(N.getOperand(0));
1229 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001230 return Result;
1231 case ISD::ZERO_EXTEND: {
1232 int DestIs16 = N.getValueType() == MVT::i16;
1233 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001234
1235 // FIXME: This hack is here for zero extension casts from bool to i8. This
1236 // would not be needed if bools were promoted by Legalize.
1237 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001238 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001239 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1240 return Result;
1241 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001242
Chris Lattner4ff348b2005-01-17 06:26:58 +00001243 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001244 static const unsigned Opc[3] = {
1245 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1246 };
1247
1248 X86AddressMode AM;
1249 EmitFoldedLoad(N.getOperand(0), AM);
1250 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1251
1252 return Result;
1253 }
1254
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001255 static const unsigned Opc[3] = {
1256 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1257 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001258 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001259 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1260 return Result;
1261 }
1262 case ISD::SIGN_EXTEND: {
1263 int DestIs16 = N.getValueType() == MVT::i16;
1264 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1265
Chris Lattner590d8002005-01-09 18:52:44 +00001266 // FIXME: Legalize should promote bools to i8!
1267 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1268 "Sign extend from bool not implemented!");
1269
Chris Lattner4ff348b2005-01-17 06:26:58 +00001270 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001271 static const unsigned Opc[3] = {
1272 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1273 };
1274
1275 X86AddressMode AM;
1276 EmitFoldedLoad(N.getOperand(0), AM);
1277 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1278 return Result;
1279 }
1280
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001281 static const unsigned Opc[3] = {
1282 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1283 };
1284 Tmp1 = SelectExpr(N.getOperand(0));
1285 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1286 return Result;
1287 }
1288 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001289 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001290 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001291 switch (N.getValueType()) {
1292 default: assert(0 && "Unknown truncate!");
1293 case MVT::i1:
1294 case MVT::i8: Opc = X86::MOV8rm; break;
1295 case MVT::i16: Opc = X86::MOV16rm; break;
1296 }
1297 X86AddressMode AM;
1298 EmitFoldedLoad(N.getOperand(0), AM);
1299 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1300 return Result;
1301 }
1302
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001303 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1304 // a move out of AX or AL.
1305 switch (N.getOperand(0).getValueType()) {
1306 default: assert(0 && "Unknown truncate!");
1307 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1308 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1309 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1310 }
1311 Tmp1 = SelectExpr(N.getOperand(0));
1312 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1313
1314 switch (N.getValueType()) {
1315 default: assert(0 && "Unknown truncate!");
1316 case MVT::i1:
1317 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1318 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1319 }
1320 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1321 return Result;
1322
Chris Lattner590d8002005-01-09 18:52:44 +00001323 case ISD::SINT_TO_FP:
1324 case ISD::UINT_TO_FP: {
1325 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001326 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001327
1328 // Promote the integer to a type supported by FLD. We do this because there
1329 // are no unsigned FLD instructions, so we must promote an unsigned value to
1330 // a larger signed value, then use FLD on the larger value.
1331 //
1332 MVT::ValueType PromoteType = MVT::Other;
1333 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1334 unsigned PromoteOpcode = 0;
1335 unsigned RealDestReg = Result;
1336 switch (SrcTy) {
1337 case MVT::i1:
1338 case MVT::i8:
1339 // We don't have the facilities for directly loading byte sized data from
1340 // memory (even signed). Promote it to 16 bits.
1341 PromoteType = MVT::i16;
1342 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1343 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1344 break;
1345 case MVT::i16:
1346 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1347 PromoteType = MVT::i32;
1348 PromoteOpcode = X86::MOVZX32rr16;
1349 }
1350 break;
1351 default:
1352 // Don't fild into the real destination.
1353 if (Node->getOpcode() == ISD::UINT_TO_FP)
1354 Result = MakeReg(Node->getValueType(0));
1355 break;
1356 }
1357
1358 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1359
1360 if (PromoteType != MVT::Other) {
1361 Tmp2 = MakeReg(PromoteType);
1362 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1363 SrcTy = PromoteType;
1364 Tmp1 = Tmp2;
1365 }
1366
1367 // Spill the integer to memory and reload it from there.
1368 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1369 MachineFunction *F = BB->getParent();
1370 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1371
1372 switch (SrcTy) {
1373 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001374 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001375 // FIXME: this won't work for cast [u]long to FP
1376 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1377 FrameIdx).addReg(Tmp1);
1378 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1379 FrameIdx, 4).addReg(Tmp1+1);
1380 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1381 break;
1382 case MVT::i32:
1383 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1384 FrameIdx).addReg(Tmp1);
1385 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1386 break;
1387 case MVT::i16:
1388 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1389 FrameIdx).addReg(Tmp1);
1390 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1391 break;
1392 default: break; // No promotion required.
1393 }
1394
Chris Lattner085c9952005-01-12 04:00:00 +00001395 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001396 // If this is a cast from uint -> double, we need to be careful when if
1397 // the "sign" bit is set. If so, we don't want to make a negative number,
1398 // we want to make a positive number. Emit code to add an offset if the
1399 // sign bit is set.
1400
1401 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1402 unsigned IsNeg = MakeReg(MVT::i32);
1403 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1404
1405 // Create a CP value that has the offset in one word and 0 in the other.
1406 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1407 0x4f80000000000000ULL);
1408 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1409 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1410 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1411
1412 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1413 // We need special handling for unsigned 64-bit integer sources. If the
1414 // input number has the "sign bit" set, then we loaded it incorrectly as a
1415 // negative 64-bit number. In this case, add an offset value.
1416
1417 // Emit a test instruction to see if the dynamic input value was signed.
1418 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1419
1420 // If the sign bit is set, get a pointer to an offset, otherwise get a
1421 // pointer to a zero.
1422 MachineConstantPool *CP = F->getConstantPool();
1423 unsigned Zero = MakeReg(MVT::i32);
1424 Constant *Null = Constant::getNullValue(Type::UIntTy);
1425 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1426 CP->getConstantPoolIndex(Null));
1427 unsigned Offset = MakeReg(MVT::i32);
1428 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1429
1430 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1431 CP->getConstantPoolIndex(OffsetCst));
1432 unsigned Addr = MakeReg(MVT::i32);
1433 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1434
1435 // Load the constant for an add. FIXME: this could make an 'fadd' that
1436 // reads directly from memory, but we don't support these yet.
1437 unsigned ConstReg = MakeReg(MVT::f64);
1438 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1439
1440 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1441 }
1442 return RealDestReg;
1443 }
1444 case ISD::FP_TO_SINT:
1445 case ISD::FP_TO_UINT: {
1446 // FIXME: Most of this grunt work should be done by legalize!
1447 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1448
1449 // Change the floating point control register to use "round towards zero"
1450 // mode when truncating to an integer value.
1451 //
1452 MachineFunction *F = BB->getParent();
1453 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1454 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1455
1456 // Load the old value of the high byte of the control word...
1457 unsigned HighPartOfCW = MakeReg(MVT::i8);
1458 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1459 CWFrameIdx, 1);
1460
1461 // Set the high part to be round to zero...
1462 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1463 CWFrameIdx, 1).addImm(12);
1464
1465 // Reload the modified control word now...
1466 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1467
1468 // Restore the memory image of control word to original value
1469 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1470 CWFrameIdx, 1).addReg(HighPartOfCW);
1471
1472 // We don't have the facilities for directly storing byte sized data to
1473 // memory. Promote it to 16 bits. We also must promote unsigned values to
1474 // larger classes because we only have signed FP stores.
1475 MVT::ValueType StoreClass = Node->getValueType(0);
1476 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1477 switch (StoreClass) {
1478 case MVT::i8: StoreClass = MVT::i16; break;
1479 case MVT::i16: StoreClass = MVT::i32; break;
1480 case MVT::i32: StoreClass = MVT::i64; break;
1481 // The following treatment of cLong may not be perfectly right,
1482 // but it survives chains of casts of the form
1483 // double->ulong->double.
1484 case MVT::i64: StoreClass = MVT::i64; break;
1485 default: assert(0 && "Unknown store class!");
1486 }
1487
1488 // Spill the integer to memory and reload it from there.
1489 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1490 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1491
1492 switch (StoreClass) {
1493 default: assert(0 && "Unknown store class!");
1494 case MVT::i16:
1495 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1496 break;
1497 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001498 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001499 break;
1500 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001501 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001502 break;
1503 }
1504
1505 switch (Node->getValueType(0)) {
1506 default:
1507 assert(0 && "Unknown integer type!");
1508 case MVT::i64:
1509 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001510 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001511 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1512 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1513 case MVT::i32:
1514 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1515 break;
1516 case MVT::i16:
1517 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1518 break;
1519 case MVT::i8:
1520 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1521 break;
1522 }
1523
1524 // Reload the original control word now.
1525 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1526 return Result;
1527 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001528 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001529 Op0 = N.getOperand(0);
1530 Op1 = N.getOperand(1);
1531
Chris Lattner4ff348b2005-01-17 06:26:58 +00001532 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001533 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001534 goto FoldAdd;
1535 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001536
Chris Lattner4ff348b2005-01-17 06:26:58 +00001537 if (isFoldableLoad(Op1, Op0)) {
1538 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001539 switch (N.getValueType()) {
1540 default: assert(0 && "Cannot add this type!");
1541 case MVT::i1:
1542 case MVT::i8: Opc = X86::ADD8rm; break;
1543 case MVT::i16: Opc = X86::ADD16rm; break;
1544 case MVT::i32: Opc = X86::ADD32rm; break;
1545 case MVT::f32: Opc = X86::FADD32m; break;
1546 case MVT::f64: Opc = X86::FADD64m; break;
1547 }
1548 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001549 EmitFoldedLoad(Op1, AM);
1550 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001551 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1552 return Result;
1553 }
1554
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001555 // See if we can codegen this as an LEA to fold operations together.
1556 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001557 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001558 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001559 MatchAddress(N, AM);
1560 ExprMap[N] = Result;
1561
1562 // If this is not just an add, emit the LEA. For a simple add (like
1563 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1564 // leave this as LEA, then peephole it to 'ADD' after two address elim
1565 // happens.
1566 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1567 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1568 X86AddressMode XAM = SelectAddrExprs(AM);
1569 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1570 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001571 }
1572 }
Chris Lattner11333092005-01-11 03:11:44 +00001573
Chris Lattnera5ade062005-01-11 21:19:59 +00001574 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001575 Opc = 0;
1576 if (CN->getValue() == 1) { // add X, 1 -> inc X
1577 switch (N.getValueType()) {
1578 default: assert(0 && "Cannot integer add this type!");
1579 case MVT::i8: Opc = X86::INC8r; break;
1580 case MVT::i16: Opc = X86::INC16r; break;
1581 case MVT::i32: Opc = X86::INC32r; break;
1582 }
1583 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1584 switch (N.getValueType()) {
1585 default: assert(0 && "Cannot integer add this type!");
1586 case MVT::i8: Opc = X86::DEC8r; break;
1587 case MVT::i16: Opc = X86::DEC16r; break;
1588 case MVT::i32: Opc = X86::DEC32r; break;
1589 }
1590 }
1591
1592 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001593 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001594 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1595 return Result;
1596 }
1597
1598 switch (N.getValueType()) {
1599 default: assert(0 && "Cannot add this type!");
1600 case MVT::i8: Opc = X86::ADD8ri; break;
1601 case MVT::i16: Opc = X86::ADD16ri; break;
1602 case MVT::i32: Opc = X86::ADD32ri; break;
1603 }
1604 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001605 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001606 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1607 return Result;
1608 }
1609 }
1610
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001611 switch (N.getValueType()) {
1612 default: assert(0 && "Cannot add this type!");
1613 case MVT::i8: Opc = X86::ADD8rr; break;
1614 case MVT::i16: Opc = X86::ADD16rr; break;
1615 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001616 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001617 }
Chris Lattner11333092005-01-11 03:11:44 +00001618
Chris Lattnera5ade062005-01-11 21:19:59 +00001619 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1620 Tmp1 = SelectExpr(Op0);
1621 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001622 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001623 Tmp2 = SelectExpr(Op1);
1624 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001625 }
1626
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001627 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1628 return Result;
1629 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001630 case ISD::MUL:
1631 case ISD::AND:
1632 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001633 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001634 static const unsigned SUBTab[] = {
1635 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1636 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1637 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1638 };
1639 static const unsigned MULTab[] = {
1640 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1641 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1642 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1643 };
1644 static const unsigned ANDTab[] = {
1645 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1646 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1647 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1648 };
1649 static const unsigned ORTab[] = {
1650 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1651 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1652 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1653 };
1654 static const unsigned XORTab[] = {
1655 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1656 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1657 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1658 };
1659
1660 Op0 = Node->getOperand(0);
1661 Op1 = Node->getOperand(1);
1662
1663 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001664 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1665 if (CN->isNullValue()) { // 0 - N -> neg N
1666 switch (N.getValueType()) {
1667 default: assert(0 && "Cannot sub this type!");
1668 case MVT::i1:
1669 case MVT::i8: Opc = X86::NEG8r; break;
1670 case MVT::i16: Opc = X86::NEG16r; break;
1671 case MVT::i32: Opc = X86::NEG32r; break;
1672 }
1673 Tmp1 = SelectExpr(N.getOperand(1));
1674 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1675 return Result;
1676 }
1677
Chris Lattnera5ade062005-01-11 21:19:59 +00001678 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1679 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001680 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001681 switch (N.getValueType()) {
1682 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001683 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001684 case MVT::i8: Opc = X86::NOT8r; break;
1685 case MVT::i16: Opc = X86::NOT16r; break;
1686 case MVT::i32: Opc = X86::NOT32r; break;
1687 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001688 if (Opc) {
1689 Tmp1 = SelectExpr(Op0);
1690 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1691 return Result;
1692 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001693 }
1694
Chris Lattner2a4e5082005-01-17 06:48:02 +00001695 // Fold common multiplies into LEA instructions.
1696 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1697 switch ((int)CN->getValue()) {
1698 default: break;
1699 case 3:
1700 case 5:
1701 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001702 // Remove N from exprmap so SelectAddress doesn't get confused.
1703 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001704 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001705 SelectAddress(N, AM);
1706 // Restore it to the map.
1707 ExprMap[N] = Result;
1708 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1709 return Result;
1710 }
1711 }
1712
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001713 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001714 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001715 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001716 case MVT::i8: Opc = 0; break;
1717 case MVT::i16: Opc = 1; break;
1718 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001719 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001720 switch (Node->getOpcode()) {
1721 default: assert(0 && "Unreachable!");
1722 case ISD::SUB: Opc = SUBTab[Opc]; break;
1723 case ISD::MUL: Opc = MULTab[Opc]; break;
1724 case ISD::AND: Opc = ANDTab[Opc]; break;
1725 case ISD::OR: Opc = ORTab[Opc]; break;
1726 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001727 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001728 if (Opc) { // Can't fold MUL:i8 R, imm
1729 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001730 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1731 return Result;
1732 }
1733 }
Chris Lattner11333092005-01-11 03:11:44 +00001734
Chris Lattner4ff348b2005-01-17 06:26:58 +00001735 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001736 if (Node->getOpcode() != ISD::SUB) {
1737 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001738 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001739 } else {
1740 // Emit 'reverse' subract, with a memory operand.
1741 switch (N.getValueType()) {
1742 default: Opc = 0; break;
1743 case MVT::f32: Opc = X86::FSUBR32m; break;
1744 case MVT::f64: Opc = X86::FSUBR64m; break;
1745 }
1746 if (Opc) {
1747 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001748 EmitFoldedLoad(Op0, AM);
1749 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001750 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1751 return Result;
1752 }
1753 }
1754
Chris Lattner4ff348b2005-01-17 06:26:58 +00001755 if (isFoldableLoad(Op1, Op0)) {
1756 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 switch (N.getValueType()) {
1758 default: assert(0 && "Cannot operate on this type!");
1759 case MVT::i1:
1760 case MVT::i8: Opc = 5; break;
1761 case MVT::i16: Opc = 6; break;
1762 case MVT::i32: Opc = 7; break;
1763 case MVT::f32: Opc = 8; break;
1764 case MVT::f64: Opc = 9; break;
1765 }
1766 switch (Node->getOpcode()) {
1767 default: assert(0 && "Unreachable!");
1768 case ISD::SUB: Opc = SUBTab[Opc]; break;
1769 case ISD::MUL: Opc = MULTab[Opc]; break;
1770 case ISD::AND: Opc = ANDTab[Opc]; break;
1771 case ISD::OR: Opc = ORTab[Opc]; break;
1772 case ISD::XOR: Opc = XORTab[Opc]; break;
1773 }
1774
1775 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001776 EmitFoldedLoad(Op1, AM);
1777 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001778 if (Opc) {
1779 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1780 } else {
1781 assert(Node->getOpcode() == ISD::MUL &&
1782 N.getValueType() == MVT::i8 && "Unexpected situation!");
1783 // Must use the MUL instruction, which forces use of AL.
1784 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1785 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1786 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1787 }
1788 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001789 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001790
1791 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1792 Tmp1 = SelectExpr(Op0);
1793 Tmp2 = SelectExpr(Op1);
1794 } else {
1795 Tmp2 = SelectExpr(Op1);
1796 Tmp1 = SelectExpr(Op0);
1797 }
1798
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001799 switch (N.getValueType()) {
1800 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001801 case MVT::i1:
1802 case MVT::i8: Opc = 10; break;
1803 case MVT::i16: Opc = 11; break;
1804 case MVT::i32: Opc = 12; break;
1805 case MVT::f32: Opc = 13; break;
1806 case MVT::f64: Opc = 14; break;
1807 }
1808 switch (Node->getOpcode()) {
1809 default: assert(0 && "Unreachable!");
1810 case ISD::SUB: Opc = SUBTab[Opc]; break;
1811 case ISD::MUL: Opc = MULTab[Opc]; break;
1812 case ISD::AND: Opc = ANDTab[Opc]; break;
1813 case ISD::OR: Opc = ORTab[Opc]; break;
1814 case ISD::XOR: Opc = XORTab[Opc]; break;
1815 }
1816 if (Opc) {
1817 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1818 } else {
1819 assert(Node->getOpcode() == ISD::MUL &&
1820 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001821 // Must use the MUL instruction, which forces use of AL.
1822 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1823 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1824 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001826 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001827 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001828 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001829 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1830 Tmp2 = SelectExpr(N.getOperand(1));
1831 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001832 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001833 Tmp3 = SelectExpr(N.getOperand(2));
1834 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001835 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001836 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1837 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001838
1839 case ISD::SDIV:
1840 case ISD::UDIV:
1841 case ISD::SREM:
1842 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001843 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1844 "We don't support this operator!");
1845
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001846 if (N.getOpcode() == ISD::SDIV)
1847 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1848 // FIXME: These special cases should be handled by the lowering impl!
1849 unsigned RHS = CN->getValue();
1850 bool isNeg = false;
1851 if ((int)RHS < 0) {
1852 isNeg = true;
1853 RHS = -RHS;
1854 }
1855 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1856 unsigned Log = log2(RHS);
1857 unsigned TmpReg = MakeReg(N.getValueType());
1858 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1859 switch (N.getValueType()) {
1860 default: assert("Unknown type to signed divide!");
1861 case MVT::i8:
1862 SAROpc = X86::SAR8ri;
1863 SHROpc = X86::SHR8ri;
1864 ADDOpc = X86::ADD8rr;
1865 NEGOpc = X86::NEG8r;
1866 break;
1867 case MVT::i16:
1868 SAROpc = X86::SAR16ri;
1869 SHROpc = X86::SHR16ri;
1870 ADDOpc = X86::ADD16rr;
1871 NEGOpc = X86::NEG16r;
1872 break;
1873 case MVT::i32:
1874 SAROpc = X86::SAR32ri;
1875 SHROpc = X86::SHR32ri;
1876 ADDOpc = X86::ADD32rr;
1877 NEGOpc = X86::NEG32r;
1878 break;
1879 }
Chris Lattner11333092005-01-11 03:11:44 +00001880 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001881 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1882 unsigned TmpReg2 = MakeReg(N.getValueType());
1883 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1884 unsigned TmpReg3 = MakeReg(N.getValueType());
1885 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1886
1887 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1888 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1889 if (isNeg)
1890 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1891 return Result;
1892 }
1893 }
1894
Chris Lattner11333092005-01-11 03:11:44 +00001895 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1896 Tmp1 = SelectExpr(N.getOperand(0));
1897 Tmp2 = SelectExpr(N.getOperand(1));
1898 } else {
1899 Tmp2 = SelectExpr(N.getOperand(1));
1900 Tmp1 = SelectExpr(N.getOperand(0));
1901 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001902
1903 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1904 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1905 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1906 switch (N.getValueType()) {
1907 default: assert(0 && "Cannot sdiv this type!");
1908 case MVT::i8:
1909 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1910 LoReg = X86::AL;
1911 HiReg = X86::AH;
1912 MovOpcode = X86::MOV8rr;
1913 ClrOpcode = X86::MOV8ri;
1914 SExtOpcode = X86::CBW;
1915 break;
1916 case MVT::i16:
1917 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1918 LoReg = X86::AX;
1919 HiReg = X86::DX;
1920 MovOpcode = X86::MOV16rr;
1921 ClrOpcode = X86::MOV16ri;
1922 SExtOpcode = X86::CWD;
1923 break;
1924 case MVT::i32:
1925 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001926 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 HiReg = X86::EDX;
1928 MovOpcode = X86::MOV32rr;
1929 ClrOpcode = X86::MOV32ri;
1930 SExtOpcode = X86::CDQ;
1931 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001932 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001933 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001934 return Result;
1935 }
1936
1937 // Set up the low part.
1938 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1939
1940 if (isSigned) {
1941 // Sign extend the low part into the high part.
1942 BuildMI(BB, SExtOpcode, 0);
1943 } else {
1944 // Zero out the high part, effectively zero extending the input.
1945 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1946 }
1947
1948 // Emit the DIV/IDIV instruction.
1949 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1950
1951 // Get the result of the divide or rem.
1952 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1953 return Result;
1954 }
1955
1956 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001958 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1959 switch (N.getValueType()) {
1960 default: assert(0 && "Cannot shift this type!");
1961 case MVT::i8: Opc = X86::ADD8rr; break;
1962 case MVT::i16: Opc = X86::ADD16rr; break;
1963 case MVT::i32: Opc = X86::ADD32rr; break;
1964 }
1965 Tmp1 = SelectExpr(N.getOperand(0));
1966 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1967 return Result;
1968 }
1969
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970 switch (N.getValueType()) {
1971 default: assert(0 && "Cannot shift this type!");
1972 case MVT::i8: Opc = X86::SHL8ri; break;
1973 case MVT::i16: Opc = X86::SHL16ri; break;
1974 case MVT::i32: Opc = X86::SHL32ri; break;
1975 }
Chris Lattner11333092005-01-11 03:11:44 +00001976 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001977 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1978 return Result;
1979 }
Chris Lattner11333092005-01-11 03:11:44 +00001980
1981 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1982 Tmp1 = SelectExpr(N.getOperand(0));
1983 Tmp2 = SelectExpr(N.getOperand(1));
1984 } else {
1985 Tmp2 = SelectExpr(N.getOperand(1));
1986 Tmp1 = SelectExpr(N.getOperand(0));
1987 }
1988
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001989 switch (N.getValueType()) {
1990 default: assert(0 && "Cannot shift this type!");
1991 case MVT::i8 : Opc = X86::SHL8rCL; break;
1992 case MVT::i16: Opc = X86::SHL16rCL; break;
1993 case MVT::i32: Opc = X86::SHL32rCL; break;
1994 }
1995 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1996 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1997 return Result;
1998 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001999 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2000 switch (N.getValueType()) {
2001 default: assert(0 && "Cannot shift this type!");
2002 case MVT::i8: Opc = X86::SHR8ri; break;
2003 case MVT::i16: Opc = X86::SHR16ri; break;
2004 case MVT::i32: Opc = X86::SHR32ri; break;
2005 }
Chris Lattner11333092005-01-11 03:11:44 +00002006 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2008 return Result;
2009 }
Chris Lattner11333092005-01-11 03:11:44 +00002010
2011 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2012 Tmp1 = SelectExpr(N.getOperand(0));
2013 Tmp2 = SelectExpr(N.getOperand(1));
2014 } else {
2015 Tmp2 = SelectExpr(N.getOperand(1));
2016 Tmp1 = SelectExpr(N.getOperand(0));
2017 }
2018
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002019 switch (N.getValueType()) {
2020 default: assert(0 && "Cannot shift this type!");
2021 case MVT::i8 : Opc = X86::SHR8rCL; break;
2022 case MVT::i16: Opc = X86::SHR16rCL; break;
2023 case MVT::i32: Opc = X86::SHR32rCL; break;
2024 }
2025 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2026 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2027 return Result;
2028 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002029 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2030 switch (N.getValueType()) {
2031 default: assert(0 && "Cannot shift this type!");
2032 case MVT::i8: Opc = X86::SAR8ri; break;
2033 case MVT::i16: Opc = X86::SAR16ri; break;
2034 case MVT::i32: Opc = X86::SAR32ri; break;
2035 }
Chris Lattner11333092005-01-11 03:11:44 +00002036 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002037 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2038 return Result;
2039 }
Chris Lattner11333092005-01-11 03:11:44 +00002040
2041 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2042 Tmp1 = SelectExpr(N.getOperand(0));
2043 Tmp2 = SelectExpr(N.getOperand(1));
2044 } else {
2045 Tmp2 = SelectExpr(N.getOperand(1));
2046 Tmp1 = SelectExpr(N.getOperand(0));
2047 }
2048
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002049 switch (N.getValueType()) {
2050 default: assert(0 && "Cannot shift this type!");
2051 case MVT::i8 : Opc = X86::SAR8rCL; break;
2052 case MVT::i16: Opc = X86::SAR16rCL; break;
2053 case MVT::i32: Opc = X86::SAR32rCL; break;
2054 }
2055 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2056 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2057 return Result;
2058
2059 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002060 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002061 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2062 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2063 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002064 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002065 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002066 if (Result != 1) { // Generate the token
2067 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2068 assert(0 && "Load already emitted!?");
2069 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002070 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2071
Chris Lattner5188ad72005-01-08 19:28:19 +00002072 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002073 default: assert(0 && "Cannot load this type!");
2074 case MVT::i1:
2075 case MVT::i8: Opc = X86::MOV8rm; break;
2076 case MVT::i16: Opc = X86::MOV16rm; break;
2077 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002078 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2079 }
Chris Lattner11333092005-01-11 03:11:44 +00002080
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002081 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002082 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002083 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2084 } else {
2085 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002086
2087 SDOperand Chain = N.getOperand(0);
2088 SDOperand Address = N.getOperand(1);
2089 if (getRegPressure(Chain) > getRegPressure(Address)) {
2090 Select(Chain);
2091 SelectAddress(Address, AM);
2092 } else {
2093 SelectAddress(Address, AM);
2094 Select(Chain);
2095 }
2096
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2098 }
2099 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002100
2101 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2102 case ISD::ZEXTLOAD: {
2103 // Make sure we generate both values.
2104 if (Result != 1)
2105 ExprMap[N.getValue(1)] = 1; // Generate the token
2106 else
2107 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2108
Chris Lattnerda2ce112005-01-16 07:34:08 +00002109 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2110 if (Node->getValueType(0) == MVT::f64) {
2111 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2112 "Bad EXTLOAD!");
2113 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2114 CP->getIndex());
2115 return Result;
2116 }
2117
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002118 X86AddressMode AM;
2119 if (getRegPressure(Node->getOperand(0)) >
2120 getRegPressure(Node->getOperand(1))) {
2121 Select(Node->getOperand(0)); // chain
2122 SelectAddress(Node->getOperand(1), AM);
2123 } else {
2124 SelectAddress(Node->getOperand(1), AM);
2125 Select(Node->getOperand(0)); // chain
2126 }
2127
2128 switch (Node->getValueType(0)) {
2129 default: assert(0 && "Unknown type to sign extend to.");
2130 case MVT::f64:
2131 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2132 "Bad EXTLOAD!");
2133 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2134 break;
2135 case MVT::i32:
2136 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2137 default:
2138 assert(0 && "Bad zero extend!");
2139 case MVT::i1:
2140 case MVT::i8:
2141 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2142 break;
2143 case MVT::i16:
2144 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2145 break;
2146 }
2147 break;
2148 case MVT::i16:
2149 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2150 "Bad zero extend!");
2151 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2152 break;
2153 case MVT::i8:
2154 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2155 "Bad zero extend!");
2156 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2157 break;
2158 }
2159 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002160 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002161 case ISD::SEXTLOAD: {
2162 // Make sure we generate both values.
2163 if (Result != 1)
2164 ExprMap[N.getValue(1)] = 1; // Generate the token
2165 else
2166 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2167
2168 X86AddressMode AM;
2169 if (getRegPressure(Node->getOperand(0)) >
2170 getRegPressure(Node->getOperand(1))) {
2171 Select(Node->getOperand(0)); // chain
2172 SelectAddress(Node->getOperand(1), AM);
2173 } else {
2174 SelectAddress(Node->getOperand(1), AM);
2175 Select(Node->getOperand(0)); // chain
2176 }
2177
2178 switch (Node->getValueType(0)) {
2179 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2180 default: assert(0 && "Unknown type to sign extend to.");
2181 case MVT::i32:
2182 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2183 default:
2184 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2185 case MVT::i8:
2186 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2187 break;
2188 case MVT::i16:
2189 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2190 break;
2191 }
2192 break;
2193 case MVT::i16:
2194 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2195 "Cannot sign extend from bool!");
2196 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2197 break;
2198 }
2199 return Result;
2200 }
2201
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002202 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002203 // Generate both result values.
2204 if (Result != 1)
2205 ExprMap[N.getValue(1)] = 1; // Generate the token
2206 else
2207 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2208
2209 // FIXME: We are currently ignoring the requested alignment for handling
2210 // greater than the stack alignment. This will need to be revisited at some
2211 // point. Align = N.getOperand(2);
2212
2213 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2214 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2215 std::cerr << "Cannot allocate stack object with greater alignment than"
2216 << " the stack alignment yet!";
2217 abort();
2218 }
2219
2220 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002221 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002222 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2223 .addImm(CN->getValue());
2224 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002225 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2226 Select(N.getOperand(0));
2227 Tmp1 = SelectExpr(N.getOperand(1));
2228 } else {
2229 Tmp1 = SelectExpr(N.getOperand(1));
2230 Select(N.getOperand(0));
2231 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002232
2233 // Subtract size from stack pointer, thereby allocating some space.
2234 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2235 }
2236
2237 // Put a pointer to the space into the result register, by copying the stack
2238 // pointer.
2239 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2240 return Result;
2241
2242 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002243 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002244 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002245
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002246 if (GlobalAddressSDNode *GASD =
2247 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002248 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002249 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2250 } else if (ExternalSymbolSDNode *ESSDN =
2251 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002252 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002253 BuildMI(BB, X86::CALLpcrel32,
2254 1).addExternalSymbol(ESSDN->getSymbol(), true);
2255 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002256 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2257 Select(N.getOperand(0));
2258 Tmp1 = SelectExpr(N.getOperand(1));
2259 } else {
2260 Tmp1 = SelectExpr(N.getOperand(1));
2261 Select(N.getOperand(0));
2262 }
2263
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002264 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2265 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002266 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002267 default: assert(0 && "Unknown value type for call result!");
2268 case MVT::Other: return 1;
2269 case MVT::i1:
2270 case MVT::i8:
2271 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2272 break;
2273 case MVT::i16:
2274 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2275 break;
2276 case MVT::i32:
2277 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002278 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2280 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002281 case MVT::f64: // Floating-point return values live in %ST(0)
2282 ContainsFPCode = true;
2283 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2284 break;
2285 }
2286 return Result+N.ResNo;
2287 }
2288
2289 return 0;
2290}
2291
Chris Lattnere10269b2005-01-17 19:25:26 +00002292/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2293/// load/op/store instruction. If successful return true.
2294bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2295 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2296 SDOperand Chain = Node->getOperand(0);
2297 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002298 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002299
2300 // The chain has to be a load, the stored value must be an integer binary
2301 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002302 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002303 MVT::isFloatingPoint(StVal.getValueType()))
2304 return false;
2305
Chris Lattner5c659812005-01-17 22:10:42 +00002306 // Token chain must either be a factor node or the load to fold.
2307 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2308 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002309
Chris Lattner5c659812005-01-17 22:10:42 +00002310 SDOperand TheLoad;
2311
2312 // Check to see if there is a load from the same pointer that we're storing
2313 // to in either operand of the binop.
2314 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2315 StVal.getOperand(0).getOperand(1) == StPtr)
2316 TheLoad = StVal.getOperand(0);
2317 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2318 StVal.getOperand(1).getOperand(1) == StPtr)
2319 TheLoad = StVal.getOperand(1);
2320 else
2321 return false; // No matching load operand.
2322
2323 // We can only fold the load if there are no intervening side-effecting
2324 // operations. This means that the store uses the load as its token chain, or
2325 // there are only token factor nodes in between the store and load.
2326 if (Chain != TheLoad.getValue(1)) {
2327 // Okay, the other option is that we have a store referring to (possibly
2328 // nested) token factor nodes. For now, just try peeking through one level
2329 // of token factors to see if this is the case.
2330 bool ChainOk = false;
2331 if (Chain.getOpcode() == ISD::TokenFactor) {
2332 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2333 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2334 ChainOk = true;
2335 break;
2336 }
2337 }
2338
2339 if (!ChainOk) return false;
2340 }
2341
2342 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002343 return false;
2344
2345 // Make sure that one of the operands of the binop is the load, and that the
2346 // load folds into the binop.
2347 if (((StVal.getOperand(0) != TheLoad ||
2348 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2349 (StVal.getOperand(1) != TheLoad ||
2350 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2351 return false;
2352
2353 // Finally, check to see if this is one of the ops we can handle!
2354 static const unsigned ADDTAB[] = {
2355 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2356 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2357 };
2358 static const unsigned SUBTAB[] = {
2359 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2360 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2361 };
2362 static const unsigned ANDTAB[] = {
2363 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2364 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2365 };
2366 static const unsigned ORTAB[] = {
2367 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2368 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2369 };
2370 static const unsigned XORTAB[] = {
2371 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2372 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2373 };
2374 static const unsigned SHLTAB[] = {
2375 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2376 /*Have to put the reg in CL*/0, 0, 0,
2377 };
2378 static const unsigned SARTAB[] = {
2379 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2380 /*Have to put the reg in CL*/0, 0, 0,
2381 };
2382 static const unsigned SHRTAB[] = {
2383 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2384 /*Have to put the reg in CL*/0, 0, 0,
2385 };
2386
2387 const unsigned *TabPtr = 0;
2388 switch (StVal.getOpcode()) {
2389 default:
2390 std::cerr << "CANNOT [mem] op= val: ";
2391 StVal.Val->dump(); std::cerr << "\n";
2392 case ISD::MUL:
2393 case ISD::SDIV:
2394 case ISD::UDIV:
2395 case ISD::SREM:
2396 case ISD::UREM: return false;
2397
2398 case ISD::ADD: TabPtr = ADDTAB; break;
2399 case ISD::SUB: TabPtr = SUBTAB; break;
2400 case ISD::AND: TabPtr = ANDTAB; break;
2401 case ISD:: OR: TabPtr = ORTAB; break;
2402 case ISD::XOR: TabPtr = XORTAB; break;
2403 case ISD::SHL: TabPtr = SHLTAB; break;
2404 case ISD::SRA: TabPtr = SARTAB; break;
2405 case ISD::SRL: TabPtr = SHRTAB; break;
2406 }
2407
2408 // Handle: [mem] op= CST
2409 SDOperand Op0 = StVal.getOperand(0);
2410 SDOperand Op1 = StVal.getOperand(1);
2411 unsigned Opc;
2412 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2413 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2414 default: break;
2415 case MVT::i1:
2416 case MVT::i8: Opc = TabPtr[0]; break;
2417 case MVT::i16: Opc = TabPtr[1]; break;
2418 case MVT::i32: Opc = TabPtr[2]; break;
2419 }
2420
2421 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002422 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2423 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002424 Select(Chain);
2425
Chris Lattnere10269b2005-01-17 19:25:26 +00002426 X86AddressMode AM;
2427 if (getRegPressure(TheLoad.getOperand(0)) >
2428 getRegPressure(TheLoad.getOperand(1))) {
2429 Select(TheLoad.getOperand(0));
2430 SelectAddress(TheLoad.getOperand(1), AM);
2431 } else {
2432 SelectAddress(TheLoad.getOperand(1), AM);
2433 Select(TheLoad.getOperand(0));
2434 }
Chris Lattner5c659812005-01-17 22:10:42 +00002435
2436 if (StVal.getOpcode() == ISD::ADD) {
2437 if (CN->getValue() == 1) {
2438 switch (Op0.getValueType()) {
2439 default: break;
2440 case MVT::i8:
2441 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2442 return true;
2443 case MVT::i16: Opc = TabPtr[1];
2444 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2445 return true;
2446 case MVT::i32: Opc = TabPtr[2];
2447 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2448 return true;
2449 }
2450 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2451 switch (Op0.getValueType()) {
2452 default: break;
2453 case MVT::i8:
2454 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2455 return true;
2456 case MVT::i16: Opc = TabPtr[1];
2457 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2458 return true;
2459 case MVT::i32: Opc = TabPtr[2];
2460 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2461 return true;
2462 }
2463 }
2464 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002465
2466 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2467 return true;
2468 }
2469 }
2470
2471 // If we have [mem] = V op [mem], try to turn it into:
2472 // [mem] = [mem] op V.
2473 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2474 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2475 StVal.getOpcode() != ISD::SRL)
2476 std::swap(Op0, Op1);
2477
2478 if (Op0 != TheLoad) return false;
2479
2480 switch (Op0.getValueType()) {
2481 default: return false;
2482 case MVT::i1:
2483 case MVT::i8: Opc = TabPtr[3]; break;
2484 case MVT::i16: Opc = TabPtr[4]; break;
2485 case MVT::i32: Opc = TabPtr[5]; break;
2486 }
Chris Lattner5c659812005-01-17 22:10:42 +00002487
Chris Lattnerb422aea2005-01-18 17:35:28 +00002488 // Table entry doesn't exist?
2489 if (Opc == 0) return false;
2490
Chris Lattner4a108662005-01-18 03:51:59 +00002491 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2492 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002493 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002494 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002495
Chris Lattnere10269b2005-01-17 19:25:26 +00002496 X86AddressMode AM;
2497 SelectAddress(TheLoad.getOperand(1), AM);
2498 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002499 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002500 return true;
2501}
2502
2503
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002504void ISel::Select(SDOperand N) {
2505 unsigned Tmp1, Tmp2, Opc;
2506
2507 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002508 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002509 return; // Already selected.
2510
Chris Lattner989de032005-01-11 06:14:36 +00002511 SDNode *Node = N.Val;
2512
2513 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002514 default:
Chris Lattner989de032005-01-11 06:14:36 +00002515 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002516 assert(0 && "Node not handled yet!");
2517 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002518 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002519 if (Node->getNumOperands() == 2) {
2520 bool OneFirst =
2521 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2522 Select(Node->getOperand(OneFirst));
2523 Select(Node->getOperand(!OneFirst));
2524 } else {
2525 std::vector<std::pair<unsigned, unsigned> > OpsP;
2526 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2527 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2528 std::sort(OpsP.begin(), OpsP.end());
2529 std::reverse(OpsP.begin(), OpsP.end());
2530 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2531 Select(Node->getOperand(OpsP[i].second));
2532 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002533 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002534 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002535 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2536 Select(N.getOperand(0));
2537 Tmp1 = SelectExpr(N.getOperand(1));
2538 } else {
2539 Tmp1 = SelectExpr(N.getOperand(1));
2540 Select(N.getOperand(0));
2541 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002542 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002543
2544 if (Tmp1 != Tmp2) {
2545 switch (N.getOperand(1).getValueType()) {
2546 default: assert(0 && "Invalid type for operation!");
2547 case MVT::i1:
2548 case MVT::i8: Opc = X86::MOV8rr; break;
2549 case MVT::i16: Opc = X86::MOV16rr; break;
2550 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002551 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002552 }
2553 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2554 }
2555 return;
2556 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002557 switch (N.getNumOperands()) {
2558 default:
2559 assert(0 && "Unknown return instruction!");
2560 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002561 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2562 N.getOperand(2).getValueType() == MVT::i32 &&
2563 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002564 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2565 Tmp1 = SelectExpr(N.getOperand(1));
2566 Tmp2 = SelectExpr(N.getOperand(2));
2567 } else {
2568 Tmp2 = SelectExpr(N.getOperand(2));
2569 Tmp1 = SelectExpr(N.getOperand(1));
2570 }
2571 Select(N.getOperand(0));
2572
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002573 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2574 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2575 // Declare that EAX & EDX are live on exit.
2576 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2577 .addReg(X86::ESP);
2578 break;
2579 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002580 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2581 Select(N.getOperand(0));
2582 Tmp1 = SelectExpr(N.getOperand(1));
2583 } else {
2584 Tmp1 = SelectExpr(N.getOperand(1));
2585 Select(N.getOperand(0));
2586 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002587 switch (N.getOperand(1).getValueType()) {
2588 default: assert(0 && "All other types should have been promoted!!");
2589 case MVT::f64:
2590 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2591 // Declare that top-of-stack is live on exit
2592 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2593 break;
2594 case MVT::i32:
2595 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2596 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2597 break;
2598 }
2599 break;
2600 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002601 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002602 break;
2603 }
2604 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2605 return;
2606 case ISD::BR: {
2607 Select(N.getOperand(0));
2608 MachineBasicBlock *Dest =
2609 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2610 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2611 return;
2612 }
2613
2614 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002615 MachineBasicBlock *Dest =
2616 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002617
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002618 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2619 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002620 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2621 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2622 Select(N.getOperand(0));
2623 Tmp1 = SelectExpr(N.getOperand(1));
2624 } else {
2625 Tmp1 = SelectExpr(N.getOperand(1));
2626 Select(N.getOperand(0));
2627 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002628 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2629 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2630 }
Chris Lattner11333092005-01-11 03:11:44 +00002631
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002632 return;
2633 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002634
Chris Lattner4df0de92005-01-17 00:00:33 +00002635 case ISD::LOAD:
2636 // If this load could be folded into the only using instruction, and if it
2637 // is safe to emit the instruction here, try to do so now.
2638 if (Node->hasNUsesOfValue(1, 0)) {
2639 SDOperand TheVal = N.getValue(0);
2640 SDNode *User = 0;
2641 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2642 assert(UI != Node->use_end() && "Didn't find use!");
2643 SDNode *UN = *UI;
2644 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2645 if (UN->getOperand(i) == TheVal) {
2646 User = UN;
2647 goto FoundIt;
2648 }
2649 }
2650 FoundIt:
2651 // Only handle unary operators right now.
2652 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002653 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002654 SelectExpr(SDOperand(User, 0));
2655 return;
2656 }
2657 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002658 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002659 SelectExpr(N);
2660 return;
2661
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002662 case ISD::EXTLOAD:
2663 case ISD::SEXTLOAD:
2664 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002665 case ISD::CALL:
2666 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002667 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002668 SelectExpr(N);
2669 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002670
2671 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2672 // On X86, we can represent all types except for Bool and Float natively.
2673 X86AddressMode AM;
2674 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002675 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2676 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2677 && "Unsupported TRUNCSTORE for this target!");
2678
2679 if (StoredTy == MVT::i16) {
2680 // FIXME: This is here just to allow testing. X86 doesn't really have a
2681 // TRUNCSTORE i16 operation, but this is required for targets that do not
2682 // have 16-bit integer registers. We occasionally disable 16-bit integer
2683 // registers to test the promotion code.
2684 Select(N.getOperand(0));
2685 Tmp1 = SelectExpr(N.getOperand(1));
2686 SelectAddress(N.getOperand(2), AM);
2687
2688 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2689 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2690 return;
2691 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002692
2693 // Store of constant bool?
2694 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2695 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2696 Select(N.getOperand(0));
2697 SelectAddress(N.getOperand(2), AM);
2698 } else {
2699 SelectAddress(N.getOperand(2), AM);
2700 Select(N.getOperand(0));
2701 }
2702 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2703 return;
2704 }
2705
2706 switch (StoredTy) {
2707 default: assert(0 && "Cannot truncstore this type!");
2708 case MVT::i1: Opc = X86::MOV8mr; break;
2709 case MVT::f32: Opc = X86::FST32m; break;
2710 }
2711
2712 std::vector<std::pair<unsigned, unsigned> > RP;
2713 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2714 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2715 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2716 std::sort(RP.begin(), RP.end());
2717
2718 for (unsigned i = 0; i != 3; ++i)
2719 switch (RP[2-i].second) {
2720 default: assert(0 && "Unknown operand number!");
2721 case 0: Select(N.getOperand(0)); break;
2722 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2723 case 2: SelectAddress(N.getOperand(2), AM); break;
2724 }
2725
2726 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2727 return;
2728 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002729 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002730 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002731
2732 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2733 Opc = 0;
2734 switch (CN->getValueType(0)) {
2735 default: assert(0 && "Invalid type for operation!");
2736 case MVT::i1:
2737 case MVT::i8: Opc = X86::MOV8mi; break;
2738 case MVT::i16: Opc = X86::MOV16mi; break;
2739 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002740 case MVT::f64: break;
2741 }
2742 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002743 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2744 Select(N.getOperand(0));
2745 SelectAddress(N.getOperand(2), AM);
2746 } else {
2747 SelectAddress(N.getOperand(2), AM);
2748 Select(N.getOperand(0));
2749 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002750 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2751 return;
2752 }
2753 }
Chris Lattner837caa72005-01-11 23:21:30 +00002754
2755 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002756 if (TryToFoldLoadOpStore(Node))
2757 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002758
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002759 switch (N.getOperand(1).getValueType()) {
2760 default: assert(0 && "Cannot store this type!");
2761 case MVT::i1:
2762 case MVT::i8: Opc = X86::MOV8mr; break;
2763 case MVT::i16: Opc = X86::MOV16mr; break;
2764 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002765 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002766 }
Chris Lattner11333092005-01-11 03:11:44 +00002767
2768 std::vector<std::pair<unsigned, unsigned> > RP;
2769 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2770 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2771 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2772 std::sort(RP.begin(), RP.end());
2773
2774 for (unsigned i = 0; i != 3; ++i)
2775 switch (RP[2-i].second) {
2776 default: assert(0 && "Unknown operand number!");
2777 case 0: Select(N.getOperand(0)); break;
2778 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002779 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002780 }
2781
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002782 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2783 return;
2784 }
2785 case ISD::ADJCALLSTACKDOWN:
2786 case ISD::ADJCALLSTACKUP:
2787 Select(N.getOperand(0));
2788 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2789
2790 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2791 X86::ADJCALLSTACKUP;
2792 BuildMI(BB, Opc, 1).addImm(Tmp1);
2793 return;
Chris Lattner989de032005-01-11 06:14:36 +00002794 case ISD::MEMSET: {
2795 Select(N.getOperand(0)); // Select the chain.
2796 unsigned Align =
2797 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2798 if (Align == 0) Align = 1;
2799
2800 // Turn the byte code into # iterations
2801 unsigned CountReg;
2802 unsigned Opcode;
2803 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2804 unsigned Val = ValC->getValue() & 255;
2805
2806 // If the value is a constant, then we can potentially use larger sets.
2807 switch (Align & 3) {
2808 case 2: // WORD aligned
2809 CountReg = MakeReg(MVT::i32);
2810 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2811 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2812 } else {
2813 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2814 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2815 }
2816 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2817 Opcode = X86::REP_STOSW;
2818 break;
2819 case 0: // DWORD aligned
2820 CountReg = MakeReg(MVT::i32);
2821 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2822 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2823 } else {
2824 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2825 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2826 }
2827 Val = (Val << 8) | Val;
2828 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2829 Opcode = X86::REP_STOSD;
2830 break;
2831 default: // BYTE aligned
2832 CountReg = SelectExpr(Node->getOperand(3));
2833 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2834 Opcode = X86::REP_STOSB;
2835 break;
2836 }
2837 } else {
2838 // If it's not a constant value we are storing, just fall back. We could
2839 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2840 unsigned ValReg = SelectExpr(Node->getOperand(2));
2841 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2842 CountReg = SelectExpr(Node->getOperand(3));
2843 Opcode = X86::REP_STOSB;
2844 }
2845
2846 // No matter what the alignment is, we put the source in ESI, the
2847 // destination in EDI, and the count in ECX.
2848 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2849 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2850 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2851 BuildMI(BB, Opcode, 0);
2852 return;
2853 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002854 case ISD::MEMCPY:
2855 Select(N.getOperand(0)); // Select the chain.
2856 unsigned Align =
2857 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2858 if (Align == 0) Align = 1;
2859
2860 // Turn the byte code into # iterations
2861 unsigned CountReg;
2862 unsigned Opcode;
2863 switch (Align & 3) {
2864 case 2: // WORD aligned
2865 CountReg = MakeReg(MVT::i32);
2866 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2867 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2868 } else {
2869 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2870 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2871 }
2872 Opcode = X86::REP_MOVSW;
2873 break;
2874 case 0: // DWORD aligned
2875 CountReg = MakeReg(MVT::i32);
2876 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2877 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2878 } else {
2879 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2880 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2881 }
2882 Opcode = X86::REP_MOVSD;
2883 break;
2884 default: // BYTE aligned
2885 CountReg = SelectExpr(Node->getOperand(3));
2886 Opcode = X86::REP_MOVSB;
2887 break;
2888 }
2889
2890 // No matter what the alignment is, we put the source in ESI, the
2891 // destination in EDI, and the count in ECX.
2892 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2893 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2894 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2895 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2896 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2897 BuildMI(BB, Opcode, 0);
2898 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002899 }
2900 assert(0 && "Should not be reached!");
2901}
2902
2903
2904/// createX86PatternInstructionSelector - This pass converts an LLVM function
2905/// into a machine code representation using pattern matching and a machine
2906/// description file.
2907///
2908FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2909 return new ISel(TM);
2910}