blob: a75854e5592787c1953ec46fcf45f3c1523393c1 [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
303
304
305
306namespace {
307 Statistic<>
308 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
309
310 //===--------------------------------------------------------------------===//
311 /// ISel - X86 specific code to select X86 machine instructions for
312 /// SelectionDAG operations.
313 ///
314 class ISel : public SelectionDAGISel {
315 /// ContainsFPCode - Every instruction we select that uses or defines a FP
316 /// register should set this to true.
317 bool ContainsFPCode;
318
319 /// X86Lowering - This object fully describes how to lower LLVM code to an
320 /// X86-specific SelectionDAG.
321 X86TargetLowering X86Lowering;
322
Chris Lattner11333092005-01-11 03:11:44 +0000323 /// RegPressureMap - This keeps an approximate count of the number of
324 /// registers required to evaluate each node in the graph.
325 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000326
327 /// ExprMap - As shared expressions are codegen'd, we keep track of which
328 /// vreg the value is produced in, so we only emit one copy of each compiled
329 /// tree.
330 std::map<SDOperand, unsigned> ExprMap;
331 std::set<SDOperand> LoweredTokens;
332
333 public:
334 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
335 }
336
Chris Lattner11333092005-01-11 03:11:44 +0000337 unsigned getRegPressure(SDOperand O) {
338 return RegPressureMap[O.Val];
339 }
340 unsigned ComputeRegPressure(SDOperand O);
341
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000342 /// InstructionSelectBasicBlock - This callback is invoked by
343 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000344 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000345
Chris Lattner4ff348b2005-01-17 06:26:58 +0000346 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000347 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000348 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000349
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000350 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000351 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000352 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
353 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000354 unsigned SelectExpr(SDOperand N);
355 bool SelectAddress(SDOperand N, X86AddressMode &AM);
356 void Select(SDOperand N);
357 };
358}
359
Chris Lattner7dbcb752005-01-12 04:21:28 +0000360/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
361/// when it has created a SelectionDAG for us to codegen.
362void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
363 // While we're doing this, keep track of whether we see any FP code for
364 // FP_REG_KILL insertion.
365 ContainsFPCode = false;
366
367 // Scan the PHI nodes that already are inserted into this basic block. If any
368 // of them is a PHI of a floating point value, we need to insert an
369 // FP_REG_KILL.
370 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
371 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
372 I != E; ++I) {
373 assert(I->getOpcode() == X86::PHI &&
374 "Isn't just PHI nodes?");
375 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
376 X86::RFPRegisterClass) {
377 ContainsFPCode = true;
378 break;
379 }
380 }
381
382 // Compute the RegPressureMap, which is an approximation for the number of
383 // registers required to compute each node.
384 ComputeRegPressure(DAG.getRoot());
385
386 // Codegen the basic block.
387 Select(DAG.getRoot());
388
389 // Finally, look at all of the successors of this block. If any contain a PHI
390 // node of FP type, we need to insert an FP_REG_KILL in this block.
391 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
392 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
393 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
394 I != E && I->getOpcode() == X86::PHI; ++I) {
395 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
396 X86::RFPRegisterClass) {
397 ContainsFPCode = true;
398 break;
399 }
400 }
401
402 // Insert FP_REG_KILL instructions into basic blocks that need them. This
403 // only occurs due to the floating point stackifier not being aggressive
404 // enough to handle arbitrary global stackification.
405 //
406 // Currently we insert an FP_REG_KILL instruction into each block that uses or
407 // defines a floating point virtual register.
408 //
409 // When the global register allocators (like linear scan) finally update live
410 // variable analysis, we can keep floating point values in registers across
411 // basic blocks. This will be a huge win, but we are waiting on the global
412 // allocators before we can do this.
413 //
414 if (ContainsFPCode && BB->succ_size()) {
415 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
416 ++NumFPKill;
417 }
418
419 // Clear state used for selection.
420 ExprMap.clear();
421 LoweredTokens.clear();
422 RegPressureMap.clear();
423}
424
425
Chris Lattner11333092005-01-11 03:11:44 +0000426// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
427// for the number of registers required to compute each node. This is basically
428// computing a generalized form of the Sethi-Ullman number for each node.
429unsigned ISel::ComputeRegPressure(SDOperand O) {
430 SDNode *N = O.Val;
431 unsigned &Result = RegPressureMap[N];
432 if (Result) return Result;
433
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000434 // FIXME: Should operations like CALL (which clobber lots o regs) have a
435 // higher fixed cost??
436
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000437 if (N->getNumOperands() == 0) {
438 Result = 1;
439 } else {
440 unsigned MaxRegUse = 0;
441 unsigned NumExtraMaxRegUsers = 0;
442 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
443 unsigned Regs;
444 if (N->getOperand(i).getOpcode() == ISD::Constant)
445 Regs = 0;
446 else
447 Regs = ComputeRegPressure(N->getOperand(i));
448 if (Regs > MaxRegUse) {
449 MaxRegUse = Regs;
450 NumExtraMaxRegUsers = 0;
451 } else if (Regs == MaxRegUse &&
452 N->getOperand(i).getValueType() != MVT::Other) {
453 ++NumExtraMaxRegUsers;
454 }
Chris Lattner11333092005-01-11 03:11:44 +0000455 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000456
457 if (O.getOpcode() != ISD::TokenFactor)
458 Result = MaxRegUse+NumExtraMaxRegUsers;
459 else
460 Result = std::max(MaxRegUse-1, 1);
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000461 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000462
Chris Lattner837caa72005-01-11 23:21:30 +0000463 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000464 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000465}
466
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000467/// SelectAddress - Add the specified node to the specified addressing mode,
468/// returning true if it cannot be done.
469bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
470 switch (N.getOpcode()) {
471 default: break;
472 case ISD::FrameIndex:
473 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
474 AM.BaseType = X86AddressMode::FrameIndexBase;
475 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
476 return false;
477 }
478 break;
479 case ISD::GlobalAddress:
480 if (AM.GV == 0) {
481 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
482 return false;
483 }
484 break;
485 case ISD::Constant:
486 AM.Disp += cast<ConstantSDNode>(N)->getValue();
487 return false;
488 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000489 // We might have folded the load into this shift, so don't regen the value
490 // if so.
491 if (ExprMap.count(N)) break;
492
Chris Lattner2b937862005-01-12 07:33:20 +0000493 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000494 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
495 unsigned Val = CN->getValue();
496 if (Val == 1 || Val == 2 || Val == 3) {
497 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000498 SDOperand ShVal = N.Val->getOperand(0);
499
500 // Okay, we know that we have a scale by now. However, if the scaled
501 // value is an add of something and a constant, we can fold the
502 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000503 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000504 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
505 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
506 ConstantSDNode *AddVal =
507 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
508 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000509 } else {
Chris Lattner51a26342005-01-11 06:36:20 +0000510 AM.IndexReg = SelectExpr(ShVal);
511 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000512 return false;
513 }
514 }
515 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000516 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000517 // We might have folded the load into this mul, so don't regen the value if
518 // so.
519 if (ExprMap.count(N)) break;
520
Chris Lattner947d5442005-01-11 19:37:02 +0000521 // X*[3,5,9] -> X+X*[2,4,8]
522 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
523 AM.Base.Reg == 0)
524 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
525 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
526 AM.Scale = unsigned(CN->getValue())-1;
527
528 SDOperand MulVal = N.Val->getOperand(0);
529 unsigned Reg;
530
531 // Okay, we know that we have a scale by now. However, if the scaled
532 // value is an add of something and a constant, we can fold the
533 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000534 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000535 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
536 Reg = SelectExpr(MulVal.Val->getOperand(0));
537 ConstantSDNode *AddVal =
538 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
539 AM.Disp += AddVal->getValue() * CN->getValue();
540 } else {
541 Reg = SelectExpr(N.Val->getOperand(0));
542 }
543
544 AM.IndexReg = AM.Base.Reg = Reg;
545 return false;
546 }
547 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000548
549 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000550 // We might have folded the load into this mul, so don't regen the value if
551 // so.
552 if (ExprMap.count(N)) break;
553
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000554 X86AddressMode Backup = AM;
555 if (!SelectAddress(N.Val->getOperand(0), AM) &&
556 !SelectAddress(N.Val->getOperand(1), AM))
557 return false;
558 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000559 if (!SelectAddress(N.Val->getOperand(1), AM) &&
560 !SelectAddress(N.Val->getOperand(0), AM))
561 return false;
562 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000563 break;
564 }
565 }
566
Chris Lattnera95589b2005-01-11 04:40:19 +0000567 // Is the base register already occupied?
568 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
569 // If so, check to see if the scale index register is set.
570 if (AM.IndexReg == 0) {
571 AM.IndexReg = SelectExpr(N);
572 AM.Scale = 1;
573 return false;
574 }
575
576 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000577 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000578 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000579
580 // Default, generate it as a register.
581 AM.BaseType = X86AddressMode::RegBase;
582 AM.Base.Reg = SelectExpr(N);
583 return false;
584}
585
586/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
587/// assuming that the temporary registers are in the 8-bit register class.
588///
589/// Tmp1 = setcc1
590/// Tmp2 = setcc2
591/// DestReg = logicalop Tmp1, Tmp2
592///
593static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
594 unsigned SetCC2, unsigned LogicalOp,
595 unsigned DestReg) {
596 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
597 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
598 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
599 BuildMI(BB, SetCC1, 0, Tmp1);
600 BuildMI(BB, SetCC2, 0, Tmp2);
601 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
602}
603
604/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
605/// condition codes match the specified SetCCOpcode. Note that some conditions
606/// require multiple instructions to generate the correct value.
607static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
608 ISD::CondCode SetCCOpcode, bool isFP) {
609 unsigned Opc;
610 if (!isFP) {
611 switch (SetCCOpcode) {
612 default: assert(0 && "Illegal integer SetCC!");
613 case ISD::SETEQ: Opc = X86::SETEr; break;
614 case ISD::SETGT: Opc = X86::SETGr; break;
615 case ISD::SETGE: Opc = X86::SETGEr; break;
616 case ISD::SETLT: Opc = X86::SETLr; break;
617 case ISD::SETLE: Opc = X86::SETLEr; break;
618 case ISD::SETNE: Opc = X86::SETNEr; break;
619 case ISD::SETULT: Opc = X86::SETBr; break;
620 case ISD::SETUGT: Opc = X86::SETAr; break;
621 case ISD::SETULE: Opc = X86::SETBEr; break;
622 case ISD::SETUGE: Opc = X86::SETAEr; break;
623 }
624 } else {
625 // On a floating point condition, the flags are set as follows:
626 // ZF PF CF op
627 // 0 | 0 | 0 | X > Y
628 // 0 | 0 | 1 | X < Y
629 // 1 | 0 | 0 | X == Y
630 // 1 | 1 | 1 | unordered
631 //
632 switch (SetCCOpcode) {
633 default: assert(0 && "Invalid FP setcc!");
634 case ISD::SETUEQ:
635 case ISD::SETEQ:
636 Opc = X86::SETEr; // True if ZF = 1
637 break;
638 case ISD::SETOGT:
639 case ISD::SETGT:
640 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
641 break;
642 case ISD::SETOGE:
643 case ISD::SETGE:
644 Opc = X86::SETAEr; // True if CF = 0
645 break;
646 case ISD::SETULT:
647 case ISD::SETLT:
648 Opc = X86::SETBr; // True if CF = 1
649 break;
650 case ISD::SETULE:
651 case ISD::SETLE:
652 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
653 break;
654 case ISD::SETONE:
655 case ISD::SETNE:
656 Opc = X86::SETNEr; // True if ZF = 0
657 break;
658 case ISD::SETUO:
659 Opc = X86::SETPr; // True if PF = 1
660 break;
661 case ISD::SETO:
662 Opc = X86::SETNPr; // True if PF = 0
663 break;
664 case ISD::SETOEQ: // !PF & ZF
665 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
666 return;
667 case ISD::SETOLT: // !PF & CF
668 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
669 return;
670 case ISD::SETOLE: // !PF & (CF || ZF)
671 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
672 return;
673 case ISD::SETUGT: // PF | (!ZF & !CF)
674 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
675 return;
676 case ISD::SETUGE: // PF | !CF
677 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
678 return;
679 case ISD::SETUNE: // PF | !ZF
680 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
681 return;
682 }
683 }
684 BuildMI(BB, Opc, 0, DestReg);
685}
686
687
688/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
689/// the Dest block if the Cond condition is true. If we cannot fold this
690/// condition into the branch, return true.
691///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000692bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
693 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000694 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
695 // B) using two conditional branches instead of one condbr, two setcc's, and
696 // an or.
697 if ((Cond.getOpcode() == ISD::OR ||
698 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
699 // And and or set the flags for us, so there is no need to emit a TST of the
700 // result. It is only safe to do this if there is only a single use of the
701 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000702 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000703 SelectExpr(Cond);
704 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
705 return false;
706 }
707
708 // Codegen br not C -> JE.
709 if (Cond.getOpcode() == ISD::XOR)
710 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
711 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000712 unsigned CondR;
713 if (getRegPressure(Chain) > getRegPressure(Cond)) {
714 Select(Chain);
715 CondR = SelectExpr(Cond.Val->getOperand(0));
716 } else {
717 CondR = SelectExpr(Cond.Val->getOperand(0));
718 Select(Chain);
719 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000720 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
721 BuildMI(BB, X86::JE, 1).addMBB(Dest);
722 return false;
723 }
724
725 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
726 if (SetCC == 0)
727 return true; // Can only handle simple setcc's so far.
728
729 unsigned Opc;
730
731 // Handle integer conditions first.
732 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
733 switch (SetCC->getCondition()) {
734 default: assert(0 && "Illegal integer SetCC!");
735 case ISD::SETEQ: Opc = X86::JE; break;
736 case ISD::SETGT: Opc = X86::JG; break;
737 case ISD::SETGE: Opc = X86::JGE; break;
738 case ISD::SETLT: Opc = X86::JL; break;
739 case ISD::SETLE: Opc = X86::JLE; break;
740 case ISD::SETNE: Opc = X86::JNE; break;
741 case ISD::SETULT: Opc = X86::JB; break;
742 case ISD::SETUGT: Opc = X86::JA; break;
743 case ISD::SETULE: Opc = X86::JBE; break;
744 case ISD::SETUGE: Opc = X86::JAE; break;
745 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000746 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000747 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000748 BuildMI(BB, Opc, 1).addMBB(Dest);
749 return false;
750 }
751
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000752 unsigned Opc2 = 0; // Second branch if needed.
753
754 // On a floating point condition, the flags are set as follows:
755 // ZF PF CF op
756 // 0 | 0 | 0 | X > Y
757 // 0 | 0 | 1 | X < Y
758 // 1 | 0 | 0 | X == Y
759 // 1 | 1 | 1 | unordered
760 //
761 switch (SetCC->getCondition()) {
762 default: assert(0 && "Invalid FP setcc!");
763 case ISD::SETUEQ:
764 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
765 case ISD::SETOGT:
766 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
767 case ISD::SETOGE:
768 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
769 case ISD::SETULT:
770 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
771 case ISD::SETULE:
772 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
773 case ISD::SETONE:
774 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
775 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
776 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
777 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
778 Opc = X86::JA; // ZF = 0 & CF = 0
779 Opc2 = X86::JP; // PF = 1
780 break;
781 case ISD::SETUGE: // PF = 1 | CF = 0
782 Opc = X86::JAE; // CF = 0
783 Opc2 = X86::JP; // PF = 1
784 break;
785 case ISD::SETUNE: // PF = 1 | ZF = 0
786 Opc = X86::JNE; // ZF = 0
787 Opc2 = X86::JP; // PF = 1
788 break;
789 case ISD::SETOEQ: // PF = 0 & ZF = 1
790 //X86::JNP, X86::JE
791 //X86::AND8rr
792 return true; // FIXME: Emit more efficient code for this branch.
793 case ISD::SETOLT: // PF = 0 & CF = 1
794 //X86::JNP, X86::JB
795 //X86::AND8rr
796 return true; // FIXME: Emit more efficient code for this branch.
797 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
798 //X86::JNP, X86::JBE
799 //X86::AND8rr
800 return true; // FIXME: Emit more efficient code for this branch.
801 }
802
Chris Lattner6c07aee2005-01-11 04:06:27 +0000803 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000804 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000805 BuildMI(BB, Opc, 1).addMBB(Dest);
806 if (Opc2)
807 BuildMI(BB, Opc2, 1).addMBB(Dest);
808 return false;
809}
810
Chris Lattner24aad1b2005-01-10 22:10:13 +0000811/// EmitSelectCC - Emit code into BB that performs a select operation between
812/// the two registers RTrue and RFalse, generating a result into RDest. Return
813/// true if the fold cannot be performed.
814///
815void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
816 unsigned RTrue, unsigned RFalse, unsigned RDest) {
817 enum Condition {
818 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
819 NOT_SET
820 } CondCode = NOT_SET;
821
822 static const unsigned CMOVTAB16[] = {
823 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
824 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
825 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
826 };
827 static const unsigned CMOVTAB32[] = {
828 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
829 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
830 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
831 };
832 static const unsigned CMOVTABFP[] = {
833 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
834 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
835 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
836 };
837
838 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
839 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
840 switch (SetCC->getCondition()) {
841 default: assert(0 && "Unknown integer comparison!");
842 case ISD::SETEQ: CondCode = EQ; break;
843 case ISD::SETGT: CondCode = GT; break;
844 case ISD::SETGE: CondCode = GE; break;
845 case ISD::SETLT: CondCode = LT; break;
846 case ISD::SETLE: CondCode = LE; break;
847 case ISD::SETNE: CondCode = NE; break;
848 case ISD::SETULT: CondCode = B; break;
849 case ISD::SETUGT: CondCode = A; break;
850 case ISD::SETULE: CondCode = BE; break;
851 case ISD::SETUGE: CondCode = AE; break;
852 }
853 } else {
854 // On a floating point condition, the flags are set as follows:
855 // ZF PF CF op
856 // 0 | 0 | 0 | X > Y
857 // 0 | 0 | 1 | X < Y
858 // 1 | 0 | 0 | X == Y
859 // 1 | 1 | 1 | unordered
860 //
861 switch (SetCC->getCondition()) {
862 default: assert(0 && "Unknown FP comparison!");
863 case ISD::SETUEQ:
864 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
865 case ISD::SETOGT:
866 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
867 case ISD::SETOGE:
868 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
869 case ISD::SETULT:
870 case ISD::SETLT: CondCode = B; break; // True if CF = 1
871 case ISD::SETULE:
872 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
873 case ISD::SETONE:
874 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
875 case ISD::SETUO: CondCode = P; break; // True if PF = 1
876 case ISD::SETO: CondCode = NP; break; // True if PF = 0
877 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
878 case ISD::SETUGE: // PF = 1 | CF = 0
879 case ISD::SETUNE: // PF = 1 | ZF = 0
880 case ISD::SETOEQ: // PF = 0 & ZF = 1
881 case ISD::SETOLT: // PF = 0 & CF = 1
882 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
883 // We cannot emit this comparison as a single cmov.
884 break;
885 }
886 }
887 }
888
889 unsigned Opc = 0;
890 if (CondCode != NOT_SET) {
891 switch (SVT) {
892 default: assert(0 && "Cannot select this type!");
893 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
894 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
895 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000896 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000897 }
898 }
899
900 // Finally, if we weren't able to fold this, just emit the condition and test
901 // it.
902 if (CondCode == NOT_SET || Opc == 0) {
903 // Get the condition into the zero flag.
904 unsigned CondReg = SelectExpr(Cond);
905 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
906
907 switch (SVT) {
908 default: assert(0 && "Cannot select this type!");
909 case MVT::i16: Opc = X86::CMOVE16rr; break;
910 case MVT::i32: Opc = X86::CMOVE32rr; break;
911 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000912 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000913 }
914 } else {
915 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000916 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000917 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000918 }
919 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
920}
921
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000922void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000923 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000924 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
925 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000926 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000927 switch (RHS.getValueType()) {
928 default: break;
929 case MVT::i1:
930 case MVT::i8: Opc = X86::CMP8mi; break;
931 case MVT::i16: Opc = X86::CMP16mi; break;
932 case MVT::i32: Opc = X86::CMP32mi; break;
933 }
934 if (Opc) {
935 X86AddressMode AM;
936 EmitFoldedLoad(LHS, AM);
937 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
938 return;
939 }
940 }
941
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000942 switch (RHS.getValueType()) {
943 default: break;
944 case MVT::i1:
945 case MVT::i8: Opc = X86::CMP8ri; break;
946 case MVT::i16: Opc = X86::CMP16ri; break;
947 case MVT::i32: Opc = X86::CMP32ri; break;
948 }
949 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000950 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000951 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
952 return;
953 }
Chris Lattner7f2afac2005-01-14 22:37:41 +0000954 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
955 if (CN->isExactlyValue(+0.0) ||
956 CN->isExactlyValue(-0.0)) {
957 unsigned Reg = SelectExpr(LHS);
958 BuildMI(BB, X86::FTST, 1).addReg(Reg);
959 BuildMI(BB, X86::FNSTSW8r, 0);
960 BuildMI(BB, X86::SAHF, 1);
961 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000962 }
963
Chris Lattneref6806c2005-01-12 02:02:48 +0000964 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000965 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000966 switch (RHS.getValueType()) {
967 default: break;
968 case MVT::i1:
969 case MVT::i8: Opc = X86::CMP8mr; break;
970 case MVT::i16: Opc = X86::CMP16mr; break;
971 case MVT::i32: Opc = X86::CMP32mr; break;
972 }
973 if (Opc) {
974 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +0000975 EmitFoldedLoad(LHS, AM);
976 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +0000977 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
978 return;
979 }
980 }
981
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000982 switch (LHS.getValueType()) {
983 default: assert(0 && "Cannot compare this value!");
984 case MVT::i1:
985 case MVT::i8: Opc = X86::CMP8rr; break;
986 case MVT::i16: Opc = X86::CMP16rr; break;
987 case MVT::i32: Opc = X86::CMP32rr; break;
988 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000989 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000990 }
Chris Lattner11333092005-01-11 03:11:44 +0000991 unsigned Tmp1, Tmp2;
992 if (getRegPressure(LHS) > getRegPressure(RHS)) {
993 Tmp1 = SelectExpr(LHS);
994 Tmp2 = SelectExpr(RHS);
995 } else {
996 Tmp2 = SelectExpr(RHS);
997 Tmp1 = SelectExpr(LHS);
998 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000999 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1000}
1001
Chris Lattner4ff348b2005-01-17 06:26:58 +00001002/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1003/// The DAG cannot have cycles in it, by definition, so the visited set is not
1004/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1005/// reuse, so it prevents exponential cases.
1006///
1007static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1008 std::set<SDNode*> &Visited) {
1009 if (N == Op) return true; // Found it.
1010 SDNode *Node = N.Val;
1011 if (Node->getNumOperands() == 0) return false; // Leaf?
1012 if (!Visited.insert(Node).second) return false; // Already visited?
1013
1014 // Recurse for the first N-1 operands.
1015 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1016 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1017 return true;
1018
1019 // Tail recurse for the last operand.
1020 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1021}
1022
Chris Lattnera5ade062005-01-11 21:19:59 +00001023/// isFoldableLoad - Return true if this is a load instruction that can safely
1024/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001025bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001026 if (Op.getOpcode() != ISD::LOAD ||
1027 // FIXME: currently can't fold constant pool indexes.
1028 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1029 return false;
1030
1031 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001032 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1033 if (ExprMap.count(Op.getValue(1))) return false;
1034 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1035 assert(!LoweredTokens.count(Op.getValue(1)) &&
1036 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001037
Chris Lattner4ff348b2005-01-17 06:26:58 +00001038 // If there is not just one use of its value, we cannot fold.
1039 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1040
1041 // Finally, we cannot fold the load into the operation if this would induce a
1042 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1043 // operand of the operation we are folding the load into) can possible use the
1044 // chain node defined by the load.
1045 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1046 std::set<SDNode*> Visited;
1047 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1048 return false;
1049 }
1050 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001051}
1052
Chris Lattner4ff348b2005-01-17 06:26:58 +00001053
Chris Lattnera5ade062005-01-11 21:19:59 +00001054/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1055/// and compute the address being loaded into AM.
1056void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1057 SDOperand Chain = Op.getOperand(0);
1058 SDOperand Address = Op.getOperand(1);
1059 if (getRegPressure(Chain) > getRegPressure(Address)) {
1060 Select(Chain);
1061 SelectAddress(Address, AM);
1062 } else {
1063 SelectAddress(Address, AM);
1064 Select(Chain);
1065 }
1066
1067 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001068 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1069 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001070 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001071 if (!LoweredTokens.insert(Op.getValue(1)).second)
1072 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001073}
1074
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001075unsigned ISel::SelectExpr(SDOperand N) {
1076 unsigned Result;
1077 unsigned Tmp1, Tmp2, Tmp3;
1078 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001079 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001080 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001081
Chris Lattner7f2afac2005-01-14 22:37:41 +00001082 if (Node->getOpcode() == ISD::CopyFromReg) {
1083 // FIXME: Handle copy from physregs!
1084
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001085 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001086 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001087 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001088
1089 unsigned &Reg = ExprMap[N];
1090 if (Reg) return Reg;
1091
1092 if (N.getOpcode() != ISD::CALL)
1093 Reg = Result = (N.getValueType() != MVT::Other) ?
1094 MakeReg(N.getValueType()) : 1;
1095 else {
1096 // If this is a call instruction, make sure to prepare ALL of the result
1097 // values as well as the chain.
1098 if (Node->getNumValues() == 1)
1099 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001100 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001101 Result = MakeReg(Node->getValueType(0));
1102 ExprMap[N.getValue(0)] = Result;
1103 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1104 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1105 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001106 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001108
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001109 switch (N.getOpcode()) {
1110 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001111 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 assert(0 && "Node not handled!\n");
1113 case ISD::FrameIndex:
1114 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1115 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1116 return Result;
1117 case ISD::ConstantPool:
1118 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1119 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1120 return Result;
1121 case ISD::ConstantFP:
1122 ContainsFPCode = true;
1123 Tmp1 = Result; // Intermediate Register
1124 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1125 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1126 Tmp1 = MakeReg(MVT::f64);
1127
1128 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1129 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1130 BuildMI(BB, X86::FLD0, 0, Tmp1);
1131 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1132 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1133 BuildMI(BB, X86::FLD1, 0, Tmp1);
1134 else
1135 assert(0 && "Unexpected constant!");
1136 if (Tmp1 != Result)
1137 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1138 return Result;
1139 case ISD::Constant:
1140 switch (N.getValueType()) {
1141 default: assert(0 && "Cannot use constants of this type!");
1142 case MVT::i1:
1143 case MVT::i8: Opc = X86::MOV8ri; break;
1144 case MVT::i16: Opc = X86::MOV16ri; break;
1145 case MVT::i32: Opc = X86::MOV32ri; break;
1146 }
1147 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1148 return Result;
1149 case ISD::GlobalAddress: {
1150 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1151 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1152 return Result;
1153 }
1154 case ISD::ExternalSymbol: {
1155 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1156 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1157 return Result;
1158 }
1159 case ISD::FP_EXTEND:
1160 Tmp1 = SelectExpr(N.getOperand(0));
1161 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001162 return Result;
1163 case ISD::ZERO_EXTEND: {
1164 int DestIs16 = N.getValueType() == MVT::i16;
1165 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001166
1167 // FIXME: This hack is here for zero extension casts from bool to i8. This
1168 // would not be needed if bools were promoted by Legalize.
1169 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001170 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001171 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1172 return Result;
1173 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001174
Chris Lattner4ff348b2005-01-17 06:26:58 +00001175 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001176 static const unsigned Opc[3] = {
1177 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1178 };
1179
1180 X86AddressMode AM;
1181 EmitFoldedLoad(N.getOperand(0), AM);
1182 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1183
1184 return Result;
1185 }
1186
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001187 static const unsigned Opc[3] = {
1188 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1189 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001190 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001191 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1192 return Result;
1193 }
1194 case ISD::SIGN_EXTEND: {
1195 int DestIs16 = N.getValueType() == MVT::i16;
1196 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1197
Chris Lattner590d8002005-01-09 18:52:44 +00001198 // FIXME: Legalize should promote bools to i8!
1199 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1200 "Sign extend from bool not implemented!");
1201
Chris Lattner4ff348b2005-01-17 06:26:58 +00001202 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001203 static const unsigned Opc[3] = {
1204 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1205 };
1206
1207 X86AddressMode AM;
1208 EmitFoldedLoad(N.getOperand(0), AM);
1209 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1210 return Result;
1211 }
1212
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001213 static const unsigned Opc[3] = {
1214 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1215 };
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1218 return Result;
1219 }
1220 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001221 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001222 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001223 switch (N.getValueType()) {
1224 default: assert(0 && "Unknown truncate!");
1225 case MVT::i1:
1226 case MVT::i8: Opc = X86::MOV8rm; break;
1227 case MVT::i16: Opc = X86::MOV16rm; break;
1228 }
1229 X86AddressMode AM;
1230 EmitFoldedLoad(N.getOperand(0), AM);
1231 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1232 return Result;
1233 }
1234
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001235 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1236 // a move out of AX or AL.
1237 switch (N.getOperand(0).getValueType()) {
1238 default: assert(0 && "Unknown truncate!");
1239 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1240 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1241 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1242 }
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1245
1246 switch (N.getValueType()) {
1247 default: assert(0 && "Unknown truncate!");
1248 case MVT::i1:
1249 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1250 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1251 }
1252 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1253 return Result;
1254
1255 case ISD::FP_ROUND:
1256 // Truncate from double to float by storing to memory as float,
1257 // then reading it back into a register.
1258
1259 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001260 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001261 Tmp1 = TLI.getTargetData().getFloatAlignment();
1262 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1263
1264 // Codegen the input.
1265 Tmp1 = SelectExpr(N.getOperand(0));
1266
1267 // Emit the store, then the reload.
1268 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1269 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001270 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001271
1272 case ISD::SINT_TO_FP:
1273 case ISD::UINT_TO_FP: {
1274 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001275 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001276
1277 // Promote the integer to a type supported by FLD. We do this because there
1278 // are no unsigned FLD instructions, so we must promote an unsigned value to
1279 // a larger signed value, then use FLD on the larger value.
1280 //
1281 MVT::ValueType PromoteType = MVT::Other;
1282 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1283 unsigned PromoteOpcode = 0;
1284 unsigned RealDestReg = Result;
1285 switch (SrcTy) {
1286 case MVT::i1:
1287 case MVT::i8:
1288 // We don't have the facilities for directly loading byte sized data from
1289 // memory (even signed). Promote it to 16 bits.
1290 PromoteType = MVT::i16;
1291 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1292 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1293 break;
1294 case MVT::i16:
1295 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1296 PromoteType = MVT::i32;
1297 PromoteOpcode = X86::MOVZX32rr16;
1298 }
1299 break;
1300 default:
1301 // Don't fild into the real destination.
1302 if (Node->getOpcode() == ISD::UINT_TO_FP)
1303 Result = MakeReg(Node->getValueType(0));
1304 break;
1305 }
1306
1307 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1308
1309 if (PromoteType != MVT::Other) {
1310 Tmp2 = MakeReg(PromoteType);
1311 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1312 SrcTy = PromoteType;
1313 Tmp1 = Tmp2;
1314 }
1315
1316 // Spill the integer to memory and reload it from there.
1317 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1318 MachineFunction *F = BB->getParent();
1319 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1320
1321 switch (SrcTy) {
1322 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001323 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001324 // FIXME: this won't work for cast [u]long to FP
1325 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1326 FrameIdx).addReg(Tmp1);
1327 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1328 FrameIdx, 4).addReg(Tmp1+1);
1329 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1330 break;
1331 case MVT::i32:
1332 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1333 FrameIdx).addReg(Tmp1);
1334 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1335 break;
1336 case MVT::i16:
1337 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1338 FrameIdx).addReg(Tmp1);
1339 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1340 break;
1341 default: break; // No promotion required.
1342 }
1343
Chris Lattner085c9952005-01-12 04:00:00 +00001344 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001345 // If this is a cast from uint -> double, we need to be careful when if
1346 // the "sign" bit is set. If so, we don't want to make a negative number,
1347 // we want to make a positive number. Emit code to add an offset if the
1348 // sign bit is set.
1349
1350 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1351 unsigned IsNeg = MakeReg(MVT::i32);
1352 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1353
1354 // Create a CP value that has the offset in one word and 0 in the other.
1355 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1356 0x4f80000000000000ULL);
1357 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1358 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1359 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1360
1361 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1362 // We need special handling for unsigned 64-bit integer sources. If the
1363 // input number has the "sign bit" set, then we loaded it incorrectly as a
1364 // negative 64-bit number. In this case, add an offset value.
1365
1366 // Emit a test instruction to see if the dynamic input value was signed.
1367 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1368
1369 // If the sign bit is set, get a pointer to an offset, otherwise get a
1370 // pointer to a zero.
1371 MachineConstantPool *CP = F->getConstantPool();
1372 unsigned Zero = MakeReg(MVT::i32);
1373 Constant *Null = Constant::getNullValue(Type::UIntTy);
1374 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1375 CP->getConstantPoolIndex(Null));
1376 unsigned Offset = MakeReg(MVT::i32);
1377 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1378
1379 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1380 CP->getConstantPoolIndex(OffsetCst));
1381 unsigned Addr = MakeReg(MVT::i32);
1382 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1383
1384 // Load the constant for an add. FIXME: this could make an 'fadd' that
1385 // reads directly from memory, but we don't support these yet.
1386 unsigned ConstReg = MakeReg(MVT::f64);
1387 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1388
1389 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1390 }
1391 return RealDestReg;
1392 }
1393 case ISD::FP_TO_SINT:
1394 case ISD::FP_TO_UINT: {
1395 // FIXME: Most of this grunt work should be done by legalize!
1396 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1397
1398 // Change the floating point control register to use "round towards zero"
1399 // mode when truncating to an integer value.
1400 //
1401 MachineFunction *F = BB->getParent();
1402 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1403 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1404
1405 // Load the old value of the high byte of the control word...
1406 unsigned HighPartOfCW = MakeReg(MVT::i8);
1407 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1408 CWFrameIdx, 1);
1409
1410 // Set the high part to be round to zero...
1411 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1412 CWFrameIdx, 1).addImm(12);
1413
1414 // Reload the modified control word now...
1415 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1416
1417 // Restore the memory image of control word to original value
1418 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1419 CWFrameIdx, 1).addReg(HighPartOfCW);
1420
1421 // We don't have the facilities for directly storing byte sized data to
1422 // memory. Promote it to 16 bits. We also must promote unsigned values to
1423 // larger classes because we only have signed FP stores.
1424 MVT::ValueType StoreClass = Node->getValueType(0);
1425 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1426 switch (StoreClass) {
1427 case MVT::i8: StoreClass = MVT::i16; break;
1428 case MVT::i16: StoreClass = MVT::i32; break;
1429 case MVT::i32: StoreClass = MVT::i64; break;
1430 // The following treatment of cLong may not be perfectly right,
1431 // but it survives chains of casts of the form
1432 // double->ulong->double.
1433 case MVT::i64: StoreClass = MVT::i64; break;
1434 default: assert(0 && "Unknown store class!");
1435 }
1436
1437 // Spill the integer to memory and reload it from there.
1438 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1439 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1440
1441 switch (StoreClass) {
1442 default: assert(0 && "Unknown store class!");
1443 case MVT::i16:
1444 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1445 break;
1446 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001447 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001448 break;
1449 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001450 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001451 break;
1452 }
1453
1454 switch (Node->getValueType(0)) {
1455 default:
1456 assert(0 && "Unknown integer type!");
1457 case MVT::i64:
1458 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001459 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001460 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1461 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1462 case MVT::i32:
1463 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1464 break;
1465 case MVT::i16:
1466 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1467 break;
1468 case MVT::i8:
1469 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1470 break;
1471 }
1472
1473 // Reload the original control word now.
1474 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1475 return Result;
1476 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001477 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001478 Op0 = N.getOperand(0);
1479 Op1 = N.getOperand(1);
1480
Chris Lattner4ff348b2005-01-17 06:26:58 +00001481 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001482 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001483 goto FoldAdd;
1484 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001485
Chris Lattner4ff348b2005-01-17 06:26:58 +00001486 if (isFoldableLoad(Op1, Op0)) {
1487 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001488 switch (N.getValueType()) {
1489 default: assert(0 && "Cannot add this type!");
1490 case MVT::i1:
1491 case MVT::i8: Opc = X86::ADD8rm; break;
1492 case MVT::i16: Opc = X86::ADD16rm; break;
1493 case MVT::i32: Opc = X86::ADD32rm; break;
1494 case MVT::f32: Opc = X86::FADD32m; break;
1495 case MVT::f64: Opc = X86::FADD64m; break;
1496 }
1497 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001498 EmitFoldedLoad(Op1, AM);
1499 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001500 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1501 return Result;
1502 }
1503
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001504 // See if we can codegen this as an LEA to fold operations together.
1505 if (N.getValueType() == MVT::i32) {
1506 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001507 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001508 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001509 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001510 // leave this as LEA, then peephole it to 'ADD' after two address elim
1511 // happens.
1512 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001513 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001514 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1515 return Result;
1516 }
1517 }
1518 }
Chris Lattner11333092005-01-11 03:11:44 +00001519
Chris Lattnera5ade062005-01-11 21:19:59 +00001520 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001521 Opc = 0;
1522 if (CN->getValue() == 1) { // add X, 1 -> inc X
1523 switch (N.getValueType()) {
1524 default: assert(0 && "Cannot integer add this type!");
1525 case MVT::i8: Opc = X86::INC8r; break;
1526 case MVT::i16: Opc = X86::INC16r; break;
1527 case MVT::i32: Opc = X86::INC32r; break;
1528 }
1529 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1530 switch (N.getValueType()) {
1531 default: assert(0 && "Cannot integer add this type!");
1532 case MVT::i8: Opc = X86::DEC8r; break;
1533 case MVT::i16: Opc = X86::DEC16r; break;
1534 case MVT::i32: Opc = X86::DEC32r; break;
1535 }
1536 }
1537
1538 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001539 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001540 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1541 return Result;
1542 }
1543
1544 switch (N.getValueType()) {
1545 default: assert(0 && "Cannot add this type!");
1546 case MVT::i8: Opc = X86::ADD8ri; break;
1547 case MVT::i16: Opc = X86::ADD16ri; break;
1548 case MVT::i32: Opc = X86::ADD32ri; break;
1549 }
1550 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001551 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001552 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1553 return Result;
1554 }
1555 }
1556
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001557 switch (N.getValueType()) {
1558 default: assert(0 && "Cannot add this type!");
1559 case MVT::i8: Opc = X86::ADD8rr; break;
1560 case MVT::i16: Opc = X86::ADD16rr; break;
1561 case MVT::i32: Opc = X86::ADD32rr; break;
1562 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001563 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001564 }
Chris Lattner11333092005-01-11 03:11:44 +00001565
Chris Lattnera5ade062005-01-11 21:19:59 +00001566 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1567 Tmp1 = SelectExpr(Op0);
1568 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001569 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001570 Tmp2 = SelectExpr(Op1);
1571 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001572 }
1573
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001574 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1575 return Result;
1576 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001577 case ISD::MUL:
1578 case ISD::AND:
1579 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001580 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001581 static const unsigned SUBTab[] = {
1582 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1583 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1584 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1585 };
1586 static const unsigned MULTab[] = {
1587 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1588 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1589 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1590 };
1591 static const unsigned ANDTab[] = {
1592 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1593 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1594 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1595 };
1596 static const unsigned ORTab[] = {
1597 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1598 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1599 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1600 };
1601 static const unsigned XORTab[] = {
1602 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1603 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1604 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1605 };
1606
1607 Op0 = Node->getOperand(0);
1608 Op1 = Node->getOperand(1);
1609
1610 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001611 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1612 if (CN->isNullValue()) { // 0 - N -> neg N
1613 switch (N.getValueType()) {
1614 default: assert(0 && "Cannot sub this type!");
1615 case MVT::i1:
1616 case MVT::i8: Opc = X86::NEG8r; break;
1617 case MVT::i16: Opc = X86::NEG16r; break;
1618 case MVT::i32: Opc = X86::NEG32r; break;
1619 }
1620 Tmp1 = SelectExpr(N.getOperand(1));
1621 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1622 return Result;
1623 }
1624
Chris Lattnera5ade062005-01-11 21:19:59 +00001625 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1626 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001627 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001628 switch (N.getValueType()) {
1629 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001630 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001631 case MVT::i8: Opc = X86::NOT8r; break;
1632 case MVT::i16: Opc = X86::NOT16r; break;
1633 case MVT::i32: Opc = X86::NOT32r; break;
1634 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001635 if (Opc) {
1636 Tmp1 = SelectExpr(Op0);
1637 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1638 return Result;
1639 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001640 }
1641
Chris Lattner2a4e5082005-01-17 06:48:02 +00001642 // Fold common multiplies into LEA instructions.
1643 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1644 switch ((int)CN->getValue()) {
1645 default: break;
1646 case 3:
1647 case 5:
1648 case 9:
1649 X86AddressMode AM;
1650 // Remove N from exprmap so SelectAddress doesn't get confused.
1651 ExprMap.erase(N);
1652 SelectAddress(N, AM);
1653 // Restore it to the map.
1654 ExprMap[N] = Result;
1655 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1656 return Result;
1657 }
1658 }
1659
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001660 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001661 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001662 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001663 case MVT::i8: Opc = 0; break;
1664 case MVT::i16: Opc = 1; break;
1665 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001666 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001667 switch (Node->getOpcode()) {
1668 default: assert(0 && "Unreachable!");
1669 case ISD::SUB: Opc = SUBTab[Opc]; break;
1670 case ISD::MUL: Opc = MULTab[Opc]; break;
1671 case ISD::AND: Opc = ANDTab[Opc]; break;
1672 case ISD::OR: Opc = ORTab[Opc]; break;
1673 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001674 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001675 if (Opc) { // Can't fold MUL:i8 R, imm
1676 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001677 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1678 return Result;
1679 }
1680 }
Chris Lattner11333092005-01-11 03:11:44 +00001681
Chris Lattner4ff348b2005-01-17 06:26:58 +00001682 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001683 if (Node->getOpcode() != ISD::SUB) {
1684 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001685 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001686 } else {
1687 // Emit 'reverse' subract, with a memory operand.
1688 switch (N.getValueType()) {
1689 default: Opc = 0; break;
1690 case MVT::f32: Opc = X86::FSUBR32m; break;
1691 case MVT::f64: Opc = X86::FSUBR64m; break;
1692 }
1693 if (Opc) {
1694 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001695 EmitFoldedLoad(Op0, AM);
1696 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001697 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1698 return Result;
1699 }
1700 }
1701
Chris Lattner4ff348b2005-01-17 06:26:58 +00001702 if (isFoldableLoad(Op1, Op0)) {
1703 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001704 switch (N.getValueType()) {
1705 default: assert(0 && "Cannot operate on this type!");
1706 case MVT::i1:
1707 case MVT::i8: Opc = 5; break;
1708 case MVT::i16: Opc = 6; break;
1709 case MVT::i32: Opc = 7; break;
1710 case MVT::f32: Opc = 8; break;
1711 case MVT::f64: Opc = 9; break;
1712 }
1713 switch (Node->getOpcode()) {
1714 default: assert(0 && "Unreachable!");
1715 case ISD::SUB: Opc = SUBTab[Opc]; break;
1716 case ISD::MUL: Opc = MULTab[Opc]; break;
1717 case ISD::AND: Opc = ANDTab[Opc]; break;
1718 case ISD::OR: Opc = ORTab[Opc]; break;
1719 case ISD::XOR: Opc = XORTab[Opc]; break;
1720 }
1721
1722 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001723 EmitFoldedLoad(Op1, AM);
1724 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001725 if (Opc) {
1726 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1727 } else {
1728 assert(Node->getOpcode() == ISD::MUL &&
1729 N.getValueType() == MVT::i8 && "Unexpected situation!");
1730 // Must use the MUL instruction, which forces use of AL.
1731 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1732 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1733 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1734 }
1735 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001736 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001737
1738 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1739 Tmp1 = SelectExpr(Op0);
1740 Tmp2 = SelectExpr(Op1);
1741 } else {
1742 Tmp2 = SelectExpr(Op1);
1743 Tmp1 = SelectExpr(Op0);
1744 }
1745
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001746 switch (N.getValueType()) {
1747 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001748 case MVT::i1:
1749 case MVT::i8: Opc = 10; break;
1750 case MVT::i16: Opc = 11; break;
1751 case MVT::i32: Opc = 12; break;
1752 case MVT::f32: Opc = 13; break;
1753 case MVT::f64: Opc = 14; break;
1754 }
1755 switch (Node->getOpcode()) {
1756 default: assert(0 && "Unreachable!");
1757 case ISD::SUB: Opc = SUBTab[Opc]; break;
1758 case ISD::MUL: Opc = MULTab[Opc]; break;
1759 case ISD::AND: Opc = ANDTab[Opc]; break;
1760 case ISD::OR: Opc = ORTab[Opc]; break;
1761 case ISD::XOR: Opc = XORTab[Opc]; break;
1762 }
1763 if (Opc) {
1764 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1765 } else {
1766 assert(Node->getOpcode() == ISD::MUL &&
1767 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001768 // Must use the MUL instruction, which forces use of AL.
1769 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1770 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1771 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001772 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001773 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001774 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001775 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001776 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1777 Tmp2 = SelectExpr(N.getOperand(1));
1778 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001780 Tmp3 = SelectExpr(N.getOperand(2));
1781 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001782 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001783 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1784 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001785
1786 case ISD::SDIV:
1787 case ISD::UDIV:
1788 case ISD::SREM:
1789 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001790 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1791 "We don't support this operator!");
1792
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001793 if (N.getOpcode() == ISD::SDIV)
1794 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1795 // FIXME: These special cases should be handled by the lowering impl!
1796 unsigned RHS = CN->getValue();
1797 bool isNeg = false;
1798 if ((int)RHS < 0) {
1799 isNeg = true;
1800 RHS = -RHS;
1801 }
1802 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1803 unsigned Log = log2(RHS);
1804 unsigned TmpReg = MakeReg(N.getValueType());
1805 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1806 switch (N.getValueType()) {
1807 default: assert("Unknown type to signed divide!");
1808 case MVT::i8:
1809 SAROpc = X86::SAR8ri;
1810 SHROpc = X86::SHR8ri;
1811 ADDOpc = X86::ADD8rr;
1812 NEGOpc = X86::NEG8r;
1813 break;
1814 case MVT::i16:
1815 SAROpc = X86::SAR16ri;
1816 SHROpc = X86::SHR16ri;
1817 ADDOpc = X86::ADD16rr;
1818 NEGOpc = X86::NEG16r;
1819 break;
1820 case MVT::i32:
1821 SAROpc = X86::SAR32ri;
1822 SHROpc = X86::SHR32ri;
1823 ADDOpc = X86::ADD32rr;
1824 NEGOpc = X86::NEG32r;
1825 break;
1826 }
Chris Lattner11333092005-01-11 03:11:44 +00001827 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001828 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1829 unsigned TmpReg2 = MakeReg(N.getValueType());
1830 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1831 unsigned TmpReg3 = MakeReg(N.getValueType());
1832 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1833
1834 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1835 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1836 if (isNeg)
1837 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1838 return Result;
1839 }
1840 }
1841
Chris Lattner11333092005-01-11 03:11:44 +00001842 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1843 Tmp1 = SelectExpr(N.getOperand(0));
1844 Tmp2 = SelectExpr(N.getOperand(1));
1845 } else {
1846 Tmp2 = SelectExpr(N.getOperand(1));
1847 Tmp1 = SelectExpr(N.getOperand(0));
1848 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001849
1850 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1851 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1852 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1853 switch (N.getValueType()) {
1854 default: assert(0 && "Cannot sdiv this type!");
1855 case MVT::i8:
1856 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1857 LoReg = X86::AL;
1858 HiReg = X86::AH;
1859 MovOpcode = X86::MOV8rr;
1860 ClrOpcode = X86::MOV8ri;
1861 SExtOpcode = X86::CBW;
1862 break;
1863 case MVT::i16:
1864 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1865 LoReg = X86::AX;
1866 HiReg = X86::DX;
1867 MovOpcode = X86::MOV16rr;
1868 ClrOpcode = X86::MOV16ri;
1869 SExtOpcode = X86::CWD;
1870 break;
1871 case MVT::i32:
1872 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001873 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001874 HiReg = X86::EDX;
1875 MovOpcode = X86::MOV32rr;
1876 ClrOpcode = X86::MOV32ri;
1877 SExtOpcode = X86::CDQ;
1878 break;
1879 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1880 case MVT::f32:
1881 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001882 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001883 return Result;
1884 }
1885
1886 // Set up the low part.
1887 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1888
1889 if (isSigned) {
1890 // Sign extend the low part into the high part.
1891 BuildMI(BB, SExtOpcode, 0);
1892 } else {
1893 // Zero out the high part, effectively zero extending the input.
1894 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1895 }
1896
1897 // Emit the DIV/IDIV instruction.
1898 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1899
1900 // Get the result of the divide or rem.
1901 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1902 return Result;
1903 }
1904
1905 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001906 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001907 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1908 switch (N.getValueType()) {
1909 default: assert(0 && "Cannot shift this type!");
1910 case MVT::i8: Opc = X86::ADD8rr; break;
1911 case MVT::i16: Opc = X86::ADD16rr; break;
1912 case MVT::i32: Opc = X86::ADD32rr; break;
1913 }
1914 Tmp1 = SelectExpr(N.getOperand(0));
1915 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1916 return Result;
1917 }
1918
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001919 switch (N.getValueType()) {
1920 default: assert(0 && "Cannot shift this type!");
1921 case MVT::i8: Opc = X86::SHL8ri; break;
1922 case MVT::i16: Opc = X86::SHL16ri; break;
1923 case MVT::i32: Opc = X86::SHL32ri; break;
1924 }
Chris Lattner11333092005-01-11 03:11:44 +00001925 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001926 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1927 return Result;
1928 }
Chris Lattner11333092005-01-11 03:11:44 +00001929
1930 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1931 Tmp1 = SelectExpr(N.getOperand(0));
1932 Tmp2 = SelectExpr(N.getOperand(1));
1933 } else {
1934 Tmp2 = SelectExpr(N.getOperand(1));
1935 Tmp1 = SelectExpr(N.getOperand(0));
1936 }
1937
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001938 switch (N.getValueType()) {
1939 default: assert(0 && "Cannot shift this type!");
1940 case MVT::i8 : Opc = X86::SHL8rCL; break;
1941 case MVT::i16: Opc = X86::SHL16rCL; break;
1942 case MVT::i32: Opc = X86::SHL32rCL; break;
1943 }
1944 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1945 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1946 return Result;
1947 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001948 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1949 switch (N.getValueType()) {
1950 default: assert(0 && "Cannot shift this type!");
1951 case MVT::i8: Opc = X86::SHR8ri; break;
1952 case MVT::i16: Opc = X86::SHR16ri; break;
1953 case MVT::i32: Opc = X86::SHR32ri; break;
1954 }
Chris Lattner11333092005-01-11 03:11:44 +00001955 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001956 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1957 return Result;
1958 }
Chris Lattner11333092005-01-11 03:11:44 +00001959
1960 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1961 Tmp1 = SelectExpr(N.getOperand(0));
1962 Tmp2 = SelectExpr(N.getOperand(1));
1963 } else {
1964 Tmp2 = SelectExpr(N.getOperand(1));
1965 Tmp1 = SelectExpr(N.getOperand(0));
1966 }
1967
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 switch (N.getValueType()) {
1969 default: assert(0 && "Cannot shift this type!");
1970 case MVT::i8 : Opc = X86::SHR8rCL; break;
1971 case MVT::i16: Opc = X86::SHR16rCL; break;
1972 case MVT::i32: Opc = X86::SHR32rCL; break;
1973 }
1974 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1975 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1976 return Result;
1977 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001978 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1979 switch (N.getValueType()) {
1980 default: assert(0 && "Cannot shift this type!");
1981 case MVT::i8: Opc = X86::SAR8ri; break;
1982 case MVT::i16: Opc = X86::SAR16ri; break;
1983 case MVT::i32: Opc = X86::SAR32ri; break;
1984 }
Chris Lattner11333092005-01-11 03:11:44 +00001985 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1987 return Result;
1988 }
Chris Lattner11333092005-01-11 03:11:44 +00001989
1990 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1991 Tmp1 = SelectExpr(N.getOperand(0));
1992 Tmp2 = SelectExpr(N.getOperand(1));
1993 } else {
1994 Tmp2 = SelectExpr(N.getOperand(1));
1995 Tmp1 = SelectExpr(N.getOperand(0));
1996 }
1997
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 switch (N.getValueType()) {
1999 default: assert(0 && "Cannot shift this type!");
2000 case MVT::i8 : Opc = X86::SAR8rCL; break;
2001 case MVT::i16: Opc = X86::SAR16rCL; break;
2002 case MVT::i32: Opc = X86::SAR32rCL; break;
2003 }
2004 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2005 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2006 return Result;
2007
2008 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002009 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002010 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2011 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2012 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002013 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002014 // Make sure we generate both values.
2015 if (Result != 1)
2016 ExprMap[N.getValue(1)] = 1; // Generate the token
2017 else
2018 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2019
Chris Lattner5188ad72005-01-08 19:28:19 +00002020 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002021 default: assert(0 && "Cannot load this type!");
2022 case MVT::i1:
2023 case MVT::i8: Opc = X86::MOV8rm; break;
2024 case MVT::i16: Opc = X86::MOV16rm; break;
2025 case MVT::i32: Opc = X86::MOV32rm; break;
2026 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
2027 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2028 }
Chris Lattner11333092005-01-11 03:11:44 +00002029
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002030 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002031 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002032 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2033 } else {
2034 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002035
2036 SDOperand Chain = N.getOperand(0);
2037 SDOperand Address = N.getOperand(1);
2038 if (getRegPressure(Chain) > getRegPressure(Address)) {
2039 Select(Chain);
2040 SelectAddress(Address, AM);
2041 } else {
2042 SelectAddress(Address, AM);
2043 Select(Chain);
2044 }
2045
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002046 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2047 }
2048 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002049
2050 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2051 case ISD::ZEXTLOAD: {
2052 // Make sure we generate both values.
2053 if (Result != 1)
2054 ExprMap[N.getValue(1)] = 1; // Generate the token
2055 else
2056 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2057
Chris Lattnerda2ce112005-01-16 07:34:08 +00002058 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2059 if (Node->getValueType(0) == MVT::f64) {
2060 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2061 "Bad EXTLOAD!");
2062 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2063 CP->getIndex());
2064 return Result;
2065 }
2066
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002067 X86AddressMode AM;
2068 if (getRegPressure(Node->getOperand(0)) >
2069 getRegPressure(Node->getOperand(1))) {
2070 Select(Node->getOperand(0)); // chain
2071 SelectAddress(Node->getOperand(1), AM);
2072 } else {
2073 SelectAddress(Node->getOperand(1), AM);
2074 Select(Node->getOperand(0)); // chain
2075 }
2076
2077 switch (Node->getValueType(0)) {
2078 default: assert(0 && "Unknown type to sign extend to.");
2079 case MVT::f64:
2080 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2081 "Bad EXTLOAD!");
2082 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2083 break;
2084 case MVT::i32:
2085 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2086 default:
2087 assert(0 && "Bad zero extend!");
2088 case MVT::i1:
2089 case MVT::i8:
2090 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2091 break;
2092 case MVT::i16:
2093 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2094 break;
2095 }
2096 break;
2097 case MVT::i16:
2098 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2099 "Bad zero extend!");
2100 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2101 break;
2102 case MVT::i8:
2103 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2104 "Bad zero extend!");
2105 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2106 break;
2107 }
2108 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002110 case ISD::SEXTLOAD: {
2111 // Make sure we generate both values.
2112 if (Result != 1)
2113 ExprMap[N.getValue(1)] = 1; // Generate the token
2114 else
2115 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2116
2117 X86AddressMode AM;
2118 if (getRegPressure(Node->getOperand(0)) >
2119 getRegPressure(Node->getOperand(1))) {
2120 Select(Node->getOperand(0)); // chain
2121 SelectAddress(Node->getOperand(1), AM);
2122 } else {
2123 SelectAddress(Node->getOperand(1), AM);
2124 Select(Node->getOperand(0)); // chain
2125 }
2126
2127 switch (Node->getValueType(0)) {
2128 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2129 default: assert(0 && "Unknown type to sign extend to.");
2130 case MVT::i32:
2131 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2132 default:
2133 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2134 case MVT::i8:
2135 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2136 break;
2137 case MVT::i16:
2138 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2139 break;
2140 }
2141 break;
2142 case MVT::i16:
2143 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2144 "Cannot sign extend from bool!");
2145 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2146 break;
2147 }
2148 return Result;
2149 }
2150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002152 // Generate both result values.
2153 if (Result != 1)
2154 ExprMap[N.getValue(1)] = 1; // Generate the token
2155 else
2156 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2157
2158 // FIXME: We are currently ignoring the requested alignment for handling
2159 // greater than the stack alignment. This will need to be revisited at some
2160 // point. Align = N.getOperand(2);
2161
2162 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2163 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2164 std::cerr << "Cannot allocate stack object with greater alignment than"
2165 << " the stack alignment yet!";
2166 abort();
2167 }
2168
2169 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002170 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002171 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2172 .addImm(CN->getValue());
2173 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002174 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2175 Select(N.getOperand(0));
2176 Tmp1 = SelectExpr(N.getOperand(1));
2177 } else {
2178 Tmp1 = SelectExpr(N.getOperand(1));
2179 Select(N.getOperand(0));
2180 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002181
2182 // Subtract size from stack pointer, thereby allocating some space.
2183 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2184 }
2185
2186 // Put a pointer to the space into the result register, by copying the stack
2187 // pointer.
2188 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2189 return Result;
2190
2191 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002192 // The chain for this call is now lowered.
2193 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2194
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002195 if (GlobalAddressSDNode *GASD =
2196 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002197 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002198 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2199 } else if (ExternalSymbolSDNode *ESSDN =
2200 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002201 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002202 BuildMI(BB, X86::CALLpcrel32,
2203 1).addExternalSymbol(ESSDN->getSymbol(), true);
2204 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002205 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2206 Select(N.getOperand(0));
2207 Tmp1 = SelectExpr(N.getOperand(1));
2208 } else {
2209 Tmp1 = SelectExpr(N.getOperand(1));
2210 Select(N.getOperand(0));
2211 }
2212
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002213 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2214 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002215 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002216 default: assert(0 && "Unknown value type for call result!");
2217 case MVT::Other: return 1;
2218 case MVT::i1:
2219 case MVT::i8:
2220 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2221 break;
2222 case MVT::i16:
2223 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2224 break;
2225 case MVT::i32:
2226 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002227 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002228 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2229 break;
2230 case MVT::f32:
2231 case MVT::f64: // Floating-point return values live in %ST(0)
2232 ContainsFPCode = true;
2233 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2234 break;
2235 }
2236 return Result+N.ResNo;
2237 }
2238
2239 return 0;
2240}
2241
Chris Lattnere10269b2005-01-17 19:25:26 +00002242/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2243/// load/op/store instruction. If successful return true.
2244bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2245 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2246 SDOperand Chain = Node->getOperand(0);
2247 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002248 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002249
2250 // The chain has to be a load, the stored value must be an integer binary
2251 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002252 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002253 MVT::isFloatingPoint(StVal.getValueType()))
2254 return false;
2255
Chris Lattner5c659812005-01-17 22:10:42 +00002256 // Token chain must either be a factor node or the load to fold.
2257 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2258 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002259
Chris Lattner5c659812005-01-17 22:10:42 +00002260 SDOperand TheLoad;
2261
2262 // Check to see if there is a load from the same pointer that we're storing
2263 // to in either operand of the binop.
2264 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2265 StVal.getOperand(0).getOperand(1) == StPtr)
2266 TheLoad = StVal.getOperand(0);
2267 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2268 StVal.getOperand(1).getOperand(1) == StPtr)
2269 TheLoad = StVal.getOperand(1);
2270 else
2271 return false; // No matching load operand.
2272
2273 // We can only fold the load if there are no intervening side-effecting
2274 // operations. This means that the store uses the load as its token chain, or
2275 // there are only token factor nodes in between the store and load.
2276 if (Chain != TheLoad.getValue(1)) {
2277 // Okay, the other option is that we have a store referring to (possibly
2278 // nested) token factor nodes. For now, just try peeking through one level
2279 // of token factors to see if this is the case.
2280 bool ChainOk = false;
2281 if (Chain.getOpcode() == ISD::TokenFactor) {
2282 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2283 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2284 ChainOk = true;
2285 break;
2286 }
2287 }
2288
2289 if (!ChainOk) return false;
2290 }
2291
2292 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002293 return false;
2294
2295 // Make sure that one of the operands of the binop is the load, and that the
2296 // load folds into the binop.
2297 if (((StVal.getOperand(0) != TheLoad ||
2298 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2299 (StVal.getOperand(1) != TheLoad ||
2300 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2301 return false;
2302
2303 // Finally, check to see if this is one of the ops we can handle!
2304 static const unsigned ADDTAB[] = {
2305 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2306 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2307 };
2308 static const unsigned SUBTAB[] = {
2309 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2310 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2311 };
2312 static const unsigned ANDTAB[] = {
2313 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2314 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2315 };
2316 static const unsigned ORTAB[] = {
2317 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2318 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2319 };
2320 static const unsigned XORTAB[] = {
2321 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2322 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2323 };
2324 static const unsigned SHLTAB[] = {
2325 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2326 /*Have to put the reg in CL*/0, 0, 0,
2327 };
2328 static const unsigned SARTAB[] = {
2329 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2330 /*Have to put the reg in CL*/0, 0, 0,
2331 };
2332 static const unsigned SHRTAB[] = {
2333 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2334 /*Have to put the reg in CL*/0, 0, 0,
2335 };
2336
2337 const unsigned *TabPtr = 0;
2338 switch (StVal.getOpcode()) {
2339 default:
2340 std::cerr << "CANNOT [mem] op= val: ";
2341 StVal.Val->dump(); std::cerr << "\n";
2342 case ISD::MUL:
2343 case ISD::SDIV:
2344 case ISD::UDIV:
2345 case ISD::SREM:
2346 case ISD::UREM: return false;
2347
2348 case ISD::ADD: TabPtr = ADDTAB; break;
2349 case ISD::SUB: TabPtr = SUBTAB; break;
2350 case ISD::AND: TabPtr = ANDTAB; break;
2351 case ISD:: OR: TabPtr = ORTAB; break;
2352 case ISD::XOR: TabPtr = XORTAB; break;
2353 case ISD::SHL: TabPtr = SHLTAB; break;
2354 case ISD::SRA: TabPtr = SARTAB; break;
2355 case ISD::SRL: TabPtr = SHRTAB; break;
2356 }
2357
2358 // Handle: [mem] op= CST
2359 SDOperand Op0 = StVal.getOperand(0);
2360 SDOperand Op1 = StVal.getOperand(1);
2361 unsigned Opc;
2362 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2363 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2364 default: break;
2365 case MVT::i1:
2366 case MVT::i8: Opc = TabPtr[0]; break;
2367 case MVT::i16: Opc = TabPtr[1]; break;
2368 case MVT::i32: Opc = TabPtr[2]; break;
2369 }
2370
2371 if (Opc) {
Chris Lattner5c659812005-01-17 22:10:42 +00002372 LoweredTokens.insert(TheLoad.getValue(1));
2373 Select(Chain);
2374
Chris Lattnere10269b2005-01-17 19:25:26 +00002375 X86AddressMode AM;
2376 if (getRegPressure(TheLoad.getOperand(0)) >
2377 getRegPressure(TheLoad.getOperand(1))) {
2378 Select(TheLoad.getOperand(0));
2379 SelectAddress(TheLoad.getOperand(1), AM);
2380 } else {
2381 SelectAddress(TheLoad.getOperand(1), AM);
2382 Select(TheLoad.getOperand(0));
2383 }
Chris Lattner5c659812005-01-17 22:10:42 +00002384
2385 if (StVal.getOpcode() == ISD::ADD) {
2386 if (CN->getValue() == 1) {
2387 switch (Op0.getValueType()) {
2388 default: break;
2389 case MVT::i8:
2390 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2391 return true;
2392 case MVT::i16: Opc = TabPtr[1];
2393 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2394 return true;
2395 case MVT::i32: Opc = TabPtr[2];
2396 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2397 return true;
2398 }
2399 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2400 switch (Op0.getValueType()) {
2401 default: break;
2402 case MVT::i8:
2403 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2404 return true;
2405 case MVT::i16: Opc = TabPtr[1];
2406 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2407 return true;
2408 case MVT::i32: Opc = TabPtr[2];
2409 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2410 return true;
2411 }
2412 }
2413 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002414
2415 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2416 return true;
2417 }
2418 }
2419
2420 // If we have [mem] = V op [mem], try to turn it into:
2421 // [mem] = [mem] op V.
2422 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2423 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2424 StVal.getOpcode() != ISD::SRL)
2425 std::swap(Op0, Op1);
2426
2427 if (Op0 != TheLoad) return false;
2428
2429 switch (Op0.getValueType()) {
2430 default: return false;
2431 case MVT::i1:
2432 case MVT::i8: Opc = TabPtr[3]; break;
2433 case MVT::i16: Opc = TabPtr[4]; break;
2434 case MVT::i32: Opc = TabPtr[5]; break;
2435 }
Chris Lattner5c659812005-01-17 22:10:42 +00002436
2437 LoweredTokens.insert(TheLoad.getValue(1));
2438 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002439
2440 Select(TheLoad.getOperand(0));
2441 X86AddressMode AM;
2442 SelectAddress(TheLoad.getOperand(1), AM);
2443 unsigned Reg = SelectExpr(Op1);
2444 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2445 return true;
2446}
2447
2448
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449void ISel::Select(SDOperand N) {
2450 unsigned Tmp1, Tmp2, Opc;
2451
2452 // FIXME: Disable for our current expansion model!
2453 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2454 return; // Already selected.
2455
Chris Lattner989de032005-01-11 06:14:36 +00002456 SDNode *Node = N.Val;
2457
2458 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 default:
Chris Lattner989de032005-01-11 06:14:36 +00002460 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002461 assert(0 && "Node not handled yet!");
2462 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002463 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002464 if (Node->getNumOperands() == 2) {
2465 bool OneFirst =
2466 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2467 Select(Node->getOperand(OneFirst));
2468 Select(Node->getOperand(!OneFirst));
2469 } else {
2470 std::vector<std::pair<unsigned, unsigned> > OpsP;
2471 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2472 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2473 std::sort(OpsP.begin(), OpsP.end());
2474 std::reverse(OpsP.begin(), OpsP.end());
2475 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2476 Select(Node->getOperand(OpsP[i].second));
2477 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002478 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002479 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002480 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2481 Select(N.getOperand(0));
2482 Tmp1 = SelectExpr(N.getOperand(1));
2483 } else {
2484 Tmp1 = SelectExpr(N.getOperand(1));
2485 Select(N.getOperand(0));
2486 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002487 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002488
2489 if (Tmp1 != Tmp2) {
2490 switch (N.getOperand(1).getValueType()) {
2491 default: assert(0 && "Invalid type for operation!");
2492 case MVT::i1:
2493 case MVT::i8: Opc = X86::MOV8rr; break;
2494 case MVT::i16: Opc = X86::MOV16rr; break;
2495 case MVT::i32: Opc = X86::MOV32rr; break;
2496 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002497 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002498 }
2499 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2500 }
2501 return;
2502 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002503 switch (N.getNumOperands()) {
2504 default:
2505 assert(0 && "Unknown return instruction!");
2506 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002507 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2508 N.getOperand(2).getValueType() == MVT::i32 &&
2509 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002510 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2511 Tmp1 = SelectExpr(N.getOperand(1));
2512 Tmp2 = SelectExpr(N.getOperand(2));
2513 } else {
2514 Tmp2 = SelectExpr(N.getOperand(2));
2515 Tmp1 = SelectExpr(N.getOperand(1));
2516 }
2517 Select(N.getOperand(0));
2518
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002519 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2520 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2521 // Declare that EAX & EDX are live on exit.
2522 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2523 .addReg(X86::ESP);
2524 break;
2525 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002526 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2527 Select(N.getOperand(0));
2528 Tmp1 = SelectExpr(N.getOperand(1));
2529 } else {
2530 Tmp1 = SelectExpr(N.getOperand(1));
2531 Select(N.getOperand(0));
2532 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002533 switch (N.getOperand(1).getValueType()) {
2534 default: assert(0 && "All other types should have been promoted!!");
2535 case MVT::f64:
2536 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2537 // Declare that top-of-stack is live on exit
2538 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2539 break;
2540 case MVT::i32:
2541 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2542 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2543 break;
2544 }
2545 break;
2546 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002547 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002548 break;
2549 }
2550 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2551 return;
2552 case ISD::BR: {
2553 Select(N.getOperand(0));
2554 MachineBasicBlock *Dest =
2555 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2556 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2557 return;
2558 }
2559
2560 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002561 MachineBasicBlock *Dest =
2562 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002563
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002564 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2565 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002566 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2567 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2568 Select(N.getOperand(0));
2569 Tmp1 = SelectExpr(N.getOperand(1));
2570 } else {
2571 Tmp1 = SelectExpr(N.getOperand(1));
2572 Select(N.getOperand(0));
2573 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002574 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2575 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2576 }
Chris Lattner11333092005-01-11 03:11:44 +00002577
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002578 return;
2579 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002580
Chris Lattner4df0de92005-01-17 00:00:33 +00002581 case ISD::LOAD:
2582 // If this load could be folded into the only using instruction, and if it
2583 // is safe to emit the instruction here, try to do so now.
2584 if (Node->hasNUsesOfValue(1, 0)) {
2585 SDOperand TheVal = N.getValue(0);
2586 SDNode *User = 0;
2587 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2588 assert(UI != Node->use_end() && "Didn't find use!");
2589 SDNode *UN = *UI;
2590 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2591 if (UN->getOperand(i) == TheVal) {
2592 User = UN;
2593 goto FoundIt;
2594 }
2595 }
2596 FoundIt:
2597 // Only handle unary operators right now.
2598 if (User->getNumOperands() == 1) {
2599 LoweredTokens.erase(N);
2600 SelectExpr(SDOperand(User, 0));
2601 return;
2602 }
2603 }
2604 SelectExpr(N);
2605 return;
2606
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002607 case ISD::EXTLOAD:
2608 case ISD::SEXTLOAD:
2609 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002610 case ISD::CALL:
2611 case ISD::DYNAMIC_STACKALLOC:
2612 SelectExpr(N);
2613 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002614
2615 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2616 // On X86, we can represent all types except for Bool and Float natively.
2617 X86AddressMode AM;
2618 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002619 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2620 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2621 && "Unsupported TRUNCSTORE for this target!");
2622
2623 if (StoredTy == MVT::i16) {
2624 // FIXME: This is here just to allow testing. X86 doesn't really have a
2625 // TRUNCSTORE i16 operation, but this is required for targets that do not
2626 // have 16-bit integer registers. We occasionally disable 16-bit integer
2627 // registers to test the promotion code.
2628 Select(N.getOperand(0));
2629 Tmp1 = SelectExpr(N.getOperand(1));
2630 SelectAddress(N.getOperand(2), AM);
2631
2632 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2633 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2634 return;
2635 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002636
2637 // Store of constant bool?
2638 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2639 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2640 Select(N.getOperand(0));
2641 SelectAddress(N.getOperand(2), AM);
2642 } else {
2643 SelectAddress(N.getOperand(2), AM);
2644 Select(N.getOperand(0));
2645 }
2646 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2647 return;
2648 }
2649
2650 switch (StoredTy) {
2651 default: assert(0 && "Cannot truncstore this type!");
2652 case MVT::i1: Opc = X86::MOV8mr; break;
2653 case MVT::f32: Opc = X86::FST32m; break;
2654 }
2655
2656 std::vector<std::pair<unsigned, unsigned> > RP;
2657 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2658 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2659 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2660 std::sort(RP.begin(), RP.end());
2661
2662 for (unsigned i = 0; i != 3; ++i)
2663 switch (RP[2-i].second) {
2664 default: assert(0 && "Unknown operand number!");
2665 case 0: Select(N.getOperand(0)); break;
2666 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2667 case 2: SelectAddress(N.getOperand(2), AM); break;
2668 }
2669
2670 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2671 return;
2672 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002673 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002674 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002675
2676 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2677 Opc = 0;
2678 switch (CN->getValueType(0)) {
2679 default: assert(0 && "Invalid type for operation!");
2680 case MVT::i1:
2681 case MVT::i8: Opc = X86::MOV8mi; break;
2682 case MVT::i16: Opc = X86::MOV16mi; break;
2683 case MVT::i32: Opc = X86::MOV32mi; break;
2684 case MVT::f32:
2685 case MVT::f64: break;
2686 }
2687 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002688 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2689 Select(N.getOperand(0));
2690 SelectAddress(N.getOperand(2), AM);
2691 } else {
2692 SelectAddress(N.getOperand(2), AM);
2693 Select(N.getOperand(0));
2694 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002695 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2696 return;
2697 }
2698 }
Chris Lattner837caa72005-01-11 23:21:30 +00002699
2700 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002701 if (TryToFoldLoadOpStore(Node))
2702 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002703
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002704 switch (N.getOperand(1).getValueType()) {
2705 default: assert(0 && "Cannot store this type!");
2706 case MVT::i1:
2707 case MVT::i8: Opc = X86::MOV8mr; break;
2708 case MVT::i16: Opc = X86::MOV16mr; break;
2709 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002710 case MVT::f32: Opc = X86::FST32m; break;
2711 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002712 }
Chris Lattner11333092005-01-11 03:11:44 +00002713
2714 std::vector<std::pair<unsigned, unsigned> > RP;
2715 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2716 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2717 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2718 std::sort(RP.begin(), RP.end());
2719
2720 for (unsigned i = 0; i != 3; ++i)
2721 switch (RP[2-i].second) {
2722 default: assert(0 && "Unknown operand number!");
2723 case 0: Select(N.getOperand(0)); break;
2724 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002725 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002726 }
2727
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002728 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2729 return;
2730 }
2731 case ISD::ADJCALLSTACKDOWN:
2732 case ISD::ADJCALLSTACKUP:
2733 Select(N.getOperand(0));
2734 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2735
2736 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2737 X86::ADJCALLSTACKUP;
2738 BuildMI(BB, Opc, 1).addImm(Tmp1);
2739 return;
Chris Lattner989de032005-01-11 06:14:36 +00002740 case ISD::MEMSET: {
2741 Select(N.getOperand(0)); // Select the chain.
2742 unsigned Align =
2743 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2744 if (Align == 0) Align = 1;
2745
2746 // Turn the byte code into # iterations
2747 unsigned CountReg;
2748 unsigned Opcode;
2749 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2750 unsigned Val = ValC->getValue() & 255;
2751
2752 // If the value is a constant, then we can potentially use larger sets.
2753 switch (Align & 3) {
2754 case 2: // WORD aligned
2755 CountReg = MakeReg(MVT::i32);
2756 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2757 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2758 } else {
2759 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2760 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2761 }
2762 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2763 Opcode = X86::REP_STOSW;
2764 break;
2765 case 0: // DWORD aligned
2766 CountReg = MakeReg(MVT::i32);
2767 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2768 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2769 } else {
2770 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2771 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2772 }
2773 Val = (Val << 8) | Val;
2774 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2775 Opcode = X86::REP_STOSD;
2776 break;
2777 default: // BYTE aligned
2778 CountReg = SelectExpr(Node->getOperand(3));
2779 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2780 Opcode = X86::REP_STOSB;
2781 break;
2782 }
2783 } else {
2784 // If it's not a constant value we are storing, just fall back. We could
2785 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2786 unsigned ValReg = SelectExpr(Node->getOperand(2));
2787 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2788 CountReg = SelectExpr(Node->getOperand(3));
2789 Opcode = X86::REP_STOSB;
2790 }
2791
2792 // No matter what the alignment is, we put the source in ESI, the
2793 // destination in EDI, and the count in ECX.
2794 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2795 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2796 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2797 BuildMI(BB, Opcode, 0);
2798 return;
2799 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002800 case ISD::MEMCPY:
2801 Select(N.getOperand(0)); // Select the chain.
2802 unsigned Align =
2803 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2804 if (Align == 0) Align = 1;
2805
2806 // Turn the byte code into # iterations
2807 unsigned CountReg;
2808 unsigned Opcode;
2809 switch (Align & 3) {
2810 case 2: // WORD aligned
2811 CountReg = MakeReg(MVT::i32);
2812 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2813 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2814 } else {
2815 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2816 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2817 }
2818 Opcode = X86::REP_MOVSW;
2819 break;
2820 case 0: // DWORD aligned
2821 CountReg = MakeReg(MVT::i32);
2822 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2823 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2824 } else {
2825 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2826 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2827 }
2828 Opcode = X86::REP_MOVSD;
2829 break;
2830 default: // BYTE aligned
2831 CountReg = SelectExpr(Node->getOperand(3));
2832 Opcode = X86::REP_MOVSB;
2833 break;
2834 }
2835
2836 // No matter what the alignment is, we put the source in ESI, the
2837 // destination in EDI, and the count in ECX.
2838 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2839 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2840 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2841 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2842 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2843 BuildMI(BB, Opcode, 0);
2844 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002845 }
2846 assert(0 && "Should not be reached!");
2847}
2848
2849
2850/// createX86PatternInstructionSelector - This pass converts an LLVM function
2851/// into a machine code representation using pattern matching and a machine
2852/// description file.
2853///
2854FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2855 return new ISel(TM);
2856}