blob: c4adfa46faac727dc668c887e9747b39bb3cc3f0 [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>
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// X86TargetLowering - X86 Implementation of the TargetLowering interface
34namespace {
35 class X86TargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000037 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000038 public:
39 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 addRegisterClass(MVT::i8, X86::R8RegisterClass);
42 addRegisterClass(MVT::i16, X86::R16RegisterClass);
43 addRegisterClass(MVT::i32, X86::R32RegisterClass);
44 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
45
46 // FIXME: Eliminate these two classes when legalize can handle promotions
47 // well.
48 addRegisterClass(MVT::i1, X86::R8RegisterClass);
49 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
50
51 computeRegisterProperties();
Chris Lattner795069d2005-01-11 05:57:36 +000052
Chris Lattner795069d2005-01-11 05:57:36 +000053 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
54
Chris Lattner8acb1ba2005-01-07 07:49:41 +000055 setOperationUnsupported(ISD::MUL, MVT::i8);
56 setOperationUnsupported(ISD::SELECT, MVT::i1);
57 setOperationUnsupported(ISD::SELECT, MVT::i8);
58
59 addLegalFPImmediate(+0.0); // FLD0
60 addLegalFPImmediate(+1.0); // FLD1
61 addLegalFPImmediate(-0.0); // FLD0/FCHS
62 addLegalFPImmediate(-1.0); // FLD1/FCHS
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000072 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
74 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000075
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000086 };
87}
88
89
90std::vector<SDOperand>
91X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 std::vector<SDOperand> ArgValues;
93
94 // Add DAG nodes to load the arguments... On entry to a function on the X86,
95 // the stack frame looks like this:
96 //
97 // [ESP] -- return address
98 // [ESP + 4] -- first argument (leftmost lexically)
99 // [ESP + 8] -- second argument, if first argument is four bytes in size
100 // ...
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104
105 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
106 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
107 MVT::ValueType ObjectVT = getValueType(I->getType());
108 unsigned ArgIncrement = 4;
109 unsigned ObjSize;
110 switch (ObjectVT) {
111 default: assert(0 && "Unhandled argument type!");
112 case MVT::i1:
113 case MVT::i8: ObjSize = 1; break;
114 case MVT::i16: ObjSize = 2; break;
115 case MVT::i32: ObjSize = 4; break;
116 case MVT::i64: ObjSize = ArgIncrement = 8; break;
117 case MVT::f32: ObjSize = 4; break;
118 case MVT::f64: ObjSize = ArgIncrement = 8; break;
119 }
120 // Create the frame index object for this incoming parameter...
121 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
122
123 // Create the SelectionDAG nodes corresponding to a load from this parameter
124 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
125
126 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
127 // dead loads.
128 SDOperand ArgValue;
129 if (!I->use_empty())
130 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
131 else {
132 if (MVT::isInteger(ObjectVT))
133 ArgValue = DAG.getConstant(0, ObjectVT);
134 else
135 ArgValue = DAG.getConstantFP(0, ObjectVT);
136 }
137 ArgValues.push_back(ArgValue);
138
139 ArgOffset += ArgIncrement; // Move on to the next argument...
140 }
141
142 // If the function takes variable number of arguments, make a frame index for
143 // the start of the first vararg value... for expansion of llvm.va_start.
144 if (F.isVarArg())
145 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000146 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000147 return ArgValues;
148}
149
Chris Lattner5188ad72005-01-08 19:28:19 +0000150std::pair<SDOperand, SDOperand>
151X86TargetLowering::LowerCallTo(SDOperand Chain,
152 const Type *RetTy, SDOperand Callee,
153 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000154 // Count how many bytes are to be pushed on the stack.
155 unsigned NumBytes = 0;
156
157 if (Args.empty()) {
158 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000159 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
160 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 } else {
162 for (unsigned i = 0, e = Args.size(); i != e; ++i)
163 switch (getValueType(Args[i].second)) {
164 default: assert(0 && "Unknown value type!");
165 case MVT::i1:
166 case MVT::i8:
167 case MVT::i16:
168 case MVT::i32:
169 case MVT::f32:
170 NumBytes += 4;
171 break;
172 case MVT::i64:
173 case MVT::f64:
174 NumBytes += 8;
175 break;
176 }
177
Chris Lattner5188ad72005-01-08 19:28:19 +0000178 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
179 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000180
181 // Arguments go on the stack in reverse order, as specified by the ABI.
182 unsigned ArgOffset = 0;
183 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
184 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
185 unsigned ArgReg;
186 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
187 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
188
189 switch (getValueType(Args[i].second)) {
190 default: assert(0 && "Unexpected ValueType for argument!");
191 case MVT::i1:
192 case MVT::i8:
193 case MVT::i16:
194 // Promote the integer to 32 bits. If the input type is signed use a
195 // sign extend, otherwise use a zero extend.
196 if (Args[i].second->isSigned())
197 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
198 else
199 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
200
201 // FALL THROUGH
202 case MVT::i32:
203 case MVT::f32:
204 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
206 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 ArgOffset += 4;
208 break;
209 case MVT::i64:
210 case MVT::f64:
211 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000212 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
213 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000214 ArgOffset += 8;
215 break;
216 }
217 }
218 }
219
220 std::vector<MVT::ValueType> RetVals;
221 MVT::ValueType RetTyVT = getValueType(RetTy);
222 if (RetTyVT != MVT::isVoid)
223 RetVals.push_back(RetTyVT);
224 RetVals.push_back(MVT::Other);
225
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000227 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
229 DAG.getConstant(NumBytes, getPointerTy()));
230 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000231}
232
Chris Lattner14824582005-01-09 00:01:27 +0000233std::pair<SDOperand, SDOperand>
234X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
235 // vastart just returns the address of the VarArgsFrameIndex slot.
236 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
237}
238
239std::pair<SDOperand,SDOperand> X86TargetLowering::
240LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
241 const Type *ArgTy, SelectionDAG &DAG) {
242 MVT::ValueType ArgVT = getValueType(ArgTy);
243 SDOperand Result;
244 if (!isVANext) {
245 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
246 } else {
247 unsigned Amt;
248 if (ArgVT == MVT::i32)
249 Amt = 4;
250 else {
251 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
252 "Other types should have been promoted for varargs!");
253 Amt = 8;
254 }
255 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
256 DAG.getConstant(Amt, VAList.getValueType()));
257 }
258 return std::make_pair(Result, Chain);
259}
260
261
262std::pair<SDOperand, SDOperand> X86TargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 SDOperand Result;
266 if (Depth) // Depths > 0 not supported yet!
267 Result = DAG.getConstant(0, getPointerTy());
268 else {
269 if (ReturnAddrIndex == 0) {
270 // Set up a frame object for the return address.
271 MachineFunction &MF = DAG.getMachineFunction();
272 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
273 }
274
275 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
276
277 if (!isFrameAddress)
278 // Just load the return address
279 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
280 else
281 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
282 DAG.getConstant(4, MVT::i32));
283 }
284 return std::make_pair(Result, Chain);
285}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000286
287
288
289
290
291namespace {
292 Statistic<>
293 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
294
295 //===--------------------------------------------------------------------===//
296 /// ISel - X86 specific code to select X86 machine instructions for
297 /// SelectionDAG operations.
298 ///
299 class ISel : public SelectionDAGISel {
300 /// ContainsFPCode - Every instruction we select that uses or defines a FP
301 /// register should set this to true.
302 bool ContainsFPCode;
303
304 /// X86Lowering - This object fully describes how to lower LLVM code to an
305 /// X86-specific SelectionDAG.
306 X86TargetLowering X86Lowering;
307
Chris Lattner11333092005-01-11 03:11:44 +0000308 /// RegPressureMap - This keeps an approximate count of the number of
309 /// registers required to evaluate each node in the graph.
310 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000311
312 /// ExprMap - As shared expressions are codegen'd, we keep track of which
313 /// vreg the value is produced in, so we only emit one copy of each compiled
314 /// tree.
315 std::map<SDOperand, unsigned> ExprMap;
316 std::set<SDOperand> LoweredTokens;
317
318 public:
319 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
320 }
321
Chris Lattner11333092005-01-11 03:11:44 +0000322 unsigned getRegPressure(SDOperand O) {
323 return RegPressureMap[O.Val];
324 }
325 unsigned ComputeRegPressure(SDOperand O);
326
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000327 /// InstructionSelectBasicBlock - This callback is invoked by
328 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000329 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000330
Chris Lattnera5ade062005-01-11 21:19:59 +0000331 bool isFoldableLoad(SDOperand Op);
332 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
333
334
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000335 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000336 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000337 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
338 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000339 unsigned SelectExpr(SDOperand N);
340 bool SelectAddress(SDOperand N, X86AddressMode &AM);
341 void Select(SDOperand N);
342 };
343}
344
Chris Lattner7dbcb752005-01-12 04:21:28 +0000345/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
346/// when it has created a SelectionDAG for us to codegen.
347void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
348 // While we're doing this, keep track of whether we see any FP code for
349 // FP_REG_KILL insertion.
350 ContainsFPCode = false;
351
352 // Scan the PHI nodes that already are inserted into this basic block. If any
353 // of them is a PHI of a floating point value, we need to insert an
354 // FP_REG_KILL.
355 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
356 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
357 I != E; ++I) {
358 assert(I->getOpcode() == X86::PHI &&
359 "Isn't just PHI nodes?");
360 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
361 X86::RFPRegisterClass) {
362 ContainsFPCode = true;
363 break;
364 }
365 }
366
367 // Compute the RegPressureMap, which is an approximation for the number of
368 // registers required to compute each node.
369 ComputeRegPressure(DAG.getRoot());
370
371 // Codegen the basic block.
372 Select(DAG.getRoot());
373
374 // Finally, look at all of the successors of this block. If any contain a PHI
375 // node of FP type, we need to insert an FP_REG_KILL in this block.
376 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
377 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
378 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
379 I != E && I->getOpcode() == X86::PHI; ++I) {
380 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
381 X86::RFPRegisterClass) {
382 ContainsFPCode = true;
383 break;
384 }
385 }
386
387 // Insert FP_REG_KILL instructions into basic blocks that need them. This
388 // only occurs due to the floating point stackifier not being aggressive
389 // enough to handle arbitrary global stackification.
390 //
391 // Currently we insert an FP_REG_KILL instruction into each block that uses or
392 // defines a floating point virtual register.
393 //
394 // When the global register allocators (like linear scan) finally update live
395 // variable analysis, we can keep floating point values in registers across
396 // basic blocks. This will be a huge win, but we are waiting on the global
397 // allocators before we can do this.
398 //
399 if (ContainsFPCode && BB->succ_size()) {
400 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
401 ++NumFPKill;
402 }
403
404 // Clear state used for selection.
405 ExprMap.clear();
406 LoweredTokens.clear();
407 RegPressureMap.clear();
408}
409
410
Chris Lattner11333092005-01-11 03:11:44 +0000411// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
412// for the number of registers required to compute each node. This is basically
413// computing a generalized form of the Sethi-Ullman number for each node.
414unsigned ISel::ComputeRegPressure(SDOperand O) {
415 SDNode *N = O.Val;
416 unsigned &Result = RegPressureMap[N];
417 if (Result) return Result;
418
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000419 // FIXME: Should operations like CALL (which clobber lots o regs) have a
420 // higher fixed cost??
421
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000422 if (N->getNumOperands() == 0) {
423 Result = 1;
424 } else {
425 unsigned MaxRegUse = 0;
426 unsigned NumExtraMaxRegUsers = 0;
427 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
428 unsigned Regs;
429 if (N->getOperand(i).getOpcode() == ISD::Constant)
430 Regs = 0;
431 else
432 Regs = ComputeRegPressure(N->getOperand(i));
433 if (Regs > MaxRegUse) {
434 MaxRegUse = Regs;
435 NumExtraMaxRegUsers = 0;
436 } else if (Regs == MaxRegUse &&
437 N->getOperand(i).getValueType() != MVT::Other) {
438 ++NumExtraMaxRegUsers;
439 }
Chris Lattner11333092005-01-11 03:11:44 +0000440 }
Chris Lattner11333092005-01-11 03:11:44 +0000441
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000442 Result = MaxRegUse+NumExtraMaxRegUsers;
443 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000444
Chris Lattner837caa72005-01-11 23:21:30 +0000445 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000446 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000447}
448
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000449/// SelectAddress - Add the specified node to the specified addressing mode,
450/// returning true if it cannot be done.
451bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
452 switch (N.getOpcode()) {
453 default: break;
454 case ISD::FrameIndex:
455 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
456 AM.BaseType = X86AddressMode::FrameIndexBase;
457 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
458 return false;
459 }
460 break;
461 case ISD::GlobalAddress:
462 if (AM.GV == 0) {
463 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
464 return false;
465 }
466 break;
467 case ISD::Constant:
468 AM.Disp += cast<ConstantSDNode>(N)->getValue();
469 return false;
470 case ISD::SHL:
471 if (AM.IndexReg == 0 || AM.Scale == 1)
472 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
473 unsigned Val = CN->getValue();
474 if (Val == 1 || Val == 2 || Val == 3) {
475 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000476 SDOperand ShVal = N.Val->getOperand(0);
477
478 // Okay, we know that we have a scale by now. However, if the scaled
479 // value is an add of something and a constant, we can fold the
480 // constant into the disp field here.
481 if (ShVal.Val->getOpcode() == ISD::ADD &&
482 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
483 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
484 ConstantSDNode *AddVal =
485 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
486 AM.Disp += AddVal->getValue() << Val;
487 } else {
488 AM.IndexReg = SelectExpr(ShVal);
489 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000490 return false;
491 }
492 }
493 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000494 case ISD::MUL:
495 // X*[3,5,9] -> X+X*[2,4,8]
496 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
497 AM.Base.Reg == 0)
498 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
499 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
500 AM.Scale = unsigned(CN->getValue())-1;
501
502 SDOperand MulVal = N.Val->getOperand(0);
503 unsigned Reg;
504
505 // Okay, we know that we have a scale by now. However, if the scaled
506 // value is an add of something and a constant, we can fold the
507 // constant into the disp field here.
508 if (MulVal.Val->getOpcode() == ISD::ADD &&
509 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
510 Reg = SelectExpr(MulVal.Val->getOperand(0));
511 ConstantSDNode *AddVal =
512 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
513 AM.Disp += AddVal->getValue() * CN->getValue();
514 } else {
515 Reg = SelectExpr(N.Val->getOperand(0));
516 }
517
518 AM.IndexReg = AM.Base.Reg = Reg;
519 return false;
520 }
521 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000522
523 case ISD::ADD: {
524 X86AddressMode Backup = AM;
525 if (!SelectAddress(N.Val->getOperand(0), AM) &&
526 !SelectAddress(N.Val->getOperand(1), AM))
527 return false;
528 AM = Backup;
529 break;
530 }
531 }
532
Chris Lattnera95589b2005-01-11 04:40:19 +0000533 // Is the base register already occupied?
534 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
535 // If so, check to see if the scale index register is set.
536 if (AM.IndexReg == 0) {
537 AM.IndexReg = SelectExpr(N);
538 AM.Scale = 1;
539 return false;
540 }
541
542 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000543 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000544 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545
546 // Default, generate it as a register.
547 AM.BaseType = X86AddressMode::RegBase;
548 AM.Base.Reg = SelectExpr(N);
549 return false;
550}
551
552/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
553/// assuming that the temporary registers are in the 8-bit register class.
554///
555/// Tmp1 = setcc1
556/// Tmp2 = setcc2
557/// DestReg = logicalop Tmp1, Tmp2
558///
559static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
560 unsigned SetCC2, unsigned LogicalOp,
561 unsigned DestReg) {
562 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
563 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
564 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
565 BuildMI(BB, SetCC1, 0, Tmp1);
566 BuildMI(BB, SetCC2, 0, Tmp2);
567 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
568}
569
570/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
571/// condition codes match the specified SetCCOpcode. Note that some conditions
572/// require multiple instructions to generate the correct value.
573static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
574 ISD::CondCode SetCCOpcode, bool isFP) {
575 unsigned Opc;
576 if (!isFP) {
577 switch (SetCCOpcode) {
578 default: assert(0 && "Illegal integer SetCC!");
579 case ISD::SETEQ: Opc = X86::SETEr; break;
580 case ISD::SETGT: Opc = X86::SETGr; break;
581 case ISD::SETGE: Opc = X86::SETGEr; break;
582 case ISD::SETLT: Opc = X86::SETLr; break;
583 case ISD::SETLE: Opc = X86::SETLEr; break;
584 case ISD::SETNE: Opc = X86::SETNEr; break;
585 case ISD::SETULT: Opc = X86::SETBr; break;
586 case ISD::SETUGT: Opc = X86::SETAr; break;
587 case ISD::SETULE: Opc = X86::SETBEr; break;
588 case ISD::SETUGE: Opc = X86::SETAEr; break;
589 }
590 } else {
591 // On a floating point condition, the flags are set as follows:
592 // ZF PF CF op
593 // 0 | 0 | 0 | X > Y
594 // 0 | 0 | 1 | X < Y
595 // 1 | 0 | 0 | X == Y
596 // 1 | 1 | 1 | unordered
597 //
598 switch (SetCCOpcode) {
599 default: assert(0 && "Invalid FP setcc!");
600 case ISD::SETUEQ:
601 case ISD::SETEQ:
602 Opc = X86::SETEr; // True if ZF = 1
603 break;
604 case ISD::SETOGT:
605 case ISD::SETGT:
606 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
607 break;
608 case ISD::SETOGE:
609 case ISD::SETGE:
610 Opc = X86::SETAEr; // True if CF = 0
611 break;
612 case ISD::SETULT:
613 case ISD::SETLT:
614 Opc = X86::SETBr; // True if CF = 1
615 break;
616 case ISD::SETULE:
617 case ISD::SETLE:
618 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
619 break;
620 case ISD::SETONE:
621 case ISD::SETNE:
622 Opc = X86::SETNEr; // True if ZF = 0
623 break;
624 case ISD::SETUO:
625 Opc = X86::SETPr; // True if PF = 1
626 break;
627 case ISD::SETO:
628 Opc = X86::SETNPr; // True if PF = 0
629 break;
630 case ISD::SETOEQ: // !PF & ZF
631 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
632 return;
633 case ISD::SETOLT: // !PF & CF
634 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
635 return;
636 case ISD::SETOLE: // !PF & (CF || ZF)
637 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
638 return;
639 case ISD::SETUGT: // PF | (!ZF & !CF)
640 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
641 return;
642 case ISD::SETUGE: // PF | !CF
643 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
644 return;
645 case ISD::SETUNE: // PF | !ZF
646 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
647 return;
648 }
649 }
650 BuildMI(BB, Opc, 0, DestReg);
651}
652
653
654/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
655/// the Dest block if the Cond condition is true. If we cannot fold this
656/// condition into the branch, return true.
657///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000658bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
659 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000660 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
661 // B) using two conditional branches instead of one condbr, two setcc's, and
662 // an or.
663 if ((Cond.getOpcode() == ISD::OR ||
664 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
665 // And and or set the flags for us, so there is no need to emit a TST of the
666 // result. It is only safe to do this if there is only a single use of the
667 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000668 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000669 SelectExpr(Cond);
670 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
671 return false;
672 }
673
674 // Codegen br not C -> JE.
675 if (Cond.getOpcode() == ISD::XOR)
676 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
677 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000678 unsigned CondR;
679 if (getRegPressure(Chain) > getRegPressure(Cond)) {
680 Select(Chain);
681 CondR = SelectExpr(Cond.Val->getOperand(0));
682 } else {
683 CondR = SelectExpr(Cond.Val->getOperand(0));
684 Select(Chain);
685 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000686 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
687 BuildMI(BB, X86::JE, 1).addMBB(Dest);
688 return false;
689 }
690
691 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
692 if (SetCC == 0)
693 return true; // Can only handle simple setcc's so far.
694
695 unsigned Opc;
696
697 // Handle integer conditions first.
698 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
699 switch (SetCC->getCondition()) {
700 default: assert(0 && "Illegal integer SetCC!");
701 case ISD::SETEQ: Opc = X86::JE; break;
702 case ISD::SETGT: Opc = X86::JG; break;
703 case ISD::SETGE: Opc = X86::JGE; break;
704 case ISD::SETLT: Opc = X86::JL; break;
705 case ISD::SETLE: Opc = X86::JLE; break;
706 case ISD::SETNE: Opc = X86::JNE; break;
707 case ISD::SETULT: Opc = X86::JB; break;
708 case ISD::SETUGT: Opc = X86::JA; break;
709 case ISD::SETULE: Opc = X86::JBE; break;
710 case ISD::SETUGE: Opc = X86::JAE; break;
711 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000712 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000713 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
714 BuildMI(BB, Opc, 1).addMBB(Dest);
715 return false;
716 }
717
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000718 unsigned Opc2 = 0; // Second branch if needed.
719
720 // On a floating point condition, the flags are set as follows:
721 // ZF PF CF op
722 // 0 | 0 | 0 | X > Y
723 // 0 | 0 | 1 | X < Y
724 // 1 | 0 | 0 | X == Y
725 // 1 | 1 | 1 | unordered
726 //
727 switch (SetCC->getCondition()) {
728 default: assert(0 && "Invalid FP setcc!");
729 case ISD::SETUEQ:
730 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
731 case ISD::SETOGT:
732 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
733 case ISD::SETOGE:
734 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
735 case ISD::SETULT:
736 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
737 case ISD::SETULE:
738 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
739 case ISD::SETONE:
740 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
741 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
742 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
743 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
744 Opc = X86::JA; // ZF = 0 & CF = 0
745 Opc2 = X86::JP; // PF = 1
746 break;
747 case ISD::SETUGE: // PF = 1 | CF = 0
748 Opc = X86::JAE; // CF = 0
749 Opc2 = X86::JP; // PF = 1
750 break;
751 case ISD::SETUNE: // PF = 1 | ZF = 0
752 Opc = X86::JNE; // ZF = 0
753 Opc2 = X86::JP; // PF = 1
754 break;
755 case ISD::SETOEQ: // PF = 0 & ZF = 1
756 //X86::JNP, X86::JE
757 //X86::AND8rr
758 return true; // FIXME: Emit more efficient code for this branch.
759 case ISD::SETOLT: // PF = 0 & CF = 1
760 //X86::JNP, X86::JB
761 //X86::AND8rr
762 return true; // FIXME: Emit more efficient code for this branch.
763 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
764 //X86::JNP, X86::JBE
765 //X86::AND8rr
766 return true; // FIXME: Emit more efficient code for this branch.
767 }
768
Chris Lattner6c07aee2005-01-11 04:06:27 +0000769 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000770 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
771 BuildMI(BB, Opc, 1).addMBB(Dest);
772 if (Opc2)
773 BuildMI(BB, Opc2, 1).addMBB(Dest);
774 return false;
775}
776
Chris Lattner24aad1b2005-01-10 22:10:13 +0000777/// EmitSelectCC - Emit code into BB that performs a select operation between
778/// the two registers RTrue and RFalse, generating a result into RDest. Return
779/// true if the fold cannot be performed.
780///
781void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
782 unsigned RTrue, unsigned RFalse, unsigned RDest) {
783 enum Condition {
784 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
785 NOT_SET
786 } CondCode = NOT_SET;
787
788 static const unsigned CMOVTAB16[] = {
789 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
790 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
791 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
792 };
793 static const unsigned CMOVTAB32[] = {
794 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
795 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
796 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
797 };
798 static const unsigned CMOVTABFP[] = {
799 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
800 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
801 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
802 };
803
804 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
805 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
806 switch (SetCC->getCondition()) {
807 default: assert(0 && "Unknown integer comparison!");
808 case ISD::SETEQ: CondCode = EQ; break;
809 case ISD::SETGT: CondCode = GT; break;
810 case ISD::SETGE: CondCode = GE; break;
811 case ISD::SETLT: CondCode = LT; break;
812 case ISD::SETLE: CondCode = LE; break;
813 case ISD::SETNE: CondCode = NE; break;
814 case ISD::SETULT: CondCode = B; break;
815 case ISD::SETUGT: CondCode = A; break;
816 case ISD::SETULE: CondCode = BE; break;
817 case ISD::SETUGE: CondCode = AE; break;
818 }
819 } else {
820 // On a floating point condition, the flags are set as follows:
821 // ZF PF CF op
822 // 0 | 0 | 0 | X > Y
823 // 0 | 0 | 1 | X < Y
824 // 1 | 0 | 0 | X == Y
825 // 1 | 1 | 1 | unordered
826 //
827 switch (SetCC->getCondition()) {
828 default: assert(0 && "Unknown FP comparison!");
829 case ISD::SETUEQ:
830 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
831 case ISD::SETOGT:
832 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
833 case ISD::SETOGE:
834 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
835 case ISD::SETULT:
836 case ISD::SETLT: CondCode = B; break; // True if CF = 1
837 case ISD::SETULE:
838 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
839 case ISD::SETONE:
840 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
841 case ISD::SETUO: CondCode = P; break; // True if PF = 1
842 case ISD::SETO: CondCode = NP; break; // True if PF = 0
843 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
844 case ISD::SETUGE: // PF = 1 | CF = 0
845 case ISD::SETUNE: // PF = 1 | ZF = 0
846 case ISD::SETOEQ: // PF = 0 & ZF = 1
847 case ISD::SETOLT: // PF = 0 & CF = 1
848 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
849 // We cannot emit this comparison as a single cmov.
850 break;
851 }
852 }
853 }
854
855 unsigned Opc = 0;
856 if (CondCode != NOT_SET) {
857 switch (SVT) {
858 default: assert(0 && "Cannot select this type!");
859 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
860 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
861 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000862 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000863 }
864 }
865
866 // Finally, if we weren't able to fold this, just emit the condition and test
867 // it.
868 if (CondCode == NOT_SET || Opc == 0) {
869 // Get the condition into the zero flag.
870 unsigned CondReg = SelectExpr(Cond);
871 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
872
873 switch (SVT) {
874 default: assert(0 && "Cannot select this type!");
875 case MVT::i16: Opc = X86::CMOVE16rr; break;
876 case MVT::i32: Opc = X86::CMOVE32rr; break;
877 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000878 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000879 }
880 } else {
881 // FIXME: CMP R, 0 -> TEST R, R
882 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000883 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000884 }
885 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
886}
887
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000888void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000889 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000890 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
891 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000892 if (isFoldableLoad(LHS)) {
893 switch (RHS.getValueType()) {
894 default: break;
895 case MVT::i1:
896 case MVT::i8: Opc = X86::CMP8mi; break;
897 case MVT::i16: Opc = X86::CMP16mi; break;
898 case MVT::i32: Opc = X86::CMP32mi; break;
899 }
900 if (Opc) {
901 X86AddressMode AM;
902 EmitFoldedLoad(LHS, AM);
903 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
904 return;
905 }
906 }
907
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000908 switch (RHS.getValueType()) {
909 default: break;
910 case MVT::i1:
911 case MVT::i8: Opc = X86::CMP8ri; break;
912 case MVT::i16: Opc = X86::CMP16ri; break;
913 case MVT::i32: Opc = X86::CMP32ri; break;
914 }
915 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000916 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000917 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
918 return;
919 }
920 }
921
Chris Lattneref6806c2005-01-12 02:02:48 +0000922 Opc = 0;
923 if (isFoldableLoad(LHS)) {
924 switch (RHS.getValueType()) {
925 default: break;
926 case MVT::i1:
927 case MVT::i8: Opc = X86::CMP8mr; break;
928 case MVT::i16: Opc = X86::CMP16mr; break;
929 case MVT::i32: Opc = X86::CMP32mr; break;
930 }
931 if (Opc) {
932 X86AddressMode AM;
933 unsigned Reg;
934 if (getRegPressure(LHS) > getRegPressure(RHS)) {
935 EmitFoldedLoad(LHS, AM);
936 Reg = SelectExpr(RHS);
937 } else {
938 Reg = SelectExpr(RHS);
939 EmitFoldedLoad(LHS, AM);
940 }
941 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
942 return;
943 }
944 }
945
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000946 switch (LHS.getValueType()) {
947 default: assert(0 && "Cannot compare this value!");
948 case MVT::i1:
949 case MVT::i8: Opc = X86::CMP8rr; break;
950 case MVT::i16: Opc = X86::CMP16rr; break;
951 case MVT::i32: Opc = X86::CMP32rr; break;
952 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000953 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000954 }
Chris Lattner11333092005-01-11 03:11:44 +0000955 unsigned Tmp1, Tmp2;
956 if (getRegPressure(LHS) > getRegPressure(RHS)) {
957 Tmp1 = SelectExpr(LHS);
958 Tmp2 = SelectExpr(RHS);
959 } else {
960 Tmp2 = SelectExpr(RHS);
961 Tmp1 = SelectExpr(LHS);
962 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000963 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
964}
965
Chris Lattnera5ade062005-01-11 21:19:59 +0000966/// isFoldableLoad - Return true if this is a load instruction that can safely
967/// be folded into an operation that uses it.
968bool ISel::isFoldableLoad(SDOperand Op) {
969 if (Op.getOpcode() != ISD::LOAD ||
970 // FIXME: currently can't fold constant pool indexes.
971 isa<ConstantPoolSDNode>(Op.getOperand(1)))
972 return false;
973
974 // If this load has already been emitted, we clearly can't fold it.
975 if (ExprMap.count(Op)) return false;
976
977 return Op.Val->use_size() == 2;
978}
979
980/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
981/// and compute the address being loaded into AM.
982void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
983 SDOperand Chain = Op.getOperand(0);
984 SDOperand Address = Op.getOperand(1);
985 if (getRegPressure(Chain) > getRegPressure(Address)) {
986 Select(Chain);
987 SelectAddress(Address, AM);
988 } else {
989 SelectAddress(Address, AM);
990 Select(Chain);
991 }
992
993 // The chain for this load is now lowered.
994 LoweredTokens.insert(SDOperand(Op.Val, 1));
995 ExprMap[SDOperand(Op.Val, 1)] = 1;
996}
997
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000998unsigned ISel::SelectExpr(SDOperand N) {
999 unsigned Result;
1000 unsigned Tmp1, Tmp2, Tmp3;
1001 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001002 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001003 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001004
Chris Lattner590d8002005-01-09 18:52:44 +00001005 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001006 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +00001007 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +00001008
1009 unsigned &Reg = ExprMap[N];
1010 if (Reg) return Reg;
1011
1012 if (N.getOpcode() != ISD::CALL)
1013 Reg = Result = (N.getValueType() != MVT::Other) ?
1014 MakeReg(N.getValueType()) : 1;
1015 else {
1016 // If this is a call instruction, make sure to prepare ALL of the result
1017 // values as well as the chain.
1018 if (Node->getNumValues() == 1)
1019 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001020 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001021 Result = MakeReg(Node->getValueType(0));
1022 ExprMap[N.getValue(0)] = Result;
1023 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1024 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1025 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001026 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001027 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001028
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001029 switch (N.getOpcode()) {
1030 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001031 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001032 assert(0 && "Node not handled!\n");
1033 case ISD::FrameIndex:
1034 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1035 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1036 return Result;
1037 case ISD::ConstantPool:
1038 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1039 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1040 return Result;
1041 case ISD::ConstantFP:
1042 ContainsFPCode = true;
1043 Tmp1 = Result; // Intermediate Register
1044 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1045 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1046 Tmp1 = MakeReg(MVT::f64);
1047
1048 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1049 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1050 BuildMI(BB, X86::FLD0, 0, Tmp1);
1051 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1052 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1053 BuildMI(BB, X86::FLD1, 0, Tmp1);
1054 else
1055 assert(0 && "Unexpected constant!");
1056 if (Tmp1 != Result)
1057 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1058 return Result;
1059 case ISD::Constant:
1060 switch (N.getValueType()) {
1061 default: assert(0 && "Cannot use constants of this type!");
1062 case MVT::i1:
1063 case MVT::i8: Opc = X86::MOV8ri; break;
1064 case MVT::i16: Opc = X86::MOV16ri; break;
1065 case MVT::i32: Opc = X86::MOV32ri; break;
1066 }
1067 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1068 return Result;
1069 case ISD::GlobalAddress: {
1070 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1071 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1072 return Result;
1073 }
1074 case ISD::ExternalSymbol: {
1075 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1076 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1077 return Result;
1078 }
1079 case ISD::FP_EXTEND:
1080 Tmp1 = SelectExpr(N.getOperand(0));
1081 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001082 return Result;
1083 case ISD::ZERO_EXTEND: {
1084 int DestIs16 = N.getValueType() == MVT::i16;
1085 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001086
1087 // FIXME: This hack is here for zero extension casts from bool to i8. This
1088 // would not be needed if bools were promoted by Legalize.
1089 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001090 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001091 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1092 return Result;
1093 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001094
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001095 if (isFoldableLoad(N.getOperand(0))) {
1096 static const unsigned Opc[3] = {
1097 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1098 };
1099
1100 X86AddressMode AM;
1101 EmitFoldedLoad(N.getOperand(0), AM);
1102 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1103
1104 return Result;
1105 }
1106
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107 static const unsigned Opc[3] = {
1108 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1109 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001110 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001111 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1112 return Result;
1113 }
1114 case ISD::SIGN_EXTEND: {
1115 int DestIs16 = N.getValueType() == MVT::i16;
1116 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1117
Chris Lattner590d8002005-01-09 18:52:44 +00001118 // FIXME: Legalize should promote bools to i8!
1119 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1120 "Sign extend from bool not implemented!");
1121
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001122 if (isFoldableLoad(N.getOperand(0))) {
1123 static const unsigned Opc[3] = {
1124 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1125 };
1126
1127 X86AddressMode AM;
1128 EmitFoldedLoad(N.getOperand(0), AM);
1129 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1130 return Result;
1131 }
1132
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001133 static const unsigned Opc[3] = {
1134 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1135 };
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1138 return Result;
1139 }
1140 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001141 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1142 if (isFoldableLoad(N.getOperand(0))) {
1143 switch (N.getValueType()) {
1144 default: assert(0 && "Unknown truncate!");
1145 case MVT::i1:
1146 case MVT::i8: Opc = X86::MOV8rm; break;
1147 case MVT::i16: Opc = X86::MOV16rm; break;
1148 }
1149 X86AddressMode AM;
1150 EmitFoldedLoad(N.getOperand(0), AM);
1151 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1152 return Result;
1153 }
1154
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001155 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1156 // a move out of AX or AL.
1157 switch (N.getOperand(0).getValueType()) {
1158 default: assert(0 && "Unknown truncate!");
1159 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1160 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1161 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1162 }
1163 Tmp1 = SelectExpr(N.getOperand(0));
1164 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1165
1166 switch (N.getValueType()) {
1167 default: assert(0 && "Unknown truncate!");
1168 case MVT::i1:
1169 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1170 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1171 }
1172 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1173 return Result;
1174
1175 case ISD::FP_ROUND:
1176 // Truncate from double to float by storing to memory as float,
1177 // then reading it back into a register.
1178
1179 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001180 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001181 Tmp1 = TLI.getTargetData().getFloatAlignment();
1182 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1183
1184 // Codegen the input.
1185 Tmp1 = SelectExpr(N.getOperand(0));
1186
1187 // Emit the store, then the reload.
1188 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1189 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001190 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001191
1192 case ISD::SINT_TO_FP:
1193 case ISD::UINT_TO_FP: {
1194 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001195 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001196
1197 // Promote the integer to a type supported by FLD. We do this because there
1198 // are no unsigned FLD instructions, so we must promote an unsigned value to
1199 // a larger signed value, then use FLD on the larger value.
1200 //
1201 MVT::ValueType PromoteType = MVT::Other;
1202 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1203 unsigned PromoteOpcode = 0;
1204 unsigned RealDestReg = Result;
1205 switch (SrcTy) {
1206 case MVT::i1:
1207 case MVT::i8:
1208 // We don't have the facilities for directly loading byte sized data from
1209 // memory (even signed). Promote it to 16 bits.
1210 PromoteType = MVT::i16;
1211 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1212 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1213 break;
1214 case MVT::i16:
1215 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1216 PromoteType = MVT::i32;
1217 PromoteOpcode = X86::MOVZX32rr16;
1218 }
1219 break;
1220 default:
1221 // Don't fild into the real destination.
1222 if (Node->getOpcode() == ISD::UINT_TO_FP)
1223 Result = MakeReg(Node->getValueType(0));
1224 break;
1225 }
1226
1227 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1228
1229 if (PromoteType != MVT::Other) {
1230 Tmp2 = MakeReg(PromoteType);
1231 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1232 SrcTy = PromoteType;
1233 Tmp1 = Tmp2;
1234 }
1235
1236 // Spill the integer to memory and reload it from there.
1237 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1238 MachineFunction *F = BB->getParent();
1239 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1240
1241 switch (SrcTy) {
1242 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001243 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001244 // FIXME: this won't work for cast [u]long to FP
1245 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1246 FrameIdx).addReg(Tmp1);
1247 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1248 FrameIdx, 4).addReg(Tmp1+1);
1249 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1250 break;
1251 case MVT::i32:
1252 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1253 FrameIdx).addReg(Tmp1);
1254 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1255 break;
1256 case MVT::i16:
1257 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1258 FrameIdx).addReg(Tmp1);
1259 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1260 break;
1261 default: break; // No promotion required.
1262 }
1263
Chris Lattner085c9952005-01-12 04:00:00 +00001264 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001265 // If this is a cast from uint -> double, we need to be careful when if
1266 // the "sign" bit is set. If so, we don't want to make a negative number,
1267 // we want to make a positive number. Emit code to add an offset if the
1268 // sign bit is set.
1269
1270 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1271 unsigned IsNeg = MakeReg(MVT::i32);
1272 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1273
1274 // Create a CP value that has the offset in one word and 0 in the other.
1275 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1276 0x4f80000000000000ULL);
1277 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1278 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1279 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1280
1281 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1282 // We need special handling for unsigned 64-bit integer sources. If the
1283 // input number has the "sign bit" set, then we loaded it incorrectly as a
1284 // negative 64-bit number. In this case, add an offset value.
1285
1286 // Emit a test instruction to see if the dynamic input value was signed.
1287 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1288
1289 // If the sign bit is set, get a pointer to an offset, otherwise get a
1290 // pointer to a zero.
1291 MachineConstantPool *CP = F->getConstantPool();
1292 unsigned Zero = MakeReg(MVT::i32);
1293 Constant *Null = Constant::getNullValue(Type::UIntTy);
1294 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1295 CP->getConstantPoolIndex(Null));
1296 unsigned Offset = MakeReg(MVT::i32);
1297 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1298
1299 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1300 CP->getConstantPoolIndex(OffsetCst));
1301 unsigned Addr = MakeReg(MVT::i32);
1302 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1303
1304 // Load the constant for an add. FIXME: this could make an 'fadd' that
1305 // reads directly from memory, but we don't support these yet.
1306 unsigned ConstReg = MakeReg(MVT::f64);
1307 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1308
1309 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1310 }
1311 return RealDestReg;
1312 }
1313 case ISD::FP_TO_SINT:
1314 case ISD::FP_TO_UINT: {
1315 // FIXME: Most of this grunt work should be done by legalize!
1316 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1317
1318 // Change the floating point control register to use "round towards zero"
1319 // mode when truncating to an integer value.
1320 //
1321 MachineFunction *F = BB->getParent();
1322 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1323 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1324
1325 // Load the old value of the high byte of the control word...
1326 unsigned HighPartOfCW = MakeReg(MVT::i8);
1327 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1328 CWFrameIdx, 1);
1329
1330 // Set the high part to be round to zero...
1331 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1332 CWFrameIdx, 1).addImm(12);
1333
1334 // Reload the modified control word now...
1335 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1336
1337 // Restore the memory image of control word to original value
1338 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1339 CWFrameIdx, 1).addReg(HighPartOfCW);
1340
1341 // We don't have the facilities for directly storing byte sized data to
1342 // memory. Promote it to 16 bits. We also must promote unsigned values to
1343 // larger classes because we only have signed FP stores.
1344 MVT::ValueType StoreClass = Node->getValueType(0);
1345 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1346 switch (StoreClass) {
1347 case MVT::i8: StoreClass = MVT::i16; break;
1348 case MVT::i16: StoreClass = MVT::i32; break;
1349 case MVT::i32: StoreClass = MVT::i64; break;
1350 // The following treatment of cLong may not be perfectly right,
1351 // but it survives chains of casts of the form
1352 // double->ulong->double.
1353 case MVT::i64: StoreClass = MVT::i64; break;
1354 default: assert(0 && "Unknown store class!");
1355 }
1356
1357 // Spill the integer to memory and reload it from there.
1358 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1359 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1360
1361 switch (StoreClass) {
1362 default: assert(0 && "Unknown store class!");
1363 case MVT::i16:
1364 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1365 break;
1366 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001367 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001368 break;
1369 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001370 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001371 break;
1372 }
1373
1374 switch (Node->getValueType(0)) {
1375 default:
1376 assert(0 && "Unknown integer type!");
1377 case MVT::i64:
1378 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001379 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001380 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1381 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1382 case MVT::i32:
1383 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1384 break;
1385 case MVT::i16:
1386 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1387 break;
1388 case MVT::i8:
1389 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1390 break;
1391 }
1392
1393 // Reload the original control word now.
1394 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1395 return Result;
1396 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001398 Op0 = N.getOperand(0);
1399 Op1 = N.getOperand(1);
1400
1401 if (isFoldableLoad(Op0))
1402 std::swap(Op0, Op1);
1403
1404 if (isFoldableLoad(Op1)) {
1405 switch (N.getValueType()) {
1406 default: assert(0 && "Cannot add this type!");
1407 case MVT::i1:
1408 case MVT::i8: Opc = X86::ADD8rm; break;
1409 case MVT::i16: Opc = X86::ADD16rm; break;
1410 case MVT::i32: Opc = X86::ADD32rm; break;
1411 case MVT::f32: Opc = X86::FADD32m; break;
1412 case MVT::f64: Opc = X86::FADD64m; break;
1413 }
1414 X86AddressMode AM;
1415 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1416 Tmp1 = SelectExpr(Op0);
1417 EmitFoldedLoad(Op1, AM);
1418 } else {
1419 EmitFoldedLoad(Op1, AM);
1420 Tmp1 = SelectExpr(Op0);
1421 }
1422 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1423 return Result;
1424 }
1425
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001426 // See if we can codegen this as an LEA to fold operations together.
1427 if (N.getValueType() == MVT::i32) {
1428 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001429 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001430 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001431 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 // leave this as LEA, then peephole it to 'ADD' after two address elim
1433 // happens.
1434 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001435 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1437 return Result;
1438 }
1439 }
1440 }
Chris Lattner11333092005-01-11 03:11:44 +00001441
Chris Lattnera5ade062005-01-11 21:19:59 +00001442 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001443 Opc = 0;
1444 if (CN->getValue() == 1) { // add X, 1 -> inc X
1445 switch (N.getValueType()) {
1446 default: assert(0 && "Cannot integer add this type!");
1447 case MVT::i8: Opc = X86::INC8r; break;
1448 case MVT::i16: Opc = X86::INC16r; break;
1449 case MVT::i32: Opc = X86::INC32r; break;
1450 }
1451 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1452 switch (N.getValueType()) {
1453 default: assert(0 && "Cannot integer add this type!");
1454 case MVT::i8: Opc = X86::DEC8r; break;
1455 case MVT::i16: Opc = X86::DEC16r; break;
1456 case MVT::i32: Opc = X86::DEC32r; break;
1457 }
1458 }
1459
1460 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001461 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1463 return Result;
1464 }
1465
1466 switch (N.getValueType()) {
1467 default: assert(0 && "Cannot add this type!");
1468 case MVT::i8: Opc = X86::ADD8ri; break;
1469 case MVT::i16: Opc = X86::ADD16ri; break;
1470 case MVT::i32: Opc = X86::ADD32ri; break;
1471 }
1472 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001473 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001474 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1475 return Result;
1476 }
1477 }
1478
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001479 switch (N.getValueType()) {
1480 default: assert(0 && "Cannot add this type!");
1481 case MVT::i8: Opc = X86::ADD8rr; break;
1482 case MVT::i16: Opc = X86::ADD16rr; break;
1483 case MVT::i32: Opc = X86::ADD32rr; break;
1484 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001485 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001486 }
Chris Lattner11333092005-01-11 03:11:44 +00001487
Chris Lattnera5ade062005-01-11 21:19:59 +00001488 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1489 Tmp1 = SelectExpr(Op0);
1490 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001491 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001492 Tmp2 = SelectExpr(Op1);
1493 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001494 }
1495
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001496 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1497 return Result;
1498 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001499 case ISD::MUL:
1500 case ISD::AND:
1501 case ISD::OR:
1502 case ISD::XOR:
1503 static const unsigned SUBTab[] = {
1504 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1505 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1506 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1507 };
1508 static const unsigned MULTab[] = {
1509 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1510 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1511 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1512 };
1513 static const unsigned ANDTab[] = {
1514 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1515 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1516 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1517 };
1518 static const unsigned ORTab[] = {
1519 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1520 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1521 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1522 };
1523 static const unsigned XORTab[] = {
1524 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1525 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1526 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1527 };
1528
1529 Op0 = Node->getOperand(0);
1530 Op1 = Node->getOperand(1);
1531
1532 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001533 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1534 if (CN->isNullValue()) { // 0 - N -> neg N
1535 switch (N.getValueType()) {
1536 default: assert(0 && "Cannot sub this type!");
1537 case MVT::i1:
1538 case MVT::i8: Opc = X86::NEG8r; break;
1539 case MVT::i16: Opc = X86::NEG16r; break;
1540 case MVT::i32: Opc = X86::NEG32r; break;
1541 }
1542 Tmp1 = SelectExpr(N.getOperand(1));
1543 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1544 return Result;
1545 }
1546
Chris Lattnera5ade062005-01-11 21:19:59 +00001547 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1548 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001549 switch (N.getValueType()) {
1550 default: assert(0 && "Cannot add this type!");
1551 case MVT::i1:
1552 case MVT::i8: Opc = X86::NOT8r; break;
1553 case MVT::i16: Opc = X86::NOT16r; break;
1554 case MVT::i32: Opc = X86::NOT32r; break;
1555 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001556 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001557 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1558 return Result;
1559 }
1560
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001561 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001562 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001563 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001564 case MVT::i8: Opc = 0; break;
1565 case MVT::i16: Opc = 1; break;
1566 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001567 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001568 switch (Node->getOpcode()) {
1569 default: assert(0 && "Unreachable!");
1570 case ISD::SUB: Opc = SUBTab[Opc]; break;
1571 case ISD::MUL: Opc = MULTab[Opc]; break;
1572 case ISD::AND: Opc = ANDTab[Opc]; break;
1573 case ISD::OR: Opc = ORTab[Opc]; break;
1574 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001575 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001576 if (Opc) { // Can't fold MUL:i8 R, imm
1577 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001578 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1579 return Result;
1580 }
1581 }
Chris Lattner11333092005-01-11 03:11:44 +00001582
Chris Lattnera5ade062005-01-11 21:19:59 +00001583 if (isFoldableLoad(Op0))
1584 if (Node->getOpcode() != ISD::SUB) {
1585 std::swap(Op0, Op1);
1586 } else {
1587 // Emit 'reverse' subract, with a memory operand.
1588 switch (N.getValueType()) {
1589 default: Opc = 0; break;
1590 case MVT::f32: Opc = X86::FSUBR32m; break;
1591 case MVT::f64: Opc = X86::FSUBR64m; break;
1592 }
1593 if (Opc) {
1594 X86AddressMode AM;
1595 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1596 EmitFoldedLoad(Op0, AM);
1597 Tmp1 = SelectExpr(Op1);
1598 } else {
1599 Tmp1 = SelectExpr(Op1);
1600 EmitFoldedLoad(Op0, AM);
1601 }
1602 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1603 return Result;
1604 }
1605 }
1606
1607 if (isFoldableLoad(Op1)) {
1608 switch (N.getValueType()) {
1609 default: assert(0 && "Cannot operate on this type!");
1610 case MVT::i1:
1611 case MVT::i8: Opc = 5; break;
1612 case MVT::i16: Opc = 6; break;
1613 case MVT::i32: Opc = 7; break;
1614 case MVT::f32: Opc = 8; break;
1615 case MVT::f64: Opc = 9; break;
1616 }
1617 switch (Node->getOpcode()) {
1618 default: assert(0 && "Unreachable!");
1619 case ISD::SUB: Opc = SUBTab[Opc]; break;
1620 case ISD::MUL: Opc = MULTab[Opc]; break;
1621 case ISD::AND: Opc = ANDTab[Opc]; break;
1622 case ISD::OR: Opc = ORTab[Opc]; break;
1623 case ISD::XOR: Opc = XORTab[Opc]; break;
1624 }
1625
1626 X86AddressMode AM;
1627 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1628 Tmp1 = SelectExpr(Op0);
1629 EmitFoldedLoad(Op1, AM);
1630 } else {
1631 EmitFoldedLoad(Op1, AM);
1632 Tmp1 = SelectExpr(Op0);
1633 }
1634 if (Opc) {
1635 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1636 } else {
1637 assert(Node->getOpcode() == ISD::MUL &&
1638 N.getValueType() == MVT::i8 && "Unexpected situation!");
1639 // Must use the MUL instruction, which forces use of AL.
1640 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1641 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1642 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1643 }
1644 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001645 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001646
1647 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1648 Tmp1 = SelectExpr(Op0);
1649 Tmp2 = SelectExpr(Op1);
1650 } else {
1651 Tmp2 = SelectExpr(Op1);
1652 Tmp1 = SelectExpr(Op0);
1653 }
1654
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001655 switch (N.getValueType()) {
1656 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001657 case MVT::i1:
1658 case MVT::i8: Opc = 10; break;
1659 case MVT::i16: Opc = 11; break;
1660 case MVT::i32: Opc = 12; break;
1661 case MVT::f32: Opc = 13; break;
1662 case MVT::f64: Opc = 14; break;
1663 }
1664 switch (Node->getOpcode()) {
1665 default: assert(0 && "Unreachable!");
1666 case ISD::SUB: Opc = SUBTab[Opc]; break;
1667 case ISD::MUL: Opc = MULTab[Opc]; break;
1668 case ISD::AND: Opc = ANDTab[Opc]; break;
1669 case ISD::OR: Opc = ORTab[Opc]; break;
1670 case ISD::XOR: Opc = XORTab[Opc]; break;
1671 }
1672 if (Opc) {
1673 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1674 } else {
1675 assert(Node->getOpcode() == ISD::MUL &&
1676 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001677 // Must use the MUL instruction, which forces use of AL.
1678 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1679 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1680 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001681 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001682 return Result;
1683
1684 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001685 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001686 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1687 Tmp2 = SelectExpr(N.getOperand(1));
1688 Tmp3 = SelectExpr(N.getOperand(2));
1689 } else {
1690 Tmp3 = SelectExpr(N.getOperand(2));
1691 Tmp2 = SelectExpr(N.getOperand(1));
1692 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001693 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001694 return Result;
1695 } else {
1696 // FIXME: This should not be implemented here, it should be in the generic
1697 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001698 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1699 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1700 N.getOperand(1)));
1701 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1702 N.getOperand(2)));
1703 } else {
1704 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1705 N.getOperand(2)));
1706 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1707 N.getOperand(1)));
1708 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001709 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001710 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1711 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001712 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1713 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1714 return Result;
1715 }
1716
1717 case ISD::SDIV:
1718 case ISD::UDIV:
1719 case ISD::SREM:
1720 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001721 if (N.getOpcode() == ISD::SDIV)
1722 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1723 // FIXME: These special cases should be handled by the lowering impl!
1724 unsigned RHS = CN->getValue();
1725 bool isNeg = false;
1726 if ((int)RHS < 0) {
1727 isNeg = true;
1728 RHS = -RHS;
1729 }
1730 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1731 unsigned Log = log2(RHS);
1732 unsigned TmpReg = MakeReg(N.getValueType());
1733 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1734 switch (N.getValueType()) {
1735 default: assert("Unknown type to signed divide!");
1736 case MVT::i8:
1737 SAROpc = X86::SAR8ri;
1738 SHROpc = X86::SHR8ri;
1739 ADDOpc = X86::ADD8rr;
1740 NEGOpc = X86::NEG8r;
1741 break;
1742 case MVT::i16:
1743 SAROpc = X86::SAR16ri;
1744 SHROpc = X86::SHR16ri;
1745 ADDOpc = X86::ADD16rr;
1746 NEGOpc = X86::NEG16r;
1747 break;
1748 case MVT::i32:
1749 SAROpc = X86::SAR32ri;
1750 SHROpc = X86::SHR32ri;
1751 ADDOpc = X86::ADD32rr;
1752 NEGOpc = X86::NEG32r;
1753 break;
1754 }
Chris Lattner11333092005-01-11 03:11:44 +00001755 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001756 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1757 unsigned TmpReg2 = MakeReg(N.getValueType());
1758 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1759 unsigned TmpReg3 = MakeReg(N.getValueType());
1760 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1761
1762 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1763 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1764 if (isNeg)
1765 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1766 return Result;
1767 }
1768 }
1769
Chris Lattner11333092005-01-11 03:11:44 +00001770 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1771 Tmp1 = SelectExpr(N.getOperand(0));
1772 Tmp2 = SelectExpr(N.getOperand(1));
1773 } else {
1774 Tmp2 = SelectExpr(N.getOperand(1));
1775 Tmp1 = SelectExpr(N.getOperand(0));
1776 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001777
1778 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1779 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1780 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1781 switch (N.getValueType()) {
1782 default: assert(0 && "Cannot sdiv this type!");
1783 case MVT::i8:
1784 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1785 LoReg = X86::AL;
1786 HiReg = X86::AH;
1787 MovOpcode = X86::MOV8rr;
1788 ClrOpcode = X86::MOV8ri;
1789 SExtOpcode = X86::CBW;
1790 break;
1791 case MVT::i16:
1792 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1793 LoReg = X86::AX;
1794 HiReg = X86::DX;
1795 MovOpcode = X86::MOV16rr;
1796 ClrOpcode = X86::MOV16ri;
1797 SExtOpcode = X86::CWD;
1798 break;
1799 case MVT::i32:
1800 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001801 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001802 HiReg = X86::EDX;
1803 MovOpcode = X86::MOV32rr;
1804 ClrOpcode = X86::MOV32ri;
1805 SExtOpcode = X86::CDQ;
1806 break;
1807 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1808 case MVT::f32:
1809 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 if (N.getOpcode() == ISD::SDIV)
1811 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1812 else
1813 assert(0 && "FIXME: Emit frem libcall to fmod!");
1814 return Result;
1815 }
1816
1817 // Set up the low part.
1818 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1819
1820 if (isSigned) {
1821 // Sign extend the low part into the high part.
1822 BuildMI(BB, SExtOpcode, 0);
1823 } else {
1824 // Zero out the high part, effectively zero extending the input.
1825 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1826 }
1827
1828 // Emit the DIV/IDIV instruction.
1829 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1830
1831 // Get the result of the divide or rem.
1832 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1833 return Result;
1834 }
1835
1836 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001837 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001838 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1839 switch (N.getValueType()) {
1840 default: assert(0 && "Cannot shift this type!");
1841 case MVT::i8: Opc = X86::ADD8rr; break;
1842 case MVT::i16: Opc = X86::ADD16rr; break;
1843 case MVT::i32: Opc = X86::ADD32rr; break;
1844 }
1845 Tmp1 = SelectExpr(N.getOperand(0));
1846 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1847 return Result;
1848 }
1849
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001850 switch (N.getValueType()) {
1851 default: assert(0 && "Cannot shift this type!");
1852 case MVT::i8: Opc = X86::SHL8ri; break;
1853 case MVT::i16: Opc = X86::SHL16ri; break;
1854 case MVT::i32: Opc = X86::SHL32ri; break;
1855 }
Chris Lattner11333092005-01-11 03:11:44 +00001856 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1858 return Result;
1859 }
Chris Lattner11333092005-01-11 03:11:44 +00001860
1861 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1862 Tmp1 = SelectExpr(N.getOperand(0));
1863 Tmp2 = SelectExpr(N.getOperand(1));
1864 } else {
1865 Tmp2 = SelectExpr(N.getOperand(1));
1866 Tmp1 = SelectExpr(N.getOperand(0));
1867 }
1868
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001869 switch (N.getValueType()) {
1870 default: assert(0 && "Cannot shift this type!");
1871 case MVT::i8 : Opc = X86::SHL8rCL; break;
1872 case MVT::i16: Opc = X86::SHL16rCL; break;
1873 case MVT::i32: Opc = X86::SHL32rCL; break;
1874 }
1875 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1876 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1877 return Result;
1878 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001879 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1880 switch (N.getValueType()) {
1881 default: assert(0 && "Cannot shift this type!");
1882 case MVT::i8: Opc = X86::SHR8ri; break;
1883 case MVT::i16: Opc = X86::SHR16ri; break;
1884 case MVT::i32: Opc = X86::SHR32ri; break;
1885 }
Chris Lattner11333092005-01-11 03:11:44 +00001886 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001887 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1888 return Result;
1889 }
Chris Lattner11333092005-01-11 03:11:44 +00001890
1891 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1892 Tmp1 = SelectExpr(N.getOperand(0));
1893 Tmp2 = SelectExpr(N.getOperand(1));
1894 } else {
1895 Tmp2 = SelectExpr(N.getOperand(1));
1896 Tmp1 = SelectExpr(N.getOperand(0));
1897 }
1898
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001899 switch (N.getValueType()) {
1900 default: assert(0 && "Cannot shift this type!");
1901 case MVT::i8 : Opc = X86::SHR8rCL; break;
1902 case MVT::i16: Opc = X86::SHR16rCL; break;
1903 case MVT::i32: Opc = X86::SHR32rCL; break;
1904 }
1905 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1906 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 return Result;
1908 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1910 switch (N.getValueType()) {
1911 default: assert(0 && "Cannot shift this type!");
1912 case MVT::i8: Opc = X86::SAR8ri; break;
1913 case MVT::i16: Opc = X86::SAR16ri; break;
1914 case MVT::i32: Opc = X86::SAR32ri; break;
1915 }
Chris Lattner11333092005-01-11 03:11:44 +00001916 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001917 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1918 return Result;
1919 }
Chris Lattner11333092005-01-11 03:11:44 +00001920
1921 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1922 Tmp1 = SelectExpr(N.getOperand(0));
1923 Tmp2 = SelectExpr(N.getOperand(1));
1924 } else {
1925 Tmp2 = SelectExpr(N.getOperand(1));
1926 Tmp1 = SelectExpr(N.getOperand(0));
1927 }
1928
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001929 switch (N.getValueType()) {
1930 default: assert(0 && "Cannot shift this type!");
1931 case MVT::i8 : Opc = X86::SAR8rCL; break;
1932 case MVT::i16: Opc = X86::SAR16rCL; break;
1933 case MVT::i32: Opc = X86::SAR32rCL; break;
1934 }
1935 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1936 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1937 return Result;
1938
1939 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001940 EmitCMP(N.getOperand(0), N.getOperand(1));
1941 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1942 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1943 return Result;
1944 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001945 // Make sure we generate both values.
1946 if (Result != 1)
1947 ExprMap[N.getValue(1)] = 1; // Generate the token
1948 else
1949 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1950
Chris Lattner5188ad72005-01-08 19:28:19 +00001951 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001952 default: assert(0 && "Cannot load this type!");
1953 case MVT::i1:
1954 case MVT::i8: Opc = X86::MOV8rm; break;
1955 case MVT::i16: Opc = X86::MOV16rm; break;
1956 case MVT::i32: Opc = X86::MOV32rm; break;
1957 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1958 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1959 }
Chris Lattner11333092005-01-11 03:11:44 +00001960
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001961 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001962 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001963 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1964 } else {
1965 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001966 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1968 }
1969 return Result;
1970 }
1971 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001972 // Generate both result values.
1973 if (Result != 1)
1974 ExprMap[N.getValue(1)] = 1; // Generate the token
1975 else
1976 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1977
1978 // FIXME: We are currently ignoring the requested alignment for handling
1979 // greater than the stack alignment. This will need to be revisited at some
1980 // point. Align = N.getOperand(2);
1981
1982 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1983 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1984 std::cerr << "Cannot allocate stack object with greater alignment than"
1985 << " the stack alignment yet!";
1986 abort();
1987 }
1988
1989 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001990 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001991 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1992 .addImm(CN->getValue());
1993 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001994 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1995 Select(N.getOperand(0));
1996 Tmp1 = SelectExpr(N.getOperand(1));
1997 } else {
1998 Tmp1 = SelectExpr(N.getOperand(1));
1999 Select(N.getOperand(0));
2000 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002001
2002 // Subtract size from stack pointer, thereby allocating some space.
2003 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2004 }
2005
2006 // Put a pointer to the space into the result register, by copying the stack
2007 // pointer.
2008 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2009 return Result;
2010
2011 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002012 // The chain for this call is now lowered.
2013 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2014
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 if (GlobalAddressSDNode *GASD =
2016 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002017 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2019 } else if (ExternalSymbolSDNode *ESSDN =
2020 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002021 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002022 BuildMI(BB, X86::CALLpcrel32,
2023 1).addExternalSymbol(ESSDN->getSymbol(), true);
2024 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002025 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2026 Select(N.getOperand(0));
2027 Tmp1 = SelectExpr(N.getOperand(1));
2028 } else {
2029 Tmp1 = SelectExpr(N.getOperand(1));
2030 Select(N.getOperand(0));
2031 }
2032
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002033 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2034 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002035 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002036 default: assert(0 && "Unknown value type for call result!");
2037 case MVT::Other: return 1;
2038 case MVT::i1:
2039 case MVT::i8:
2040 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2041 break;
2042 case MVT::i16:
2043 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2044 break;
2045 case MVT::i32:
2046 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002047 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002048 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2049 break;
2050 case MVT::f32:
2051 case MVT::f64: // Floating-point return values live in %ST(0)
2052 ContainsFPCode = true;
2053 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2054 break;
2055 }
2056 return Result+N.ResNo;
2057 }
2058
2059 return 0;
2060}
2061
2062void ISel::Select(SDOperand N) {
2063 unsigned Tmp1, Tmp2, Opc;
2064
2065 // FIXME: Disable for our current expansion model!
2066 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2067 return; // Already selected.
2068
Chris Lattner989de032005-01-11 06:14:36 +00002069 SDNode *Node = N.Val;
2070
2071 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002072 default:
Chris Lattner989de032005-01-11 06:14:36 +00002073 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002074 assert(0 && "Node not handled yet!");
2075 case ISD::EntryToken: return; // Noop
2076 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002077 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2078 Select(N.getOperand(0));
2079 Tmp1 = SelectExpr(N.getOperand(1));
2080 } else {
2081 Tmp1 = SelectExpr(N.getOperand(1));
2082 Select(N.getOperand(0));
2083 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2085
2086 if (Tmp1 != Tmp2) {
2087 switch (N.getOperand(1).getValueType()) {
2088 default: assert(0 && "Invalid type for operation!");
2089 case MVT::i1:
2090 case MVT::i8: Opc = X86::MOV8rr; break;
2091 case MVT::i16: Opc = X86::MOV16rr; break;
2092 case MVT::i32: Opc = X86::MOV32rr; break;
2093 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002094 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002095 }
2096 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2097 }
2098 return;
2099 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002100 switch (N.getNumOperands()) {
2101 default:
2102 assert(0 && "Unknown return instruction!");
2103 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002104 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2105 N.getOperand(2).getValueType() == MVT::i32 &&
2106 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002107 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2108 Tmp1 = SelectExpr(N.getOperand(1));
2109 Tmp2 = SelectExpr(N.getOperand(2));
2110 } else {
2111 Tmp2 = SelectExpr(N.getOperand(2));
2112 Tmp1 = SelectExpr(N.getOperand(1));
2113 }
2114 Select(N.getOperand(0));
2115
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002116 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2117 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2118 // Declare that EAX & EDX are live on exit.
2119 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2120 .addReg(X86::ESP);
2121 break;
2122 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002123 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2124 Select(N.getOperand(0));
2125 Tmp1 = SelectExpr(N.getOperand(1));
2126 } else {
2127 Tmp1 = SelectExpr(N.getOperand(1));
2128 Select(N.getOperand(0));
2129 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002130 switch (N.getOperand(1).getValueType()) {
2131 default: assert(0 && "All other types should have been promoted!!");
2132 case MVT::f64:
2133 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2134 // Declare that top-of-stack is live on exit
2135 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2136 break;
2137 case MVT::i32:
2138 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2139 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2140 break;
2141 }
2142 break;
2143 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002144 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002145 break;
2146 }
2147 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2148 return;
2149 case ISD::BR: {
2150 Select(N.getOperand(0));
2151 MachineBasicBlock *Dest =
2152 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2153 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2154 return;
2155 }
2156
2157 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002158 MachineBasicBlock *Dest =
2159 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002160
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002161 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2162 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002163 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2164 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2165 Select(N.getOperand(0));
2166 Tmp1 = SelectExpr(N.getOperand(1));
2167 } else {
2168 Tmp1 = SelectExpr(N.getOperand(1));
2169 Select(N.getOperand(0));
2170 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002171 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2172 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2173 }
Chris Lattner11333092005-01-11 03:11:44 +00002174
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002175 return;
2176 }
2177 case ISD::LOAD:
2178 case ISD::CALL:
2179 case ISD::DYNAMIC_STACKALLOC:
2180 SelectExpr(N);
2181 return;
2182 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002183 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184
2185 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2186 Opc = 0;
2187 switch (CN->getValueType(0)) {
2188 default: assert(0 && "Invalid type for operation!");
2189 case MVT::i1:
2190 case MVT::i8: Opc = X86::MOV8mi; break;
2191 case MVT::i16: Opc = X86::MOV16mi; break;
2192 case MVT::i32: Opc = X86::MOV32mi; break;
2193 case MVT::f32:
2194 case MVT::f64: break;
2195 }
2196 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002197 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2198 Select(N.getOperand(0));
2199 SelectAddress(N.getOperand(2), AM);
2200 } else {
2201 SelectAddress(N.getOperand(2), AM);
2202 Select(N.getOperand(0));
2203 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002204 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2205 return;
2206 }
2207 }
Chris Lattner837caa72005-01-11 23:21:30 +00002208
2209 // Check to see if this is a load/op/store combination.
2210 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002211 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2212 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002213 SDOperand TheLoad = N.getOperand(0).getValue(0);
2214 // Check to see if we are loading the same pointer that we're storing to.
2215 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2216 // See if the stored value is a simple binary operator that uses the
2217 // load as one of its operands.
2218 SDOperand Op = N.getOperand(1);
2219 if (Op.Val->getNumOperands() == 2 &&
2220 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2221 // Finally, check to see if this is one of the ops we can handle!
2222 static const unsigned ADDTAB[] = {
2223 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002224 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002225 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002226 static const unsigned SUBTAB[] = {
2227 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002228 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002229 };
2230 static const unsigned ANDTAB[] = {
2231 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002232 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002233 };
2234 static const unsigned ORTAB[] = {
2235 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002236 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002237 };
2238 static const unsigned XORTAB[] = {
2239 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002240 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002241 };
2242 static const unsigned SHLTAB[] = {
2243 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002244 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002245 };
2246 static const unsigned SARTAB[] = {
2247 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002248 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002249 };
2250 static const unsigned SHRTAB[] = {
2251 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002252 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002253 };
Chris Lattner837caa72005-01-11 23:21:30 +00002254
2255 const unsigned *TabPtr = 0;
2256 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002257 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002258 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002259 case ISD::SUB: TabPtr = SUBTAB; break;
2260 case ISD::AND: TabPtr = ANDTAB; break;
2261 case ISD:: OR: TabPtr = ORTAB; break;
2262 case ISD::XOR: TabPtr = XORTAB; break;
2263 case ISD::SHL: TabPtr = SHLTAB; break;
2264 case ISD::SRA: TabPtr = SARTAB; break;
2265 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002266 }
2267
2268 if (TabPtr) {
2269 // Handle: [mem] op= CST
2270 SDOperand Op0 = Op.getOperand(0);
2271 SDOperand Op1 = Op.getOperand(1);
2272 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2273 switch (CN->getValueType(0)) {
2274 default: break;
2275 case MVT::i1:
2276 case MVT::i8: Opc = TabPtr[0]; break;
2277 case MVT::i16: Opc = TabPtr[1]; break;
2278 case MVT::i32: Opc = TabPtr[2]; break;
2279 }
2280
2281 if (Opc) {
2282 if (getRegPressure(TheLoad.getOperand(0)) >
2283 getRegPressure(TheLoad.getOperand(1))) {
2284 Select(TheLoad.getOperand(0));
2285 SelectAddress(TheLoad.getOperand(1), AM);
2286 } else {
2287 SelectAddress(TheLoad.getOperand(1), AM);
2288 Select(TheLoad.getOperand(0));
2289 }
2290
2291 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2292 return;
2293 }
2294 }
2295
2296 // If we have [mem] = V op [mem], try to turn it into:
2297 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002298 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2299 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2300 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002301 std::swap(Op0, Op1);
2302
2303 if (Op0 == TheLoad) {
2304 switch (Op0.getValueType()) {
2305 default: break;
2306 case MVT::i1:
2307 case MVT::i8: Opc = TabPtr[3]; break;
2308 case MVT::i16: Opc = TabPtr[4]; break;
2309 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002310 }
2311
2312 if (Opc) {
2313 Select(TheLoad.getOperand(0));
2314 SelectAddress(TheLoad.getOperand(1), AM);
2315 unsigned Reg = SelectExpr(Op1);
2316 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2317 return;
2318 }
2319 }
Chris Lattner837caa72005-01-11 23:21:30 +00002320 }
2321 }
2322 }
2323 }
2324
2325
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002326 switch (N.getOperand(1).getValueType()) {
2327 default: assert(0 && "Cannot store this type!");
2328 case MVT::i1:
2329 case MVT::i8: Opc = X86::MOV8mr; break;
2330 case MVT::i16: Opc = X86::MOV16mr; break;
2331 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002332 case MVT::f32: Opc = X86::FST32m; break;
2333 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002334 }
Chris Lattner11333092005-01-11 03:11:44 +00002335
2336 std::vector<std::pair<unsigned, unsigned> > RP;
2337 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2338 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2339 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2340 std::sort(RP.begin(), RP.end());
2341
2342 for (unsigned i = 0; i != 3; ++i)
2343 switch (RP[2-i].second) {
2344 default: assert(0 && "Unknown operand number!");
2345 case 0: Select(N.getOperand(0)); break;
2346 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002347 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002348 }
2349
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002350 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2351 return;
2352 }
2353 case ISD::ADJCALLSTACKDOWN:
2354 case ISD::ADJCALLSTACKUP:
2355 Select(N.getOperand(0));
2356 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2357
2358 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2359 X86::ADJCALLSTACKUP;
2360 BuildMI(BB, Opc, 1).addImm(Tmp1);
2361 return;
Chris Lattner989de032005-01-11 06:14:36 +00002362 case ISD::MEMSET: {
2363 Select(N.getOperand(0)); // Select the chain.
2364 unsigned Align =
2365 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2366 if (Align == 0) Align = 1;
2367
2368 // Turn the byte code into # iterations
2369 unsigned CountReg;
2370 unsigned Opcode;
2371 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2372 unsigned Val = ValC->getValue() & 255;
2373
2374 // If the value is a constant, then we can potentially use larger sets.
2375 switch (Align & 3) {
2376 case 2: // WORD aligned
2377 CountReg = MakeReg(MVT::i32);
2378 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2379 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2380 } else {
2381 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2382 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2383 }
2384 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2385 Opcode = X86::REP_STOSW;
2386 break;
2387 case 0: // DWORD aligned
2388 CountReg = MakeReg(MVT::i32);
2389 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2390 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2391 } else {
2392 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2393 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2394 }
2395 Val = (Val << 8) | Val;
2396 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2397 Opcode = X86::REP_STOSD;
2398 break;
2399 default: // BYTE aligned
2400 CountReg = SelectExpr(Node->getOperand(3));
2401 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2402 Opcode = X86::REP_STOSB;
2403 break;
2404 }
2405 } else {
2406 // If it's not a constant value we are storing, just fall back. We could
2407 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2408 unsigned ValReg = SelectExpr(Node->getOperand(2));
2409 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2410 CountReg = SelectExpr(Node->getOperand(3));
2411 Opcode = X86::REP_STOSB;
2412 }
2413
2414 // No matter what the alignment is, we put the source in ESI, the
2415 // destination in EDI, and the count in ECX.
2416 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2417 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2418 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2419 BuildMI(BB, Opcode, 0);
2420 return;
2421 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002422 case ISD::MEMCPY:
2423 Select(N.getOperand(0)); // Select the chain.
2424 unsigned Align =
2425 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2426 if (Align == 0) Align = 1;
2427
2428 // Turn the byte code into # iterations
2429 unsigned CountReg;
2430 unsigned Opcode;
2431 switch (Align & 3) {
2432 case 2: // WORD aligned
2433 CountReg = MakeReg(MVT::i32);
2434 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2435 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2436 } else {
2437 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2438 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2439 }
2440 Opcode = X86::REP_MOVSW;
2441 break;
2442 case 0: // DWORD aligned
2443 CountReg = MakeReg(MVT::i32);
2444 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2445 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2446 } else {
2447 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2448 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2449 }
2450 Opcode = X86::REP_MOVSD;
2451 break;
2452 default: // BYTE aligned
2453 CountReg = SelectExpr(Node->getOperand(3));
2454 Opcode = X86::REP_MOVSB;
2455 break;
2456 }
2457
2458 // No matter what the alignment is, we put the source in ESI, the
2459 // destination in EDI, and the count in ECX.
2460 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2461 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2462 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2463 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2464 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2465 BuildMI(BB, Opcode, 0);
2466 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002467 }
2468 assert(0 && "Should not be reached!");
2469}
2470
2471
2472/// createX86PatternInstructionSelector - This pass converts an LLVM function
2473/// into a machine code representation using pattern matching and a machine
2474/// description file.
2475///
2476FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2477 return new ISel(TM);
2478}