blob: 36d34ce0ef7dbf46821142eeb6345be734df888b [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).
Chris Lattner01546c52005-07-30 00:05:54 +000062 FP_TO_INT16_IN_MEM,
63 FP_TO_INT32_IN_MEM,
Chris Lattnerf7443da2005-07-29 00:54:34 +000064 FP_TO_INT64_IN_MEM,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000065
Chris Lattner239738a2005-05-14 08:48:15 +000066 /// CALL/TAILCALL - These operations represent an abstract X86 call
67 /// instruction, which includes a bunch of information. In particular the
68 /// operands of these node are:
69 ///
70 /// #0 - The incoming token chain
71 /// #1 - The callee
72 /// #2 - The number of arg bytes the caller pushes on the stack.
73 /// #3 - The number of arg bytes the callee pops off the stack.
74 /// #4 - The value to pass in AL/AX/EAX (optional)
75 /// #5 - The value to pass in DL/DX/EDX (optional)
76 ///
77 /// The result values of these nodes are:
78 ///
79 /// #0 - The outgoing token chain
80 /// #1 - The first register result value (optional)
81 /// #2 - The second register result value (optional)
82 ///
83 /// The CALL vs TAILCALL distinction boils down to whether the callee is
84 /// known not to modify the caller's stack frame, as is standard with
85 /// LLVM.
86 CALL,
87 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000088 };
89 }
90}
91
Chris Lattner8acb1ba2005-01-07 07:49:41 +000092//===----------------------------------------------------------------------===//
93// X86TargetLowering - X86 Implementation of the TargetLowering interface
94namespace {
95 class X86TargetLowering : public TargetLowering {
96 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000097 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner381e8872005-05-15 05:46:45 +000098 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
99 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 public:
101 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
102 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +0000103
Chris Lattner653f7232005-05-13 22:46:57 +0000104 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +0000105 setShiftAmountType(MVT::i8);
106 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +0000107 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +0000108 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +0000109
110 // Set up the register classes.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000111 // FIXME: Eliminate these two classes when legalize can handle promotions
112 // well.
113 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000114 addRegisterClass(MVT::i8, X86::R8RegisterClass);
115 addRegisterClass(MVT::i16, X86::R16RegisterClass);
116 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000117
Chris Lattnera28381c2005-07-16 00:28:20 +0000118 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
119 // operation.
120 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
121 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
122 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000124
125 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
126 // this operation.
127 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Chris Lattner745d5382005-07-29 00:40:01 +0000130 if (!X86ScalarSSE) {
131 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
132 // isn't legal.
Chris Lattner01546c52005-07-30 00:05:54 +0000133 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Chris Lattner745d5382005-07-29 00:40:01 +0000137 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000138
Chris Lattner5d06b8c2005-07-29 01:00:29 +0000139 // Handle FP_TO_UINT by promoting the destination to a larger signed
140 // conversion.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
142 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
143 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
145
146 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
147 // this operation.
148 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000150
Chris Lattnerda4d4692005-04-09 03:22:37 +0000151 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000152 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
153 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000155 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
156 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
157 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +0000158 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
159 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
160 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
161 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
162 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
163 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000164 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
165 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000166 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000167
Chris Lattner4e6ce5f2005-05-09 20:37:29 +0000168 setOperationAction(ISD::READIO , MVT::i1 , Expand);
169 setOperationAction(ISD::READIO , MVT::i8 , Expand);
170 setOperationAction(ISD::READIO , MVT::i16 , Expand);
171 setOperationAction(ISD::READIO , MVT::i32 , Expand);
172 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
173 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
174 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
175 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattnerda2ce112005-01-16 07:34:08 +0000177 // These should be promoted to a larger select which is supported.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000178 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000179 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000180
Nate Begemanf63be7d2005-07-06 18:59:04 +0000181 if (X86ScalarSSE) {
182 // Set up the FP register classes.
183 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
184 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000185
Nate Begeman5a8441e2005-07-16 02:02:34 +0000186 // SSE has no load+extend ops
Nate Begemanf63be7d2005-07-06 18:59:04 +0000187 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
188 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000189
190 // SSE has no i16 to fp conversion, only i32
191 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Nate Begeman1c73c7b2005-08-03 23:26:28 +0000192 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000193
Nate Begemanf63be7d2005-07-06 18:59:04 +0000194 // We don't support sin/cos/sqrt/fmod
195 setOperationAction(ISD::FSIN , MVT::f64, Expand);
196 setOperationAction(ISD::FCOS , MVT::f64, Expand);
197 setOperationAction(ISD::FABS , MVT::f64, Expand);
198 setOperationAction(ISD::FNEG , MVT::f64, Expand);
199 setOperationAction(ISD::SREM , MVT::f64, Expand);
200 setOperationAction(ISD::FSIN , MVT::f32, Expand);
201 setOperationAction(ISD::FCOS , MVT::f32, Expand);
202 setOperationAction(ISD::FABS , MVT::f32, Expand);
203 setOperationAction(ISD::FNEG , MVT::f32, Expand);
204 setOperationAction(ISD::SREM , MVT::f32, Expand);
Nate Begeman1c73c7b2005-08-03 23:26:28 +0000205
206 addLegalFPImmediate(+0.0); // xorps / xorpd
Nate Begemanf63be7d2005-07-06 18:59:04 +0000207 } else {
208 // Set up the FP register classes.
209 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000210
Nate Begemanf63be7d2005-07-06 18:59:04 +0000211 if (!UnsafeFPMath) {
212 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
213 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
214 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000215
Nate Begemanf63be7d2005-07-06 18:59:04 +0000216 addLegalFPImmediate(+0.0); // FLD0
217 addLegalFPImmediate(+1.0); // FLD1
218 addLegalFPImmediate(-0.0); // FLD0/FCHS
219 addLegalFPImmediate(-1.0); // FLD1/FCHS
220 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000221 computeRegisterProperties();
Reid Spencera0f5bf32005-07-19 04:52:44 +0000222
223 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
224 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
225 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
226 allowUnalignedStores = true; // x86 supports it!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000227 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Chris Lattner3648c672005-05-13 21:44:04 +0000229 // Return the number of bytes that a function should pop when it returns (in
230 // addition to the space used by the return address).
231 //
232 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
233
Chris Lattner381e8872005-05-15 05:46:45 +0000234 // Return the number of bytes that the caller reserves for arguments passed
235 // to this function.
236 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
237
Chris Lattner67649df2005-05-14 06:52:07 +0000238 /// LowerOperation - Provide custom lowering hooks for some operations.
239 ///
240 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
241
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000242 /// LowerArguments - This hook must be implemented to indicate how we should
243 /// lower the arguments for the specified function, into the specified DAG.
244 virtual std::vector<SDOperand>
245 LowerArguments(Function &F, SelectionDAG &DAG);
246
247 /// LowerCallTo - This hook lowers an abstract call to a function into an
248 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000249 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen00b168892005-07-27 06:12:32 +0000250 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000251 bool isTailCall, SDOperand Callee, ArgListTy &Args,
252 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000253
Chris Lattnere0fe2252005-07-05 19:58:54 +0000254 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
255 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000256 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000257 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
258 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000259
Chris Lattner14824582005-01-09 00:01:27 +0000260 virtual std::pair<SDOperand, SDOperand>
261 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
262 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000263
264 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
265
Chris Lattnerc6f41812005-05-12 23:06:28 +0000266 private:
267 // C Calling Convention implementation.
268 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
269 std::pair<SDOperand, SDOperand>
270 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000271 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000272 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000273
Chris Lattnerc6f41812005-05-12 23:06:28 +0000274 // Fast Calling Convention implementation.
275 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
276 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000277 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000278 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000279 };
280}
281
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000282std::vector<SDOperand>
283X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000284 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
285 return LowerFastCCArguments(F, DAG);
286 return LowerCCCArguments(F, DAG);
287}
288
289std::pair<SDOperand, SDOperand>
290X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
291 bool isVarArg, unsigned CallingConv,
Jeff Cohen00b168892005-07-27 06:12:32 +0000292 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000293 SDOperand Callee, ArgListTy &Args,
294 SelectionDAG &DAG) {
295 assert((!isVarArg || CallingConv == CallingConv::C) &&
296 "Only C takes varargs!");
297 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000298 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
299 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000300}
301
302//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000303// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000304//===----------------------------------------------------------------------===//
305
306std::vector<SDOperand>
307X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000308 std::vector<SDOperand> ArgValues;
309
Chris Lattner6415bb42005-05-10 03:53:18 +0000310 MachineFunction &MF = DAG.getMachineFunction();
311 MachineFrameInfo *MFI = MF.getFrameInfo();
312
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000313 // Add DAG nodes to load the arguments... On entry to a function on the X86,
314 // the stack frame looks like this:
315 //
316 // [ESP] -- return address
317 // [ESP + 4] -- first argument (leftmost lexically)
318 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000319 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000320 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000321 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000322 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000323 MVT::ValueType ObjectVT = getValueType(I->getType());
324 unsigned ArgIncrement = 4;
325 unsigned ObjSize;
326 switch (ObjectVT) {
327 default: assert(0 && "Unhandled argument type!");
328 case MVT::i1:
329 case MVT::i8: ObjSize = 1; break;
330 case MVT::i16: ObjSize = 2; break;
331 case MVT::i32: ObjSize = 4; break;
332 case MVT::i64: ObjSize = ArgIncrement = 8; break;
333 case MVT::f32: ObjSize = 4; break;
334 case MVT::f64: ObjSize = ArgIncrement = 8; break;
335 }
336 // Create the frame index object for this incoming parameter...
337 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000338
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000339 // Create the SelectionDAG nodes corresponding to a load from this parameter
340 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
341
342 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
343 // dead loads.
344 SDOperand ArgValue;
345 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000346 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
347 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000348 else {
349 if (MVT::isInteger(ObjectVT))
350 ArgValue = DAG.getConstant(0, ObjectVT);
351 else
352 ArgValue = DAG.getConstantFP(0, ObjectVT);
353 }
354 ArgValues.push_back(ArgValue);
355
356 ArgOffset += ArgIncrement; // Move on to the next argument...
357 }
358
359 // If the function takes variable number of arguments, make a frame index for
360 // the start of the first vararg value... for expansion of llvm.va_start.
361 if (F.isVarArg())
362 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000363 ReturnAddrIndex = 0; // No return address slot generated yet.
364 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000365 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000366
367 // Finally, inform the code generator which regs we return values in.
368 switch (getValueType(F.getReturnType())) {
369 default: assert(0 && "Unknown type!");
370 case MVT::isVoid: break;
371 case MVT::i1:
372 case MVT::i8:
373 case MVT::i16:
374 case MVT::i32:
375 MF.addLiveOut(X86::EAX);
376 break;
377 case MVT::i64:
378 MF.addLiveOut(X86::EAX);
379 MF.addLiveOut(X86::EDX);
380 break;
381 case MVT::f32:
382 case MVT::f64:
383 MF.addLiveOut(X86::ST0);
384 break;
385 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000386 return ArgValues;
387}
388
Chris Lattner5188ad72005-01-08 19:28:19 +0000389std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000390X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000391 bool isVarArg, bool isTailCall,
392 SDOperand Callee, ArgListTy &Args,
393 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000394 // Count how many bytes are to be pushed on the stack.
395 unsigned NumBytes = 0;
396
397 if (Args.empty()) {
398 // Save zero bytes.
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(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000401 } else {
402 for (unsigned i = 0, e = Args.size(); i != e; ++i)
403 switch (getValueType(Args[i].second)) {
404 default: assert(0 && "Unknown value type!");
405 case MVT::i1:
406 case MVT::i8:
407 case MVT::i16:
408 case MVT::i32:
409 case MVT::f32:
410 NumBytes += 4;
411 break;
412 case MVT::i64:
413 case MVT::f64:
414 NumBytes += 8;
415 break;
416 }
417
Chris Lattner16cd04d2005-05-12 23:24:06 +0000418 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000419 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000420
421 // Arguments go on the stack in reverse order, as specified by the ABI.
422 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000423 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
424 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000425 std::vector<SDOperand> Stores;
426
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000427 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000428 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
429 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
430
431 switch (getValueType(Args[i].second)) {
432 default: assert(0 && "Unexpected ValueType for argument!");
433 case MVT::i1:
434 case MVT::i8:
435 case MVT::i16:
436 // Promote the integer to 32 bits. If the input type is signed use a
437 // sign extend, otherwise use a zero extend.
438 if (Args[i].second->isSigned())
439 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
440 else
441 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
442
443 // FALL THROUGH
444 case MVT::i32:
445 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000446 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000447 Args[i].first, PtrOff,
448 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000449 ArgOffset += 4;
450 break;
451 case MVT::i64:
452 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000453 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000454 Args[i].first, PtrOff,
455 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000456 ArgOffset += 8;
457 break;
458 }
459 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000460 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000461 }
462
463 std::vector<MVT::ValueType> RetVals;
464 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000465 RetVals.push_back(MVT::Other);
466
Chris Lattner239738a2005-05-14 08:48:15 +0000467 // The result values produced have to be legal. Promote the result.
468 switch (RetTyVT) {
469 case MVT::isVoid: break;
470 default:
471 RetVals.push_back(RetTyVT);
472 break;
473 case MVT::i1:
474 case MVT::i8:
475 case MVT::i16:
476 RetVals.push_back(MVT::i32);
477 break;
478 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000479 if (X86ScalarSSE)
480 RetVals.push_back(MVT::f32);
481 else
482 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000483 break;
484 case MVT::i64:
485 RetVals.push_back(MVT::i32);
486 RetVals.push_back(MVT::i32);
487 break;
488 }
489 std::vector<SDOperand> Ops;
490 Ops.push_back(Chain);
491 Ops.push_back(Callee);
492 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
493 Ops.push_back(DAG.getConstant(0, getPointerTy()));
494 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
495 RetVals, Ops);
496 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
497
498 SDOperand ResultVal;
499 switch (RetTyVT) {
500 case MVT::isVoid: break;
501 default:
502 ResultVal = TheCall.getValue(1);
503 break;
504 case MVT::i1:
505 case MVT::i8:
506 case MVT::i16:
507 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
508 break;
509 case MVT::f32:
510 // FIXME: we would really like to remember that this FP_ROUND operation is
511 // okay to eliminate if we allow excess FP precision.
512 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
513 break;
514 case MVT::i64:
515 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
516 TheCall.getValue(2));
517 break;
518 }
519
520 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000521}
522
Chris Lattnere0fe2252005-07-05 19:58:54 +0000523SDOperand
524X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
525 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000526 // vastart just stores the address of the VarArgsFrameIndex slot.
527 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000528 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
529 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000530}
531
Chris Lattnere0fe2252005-07-05 19:58:54 +0000532
533std::pair<SDOperand,SDOperand>
534X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
535 Value *VAListV, const Type *ArgTy,
536 SelectionDAG &DAG) {
Chris Lattner14824582005-01-09 00:01:27 +0000537 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000538 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
539 VAListP, DAG.getSrcValue(VAListV));
540 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner08568cf2005-07-05 17:50:16 +0000541 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000542 unsigned Amt;
543 if (ArgVT == MVT::i32)
544 Amt = 4;
545 else {
546 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
547 "Other types should have been promoted for varargs!");
548 Amt = 8;
Chris Lattner14824582005-01-09 00:01:27 +0000549 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000550 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
551 DAG.getConstant(Amt, Val.getValueType()));
552 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000553 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000554 return std::make_pair(Result, Chain);
555}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000556
Chris Lattnerc6f41812005-05-12 23:06:28 +0000557//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000558// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000559//===----------------------------------------------------------------------===//
560//
561// The X86 'fast' calling convention passes up to two integer arguments in
562// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
563// and requires that the callee pop its arguments off the stack (allowing proper
564// tail calls), and has the same return value conventions as C calling convs.
565//
Chris Lattner10d26452005-05-13 23:49:10 +0000566// This calling convention always arranges for the callee pop value to be 8n+4
567// bytes, which is needed for tail recursion elimination and stack alignment
568// reasons.
569//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000570// Note that this can be enhanced in the future to pass fp vals in registers
571// (when we have a global fp allocator) and do other tricks.
572//
Chris Lattner63602fb2005-05-13 07:38:09 +0000573
574/// AddLiveIn - This helper function adds the specified physical register to the
575/// MachineFunction as a live in value. It also creates a corresponding virtual
576/// register for it.
577static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
578 TargetRegisterClass *RC) {
579 assert(RC->contains(PReg) && "Not the correct regclass!");
580 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
581 MF.addLiveIn(PReg, VReg);
582 return VReg;
583}
584
585
Chris Lattnerc6f41812005-05-12 23:06:28 +0000586std::vector<SDOperand>
587X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
588 std::vector<SDOperand> ArgValues;
589
590 MachineFunction &MF = DAG.getMachineFunction();
591 MachineFrameInfo *MFI = MF.getFrameInfo();
592
593 // Add DAG nodes to load the arguments... On entry to a function the stack
594 // frame looks like this:
595 //
596 // [ESP] -- return address
597 // [ESP + 4] -- first nonreg argument (leftmost lexically)
598 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
599 // ...
600 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
601
602 // Keep track of the number of integer regs passed so far. This can be either
603 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
604 // used).
605 unsigned NumIntRegs = 0;
606
607 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
608 MVT::ValueType ObjectVT = getValueType(I->getType());
609 unsigned ArgIncrement = 4;
610 unsigned ObjSize = 0;
611 SDOperand ArgValue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000612
Chris Lattnerc6f41812005-05-12 23:06:28 +0000613 switch (ObjectVT) {
614 default: assert(0 && "Unhandled argument type!");
615 case MVT::i1:
616 case MVT::i8:
617 if (NumIntRegs < 2) {
618 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000619 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
620 X86::R8RegisterClass);
621 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000622 DAG.setRoot(ArgValue.getValue(1));
623 }
624 ++NumIntRegs;
625 break;
626 }
627
628 ObjSize = 1;
629 break;
630 case MVT::i16:
631 if (NumIntRegs < 2) {
632 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000633 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
634 X86::R16RegisterClass);
635 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000636 DAG.setRoot(ArgValue.getValue(1));
637 }
638 ++NumIntRegs;
639 break;
640 }
641 ObjSize = 2;
642 break;
643 case MVT::i32:
644 if (NumIntRegs < 2) {
645 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000646 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
647 X86::R32RegisterClass);
648 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000649 DAG.setRoot(ArgValue.getValue(1));
650 }
651 ++NumIntRegs;
652 break;
653 }
654 ObjSize = 4;
655 break;
656 case MVT::i64:
657 if (NumIntRegs == 0) {
658 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000659 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
660 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000661
Chris Lattner63602fb2005-05-13 07:38:09 +0000662 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
663 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000664 DAG.setRoot(Hi.getValue(1));
665
666 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
667 }
668 NumIntRegs = 2;
669 break;
670 } else if (NumIntRegs == 1) {
671 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000672 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
673 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000674 DAG.setRoot(Low.getValue(1));
675
676 // Load the high part from memory.
677 // Create the frame index object for this incoming parameter...
678 int FI = MFI->CreateFixedObject(4, ArgOffset);
679 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
680 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
681 DAG.getSrcValue(NULL));
682 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
683 }
684 ArgOffset += 4;
685 NumIntRegs = 2;
686 break;
687 }
688 ObjSize = ArgIncrement = 8;
689 break;
690 case MVT::f32: ObjSize = 4; break;
691 case MVT::f64: ObjSize = ArgIncrement = 8; break;
692 }
693
694 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
695 // dead loads.
696 if (ObjSize && !I->use_empty()) {
697 // Create the frame index object for this incoming parameter...
698 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
699
700 // Create the SelectionDAG nodes corresponding to a load from this
701 // parameter.
702 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
703
704 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
705 DAG.getSrcValue(NULL));
706 } else if (ArgValue.Val == 0) {
707 if (MVT::isInteger(ObjectVT))
708 ArgValue = DAG.getConstant(0, ObjectVT);
709 else
710 ArgValue = DAG.getConstantFP(0, ObjectVT);
711 }
712 ArgValues.push_back(ArgValue);
713
714 if (ObjSize)
715 ArgOffset += ArgIncrement; // Move on to the next argument.
716 }
717
Chris Lattner10d26452005-05-13 23:49:10 +0000718 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
719 // arguments and the arguments after the retaddr has been pushed are aligned.
720 if ((ArgOffset & 7) == 0)
721 ArgOffset += 4;
722
Chris Lattner3648c672005-05-13 21:44:04 +0000723 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
724 ReturnAddrIndex = 0; // No return address slot generated yet.
725 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattner381e8872005-05-15 05:46:45 +0000726 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +0000727
728 // Finally, inform the code generator which regs we return values in.
729 switch (getValueType(F.getReturnType())) {
730 default: assert(0 && "Unknown type!");
731 case MVT::isVoid: break;
732 case MVT::i1:
733 case MVT::i8:
734 case MVT::i16:
735 case MVT::i32:
736 MF.addLiveOut(X86::EAX);
737 break;
738 case MVT::i64:
739 MF.addLiveOut(X86::EAX);
740 MF.addLiveOut(X86::EDX);
741 break;
742 case MVT::f32:
743 case MVT::f64:
744 MF.addLiveOut(X86::ST0);
745 break;
746 }
747 return ArgValues;
748}
749
750std::pair<SDOperand, SDOperand>
751X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000752 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000753 ArgListTy &Args, SelectionDAG &DAG) {
754 // Count how many bytes are to be pushed on the stack.
755 unsigned NumBytes = 0;
756
757 // Keep track of the number of integer regs passed so far. This can be either
758 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
759 // used).
760 unsigned NumIntRegs = 0;
761
762 for (unsigned i = 0, e = Args.size(); i != e; ++i)
763 switch (getValueType(Args[i].second)) {
764 default: assert(0 && "Unknown value type!");
765 case MVT::i1:
766 case MVT::i8:
767 case MVT::i16:
768 case MVT::i32:
769 if (NumIntRegs < 2) {
770 ++NumIntRegs;
771 break;
772 }
773 // fall through
774 case MVT::f32:
775 NumBytes += 4;
776 break;
777 case MVT::i64:
778 if (NumIntRegs == 0) {
779 NumIntRegs = 2;
780 break;
781 } else if (NumIntRegs == 1) {
782 NumIntRegs = 2;
783 NumBytes += 4;
784 break;
785 }
786
787 // fall through
788 case MVT::f64:
789 NumBytes += 8;
790 break;
791 }
792
Chris Lattner10d26452005-05-13 23:49:10 +0000793 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
794 // arguments and the arguments after the retaddr has been pushed are aligned.
795 if ((NumBytes & 7) == 0)
796 NumBytes += 4;
797
Chris Lattner16cd04d2005-05-12 23:24:06 +0000798 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000799 DAG.getConstant(NumBytes, getPointerTy()));
800
801 // Arguments go on the stack in reverse order, as specified by the ABI.
802 unsigned ArgOffset = 0;
803 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
804 DAG.getEntryNode());
805 NumIntRegs = 0;
806 std::vector<SDOperand> Stores;
807 std::vector<SDOperand> RegValuesToPass;
808 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
809 switch (getValueType(Args[i].second)) {
810 default: assert(0 && "Unexpected ValueType for argument!");
811 case MVT::i1:
812 case MVT::i8:
813 case MVT::i16:
814 case MVT::i32:
815 if (NumIntRegs < 2) {
816 RegValuesToPass.push_back(Args[i].first);
817 ++NumIntRegs;
818 break;
819 }
820 // Fall through
821 case MVT::f32: {
822 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
823 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
824 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
825 Args[i].first, PtrOff,
826 DAG.getSrcValue(NULL)));
827 ArgOffset += 4;
828 break;
829 }
830 case MVT::i64:
831 if (NumIntRegs < 2) { // Can pass part of it in regs?
832 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
833 Args[i].first, DAG.getConstant(1, MVT::i32));
834 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
835 Args[i].first, DAG.getConstant(0, MVT::i32));
836 RegValuesToPass.push_back(Lo);
837 ++NumIntRegs;
838 if (NumIntRegs < 2) { // Pass both parts in regs?
839 RegValuesToPass.push_back(Hi);
840 ++NumIntRegs;
841 } else {
842 // Pass the high part in memory.
843 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
844 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
845 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000846 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000847 ArgOffset += 4;
848 }
849 break;
850 }
851 // Fall through
852 case MVT::f64:
853 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
854 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
855 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
856 Args[i].first, PtrOff,
857 DAG.getSrcValue(NULL)));
858 ArgOffset += 8;
859 break;
860 }
861 }
862 if (!Stores.empty())
863 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
864
Chris Lattner10d26452005-05-13 23:49:10 +0000865 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
866 // arguments and the arguments after the retaddr has been pushed are aligned.
867 if ((ArgOffset & 7) == 0)
868 ArgOffset += 4;
869
Chris Lattner239738a2005-05-14 08:48:15 +0000870 std::vector<MVT::ValueType> RetVals;
871 MVT::ValueType RetTyVT = getValueType(RetTy);
872
873 RetVals.push_back(MVT::Other);
874
875 // The result values produced have to be legal. Promote the result.
876 switch (RetTyVT) {
877 case MVT::isVoid: break;
878 default:
879 RetVals.push_back(RetTyVT);
880 break;
881 case MVT::i1:
882 case MVT::i8:
883 case MVT::i16:
884 RetVals.push_back(MVT::i32);
885 break;
886 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000887 if (X86ScalarSSE)
888 RetVals.push_back(MVT::f32);
889 else
890 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000891 break;
892 case MVT::i64:
893 RetVals.push_back(MVT::i32);
894 RetVals.push_back(MVT::i32);
895 break;
896 }
897
898 std::vector<SDOperand> Ops;
899 Ops.push_back(Chain);
900 Ops.push_back(Callee);
901 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
902 // Callee pops all arg values on the stack.
903 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
904
905 // Pass register arguments as needed.
906 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
907
908 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
909 RetVals, Ops);
910 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
911
912 SDOperand ResultVal;
913 switch (RetTyVT) {
914 case MVT::isVoid: break;
915 default:
916 ResultVal = TheCall.getValue(1);
917 break;
918 case MVT::i1:
919 case MVT::i8:
920 case MVT::i16:
921 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
922 break;
923 case MVT::f32:
924 // FIXME: we would really like to remember that this FP_ROUND operation is
925 // okay to eliminate if we allow excess FP precision.
926 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
927 break;
928 case MVT::i64:
929 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
930 TheCall.getValue(2));
931 break;
932 }
933
934 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000935}
936
Chris Lattner381e8872005-05-15 05:46:45 +0000937SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
938 if (ReturnAddrIndex == 0) {
939 // Set up a frame object for the return address.
940 MachineFunction &MF = DAG.getMachineFunction();
941 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
942 }
943
944 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
945}
Chris Lattnerc6f41812005-05-12 23:06:28 +0000946
947
Chris Lattner14824582005-01-09 00:01:27 +0000948
949std::pair<SDOperand, SDOperand> X86TargetLowering::
950LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
951 SelectionDAG &DAG) {
952 SDOperand Result;
953 if (Depth) // Depths > 0 not supported yet!
954 Result = DAG.getConstant(0, getPointerTy());
955 else {
Chris Lattner381e8872005-05-15 05:46:45 +0000956 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000957 if (!isFrameAddress)
958 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000959 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
960 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000961 else
962 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
963 DAG.getConstant(4, MVT::i32));
964 }
965 return std::make_pair(Result, Chain);
966}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000967
Chris Lattnera28381c2005-07-16 00:28:20 +0000968//===----------------------------------------------------------------------===//
969// X86 Custom Lowering Hooks
970//===----------------------------------------------------------------------===//
971
Chris Lattner67649df2005-05-14 06:52:07 +0000972/// LowerOperation - Provide custom lowering hooks for some operations.
973///
974SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
975 switch (Op.getOpcode()) {
976 default: assert(0 && "Should not custom lower this!");
Chris Lattner745d5382005-07-29 00:40:01 +0000977 case ISD::SINT_TO_FP: {
Chris Lattner67649df2005-05-14 06:52:07 +0000978 assert(Op.getValueType() == MVT::f64 &&
979 Op.getOperand(0).getValueType() == MVT::i64 &&
980 "Unknown SINT_TO_FP to lower!");
981 // We lower sint64->FP into a store to a temporary stack slot, followed by a
982 // FILD64m node.
983 MachineFunction &MF = DAG.getMachineFunction();
984 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
985 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
986 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
987 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
988 std::vector<MVT::ValueType> RTs;
989 RTs.push_back(MVT::f64);
990 RTs.push_back(MVT::Other);
991 std::vector<SDOperand> Ops;
992 Ops.push_back(Store);
993 Ops.push_back(StackSlot);
994 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
995 }
Chris Lattner745d5382005-07-29 00:40:01 +0000996 case ISD::FP_TO_SINT: {
Chris Lattner01546c52005-07-30 00:05:54 +0000997 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner745d5382005-07-29 00:40:01 +0000998 Op.getOperand(0).getValueType() == MVT::f64 &&
999 "Unknown FP_TO_SINT to lower!");
1000 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1001 // stack slot.
1002 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner01546c52005-07-30 00:05:54 +00001003 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1004 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Chris Lattner745d5382005-07-29 00:40:01 +00001005 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1006
Chris Lattner01546c52005-07-30 00:05:54 +00001007 unsigned Opc;
1008 switch (Op.getValueType()) {
1009 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1010 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1011 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1012 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1013 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001014
Chris Lattner01546c52005-07-30 00:05:54 +00001015 // Build the FP_TO_INT*_IN_MEM
Chris Lattner745d5382005-07-29 00:40:01 +00001016 std::vector<SDOperand> Ops;
1017 Ops.push_back(DAG.getEntryNode());
1018 Ops.push_back(Op.getOperand(0));
1019 Ops.push_back(StackSlot);
Chris Lattner01546c52005-07-30 00:05:54 +00001020 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001021
Chris Lattner745d5382005-07-29 00:40:01 +00001022 // Load the result.
Chris Lattner01546c52005-07-30 00:05:54 +00001023 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1024 DAG.getSrcValue(NULL));
Chris Lattner745d5382005-07-29 00:40:01 +00001025 }
1026 }
Chris Lattner67649df2005-05-14 06:52:07 +00001027}
1028
1029
1030//===----------------------------------------------------------------------===//
1031// Pattern Matcher Implementation
1032//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001033
Chris Lattner98a8ba02005-01-18 01:06:26 +00001034namespace {
1035 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
1036 /// SDOperand's instead of register numbers for the leaves of the matched
1037 /// tree.
1038 struct X86ISelAddressMode {
1039 enum {
1040 RegBase,
1041 FrameIndexBase,
1042 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001043
Chris Lattner98a8ba02005-01-18 01:06:26 +00001044 struct { // This is really a union, discriminated by BaseType!
1045 SDOperand Reg;
1046 int FrameIndex;
1047 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001048
Chris Lattner98a8ba02005-01-18 01:06:26 +00001049 unsigned Scale;
1050 SDOperand IndexReg;
1051 unsigned Disp;
1052 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001053
Chris Lattner98a8ba02005-01-18 01:06:26 +00001054 X86ISelAddressMode()
1055 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1056 }
1057 };
1058}
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001059
1060
1061namespace {
1062 Statistic<>
1063 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1064
1065 //===--------------------------------------------------------------------===//
1066 /// ISel - X86 specific code to select X86 machine instructions for
1067 /// SelectionDAG operations.
1068 ///
1069 class ISel : public SelectionDAGISel {
1070 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1071 /// register should set this to true.
1072 bool ContainsFPCode;
1073
1074 /// X86Lowering - This object fully describes how to lower LLVM code to an
1075 /// X86-specific SelectionDAG.
1076 X86TargetLowering X86Lowering;
1077
Chris Lattner11333092005-01-11 03:11:44 +00001078 /// RegPressureMap - This keeps an approximate count of the number of
1079 /// registers required to evaluate each node in the graph.
1080 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001081
1082 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1083 /// vreg the value is produced in, so we only emit one copy of each compiled
1084 /// tree.
1085 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001086
Chris Lattner381e8872005-05-15 05:46:45 +00001087 /// TheDAG - The DAG being selected during Select* operations.
1088 SelectionDAG *TheDAG;
Jeff Cohen00b168892005-07-27 06:12:32 +00001089
1090 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanfb5792f2005-07-12 01:41:54 +00001091 /// make the right decision when generating code for different targets.
1092 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001093 public:
1094 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner8c4a8732005-08-05 21:54:27 +00001095 Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001096 }
1097
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001098 virtual const char *getPassName() const {
1099 return "X86 Pattern Instruction Selection";
1100 }
1101
Chris Lattner11333092005-01-11 03:11:44 +00001102 unsigned getRegPressure(SDOperand O) {
1103 return RegPressureMap[O.Val];
1104 }
1105 unsigned ComputeRegPressure(SDOperand O);
1106
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107 /// InstructionSelectBasicBlock - This callback is invoked by
1108 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001109 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001110
Chris Lattner63602fb2005-05-13 07:38:09 +00001111 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1112
Chris Lattner44129b52005-01-25 20:03:11 +00001113 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1114 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001115 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001116 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001117 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001118 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001119 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001120 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1121 MVT::ValueType SVT, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001122 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001123
1124 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1125 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1126 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner381e8872005-05-15 05:46:45 +00001127 bool EmitPotentialTailCall(SDNode *Node);
1128 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001129 void Select(SDOperand N);
1130 };
1131}
1132
Chris Lattner6415bb42005-05-10 03:53:18 +00001133/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1134/// the main function.
1135static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1136 MachineFrameInfo *MFI) {
1137 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1138 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1139 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1140
1141 // Set the high part to be 64-bit precision.
1142 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1143 CWFrameIdx, 1).addImm(2);
1144
1145 // Reload the modified control word now.
1146 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1147}
1148
Chris Lattner63602fb2005-05-13 07:38:09 +00001149void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1150 // If this function has live-in values, emit the copies from pregs to vregs at
1151 // the top of the function, before anything else.
1152 MachineBasicBlock *BB = MF.begin();
1153 if (MF.livein_begin() != MF.livein_end()) {
1154 SSARegMap *RegMap = MF.getSSARegMap();
1155 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1156 E = MF.livein_end(); LI != E; ++LI) {
1157 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1158 if (RC == X86::R8RegisterClass) {
1159 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1160 } else if (RC == X86::R16RegisterClass) {
1161 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1162 } else if (RC == X86::R32RegisterClass) {
1163 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1164 } else if (RC == X86::RFPRegisterClass) {
1165 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001166 } else if (RC == X86::RXMMRegisterClass) {
1167 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001168 } else {
1169 assert(0 && "Unknown regclass!");
1170 }
1171 }
1172 }
1173
1174
1175 // If this is main, emit special code for main.
1176 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1177 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1178}
1179
1180
Chris Lattner7dbcb752005-01-12 04:21:28 +00001181/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1182/// when it has created a SelectionDAG for us to codegen.
1183void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1184 // While we're doing this, keep track of whether we see any FP code for
1185 // FP_REG_KILL insertion.
1186 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001187 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001188
1189 // Scan the PHI nodes that already are inserted into this basic block. If any
1190 // of them is a PHI of a floating point value, we need to insert an
1191 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001192 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001193 if (BB != MF->begin())
1194 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1195 I != E; ++I) {
1196 assert(I->getOpcode() == X86::PHI &&
1197 "Isn't just PHI nodes?");
1198 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1199 X86::RFPRegisterClass) {
1200 ContainsFPCode = true;
1201 break;
1202 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001203 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001204
Chris Lattner7dbcb752005-01-12 04:21:28 +00001205 // Compute the RegPressureMap, which is an approximation for the number of
1206 // registers required to compute each node.
1207 ComputeRegPressure(DAG.getRoot());
1208
Chris Lattner381e8872005-05-15 05:46:45 +00001209 TheDAG = &DAG;
1210
Chris Lattner7dbcb752005-01-12 04:21:28 +00001211 // Codegen the basic block.
1212 Select(DAG.getRoot());
1213
Chris Lattner381e8872005-05-15 05:46:45 +00001214 TheDAG = 0;
1215
Chris Lattner7dbcb752005-01-12 04:21:28 +00001216 // Finally, look at all of the successors of this block. If any contain a PHI
1217 // node of FP type, we need to insert an FP_REG_KILL in this block.
1218 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1219 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1220 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1221 I != E && I->getOpcode() == X86::PHI; ++I) {
1222 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1223 X86::RFPRegisterClass) {
1224 ContainsFPCode = true;
1225 break;
1226 }
1227 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001228
Chris Lattnere3e0f272005-05-09 03:36:39 +00001229 // Final check, check LLVM BB's that are successors to the LLVM BB
1230 // corresponding to BB for FP PHI nodes.
1231 const BasicBlock *LLVMBB = BB->getBasicBlock();
1232 const PHINode *PN;
1233 if (!ContainsFPCode)
1234 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1235 SI != E && !ContainsFPCode; ++SI)
1236 for (BasicBlock::const_iterator II = SI->begin();
1237 (PN = dyn_cast<PHINode>(II)); ++II)
1238 if (PN->getType()->isFloatingPoint()) {
1239 ContainsFPCode = true;
1240 break;
1241 }
1242
1243
Chris Lattner7dbcb752005-01-12 04:21:28 +00001244 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1245 // only occurs due to the floating point stackifier not being aggressive
1246 // enough to handle arbitrary global stackification.
1247 //
1248 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1249 // defines a floating point virtual register.
1250 //
1251 // When the global register allocators (like linear scan) finally update live
1252 // variable analysis, we can keep floating point values in registers across
1253 // basic blocks. This will be a huge win, but we are waiting on the global
1254 // allocators before we can do this.
1255 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001256 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001257 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1258 ++NumFPKill;
1259 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001260
Chris Lattner7dbcb752005-01-12 04:21:28 +00001261 // Clear state used for selection.
1262 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001263 RegPressureMap.clear();
1264}
1265
1266
Chris Lattner11333092005-01-11 03:11:44 +00001267// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1268// for the number of registers required to compute each node. This is basically
1269// computing a generalized form of the Sethi-Ullman number for each node.
1270unsigned ISel::ComputeRegPressure(SDOperand O) {
1271 SDNode *N = O.Val;
1272 unsigned &Result = RegPressureMap[N];
1273 if (Result) return Result;
1274
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001275 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1276 // higher fixed cost??
1277
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001278 if (N->getNumOperands() == 0) {
1279 Result = 1;
1280 } else {
1281 unsigned MaxRegUse = 0;
1282 unsigned NumExtraMaxRegUsers = 0;
1283 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1284 unsigned Regs;
1285 if (N->getOperand(i).getOpcode() == ISD::Constant)
1286 Regs = 0;
1287 else
1288 Regs = ComputeRegPressure(N->getOperand(i));
1289 if (Regs > MaxRegUse) {
1290 MaxRegUse = Regs;
1291 NumExtraMaxRegUsers = 0;
1292 } else if (Regs == MaxRegUse &&
1293 N->getOperand(i).getValueType() != MVT::Other) {
1294 ++NumExtraMaxRegUsers;
1295 }
Chris Lattner11333092005-01-11 03:11:44 +00001296 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001297
1298 if (O.getOpcode() != ISD::TokenFactor)
1299 Result = MaxRegUse+NumExtraMaxRegUsers;
1300 else
Chris Lattner869e0432005-01-17 23:02:13 +00001301 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001302 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001303
Chris Lattner837caa72005-01-11 23:21:30 +00001304 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001305 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001306}
1307
Chris Lattnerbf52d492005-01-20 16:50:16 +00001308/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1309/// The DAG cannot have cycles in it, by definition, so the visited set is not
1310/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1311/// reuse, so it prevents exponential cases.
1312///
1313static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1314 std::set<SDNode*> &Visited) {
1315 if (N == Op) return true; // Found it.
1316 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001317 if (Node->getNumOperands() == 0 || // Leaf?
1318 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001319 if (!Visited.insert(Node).second) return false; // Already visited?
1320
1321 // Recurse for the first N-1 operands.
1322 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1323 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1324 return true;
1325
1326 // Tail recurse for the last operand.
1327 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1328}
1329
Chris Lattner98a8ba02005-01-18 01:06:26 +00001330X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1331 X86AddressMode Result;
1332
1333 // If we need to emit two register operands, emit the one with the highest
1334 // register pressure first.
1335 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1336 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001337 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001338 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001339 std::set<SDNode*> Visited;
1340 EmitBaseThenIndex = true;
1341 // If Base ends up pointing to Index, we must emit index first. This is
1342 // because of the way we fold loads, we may end up doing bad things with
1343 // the folded add.
1344 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1345 EmitBaseThenIndex = false;
1346 } else {
1347 std::set<SDNode*> Visited;
1348 EmitBaseThenIndex = false;
1349 // If Base ends up pointing to Index, we must emit index first. This is
1350 // because of the way we fold loads, we may end up doing bad things with
1351 // the folded add.
1352 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1353 EmitBaseThenIndex = true;
1354 }
1355
1356 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001357 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1358 Result.IndexReg = SelectExpr(IAM.IndexReg);
1359 } else {
1360 Result.IndexReg = SelectExpr(IAM.IndexReg);
1361 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1362 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001363
Chris Lattner98a8ba02005-01-18 01:06:26 +00001364 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1365 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1366 } else if (IAM.IndexReg.Val) {
1367 Result.IndexReg = SelectExpr(IAM.IndexReg);
1368 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001369
Chris Lattner98a8ba02005-01-18 01:06:26 +00001370 switch (IAM.BaseType) {
1371 case X86ISelAddressMode::RegBase:
1372 Result.BaseType = X86AddressMode::RegBase;
1373 break;
1374 case X86ISelAddressMode::FrameIndexBase:
1375 Result.BaseType = X86AddressMode::FrameIndexBase;
1376 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1377 break;
1378 default:
1379 assert(0 && "Unknown base type!");
1380 break;
1381 }
1382 Result.Scale = IAM.Scale;
1383 Result.Disp = IAM.Disp;
1384 Result.GV = IAM.GV;
1385 return Result;
1386}
1387
1388/// SelectAddress - Pattern match the maximal addressing mode for this node and
1389/// emit all of the leaf registers.
1390void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1391 X86ISelAddressMode IAM;
1392 MatchAddress(N, IAM);
1393 AM = SelectAddrExprs(IAM);
1394}
1395
1396/// MatchAddress - Add the specified node to the specified addressing mode,
1397/// returning true if it cannot be done. This just pattern matches for the
1398/// addressing mode, it does not cause any code to be emitted. For that, use
1399/// SelectAddress.
1400bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001401 switch (N.getOpcode()) {
1402 default: break;
1403 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001404 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1405 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001406 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1407 return false;
1408 }
1409 break;
1410 case ISD::GlobalAddress:
1411 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001412 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1413 // For Darwin, external and weak symbols are indirect, so we want to load
1414 // the value at address GV, not the value of GV itself. This means that
1415 // the GlobalAddress must be in the base or index register of the address,
1416 // not the GV offset field.
Jeff Cohen00b168892005-07-27 06:12:32 +00001417 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00001418 (GV->hasWeakLinkage() || GV->isExternal())) {
1419 break;
1420 } else {
1421 AM.GV = GV;
1422 return false;
1423 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001424 }
1425 break;
1426 case ISD::Constant:
1427 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1428 return false;
1429 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001430 // We might have folded the load into this shift, so don't regen the value
1431 // if so.
1432 if (ExprMap.count(N)) break;
1433
Chris Lattner98a8ba02005-01-18 01:06:26 +00001434 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001435 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1436 unsigned Val = CN->getValue();
1437 if (Val == 1 || Val == 2 || Val == 3) {
1438 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001439 SDOperand ShVal = N.Val->getOperand(0);
1440
1441 // Okay, we know that we have a scale by now. However, if the scaled
1442 // value is an add of something and a constant, we can fold the
1443 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001444 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001445 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001446 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001447 ConstantSDNode *AddVal =
1448 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1449 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001450 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001451 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001452 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001453 return false;
1454 }
1455 }
1456 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001457 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001458 // We might have folded the load into this mul, so don't regen the value if
1459 // so.
1460 if (ExprMap.count(N)) break;
1461
Chris Lattner947d5442005-01-11 19:37:02 +00001462 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001463 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1464 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001465 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1466 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1467 AM.Scale = unsigned(CN->getValue())-1;
1468
1469 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001470 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001471
1472 // Okay, we know that we have a scale by now. However, if the scaled
1473 // value is an add of something and a constant, we can fold the
1474 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001475 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001476 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001477 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001478 ConstantSDNode *AddVal =
1479 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1480 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001481 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001482 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001483 }
1484
1485 AM.IndexReg = AM.Base.Reg = Reg;
1486 return false;
1487 }
1488 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001489
1490 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001491 // We might have folded the load into this mul, so don't regen the value if
1492 // so.
1493 if (ExprMap.count(N)) break;
1494
Chris Lattner98a8ba02005-01-18 01:06:26 +00001495 X86ISelAddressMode Backup = AM;
1496 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1497 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001498 return false;
1499 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001500 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1501 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001502 return false;
1503 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001504 break;
1505 }
1506 }
1507
Chris Lattnera95589b2005-01-11 04:40:19 +00001508 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001509 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001510 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001511 if (AM.IndexReg.Val == 0) {
1512 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001513 AM.Scale = 1;
1514 return false;
1515 }
1516
1517 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001519 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001520
1521 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001522 AM.BaseType = X86ISelAddressMode::RegBase;
1523 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001524 return false;
1525}
1526
1527/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1528/// assuming that the temporary registers are in the 8-bit register class.
1529///
1530/// Tmp1 = setcc1
1531/// Tmp2 = setcc2
1532/// DestReg = logicalop Tmp1, Tmp2
1533///
1534static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1535 unsigned SetCC2, unsigned LogicalOp,
1536 unsigned DestReg) {
1537 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1538 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1539 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1540 BuildMI(BB, SetCC1, 0, Tmp1);
1541 BuildMI(BB, SetCC2, 0, Tmp2);
1542 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1543}
1544
1545/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1546/// condition codes match the specified SetCCOpcode. Note that some conditions
1547/// require multiple instructions to generate the correct value.
1548static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1549 ISD::CondCode SetCCOpcode, bool isFP) {
1550 unsigned Opc;
1551 if (!isFP) {
1552 switch (SetCCOpcode) {
1553 default: assert(0 && "Illegal integer SetCC!");
1554 case ISD::SETEQ: Opc = X86::SETEr; break;
1555 case ISD::SETGT: Opc = X86::SETGr; break;
1556 case ISD::SETGE: Opc = X86::SETGEr; break;
1557 case ISD::SETLT: Opc = X86::SETLr; break;
1558 case ISD::SETLE: Opc = X86::SETLEr; break;
1559 case ISD::SETNE: Opc = X86::SETNEr; break;
1560 case ISD::SETULT: Opc = X86::SETBr; break;
1561 case ISD::SETUGT: Opc = X86::SETAr; break;
1562 case ISD::SETULE: Opc = X86::SETBEr; break;
1563 case ISD::SETUGE: Opc = X86::SETAEr; break;
1564 }
1565 } else {
1566 // On a floating point condition, the flags are set as follows:
1567 // ZF PF CF op
1568 // 0 | 0 | 0 | X > Y
1569 // 0 | 0 | 1 | X < Y
1570 // 1 | 0 | 0 | X == Y
1571 // 1 | 1 | 1 | unordered
1572 //
1573 switch (SetCCOpcode) {
1574 default: assert(0 && "Invalid FP setcc!");
1575 case ISD::SETUEQ:
1576 case ISD::SETEQ:
1577 Opc = X86::SETEr; // True if ZF = 1
1578 break;
1579 case ISD::SETOGT:
1580 case ISD::SETGT:
1581 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1582 break;
1583 case ISD::SETOGE:
1584 case ISD::SETGE:
1585 Opc = X86::SETAEr; // True if CF = 0
1586 break;
1587 case ISD::SETULT:
1588 case ISD::SETLT:
1589 Opc = X86::SETBr; // True if CF = 1
1590 break;
1591 case ISD::SETULE:
1592 case ISD::SETLE:
1593 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1594 break;
1595 case ISD::SETONE:
1596 case ISD::SETNE:
1597 Opc = X86::SETNEr; // True if ZF = 0
1598 break;
1599 case ISD::SETUO:
1600 Opc = X86::SETPr; // True if PF = 1
1601 break;
1602 case ISD::SETO:
1603 Opc = X86::SETNPr; // True if PF = 0
1604 break;
1605 case ISD::SETOEQ: // !PF & ZF
1606 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1607 return;
1608 case ISD::SETOLT: // !PF & CF
1609 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1610 return;
1611 case ISD::SETOLE: // !PF & (CF || ZF)
1612 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1613 return;
1614 case ISD::SETUGT: // PF | (!ZF & !CF)
1615 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1616 return;
1617 case ISD::SETUGE: // PF | !CF
1618 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1619 return;
1620 case ISD::SETUNE: // PF | !ZF
1621 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1622 return;
1623 }
1624 }
1625 BuildMI(BB, Opc, 0, DestReg);
1626}
1627
1628
1629/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1630/// the Dest block if the Cond condition is true. If we cannot fold this
1631/// condition into the branch, return true.
1632///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001633bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1634 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001635 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1636 // B) using two conditional branches instead of one condbr, two setcc's, and
1637 // an or.
1638 if ((Cond.getOpcode() == ISD::OR ||
1639 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1640 // And and or set the flags for us, so there is no need to emit a TST of the
1641 // result. It is only safe to do this if there is only a single use of the
1642 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001643 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001644 SelectExpr(Cond);
1645 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1646 return false;
1647 }
1648
1649 // Codegen br not C -> JE.
1650 if (Cond.getOpcode() == ISD::XOR)
1651 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1652 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001653 unsigned CondR;
1654 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1655 Select(Chain);
1656 CondR = SelectExpr(Cond.Val->getOperand(0));
1657 } else {
1658 CondR = SelectExpr(Cond.Val->getOperand(0));
1659 Select(Chain);
1660 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001661 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1662 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1663 return false;
1664 }
1665
Chris Lattner88ac32c2005-08-09 20:21:10 +00001666 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001667 return true; // Can only handle simple setcc's so far.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001668 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001669
1670 unsigned Opc;
1671
1672 // Handle integer conditions first.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001673 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1674 switch (CC) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001675 default: assert(0 && "Illegal integer SetCC!");
1676 case ISD::SETEQ: Opc = X86::JE; break;
1677 case ISD::SETGT: Opc = X86::JG; break;
1678 case ISD::SETGE: Opc = X86::JGE; break;
1679 case ISD::SETLT: Opc = X86::JL; break;
1680 case ISD::SETLE: Opc = X86::JLE; break;
1681 case ISD::SETNE: Opc = X86::JNE; break;
1682 case ISD::SETULT: Opc = X86::JB; break;
1683 case ISD::SETUGT: Opc = X86::JA; break;
1684 case ISD::SETULE: Opc = X86::JBE; break;
1685 case ISD::SETUGE: Opc = X86::JAE; break;
1686 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001687 Select(Chain);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001688 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001689 BuildMI(BB, Opc, 1).addMBB(Dest);
1690 return false;
1691 }
1692
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001693 unsigned Opc2 = 0; // Second branch if needed.
1694
1695 // On a floating point condition, the flags are set as follows:
1696 // ZF PF CF op
1697 // 0 | 0 | 0 | X > Y
1698 // 0 | 0 | 1 | X < Y
1699 // 1 | 0 | 0 | X == Y
1700 // 1 | 1 | 1 | unordered
1701 //
Chris Lattner88ac32c2005-08-09 20:21:10 +00001702 switch (CC) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001703 default: assert(0 && "Invalid FP setcc!");
1704 case ISD::SETUEQ:
1705 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1706 case ISD::SETOGT:
1707 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1708 case ISD::SETOGE:
1709 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1710 case ISD::SETULT:
1711 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1712 case ISD::SETULE:
1713 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1714 case ISD::SETONE:
1715 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1716 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1717 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1718 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1719 Opc = X86::JA; // ZF = 0 & CF = 0
1720 Opc2 = X86::JP; // PF = 1
1721 break;
1722 case ISD::SETUGE: // PF = 1 | CF = 0
1723 Opc = X86::JAE; // CF = 0
1724 Opc2 = X86::JP; // PF = 1
1725 break;
1726 case ISD::SETUNE: // PF = 1 | ZF = 0
1727 Opc = X86::JNE; // ZF = 0
1728 Opc2 = X86::JP; // PF = 1
1729 break;
1730 case ISD::SETOEQ: // PF = 0 & ZF = 1
1731 //X86::JNP, X86::JE
1732 //X86::AND8rr
1733 return true; // FIXME: Emit more efficient code for this branch.
1734 case ISD::SETOLT: // PF = 0 & CF = 1
1735 //X86::JNP, X86::JB
1736 //X86::AND8rr
1737 return true; // FIXME: Emit more efficient code for this branch.
1738 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1739 //X86::JNP, X86::JBE
1740 //X86::AND8rr
1741 return true; // FIXME: Emit more efficient code for this branch.
1742 }
1743
Chris Lattner6c07aee2005-01-11 04:06:27 +00001744 Select(Chain);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001745 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001746 BuildMI(BB, Opc, 1).addMBB(Dest);
1747 if (Opc2)
1748 BuildMI(BB, Opc2, 1).addMBB(Dest);
1749 return false;
1750}
1751
Chris Lattner24aad1b2005-01-10 22:10:13 +00001752/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001753/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner24aad1b2005-01-10 22:10:13 +00001754///
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001755void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1756 MVT::ValueType SVT, unsigned RDest) {
1757 unsigned RTrue, RFalse;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001758 enum Condition {
1759 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1760 NOT_SET
1761 } CondCode = NOT_SET;
1762
1763 static const unsigned CMOVTAB16[] = {
1764 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1765 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001766 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001767 };
1768 static const unsigned CMOVTAB32[] = {
1769 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1770 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001771 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001772 };
1773 static const unsigned CMOVTABFP[] = {
1774 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1775 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1776 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1777 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001778 static const int SSE_CMOVTAB[] = {
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001779 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
1780 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
1781 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begemanf63be7d2005-07-06 18:59:04 +00001782 };
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001783
Chris Lattner88ac32c2005-08-09 20:21:10 +00001784 if (Cond.getOpcode() == ISD::SETCC) {
1785 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1786 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1787 switch (CC) {
Chris Lattner24aad1b2005-01-10 22:10:13 +00001788 default: assert(0 && "Unknown integer comparison!");
1789 case ISD::SETEQ: CondCode = EQ; break;
1790 case ISD::SETGT: CondCode = GT; break;
1791 case ISD::SETGE: CondCode = GE; break;
1792 case ISD::SETLT: CondCode = LT; break;
1793 case ISD::SETLE: CondCode = LE; break;
1794 case ISD::SETNE: CondCode = NE; break;
1795 case ISD::SETULT: CondCode = B; break;
1796 case ISD::SETUGT: CondCode = A; break;
1797 case ISD::SETULE: CondCode = BE; break;
1798 case ISD::SETUGE: CondCode = AE; break;
1799 }
1800 } else {
1801 // On a floating point condition, the flags are set as follows:
1802 // ZF PF CF op
1803 // 0 | 0 | 0 | X > Y
1804 // 0 | 0 | 1 | X < Y
1805 // 1 | 0 | 0 | X == Y
1806 // 1 | 1 | 1 | unordered
1807 //
Chris Lattner88ac32c2005-08-09 20:21:10 +00001808 switch (CC) {
Chris Lattner24aad1b2005-01-10 22:10:13 +00001809 default: assert(0 && "Unknown FP comparison!");
1810 case ISD::SETUEQ:
1811 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1812 case ISD::SETOGT:
1813 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1814 case ISD::SETOGE:
1815 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1816 case ISD::SETULT:
1817 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1818 case ISD::SETULE:
1819 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1820 case ISD::SETONE:
1821 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1822 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1823 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1824 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1825 case ISD::SETUGE: // PF = 1 | CF = 0
1826 case ISD::SETUNE: // PF = 1 | ZF = 0
1827 case ISD::SETOEQ: // PF = 0 & ZF = 1
1828 case ISD::SETOLT: // PF = 0 & CF = 1
1829 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1830 // We cannot emit this comparison as a single cmov.
1831 break;
1832 }
1833 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001834
Chris Lattner24aad1b2005-01-10 22:10:13 +00001835
Chris Lattner88ac32c2005-08-09 20:21:10 +00001836 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
1837 // code above and one of the results of the select is +0.0, then we can fake
1838 // it up through a clever AND with mask. Otherwise, we will fall through to
1839 // the code below that will use a PHI node to select the right value.
1840 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1841 if (Cond.getOperand(0).getValueType() == SVT &&
1842 NOT_SET != CondCode) {
1843 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
1844 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
1845 bool TrueZero = CT && CT->isExactlyValue(0.0);
1846 bool FalseZero = CF && CF->isExactlyValue(0.0);
1847 if (TrueZero || FalseZero) {
1848 SDOperand LHS = Cond.getOperand(0);
1849 SDOperand RHS = Cond.getOperand(1);
1850
1851 // Select the two halves of the condition
1852 unsigned RLHS, RRHS;
1853 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1854 RLHS = SelectExpr(LHS);
1855 RRHS = SelectExpr(RHS);
1856 } else {
1857 RRHS = SelectExpr(RHS);
1858 RLHS = SelectExpr(LHS);
1859 }
1860
1861 // Emit the comparison and generate a mask from it
1862 unsigned MaskReg = MakeReg(SVT);
1863 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
1864 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
1865 .addImm(SSE_CMOVTAB[CondCode]);
1866
1867 if (TrueZero) {
1868 RFalse = SelectExpr(False);
1869 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
1870 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
1871 } else {
1872 RTrue = SelectExpr(True);
1873 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
1874 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
1875 }
1876 return;
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001877 }
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001878 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001879 }
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001880 }
1881
1882 // Select the true and false values for use in both the SSE PHI case, and the
1883 // integer or x87 cmov cases below.
1884 if (getRegPressure(True) > getRegPressure(False)) {
1885 RTrue = SelectExpr(True);
1886 RFalse = SelectExpr(False);
1887 } else {
1888 RFalse = SelectExpr(False);
1889 RTrue = SelectExpr(True);
1890 }
1891
1892 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
1893 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
1894 // node to select between the true and false values.
1895 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1896 // FIXME: emit a direct compare and branch rather than setting a cond reg
1897 // and testing it.
1898 unsigned CondReg = SelectExpr(Cond);
1899 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1900
1901 // Create an iterator with which to insert the MBB for copying the false
1902 // value and the MBB to hold the PHI instruction for this SetCC.
1903 MachineBasicBlock *thisMBB = BB;
1904 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1905 ilist<MachineBasicBlock>::iterator It = BB;
1906 ++It;
1907
1908 // thisMBB:
1909 // ...
1910 // TrueVal = ...
1911 // cmpTY ccX, r1, r2
1912 // bCC sinkMBB
1913 // fallthrough --> copy0MBB
1914 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1915 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1916 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1917 MachineFunction *F = BB->getParent();
1918 F->getBasicBlockList().insert(It, copy0MBB);
1919 F->getBasicBlockList().insert(It, sinkMBB);
1920 // Update machine-CFG edges
1921 BB->addSuccessor(copy0MBB);
1922 BB->addSuccessor(sinkMBB);
1923
1924 // copy0MBB:
1925 // %FalseValue = ...
1926 // # fallthrough to sinkMBB
1927 BB = copy0MBB;
1928 // Update machine-CFG edges
1929 BB->addSuccessor(sinkMBB);
1930
1931 // sinkMBB:
1932 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1933 // ...
1934 BB = sinkMBB;
1935 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1936 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001937 return;
1938 }
1939
Chris Lattner24aad1b2005-01-10 22:10:13 +00001940 unsigned Opc = 0;
1941 if (CondCode != NOT_SET) {
1942 switch (SVT) {
1943 default: assert(0 && "Cannot select this type!");
1944 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1945 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001946 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001947 }
1948 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001949
Chris Lattner24aad1b2005-01-10 22:10:13 +00001950 // Finally, if we weren't able to fold this, just emit the condition and test
1951 // it.
1952 if (CondCode == NOT_SET || Opc == 0) {
1953 // Get the condition into the zero flag.
1954 unsigned CondReg = SelectExpr(Cond);
1955 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1956
1957 switch (SVT) {
1958 default: assert(0 && "Cannot select this type!");
1959 case MVT::i16: Opc = X86::CMOVE16rr; break;
1960 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001961 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001962 }
1963 } else {
1964 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001965 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001966 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001967 }
1968 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1969}
1970
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001971void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001972 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1974 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001975 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001976 switch (RHS.getValueType()) {
1977 default: break;
1978 case MVT::i1:
1979 case MVT::i8: Opc = X86::CMP8mi; break;
1980 case MVT::i16: Opc = X86::CMP16mi; break;
1981 case MVT::i32: Opc = X86::CMP32mi; break;
1982 }
1983 if (Opc) {
1984 X86AddressMode AM;
1985 EmitFoldedLoad(LHS, AM);
1986 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1987 return;
1988 }
1989 }
1990
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001991 switch (RHS.getValueType()) {
1992 default: break;
1993 case MVT::i1:
1994 case MVT::i8: Opc = X86::CMP8ri; break;
1995 case MVT::i16: Opc = X86::CMP16ri; break;
1996 case MVT::i32: Opc = X86::CMP32ri; break;
1997 }
1998 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001999 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
2001 return;
2002 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002003 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002004 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
2005 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00002006 unsigned Reg = SelectExpr(LHS);
2007 BuildMI(BB, X86::FTST, 1).addReg(Reg);
2008 BuildMI(BB, X86::FNSTSW8r, 0);
2009 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00002010 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00002011 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002012 }
2013
Chris Lattneref6806c2005-01-12 02:02:48 +00002014 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00002015 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00002016 switch (RHS.getValueType()) {
2017 default: break;
2018 case MVT::i1:
2019 case MVT::i8: Opc = X86::CMP8mr; break;
2020 case MVT::i16: Opc = X86::CMP16mr; break;
2021 case MVT::i32: Opc = X86::CMP32mr; break;
2022 }
2023 if (Opc) {
2024 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002025 EmitFoldedLoad(LHS, AM);
2026 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00002027 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
2028 return;
2029 }
2030 }
2031
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002032 switch (LHS.getValueType()) {
2033 default: assert(0 && "Cannot compare this value!");
2034 case MVT::i1:
2035 case MVT::i8: Opc = X86::CMP8rr; break;
2036 case MVT::i16: Opc = X86::CMP16rr; break;
2037 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002038 case MVT::f32: Opc = X86::UCOMISSrr; break;
2039 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002040 }
Chris Lattner11333092005-01-11 03:11:44 +00002041 unsigned Tmp1, Tmp2;
2042 if (getRegPressure(LHS) > getRegPressure(RHS)) {
2043 Tmp1 = SelectExpr(LHS);
2044 Tmp2 = SelectExpr(RHS);
2045 } else {
2046 Tmp2 = SelectExpr(RHS);
2047 Tmp1 = SelectExpr(LHS);
2048 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002049 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
2050}
2051
Chris Lattnera5ade062005-01-11 21:19:59 +00002052/// isFoldableLoad - Return true if this is a load instruction that can safely
2053/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00002054bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
2055 if (Op.getOpcode() == ISD::LOAD) {
2056 // FIXME: currently can't fold constant pool indexes.
2057 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2058 return false;
2059 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00002060 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00002061 // FIXME: currently can't fold constant pool indexes.
2062 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2063 return false;
2064 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002065 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00002066 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002067
2068 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00002069 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2070 if (ExprMap.count(Op.getValue(1))) return false;
2071 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00002072 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00002073
Chris Lattner4ff348b2005-01-17 06:26:58 +00002074 // If there is not just one use of its value, we cannot fold.
2075 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2076
2077 // Finally, we cannot fold the load into the operation if this would induce a
2078 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2079 // operand of the operation we are folding the load into) can possible use the
2080 // chain node defined by the load.
2081 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2082 std::set<SDNode*> Visited;
2083 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2084 return false;
2085 }
2086 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00002087}
2088
Chris Lattner4ff348b2005-01-17 06:26:58 +00002089
Chris Lattnera5ade062005-01-11 21:19:59 +00002090/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2091/// and compute the address being loaded into AM.
2092void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2093 SDOperand Chain = Op.getOperand(0);
2094 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002095
Chris Lattnera5ade062005-01-11 21:19:59 +00002096 if (getRegPressure(Chain) > getRegPressure(Address)) {
2097 Select(Chain);
2098 SelectAddress(Address, AM);
2099 } else {
2100 SelectAddress(Address, AM);
2101 Select(Chain);
2102 }
2103
2104 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002105 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2106 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002107 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002108 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002109}
2110
Chris Lattner30ea1e92005-01-19 07:37:26 +00002111// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2112// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2113// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2114// return true.
2115bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002116 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2117 // good!
2118 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2119 std::swap(Op1, Op2); // Op1 is the SHL now.
2120 } else {
2121 return false; // No match
2122 }
2123
2124 SDOperand ShlVal = Op1.getOperand(0);
2125 SDOperand ShlAmt = Op1.getOperand(1);
2126 SDOperand ShrVal = Op2.getOperand(0);
2127 SDOperand ShrAmt = Op2.getOperand(1);
2128
Chris Lattner30ea1e92005-01-19 07:37:26 +00002129 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2130
Chris Lattner85716372005-01-19 06:18:43 +00002131 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2132 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2133 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002134 if (SubCST->getValue() == RegSize) {
2135 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002136 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002137 if (ShrVal == ShlVal) {
2138 unsigned Reg, ShAmt;
2139 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2140 Reg = SelectExpr(ShrVal);
2141 ShAmt = SelectExpr(ShrAmt);
2142 } else {
2143 ShAmt = SelectExpr(ShrAmt);
2144 Reg = SelectExpr(ShrVal);
2145 }
2146 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2147 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2148 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2149 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2150 return true;
2151 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002152 unsigned AReg, BReg;
2153 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002154 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002155 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002156 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002157 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002158 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002159 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002160 unsigned ShAmt = SelectExpr(ShrAmt);
2161 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2162 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2163 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002164 return true;
2165 }
2166 }
2167
Chris Lattner4053b1e2005-01-19 08:07:05 +00002168 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2169 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2170 if (SubCST->getValue() == RegSize) {
2171 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2172 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2173 if (ShrVal == ShlVal) {
2174 unsigned Reg, ShAmt;
2175 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2176 Reg = SelectExpr(ShrVal);
2177 ShAmt = SelectExpr(ShlAmt);
2178 } else {
2179 ShAmt = SelectExpr(ShlAmt);
2180 Reg = SelectExpr(ShrVal);
2181 }
2182 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2183 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2184 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2185 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2186 return true;
2187 } else if (RegSize != 8) {
2188 unsigned AReg, BReg;
2189 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002190 AReg = SelectExpr(ShlVal);
2191 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002192 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002193 BReg = SelectExpr(ShrVal);
2194 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002195 }
2196 unsigned ShAmt = SelectExpr(ShlAmt);
2197 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2198 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2199 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2200 return true;
2201 }
2202 }
Chris Lattner85716372005-01-19 06:18:43 +00002203
Chris Lattner4053b1e2005-01-19 08:07:05 +00002204 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2205 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2206 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2207 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2208 // (A >> 5) | (A << 27) --> ROR A, 5
2209 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2210 if (ShrVal == ShlVal) {
2211 unsigned Reg = SelectExpr(ShrVal);
2212 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2213 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2214 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2215 return true;
2216 } else if (RegSize != 8) {
2217 unsigned AReg, BReg;
2218 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002219 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002220 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002221 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002222 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002223 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002224 }
2225 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2226 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2227 .addImm(ShrCst->getValue());
2228 return true;
2229 }
2230 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002231
Chris Lattner85716372005-01-19 06:18:43 +00002232 return false;
2233}
2234
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002235unsigned ISel::SelectExpr(SDOperand N) {
2236 unsigned Result;
2237 unsigned Tmp1, Tmp2, Tmp3;
2238 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002239 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002240 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002241
Chris Lattner7f2afac2005-01-14 22:37:41 +00002242 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002243 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2244 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2245 // Just use the specified register as our input.
2246 return cast<RegSDNode>(Node)->getReg();
2247 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002248 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002249
Chris Lattnera5ade062005-01-11 21:19:59 +00002250 unsigned &Reg = ExprMap[N];
2251 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002252
Chris Lattnerb38a7492005-04-02 04:01:14 +00002253 switch (N.getOpcode()) {
2254 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002255 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002256 MakeReg(N.getValueType()) : 1;
2257 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002258 case X86ISD::TAILCALL:
2259 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002260 // If this is a call instruction, make sure to prepare ALL of the result
2261 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002262 ExprMap[N.getValue(0)] = 1;
2263 if (Node->getNumValues() > 1) {
2264 Result = MakeReg(Node->getValueType(1));
2265 ExprMap[N.getValue(1)] = Result;
2266 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002267 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002268 } else {
2269 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002270 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002271 break;
2272 case ISD::ADD_PARTS:
2273 case ISD::SUB_PARTS:
2274 case ISD::SHL_PARTS:
2275 case ISD::SRL_PARTS:
2276 case ISD::SRA_PARTS:
2277 Result = MakeReg(Node->getValueType(0));
2278 ExprMap[N.getValue(0)] = Result;
2279 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2280 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2281 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002282 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002283
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002284 switch (N.getOpcode()) {
2285 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002286 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002287 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002288 case ISD::FP_EXTEND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002289 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002290 Tmp1 = SelectExpr(N.getOperand(0));
2291 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2292 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002293 case ISD::FP_ROUND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002294 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman16b04f32005-07-15 00:38:55 +00002295 Tmp1 = SelectExpr(N.getOperand(0));
2296 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2297 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002298 case ISD::CopyFromReg:
2299 Select(N.getOperand(0));
2300 if (Result == 1) {
2301 Reg = Result = ExprMap[N.getValue(0)] =
2302 MakeReg(N.getValue(0).getValueType());
2303 }
2304 switch (Node->getValueType(0)) {
2305 default: assert(0 && "Cannot CopyFromReg this!");
2306 case MVT::i1:
2307 case MVT::i8:
2308 BuildMI(BB, X86::MOV8rr, 1,
2309 Result).addReg(cast<RegSDNode>(Node)->getReg());
2310 return Result;
2311 case MVT::i16:
2312 BuildMI(BB, X86::MOV16rr, 1,
2313 Result).addReg(cast<RegSDNode>(Node)->getReg());
2314 return Result;
2315 case MVT::i32:
2316 BuildMI(BB, X86::MOV32rr, 1,
2317 Result).addReg(cast<RegSDNode>(Node)->getReg());
2318 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002319 }
Chris Lattnerc6f41812005-05-12 23:06:28 +00002320
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002321 case ISD::FrameIndex:
2322 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2323 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2324 return Result;
2325 case ISD::ConstantPool:
2326 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2327 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2328 return Result;
2329 case ISD::ConstantFP:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00002330 if (X86ScalarSSE) {
2331 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
2332 "SSE only supports +0.0");
2333 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
2334 BuildMI(BB, Opc, 0, Result);
2335 return Result;
2336 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002337 ContainsFPCode = true;
2338 Tmp1 = Result; // Intermediate Register
2339 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2340 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2341 Tmp1 = MakeReg(MVT::f64);
2342
2343 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2344 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2345 BuildMI(BB, X86::FLD0, 0, Tmp1);
2346 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2347 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2348 BuildMI(BB, X86::FLD1, 0, Tmp1);
2349 else
2350 assert(0 && "Unexpected constant!");
2351 if (Tmp1 != Result)
2352 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2353 return Result;
2354 case ISD::Constant:
2355 switch (N.getValueType()) {
2356 default: assert(0 && "Cannot use constants of this type!");
2357 case MVT::i1:
2358 case MVT::i8: Opc = X86::MOV8ri; break;
2359 case MVT::i16: Opc = X86::MOV16ri; break;
2360 case MVT::i32: Opc = X86::MOV32ri; break;
2361 }
2362 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2363 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002364 case ISD::UNDEF:
2365 if (Node->getValueType(0) == MVT::f64) {
2366 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2367 BuildMI(BB, X86::FLD0, 0, Result);
2368 } else {
2369 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2370 }
2371 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002372 case ISD::GlobalAddress: {
2373 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002374 // For Darwin, external and weak symbols are indirect, so we want to load
2375 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00002376 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00002377 (GV->hasWeakLinkage() || GV->isExternal())) {
2378 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2379 .addGlobalAddress(GV, false, 0);
2380 } else {
2381 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2382 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002383 return Result;
2384 }
2385 case ISD::ExternalSymbol: {
2386 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2387 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2388 return Result;
2389 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002390 case ISD::ZERO_EXTEND: {
2391 int DestIs16 = N.getValueType() == MVT::i16;
2392 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002393
2394 // FIXME: This hack is here for zero extension casts from bool to i8. This
2395 // would not be needed if bools were promoted by Legalize.
2396 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002397 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002398 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2399 return Result;
2400 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002401
Chris Lattner4ff348b2005-01-17 06:26:58 +00002402 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002403 static const unsigned Opc[3] = {
2404 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2405 };
2406
2407 X86AddressMode AM;
2408 EmitFoldedLoad(N.getOperand(0), AM);
2409 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002410
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002411 return Result;
2412 }
2413
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002414 static const unsigned Opc[3] = {
2415 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2416 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002417 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002418 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2419 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002420 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002421 case ISD::SIGN_EXTEND: {
2422 int DestIs16 = N.getValueType() == MVT::i16;
2423 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2424
Chris Lattner590d8002005-01-09 18:52:44 +00002425 // FIXME: Legalize should promote bools to i8!
2426 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2427 "Sign extend from bool not implemented!");
2428
Chris Lattner4ff348b2005-01-17 06:26:58 +00002429 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002430 static const unsigned Opc[3] = {
2431 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2432 };
2433
2434 X86AddressMode AM;
2435 EmitFoldedLoad(N.getOperand(0), AM);
2436 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2437 return Result;
2438 }
2439
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 static const unsigned Opc[3] = {
2441 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2442 };
2443 Tmp1 = SelectExpr(N.getOperand(0));
2444 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2445 return Result;
2446 }
2447 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002448 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002449 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002450 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002451 switch (N.getValueType()) {
2452 default: assert(0 && "Unknown truncate!");
2453 case MVT::i1:
2454 case MVT::i8: Opc = X86::MOV8rm; break;
2455 case MVT::i16: Opc = X86::MOV16rm; break;
2456 }
2457 X86AddressMode AM;
2458 EmitFoldedLoad(N.getOperand(0), AM);
2459 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2460 return Result;
2461 }
2462
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002463 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2464 // a move out of AX or AL.
2465 switch (N.getOperand(0).getValueType()) {
2466 default: assert(0 && "Unknown truncate!");
2467 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2468 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2469 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2470 }
2471 Tmp1 = SelectExpr(N.getOperand(0));
2472 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2473
2474 switch (N.getValueType()) {
2475 default: assert(0 && "Unknown truncate!");
2476 case MVT::i1:
2477 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2478 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2479 }
2480 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2481 return Result;
2482
Chris Lattnera28381c2005-07-16 00:28:20 +00002483 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002484 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2485 unsigned PromoteOpcode = 0;
2486
Nate Begeman5a8441e2005-07-16 02:02:34 +00002487 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002488 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002489 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002490 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2491 return Result;
2492 }
Jeff Cohen00b168892005-07-27 06:12:32 +00002493
Chris Lattneref7ba072005-01-11 03:50:45 +00002494 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002495
Chris Lattner590d8002005-01-09 18:52:44 +00002496 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002497 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002498 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2499 MachineFunction *F = BB->getParent();
2500 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2501
2502 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002503 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002504 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002505 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2506 break;
2507 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002508 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002509 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2510 break;
2511 default: break; // No promotion required.
2512 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002513 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002514 }
Chris Lattner01546c52005-07-30 00:05:54 +00002515 case ISD::FP_TO_SINT:
Chris Lattner590d8002005-01-09 18:52:44 +00002516 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2517
Nate Begemanf63be7d2005-07-06 18:59:04 +00002518 // If the target supports SSE2 and is performing FP operations in SSE regs
2519 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2520 // CVTSD2SI instructions.
Chris Lattner01546c52005-07-30 00:05:54 +00002521 assert(X86ScalarSSE);
2522 if (MVT::f32 == N.getOperand(0).getValueType()) {
2523 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
2524 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2525 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
2526 } else {
2527 assert(0 && "Not an f32 or f64?");
2528 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +00002529 }
Chris Lattner590d8002005-01-09 18:52:44 +00002530 return Result;
Chris Lattner01546c52005-07-30 00:05:54 +00002531
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:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00003021 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
3022 N.getValueType(), Result);
Chris Lattnerda2ce112005-01-16 07:34:08 +00003023 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003024
3025 case ISD::SDIV:
3026 case ISD::UDIV:
3027 case ISD::SREM:
3028 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003029 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3030 "We don't support this operator!");
3031
Chris Lattner5bf26862005-04-13 03:29:53 +00003032 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003033 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003034 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003035 // Check for reversed and unreversed DIV.
3036 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3037 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3038 Opc = X86::FDIVR32m;
3039 else
3040 Opc = X86::FDIVR64m;
3041 X86AddressMode AM;
3042 EmitFoldedLoad(N.getOperand(0), AM);
3043 Tmp1 = SelectExpr(N.getOperand(1));
3044 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3045 return Result;
3046 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3047 N.getOperand(1).getOpcode() == ISD::LOAD) {
3048 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3049 Opc = X86::FDIV32m;
3050 else
3051 Opc = X86::FDIV64m;
3052 X86AddressMode AM;
3053 EmitFoldedLoad(N.getOperand(1), AM);
3054 Tmp1 = SelectExpr(N.getOperand(0));
3055 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3056 return Result;
3057 }
3058 }
3059
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003060 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3061 // FIXME: These special cases should be handled by the lowering impl!
3062 unsigned RHS = CN->getValue();
3063 bool isNeg = false;
3064 if ((int)RHS < 0) {
3065 isNeg = true;
3066 RHS = -RHS;
3067 }
3068 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
Chris Lattner0561b3f2005-08-02 19:26:06 +00003069 unsigned Log = Log2_32(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003070 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3071 switch (N.getValueType()) {
3072 default: assert("Unknown type to signed divide!");
3073 case MVT::i8:
3074 SAROpc = X86::SAR8ri;
3075 SHROpc = X86::SHR8ri;
3076 ADDOpc = X86::ADD8rr;
3077 NEGOpc = X86::NEG8r;
3078 break;
3079 case MVT::i16:
3080 SAROpc = X86::SAR16ri;
3081 SHROpc = X86::SHR16ri;
3082 ADDOpc = X86::ADD16rr;
3083 NEGOpc = X86::NEG16r;
3084 break;
3085 case MVT::i32:
3086 SAROpc = X86::SAR32ri;
3087 SHROpc = X86::SHR32ri;
3088 ADDOpc = X86::ADD32rr;
3089 NEGOpc = X86::NEG32r;
3090 break;
3091 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003092 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003093 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003094 unsigned TmpReg;
3095 if (Log != 1) {
3096 TmpReg = MakeReg(N.getValueType());
3097 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3098 } else {
3099 TmpReg = Tmp1;
3100 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003101 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003102 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003103 unsigned TmpReg3 = MakeReg(N.getValueType());
3104 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003105
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003106 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3107 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3108 if (isNeg)
3109 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3110 return Result;
3111 }
3112 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003113 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003114
Chris Lattner11333092005-01-11 03:11:44 +00003115 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3116 Tmp1 = SelectExpr(N.getOperand(0));
3117 Tmp2 = SelectExpr(N.getOperand(1));
3118 } else {
3119 Tmp2 = SelectExpr(N.getOperand(1));
3120 Tmp1 = SelectExpr(N.getOperand(0));
3121 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003122
3123 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3124 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3125 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3126 switch (N.getValueType()) {
3127 default: assert(0 && "Cannot sdiv this type!");
3128 case MVT::i8:
3129 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3130 LoReg = X86::AL;
3131 HiReg = X86::AH;
3132 MovOpcode = X86::MOV8rr;
3133 ClrOpcode = X86::MOV8ri;
3134 SExtOpcode = X86::CBW;
3135 break;
3136 case MVT::i16:
3137 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3138 LoReg = X86::AX;
3139 HiReg = X86::DX;
3140 MovOpcode = X86::MOV16rr;
3141 ClrOpcode = X86::MOV16ri;
3142 SExtOpcode = X86::CWD;
3143 break;
3144 case MVT::i32:
3145 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003146 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003147 HiReg = X86::EDX;
3148 MovOpcode = X86::MOV32rr;
3149 ClrOpcode = X86::MOV32ri;
3150 SExtOpcode = X86::CDQ;
3151 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003152 case MVT::f32:
3153 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3154 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003155 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003156 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3157 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003158 return Result;
3159 }
3160
3161 // Set up the low part.
3162 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3163
3164 if (isSigned) {
3165 // Sign extend the low part into the high part.
3166 BuildMI(BB, SExtOpcode, 0);
3167 } else {
3168 // Zero out the high part, effectively zero extending the input.
3169 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3170 }
3171
3172 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003173 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003174
3175 // Get the result of the divide or rem.
3176 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3177 return Result;
3178 }
3179
3180 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003181 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003182 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3183 switch (N.getValueType()) {
3184 default: assert(0 && "Cannot shift this type!");
3185 case MVT::i8: Opc = X86::ADD8rr; break;
3186 case MVT::i16: Opc = X86::ADD16rr; break;
3187 case MVT::i32: Opc = X86::ADD32rr; break;
3188 }
3189 Tmp1 = SelectExpr(N.getOperand(0));
3190 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3191 return Result;
3192 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003193
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003194 switch (N.getValueType()) {
3195 default: assert(0 && "Cannot shift this type!");
3196 case MVT::i8: Opc = X86::SHL8ri; break;
3197 case MVT::i16: Opc = X86::SHL16ri; break;
3198 case MVT::i32: Opc = X86::SHL32ri; break;
3199 }
Chris Lattner11333092005-01-11 03:11:44 +00003200 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003201 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3202 return Result;
3203 }
Chris Lattner11333092005-01-11 03:11:44 +00003204
3205 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3206 Tmp1 = SelectExpr(N.getOperand(0));
3207 Tmp2 = SelectExpr(N.getOperand(1));
3208 } else {
3209 Tmp2 = SelectExpr(N.getOperand(1));
3210 Tmp1 = SelectExpr(N.getOperand(0));
3211 }
3212
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003213 switch (N.getValueType()) {
3214 default: assert(0 && "Cannot shift this type!");
3215 case MVT::i8 : Opc = X86::SHL8rCL; break;
3216 case MVT::i16: Opc = X86::SHL16rCL; break;
3217 case MVT::i32: Opc = X86::SHL32rCL; break;
3218 }
3219 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3220 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3221 return Result;
3222 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003223 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3224 switch (N.getValueType()) {
3225 default: assert(0 && "Cannot shift this type!");
3226 case MVT::i8: Opc = X86::SHR8ri; break;
3227 case MVT::i16: Opc = X86::SHR16ri; break;
3228 case MVT::i32: Opc = X86::SHR32ri; break;
3229 }
Chris Lattner11333092005-01-11 03:11:44 +00003230 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003231 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3232 return Result;
3233 }
Chris Lattner11333092005-01-11 03:11:44 +00003234
3235 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3236 Tmp1 = SelectExpr(N.getOperand(0));
3237 Tmp2 = SelectExpr(N.getOperand(1));
3238 } else {
3239 Tmp2 = SelectExpr(N.getOperand(1));
3240 Tmp1 = SelectExpr(N.getOperand(0));
3241 }
3242
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003243 switch (N.getValueType()) {
3244 default: assert(0 && "Cannot shift this type!");
3245 case MVT::i8 : Opc = X86::SHR8rCL; break;
3246 case MVT::i16: Opc = X86::SHR16rCL; break;
3247 case MVT::i32: Opc = X86::SHR32rCL; break;
3248 }
3249 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3250 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3251 return Result;
3252 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003253 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3254 switch (N.getValueType()) {
3255 default: assert(0 && "Cannot shift this type!");
3256 case MVT::i8: Opc = X86::SAR8ri; break;
3257 case MVT::i16: Opc = X86::SAR16ri; break;
3258 case MVT::i32: Opc = X86::SAR32ri; break;
3259 }
Chris Lattner11333092005-01-11 03:11:44 +00003260 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003261 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3262 return Result;
3263 }
Chris Lattner11333092005-01-11 03:11:44 +00003264
3265 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3266 Tmp1 = SelectExpr(N.getOperand(0));
3267 Tmp2 = SelectExpr(N.getOperand(1));
3268 } else {
3269 Tmp2 = SelectExpr(N.getOperand(1));
3270 Tmp1 = SelectExpr(N.getOperand(0));
3271 }
3272
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003273 switch (N.getValueType()) {
3274 default: assert(0 && "Cannot shift this type!");
3275 case MVT::i8 : Opc = X86::SAR8rCL; break;
3276 case MVT::i16: Opc = X86::SAR16rCL; break;
3277 case MVT::i32: Opc = X86::SAR32rCL; break;
3278 }
3279 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3280 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3281 return Result;
3282
3283 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003284 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88ac32c2005-08-09 20:21:10 +00003285 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003286 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3287 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003288 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003289 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003290 if (Result != 1) { // Generate the token
3291 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3292 assert(0 && "Load already emitted!?");
3293 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003294 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3295
Chris Lattner5188ad72005-01-08 19:28:19 +00003296 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003297 default: assert(0 && "Cannot load this type!");
3298 case MVT::i1:
3299 case MVT::i8: Opc = X86::MOV8rm; break;
3300 case MVT::i16: Opc = X86::MOV16rm; break;
3301 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003302 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003303 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003304 if (X86ScalarSSE) {
3305 Opc = X86::MOVSDrm;
3306 } else {
3307 Opc = X86::FLD64m;
Jeff Cohen00b168892005-07-27 06:12:32 +00003308 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003309 }
3310 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003311 }
Chris Lattner11333092005-01-11 03:11:44 +00003312
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003313 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003314 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003315 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3316 } else {
3317 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003318
3319 SDOperand Chain = N.getOperand(0);
3320 SDOperand Address = N.getOperand(1);
3321 if (getRegPressure(Chain) > getRegPressure(Address)) {
3322 Select(Chain);
3323 SelectAddress(Address, AM);
3324 } else {
3325 SelectAddress(Address, AM);
3326 Select(Chain);
3327 }
3328
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003329 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3330 }
3331 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003332 case X86ISD::FILD64m:
3333 // Make sure we generate both values.
3334 assert(Result != 1 && N.getValueType() == MVT::f64);
3335 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3336 assert(0 && "Load already emitted!?");
3337
3338 {
3339 X86AddressMode AM;
3340
3341 SDOperand Chain = N.getOperand(0);
3342 SDOperand Address = N.getOperand(1);
3343 if (getRegPressure(Chain) > getRegPressure(Address)) {
3344 Select(Chain);
3345 SelectAddress(Address, AM);
3346 } else {
3347 SelectAddress(Address, AM);
3348 Select(Chain);
3349 }
Chris Lattner745d5382005-07-29 00:40:01 +00003350
3351 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattner67649df2005-05-14 06:52:07 +00003352 }
3353 return Result;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003354
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003355 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3356 case ISD::ZEXTLOAD: {
3357 // Make sure we generate both values.
3358 if (Result != 1)
3359 ExprMap[N.getValue(1)] = 1; // Generate the token
3360 else
3361 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3362
Chris Lattnerda2ce112005-01-16 07:34:08 +00003363 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3364 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003365 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003366 "Bad EXTLOAD!");
3367 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3368 CP->getIndex());
3369 return Result;
3370 }
3371
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003372 X86AddressMode AM;
3373 if (getRegPressure(Node->getOperand(0)) >
3374 getRegPressure(Node->getOperand(1))) {
3375 Select(Node->getOperand(0)); // chain
3376 SelectAddress(Node->getOperand(1), AM);
3377 } else {
3378 SelectAddress(Node->getOperand(1), AM);
3379 Select(Node->getOperand(0)); // chain
3380 }
3381
3382 switch (Node->getValueType(0)) {
3383 default: assert(0 && "Unknown type to sign extend to.");
3384 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003385 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003386 "Bad EXTLOAD!");
3387 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3388 break;
3389 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003390 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003391 default:
3392 assert(0 && "Bad zero extend!");
3393 case MVT::i1:
3394 case MVT::i8:
3395 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3396 break;
3397 case MVT::i16:
3398 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3399 break;
3400 }
3401 break;
3402 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003403 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003404 "Bad zero extend!");
3405 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3406 break;
3407 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003408 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003409 "Bad zero extend!");
3410 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3411 break;
3412 }
3413 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003414 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003415 case ISD::SEXTLOAD: {
3416 // Make sure we generate both values.
3417 if (Result != 1)
3418 ExprMap[N.getValue(1)] = 1; // Generate the token
3419 else
3420 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3421
3422 X86AddressMode AM;
3423 if (getRegPressure(Node->getOperand(0)) >
3424 getRegPressure(Node->getOperand(1))) {
3425 Select(Node->getOperand(0)); // chain
3426 SelectAddress(Node->getOperand(1), AM);
3427 } else {
3428 SelectAddress(Node->getOperand(1), AM);
3429 Select(Node->getOperand(0)); // chain
3430 }
3431
3432 switch (Node->getValueType(0)) {
3433 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3434 default: assert(0 && "Unknown type to sign extend to.");
3435 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003436 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003437 default:
3438 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3439 case MVT::i8:
3440 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3441 break;
3442 case MVT::i16:
3443 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3444 break;
3445 }
3446 break;
3447 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003448 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003449 "Cannot sign extend from bool!");
3450 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3451 break;
3452 }
3453 return Result;
3454 }
3455
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003456 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003457 // Generate both result values.
3458 if (Result != 1)
3459 ExprMap[N.getValue(1)] = 1; // Generate the token
3460 else
3461 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3462
3463 // FIXME: We are currently ignoring the requested alignment for handling
3464 // greater than the stack alignment. This will need to be revisited at some
3465 // point. Align = N.getOperand(2);
3466
3467 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3468 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3469 std::cerr << "Cannot allocate stack object with greater alignment than"
3470 << " the stack alignment yet!";
3471 abort();
3472 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003473
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003474 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003475 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003476 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3477 .addImm(CN->getValue());
3478 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003479 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3480 Select(N.getOperand(0));
3481 Tmp1 = SelectExpr(N.getOperand(1));
3482 } else {
3483 Tmp1 = SelectExpr(N.getOperand(1));
3484 Select(N.getOperand(0));
3485 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003486
3487 // Subtract size from stack pointer, thereby allocating some space.
3488 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3489 }
3490
3491 // Put a pointer to the space into the result register, by copying the stack
3492 // pointer.
3493 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3494 return Result;
3495
Chris Lattner239738a2005-05-14 08:48:15 +00003496 case X86ISD::TAILCALL:
3497 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003498 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003499 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003500
Chris Lattnerc6f41812005-05-12 23:06:28 +00003501 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3502 isa<ExternalSymbolSDNode>(N.getOperand(1));
3503 unsigned Callee = 0;
3504 if (isDirect) {
3505 Select(N.getOperand(0));
3506 } else {
3507 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3508 Select(N.getOperand(0));
3509 Callee = SelectExpr(N.getOperand(1));
3510 } else {
3511 Callee = SelectExpr(N.getOperand(1));
3512 Select(N.getOperand(0));
3513 }
3514 }
3515
3516 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003517 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003518 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003519 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003520 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003521 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen00b168892005-07-27 06:12:32 +00003522
Chris Lattner239738a2005-05-14 08:48:15 +00003523 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003524 default: assert(0 && "Bad thing to pass in regs");
3525 case MVT::i1:
3526 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3527 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3528 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3529 }
3530 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003531 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003532 default: assert(0 && "Bad thing to pass in regs");
3533 case MVT::i1:
3534 case MVT::i8:
3535 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3536 break;
3537 case MVT::i16:
3538 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3539 break;
3540 case MVT::i32:
3541 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3542 break;
3543 }
3544 }
3545
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003546 if (GlobalAddressSDNode *GASD =
3547 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3548 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3549 } else if (ExternalSymbolSDNode *ESSDN =
3550 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3551 BuildMI(BB, X86::CALLpcrel32,
3552 1).addExternalSymbol(ESSDN->getSymbol(), true);
3553 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003554 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3555 Select(N.getOperand(0));
3556 Tmp1 = SelectExpr(N.getOperand(1));
3557 } else {
3558 Tmp1 = SelectExpr(N.getOperand(1));
3559 Select(N.getOperand(0));
3560 }
3561
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003562 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3563 }
Chris Lattner239738a2005-05-14 08:48:15 +00003564
3565 // Get caller stack amount and amount the callee added to the stack pointer.
3566 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3567 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3568 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3569
3570 if (Node->getNumValues() != 1)
3571 switch (Node->getValueType(1)) {
3572 default: assert(0 && "Unknown value type for call result!");
3573 case MVT::Other: return 1;
3574 case MVT::i1:
3575 case MVT::i8:
3576 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3577 break;
3578 case MVT::i16:
3579 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3580 break;
3581 case MVT::i32:
3582 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3583 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3584 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3585 break;
3586 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003587 if (X86ScalarSSE) {
3588 ContainsFPCode = true;
3589 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3590
3591 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3592 MachineFunction *F = BB->getParent();
3593 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3594 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3595 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3596 break;
3597 } else {
3598 ContainsFPCode = true;
3599 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3600 break;
3601 }
Chris Lattner239738a2005-05-14 08:48:15 +00003602 }
3603 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003604 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003605 case ISD::READPORT:
3606 // First, determine that the size of the operand falls within the acceptable
3607 // range for this architecture.
3608 //
3609 if (Node->getOperand(1).getValueType() != MVT::i16) {
3610 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3611 exit(1);
3612 }
3613
3614 // Make sure we generate both values.
3615 if (Result != 1) { // Generate the token
3616 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3617 assert(0 && "readport already emitted!?");
3618 } else
3619 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen00b168892005-07-27 06:12:32 +00003620
Chris Lattner966cdfb2005-05-09 21:17:38 +00003621 Select(Node->getOperand(0)); // Select the chain.
3622
3623 // If the port is a single-byte constant, use the immediate form.
3624 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3625 if ((Port->getValue() & 255) == Port->getValue()) {
3626 switch (Node->getValueType(0)) {
3627 case MVT::i8:
3628 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3629 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3630 return Result;
3631 case MVT::i16:
3632 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3633 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3634 return Result;
3635 case MVT::i32:
3636 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3637 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3638 return Result;
3639 default: break;
3640 }
3641 }
3642
3643 // Now, move the I/O port address into the DX register and use the IN
3644 // instruction to get the input data.
3645 //
3646 Tmp1 = SelectExpr(Node->getOperand(1));
3647 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3648 switch (Node->getValueType(0)) {
3649 case MVT::i8:
3650 BuildMI(BB, X86::IN8rr, 0);
3651 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3652 return Result;
3653 case MVT::i16:
3654 BuildMI(BB, X86::IN16rr, 0);
3655 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3656 return Result;
3657 case MVT::i32:
3658 BuildMI(BB, X86::IN32rr, 0);
3659 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3660 return Result;
3661 default:
3662 std::cerr << "Cannot do input on this data type";
3663 exit(1);
3664 }
Jeff Cohen00b168892005-07-27 06:12:32 +00003665
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003666 }
3667
3668 return 0;
3669}
3670
Chris Lattnere10269b2005-01-17 19:25:26 +00003671/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3672/// load/op/store instruction. If successful return true.
3673bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3674 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3675 SDOperand Chain = Node->getOperand(0);
3676 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003677 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003678
3679 // The chain has to be a load, the stored value must be an integer binary
3680 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003681 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003682 MVT::isFloatingPoint(StVal.getValueType()))
3683 return false;
3684
Chris Lattner5c659812005-01-17 22:10:42 +00003685 // Token chain must either be a factor node or the load to fold.
3686 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3687 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003688
Chris Lattner5c659812005-01-17 22:10:42 +00003689 SDOperand TheLoad;
3690
3691 // Check to see if there is a load from the same pointer that we're storing
3692 // to in either operand of the binop.
3693 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3694 StVal.getOperand(0).getOperand(1) == StPtr)
3695 TheLoad = StVal.getOperand(0);
3696 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3697 StVal.getOperand(1).getOperand(1) == StPtr)
3698 TheLoad = StVal.getOperand(1);
3699 else
3700 return false; // No matching load operand.
3701
3702 // We can only fold the load if there are no intervening side-effecting
3703 // operations. This means that the store uses the load as its token chain, or
3704 // there are only token factor nodes in between the store and load.
3705 if (Chain != TheLoad.getValue(1)) {
3706 // Okay, the other option is that we have a store referring to (possibly
3707 // nested) token factor nodes. For now, just try peeking through one level
3708 // of token factors to see if this is the case.
3709 bool ChainOk = false;
3710 if (Chain.getOpcode() == ISD::TokenFactor) {
3711 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3712 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3713 ChainOk = true;
3714 break;
3715 }
3716 }
3717
3718 if (!ChainOk) return false;
3719 }
3720
3721 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003722 return false;
3723
3724 // Make sure that one of the operands of the binop is the load, and that the
3725 // load folds into the binop.
3726 if (((StVal.getOperand(0) != TheLoad ||
3727 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3728 (StVal.getOperand(1) != TheLoad ||
3729 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3730 return false;
3731
3732 // Finally, check to see if this is one of the ops we can handle!
3733 static const unsigned ADDTAB[] = {
3734 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3735 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3736 };
3737 static const unsigned SUBTAB[] = {
3738 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3739 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3740 };
3741 static const unsigned ANDTAB[] = {
3742 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3743 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3744 };
3745 static const unsigned ORTAB[] = {
3746 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3747 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3748 };
3749 static const unsigned XORTAB[] = {
3750 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3751 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3752 };
3753 static const unsigned SHLTAB[] = {
3754 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3755 /*Have to put the reg in CL*/0, 0, 0,
3756 };
3757 static const unsigned SARTAB[] = {
3758 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3759 /*Have to put the reg in CL*/0, 0, 0,
3760 };
3761 static const unsigned SHRTAB[] = {
3762 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3763 /*Have to put the reg in CL*/0, 0, 0,
3764 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003765
Chris Lattnere10269b2005-01-17 19:25:26 +00003766 const unsigned *TabPtr = 0;
3767 switch (StVal.getOpcode()) {
3768 default:
3769 std::cerr << "CANNOT [mem] op= val: ";
3770 StVal.Val->dump(); std::cerr << "\n";
3771 case ISD::MUL:
3772 case ISD::SDIV:
3773 case ISD::UDIV:
3774 case ISD::SREM:
3775 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003776
Chris Lattnere10269b2005-01-17 19:25:26 +00003777 case ISD::ADD: TabPtr = ADDTAB; break;
3778 case ISD::SUB: TabPtr = SUBTAB; break;
3779 case ISD::AND: TabPtr = ANDTAB; break;
3780 case ISD:: OR: TabPtr = ORTAB; break;
3781 case ISD::XOR: TabPtr = XORTAB; break;
3782 case ISD::SHL: TabPtr = SHLTAB; break;
3783 case ISD::SRA: TabPtr = SARTAB; break;
3784 case ISD::SRL: TabPtr = SHRTAB; break;
3785 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003786
Chris Lattnere10269b2005-01-17 19:25:26 +00003787 // Handle: [mem] op= CST
3788 SDOperand Op0 = StVal.getOperand(0);
3789 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003790 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003791 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3792 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3793 default: break;
3794 case MVT::i1:
3795 case MVT::i8: Opc = TabPtr[0]; break;
3796 case MVT::i16: Opc = TabPtr[1]; break;
3797 case MVT::i32: Opc = TabPtr[2]; break;
3798 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003799
Chris Lattnere10269b2005-01-17 19:25:26 +00003800 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003801 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3802 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003803 Select(Chain);
3804
Chris Lattnere10269b2005-01-17 19:25:26 +00003805 X86AddressMode AM;
3806 if (getRegPressure(TheLoad.getOperand(0)) >
3807 getRegPressure(TheLoad.getOperand(1))) {
3808 Select(TheLoad.getOperand(0));
3809 SelectAddress(TheLoad.getOperand(1), AM);
3810 } else {
3811 SelectAddress(TheLoad.getOperand(1), AM);
3812 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003813 }
Chris Lattner5c659812005-01-17 22:10:42 +00003814
3815 if (StVal.getOpcode() == ISD::ADD) {
3816 if (CN->getValue() == 1) {
3817 switch (Op0.getValueType()) {
3818 default: break;
3819 case MVT::i8:
3820 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3821 return true;
3822 case MVT::i16: Opc = TabPtr[1];
3823 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3824 return true;
3825 case MVT::i32: Opc = TabPtr[2];
3826 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3827 return true;
3828 }
3829 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3830 switch (Op0.getValueType()) {
3831 default: break;
3832 case MVT::i8:
3833 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3834 return true;
3835 case MVT::i16: Opc = TabPtr[1];
3836 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3837 return true;
3838 case MVT::i32: Opc = TabPtr[2];
3839 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3840 return true;
3841 }
3842 }
3843 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003844
Chris Lattnere10269b2005-01-17 19:25:26 +00003845 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3846 return true;
3847 }
3848 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003849
Chris Lattnere10269b2005-01-17 19:25:26 +00003850 // If we have [mem] = V op [mem], try to turn it into:
3851 // [mem] = [mem] op V.
3852 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3853 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3854 StVal.getOpcode() != ISD::SRL)
3855 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003856
Chris Lattnere10269b2005-01-17 19:25:26 +00003857 if (Op0 != TheLoad) return false;
3858
3859 switch (Op0.getValueType()) {
3860 default: return false;
3861 case MVT::i1:
3862 case MVT::i8: Opc = TabPtr[3]; break;
3863 case MVT::i16: Opc = TabPtr[4]; break;
3864 case MVT::i32: Opc = TabPtr[5]; break;
3865 }
Chris Lattner5c659812005-01-17 22:10:42 +00003866
Chris Lattnerb422aea2005-01-18 17:35:28 +00003867 // Table entry doesn't exist?
3868 if (Opc == 0) return false;
3869
Chris Lattner4a108662005-01-18 03:51:59 +00003870 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3871 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003872 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003873 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003874
Chris Lattnere10269b2005-01-17 19:25:26 +00003875 X86AddressMode AM;
3876 SelectAddress(TheLoad.getOperand(1), AM);
3877 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003878 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003879 return true;
3880}
3881
Chris Lattner381e8872005-05-15 05:46:45 +00003882/// If node is a ret(tailcall) node, emit the specified tail call and return
3883/// true, otherwise return false.
3884///
3885/// FIXME: This whole thing should be a post-legalize optimization pass which
3886/// recognizes and transforms the dag. We don't want the selection phase doing
3887/// this stuff!!
3888///
3889bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3890 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3891
3892 SDOperand Chain = RetNode->getOperand(0);
3893
3894 // If this is a token factor node where one operand is a call, dig into it.
3895 SDOperand TokFactor;
3896 unsigned TokFactorOperand = 0;
3897 if (Chain.getOpcode() == ISD::TokenFactor) {
3898 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3899 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3900 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3901 TokFactorOperand = i;
3902 TokFactor = Chain;
3903 Chain = Chain.getOperand(i);
3904 break;
3905 }
3906 if (TokFactor.Val == 0) return false; // No call operand.
3907 }
3908
3909 // Skip the CALLSEQ_END node if present.
3910 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3911 Chain = Chain.getOperand(0);
3912
3913 // Is a tailcall the last control operation that occurs before the return?
3914 if (Chain.getOpcode() != X86ISD::TAILCALL)
3915 return false;
3916
3917 // If we return a value, is it the value produced by the call?
3918 if (RetNode->getNumOperands() > 1) {
3919 // Not returning the ret val of the call?
3920 if (Chain.Val->getNumValues() == 1 ||
3921 RetNode->getOperand(1) != Chain.getValue(1))
3922 return false;
3923
3924 if (RetNode->getNumOperands() > 2) {
3925 if (Chain.Val->getNumValues() == 2 ||
3926 RetNode->getOperand(2) != Chain.getValue(2))
3927 return false;
3928 }
3929 assert(RetNode->getNumOperands() <= 3);
3930 }
3931
3932 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3933 // For FastCC, this will always be > 0.
3934 unsigned CalleeCallArgAmt =
3935 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3936
3937 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3938 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3939 unsigned CalleeCallArgPopAmt =
3940 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3941
3942 // There are several cases we can handle here. First, if the caller and
3943 // callee are both CCC functions, we can tailcall if the callee takes <= the
3944 // number of argument bytes that the caller does.
3945 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3946 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3947 // Check to see if caller arg area size >= callee arg area size.
3948 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3949 //std::cerr << "CCC TAILCALL UNIMP!\n";
3950 // If TokFactor is non-null, emit all operands.
3951
3952 //EmitCCCToCCCTailCall(Chain.Val);
3953 //return true;
3954 }
3955 return false;
3956 }
3957
3958 // Second, if both are FastCC functions, we can always perform the tail call.
3959 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3960 // If TokFactor is non-null, emit all operands before the call.
3961 if (TokFactor.Val) {
3962 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3963 if (i != TokFactorOperand)
3964 Select(TokFactor.getOperand(i));
3965 }
3966
3967 EmitFastCCToFastCCTailCall(Chain.Val);
3968 return true;
3969 }
3970
3971 // We don't support mixed calls, due to issues with alignment. We could in
3972 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3973 // aligned (which depends on the number of arguments to the callee). TODO.
3974 return false;
3975}
3976
3977static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3978 SelectionDAG &DAG) {
3979 MVT::ValueType StoreVT;
3980 switch (Chain.getOpcode()) {
3981 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003982 // If we found the start of the call sequence, we're done. We actually
3983 // strip off the CALLSEQ_START node, to avoid generating the
3984 // ADJCALLSTACKDOWN marker for the tail call.
3985 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003986 case ISD::TokenFactor: {
3987 std::vector<SDOperand> Ops;
3988 Ops.reserve(Chain.getNumOperands());
3989 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3990 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3991 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3992 }
3993 case ISD::STORE: // Normal store
3994 StoreVT = Chain.getOperand(1).getValueType();
3995 break;
3996 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003997 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00003998 break;
3999 }
4000
4001 SDOperand OrigDest = Chain.getOperand(2);
4002 unsigned OrigOffset;
4003
4004 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4005 OrigOffset = 0;
4006 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
4007 } else {
4008 // We expect only (ESP+C)
4009 assert(OrigDest.getOpcode() == ISD::ADD &&
4010 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4011 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
4012 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
4013 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4014 }
4015
4016 // Compute the new offset from the incoming ESP value we wish to use.
4017 unsigned NewOffset = OrigOffset + Offset;
4018
4019 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4020 MachineFunction &MF = DAG.getMachineFunction();
4021 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4022 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4023
4024 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4025 DAG);
4026 if (Chain.getOpcode() == ISD::STORE)
4027 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4028 FIN);
4029 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4030 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004031 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004032}
4033
4034
4035/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4036/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4037/// tail call.
4038void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4039 unsigned CalleeCallArgSize =
4040 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4041 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4042
4043 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4044
4045 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4046 // indexes that are relative to the incoming ESP. If the incoming and
4047 // outgoing arg sizes are the same we will store to [InESP] instead of
4048 // [CurESP] and the ESP referenced will be relative to the incoming function
4049 // ESP.
4050 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4051 SDOperand AdjustedArgStores =
4052 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4053
4054 // Copy the return address of the caller into a virtual register so we don't
4055 // clobber it.
4056 SDOperand RetVal;
4057 if (ESPOffset) {
4058 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4059 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4060 RetValAddr, TheDAG->getSrcValue(NULL));
4061 SelectExpr(RetVal);
4062 }
4063
4064 // Codegen all of the argument stores.
4065 Select(AdjustedArgStores);
4066
4067 if (RetVal.Val) {
4068 // Emit a store of the saved ret value to the new location.
4069 MachineFunction &MF = TheDAG->getMachineFunction();
4070 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4071 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4072 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4073 RetVal, RetValAddr));
4074 }
4075
4076 // Get the destination value.
4077 SDOperand Callee = TailCallNode->getOperand(1);
4078 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4079 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004080 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004081 if (!isDirect) CalleeReg = SelectExpr(Callee);
4082
4083 unsigned RegOp1 = 0;
4084 unsigned RegOp2 = 0;
4085
4086 if (TailCallNode->getNumOperands() > 4) {
4087 // The first value is passed in (a part of) EAX, the second in EDX.
4088 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4089 if (TailCallNode->getNumOperands() > 5)
4090 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen00b168892005-07-27 06:12:32 +00004091
Chris Lattner381e8872005-05-15 05:46:45 +00004092 switch (TailCallNode->getOperand(4).getValueType()) {
4093 default: assert(0 && "Bad thing to pass in regs");
4094 case MVT::i1:
4095 case MVT::i8:
4096 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4097 RegOp1 = X86::AL;
4098 break;
4099 case MVT::i16:
4100 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4101 RegOp1 = X86::AX;
4102 break;
4103 case MVT::i32:
4104 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4105 RegOp1 = X86::EAX;
4106 break;
4107 }
4108 if (RegOp2)
4109 switch (TailCallNode->getOperand(5).getValueType()) {
4110 default: assert(0 && "Bad thing to pass in regs");
4111 case MVT::i1:
4112 case MVT::i8:
4113 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4114 RegOp2 = X86::DL;
4115 break;
4116 case MVT::i16:
4117 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4118 RegOp2 = X86::DX;
4119 break;
4120 case MVT::i32:
4121 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4122 RegOp2 = X86::EDX;
4123 break;
4124 }
4125 }
4126
4127 // Adjust ESP.
4128 if (ESPOffset)
4129 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4130 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4131
4132 // TODO: handle jmp [mem]
4133 if (!isDirect) {
4134 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4135 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004136 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004137 } else {
4138 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4139 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4140 }
4141 // ADD IMPLICIT USE RegOp1/RegOp2's
4142}
4143
Chris Lattnere10269b2005-01-17 19:25:26 +00004144
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004145void ISel::Select(SDOperand N) {
4146 unsigned Tmp1, Tmp2, Opc;
4147
Nate Begeman85fdeb22005-03-24 04:39:54 +00004148 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004149 return; // Already selected.
4150
Chris Lattner989de032005-01-11 06:14:36 +00004151 SDNode *Node = N.Val;
4152
4153 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004154 default:
Chris Lattner989de032005-01-11 06:14:36 +00004155 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004156 assert(0 && "Node not handled yet!");
4157 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004158 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004159 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004160 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004161 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4162 Select(Node->getOperand(OneFirst));
4163 Select(Node->getOperand(!OneFirst));
4164 } else {
4165 std::vector<std::pair<unsigned, unsigned> > OpsP;
4166 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4167 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4168 std::sort(OpsP.begin(), OpsP.end());
4169 std::reverse(OpsP.begin(), OpsP.end());
4170 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4171 Select(Node->getOperand(OpsP[i].second));
4172 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004173 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004174 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004175 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4176 Select(N.getOperand(0));
4177 Tmp1 = SelectExpr(N.getOperand(1));
4178 } else {
4179 Tmp1 = SelectExpr(N.getOperand(1));
4180 Select(N.getOperand(0));
4181 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004182 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004184 if (Tmp1 != Tmp2) {
4185 switch (N.getOperand(1).getValueType()) {
4186 default: assert(0 && "Invalid type for operation!");
4187 case MVT::i1:
4188 case MVT::i8: Opc = X86::MOV8rr; break;
4189 case MVT::i16: Opc = X86::MOV16rr; break;
4190 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004191 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00004192 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004193 if (X86ScalarSSE) {
4194 Opc = X86::MOVAPDrr;
4195 } else {
Jeff Cohen00b168892005-07-27 06:12:32 +00004196 Opc = X86::FpMOV;
4197 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004198 }
4199 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004200 }
4201 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4202 }
4203 return;
4204 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004205 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4206 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4207 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4208 if (EmitPotentialTailCall(Node))
4209 return;
4210
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004211 switch (N.getNumOperands()) {
4212 default:
4213 assert(0 && "Unknown return instruction!");
4214 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004215 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00004216 N.getOperand(2).getValueType() == MVT::i32 &&
4217 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004218 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4219 Tmp1 = SelectExpr(N.getOperand(1));
4220 Tmp2 = SelectExpr(N.getOperand(2));
4221 } else {
4222 Tmp2 = SelectExpr(N.getOperand(2));
4223 Tmp1 = SelectExpr(N.getOperand(1));
4224 }
4225 Select(N.getOperand(0));
4226
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004227 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4228 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004229 break;
4230 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004231 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4232 Select(N.getOperand(0));
4233 Tmp1 = SelectExpr(N.getOperand(1));
4234 } else {
4235 Tmp1 = SelectExpr(N.getOperand(1));
4236 Select(N.getOperand(0));
4237 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004238 switch (N.getOperand(1).getValueType()) {
4239 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004240 case MVT::f32:
4241 if (X86ScalarSSE) {
4242 // Spill the value to memory and reload it into top of stack.
4243 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4244 MachineFunction *F = BB->getParent();
4245 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4246 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4247 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4248 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004249 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004250 } else {
4251 assert(0 && "MVT::f32 only legal with scalar sse fp");
4252 abort();
4253 }
4254 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004255 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004256 if (X86ScalarSSE) {
4257 // Spill the value to memory and reload it into top of stack.
4258 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4259 MachineFunction *F = BB->getParent();
4260 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4261 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4262 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4263 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004264 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004265 } else {
4266 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4267 }
4268 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004269 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004270 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4271 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004272 }
4273 break;
4274 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004275 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004276 break;
4277 }
Chris Lattner3648c672005-05-13 21:44:04 +00004278 if (X86Lowering.getBytesToPopOnReturn() == 0)
4279 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4280 else
4281 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004282 return;
4283 case ISD::BR: {
4284 Select(N.getOperand(0));
4285 MachineBasicBlock *Dest =
4286 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4287 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4288 return;
4289 }
4290
4291 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004292 MachineBasicBlock *Dest =
4293 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004294
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004295 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4296 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004297 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4298 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4299 Select(N.getOperand(0));
4300 Tmp1 = SelectExpr(N.getOperand(1));
4301 } else {
4302 Tmp1 = SelectExpr(N.getOperand(1));
4303 Select(N.getOperand(0));
4304 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004305 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4306 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4307 }
Chris Lattner11333092005-01-11 03:11:44 +00004308
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004309 return;
4310 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004311
Chris Lattner4df0de92005-01-17 00:00:33 +00004312 case ISD::LOAD:
4313 // If this load could be folded into the only using instruction, and if it
4314 // is safe to emit the instruction here, try to do so now.
4315 if (Node->hasNUsesOfValue(1, 0)) {
4316 SDOperand TheVal = N.getValue(0);
4317 SDNode *User = 0;
4318 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4319 assert(UI != Node->use_end() && "Didn't find use!");
4320 SDNode *UN = *UI;
4321 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4322 if (UN->getOperand(i) == TheVal) {
4323 User = UN;
4324 goto FoundIt;
4325 }
4326 }
4327 FoundIt:
4328 // Only handle unary operators right now.
4329 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004330 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004331 SelectExpr(SDOperand(User, 0));
4332 return;
4333 }
4334 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004335 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004336 SelectExpr(N);
4337 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004338 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004339 case ISD::EXTLOAD:
4340 case ISD::SEXTLOAD:
4341 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004342 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004343 case X86ISD::TAILCALL:
4344 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004345 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004346 SelectExpr(N);
4347 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004348 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004349 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004350 ExprMap.erase(N);
4351 SelectExpr(N.getValue(0));
4352 return;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004353
Chris Lattner01546c52005-07-30 00:05:54 +00004354 case X86ISD::FP_TO_INT16_IN_MEM:
4355 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattnerf7443da2005-07-29 00:54:34 +00004356 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner745d5382005-07-29 00:40:01 +00004357 assert(N.getOperand(1).getValueType() == MVT::f64);
4358 X86AddressMode AM;
4359 Select(N.getOperand(0)); // Select the token chain
4360
4361 unsigned ValReg;
4362 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4363 ValReg = SelectExpr(N.getOperand(1));
4364 SelectAddress(N.getOperand(2), AM);
4365 } else {
4366 SelectAddress(N.getOperand(2), AM);
4367 ValReg = SelectExpr(N.getOperand(1));
4368 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004369
Chris Lattnerf7443da2005-07-29 00:54:34 +00004370 // Change the floating point control register to use "round towards zero"
4371 // mode when truncating to an integer value.
4372 //
4373 MachineFunction *F = BB->getParent();
4374 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4375 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004376
Chris Lattnerf7443da2005-07-29 00:54:34 +00004377 // Load the old value of the high byte of the control word...
Chris Lattnera35e1df2005-07-30 00:17:52 +00004378 unsigned OldCW = MakeReg(MVT::i16);
4379 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004380
Chris Lattnerf7443da2005-07-29 00:54:34 +00004381 // Set the high part to be round to zero...
Chris Lattnera88da082005-07-30 00:43:00 +00004382 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004383
Chris Lattnerf7443da2005-07-29 00:54:34 +00004384 // Reload the modified control word now...
4385 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004386
Chris Lattnerf7443da2005-07-29 00:54:34 +00004387 // Restore the memory image of control word to original value
Chris Lattnera35e1df2005-07-30 00:17:52 +00004388 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner01546c52005-07-30 00:05:54 +00004389
4390 // Get the X86 opcode to use.
4391 switch (N.getOpcode()) {
4392 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
4393 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
4394 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
4395 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004396
Chris Lattner01546c52005-07-30 00:05:54 +00004397 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004398
Chris Lattnerf7443da2005-07-29 00:54:34 +00004399 // 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}