blob: 47e8179147911a3bc0a0d9b3b0c6b1f1574b1ba7 [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) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001095 Subtarget = TM.getSubtarget<const 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
1666 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1667 if (SetCC == 0)
1668 return true; // Can only handle simple setcc's so far.
1669
1670 unsigned Opc;
1671
1672 // Handle integer conditions first.
1673 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1674 switch (SetCC->getCondition()) {
1675 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 Lattnercb1aa8d2005-01-17 01:34:14 +00001688 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->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 //
1702 switch (SetCC->getCondition()) {
1703 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 Lattnercb1aa8d2005-01-17 01:34:14 +00001745 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->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
1784 SetCCSDNode *SetCC;
1785 if ((SetCC = dyn_cast<SetCCSDNode>(Cond))) {
Chris Lattner24aad1b2005-01-10 22:10:13 +00001786 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1787 switch (SetCC->getCondition()) {
1788 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 //
1808 switch (SetCC->getCondition()) {
1809 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 }
1834 }
1835
Nate Begeman1c73c7b2005-08-03 23:26:28 +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.
Jeff Cohen00b168892005-07-27 06:12:32 +00001840 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001841 if (SetCC && SetCC->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;
1877 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001878 }
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001879 }
1880
1881 // Select the true and false values for use in both the SSE PHI case, and the
1882 // integer or x87 cmov cases below.
1883 if (getRegPressure(True) > getRegPressure(False)) {
1884 RTrue = SelectExpr(True);
1885 RFalse = SelectExpr(False);
1886 } else {
1887 RFalse = SelectExpr(False);
1888 RTrue = SelectExpr(True);
1889 }
1890
1891 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
1892 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
1893 // node to select between the true and false values.
1894 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1895 // FIXME: emit a direct compare and branch rather than setting a cond reg
1896 // and testing it.
1897 unsigned CondReg = SelectExpr(Cond);
1898 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1899
1900 // Create an iterator with which to insert the MBB for copying the false
1901 // value and the MBB to hold the PHI instruction for this SetCC.
1902 MachineBasicBlock *thisMBB = BB;
1903 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1904 ilist<MachineBasicBlock>::iterator It = BB;
1905 ++It;
1906
1907 // thisMBB:
1908 // ...
1909 // TrueVal = ...
1910 // cmpTY ccX, r1, r2
1911 // bCC sinkMBB
1912 // fallthrough --> copy0MBB
1913 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1914 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1915 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1916 MachineFunction *F = BB->getParent();
1917 F->getBasicBlockList().insert(It, copy0MBB);
1918 F->getBasicBlockList().insert(It, sinkMBB);
1919 // Update machine-CFG edges
1920 BB->addSuccessor(copy0MBB);
1921 BB->addSuccessor(sinkMBB);
1922
1923 // copy0MBB:
1924 // %FalseValue = ...
1925 // # fallthrough to sinkMBB
1926 BB = copy0MBB;
1927 // Update machine-CFG edges
1928 BB->addSuccessor(sinkMBB);
1929
1930 // sinkMBB:
1931 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1932 // ...
1933 BB = sinkMBB;
1934 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1935 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001936 return;
1937 }
1938
Chris Lattner24aad1b2005-01-10 22:10:13 +00001939 unsigned Opc = 0;
1940 if (CondCode != NOT_SET) {
1941 switch (SVT) {
1942 default: assert(0 && "Cannot select this type!");
1943 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1944 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001945 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001946 }
1947 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001948
Chris Lattner24aad1b2005-01-10 22:10:13 +00001949 // Finally, if we weren't able to fold this, just emit the condition and test
1950 // it.
1951 if (CondCode == NOT_SET || Opc == 0) {
1952 // Get the condition into the zero flag.
1953 unsigned CondReg = SelectExpr(Cond);
1954 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1955
1956 switch (SVT) {
1957 default: assert(0 && "Cannot select this type!");
1958 case MVT::i16: Opc = X86::CMOVE16rr; break;
1959 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001960 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001961 }
1962 } else {
1963 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001964 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001965 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001966 }
1967 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1968}
1969
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001970void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001971 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001972 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1973 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001974 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001975 switch (RHS.getValueType()) {
1976 default: break;
1977 case MVT::i1:
1978 case MVT::i8: Opc = X86::CMP8mi; break;
1979 case MVT::i16: Opc = X86::CMP16mi; break;
1980 case MVT::i32: Opc = X86::CMP32mi; break;
1981 }
1982 if (Opc) {
1983 X86AddressMode AM;
1984 EmitFoldedLoad(LHS, AM);
1985 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1986 return;
1987 }
1988 }
1989
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001990 switch (RHS.getValueType()) {
1991 default: break;
1992 case MVT::i1:
1993 case MVT::i8: Opc = X86::CMP8ri; break;
1994 case MVT::i16: Opc = X86::CMP16ri; break;
1995 case MVT::i32: Opc = X86::CMP32ri; break;
1996 }
1997 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001998 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001999 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
2000 return;
2001 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002002 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002003 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
2004 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00002005 unsigned Reg = SelectExpr(LHS);
2006 BuildMI(BB, X86::FTST, 1).addReg(Reg);
2007 BuildMI(BB, X86::FNSTSW8r, 0);
2008 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00002009 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00002010 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011 }
2012
Chris Lattneref6806c2005-01-12 02:02:48 +00002013 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00002014 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00002015 switch (RHS.getValueType()) {
2016 default: break;
2017 case MVT::i1:
2018 case MVT::i8: Opc = X86::CMP8mr; break;
2019 case MVT::i16: Opc = X86::CMP16mr; break;
2020 case MVT::i32: Opc = X86::CMP32mr; break;
2021 }
2022 if (Opc) {
2023 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002024 EmitFoldedLoad(LHS, AM);
2025 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00002026 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
2027 return;
2028 }
2029 }
2030
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002031 switch (LHS.getValueType()) {
2032 default: assert(0 && "Cannot compare this value!");
2033 case MVT::i1:
2034 case MVT::i8: Opc = X86::CMP8rr; break;
2035 case MVT::i16: Opc = X86::CMP16rr; break;
2036 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002037 case MVT::f32: Opc = X86::UCOMISSrr; break;
2038 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002039 }
Chris Lattner11333092005-01-11 03:11:44 +00002040 unsigned Tmp1, Tmp2;
2041 if (getRegPressure(LHS) > getRegPressure(RHS)) {
2042 Tmp1 = SelectExpr(LHS);
2043 Tmp2 = SelectExpr(RHS);
2044 } else {
2045 Tmp2 = SelectExpr(RHS);
2046 Tmp1 = SelectExpr(LHS);
2047 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002048 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
2049}
2050
Chris Lattnera5ade062005-01-11 21:19:59 +00002051/// isFoldableLoad - Return true if this is a load instruction that can safely
2052/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00002053bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
2054 if (Op.getOpcode() == ISD::LOAD) {
2055 // FIXME: currently can't fold constant pool indexes.
2056 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2057 return false;
2058 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00002059 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00002060 // FIXME: currently can't fold constant pool indexes.
2061 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2062 return false;
2063 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002064 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00002065 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002066
2067 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00002068 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2069 if (ExprMap.count(Op.getValue(1))) return false;
2070 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00002071 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00002072
Chris Lattner4ff348b2005-01-17 06:26:58 +00002073 // If there is not just one use of its value, we cannot fold.
2074 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2075
2076 // Finally, we cannot fold the load into the operation if this would induce a
2077 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2078 // operand of the operation we are folding the load into) can possible use the
2079 // chain node defined by the load.
2080 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2081 std::set<SDNode*> Visited;
2082 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2083 return false;
2084 }
2085 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00002086}
2087
Chris Lattner4ff348b2005-01-17 06:26:58 +00002088
Chris Lattnera5ade062005-01-11 21:19:59 +00002089/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2090/// and compute the address being loaded into AM.
2091void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2092 SDOperand Chain = Op.getOperand(0);
2093 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002094
Chris Lattnera5ade062005-01-11 21:19:59 +00002095 if (getRegPressure(Chain) > getRegPressure(Address)) {
2096 Select(Chain);
2097 SelectAddress(Address, AM);
2098 } else {
2099 SelectAddress(Address, AM);
2100 Select(Chain);
2101 }
2102
2103 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002104 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2105 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002106 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002107 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002108}
2109
Chris Lattner30ea1e92005-01-19 07:37:26 +00002110// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2111// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2112// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2113// return true.
2114bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002115 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2116 // good!
2117 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2118 std::swap(Op1, Op2); // Op1 is the SHL now.
2119 } else {
2120 return false; // No match
2121 }
2122
2123 SDOperand ShlVal = Op1.getOperand(0);
2124 SDOperand ShlAmt = Op1.getOperand(1);
2125 SDOperand ShrVal = Op2.getOperand(0);
2126 SDOperand ShrAmt = Op2.getOperand(1);
2127
Chris Lattner30ea1e92005-01-19 07:37:26 +00002128 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2129
Chris Lattner85716372005-01-19 06:18:43 +00002130 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2131 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2132 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002133 if (SubCST->getValue() == RegSize) {
2134 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002135 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002136 if (ShrVal == ShlVal) {
2137 unsigned Reg, ShAmt;
2138 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2139 Reg = SelectExpr(ShrVal);
2140 ShAmt = SelectExpr(ShrAmt);
2141 } else {
2142 ShAmt = SelectExpr(ShrAmt);
2143 Reg = SelectExpr(ShrVal);
2144 }
2145 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2146 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2147 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2148 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2149 return true;
2150 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002151 unsigned AReg, BReg;
2152 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002153 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002154 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002155 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002156 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002157 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002158 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002159 unsigned ShAmt = SelectExpr(ShrAmt);
2160 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2161 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2162 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002163 return true;
2164 }
2165 }
2166
Chris Lattner4053b1e2005-01-19 08:07:05 +00002167 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2168 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2169 if (SubCST->getValue() == RegSize) {
2170 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2171 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2172 if (ShrVal == ShlVal) {
2173 unsigned Reg, ShAmt;
2174 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2175 Reg = SelectExpr(ShrVal);
2176 ShAmt = SelectExpr(ShlAmt);
2177 } else {
2178 ShAmt = SelectExpr(ShlAmt);
2179 Reg = SelectExpr(ShrVal);
2180 }
2181 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2182 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2183 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2184 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2185 return true;
2186 } else if (RegSize != 8) {
2187 unsigned AReg, BReg;
2188 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002189 AReg = SelectExpr(ShlVal);
2190 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002191 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002192 BReg = SelectExpr(ShrVal);
2193 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002194 }
2195 unsigned ShAmt = SelectExpr(ShlAmt);
2196 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2197 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2198 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2199 return true;
2200 }
2201 }
Chris Lattner85716372005-01-19 06:18:43 +00002202
Chris Lattner4053b1e2005-01-19 08:07:05 +00002203 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2204 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2205 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2206 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2207 // (A >> 5) | (A << 27) --> ROR A, 5
2208 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2209 if (ShrVal == ShlVal) {
2210 unsigned Reg = SelectExpr(ShrVal);
2211 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2212 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2213 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2214 return true;
2215 } else if (RegSize != 8) {
2216 unsigned AReg, BReg;
2217 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002218 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002219 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002220 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002221 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002222 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002223 }
2224 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2225 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2226 .addImm(ShrCst->getValue());
2227 return true;
2228 }
2229 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002230
Chris Lattner85716372005-01-19 06:18:43 +00002231 return false;
2232}
2233
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234unsigned ISel::SelectExpr(SDOperand N) {
2235 unsigned Result;
2236 unsigned Tmp1, Tmp2, Tmp3;
2237 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002238 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002239 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002240
Chris Lattner7f2afac2005-01-14 22:37:41 +00002241 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002242 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2243 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2244 // Just use the specified register as our input.
2245 return cast<RegSDNode>(Node)->getReg();
2246 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002247 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002248
Chris Lattnera5ade062005-01-11 21:19:59 +00002249 unsigned &Reg = ExprMap[N];
2250 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002251
Chris Lattnerb38a7492005-04-02 04:01:14 +00002252 switch (N.getOpcode()) {
2253 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002254 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002255 MakeReg(N.getValueType()) : 1;
2256 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002257 case X86ISD::TAILCALL:
2258 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002259 // If this is a call instruction, make sure to prepare ALL of the result
2260 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002261 ExprMap[N.getValue(0)] = 1;
2262 if (Node->getNumValues() > 1) {
2263 Result = MakeReg(Node->getValueType(1));
2264 ExprMap[N.getValue(1)] = Result;
2265 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002266 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002267 } else {
2268 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002269 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002270 break;
2271 case ISD::ADD_PARTS:
2272 case ISD::SUB_PARTS:
2273 case ISD::SHL_PARTS:
2274 case ISD::SRL_PARTS:
2275 case ISD::SRA_PARTS:
2276 Result = MakeReg(Node->getValueType(0));
2277 ExprMap[N.getValue(0)] = Result;
2278 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2279 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2280 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002281 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002282
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002283 switch (N.getOpcode()) {
2284 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002285 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002286 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002287 case ISD::FP_EXTEND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002288 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002289 Tmp1 = SelectExpr(N.getOperand(0));
2290 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2291 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002292 case ISD::FP_ROUND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002293 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman16b04f32005-07-15 00:38:55 +00002294 Tmp1 = SelectExpr(N.getOperand(0));
2295 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2296 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002297 case ISD::CopyFromReg:
2298 Select(N.getOperand(0));
2299 if (Result == 1) {
2300 Reg = Result = ExprMap[N.getValue(0)] =
2301 MakeReg(N.getValue(0).getValueType());
2302 }
2303 switch (Node->getValueType(0)) {
2304 default: assert(0 && "Cannot CopyFromReg this!");
2305 case MVT::i1:
2306 case MVT::i8:
2307 BuildMI(BB, X86::MOV8rr, 1,
2308 Result).addReg(cast<RegSDNode>(Node)->getReg());
2309 return Result;
2310 case MVT::i16:
2311 BuildMI(BB, X86::MOV16rr, 1,
2312 Result).addReg(cast<RegSDNode>(Node)->getReg());
2313 return Result;
2314 case MVT::i32:
2315 BuildMI(BB, X86::MOV32rr, 1,
2316 Result).addReg(cast<RegSDNode>(Node)->getReg());
2317 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002318 }
Chris Lattnerc6f41812005-05-12 23:06:28 +00002319
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002320 case ISD::FrameIndex:
2321 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2322 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2323 return Result;
2324 case ISD::ConstantPool:
2325 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2326 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2327 return Result;
2328 case ISD::ConstantFP:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00002329 if (X86ScalarSSE) {
2330 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
2331 "SSE only supports +0.0");
2332 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
2333 BuildMI(BB, Opc, 0, Result);
2334 return Result;
2335 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002336 ContainsFPCode = true;
2337 Tmp1 = Result; // Intermediate Register
2338 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2339 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2340 Tmp1 = MakeReg(MVT::f64);
2341
2342 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2343 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2344 BuildMI(BB, X86::FLD0, 0, Tmp1);
2345 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2346 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2347 BuildMI(BB, X86::FLD1, 0, Tmp1);
2348 else
2349 assert(0 && "Unexpected constant!");
2350 if (Tmp1 != Result)
2351 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2352 return Result;
2353 case ISD::Constant:
2354 switch (N.getValueType()) {
2355 default: assert(0 && "Cannot use constants of this type!");
2356 case MVT::i1:
2357 case MVT::i8: Opc = X86::MOV8ri; break;
2358 case MVT::i16: Opc = X86::MOV16ri; break;
2359 case MVT::i32: Opc = X86::MOV32ri; break;
2360 }
2361 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2362 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002363 case ISD::UNDEF:
2364 if (Node->getValueType(0) == MVT::f64) {
2365 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2366 BuildMI(BB, X86::FLD0, 0, Result);
2367 } else {
2368 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2369 }
2370 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002371 case ISD::GlobalAddress: {
2372 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002373 // For Darwin, external and weak symbols are indirect, so we want to load
2374 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00002375 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00002376 (GV->hasWeakLinkage() || GV->isExternal())) {
2377 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2378 .addGlobalAddress(GV, false, 0);
2379 } else {
2380 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2381 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 return Result;
2383 }
2384 case ISD::ExternalSymbol: {
2385 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2386 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2387 return Result;
2388 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002389 case ISD::ZERO_EXTEND: {
2390 int DestIs16 = N.getValueType() == MVT::i16;
2391 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002392
2393 // FIXME: This hack is here for zero extension casts from bool to i8. This
2394 // would not be needed if bools were promoted by Legalize.
2395 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002396 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002397 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2398 return Result;
2399 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002400
Chris Lattner4ff348b2005-01-17 06:26:58 +00002401 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002402 static const unsigned Opc[3] = {
2403 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2404 };
2405
2406 X86AddressMode AM;
2407 EmitFoldedLoad(N.getOperand(0), AM);
2408 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002409
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002410 return Result;
2411 }
2412
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002413 static const unsigned Opc[3] = {
2414 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2415 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002416 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002417 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2418 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002419 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002420 case ISD::SIGN_EXTEND: {
2421 int DestIs16 = N.getValueType() == MVT::i16;
2422 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2423
Chris Lattner590d8002005-01-09 18:52:44 +00002424 // FIXME: Legalize should promote bools to i8!
2425 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2426 "Sign extend from bool not implemented!");
2427
Chris Lattner4ff348b2005-01-17 06:26:58 +00002428 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002429 static const unsigned Opc[3] = {
2430 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2431 };
2432
2433 X86AddressMode AM;
2434 EmitFoldedLoad(N.getOperand(0), AM);
2435 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2436 return Result;
2437 }
2438
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002439 static const unsigned Opc[3] = {
2440 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2441 };
2442 Tmp1 = SelectExpr(N.getOperand(0));
2443 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2444 return Result;
2445 }
2446 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002447 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002448 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002449 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002450 switch (N.getValueType()) {
2451 default: assert(0 && "Unknown truncate!");
2452 case MVT::i1:
2453 case MVT::i8: Opc = X86::MOV8rm; break;
2454 case MVT::i16: Opc = X86::MOV16rm; break;
2455 }
2456 X86AddressMode AM;
2457 EmitFoldedLoad(N.getOperand(0), AM);
2458 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2459 return Result;
2460 }
2461
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002462 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2463 // a move out of AX or AL.
2464 switch (N.getOperand(0).getValueType()) {
2465 default: assert(0 && "Unknown truncate!");
2466 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2467 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2468 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2469 }
2470 Tmp1 = SelectExpr(N.getOperand(0));
2471 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2472
2473 switch (N.getValueType()) {
2474 default: assert(0 && "Unknown truncate!");
2475 case MVT::i1:
2476 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2477 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2478 }
2479 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2480 return Result;
2481
Chris Lattnera28381c2005-07-16 00:28:20 +00002482 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002483 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2484 unsigned PromoteOpcode = 0;
2485
Nate Begeman5a8441e2005-07-16 02:02:34 +00002486 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002487 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002488 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002489 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2490 return Result;
2491 }
Jeff Cohen00b168892005-07-27 06:12:32 +00002492
Chris Lattneref7ba072005-01-11 03:50:45 +00002493 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002494
Chris Lattner590d8002005-01-09 18:52:44 +00002495 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002496 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002497 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2498 MachineFunction *F = BB->getParent();
2499 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2500
2501 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002502 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002503 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002504 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2505 break;
2506 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002507 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002508 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2509 break;
2510 default: break; // No promotion required.
2511 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002512 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002513 }
Chris Lattner01546c52005-07-30 00:05:54 +00002514 case ISD::FP_TO_SINT:
Chris Lattner590d8002005-01-09 18:52:44 +00002515 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2516
Nate Begemanf63be7d2005-07-06 18:59:04 +00002517 // If the target supports SSE2 and is performing FP operations in SSE regs
2518 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2519 // CVTSD2SI instructions.
Chris Lattner01546c52005-07-30 00:05:54 +00002520 assert(X86ScalarSSE);
2521 if (MVT::f32 == N.getOperand(0).getValueType()) {
2522 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
2523 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2524 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
2525 } else {
2526 assert(0 && "Not an f32 or f64?");
2527 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +00002528 }
Chris Lattner590d8002005-01-09 18:52:44 +00002529 return Result;
Chris Lattner01546c52005-07-30 00:05:54 +00002530
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002531 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002532 Op0 = N.getOperand(0);
2533 Op1 = N.getOperand(1);
2534
Chris Lattner44129b52005-01-25 20:03:11 +00002535 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002536 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002537 goto FoldAdd;
2538 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002539
Chris Lattner44129b52005-01-25 20:03:11 +00002540 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002541 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002542 switch (N.getValueType()) {
2543 default: assert(0 && "Cannot add this type!");
2544 case MVT::i1:
2545 case MVT::i8: Opc = X86::ADD8rm; break;
2546 case MVT::i16: Opc = X86::ADD16rm; break;
2547 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002548 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002549 case MVT::f64:
2550 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002551 if (X86ScalarSSE) {
2552 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2553 Opc = X86::ADDSDrm;
2554 } else {
2555 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2556 }
Chris Lattner44129b52005-01-25 20:03:11 +00002557 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002558 }
2559 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002560 EmitFoldedLoad(Op1, AM);
2561 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002562 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2563 return Result;
2564 }
2565
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002566 // See if we can codegen this as an LEA to fold operations together.
2567 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002568 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002569 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002570 MatchAddress(N, AM);
2571 ExprMap[N] = Result;
2572
2573 // If this is not just an add, emit the LEA. For a simple add (like
2574 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2575 // leave this as LEA, then peephole it to 'ADD' after two address elim
2576 // happens.
2577 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2578 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2579 X86AddressMode XAM = SelectAddrExprs(AM);
2580 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2581 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002582 }
2583 }
Chris Lattner11333092005-01-11 03:11:44 +00002584
Chris Lattnera5ade062005-01-11 21:19:59 +00002585 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002586 Opc = 0;
2587 if (CN->getValue() == 1) { // add X, 1 -> inc X
2588 switch (N.getValueType()) {
2589 default: assert(0 && "Cannot integer add this type!");
2590 case MVT::i8: Opc = X86::INC8r; break;
2591 case MVT::i16: Opc = X86::INC16r; break;
2592 case MVT::i32: Opc = X86::INC32r; break;
2593 }
2594 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2595 switch (N.getValueType()) {
2596 default: assert(0 && "Cannot integer add this type!");
2597 case MVT::i8: Opc = X86::DEC8r; break;
2598 case MVT::i16: Opc = X86::DEC16r; break;
2599 case MVT::i32: Opc = X86::DEC32r; break;
2600 }
2601 }
2602
2603 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002604 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002605 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2606 return Result;
2607 }
2608
2609 switch (N.getValueType()) {
2610 default: assert(0 && "Cannot add this type!");
2611 case MVT::i8: Opc = X86::ADD8ri; break;
2612 case MVT::i16: Opc = X86::ADD16ri; break;
2613 case MVT::i32: Opc = X86::ADD32ri; break;
2614 }
2615 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002616 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002617 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2618 return Result;
2619 }
2620 }
2621
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622 switch (N.getValueType()) {
2623 default: assert(0 && "Cannot add this type!");
2624 case MVT::i8: Opc = X86::ADD8rr; break;
2625 case MVT::i16: Opc = X86::ADD16rr; break;
2626 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002627 case MVT::f32: Opc = X86::ADDSSrr; break;
2628 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002629 }
Chris Lattner11333092005-01-11 03:11:44 +00002630
Chris Lattnera5ade062005-01-11 21:19:59 +00002631 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2632 Tmp1 = SelectExpr(Op0);
2633 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002634 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002635 Tmp2 = SelectExpr(Op1);
2636 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002637 }
2638
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2640 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002641
Nate Begemanf63be7d2005-07-06 18:59:04 +00002642 case ISD::FSQRT:
2643 Tmp1 = SelectExpr(Node->getOperand(0));
2644 if (X86ScalarSSE) {
2645 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2646 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2647 } else {
2648 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2649 }
2650 return Result;
2651
2652 // FIXME:
2653 // Once we can spill 16 byte constants into the constant pool, we can
2654 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002655 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002656 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002657 case ISD::FSIN:
2658 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002659 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002660 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002661 switch (N.getOpcode()) {
2662 default: assert(0 && "Unreachable!");
2663 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2664 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002665 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2666 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002667 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002668 return Result;
2669
Chris Lattner8db0af12005-04-06 04:21:07 +00002670 case ISD::MULHU:
2671 switch (N.getValueType()) {
2672 default: assert(0 && "Unsupported VT!");
2673 case MVT::i8: Tmp2 = X86::MUL8r; break;
2674 case MVT::i16: Tmp2 = X86::MUL16r; break;
2675 case MVT::i32: Tmp2 = X86::MUL32r; break;
2676 }
2677 // FALL THROUGH
2678 case ISD::MULHS: {
2679 unsigned MovOpc, LowReg, HiReg;
2680 switch (N.getValueType()) {
2681 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002682 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002683 MovOpc = X86::MOV8rr;
2684 LowReg = X86::AL;
2685 HiReg = X86::AH;
2686 Opc = X86::IMUL8r;
2687 break;
2688 case MVT::i16:
2689 MovOpc = X86::MOV16rr;
2690 LowReg = X86::AX;
2691 HiReg = X86::DX;
2692 Opc = X86::IMUL16r;
2693 break;
2694 case MVT::i32:
2695 MovOpc = X86::MOV32rr;
2696 LowReg = X86::EAX;
2697 HiReg = X86::EDX;
2698 Opc = X86::IMUL32r;
2699 break;
2700 }
2701 if (Node->getOpcode() != ISD::MULHS)
2702 Opc = Tmp2; // Get the MULHU opcode.
2703
2704 Op0 = Node->getOperand(0);
2705 Op1 = Node->getOperand(1);
2706 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2707 Tmp1 = SelectExpr(Op0);
2708 Tmp2 = SelectExpr(Op1);
2709 } else {
2710 Tmp2 = SelectExpr(Op1);
2711 Tmp1 = SelectExpr(Op0);
2712 }
2713
2714 // FIXME: Implement folding of loads into the memory operands here!
2715 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2716 BuildMI(BB, Opc, 1).addReg(Tmp2);
2717 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2718 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002719 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002720
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002721 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002722 case ISD::MUL:
2723 case ISD::AND:
2724 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002725 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002726 static const unsigned SUBTab[] = {
2727 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2728 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2729 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2730 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002731 static const unsigned SSE_SUBTab[] = {
2732 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2733 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2734 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2735 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002736 static const unsigned MULTab[] = {
2737 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2738 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2739 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2740 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002741 static const unsigned SSE_MULTab[] = {
2742 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2743 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2744 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2745 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002746 static const unsigned ANDTab[] = {
2747 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2748 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002749 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002750 };
2751 static const unsigned ORTab[] = {
2752 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2753 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2754 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2755 };
2756 static const unsigned XORTab[] = {
2757 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2758 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2759 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2760 };
2761
2762 Op0 = Node->getOperand(0);
2763 Op1 = Node->getOperand(1);
2764
Chris Lattner30ea1e92005-01-19 07:37:26 +00002765 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2766 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002767 return Result;
2768
2769 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002770 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2771 if (CN->isNullValue()) { // 0 - N -> neg N
2772 switch (N.getValueType()) {
2773 default: assert(0 && "Cannot sub this type!");
2774 case MVT::i1:
2775 case MVT::i8: Opc = X86::NEG8r; break;
2776 case MVT::i16: Opc = X86::NEG16r; break;
2777 case MVT::i32: Opc = X86::NEG32r; break;
2778 }
2779 Tmp1 = SelectExpr(N.getOperand(1));
2780 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2781 return Result;
2782 }
2783
Chris Lattnera5ade062005-01-11 21:19:59 +00002784 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2785 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002786 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002787 switch (N.getValueType()) {
2788 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002789 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002790 case MVT::i8: Opc = X86::NOT8r; break;
2791 case MVT::i16: Opc = X86::NOT16r; break;
2792 case MVT::i32: Opc = X86::NOT32r; break;
2793 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002794 if (Opc) {
2795 Tmp1 = SelectExpr(Op0);
2796 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2797 return Result;
2798 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002799 }
2800
Chris Lattner2a4e5082005-01-17 06:48:02 +00002801 // Fold common multiplies into LEA instructions.
2802 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2803 switch ((int)CN->getValue()) {
2804 default: break;
2805 case 3:
2806 case 5:
2807 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002808 // Remove N from exprmap so SelectAddress doesn't get confused.
2809 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002810 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002811 SelectAddress(N, AM);
2812 // Restore it to the map.
2813 ExprMap[N] = Result;
2814 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2815 return Result;
2816 }
2817 }
2818
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002819 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002820 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002821 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002822 case MVT::i8: Opc = 0; break;
2823 case MVT::i16: Opc = 1; break;
2824 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002825 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002826 switch (Node->getOpcode()) {
2827 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002828 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2829 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002830 case ISD::AND: Opc = ANDTab[Opc]; break;
2831 case ISD::OR: Opc = ORTab[Opc]; break;
2832 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002833 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002834 if (Opc) { // Can't fold MUL:i8 R, imm
2835 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2837 return Result;
2838 }
2839 }
Chris Lattner11333092005-01-11 03:11:44 +00002840
Chris Lattner44129b52005-01-25 20:03:11 +00002841 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002842 if (Node->getOpcode() != ISD::SUB) {
2843 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002844 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002845 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002846 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002847 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002848 if (Op0.getOpcode() == ISD::EXTLOAD)
2849 Opc = X86::FSUBR32m;
2850 else
2851 Opc = X86::FSUBR64m;
2852
Chris Lattnera5ade062005-01-11 21:19:59 +00002853 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002854 EmitFoldedLoad(Op0, AM);
2855 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002856 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2857 return Result;
2858 }
2859 }
2860
Chris Lattner44129b52005-01-25 20:03:11 +00002861 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002862 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002863 switch (N.getValueType()) {
2864 default: assert(0 && "Cannot operate on this type!");
2865 case MVT::i1:
2866 case MVT::i8: Opc = 5; break;
2867 case MVT::i16: Opc = 6; break;
2868 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002869 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002870 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen00b168892005-07-27 06:12:32 +00002871 case MVT::f64:
2872 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begemanf63be7d2005-07-06 18:59:04 +00002873 "SSE load should have been promoted");
2874 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002875 }
2876 switch (Node->getOpcode()) {
2877 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002878 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2879 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002880 case ISD::AND: Opc = ANDTab[Opc]; break;
2881 case ISD::OR: Opc = ORTab[Opc]; break;
2882 case ISD::XOR: Opc = XORTab[Opc]; break;
2883 }
2884
2885 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002886 EmitFoldedLoad(Op1, AM);
2887 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002888 if (Opc) {
2889 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2890 } else {
2891 assert(Node->getOpcode() == ISD::MUL &&
2892 N.getValueType() == MVT::i8 && "Unexpected situation!");
2893 // Must use the MUL instruction, which forces use of AL.
2894 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2895 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2896 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2897 }
2898 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002899 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002900
2901 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2902 Tmp1 = SelectExpr(Op0);
2903 Tmp2 = SelectExpr(Op1);
2904 } else {
2905 Tmp2 = SelectExpr(Op1);
2906 Tmp1 = SelectExpr(Op0);
2907 }
2908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002909 switch (N.getValueType()) {
2910 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002911 case MVT::i1:
2912 case MVT::i8: Opc = 10; break;
2913 case MVT::i16: Opc = 11; break;
2914 case MVT::i32: Opc = 12; break;
2915 case MVT::f32: Opc = 13; break;
2916 case MVT::f64: Opc = 14; break;
2917 }
2918 switch (Node->getOpcode()) {
2919 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002920 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2921 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002922 case ISD::AND: Opc = ANDTab[Opc]; break;
2923 case ISD::OR: Opc = ORTab[Opc]; break;
2924 case ISD::XOR: Opc = XORTab[Opc]; break;
2925 }
2926 if (Opc) {
2927 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2928 } else {
2929 assert(Node->getOpcode() == ISD::MUL &&
2930 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002931 // Must use the MUL instruction, which forces use of AL.
2932 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2933 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2934 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002935 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002936 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002937 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002938 case ISD::ADD_PARTS:
2939 case ISD::SUB_PARTS: {
2940 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2941 "Not an i64 add/sub!");
2942 // Emit all of the operands.
2943 std::vector<unsigned> InVals;
2944 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2945 InVals.push_back(SelectExpr(N.getOperand(i)));
2946 if (N.getOpcode() == ISD::ADD_PARTS) {
2947 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2948 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2949 } else {
2950 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2951 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2952 }
2953 return Result+N.ResNo;
2954 }
2955
Chris Lattnerb38a7492005-04-02 04:01:14 +00002956 case ISD::SHL_PARTS:
2957 case ISD::SRA_PARTS:
2958 case ISD::SRL_PARTS: {
2959 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2960 "Not an i64 shift!");
2961 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2962 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2963 unsigned TmpReg = MakeReg(MVT::i32);
2964 if (N.getOpcode() == ISD::SRA_PARTS) {
2965 // If this is a SHR of a Long, then we need to do funny sign extension
2966 // stuff. TmpReg gets the value to use as the high-part if we are
2967 // shifting more than 32 bits.
2968 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2969 } else {
2970 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2971 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2972 }
2973
2974 // Initialize CL with the shift amount.
2975 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2976 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2977
2978 unsigned TmpReg2 = MakeReg(MVT::i32);
2979 unsigned TmpReg3 = MakeReg(MVT::i32);
2980 if (N.getOpcode() == ISD::SHL_PARTS) {
2981 // TmpReg2 = shld inHi, inLo
2982 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2983 .addReg(ShiftOpLo);
2984 // TmpReg3 = shl inLo, CL
2985 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002986
Chris Lattnerb38a7492005-04-02 04:01:14 +00002987 // Set the flags to indicate whether the shift was by more than 32 bits.
2988 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002989
Chris Lattnerb38a7492005-04-02 04:01:14 +00002990 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002991 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002992 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2993 // DestLo = (>32) ? TmpReg : TmpReg3;
2994 BuildMI(BB, X86::CMOVNE32rr, 2,
2995 Result).addReg(TmpReg3).addReg(TmpReg);
2996 } else {
2997 // TmpReg2 = shrd inLo, inHi
2998 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2999 .addReg(ShiftOpHi);
3000 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003001 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00003002 : X86::SHR32rCL, 1, TmpReg3)
3003 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003004
Chris Lattnerb38a7492005-04-02 04:01:14 +00003005 // Set the flags to indicate whether the shift was by more than 32 bits.
3006 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003007
Chris Lattnerb38a7492005-04-02 04:01:14 +00003008 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003009 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003010 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003011
Chris Lattnerb38a7492005-04-02 04:01:14 +00003012 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003013 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003014 Result+1).addReg(TmpReg3).addReg(TmpReg);
3015 }
3016 return Result+N.ResNo;
3017 }
3018
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003019 case ISD::SELECT:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00003020 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
3021 N.getValueType(), Result);
Chris Lattnerda2ce112005-01-16 07:34:08 +00003022 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003023
3024 case ISD::SDIV:
3025 case ISD::UDIV:
3026 case ISD::SREM:
3027 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003028 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3029 "We don't support this operator!");
3030
Chris Lattner5bf26862005-04-13 03:29:53 +00003031 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003032 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003033 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003034 // Check for reversed and unreversed DIV.
3035 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3036 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3037 Opc = X86::FDIVR32m;
3038 else
3039 Opc = X86::FDIVR64m;
3040 X86AddressMode AM;
3041 EmitFoldedLoad(N.getOperand(0), AM);
3042 Tmp1 = SelectExpr(N.getOperand(1));
3043 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3044 return Result;
3045 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3046 N.getOperand(1).getOpcode() == ISD::LOAD) {
3047 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3048 Opc = X86::FDIV32m;
3049 else
3050 Opc = X86::FDIV64m;
3051 X86AddressMode AM;
3052 EmitFoldedLoad(N.getOperand(1), AM);
3053 Tmp1 = SelectExpr(N.getOperand(0));
3054 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3055 return Result;
3056 }
3057 }
3058
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003059 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3060 // FIXME: These special cases should be handled by the lowering impl!
3061 unsigned RHS = CN->getValue();
3062 bool isNeg = false;
3063 if ((int)RHS < 0) {
3064 isNeg = true;
3065 RHS = -RHS;
3066 }
3067 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
Chris Lattner0561b3f2005-08-02 19:26:06 +00003068 unsigned Log = Log2_32(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003069 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3070 switch (N.getValueType()) {
3071 default: assert("Unknown type to signed divide!");
3072 case MVT::i8:
3073 SAROpc = X86::SAR8ri;
3074 SHROpc = X86::SHR8ri;
3075 ADDOpc = X86::ADD8rr;
3076 NEGOpc = X86::NEG8r;
3077 break;
3078 case MVT::i16:
3079 SAROpc = X86::SAR16ri;
3080 SHROpc = X86::SHR16ri;
3081 ADDOpc = X86::ADD16rr;
3082 NEGOpc = X86::NEG16r;
3083 break;
3084 case MVT::i32:
3085 SAROpc = X86::SAR32ri;
3086 SHROpc = X86::SHR32ri;
3087 ADDOpc = X86::ADD32rr;
3088 NEGOpc = X86::NEG32r;
3089 break;
3090 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003091 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003092 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003093 unsigned TmpReg;
3094 if (Log != 1) {
3095 TmpReg = MakeReg(N.getValueType());
3096 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3097 } else {
3098 TmpReg = Tmp1;
3099 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003100 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003101 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003102 unsigned TmpReg3 = MakeReg(N.getValueType());
3103 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003104
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003105 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3106 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3107 if (isNeg)
3108 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3109 return Result;
3110 }
3111 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003112 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003113
Chris Lattner11333092005-01-11 03:11:44 +00003114 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3115 Tmp1 = SelectExpr(N.getOperand(0));
3116 Tmp2 = SelectExpr(N.getOperand(1));
3117 } else {
3118 Tmp2 = SelectExpr(N.getOperand(1));
3119 Tmp1 = SelectExpr(N.getOperand(0));
3120 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003121
3122 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3123 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3124 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3125 switch (N.getValueType()) {
3126 default: assert(0 && "Cannot sdiv this type!");
3127 case MVT::i8:
3128 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3129 LoReg = X86::AL;
3130 HiReg = X86::AH;
3131 MovOpcode = X86::MOV8rr;
3132 ClrOpcode = X86::MOV8ri;
3133 SExtOpcode = X86::CBW;
3134 break;
3135 case MVT::i16:
3136 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3137 LoReg = X86::AX;
3138 HiReg = X86::DX;
3139 MovOpcode = X86::MOV16rr;
3140 ClrOpcode = X86::MOV16ri;
3141 SExtOpcode = X86::CWD;
3142 break;
3143 case MVT::i32:
3144 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003145 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003146 HiReg = X86::EDX;
3147 MovOpcode = X86::MOV32rr;
3148 ClrOpcode = X86::MOV32ri;
3149 SExtOpcode = X86::CDQ;
3150 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003151 case MVT::f32:
3152 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3153 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003155 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3156 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003157 return Result;
3158 }
3159
3160 // Set up the low part.
3161 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3162
3163 if (isSigned) {
3164 // Sign extend the low part into the high part.
3165 BuildMI(BB, SExtOpcode, 0);
3166 } else {
3167 // Zero out the high part, effectively zero extending the input.
3168 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3169 }
3170
3171 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003172 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003173
3174 // Get the result of the divide or rem.
3175 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3176 return Result;
3177 }
3178
3179 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003180 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003181 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3182 switch (N.getValueType()) {
3183 default: assert(0 && "Cannot shift this type!");
3184 case MVT::i8: Opc = X86::ADD8rr; break;
3185 case MVT::i16: Opc = X86::ADD16rr; break;
3186 case MVT::i32: Opc = X86::ADD32rr; break;
3187 }
3188 Tmp1 = SelectExpr(N.getOperand(0));
3189 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3190 return Result;
3191 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003192
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003193 switch (N.getValueType()) {
3194 default: assert(0 && "Cannot shift this type!");
3195 case MVT::i8: Opc = X86::SHL8ri; break;
3196 case MVT::i16: Opc = X86::SHL16ri; break;
3197 case MVT::i32: Opc = X86::SHL32ri; break;
3198 }
Chris Lattner11333092005-01-11 03:11:44 +00003199 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003200 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3201 return Result;
3202 }
Chris Lattner11333092005-01-11 03:11:44 +00003203
3204 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3205 Tmp1 = SelectExpr(N.getOperand(0));
3206 Tmp2 = SelectExpr(N.getOperand(1));
3207 } else {
3208 Tmp2 = SelectExpr(N.getOperand(1));
3209 Tmp1 = SelectExpr(N.getOperand(0));
3210 }
3211
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003212 switch (N.getValueType()) {
3213 default: assert(0 && "Cannot shift this type!");
3214 case MVT::i8 : Opc = X86::SHL8rCL; break;
3215 case MVT::i16: Opc = X86::SHL16rCL; break;
3216 case MVT::i32: Opc = X86::SHL32rCL; break;
3217 }
3218 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3219 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3220 return Result;
3221 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003222 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3223 switch (N.getValueType()) {
3224 default: assert(0 && "Cannot shift this type!");
3225 case MVT::i8: Opc = X86::SHR8ri; break;
3226 case MVT::i16: Opc = X86::SHR16ri; break;
3227 case MVT::i32: Opc = X86::SHR32ri; break;
3228 }
Chris Lattner11333092005-01-11 03:11:44 +00003229 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003230 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3231 return Result;
3232 }
Chris Lattner11333092005-01-11 03:11:44 +00003233
3234 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3235 Tmp1 = SelectExpr(N.getOperand(0));
3236 Tmp2 = SelectExpr(N.getOperand(1));
3237 } else {
3238 Tmp2 = SelectExpr(N.getOperand(1));
3239 Tmp1 = SelectExpr(N.getOperand(0));
3240 }
3241
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003242 switch (N.getValueType()) {
3243 default: assert(0 && "Cannot shift this type!");
3244 case MVT::i8 : Opc = X86::SHR8rCL; break;
3245 case MVT::i16: Opc = X86::SHR16rCL; break;
3246 case MVT::i32: Opc = X86::SHR32rCL; break;
3247 }
3248 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3249 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3250 return Result;
3251 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003252 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3253 switch (N.getValueType()) {
3254 default: assert(0 && "Cannot shift this type!");
3255 case MVT::i8: Opc = X86::SAR8ri; break;
3256 case MVT::i16: Opc = X86::SAR16ri; break;
3257 case MVT::i32: Opc = X86::SAR32ri; break;
3258 }
Chris Lattner11333092005-01-11 03:11:44 +00003259 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003260 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3261 return Result;
3262 }
Chris Lattner11333092005-01-11 03:11:44 +00003263
3264 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3265 Tmp1 = SelectExpr(N.getOperand(0));
3266 Tmp2 = SelectExpr(N.getOperand(1));
3267 } else {
3268 Tmp2 = SelectExpr(N.getOperand(1));
3269 Tmp1 = SelectExpr(N.getOperand(0));
3270 }
3271
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003272 switch (N.getValueType()) {
3273 default: assert(0 && "Cannot shift this type!");
3274 case MVT::i8 : Opc = X86::SAR8rCL; break;
3275 case MVT::i16: Opc = X86::SAR16rCL; break;
3276 case MVT::i32: Opc = X86::SAR32rCL; break;
3277 }
3278 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3279 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3280 return Result;
3281
3282 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003283 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003284 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3285 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3286 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003287 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003288 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003289 if (Result != 1) { // Generate the token
3290 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3291 assert(0 && "Load already emitted!?");
3292 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003293 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3294
Chris Lattner5188ad72005-01-08 19:28:19 +00003295 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003296 default: assert(0 && "Cannot load this type!");
3297 case MVT::i1:
3298 case MVT::i8: Opc = X86::MOV8rm; break;
3299 case MVT::i16: Opc = X86::MOV16rm; break;
3300 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003301 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003302 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003303 if (X86ScalarSSE) {
3304 Opc = X86::MOVSDrm;
3305 } else {
3306 Opc = X86::FLD64m;
Jeff Cohen00b168892005-07-27 06:12:32 +00003307 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003308 }
3309 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003310 }
Chris Lattner11333092005-01-11 03:11:44 +00003311
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003312 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003313 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003314 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3315 } else {
3316 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003317
3318 SDOperand Chain = N.getOperand(0);
3319 SDOperand Address = N.getOperand(1);
3320 if (getRegPressure(Chain) > getRegPressure(Address)) {
3321 Select(Chain);
3322 SelectAddress(Address, AM);
3323 } else {
3324 SelectAddress(Address, AM);
3325 Select(Chain);
3326 }
3327
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003328 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3329 }
3330 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003331 case X86ISD::FILD64m:
3332 // Make sure we generate both values.
3333 assert(Result != 1 && N.getValueType() == MVT::f64);
3334 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3335 assert(0 && "Load already emitted!?");
3336
3337 {
3338 X86AddressMode AM;
3339
3340 SDOperand Chain = N.getOperand(0);
3341 SDOperand Address = N.getOperand(1);
3342 if (getRegPressure(Chain) > getRegPressure(Address)) {
3343 Select(Chain);
3344 SelectAddress(Address, AM);
3345 } else {
3346 SelectAddress(Address, AM);
3347 Select(Chain);
3348 }
Chris Lattner745d5382005-07-29 00:40:01 +00003349
3350 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattner67649df2005-05-14 06:52:07 +00003351 }
3352 return Result;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003353
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003354 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3355 case ISD::ZEXTLOAD: {
3356 // Make sure we generate both values.
3357 if (Result != 1)
3358 ExprMap[N.getValue(1)] = 1; // Generate the token
3359 else
3360 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3361
Chris Lattnerda2ce112005-01-16 07:34:08 +00003362 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3363 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003364 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003365 "Bad EXTLOAD!");
3366 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3367 CP->getIndex());
3368 return Result;
3369 }
3370
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003371 X86AddressMode AM;
3372 if (getRegPressure(Node->getOperand(0)) >
3373 getRegPressure(Node->getOperand(1))) {
3374 Select(Node->getOperand(0)); // chain
3375 SelectAddress(Node->getOperand(1), AM);
3376 } else {
3377 SelectAddress(Node->getOperand(1), AM);
3378 Select(Node->getOperand(0)); // chain
3379 }
3380
3381 switch (Node->getValueType(0)) {
3382 default: assert(0 && "Unknown type to sign extend to.");
3383 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003384 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003385 "Bad EXTLOAD!");
3386 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3387 break;
3388 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003389 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003390 default:
3391 assert(0 && "Bad zero extend!");
3392 case MVT::i1:
3393 case MVT::i8:
3394 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3395 break;
3396 case MVT::i16:
3397 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3398 break;
3399 }
3400 break;
3401 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003402 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003403 "Bad zero extend!");
3404 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3405 break;
3406 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003407 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003408 "Bad zero extend!");
3409 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3410 break;
3411 }
3412 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003413 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003414 case ISD::SEXTLOAD: {
3415 // Make sure we generate both values.
3416 if (Result != 1)
3417 ExprMap[N.getValue(1)] = 1; // Generate the token
3418 else
3419 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3420
3421 X86AddressMode AM;
3422 if (getRegPressure(Node->getOperand(0)) >
3423 getRegPressure(Node->getOperand(1))) {
3424 Select(Node->getOperand(0)); // chain
3425 SelectAddress(Node->getOperand(1), AM);
3426 } else {
3427 SelectAddress(Node->getOperand(1), AM);
3428 Select(Node->getOperand(0)); // chain
3429 }
3430
3431 switch (Node->getValueType(0)) {
3432 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3433 default: assert(0 && "Unknown type to sign extend to.");
3434 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003435 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003436 default:
3437 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3438 case MVT::i8:
3439 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3440 break;
3441 case MVT::i16:
3442 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3443 break;
3444 }
3445 break;
3446 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003447 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003448 "Cannot sign extend from bool!");
3449 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3450 break;
3451 }
3452 return Result;
3453 }
3454
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003455 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003456 // Generate both result values.
3457 if (Result != 1)
3458 ExprMap[N.getValue(1)] = 1; // Generate the token
3459 else
3460 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3461
3462 // FIXME: We are currently ignoring the requested alignment for handling
3463 // greater than the stack alignment. This will need to be revisited at some
3464 // point. Align = N.getOperand(2);
3465
3466 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3467 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3468 std::cerr << "Cannot allocate stack object with greater alignment than"
3469 << " the stack alignment yet!";
3470 abort();
3471 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003472
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003473 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003474 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003475 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3476 .addImm(CN->getValue());
3477 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003478 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3479 Select(N.getOperand(0));
3480 Tmp1 = SelectExpr(N.getOperand(1));
3481 } else {
3482 Tmp1 = SelectExpr(N.getOperand(1));
3483 Select(N.getOperand(0));
3484 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003485
3486 // Subtract size from stack pointer, thereby allocating some space.
3487 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3488 }
3489
3490 // Put a pointer to the space into the result register, by copying the stack
3491 // pointer.
3492 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3493 return Result;
3494
Chris Lattner239738a2005-05-14 08:48:15 +00003495 case X86ISD::TAILCALL:
3496 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003497 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003498 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003499
Chris Lattnerc6f41812005-05-12 23:06:28 +00003500 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3501 isa<ExternalSymbolSDNode>(N.getOperand(1));
3502 unsigned Callee = 0;
3503 if (isDirect) {
3504 Select(N.getOperand(0));
3505 } else {
3506 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3507 Select(N.getOperand(0));
3508 Callee = SelectExpr(N.getOperand(1));
3509 } else {
3510 Callee = SelectExpr(N.getOperand(1));
3511 Select(N.getOperand(0));
3512 }
3513 }
3514
3515 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003516 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003517 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003518 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003519 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003520 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen00b168892005-07-27 06:12:32 +00003521
Chris Lattner239738a2005-05-14 08:48:15 +00003522 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003523 default: assert(0 && "Bad thing to pass in regs");
3524 case MVT::i1:
3525 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3526 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3527 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3528 }
3529 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003530 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003531 default: assert(0 && "Bad thing to pass in regs");
3532 case MVT::i1:
3533 case MVT::i8:
3534 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3535 break;
3536 case MVT::i16:
3537 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3538 break;
3539 case MVT::i32:
3540 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3541 break;
3542 }
3543 }
3544
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003545 if (GlobalAddressSDNode *GASD =
3546 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3547 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3548 } else if (ExternalSymbolSDNode *ESSDN =
3549 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3550 BuildMI(BB, X86::CALLpcrel32,
3551 1).addExternalSymbol(ESSDN->getSymbol(), true);
3552 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003553 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3554 Select(N.getOperand(0));
3555 Tmp1 = SelectExpr(N.getOperand(1));
3556 } else {
3557 Tmp1 = SelectExpr(N.getOperand(1));
3558 Select(N.getOperand(0));
3559 }
3560
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003561 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3562 }
Chris Lattner239738a2005-05-14 08:48:15 +00003563
3564 // Get caller stack amount and amount the callee added to the stack pointer.
3565 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3566 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3567 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3568
3569 if (Node->getNumValues() != 1)
3570 switch (Node->getValueType(1)) {
3571 default: assert(0 && "Unknown value type for call result!");
3572 case MVT::Other: return 1;
3573 case MVT::i1:
3574 case MVT::i8:
3575 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3576 break;
3577 case MVT::i16:
3578 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3579 break;
3580 case MVT::i32:
3581 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3582 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3583 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3584 break;
3585 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003586 if (X86ScalarSSE) {
3587 ContainsFPCode = true;
3588 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3589
3590 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3591 MachineFunction *F = BB->getParent();
3592 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3593 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3594 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3595 break;
3596 } else {
3597 ContainsFPCode = true;
3598 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3599 break;
3600 }
Chris Lattner239738a2005-05-14 08:48:15 +00003601 }
3602 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003603 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003604 case ISD::READPORT:
3605 // First, determine that the size of the operand falls within the acceptable
3606 // range for this architecture.
3607 //
3608 if (Node->getOperand(1).getValueType() != MVT::i16) {
3609 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3610 exit(1);
3611 }
3612
3613 // Make sure we generate both values.
3614 if (Result != 1) { // Generate the token
3615 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3616 assert(0 && "readport already emitted!?");
3617 } else
3618 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen00b168892005-07-27 06:12:32 +00003619
Chris Lattner966cdfb2005-05-09 21:17:38 +00003620 Select(Node->getOperand(0)); // Select the chain.
3621
3622 // If the port is a single-byte constant, use the immediate form.
3623 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3624 if ((Port->getValue() & 255) == Port->getValue()) {
3625 switch (Node->getValueType(0)) {
3626 case MVT::i8:
3627 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3628 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3629 return Result;
3630 case MVT::i16:
3631 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3632 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3633 return Result;
3634 case MVT::i32:
3635 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3636 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3637 return Result;
3638 default: break;
3639 }
3640 }
3641
3642 // Now, move the I/O port address into the DX register and use the IN
3643 // instruction to get the input data.
3644 //
3645 Tmp1 = SelectExpr(Node->getOperand(1));
3646 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3647 switch (Node->getValueType(0)) {
3648 case MVT::i8:
3649 BuildMI(BB, X86::IN8rr, 0);
3650 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3651 return Result;
3652 case MVT::i16:
3653 BuildMI(BB, X86::IN16rr, 0);
3654 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3655 return Result;
3656 case MVT::i32:
3657 BuildMI(BB, X86::IN32rr, 0);
3658 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3659 return Result;
3660 default:
3661 std::cerr << "Cannot do input on this data type";
3662 exit(1);
3663 }
Jeff Cohen00b168892005-07-27 06:12:32 +00003664
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003665 }
3666
3667 return 0;
3668}
3669
Chris Lattnere10269b2005-01-17 19:25:26 +00003670/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3671/// load/op/store instruction. If successful return true.
3672bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3673 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3674 SDOperand Chain = Node->getOperand(0);
3675 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003676 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003677
3678 // The chain has to be a load, the stored value must be an integer binary
3679 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003680 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003681 MVT::isFloatingPoint(StVal.getValueType()))
3682 return false;
3683
Chris Lattner5c659812005-01-17 22:10:42 +00003684 // Token chain must either be a factor node or the load to fold.
3685 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3686 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003687
Chris Lattner5c659812005-01-17 22:10:42 +00003688 SDOperand TheLoad;
3689
3690 // Check to see if there is a load from the same pointer that we're storing
3691 // to in either operand of the binop.
3692 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3693 StVal.getOperand(0).getOperand(1) == StPtr)
3694 TheLoad = StVal.getOperand(0);
3695 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3696 StVal.getOperand(1).getOperand(1) == StPtr)
3697 TheLoad = StVal.getOperand(1);
3698 else
3699 return false; // No matching load operand.
3700
3701 // We can only fold the load if there are no intervening side-effecting
3702 // operations. This means that the store uses the load as its token chain, or
3703 // there are only token factor nodes in between the store and load.
3704 if (Chain != TheLoad.getValue(1)) {
3705 // Okay, the other option is that we have a store referring to (possibly
3706 // nested) token factor nodes. For now, just try peeking through one level
3707 // of token factors to see if this is the case.
3708 bool ChainOk = false;
3709 if (Chain.getOpcode() == ISD::TokenFactor) {
3710 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3711 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3712 ChainOk = true;
3713 break;
3714 }
3715 }
3716
3717 if (!ChainOk) return false;
3718 }
3719
3720 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003721 return false;
3722
3723 // Make sure that one of the operands of the binop is the load, and that the
3724 // load folds into the binop.
3725 if (((StVal.getOperand(0) != TheLoad ||
3726 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3727 (StVal.getOperand(1) != TheLoad ||
3728 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3729 return false;
3730
3731 // Finally, check to see if this is one of the ops we can handle!
3732 static const unsigned ADDTAB[] = {
3733 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3734 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3735 };
3736 static const unsigned SUBTAB[] = {
3737 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3738 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3739 };
3740 static const unsigned ANDTAB[] = {
3741 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3742 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3743 };
3744 static const unsigned ORTAB[] = {
3745 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3746 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3747 };
3748 static const unsigned XORTAB[] = {
3749 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3750 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3751 };
3752 static const unsigned SHLTAB[] = {
3753 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3754 /*Have to put the reg in CL*/0, 0, 0,
3755 };
3756 static const unsigned SARTAB[] = {
3757 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3758 /*Have to put the reg in CL*/0, 0, 0,
3759 };
3760 static const unsigned SHRTAB[] = {
3761 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3762 /*Have to put the reg in CL*/0, 0, 0,
3763 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003764
Chris Lattnere10269b2005-01-17 19:25:26 +00003765 const unsigned *TabPtr = 0;
3766 switch (StVal.getOpcode()) {
3767 default:
3768 std::cerr << "CANNOT [mem] op= val: ";
3769 StVal.Val->dump(); std::cerr << "\n";
3770 case ISD::MUL:
3771 case ISD::SDIV:
3772 case ISD::UDIV:
3773 case ISD::SREM:
3774 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003775
Chris Lattnere10269b2005-01-17 19:25:26 +00003776 case ISD::ADD: TabPtr = ADDTAB; break;
3777 case ISD::SUB: TabPtr = SUBTAB; break;
3778 case ISD::AND: TabPtr = ANDTAB; break;
3779 case ISD:: OR: TabPtr = ORTAB; break;
3780 case ISD::XOR: TabPtr = XORTAB; break;
3781 case ISD::SHL: TabPtr = SHLTAB; break;
3782 case ISD::SRA: TabPtr = SARTAB; break;
3783 case ISD::SRL: TabPtr = SHRTAB; break;
3784 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003785
Chris Lattnere10269b2005-01-17 19:25:26 +00003786 // Handle: [mem] op= CST
3787 SDOperand Op0 = StVal.getOperand(0);
3788 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003789 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003790 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3791 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3792 default: break;
3793 case MVT::i1:
3794 case MVT::i8: Opc = TabPtr[0]; break;
3795 case MVT::i16: Opc = TabPtr[1]; break;
3796 case MVT::i32: Opc = TabPtr[2]; break;
3797 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003798
Chris Lattnere10269b2005-01-17 19:25:26 +00003799 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003800 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3801 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003802 Select(Chain);
3803
Chris Lattnere10269b2005-01-17 19:25:26 +00003804 X86AddressMode AM;
3805 if (getRegPressure(TheLoad.getOperand(0)) >
3806 getRegPressure(TheLoad.getOperand(1))) {
3807 Select(TheLoad.getOperand(0));
3808 SelectAddress(TheLoad.getOperand(1), AM);
3809 } else {
3810 SelectAddress(TheLoad.getOperand(1), AM);
3811 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003812 }
Chris Lattner5c659812005-01-17 22:10:42 +00003813
3814 if (StVal.getOpcode() == ISD::ADD) {
3815 if (CN->getValue() == 1) {
3816 switch (Op0.getValueType()) {
3817 default: break;
3818 case MVT::i8:
3819 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3820 return true;
3821 case MVT::i16: Opc = TabPtr[1];
3822 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3823 return true;
3824 case MVT::i32: Opc = TabPtr[2];
3825 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3826 return true;
3827 }
3828 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3829 switch (Op0.getValueType()) {
3830 default: break;
3831 case MVT::i8:
3832 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3833 return true;
3834 case MVT::i16: Opc = TabPtr[1];
3835 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3836 return true;
3837 case MVT::i32: Opc = TabPtr[2];
3838 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3839 return true;
3840 }
3841 }
3842 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003843
Chris Lattnere10269b2005-01-17 19:25:26 +00003844 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3845 return true;
3846 }
3847 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003848
Chris Lattnere10269b2005-01-17 19:25:26 +00003849 // If we have [mem] = V op [mem], try to turn it into:
3850 // [mem] = [mem] op V.
3851 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3852 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3853 StVal.getOpcode() != ISD::SRL)
3854 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003855
Chris Lattnere10269b2005-01-17 19:25:26 +00003856 if (Op0 != TheLoad) return false;
3857
3858 switch (Op0.getValueType()) {
3859 default: return false;
3860 case MVT::i1:
3861 case MVT::i8: Opc = TabPtr[3]; break;
3862 case MVT::i16: Opc = TabPtr[4]; break;
3863 case MVT::i32: Opc = TabPtr[5]; break;
3864 }
Chris Lattner5c659812005-01-17 22:10:42 +00003865
Chris Lattnerb422aea2005-01-18 17:35:28 +00003866 // Table entry doesn't exist?
3867 if (Opc == 0) return false;
3868
Chris Lattner4a108662005-01-18 03:51:59 +00003869 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3870 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003871 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003872 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003873
Chris Lattnere10269b2005-01-17 19:25:26 +00003874 X86AddressMode AM;
3875 SelectAddress(TheLoad.getOperand(1), AM);
3876 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003877 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003878 return true;
3879}
3880
Chris Lattner381e8872005-05-15 05:46:45 +00003881/// If node is a ret(tailcall) node, emit the specified tail call and return
3882/// true, otherwise return false.
3883///
3884/// FIXME: This whole thing should be a post-legalize optimization pass which
3885/// recognizes and transforms the dag. We don't want the selection phase doing
3886/// this stuff!!
3887///
3888bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3889 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3890
3891 SDOperand Chain = RetNode->getOperand(0);
3892
3893 // If this is a token factor node where one operand is a call, dig into it.
3894 SDOperand TokFactor;
3895 unsigned TokFactorOperand = 0;
3896 if (Chain.getOpcode() == ISD::TokenFactor) {
3897 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3898 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3899 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3900 TokFactorOperand = i;
3901 TokFactor = Chain;
3902 Chain = Chain.getOperand(i);
3903 break;
3904 }
3905 if (TokFactor.Val == 0) return false; // No call operand.
3906 }
3907
3908 // Skip the CALLSEQ_END node if present.
3909 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3910 Chain = Chain.getOperand(0);
3911
3912 // Is a tailcall the last control operation that occurs before the return?
3913 if (Chain.getOpcode() != X86ISD::TAILCALL)
3914 return false;
3915
3916 // If we return a value, is it the value produced by the call?
3917 if (RetNode->getNumOperands() > 1) {
3918 // Not returning the ret val of the call?
3919 if (Chain.Val->getNumValues() == 1 ||
3920 RetNode->getOperand(1) != Chain.getValue(1))
3921 return false;
3922
3923 if (RetNode->getNumOperands() > 2) {
3924 if (Chain.Val->getNumValues() == 2 ||
3925 RetNode->getOperand(2) != Chain.getValue(2))
3926 return false;
3927 }
3928 assert(RetNode->getNumOperands() <= 3);
3929 }
3930
3931 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3932 // For FastCC, this will always be > 0.
3933 unsigned CalleeCallArgAmt =
3934 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3935
3936 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3937 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3938 unsigned CalleeCallArgPopAmt =
3939 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3940
3941 // There are several cases we can handle here. First, if the caller and
3942 // callee are both CCC functions, we can tailcall if the callee takes <= the
3943 // number of argument bytes that the caller does.
3944 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3945 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3946 // Check to see if caller arg area size >= callee arg area size.
3947 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3948 //std::cerr << "CCC TAILCALL UNIMP!\n";
3949 // If TokFactor is non-null, emit all operands.
3950
3951 //EmitCCCToCCCTailCall(Chain.Val);
3952 //return true;
3953 }
3954 return false;
3955 }
3956
3957 // Second, if both are FastCC functions, we can always perform the tail call.
3958 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3959 // If TokFactor is non-null, emit all operands before the call.
3960 if (TokFactor.Val) {
3961 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3962 if (i != TokFactorOperand)
3963 Select(TokFactor.getOperand(i));
3964 }
3965
3966 EmitFastCCToFastCCTailCall(Chain.Val);
3967 return true;
3968 }
3969
3970 // We don't support mixed calls, due to issues with alignment. We could in
3971 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3972 // aligned (which depends on the number of arguments to the callee). TODO.
3973 return false;
3974}
3975
3976static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3977 SelectionDAG &DAG) {
3978 MVT::ValueType StoreVT;
3979 switch (Chain.getOpcode()) {
3980 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003981 // If we found the start of the call sequence, we're done. We actually
3982 // strip off the CALLSEQ_START node, to avoid generating the
3983 // ADJCALLSTACKDOWN marker for the tail call.
3984 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003985 case ISD::TokenFactor: {
3986 std::vector<SDOperand> Ops;
3987 Ops.reserve(Chain.getNumOperands());
3988 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3989 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3990 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3991 }
3992 case ISD::STORE: // Normal store
3993 StoreVT = Chain.getOperand(1).getValueType();
3994 break;
3995 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003996 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00003997 break;
3998 }
3999
4000 SDOperand OrigDest = Chain.getOperand(2);
4001 unsigned OrigOffset;
4002
4003 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4004 OrigOffset = 0;
4005 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
4006 } else {
4007 // We expect only (ESP+C)
4008 assert(OrigDest.getOpcode() == ISD::ADD &&
4009 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4010 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
4011 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
4012 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4013 }
4014
4015 // Compute the new offset from the incoming ESP value we wish to use.
4016 unsigned NewOffset = OrigOffset + Offset;
4017
4018 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4019 MachineFunction &MF = DAG.getMachineFunction();
4020 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4021 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4022
4023 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4024 DAG);
4025 if (Chain.getOpcode() == ISD::STORE)
4026 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4027 FIN);
4028 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4029 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004030 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004031}
4032
4033
4034/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4035/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4036/// tail call.
4037void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4038 unsigned CalleeCallArgSize =
4039 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4040 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4041
4042 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4043
4044 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4045 // indexes that are relative to the incoming ESP. If the incoming and
4046 // outgoing arg sizes are the same we will store to [InESP] instead of
4047 // [CurESP] and the ESP referenced will be relative to the incoming function
4048 // ESP.
4049 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4050 SDOperand AdjustedArgStores =
4051 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4052
4053 // Copy the return address of the caller into a virtual register so we don't
4054 // clobber it.
4055 SDOperand RetVal;
4056 if (ESPOffset) {
4057 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4058 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4059 RetValAddr, TheDAG->getSrcValue(NULL));
4060 SelectExpr(RetVal);
4061 }
4062
4063 // Codegen all of the argument stores.
4064 Select(AdjustedArgStores);
4065
4066 if (RetVal.Val) {
4067 // Emit a store of the saved ret value to the new location.
4068 MachineFunction &MF = TheDAG->getMachineFunction();
4069 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4070 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4071 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4072 RetVal, RetValAddr));
4073 }
4074
4075 // Get the destination value.
4076 SDOperand Callee = TailCallNode->getOperand(1);
4077 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4078 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004079 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004080 if (!isDirect) CalleeReg = SelectExpr(Callee);
4081
4082 unsigned RegOp1 = 0;
4083 unsigned RegOp2 = 0;
4084
4085 if (TailCallNode->getNumOperands() > 4) {
4086 // The first value is passed in (a part of) EAX, the second in EDX.
4087 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4088 if (TailCallNode->getNumOperands() > 5)
4089 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen00b168892005-07-27 06:12:32 +00004090
Chris Lattner381e8872005-05-15 05:46:45 +00004091 switch (TailCallNode->getOperand(4).getValueType()) {
4092 default: assert(0 && "Bad thing to pass in regs");
4093 case MVT::i1:
4094 case MVT::i8:
4095 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4096 RegOp1 = X86::AL;
4097 break;
4098 case MVT::i16:
4099 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4100 RegOp1 = X86::AX;
4101 break;
4102 case MVT::i32:
4103 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4104 RegOp1 = X86::EAX;
4105 break;
4106 }
4107 if (RegOp2)
4108 switch (TailCallNode->getOperand(5).getValueType()) {
4109 default: assert(0 && "Bad thing to pass in regs");
4110 case MVT::i1:
4111 case MVT::i8:
4112 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4113 RegOp2 = X86::DL;
4114 break;
4115 case MVT::i16:
4116 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4117 RegOp2 = X86::DX;
4118 break;
4119 case MVT::i32:
4120 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4121 RegOp2 = X86::EDX;
4122 break;
4123 }
4124 }
4125
4126 // Adjust ESP.
4127 if (ESPOffset)
4128 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4129 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4130
4131 // TODO: handle jmp [mem]
4132 if (!isDirect) {
4133 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4134 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004135 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004136 } else {
4137 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4138 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4139 }
4140 // ADD IMPLICIT USE RegOp1/RegOp2's
4141}
4142
Chris Lattnere10269b2005-01-17 19:25:26 +00004143
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004144void ISel::Select(SDOperand N) {
4145 unsigned Tmp1, Tmp2, Opc;
4146
Nate Begeman85fdeb22005-03-24 04:39:54 +00004147 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004148 return; // Already selected.
4149
Chris Lattner989de032005-01-11 06:14:36 +00004150 SDNode *Node = N.Val;
4151
4152 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004153 default:
Chris Lattner989de032005-01-11 06:14:36 +00004154 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004155 assert(0 && "Node not handled yet!");
4156 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004157 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004158 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004159 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004160 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4161 Select(Node->getOperand(OneFirst));
4162 Select(Node->getOperand(!OneFirst));
4163 } else {
4164 std::vector<std::pair<unsigned, unsigned> > OpsP;
4165 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4166 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4167 std::sort(OpsP.begin(), OpsP.end());
4168 std::reverse(OpsP.begin(), OpsP.end());
4169 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4170 Select(Node->getOperand(OpsP[i].second));
4171 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004172 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004173 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004174 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4175 Select(N.getOperand(0));
4176 Tmp1 = SelectExpr(N.getOperand(1));
4177 } else {
4178 Tmp1 = SelectExpr(N.getOperand(1));
4179 Select(N.getOperand(0));
4180 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004181 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004182
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004183 if (Tmp1 != Tmp2) {
4184 switch (N.getOperand(1).getValueType()) {
4185 default: assert(0 && "Invalid type for operation!");
4186 case MVT::i1:
4187 case MVT::i8: Opc = X86::MOV8rr; break;
4188 case MVT::i16: Opc = X86::MOV16rr; break;
4189 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004190 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00004191 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004192 if (X86ScalarSSE) {
4193 Opc = X86::MOVAPDrr;
4194 } else {
Jeff Cohen00b168892005-07-27 06:12:32 +00004195 Opc = X86::FpMOV;
4196 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004197 }
4198 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004199 }
4200 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4201 }
4202 return;
4203 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004204 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4205 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4206 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4207 if (EmitPotentialTailCall(Node))
4208 return;
4209
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004210 switch (N.getNumOperands()) {
4211 default:
4212 assert(0 && "Unknown return instruction!");
4213 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004214 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00004215 N.getOperand(2).getValueType() == MVT::i32 &&
4216 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004217 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4218 Tmp1 = SelectExpr(N.getOperand(1));
4219 Tmp2 = SelectExpr(N.getOperand(2));
4220 } else {
4221 Tmp2 = SelectExpr(N.getOperand(2));
4222 Tmp1 = SelectExpr(N.getOperand(1));
4223 }
4224 Select(N.getOperand(0));
4225
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004226 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4227 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004228 break;
4229 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004230 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4231 Select(N.getOperand(0));
4232 Tmp1 = SelectExpr(N.getOperand(1));
4233 } else {
4234 Tmp1 = SelectExpr(N.getOperand(1));
4235 Select(N.getOperand(0));
4236 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004237 switch (N.getOperand(1).getValueType()) {
4238 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004239 case MVT::f32:
4240 if (X86ScalarSSE) {
4241 // Spill the value to memory and reload it into top of stack.
4242 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4243 MachineFunction *F = BB->getParent();
4244 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4245 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4246 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4247 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004248 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004249 } else {
4250 assert(0 && "MVT::f32 only legal with scalar sse fp");
4251 abort();
4252 }
4253 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004254 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004255 if (X86ScalarSSE) {
4256 // Spill the value to memory and reload it into top of stack.
4257 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4258 MachineFunction *F = BB->getParent();
4259 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4260 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4261 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4262 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004263 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004264 } else {
4265 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4266 }
4267 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004268 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004269 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4270 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004271 }
4272 break;
4273 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004274 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004275 break;
4276 }
Chris Lattner3648c672005-05-13 21:44:04 +00004277 if (X86Lowering.getBytesToPopOnReturn() == 0)
4278 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4279 else
4280 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004281 return;
4282 case ISD::BR: {
4283 Select(N.getOperand(0));
4284 MachineBasicBlock *Dest =
4285 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4286 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4287 return;
4288 }
4289
4290 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004291 MachineBasicBlock *Dest =
4292 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004293
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004294 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4295 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004296 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4297 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4298 Select(N.getOperand(0));
4299 Tmp1 = SelectExpr(N.getOperand(1));
4300 } else {
4301 Tmp1 = SelectExpr(N.getOperand(1));
4302 Select(N.getOperand(0));
4303 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004304 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4305 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4306 }
Chris Lattner11333092005-01-11 03:11:44 +00004307
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004308 return;
4309 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004310
Chris Lattner4df0de92005-01-17 00:00:33 +00004311 case ISD::LOAD:
4312 // If this load could be folded into the only using instruction, and if it
4313 // is safe to emit the instruction here, try to do so now.
4314 if (Node->hasNUsesOfValue(1, 0)) {
4315 SDOperand TheVal = N.getValue(0);
4316 SDNode *User = 0;
4317 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4318 assert(UI != Node->use_end() && "Didn't find use!");
4319 SDNode *UN = *UI;
4320 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4321 if (UN->getOperand(i) == TheVal) {
4322 User = UN;
4323 goto FoundIt;
4324 }
4325 }
4326 FoundIt:
4327 // Only handle unary operators right now.
4328 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004329 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004330 SelectExpr(SDOperand(User, 0));
4331 return;
4332 }
4333 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004334 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004335 SelectExpr(N);
4336 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004337 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004338 case ISD::EXTLOAD:
4339 case ISD::SEXTLOAD:
4340 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004341 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004342 case X86ISD::TAILCALL:
4343 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004344 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004345 SelectExpr(N);
4346 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004347 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004348 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004349 ExprMap.erase(N);
4350 SelectExpr(N.getValue(0));
4351 return;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004352
Chris Lattner01546c52005-07-30 00:05:54 +00004353 case X86ISD::FP_TO_INT16_IN_MEM:
4354 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattnerf7443da2005-07-29 00:54:34 +00004355 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner745d5382005-07-29 00:40:01 +00004356 assert(N.getOperand(1).getValueType() == MVT::f64);
4357 X86AddressMode AM;
4358 Select(N.getOperand(0)); // Select the token chain
4359
4360 unsigned ValReg;
4361 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4362 ValReg = SelectExpr(N.getOperand(1));
4363 SelectAddress(N.getOperand(2), AM);
4364 } else {
4365 SelectAddress(N.getOperand(2), AM);
4366 ValReg = SelectExpr(N.getOperand(1));
4367 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004368
Chris Lattnerf7443da2005-07-29 00:54:34 +00004369 // Change the floating point control register to use "round towards zero"
4370 // mode when truncating to an integer value.
4371 //
4372 MachineFunction *F = BB->getParent();
4373 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4374 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004375
Chris Lattnerf7443da2005-07-29 00:54:34 +00004376 // Load the old value of the high byte of the control word...
Chris Lattnera35e1df2005-07-30 00:17:52 +00004377 unsigned OldCW = MakeReg(MVT::i16);
4378 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004379
Chris Lattnerf7443da2005-07-29 00:54:34 +00004380 // Set the high part to be round to zero...
Chris Lattnera88da082005-07-30 00:43:00 +00004381 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004382
Chris Lattnerf7443da2005-07-29 00:54:34 +00004383 // Reload the modified control word now...
4384 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004385
Chris Lattnerf7443da2005-07-29 00:54:34 +00004386 // Restore the memory image of control word to original value
Chris Lattnera35e1df2005-07-30 00:17:52 +00004387 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner01546c52005-07-30 00:05:54 +00004388
4389 // Get the X86 opcode to use.
4390 switch (N.getOpcode()) {
4391 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
4392 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
4393 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
4394 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004395
Chris Lattner01546c52005-07-30 00:05:54 +00004396 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004397
Chris Lattnerf7443da2005-07-29 00:54:34 +00004398 // Reload the original control word now.
4399 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner745d5382005-07-29 00:40:01 +00004400 return;
4401 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004402
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004403 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004404 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004405 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004406 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4407 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4408 && "Unsupported TRUNCSTORE for this target!");
4409
4410 if (StoredTy == MVT::i16) {
4411 // FIXME: This is here just to allow testing. X86 doesn't really have a
4412 // TRUNCSTORE i16 operation, but this is required for targets that do not
4413 // have 16-bit integer registers. We occasionally disable 16-bit integer
4414 // registers to test the promotion code.
4415 Select(N.getOperand(0));
4416 Tmp1 = SelectExpr(N.getOperand(1));
4417 SelectAddress(N.getOperand(2), AM);
4418
4419 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4420 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4421 return;
4422 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004423
4424 // Store of constant bool?
4425 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4426 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4427 Select(N.getOperand(0));
4428 SelectAddress(N.getOperand(2), AM);
4429 } else {
4430 SelectAddress(N.getOperand(2), AM);
4431 Select(N.getOperand(0));
4432 }
4433 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4434 return;
4435 }
4436
4437 switch (StoredTy) {
4438 default: assert(0 && "Cannot truncstore this type!");
4439 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004440 case MVT::f32:
Jeff Cohen00b168892005-07-27 06:12:32 +00004441 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004442 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004443 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004444
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004445 std::vector<std::pair<unsigned, unsigned> > RP;
4446 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4447 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4448 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4449 std::sort(RP.begin(), RP.end());
4450
Chris Lattner572dd082005-02-23 05:57:21 +00004451 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004452 for (unsigned i = 0; i != 3; ++i)
4453 switch (RP[2-i].second) {
4454 default: assert(0 && "Unknown operand number!");
4455 case 0: Select(N.getOperand(0)); break;
4456 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4457 case 2: SelectAddress(N.getOperand(2), AM); break;
4458 }
4459
4460 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4461 return;
4462 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004463 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004464 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004465
4466 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4467 Opc = 0;
4468 switch (CN->getValueType(0)) {
4469 default: assert(0 && "Invalid type for operation!");
4470 case MVT::i1:
4471 case MVT::i8: Opc = X86::MOV8mi; break;
4472 case MVT::i16: Opc = X86::MOV16mi; break;
4473 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004474 }
4475 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004476 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4477 Select(N.getOperand(0));
4478 SelectAddress(N.getOperand(2), AM);
4479 } else {
4480 SelectAddress(N.getOperand(2), AM);
4481 Select(N.getOperand(0));
4482 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004483 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4484 return;
4485 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004486 } else if (GlobalAddressSDNode *GA =
4487 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4488 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4489
4490 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4491 Select(N.getOperand(0));
4492 SelectAddress(N.getOperand(2), AM);
4493 } else {
4494 SelectAddress(N.getOperand(2), AM);
4495 Select(N.getOperand(0));
4496 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004497 GlobalValue *GV = GA->getGlobal();
4498 // For Darwin, external and weak symbols are indirect, so we want to load
4499 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00004500 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begeman16b04f32005-07-15 00:38:55 +00004501 (GV->hasWeakLinkage() || GV->isExternal())) {
4502 Tmp1 = MakeReg(MVT::i32);
4503 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4504 .addGlobalAddress(GV, false, 0);
4505 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4506 } else {
4507 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4508 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004509 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004510 }
Chris Lattner837caa72005-01-11 23:21:30 +00004511
4512 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004513 if (TryToFoldLoadOpStore(Node))
4514 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004515
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004516 switch (N.getOperand(1).getValueType()) {
4517 default: assert(0 && "Cannot store this type!");
4518 case MVT::i1:
4519 case MVT::i8: Opc = X86::MOV8mr; break;
4520 case MVT::i16: Opc = X86::MOV16mr; break;
4521 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004522 case MVT::f32: Opc = X86::MOVSSmr; break;
4523 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004524 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004525
Chris Lattner11333092005-01-11 03:11:44 +00004526 std::vector<std::pair<unsigned, unsigned> > RP;
4527 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4528 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4529 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4530 std::sort(RP.begin(), RP.end());
4531
Chris Lattner572dd082005-02-23 05:57:21 +00004532 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004533 for (unsigned i = 0; i != 3; ++i)
4534 switch (RP[2-i].second) {
4535 default: assert(0 && "Unknown operand number!");
4536 case 0: Select(N.getOperand(0)); break;
4537 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004538 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004539 }
4540
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004541 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4542 return;
4543 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004544 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004545 Select(N.getOperand(0));
4546 // Stack amount
4547 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4548 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4549 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004550 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004551 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004552 return;
Chris Lattner989de032005-01-11 06:14:36 +00004553 case ISD::MEMSET: {
4554 Select(N.getOperand(0)); // Select the chain.
4555 unsigned Align =
4556 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4557 if (Align == 0) Align = 1;
4558
4559 // Turn the byte code into # iterations
4560 unsigned CountReg;
4561 unsigned Opcode;
4562 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4563 unsigned Val = ValC->getValue() & 255;
4564
4565 // If the value is a constant, then we can potentially use larger sets.
4566 switch (Align & 3) {
4567 case 2: // WORD aligned
4568 CountReg = MakeReg(MVT::i32);
4569 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4570 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4571 } else {
4572 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4573 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4574 }
4575 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4576 Opcode = X86::REP_STOSW;
4577 break;
4578 case 0: // DWORD aligned
4579 CountReg = MakeReg(MVT::i32);
4580 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4581 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4582 } else {
4583 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4584 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4585 }
4586 Val = (Val << 8) | Val;
4587 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4588 Opcode = X86::REP_STOSD;
4589 break;
4590 default: // BYTE aligned
4591 CountReg = SelectExpr(Node->getOperand(3));
4592 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4593 Opcode = X86::REP_STOSB;
4594 break;
4595 }
4596 } else {
4597 // If it's not a constant value we are storing, just fall back. We could
4598 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4599 unsigned ValReg = SelectExpr(Node->getOperand(2));
4600 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4601 CountReg = SelectExpr(Node->getOperand(3));
4602 Opcode = X86::REP_STOSB;
4603 }
4604
4605 // No matter what the alignment is, we put the source in ESI, the
4606 // destination in EDI, and the count in ECX.
4607 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4608 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4609 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4610 BuildMI(BB, Opcode, 0);
4611 return;
4612 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004613 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004614 Select(N.getOperand(0)); // Select the chain.
4615 unsigned Align =
4616 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4617 if (Align == 0) Align = 1;
4618
4619 // Turn the byte code into # iterations
4620 unsigned CountReg;
4621 unsigned Opcode;
4622 switch (Align & 3) {
4623 case 2: // WORD aligned
4624 CountReg = MakeReg(MVT::i32);
4625 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4626 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4627 } else {
4628 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4629 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4630 }
4631 Opcode = X86::REP_MOVSW;
4632 break;
4633 case 0: // DWORD aligned
4634 CountReg = MakeReg(MVT::i32);
4635 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4636 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4637 } else {
4638 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4639 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4640 }
4641 Opcode = X86::REP_MOVSD;
4642 break;
4643 default: // BYTE aligned
4644 CountReg = SelectExpr(Node->getOperand(3));
4645 Opcode = X86::REP_MOVSB;
4646 break;
4647 }
4648
4649 // No matter what the alignment is, we put the source in ESI, the
4650 // destination in EDI, and the count in ECX.
4651 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4652 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4653 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4654 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4655 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4656 BuildMI(BB, Opcode, 0);
4657 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004658 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004659 case ISD::WRITEPORT:
4660 if (Node->getOperand(2).getValueType() != MVT::i16) {
4661 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4662 exit(1);
4663 }
4664 Select(Node->getOperand(0)); // Emit the chain.
4665
4666 Tmp1 = SelectExpr(Node->getOperand(1));
4667 switch (Node->getOperand(1).getValueType()) {
4668 case MVT::i8:
4669 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4670 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4671 break;
4672 case MVT::i16:
4673 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4674 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4675 break;
4676 case MVT::i32:
4677 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4678 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4679 break;
4680 default:
4681 std::cerr << "llvm.writeport: invalid data type for X86 target";
4682 exit(1);
4683 }
4684
4685 // If the port is a single-byte constant, use the immediate form.
4686 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4687 if ((CN->getValue() & 255) == CN->getValue()) {
4688 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4689 return;
4690 }
4691
4692 // Otherwise, move the I/O port address into the DX register.
4693 unsigned Reg = SelectExpr(Node->getOperand(2));
4694 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4695 BuildMI(BB, Opc, 0);
4696 return;
4697 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004698 assert(0 && "Should not be reached!");
4699}
4700
4701
4702/// createX86PatternInstructionSelector - This pass converts an LLVM function
4703/// into a machine code representation using pattern matching and a machine
4704/// description file.
4705///
4706FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004707 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004708}