blob: 86260ae2dba8d2abd3ecc5e68f3711d9dfa7199d [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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
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"
Nate Begemanfb5792f2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattnerc6f41812005-05-12 23:06:28 +000018#include "llvm/CallingConv.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000019#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000032#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/ADT/Statistic.h"
35#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000036#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000037using namespace llvm;
38
Chris Lattnerc6f41812005-05-12 23:06:28 +000039// FIXME: temporary.
40#include "llvm/Support/CommandLine.h"
41static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
42 cl::desc("Enable fastcc on X86"));
43
Chris Lattner67649df2005-05-14 06:52:07 +000044namespace {
45 // X86 Specific DAG Nodes
46 namespace X86ISD {
47 enum NodeType {
48 // Start the numbering where the builtin ops leave off.
49 FIRST_NUMBER = ISD::BUILTIN_OP_END,
50
51 /// FILD64m - This instruction implements SINT_TO_FP with a
52 /// 64-bit source in memory and a FP reg result. This corresponds to
53 /// the X86::FILD64m instruction. It has two inputs (token chain and
54 /// address) and two outputs (FP value and token chain).
55 FILD64m,
Chris Lattner239738a2005-05-14 08:48:15 +000056
Chris Lattnerf7443da2005-07-29 00:54:34 +000057 /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the
58 /// integer destination in memory and a FP reg source. This corresponds
59 /// to the X86::FIST*m instructions and the rounding mode change stuff. It
60 /// has two inputs (token chain and address) and two outputs (FP value and
61 /// token chain).
62 FP_TO_INT64_IN_MEM,
Chris Lattner745d5382005-07-29 00:40:01 +000063
Chris Lattner239738a2005-05-14 08:48:15 +000064 /// CALL/TAILCALL - These operations represent an abstract X86 call
65 /// instruction, which includes a bunch of information. In particular the
66 /// operands of these node are:
67 ///
68 /// #0 - The incoming token chain
69 /// #1 - The callee
70 /// #2 - The number of arg bytes the caller pushes on the stack.
71 /// #3 - The number of arg bytes the callee pops off the stack.
72 /// #4 - The value to pass in AL/AX/EAX (optional)
73 /// #5 - The value to pass in DL/DX/EDX (optional)
74 ///
75 /// The result values of these nodes are:
76 ///
77 /// #0 - The outgoing token chain
78 /// #1 - The first register result value (optional)
79 /// #2 - The second register result value (optional)
80 ///
81 /// The CALL vs TAILCALL distinction boils down to whether the callee is
82 /// known not to modify the caller's stack frame, as is standard with
83 /// LLVM.
84 CALL,
85 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000086 };
87 }
88}
89
Chris Lattner8acb1ba2005-01-07 07:49:41 +000090//===----------------------------------------------------------------------===//
91// X86TargetLowering - X86 Implementation of the TargetLowering interface
92namespace {
93 class X86TargetLowering : public TargetLowering {
94 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000095 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner381e8872005-05-15 05:46:45 +000096 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
97 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000098 public:
99 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
100 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +0000101
Chris Lattner653f7232005-05-13 22:46:57 +0000102 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +0000103 setShiftAmountType(MVT::i8);
104 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +0000105 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +0000106 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +0000107
108 // Set up the register classes.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000109 // FIXME: Eliminate these two classes when legalize can handle promotions
110 // well.
111 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000112 addRegisterClass(MVT::i8, X86::R8RegisterClass);
113 addRegisterClass(MVT::i16, X86::R16RegisterClass);
114 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000115
Chris Lattnera28381c2005-07-16 00:28:20 +0000116 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
117 // operation.
118 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
119 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
120 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000122
123 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
124 // this operation.
125 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
126 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000127
Chris Lattner745d5382005-07-29 00:40:01 +0000128 if (!X86ScalarSSE) {
129 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
130 // isn't legal.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
132 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
133 }
134
Chris Lattnerda4d4692005-04-09 03:22:37 +0000135 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000136 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000139 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
140 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
141 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +0000142 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000148 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000150 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000151
Chris Lattner4e6ce5f2005-05-09 20:37:29 +0000152 setOperationAction(ISD::READIO , MVT::i1 , Expand);
153 setOperationAction(ISD::READIO , MVT::i8 , Expand);
154 setOperationAction(ISD::READIO , MVT::i16 , Expand);
155 setOperationAction(ISD::READIO , MVT::i32 , Expand);
156 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
157 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
158 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
159 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000160
Chris Lattnerda2ce112005-01-16 07:34:08 +0000161 // These should be promoted to a larger select which is supported.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000162 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000163 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000164
Nate Begemanf63be7d2005-07-06 18:59:04 +0000165 if (X86ScalarSSE) {
166 // Set up the FP register classes.
167 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
168 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Nate Begeman5a8441e2005-07-16 02:02:34 +0000170 // SSE has no load+extend ops
Nate Begemanf63be7d2005-07-06 18:59:04 +0000171 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
172 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000173
174 // SSE has no i16 to fp conversion, only i32
175 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
176
Nate Begemanf63be7d2005-07-06 18:59:04 +0000177 // We don't support sin/cos/sqrt/fmod
178 setOperationAction(ISD::FSIN , MVT::f64, Expand);
179 setOperationAction(ISD::FCOS , MVT::f64, Expand);
180 setOperationAction(ISD::FABS , MVT::f64, Expand);
181 setOperationAction(ISD::FNEG , MVT::f64, Expand);
182 setOperationAction(ISD::SREM , MVT::f64, Expand);
183 setOperationAction(ISD::FSIN , MVT::f32, Expand);
184 setOperationAction(ISD::FCOS , MVT::f32, Expand);
185 setOperationAction(ISD::FABS , MVT::f32, Expand);
186 setOperationAction(ISD::FNEG , MVT::f32, Expand);
187 setOperationAction(ISD::SREM , MVT::f32, Expand);
188 } else {
189 // Set up the FP register classes.
190 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000191
Nate Begemanf63be7d2005-07-06 18:59:04 +0000192 if (!UnsafeFPMath) {
193 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
194 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
195 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000196
Nate Begemanf63be7d2005-07-06 18:59:04 +0000197 addLegalFPImmediate(+0.0); // FLD0
198 addLegalFPImmediate(+1.0); // FLD1
199 addLegalFPImmediate(-0.0); // FLD0/FCHS
200 addLegalFPImmediate(-1.0); // FLD1/FCHS
201 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000202 computeRegisterProperties();
Reid Spencera0f5bf32005-07-19 04:52:44 +0000203
204 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
205 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
206 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
207 allowUnalignedStores = true; // x86 supports it!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000208 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000209
Chris Lattner3648c672005-05-13 21:44:04 +0000210 // Return the number of bytes that a function should pop when it returns (in
211 // addition to the space used by the return address).
212 //
213 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
214
Chris Lattner381e8872005-05-15 05:46:45 +0000215 // Return the number of bytes that the caller reserves for arguments passed
216 // to this function.
217 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
218
Chris Lattner67649df2005-05-14 06:52:07 +0000219 /// LowerOperation - Provide custom lowering hooks for some operations.
220 ///
221 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
222
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000223 /// LowerArguments - This hook must be implemented to indicate how we should
224 /// lower the arguments for the specified function, into the specified DAG.
225 virtual std::vector<SDOperand>
226 LowerArguments(Function &F, SelectionDAG &DAG);
227
228 /// LowerCallTo - This hook lowers an abstract call to a function into an
229 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000230 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen00b168892005-07-27 06:12:32 +0000231 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000232 bool isTailCall, SDOperand Callee, ArgListTy &Args,
233 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000234
Chris Lattnere0fe2252005-07-05 19:58:54 +0000235 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
236 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000237 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000238 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
239 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Chris Lattner14824582005-01-09 00:01:27 +0000241 virtual std::pair<SDOperand, SDOperand>
242 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
243 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000244
245 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
246
Chris Lattnerc6f41812005-05-12 23:06:28 +0000247 private:
248 // C Calling Convention implementation.
249 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
250 std::pair<SDOperand, SDOperand>
251 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000252 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000253 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000254
Chris Lattnerc6f41812005-05-12 23:06:28 +0000255 // Fast Calling Convention implementation.
256 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
257 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000258 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000259 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000260 };
261}
262
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000263std::vector<SDOperand>
264X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000265 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
266 return LowerFastCCArguments(F, DAG);
267 return LowerCCCArguments(F, DAG);
268}
269
270std::pair<SDOperand, SDOperand>
271X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
272 bool isVarArg, unsigned CallingConv,
Jeff Cohen00b168892005-07-27 06:12:32 +0000273 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000274 SDOperand Callee, ArgListTy &Args,
275 SelectionDAG &DAG) {
276 assert((!isVarArg || CallingConv == CallingConv::C) &&
277 "Only C takes varargs!");
278 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000279 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
280 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000281}
282
283//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000284// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000285//===----------------------------------------------------------------------===//
286
287std::vector<SDOperand>
288X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000289 std::vector<SDOperand> ArgValues;
290
Chris Lattner6415bb42005-05-10 03:53:18 +0000291 MachineFunction &MF = DAG.getMachineFunction();
292 MachineFrameInfo *MFI = MF.getFrameInfo();
293
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000294 // Add DAG nodes to load the arguments... On entry to a function on the X86,
295 // the stack frame looks like this:
296 //
297 // [ESP] -- return address
298 // [ESP + 4] -- first argument (leftmost lexically)
299 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000300 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000302 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000303 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000304 MVT::ValueType ObjectVT = getValueType(I->getType());
305 unsigned ArgIncrement = 4;
306 unsigned ObjSize;
307 switch (ObjectVT) {
308 default: assert(0 && "Unhandled argument type!");
309 case MVT::i1:
310 case MVT::i8: ObjSize = 1; break;
311 case MVT::i16: ObjSize = 2; break;
312 case MVT::i32: ObjSize = 4; break;
313 case MVT::i64: ObjSize = ArgIncrement = 8; break;
314 case MVT::f32: ObjSize = 4; break;
315 case MVT::f64: ObjSize = ArgIncrement = 8; break;
316 }
317 // Create the frame index object for this incoming parameter...
318 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000319
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000320 // Create the SelectionDAG nodes corresponding to a load from this parameter
321 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
322
323 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
324 // dead loads.
325 SDOperand ArgValue;
326 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000327 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
328 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329 else {
330 if (MVT::isInteger(ObjectVT))
331 ArgValue = DAG.getConstant(0, ObjectVT);
332 else
333 ArgValue = DAG.getConstantFP(0, ObjectVT);
334 }
335 ArgValues.push_back(ArgValue);
336
337 ArgOffset += ArgIncrement; // Move on to the next argument...
338 }
339
340 // If the function takes variable number of arguments, make a frame index for
341 // the start of the first vararg value... for expansion of llvm.va_start.
342 if (F.isVarArg())
343 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000344 ReturnAddrIndex = 0; // No return address slot generated yet.
345 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000346 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000347
348 // Finally, inform the code generator which regs we return values in.
349 switch (getValueType(F.getReturnType())) {
350 default: assert(0 && "Unknown type!");
351 case MVT::isVoid: break;
352 case MVT::i1:
353 case MVT::i8:
354 case MVT::i16:
355 case MVT::i32:
356 MF.addLiveOut(X86::EAX);
357 break;
358 case MVT::i64:
359 MF.addLiveOut(X86::EAX);
360 MF.addLiveOut(X86::EDX);
361 break;
362 case MVT::f32:
363 case MVT::f64:
364 MF.addLiveOut(X86::ST0);
365 break;
366 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000367 return ArgValues;
368}
369
Chris Lattner5188ad72005-01-08 19:28:19 +0000370std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000371X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000372 bool isVarArg, bool isTailCall,
373 SDOperand Callee, ArgListTy &Args,
374 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000375 // Count how many bytes are to be pushed on the stack.
376 unsigned NumBytes = 0;
377
378 if (Args.empty()) {
379 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000380 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000381 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000382 } else {
383 for (unsigned i = 0, e = Args.size(); i != e; ++i)
384 switch (getValueType(Args[i].second)) {
385 default: assert(0 && "Unknown value type!");
386 case MVT::i1:
387 case MVT::i8:
388 case MVT::i16:
389 case MVT::i32:
390 case MVT::f32:
391 NumBytes += 4;
392 break;
393 case MVT::i64:
394 case MVT::f64:
395 NumBytes += 8;
396 break;
397 }
398
Chris Lattner16cd04d2005-05-12 23:24:06 +0000399 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000400 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000401
402 // Arguments go on the stack in reverse order, as specified by the ABI.
403 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000404 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
405 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000406 std::vector<SDOperand> Stores;
407
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000408 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000409 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
410 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
411
412 switch (getValueType(Args[i].second)) {
413 default: assert(0 && "Unexpected ValueType for argument!");
414 case MVT::i1:
415 case MVT::i8:
416 case MVT::i16:
417 // Promote the integer to 32 bits. If the input type is signed use a
418 // sign extend, otherwise use a zero extend.
419 if (Args[i].second->isSigned())
420 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
421 else
422 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
423
424 // FALL THROUGH
425 case MVT::i32:
426 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000427 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000428 Args[i].first, PtrOff,
429 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000430 ArgOffset += 4;
431 break;
432 case MVT::i64:
433 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000434 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000435 Args[i].first, PtrOff,
436 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000437 ArgOffset += 8;
438 break;
439 }
440 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000441 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000442 }
443
444 std::vector<MVT::ValueType> RetVals;
445 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000446 RetVals.push_back(MVT::Other);
447
Chris Lattner239738a2005-05-14 08:48:15 +0000448 // The result values produced have to be legal. Promote the result.
449 switch (RetTyVT) {
450 case MVT::isVoid: break;
451 default:
452 RetVals.push_back(RetTyVT);
453 break;
454 case MVT::i1:
455 case MVT::i8:
456 case MVT::i16:
457 RetVals.push_back(MVT::i32);
458 break;
459 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000460 if (X86ScalarSSE)
461 RetVals.push_back(MVT::f32);
462 else
463 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000464 break;
465 case MVT::i64:
466 RetVals.push_back(MVT::i32);
467 RetVals.push_back(MVT::i32);
468 break;
469 }
470 std::vector<SDOperand> Ops;
471 Ops.push_back(Chain);
472 Ops.push_back(Callee);
473 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
474 Ops.push_back(DAG.getConstant(0, getPointerTy()));
475 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
476 RetVals, Ops);
477 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
478
479 SDOperand ResultVal;
480 switch (RetTyVT) {
481 case MVT::isVoid: break;
482 default:
483 ResultVal = TheCall.getValue(1);
484 break;
485 case MVT::i1:
486 case MVT::i8:
487 case MVT::i16:
488 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
489 break;
490 case MVT::f32:
491 // FIXME: we would really like to remember that this FP_ROUND operation is
492 // okay to eliminate if we allow excess FP precision.
493 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
494 break;
495 case MVT::i64:
496 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
497 TheCall.getValue(2));
498 break;
499 }
500
501 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000502}
503
Chris Lattnere0fe2252005-07-05 19:58:54 +0000504SDOperand
505X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
506 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000507 // vastart just stores the address of the VarArgsFrameIndex slot.
508 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000509 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
510 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000511}
512
Chris Lattnere0fe2252005-07-05 19:58:54 +0000513
514std::pair<SDOperand,SDOperand>
515X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
516 Value *VAListV, const Type *ArgTy,
517 SelectionDAG &DAG) {
Chris Lattner14824582005-01-09 00:01:27 +0000518 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000519 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
520 VAListP, DAG.getSrcValue(VAListV));
521 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner08568cf2005-07-05 17:50:16 +0000522 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000523 unsigned Amt;
524 if (ArgVT == MVT::i32)
525 Amt = 4;
526 else {
527 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
528 "Other types should have been promoted for varargs!");
529 Amt = 8;
Chris Lattner14824582005-01-09 00:01:27 +0000530 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000531 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
532 DAG.getConstant(Amt, Val.getValueType()));
533 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000534 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000535 return std::make_pair(Result, Chain);
536}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000537
Chris Lattnerc6f41812005-05-12 23:06:28 +0000538//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000539// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000540//===----------------------------------------------------------------------===//
541//
542// The X86 'fast' calling convention passes up to two integer arguments in
543// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
544// and requires that the callee pop its arguments off the stack (allowing proper
545// tail calls), and has the same return value conventions as C calling convs.
546//
Chris Lattner10d26452005-05-13 23:49:10 +0000547// This calling convention always arranges for the callee pop value to be 8n+4
548// bytes, which is needed for tail recursion elimination and stack alignment
549// reasons.
550//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000551// Note that this can be enhanced in the future to pass fp vals in registers
552// (when we have a global fp allocator) and do other tricks.
553//
Chris Lattner63602fb2005-05-13 07:38:09 +0000554
555/// AddLiveIn - This helper function adds the specified physical register to the
556/// MachineFunction as a live in value. It also creates a corresponding virtual
557/// register for it.
558static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
559 TargetRegisterClass *RC) {
560 assert(RC->contains(PReg) && "Not the correct regclass!");
561 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
562 MF.addLiveIn(PReg, VReg);
563 return VReg;
564}
565
566
Chris Lattnerc6f41812005-05-12 23:06:28 +0000567std::vector<SDOperand>
568X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
569 std::vector<SDOperand> ArgValues;
570
571 MachineFunction &MF = DAG.getMachineFunction();
572 MachineFrameInfo *MFI = MF.getFrameInfo();
573
574 // Add DAG nodes to load the arguments... On entry to a function the stack
575 // frame looks like this:
576 //
577 // [ESP] -- return address
578 // [ESP + 4] -- first nonreg argument (leftmost lexically)
579 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
580 // ...
581 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
582
583 // Keep track of the number of integer regs passed so far. This can be either
584 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
585 // used).
586 unsigned NumIntRegs = 0;
587
588 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
589 MVT::ValueType ObjectVT = getValueType(I->getType());
590 unsigned ArgIncrement = 4;
591 unsigned ObjSize = 0;
592 SDOperand ArgValue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000593
Chris Lattnerc6f41812005-05-12 23:06:28 +0000594 switch (ObjectVT) {
595 default: assert(0 && "Unhandled argument type!");
596 case MVT::i1:
597 case MVT::i8:
598 if (NumIntRegs < 2) {
599 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000600 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
601 X86::R8RegisterClass);
602 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000603 DAG.setRoot(ArgValue.getValue(1));
604 }
605 ++NumIntRegs;
606 break;
607 }
608
609 ObjSize = 1;
610 break;
611 case MVT::i16:
612 if (NumIntRegs < 2) {
613 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000614 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
615 X86::R16RegisterClass);
616 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000617 DAG.setRoot(ArgValue.getValue(1));
618 }
619 ++NumIntRegs;
620 break;
621 }
622 ObjSize = 2;
623 break;
624 case MVT::i32:
625 if (NumIntRegs < 2) {
626 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000627 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
628 X86::R32RegisterClass);
629 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000630 DAG.setRoot(ArgValue.getValue(1));
631 }
632 ++NumIntRegs;
633 break;
634 }
635 ObjSize = 4;
636 break;
637 case MVT::i64:
638 if (NumIntRegs == 0) {
639 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000640 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
641 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000642
Chris Lattner63602fb2005-05-13 07:38:09 +0000643 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
644 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000645 DAG.setRoot(Hi.getValue(1));
646
647 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
648 }
649 NumIntRegs = 2;
650 break;
651 } else if (NumIntRegs == 1) {
652 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000653 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
654 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000655 DAG.setRoot(Low.getValue(1));
656
657 // Load the high part from memory.
658 // Create the frame index object for this incoming parameter...
659 int FI = MFI->CreateFixedObject(4, ArgOffset);
660 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
661 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
662 DAG.getSrcValue(NULL));
663 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
664 }
665 ArgOffset += 4;
666 NumIntRegs = 2;
667 break;
668 }
669 ObjSize = ArgIncrement = 8;
670 break;
671 case MVT::f32: ObjSize = 4; break;
672 case MVT::f64: ObjSize = ArgIncrement = 8; break;
673 }
674
675 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
676 // dead loads.
677 if (ObjSize && !I->use_empty()) {
678 // Create the frame index object for this incoming parameter...
679 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
680
681 // Create the SelectionDAG nodes corresponding to a load from this
682 // parameter.
683 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
684
685 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
686 DAG.getSrcValue(NULL));
687 } else if (ArgValue.Val == 0) {
688 if (MVT::isInteger(ObjectVT))
689 ArgValue = DAG.getConstant(0, ObjectVT);
690 else
691 ArgValue = DAG.getConstantFP(0, ObjectVT);
692 }
693 ArgValues.push_back(ArgValue);
694
695 if (ObjSize)
696 ArgOffset += ArgIncrement; // Move on to the next argument.
697 }
698
Chris Lattner10d26452005-05-13 23:49:10 +0000699 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
700 // arguments and the arguments after the retaddr has been pushed are aligned.
701 if ((ArgOffset & 7) == 0)
702 ArgOffset += 4;
703
Chris Lattner3648c672005-05-13 21:44:04 +0000704 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
705 ReturnAddrIndex = 0; // No return address slot generated yet.
706 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattner381e8872005-05-15 05:46:45 +0000707 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +0000708
709 // Finally, inform the code generator which regs we return values in.
710 switch (getValueType(F.getReturnType())) {
711 default: assert(0 && "Unknown type!");
712 case MVT::isVoid: break;
713 case MVT::i1:
714 case MVT::i8:
715 case MVT::i16:
716 case MVT::i32:
717 MF.addLiveOut(X86::EAX);
718 break;
719 case MVT::i64:
720 MF.addLiveOut(X86::EAX);
721 MF.addLiveOut(X86::EDX);
722 break;
723 case MVT::f32:
724 case MVT::f64:
725 MF.addLiveOut(X86::ST0);
726 break;
727 }
728 return ArgValues;
729}
730
731std::pair<SDOperand, SDOperand>
732X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000733 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000734 ArgListTy &Args, SelectionDAG &DAG) {
735 // Count how many bytes are to be pushed on the stack.
736 unsigned NumBytes = 0;
737
738 // Keep track of the number of integer regs passed so far. This can be either
739 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
740 // used).
741 unsigned NumIntRegs = 0;
742
743 for (unsigned i = 0, e = Args.size(); i != e; ++i)
744 switch (getValueType(Args[i].second)) {
745 default: assert(0 && "Unknown value type!");
746 case MVT::i1:
747 case MVT::i8:
748 case MVT::i16:
749 case MVT::i32:
750 if (NumIntRegs < 2) {
751 ++NumIntRegs;
752 break;
753 }
754 // fall through
755 case MVT::f32:
756 NumBytes += 4;
757 break;
758 case MVT::i64:
759 if (NumIntRegs == 0) {
760 NumIntRegs = 2;
761 break;
762 } else if (NumIntRegs == 1) {
763 NumIntRegs = 2;
764 NumBytes += 4;
765 break;
766 }
767
768 // fall through
769 case MVT::f64:
770 NumBytes += 8;
771 break;
772 }
773
Chris Lattner10d26452005-05-13 23:49:10 +0000774 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
775 // arguments and the arguments after the retaddr has been pushed are aligned.
776 if ((NumBytes & 7) == 0)
777 NumBytes += 4;
778
Chris Lattner16cd04d2005-05-12 23:24:06 +0000779 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000780 DAG.getConstant(NumBytes, getPointerTy()));
781
782 // Arguments go on the stack in reverse order, as specified by the ABI.
783 unsigned ArgOffset = 0;
784 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
785 DAG.getEntryNode());
786 NumIntRegs = 0;
787 std::vector<SDOperand> Stores;
788 std::vector<SDOperand> RegValuesToPass;
789 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
790 switch (getValueType(Args[i].second)) {
791 default: assert(0 && "Unexpected ValueType for argument!");
792 case MVT::i1:
793 case MVT::i8:
794 case MVT::i16:
795 case MVT::i32:
796 if (NumIntRegs < 2) {
797 RegValuesToPass.push_back(Args[i].first);
798 ++NumIntRegs;
799 break;
800 }
801 // Fall through
802 case MVT::f32: {
803 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
804 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
805 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
806 Args[i].first, PtrOff,
807 DAG.getSrcValue(NULL)));
808 ArgOffset += 4;
809 break;
810 }
811 case MVT::i64:
812 if (NumIntRegs < 2) { // Can pass part of it in regs?
813 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
814 Args[i].first, DAG.getConstant(1, MVT::i32));
815 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
816 Args[i].first, DAG.getConstant(0, MVT::i32));
817 RegValuesToPass.push_back(Lo);
818 ++NumIntRegs;
819 if (NumIntRegs < 2) { // Pass both parts in regs?
820 RegValuesToPass.push_back(Hi);
821 ++NumIntRegs;
822 } else {
823 // Pass the high part in memory.
824 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
825 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
826 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000827 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000828 ArgOffset += 4;
829 }
830 break;
831 }
832 // Fall through
833 case MVT::f64:
834 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
835 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
836 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
837 Args[i].first, PtrOff,
838 DAG.getSrcValue(NULL)));
839 ArgOffset += 8;
840 break;
841 }
842 }
843 if (!Stores.empty())
844 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
845
Chris Lattner10d26452005-05-13 23:49:10 +0000846 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
847 // arguments and the arguments after the retaddr has been pushed are aligned.
848 if ((ArgOffset & 7) == 0)
849 ArgOffset += 4;
850
Chris Lattner239738a2005-05-14 08:48:15 +0000851 std::vector<MVT::ValueType> RetVals;
852 MVT::ValueType RetTyVT = getValueType(RetTy);
853
854 RetVals.push_back(MVT::Other);
855
856 // The result values produced have to be legal. Promote the result.
857 switch (RetTyVT) {
858 case MVT::isVoid: break;
859 default:
860 RetVals.push_back(RetTyVT);
861 break;
862 case MVT::i1:
863 case MVT::i8:
864 case MVT::i16:
865 RetVals.push_back(MVT::i32);
866 break;
867 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000868 if (X86ScalarSSE)
869 RetVals.push_back(MVT::f32);
870 else
871 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000872 break;
873 case MVT::i64:
874 RetVals.push_back(MVT::i32);
875 RetVals.push_back(MVT::i32);
876 break;
877 }
878
879 std::vector<SDOperand> Ops;
880 Ops.push_back(Chain);
881 Ops.push_back(Callee);
882 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
883 // Callee pops all arg values on the stack.
884 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
885
886 // Pass register arguments as needed.
887 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
888
889 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
890 RetVals, Ops);
891 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
892
893 SDOperand ResultVal;
894 switch (RetTyVT) {
895 case MVT::isVoid: break;
896 default:
897 ResultVal = TheCall.getValue(1);
898 break;
899 case MVT::i1:
900 case MVT::i8:
901 case MVT::i16:
902 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
903 break;
904 case MVT::f32:
905 // FIXME: we would really like to remember that this FP_ROUND operation is
906 // okay to eliminate if we allow excess FP precision.
907 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
908 break;
909 case MVT::i64:
910 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
911 TheCall.getValue(2));
912 break;
913 }
914
915 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000916}
917
Chris Lattner381e8872005-05-15 05:46:45 +0000918SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
919 if (ReturnAddrIndex == 0) {
920 // Set up a frame object for the return address.
921 MachineFunction &MF = DAG.getMachineFunction();
922 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
923 }
924
925 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
926}
Chris Lattnerc6f41812005-05-12 23:06:28 +0000927
928
Chris Lattner14824582005-01-09 00:01:27 +0000929
930std::pair<SDOperand, SDOperand> X86TargetLowering::
931LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
932 SelectionDAG &DAG) {
933 SDOperand Result;
934 if (Depth) // Depths > 0 not supported yet!
935 Result = DAG.getConstant(0, getPointerTy());
936 else {
Chris Lattner381e8872005-05-15 05:46:45 +0000937 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000938 if (!isFrameAddress)
939 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000940 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
941 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000942 else
943 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
944 DAG.getConstant(4, MVT::i32));
945 }
946 return std::make_pair(Result, Chain);
947}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000948
Chris Lattnera28381c2005-07-16 00:28:20 +0000949//===----------------------------------------------------------------------===//
950// X86 Custom Lowering Hooks
951//===----------------------------------------------------------------------===//
952
Chris Lattner67649df2005-05-14 06:52:07 +0000953/// LowerOperation - Provide custom lowering hooks for some operations.
954///
955SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
956 switch (Op.getOpcode()) {
957 default: assert(0 && "Should not custom lower this!");
Chris Lattner745d5382005-07-29 00:40:01 +0000958 case ISD::SINT_TO_FP: {
Chris Lattner67649df2005-05-14 06:52:07 +0000959 assert(Op.getValueType() == MVT::f64 &&
960 Op.getOperand(0).getValueType() == MVT::i64 &&
961 "Unknown SINT_TO_FP to lower!");
962 // We lower sint64->FP into a store to a temporary stack slot, followed by a
963 // FILD64m node.
964 MachineFunction &MF = DAG.getMachineFunction();
965 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
966 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
967 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
968 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
969 std::vector<MVT::ValueType> RTs;
970 RTs.push_back(MVT::f64);
971 RTs.push_back(MVT::Other);
972 std::vector<SDOperand> Ops;
973 Ops.push_back(Store);
974 Ops.push_back(StackSlot);
975 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
976 }
Chris Lattner745d5382005-07-29 00:40:01 +0000977 case ISD::FP_TO_SINT: {
978 assert(Op.getValueType() == MVT::i64 &&
979 Op.getOperand(0).getValueType() == MVT::f64 &&
980 "Unknown FP_TO_SINT to lower!");
981 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
982 // stack slot.
983 MachineFunction &MF = DAG.getMachineFunction();
984 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
985 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
986
987 // Build the FISTP64
988 std::vector<SDOperand> Ops;
989 Ops.push_back(DAG.getEntryNode());
990 Ops.push_back(Op.getOperand(0));
991 Ops.push_back(StackSlot);
Chris Lattnerf7443da2005-07-29 00:54:34 +0000992 SDOperand FIST = DAG.getNode(X86ISD::FP_TO_INT64_IN_MEM, MVT::Other, Ops);
Chris Lattner745d5382005-07-29 00:40:01 +0000993
994 // Load the result.
Chris Lattnerf7443da2005-07-29 00:54:34 +0000995 return DAG.getLoad(MVT::i64, FIST, StackSlot, DAG.getSrcValue(NULL));
Chris Lattner745d5382005-07-29 00:40:01 +0000996 }
997 }
Chris Lattner67649df2005-05-14 06:52:07 +0000998}
999
1000
1001//===----------------------------------------------------------------------===//
1002// Pattern Matcher Implementation
1003//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001004
Chris Lattner98a8ba02005-01-18 01:06:26 +00001005namespace {
1006 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
1007 /// SDOperand's instead of register numbers for the leaves of the matched
1008 /// tree.
1009 struct X86ISelAddressMode {
1010 enum {
1011 RegBase,
1012 FrameIndexBase,
1013 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001014
Chris Lattner98a8ba02005-01-18 01:06:26 +00001015 struct { // This is really a union, discriminated by BaseType!
1016 SDOperand Reg;
1017 int FrameIndex;
1018 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001019
Chris Lattner98a8ba02005-01-18 01:06:26 +00001020 unsigned Scale;
1021 SDOperand IndexReg;
1022 unsigned Disp;
1023 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001024
Chris Lattner98a8ba02005-01-18 01:06:26 +00001025 X86ISelAddressMode()
1026 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1027 }
1028 };
1029}
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001030
1031
1032namespace {
1033 Statistic<>
1034 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1035
1036 //===--------------------------------------------------------------------===//
1037 /// ISel - X86 specific code to select X86 machine instructions for
1038 /// SelectionDAG operations.
1039 ///
1040 class ISel : public SelectionDAGISel {
1041 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1042 /// register should set this to true.
1043 bool ContainsFPCode;
1044
1045 /// X86Lowering - This object fully describes how to lower LLVM code to an
1046 /// X86-specific SelectionDAG.
1047 X86TargetLowering X86Lowering;
1048
Chris Lattner11333092005-01-11 03:11:44 +00001049 /// RegPressureMap - This keeps an approximate count of the number of
1050 /// registers required to evaluate each node in the graph.
1051 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001052
1053 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1054 /// vreg the value is produced in, so we only emit one copy of each compiled
1055 /// tree.
1056 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001057
Chris Lattner381e8872005-05-15 05:46:45 +00001058 /// TheDAG - The DAG being selected during Select* operations.
1059 SelectionDAG *TheDAG;
Jeff Cohen00b168892005-07-27 06:12:32 +00001060
1061 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanfb5792f2005-07-12 01:41:54 +00001062 /// make the right decision when generating code for different targets.
1063 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 public:
1065 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001066 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001067 }
1068
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001069 virtual const char *getPassName() const {
1070 return "X86 Pattern Instruction Selection";
1071 }
1072
Chris Lattner11333092005-01-11 03:11:44 +00001073 unsigned getRegPressure(SDOperand O) {
1074 return RegPressureMap[O.Val];
1075 }
1076 unsigned ComputeRegPressure(SDOperand O);
1077
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001078 /// InstructionSelectBasicBlock - This callback is invoked by
1079 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001080 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001081
Chris Lattner63602fb2005-05-13 07:38:09 +00001082 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1083
Chris Lattner44129b52005-01-25 20:03:11 +00001084 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1085 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001086 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001087 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001088 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001089 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001090 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001091 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1092 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001093 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001094
1095 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1096 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1097 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner381e8872005-05-15 05:46:45 +00001098 bool EmitPotentialTailCall(SDNode *Node);
1099 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001100 void Select(SDOperand N);
1101 };
1102}
1103
Chris Lattner6415bb42005-05-10 03:53:18 +00001104/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1105/// the main function.
1106static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1107 MachineFrameInfo *MFI) {
1108 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1109 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1110 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1111
1112 // Set the high part to be 64-bit precision.
1113 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1114 CWFrameIdx, 1).addImm(2);
1115
1116 // Reload the modified control word now.
1117 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1118}
1119
Chris Lattner63602fb2005-05-13 07:38:09 +00001120void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1121 // If this function has live-in values, emit the copies from pregs to vregs at
1122 // the top of the function, before anything else.
1123 MachineBasicBlock *BB = MF.begin();
1124 if (MF.livein_begin() != MF.livein_end()) {
1125 SSARegMap *RegMap = MF.getSSARegMap();
1126 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1127 E = MF.livein_end(); LI != E; ++LI) {
1128 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1129 if (RC == X86::R8RegisterClass) {
1130 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1131 } else if (RC == X86::R16RegisterClass) {
1132 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1133 } else if (RC == X86::R32RegisterClass) {
1134 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1135 } else if (RC == X86::RFPRegisterClass) {
1136 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001137 } else if (RC == X86::RXMMRegisterClass) {
1138 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001139 } else {
1140 assert(0 && "Unknown regclass!");
1141 }
1142 }
1143 }
1144
1145
1146 // If this is main, emit special code for main.
1147 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1148 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1149}
1150
1151
Chris Lattner7dbcb752005-01-12 04:21:28 +00001152/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1153/// when it has created a SelectionDAG for us to codegen.
1154void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1155 // While we're doing this, keep track of whether we see any FP code for
1156 // FP_REG_KILL insertion.
1157 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001158 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001159
1160 // Scan the PHI nodes that already are inserted into this basic block. If any
1161 // of them is a PHI of a floating point value, we need to insert an
1162 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001163 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001164 if (BB != MF->begin())
1165 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1166 I != E; ++I) {
1167 assert(I->getOpcode() == X86::PHI &&
1168 "Isn't just PHI nodes?");
1169 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1170 X86::RFPRegisterClass) {
1171 ContainsFPCode = true;
1172 break;
1173 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001174 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001175
Chris Lattner7dbcb752005-01-12 04:21:28 +00001176 // Compute the RegPressureMap, which is an approximation for the number of
1177 // registers required to compute each node.
1178 ComputeRegPressure(DAG.getRoot());
1179
Chris Lattner381e8872005-05-15 05:46:45 +00001180 TheDAG = &DAG;
1181
Chris Lattner7dbcb752005-01-12 04:21:28 +00001182 // Codegen the basic block.
1183 Select(DAG.getRoot());
1184
Chris Lattner381e8872005-05-15 05:46:45 +00001185 TheDAG = 0;
1186
Chris Lattner7dbcb752005-01-12 04:21:28 +00001187 // Finally, look at all of the successors of this block. If any contain a PHI
1188 // node of FP type, we need to insert an FP_REG_KILL in this block.
1189 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1190 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1191 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1192 I != E && I->getOpcode() == X86::PHI; ++I) {
1193 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1194 X86::RFPRegisterClass) {
1195 ContainsFPCode = true;
1196 break;
1197 }
1198 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001199
Chris Lattnere3e0f272005-05-09 03:36:39 +00001200 // Final check, check LLVM BB's that are successors to the LLVM BB
1201 // corresponding to BB for FP PHI nodes.
1202 const BasicBlock *LLVMBB = BB->getBasicBlock();
1203 const PHINode *PN;
1204 if (!ContainsFPCode)
1205 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1206 SI != E && !ContainsFPCode; ++SI)
1207 for (BasicBlock::const_iterator II = SI->begin();
1208 (PN = dyn_cast<PHINode>(II)); ++II)
1209 if (PN->getType()->isFloatingPoint()) {
1210 ContainsFPCode = true;
1211 break;
1212 }
1213
1214
Chris Lattner7dbcb752005-01-12 04:21:28 +00001215 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1216 // only occurs due to the floating point stackifier not being aggressive
1217 // enough to handle arbitrary global stackification.
1218 //
1219 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1220 // defines a floating point virtual register.
1221 //
1222 // When the global register allocators (like linear scan) finally update live
1223 // variable analysis, we can keep floating point values in registers across
1224 // basic blocks. This will be a huge win, but we are waiting on the global
1225 // allocators before we can do this.
1226 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001227 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001228 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1229 ++NumFPKill;
1230 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001231
Chris Lattner7dbcb752005-01-12 04:21:28 +00001232 // Clear state used for selection.
1233 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001234 RegPressureMap.clear();
1235}
1236
1237
Chris Lattner11333092005-01-11 03:11:44 +00001238// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1239// for the number of registers required to compute each node. This is basically
1240// computing a generalized form of the Sethi-Ullman number for each node.
1241unsigned ISel::ComputeRegPressure(SDOperand O) {
1242 SDNode *N = O.Val;
1243 unsigned &Result = RegPressureMap[N];
1244 if (Result) return Result;
1245
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001246 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1247 // higher fixed cost??
1248
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001249 if (N->getNumOperands() == 0) {
1250 Result = 1;
1251 } else {
1252 unsigned MaxRegUse = 0;
1253 unsigned NumExtraMaxRegUsers = 0;
1254 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1255 unsigned Regs;
1256 if (N->getOperand(i).getOpcode() == ISD::Constant)
1257 Regs = 0;
1258 else
1259 Regs = ComputeRegPressure(N->getOperand(i));
1260 if (Regs > MaxRegUse) {
1261 MaxRegUse = Regs;
1262 NumExtraMaxRegUsers = 0;
1263 } else if (Regs == MaxRegUse &&
1264 N->getOperand(i).getValueType() != MVT::Other) {
1265 ++NumExtraMaxRegUsers;
1266 }
Chris Lattner11333092005-01-11 03:11:44 +00001267 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001268
1269 if (O.getOpcode() != ISD::TokenFactor)
1270 Result = MaxRegUse+NumExtraMaxRegUsers;
1271 else
Chris Lattner869e0432005-01-17 23:02:13 +00001272 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001273 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001274
Chris Lattner837caa72005-01-11 23:21:30 +00001275 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001276 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001277}
1278
Chris Lattnerbf52d492005-01-20 16:50:16 +00001279/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1280/// The DAG cannot have cycles in it, by definition, so the visited set is not
1281/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1282/// reuse, so it prevents exponential cases.
1283///
1284static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1285 std::set<SDNode*> &Visited) {
1286 if (N == Op) return true; // Found it.
1287 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001288 if (Node->getNumOperands() == 0 || // Leaf?
1289 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001290 if (!Visited.insert(Node).second) return false; // Already visited?
1291
1292 // Recurse for the first N-1 operands.
1293 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1294 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1295 return true;
1296
1297 // Tail recurse for the last operand.
1298 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1299}
1300
Chris Lattner98a8ba02005-01-18 01:06:26 +00001301X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1302 X86AddressMode Result;
1303
1304 // If we need to emit two register operands, emit the one with the highest
1305 // register pressure first.
1306 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1307 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001308 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001309 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001310 std::set<SDNode*> Visited;
1311 EmitBaseThenIndex = true;
1312 // If Base ends up pointing to Index, we must emit index first. This is
1313 // because of the way we fold loads, we may end up doing bad things with
1314 // the folded add.
1315 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1316 EmitBaseThenIndex = false;
1317 } else {
1318 std::set<SDNode*> Visited;
1319 EmitBaseThenIndex = false;
1320 // If Base ends up pointing to Index, we must emit index first. This is
1321 // because of the way we fold loads, we may end up doing bad things with
1322 // the folded add.
1323 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1324 EmitBaseThenIndex = true;
1325 }
1326
1327 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001328 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1329 Result.IndexReg = SelectExpr(IAM.IndexReg);
1330 } else {
1331 Result.IndexReg = SelectExpr(IAM.IndexReg);
1332 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1333 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001334
Chris Lattner98a8ba02005-01-18 01:06:26 +00001335 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1336 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1337 } else if (IAM.IndexReg.Val) {
1338 Result.IndexReg = SelectExpr(IAM.IndexReg);
1339 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001340
Chris Lattner98a8ba02005-01-18 01:06:26 +00001341 switch (IAM.BaseType) {
1342 case X86ISelAddressMode::RegBase:
1343 Result.BaseType = X86AddressMode::RegBase;
1344 break;
1345 case X86ISelAddressMode::FrameIndexBase:
1346 Result.BaseType = X86AddressMode::FrameIndexBase;
1347 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1348 break;
1349 default:
1350 assert(0 && "Unknown base type!");
1351 break;
1352 }
1353 Result.Scale = IAM.Scale;
1354 Result.Disp = IAM.Disp;
1355 Result.GV = IAM.GV;
1356 return Result;
1357}
1358
1359/// SelectAddress - Pattern match the maximal addressing mode for this node and
1360/// emit all of the leaf registers.
1361void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1362 X86ISelAddressMode IAM;
1363 MatchAddress(N, IAM);
1364 AM = SelectAddrExprs(IAM);
1365}
1366
1367/// MatchAddress - Add the specified node to the specified addressing mode,
1368/// returning true if it cannot be done. This just pattern matches for the
1369/// addressing mode, it does not cause any code to be emitted. For that, use
1370/// SelectAddress.
1371bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001372 switch (N.getOpcode()) {
1373 default: break;
1374 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001375 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1376 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001377 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1378 return false;
1379 }
1380 break;
1381 case ISD::GlobalAddress:
1382 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001383 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1384 // For Darwin, external and weak symbols are indirect, so we want to load
1385 // the value at address GV, not the value of GV itself. This means that
1386 // the GlobalAddress must be in the base or index register of the address,
1387 // not the GV offset field.
Jeff Cohen00b168892005-07-27 06:12:32 +00001388 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00001389 (GV->hasWeakLinkage() || GV->isExternal())) {
1390 break;
1391 } else {
1392 AM.GV = GV;
1393 return false;
1394 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001395 }
1396 break;
1397 case ISD::Constant:
1398 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1399 return false;
1400 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001401 // We might have folded the load into this shift, so don't regen the value
1402 // if so.
1403 if (ExprMap.count(N)) break;
1404
Chris Lattner98a8ba02005-01-18 01:06:26 +00001405 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001406 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1407 unsigned Val = CN->getValue();
1408 if (Val == 1 || Val == 2 || Val == 3) {
1409 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001410 SDOperand ShVal = N.Val->getOperand(0);
1411
1412 // Okay, we know that we have a scale by now. However, if the scaled
1413 // value is an add of something and a constant, we can fold the
1414 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001415 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001416 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001417 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001418 ConstantSDNode *AddVal =
1419 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1420 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001421 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001422 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001423 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001424 return false;
1425 }
1426 }
1427 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001428 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001429 // We might have folded the load into this mul, so don't regen the value if
1430 // so.
1431 if (ExprMap.count(N)) break;
1432
Chris Lattner947d5442005-01-11 19:37:02 +00001433 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001434 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1435 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001436 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1437 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1438 AM.Scale = unsigned(CN->getValue())-1;
1439
1440 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001441 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001442
1443 // Okay, we know that we have a scale by now. However, if the scaled
1444 // value is an add of something and a constant, we can fold the
1445 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001446 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001447 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001448 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001449 ConstantSDNode *AddVal =
1450 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1451 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001452 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001453 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001454 }
1455
1456 AM.IndexReg = AM.Base.Reg = Reg;
1457 return false;
1458 }
1459 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001460
1461 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001462 // We might have folded the load into this mul, so don't regen the value if
1463 // so.
1464 if (ExprMap.count(N)) break;
1465
Chris Lattner98a8ba02005-01-18 01:06:26 +00001466 X86ISelAddressMode Backup = AM;
1467 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1468 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 return false;
1470 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001471 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1472 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001473 return false;
1474 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001475 break;
1476 }
1477 }
1478
Chris Lattnera95589b2005-01-11 04:40:19 +00001479 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001480 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001481 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001482 if (AM.IndexReg.Val == 0) {
1483 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001484 AM.Scale = 1;
1485 return false;
1486 }
1487
1488 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001489 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001490 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001491
1492 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001493 AM.BaseType = X86ISelAddressMode::RegBase;
1494 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001495 return false;
1496}
1497
1498/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1499/// assuming that the temporary registers are in the 8-bit register class.
1500///
1501/// Tmp1 = setcc1
1502/// Tmp2 = setcc2
1503/// DestReg = logicalop Tmp1, Tmp2
1504///
1505static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1506 unsigned SetCC2, unsigned LogicalOp,
1507 unsigned DestReg) {
1508 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1509 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1510 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1511 BuildMI(BB, SetCC1, 0, Tmp1);
1512 BuildMI(BB, SetCC2, 0, Tmp2);
1513 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1514}
1515
1516/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1517/// condition codes match the specified SetCCOpcode. Note that some conditions
1518/// require multiple instructions to generate the correct value.
1519static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1520 ISD::CondCode SetCCOpcode, bool isFP) {
1521 unsigned Opc;
1522 if (!isFP) {
1523 switch (SetCCOpcode) {
1524 default: assert(0 && "Illegal integer SetCC!");
1525 case ISD::SETEQ: Opc = X86::SETEr; break;
1526 case ISD::SETGT: Opc = X86::SETGr; break;
1527 case ISD::SETGE: Opc = X86::SETGEr; break;
1528 case ISD::SETLT: Opc = X86::SETLr; break;
1529 case ISD::SETLE: Opc = X86::SETLEr; break;
1530 case ISD::SETNE: Opc = X86::SETNEr; break;
1531 case ISD::SETULT: Opc = X86::SETBr; break;
1532 case ISD::SETUGT: Opc = X86::SETAr; break;
1533 case ISD::SETULE: Opc = X86::SETBEr; break;
1534 case ISD::SETUGE: Opc = X86::SETAEr; break;
1535 }
1536 } else {
1537 // On a floating point condition, the flags are set as follows:
1538 // ZF PF CF op
1539 // 0 | 0 | 0 | X > Y
1540 // 0 | 0 | 1 | X < Y
1541 // 1 | 0 | 0 | X == Y
1542 // 1 | 1 | 1 | unordered
1543 //
1544 switch (SetCCOpcode) {
1545 default: assert(0 && "Invalid FP setcc!");
1546 case ISD::SETUEQ:
1547 case ISD::SETEQ:
1548 Opc = X86::SETEr; // True if ZF = 1
1549 break;
1550 case ISD::SETOGT:
1551 case ISD::SETGT:
1552 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1553 break;
1554 case ISD::SETOGE:
1555 case ISD::SETGE:
1556 Opc = X86::SETAEr; // True if CF = 0
1557 break;
1558 case ISD::SETULT:
1559 case ISD::SETLT:
1560 Opc = X86::SETBr; // True if CF = 1
1561 break;
1562 case ISD::SETULE:
1563 case ISD::SETLE:
1564 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1565 break;
1566 case ISD::SETONE:
1567 case ISD::SETNE:
1568 Opc = X86::SETNEr; // True if ZF = 0
1569 break;
1570 case ISD::SETUO:
1571 Opc = X86::SETPr; // True if PF = 1
1572 break;
1573 case ISD::SETO:
1574 Opc = X86::SETNPr; // True if PF = 0
1575 break;
1576 case ISD::SETOEQ: // !PF & ZF
1577 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1578 return;
1579 case ISD::SETOLT: // !PF & CF
1580 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1581 return;
1582 case ISD::SETOLE: // !PF & (CF || ZF)
1583 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1584 return;
1585 case ISD::SETUGT: // PF | (!ZF & !CF)
1586 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1587 return;
1588 case ISD::SETUGE: // PF | !CF
1589 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1590 return;
1591 case ISD::SETUNE: // PF | !ZF
1592 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1593 return;
1594 }
1595 }
1596 BuildMI(BB, Opc, 0, DestReg);
1597}
1598
1599
1600/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1601/// the Dest block if the Cond condition is true. If we cannot fold this
1602/// condition into the branch, return true.
1603///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001604bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1605 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001606 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1607 // B) using two conditional branches instead of one condbr, two setcc's, and
1608 // an or.
1609 if ((Cond.getOpcode() == ISD::OR ||
1610 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1611 // And and or set the flags for us, so there is no need to emit a TST of the
1612 // result. It is only safe to do this if there is only a single use of the
1613 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001614 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001615 SelectExpr(Cond);
1616 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1617 return false;
1618 }
1619
1620 // Codegen br not C -> JE.
1621 if (Cond.getOpcode() == ISD::XOR)
1622 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1623 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001624 unsigned CondR;
1625 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1626 Select(Chain);
1627 CondR = SelectExpr(Cond.Val->getOperand(0));
1628 } else {
1629 CondR = SelectExpr(Cond.Val->getOperand(0));
1630 Select(Chain);
1631 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001632 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1633 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1634 return false;
1635 }
1636
1637 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1638 if (SetCC == 0)
1639 return true; // Can only handle simple setcc's so far.
1640
1641 unsigned Opc;
1642
1643 // Handle integer conditions first.
1644 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1645 switch (SetCC->getCondition()) {
1646 default: assert(0 && "Illegal integer SetCC!");
1647 case ISD::SETEQ: Opc = X86::JE; break;
1648 case ISD::SETGT: Opc = X86::JG; break;
1649 case ISD::SETGE: Opc = X86::JGE; break;
1650 case ISD::SETLT: Opc = X86::JL; break;
1651 case ISD::SETLE: Opc = X86::JLE; break;
1652 case ISD::SETNE: Opc = X86::JNE; break;
1653 case ISD::SETULT: Opc = X86::JB; break;
1654 case ISD::SETUGT: Opc = X86::JA; break;
1655 case ISD::SETULE: Opc = X86::JBE; break;
1656 case ISD::SETUGE: Opc = X86::JAE; break;
1657 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001658 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001659 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001660 BuildMI(BB, Opc, 1).addMBB(Dest);
1661 return false;
1662 }
1663
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001664 unsigned Opc2 = 0; // Second branch if needed.
1665
1666 // On a floating point condition, the flags are set as follows:
1667 // ZF PF CF op
1668 // 0 | 0 | 0 | X > Y
1669 // 0 | 0 | 1 | X < Y
1670 // 1 | 0 | 0 | X == Y
1671 // 1 | 1 | 1 | unordered
1672 //
1673 switch (SetCC->getCondition()) {
1674 default: assert(0 && "Invalid FP setcc!");
1675 case ISD::SETUEQ:
1676 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1677 case ISD::SETOGT:
1678 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1679 case ISD::SETOGE:
1680 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1681 case ISD::SETULT:
1682 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1683 case ISD::SETULE:
1684 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1685 case ISD::SETONE:
1686 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1687 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1688 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1689 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1690 Opc = X86::JA; // ZF = 0 & CF = 0
1691 Opc2 = X86::JP; // PF = 1
1692 break;
1693 case ISD::SETUGE: // PF = 1 | CF = 0
1694 Opc = X86::JAE; // CF = 0
1695 Opc2 = X86::JP; // PF = 1
1696 break;
1697 case ISD::SETUNE: // PF = 1 | ZF = 0
1698 Opc = X86::JNE; // ZF = 0
1699 Opc2 = X86::JP; // PF = 1
1700 break;
1701 case ISD::SETOEQ: // PF = 0 & ZF = 1
1702 //X86::JNP, X86::JE
1703 //X86::AND8rr
1704 return true; // FIXME: Emit more efficient code for this branch.
1705 case ISD::SETOLT: // PF = 0 & CF = 1
1706 //X86::JNP, X86::JB
1707 //X86::AND8rr
1708 return true; // FIXME: Emit more efficient code for this branch.
1709 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1710 //X86::JNP, X86::JBE
1711 //X86::AND8rr
1712 return true; // FIXME: Emit more efficient code for this branch.
1713 }
1714
Chris Lattner6c07aee2005-01-11 04:06:27 +00001715 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001716 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717 BuildMI(BB, Opc, 1).addMBB(Dest);
1718 if (Opc2)
1719 BuildMI(BB, Opc2, 1).addMBB(Dest);
1720 return false;
1721}
1722
Chris Lattner24aad1b2005-01-10 22:10:13 +00001723/// EmitSelectCC - Emit code into BB that performs a select operation between
1724/// the two registers RTrue and RFalse, generating a result into RDest. Return
1725/// true if the fold cannot be performed.
1726///
1727void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1728 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1729 enum Condition {
1730 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1731 NOT_SET
1732 } CondCode = NOT_SET;
1733
1734 static const unsigned CMOVTAB16[] = {
1735 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1736 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001737 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001738 };
1739 static const unsigned CMOVTAB32[] = {
1740 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1741 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001742 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001743 };
1744 static const unsigned CMOVTABFP[] = {
1745 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1746 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1747 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1748 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001749 static const int SSE_CMOVTAB[] = {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001750 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
Nate Begeman16b04f32005-07-15 00:38:55 +00001751 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0,
Nate Begemanf63be7d2005-07-06 18:59:04 +00001752 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1753 };
Chris Lattner24aad1b2005-01-10 22:10:13 +00001754
1755 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1756 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1757 switch (SetCC->getCondition()) {
1758 default: assert(0 && "Unknown integer comparison!");
1759 case ISD::SETEQ: CondCode = EQ; break;
1760 case ISD::SETGT: CondCode = GT; break;
1761 case ISD::SETGE: CondCode = GE; break;
1762 case ISD::SETLT: CondCode = LT; break;
1763 case ISD::SETLE: CondCode = LE; break;
1764 case ISD::SETNE: CondCode = NE; break;
1765 case ISD::SETULT: CondCode = B; break;
1766 case ISD::SETUGT: CondCode = A; break;
1767 case ISD::SETULE: CondCode = BE; break;
1768 case ISD::SETUGE: CondCode = AE; break;
1769 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001770 } else if (X86ScalarSSE) {
1771 switch (SetCC->getCondition()) {
1772 default: assert(0 && "Unknown scalar fp comparison!");
1773 case ISD::SETEQ: CondCode = EQ; break;
1774 case ISD::SETNE: CondCode = NE; break;
1775 case ISD::SETULT:
1776 case ISD::SETLT: CondCode = LT; break;
1777 case ISD::SETULE:
1778 case ISD::SETLE: CondCode = LE; break;
1779 case ISD::SETUGT:
1780 case ISD::SETGT: CondCode = GT; break;
1781 case ISD::SETUGE:
1782 case ISD::SETGE: CondCode = GE; break;
1783 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001784 } else {
1785 // On a floating point condition, the flags are set as follows:
1786 // ZF PF CF op
1787 // 0 | 0 | 0 | X > Y
1788 // 0 | 0 | 1 | X < Y
1789 // 1 | 0 | 0 | X == Y
1790 // 1 | 1 | 1 | unordered
1791 //
1792 switch (SetCC->getCondition()) {
1793 default: assert(0 && "Unknown FP comparison!");
1794 case ISD::SETUEQ:
1795 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1796 case ISD::SETOGT:
1797 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1798 case ISD::SETOGE:
1799 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1800 case ISD::SETULT:
1801 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1802 case ISD::SETULE:
1803 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1804 case ISD::SETONE:
1805 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1806 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1807 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1808 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1809 case ISD::SETUGE: // PF = 1 | CF = 0
1810 case ISD::SETUNE: // PF = 1 | ZF = 0
1811 case ISD::SETOEQ: // PF = 0 & ZF = 1
1812 case ISD::SETOLT: // PF = 0 & CF = 1
1813 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1814 // We cannot emit this comparison as a single cmov.
1815 break;
1816 }
1817 }
1818 }
1819
Nate Begemanf63be7d2005-07-06 18:59:04 +00001820 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1821 // Others we will have to do the PowerPC thing and generate an MBB for the
1822 // true and false values and select between them with a PHI.
Jeff Cohen00b168892005-07-27 06:12:32 +00001823 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
Nate Begeman16b04f32005-07-15 00:38:55 +00001824 if (0 && CondCode != NOT_SET) {
1825 // FIXME: check for min and max
Nate Begemanf63be7d2005-07-06 18:59:04 +00001826 } else {
Nate Begeman16b04f32005-07-15 00:38:55 +00001827 // FIXME: emit a direct compare and branch rather than setting a cond reg
1828 // and testing it.
Nate Begemanf63be7d2005-07-06 18:59:04 +00001829 unsigned CondReg = SelectExpr(Cond);
1830 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1831
1832 // Create an iterator with which to insert the MBB for copying the false
1833 // value and the MBB to hold the PHI instruction for this SetCC.
1834 MachineBasicBlock *thisMBB = BB;
1835 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1836 ilist<MachineBasicBlock>::iterator It = BB;
1837 ++It;
1838
1839 // thisMBB:
1840 // ...
1841 // TrueVal = ...
1842 // cmpTY ccX, r1, r2
1843 // bCC sinkMBB
1844 // fallthrough --> copy0MBB
1845 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1846 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1847 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1848 MachineFunction *F = BB->getParent();
1849 F->getBasicBlockList().insert(It, copy0MBB);
1850 F->getBasicBlockList().insert(It, sinkMBB);
1851 // Update machine-CFG edges
1852 BB->addSuccessor(copy0MBB);
1853 BB->addSuccessor(sinkMBB);
1854
1855 // copy0MBB:
1856 // %FalseValue = ...
1857 // # fallthrough to sinkMBB
1858 BB = copy0MBB;
1859 // Update machine-CFG edges
1860 BB->addSuccessor(sinkMBB);
1861
1862 // sinkMBB:
1863 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1864 // ...
1865 BB = sinkMBB;
1866 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1867 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1868 }
1869 return;
1870 }
1871
Chris Lattner24aad1b2005-01-10 22:10:13 +00001872 unsigned Opc = 0;
1873 if (CondCode != NOT_SET) {
1874 switch (SVT) {
1875 default: assert(0 && "Cannot select this type!");
1876 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1877 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001878 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001879 }
1880 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001881
Chris Lattner24aad1b2005-01-10 22:10:13 +00001882 // Finally, if we weren't able to fold this, just emit the condition and test
1883 // it.
1884 if (CondCode == NOT_SET || Opc == 0) {
1885 // Get the condition into the zero flag.
1886 unsigned CondReg = SelectExpr(Cond);
1887 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1888
1889 switch (SVT) {
1890 default: assert(0 && "Cannot select this type!");
1891 case MVT::i16: Opc = X86::CMOVE16rr; break;
1892 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001893 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001894 }
1895 } else {
1896 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001897 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001898 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001899 }
1900 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1901}
1902
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001903void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001904 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1906 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001907 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001908 switch (RHS.getValueType()) {
1909 default: break;
1910 case MVT::i1:
1911 case MVT::i8: Opc = X86::CMP8mi; break;
1912 case MVT::i16: Opc = X86::CMP16mi; break;
1913 case MVT::i32: Opc = X86::CMP32mi; break;
1914 }
1915 if (Opc) {
1916 X86AddressMode AM;
1917 EmitFoldedLoad(LHS, AM);
1918 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1919 return;
1920 }
1921 }
1922
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001923 switch (RHS.getValueType()) {
1924 default: break;
1925 case MVT::i1:
1926 case MVT::i8: Opc = X86::CMP8ri; break;
1927 case MVT::i16: Opc = X86::CMP16ri; break;
1928 case MVT::i32: Opc = X86::CMP32ri; break;
1929 }
1930 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001931 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001932 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1933 return;
1934 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001935 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001936 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1937 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00001938 unsigned Reg = SelectExpr(LHS);
1939 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1940 BuildMI(BB, X86::FNSTSW8r, 0);
1941 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001942 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001943 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001944 }
1945
Chris Lattneref6806c2005-01-12 02:02:48 +00001946 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001947 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001948 switch (RHS.getValueType()) {
1949 default: break;
1950 case MVT::i1:
1951 case MVT::i8: Opc = X86::CMP8mr; break;
1952 case MVT::i16: Opc = X86::CMP16mr; break;
1953 case MVT::i32: Opc = X86::CMP32mr; break;
1954 }
1955 if (Opc) {
1956 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001957 EmitFoldedLoad(LHS, AM);
1958 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001959 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1960 return;
1961 }
1962 }
1963
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001964 switch (LHS.getValueType()) {
1965 default: assert(0 && "Cannot compare this value!");
1966 case MVT::i1:
1967 case MVT::i8: Opc = X86::CMP8rr; break;
1968 case MVT::i16: Opc = X86::CMP16rr; break;
1969 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00001970 case MVT::f32: Opc = X86::UCOMISSrr; break;
1971 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001972 }
Chris Lattner11333092005-01-11 03:11:44 +00001973 unsigned Tmp1, Tmp2;
1974 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1975 Tmp1 = SelectExpr(LHS);
1976 Tmp2 = SelectExpr(RHS);
1977 } else {
1978 Tmp2 = SelectExpr(RHS);
1979 Tmp1 = SelectExpr(LHS);
1980 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1982}
1983
Chris Lattnera5ade062005-01-11 21:19:59 +00001984/// isFoldableLoad - Return true if this is a load instruction that can safely
1985/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001986bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1987 if (Op.getOpcode() == ISD::LOAD) {
1988 // FIXME: currently can't fold constant pool indexes.
1989 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1990 return false;
1991 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001992 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00001993 // FIXME: currently can't fold constant pool indexes.
1994 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1995 return false;
1996 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001997 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001998 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001999
2000 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00002001 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2002 if (ExprMap.count(Op.getValue(1))) return false;
2003 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00002004 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00002005
Chris Lattner4ff348b2005-01-17 06:26:58 +00002006 // If there is not just one use of its value, we cannot fold.
2007 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2008
2009 // Finally, we cannot fold the load into the operation if this would induce a
2010 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2011 // operand of the operation we are folding the load into) can possible use the
2012 // chain node defined by the load.
2013 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2014 std::set<SDNode*> Visited;
2015 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2016 return false;
2017 }
2018 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00002019}
2020
Chris Lattner4ff348b2005-01-17 06:26:58 +00002021
Chris Lattnera5ade062005-01-11 21:19:59 +00002022/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2023/// and compute the address being loaded into AM.
2024void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2025 SDOperand Chain = Op.getOperand(0);
2026 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002027
Chris Lattnera5ade062005-01-11 21:19:59 +00002028 if (getRegPressure(Chain) > getRegPressure(Address)) {
2029 Select(Chain);
2030 SelectAddress(Address, AM);
2031 } else {
2032 SelectAddress(Address, AM);
2033 Select(Chain);
2034 }
2035
2036 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002037 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2038 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002039 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002040 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002041}
2042
Chris Lattner30ea1e92005-01-19 07:37:26 +00002043// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2044// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2045// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2046// return true.
2047bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002048 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2049 // good!
2050 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2051 std::swap(Op1, Op2); // Op1 is the SHL now.
2052 } else {
2053 return false; // No match
2054 }
2055
2056 SDOperand ShlVal = Op1.getOperand(0);
2057 SDOperand ShlAmt = Op1.getOperand(1);
2058 SDOperand ShrVal = Op2.getOperand(0);
2059 SDOperand ShrAmt = Op2.getOperand(1);
2060
Chris Lattner30ea1e92005-01-19 07:37:26 +00002061 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2062
Chris Lattner85716372005-01-19 06:18:43 +00002063 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2064 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2065 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002066 if (SubCST->getValue() == RegSize) {
2067 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002068 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002069 if (ShrVal == ShlVal) {
2070 unsigned Reg, ShAmt;
2071 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2072 Reg = SelectExpr(ShrVal);
2073 ShAmt = SelectExpr(ShrAmt);
2074 } else {
2075 ShAmt = SelectExpr(ShrAmt);
2076 Reg = SelectExpr(ShrVal);
2077 }
2078 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2079 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2080 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2081 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2082 return true;
2083 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002084 unsigned AReg, BReg;
2085 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002086 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002087 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002088 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002089 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002090 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002091 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002092 unsigned ShAmt = SelectExpr(ShrAmt);
2093 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2094 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2095 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002096 return true;
2097 }
2098 }
2099
Chris Lattner4053b1e2005-01-19 08:07:05 +00002100 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2101 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2102 if (SubCST->getValue() == RegSize) {
2103 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2104 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2105 if (ShrVal == ShlVal) {
2106 unsigned Reg, ShAmt;
2107 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2108 Reg = SelectExpr(ShrVal);
2109 ShAmt = SelectExpr(ShlAmt);
2110 } else {
2111 ShAmt = SelectExpr(ShlAmt);
2112 Reg = SelectExpr(ShrVal);
2113 }
2114 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2115 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2116 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2117 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2118 return true;
2119 } else if (RegSize != 8) {
2120 unsigned AReg, BReg;
2121 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002122 AReg = SelectExpr(ShlVal);
2123 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002124 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002125 BReg = SelectExpr(ShrVal);
2126 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002127 }
2128 unsigned ShAmt = SelectExpr(ShlAmt);
2129 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2130 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2131 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2132 return true;
2133 }
2134 }
Chris Lattner85716372005-01-19 06:18:43 +00002135
Chris Lattner4053b1e2005-01-19 08:07:05 +00002136 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2137 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2138 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2139 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2140 // (A >> 5) | (A << 27) --> ROR A, 5
2141 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2142 if (ShrVal == ShlVal) {
2143 unsigned Reg = SelectExpr(ShrVal);
2144 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2145 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2146 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2147 return true;
2148 } else if (RegSize != 8) {
2149 unsigned AReg, BReg;
2150 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002151 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002152 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002153 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002154 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002155 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002156 }
2157 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2158 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2159 .addImm(ShrCst->getValue());
2160 return true;
2161 }
2162 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002163
Chris Lattner85716372005-01-19 06:18:43 +00002164 return false;
2165}
2166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002167unsigned ISel::SelectExpr(SDOperand N) {
2168 unsigned Result;
2169 unsigned Tmp1, Tmp2, Tmp3;
2170 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002171 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002172 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002173
Chris Lattner7f2afac2005-01-14 22:37:41 +00002174 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002175 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2176 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2177 // Just use the specified register as our input.
2178 return cast<RegSDNode>(Node)->getReg();
2179 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002180 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002181
Chris Lattnera5ade062005-01-11 21:19:59 +00002182 unsigned &Reg = ExprMap[N];
2183 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002184
Chris Lattnerb38a7492005-04-02 04:01:14 +00002185 switch (N.getOpcode()) {
2186 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002187 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002188 MakeReg(N.getValueType()) : 1;
2189 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002190 case X86ISD::TAILCALL:
2191 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002192 // If this is a call instruction, make sure to prepare ALL of the result
2193 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002194 ExprMap[N.getValue(0)] = 1;
2195 if (Node->getNumValues() > 1) {
2196 Result = MakeReg(Node->getValueType(1));
2197 ExprMap[N.getValue(1)] = Result;
2198 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002199 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002200 } else {
2201 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002202 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002203 break;
2204 case ISD::ADD_PARTS:
2205 case ISD::SUB_PARTS:
2206 case ISD::SHL_PARTS:
2207 case ISD::SRL_PARTS:
2208 case ISD::SRA_PARTS:
2209 Result = MakeReg(Node->getValueType(0));
2210 ExprMap[N.getValue(0)] = Result;
2211 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2212 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2213 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002214 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002215
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002216 switch (N.getOpcode()) {
2217 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002218 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002219 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002220 case ISD::FP_EXTEND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002221 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002222 Tmp1 = SelectExpr(N.getOperand(0));
2223 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2224 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002225 case ISD::FP_ROUND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002226 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman16b04f32005-07-15 00:38:55 +00002227 Tmp1 = SelectExpr(N.getOperand(0));
2228 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2229 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002230 case ISD::CopyFromReg:
2231 Select(N.getOperand(0));
2232 if (Result == 1) {
2233 Reg = Result = ExprMap[N.getValue(0)] =
2234 MakeReg(N.getValue(0).getValueType());
2235 }
2236 switch (Node->getValueType(0)) {
2237 default: assert(0 && "Cannot CopyFromReg this!");
2238 case MVT::i1:
2239 case MVT::i8:
2240 BuildMI(BB, X86::MOV8rr, 1,
2241 Result).addReg(cast<RegSDNode>(Node)->getReg());
2242 return Result;
2243 case MVT::i16:
2244 BuildMI(BB, X86::MOV16rr, 1,
2245 Result).addReg(cast<RegSDNode>(Node)->getReg());
2246 return Result;
2247 case MVT::i32:
2248 BuildMI(BB, X86::MOV32rr, 1,
2249 Result).addReg(cast<RegSDNode>(Node)->getReg());
2250 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002251 }
Chris Lattnerc6f41812005-05-12 23:06:28 +00002252
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002253 case ISD::FrameIndex:
2254 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2255 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2256 return Result;
2257 case ISD::ConstantPool:
2258 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2259 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2260 return Result;
2261 case ISD::ConstantFP:
2262 ContainsFPCode = true;
2263 Tmp1 = Result; // Intermediate Register
2264 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2265 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2266 Tmp1 = MakeReg(MVT::f64);
2267
2268 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2269 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2270 BuildMI(BB, X86::FLD0, 0, Tmp1);
2271 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2272 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2273 BuildMI(BB, X86::FLD1, 0, Tmp1);
2274 else
2275 assert(0 && "Unexpected constant!");
2276 if (Tmp1 != Result)
2277 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2278 return Result;
2279 case ISD::Constant:
2280 switch (N.getValueType()) {
2281 default: assert(0 && "Cannot use constants of this type!");
2282 case MVT::i1:
2283 case MVT::i8: Opc = X86::MOV8ri; break;
2284 case MVT::i16: Opc = X86::MOV16ri; break;
2285 case MVT::i32: Opc = X86::MOV32ri; break;
2286 }
2287 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2288 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002289 case ISD::UNDEF:
2290 if (Node->getValueType(0) == MVT::f64) {
2291 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2292 BuildMI(BB, X86::FLD0, 0, Result);
2293 } else {
2294 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2295 }
2296 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002297 case ISD::GlobalAddress: {
2298 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002299 // For Darwin, external and weak symbols are indirect, so we want to load
2300 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00002301 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00002302 (GV->hasWeakLinkage() || GV->isExternal())) {
2303 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2304 .addGlobalAddress(GV, false, 0);
2305 } else {
2306 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2307 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002308 return Result;
2309 }
2310 case ISD::ExternalSymbol: {
2311 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2312 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2313 return Result;
2314 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002315 case ISD::ZERO_EXTEND: {
2316 int DestIs16 = N.getValueType() == MVT::i16;
2317 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002318
2319 // FIXME: This hack is here for zero extension casts from bool to i8. This
2320 // would not be needed if bools were promoted by Legalize.
2321 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002322 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002323 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2324 return Result;
2325 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002326
Chris Lattner4ff348b2005-01-17 06:26:58 +00002327 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002328 static const unsigned Opc[3] = {
2329 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2330 };
2331
2332 X86AddressMode AM;
2333 EmitFoldedLoad(N.getOperand(0), AM);
2334 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002335
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002336 return Result;
2337 }
2338
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002339 static const unsigned Opc[3] = {
2340 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2341 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002342 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002343 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2344 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002345 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002346 case ISD::SIGN_EXTEND: {
2347 int DestIs16 = N.getValueType() == MVT::i16;
2348 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2349
Chris Lattner590d8002005-01-09 18:52:44 +00002350 // FIXME: Legalize should promote bools to i8!
2351 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2352 "Sign extend from bool not implemented!");
2353
Chris Lattner4ff348b2005-01-17 06:26:58 +00002354 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002355 static const unsigned Opc[3] = {
2356 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2357 };
2358
2359 X86AddressMode AM;
2360 EmitFoldedLoad(N.getOperand(0), AM);
2361 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2362 return Result;
2363 }
2364
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002365 static const unsigned Opc[3] = {
2366 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2367 };
2368 Tmp1 = SelectExpr(N.getOperand(0));
2369 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2370 return Result;
2371 }
2372 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002373 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002374 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002375 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002376 switch (N.getValueType()) {
2377 default: assert(0 && "Unknown truncate!");
2378 case MVT::i1:
2379 case MVT::i8: Opc = X86::MOV8rm; break;
2380 case MVT::i16: Opc = X86::MOV16rm; break;
2381 }
2382 X86AddressMode AM;
2383 EmitFoldedLoad(N.getOperand(0), AM);
2384 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2385 return Result;
2386 }
2387
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002388 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2389 // a move out of AX or AL.
2390 switch (N.getOperand(0).getValueType()) {
2391 default: assert(0 && "Unknown truncate!");
2392 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2393 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2394 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2395 }
2396 Tmp1 = SelectExpr(N.getOperand(0));
2397 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2398
2399 switch (N.getValueType()) {
2400 default: assert(0 && "Unknown truncate!");
2401 case MVT::i1:
2402 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2403 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2404 }
2405 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2406 return Result;
2407
Chris Lattnera28381c2005-07-16 00:28:20 +00002408 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002409 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2410 unsigned PromoteOpcode = 0;
2411
Nate Begeman5a8441e2005-07-16 02:02:34 +00002412 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002413 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002414 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002415 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2416 return Result;
2417 }
Jeff Cohen00b168892005-07-27 06:12:32 +00002418
Chris Lattneref7ba072005-01-11 03:50:45 +00002419 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002420
Chris Lattner590d8002005-01-09 18:52:44 +00002421 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002422 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002423 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2424 MachineFunction *F = BB->getParent();
2425 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2426
2427 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002428 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002429 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002430 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2431 break;
2432 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002433 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002434 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2435 break;
2436 default: break; // No promotion required.
2437 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002438 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002439 }
2440 case ISD::FP_TO_SINT:
2441 case ISD::FP_TO_UINT: {
2442 // FIXME: Most of this grunt work should be done by legalize!
2443 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2444
Nate Begemanf63be7d2005-07-06 18:59:04 +00002445 // If the target supports SSE2 and is performing FP operations in SSE regs
2446 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2447 // CVTSD2SI instructions.
2448 if (ISD::FP_TO_SINT == N.getOpcode() && X86ScalarSSE) {
2449 if (MVT::f32 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002450 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002451 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002452 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002453 } else {
2454 assert(0 && "Not an f32 or f64?");
2455 abort();
2456 }
2457 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002458 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00002459
Chris Lattner590d8002005-01-09 18:52:44 +00002460 // Change the floating point control register to use "round towards zero"
2461 // mode when truncating to an integer value.
2462 //
2463 MachineFunction *F = BB->getParent();
2464 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2465 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2466
2467 // Load the old value of the high byte of the control word...
2468 unsigned HighPartOfCW = MakeReg(MVT::i8);
2469 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2470 CWFrameIdx, 1);
2471
2472 // Set the high part to be round to zero...
2473 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2474 CWFrameIdx, 1).addImm(12);
2475
2476 // Reload the modified control word now...
2477 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002478
Chris Lattner590d8002005-01-09 18:52:44 +00002479 // Restore the memory image of control word to original value
2480 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2481 CWFrameIdx, 1).addReg(HighPartOfCW);
2482
2483 // We don't have the facilities for directly storing byte sized data to
2484 // memory. Promote it to 16 bits. We also must promote unsigned values to
2485 // larger classes because we only have signed FP stores.
2486 MVT::ValueType StoreClass = Node->getValueType(0);
2487 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2488 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00002489 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002490 case MVT::i8: StoreClass = MVT::i16; break;
2491 case MVT::i16: StoreClass = MVT::i32; break;
2492 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00002493 default: assert(0 && "Unknown store class!");
2494 }
2495
2496 // Spill the integer to memory and reload it from there.
2497 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2498 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2499
2500 switch (StoreClass) {
2501 default: assert(0 && "Unknown store class!");
2502 case MVT::i16:
2503 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2504 break;
2505 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00002506 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002507 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00002508 case MVT::i64:
2509 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner745d5382005-07-29 00:40:01 +00002510 break;
2511 }
Chris Lattner590d8002005-01-09 18:52:44 +00002512
2513 switch (Node->getValueType(0)) {
2514 default:
2515 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00002516 case MVT::i32:
2517 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2518 break;
2519 case MVT::i16:
2520 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2521 break;
2522 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00002523 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002524 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2525 break;
2526 }
2527
2528 // Reload the original control word now.
2529 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2530 return Result;
2531 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002532 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002533 Op0 = N.getOperand(0);
2534 Op1 = N.getOperand(1);
2535
Chris Lattner44129b52005-01-25 20:03:11 +00002536 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002537 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002538 goto FoldAdd;
2539 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002540
Chris Lattner44129b52005-01-25 20:03:11 +00002541 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002542 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002543 switch (N.getValueType()) {
2544 default: assert(0 && "Cannot add this type!");
2545 case MVT::i1:
2546 case MVT::i8: Opc = X86::ADD8rm; break;
2547 case MVT::i16: Opc = X86::ADD16rm; break;
2548 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002549 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002550 case MVT::f64:
2551 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002552 if (X86ScalarSSE) {
2553 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2554 Opc = X86::ADDSDrm;
2555 } else {
2556 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2557 }
Chris Lattner44129b52005-01-25 20:03:11 +00002558 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002559 }
2560 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002561 EmitFoldedLoad(Op1, AM);
2562 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002563 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2564 return Result;
2565 }
2566
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002567 // See if we can codegen this as an LEA to fold operations together.
2568 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002569 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002570 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002571 MatchAddress(N, AM);
2572 ExprMap[N] = Result;
2573
2574 // If this is not just an add, emit the LEA. For a simple add (like
2575 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2576 // leave this as LEA, then peephole it to 'ADD' after two address elim
2577 // happens.
2578 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2579 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2580 X86AddressMode XAM = SelectAddrExprs(AM);
2581 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2582 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002583 }
2584 }
Chris Lattner11333092005-01-11 03:11:44 +00002585
Chris Lattnera5ade062005-01-11 21:19:59 +00002586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002587 Opc = 0;
2588 if (CN->getValue() == 1) { // add X, 1 -> inc X
2589 switch (N.getValueType()) {
2590 default: assert(0 && "Cannot integer add this type!");
2591 case MVT::i8: Opc = X86::INC8r; break;
2592 case MVT::i16: Opc = X86::INC16r; break;
2593 case MVT::i32: Opc = X86::INC32r; break;
2594 }
2595 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2596 switch (N.getValueType()) {
2597 default: assert(0 && "Cannot integer add this type!");
2598 case MVT::i8: Opc = X86::DEC8r; break;
2599 case MVT::i16: Opc = X86::DEC16r; break;
2600 case MVT::i32: Opc = X86::DEC32r; break;
2601 }
2602 }
2603
2604 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002605 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002606 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2607 return Result;
2608 }
2609
2610 switch (N.getValueType()) {
2611 default: assert(0 && "Cannot add this type!");
2612 case MVT::i8: Opc = X86::ADD8ri; break;
2613 case MVT::i16: Opc = X86::ADD16ri; break;
2614 case MVT::i32: Opc = X86::ADD32ri; break;
2615 }
2616 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002617 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002618 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2619 return Result;
2620 }
2621 }
2622
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002623 switch (N.getValueType()) {
2624 default: assert(0 && "Cannot add this type!");
2625 case MVT::i8: Opc = X86::ADD8rr; break;
2626 case MVT::i16: Opc = X86::ADD16rr; break;
2627 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002628 case MVT::f32: Opc = X86::ADDSSrr; break;
2629 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002630 }
Chris Lattner11333092005-01-11 03:11:44 +00002631
Chris Lattnera5ade062005-01-11 21:19:59 +00002632 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2633 Tmp1 = SelectExpr(Op0);
2634 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002635 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002636 Tmp2 = SelectExpr(Op1);
2637 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002638 }
2639
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002640 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2641 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002642
Nate Begemanf63be7d2005-07-06 18:59:04 +00002643 case ISD::FSQRT:
2644 Tmp1 = SelectExpr(Node->getOperand(0));
2645 if (X86ScalarSSE) {
2646 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2647 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2648 } else {
2649 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2650 }
2651 return Result;
2652
2653 // FIXME:
2654 // Once we can spill 16 byte constants into the constant pool, we can
2655 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002656 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002657 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002658 case ISD::FSIN:
2659 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002660 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002661 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002662 switch (N.getOpcode()) {
2663 default: assert(0 && "Unreachable!");
2664 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2665 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002666 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2667 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002668 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002669 return Result;
2670
Chris Lattner8db0af12005-04-06 04:21:07 +00002671 case ISD::MULHU:
2672 switch (N.getValueType()) {
2673 default: assert(0 && "Unsupported VT!");
2674 case MVT::i8: Tmp2 = X86::MUL8r; break;
2675 case MVT::i16: Tmp2 = X86::MUL16r; break;
2676 case MVT::i32: Tmp2 = X86::MUL32r; break;
2677 }
2678 // FALL THROUGH
2679 case ISD::MULHS: {
2680 unsigned MovOpc, LowReg, HiReg;
2681 switch (N.getValueType()) {
2682 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002683 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002684 MovOpc = X86::MOV8rr;
2685 LowReg = X86::AL;
2686 HiReg = X86::AH;
2687 Opc = X86::IMUL8r;
2688 break;
2689 case MVT::i16:
2690 MovOpc = X86::MOV16rr;
2691 LowReg = X86::AX;
2692 HiReg = X86::DX;
2693 Opc = X86::IMUL16r;
2694 break;
2695 case MVT::i32:
2696 MovOpc = X86::MOV32rr;
2697 LowReg = X86::EAX;
2698 HiReg = X86::EDX;
2699 Opc = X86::IMUL32r;
2700 break;
2701 }
2702 if (Node->getOpcode() != ISD::MULHS)
2703 Opc = Tmp2; // Get the MULHU opcode.
2704
2705 Op0 = Node->getOperand(0);
2706 Op1 = Node->getOperand(1);
2707 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2708 Tmp1 = SelectExpr(Op0);
2709 Tmp2 = SelectExpr(Op1);
2710 } else {
2711 Tmp2 = SelectExpr(Op1);
2712 Tmp1 = SelectExpr(Op0);
2713 }
2714
2715 // FIXME: Implement folding of loads into the memory operands here!
2716 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2717 BuildMI(BB, Opc, 1).addReg(Tmp2);
2718 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2719 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002720 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002721
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002722 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002723 case ISD::MUL:
2724 case ISD::AND:
2725 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002726 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002727 static const unsigned SUBTab[] = {
2728 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2729 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2730 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2731 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002732 static const unsigned SSE_SUBTab[] = {
2733 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2734 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2735 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2736 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002737 static const unsigned MULTab[] = {
2738 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2739 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2740 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2741 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002742 static const unsigned SSE_MULTab[] = {
2743 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2744 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2745 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2746 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002747 static const unsigned ANDTab[] = {
2748 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2749 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002750 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002751 };
2752 static const unsigned ORTab[] = {
2753 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2754 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2755 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2756 };
2757 static const unsigned XORTab[] = {
2758 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2759 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2760 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2761 };
2762
2763 Op0 = Node->getOperand(0);
2764 Op1 = Node->getOperand(1);
2765
Chris Lattner30ea1e92005-01-19 07:37:26 +00002766 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2767 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002768 return Result;
2769
2770 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002771 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2772 if (CN->isNullValue()) { // 0 - N -> neg N
2773 switch (N.getValueType()) {
2774 default: assert(0 && "Cannot sub this type!");
2775 case MVT::i1:
2776 case MVT::i8: Opc = X86::NEG8r; break;
2777 case MVT::i16: Opc = X86::NEG16r; break;
2778 case MVT::i32: Opc = X86::NEG32r; break;
2779 }
2780 Tmp1 = SelectExpr(N.getOperand(1));
2781 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2782 return Result;
2783 }
2784
Chris Lattnera5ade062005-01-11 21:19:59 +00002785 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2786 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002787 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002788 switch (N.getValueType()) {
2789 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002790 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002791 case MVT::i8: Opc = X86::NOT8r; break;
2792 case MVT::i16: Opc = X86::NOT16r; break;
2793 case MVT::i32: Opc = X86::NOT32r; break;
2794 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002795 if (Opc) {
2796 Tmp1 = SelectExpr(Op0);
2797 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2798 return Result;
2799 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002800 }
2801
Chris Lattner2a4e5082005-01-17 06:48:02 +00002802 // Fold common multiplies into LEA instructions.
2803 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2804 switch ((int)CN->getValue()) {
2805 default: break;
2806 case 3:
2807 case 5:
2808 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002809 // Remove N from exprmap so SelectAddress doesn't get confused.
2810 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002811 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002812 SelectAddress(N, AM);
2813 // Restore it to the map.
2814 ExprMap[N] = Result;
2815 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2816 return Result;
2817 }
2818 }
2819
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002820 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002821 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002822 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002823 case MVT::i8: Opc = 0; break;
2824 case MVT::i16: Opc = 1; break;
2825 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002826 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002827 switch (Node->getOpcode()) {
2828 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002829 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2830 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002831 case ISD::AND: Opc = ANDTab[Opc]; break;
2832 case ISD::OR: Opc = ORTab[Opc]; break;
2833 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002834 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002835 if (Opc) { // Can't fold MUL:i8 R, imm
2836 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002837 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2838 return Result;
2839 }
2840 }
Chris Lattner11333092005-01-11 03:11:44 +00002841
Chris Lattner44129b52005-01-25 20:03:11 +00002842 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002843 if (Node->getOpcode() != ISD::SUB) {
2844 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002845 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002846 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002847 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002848 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002849 if (Op0.getOpcode() == ISD::EXTLOAD)
2850 Opc = X86::FSUBR32m;
2851 else
2852 Opc = X86::FSUBR64m;
2853
Chris Lattnera5ade062005-01-11 21:19:59 +00002854 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002855 EmitFoldedLoad(Op0, AM);
2856 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002857 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2858 return Result;
2859 }
2860 }
2861
Chris Lattner44129b52005-01-25 20:03:11 +00002862 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002863 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002864 switch (N.getValueType()) {
2865 default: assert(0 && "Cannot operate on this type!");
2866 case MVT::i1:
2867 case MVT::i8: Opc = 5; break;
2868 case MVT::i16: Opc = 6; break;
2869 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002870 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002871 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen00b168892005-07-27 06:12:32 +00002872 case MVT::f64:
2873 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begemanf63be7d2005-07-06 18:59:04 +00002874 "SSE load should have been promoted");
2875 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002876 }
2877 switch (Node->getOpcode()) {
2878 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002879 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2880 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002881 case ISD::AND: Opc = ANDTab[Opc]; break;
2882 case ISD::OR: Opc = ORTab[Opc]; break;
2883 case ISD::XOR: Opc = XORTab[Opc]; break;
2884 }
2885
2886 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002887 EmitFoldedLoad(Op1, AM);
2888 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002889 if (Opc) {
2890 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2891 } else {
2892 assert(Node->getOpcode() == ISD::MUL &&
2893 N.getValueType() == MVT::i8 && "Unexpected situation!");
2894 // Must use the MUL instruction, which forces use of AL.
2895 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2896 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2897 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2898 }
2899 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002900 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002901
2902 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2903 Tmp1 = SelectExpr(Op0);
2904 Tmp2 = SelectExpr(Op1);
2905 } else {
2906 Tmp2 = SelectExpr(Op1);
2907 Tmp1 = SelectExpr(Op0);
2908 }
2909
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002910 switch (N.getValueType()) {
2911 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002912 case MVT::i1:
2913 case MVT::i8: Opc = 10; break;
2914 case MVT::i16: Opc = 11; break;
2915 case MVT::i32: Opc = 12; break;
2916 case MVT::f32: Opc = 13; break;
2917 case MVT::f64: Opc = 14; break;
2918 }
2919 switch (Node->getOpcode()) {
2920 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002921 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2922 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002923 case ISD::AND: Opc = ANDTab[Opc]; break;
2924 case ISD::OR: Opc = ORTab[Opc]; break;
2925 case ISD::XOR: Opc = XORTab[Opc]; break;
2926 }
2927 if (Opc) {
2928 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2929 } else {
2930 assert(Node->getOpcode() == ISD::MUL &&
2931 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002932 // Must use the MUL instruction, which forces use of AL.
2933 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2934 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2935 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002936 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002937 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002938 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002939 case ISD::ADD_PARTS:
2940 case ISD::SUB_PARTS: {
2941 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2942 "Not an i64 add/sub!");
2943 // Emit all of the operands.
2944 std::vector<unsigned> InVals;
2945 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2946 InVals.push_back(SelectExpr(N.getOperand(i)));
2947 if (N.getOpcode() == ISD::ADD_PARTS) {
2948 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2949 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2950 } else {
2951 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2952 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2953 }
2954 return Result+N.ResNo;
2955 }
2956
Chris Lattnerb38a7492005-04-02 04:01:14 +00002957 case ISD::SHL_PARTS:
2958 case ISD::SRA_PARTS:
2959 case ISD::SRL_PARTS: {
2960 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2961 "Not an i64 shift!");
2962 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2963 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2964 unsigned TmpReg = MakeReg(MVT::i32);
2965 if (N.getOpcode() == ISD::SRA_PARTS) {
2966 // If this is a SHR of a Long, then we need to do funny sign extension
2967 // stuff. TmpReg gets the value to use as the high-part if we are
2968 // shifting more than 32 bits.
2969 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2970 } else {
2971 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2972 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2973 }
2974
2975 // Initialize CL with the shift amount.
2976 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2977 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2978
2979 unsigned TmpReg2 = MakeReg(MVT::i32);
2980 unsigned TmpReg3 = MakeReg(MVT::i32);
2981 if (N.getOpcode() == ISD::SHL_PARTS) {
2982 // TmpReg2 = shld inHi, inLo
2983 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2984 .addReg(ShiftOpLo);
2985 // TmpReg3 = shl inLo, CL
2986 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002987
Chris Lattnerb38a7492005-04-02 04:01:14 +00002988 // Set the flags to indicate whether the shift was by more than 32 bits.
2989 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002990
Chris Lattnerb38a7492005-04-02 04:01:14 +00002991 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002992 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002993 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2994 // DestLo = (>32) ? TmpReg : TmpReg3;
2995 BuildMI(BB, X86::CMOVNE32rr, 2,
2996 Result).addReg(TmpReg3).addReg(TmpReg);
2997 } else {
2998 // TmpReg2 = shrd inLo, inHi
2999 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
3000 .addReg(ShiftOpHi);
3001 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003002 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00003003 : X86::SHR32rCL, 1, TmpReg3)
3004 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003005
Chris Lattnerb38a7492005-04-02 04:01:14 +00003006 // Set the flags to indicate whether the shift was by more than 32 bits.
3007 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003008
Chris Lattnerb38a7492005-04-02 04:01:14 +00003009 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003010 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003011 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003012
Chris Lattnerb38a7492005-04-02 04:01:14 +00003013 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003014 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003015 Result+1).addReg(TmpReg3).addReg(TmpReg);
3016 }
3017 return Result+N.ResNo;
3018 }
3019
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003020 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00003021 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3022 Tmp2 = SelectExpr(N.getOperand(1));
3023 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003024 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003025 Tmp3 = SelectExpr(N.getOperand(2));
3026 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003027 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00003028 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
3029 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003030
3031 case ISD::SDIV:
3032 case ISD::UDIV:
3033 case ISD::SREM:
3034 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003035 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3036 "We don't support this operator!");
3037
Chris Lattner5bf26862005-04-13 03:29:53 +00003038 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003039 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003040 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003041 // Check for reversed and unreversed DIV.
3042 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3043 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3044 Opc = X86::FDIVR32m;
3045 else
3046 Opc = X86::FDIVR64m;
3047 X86AddressMode AM;
3048 EmitFoldedLoad(N.getOperand(0), AM);
3049 Tmp1 = SelectExpr(N.getOperand(1));
3050 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3051 return Result;
3052 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3053 N.getOperand(1).getOpcode() == ISD::LOAD) {
3054 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3055 Opc = X86::FDIV32m;
3056 else
3057 Opc = X86::FDIV64m;
3058 X86AddressMode AM;
3059 EmitFoldedLoad(N.getOperand(1), AM);
3060 Tmp1 = SelectExpr(N.getOperand(0));
3061 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3062 return Result;
3063 }
3064 }
3065
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003066 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3067 // FIXME: These special cases should be handled by the lowering impl!
3068 unsigned RHS = CN->getValue();
3069 bool isNeg = false;
3070 if ((int)RHS < 0) {
3071 isNeg = true;
3072 RHS = -RHS;
3073 }
3074 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
3075 unsigned Log = log2(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003076 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3077 switch (N.getValueType()) {
3078 default: assert("Unknown type to signed divide!");
3079 case MVT::i8:
3080 SAROpc = X86::SAR8ri;
3081 SHROpc = X86::SHR8ri;
3082 ADDOpc = X86::ADD8rr;
3083 NEGOpc = X86::NEG8r;
3084 break;
3085 case MVT::i16:
3086 SAROpc = X86::SAR16ri;
3087 SHROpc = X86::SHR16ri;
3088 ADDOpc = X86::ADD16rr;
3089 NEGOpc = X86::NEG16r;
3090 break;
3091 case MVT::i32:
3092 SAROpc = X86::SAR32ri;
3093 SHROpc = X86::SHR32ri;
3094 ADDOpc = X86::ADD32rr;
3095 NEGOpc = X86::NEG32r;
3096 break;
3097 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003098 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003099 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003100 unsigned TmpReg;
3101 if (Log != 1) {
3102 TmpReg = MakeReg(N.getValueType());
3103 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3104 } else {
3105 TmpReg = Tmp1;
3106 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003107 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003108 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003109 unsigned TmpReg3 = MakeReg(N.getValueType());
3110 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003111
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003112 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3113 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3114 if (isNeg)
3115 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3116 return Result;
3117 }
3118 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003119 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003120
Chris Lattner11333092005-01-11 03:11:44 +00003121 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3122 Tmp1 = SelectExpr(N.getOperand(0));
3123 Tmp2 = SelectExpr(N.getOperand(1));
3124 } else {
3125 Tmp2 = SelectExpr(N.getOperand(1));
3126 Tmp1 = SelectExpr(N.getOperand(0));
3127 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003128
3129 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3130 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3131 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3132 switch (N.getValueType()) {
3133 default: assert(0 && "Cannot sdiv this type!");
3134 case MVT::i8:
3135 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3136 LoReg = X86::AL;
3137 HiReg = X86::AH;
3138 MovOpcode = X86::MOV8rr;
3139 ClrOpcode = X86::MOV8ri;
3140 SExtOpcode = X86::CBW;
3141 break;
3142 case MVT::i16:
3143 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3144 LoReg = X86::AX;
3145 HiReg = X86::DX;
3146 MovOpcode = X86::MOV16rr;
3147 ClrOpcode = X86::MOV16ri;
3148 SExtOpcode = X86::CWD;
3149 break;
3150 case MVT::i32:
3151 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003152 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003153 HiReg = X86::EDX;
3154 MovOpcode = X86::MOV32rr;
3155 ClrOpcode = X86::MOV32ri;
3156 SExtOpcode = X86::CDQ;
3157 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003158 case MVT::f32:
3159 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3160 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003161 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003162 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3163 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003164 return Result;
3165 }
3166
3167 // Set up the low part.
3168 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3169
3170 if (isSigned) {
3171 // Sign extend the low part into the high part.
3172 BuildMI(BB, SExtOpcode, 0);
3173 } else {
3174 // Zero out the high part, effectively zero extending the input.
3175 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3176 }
3177
3178 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003179 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003180
3181 // Get the result of the divide or rem.
3182 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3183 return Result;
3184 }
3185
3186 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003187 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003188 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3189 switch (N.getValueType()) {
3190 default: assert(0 && "Cannot shift this type!");
3191 case MVT::i8: Opc = X86::ADD8rr; break;
3192 case MVT::i16: Opc = X86::ADD16rr; break;
3193 case MVT::i32: Opc = X86::ADD32rr; break;
3194 }
3195 Tmp1 = SelectExpr(N.getOperand(0));
3196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3197 return Result;
3198 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003199
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003200 switch (N.getValueType()) {
3201 default: assert(0 && "Cannot shift this type!");
3202 case MVT::i8: Opc = X86::SHL8ri; break;
3203 case MVT::i16: Opc = X86::SHL16ri; break;
3204 case MVT::i32: Opc = X86::SHL32ri; break;
3205 }
Chris Lattner11333092005-01-11 03:11:44 +00003206 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003207 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3208 return Result;
3209 }
Chris Lattner11333092005-01-11 03:11:44 +00003210
3211 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3212 Tmp1 = SelectExpr(N.getOperand(0));
3213 Tmp2 = SelectExpr(N.getOperand(1));
3214 } else {
3215 Tmp2 = SelectExpr(N.getOperand(1));
3216 Tmp1 = SelectExpr(N.getOperand(0));
3217 }
3218
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003219 switch (N.getValueType()) {
3220 default: assert(0 && "Cannot shift this type!");
3221 case MVT::i8 : Opc = X86::SHL8rCL; break;
3222 case MVT::i16: Opc = X86::SHL16rCL; break;
3223 case MVT::i32: Opc = X86::SHL32rCL; break;
3224 }
3225 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3226 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3227 return Result;
3228 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003229 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3230 switch (N.getValueType()) {
3231 default: assert(0 && "Cannot shift this type!");
3232 case MVT::i8: Opc = X86::SHR8ri; break;
3233 case MVT::i16: Opc = X86::SHR16ri; break;
3234 case MVT::i32: Opc = X86::SHR32ri; break;
3235 }
Chris Lattner11333092005-01-11 03:11:44 +00003236 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3238 return Result;
3239 }
Chris Lattner11333092005-01-11 03:11:44 +00003240
3241 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3242 Tmp1 = SelectExpr(N.getOperand(0));
3243 Tmp2 = SelectExpr(N.getOperand(1));
3244 } else {
3245 Tmp2 = SelectExpr(N.getOperand(1));
3246 Tmp1 = SelectExpr(N.getOperand(0));
3247 }
3248
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003249 switch (N.getValueType()) {
3250 default: assert(0 && "Cannot shift this type!");
3251 case MVT::i8 : Opc = X86::SHR8rCL; break;
3252 case MVT::i16: Opc = X86::SHR16rCL; break;
3253 case MVT::i32: Opc = X86::SHR32rCL; break;
3254 }
3255 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3256 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3257 return Result;
3258 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003259 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3260 switch (N.getValueType()) {
3261 default: assert(0 && "Cannot shift this type!");
3262 case MVT::i8: Opc = X86::SAR8ri; break;
3263 case MVT::i16: Opc = X86::SAR16ri; break;
3264 case MVT::i32: Opc = X86::SAR32ri; break;
3265 }
Chris Lattner11333092005-01-11 03:11:44 +00003266 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003267 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3268 return Result;
3269 }
Chris Lattner11333092005-01-11 03:11:44 +00003270
3271 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3272 Tmp1 = SelectExpr(N.getOperand(0));
3273 Tmp2 = SelectExpr(N.getOperand(1));
3274 } else {
3275 Tmp2 = SelectExpr(N.getOperand(1));
3276 Tmp1 = SelectExpr(N.getOperand(0));
3277 }
3278
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003279 switch (N.getValueType()) {
3280 default: assert(0 && "Cannot shift this type!");
3281 case MVT::i8 : Opc = X86::SAR8rCL; break;
3282 case MVT::i16: Opc = X86::SAR16rCL; break;
3283 case MVT::i32: Opc = X86::SAR32rCL; break;
3284 }
3285 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3286 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3287 return Result;
3288
3289 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003290 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003291 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3292 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3293 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003294 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003295 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003296 if (Result != 1) { // Generate the token
3297 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3298 assert(0 && "Load already emitted!?");
3299 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003300 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3301
Chris Lattner5188ad72005-01-08 19:28:19 +00003302 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003303 default: assert(0 && "Cannot load this type!");
3304 case MVT::i1:
3305 case MVT::i8: Opc = X86::MOV8rm; break;
3306 case MVT::i16: Opc = X86::MOV16rm; break;
3307 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003308 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003309 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003310 if (X86ScalarSSE) {
3311 Opc = X86::MOVSDrm;
3312 } else {
3313 Opc = X86::FLD64m;
Jeff Cohen00b168892005-07-27 06:12:32 +00003314 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003315 }
3316 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003317 }
Chris Lattner11333092005-01-11 03:11:44 +00003318
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003319 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003320 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003321 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3322 } else {
3323 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003324
3325 SDOperand Chain = N.getOperand(0);
3326 SDOperand Address = N.getOperand(1);
3327 if (getRegPressure(Chain) > getRegPressure(Address)) {
3328 Select(Chain);
3329 SelectAddress(Address, AM);
3330 } else {
3331 SelectAddress(Address, AM);
3332 Select(Chain);
3333 }
3334
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003335 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3336 }
3337 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003338 case X86ISD::FILD64m:
3339 // Make sure we generate both values.
3340 assert(Result != 1 && N.getValueType() == MVT::f64);
3341 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3342 assert(0 && "Load already emitted!?");
3343
3344 {
3345 X86AddressMode AM;
3346
3347 SDOperand Chain = N.getOperand(0);
3348 SDOperand Address = N.getOperand(1);
3349 if (getRegPressure(Chain) > getRegPressure(Address)) {
3350 Select(Chain);
3351 SelectAddress(Address, AM);
3352 } else {
3353 SelectAddress(Address, AM);
3354 Select(Chain);
3355 }
Chris Lattner745d5382005-07-29 00:40:01 +00003356
3357 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattner67649df2005-05-14 06:52:07 +00003358 }
3359 return Result;
Chris Lattner745d5382005-07-29 00:40:01 +00003360
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003361 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3362 case ISD::ZEXTLOAD: {
3363 // Make sure we generate both values.
3364 if (Result != 1)
3365 ExprMap[N.getValue(1)] = 1; // Generate the token
3366 else
3367 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3368
Chris Lattnerda2ce112005-01-16 07:34:08 +00003369 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3370 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003371 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003372 "Bad EXTLOAD!");
3373 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3374 CP->getIndex());
3375 return Result;
3376 }
3377
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003378 X86AddressMode AM;
3379 if (getRegPressure(Node->getOperand(0)) >
3380 getRegPressure(Node->getOperand(1))) {
3381 Select(Node->getOperand(0)); // chain
3382 SelectAddress(Node->getOperand(1), AM);
3383 } else {
3384 SelectAddress(Node->getOperand(1), AM);
3385 Select(Node->getOperand(0)); // chain
3386 }
3387
3388 switch (Node->getValueType(0)) {
3389 default: assert(0 && "Unknown type to sign extend to.");
3390 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003391 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003392 "Bad EXTLOAD!");
3393 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3394 break;
3395 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003396 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003397 default:
3398 assert(0 && "Bad zero extend!");
3399 case MVT::i1:
3400 case MVT::i8:
3401 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3402 break;
3403 case MVT::i16:
3404 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3405 break;
3406 }
3407 break;
3408 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003409 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003410 "Bad zero extend!");
3411 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3412 break;
3413 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003414 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003415 "Bad zero extend!");
3416 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3417 break;
3418 }
3419 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003420 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003421 case ISD::SEXTLOAD: {
3422 // Make sure we generate both values.
3423 if (Result != 1)
3424 ExprMap[N.getValue(1)] = 1; // Generate the token
3425 else
3426 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3427
3428 X86AddressMode AM;
3429 if (getRegPressure(Node->getOperand(0)) >
3430 getRegPressure(Node->getOperand(1))) {
3431 Select(Node->getOperand(0)); // chain
3432 SelectAddress(Node->getOperand(1), AM);
3433 } else {
3434 SelectAddress(Node->getOperand(1), AM);
3435 Select(Node->getOperand(0)); // chain
3436 }
3437
3438 switch (Node->getValueType(0)) {
3439 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3440 default: assert(0 && "Unknown type to sign extend to.");
3441 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003442 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003443 default:
3444 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3445 case MVT::i8:
3446 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3447 break;
3448 case MVT::i16:
3449 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3450 break;
3451 }
3452 break;
3453 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003454 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003455 "Cannot sign extend from bool!");
3456 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3457 break;
3458 }
3459 return Result;
3460 }
3461
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003462 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003463 // Generate both result values.
3464 if (Result != 1)
3465 ExprMap[N.getValue(1)] = 1; // Generate the token
3466 else
3467 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3468
3469 // FIXME: We are currently ignoring the requested alignment for handling
3470 // greater than the stack alignment. This will need to be revisited at some
3471 // point. Align = N.getOperand(2);
3472
3473 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3474 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3475 std::cerr << "Cannot allocate stack object with greater alignment than"
3476 << " the stack alignment yet!";
3477 abort();
3478 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003479
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003480 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003481 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003482 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3483 .addImm(CN->getValue());
3484 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003485 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3486 Select(N.getOperand(0));
3487 Tmp1 = SelectExpr(N.getOperand(1));
3488 } else {
3489 Tmp1 = SelectExpr(N.getOperand(1));
3490 Select(N.getOperand(0));
3491 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003492
3493 // Subtract size from stack pointer, thereby allocating some space.
3494 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3495 }
3496
3497 // Put a pointer to the space into the result register, by copying the stack
3498 // pointer.
3499 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3500 return Result;
3501
Chris Lattner239738a2005-05-14 08:48:15 +00003502 case X86ISD::TAILCALL:
3503 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003504 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003505 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003506
Chris Lattnerc6f41812005-05-12 23:06:28 +00003507 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3508 isa<ExternalSymbolSDNode>(N.getOperand(1));
3509 unsigned Callee = 0;
3510 if (isDirect) {
3511 Select(N.getOperand(0));
3512 } else {
3513 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3514 Select(N.getOperand(0));
3515 Callee = SelectExpr(N.getOperand(1));
3516 } else {
3517 Callee = SelectExpr(N.getOperand(1));
3518 Select(N.getOperand(0));
3519 }
3520 }
3521
3522 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003523 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003524 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003525 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003526 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003527 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen00b168892005-07-27 06:12:32 +00003528
Chris Lattner239738a2005-05-14 08:48:15 +00003529 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003530 default: assert(0 && "Bad thing to pass in regs");
3531 case MVT::i1:
3532 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3533 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3534 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3535 }
3536 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003537 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003538 default: assert(0 && "Bad thing to pass in regs");
3539 case MVT::i1:
3540 case MVT::i8:
3541 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3542 break;
3543 case MVT::i16:
3544 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3545 break;
3546 case MVT::i32:
3547 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3548 break;
3549 }
3550 }
3551
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003552 if (GlobalAddressSDNode *GASD =
3553 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3554 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3555 } else if (ExternalSymbolSDNode *ESSDN =
3556 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3557 BuildMI(BB, X86::CALLpcrel32,
3558 1).addExternalSymbol(ESSDN->getSymbol(), true);
3559 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003560 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3561 Select(N.getOperand(0));
3562 Tmp1 = SelectExpr(N.getOperand(1));
3563 } else {
3564 Tmp1 = SelectExpr(N.getOperand(1));
3565 Select(N.getOperand(0));
3566 }
3567
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003568 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3569 }
Chris Lattner239738a2005-05-14 08:48:15 +00003570
3571 // Get caller stack amount and amount the callee added to the stack pointer.
3572 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3573 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3574 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3575
3576 if (Node->getNumValues() != 1)
3577 switch (Node->getValueType(1)) {
3578 default: assert(0 && "Unknown value type for call result!");
3579 case MVT::Other: return 1;
3580 case MVT::i1:
3581 case MVT::i8:
3582 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3583 break;
3584 case MVT::i16:
3585 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3586 break;
3587 case MVT::i32:
3588 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3589 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3590 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3591 break;
3592 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003593 if (X86ScalarSSE) {
3594 ContainsFPCode = true;
3595 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3596
3597 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3598 MachineFunction *F = BB->getParent();
3599 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3600 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3601 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3602 break;
3603 } else {
3604 ContainsFPCode = true;
3605 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3606 break;
3607 }
Chris Lattner239738a2005-05-14 08:48:15 +00003608 }
3609 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003610 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003611 case ISD::READPORT:
3612 // First, determine that the size of the operand falls within the acceptable
3613 // range for this architecture.
3614 //
3615 if (Node->getOperand(1).getValueType() != MVT::i16) {
3616 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3617 exit(1);
3618 }
3619
3620 // Make sure we generate both values.
3621 if (Result != 1) { // Generate the token
3622 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3623 assert(0 && "readport already emitted!?");
3624 } else
3625 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen00b168892005-07-27 06:12:32 +00003626
Chris Lattner966cdfb2005-05-09 21:17:38 +00003627 Select(Node->getOperand(0)); // Select the chain.
3628
3629 // If the port is a single-byte constant, use the immediate form.
3630 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3631 if ((Port->getValue() & 255) == Port->getValue()) {
3632 switch (Node->getValueType(0)) {
3633 case MVT::i8:
3634 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3635 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3636 return Result;
3637 case MVT::i16:
3638 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3639 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3640 return Result;
3641 case MVT::i32:
3642 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3643 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3644 return Result;
3645 default: break;
3646 }
3647 }
3648
3649 // Now, move the I/O port address into the DX register and use the IN
3650 // instruction to get the input data.
3651 //
3652 Tmp1 = SelectExpr(Node->getOperand(1));
3653 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3654 switch (Node->getValueType(0)) {
3655 case MVT::i8:
3656 BuildMI(BB, X86::IN8rr, 0);
3657 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3658 return Result;
3659 case MVT::i16:
3660 BuildMI(BB, X86::IN16rr, 0);
3661 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3662 return Result;
3663 case MVT::i32:
3664 BuildMI(BB, X86::IN32rr, 0);
3665 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3666 return Result;
3667 default:
3668 std::cerr << "Cannot do input on this data type";
3669 exit(1);
3670 }
Jeff Cohen00b168892005-07-27 06:12:32 +00003671
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003672 }
3673
3674 return 0;
3675}
3676
Chris Lattnere10269b2005-01-17 19:25:26 +00003677/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3678/// load/op/store instruction. If successful return true.
3679bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3680 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3681 SDOperand Chain = Node->getOperand(0);
3682 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003683 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003684
3685 // The chain has to be a load, the stored value must be an integer binary
3686 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003687 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003688 MVT::isFloatingPoint(StVal.getValueType()))
3689 return false;
3690
Chris Lattner5c659812005-01-17 22:10:42 +00003691 // Token chain must either be a factor node or the load to fold.
3692 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3693 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003694
Chris Lattner5c659812005-01-17 22:10:42 +00003695 SDOperand TheLoad;
3696
3697 // Check to see if there is a load from the same pointer that we're storing
3698 // to in either operand of the binop.
3699 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3700 StVal.getOperand(0).getOperand(1) == StPtr)
3701 TheLoad = StVal.getOperand(0);
3702 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3703 StVal.getOperand(1).getOperand(1) == StPtr)
3704 TheLoad = StVal.getOperand(1);
3705 else
3706 return false; // No matching load operand.
3707
3708 // We can only fold the load if there are no intervening side-effecting
3709 // operations. This means that the store uses the load as its token chain, or
3710 // there are only token factor nodes in between the store and load.
3711 if (Chain != TheLoad.getValue(1)) {
3712 // Okay, the other option is that we have a store referring to (possibly
3713 // nested) token factor nodes. For now, just try peeking through one level
3714 // of token factors to see if this is the case.
3715 bool ChainOk = false;
3716 if (Chain.getOpcode() == ISD::TokenFactor) {
3717 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3718 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3719 ChainOk = true;
3720 break;
3721 }
3722 }
3723
3724 if (!ChainOk) return false;
3725 }
3726
3727 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003728 return false;
3729
3730 // Make sure that one of the operands of the binop is the load, and that the
3731 // load folds into the binop.
3732 if (((StVal.getOperand(0) != TheLoad ||
3733 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3734 (StVal.getOperand(1) != TheLoad ||
3735 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3736 return false;
3737
3738 // Finally, check to see if this is one of the ops we can handle!
3739 static const unsigned ADDTAB[] = {
3740 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3741 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3742 };
3743 static const unsigned SUBTAB[] = {
3744 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3745 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3746 };
3747 static const unsigned ANDTAB[] = {
3748 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3749 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3750 };
3751 static const unsigned ORTAB[] = {
3752 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3753 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3754 };
3755 static const unsigned XORTAB[] = {
3756 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3757 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3758 };
3759 static const unsigned SHLTAB[] = {
3760 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3761 /*Have to put the reg in CL*/0, 0, 0,
3762 };
3763 static const unsigned SARTAB[] = {
3764 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3765 /*Have to put the reg in CL*/0, 0, 0,
3766 };
3767 static const unsigned SHRTAB[] = {
3768 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3769 /*Have to put the reg in CL*/0, 0, 0,
3770 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003771
Chris Lattnere10269b2005-01-17 19:25:26 +00003772 const unsigned *TabPtr = 0;
3773 switch (StVal.getOpcode()) {
3774 default:
3775 std::cerr << "CANNOT [mem] op= val: ";
3776 StVal.Val->dump(); std::cerr << "\n";
3777 case ISD::MUL:
3778 case ISD::SDIV:
3779 case ISD::UDIV:
3780 case ISD::SREM:
3781 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003782
Chris Lattnere10269b2005-01-17 19:25:26 +00003783 case ISD::ADD: TabPtr = ADDTAB; break;
3784 case ISD::SUB: TabPtr = SUBTAB; break;
3785 case ISD::AND: TabPtr = ANDTAB; break;
3786 case ISD:: OR: TabPtr = ORTAB; break;
3787 case ISD::XOR: TabPtr = XORTAB; break;
3788 case ISD::SHL: TabPtr = SHLTAB; break;
3789 case ISD::SRA: TabPtr = SARTAB; break;
3790 case ISD::SRL: TabPtr = SHRTAB; break;
3791 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003792
Chris Lattnere10269b2005-01-17 19:25:26 +00003793 // Handle: [mem] op= CST
3794 SDOperand Op0 = StVal.getOperand(0);
3795 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003796 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003797 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3798 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3799 default: break;
3800 case MVT::i1:
3801 case MVT::i8: Opc = TabPtr[0]; break;
3802 case MVT::i16: Opc = TabPtr[1]; break;
3803 case MVT::i32: Opc = TabPtr[2]; break;
3804 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003805
Chris Lattnere10269b2005-01-17 19:25:26 +00003806 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003807 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3808 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003809 Select(Chain);
3810
Chris Lattnere10269b2005-01-17 19:25:26 +00003811 X86AddressMode AM;
3812 if (getRegPressure(TheLoad.getOperand(0)) >
3813 getRegPressure(TheLoad.getOperand(1))) {
3814 Select(TheLoad.getOperand(0));
3815 SelectAddress(TheLoad.getOperand(1), AM);
3816 } else {
3817 SelectAddress(TheLoad.getOperand(1), AM);
3818 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003819 }
Chris Lattner5c659812005-01-17 22:10:42 +00003820
3821 if (StVal.getOpcode() == ISD::ADD) {
3822 if (CN->getValue() == 1) {
3823 switch (Op0.getValueType()) {
3824 default: break;
3825 case MVT::i8:
3826 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3827 return true;
3828 case MVT::i16: Opc = TabPtr[1];
3829 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3830 return true;
3831 case MVT::i32: Opc = TabPtr[2];
3832 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3833 return true;
3834 }
3835 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3836 switch (Op0.getValueType()) {
3837 default: break;
3838 case MVT::i8:
3839 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3840 return true;
3841 case MVT::i16: Opc = TabPtr[1];
3842 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3843 return true;
3844 case MVT::i32: Opc = TabPtr[2];
3845 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3846 return true;
3847 }
3848 }
3849 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003850
Chris Lattnere10269b2005-01-17 19:25:26 +00003851 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3852 return true;
3853 }
3854 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003855
Chris Lattnere10269b2005-01-17 19:25:26 +00003856 // If we have [mem] = V op [mem], try to turn it into:
3857 // [mem] = [mem] op V.
3858 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3859 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3860 StVal.getOpcode() != ISD::SRL)
3861 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003862
Chris Lattnere10269b2005-01-17 19:25:26 +00003863 if (Op0 != TheLoad) return false;
3864
3865 switch (Op0.getValueType()) {
3866 default: return false;
3867 case MVT::i1:
3868 case MVT::i8: Opc = TabPtr[3]; break;
3869 case MVT::i16: Opc = TabPtr[4]; break;
3870 case MVT::i32: Opc = TabPtr[5]; break;
3871 }
Chris Lattner5c659812005-01-17 22:10:42 +00003872
Chris Lattnerb422aea2005-01-18 17:35:28 +00003873 // Table entry doesn't exist?
3874 if (Opc == 0) return false;
3875
Chris Lattner4a108662005-01-18 03:51:59 +00003876 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3877 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003878 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003879 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003880
Chris Lattnere10269b2005-01-17 19:25:26 +00003881 X86AddressMode AM;
3882 SelectAddress(TheLoad.getOperand(1), AM);
3883 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003884 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003885 return true;
3886}
3887
Chris Lattner381e8872005-05-15 05:46:45 +00003888/// If node is a ret(tailcall) node, emit the specified tail call and return
3889/// true, otherwise return false.
3890///
3891/// FIXME: This whole thing should be a post-legalize optimization pass which
3892/// recognizes and transforms the dag. We don't want the selection phase doing
3893/// this stuff!!
3894///
3895bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3896 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3897
3898 SDOperand Chain = RetNode->getOperand(0);
3899
3900 // If this is a token factor node where one operand is a call, dig into it.
3901 SDOperand TokFactor;
3902 unsigned TokFactorOperand = 0;
3903 if (Chain.getOpcode() == ISD::TokenFactor) {
3904 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3905 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3906 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3907 TokFactorOperand = i;
3908 TokFactor = Chain;
3909 Chain = Chain.getOperand(i);
3910 break;
3911 }
3912 if (TokFactor.Val == 0) return false; // No call operand.
3913 }
3914
3915 // Skip the CALLSEQ_END node if present.
3916 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3917 Chain = Chain.getOperand(0);
3918
3919 // Is a tailcall the last control operation that occurs before the return?
3920 if (Chain.getOpcode() != X86ISD::TAILCALL)
3921 return false;
3922
3923 // If we return a value, is it the value produced by the call?
3924 if (RetNode->getNumOperands() > 1) {
3925 // Not returning the ret val of the call?
3926 if (Chain.Val->getNumValues() == 1 ||
3927 RetNode->getOperand(1) != Chain.getValue(1))
3928 return false;
3929
3930 if (RetNode->getNumOperands() > 2) {
3931 if (Chain.Val->getNumValues() == 2 ||
3932 RetNode->getOperand(2) != Chain.getValue(2))
3933 return false;
3934 }
3935 assert(RetNode->getNumOperands() <= 3);
3936 }
3937
3938 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3939 // For FastCC, this will always be > 0.
3940 unsigned CalleeCallArgAmt =
3941 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3942
3943 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3944 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3945 unsigned CalleeCallArgPopAmt =
3946 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3947
3948 // There are several cases we can handle here. First, if the caller and
3949 // callee are both CCC functions, we can tailcall if the callee takes <= the
3950 // number of argument bytes that the caller does.
3951 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3952 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3953 // Check to see if caller arg area size >= callee arg area size.
3954 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3955 //std::cerr << "CCC TAILCALL UNIMP!\n";
3956 // If TokFactor is non-null, emit all operands.
3957
3958 //EmitCCCToCCCTailCall(Chain.Val);
3959 //return true;
3960 }
3961 return false;
3962 }
3963
3964 // Second, if both are FastCC functions, we can always perform the tail call.
3965 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3966 // If TokFactor is non-null, emit all operands before the call.
3967 if (TokFactor.Val) {
3968 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3969 if (i != TokFactorOperand)
3970 Select(TokFactor.getOperand(i));
3971 }
3972
3973 EmitFastCCToFastCCTailCall(Chain.Val);
3974 return true;
3975 }
3976
3977 // We don't support mixed calls, due to issues with alignment. We could in
3978 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3979 // aligned (which depends on the number of arguments to the callee). TODO.
3980 return false;
3981}
3982
3983static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3984 SelectionDAG &DAG) {
3985 MVT::ValueType StoreVT;
3986 switch (Chain.getOpcode()) {
3987 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003988 // If we found the start of the call sequence, we're done. We actually
3989 // strip off the CALLSEQ_START node, to avoid generating the
3990 // ADJCALLSTACKDOWN marker for the tail call.
3991 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003992 case ISD::TokenFactor: {
3993 std::vector<SDOperand> Ops;
3994 Ops.reserve(Chain.getNumOperands());
3995 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3996 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3997 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3998 }
3999 case ISD::STORE: // Normal store
4000 StoreVT = Chain.getOperand(1).getValueType();
4001 break;
4002 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004003 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00004004 break;
4005 }
4006
4007 SDOperand OrigDest = Chain.getOperand(2);
4008 unsigned OrigOffset;
4009
4010 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4011 OrigOffset = 0;
4012 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
4013 } else {
4014 // We expect only (ESP+C)
4015 assert(OrigDest.getOpcode() == ISD::ADD &&
4016 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4017 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
4018 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
4019 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4020 }
4021
4022 // Compute the new offset from the incoming ESP value we wish to use.
4023 unsigned NewOffset = OrigOffset + Offset;
4024
4025 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4026 MachineFunction &MF = DAG.getMachineFunction();
4027 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4028 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4029
4030 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4031 DAG);
4032 if (Chain.getOpcode() == ISD::STORE)
4033 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4034 FIN);
4035 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4036 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004037 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004038}
4039
4040
4041/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4042/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4043/// tail call.
4044void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4045 unsigned CalleeCallArgSize =
4046 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4047 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4048
4049 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4050
4051 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4052 // indexes that are relative to the incoming ESP. If the incoming and
4053 // outgoing arg sizes are the same we will store to [InESP] instead of
4054 // [CurESP] and the ESP referenced will be relative to the incoming function
4055 // ESP.
4056 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4057 SDOperand AdjustedArgStores =
4058 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4059
4060 // Copy the return address of the caller into a virtual register so we don't
4061 // clobber it.
4062 SDOperand RetVal;
4063 if (ESPOffset) {
4064 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4065 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4066 RetValAddr, TheDAG->getSrcValue(NULL));
4067 SelectExpr(RetVal);
4068 }
4069
4070 // Codegen all of the argument stores.
4071 Select(AdjustedArgStores);
4072
4073 if (RetVal.Val) {
4074 // Emit a store of the saved ret value to the new location.
4075 MachineFunction &MF = TheDAG->getMachineFunction();
4076 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4077 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4078 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4079 RetVal, RetValAddr));
4080 }
4081
4082 // Get the destination value.
4083 SDOperand Callee = TailCallNode->getOperand(1);
4084 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4085 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004086 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004087 if (!isDirect) CalleeReg = SelectExpr(Callee);
4088
4089 unsigned RegOp1 = 0;
4090 unsigned RegOp2 = 0;
4091
4092 if (TailCallNode->getNumOperands() > 4) {
4093 // The first value is passed in (a part of) EAX, the second in EDX.
4094 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4095 if (TailCallNode->getNumOperands() > 5)
4096 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen00b168892005-07-27 06:12:32 +00004097
Chris Lattner381e8872005-05-15 05:46:45 +00004098 switch (TailCallNode->getOperand(4).getValueType()) {
4099 default: assert(0 && "Bad thing to pass in regs");
4100 case MVT::i1:
4101 case MVT::i8:
4102 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4103 RegOp1 = X86::AL;
4104 break;
4105 case MVT::i16:
4106 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4107 RegOp1 = X86::AX;
4108 break;
4109 case MVT::i32:
4110 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4111 RegOp1 = X86::EAX;
4112 break;
4113 }
4114 if (RegOp2)
4115 switch (TailCallNode->getOperand(5).getValueType()) {
4116 default: assert(0 && "Bad thing to pass in regs");
4117 case MVT::i1:
4118 case MVT::i8:
4119 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4120 RegOp2 = X86::DL;
4121 break;
4122 case MVT::i16:
4123 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4124 RegOp2 = X86::DX;
4125 break;
4126 case MVT::i32:
4127 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4128 RegOp2 = X86::EDX;
4129 break;
4130 }
4131 }
4132
4133 // Adjust ESP.
4134 if (ESPOffset)
4135 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4136 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4137
4138 // TODO: handle jmp [mem]
4139 if (!isDirect) {
4140 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4141 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004142 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004143 } else {
4144 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4145 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4146 }
4147 // ADD IMPLICIT USE RegOp1/RegOp2's
4148}
4149
Chris Lattnere10269b2005-01-17 19:25:26 +00004150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004151void ISel::Select(SDOperand N) {
4152 unsigned Tmp1, Tmp2, Opc;
4153
Nate Begeman85fdeb22005-03-24 04:39:54 +00004154 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004155 return; // Already selected.
4156
Chris Lattner989de032005-01-11 06:14:36 +00004157 SDNode *Node = N.Val;
4158
4159 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004160 default:
Chris Lattner989de032005-01-11 06:14:36 +00004161 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004162 assert(0 && "Node not handled yet!");
4163 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004164 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004165 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004166 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004167 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4168 Select(Node->getOperand(OneFirst));
4169 Select(Node->getOperand(!OneFirst));
4170 } else {
4171 std::vector<std::pair<unsigned, unsigned> > OpsP;
4172 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4173 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4174 std::sort(OpsP.begin(), OpsP.end());
4175 std::reverse(OpsP.begin(), OpsP.end());
4176 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4177 Select(Node->getOperand(OpsP[i].second));
4178 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004179 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004180 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004181 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4182 Select(N.getOperand(0));
4183 Tmp1 = SelectExpr(N.getOperand(1));
4184 } else {
4185 Tmp1 = SelectExpr(N.getOperand(1));
4186 Select(N.getOperand(0));
4187 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004188 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004189
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004190 if (Tmp1 != Tmp2) {
4191 switch (N.getOperand(1).getValueType()) {
4192 default: assert(0 && "Invalid type for operation!");
4193 case MVT::i1:
4194 case MVT::i8: Opc = X86::MOV8rr; break;
4195 case MVT::i16: Opc = X86::MOV16rr; break;
4196 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004197 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00004198 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004199 if (X86ScalarSSE) {
4200 Opc = X86::MOVAPDrr;
4201 } else {
Jeff Cohen00b168892005-07-27 06:12:32 +00004202 Opc = X86::FpMOV;
4203 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004204 }
4205 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004206 }
4207 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4208 }
4209 return;
4210 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004211 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4212 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4213 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4214 if (EmitPotentialTailCall(Node))
4215 return;
4216
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004217 switch (N.getNumOperands()) {
4218 default:
4219 assert(0 && "Unknown return instruction!");
4220 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004221 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00004222 N.getOperand(2).getValueType() == MVT::i32 &&
4223 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004224 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4225 Tmp1 = SelectExpr(N.getOperand(1));
4226 Tmp2 = SelectExpr(N.getOperand(2));
4227 } else {
4228 Tmp2 = SelectExpr(N.getOperand(2));
4229 Tmp1 = SelectExpr(N.getOperand(1));
4230 }
4231 Select(N.getOperand(0));
4232
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004233 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4234 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004235 break;
4236 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004237 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4238 Select(N.getOperand(0));
4239 Tmp1 = SelectExpr(N.getOperand(1));
4240 } else {
4241 Tmp1 = SelectExpr(N.getOperand(1));
4242 Select(N.getOperand(0));
4243 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004244 switch (N.getOperand(1).getValueType()) {
4245 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004246 case MVT::f32:
4247 if (X86ScalarSSE) {
4248 // Spill the value to memory and reload it into top of stack.
4249 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4250 MachineFunction *F = BB->getParent();
4251 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4252 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4253 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4254 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004255 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004256 } else {
4257 assert(0 && "MVT::f32 only legal with scalar sse fp");
4258 abort();
4259 }
4260 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004261 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004262 if (X86ScalarSSE) {
4263 // Spill the value to memory and reload it into top of stack.
4264 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4265 MachineFunction *F = BB->getParent();
4266 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4267 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4268 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4269 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004270 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004271 } else {
4272 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4273 }
4274 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004275 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004276 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4277 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004278 }
4279 break;
4280 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004281 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004282 break;
4283 }
Chris Lattner3648c672005-05-13 21:44:04 +00004284 if (X86Lowering.getBytesToPopOnReturn() == 0)
4285 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4286 else
4287 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004288 return;
4289 case ISD::BR: {
4290 Select(N.getOperand(0));
4291 MachineBasicBlock *Dest =
4292 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4293 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4294 return;
4295 }
4296
4297 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004298 MachineBasicBlock *Dest =
4299 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004300
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004301 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4302 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004303 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4304 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4305 Select(N.getOperand(0));
4306 Tmp1 = SelectExpr(N.getOperand(1));
4307 } else {
4308 Tmp1 = SelectExpr(N.getOperand(1));
4309 Select(N.getOperand(0));
4310 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004311 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4312 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4313 }
Chris Lattner11333092005-01-11 03:11:44 +00004314
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004315 return;
4316 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004317
Chris Lattner4df0de92005-01-17 00:00:33 +00004318 case ISD::LOAD:
4319 // If this load could be folded into the only using instruction, and if it
4320 // is safe to emit the instruction here, try to do so now.
4321 if (Node->hasNUsesOfValue(1, 0)) {
4322 SDOperand TheVal = N.getValue(0);
4323 SDNode *User = 0;
4324 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4325 assert(UI != Node->use_end() && "Didn't find use!");
4326 SDNode *UN = *UI;
4327 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4328 if (UN->getOperand(i) == TheVal) {
4329 User = UN;
4330 goto FoundIt;
4331 }
4332 }
4333 FoundIt:
4334 // Only handle unary operators right now.
4335 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004336 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004337 SelectExpr(SDOperand(User, 0));
4338 return;
4339 }
4340 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004341 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004342 SelectExpr(N);
4343 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004344 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004345 case ISD::EXTLOAD:
4346 case ISD::SEXTLOAD:
4347 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004348 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004349 case X86ISD::TAILCALL:
4350 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004351 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004352 SelectExpr(N);
4353 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004354 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004355 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004356 ExprMap.erase(N);
4357 SelectExpr(N.getValue(0));
4358 return;
Chris Lattner745d5382005-07-29 00:40:01 +00004359
Chris Lattnerf7443da2005-07-29 00:54:34 +00004360 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner745d5382005-07-29 00:40:01 +00004361 assert(N.getOperand(1).getValueType() == MVT::f64);
4362 X86AddressMode AM;
4363 Select(N.getOperand(0)); // Select the token chain
4364
4365 unsigned ValReg;
4366 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4367 ValReg = SelectExpr(N.getOperand(1));
4368 SelectAddress(N.getOperand(2), AM);
4369 } else {
4370 SelectAddress(N.getOperand(2), AM);
4371 ValReg = SelectExpr(N.getOperand(1));
4372 }
Chris Lattnerf7443da2005-07-29 00:54:34 +00004373
4374 // Change the floating point control register to use "round towards zero"
4375 // mode when truncating to an integer value.
4376 //
4377 MachineFunction *F = BB->getParent();
4378 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4379 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
4380
4381 // Load the old value of the high byte of the control word...
4382 unsigned HighPartOfCW = MakeReg(MVT::i8);
4383 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
4384 CWFrameIdx, 1);
4385
4386 // Set the high part to be round to zero...
4387 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
4388 CWFrameIdx, 1).addImm(12);
4389
4390 // Reload the modified control word now...
4391 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
4392
4393 // Restore the memory image of control word to original value
4394 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
4395 CWFrameIdx, 1).addReg(HighPartOfCW);
4396
Chris Lattner745d5382005-07-29 00:40:01 +00004397 addFullAddress(BuildMI(BB, X86::FISTP64m, 5), AM).addReg(ValReg);
Chris Lattnerf7443da2005-07-29 00:54:34 +00004398
4399 // Reload the original control word now.
4400 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner745d5382005-07-29 00:40:01 +00004401 return;
4402 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004403
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004404 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004405 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004406 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004407 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4408 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4409 && "Unsupported TRUNCSTORE for this target!");
4410
4411 if (StoredTy == MVT::i16) {
4412 // FIXME: This is here just to allow testing. X86 doesn't really have a
4413 // TRUNCSTORE i16 operation, but this is required for targets that do not
4414 // have 16-bit integer registers. We occasionally disable 16-bit integer
4415 // registers to test the promotion code.
4416 Select(N.getOperand(0));
4417 Tmp1 = SelectExpr(N.getOperand(1));
4418 SelectAddress(N.getOperand(2), AM);
4419
4420 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4421 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4422 return;
4423 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004424
4425 // Store of constant bool?
4426 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4427 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4428 Select(N.getOperand(0));
4429 SelectAddress(N.getOperand(2), AM);
4430 } else {
4431 SelectAddress(N.getOperand(2), AM);
4432 Select(N.getOperand(0));
4433 }
4434 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4435 return;
4436 }
4437
4438 switch (StoredTy) {
4439 default: assert(0 && "Cannot truncstore this type!");
4440 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004441 case MVT::f32:
Jeff Cohen00b168892005-07-27 06:12:32 +00004442 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004443 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004444 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004445
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004446 std::vector<std::pair<unsigned, unsigned> > RP;
4447 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4448 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4449 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4450 std::sort(RP.begin(), RP.end());
4451
Chris Lattner572dd082005-02-23 05:57:21 +00004452 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004453 for (unsigned i = 0; i != 3; ++i)
4454 switch (RP[2-i].second) {
4455 default: assert(0 && "Unknown operand number!");
4456 case 0: Select(N.getOperand(0)); break;
4457 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4458 case 2: SelectAddress(N.getOperand(2), AM); break;
4459 }
4460
4461 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4462 return;
4463 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004464 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004465 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004466
4467 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4468 Opc = 0;
4469 switch (CN->getValueType(0)) {
4470 default: assert(0 && "Invalid type for operation!");
4471 case MVT::i1:
4472 case MVT::i8: Opc = X86::MOV8mi; break;
4473 case MVT::i16: Opc = X86::MOV16mi; break;
4474 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004475 }
4476 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004477 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4478 Select(N.getOperand(0));
4479 SelectAddress(N.getOperand(2), AM);
4480 } else {
4481 SelectAddress(N.getOperand(2), AM);
4482 Select(N.getOperand(0));
4483 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004484 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4485 return;
4486 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004487 } else if (GlobalAddressSDNode *GA =
4488 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4489 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4490
4491 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4492 Select(N.getOperand(0));
4493 SelectAddress(N.getOperand(2), AM);
4494 } else {
4495 SelectAddress(N.getOperand(2), AM);
4496 Select(N.getOperand(0));
4497 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004498 GlobalValue *GV = GA->getGlobal();
4499 // For Darwin, external and weak symbols are indirect, so we want to load
4500 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00004501 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begeman16b04f32005-07-15 00:38:55 +00004502 (GV->hasWeakLinkage() || GV->isExternal())) {
4503 Tmp1 = MakeReg(MVT::i32);
4504 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4505 .addGlobalAddress(GV, false, 0);
4506 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4507 } else {
4508 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4509 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004510 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004511 }
Chris Lattner837caa72005-01-11 23:21:30 +00004512
4513 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004514 if (TryToFoldLoadOpStore(Node))
4515 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004516
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004517 switch (N.getOperand(1).getValueType()) {
4518 default: assert(0 && "Cannot store this type!");
4519 case MVT::i1:
4520 case MVT::i8: Opc = X86::MOV8mr; break;
4521 case MVT::i16: Opc = X86::MOV16mr; break;
4522 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004523 case MVT::f32: Opc = X86::MOVSSmr; break;
4524 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004525 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004526
Chris Lattner11333092005-01-11 03:11:44 +00004527 std::vector<std::pair<unsigned, unsigned> > RP;
4528 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4529 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4530 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4531 std::sort(RP.begin(), RP.end());
4532
Chris Lattner572dd082005-02-23 05:57:21 +00004533 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004534 for (unsigned i = 0; i != 3; ++i)
4535 switch (RP[2-i].second) {
4536 default: assert(0 && "Unknown operand number!");
4537 case 0: Select(N.getOperand(0)); break;
4538 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004539 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004540 }
4541
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004542 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4543 return;
4544 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004545 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004546 Select(N.getOperand(0));
4547 // Stack amount
4548 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4549 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4550 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004551 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004552 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004553 return;
Chris Lattner989de032005-01-11 06:14:36 +00004554 case ISD::MEMSET: {
4555 Select(N.getOperand(0)); // Select the chain.
4556 unsigned Align =
4557 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4558 if (Align == 0) Align = 1;
4559
4560 // Turn the byte code into # iterations
4561 unsigned CountReg;
4562 unsigned Opcode;
4563 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4564 unsigned Val = ValC->getValue() & 255;
4565
4566 // If the value is a constant, then we can potentially use larger sets.
4567 switch (Align & 3) {
4568 case 2: // WORD aligned
4569 CountReg = MakeReg(MVT::i32);
4570 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4571 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4572 } else {
4573 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4574 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4575 }
4576 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4577 Opcode = X86::REP_STOSW;
4578 break;
4579 case 0: // DWORD aligned
4580 CountReg = MakeReg(MVT::i32);
4581 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4582 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4583 } else {
4584 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4585 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4586 }
4587 Val = (Val << 8) | Val;
4588 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4589 Opcode = X86::REP_STOSD;
4590 break;
4591 default: // BYTE aligned
4592 CountReg = SelectExpr(Node->getOperand(3));
4593 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4594 Opcode = X86::REP_STOSB;
4595 break;
4596 }
4597 } else {
4598 // If it's not a constant value we are storing, just fall back. We could
4599 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4600 unsigned ValReg = SelectExpr(Node->getOperand(2));
4601 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4602 CountReg = SelectExpr(Node->getOperand(3));
4603 Opcode = X86::REP_STOSB;
4604 }
4605
4606 // No matter what the alignment is, we put the source in ESI, the
4607 // destination in EDI, and the count in ECX.
4608 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4609 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4610 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4611 BuildMI(BB, Opcode, 0);
4612 return;
4613 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004614 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004615 Select(N.getOperand(0)); // Select the chain.
4616 unsigned Align =
4617 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4618 if (Align == 0) Align = 1;
4619
4620 // Turn the byte code into # iterations
4621 unsigned CountReg;
4622 unsigned Opcode;
4623 switch (Align & 3) {
4624 case 2: // WORD aligned
4625 CountReg = MakeReg(MVT::i32);
4626 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4627 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4628 } else {
4629 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4630 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4631 }
4632 Opcode = X86::REP_MOVSW;
4633 break;
4634 case 0: // DWORD aligned
4635 CountReg = MakeReg(MVT::i32);
4636 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4637 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4638 } else {
4639 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4640 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4641 }
4642 Opcode = X86::REP_MOVSD;
4643 break;
4644 default: // BYTE aligned
4645 CountReg = SelectExpr(Node->getOperand(3));
4646 Opcode = X86::REP_MOVSB;
4647 break;
4648 }
4649
4650 // No matter what the alignment is, we put the source in ESI, the
4651 // destination in EDI, and the count in ECX.
4652 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4653 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4654 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4655 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4656 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4657 BuildMI(BB, Opcode, 0);
4658 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004659 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004660 case ISD::WRITEPORT:
4661 if (Node->getOperand(2).getValueType() != MVT::i16) {
4662 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4663 exit(1);
4664 }
4665 Select(Node->getOperand(0)); // Emit the chain.
4666
4667 Tmp1 = SelectExpr(Node->getOperand(1));
4668 switch (Node->getOperand(1).getValueType()) {
4669 case MVT::i8:
4670 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4671 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4672 break;
4673 case MVT::i16:
4674 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4675 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4676 break;
4677 case MVT::i32:
4678 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4679 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4680 break;
4681 default:
4682 std::cerr << "llvm.writeport: invalid data type for X86 target";
4683 exit(1);
4684 }
4685
4686 // If the port is a single-byte constant, use the immediate form.
4687 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4688 if ((CN->getValue() & 255) == CN->getValue()) {
4689 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4690 return;
4691 }
4692
4693 // Otherwise, move the I/O port address into the DX register.
4694 unsigned Reg = SelectExpr(Node->getOperand(2));
4695 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4696 BuildMI(BB, Opc, 0);
4697 return;
4698 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004699 assert(0 && "Should not be reached!");
4700}
4701
4702
4703/// createX86PatternInstructionSelector - This pass converts an LLVM function
4704/// into a machine code representation using pattern matching and a machine
4705/// description file.
4706///
4707FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004708 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004709}