blob: 49c16d515a72e25be6b9ba648537c2d055be80de [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.
329 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
330 // While we're doing this, keep track of whether we see any FP code for
331 // FP_REG_KILL insertion.
332 ContainsFPCode = false;
333
Chris Lattnerdb8c3682005-01-12 02:57:10 +0000334 // Scan the PHI nodes that already are inserted into this basic block. If
335 // any of them is a PHI of a floating point value, we need to insert an
336 // FP_REG_KILL.
337 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
338 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
339 I != E; ++I) {
340 assert(I->getOpcode() == X86::PHI &&
341 "Isn't just PHI nodes?");
342 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
343 X86::RFPRegisterClass) {
344 ContainsFPCode = true;
345 break;
346 }
347 }
348
Chris Lattner11333092005-01-11 03:11:44 +0000349 // Compute the RegPressureMap, which is an approximation for the number of
350 // registers required to compute each node.
351 ComputeRegPressure(DAG.getRoot());
352
353 //DAG.viewGraph();
354
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000355 // Codegen the basic block.
356 Select(DAG.getRoot());
357
358 // Insert FP_REG_KILL instructions into basic blocks that need them. This
359 // only occurs due to the floating point stackifier not being aggressive
360 // enough to handle arbitrary global stackification.
361 //
362 // Currently we insert an FP_REG_KILL instruction into each block that
363 // uses or defines a floating point virtual register.
364 //
365 // When the global register allocators (like linear scan) finally update
366 // live variable analysis, we can keep floating point values in registers
367 // across basic blocks. This will be a huge win, but we are waiting on
368 // the global allocators before we can do this.
369 //
370 if (ContainsFPCode && BB->succ_size()) {
371 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
372 ++NumFPKill;
373 }
374
375 // Clear state used for selection.
376 ExprMap.clear();
377 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000378 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000379 }
380
Chris Lattnera5ade062005-01-11 21:19:59 +0000381 bool isFoldableLoad(SDOperand Op);
382 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
383
384
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000385 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000386 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000387 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
388 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000389 unsigned SelectExpr(SDOperand N);
390 bool SelectAddress(SDOperand N, X86AddressMode &AM);
391 void Select(SDOperand N);
392 };
393}
394
Chris Lattner11333092005-01-11 03:11:44 +0000395// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
396// for the number of registers required to compute each node. This is basically
397// computing a generalized form of the Sethi-Ullman number for each node.
398unsigned ISel::ComputeRegPressure(SDOperand O) {
399 SDNode *N = O.Val;
400 unsigned &Result = RegPressureMap[N];
401 if (Result) return Result;
402
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000403 // FIXME: Should operations like CALL (which clobber lots o regs) have a
404 // higher fixed cost??
405
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000406 if (N->getNumOperands() == 0) {
407 Result = 1;
408 } else {
409 unsigned MaxRegUse = 0;
410 unsigned NumExtraMaxRegUsers = 0;
411 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
412 unsigned Regs;
413 if (N->getOperand(i).getOpcode() == ISD::Constant)
414 Regs = 0;
415 else
416 Regs = ComputeRegPressure(N->getOperand(i));
417 if (Regs > MaxRegUse) {
418 MaxRegUse = Regs;
419 NumExtraMaxRegUsers = 0;
420 } else if (Regs == MaxRegUse &&
421 N->getOperand(i).getValueType() != MVT::Other) {
422 ++NumExtraMaxRegUsers;
423 }
Chris Lattner11333092005-01-11 03:11:44 +0000424 }
Chris Lattner11333092005-01-11 03:11:44 +0000425
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000426 Result = MaxRegUse+NumExtraMaxRegUsers;
427 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000428
Chris Lattner837caa72005-01-11 23:21:30 +0000429 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000430 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000431}
432
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000433/// SelectAddress - Add the specified node to the specified addressing mode,
434/// returning true if it cannot be done.
435bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
436 switch (N.getOpcode()) {
437 default: break;
438 case ISD::FrameIndex:
439 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
440 AM.BaseType = X86AddressMode::FrameIndexBase;
441 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
442 return false;
443 }
444 break;
445 case ISD::GlobalAddress:
446 if (AM.GV == 0) {
447 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
448 return false;
449 }
450 break;
451 case ISD::Constant:
452 AM.Disp += cast<ConstantSDNode>(N)->getValue();
453 return false;
454 case ISD::SHL:
455 if (AM.IndexReg == 0 || AM.Scale == 1)
456 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
457 unsigned Val = CN->getValue();
458 if (Val == 1 || Val == 2 || Val == 3) {
459 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000460 SDOperand ShVal = N.Val->getOperand(0);
461
462 // Okay, we know that we have a scale by now. However, if the scaled
463 // value is an add of something and a constant, we can fold the
464 // constant into the disp field here.
465 if (ShVal.Val->getOpcode() == ISD::ADD &&
466 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
467 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
468 ConstantSDNode *AddVal =
469 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
470 AM.Disp += AddVal->getValue() << Val;
471 } else {
472 AM.IndexReg = SelectExpr(ShVal);
473 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000474 return false;
475 }
476 }
477 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000478 case ISD::MUL:
479 // X*[3,5,9] -> X+X*[2,4,8]
480 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
481 AM.Base.Reg == 0)
482 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
483 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
484 AM.Scale = unsigned(CN->getValue())-1;
485
486 SDOperand MulVal = N.Val->getOperand(0);
487 unsigned Reg;
488
489 // Okay, we know that we have a scale by now. However, if the scaled
490 // value is an add of something and a constant, we can fold the
491 // constant into the disp field here.
492 if (MulVal.Val->getOpcode() == ISD::ADD &&
493 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
494 Reg = SelectExpr(MulVal.Val->getOperand(0));
495 ConstantSDNode *AddVal =
496 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
497 AM.Disp += AddVal->getValue() * CN->getValue();
498 } else {
499 Reg = SelectExpr(N.Val->getOperand(0));
500 }
501
502 AM.IndexReg = AM.Base.Reg = Reg;
503 return false;
504 }
505 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000506
507 case ISD::ADD: {
508 X86AddressMode Backup = AM;
509 if (!SelectAddress(N.Val->getOperand(0), AM) &&
510 !SelectAddress(N.Val->getOperand(1), AM))
511 return false;
512 AM = Backup;
513 break;
514 }
515 }
516
Chris Lattnera95589b2005-01-11 04:40:19 +0000517 // Is the base register already occupied?
518 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
519 // If so, check to see if the scale index register is set.
520 if (AM.IndexReg == 0) {
521 AM.IndexReg = SelectExpr(N);
522 AM.Scale = 1;
523 return false;
524 }
525
526 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000527 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000528 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000529
530 // Default, generate it as a register.
531 AM.BaseType = X86AddressMode::RegBase;
532 AM.Base.Reg = SelectExpr(N);
533 return false;
534}
535
536/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
537/// assuming that the temporary registers are in the 8-bit register class.
538///
539/// Tmp1 = setcc1
540/// Tmp2 = setcc2
541/// DestReg = logicalop Tmp1, Tmp2
542///
543static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
544 unsigned SetCC2, unsigned LogicalOp,
545 unsigned DestReg) {
546 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
547 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
548 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
549 BuildMI(BB, SetCC1, 0, Tmp1);
550 BuildMI(BB, SetCC2, 0, Tmp2);
551 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
552}
553
554/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
555/// condition codes match the specified SetCCOpcode. Note that some conditions
556/// require multiple instructions to generate the correct value.
557static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
558 ISD::CondCode SetCCOpcode, bool isFP) {
559 unsigned Opc;
560 if (!isFP) {
561 switch (SetCCOpcode) {
562 default: assert(0 && "Illegal integer SetCC!");
563 case ISD::SETEQ: Opc = X86::SETEr; break;
564 case ISD::SETGT: Opc = X86::SETGr; break;
565 case ISD::SETGE: Opc = X86::SETGEr; break;
566 case ISD::SETLT: Opc = X86::SETLr; break;
567 case ISD::SETLE: Opc = X86::SETLEr; break;
568 case ISD::SETNE: Opc = X86::SETNEr; break;
569 case ISD::SETULT: Opc = X86::SETBr; break;
570 case ISD::SETUGT: Opc = X86::SETAr; break;
571 case ISD::SETULE: Opc = X86::SETBEr; break;
572 case ISD::SETUGE: Opc = X86::SETAEr; break;
573 }
574 } else {
575 // On a floating point condition, the flags are set as follows:
576 // ZF PF CF op
577 // 0 | 0 | 0 | X > Y
578 // 0 | 0 | 1 | X < Y
579 // 1 | 0 | 0 | X == Y
580 // 1 | 1 | 1 | unordered
581 //
582 switch (SetCCOpcode) {
583 default: assert(0 && "Invalid FP setcc!");
584 case ISD::SETUEQ:
585 case ISD::SETEQ:
586 Opc = X86::SETEr; // True if ZF = 1
587 break;
588 case ISD::SETOGT:
589 case ISD::SETGT:
590 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
591 break;
592 case ISD::SETOGE:
593 case ISD::SETGE:
594 Opc = X86::SETAEr; // True if CF = 0
595 break;
596 case ISD::SETULT:
597 case ISD::SETLT:
598 Opc = X86::SETBr; // True if CF = 1
599 break;
600 case ISD::SETULE:
601 case ISD::SETLE:
602 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
603 break;
604 case ISD::SETONE:
605 case ISD::SETNE:
606 Opc = X86::SETNEr; // True if ZF = 0
607 break;
608 case ISD::SETUO:
609 Opc = X86::SETPr; // True if PF = 1
610 break;
611 case ISD::SETO:
612 Opc = X86::SETNPr; // True if PF = 0
613 break;
614 case ISD::SETOEQ: // !PF & ZF
615 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
616 return;
617 case ISD::SETOLT: // !PF & CF
618 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
619 return;
620 case ISD::SETOLE: // !PF & (CF || ZF)
621 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
622 return;
623 case ISD::SETUGT: // PF | (!ZF & !CF)
624 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
625 return;
626 case ISD::SETUGE: // PF | !CF
627 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
628 return;
629 case ISD::SETUNE: // PF | !ZF
630 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
631 return;
632 }
633 }
634 BuildMI(BB, Opc, 0, DestReg);
635}
636
637
638/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
639/// the Dest block if the Cond condition is true. If we cannot fold this
640/// condition into the branch, return true.
641///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000642bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
643 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000644 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
645 // B) using two conditional branches instead of one condbr, two setcc's, and
646 // an or.
647 if ((Cond.getOpcode() == ISD::OR ||
648 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
649 // And and or set the flags for us, so there is no need to emit a TST of the
650 // result. It is only safe to do this if there is only a single use of the
651 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000652 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000653 SelectExpr(Cond);
654 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
655 return false;
656 }
657
658 // Codegen br not C -> JE.
659 if (Cond.getOpcode() == ISD::XOR)
660 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
661 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000662 unsigned CondR;
663 if (getRegPressure(Chain) > getRegPressure(Cond)) {
664 Select(Chain);
665 CondR = SelectExpr(Cond.Val->getOperand(0));
666 } else {
667 CondR = SelectExpr(Cond.Val->getOperand(0));
668 Select(Chain);
669 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000670 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
671 BuildMI(BB, X86::JE, 1).addMBB(Dest);
672 return false;
673 }
674
675 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
676 if (SetCC == 0)
677 return true; // Can only handle simple setcc's so far.
678
679 unsigned Opc;
680
681 // Handle integer conditions first.
682 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
683 switch (SetCC->getCondition()) {
684 default: assert(0 && "Illegal integer SetCC!");
685 case ISD::SETEQ: Opc = X86::JE; break;
686 case ISD::SETGT: Opc = X86::JG; break;
687 case ISD::SETGE: Opc = X86::JGE; break;
688 case ISD::SETLT: Opc = X86::JL; break;
689 case ISD::SETLE: Opc = X86::JLE; break;
690 case ISD::SETNE: Opc = X86::JNE; break;
691 case ISD::SETULT: Opc = X86::JB; break;
692 case ISD::SETUGT: Opc = X86::JA; break;
693 case ISD::SETULE: Opc = X86::JBE; break;
694 case ISD::SETUGE: Opc = X86::JAE; break;
695 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000696 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000697 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
698 BuildMI(BB, Opc, 1).addMBB(Dest);
699 return false;
700 }
701
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000702 unsigned Opc2 = 0; // Second branch if needed.
703
704 // On a floating point condition, the flags are set as follows:
705 // ZF PF CF op
706 // 0 | 0 | 0 | X > Y
707 // 0 | 0 | 1 | X < Y
708 // 1 | 0 | 0 | X == Y
709 // 1 | 1 | 1 | unordered
710 //
711 switch (SetCC->getCondition()) {
712 default: assert(0 && "Invalid FP setcc!");
713 case ISD::SETUEQ:
714 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
715 case ISD::SETOGT:
716 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
717 case ISD::SETOGE:
718 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
719 case ISD::SETULT:
720 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
721 case ISD::SETULE:
722 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
723 case ISD::SETONE:
724 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
725 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
726 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
727 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
728 Opc = X86::JA; // ZF = 0 & CF = 0
729 Opc2 = X86::JP; // PF = 1
730 break;
731 case ISD::SETUGE: // PF = 1 | CF = 0
732 Opc = X86::JAE; // CF = 0
733 Opc2 = X86::JP; // PF = 1
734 break;
735 case ISD::SETUNE: // PF = 1 | ZF = 0
736 Opc = X86::JNE; // ZF = 0
737 Opc2 = X86::JP; // PF = 1
738 break;
739 case ISD::SETOEQ: // PF = 0 & ZF = 1
740 //X86::JNP, X86::JE
741 //X86::AND8rr
742 return true; // FIXME: Emit more efficient code for this branch.
743 case ISD::SETOLT: // PF = 0 & CF = 1
744 //X86::JNP, X86::JB
745 //X86::AND8rr
746 return true; // FIXME: Emit more efficient code for this branch.
747 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
748 //X86::JNP, X86::JBE
749 //X86::AND8rr
750 return true; // FIXME: Emit more efficient code for this branch.
751 }
752
Chris Lattner6c07aee2005-01-11 04:06:27 +0000753 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000754 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
755 BuildMI(BB, Opc, 1).addMBB(Dest);
756 if (Opc2)
757 BuildMI(BB, Opc2, 1).addMBB(Dest);
758 return false;
759}
760
Chris Lattner24aad1b2005-01-10 22:10:13 +0000761/// EmitSelectCC - Emit code into BB that performs a select operation between
762/// the two registers RTrue and RFalse, generating a result into RDest. Return
763/// true if the fold cannot be performed.
764///
765void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
766 unsigned RTrue, unsigned RFalse, unsigned RDest) {
767 enum Condition {
768 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
769 NOT_SET
770 } CondCode = NOT_SET;
771
772 static const unsigned CMOVTAB16[] = {
773 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
774 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
775 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
776 };
777 static const unsigned CMOVTAB32[] = {
778 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
779 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
780 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
781 };
782 static const unsigned CMOVTABFP[] = {
783 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
784 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
785 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
786 };
787
788 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
789 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
790 switch (SetCC->getCondition()) {
791 default: assert(0 && "Unknown integer comparison!");
792 case ISD::SETEQ: CondCode = EQ; break;
793 case ISD::SETGT: CondCode = GT; break;
794 case ISD::SETGE: CondCode = GE; break;
795 case ISD::SETLT: CondCode = LT; break;
796 case ISD::SETLE: CondCode = LE; break;
797 case ISD::SETNE: CondCode = NE; break;
798 case ISD::SETULT: CondCode = B; break;
799 case ISD::SETUGT: CondCode = A; break;
800 case ISD::SETULE: CondCode = BE; break;
801 case ISD::SETUGE: CondCode = AE; break;
802 }
803 } else {
804 // On a floating point condition, the flags are set as follows:
805 // ZF PF CF op
806 // 0 | 0 | 0 | X > Y
807 // 0 | 0 | 1 | X < Y
808 // 1 | 0 | 0 | X == Y
809 // 1 | 1 | 1 | unordered
810 //
811 switch (SetCC->getCondition()) {
812 default: assert(0 && "Unknown FP comparison!");
813 case ISD::SETUEQ:
814 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
815 case ISD::SETOGT:
816 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
817 case ISD::SETOGE:
818 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
819 case ISD::SETULT:
820 case ISD::SETLT: CondCode = B; break; // True if CF = 1
821 case ISD::SETULE:
822 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
823 case ISD::SETONE:
824 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
825 case ISD::SETUO: CondCode = P; break; // True if PF = 1
826 case ISD::SETO: CondCode = NP; break; // True if PF = 0
827 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
828 case ISD::SETUGE: // PF = 1 | CF = 0
829 case ISD::SETUNE: // PF = 1 | ZF = 0
830 case ISD::SETOEQ: // PF = 0 & ZF = 1
831 case ISD::SETOLT: // PF = 0 & CF = 1
832 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
833 // We cannot emit this comparison as a single cmov.
834 break;
835 }
836 }
837 }
838
839 unsigned Opc = 0;
840 if (CondCode != NOT_SET) {
841 switch (SVT) {
842 default: assert(0 && "Cannot select this type!");
843 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
844 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
845 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000846 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000847 }
848 }
849
850 // Finally, if we weren't able to fold this, just emit the condition and test
851 // it.
852 if (CondCode == NOT_SET || Opc == 0) {
853 // Get the condition into the zero flag.
854 unsigned CondReg = SelectExpr(Cond);
855 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
856
857 switch (SVT) {
858 default: assert(0 && "Cannot select this type!");
859 case MVT::i16: Opc = X86::CMOVE16rr; break;
860 case MVT::i32: Opc = X86::CMOVE32rr; break;
861 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000862 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000863 }
864 } else {
865 // FIXME: CMP R, 0 -> TEST R, R
866 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000867 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000868 }
869 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
870}
871
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000872void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000873 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
875 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000876 if (isFoldableLoad(LHS)) {
877 switch (RHS.getValueType()) {
878 default: break;
879 case MVT::i1:
880 case MVT::i8: Opc = X86::CMP8mi; break;
881 case MVT::i16: Opc = X86::CMP16mi; break;
882 case MVT::i32: Opc = X86::CMP32mi; break;
883 }
884 if (Opc) {
885 X86AddressMode AM;
886 EmitFoldedLoad(LHS, AM);
887 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
888 return;
889 }
890 }
891
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000892 switch (RHS.getValueType()) {
893 default: break;
894 case MVT::i1:
895 case MVT::i8: Opc = X86::CMP8ri; break;
896 case MVT::i16: Opc = X86::CMP16ri; break;
897 case MVT::i32: Opc = X86::CMP32ri; break;
898 }
899 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000900 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000901 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
902 return;
903 }
904 }
905
Chris Lattneref6806c2005-01-12 02:02:48 +0000906 Opc = 0;
907 if (isFoldableLoad(LHS)) {
908 switch (RHS.getValueType()) {
909 default: break;
910 case MVT::i1:
911 case MVT::i8: Opc = X86::CMP8mr; break;
912 case MVT::i16: Opc = X86::CMP16mr; break;
913 case MVT::i32: Opc = X86::CMP32mr; break;
914 }
915 if (Opc) {
916 X86AddressMode AM;
917 unsigned Reg;
918 if (getRegPressure(LHS) > getRegPressure(RHS)) {
919 EmitFoldedLoad(LHS, AM);
920 Reg = SelectExpr(RHS);
921 } else {
922 Reg = SelectExpr(RHS);
923 EmitFoldedLoad(LHS, AM);
924 }
925 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
926 return;
927 }
928 }
929
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000930 switch (LHS.getValueType()) {
931 default: assert(0 && "Cannot compare this value!");
932 case MVT::i1:
933 case MVT::i8: Opc = X86::CMP8rr; break;
934 case MVT::i16: Opc = X86::CMP16rr; break;
935 case MVT::i32: Opc = X86::CMP32rr; break;
936 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000937 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000938 }
Chris Lattner11333092005-01-11 03:11:44 +0000939 unsigned Tmp1, Tmp2;
940 if (getRegPressure(LHS) > getRegPressure(RHS)) {
941 Tmp1 = SelectExpr(LHS);
942 Tmp2 = SelectExpr(RHS);
943 } else {
944 Tmp2 = SelectExpr(RHS);
945 Tmp1 = SelectExpr(LHS);
946 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000947 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
948}
949
Chris Lattnera5ade062005-01-11 21:19:59 +0000950/// isFoldableLoad - Return true if this is a load instruction that can safely
951/// be folded into an operation that uses it.
952bool ISel::isFoldableLoad(SDOperand Op) {
953 if (Op.getOpcode() != ISD::LOAD ||
954 // FIXME: currently can't fold constant pool indexes.
955 isa<ConstantPoolSDNode>(Op.getOperand(1)))
956 return false;
957
958 // If this load has already been emitted, we clearly can't fold it.
959 if (ExprMap.count(Op)) return false;
960
961 return Op.Val->use_size() == 2;
962}
963
964/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
965/// and compute the address being loaded into AM.
966void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
967 SDOperand Chain = Op.getOperand(0);
968 SDOperand Address = Op.getOperand(1);
969 if (getRegPressure(Chain) > getRegPressure(Address)) {
970 Select(Chain);
971 SelectAddress(Address, AM);
972 } else {
973 SelectAddress(Address, AM);
974 Select(Chain);
975 }
976
977 // The chain for this load is now lowered.
978 LoweredTokens.insert(SDOperand(Op.Val, 1));
979 ExprMap[SDOperand(Op.Val, 1)] = 1;
980}
981
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000982unsigned ISel::SelectExpr(SDOperand N) {
983 unsigned Result;
984 unsigned Tmp1, Tmp2, Tmp3;
985 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000986 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000987 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000988
Chris Lattner590d8002005-01-09 18:52:44 +0000989 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000990 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000991 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000992
993 unsigned &Reg = ExprMap[N];
994 if (Reg) return Reg;
995
996 if (N.getOpcode() != ISD::CALL)
997 Reg = Result = (N.getValueType() != MVT::Other) ?
998 MakeReg(N.getValueType()) : 1;
999 else {
1000 // If this is a call instruction, make sure to prepare ALL of the result
1001 // values as well as the chain.
1002 if (Node->getNumValues() == 1)
1003 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001004 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001005 Result = MakeReg(Node->getValueType(0));
1006 ExprMap[N.getValue(0)] = Result;
1007 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1008 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1009 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001010 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001011 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001012
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001013 switch (N.getOpcode()) {
1014 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001015 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001016 assert(0 && "Node not handled!\n");
1017 case ISD::FrameIndex:
1018 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1019 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1020 return Result;
1021 case ISD::ConstantPool:
1022 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1023 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1024 return Result;
1025 case ISD::ConstantFP:
1026 ContainsFPCode = true;
1027 Tmp1 = Result; // Intermediate Register
1028 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1029 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1030 Tmp1 = MakeReg(MVT::f64);
1031
1032 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1033 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1034 BuildMI(BB, X86::FLD0, 0, Tmp1);
1035 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1036 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1037 BuildMI(BB, X86::FLD1, 0, Tmp1);
1038 else
1039 assert(0 && "Unexpected constant!");
1040 if (Tmp1 != Result)
1041 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1042 return Result;
1043 case ISD::Constant:
1044 switch (N.getValueType()) {
1045 default: assert(0 && "Cannot use constants of this type!");
1046 case MVT::i1:
1047 case MVT::i8: Opc = X86::MOV8ri; break;
1048 case MVT::i16: Opc = X86::MOV16ri; break;
1049 case MVT::i32: Opc = X86::MOV32ri; break;
1050 }
1051 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1052 return Result;
1053 case ISD::GlobalAddress: {
1054 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1055 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1056 return Result;
1057 }
1058 case ISD::ExternalSymbol: {
1059 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1060 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1061 return Result;
1062 }
1063 case ISD::FP_EXTEND:
1064 Tmp1 = SelectExpr(N.getOperand(0));
1065 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001066 return Result;
1067 case ISD::ZERO_EXTEND: {
1068 int DestIs16 = N.getValueType() == MVT::i16;
1069 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001070
1071 // FIXME: This hack is here for zero extension casts from bool to i8. This
1072 // would not be needed if bools were promoted by Legalize.
1073 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001074 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001075 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1076 return Result;
1077 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001078
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001079 if (isFoldableLoad(N.getOperand(0))) {
1080 static const unsigned Opc[3] = {
1081 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1082 };
1083
1084 X86AddressMode AM;
1085 EmitFoldedLoad(N.getOperand(0), AM);
1086 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1087
1088 return Result;
1089 }
1090
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001091 static const unsigned Opc[3] = {
1092 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1093 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001094 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001095 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1096 return Result;
1097 }
1098 case ISD::SIGN_EXTEND: {
1099 int DestIs16 = N.getValueType() == MVT::i16;
1100 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1101
Chris Lattner590d8002005-01-09 18:52:44 +00001102 // FIXME: Legalize should promote bools to i8!
1103 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1104 "Sign extend from bool not implemented!");
1105
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001106 if (isFoldableLoad(N.getOperand(0))) {
1107 static const unsigned Opc[3] = {
1108 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1109 };
1110
1111 X86AddressMode AM;
1112 EmitFoldedLoad(N.getOperand(0), AM);
1113 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1114 return Result;
1115 }
1116
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001117 static const unsigned Opc[3] = {
1118 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1119 };
1120 Tmp1 = SelectExpr(N.getOperand(0));
1121 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1122 return Result;
1123 }
1124 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001125 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1126 if (isFoldableLoad(N.getOperand(0))) {
1127 switch (N.getValueType()) {
1128 default: assert(0 && "Unknown truncate!");
1129 case MVT::i1:
1130 case MVT::i8: Opc = X86::MOV8rm; break;
1131 case MVT::i16: Opc = X86::MOV16rm; break;
1132 }
1133 X86AddressMode AM;
1134 EmitFoldedLoad(N.getOperand(0), AM);
1135 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1136 return Result;
1137 }
1138
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001139 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1140 // a move out of AX or AL.
1141 switch (N.getOperand(0).getValueType()) {
1142 default: assert(0 && "Unknown truncate!");
1143 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1144 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1145 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1146 }
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1149
1150 switch (N.getValueType()) {
1151 default: assert(0 && "Unknown truncate!");
1152 case MVT::i1:
1153 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1154 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1155 }
1156 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1157 return Result;
1158
1159 case ISD::FP_ROUND:
1160 // Truncate from double to float by storing to memory as float,
1161 // then reading it back into a register.
1162
1163 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001164 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001165 Tmp1 = TLI.getTargetData().getFloatAlignment();
1166 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1167
1168 // Codegen the input.
1169 Tmp1 = SelectExpr(N.getOperand(0));
1170
1171 // Emit the store, then the reload.
1172 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1173 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001174 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001175
1176 case ISD::SINT_TO_FP:
1177 case ISD::UINT_TO_FP: {
1178 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001179 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001180
1181 // Promote the integer to a type supported by FLD. We do this because there
1182 // are no unsigned FLD instructions, so we must promote an unsigned value to
1183 // a larger signed value, then use FLD on the larger value.
1184 //
1185 MVT::ValueType PromoteType = MVT::Other;
1186 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1187 unsigned PromoteOpcode = 0;
1188 unsigned RealDestReg = Result;
1189 switch (SrcTy) {
1190 case MVT::i1:
1191 case MVT::i8:
1192 // We don't have the facilities for directly loading byte sized data from
1193 // memory (even signed). Promote it to 16 bits.
1194 PromoteType = MVT::i16;
1195 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1196 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1197 break;
1198 case MVT::i16:
1199 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1200 PromoteType = MVT::i32;
1201 PromoteOpcode = X86::MOVZX32rr16;
1202 }
1203 break;
1204 default:
1205 // Don't fild into the real destination.
1206 if (Node->getOpcode() == ISD::UINT_TO_FP)
1207 Result = MakeReg(Node->getValueType(0));
1208 break;
1209 }
1210
1211 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1212
1213 if (PromoteType != MVT::Other) {
1214 Tmp2 = MakeReg(PromoteType);
1215 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1216 SrcTy = PromoteType;
1217 Tmp1 = Tmp2;
1218 }
1219
1220 // Spill the integer to memory and reload it from there.
1221 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1222 MachineFunction *F = BB->getParent();
1223 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1224
1225 switch (SrcTy) {
1226 case MVT::i64:
1227 // FIXME: this won't work for cast [u]long to FP
1228 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1229 FrameIdx).addReg(Tmp1);
1230 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1231 FrameIdx, 4).addReg(Tmp1+1);
1232 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1233 break;
1234 case MVT::i32:
1235 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1236 FrameIdx).addReg(Tmp1);
1237 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1238 break;
1239 case MVT::i16:
1240 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1241 FrameIdx).addReg(Tmp1);
1242 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1243 break;
1244 default: break; // No promotion required.
1245 }
1246
1247 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1248 // If this is a cast from uint -> double, we need to be careful when if
1249 // the "sign" bit is set. If so, we don't want to make a negative number,
1250 // we want to make a positive number. Emit code to add an offset if the
1251 // sign bit is set.
1252
1253 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1254 unsigned IsNeg = MakeReg(MVT::i32);
1255 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1256
1257 // Create a CP value that has the offset in one word and 0 in the other.
1258 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1259 0x4f80000000000000ULL);
1260 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1261 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1262 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1263
1264 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1265 // We need special handling for unsigned 64-bit integer sources. If the
1266 // input number has the "sign bit" set, then we loaded it incorrectly as a
1267 // negative 64-bit number. In this case, add an offset value.
1268
1269 // Emit a test instruction to see if the dynamic input value was signed.
1270 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1271
1272 // If the sign bit is set, get a pointer to an offset, otherwise get a
1273 // pointer to a zero.
1274 MachineConstantPool *CP = F->getConstantPool();
1275 unsigned Zero = MakeReg(MVT::i32);
1276 Constant *Null = Constant::getNullValue(Type::UIntTy);
1277 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1278 CP->getConstantPoolIndex(Null));
1279 unsigned Offset = MakeReg(MVT::i32);
1280 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1281
1282 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1283 CP->getConstantPoolIndex(OffsetCst));
1284 unsigned Addr = MakeReg(MVT::i32);
1285 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1286
1287 // Load the constant for an add. FIXME: this could make an 'fadd' that
1288 // reads directly from memory, but we don't support these yet.
1289 unsigned ConstReg = MakeReg(MVT::f64);
1290 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1291
1292 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1293 }
1294 return RealDestReg;
1295 }
1296 case ISD::FP_TO_SINT:
1297 case ISD::FP_TO_UINT: {
1298 // FIXME: Most of this grunt work should be done by legalize!
1299 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1300
1301 // Change the floating point control register to use "round towards zero"
1302 // mode when truncating to an integer value.
1303 //
1304 MachineFunction *F = BB->getParent();
1305 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1306 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1307
1308 // Load the old value of the high byte of the control word...
1309 unsigned HighPartOfCW = MakeReg(MVT::i8);
1310 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1311 CWFrameIdx, 1);
1312
1313 // Set the high part to be round to zero...
1314 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1315 CWFrameIdx, 1).addImm(12);
1316
1317 // Reload the modified control word now...
1318 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1319
1320 // Restore the memory image of control word to original value
1321 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1322 CWFrameIdx, 1).addReg(HighPartOfCW);
1323
1324 // We don't have the facilities for directly storing byte sized data to
1325 // memory. Promote it to 16 bits. We also must promote unsigned values to
1326 // larger classes because we only have signed FP stores.
1327 MVT::ValueType StoreClass = Node->getValueType(0);
1328 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1329 switch (StoreClass) {
1330 case MVT::i8: StoreClass = MVT::i16; break;
1331 case MVT::i16: StoreClass = MVT::i32; break;
1332 case MVT::i32: StoreClass = MVT::i64; break;
1333 // The following treatment of cLong may not be perfectly right,
1334 // but it survives chains of casts of the form
1335 // double->ulong->double.
1336 case MVT::i64: StoreClass = MVT::i64; break;
1337 default: assert(0 && "Unknown store class!");
1338 }
1339
1340 // Spill the integer to memory and reload it from there.
1341 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1342 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1343
1344 switch (StoreClass) {
1345 default: assert(0 && "Unknown store class!");
1346 case MVT::i16:
1347 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1348 break;
1349 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001350 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001351 break;
1352 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001353 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001354 break;
1355 }
1356
1357 switch (Node->getValueType(0)) {
1358 default:
1359 assert(0 && "Unknown integer type!");
1360 case MVT::i64:
1361 // FIXME: this isn't gunna work.
1362 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1363 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1364 case MVT::i32:
1365 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1366 break;
1367 case MVT::i16:
1368 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1369 break;
1370 case MVT::i8:
1371 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1372 break;
1373 }
1374
1375 // Reload the original control word now.
1376 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1377 return Result;
1378 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001379 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001380 Op0 = N.getOperand(0);
1381 Op1 = N.getOperand(1);
1382
1383 if (isFoldableLoad(Op0))
1384 std::swap(Op0, Op1);
1385
1386 if (isFoldableLoad(Op1)) {
1387 switch (N.getValueType()) {
1388 default: assert(0 && "Cannot add this type!");
1389 case MVT::i1:
1390 case MVT::i8: Opc = X86::ADD8rm; break;
1391 case MVT::i16: Opc = X86::ADD16rm; break;
1392 case MVT::i32: Opc = X86::ADD32rm; break;
1393 case MVT::f32: Opc = X86::FADD32m; break;
1394 case MVT::f64: Opc = X86::FADD64m; break;
1395 }
1396 X86AddressMode AM;
1397 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1398 Tmp1 = SelectExpr(Op0);
1399 EmitFoldedLoad(Op1, AM);
1400 } else {
1401 EmitFoldedLoad(Op1, AM);
1402 Tmp1 = SelectExpr(Op0);
1403 }
1404 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1405 return Result;
1406 }
1407
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001408 // See if we can codegen this as an LEA to fold operations together.
1409 if (N.getValueType() == MVT::i32) {
1410 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001411 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001412 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001413 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001414 // leave this as LEA, then peephole it to 'ADD' after two address elim
1415 // happens.
1416 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001417 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001418 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1419 return Result;
1420 }
1421 }
1422 }
Chris Lattner11333092005-01-11 03:11:44 +00001423
Chris Lattnera5ade062005-01-11 21:19:59 +00001424 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001425 Opc = 0;
1426 if (CN->getValue() == 1) { // add X, 1 -> inc X
1427 switch (N.getValueType()) {
1428 default: assert(0 && "Cannot integer add this type!");
1429 case MVT::i8: Opc = X86::INC8r; break;
1430 case MVT::i16: Opc = X86::INC16r; break;
1431 case MVT::i32: Opc = X86::INC32r; break;
1432 }
1433 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1434 switch (N.getValueType()) {
1435 default: assert(0 && "Cannot integer add this type!");
1436 case MVT::i8: Opc = X86::DEC8r; break;
1437 case MVT::i16: Opc = X86::DEC16r; break;
1438 case MVT::i32: Opc = X86::DEC32r; break;
1439 }
1440 }
1441
1442 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001443 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001444 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1445 return Result;
1446 }
1447
1448 switch (N.getValueType()) {
1449 default: assert(0 && "Cannot add this type!");
1450 case MVT::i8: Opc = X86::ADD8ri; break;
1451 case MVT::i16: Opc = X86::ADD16ri; break;
1452 case MVT::i32: Opc = X86::ADD32ri; break;
1453 }
1454 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001455 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001456 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1457 return Result;
1458 }
1459 }
1460
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001461 switch (N.getValueType()) {
1462 default: assert(0 && "Cannot add this type!");
1463 case MVT::i8: Opc = X86::ADD8rr; break;
1464 case MVT::i16: Opc = X86::ADD16rr; break;
1465 case MVT::i32: Opc = X86::ADD32rr; break;
1466 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001467 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001468 }
Chris Lattner11333092005-01-11 03:11:44 +00001469
Chris Lattnera5ade062005-01-11 21:19:59 +00001470 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1471 Tmp1 = SelectExpr(Op0);
1472 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001473 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001474 Tmp2 = SelectExpr(Op1);
1475 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001476 }
1477
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001478 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1479 return Result;
1480 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001481 case ISD::MUL:
1482 case ISD::AND:
1483 case ISD::OR:
1484 case ISD::XOR:
1485 static const unsigned SUBTab[] = {
1486 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1487 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1488 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1489 };
1490 static const unsigned MULTab[] = {
1491 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1492 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1493 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1494 };
1495 static const unsigned ANDTab[] = {
1496 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1497 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1498 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1499 };
1500 static const unsigned ORTab[] = {
1501 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1502 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1503 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1504 };
1505 static const unsigned XORTab[] = {
1506 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1507 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1508 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1509 };
1510
1511 Op0 = Node->getOperand(0);
1512 Op1 = Node->getOperand(1);
1513
1514 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001515 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1516 if (CN->isNullValue()) { // 0 - N -> neg N
1517 switch (N.getValueType()) {
1518 default: assert(0 && "Cannot sub this type!");
1519 case MVT::i1:
1520 case MVT::i8: Opc = X86::NEG8r; break;
1521 case MVT::i16: Opc = X86::NEG16r; break;
1522 case MVT::i32: Opc = X86::NEG32r; break;
1523 }
1524 Tmp1 = SelectExpr(N.getOperand(1));
1525 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1526 return Result;
1527 }
1528
Chris Lattnera5ade062005-01-11 21:19:59 +00001529 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1530 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001531 switch (N.getValueType()) {
1532 default: assert(0 && "Cannot add this type!");
1533 case MVT::i1:
1534 case MVT::i8: Opc = X86::NOT8r; break;
1535 case MVT::i16: Opc = X86::NOT16r; break;
1536 case MVT::i32: Opc = X86::NOT32r; break;
1537 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001538 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001539 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1540 return Result;
1541 }
1542
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001543 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001544 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001545 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001546 case MVT::i8: Opc = 0; break;
1547 case MVT::i16: Opc = 1; break;
1548 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001549 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001550 switch (Node->getOpcode()) {
1551 default: assert(0 && "Unreachable!");
1552 case ISD::SUB: Opc = SUBTab[Opc]; break;
1553 case ISD::MUL: Opc = MULTab[Opc]; break;
1554 case ISD::AND: Opc = ANDTab[Opc]; break;
1555 case ISD::OR: Opc = ORTab[Opc]; break;
1556 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001557 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001558 if (Opc) { // Can't fold MUL:i8 R, imm
1559 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001560 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1561 return Result;
1562 }
1563 }
Chris Lattner11333092005-01-11 03:11:44 +00001564
Chris Lattnera5ade062005-01-11 21:19:59 +00001565 if (isFoldableLoad(Op0))
1566 if (Node->getOpcode() != ISD::SUB) {
1567 std::swap(Op0, Op1);
1568 } else {
1569 // Emit 'reverse' subract, with a memory operand.
1570 switch (N.getValueType()) {
1571 default: Opc = 0; break;
1572 case MVT::f32: Opc = X86::FSUBR32m; break;
1573 case MVT::f64: Opc = X86::FSUBR64m; break;
1574 }
1575 if (Opc) {
1576 X86AddressMode AM;
1577 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1578 EmitFoldedLoad(Op0, AM);
1579 Tmp1 = SelectExpr(Op1);
1580 } else {
1581 Tmp1 = SelectExpr(Op1);
1582 EmitFoldedLoad(Op0, AM);
1583 }
1584 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1585 return Result;
1586 }
1587 }
1588
1589 if (isFoldableLoad(Op1)) {
1590 switch (N.getValueType()) {
1591 default: assert(0 && "Cannot operate on this type!");
1592 case MVT::i1:
1593 case MVT::i8: Opc = 5; break;
1594 case MVT::i16: Opc = 6; break;
1595 case MVT::i32: Opc = 7; break;
1596 case MVT::f32: Opc = 8; break;
1597 case MVT::f64: Opc = 9; break;
1598 }
1599 switch (Node->getOpcode()) {
1600 default: assert(0 && "Unreachable!");
1601 case ISD::SUB: Opc = SUBTab[Opc]; break;
1602 case ISD::MUL: Opc = MULTab[Opc]; break;
1603 case ISD::AND: Opc = ANDTab[Opc]; break;
1604 case ISD::OR: Opc = ORTab[Opc]; break;
1605 case ISD::XOR: Opc = XORTab[Opc]; break;
1606 }
1607
1608 X86AddressMode AM;
1609 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1610 Tmp1 = SelectExpr(Op0);
1611 EmitFoldedLoad(Op1, AM);
1612 } else {
1613 EmitFoldedLoad(Op1, AM);
1614 Tmp1 = SelectExpr(Op0);
1615 }
1616 if (Opc) {
1617 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1618 } else {
1619 assert(Node->getOpcode() == ISD::MUL &&
1620 N.getValueType() == MVT::i8 && "Unexpected situation!");
1621 // Must use the MUL instruction, which forces use of AL.
1622 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1623 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1624 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1625 }
1626 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001627 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001628
1629 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1630 Tmp1 = SelectExpr(Op0);
1631 Tmp2 = SelectExpr(Op1);
1632 } else {
1633 Tmp2 = SelectExpr(Op1);
1634 Tmp1 = SelectExpr(Op0);
1635 }
1636
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001637 switch (N.getValueType()) {
1638 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001639 case MVT::i1:
1640 case MVT::i8: Opc = 10; break;
1641 case MVT::i16: Opc = 11; break;
1642 case MVT::i32: Opc = 12; break;
1643 case MVT::f32: Opc = 13; break;
1644 case MVT::f64: Opc = 14; break;
1645 }
1646 switch (Node->getOpcode()) {
1647 default: assert(0 && "Unreachable!");
1648 case ISD::SUB: Opc = SUBTab[Opc]; break;
1649 case ISD::MUL: Opc = MULTab[Opc]; break;
1650 case ISD::AND: Opc = ANDTab[Opc]; break;
1651 case ISD::OR: Opc = ORTab[Opc]; break;
1652 case ISD::XOR: Opc = XORTab[Opc]; break;
1653 }
1654 if (Opc) {
1655 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1656 } else {
1657 assert(Node->getOpcode() == ISD::MUL &&
1658 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001659 // Must use the MUL instruction, which forces use of AL.
1660 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1661 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1662 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001663 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001664 return Result;
1665
1666 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001667 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001668 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1669 Tmp2 = SelectExpr(N.getOperand(1));
1670 Tmp3 = SelectExpr(N.getOperand(2));
1671 } else {
1672 Tmp3 = SelectExpr(N.getOperand(2));
1673 Tmp2 = SelectExpr(N.getOperand(1));
1674 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001675 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001676 return Result;
1677 } else {
1678 // FIXME: This should not be implemented here, it should be in the generic
1679 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001680 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1681 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1682 N.getOperand(1)));
1683 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1684 N.getOperand(2)));
1685 } else {
1686 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1687 N.getOperand(2)));
1688 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1689 N.getOperand(1)));
1690 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001691 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001692 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1693 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001694 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1695 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1696 return Result;
1697 }
1698
1699 case ISD::SDIV:
1700 case ISD::UDIV:
1701 case ISD::SREM:
1702 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001703 if (N.getOpcode() == ISD::SDIV)
1704 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1705 // FIXME: These special cases should be handled by the lowering impl!
1706 unsigned RHS = CN->getValue();
1707 bool isNeg = false;
1708 if ((int)RHS < 0) {
1709 isNeg = true;
1710 RHS = -RHS;
1711 }
1712 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1713 unsigned Log = log2(RHS);
1714 unsigned TmpReg = MakeReg(N.getValueType());
1715 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1716 switch (N.getValueType()) {
1717 default: assert("Unknown type to signed divide!");
1718 case MVT::i8:
1719 SAROpc = X86::SAR8ri;
1720 SHROpc = X86::SHR8ri;
1721 ADDOpc = X86::ADD8rr;
1722 NEGOpc = X86::NEG8r;
1723 break;
1724 case MVT::i16:
1725 SAROpc = X86::SAR16ri;
1726 SHROpc = X86::SHR16ri;
1727 ADDOpc = X86::ADD16rr;
1728 NEGOpc = X86::NEG16r;
1729 break;
1730 case MVT::i32:
1731 SAROpc = X86::SAR32ri;
1732 SHROpc = X86::SHR32ri;
1733 ADDOpc = X86::ADD32rr;
1734 NEGOpc = X86::NEG32r;
1735 break;
1736 }
Chris Lattner11333092005-01-11 03:11:44 +00001737 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001738 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1739 unsigned TmpReg2 = MakeReg(N.getValueType());
1740 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1741 unsigned TmpReg3 = MakeReg(N.getValueType());
1742 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1743
1744 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1745 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1746 if (isNeg)
1747 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1748 return Result;
1749 }
1750 }
1751
Chris Lattner11333092005-01-11 03:11:44 +00001752 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1753 Tmp1 = SelectExpr(N.getOperand(0));
1754 Tmp2 = SelectExpr(N.getOperand(1));
1755 } else {
1756 Tmp2 = SelectExpr(N.getOperand(1));
1757 Tmp1 = SelectExpr(N.getOperand(0));
1758 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001759
1760 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1761 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1762 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1763 switch (N.getValueType()) {
1764 default: assert(0 && "Cannot sdiv this type!");
1765 case MVT::i8:
1766 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1767 LoReg = X86::AL;
1768 HiReg = X86::AH;
1769 MovOpcode = X86::MOV8rr;
1770 ClrOpcode = X86::MOV8ri;
1771 SExtOpcode = X86::CBW;
1772 break;
1773 case MVT::i16:
1774 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1775 LoReg = X86::AX;
1776 HiReg = X86::DX;
1777 MovOpcode = X86::MOV16rr;
1778 ClrOpcode = X86::MOV16ri;
1779 SExtOpcode = X86::CWD;
1780 break;
1781 case MVT::i32:
1782 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001783 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001784 HiReg = X86::EDX;
1785 MovOpcode = X86::MOV32rr;
1786 ClrOpcode = X86::MOV32ri;
1787 SExtOpcode = X86::CDQ;
1788 break;
1789 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1790 case MVT::f32:
1791 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001792 if (N.getOpcode() == ISD::SDIV)
1793 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1794 else
1795 assert(0 && "FIXME: Emit frem libcall to fmod!");
1796 return Result;
1797 }
1798
1799 // Set up the low part.
1800 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1801
1802 if (isSigned) {
1803 // Sign extend the low part into the high part.
1804 BuildMI(BB, SExtOpcode, 0);
1805 } else {
1806 // Zero out the high part, effectively zero extending the input.
1807 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1808 }
1809
1810 // Emit the DIV/IDIV instruction.
1811 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1812
1813 // Get the result of the divide or rem.
1814 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1815 return Result;
1816 }
1817
1818 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001819 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001820 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1821 switch (N.getValueType()) {
1822 default: assert(0 && "Cannot shift this type!");
1823 case MVT::i8: Opc = X86::ADD8rr; break;
1824 case MVT::i16: Opc = X86::ADD16rr; break;
1825 case MVT::i32: Opc = X86::ADD32rr; break;
1826 }
1827 Tmp1 = SelectExpr(N.getOperand(0));
1828 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1829 return Result;
1830 }
1831
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001832 switch (N.getValueType()) {
1833 default: assert(0 && "Cannot shift this type!");
1834 case MVT::i8: Opc = X86::SHL8ri; break;
1835 case MVT::i16: Opc = X86::SHL16ri; break;
1836 case MVT::i32: Opc = X86::SHL32ri; break;
1837 }
Chris Lattner11333092005-01-11 03:11:44 +00001838 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001839 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1840 return Result;
1841 }
Chris Lattner11333092005-01-11 03:11:44 +00001842
1843 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1844 Tmp1 = SelectExpr(N.getOperand(0));
1845 Tmp2 = SelectExpr(N.getOperand(1));
1846 } else {
1847 Tmp2 = SelectExpr(N.getOperand(1));
1848 Tmp1 = SelectExpr(N.getOperand(0));
1849 }
1850
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001851 switch (N.getValueType()) {
1852 default: assert(0 && "Cannot shift this type!");
1853 case MVT::i8 : Opc = X86::SHL8rCL; break;
1854 case MVT::i16: Opc = X86::SHL16rCL; break;
1855 case MVT::i32: Opc = X86::SHL32rCL; break;
1856 }
1857 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1858 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1859 return Result;
1860 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001861 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1862 switch (N.getValueType()) {
1863 default: assert(0 && "Cannot shift this type!");
1864 case MVT::i8: Opc = X86::SHR8ri; break;
1865 case MVT::i16: Opc = X86::SHR16ri; break;
1866 case MVT::i32: Opc = X86::SHR32ri; break;
1867 }
Chris Lattner11333092005-01-11 03:11:44 +00001868 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1870 return Result;
1871 }
Chris Lattner11333092005-01-11 03:11:44 +00001872
1873 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1874 Tmp1 = SelectExpr(N.getOperand(0));
1875 Tmp2 = SelectExpr(N.getOperand(1));
1876 } else {
1877 Tmp2 = SelectExpr(N.getOperand(1));
1878 Tmp1 = SelectExpr(N.getOperand(0));
1879 }
1880
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001881 switch (N.getValueType()) {
1882 default: assert(0 && "Cannot shift this type!");
1883 case MVT::i8 : Opc = X86::SHR8rCL; break;
1884 case MVT::i16: Opc = X86::SHR16rCL; break;
1885 case MVT::i32: Opc = X86::SHR32rCL; break;
1886 }
1887 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 return Result;
1890 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001891 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1892 switch (N.getValueType()) {
1893 default: assert(0 && "Cannot shift this type!");
1894 case MVT::i8: Opc = X86::SAR8ri; break;
1895 case MVT::i16: Opc = X86::SAR16ri; break;
1896 case MVT::i32: Opc = X86::SAR32ri; break;
1897 }
Chris Lattner11333092005-01-11 03:11:44 +00001898 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001899 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1900 return Result;
1901 }
Chris Lattner11333092005-01-11 03:11:44 +00001902
1903 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1904 Tmp1 = SelectExpr(N.getOperand(0));
1905 Tmp2 = SelectExpr(N.getOperand(1));
1906 } else {
1907 Tmp2 = SelectExpr(N.getOperand(1));
1908 Tmp1 = SelectExpr(N.getOperand(0));
1909 }
1910
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001911 switch (N.getValueType()) {
1912 default: assert(0 && "Cannot shift this type!");
1913 case MVT::i8 : Opc = X86::SAR8rCL; break;
1914 case MVT::i16: Opc = X86::SAR16rCL; break;
1915 case MVT::i32: Opc = X86::SAR32rCL; break;
1916 }
1917 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1918 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1919 return Result;
1920
1921 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001922 EmitCMP(N.getOperand(0), N.getOperand(1));
1923 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1924 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1925 return Result;
1926 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 // Make sure we generate both values.
1928 if (Result != 1)
1929 ExprMap[N.getValue(1)] = 1; // Generate the token
1930 else
1931 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1932
Chris Lattner5188ad72005-01-08 19:28:19 +00001933 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001934 default: assert(0 && "Cannot load this type!");
1935 case MVT::i1:
1936 case MVT::i8: Opc = X86::MOV8rm; break;
1937 case MVT::i16: Opc = X86::MOV16rm; break;
1938 case MVT::i32: Opc = X86::MOV32rm; break;
1939 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1940 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1941 }
Chris Lattner11333092005-01-11 03:11:44 +00001942
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001944 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001945 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1946 } else {
1947 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001948 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001949 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1950 }
1951 return Result;
1952 }
1953 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001954 // Generate both result values.
1955 if (Result != 1)
1956 ExprMap[N.getValue(1)] = 1; // Generate the token
1957 else
1958 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1959
1960 // FIXME: We are currently ignoring the requested alignment for handling
1961 // greater than the stack alignment. This will need to be revisited at some
1962 // point. Align = N.getOperand(2);
1963
1964 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1965 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1966 std::cerr << "Cannot allocate stack object with greater alignment than"
1967 << " the stack alignment yet!";
1968 abort();
1969 }
1970
1971 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001972 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1974 .addImm(CN->getValue());
1975 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001976 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1977 Select(N.getOperand(0));
1978 Tmp1 = SelectExpr(N.getOperand(1));
1979 } else {
1980 Tmp1 = SelectExpr(N.getOperand(1));
1981 Select(N.getOperand(0));
1982 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001983
1984 // Subtract size from stack pointer, thereby allocating some space.
1985 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1986 }
1987
1988 // Put a pointer to the space into the result register, by copying the stack
1989 // pointer.
1990 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1991 return Result;
1992
1993 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001994 // The chain for this call is now lowered.
1995 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1996
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001997 if (GlobalAddressSDNode *GASD =
1998 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001999 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2001 } else if (ExternalSymbolSDNode *ESSDN =
2002 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002003 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002004 BuildMI(BB, X86::CALLpcrel32,
2005 1).addExternalSymbol(ESSDN->getSymbol(), true);
2006 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002007 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2008 Select(N.getOperand(0));
2009 Tmp1 = SelectExpr(N.getOperand(1));
2010 } else {
2011 Tmp1 = SelectExpr(N.getOperand(1));
2012 Select(N.getOperand(0));
2013 }
2014
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2016 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002017 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 default: assert(0 && "Unknown value type for call result!");
2019 case MVT::Other: return 1;
2020 case MVT::i1:
2021 case MVT::i8:
2022 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2023 break;
2024 case MVT::i16:
2025 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2026 break;
2027 case MVT::i32:
2028 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002029 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002030 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2031 break;
2032 case MVT::f32:
2033 case MVT::f64: // Floating-point return values live in %ST(0)
2034 ContainsFPCode = true;
2035 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2036 break;
2037 }
2038 return Result+N.ResNo;
2039 }
2040
2041 return 0;
2042}
2043
2044void ISel::Select(SDOperand N) {
2045 unsigned Tmp1, Tmp2, Opc;
2046
2047 // FIXME: Disable for our current expansion model!
2048 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2049 return; // Already selected.
2050
Chris Lattner989de032005-01-11 06:14:36 +00002051 SDNode *Node = N.Val;
2052
2053 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002054 default:
Chris Lattner989de032005-01-11 06:14:36 +00002055 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002056 assert(0 && "Node not handled yet!");
2057 case ISD::EntryToken: return; // Noop
2058 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002059 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2060 Select(N.getOperand(0));
2061 Tmp1 = SelectExpr(N.getOperand(1));
2062 } else {
2063 Tmp1 = SelectExpr(N.getOperand(1));
2064 Select(N.getOperand(0));
2065 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002066 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2067
2068 if (Tmp1 != Tmp2) {
2069 switch (N.getOperand(1).getValueType()) {
2070 default: assert(0 && "Invalid type for operation!");
2071 case MVT::i1:
2072 case MVT::i8: Opc = X86::MOV8rr; break;
2073 case MVT::i16: Opc = X86::MOV16rr; break;
2074 case MVT::i32: Opc = X86::MOV32rr; break;
2075 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002076 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002077 }
2078 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2079 }
2080 return;
2081 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002082 switch (N.getNumOperands()) {
2083 default:
2084 assert(0 && "Unknown return instruction!");
2085 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002086 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2087 N.getOperand(2).getValueType() == MVT::i32 &&
2088 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002089 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2090 Tmp1 = SelectExpr(N.getOperand(1));
2091 Tmp2 = SelectExpr(N.getOperand(2));
2092 } else {
2093 Tmp2 = SelectExpr(N.getOperand(2));
2094 Tmp1 = SelectExpr(N.getOperand(1));
2095 }
2096 Select(N.getOperand(0));
2097
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002098 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2099 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2100 // Declare that EAX & EDX are live on exit.
2101 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2102 .addReg(X86::ESP);
2103 break;
2104 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002105 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2106 Select(N.getOperand(0));
2107 Tmp1 = SelectExpr(N.getOperand(1));
2108 } else {
2109 Tmp1 = SelectExpr(N.getOperand(1));
2110 Select(N.getOperand(0));
2111 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002112 switch (N.getOperand(1).getValueType()) {
2113 default: assert(0 && "All other types should have been promoted!!");
2114 case MVT::f64:
2115 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2116 // Declare that top-of-stack is live on exit
2117 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2118 break;
2119 case MVT::i32:
2120 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2121 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2122 break;
2123 }
2124 break;
2125 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002126 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002127 break;
2128 }
2129 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2130 return;
2131 case ISD::BR: {
2132 Select(N.getOperand(0));
2133 MachineBasicBlock *Dest =
2134 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2135 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2136 return;
2137 }
2138
2139 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002140 MachineBasicBlock *Dest =
2141 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002142
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002143 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2144 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002145 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2146 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2147 Select(N.getOperand(0));
2148 Tmp1 = SelectExpr(N.getOperand(1));
2149 } else {
2150 Tmp1 = SelectExpr(N.getOperand(1));
2151 Select(N.getOperand(0));
2152 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002153 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2154 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2155 }
Chris Lattner11333092005-01-11 03:11:44 +00002156
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002157 return;
2158 }
2159 case ISD::LOAD:
2160 case ISD::CALL:
2161 case ISD::DYNAMIC_STACKALLOC:
2162 SelectExpr(N);
2163 return;
2164 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002165 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002166
2167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2168 Opc = 0;
2169 switch (CN->getValueType(0)) {
2170 default: assert(0 && "Invalid type for operation!");
2171 case MVT::i1:
2172 case MVT::i8: Opc = X86::MOV8mi; break;
2173 case MVT::i16: Opc = X86::MOV16mi; break;
2174 case MVT::i32: Opc = X86::MOV32mi; break;
2175 case MVT::f32:
2176 case MVT::f64: break;
2177 }
2178 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002179 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2180 Select(N.getOperand(0));
2181 SelectAddress(N.getOperand(2), AM);
2182 } else {
2183 SelectAddress(N.getOperand(2), AM);
2184 Select(N.getOperand(0));
2185 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002186 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2187 return;
2188 }
2189 }
Chris Lattner837caa72005-01-11 23:21:30 +00002190
2191 // Check to see if this is a load/op/store combination.
2192 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002193 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2194 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002195 SDOperand TheLoad = N.getOperand(0).getValue(0);
2196 // Check to see if we are loading the same pointer that we're storing to.
2197 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2198 // See if the stored value is a simple binary operator that uses the
2199 // load as one of its operands.
2200 SDOperand Op = N.getOperand(1);
2201 if (Op.Val->getNumOperands() == 2 &&
2202 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2203 // Finally, check to see if this is one of the ops we can handle!
2204 static const unsigned ADDTAB[] = {
2205 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002206 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002207 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002208 static const unsigned SUBTAB[] = {
2209 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002210 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002211 };
2212 static const unsigned ANDTAB[] = {
2213 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002214 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002215 };
2216 static const unsigned ORTAB[] = {
2217 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002218 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002219 };
2220 static const unsigned XORTAB[] = {
2221 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002222 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002223 };
2224 static const unsigned SHLTAB[] = {
2225 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002226 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002227 };
2228 static const unsigned SARTAB[] = {
2229 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002230 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002231 };
2232 static const unsigned SHRTAB[] = {
2233 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002234 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002235 };
Chris Lattner837caa72005-01-11 23:21:30 +00002236
2237 const unsigned *TabPtr = 0;
2238 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002239 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002240 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002241 case ISD::SUB: TabPtr = SUBTAB; break;
2242 case ISD::AND: TabPtr = ANDTAB; break;
2243 case ISD:: OR: TabPtr = ORTAB; break;
2244 case ISD::XOR: TabPtr = XORTAB; break;
2245 case ISD::SHL: TabPtr = SHLTAB; break;
2246 case ISD::SRA: TabPtr = SARTAB; break;
2247 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002248 }
2249
2250 if (TabPtr) {
2251 // Handle: [mem] op= CST
2252 SDOperand Op0 = Op.getOperand(0);
2253 SDOperand Op1 = Op.getOperand(1);
2254 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2255 switch (CN->getValueType(0)) {
2256 default: break;
2257 case MVT::i1:
2258 case MVT::i8: Opc = TabPtr[0]; break;
2259 case MVT::i16: Opc = TabPtr[1]; break;
2260 case MVT::i32: Opc = TabPtr[2]; break;
2261 }
2262
2263 if (Opc) {
2264 if (getRegPressure(TheLoad.getOperand(0)) >
2265 getRegPressure(TheLoad.getOperand(1))) {
2266 Select(TheLoad.getOperand(0));
2267 SelectAddress(TheLoad.getOperand(1), AM);
2268 } else {
2269 SelectAddress(TheLoad.getOperand(1), AM);
2270 Select(TheLoad.getOperand(0));
2271 }
2272
2273 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2274 return;
2275 }
2276 }
2277
2278 // If we have [mem] = V op [mem], try to turn it into:
2279 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002280 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2281 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2282 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002283 std::swap(Op0, Op1);
2284
2285 if (Op0 == TheLoad) {
2286 switch (Op0.getValueType()) {
2287 default: break;
2288 case MVT::i1:
2289 case MVT::i8: Opc = TabPtr[3]; break;
2290 case MVT::i16: Opc = TabPtr[4]; break;
2291 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002292 }
2293
2294 if (Opc) {
2295 Select(TheLoad.getOperand(0));
2296 SelectAddress(TheLoad.getOperand(1), AM);
2297 unsigned Reg = SelectExpr(Op1);
2298 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2299 return;
2300 }
2301 }
Chris Lattner837caa72005-01-11 23:21:30 +00002302 }
2303 }
2304 }
2305 }
2306
2307
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002308 switch (N.getOperand(1).getValueType()) {
2309 default: assert(0 && "Cannot store this type!");
2310 case MVT::i1:
2311 case MVT::i8: Opc = X86::MOV8mr; break;
2312 case MVT::i16: Opc = X86::MOV16mr; break;
2313 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002314 case MVT::f32: Opc = X86::FST32m; break;
2315 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002316 }
Chris Lattner11333092005-01-11 03:11:44 +00002317
2318 std::vector<std::pair<unsigned, unsigned> > RP;
2319 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2320 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2321 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2322 std::sort(RP.begin(), RP.end());
2323
2324 for (unsigned i = 0; i != 3; ++i)
2325 switch (RP[2-i].second) {
2326 default: assert(0 && "Unknown operand number!");
2327 case 0: Select(N.getOperand(0)); break;
2328 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002329 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002330 }
2331
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002332 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2333 return;
2334 }
2335 case ISD::ADJCALLSTACKDOWN:
2336 case ISD::ADJCALLSTACKUP:
2337 Select(N.getOperand(0));
2338 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2339
2340 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2341 X86::ADJCALLSTACKUP;
2342 BuildMI(BB, Opc, 1).addImm(Tmp1);
2343 return;
Chris Lattner989de032005-01-11 06:14:36 +00002344 case ISD::MEMSET: {
2345 Select(N.getOperand(0)); // Select the chain.
2346 unsigned Align =
2347 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2348 if (Align == 0) Align = 1;
2349
2350 // Turn the byte code into # iterations
2351 unsigned CountReg;
2352 unsigned Opcode;
2353 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2354 unsigned Val = ValC->getValue() & 255;
2355
2356 // If the value is a constant, then we can potentially use larger sets.
2357 switch (Align & 3) {
2358 case 2: // WORD aligned
2359 CountReg = MakeReg(MVT::i32);
2360 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2361 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2362 } else {
2363 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2364 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2365 }
2366 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2367 Opcode = X86::REP_STOSW;
2368 break;
2369 case 0: // DWORD aligned
2370 CountReg = MakeReg(MVT::i32);
2371 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2372 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2373 } else {
2374 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2375 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2376 }
2377 Val = (Val << 8) | Val;
2378 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2379 Opcode = X86::REP_STOSD;
2380 break;
2381 default: // BYTE aligned
2382 CountReg = SelectExpr(Node->getOperand(3));
2383 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2384 Opcode = X86::REP_STOSB;
2385 break;
2386 }
2387 } else {
2388 // If it's not a constant value we are storing, just fall back. We could
2389 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2390 unsigned ValReg = SelectExpr(Node->getOperand(2));
2391 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2392 CountReg = SelectExpr(Node->getOperand(3));
2393 Opcode = X86::REP_STOSB;
2394 }
2395
2396 // No matter what the alignment is, we put the source in ESI, the
2397 // destination in EDI, and the count in ECX.
2398 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2399 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2400 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2401 BuildMI(BB, Opcode, 0);
2402 return;
2403 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002404 case ISD::MEMCPY:
2405 Select(N.getOperand(0)); // Select the chain.
2406 unsigned Align =
2407 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2408 if (Align == 0) Align = 1;
2409
2410 // Turn the byte code into # iterations
2411 unsigned CountReg;
2412 unsigned Opcode;
2413 switch (Align & 3) {
2414 case 2: // WORD aligned
2415 CountReg = MakeReg(MVT::i32);
2416 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2417 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2418 } else {
2419 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2420 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2421 }
2422 Opcode = X86::REP_MOVSW;
2423 break;
2424 case 0: // DWORD aligned
2425 CountReg = MakeReg(MVT::i32);
2426 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2427 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2428 } else {
2429 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2430 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2431 }
2432 Opcode = X86::REP_MOVSD;
2433 break;
2434 default: // BYTE aligned
2435 CountReg = SelectExpr(Node->getOperand(3));
2436 Opcode = X86::REP_MOVSB;
2437 break;
2438 }
2439
2440 // No matter what the alignment is, we put the source in ESI, the
2441 // destination in EDI, and the count in ECX.
2442 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2443 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2444 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2445 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2446 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2447 BuildMI(BB, Opcode, 0);
2448 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449 }
2450 assert(0 && "Should not be reached!");
2451}
2452
2453
2454/// createX86PatternInstructionSelector - This pass converts an LLVM function
2455/// into a machine code representation using pattern matching and a machine
2456/// description file.
2457///
2458FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2459 return new ISel(TM);
2460}