blob: 4dc29ea6fd59034e8226d578635fe9fe1d96e7e3 [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);
56/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
57
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
200 unsigned ArgReg;
201 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
202 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
203
204 switch (getValueType(Args[i].second)) {
205 default: assert(0 && "Unexpected ValueType for argument!");
206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
209 // Promote the integer to 32 bits. If the input type is signed use a
210 // sign extend, otherwise use a zero extend.
211 if (Args[i].second->isSigned())
212 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
213 else
214 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
215
216 // FALL THROUGH
217 case MVT::i32:
218 case MVT::f32:
219 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000220 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
221 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000222 ArgOffset += 4;
223 break;
224 case MVT::i64:
225 case MVT::f64:
226 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
228 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229 ArgOffset += 8;
230 break;
231 }
232 }
233 }
234
235 std::vector<MVT::ValueType> RetVals;
236 MVT::ValueType RetTyVT = getValueType(RetTy);
237 if (RetTyVT != MVT::isVoid)
238 RetVals.push_back(RetTyVT);
239 RetVals.push_back(MVT::Other);
240
Chris Lattner5188ad72005-01-08 19:28:19 +0000241 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000242 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
245 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000246}
247
Chris Lattner14824582005-01-09 00:01:27 +0000248std::pair<SDOperand, SDOperand>
249X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
250 // vastart just returns the address of the VarArgsFrameIndex slot.
251 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
252}
253
254std::pair<SDOperand,SDOperand> X86TargetLowering::
255LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
256 const Type *ArgTy, SelectionDAG &DAG) {
257 MVT::ValueType ArgVT = getValueType(ArgTy);
258 SDOperand Result;
259 if (!isVANext) {
260 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
261 } else {
262 unsigned Amt;
263 if (ArgVT == MVT::i32)
264 Amt = 4;
265 else {
266 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
267 "Other types should have been promoted for varargs!");
268 Amt = 8;
269 }
270 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
271 DAG.getConstant(Amt, VAList.getValueType()));
272 }
273 return std::make_pair(Result, Chain);
274}
275
276
277std::pair<SDOperand, SDOperand> X86TargetLowering::
278LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
279 SelectionDAG &DAG) {
280 SDOperand Result;
281 if (Depth) // Depths > 0 not supported yet!
282 Result = DAG.getConstant(0, getPointerTy());
283 else {
284 if (ReturnAddrIndex == 0) {
285 // Set up a frame object for the return address.
286 MachineFunction &MF = DAG.getMachineFunction();
287 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
288 }
289
290 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
291
292 if (!isFrameAddress)
293 // Just load the return address
294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
295 else
296 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
297 DAG.getConstant(4, MVT::i32));
298 }
299 return std::make_pair(Result, Chain);
300}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301
302
Chris Lattner98a8ba02005-01-18 01:06:26 +0000303namespace {
304 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
305 /// SDOperand's instead of register numbers for the leaves of the matched
306 /// tree.
307 struct X86ISelAddressMode {
308 enum {
309 RegBase,
310 FrameIndexBase,
311 } BaseType;
312
313 struct { // This is really a union, discriminated by BaseType!
314 SDOperand Reg;
315 int FrameIndex;
316 } Base;
317
318 unsigned Scale;
319 SDOperand IndexReg;
320 unsigned Disp;
321 GlobalValue *GV;
322
323 X86ISelAddressMode()
324 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
325 }
326 };
327}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328
329
330namespace {
331 Statistic<>
332 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
333
334 //===--------------------------------------------------------------------===//
335 /// ISel - X86 specific code to select X86 machine instructions for
336 /// SelectionDAG operations.
337 ///
338 class ISel : public SelectionDAGISel {
339 /// ContainsFPCode - Every instruction we select that uses or defines a FP
340 /// register should set this to true.
341 bool ContainsFPCode;
342
343 /// X86Lowering - This object fully describes how to lower LLVM code to an
344 /// X86-specific SelectionDAG.
345 X86TargetLowering X86Lowering;
346
Chris Lattner11333092005-01-11 03:11:44 +0000347 /// RegPressureMap - This keeps an approximate count of the number of
348 /// registers required to evaluate each node in the graph.
349 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000350
351 /// ExprMap - As shared expressions are codegen'd, we keep track of which
352 /// vreg the value is produced in, so we only emit one copy of each compiled
353 /// tree.
354 std::map<SDOperand, unsigned> ExprMap;
355 std::set<SDOperand> LoweredTokens;
356
357 public:
358 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
359 }
360
Chris Lattner11333092005-01-11 03:11:44 +0000361 unsigned getRegPressure(SDOperand O) {
362 return RegPressureMap[O.Val];
363 }
364 unsigned ComputeRegPressure(SDOperand O);
365
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000366 /// InstructionSelectBasicBlock - This callback is invoked by
367 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000368 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000369
Chris Lattner4ff348b2005-01-17 06:26:58 +0000370 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000371 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000372 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000373
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000374 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000375 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000376 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
377 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000378 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000379
380 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
381 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
382 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 void Select(SDOperand N);
384 };
385}
386
Chris Lattner7dbcb752005-01-12 04:21:28 +0000387/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
388/// when it has created a SelectionDAG for us to codegen.
389void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
390 // While we're doing this, keep track of whether we see any FP code for
391 // FP_REG_KILL insertion.
392 ContainsFPCode = false;
393
394 // Scan the PHI nodes that already are inserted into this basic block. If any
395 // of them is a PHI of a floating point value, we need to insert an
396 // FP_REG_KILL.
397 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
398 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
399 I != E; ++I) {
400 assert(I->getOpcode() == X86::PHI &&
401 "Isn't just PHI nodes?");
402 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
403 X86::RFPRegisterClass) {
404 ContainsFPCode = true;
405 break;
406 }
407 }
408
409 // Compute the RegPressureMap, which is an approximation for the number of
410 // registers required to compute each node.
411 ComputeRegPressure(DAG.getRoot());
412
413 // Codegen the basic block.
414 Select(DAG.getRoot());
415
416 // Finally, look at all of the successors of this block. If any contain a PHI
417 // node of FP type, we need to insert an FP_REG_KILL in this block.
418 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
419 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
420 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
421 I != E && I->getOpcode() == X86::PHI; ++I) {
422 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
423 X86::RFPRegisterClass) {
424 ContainsFPCode = true;
425 break;
426 }
427 }
428
429 // Insert FP_REG_KILL instructions into basic blocks that need them. This
430 // only occurs due to the floating point stackifier not being aggressive
431 // enough to handle arbitrary global stackification.
432 //
433 // Currently we insert an FP_REG_KILL instruction into each block that uses or
434 // defines a floating point virtual register.
435 //
436 // When the global register allocators (like linear scan) finally update live
437 // variable analysis, we can keep floating point values in registers across
438 // basic blocks. This will be a huge win, but we are waiting on the global
439 // allocators before we can do this.
440 //
441 if (ContainsFPCode && BB->succ_size()) {
442 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
443 ++NumFPKill;
444 }
445
446 // Clear state used for selection.
447 ExprMap.clear();
448 LoweredTokens.clear();
449 RegPressureMap.clear();
450}
451
452
Chris Lattner11333092005-01-11 03:11:44 +0000453// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
454// for the number of registers required to compute each node. This is basically
455// computing a generalized form of the Sethi-Ullman number for each node.
456unsigned ISel::ComputeRegPressure(SDOperand O) {
457 SDNode *N = O.Val;
458 unsigned &Result = RegPressureMap[N];
459 if (Result) return Result;
460
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000461 // FIXME: Should operations like CALL (which clobber lots o regs) have a
462 // higher fixed cost??
463
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000464 if (N->getNumOperands() == 0) {
465 Result = 1;
466 } else {
467 unsigned MaxRegUse = 0;
468 unsigned NumExtraMaxRegUsers = 0;
469 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
470 unsigned Regs;
471 if (N->getOperand(i).getOpcode() == ISD::Constant)
472 Regs = 0;
473 else
474 Regs = ComputeRegPressure(N->getOperand(i));
475 if (Regs > MaxRegUse) {
476 MaxRegUse = Regs;
477 NumExtraMaxRegUsers = 0;
478 } else if (Regs == MaxRegUse &&
479 N->getOperand(i).getValueType() != MVT::Other) {
480 ++NumExtraMaxRegUsers;
481 }
Chris Lattner11333092005-01-11 03:11:44 +0000482 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000483
484 if (O.getOpcode() != ISD::TokenFactor)
485 Result = MaxRegUse+NumExtraMaxRegUsers;
486 else
Chris Lattner869e0432005-01-17 23:02:13 +0000487 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000488 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000489
Chris Lattner837caa72005-01-11 23:21:30 +0000490 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000491 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000492}
493
Chris Lattner98a8ba02005-01-18 01:06:26 +0000494X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
495 X86AddressMode Result;
496
497 // If we need to emit two register operands, emit the one with the highest
498 // register pressure first.
499 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
500 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
501 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
502 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
503 Result.IndexReg = SelectExpr(IAM.IndexReg);
504 } else {
505 Result.IndexReg = SelectExpr(IAM.IndexReg);
506 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
507 }
508 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
509 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
510 } else if (IAM.IndexReg.Val) {
511 Result.IndexReg = SelectExpr(IAM.IndexReg);
512 }
513
514 switch (IAM.BaseType) {
515 case X86ISelAddressMode::RegBase:
516 Result.BaseType = X86AddressMode::RegBase;
517 break;
518 case X86ISelAddressMode::FrameIndexBase:
519 Result.BaseType = X86AddressMode::FrameIndexBase;
520 Result.Base.FrameIndex = IAM.Base.FrameIndex;
521 break;
522 default:
523 assert(0 && "Unknown base type!");
524 break;
525 }
526 Result.Scale = IAM.Scale;
527 Result.Disp = IAM.Disp;
528 Result.GV = IAM.GV;
529 return Result;
530}
531
532/// SelectAddress - Pattern match the maximal addressing mode for this node and
533/// emit all of the leaf registers.
534void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
535 X86ISelAddressMode IAM;
536 MatchAddress(N, IAM);
537 AM = SelectAddrExprs(IAM);
538}
539
540/// MatchAddress - Add the specified node to the specified addressing mode,
541/// returning true if it cannot be done. This just pattern matches for the
542/// addressing mode, it does not cause any code to be emitted. For that, use
543/// SelectAddress.
544bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545 switch (N.getOpcode()) {
546 default: break;
547 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000548 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
549 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000550 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
551 return false;
552 }
553 break;
554 case ISD::GlobalAddress:
555 if (AM.GV == 0) {
556 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
557 return false;
558 }
559 break;
560 case ISD::Constant:
561 AM.Disp += cast<ConstantSDNode>(N)->getValue();
562 return false;
563 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000564 // We might have folded the load into this shift, so don't regen the value
565 // if so.
566 if (ExprMap.count(N)) break;
567
Chris Lattner98a8ba02005-01-18 01:06:26 +0000568 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000569 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
570 unsigned Val = CN->getValue();
571 if (Val == 1 || Val == 2 || Val == 3) {
572 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000573 SDOperand ShVal = N.Val->getOperand(0);
574
575 // Okay, we know that we have a scale by now. However, if the scaled
576 // value is an add of something and a constant, we can fold the
577 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000578 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000579 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000580 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000581 ConstantSDNode *AddVal =
582 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
583 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000584 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000585 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000586 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000587 return false;
588 }
589 }
590 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000591 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000592 // We might have folded the load into this mul, so don't regen the value if
593 // so.
594 if (ExprMap.count(N)) break;
595
Chris Lattner947d5442005-01-11 19:37:02 +0000596 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000597 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
598 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000599 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
600 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
601 AM.Scale = unsigned(CN->getValue())-1;
602
603 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000604 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000605
606 // Okay, we know that we have a scale by now. However, if the scaled
607 // value is an add of something and a constant, we can fold the
608 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000609 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000610 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000611 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000612 ConstantSDNode *AddVal =
613 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
614 AM.Disp += AddVal->getValue() * CN->getValue();
615 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000616 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000617 }
618
619 AM.IndexReg = AM.Base.Reg = Reg;
620 return false;
621 }
622 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000623
624 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000625 // We might have folded the load into this mul, so don't regen the value if
626 // so.
627 if (ExprMap.count(N)) break;
628
Chris Lattner98a8ba02005-01-18 01:06:26 +0000629 X86ISelAddressMode Backup = AM;
630 if (!MatchAddress(N.Val->getOperand(0), AM) &&
631 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000632 return false;
633 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000634 if (!MatchAddress(N.Val->getOperand(1), AM) &&
635 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000636 return false;
637 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000638 break;
639 }
640 }
641
Chris Lattnera95589b2005-01-11 04:40:19 +0000642 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000643 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000644 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 if (AM.IndexReg.Val == 0) {
646 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000647 AM.Scale = 1;
648 return false;
649 }
650
651 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000652 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000653 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000654
655 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000656 AM.BaseType = X86ISelAddressMode::RegBase;
657 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000658 return false;
659}
660
661/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
662/// assuming that the temporary registers are in the 8-bit register class.
663///
664/// Tmp1 = setcc1
665/// Tmp2 = setcc2
666/// DestReg = logicalop Tmp1, Tmp2
667///
668static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
669 unsigned SetCC2, unsigned LogicalOp,
670 unsigned DestReg) {
671 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
672 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
673 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
674 BuildMI(BB, SetCC1, 0, Tmp1);
675 BuildMI(BB, SetCC2, 0, Tmp2);
676 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
677}
678
679/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
680/// condition codes match the specified SetCCOpcode. Note that some conditions
681/// require multiple instructions to generate the correct value.
682static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
683 ISD::CondCode SetCCOpcode, bool isFP) {
684 unsigned Opc;
685 if (!isFP) {
686 switch (SetCCOpcode) {
687 default: assert(0 && "Illegal integer SetCC!");
688 case ISD::SETEQ: Opc = X86::SETEr; break;
689 case ISD::SETGT: Opc = X86::SETGr; break;
690 case ISD::SETGE: Opc = X86::SETGEr; break;
691 case ISD::SETLT: Opc = X86::SETLr; break;
692 case ISD::SETLE: Opc = X86::SETLEr; break;
693 case ISD::SETNE: Opc = X86::SETNEr; break;
694 case ISD::SETULT: Opc = X86::SETBr; break;
695 case ISD::SETUGT: Opc = X86::SETAr; break;
696 case ISD::SETULE: Opc = X86::SETBEr; break;
697 case ISD::SETUGE: Opc = X86::SETAEr; break;
698 }
699 } else {
700 // On a floating point condition, the flags are set as follows:
701 // ZF PF CF op
702 // 0 | 0 | 0 | X > Y
703 // 0 | 0 | 1 | X < Y
704 // 1 | 0 | 0 | X == Y
705 // 1 | 1 | 1 | unordered
706 //
707 switch (SetCCOpcode) {
708 default: assert(0 && "Invalid FP setcc!");
709 case ISD::SETUEQ:
710 case ISD::SETEQ:
711 Opc = X86::SETEr; // True if ZF = 1
712 break;
713 case ISD::SETOGT:
714 case ISD::SETGT:
715 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
716 break;
717 case ISD::SETOGE:
718 case ISD::SETGE:
719 Opc = X86::SETAEr; // True if CF = 0
720 break;
721 case ISD::SETULT:
722 case ISD::SETLT:
723 Opc = X86::SETBr; // True if CF = 1
724 break;
725 case ISD::SETULE:
726 case ISD::SETLE:
727 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
728 break;
729 case ISD::SETONE:
730 case ISD::SETNE:
731 Opc = X86::SETNEr; // True if ZF = 0
732 break;
733 case ISD::SETUO:
734 Opc = X86::SETPr; // True if PF = 1
735 break;
736 case ISD::SETO:
737 Opc = X86::SETNPr; // True if PF = 0
738 break;
739 case ISD::SETOEQ: // !PF & ZF
740 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
741 return;
742 case ISD::SETOLT: // !PF & CF
743 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
744 return;
745 case ISD::SETOLE: // !PF & (CF || ZF)
746 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
747 return;
748 case ISD::SETUGT: // PF | (!ZF & !CF)
749 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
750 return;
751 case ISD::SETUGE: // PF | !CF
752 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
753 return;
754 case ISD::SETUNE: // PF | !ZF
755 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
756 return;
757 }
758 }
759 BuildMI(BB, Opc, 0, DestReg);
760}
761
762
763/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
764/// the Dest block if the Cond condition is true. If we cannot fold this
765/// condition into the branch, return true.
766///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000767bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
768 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000769 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
770 // B) using two conditional branches instead of one condbr, two setcc's, and
771 // an or.
772 if ((Cond.getOpcode() == ISD::OR ||
773 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
774 // And and or set the flags for us, so there is no need to emit a TST of the
775 // result. It is only safe to do this if there is only a single use of the
776 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000777 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000778 SelectExpr(Cond);
779 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
780 return false;
781 }
782
783 // Codegen br not C -> JE.
784 if (Cond.getOpcode() == ISD::XOR)
785 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
786 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000787 unsigned CondR;
788 if (getRegPressure(Chain) > getRegPressure(Cond)) {
789 Select(Chain);
790 CondR = SelectExpr(Cond.Val->getOperand(0));
791 } else {
792 CondR = SelectExpr(Cond.Val->getOperand(0));
793 Select(Chain);
794 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000795 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
796 BuildMI(BB, X86::JE, 1).addMBB(Dest);
797 return false;
798 }
799
800 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
801 if (SetCC == 0)
802 return true; // Can only handle simple setcc's so far.
803
804 unsigned Opc;
805
806 // Handle integer conditions first.
807 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
808 switch (SetCC->getCondition()) {
809 default: assert(0 && "Illegal integer SetCC!");
810 case ISD::SETEQ: Opc = X86::JE; break;
811 case ISD::SETGT: Opc = X86::JG; break;
812 case ISD::SETGE: Opc = X86::JGE; break;
813 case ISD::SETLT: Opc = X86::JL; break;
814 case ISD::SETLE: Opc = X86::JLE; break;
815 case ISD::SETNE: Opc = X86::JNE; break;
816 case ISD::SETULT: Opc = X86::JB; break;
817 case ISD::SETUGT: Opc = X86::JA; break;
818 case ISD::SETULE: Opc = X86::JBE; break;
819 case ISD::SETUGE: Opc = X86::JAE; break;
820 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000821 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000822 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000823 BuildMI(BB, Opc, 1).addMBB(Dest);
824 return false;
825 }
826
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000827 unsigned Opc2 = 0; // Second branch if needed.
828
829 // On a floating point condition, the flags are set as follows:
830 // ZF PF CF op
831 // 0 | 0 | 0 | X > Y
832 // 0 | 0 | 1 | X < Y
833 // 1 | 0 | 0 | X == Y
834 // 1 | 1 | 1 | unordered
835 //
836 switch (SetCC->getCondition()) {
837 default: assert(0 && "Invalid FP setcc!");
838 case ISD::SETUEQ:
839 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
840 case ISD::SETOGT:
841 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
842 case ISD::SETOGE:
843 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
844 case ISD::SETULT:
845 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
846 case ISD::SETULE:
847 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
848 case ISD::SETONE:
849 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
850 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
851 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
852 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
853 Opc = X86::JA; // ZF = 0 & CF = 0
854 Opc2 = X86::JP; // PF = 1
855 break;
856 case ISD::SETUGE: // PF = 1 | CF = 0
857 Opc = X86::JAE; // CF = 0
858 Opc2 = X86::JP; // PF = 1
859 break;
860 case ISD::SETUNE: // PF = 1 | ZF = 0
861 Opc = X86::JNE; // ZF = 0
862 Opc2 = X86::JP; // PF = 1
863 break;
864 case ISD::SETOEQ: // PF = 0 & ZF = 1
865 //X86::JNP, X86::JE
866 //X86::AND8rr
867 return true; // FIXME: Emit more efficient code for this branch.
868 case ISD::SETOLT: // PF = 0 & CF = 1
869 //X86::JNP, X86::JB
870 //X86::AND8rr
871 return true; // FIXME: Emit more efficient code for this branch.
872 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
873 //X86::JNP, X86::JBE
874 //X86::AND8rr
875 return true; // FIXME: Emit more efficient code for this branch.
876 }
877
Chris Lattner6c07aee2005-01-11 04:06:27 +0000878 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000879 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000880 BuildMI(BB, Opc, 1).addMBB(Dest);
881 if (Opc2)
882 BuildMI(BB, Opc2, 1).addMBB(Dest);
883 return false;
884}
885
Chris Lattner24aad1b2005-01-10 22:10:13 +0000886/// EmitSelectCC - Emit code into BB that performs a select operation between
887/// the two registers RTrue and RFalse, generating a result into RDest. Return
888/// true if the fold cannot be performed.
889///
890void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
891 unsigned RTrue, unsigned RFalse, unsigned RDest) {
892 enum Condition {
893 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
894 NOT_SET
895 } CondCode = NOT_SET;
896
897 static const unsigned CMOVTAB16[] = {
898 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
899 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
900 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
901 };
902 static const unsigned CMOVTAB32[] = {
903 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
904 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
905 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
906 };
907 static const unsigned CMOVTABFP[] = {
908 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
909 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
910 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
911 };
912
913 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
914 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
915 switch (SetCC->getCondition()) {
916 default: assert(0 && "Unknown integer comparison!");
917 case ISD::SETEQ: CondCode = EQ; break;
918 case ISD::SETGT: CondCode = GT; break;
919 case ISD::SETGE: CondCode = GE; break;
920 case ISD::SETLT: CondCode = LT; break;
921 case ISD::SETLE: CondCode = LE; break;
922 case ISD::SETNE: CondCode = NE; break;
923 case ISD::SETULT: CondCode = B; break;
924 case ISD::SETUGT: CondCode = A; break;
925 case ISD::SETULE: CondCode = BE; break;
926 case ISD::SETUGE: CondCode = AE; break;
927 }
928 } else {
929 // On a floating point condition, the flags are set as follows:
930 // ZF PF CF op
931 // 0 | 0 | 0 | X > Y
932 // 0 | 0 | 1 | X < Y
933 // 1 | 0 | 0 | X == Y
934 // 1 | 1 | 1 | unordered
935 //
936 switch (SetCC->getCondition()) {
937 default: assert(0 && "Unknown FP comparison!");
938 case ISD::SETUEQ:
939 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
940 case ISD::SETOGT:
941 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
942 case ISD::SETOGE:
943 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
944 case ISD::SETULT:
945 case ISD::SETLT: CondCode = B; break; // True if CF = 1
946 case ISD::SETULE:
947 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
948 case ISD::SETONE:
949 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
950 case ISD::SETUO: CondCode = P; break; // True if PF = 1
951 case ISD::SETO: CondCode = NP; break; // True if PF = 0
952 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
953 case ISD::SETUGE: // PF = 1 | CF = 0
954 case ISD::SETUNE: // PF = 1 | ZF = 0
955 case ISD::SETOEQ: // PF = 0 & ZF = 1
956 case ISD::SETOLT: // PF = 0 & CF = 1
957 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
958 // We cannot emit this comparison as a single cmov.
959 break;
960 }
961 }
962 }
963
964 unsigned Opc = 0;
965 if (CondCode != NOT_SET) {
966 switch (SVT) {
967 default: assert(0 && "Cannot select this type!");
968 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
969 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
970 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000971 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000972 }
973 }
974
975 // Finally, if we weren't able to fold this, just emit the condition and test
976 // it.
977 if (CondCode == NOT_SET || Opc == 0) {
978 // Get the condition into the zero flag.
979 unsigned CondReg = SelectExpr(Cond);
980 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
981
982 switch (SVT) {
983 default: assert(0 && "Cannot select this type!");
984 case MVT::i16: Opc = X86::CMOVE16rr; break;
985 case MVT::i32: Opc = X86::CMOVE32rr; break;
986 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000987 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000988 }
989 } else {
990 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000991 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000992 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000993 }
994 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
995}
996
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000997void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000998 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000999 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1000 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001001 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001002 switch (RHS.getValueType()) {
1003 default: break;
1004 case MVT::i1:
1005 case MVT::i8: Opc = X86::CMP8mi; break;
1006 case MVT::i16: Opc = X86::CMP16mi; break;
1007 case MVT::i32: Opc = X86::CMP32mi; break;
1008 }
1009 if (Opc) {
1010 X86AddressMode AM;
1011 EmitFoldedLoad(LHS, AM);
1012 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1013 return;
1014 }
1015 }
1016
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001017 switch (RHS.getValueType()) {
1018 default: break;
1019 case MVT::i1:
1020 case MVT::i8: Opc = X86::CMP8ri; break;
1021 case MVT::i16: Opc = X86::CMP16ri; break;
1022 case MVT::i32: Opc = X86::CMP32ri; break;
1023 }
1024 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001025 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001026 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1027 return;
1028 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001029 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1030 if (CN->isExactlyValue(+0.0) ||
1031 CN->isExactlyValue(-0.0)) {
1032 unsigned Reg = SelectExpr(LHS);
1033 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1034 BuildMI(BB, X86::FNSTSW8r, 0);
1035 BuildMI(BB, X86::SAHF, 1);
1036 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001037 }
1038
Chris Lattneref6806c2005-01-12 02:02:48 +00001039 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001040 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001041 switch (RHS.getValueType()) {
1042 default: break;
1043 case MVT::i1:
1044 case MVT::i8: Opc = X86::CMP8mr; break;
1045 case MVT::i16: Opc = X86::CMP16mr; break;
1046 case MVT::i32: Opc = X86::CMP32mr; break;
1047 }
1048 if (Opc) {
1049 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001050 EmitFoldedLoad(LHS, AM);
1051 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001052 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1053 return;
1054 }
1055 }
1056
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001057 switch (LHS.getValueType()) {
1058 default: assert(0 && "Cannot compare this value!");
1059 case MVT::i1:
1060 case MVT::i8: Opc = X86::CMP8rr; break;
1061 case MVT::i16: Opc = X86::CMP16rr; break;
1062 case MVT::i32: Opc = X86::CMP32rr; break;
1063 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001064 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001065 }
Chris Lattner11333092005-01-11 03:11:44 +00001066 unsigned Tmp1, Tmp2;
1067 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1068 Tmp1 = SelectExpr(LHS);
1069 Tmp2 = SelectExpr(RHS);
1070 } else {
1071 Tmp2 = SelectExpr(RHS);
1072 Tmp1 = SelectExpr(LHS);
1073 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001074 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1075}
1076
Chris Lattner4ff348b2005-01-17 06:26:58 +00001077/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1078/// The DAG cannot have cycles in it, by definition, so the visited set is not
1079/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1080/// reuse, so it prevents exponential cases.
1081///
1082static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1083 std::set<SDNode*> &Visited) {
1084 if (N == Op) return true; // Found it.
1085 SDNode *Node = N.Val;
1086 if (Node->getNumOperands() == 0) return false; // Leaf?
1087 if (!Visited.insert(Node).second) return false; // Already visited?
1088
1089 // Recurse for the first N-1 operands.
1090 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1091 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1092 return true;
1093
1094 // Tail recurse for the last operand.
1095 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1096}
1097
Chris Lattnera5ade062005-01-11 21:19:59 +00001098/// isFoldableLoad - Return true if this is a load instruction that can safely
1099/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001100bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001101 if (Op.getOpcode() != ISD::LOAD ||
1102 // FIXME: currently can't fold constant pool indexes.
1103 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1104 return false;
1105
1106 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001107 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1108 if (ExprMap.count(Op.getValue(1))) return false;
1109 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1110 assert(!LoweredTokens.count(Op.getValue(1)) &&
1111 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001112
Chris Lattner4ff348b2005-01-17 06:26:58 +00001113 // If there is not just one use of its value, we cannot fold.
1114 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1115
1116 // Finally, we cannot fold the load into the operation if this would induce a
1117 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1118 // operand of the operation we are folding the load into) can possible use the
1119 // chain node defined by the load.
1120 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1121 std::set<SDNode*> Visited;
1122 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1123 return false;
1124 }
1125 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001126}
1127
Chris Lattner4ff348b2005-01-17 06:26:58 +00001128
Chris Lattnera5ade062005-01-11 21:19:59 +00001129/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1130/// and compute the address being loaded into AM.
1131void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1132 SDOperand Chain = Op.getOperand(0);
1133 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001134
Chris Lattnera5ade062005-01-11 21:19:59 +00001135 if (getRegPressure(Chain) > getRegPressure(Address)) {
1136 Select(Chain);
1137 SelectAddress(Address, AM);
1138 } else {
1139 SelectAddress(Address, AM);
1140 Select(Chain);
1141 }
1142
1143 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001144 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1145 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001146 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001147 if (!LoweredTokens.insert(Op.getValue(1)).second)
1148 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001149}
1150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001151unsigned ISel::SelectExpr(SDOperand N) {
1152 unsigned Result;
1153 unsigned Tmp1, Tmp2, Tmp3;
1154 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001155 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001156 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001157
Chris Lattner7f2afac2005-01-14 22:37:41 +00001158 if (Node->getOpcode() == ISD::CopyFromReg) {
1159 // FIXME: Handle copy from physregs!
1160
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001161 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001162 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001163 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001164
1165 unsigned &Reg = ExprMap[N];
1166 if (Reg) return Reg;
1167
1168 if (N.getOpcode() != ISD::CALL)
1169 Reg = Result = (N.getValueType() != MVT::Other) ?
1170 MakeReg(N.getValueType()) : 1;
1171 else {
1172 // If this is a call instruction, make sure to prepare ALL of the result
1173 // values as well as the chain.
1174 if (Node->getNumValues() == 1)
1175 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001176 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001177 Result = MakeReg(Node->getValueType(0));
1178 ExprMap[N.getValue(0)] = Result;
1179 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1180 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1181 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001182 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001183 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001184
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001185 switch (N.getOpcode()) {
1186 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001187 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001188 assert(0 && "Node not handled!\n");
1189 case ISD::FrameIndex:
1190 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1191 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1192 return Result;
1193 case ISD::ConstantPool:
1194 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1195 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1196 return Result;
1197 case ISD::ConstantFP:
1198 ContainsFPCode = true;
1199 Tmp1 = Result; // Intermediate Register
1200 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1201 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1202 Tmp1 = MakeReg(MVT::f64);
1203
1204 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1205 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1206 BuildMI(BB, X86::FLD0, 0, Tmp1);
1207 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1208 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1209 BuildMI(BB, X86::FLD1, 0, Tmp1);
1210 else
1211 assert(0 && "Unexpected constant!");
1212 if (Tmp1 != Result)
1213 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1214 return Result;
1215 case ISD::Constant:
1216 switch (N.getValueType()) {
1217 default: assert(0 && "Cannot use constants of this type!");
1218 case MVT::i1:
1219 case MVT::i8: Opc = X86::MOV8ri; break;
1220 case MVT::i16: Opc = X86::MOV16ri; break;
1221 case MVT::i32: Opc = X86::MOV32ri; break;
1222 }
1223 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1224 return Result;
1225 case ISD::GlobalAddress: {
1226 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1227 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1228 return Result;
1229 }
1230 case ISD::ExternalSymbol: {
1231 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1232 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1233 return Result;
1234 }
1235 case ISD::FP_EXTEND:
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001238 return Result;
1239 case ISD::ZERO_EXTEND: {
1240 int DestIs16 = N.getValueType() == MVT::i16;
1241 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001242
1243 // FIXME: This hack is here for zero extension casts from bool to i8. This
1244 // would not be needed if bools were promoted by Legalize.
1245 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001246 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001247 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1248 return Result;
1249 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001250
Chris Lattner4ff348b2005-01-17 06:26:58 +00001251 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001252 static const unsigned Opc[3] = {
1253 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1254 };
1255
1256 X86AddressMode AM;
1257 EmitFoldedLoad(N.getOperand(0), AM);
1258 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1259
1260 return Result;
1261 }
1262
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001263 static const unsigned Opc[3] = {
1264 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1265 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001266 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001267 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1268 return Result;
1269 }
1270 case ISD::SIGN_EXTEND: {
1271 int DestIs16 = N.getValueType() == MVT::i16;
1272 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1273
Chris Lattner590d8002005-01-09 18:52:44 +00001274 // FIXME: Legalize should promote bools to i8!
1275 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1276 "Sign extend from bool not implemented!");
1277
Chris Lattner4ff348b2005-01-17 06:26:58 +00001278 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001279 static const unsigned Opc[3] = {
1280 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1281 };
1282
1283 X86AddressMode AM;
1284 EmitFoldedLoad(N.getOperand(0), AM);
1285 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1286 return Result;
1287 }
1288
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001289 static const unsigned Opc[3] = {
1290 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1291 };
1292 Tmp1 = SelectExpr(N.getOperand(0));
1293 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1294 return Result;
1295 }
1296 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001297 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001298 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001299 switch (N.getValueType()) {
1300 default: assert(0 && "Unknown truncate!");
1301 case MVT::i1:
1302 case MVT::i8: Opc = X86::MOV8rm; break;
1303 case MVT::i16: Opc = X86::MOV16rm; break;
1304 }
1305 X86AddressMode AM;
1306 EmitFoldedLoad(N.getOperand(0), AM);
1307 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1308 return Result;
1309 }
1310
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001311 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1312 // a move out of AX or AL.
1313 switch (N.getOperand(0).getValueType()) {
1314 default: assert(0 && "Unknown truncate!");
1315 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1316 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1317 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1318 }
1319 Tmp1 = SelectExpr(N.getOperand(0));
1320 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1321
1322 switch (N.getValueType()) {
1323 default: assert(0 && "Unknown truncate!");
1324 case MVT::i1:
1325 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1326 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1327 }
1328 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1329 return Result;
1330
1331 case ISD::FP_ROUND:
1332 // Truncate from double to float by storing to memory as float,
1333 // then reading it back into a register.
1334
1335 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001336 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001337 Tmp1 = TLI.getTargetData().getFloatAlignment();
1338 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1339
1340 // Codegen the input.
1341 Tmp1 = SelectExpr(N.getOperand(0));
1342
1343 // Emit the store, then the reload.
1344 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1345 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001346 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001347
1348 case ISD::SINT_TO_FP:
1349 case ISD::UINT_TO_FP: {
1350 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001351 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001352
1353 // Promote the integer to a type supported by FLD. We do this because there
1354 // are no unsigned FLD instructions, so we must promote an unsigned value to
1355 // a larger signed value, then use FLD on the larger value.
1356 //
1357 MVT::ValueType PromoteType = MVT::Other;
1358 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1359 unsigned PromoteOpcode = 0;
1360 unsigned RealDestReg = Result;
1361 switch (SrcTy) {
1362 case MVT::i1:
1363 case MVT::i8:
1364 // We don't have the facilities for directly loading byte sized data from
1365 // memory (even signed). Promote it to 16 bits.
1366 PromoteType = MVT::i16;
1367 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1368 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1369 break;
1370 case MVT::i16:
1371 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1372 PromoteType = MVT::i32;
1373 PromoteOpcode = X86::MOVZX32rr16;
1374 }
1375 break;
1376 default:
1377 // Don't fild into the real destination.
1378 if (Node->getOpcode() == ISD::UINT_TO_FP)
1379 Result = MakeReg(Node->getValueType(0));
1380 break;
1381 }
1382
1383 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1384
1385 if (PromoteType != MVT::Other) {
1386 Tmp2 = MakeReg(PromoteType);
1387 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1388 SrcTy = PromoteType;
1389 Tmp1 = Tmp2;
1390 }
1391
1392 // Spill the integer to memory and reload it from there.
1393 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1394 MachineFunction *F = BB->getParent();
1395 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1396
1397 switch (SrcTy) {
1398 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001399 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001400 // FIXME: this won't work for cast [u]long to FP
1401 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1402 FrameIdx).addReg(Tmp1);
1403 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1404 FrameIdx, 4).addReg(Tmp1+1);
1405 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1406 break;
1407 case MVT::i32:
1408 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1409 FrameIdx).addReg(Tmp1);
1410 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1411 break;
1412 case MVT::i16:
1413 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1414 FrameIdx).addReg(Tmp1);
1415 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1416 break;
1417 default: break; // No promotion required.
1418 }
1419
Chris Lattner085c9952005-01-12 04:00:00 +00001420 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001421 // If this is a cast from uint -> double, we need to be careful when if
1422 // the "sign" bit is set. If so, we don't want to make a negative number,
1423 // we want to make a positive number. Emit code to add an offset if the
1424 // sign bit is set.
1425
1426 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1427 unsigned IsNeg = MakeReg(MVT::i32);
1428 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1429
1430 // Create a CP value that has the offset in one word and 0 in the other.
1431 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1432 0x4f80000000000000ULL);
1433 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1434 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1435 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1436
1437 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1438 // We need special handling for unsigned 64-bit integer sources. If the
1439 // input number has the "sign bit" set, then we loaded it incorrectly as a
1440 // negative 64-bit number. In this case, add an offset value.
1441
1442 // Emit a test instruction to see if the dynamic input value was signed.
1443 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1444
1445 // If the sign bit is set, get a pointer to an offset, otherwise get a
1446 // pointer to a zero.
1447 MachineConstantPool *CP = F->getConstantPool();
1448 unsigned Zero = MakeReg(MVT::i32);
1449 Constant *Null = Constant::getNullValue(Type::UIntTy);
1450 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1451 CP->getConstantPoolIndex(Null));
1452 unsigned Offset = MakeReg(MVT::i32);
1453 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1454
1455 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1456 CP->getConstantPoolIndex(OffsetCst));
1457 unsigned Addr = MakeReg(MVT::i32);
1458 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1459
1460 // Load the constant for an add. FIXME: this could make an 'fadd' that
1461 // reads directly from memory, but we don't support these yet.
1462 unsigned ConstReg = MakeReg(MVT::f64);
1463 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1464
1465 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1466 }
1467 return RealDestReg;
1468 }
1469 case ISD::FP_TO_SINT:
1470 case ISD::FP_TO_UINT: {
1471 // FIXME: Most of this grunt work should be done by legalize!
1472 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1473
1474 // Change the floating point control register to use "round towards zero"
1475 // mode when truncating to an integer value.
1476 //
1477 MachineFunction *F = BB->getParent();
1478 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1479 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1480
1481 // Load the old value of the high byte of the control word...
1482 unsigned HighPartOfCW = MakeReg(MVT::i8);
1483 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1484 CWFrameIdx, 1);
1485
1486 // Set the high part to be round to zero...
1487 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1488 CWFrameIdx, 1).addImm(12);
1489
1490 // Reload the modified control word now...
1491 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1492
1493 // Restore the memory image of control word to original value
1494 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1495 CWFrameIdx, 1).addReg(HighPartOfCW);
1496
1497 // We don't have the facilities for directly storing byte sized data to
1498 // memory. Promote it to 16 bits. We also must promote unsigned values to
1499 // larger classes because we only have signed FP stores.
1500 MVT::ValueType StoreClass = Node->getValueType(0);
1501 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1502 switch (StoreClass) {
1503 case MVT::i8: StoreClass = MVT::i16; break;
1504 case MVT::i16: StoreClass = MVT::i32; break;
1505 case MVT::i32: StoreClass = MVT::i64; break;
1506 // The following treatment of cLong may not be perfectly right,
1507 // but it survives chains of casts of the form
1508 // double->ulong->double.
1509 case MVT::i64: StoreClass = MVT::i64; break;
1510 default: assert(0 && "Unknown store class!");
1511 }
1512
1513 // Spill the integer to memory and reload it from there.
1514 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1515 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1516
1517 switch (StoreClass) {
1518 default: assert(0 && "Unknown store class!");
1519 case MVT::i16:
1520 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1521 break;
1522 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001523 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001524 break;
1525 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001526 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001527 break;
1528 }
1529
1530 switch (Node->getValueType(0)) {
1531 default:
1532 assert(0 && "Unknown integer type!");
1533 case MVT::i64:
1534 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001535 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001536 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1537 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1538 case MVT::i32:
1539 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1540 break;
1541 case MVT::i16:
1542 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1543 break;
1544 case MVT::i8:
1545 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1546 break;
1547 }
1548
1549 // Reload the original control word now.
1550 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1551 return Result;
1552 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001553 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001554 Op0 = N.getOperand(0);
1555 Op1 = N.getOperand(1);
1556
Chris Lattner4ff348b2005-01-17 06:26:58 +00001557 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001558 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001559 goto FoldAdd;
1560 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001561
Chris Lattner4ff348b2005-01-17 06:26:58 +00001562 if (isFoldableLoad(Op1, Op0)) {
1563 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001564 switch (N.getValueType()) {
1565 default: assert(0 && "Cannot add this type!");
1566 case MVT::i1:
1567 case MVT::i8: Opc = X86::ADD8rm; break;
1568 case MVT::i16: Opc = X86::ADD16rm; break;
1569 case MVT::i32: Opc = X86::ADD32rm; break;
1570 case MVT::f32: Opc = X86::FADD32m; break;
1571 case MVT::f64: Opc = X86::FADD64m; break;
1572 }
1573 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001574 EmitFoldedLoad(Op1, AM);
1575 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001576 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1577 return Result;
1578 }
1579
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001580 // See if we can codegen this as an LEA to fold operations together.
1581 if (N.getValueType() == MVT::i32) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001582 X86ISelAddressMode AM;
1583 if (!MatchAddress(Op0, AM) && !MatchAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001584 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001585 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001586 // leave this as LEA, then peephole it to 'ADD' after two address elim
1587 // happens.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001588 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1589 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1590 X86AddressMode XAM = SelectAddrExprs(AM);
1591 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001592 return Result;
1593 }
1594 }
1595 }
Chris Lattner11333092005-01-11 03:11:44 +00001596
Chris Lattnera5ade062005-01-11 21:19:59 +00001597 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001598 Opc = 0;
1599 if (CN->getValue() == 1) { // add X, 1 -> inc X
1600 switch (N.getValueType()) {
1601 default: assert(0 && "Cannot integer add this type!");
1602 case MVT::i8: Opc = X86::INC8r; break;
1603 case MVT::i16: Opc = X86::INC16r; break;
1604 case MVT::i32: Opc = X86::INC32r; break;
1605 }
1606 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1607 switch (N.getValueType()) {
1608 default: assert(0 && "Cannot integer add this type!");
1609 case MVT::i8: Opc = X86::DEC8r; break;
1610 case MVT::i16: Opc = X86::DEC16r; break;
1611 case MVT::i32: Opc = X86::DEC32r; break;
1612 }
1613 }
1614
1615 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001616 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001617 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1618 return Result;
1619 }
1620
1621 switch (N.getValueType()) {
1622 default: assert(0 && "Cannot add this type!");
1623 case MVT::i8: Opc = X86::ADD8ri; break;
1624 case MVT::i16: Opc = X86::ADD16ri; break;
1625 case MVT::i32: Opc = X86::ADD32ri; break;
1626 }
1627 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001628 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001629 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1630 return Result;
1631 }
1632 }
1633
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001634 switch (N.getValueType()) {
1635 default: assert(0 && "Cannot add this type!");
1636 case MVT::i8: Opc = X86::ADD8rr; break;
1637 case MVT::i16: Opc = X86::ADD16rr; break;
1638 case MVT::i32: Opc = X86::ADD32rr; break;
1639 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001640 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001641 }
Chris Lattner11333092005-01-11 03:11:44 +00001642
Chris Lattnera5ade062005-01-11 21:19:59 +00001643 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1644 Tmp1 = SelectExpr(Op0);
1645 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001646 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001647 Tmp2 = SelectExpr(Op1);
1648 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001649 }
1650
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001651 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1652 return Result;
1653 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001654 case ISD::MUL:
1655 case ISD::AND:
1656 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001657 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001658 static const unsigned SUBTab[] = {
1659 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1660 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1661 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1662 };
1663 static const unsigned MULTab[] = {
1664 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1665 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1666 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1667 };
1668 static const unsigned ANDTab[] = {
1669 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1670 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1671 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1672 };
1673 static const unsigned ORTab[] = {
1674 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1675 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1676 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1677 };
1678 static const unsigned XORTab[] = {
1679 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1680 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1681 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1682 };
1683
1684 Op0 = Node->getOperand(0);
1685 Op1 = Node->getOperand(1);
1686
1687 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001688 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1689 if (CN->isNullValue()) { // 0 - N -> neg N
1690 switch (N.getValueType()) {
1691 default: assert(0 && "Cannot sub this type!");
1692 case MVT::i1:
1693 case MVT::i8: Opc = X86::NEG8r; break;
1694 case MVT::i16: Opc = X86::NEG16r; break;
1695 case MVT::i32: Opc = X86::NEG32r; break;
1696 }
1697 Tmp1 = SelectExpr(N.getOperand(1));
1698 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1699 return Result;
1700 }
1701
Chris Lattnera5ade062005-01-11 21:19:59 +00001702 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1703 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001704 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001705 switch (N.getValueType()) {
1706 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001707 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001708 case MVT::i8: Opc = X86::NOT8r; break;
1709 case MVT::i16: Opc = X86::NOT16r; break;
1710 case MVT::i32: Opc = X86::NOT32r; break;
1711 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001712 if (Opc) {
1713 Tmp1 = SelectExpr(Op0);
1714 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1715 return Result;
1716 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001717 }
1718
Chris Lattner2a4e5082005-01-17 06:48:02 +00001719 // Fold common multiplies into LEA instructions.
1720 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1721 switch ((int)CN->getValue()) {
1722 default: break;
1723 case 3:
1724 case 5:
1725 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001726 // Remove N from exprmap so SelectAddress doesn't get confused.
1727 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001728 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001729 SelectAddress(N, AM);
1730 // Restore it to the map.
1731 ExprMap[N] = Result;
1732 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1733 return Result;
1734 }
1735 }
1736
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001737 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001738 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001739 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001740 case MVT::i8: Opc = 0; break;
1741 case MVT::i16: Opc = 1; break;
1742 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001743 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001744 switch (Node->getOpcode()) {
1745 default: assert(0 && "Unreachable!");
1746 case ISD::SUB: Opc = SUBTab[Opc]; break;
1747 case ISD::MUL: Opc = MULTab[Opc]; break;
1748 case ISD::AND: Opc = ANDTab[Opc]; break;
1749 case ISD::OR: Opc = ORTab[Opc]; break;
1750 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001751 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001752 if (Opc) { // Can't fold MUL:i8 R, imm
1753 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001754 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1755 return Result;
1756 }
1757 }
Chris Lattner11333092005-01-11 03:11:44 +00001758
Chris Lattner4ff348b2005-01-17 06:26:58 +00001759 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001760 if (Node->getOpcode() != ISD::SUB) {
1761 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001762 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001763 } else {
1764 // Emit 'reverse' subract, with a memory operand.
1765 switch (N.getValueType()) {
1766 default: Opc = 0; break;
1767 case MVT::f32: Opc = X86::FSUBR32m; break;
1768 case MVT::f64: Opc = X86::FSUBR64m; break;
1769 }
1770 if (Opc) {
1771 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001772 EmitFoldedLoad(Op0, AM);
1773 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001774 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1775 return Result;
1776 }
1777 }
1778
Chris Lattner4ff348b2005-01-17 06:26:58 +00001779 if (isFoldableLoad(Op1, Op0)) {
1780 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001781 switch (N.getValueType()) {
1782 default: assert(0 && "Cannot operate on this type!");
1783 case MVT::i1:
1784 case MVT::i8: Opc = 5; break;
1785 case MVT::i16: Opc = 6; break;
1786 case MVT::i32: Opc = 7; break;
1787 case MVT::f32: Opc = 8; break;
1788 case MVT::f64: Opc = 9; break;
1789 }
1790 switch (Node->getOpcode()) {
1791 default: assert(0 && "Unreachable!");
1792 case ISD::SUB: Opc = SUBTab[Opc]; break;
1793 case ISD::MUL: Opc = MULTab[Opc]; break;
1794 case ISD::AND: Opc = ANDTab[Opc]; break;
1795 case ISD::OR: Opc = ORTab[Opc]; break;
1796 case ISD::XOR: Opc = XORTab[Opc]; break;
1797 }
1798
1799 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001800 EmitFoldedLoad(Op1, AM);
1801 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001802 if (Opc) {
1803 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1804 } else {
1805 assert(Node->getOpcode() == ISD::MUL &&
1806 N.getValueType() == MVT::i8 && "Unexpected situation!");
1807 // Must use the MUL instruction, which forces use of AL.
1808 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1809 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1810 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1811 }
1812 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001813 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001814
1815 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1816 Tmp1 = SelectExpr(Op0);
1817 Tmp2 = SelectExpr(Op1);
1818 } else {
1819 Tmp2 = SelectExpr(Op1);
1820 Tmp1 = SelectExpr(Op0);
1821 }
1822
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001823 switch (N.getValueType()) {
1824 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001825 case MVT::i1:
1826 case MVT::i8: Opc = 10; break;
1827 case MVT::i16: Opc = 11; break;
1828 case MVT::i32: Opc = 12; break;
1829 case MVT::f32: Opc = 13; break;
1830 case MVT::f64: Opc = 14; break;
1831 }
1832 switch (Node->getOpcode()) {
1833 default: assert(0 && "Unreachable!");
1834 case ISD::SUB: Opc = SUBTab[Opc]; break;
1835 case ISD::MUL: Opc = MULTab[Opc]; break;
1836 case ISD::AND: Opc = ANDTab[Opc]; break;
1837 case ISD::OR: Opc = ORTab[Opc]; break;
1838 case ISD::XOR: Opc = XORTab[Opc]; break;
1839 }
1840 if (Opc) {
1841 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 } else {
1843 assert(Node->getOpcode() == ISD::MUL &&
1844 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001845 // Must use the MUL instruction, which forces use of AL.
1846 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1847 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1848 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001849 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001850 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001851 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001852 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001853 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1854 Tmp2 = SelectExpr(N.getOperand(1));
1855 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001856 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001857 Tmp3 = SelectExpr(N.getOperand(2));
1858 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001859 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001860 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1861 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001862
1863 case ISD::SDIV:
1864 case ISD::UDIV:
1865 case ISD::SREM:
1866 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001867 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1868 "We don't support this operator!");
1869
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001870 if (N.getOpcode() == ISD::SDIV)
1871 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1872 // FIXME: These special cases should be handled by the lowering impl!
1873 unsigned RHS = CN->getValue();
1874 bool isNeg = false;
1875 if ((int)RHS < 0) {
1876 isNeg = true;
1877 RHS = -RHS;
1878 }
1879 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1880 unsigned Log = log2(RHS);
1881 unsigned TmpReg = MakeReg(N.getValueType());
1882 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1883 switch (N.getValueType()) {
1884 default: assert("Unknown type to signed divide!");
1885 case MVT::i8:
1886 SAROpc = X86::SAR8ri;
1887 SHROpc = X86::SHR8ri;
1888 ADDOpc = X86::ADD8rr;
1889 NEGOpc = X86::NEG8r;
1890 break;
1891 case MVT::i16:
1892 SAROpc = X86::SAR16ri;
1893 SHROpc = X86::SHR16ri;
1894 ADDOpc = X86::ADD16rr;
1895 NEGOpc = X86::NEG16r;
1896 break;
1897 case MVT::i32:
1898 SAROpc = X86::SAR32ri;
1899 SHROpc = X86::SHR32ri;
1900 ADDOpc = X86::ADD32rr;
1901 NEGOpc = X86::NEG32r;
1902 break;
1903 }
Chris Lattner11333092005-01-11 03:11:44 +00001904 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1906 unsigned TmpReg2 = MakeReg(N.getValueType());
1907 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1908 unsigned TmpReg3 = MakeReg(N.getValueType());
1909 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1910
1911 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1912 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1913 if (isNeg)
1914 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1915 return Result;
1916 }
1917 }
1918
Chris Lattner11333092005-01-11 03:11:44 +00001919 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1920 Tmp1 = SelectExpr(N.getOperand(0));
1921 Tmp2 = SelectExpr(N.getOperand(1));
1922 } else {
1923 Tmp2 = SelectExpr(N.getOperand(1));
1924 Tmp1 = SelectExpr(N.getOperand(0));
1925 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001926
1927 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1928 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1929 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1930 switch (N.getValueType()) {
1931 default: assert(0 && "Cannot sdiv this type!");
1932 case MVT::i8:
1933 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1934 LoReg = X86::AL;
1935 HiReg = X86::AH;
1936 MovOpcode = X86::MOV8rr;
1937 ClrOpcode = X86::MOV8ri;
1938 SExtOpcode = X86::CBW;
1939 break;
1940 case MVT::i16:
1941 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1942 LoReg = X86::AX;
1943 HiReg = X86::DX;
1944 MovOpcode = X86::MOV16rr;
1945 ClrOpcode = X86::MOV16ri;
1946 SExtOpcode = X86::CWD;
1947 break;
1948 case MVT::i32:
1949 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001950 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001951 HiReg = X86::EDX;
1952 MovOpcode = X86::MOV32rr;
1953 ClrOpcode = X86::MOV32ri;
1954 SExtOpcode = X86::CDQ;
1955 break;
1956 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1957 case MVT::f32:
1958 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001959 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001960 return Result;
1961 }
1962
1963 // Set up the low part.
1964 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1965
1966 if (isSigned) {
1967 // Sign extend the low part into the high part.
1968 BuildMI(BB, SExtOpcode, 0);
1969 } else {
1970 // Zero out the high part, effectively zero extending the input.
1971 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1972 }
1973
1974 // Emit the DIV/IDIV instruction.
1975 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1976
1977 // Get the result of the divide or rem.
1978 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1979 return Result;
1980 }
1981
1982 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001983 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001984 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1985 switch (N.getValueType()) {
1986 default: assert(0 && "Cannot shift this type!");
1987 case MVT::i8: Opc = X86::ADD8rr; break;
1988 case MVT::i16: Opc = X86::ADD16rr; break;
1989 case MVT::i32: Opc = X86::ADD32rr; break;
1990 }
1991 Tmp1 = SelectExpr(N.getOperand(0));
1992 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1993 return Result;
1994 }
1995
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001996 switch (N.getValueType()) {
1997 default: assert(0 && "Cannot shift this type!");
1998 case MVT::i8: Opc = X86::SHL8ri; break;
1999 case MVT::i16: Opc = X86::SHL16ri; break;
2000 case MVT::i32: Opc = X86::SHL32ri; break;
2001 }
Chris Lattner11333092005-01-11 03:11:44 +00002002 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002003 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2004 return Result;
2005 }
Chris Lattner11333092005-01-11 03:11:44 +00002006
2007 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2008 Tmp1 = SelectExpr(N.getOperand(0));
2009 Tmp2 = SelectExpr(N.getOperand(1));
2010 } else {
2011 Tmp2 = SelectExpr(N.getOperand(1));
2012 Tmp1 = SelectExpr(N.getOperand(0));
2013 }
2014
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 switch (N.getValueType()) {
2016 default: assert(0 && "Cannot shift this type!");
2017 case MVT::i8 : Opc = X86::SHL8rCL; break;
2018 case MVT::i16: Opc = X86::SHL16rCL; break;
2019 case MVT::i32: Opc = X86::SHL32rCL; break;
2020 }
2021 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2022 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2023 return Result;
2024 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002025 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2026 switch (N.getValueType()) {
2027 default: assert(0 && "Cannot shift this type!");
2028 case MVT::i8: Opc = X86::SHR8ri; break;
2029 case MVT::i16: Opc = X86::SHR16ri; break;
2030 case MVT::i32: Opc = X86::SHR32ri; break;
2031 }
Chris Lattner11333092005-01-11 03:11:44 +00002032 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002033 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2034 return Result;
2035 }
Chris Lattner11333092005-01-11 03:11:44 +00002036
2037 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2038 Tmp1 = SelectExpr(N.getOperand(0));
2039 Tmp2 = SelectExpr(N.getOperand(1));
2040 } else {
2041 Tmp2 = SelectExpr(N.getOperand(1));
2042 Tmp1 = SelectExpr(N.getOperand(0));
2043 }
2044
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002045 switch (N.getValueType()) {
2046 default: assert(0 && "Cannot shift this type!");
2047 case MVT::i8 : Opc = X86::SHR8rCL; break;
2048 case MVT::i16: Opc = X86::SHR16rCL; break;
2049 case MVT::i32: Opc = X86::SHR32rCL; break;
2050 }
2051 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2052 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2053 return Result;
2054 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002055 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2056 switch (N.getValueType()) {
2057 default: assert(0 && "Cannot shift this type!");
2058 case MVT::i8: Opc = X86::SAR8ri; break;
2059 case MVT::i16: Opc = X86::SAR16ri; break;
2060 case MVT::i32: Opc = X86::SAR32ri; break;
2061 }
Chris Lattner11333092005-01-11 03:11:44 +00002062 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002063 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2064 return Result;
2065 }
Chris Lattner11333092005-01-11 03:11:44 +00002066
2067 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2068 Tmp1 = SelectExpr(N.getOperand(0));
2069 Tmp2 = SelectExpr(N.getOperand(1));
2070 } else {
2071 Tmp2 = SelectExpr(N.getOperand(1));
2072 Tmp1 = SelectExpr(N.getOperand(0));
2073 }
2074
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002075 switch (N.getValueType()) {
2076 default: assert(0 && "Cannot shift this type!");
2077 case MVT::i8 : Opc = X86::SAR8rCL; break;
2078 case MVT::i16: Opc = X86::SAR16rCL; break;
2079 case MVT::i32: Opc = X86::SAR32rCL; break;
2080 }
2081 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2082 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2083 return Result;
2084
2085 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002086 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002087 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2088 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2089 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002090 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002091 // Make sure we generate both values.
2092 if (Result != 1)
2093 ExprMap[N.getValue(1)] = 1; // Generate the token
2094 else
2095 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2096
Chris Lattner5188ad72005-01-08 19:28:19 +00002097 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002098 default: assert(0 && "Cannot load this type!");
2099 case MVT::i1:
2100 case MVT::i8: Opc = X86::MOV8rm; break;
2101 case MVT::i16: Opc = X86::MOV16rm; break;
2102 case MVT::i32: Opc = X86::MOV32rm; break;
2103 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
2104 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2105 }
Chris Lattner11333092005-01-11 03:11:44 +00002106
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002107 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002108 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2110 } else {
2111 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002112
2113 SDOperand Chain = N.getOperand(0);
2114 SDOperand Address = N.getOperand(1);
2115 if (getRegPressure(Chain) > getRegPressure(Address)) {
2116 Select(Chain);
2117 SelectAddress(Address, AM);
2118 } else {
2119 SelectAddress(Address, AM);
2120 Select(Chain);
2121 }
2122
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002123 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2124 }
2125 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002126
2127 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2128 case ISD::ZEXTLOAD: {
2129 // Make sure we generate both values.
2130 if (Result != 1)
2131 ExprMap[N.getValue(1)] = 1; // Generate the token
2132 else
2133 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2134
Chris Lattnerda2ce112005-01-16 07:34:08 +00002135 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2136 if (Node->getValueType(0) == MVT::f64) {
2137 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2138 "Bad EXTLOAD!");
2139 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2140 CP->getIndex());
2141 return Result;
2142 }
2143
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002144 X86AddressMode AM;
2145 if (getRegPressure(Node->getOperand(0)) >
2146 getRegPressure(Node->getOperand(1))) {
2147 Select(Node->getOperand(0)); // chain
2148 SelectAddress(Node->getOperand(1), AM);
2149 } else {
2150 SelectAddress(Node->getOperand(1), AM);
2151 Select(Node->getOperand(0)); // chain
2152 }
2153
2154 switch (Node->getValueType(0)) {
2155 default: assert(0 && "Unknown type to sign extend to.");
2156 case MVT::f64:
2157 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2158 "Bad EXTLOAD!");
2159 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2160 break;
2161 case MVT::i32:
2162 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2163 default:
2164 assert(0 && "Bad zero extend!");
2165 case MVT::i1:
2166 case MVT::i8:
2167 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2168 break;
2169 case MVT::i16:
2170 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2171 break;
2172 }
2173 break;
2174 case MVT::i16:
2175 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2176 "Bad zero extend!");
2177 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2178 break;
2179 case MVT::i8:
2180 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2181 "Bad zero extend!");
2182 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2183 break;
2184 }
2185 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002186 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002187 case ISD::SEXTLOAD: {
2188 // Make sure we generate both values.
2189 if (Result != 1)
2190 ExprMap[N.getValue(1)] = 1; // Generate the token
2191 else
2192 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2193
2194 X86AddressMode AM;
2195 if (getRegPressure(Node->getOperand(0)) >
2196 getRegPressure(Node->getOperand(1))) {
2197 Select(Node->getOperand(0)); // chain
2198 SelectAddress(Node->getOperand(1), AM);
2199 } else {
2200 SelectAddress(Node->getOperand(1), AM);
2201 Select(Node->getOperand(0)); // chain
2202 }
2203
2204 switch (Node->getValueType(0)) {
2205 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2206 default: assert(0 && "Unknown type to sign extend to.");
2207 case MVT::i32:
2208 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2209 default:
2210 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2211 case MVT::i8:
2212 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2213 break;
2214 case MVT::i16:
2215 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2216 break;
2217 }
2218 break;
2219 case MVT::i16:
2220 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2221 "Cannot sign extend from bool!");
2222 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2223 break;
2224 }
2225 return Result;
2226 }
2227
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002228 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002229 // Generate both result values.
2230 if (Result != 1)
2231 ExprMap[N.getValue(1)] = 1; // Generate the token
2232 else
2233 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2234
2235 // FIXME: We are currently ignoring the requested alignment for handling
2236 // greater than the stack alignment. This will need to be revisited at some
2237 // point. Align = N.getOperand(2);
2238
2239 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2240 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2241 std::cerr << "Cannot allocate stack object with greater alignment than"
2242 << " the stack alignment yet!";
2243 abort();
2244 }
2245
2246 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002247 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002248 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2249 .addImm(CN->getValue());
2250 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002251 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2252 Select(N.getOperand(0));
2253 Tmp1 = SelectExpr(N.getOperand(1));
2254 } else {
2255 Tmp1 = SelectExpr(N.getOperand(1));
2256 Select(N.getOperand(0));
2257 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002258
2259 // Subtract size from stack pointer, thereby allocating some space.
2260 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2261 }
2262
2263 // Put a pointer to the space into the result register, by copying the stack
2264 // pointer.
2265 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2266 return Result;
2267
2268 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002269 // The chain for this call is now lowered.
2270 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2271
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002272 if (GlobalAddressSDNode *GASD =
2273 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002274 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002275 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2276 } else if (ExternalSymbolSDNode *ESSDN =
2277 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002278 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 BuildMI(BB, X86::CALLpcrel32,
2280 1).addExternalSymbol(ESSDN->getSymbol(), true);
2281 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002282 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2283 Select(N.getOperand(0));
2284 Tmp1 = SelectExpr(N.getOperand(1));
2285 } else {
2286 Tmp1 = SelectExpr(N.getOperand(1));
2287 Select(N.getOperand(0));
2288 }
2289
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002290 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2291 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002292 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002293 default: assert(0 && "Unknown value type for call result!");
2294 case MVT::Other: return 1;
2295 case MVT::i1:
2296 case MVT::i8:
2297 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2298 break;
2299 case MVT::i16:
2300 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2301 break;
2302 case MVT::i32:
2303 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002304 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002305 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2306 break;
2307 case MVT::f32:
2308 case MVT::f64: // Floating-point return values live in %ST(0)
2309 ContainsFPCode = true;
2310 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2311 break;
2312 }
2313 return Result+N.ResNo;
2314 }
2315
2316 return 0;
2317}
2318
Chris Lattnere10269b2005-01-17 19:25:26 +00002319/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2320/// load/op/store instruction. If successful return true.
2321bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2322 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2323 SDOperand Chain = Node->getOperand(0);
2324 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002325 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002326
2327 // The chain has to be a load, the stored value must be an integer binary
2328 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002329 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002330 MVT::isFloatingPoint(StVal.getValueType()))
2331 return false;
2332
Chris Lattner5c659812005-01-17 22:10:42 +00002333 // Token chain must either be a factor node or the load to fold.
2334 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2335 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002336
Chris Lattner5c659812005-01-17 22:10:42 +00002337 SDOperand TheLoad;
2338
2339 // Check to see if there is a load from the same pointer that we're storing
2340 // to in either operand of the binop.
2341 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2342 StVal.getOperand(0).getOperand(1) == StPtr)
2343 TheLoad = StVal.getOperand(0);
2344 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2345 StVal.getOperand(1).getOperand(1) == StPtr)
2346 TheLoad = StVal.getOperand(1);
2347 else
2348 return false; // No matching load operand.
2349
2350 // We can only fold the load if there are no intervening side-effecting
2351 // operations. This means that the store uses the load as its token chain, or
2352 // there are only token factor nodes in between the store and load.
2353 if (Chain != TheLoad.getValue(1)) {
2354 // Okay, the other option is that we have a store referring to (possibly
2355 // nested) token factor nodes. For now, just try peeking through one level
2356 // of token factors to see if this is the case.
2357 bool ChainOk = false;
2358 if (Chain.getOpcode() == ISD::TokenFactor) {
2359 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2360 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2361 ChainOk = true;
2362 break;
2363 }
2364 }
2365
2366 if (!ChainOk) return false;
2367 }
2368
2369 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002370 return false;
2371
2372 // Make sure that one of the operands of the binop is the load, and that the
2373 // load folds into the binop.
2374 if (((StVal.getOperand(0) != TheLoad ||
2375 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2376 (StVal.getOperand(1) != TheLoad ||
2377 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2378 return false;
2379
2380 // Finally, check to see if this is one of the ops we can handle!
2381 static const unsigned ADDTAB[] = {
2382 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2383 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2384 };
2385 static const unsigned SUBTAB[] = {
2386 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2387 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2388 };
2389 static const unsigned ANDTAB[] = {
2390 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2391 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2392 };
2393 static const unsigned ORTAB[] = {
2394 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2395 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2396 };
2397 static const unsigned XORTAB[] = {
2398 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2399 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2400 };
2401 static const unsigned SHLTAB[] = {
2402 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2403 /*Have to put the reg in CL*/0, 0, 0,
2404 };
2405 static const unsigned SARTAB[] = {
2406 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2407 /*Have to put the reg in CL*/0, 0, 0,
2408 };
2409 static const unsigned SHRTAB[] = {
2410 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2411 /*Have to put the reg in CL*/0, 0, 0,
2412 };
2413
2414 const unsigned *TabPtr = 0;
2415 switch (StVal.getOpcode()) {
2416 default:
2417 std::cerr << "CANNOT [mem] op= val: ";
2418 StVal.Val->dump(); std::cerr << "\n";
2419 case ISD::MUL:
2420 case ISD::SDIV:
2421 case ISD::UDIV:
2422 case ISD::SREM:
2423 case ISD::UREM: return false;
2424
2425 case ISD::ADD: TabPtr = ADDTAB; break;
2426 case ISD::SUB: TabPtr = SUBTAB; break;
2427 case ISD::AND: TabPtr = ANDTAB; break;
2428 case ISD:: OR: TabPtr = ORTAB; break;
2429 case ISD::XOR: TabPtr = XORTAB; break;
2430 case ISD::SHL: TabPtr = SHLTAB; break;
2431 case ISD::SRA: TabPtr = SARTAB; break;
2432 case ISD::SRL: TabPtr = SHRTAB; break;
2433 }
2434
2435 // Handle: [mem] op= CST
2436 SDOperand Op0 = StVal.getOperand(0);
2437 SDOperand Op1 = StVal.getOperand(1);
2438 unsigned Opc;
2439 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2440 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2441 default: break;
2442 case MVT::i1:
2443 case MVT::i8: Opc = TabPtr[0]; break;
2444 case MVT::i16: Opc = TabPtr[1]; break;
2445 case MVT::i32: Opc = TabPtr[2]; break;
2446 }
2447
2448 if (Opc) {
Chris Lattner5c659812005-01-17 22:10:42 +00002449 LoweredTokens.insert(TheLoad.getValue(1));
2450 Select(Chain);
2451
Chris Lattnere10269b2005-01-17 19:25:26 +00002452 X86AddressMode AM;
2453 if (getRegPressure(TheLoad.getOperand(0)) >
2454 getRegPressure(TheLoad.getOperand(1))) {
2455 Select(TheLoad.getOperand(0));
2456 SelectAddress(TheLoad.getOperand(1), AM);
2457 } else {
2458 SelectAddress(TheLoad.getOperand(1), AM);
2459 Select(TheLoad.getOperand(0));
2460 }
Chris Lattner5c659812005-01-17 22:10:42 +00002461
2462 if (StVal.getOpcode() == ISD::ADD) {
2463 if (CN->getValue() == 1) {
2464 switch (Op0.getValueType()) {
2465 default: break;
2466 case MVT::i8:
2467 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2468 return true;
2469 case MVT::i16: Opc = TabPtr[1];
2470 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2471 return true;
2472 case MVT::i32: Opc = TabPtr[2];
2473 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2474 return true;
2475 }
2476 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2477 switch (Op0.getValueType()) {
2478 default: break;
2479 case MVT::i8:
2480 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2481 return true;
2482 case MVT::i16: Opc = TabPtr[1];
2483 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2484 return true;
2485 case MVT::i32: Opc = TabPtr[2];
2486 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2487 return true;
2488 }
2489 }
2490 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002491
2492 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2493 return true;
2494 }
2495 }
2496
2497 // If we have [mem] = V op [mem], try to turn it into:
2498 // [mem] = [mem] op V.
2499 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2500 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2501 StVal.getOpcode() != ISD::SRL)
2502 std::swap(Op0, Op1);
2503
2504 if (Op0 != TheLoad) return false;
2505
2506 switch (Op0.getValueType()) {
2507 default: return false;
2508 case MVT::i1:
2509 case MVT::i8: Opc = TabPtr[3]; break;
2510 case MVT::i16: Opc = TabPtr[4]; break;
2511 case MVT::i32: Opc = TabPtr[5]; break;
2512 }
Chris Lattner5c659812005-01-17 22:10:42 +00002513
2514 LoweredTokens.insert(TheLoad.getValue(1));
2515 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002516 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002517
Chris Lattnere10269b2005-01-17 19:25:26 +00002518 X86AddressMode AM;
2519 SelectAddress(TheLoad.getOperand(1), AM);
2520 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002521 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002522 return true;
2523}
2524
2525
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002526void ISel::Select(SDOperand N) {
2527 unsigned Tmp1, Tmp2, Opc;
2528
2529 // FIXME: Disable for our current expansion model!
2530 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2531 return; // Already selected.
2532
Chris Lattner989de032005-01-11 06:14:36 +00002533 SDNode *Node = N.Val;
2534
2535 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002536 default:
Chris Lattner989de032005-01-11 06:14:36 +00002537 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002538 assert(0 && "Node not handled yet!");
2539 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002540 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002541 if (Node->getNumOperands() == 2) {
2542 bool OneFirst =
2543 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2544 Select(Node->getOperand(OneFirst));
2545 Select(Node->getOperand(!OneFirst));
2546 } else {
2547 std::vector<std::pair<unsigned, unsigned> > OpsP;
2548 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2549 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2550 std::sort(OpsP.begin(), OpsP.end());
2551 std::reverse(OpsP.begin(), OpsP.end());
2552 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2553 Select(Node->getOperand(OpsP[i].second));
2554 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002555 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002556 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002557 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2558 Select(N.getOperand(0));
2559 Tmp1 = SelectExpr(N.getOperand(1));
2560 } else {
2561 Tmp1 = SelectExpr(N.getOperand(1));
2562 Select(N.getOperand(0));
2563 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002564 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002565
2566 if (Tmp1 != Tmp2) {
2567 switch (N.getOperand(1).getValueType()) {
2568 default: assert(0 && "Invalid type for operation!");
2569 case MVT::i1:
2570 case MVT::i8: Opc = X86::MOV8rr; break;
2571 case MVT::i16: Opc = X86::MOV16rr; break;
2572 case MVT::i32: Opc = X86::MOV32rr; break;
2573 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002574 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002575 }
2576 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2577 }
2578 return;
2579 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002580 switch (N.getNumOperands()) {
2581 default:
2582 assert(0 && "Unknown return instruction!");
2583 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002584 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2585 N.getOperand(2).getValueType() == MVT::i32 &&
2586 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002587 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2588 Tmp1 = SelectExpr(N.getOperand(1));
2589 Tmp2 = SelectExpr(N.getOperand(2));
2590 } else {
2591 Tmp2 = SelectExpr(N.getOperand(2));
2592 Tmp1 = SelectExpr(N.getOperand(1));
2593 }
2594 Select(N.getOperand(0));
2595
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002596 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2597 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2598 // Declare that EAX & EDX are live on exit.
2599 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2600 .addReg(X86::ESP);
2601 break;
2602 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002603 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2604 Select(N.getOperand(0));
2605 Tmp1 = SelectExpr(N.getOperand(1));
2606 } else {
2607 Tmp1 = SelectExpr(N.getOperand(1));
2608 Select(N.getOperand(0));
2609 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002610 switch (N.getOperand(1).getValueType()) {
2611 default: assert(0 && "All other types should have been promoted!!");
2612 case MVT::f64:
2613 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2614 // Declare that top-of-stack is live on exit
2615 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2616 break;
2617 case MVT::i32:
2618 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2619 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2620 break;
2621 }
2622 break;
2623 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002624 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002625 break;
2626 }
2627 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2628 return;
2629 case ISD::BR: {
2630 Select(N.getOperand(0));
2631 MachineBasicBlock *Dest =
2632 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2633 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2634 return;
2635 }
2636
2637 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002638 MachineBasicBlock *Dest =
2639 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002640
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002641 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2642 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002643 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2644 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2645 Select(N.getOperand(0));
2646 Tmp1 = SelectExpr(N.getOperand(1));
2647 } else {
2648 Tmp1 = SelectExpr(N.getOperand(1));
2649 Select(N.getOperand(0));
2650 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002651 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2652 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2653 }
Chris Lattner11333092005-01-11 03:11:44 +00002654
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002655 return;
2656 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002657
Chris Lattner4df0de92005-01-17 00:00:33 +00002658 case ISD::LOAD:
2659 // If this load could be folded into the only using instruction, and if it
2660 // is safe to emit the instruction here, try to do so now.
2661 if (Node->hasNUsesOfValue(1, 0)) {
2662 SDOperand TheVal = N.getValue(0);
2663 SDNode *User = 0;
2664 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2665 assert(UI != Node->use_end() && "Didn't find use!");
2666 SDNode *UN = *UI;
2667 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2668 if (UN->getOperand(i) == TheVal) {
2669 User = UN;
2670 goto FoundIt;
2671 }
2672 }
2673 FoundIt:
2674 // Only handle unary operators right now.
2675 if (User->getNumOperands() == 1) {
2676 LoweredTokens.erase(N);
2677 SelectExpr(SDOperand(User, 0));
2678 return;
2679 }
2680 }
2681 SelectExpr(N);
2682 return;
2683
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002684 case ISD::EXTLOAD:
2685 case ISD::SEXTLOAD:
2686 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002687 case ISD::CALL:
2688 case ISD::DYNAMIC_STACKALLOC:
2689 SelectExpr(N);
2690 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002691
2692 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2693 // On X86, we can represent all types except for Bool and Float natively.
2694 X86AddressMode AM;
2695 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002696 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2697 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2698 && "Unsupported TRUNCSTORE for this target!");
2699
2700 if (StoredTy == MVT::i16) {
2701 // FIXME: This is here just to allow testing. X86 doesn't really have a
2702 // TRUNCSTORE i16 operation, but this is required for targets that do not
2703 // have 16-bit integer registers. We occasionally disable 16-bit integer
2704 // registers to test the promotion code.
2705 Select(N.getOperand(0));
2706 Tmp1 = SelectExpr(N.getOperand(1));
2707 SelectAddress(N.getOperand(2), AM);
2708
2709 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2710 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2711 return;
2712 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002713
2714 // Store of constant bool?
2715 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2716 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2717 Select(N.getOperand(0));
2718 SelectAddress(N.getOperand(2), AM);
2719 } else {
2720 SelectAddress(N.getOperand(2), AM);
2721 Select(N.getOperand(0));
2722 }
2723 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2724 return;
2725 }
2726
2727 switch (StoredTy) {
2728 default: assert(0 && "Cannot truncstore this type!");
2729 case MVT::i1: Opc = X86::MOV8mr; break;
2730 case MVT::f32: Opc = X86::FST32m; break;
2731 }
2732
2733 std::vector<std::pair<unsigned, unsigned> > RP;
2734 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2735 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2736 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2737 std::sort(RP.begin(), RP.end());
2738
2739 for (unsigned i = 0; i != 3; ++i)
2740 switch (RP[2-i].second) {
2741 default: assert(0 && "Unknown operand number!");
2742 case 0: Select(N.getOperand(0)); break;
2743 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2744 case 2: SelectAddress(N.getOperand(2), AM); break;
2745 }
2746
2747 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2748 return;
2749 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002750 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002751 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002752
2753 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2754 Opc = 0;
2755 switch (CN->getValueType(0)) {
2756 default: assert(0 && "Invalid type for operation!");
2757 case MVT::i1:
2758 case MVT::i8: Opc = X86::MOV8mi; break;
2759 case MVT::i16: Opc = X86::MOV16mi; break;
2760 case MVT::i32: Opc = X86::MOV32mi; break;
2761 case MVT::f32:
2762 case MVT::f64: break;
2763 }
2764 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002765 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2766 Select(N.getOperand(0));
2767 SelectAddress(N.getOperand(2), AM);
2768 } else {
2769 SelectAddress(N.getOperand(2), AM);
2770 Select(N.getOperand(0));
2771 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002772 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2773 return;
2774 }
2775 }
Chris Lattner837caa72005-01-11 23:21:30 +00002776
2777 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002778 if (TryToFoldLoadOpStore(Node))
2779 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002780
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002781 switch (N.getOperand(1).getValueType()) {
2782 default: assert(0 && "Cannot store this type!");
2783 case MVT::i1:
2784 case MVT::i8: Opc = X86::MOV8mr; break;
2785 case MVT::i16: Opc = X86::MOV16mr; break;
2786 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002787 case MVT::f32: Opc = X86::FST32m; break;
2788 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002789 }
Chris Lattner11333092005-01-11 03:11:44 +00002790
2791 std::vector<std::pair<unsigned, unsigned> > RP;
2792 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2793 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2794 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2795 std::sort(RP.begin(), RP.end());
2796
2797 for (unsigned i = 0; i != 3; ++i)
2798 switch (RP[2-i].second) {
2799 default: assert(0 && "Unknown operand number!");
2800 case 0: Select(N.getOperand(0)); break;
2801 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002802 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002803 }
2804
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002805 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2806 return;
2807 }
2808 case ISD::ADJCALLSTACKDOWN:
2809 case ISD::ADJCALLSTACKUP:
2810 Select(N.getOperand(0));
2811 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2812
2813 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2814 X86::ADJCALLSTACKUP;
2815 BuildMI(BB, Opc, 1).addImm(Tmp1);
2816 return;
Chris Lattner989de032005-01-11 06:14:36 +00002817 case ISD::MEMSET: {
2818 Select(N.getOperand(0)); // Select the chain.
2819 unsigned Align =
2820 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2821 if (Align == 0) Align = 1;
2822
2823 // Turn the byte code into # iterations
2824 unsigned CountReg;
2825 unsigned Opcode;
2826 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2827 unsigned Val = ValC->getValue() & 255;
2828
2829 // If the value is a constant, then we can potentially use larger sets.
2830 switch (Align & 3) {
2831 case 2: // WORD aligned
2832 CountReg = MakeReg(MVT::i32);
2833 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2834 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2835 } else {
2836 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2837 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2838 }
2839 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2840 Opcode = X86::REP_STOSW;
2841 break;
2842 case 0: // DWORD aligned
2843 CountReg = MakeReg(MVT::i32);
2844 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2845 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2846 } else {
2847 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2848 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2849 }
2850 Val = (Val << 8) | Val;
2851 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2852 Opcode = X86::REP_STOSD;
2853 break;
2854 default: // BYTE aligned
2855 CountReg = SelectExpr(Node->getOperand(3));
2856 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2857 Opcode = X86::REP_STOSB;
2858 break;
2859 }
2860 } else {
2861 // If it's not a constant value we are storing, just fall back. We could
2862 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2863 unsigned ValReg = SelectExpr(Node->getOperand(2));
2864 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2865 CountReg = SelectExpr(Node->getOperand(3));
2866 Opcode = X86::REP_STOSB;
2867 }
2868
2869 // No matter what the alignment is, we put the source in ESI, the
2870 // destination in EDI, and the count in ECX.
2871 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2872 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2873 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2874 BuildMI(BB, Opcode, 0);
2875 return;
2876 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002877 case ISD::MEMCPY:
2878 Select(N.getOperand(0)); // Select the chain.
2879 unsigned Align =
2880 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2881 if (Align == 0) Align = 1;
2882
2883 // Turn the byte code into # iterations
2884 unsigned CountReg;
2885 unsigned Opcode;
2886 switch (Align & 3) {
2887 case 2: // WORD aligned
2888 CountReg = MakeReg(MVT::i32);
2889 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2890 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2891 } else {
2892 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2893 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2894 }
2895 Opcode = X86::REP_MOVSW;
2896 break;
2897 case 0: // DWORD aligned
2898 CountReg = MakeReg(MVT::i32);
2899 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2900 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2901 } else {
2902 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2903 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2904 }
2905 Opcode = X86::REP_MOVSD;
2906 break;
2907 default: // BYTE aligned
2908 CountReg = SelectExpr(Node->getOperand(3));
2909 Opcode = X86::REP_MOVSB;
2910 break;
2911 }
2912
2913 // No matter what the alignment is, we put the source in ESI, the
2914 // destination in EDI, and the count in ECX.
2915 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2916 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2917 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2918 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2919 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2920 BuildMI(BB, Opcode, 0);
2921 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002922 }
2923 assert(0 && "Should not be reached!");
2924}
2925
2926
2927/// createX86PatternInstructionSelector - This pass converts an LLVM function
2928/// into a machine code representation using pattern matching and a machine
2929/// description file.
2930///
2931FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2932 return new ISel(TM);
2933}