blob: 31bd79ef12727ec52ff01e807f2f7093e0e157bd [file] [log] [blame]
Chris Lattner88c8a232005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner1d13a922005-01-10 22:10:13 +00002//
Chris Lattner88c8a232005-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattner88c8a232005-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 Begemanf26625e2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattner7ce7a8f2005-05-12 23:06:28 +000018#include "llvm/CallingConv.h"
Chris Lattner6972c312005-05-09 03:36:39 +000019#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000021#include "llvm/Function.h"
Chris Lattner6972c312005-05-09 03:36:39 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner88c8a232005-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 Begemanf26625e2005-07-12 01:41:54 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerdb68d392005-04-30 04:25:35 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner6972c312005-05-09 03:36:39 +000032#include "llvm/Support/CFG.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/ADT/Statistic.h"
35#include <set>
Jeff Cohen407aa012005-01-12 04:29:05 +000036#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000037using namespace llvm;
38
Chris Lattner7ce7a8f2005-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 Lattnera36117b2005-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 Lattner1b3520c2005-05-14 08:48:15 +000056
Chris Lattner6dc60e82005-07-29 00:54:34 +000057 /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the
58 /// integer destination in memory and a FP reg source. This corresponds
59 /// to the X86::FIST*m instructions and the rounding mode change stuff. It
60 /// has two inputs (token chain and address) and two outputs (FP value and
61 /// token chain).
62 FP_TO_INT64_IN_MEM,
Chris Lattner67756e22005-07-29 00:40:01 +000063
Chris Lattner1b3520c2005-05-14 08:48:15 +000064 /// CALL/TAILCALL - These operations represent an abstract X86 call
65 /// instruction, which includes a bunch of information. In particular the
66 /// operands of these node are:
67 ///
68 /// #0 - The incoming token chain
69 /// #1 - The callee
70 /// #2 - The number of arg bytes the caller pushes on the stack.
71 /// #3 - The number of arg bytes the callee pops off the stack.
72 /// #4 - The value to pass in AL/AX/EAX (optional)
73 /// #5 - The value to pass in DL/DX/EDX (optional)
74 ///
75 /// The result values of these nodes are:
76 ///
77 /// #0 - The outgoing token chain
78 /// #1 - The first register result value (optional)
79 /// #2 - The second register result value (optional)
80 ///
81 /// The CALL vs TAILCALL distinction boils down to whether the callee is
82 /// known not to modify the caller's stack frame, as is standard with
83 /// LLVM.
84 CALL,
85 TAILCALL,
Chris Lattnera36117b2005-05-14 06:52:07 +000086 };
87 }
88}
89
Chris Lattner88c8a232005-01-07 07:49:41 +000090//===----------------------------------------------------------------------===//
91// X86TargetLowering - X86 Implementation of the TargetLowering interface
92namespace {
93 class X86TargetLowering : public TargetLowering {
94 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner9f59d282005-01-09 00:01:27 +000095 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattnerdd66a412005-05-15 05:46:45 +000096 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
97 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner88c8a232005-01-07 07:49:41 +000098 public:
99 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
100 // Set up the TargetLowering object.
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000101
Chris Lattner5011ff02005-05-13 22:46:57 +0000102 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000103 setShiftAmountType(MVT::i8);
104 setSetCCResultType(MVT::i8);
Chris Lattner38fd9702005-04-07 19:41:46 +0000105 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnerd8d30662005-01-19 03:36:30 +0000106 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000107
108 // Set up the register classes.
Nate Begeman8a093362005-07-06 18:59:04 +0000109 // FIXME: Eliminate these two classes when legalize can handle promotions
110 // well.
111 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner88c8a232005-01-07 07:49:41 +0000112 addRegisterClass(MVT::i8, X86::R8RegisterClass);
113 addRegisterClass(MVT::i16, X86::R16RegisterClass);
114 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000115
Chris Lattner507a2752005-07-16 00:28:20 +0000116 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
117 // operation.
118 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
119 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
120 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman7e74c832005-07-16 02:02:34 +0000122
123 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
124 // this operation.
125 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
126 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000127
Chris Lattner67756e22005-07-29 00:40:01 +0000128 if (!X86ScalarSSE) {
129 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
130 // isn't legal.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
132 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
133 }
134
Chris Lattnerbc85c322005-07-29 01:00:29 +0000135 // Handle FP_TO_UINT by promoting the destination to a larger signed
136 // conversion.
137 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
141
142 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
143 // this operation.
144 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
145 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
146
Chris Lattnera3a135a2005-04-09 03:22:37 +0000147 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000148 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
149 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000150 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000151 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
152 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
153 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner05ad4b82005-05-11 05:00:34 +0000154 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
155 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
156 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
157 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
158 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
159 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000160 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
161 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +0000162 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000163
Chris Lattner6c6a39a2005-05-09 20:37:29 +0000164 setOperationAction(ISD::READIO , MVT::i1 , Expand);
165 setOperationAction(ISD::READIO , MVT::i8 , Expand);
166 setOperationAction(ISD::READIO , MVT::i16 , Expand);
167 setOperationAction(ISD::READIO , MVT::i32 , Expand);
168 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
169 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
170 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
171 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000172
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000173 // These should be promoted to a larger select which is supported.
Nate Begeman8a093362005-07-06 18:59:04 +0000174 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000175 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000176
Nate Begeman8a093362005-07-06 18:59:04 +0000177 if (X86ScalarSSE) {
178 // Set up the FP register classes.
179 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
180 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000181
Nate Begeman7e74c832005-07-16 02:02:34 +0000182 // SSE has no load+extend ops
Nate Begeman8a093362005-07-06 18:59:04 +0000183 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
184 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman7e74c832005-07-16 02:02:34 +0000185
186 // SSE has no i16 to fp conversion, only i32
187 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
188
Nate Begeman8a093362005-07-06 18:59:04 +0000189 // We don't support sin/cos/sqrt/fmod
190 setOperationAction(ISD::FSIN , MVT::f64, Expand);
191 setOperationAction(ISD::FCOS , MVT::f64, Expand);
192 setOperationAction(ISD::FABS , MVT::f64, Expand);
193 setOperationAction(ISD::FNEG , MVT::f64, Expand);
194 setOperationAction(ISD::SREM , MVT::f64, Expand);
195 setOperationAction(ISD::FSIN , MVT::f32, Expand);
196 setOperationAction(ISD::FCOS , MVT::f32, Expand);
197 setOperationAction(ISD::FABS , MVT::f32, Expand);
198 setOperationAction(ISD::FNEG , MVT::f32, Expand);
199 setOperationAction(ISD::SREM , MVT::f32, Expand);
200 } else {
201 // Set up the FP register classes.
202 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000203
Nate Begeman8a093362005-07-06 18:59:04 +0000204 if (!UnsafeFPMath) {
205 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
206 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
207 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000208
Nate Begeman8a093362005-07-06 18:59:04 +0000209 addLegalFPImmediate(+0.0); // FLD0
210 addLegalFPImmediate(+1.0); // FLD1
211 addLegalFPImmediate(-0.0); // FLD0/FCHS
212 addLegalFPImmediate(-1.0); // FLD1/FCHS
213 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000214 computeRegisterProperties();
Reid Spencerd37d8542005-07-19 04:52:44 +0000215
216 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
217 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
218 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
219 allowUnalignedStores = true; // x86 supports it!
Chris Lattner88c8a232005-01-07 07:49:41 +0000220 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000221
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000222 // Return the number of bytes that a function should pop when it returns (in
223 // addition to the space used by the return address).
224 //
225 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
226
Chris Lattnerdd66a412005-05-15 05:46:45 +0000227 // Return the number of bytes that the caller reserves for arguments passed
228 // to this function.
229 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
230
Chris Lattnera36117b2005-05-14 06:52:07 +0000231 /// LowerOperation - Provide custom lowering hooks for some operations.
232 ///
233 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
234
Chris Lattner88c8a232005-01-07 07:49:41 +0000235 /// LowerArguments - This hook must be implemented to indicate how we should
236 /// lower the arguments for the specified function, into the specified DAG.
237 virtual std::vector<SDOperand>
238 LowerArguments(Function &F, SelectionDAG &DAG);
239
240 /// LowerCallTo - This hook lowers an abstract call to a function into an
241 /// actual call.
Chris Lattnerb52e0412005-01-08 19:28:19 +0000242 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000244 bool isTailCall, SDOperand Callee, ArgListTy &Args,
245 SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000246
Chris Lattnera7220852005-07-05 19:58:54 +0000247 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
248 Value *VAListV, SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000249 virtual std::pair<SDOperand,SDOperand>
Chris Lattnera7220852005-07-05 19:58:54 +0000250 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
251 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000252
Chris Lattner9f59d282005-01-09 00:01:27 +0000253 virtual std::pair<SDOperand, SDOperand>
254 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
255 SelectionDAG &DAG);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000256
257 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
258
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000259 private:
260 // C Calling Convention implementation.
261 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
262 std::pair<SDOperand, SDOperand>
263 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000264 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000265 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000266
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000267 // Fast Calling Convention implementation.
268 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
269 std::pair<SDOperand, SDOperand>
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000270 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000271 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000272 };
273}
274
Chris Lattner88c8a232005-01-07 07:49:41 +0000275std::vector<SDOperand>
276X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000277 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
278 return LowerFastCCArguments(F, DAG);
279 return LowerCCCArguments(F, DAG);
280}
281
282std::pair<SDOperand, SDOperand>
283X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
284 bool isVarArg, unsigned CallingConv,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000286 SDOperand Callee, ArgListTy &Args,
287 SelectionDAG &DAG) {
288 assert((!isVarArg || CallingConv == CallingConv::C) &&
289 "Only C takes varargs!");
290 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000291 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
292 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000293}
294
295//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000296// C Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000297//===----------------------------------------------------------------------===//
298
299std::vector<SDOperand>
300X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000301 std::vector<SDOperand> ArgValues;
302
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000303 MachineFunction &MF = DAG.getMachineFunction();
304 MachineFrameInfo *MFI = MF.getFrameInfo();
305
Chris Lattner88c8a232005-01-07 07:49:41 +0000306 // Add DAG nodes to load the arguments... On entry to a function on the X86,
307 // the stack frame looks like this:
308 //
309 // [ESP] -- return address
310 // [ESP + 4] -- first argument (leftmost lexically)
311 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukmanc88330a2005-04-21 23:38:14 +0000312 // ...
Chris Lattner88c8a232005-01-07 07:49:41 +0000313 //
Chris Lattner88c8a232005-01-07 07:49:41 +0000314 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattner531f9e92005-03-15 04:54:21 +0000315 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000316 MVT::ValueType ObjectVT = getValueType(I->getType());
317 unsigned ArgIncrement = 4;
318 unsigned ObjSize;
319 switch (ObjectVT) {
320 default: assert(0 && "Unhandled argument type!");
321 case MVT::i1:
322 case MVT::i8: ObjSize = 1; break;
323 case MVT::i16: ObjSize = 2; break;
324 case MVT::i32: ObjSize = 4; break;
325 case MVT::i64: ObjSize = ArgIncrement = 8; break;
326 case MVT::f32: ObjSize = 4; break;
327 case MVT::f64: ObjSize = ArgIncrement = 8; break;
328 }
329 // Create the frame index object for this incoming parameter...
330 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000331
Chris Lattner88c8a232005-01-07 07:49:41 +0000332 // Create the SelectionDAG nodes corresponding to a load from this parameter
333 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
334
335 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
336 // dead loads.
337 SDOperand ArgValue;
338 if (!I->use_empty())
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000339 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
340 DAG.getSrcValue(NULL));
Chris Lattner88c8a232005-01-07 07:49:41 +0000341 else {
342 if (MVT::isInteger(ObjectVT))
343 ArgValue = DAG.getConstant(0, ObjectVT);
344 else
345 ArgValue = DAG.getConstantFP(0, ObjectVT);
346 }
347 ArgValues.push_back(ArgValue);
348
349 ArgOffset += ArgIncrement; // Move on to the next argument...
350 }
351
352 // If the function takes variable number of arguments, make a frame index for
353 // the start of the first vararg value... for expansion of llvm.va_start.
354 if (F.isVarArg())
355 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000356 ReturnAddrIndex = 0; // No return address slot generated yet.
357 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000358 BytesCallerReserves = ArgOffset;
Chris Lattnerb59006c2005-04-09 15:23:56 +0000359
360 // Finally, inform the code generator which regs we return values in.
361 switch (getValueType(F.getReturnType())) {
362 default: assert(0 && "Unknown type!");
363 case MVT::isVoid: break;
364 case MVT::i1:
365 case MVT::i8:
366 case MVT::i16:
367 case MVT::i32:
368 MF.addLiveOut(X86::EAX);
369 break;
370 case MVT::i64:
371 MF.addLiveOut(X86::EAX);
372 MF.addLiveOut(X86::EDX);
373 break;
374 case MVT::f32:
375 case MVT::f64:
376 MF.addLiveOut(X86::ST0);
377 break;
378 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000379 return ArgValues;
380}
381
Chris Lattnerb52e0412005-01-08 19:28:19 +0000382std::pair<SDOperand, SDOperand>
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000383X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000384 bool isVarArg, bool isTailCall,
385 SDOperand Callee, ArgListTy &Args,
386 SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000387 // Count how many bytes are to be pushed on the stack.
388 unsigned NumBytes = 0;
389
390 if (Args.empty()) {
391 // Save zero bytes.
Chris Lattner2dce7032005-05-12 23:24:06 +0000392 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000393 DAG.getConstant(0, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000394 } else {
395 for (unsigned i = 0, e = Args.size(); i != e; ++i)
396 switch (getValueType(Args[i].second)) {
397 default: assert(0 && "Unknown value type!");
398 case MVT::i1:
399 case MVT::i8:
400 case MVT::i16:
401 case MVT::i32:
402 case MVT::f32:
403 NumBytes += 4;
404 break;
405 case MVT::i64:
406 case MVT::f64:
407 NumBytes += 8;
408 break;
409 }
410
Chris Lattner2dce7032005-05-12 23:24:06 +0000411 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000412 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000413
414 // Arguments go on the stack in reverse order, as specified by the ABI.
415 unsigned ArgOffset = 0;
Chris Lattner720a62e2005-01-14 22:37:41 +0000416 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
417 DAG.getEntryNode());
Chris Lattnerc78776d2005-01-21 19:46:38 +0000418 std::vector<SDOperand> Stores;
419
Chris Lattner88c8a232005-01-07 07:49:41 +0000420 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000421 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
422 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
423
424 switch (getValueType(Args[i].second)) {
425 default: assert(0 && "Unexpected ValueType for argument!");
426 case MVT::i1:
427 case MVT::i8:
428 case MVT::i16:
429 // Promote the integer to 32 bits. If the input type is signed use a
430 // sign extend, otherwise use a zero extend.
431 if (Args[i].second->isSigned())
432 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
433 else
434 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
435
436 // FALL THROUGH
437 case MVT::i32:
438 case MVT::f32:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000439 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000440 Args[i].first, PtrOff,
441 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000442 ArgOffset += 4;
443 break;
444 case MVT::i64:
445 case MVT::f64:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000446 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000447 Args[i].first, PtrOff,
448 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000449 ArgOffset += 8;
450 break;
451 }
452 }
Chris Lattnerc78776d2005-01-21 19:46:38 +0000453 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner88c8a232005-01-07 07:49:41 +0000454 }
455
456 std::vector<MVT::ValueType> RetVals;
457 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner88c8a232005-01-07 07:49:41 +0000458 RetVals.push_back(MVT::Other);
459
Chris Lattner1b3520c2005-05-14 08:48:15 +0000460 // The result values produced have to be legal. Promote the result.
461 switch (RetTyVT) {
462 case MVT::isVoid: break;
463 default:
464 RetVals.push_back(RetTyVT);
465 break;
466 case MVT::i1:
467 case MVT::i8:
468 case MVT::i16:
469 RetVals.push_back(MVT::i32);
470 break;
471 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000472 if (X86ScalarSSE)
473 RetVals.push_back(MVT::f32);
474 else
475 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000476 break;
477 case MVT::i64:
478 RetVals.push_back(MVT::i32);
479 RetVals.push_back(MVT::i32);
480 break;
481 }
482 std::vector<SDOperand> Ops;
483 Ops.push_back(Chain);
484 Ops.push_back(Callee);
485 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
486 Ops.push_back(DAG.getConstant(0, getPointerTy()));
487 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
488 RetVals, Ops);
489 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
490
491 SDOperand ResultVal;
492 switch (RetTyVT) {
493 case MVT::isVoid: break;
494 default:
495 ResultVal = TheCall.getValue(1);
496 break;
497 case MVT::i1:
498 case MVT::i8:
499 case MVT::i16:
500 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
501 break;
502 case MVT::f32:
503 // FIXME: we would really like to remember that this FP_ROUND operation is
504 // okay to eliminate if we allow excess FP precision.
505 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
506 break;
507 case MVT::i64:
508 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
509 TheCall.getValue(2));
510 break;
511 }
512
513 return std::make_pair(ResultVal, Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000514}
515
Chris Lattnera7220852005-07-05 19:58:54 +0000516SDOperand
517X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
518 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000519 // vastart just stores the address of the VarArgsFrameIndex slot.
520 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnera7220852005-07-05 19:58:54 +0000521 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
522 DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000523}
524
Chris Lattnera7220852005-07-05 19:58:54 +0000525
526std::pair<SDOperand,SDOperand>
527X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
528 Value *VAListV, const Type *ArgTy,
529 SelectionDAG &DAG) {
Chris Lattner9f59d282005-01-09 00:01:27 +0000530 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnera7220852005-07-05 19:58:54 +0000531 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
532 VAListP, DAG.getSrcValue(VAListV));
533 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner91ae1292005-07-05 17:50:16 +0000534 DAG.getSrcValue(NULL));
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000535 unsigned Amt;
536 if (ArgVT == MVT::i32)
537 Amt = 4;
538 else {
539 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
540 "Other types should have been promoted for varargs!");
541 Amt = 8;
Chris Lattner9f59d282005-01-09 00:01:27 +0000542 }
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000543 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
544 DAG.getConstant(Amt, Val.getValueType()));
545 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera7220852005-07-05 19:58:54 +0000546 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000547 return std::make_pair(Result, Chain);
548}
Misha Brukmanc88330a2005-04-21 23:38:14 +0000549
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000550//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000551// Fast Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000552//===----------------------------------------------------------------------===//
553//
554// The X86 'fast' calling convention passes up to two integer arguments in
555// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
556// and requires that the callee pop its arguments off the stack (allowing proper
557// tail calls), and has the same return value conventions as C calling convs.
558//
Chris Lattner9b29fe22005-05-13 23:49:10 +0000559// This calling convention always arranges for the callee pop value to be 8n+4
560// bytes, which is needed for tail recursion elimination and stack alignment
561// reasons.
562//
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000563// Note that this can be enhanced in the future to pass fp vals in registers
564// (when we have a global fp allocator) and do other tricks.
565//
Chris Lattner0b17b452005-05-13 07:38:09 +0000566
567/// AddLiveIn - This helper function adds the specified physical register to the
568/// MachineFunction as a live in value. It also creates a corresponding virtual
569/// register for it.
570static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
571 TargetRegisterClass *RC) {
572 assert(RC->contains(PReg) && "Not the correct regclass!");
573 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
574 MF.addLiveIn(PReg, VReg);
575 return VReg;
576}
577
578
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000579std::vector<SDOperand>
580X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
581 std::vector<SDOperand> ArgValues;
582
583 MachineFunction &MF = DAG.getMachineFunction();
584 MachineFrameInfo *MFI = MF.getFrameInfo();
585
586 // Add DAG nodes to load the arguments... On entry to a function the stack
587 // frame looks like this:
588 //
589 // [ESP] -- return address
590 // [ESP + 4] -- first nonreg argument (leftmost lexically)
591 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
592 // ...
593 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
594
595 // Keep track of the number of integer regs passed so far. This can be either
596 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
597 // used).
598 unsigned NumIntRegs = 0;
599
600 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
601 MVT::ValueType ObjectVT = getValueType(I->getType());
602 unsigned ArgIncrement = 4;
603 unsigned ObjSize = 0;
604 SDOperand ArgValue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000605
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000606 switch (ObjectVT) {
607 default: assert(0 && "Unhandled argument type!");
608 case MVT::i1:
609 case MVT::i8:
610 if (NumIntRegs < 2) {
611 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000612 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
613 X86::R8RegisterClass);
614 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000615 DAG.setRoot(ArgValue.getValue(1));
616 }
617 ++NumIntRegs;
618 break;
619 }
620
621 ObjSize = 1;
622 break;
623 case MVT::i16:
624 if (NumIntRegs < 2) {
625 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000626 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
627 X86::R16RegisterClass);
628 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000629 DAG.setRoot(ArgValue.getValue(1));
630 }
631 ++NumIntRegs;
632 break;
633 }
634 ObjSize = 2;
635 break;
636 case MVT::i32:
637 if (NumIntRegs < 2) {
638 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000639 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
640 X86::R32RegisterClass);
641 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000642 DAG.setRoot(ArgValue.getValue(1));
643 }
644 ++NumIntRegs;
645 break;
646 }
647 ObjSize = 4;
648 break;
649 case MVT::i64:
650 if (NumIntRegs == 0) {
651 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000652 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
653 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000654
Chris Lattner0b17b452005-05-13 07:38:09 +0000655 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
656 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000657 DAG.setRoot(Hi.getValue(1));
658
659 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
660 }
661 NumIntRegs = 2;
662 break;
663 } else if (NumIntRegs == 1) {
664 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000665 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
666 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000667 DAG.setRoot(Low.getValue(1));
668
669 // Load the high part from memory.
670 // Create the frame index object for this incoming parameter...
671 int FI = MFI->CreateFixedObject(4, ArgOffset);
672 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
673 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
674 DAG.getSrcValue(NULL));
675 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
676 }
677 ArgOffset += 4;
678 NumIntRegs = 2;
679 break;
680 }
681 ObjSize = ArgIncrement = 8;
682 break;
683 case MVT::f32: ObjSize = 4; break;
684 case MVT::f64: ObjSize = ArgIncrement = 8; break;
685 }
686
687 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
688 // dead loads.
689 if (ObjSize && !I->use_empty()) {
690 // Create the frame index object for this incoming parameter...
691 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
692
693 // Create the SelectionDAG nodes corresponding to a load from this
694 // parameter.
695 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
696
697 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
698 DAG.getSrcValue(NULL));
699 } else if (ArgValue.Val == 0) {
700 if (MVT::isInteger(ObjectVT))
701 ArgValue = DAG.getConstant(0, ObjectVT);
702 else
703 ArgValue = DAG.getConstantFP(0, ObjectVT);
704 }
705 ArgValues.push_back(ArgValue);
706
707 if (ObjSize)
708 ArgOffset += ArgIncrement; // Move on to the next argument.
709 }
710
Chris Lattner9b29fe22005-05-13 23:49:10 +0000711 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
712 // arguments and the arguments after the retaddr has been pushed are aligned.
713 if ((ArgOffset & 7) == 0)
714 ArgOffset += 4;
715
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000716 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
717 ReturnAddrIndex = 0; // No return address slot generated yet.
718 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000719 BytesCallerReserves = 0;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000720
721 // Finally, inform the code generator which regs we return values in.
722 switch (getValueType(F.getReturnType())) {
723 default: assert(0 && "Unknown type!");
724 case MVT::isVoid: break;
725 case MVT::i1:
726 case MVT::i8:
727 case MVT::i16:
728 case MVT::i32:
729 MF.addLiveOut(X86::EAX);
730 break;
731 case MVT::i64:
732 MF.addLiveOut(X86::EAX);
733 MF.addLiveOut(X86::EDX);
734 break;
735 case MVT::f32:
736 case MVT::f64:
737 MF.addLiveOut(X86::ST0);
738 break;
739 }
740 return ArgValues;
741}
742
743std::pair<SDOperand, SDOperand>
744X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000745 bool isTailCall, SDOperand Callee,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000746 ArgListTy &Args, SelectionDAG &DAG) {
747 // Count how many bytes are to be pushed on the stack.
748 unsigned NumBytes = 0;
749
750 // Keep track of the number of integer regs passed so far. This can be either
751 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
752 // used).
753 unsigned NumIntRegs = 0;
754
755 for (unsigned i = 0, e = Args.size(); i != e; ++i)
756 switch (getValueType(Args[i].second)) {
757 default: assert(0 && "Unknown value type!");
758 case MVT::i1:
759 case MVT::i8:
760 case MVT::i16:
761 case MVT::i32:
762 if (NumIntRegs < 2) {
763 ++NumIntRegs;
764 break;
765 }
766 // fall through
767 case MVT::f32:
768 NumBytes += 4;
769 break;
770 case MVT::i64:
771 if (NumIntRegs == 0) {
772 NumIntRegs = 2;
773 break;
774 } else if (NumIntRegs == 1) {
775 NumIntRegs = 2;
776 NumBytes += 4;
777 break;
778 }
779
780 // fall through
781 case MVT::f64:
782 NumBytes += 8;
783 break;
784 }
785
Chris Lattner9b29fe22005-05-13 23:49:10 +0000786 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
787 // arguments and the arguments after the retaddr has been pushed are aligned.
788 if ((NumBytes & 7) == 0)
789 NumBytes += 4;
790
Chris Lattner2dce7032005-05-12 23:24:06 +0000791 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000792 DAG.getConstant(NumBytes, getPointerTy()));
793
794 // Arguments go on the stack in reverse order, as specified by the ABI.
795 unsigned ArgOffset = 0;
796 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
797 DAG.getEntryNode());
798 NumIntRegs = 0;
799 std::vector<SDOperand> Stores;
800 std::vector<SDOperand> RegValuesToPass;
801 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
802 switch (getValueType(Args[i].second)) {
803 default: assert(0 && "Unexpected ValueType for argument!");
804 case MVT::i1:
805 case MVT::i8:
806 case MVT::i16:
807 case MVT::i32:
808 if (NumIntRegs < 2) {
809 RegValuesToPass.push_back(Args[i].first);
810 ++NumIntRegs;
811 break;
812 }
813 // Fall through
814 case MVT::f32: {
815 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
816 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
817 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
818 Args[i].first, PtrOff,
819 DAG.getSrcValue(NULL)));
820 ArgOffset += 4;
821 break;
822 }
823 case MVT::i64:
824 if (NumIntRegs < 2) { // Can pass part of it in regs?
825 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
826 Args[i].first, DAG.getConstant(1, MVT::i32));
827 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
828 Args[i].first, DAG.getConstant(0, MVT::i32));
829 RegValuesToPass.push_back(Lo);
830 ++NumIntRegs;
831 if (NumIntRegs < 2) { // Pass both parts in regs?
832 RegValuesToPass.push_back(Hi);
833 ++NumIntRegs;
834 } else {
835 // Pass the high part in memory.
836 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
837 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
838 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner18b2c2f2005-05-14 12:03:10 +0000839 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000840 ArgOffset += 4;
841 }
842 break;
843 }
844 // Fall through
845 case MVT::f64:
846 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
847 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
848 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
849 Args[i].first, PtrOff,
850 DAG.getSrcValue(NULL)));
851 ArgOffset += 8;
852 break;
853 }
854 }
855 if (!Stores.empty())
856 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
857
Chris Lattner9b29fe22005-05-13 23:49:10 +0000858 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
859 // arguments and the arguments after the retaddr has been pushed are aligned.
860 if ((ArgOffset & 7) == 0)
861 ArgOffset += 4;
862
Chris Lattner1b3520c2005-05-14 08:48:15 +0000863 std::vector<MVT::ValueType> RetVals;
864 MVT::ValueType RetTyVT = getValueType(RetTy);
865
866 RetVals.push_back(MVT::Other);
867
868 // The result values produced have to be legal. Promote the result.
869 switch (RetTyVT) {
870 case MVT::isVoid: break;
871 default:
872 RetVals.push_back(RetTyVT);
873 break;
874 case MVT::i1:
875 case MVT::i8:
876 case MVT::i16:
877 RetVals.push_back(MVT::i32);
878 break;
879 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000880 if (X86ScalarSSE)
881 RetVals.push_back(MVT::f32);
882 else
883 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000884 break;
885 case MVT::i64:
886 RetVals.push_back(MVT::i32);
887 RetVals.push_back(MVT::i32);
888 break;
889 }
890
891 std::vector<SDOperand> Ops;
892 Ops.push_back(Chain);
893 Ops.push_back(Callee);
894 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
895 // Callee pops all arg values on the stack.
896 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
897
898 // Pass register arguments as needed.
899 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
900
901 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
902 RetVals, Ops);
903 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
904
905 SDOperand ResultVal;
906 switch (RetTyVT) {
907 case MVT::isVoid: break;
908 default:
909 ResultVal = TheCall.getValue(1);
910 break;
911 case MVT::i1:
912 case MVT::i8:
913 case MVT::i16:
914 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
915 break;
916 case MVT::f32:
917 // FIXME: we would really like to remember that this FP_ROUND operation is
918 // okay to eliminate if we allow excess FP precision.
919 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
920 break;
921 case MVT::i64:
922 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
923 TheCall.getValue(2));
924 break;
925 }
926
927 return std::make_pair(ResultVal, Chain);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000928}
929
Chris Lattnerdd66a412005-05-15 05:46:45 +0000930SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
931 if (ReturnAddrIndex == 0) {
932 // Set up a frame object for the return address.
933 MachineFunction &MF = DAG.getMachineFunction();
934 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
935 }
936
937 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
938}
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000939
940
Chris Lattner9f59d282005-01-09 00:01:27 +0000941
942std::pair<SDOperand, SDOperand> X86TargetLowering::
943LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
944 SelectionDAG &DAG) {
945 SDOperand Result;
946 if (Depth) // Depths > 0 not supported yet!
947 Result = DAG.getConstant(0, getPointerTy());
948 else {
Chris Lattnerdd66a412005-05-15 05:46:45 +0000949 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000950 if (!isFrameAddress)
951 // Just load the return address
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000952 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
953 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000954 else
955 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
956 DAG.getConstant(4, MVT::i32));
957 }
958 return std::make_pair(Result, Chain);
959}
Chris Lattner88c8a232005-01-07 07:49:41 +0000960
Chris Lattner507a2752005-07-16 00:28:20 +0000961//===----------------------------------------------------------------------===//
962// X86 Custom Lowering Hooks
963//===----------------------------------------------------------------------===//
964
Chris Lattnera36117b2005-05-14 06:52:07 +0000965/// LowerOperation - Provide custom lowering hooks for some operations.
966///
967SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
968 switch (Op.getOpcode()) {
969 default: assert(0 && "Should not custom lower this!");
Chris Lattner67756e22005-07-29 00:40:01 +0000970 case ISD::SINT_TO_FP: {
Chris Lattnera36117b2005-05-14 06:52:07 +0000971 assert(Op.getValueType() == MVT::f64 &&
972 Op.getOperand(0).getValueType() == MVT::i64 &&
973 "Unknown SINT_TO_FP to lower!");
974 // We lower sint64->FP into a store to a temporary stack slot, followed by a
975 // FILD64m node.
976 MachineFunction &MF = DAG.getMachineFunction();
977 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
978 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
979 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
980 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
981 std::vector<MVT::ValueType> RTs;
982 RTs.push_back(MVT::f64);
983 RTs.push_back(MVT::Other);
984 std::vector<SDOperand> Ops;
985 Ops.push_back(Store);
986 Ops.push_back(StackSlot);
987 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
988 }
Chris Lattner67756e22005-07-29 00:40:01 +0000989 case ISD::FP_TO_SINT: {
990 assert(Op.getValueType() == MVT::i64 &&
991 Op.getOperand(0).getValueType() == MVT::f64 &&
992 "Unknown FP_TO_SINT to lower!");
993 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
994 // stack slot.
995 MachineFunction &MF = DAG.getMachineFunction();
996 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
997 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
998
999 // Build the FISTP64
1000 std::vector<SDOperand> Ops;
1001 Ops.push_back(DAG.getEntryNode());
1002 Ops.push_back(Op.getOperand(0));
1003 Ops.push_back(StackSlot);
Chris Lattner6dc60e82005-07-29 00:54:34 +00001004 SDOperand FIST = DAG.getNode(X86ISD::FP_TO_INT64_IN_MEM, MVT::Other, Ops);
Chris Lattner67756e22005-07-29 00:40:01 +00001005
1006 // Load the result.
Chris Lattner6dc60e82005-07-29 00:54:34 +00001007 return DAG.getLoad(MVT::i64, FIST, StackSlot, DAG.getSrcValue(NULL));
Chris Lattner67756e22005-07-29 00:40:01 +00001008 }
1009 }
Chris Lattnera36117b2005-05-14 06:52:07 +00001010}
1011
1012
1013//===----------------------------------------------------------------------===//
1014// Pattern Matcher Implementation
1015//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +00001016
Chris Lattnera7acdda2005-01-18 01:06:26 +00001017namespace {
1018 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
1019 /// SDOperand's instead of register numbers for the leaves of the matched
1020 /// tree.
1021 struct X86ISelAddressMode {
1022 enum {
1023 RegBase,
1024 FrameIndexBase,
1025 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001026
Chris Lattnera7acdda2005-01-18 01:06:26 +00001027 struct { // This is really a union, discriminated by BaseType!
1028 SDOperand Reg;
1029 int FrameIndex;
1030 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001031
Chris Lattnera7acdda2005-01-18 01:06:26 +00001032 unsigned Scale;
1033 SDOperand IndexReg;
1034 unsigned Disp;
1035 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001036
Chris Lattnera7acdda2005-01-18 01:06:26 +00001037 X86ISelAddressMode()
1038 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1039 }
1040 };
1041}
Chris Lattner88c8a232005-01-07 07:49:41 +00001042
1043
1044namespace {
1045 Statistic<>
1046 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1047
1048 //===--------------------------------------------------------------------===//
1049 /// ISel - X86 specific code to select X86 machine instructions for
1050 /// SelectionDAG operations.
1051 ///
1052 class ISel : public SelectionDAGISel {
1053 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1054 /// register should set this to true.
1055 bool ContainsFPCode;
1056
1057 /// X86Lowering - This object fully describes how to lower LLVM code to an
1058 /// X86-specific SelectionDAG.
1059 X86TargetLowering X86Lowering;
1060
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001061 /// RegPressureMap - This keeps an approximate count of the number of
1062 /// registers required to evaluate each node in the graph.
1063 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +00001064
1065 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1066 /// vreg the value is produced in, so we only emit one copy of each compiled
1067 /// tree.
1068 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +00001069
Chris Lattnerdd66a412005-05-15 05:46:45 +00001070 /// TheDAG - The DAG being selected during Select* operations.
1071 SelectionDAG *TheDAG;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001072
1073 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanf26625e2005-07-12 01:41:54 +00001074 /// make the right decision when generating code for different targets.
1075 const X86Subtarget *Subtarget;
Chris Lattner88c8a232005-01-07 07:49:41 +00001076 public:
1077 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanf26625e2005-07-12 01:41:54 +00001078 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner88c8a232005-01-07 07:49:41 +00001079 }
1080
Chris Lattnere1e844c2005-01-21 21:35:14 +00001081 virtual const char *getPassName() const {
1082 return "X86 Pattern Instruction Selection";
1083 }
1084
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001085 unsigned getRegPressure(SDOperand O) {
1086 return RegPressureMap[O.Val];
1087 }
1088 unsigned ComputeRegPressure(SDOperand O);
1089
Chris Lattner88c8a232005-01-07 07:49:41 +00001090 /// InstructionSelectBasicBlock - This callback is invoked by
1091 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +00001092 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +00001093
Chris Lattner0b17b452005-05-13 07:38:09 +00001094 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1095
Chris Lattner30607ec2005-01-25 20:03:11 +00001096 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1097 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +00001098 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +00001099 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +00001100 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001101 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +00001102 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner1d13a922005-01-10 22:10:13 +00001103 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1104 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +00001105 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001106
1107 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1108 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1109 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +00001110 bool EmitPotentialTailCall(SDNode *Node);
1111 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +00001112 void Select(SDOperand N);
1113 };
1114}
1115
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001116/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1117/// the main function.
1118static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1119 MachineFrameInfo *MFI) {
1120 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1121 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1122 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1123
1124 // Set the high part to be 64-bit precision.
1125 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1126 CWFrameIdx, 1).addImm(2);
1127
1128 // Reload the modified control word now.
1129 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1130}
1131
Chris Lattner0b17b452005-05-13 07:38:09 +00001132void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1133 // If this function has live-in values, emit the copies from pregs to vregs at
1134 // the top of the function, before anything else.
1135 MachineBasicBlock *BB = MF.begin();
1136 if (MF.livein_begin() != MF.livein_end()) {
1137 SSARegMap *RegMap = MF.getSSARegMap();
1138 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1139 E = MF.livein_end(); LI != E; ++LI) {
1140 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1141 if (RC == X86::R8RegisterClass) {
1142 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1143 } else if (RC == X86::R16RegisterClass) {
1144 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1145 } else if (RC == X86::R32RegisterClass) {
1146 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1147 } else if (RC == X86::RFPRegisterClass) {
1148 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begeman8a093362005-07-06 18:59:04 +00001149 } else if (RC == X86::RXMMRegisterClass) {
1150 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner0b17b452005-05-13 07:38:09 +00001151 } else {
1152 assert(0 && "Unknown regclass!");
1153 }
1154 }
1155 }
1156
1157
1158 // If this is main, emit special code for main.
1159 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1160 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1161}
1162
1163
Chris Lattner6fba62d62005-01-12 04:21:28 +00001164/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1165/// when it has created a SelectionDAG for us to codegen.
1166void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1167 // While we're doing this, keep track of whether we see any FP code for
1168 // FP_REG_KILL insertion.
1169 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001170 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001171
1172 // Scan the PHI nodes that already are inserted into this basic block. If any
1173 // of them is a PHI of a floating point value, we need to insert an
1174 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001175 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +00001176 if (BB != MF->begin())
1177 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1178 I != E; ++I) {
1179 assert(I->getOpcode() == X86::PHI &&
1180 "Isn't just PHI nodes?");
1181 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1182 X86::RFPRegisterClass) {
1183 ContainsFPCode = true;
1184 break;
1185 }
Chris Lattner6fba62d62005-01-12 04:21:28 +00001186 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001187
Chris Lattner6fba62d62005-01-12 04:21:28 +00001188 // Compute the RegPressureMap, which is an approximation for the number of
1189 // registers required to compute each node.
1190 ComputeRegPressure(DAG.getRoot());
1191
Chris Lattnerdd66a412005-05-15 05:46:45 +00001192 TheDAG = &DAG;
1193
Chris Lattner6fba62d62005-01-12 04:21:28 +00001194 // Codegen the basic block.
1195 Select(DAG.getRoot());
1196
Chris Lattnerdd66a412005-05-15 05:46:45 +00001197 TheDAG = 0;
1198
Chris Lattner6fba62d62005-01-12 04:21:28 +00001199 // Finally, look at all of the successors of this block. If any contain a PHI
1200 // node of FP type, we need to insert an FP_REG_KILL in this block.
1201 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1202 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1203 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1204 I != E && I->getOpcode() == X86::PHI; ++I) {
1205 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1206 X86::RFPRegisterClass) {
1207 ContainsFPCode = true;
1208 break;
1209 }
1210 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001211
Chris Lattner6972c312005-05-09 03:36:39 +00001212 // Final check, check LLVM BB's that are successors to the LLVM BB
1213 // corresponding to BB for FP PHI nodes.
1214 const BasicBlock *LLVMBB = BB->getBasicBlock();
1215 const PHINode *PN;
1216 if (!ContainsFPCode)
1217 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1218 SI != E && !ContainsFPCode; ++SI)
1219 for (BasicBlock::const_iterator II = SI->begin();
1220 (PN = dyn_cast<PHINode>(II)); ++II)
1221 if (PN->getType()->isFloatingPoint()) {
1222 ContainsFPCode = true;
1223 break;
1224 }
1225
1226
Chris Lattner6fba62d62005-01-12 04:21:28 +00001227 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1228 // only occurs due to the floating point stackifier not being aggressive
1229 // enough to handle arbitrary global stackification.
1230 //
1231 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1232 // defines a floating point virtual register.
1233 //
1234 // When the global register allocators (like linear scan) finally update live
1235 // variable analysis, we can keep floating point values in registers across
1236 // basic blocks. This will be a huge win, but we are waiting on the global
1237 // allocators before we can do this.
1238 //
Chris Lattner472a2652005-03-30 01:10:00 +00001239 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +00001240 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1241 ++NumFPKill;
1242 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001243
Chris Lattner6fba62d62005-01-12 04:21:28 +00001244 // Clear state used for selection.
1245 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001246 RegPressureMap.clear();
1247}
1248
1249
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001250// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1251// for the number of registers required to compute each node. This is basically
1252// computing a generalized form of the Sethi-Ullman number for each node.
1253unsigned ISel::ComputeRegPressure(SDOperand O) {
1254 SDNode *N = O.Val;
1255 unsigned &Result = RegPressureMap[N];
1256 if (Result) return Result;
1257
Chris Lattner8fea42b2005-01-11 03:37:59 +00001258 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1259 // higher fixed cost??
1260
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001261 if (N->getNumOperands() == 0) {
1262 Result = 1;
1263 } else {
1264 unsigned MaxRegUse = 0;
1265 unsigned NumExtraMaxRegUsers = 0;
1266 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1267 unsigned Regs;
1268 if (N->getOperand(i).getOpcode() == ISD::Constant)
1269 Regs = 0;
1270 else
1271 Regs = ComputeRegPressure(N->getOperand(i));
1272 if (Regs > MaxRegUse) {
1273 MaxRegUse = Regs;
1274 NumExtraMaxRegUsers = 0;
1275 } else if (Regs == MaxRegUse &&
1276 N->getOperand(i).getValueType() != MVT::Other) {
1277 ++NumExtraMaxRegUsers;
1278 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001279 }
Chris Lattnerca318ed2005-01-17 22:56:09 +00001280
1281 if (O.getOpcode() != ISD::TokenFactor)
1282 Result = MaxRegUse+NumExtraMaxRegUsers;
1283 else
Chris Lattnera5d137f2005-01-17 23:02:13 +00001284 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001285 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001286
Chris Lattner75bac9f2005-01-11 23:21:30 +00001287 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001288 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001289}
1290
Chris Lattner5b04f332005-01-20 16:50:16 +00001291/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1292/// The DAG cannot have cycles in it, by definition, so the visited set is not
1293/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1294/// reuse, so it prevents exponential cases.
1295///
1296static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1297 std::set<SDNode*> &Visited) {
1298 if (N == Op) return true; // Found it.
1299 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +00001300 if (Node->getNumOperands() == 0 || // Leaf?
1301 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +00001302 if (!Visited.insert(Node).second) return false; // Already visited?
1303
1304 // Recurse for the first N-1 operands.
1305 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1306 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1307 return true;
1308
1309 // Tail recurse for the last operand.
1310 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1311}
1312
Chris Lattnera7acdda2005-01-18 01:06:26 +00001313X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1314 X86AddressMode Result;
1315
1316 // If we need to emit two register operands, emit the one with the highest
1317 // register pressure first.
1318 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1319 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001320 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001321 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001322 std::set<SDNode*> Visited;
1323 EmitBaseThenIndex = true;
1324 // If Base ends up pointing to Index, we must emit index first. This is
1325 // because of the way we fold loads, we may end up doing bad things with
1326 // the folded add.
1327 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1328 EmitBaseThenIndex = false;
1329 } else {
1330 std::set<SDNode*> Visited;
1331 EmitBaseThenIndex = false;
1332 // If Base ends up pointing to Index, we must emit index first. This is
1333 // because of the way we fold loads, we may end up doing bad things with
1334 // the folded add.
1335 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1336 EmitBaseThenIndex = true;
1337 }
1338
1339 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001340 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1341 Result.IndexReg = SelectExpr(IAM.IndexReg);
1342 } else {
1343 Result.IndexReg = SelectExpr(IAM.IndexReg);
1344 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1345 }
Chris Lattner5b04f332005-01-20 16:50:16 +00001346
Chris Lattnera7acdda2005-01-18 01:06:26 +00001347 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1348 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1349 } else if (IAM.IndexReg.Val) {
1350 Result.IndexReg = SelectExpr(IAM.IndexReg);
1351 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001352
Chris Lattnera7acdda2005-01-18 01:06:26 +00001353 switch (IAM.BaseType) {
1354 case X86ISelAddressMode::RegBase:
1355 Result.BaseType = X86AddressMode::RegBase;
1356 break;
1357 case X86ISelAddressMode::FrameIndexBase:
1358 Result.BaseType = X86AddressMode::FrameIndexBase;
1359 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1360 break;
1361 default:
1362 assert(0 && "Unknown base type!");
1363 break;
1364 }
1365 Result.Scale = IAM.Scale;
1366 Result.Disp = IAM.Disp;
1367 Result.GV = IAM.GV;
1368 return Result;
1369}
1370
1371/// SelectAddress - Pattern match the maximal addressing mode for this node and
1372/// emit all of the leaf registers.
1373void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1374 X86ISelAddressMode IAM;
1375 MatchAddress(N, IAM);
1376 AM = SelectAddrExprs(IAM);
1377}
1378
1379/// MatchAddress - Add the specified node to the specified addressing mode,
1380/// returning true if it cannot be done. This just pattern matches for the
1381/// addressing mode, it does not cause any code to be emitted. For that, use
1382/// SelectAddress.
1383bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001384 switch (N.getOpcode()) {
1385 default: break;
1386 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +00001387 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1388 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +00001389 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1390 return false;
1391 }
1392 break;
1393 case ISD::GlobalAddress:
1394 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +00001395 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1396 // For Darwin, external and weak symbols are indirect, so we want to load
1397 // the value at address GV, not the value of GV itself. This means that
1398 // the GlobalAddress must be in the base or index register of the address,
1399 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001400 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001401 (GV->hasWeakLinkage() || GV->isExternal())) {
1402 break;
1403 } else {
1404 AM.GV = GV;
1405 return false;
1406 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001407 }
1408 break;
1409 case ISD::Constant:
1410 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1411 return false;
1412 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001413 // We might have folded the load into this shift, so don't regen the value
1414 // if so.
1415 if (ExprMap.count(N)) break;
1416
Chris Lattnera7acdda2005-01-18 01:06:26 +00001417 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +00001418 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1419 unsigned Val = CN->getValue();
1420 if (Val == 1 || Val == 2 || Val == 3) {
1421 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001422 SDOperand ShVal = N.Val->getOperand(0);
1423
1424 // Okay, we know that we have a scale by now. However, if the scaled
1425 // value is an add of something and a constant, we can fold the
1426 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001427 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001428 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001429 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001430 ConstantSDNode *AddVal =
1431 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1432 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +00001433 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001434 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001435 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001436 return false;
1437 }
1438 }
1439 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001440 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001441 // We might have folded the load into this mul, so don't regen the value if
1442 // so.
1443 if (ExprMap.count(N)) break;
1444
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001445 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +00001446 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1447 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001448 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1449 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1450 AM.Scale = unsigned(CN->getValue())-1;
1451
1452 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001453 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001454
1455 // Okay, we know that we have a scale by now. However, if the scaled
1456 // value is an add of something and a constant, we can fold the
1457 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001458 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001459 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001460 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001461 ConstantSDNode *AddVal =
1462 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1463 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00001464 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001465 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001466 }
1467
1468 AM.IndexReg = AM.Base.Reg = Reg;
1469 return false;
1470 }
1471 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001472
1473 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +00001474 // We might have folded the load into this mul, so don't regen the value if
1475 // so.
1476 if (ExprMap.count(N)) break;
1477
Chris Lattnera7acdda2005-01-18 01:06:26 +00001478 X86ISelAddressMode Backup = AM;
1479 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1480 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +00001481 return false;
1482 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001483 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1484 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +00001485 return false;
1486 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +00001487 break;
1488 }
1489 }
1490
Chris Lattner378262d2005-01-11 04:40:19 +00001491 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +00001492 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +00001493 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001494 if (AM.IndexReg.Val == 0) {
1495 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +00001496 AM.Scale = 1;
1497 return false;
1498 }
1499
1500 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +00001501 return true;
Chris Lattner378262d2005-01-11 04:40:19 +00001502 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001503
1504 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001505 AM.BaseType = X86ISelAddressMode::RegBase;
1506 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +00001507 return false;
1508}
1509
1510/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1511/// assuming that the temporary registers are in the 8-bit register class.
1512///
1513/// Tmp1 = setcc1
1514/// Tmp2 = setcc2
1515/// DestReg = logicalop Tmp1, Tmp2
1516///
1517static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1518 unsigned SetCC2, unsigned LogicalOp,
1519 unsigned DestReg) {
1520 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1521 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1522 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1523 BuildMI(BB, SetCC1, 0, Tmp1);
1524 BuildMI(BB, SetCC2, 0, Tmp2);
1525 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1526}
1527
1528/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1529/// condition codes match the specified SetCCOpcode. Note that some conditions
1530/// require multiple instructions to generate the correct value.
1531static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1532 ISD::CondCode SetCCOpcode, bool isFP) {
1533 unsigned Opc;
1534 if (!isFP) {
1535 switch (SetCCOpcode) {
1536 default: assert(0 && "Illegal integer SetCC!");
1537 case ISD::SETEQ: Opc = X86::SETEr; break;
1538 case ISD::SETGT: Opc = X86::SETGr; break;
1539 case ISD::SETGE: Opc = X86::SETGEr; break;
1540 case ISD::SETLT: Opc = X86::SETLr; break;
1541 case ISD::SETLE: Opc = X86::SETLEr; break;
1542 case ISD::SETNE: Opc = X86::SETNEr; break;
1543 case ISD::SETULT: Opc = X86::SETBr; break;
1544 case ISD::SETUGT: Opc = X86::SETAr; break;
1545 case ISD::SETULE: Opc = X86::SETBEr; break;
1546 case ISD::SETUGE: Opc = X86::SETAEr; break;
1547 }
1548 } else {
1549 // On a floating point condition, the flags are set as follows:
1550 // ZF PF CF op
1551 // 0 | 0 | 0 | X > Y
1552 // 0 | 0 | 1 | X < Y
1553 // 1 | 0 | 0 | X == Y
1554 // 1 | 1 | 1 | unordered
1555 //
1556 switch (SetCCOpcode) {
1557 default: assert(0 && "Invalid FP setcc!");
1558 case ISD::SETUEQ:
1559 case ISD::SETEQ:
1560 Opc = X86::SETEr; // True if ZF = 1
1561 break;
1562 case ISD::SETOGT:
1563 case ISD::SETGT:
1564 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1565 break;
1566 case ISD::SETOGE:
1567 case ISD::SETGE:
1568 Opc = X86::SETAEr; // True if CF = 0
1569 break;
1570 case ISD::SETULT:
1571 case ISD::SETLT:
1572 Opc = X86::SETBr; // True if CF = 1
1573 break;
1574 case ISD::SETULE:
1575 case ISD::SETLE:
1576 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1577 break;
1578 case ISD::SETONE:
1579 case ISD::SETNE:
1580 Opc = X86::SETNEr; // True if ZF = 0
1581 break;
1582 case ISD::SETUO:
1583 Opc = X86::SETPr; // True if PF = 1
1584 break;
1585 case ISD::SETO:
1586 Opc = X86::SETNPr; // True if PF = 0
1587 break;
1588 case ISD::SETOEQ: // !PF & ZF
1589 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1590 return;
1591 case ISD::SETOLT: // !PF & CF
1592 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1593 return;
1594 case ISD::SETOLE: // !PF & (CF || ZF)
1595 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1596 return;
1597 case ISD::SETUGT: // PF | (!ZF & !CF)
1598 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1599 return;
1600 case ISD::SETUGE: // PF | !CF
1601 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1602 return;
1603 case ISD::SETUNE: // PF | !ZF
1604 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1605 return;
1606 }
1607 }
1608 BuildMI(BB, Opc, 0, DestReg);
1609}
1610
1611
1612/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1613/// the Dest block if the Cond condition is true. If we cannot fold this
1614/// condition into the branch, return true.
1615///
Chris Lattner37ed2852005-01-11 04:06:27 +00001616bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1617 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001618 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1619 // B) using two conditional branches instead of one condbr, two setcc's, and
1620 // an or.
1621 if ((Cond.getOpcode() == ISD::OR ||
1622 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1623 // And and or set the flags for us, so there is no need to emit a TST of the
1624 // result. It is only safe to do this if there is only a single use of the
1625 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +00001626 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +00001627 SelectExpr(Cond);
1628 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1629 return false;
1630 }
1631
1632 // Codegen br not C -> JE.
1633 if (Cond.getOpcode() == ISD::XOR)
1634 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1635 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +00001636 unsigned CondR;
1637 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1638 Select(Chain);
1639 CondR = SelectExpr(Cond.Val->getOperand(0));
1640 } else {
1641 CondR = SelectExpr(Cond.Val->getOperand(0));
1642 Select(Chain);
1643 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001644 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1645 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1646 return false;
1647 }
1648
1649 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1650 if (SetCC == 0)
1651 return true; // Can only handle simple setcc's so far.
1652
1653 unsigned Opc;
1654
1655 // Handle integer conditions first.
1656 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1657 switch (SetCC->getCondition()) {
1658 default: assert(0 && "Illegal integer SetCC!");
1659 case ISD::SETEQ: Opc = X86::JE; break;
1660 case ISD::SETGT: Opc = X86::JG; break;
1661 case ISD::SETGE: Opc = X86::JGE; break;
1662 case ISD::SETLT: Opc = X86::JL; break;
1663 case ISD::SETLE: Opc = X86::JLE; break;
1664 case ISD::SETNE: Opc = X86::JNE; break;
1665 case ISD::SETULT: Opc = X86::JB; break;
1666 case ISD::SETUGT: Opc = X86::JA; break;
1667 case ISD::SETULE: Opc = X86::JBE; break;
1668 case ISD::SETUGE: Opc = X86::JAE; break;
1669 }
Chris Lattner37ed2852005-01-11 04:06:27 +00001670 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001671 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001672 BuildMI(BB, Opc, 1).addMBB(Dest);
1673 return false;
1674 }
1675
Chris Lattner88c8a232005-01-07 07:49:41 +00001676 unsigned Opc2 = 0; // Second branch if needed.
1677
1678 // On a floating point condition, the flags are set as follows:
1679 // ZF PF CF op
1680 // 0 | 0 | 0 | X > Y
1681 // 0 | 0 | 1 | X < Y
1682 // 1 | 0 | 0 | X == Y
1683 // 1 | 1 | 1 | unordered
1684 //
1685 switch (SetCC->getCondition()) {
1686 default: assert(0 && "Invalid FP setcc!");
1687 case ISD::SETUEQ:
1688 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1689 case ISD::SETOGT:
1690 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1691 case ISD::SETOGE:
1692 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1693 case ISD::SETULT:
1694 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1695 case ISD::SETULE:
1696 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1697 case ISD::SETONE:
1698 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1699 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1700 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1701 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1702 Opc = X86::JA; // ZF = 0 & CF = 0
1703 Opc2 = X86::JP; // PF = 1
1704 break;
1705 case ISD::SETUGE: // PF = 1 | CF = 0
1706 Opc = X86::JAE; // CF = 0
1707 Opc2 = X86::JP; // PF = 1
1708 break;
1709 case ISD::SETUNE: // PF = 1 | ZF = 0
1710 Opc = X86::JNE; // ZF = 0
1711 Opc2 = X86::JP; // PF = 1
1712 break;
1713 case ISD::SETOEQ: // PF = 0 & ZF = 1
1714 //X86::JNP, X86::JE
1715 //X86::AND8rr
1716 return true; // FIXME: Emit more efficient code for this branch.
1717 case ISD::SETOLT: // PF = 0 & CF = 1
1718 //X86::JNP, X86::JB
1719 //X86::AND8rr
1720 return true; // FIXME: Emit more efficient code for this branch.
1721 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1722 //X86::JNP, X86::JBE
1723 //X86::AND8rr
1724 return true; // FIXME: Emit more efficient code for this branch.
1725 }
1726
Chris Lattner37ed2852005-01-11 04:06:27 +00001727 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001728 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001729 BuildMI(BB, Opc, 1).addMBB(Dest);
1730 if (Opc2)
1731 BuildMI(BB, Opc2, 1).addMBB(Dest);
1732 return false;
1733}
1734
Chris Lattner1d13a922005-01-10 22:10:13 +00001735/// EmitSelectCC - Emit code into BB that performs a select operation between
1736/// the two registers RTrue and RFalse, generating a result into RDest. Return
1737/// true if the fold cannot be performed.
1738///
1739void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1740 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1741 enum Condition {
1742 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1743 NOT_SET
1744 } CondCode = NOT_SET;
1745
1746 static const unsigned CMOVTAB16[] = {
1747 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1748 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001749 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001750 };
1751 static const unsigned CMOVTAB32[] = {
1752 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1753 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001754 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001755 };
1756 static const unsigned CMOVTABFP[] = {
1757 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1758 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1759 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1760 };
Nate Begemana0b5e032005-07-15 00:38:55 +00001761 static const int SSE_CMOVTAB[] = {
Nate Begeman8a093362005-07-06 18:59:04 +00001762 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
Nate Begemana0b5e032005-07-15 00:38:55 +00001763 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0,
Nate Begeman8a093362005-07-06 18:59:04 +00001764 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1765 };
Chris Lattner1d13a922005-01-10 22:10:13 +00001766
1767 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1768 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1769 switch (SetCC->getCondition()) {
1770 default: assert(0 && "Unknown integer comparison!");
1771 case ISD::SETEQ: CondCode = EQ; break;
1772 case ISD::SETGT: CondCode = GT; break;
1773 case ISD::SETGE: CondCode = GE; break;
1774 case ISD::SETLT: CondCode = LT; break;
1775 case ISD::SETLE: CondCode = LE; break;
1776 case ISD::SETNE: CondCode = NE; break;
1777 case ISD::SETULT: CondCode = B; break;
1778 case ISD::SETUGT: CondCode = A; break;
1779 case ISD::SETULE: CondCode = BE; break;
1780 case ISD::SETUGE: CondCode = AE; break;
1781 }
Nate Begeman8a093362005-07-06 18:59:04 +00001782 } else if (X86ScalarSSE) {
1783 switch (SetCC->getCondition()) {
1784 default: assert(0 && "Unknown scalar fp comparison!");
1785 case ISD::SETEQ: CondCode = EQ; break;
1786 case ISD::SETNE: CondCode = NE; break;
1787 case ISD::SETULT:
1788 case ISD::SETLT: CondCode = LT; break;
1789 case ISD::SETULE:
1790 case ISD::SETLE: CondCode = LE; break;
1791 case ISD::SETUGT:
1792 case ISD::SETGT: CondCode = GT; break;
1793 case ISD::SETUGE:
1794 case ISD::SETGE: CondCode = GE; break;
1795 }
Chris Lattner1d13a922005-01-10 22:10:13 +00001796 } else {
1797 // On a floating point condition, the flags are set as follows:
1798 // ZF PF CF op
1799 // 0 | 0 | 0 | X > Y
1800 // 0 | 0 | 1 | X < Y
1801 // 1 | 0 | 0 | X == Y
1802 // 1 | 1 | 1 | unordered
1803 //
1804 switch (SetCC->getCondition()) {
1805 default: assert(0 && "Unknown FP comparison!");
1806 case ISD::SETUEQ:
1807 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1808 case ISD::SETOGT:
1809 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1810 case ISD::SETOGE:
1811 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1812 case ISD::SETULT:
1813 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1814 case ISD::SETULE:
1815 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1816 case ISD::SETONE:
1817 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1818 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1819 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1820 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1821 case ISD::SETUGE: // PF = 1 | CF = 0
1822 case ISD::SETUNE: // PF = 1 | ZF = 0
1823 case ISD::SETOEQ: // PF = 0 & ZF = 1
1824 case ISD::SETOLT: // PF = 0 & CF = 1
1825 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1826 // We cannot emit this comparison as a single cmov.
1827 break;
1828 }
1829 }
1830 }
1831
Nate Begeman8a093362005-07-06 18:59:04 +00001832 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1833 // Others we will have to do the PowerPC thing and generate an MBB for the
1834 // true and false values and select between them with a PHI.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001835 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
Nate Begemana0b5e032005-07-15 00:38:55 +00001836 if (0 && CondCode != NOT_SET) {
1837 // FIXME: check for min and max
Nate Begeman8a093362005-07-06 18:59:04 +00001838 } else {
Nate Begemana0b5e032005-07-15 00:38:55 +00001839 // FIXME: emit a direct compare and branch rather than setting a cond reg
1840 // and testing it.
Nate Begeman8a093362005-07-06 18:59:04 +00001841 unsigned CondReg = SelectExpr(Cond);
1842 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1843
1844 // Create an iterator with which to insert the MBB for copying the false
1845 // value and the MBB to hold the PHI instruction for this SetCC.
1846 MachineBasicBlock *thisMBB = BB;
1847 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1848 ilist<MachineBasicBlock>::iterator It = BB;
1849 ++It;
1850
1851 // thisMBB:
1852 // ...
1853 // TrueVal = ...
1854 // cmpTY ccX, r1, r2
1855 // bCC sinkMBB
1856 // fallthrough --> copy0MBB
1857 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1858 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1859 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1860 MachineFunction *F = BB->getParent();
1861 F->getBasicBlockList().insert(It, copy0MBB);
1862 F->getBasicBlockList().insert(It, sinkMBB);
1863 // Update machine-CFG edges
1864 BB->addSuccessor(copy0MBB);
1865 BB->addSuccessor(sinkMBB);
1866
1867 // copy0MBB:
1868 // %FalseValue = ...
1869 // # fallthrough to sinkMBB
1870 BB = copy0MBB;
1871 // Update machine-CFG edges
1872 BB->addSuccessor(sinkMBB);
1873
1874 // sinkMBB:
1875 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1876 // ...
1877 BB = sinkMBB;
1878 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1879 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1880 }
1881 return;
1882 }
1883
Chris Lattner1d13a922005-01-10 22:10:13 +00001884 unsigned Opc = 0;
1885 if (CondCode != NOT_SET) {
1886 switch (SVT) {
1887 default: assert(0 && "Cannot select this type!");
1888 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1889 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001890 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001891 }
1892 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001893
Chris Lattner1d13a922005-01-10 22:10:13 +00001894 // Finally, if we weren't able to fold this, just emit the condition and test
1895 // it.
1896 if (CondCode == NOT_SET || Opc == 0) {
1897 // Get the condition into the zero flag.
1898 unsigned CondReg = SelectExpr(Cond);
1899 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1900
1901 switch (SVT) {
1902 default: assert(0 && "Cannot select this type!");
1903 case MVT::i16: Opc = X86::CMOVE16rr; break;
1904 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001905 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001906 }
1907 } else {
1908 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +00001909 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +00001910 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +00001911 }
1912 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1913}
1914
Chris Lattner3be6cd52005-01-17 01:34:14 +00001915void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001916 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +00001917 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1918 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001919 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001920 switch (RHS.getValueType()) {
1921 default: break;
1922 case MVT::i1:
1923 case MVT::i8: Opc = X86::CMP8mi; break;
1924 case MVT::i16: Opc = X86::CMP16mi; break;
1925 case MVT::i32: Opc = X86::CMP32mi; break;
1926 }
1927 if (Opc) {
1928 X86AddressMode AM;
1929 EmitFoldedLoad(LHS, AM);
1930 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1931 return;
1932 }
1933 }
1934
Chris Lattner88c8a232005-01-07 07:49:41 +00001935 switch (RHS.getValueType()) {
1936 default: break;
1937 case MVT::i1:
1938 case MVT::i8: Opc = X86::CMP8ri; break;
1939 case MVT::i16: Opc = X86::CMP16ri; break;
1940 case MVT::i32: Opc = X86::CMP32ri; break;
1941 }
1942 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001943 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00001944 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1945 return;
1946 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001947 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +00001948 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1949 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +00001950 unsigned Reg = SelectExpr(LHS);
1951 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1952 BuildMI(BB, X86::FNSTSW8r, 0);
1953 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001954 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001955 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001956 }
1957
Chris Lattner2cfce682005-01-12 02:02:48 +00001958 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001959 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001960 switch (RHS.getValueType()) {
1961 default: break;
1962 case MVT::i1:
1963 case MVT::i8: Opc = X86::CMP8mr; break;
1964 case MVT::i16: Opc = X86::CMP16mr; break;
1965 case MVT::i32: Opc = X86::CMP32mr; break;
1966 }
1967 if (Opc) {
1968 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001969 EmitFoldedLoad(LHS, AM);
1970 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001971 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1972 return;
1973 }
1974 }
1975
Chris Lattner88c8a232005-01-07 07:49:41 +00001976 switch (LHS.getValueType()) {
1977 default: assert(0 && "Cannot compare this value!");
1978 case MVT::i1:
1979 case MVT::i8: Opc = X86::CMP8rr; break;
1980 case MVT::i16: Opc = X86::CMP16rr; break;
1981 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001982 case MVT::f32: Opc = X86::UCOMISSrr; break;
1983 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001984 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001985 unsigned Tmp1, Tmp2;
1986 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1987 Tmp1 = SelectExpr(LHS);
1988 Tmp2 = SelectExpr(RHS);
1989 } else {
1990 Tmp2 = SelectExpr(RHS);
1991 Tmp1 = SelectExpr(LHS);
1992 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001993 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1994}
1995
Chris Lattner62b22422005-01-11 21:19:59 +00001996/// isFoldableLoad - Return true if this is a load instruction that can safely
1997/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001998bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1999 if (Op.getOpcode() == ISD::LOAD) {
2000 // FIXME: currently can't fold constant pool indexes.
2001 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2002 return false;
2003 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00002004 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00002005 // FIXME: currently can't fold constant pool indexes.
2006 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2007 return false;
2008 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002009 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00002010 }
Chris Lattner62b22422005-01-11 21:19:59 +00002011
2012 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00002013 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2014 if (ExprMap.count(Op.getValue(1))) return false;
2015 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00002016 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00002017
Chris Lattnera56d29d2005-01-17 06:26:58 +00002018 // If there is not just one use of its value, we cannot fold.
2019 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2020
2021 // Finally, we cannot fold the load into the operation if this would induce a
2022 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2023 // operand of the operation we are folding the load into) can possible use the
2024 // chain node defined by the load.
2025 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2026 std::set<SDNode*> Visited;
2027 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2028 return false;
2029 }
2030 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00002031}
2032
Chris Lattnera56d29d2005-01-17 06:26:58 +00002033
Chris Lattner62b22422005-01-11 21:19:59 +00002034/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2035/// and compute the address being loaded into AM.
2036void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2037 SDOperand Chain = Op.getOperand(0);
2038 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002039
Chris Lattner62b22422005-01-11 21:19:59 +00002040 if (getRegPressure(Chain) > getRegPressure(Address)) {
2041 Select(Chain);
2042 SelectAddress(Address, AM);
2043 } else {
2044 SelectAddress(Address, AM);
2045 Select(Chain);
2046 }
2047
2048 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00002049 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2050 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00002051 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00002052 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00002053}
2054
Chris Lattner29f58192005-01-19 07:37:26 +00002055// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2056// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2057// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2058// return true.
2059bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002060 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2061 // good!
2062 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2063 std::swap(Op1, Op2); // Op1 is the SHL now.
2064 } else {
2065 return false; // No match
2066 }
2067
2068 SDOperand ShlVal = Op1.getOperand(0);
2069 SDOperand ShlAmt = Op1.getOperand(1);
2070 SDOperand ShrVal = Op2.getOperand(0);
2071 SDOperand ShrAmt = Op2.getOperand(1);
2072
Chris Lattner29f58192005-01-19 07:37:26 +00002073 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2074
Chris Lattner41fe2012005-01-19 06:18:43 +00002075 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2076 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2077 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00002078 if (SubCST->getValue() == RegSize) {
2079 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00002080 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00002081 if (ShrVal == ShlVal) {
2082 unsigned Reg, ShAmt;
2083 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2084 Reg = SelectExpr(ShrVal);
2085 ShAmt = SelectExpr(ShrAmt);
2086 } else {
2087 ShAmt = SelectExpr(ShrAmt);
2088 Reg = SelectExpr(ShrVal);
2089 }
2090 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2091 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2092 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2093 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2094 return true;
2095 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002096 unsigned AReg, BReg;
2097 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002098 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002099 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002100 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00002101 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002102 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002103 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00002104 unsigned ShAmt = SelectExpr(ShrAmt);
2105 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2106 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2107 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00002108 return true;
2109 }
2110 }
2111
Chris Lattnerde87d1462005-01-19 08:07:05 +00002112 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2113 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2114 if (SubCST->getValue() == RegSize) {
2115 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2116 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2117 if (ShrVal == ShlVal) {
2118 unsigned Reg, ShAmt;
2119 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2120 Reg = SelectExpr(ShrVal);
2121 ShAmt = SelectExpr(ShlAmt);
2122 } else {
2123 ShAmt = SelectExpr(ShlAmt);
2124 Reg = SelectExpr(ShrVal);
2125 }
2126 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2127 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2128 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2129 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2130 return true;
2131 } else if (RegSize != 8) {
2132 unsigned AReg, BReg;
2133 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00002134 AReg = SelectExpr(ShlVal);
2135 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002136 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00002137 BReg = SelectExpr(ShrVal);
2138 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002139 }
2140 unsigned ShAmt = SelectExpr(ShlAmt);
2141 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2142 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2143 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2144 return true;
2145 }
2146 }
Chris Lattner41fe2012005-01-19 06:18:43 +00002147
Chris Lattnerde87d1462005-01-19 08:07:05 +00002148 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2149 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2150 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2151 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2152 // (A >> 5) | (A << 27) --> ROR A, 5
2153 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2154 if (ShrVal == ShlVal) {
2155 unsigned Reg = SelectExpr(ShrVal);
2156 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2157 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2158 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2159 return true;
2160 } else if (RegSize != 8) {
2161 unsigned AReg, BReg;
2162 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002163 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002164 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002165 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002166 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002167 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002168 }
2169 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2170 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2171 .addImm(ShrCst->getValue());
2172 return true;
2173 }
2174 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002175
Chris Lattner41fe2012005-01-19 06:18:43 +00002176 return false;
2177}
2178
Chris Lattner88c8a232005-01-07 07:49:41 +00002179unsigned ISel::SelectExpr(SDOperand N) {
2180 unsigned Result;
2181 unsigned Tmp1, Tmp2, Tmp3;
2182 unsigned Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002183 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00002184 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002185
Chris Lattner720a62e2005-01-14 22:37:41 +00002186 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002187 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2188 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2189 // Just use the specified register as our input.
2190 return cast<RegSDNode>(Node)->getReg();
2191 }
Chris Lattner720a62e2005-01-14 22:37:41 +00002192 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002193
Chris Lattner62b22422005-01-11 21:19:59 +00002194 unsigned &Reg = ExprMap[N];
2195 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002196
Chris Lattnera31d4c72005-04-02 04:01:14 +00002197 switch (N.getOpcode()) {
2198 default:
Chris Lattner62b22422005-01-11 21:19:59 +00002199 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00002200 MakeReg(N.getValueType()) : 1;
2201 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00002202 case X86ISD::TAILCALL:
2203 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00002204 // If this is a call instruction, make sure to prepare ALL of the result
2205 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002206 ExprMap[N.getValue(0)] = 1;
2207 if (Node->getNumValues() > 1) {
2208 Result = MakeReg(Node->getValueType(1));
2209 ExprMap[N.getValue(1)] = Result;
2210 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00002211 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00002212 } else {
2213 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00002214 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00002215 break;
2216 case ISD::ADD_PARTS:
2217 case ISD::SUB_PARTS:
2218 case ISD::SHL_PARTS:
2219 case ISD::SRL_PARTS:
2220 case ISD::SRA_PARTS:
2221 Result = MakeReg(Node->getValueType(0));
2222 ExprMap[N.getValue(0)] = Result;
2223 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2224 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2225 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002226 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002227
Chris Lattner88c8a232005-01-07 07:49:41 +00002228 switch (N.getOpcode()) {
2229 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00002230 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00002231 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00002232 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002233 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00002234 Tmp1 = SelectExpr(N.getOperand(0));
2235 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2236 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00002237 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002238 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00002239 Tmp1 = SelectExpr(N.getOperand(0));
2240 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2241 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002242 case ISD::CopyFromReg:
2243 Select(N.getOperand(0));
2244 if (Result == 1) {
2245 Reg = Result = ExprMap[N.getValue(0)] =
2246 MakeReg(N.getValue(0).getValueType());
2247 }
2248 switch (Node->getValueType(0)) {
2249 default: assert(0 && "Cannot CopyFromReg this!");
2250 case MVT::i1:
2251 case MVT::i8:
2252 BuildMI(BB, X86::MOV8rr, 1,
2253 Result).addReg(cast<RegSDNode>(Node)->getReg());
2254 return Result;
2255 case MVT::i16:
2256 BuildMI(BB, X86::MOV16rr, 1,
2257 Result).addReg(cast<RegSDNode>(Node)->getReg());
2258 return Result;
2259 case MVT::i32:
2260 BuildMI(BB, X86::MOV32rr, 1,
2261 Result).addReg(cast<RegSDNode>(Node)->getReg());
2262 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002263 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002264
Chris Lattner88c8a232005-01-07 07:49:41 +00002265 case ISD::FrameIndex:
2266 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2267 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2268 return Result;
2269 case ISD::ConstantPool:
2270 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2271 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2272 return Result;
2273 case ISD::ConstantFP:
2274 ContainsFPCode = true;
2275 Tmp1 = Result; // Intermediate Register
2276 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2277 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2278 Tmp1 = MakeReg(MVT::f64);
2279
2280 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2281 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2282 BuildMI(BB, X86::FLD0, 0, Tmp1);
2283 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2284 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2285 BuildMI(BB, X86::FLD1, 0, Tmp1);
2286 else
2287 assert(0 && "Unexpected constant!");
2288 if (Tmp1 != Result)
2289 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2290 return Result;
2291 case ISD::Constant:
2292 switch (N.getValueType()) {
2293 default: assert(0 && "Cannot use constants of this type!");
2294 case MVT::i1:
2295 case MVT::i8: Opc = X86::MOV8ri; break;
2296 case MVT::i16: Opc = X86::MOV16ri; break;
2297 case MVT::i32: Opc = X86::MOV32ri; break;
2298 }
2299 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2300 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00002301 case ISD::UNDEF:
2302 if (Node->getValueType(0) == MVT::f64) {
2303 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2304 BuildMI(BB, X86::FLD0, 0, Result);
2305 } else {
2306 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2307 }
2308 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002309 case ISD::GlobalAddress: {
2310 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00002311 // For Darwin, external and weak symbols are indirect, so we want to load
2312 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002313 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00002314 (GV->hasWeakLinkage() || GV->isExternal())) {
2315 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2316 .addGlobalAddress(GV, false, 0);
2317 } else {
2318 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2319 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002320 return Result;
2321 }
2322 case ISD::ExternalSymbol: {
2323 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2324 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2325 return Result;
2326 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002327 case ISD::ZERO_EXTEND: {
2328 int DestIs16 = N.getValueType() == MVT::i16;
2329 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00002330
2331 // FIXME: This hack is here for zero extension casts from bool to i8. This
2332 // would not be needed if bools were promoted by Legalize.
2333 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002334 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00002335 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2336 return Result;
2337 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002338
Chris Lattnera56d29d2005-01-17 06:26:58 +00002339 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002340 static const unsigned Opc[3] = {
2341 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2342 };
2343
2344 X86AddressMode AM;
2345 EmitFoldedLoad(N.getOperand(0), AM);
2346 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002347
Chris Lattnerb0eef822005-01-11 23:33:00 +00002348 return Result;
2349 }
2350
Chris Lattner88c8a232005-01-07 07:49:41 +00002351 static const unsigned Opc[3] = {
2352 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2353 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00002354 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002355 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2356 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002357 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002358 case ISD::SIGN_EXTEND: {
2359 int DestIs16 = N.getValueType() == MVT::i16;
2360 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2361
Chris Lattner282781c2005-01-09 18:52:44 +00002362 // FIXME: Legalize should promote bools to i8!
2363 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2364 "Sign extend from bool not implemented!");
2365
Chris Lattnera56d29d2005-01-17 06:26:58 +00002366 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002367 static const unsigned Opc[3] = {
2368 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2369 };
2370
2371 X86AddressMode AM;
2372 EmitFoldedLoad(N.getOperand(0), AM);
2373 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2374 return Result;
2375 }
2376
Chris Lattner88c8a232005-01-07 07:49:41 +00002377 static const unsigned Opc[3] = {
2378 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2379 };
2380 Tmp1 = SelectExpr(N.getOperand(0));
2381 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2382 return Result;
2383 }
2384 case ISD::TRUNCATE:
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00002385 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner14947c32005-01-18 20:05:56 +00002386 // FIXME: This should be performed by the DAGCombiner.
Chris Lattnera56d29d2005-01-17 06:26:58 +00002387 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00002388 switch (N.getValueType()) {
2389 default: assert(0 && "Unknown truncate!");
2390 case MVT::i1:
2391 case MVT::i8: Opc = X86::MOV8rm; break;
2392 case MVT::i16: Opc = X86::MOV16rm; break;
2393 }
2394 X86AddressMode AM;
2395 EmitFoldedLoad(N.getOperand(0), AM);
2396 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2397 return Result;
2398 }
2399
Chris Lattner88c8a232005-01-07 07:49:41 +00002400 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2401 // a move out of AX or AL.
2402 switch (N.getOperand(0).getValueType()) {
2403 default: assert(0 && "Unknown truncate!");
2404 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2405 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2406 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2407 }
2408 Tmp1 = SelectExpr(N.getOperand(0));
2409 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2410
2411 switch (N.getValueType()) {
2412 default: assert(0 && "Unknown truncate!");
2413 case MVT::i1:
2414 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2415 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2416 }
2417 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2418 return Result;
2419
Chris Lattner507a2752005-07-16 00:28:20 +00002420 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00002421 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2422 unsigned PromoteOpcode = 0;
2423
Nate Begeman7e74c832005-07-16 02:02:34 +00002424 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00002425 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00002426 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00002427 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2428 return Result;
2429 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002430
Chris Lattnere44e6d12005-01-11 03:50:45 +00002431 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00002432
Chris Lattner282781c2005-01-09 18:52:44 +00002433 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00002434 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00002435 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2436 MachineFunction *F = BB->getParent();
2437 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2438
2439 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00002440 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00002441 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002442 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2443 break;
2444 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00002445 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002446 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2447 break;
2448 default: break; // No promotion required.
2449 }
Chris Lattner507a2752005-07-16 00:28:20 +00002450 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00002451 }
Chris Lattnerbc85c322005-07-29 01:00:29 +00002452 case ISD::FP_TO_SINT: {
Chris Lattner282781c2005-01-09 18:52:44 +00002453 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2454
Nate Begeman8a093362005-07-06 18:59:04 +00002455 // If the target supports SSE2 and is performing FP operations in SSE regs
2456 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2457 // CVTSD2SI instructions.
Chris Lattnerbc85c322005-07-29 01:00:29 +00002458 if (X86ScalarSSE) {
Nate Begeman8a093362005-07-06 18:59:04 +00002459 if (MVT::f32 == N.getOperand(0).getValueType()) {
Nate Begemana0b5e032005-07-15 00:38:55 +00002460 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00002461 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
Nate Begemana0b5e032005-07-15 00:38:55 +00002462 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00002463 } else {
2464 assert(0 && "Not an f32 or f64?");
2465 abort();
2466 }
2467 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002468 }
Nate Begeman8a093362005-07-06 18:59:04 +00002469
Chris Lattner282781c2005-01-09 18:52:44 +00002470 // Change the floating point control register to use "round towards zero"
2471 // mode when truncating to an integer value.
2472 //
2473 MachineFunction *F = BB->getParent();
2474 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2475 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2476
2477 // Load the old value of the high byte of the control word...
2478 unsigned HighPartOfCW = MakeReg(MVT::i8);
2479 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2480 CWFrameIdx, 1);
2481
2482 // Set the high part to be round to zero...
2483 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2484 CWFrameIdx, 1).addImm(12);
2485
2486 // Reload the modified control word now...
2487 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002488
Chris Lattner282781c2005-01-09 18:52:44 +00002489 // Restore the memory image of control word to original value
2490 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2491 CWFrameIdx, 1).addReg(HighPartOfCW);
2492
Chris Lattner282781c2005-01-09 18:52:44 +00002493 // Spill the integer to memory and reload it from there.
Chris Lattnerbc85c322005-07-29 01:00:29 +00002494 unsigned Size = MVT::getSizeInBits(Node->getValueType(0))/8;
Chris Lattner282781c2005-01-09 18:52:44 +00002495 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2496
Chris Lattnerbc85c322005-07-29 01:00:29 +00002497 switch (Node->getValueType(0)) {
2498 default: assert(0 && "Unsupported store class!");
Chris Lattner282781c2005-01-09 18:52:44 +00002499 case MVT::i16:
2500 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2501 break;
2502 case MVT::i32:
Chris Lattner66d34302005-01-09 19:49:59 +00002503 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002504 break;
Chris Lattner67756e22005-07-29 00:40:01 +00002505 }
Chris Lattner282781c2005-01-09 18:52:44 +00002506
2507 switch (Node->getValueType(0)) {
2508 default:
2509 assert(0 && "Unknown integer type!");
Chris Lattner282781c2005-01-09 18:52:44 +00002510 case MVT::i32:
2511 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2512 break;
2513 case MVT::i16:
2514 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2515 break;
Chris Lattner282781c2005-01-09 18:52:44 +00002516 }
2517
2518 // Reload the original control word now.
2519 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2520 return Result;
2521 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002522 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00002523 Op0 = N.getOperand(0);
2524 Op1 = N.getOperand(1);
2525
Chris Lattner30607ec2005-01-25 20:03:11 +00002526 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00002527 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002528 goto FoldAdd;
2529 }
Chris Lattner62b22422005-01-11 21:19:59 +00002530
Chris Lattner30607ec2005-01-25 20:03:11 +00002531 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002532 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00002533 switch (N.getValueType()) {
2534 default: assert(0 && "Cannot add this type!");
2535 case MVT::i1:
2536 case MVT::i8: Opc = X86::ADD8rm; break;
2537 case MVT::i16: Opc = X86::ADD16rm; break;
2538 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002539 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002540 case MVT::f64:
2541 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00002542 if (X86ScalarSSE) {
2543 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2544 Opc = X86::ADDSDrm;
2545 } else {
2546 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2547 }
Chris Lattner30607ec2005-01-25 20:03:11 +00002548 break;
Chris Lattner62b22422005-01-11 21:19:59 +00002549 }
2550 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002551 EmitFoldedLoad(Op1, AM);
2552 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002553 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2554 return Result;
2555 }
2556
Chris Lattner88c8a232005-01-07 07:49:41 +00002557 // See if we can codegen this as an LEA to fold operations together.
2558 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00002559 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002560 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00002561 MatchAddress(N, AM);
2562 ExprMap[N] = Result;
2563
2564 // If this is not just an add, emit the LEA. For a simple add (like
2565 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2566 // leave this as LEA, then peephole it to 'ADD' after two address elim
2567 // happens.
2568 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2569 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2570 X86AddressMode XAM = SelectAddrExprs(AM);
2571 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2572 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002573 }
2574 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002575
Chris Lattner62b22422005-01-11 21:19:59 +00002576 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002577 Opc = 0;
2578 if (CN->getValue() == 1) { // add X, 1 -> inc X
2579 switch (N.getValueType()) {
2580 default: assert(0 && "Cannot integer add this type!");
2581 case MVT::i8: Opc = X86::INC8r; break;
2582 case MVT::i16: Opc = X86::INC16r; break;
2583 case MVT::i32: Opc = X86::INC32r; break;
2584 }
2585 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2586 switch (N.getValueType()) {
2587 default: assert(0 && "Cannot integer add this type!");
2588 case MVT::i8: Opc = X86::DEC8r; break;
2589 case MVT::i16: Opc = X86::DEC16r; break;
2590 case MVT::i32: Opc = X86::DEC32r; break;
2591 }
2592 }
2593
2594 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002595 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002596 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2597 return Result;
2598 }
2599
2600 switch (N.getValueType()) {
2601 default: assert(0 && "Cannot add this type!");
2602 case MVT::i8: Opc = X86::ADD8ri; break;
2603 case MVT::i16: Opc = X86::ADD16ri; break;
2604 case MVT::i32: Opc = X86::ADD32ri; break;
2605 }
2606 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002607 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002608 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2609 return Result;
2610 }
2611 }
2612
Chris Lattner88c8a232005-01-07 07:49:41 +00002613 switch (N.getValueType()) {
2614 default: assert(0 && "Cannot add this type!");
2615 case MVT::i8: Opc = X86::ADD8rr; break;
2616 case MVT::i16: Opc = X86::ADD16rr; break;
2617 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002618 case MVT::f32: Opc = X86::ADDSSrr; break;
2619 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002620 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002621
Chris Lattner62b22422005-01-11 21:19:59 +00002622 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2623 Tmp1 = SelectExpr(Op0);
2624 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002625 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002626 Tmp2 = SelectExpr(Op1);
2627 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002628 }
2629
Chris Lattner88c8a232005-01-07 07:49:41 +00002630 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2631 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00002632
Nate Begeman8a093362005-07-06 18:59:04 +00002633 case ISD::FSQRT:
2634 Tmp1 = SelectExpr(Node->getOperand(0));
2635 if (X86ScalarSSE) {
2636 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2637 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2638 } else {
2639 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2640 }
2641 return Result;
2642
2643 // FIXME:
2644 // Once we can spill 16 byte constants into the constant pool, we can
2645 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00002646 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00002647 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00002648 case ISD::FSIN:
2649 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00002650 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00002651 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00002652 switch (N.getOpcode()) {
2653 default: assert(0 && "Unreachable!");
2654 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2655 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00002656 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2657 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00002658 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00002659 return Result;
2660
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002661 case ISD::MULHU:
2662 switch (N.getValueType()) {
2663 default: assert(0 && "Unsupported VT!");
2664 case MVT::i8: Tmp2 = X86::MUL8r; break;
2665 case MVT::i16: Tmp2 = X86::MUL16r; break;
2666 case MVT::i32: Tmp2 = X86::MUL32r; break;
2667 }
2668 // FALL THROUGH
2669 case ISD::MULHS: {
2670 unsigned MovOpc, LowReg, HiReg;
2671 switch (N.getValueType()) {
2672 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00002673 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002674 MovOpc = X86::MOV8rr;
2675 LowReg = X86::AL;
2676 HiReg = X86::AH;
2677 Opc = X86::IMUL8r;
2678 break;
2679 case MVT::i16:
2680 MovOpc = X86::MOV16rr;
2681 LowReg = X86::AX;
2682 HiReg = X86::DX;
2683 Opc = X86::IMUL16r;
2684 break;
2685 case MVT::i32:
2686 MovOpc = X86::MOV32rr;
2687 LowReg = X86::EAX;
2688 HiReg = X86::EDX;
2689 Opc = X86::IMUL32r;
2690 break;
2691 }
2692 if (Node->getOpcode() != ISD::MULHS)
2693 Opc = Tmp2; // Get the MULHU opcode.
2694
2695 Op0 = Node->getOperand(0);
2696 Op1 = Node->getOperand(1);
2697 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2698 Tmp1 = SelectExpr(Op0);
2699 Tmp2 = SelectExpr(Op1);
2700 } else {
2701 Tmp2 = SelectExpr(Op1);
2702 Tmp1 = SelectExpr(Op0);
2703 }
2704
2705 // FIXME: Implement folding of loads into the memory operands here!
2706 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2707 BuildMI(BB, Opc, 1).addReg(Tmp2);
2708 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2709 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002710 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002711
Chris Lattner88c8a232005-01-07 07:49:41 +00002712 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00002713 case ISD::MUL:
2714 case ISD::AND:
2715 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00002716 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00002717 static const unsigned SUBTab[] = {
2718 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2719 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2720 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2721 };
Nate Begeman8a093362005-07-06 18:59:04 +00002722 static const unsigned SSE_SUBTab[] = {
2723 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2724 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2725 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2726 };
Chris Lattner62b22422005-01-11 21:19:59 +00002727 static const unsigned MULTab[] = {
2728 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2729 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2730 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2731 };
Nate Begeman8a093362005-07-06 18:59:04 +00002732 static const unsigned SSE_MULTab[] = {
2733 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2734 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2735 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2736 };
Chris Lattner62b22422005-01-11 21:19:59 +00002737 static const unsigned ANDTab[] = {
2738 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2739 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00002740 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00002741 };
2742 static const unsigned ORTab[] = {
2743 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2744 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2745 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2746 };
2747 static const unsigned XORTab[] = {
2748 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2749 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2750 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2751 };
2752
2753 Op0 = Node->getOperand(0);
2754 Op1 = Node->getOperand(1);
2755
Chris Lattner29f58192005-01-19 07:37:26 +00002756 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2757 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00002758 return Result;
2759
2760 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00002761 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2762 if (CN->isNullValue()) { // 0 - N -> neg N
2763 switch (N.getValueType()) {
2764 default: assert(0 && "Cannot sub this type!");
2765 case MVT::i1:
2766 case MVT::i8: Opc = X86::NEG8r; break;
2767 case MVT::i16: Opc = X86::NEG16r; break;
2768 case MVT::i32: Opc = X86::NEG32r; break;
2769 }
2770 Tmp1 = SelectExpr(N.getOperand(1));
2771 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2772 return Result;
2773 }
2774
Chris Lattner62b22422005-01-11 21:19:59 +00002775 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2776 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002777 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00002778 switch (N.getValueType()) {
2779 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002780 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00002781 case MVT::i8: Opc = X86::NOT8r; break;
2782 case MVT::i16: Opc = X86::NOT16r; break;
2783 case MVT::i32: Opc = X86::NOT32r; break;
2784 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002785 if (Opc) {
2786 Tmp1 = SelectExpr(Op0);
2787 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2788 return Result;
2789 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00002790 }
2791
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002792 // Fold common multiplies into LEA instructions.
2793 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2794 switch ((int)CN->getValue()) {
2795 default: break;
2796 case 3:
2797 case 5:
2798 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002799 // Remove N from exprmap so SelectAddress doesn't get confused.
2800 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002801 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002802 SelectAddress(N, AM);
2803 // Restore it to the map.
2804 ExprMap[N] = Result;
2805 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2806 return Result;
2807 }
2808 }
2809
Chris Lattner88c8a232005-01-07 07:49:41 +00002810 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00002811 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00002812 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00002813 case MVT::i8: Opc = 0; break;
2814 case MVT::i16: Opc = 1; break;
2815 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002816 }
Chris Lattner62b22422005-01-11 21:19:59 +00002817 switch (Node->getOpcode()) {
2818 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002819 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2820 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002821 case ISD::AND: Opc = ANDTab[Opc]; break;
2822 case ISD::OR: Opc = ORTab[Opc]; break;
2823 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002824 }
Chris Lattner62b22422005-01-11 21:19:59 +00002825 if (Opc) { // Can't fold MUL:i8 R, imm
2826 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002827 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2828 return Result;
2829 }
2830 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002831
Chris Lattner30607ec2005-01-25 20:03:11 +00002832 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner62b22422005-01-11 21:19:59 +00002833 if (Node->getOpcode() != ISD::SUB) {
2834 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002835 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00002836 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00002837 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00002838 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00002839 if (Op0.getOpcode() == ISD::EXTLOAD)
2840 Opc = X86::FSUBR32m;
2841 else
2842 Opc = X86::FSUBR64m;
2843
Chris Lattner62b22422005-01-11 21:19:59 +00002844 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002845 EmitFoldedLoad(Op0, AM);
2846 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00002847 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2848 return Result;
2849 }
2850 }
2851
Chris Lattner30607ec2005-01-25 20:03:11 +00002852 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002853 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00002854 switch (N.getValueType()) {
2855 default: assert(0 && "Cannot operate on this type!");
2856 case MVT::i1:
2857 case MVT::i8: Opc = 5; break;
2858 case MVT::i16: Opc = 6; break;
2859 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002860 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002861 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002862 case MVT::f64:
2863 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00002864 "SSE load should have been promoted");
2865 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002866 }
2867 switch (Node->getOpcode()) {
2868 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002869 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2870 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002871 case ISD::AND: Opc = ANDTab[Opc]; break;
2872 case ISD::OR: Opc = ORTab[Opc]; break;
2873 case ISD::XOR: Opc = XORTab[Opc]; break;
2874 }
2875
2876 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002877 EmitFoldedLoad(Op1, AM);
2878 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002879 if (Opc) {
2880 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2881 } else {
2882 assert(Node->getOpcode() == ISD::MUL &&
2883 N.getValueType() == MVT::i8 && "Unexpected situation!");
2884 // Must use the MUL instruction, which forces use of AL.
2885 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2886 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2887 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2888 }
2889 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002890 }
Chris Lattner62b22422005-01-11 21:19:59 +00002891
2892 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2893 Tmp1 = SelectExpr(Op0);
2894 Tmp2 = SelectExpr(Op1);
2895 } else {
2896 Tmp2 = SelectExpr(Op1);
2897 Tmp1 = SelectExpr(Op0);
2898 }
2899
Chris Lattner88c8a232005-01-07 07:49:41 +00002900 switch (N.getValueType()) {
2901 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00002902 case MVT::i1:
2903 case MVT::i8: Opc = 10; break;
2904 case MVT::i16: Opc = 11; break;
2905 case MVT::i32: Opc = 12; break;
2906 case MVT::f32: Opc = 13; break;
2907 case MVT::f64: Opc = 14; break;
2908 }
2909 switch (Node->getOpcode()) {
2910 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002911 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2912 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002913 case ISD::AND: Opc = ANDTab[Opc]; break;
2914 case ISD::OR: Opc = ORTab[Opc]; break;
2915 case ISD::XOR: Opc = XORTab[Opc]; break;
2916 }
2917 if (Opc) {
2918 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2919 } else {
2920 assert(Node->getOpcode() == ISD::MUL &&
2921 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00002922 // Must use the MUL instruction, which forces use of AL.
2923 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2924 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2925 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00002926 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002927 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00002928 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00002929 case ISD::ADD_PARTS:
2930 case ISD::SUB_PARTS: {
2931 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2932 "Not an i64 add/sub!");
2933 // Emit all of the operands.
2934 std::vector<unsigned> InVals;
2935 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2936 InVals.push_back(SelectExpr(N.getOperand(i)));
2937 if (N.getOpcode() == ISD::ADD_PARTS) {
2938 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2939 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2940 } else {
2941 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2942 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2943 }
2944 return Result+N.ResNo;
2945 }
2946
Chris Lattnera31d4c72005-04-02 04:01:14 +00002947 case ISD::SHL_PARTS:
2948 case ISD::SRA_PARTS:
2949 case ISD::SRL_PARTS: {
2950 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2951 "Not an i64 shift!");
2952 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2953 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2954 unsigned TmpReg = MakeReg(MVT::i32);
2955 if (N.getOpcode() == ISD::SRA_PARTS) {
2956 // If this is a SHR of a Long, then we need to do funny sign extension
2957 // stuff. TmpReg gets the value to use as the high-part if we are
2958 // shifting more than 32 bits.
2959 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2960 } else {
2961 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2962 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2963 }
2964
2965 // Initialize CL with the shift amount.
2966 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2967 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2968
2969 unsigned TmpReg2 = MakeReg(MVT::i32);
2970 unsigned TmpReg3 = MakeReg(MVT::i32);
2971 if (N.getOpcode() == ISD::SHL_PARTS) {
2972 // TmpReg2 = shld inHi, inLo
2973 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2974 .addReg(ShiftOpLo);
2975 // TmpReg3 = shl inLo, CL
2976 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002977
Chris Lattnera31d4c72005-04-02 04:01:14 +00002978 // Set the flags to indicate whether the shift was by more than 32 bits.
2979 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002980
Chris Lattnera31d4c72005-04-02 04:01:14 +00002981 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002982 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002983 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2984 // DestLo = (>32) ? TmpReg : TmpReg3;
2985 BuildMI(BB, X86::CMOVNE32rr, 2,
2986 Result).addReg(TmpReg3).addReg(TmpReg);
2987 } else {
2988 // TmpReg2 = shrd inLo, inHi
2989 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2990 .addReg(ShiftOpHi);
2991 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00002992 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00002993 : X86::SHR32rCL, 1, TmpReg3)
2994 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002995
Chris Lattnera31d4c72005-04-02 04:01:14 +00002996 // Set the flags to indicate whether the shift was by more than 32 bits.
2997 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002998
Chris Lattnera31d4c72005-04-02 04:01:14 +00002999 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003000 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00003001 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003002
Chris Lattnera31d4c72005-04-02 04:01:14 +00003003 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003004 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00003005 Result+1).addReg(TmpReg3).addReg(TmpReg);
3006 }
3007 return Result+N.ResNo;
3008 }
3009
Chris Lattner88c8a232005-01-07 07:49:41 +00003010 case ISD::SELECT:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003011 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3012 Tmp2 = SelectExpr(N.getOperand(1));
3013 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner88c8a232005-01-07 07:49:41 +00003014 } else {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003015 Tmp3 = SelectExpr(N.getOperand(2));
3016 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88c8a232005-01-07 07:49:41 +00003017 }
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003018 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
3019 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003020
3021 case ISD::SDIV:
3022 case ISD::UDIV:
3023 case ISD::SREM:
3024 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003025 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3026 "We don't support this operator!");
3027
Chris Lattner60c23bd2005-04-13 03:29:53 +00003028 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00003029 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00003030 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00003031 // Check for reversed and unreversed DIV.
3032 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3033 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3034 Opc = X86::FDIVR32m;
3035 else
3036 Opc = X86::FDIVR64m;
3037 X86AddressMode AM;
3038 EmitFoldedLoad(N.getOperand(0), AM);
3039 Tmp1 = SelectExpr(N.getOperand(1));
3040 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3041 return Result;
3042 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3043 N.getOperand(1).getOpcode() == ISD::LOAD) {
3044 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3045 Opc = X86::FDIV32m;
3046 else
3047 Opc = X86::FDIV64m;
3048 X86AddressMode AM;
3049 EmitFoldedLoad(N.getOperand(1), AM);
3050 Tmp1 = SelectExpr(N.getOperand(0));
3051 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3052 return Result;
3053 }
3054 }
3055
Chris Lattner88c8a232005-01-07 07:49:41 +00003056 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3057 // FIXME: These special cases should be handled by the lowering impl!
3058 unsigned RHS = CN->getValue();
3059 bool isNeg = false;
3060 if ((int)RHS < 0) {
3061 isNeg = true;
3062 RHS = -RHS;
3063 }
3064 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
3065 unsigned Log = log2(RHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00003066 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3067 switch (N.getValueType()) {
3068 default: assert("Unknown type to signed divide!");
3069 case MVT::i8:
3070 SAROpc = X86::SAR8ri;
3071 SHROpc = X86::SHR8ri;
3072 ADDOpc = X86::ADD8rr;
3073 NEGOpc = X86::NEG8r;
3074 break;
3075 case MVT::i16:
3076 SAROpc = X86::SAR16ri;
3077 SHROpc = X86::SHR16ri;
3078 ADDOpc = X86::ADD16rr;
3079 NEGOpc = X86::NEG16r;
3080 break;
3081 case MVT::i32:
3082 SAROpc = X86::SAR32ri;
3083 SHROpc = X86::SHR32ri;
3084 ADDOpc = X86::ADD32rr;
3085 NEGOpc = X86::NEG32r;
3086 break;
3087 }
Chris Lattner7d387d22005-05-13 21:48:20 +00003088 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003089 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner79e9fa52005-05-13 21:50:27 +00003090 unsigned TmpReg;
3091 if (Log != 1) {
3092 TmpReg = MakeReg(N.getValueType());
3093 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3094 } else {
3095 TmpReg = Tmp1;
3096 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003097 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattner7d387d22005-05-13 21:48:20 +00003098 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner88c8a232005-01-07 07:49:41 +00003099 unsigned TmpReg3 = MakeReg(N.getValueType());
3100 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003101
Chris Lattner88c8a232005-01-07 07:49:41 +00003102 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3103 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3104 if (isNeg)
3105 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3106 return Result;
3107 }
3108 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00003109 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003110
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003111 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3112 Tmp1 = SelectExpr(N.getOperand(0));
3113 Tmp2 = SelectExpr(N.getOperand(1));
3114 } else {
3115 Tmp2 = SelectExpr(N.getOperand(1));
3116 Tmp1 = SelectExpr(N.getOperand(0));
3117 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003118
3119 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3120 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3121 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3122 switch (N.getValueType()) {
3123 default: assert(0 && "Cannot sdiv this type!");
3124 case MVT::i8:
3125 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3126 LoReg = X86::AL;
3127 HiReg = X86::AH;
3128 MovOpcode = X86::MOV8rr;
3129 ClrOpcode = X86::MOV8ri;
3130 SExtOpcode = X86::CBW;
3131 break;
3132 case MVT::i16:
3133 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3134 LoReg = X86::AX;
3135 HiReg = X86::DX;
3136 MovOpcode = X86::MOV16rr;
3137 ClrOpcode = X86::MOV16ri;
3138 SExtOpcode = X86::CWD;
3139 break;
3140 case MVT::i32:
3141 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00003142 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00003143 HiReg = X86::EDX;
3144 MovOpcode = X86::MOV32rr;
3145 ClrOpcode = X86::MOV32ri;
3146 SExtOpcode = X86::CDQ;
3147 break;
Nate Begeman8a093362005-07-06 18:59:04 +00003148 case MVT::f32:
3149 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3150 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003151 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003152 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3153 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003154 return Result;
3155 }
3156
3157 // Set up the low part.
3158 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3159
3160 if (isSigned) {
3161 // Sign extend the low part into the high part.
3162 BuildMI(BB, SExtOpcode, 0);
3163 } else {
3164 // Zero out the high part, effectively zero extending the input.
3165 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3166 }
3167
3168 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00003169 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003170
3171 // Get the result of the divide or rem.
3172 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3173 return Result;
3174 }
3175
3176 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00003178 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3179 switch (N.getValueType()) {
3180 default: assert(0 && "Cannot shift this type!");
3181 case MVT::i8: Opc = X86::ADD8rr; break;
3182 case MVT::i16: Opc = X86::ADD16rr; break;
3183 case MVT::i32: Opc = X86::ADD32rr; break;
3184 }
3185 Tmp1 = SelectExpr(N.getOperand(0));
3186 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3187 return Result;
3188 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003189
Chris Lattner88c8a232005-01-07 07:49:41 +00003190 switch (N.getValueType()) {
3191 default: assert(0 && "Cannot shift this type!");
3192 case MVT::i8: Opc = X86::SHL8ri; break;
3193 case MVT::i16: Opc = X86::SHL16ri; break;
3194 case MVT::i32: Opc = X86::SHL32ri; break;
3195 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003196 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3198 return Result;
3199 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003200
3201 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3202 Tmp1 = SelectExpr(N.getOperand(0));
3203 Tmp2 = SelectExpr(N.getOperand(1));
3204 } else {
3205 Tmp2 = SelectExpr(N.getOperand(1));
3206 Tmp1 = SelectExpr(N.getOperand(0));
3207 }
3208
Chris Lattner88c8a232005-01-07 07:49:41 +00003209 switch (N.getValueType()) {
3210 default: assert(0 && "Cannot shift this type!");
3211 case MVT::i8 : Opc = X86::SHL8rCL; break;
3212 case MVT::i16: Opc = X86::SHL16rCL; break;
3213 case MVT::i32: Opc = X86::SHL32rCL; break;
3214 }
3215 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3216 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3217 return Result;
3218 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003219 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3220 switch (N.getValueType()) {
3221 default: assert(0 && "Cannot shift this type!");
3222 case MVT::i8: Opc = X86::SHR8ri; break;
3223 case MVT::i16: Opc = X86::SHR16ri; break;
3224 case MVT::i32: Opc = X86::SHR32ri; break;
3225 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003226 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003227 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3228 return Result;
3229 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003230
3231 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3232 Tmp1 = SelectExpr(N.getOperand(0));
3233 Tmp2 = SelectExpr(N.getOperand(1));
3234 } else {
3235 Tmp2 = SelectExpr(N.getOperand(1));
3236 Tmp1 = SelectExpr(N.getOperand(0));
3237 }
3238
Chris Lattner88c8a232005-01-07 07:49:41 +00003239 switch (N.getValueType()) {
3240 default: assert(0 && "Cannot shift this type!");
3241 case MVT::i8 : Opc = X86::SHR8rCL; break;
3242 case MVT::i16: Opc = X86::SHR16rCL; break;
3243 case MVT::i32: Opc = X86::SHR32rCL; break;
3244 }
3245 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3246 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3247 return Result;
3248 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00003249 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3250 switch (N.getValueType()) {
3251 default: assert(0 && "Cannot shift this type!");
3252 case MVT::i8: Opc = X86::SAR8ri; break;
3253 case MVT::i16: Opc = X86::SAR16ri; break;
3254 case MVT::i32: Opc = X86::SAR32ri; break;
3255 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003256 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003257 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3258 return Result;
3259 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003260
3261 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3262 Tmp1 = SelectExpr(N.getOperand(0));
3263 Tmp2 = SelectExpr(N.getOperand(1));
3264 } else {
3265 Tmp2 = SelectExpr(N.getOperand(1));
3266 Tmp1 = SelectExpr(N.getOperand(0));
3267 }
3268
Chris Lattner88c8a232005-01-07 07:49:41 +00003269 switch (N.getValueType()) {
3270 default: assert(0 && "Cannot shift this type!");
3271 case MVT::i8 : Opc = X86::SAR8rCL; break;
3272 case MVT::i16: Opc = X86::SAR16rCL; break;
3273 case MVT::i32: Opc = X86::SAR32rCL; break;
3274 }
3275 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3276 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3277 return Result;
3278
3279 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00003280 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00003281 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3282 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3283 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003284 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003285 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00003286 if (Result != 1) { // Generate the token
3287 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3288 assert(0 && "Load already emitted!?");
3289 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00003290 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3291
Chris Lattnerb52e0412005-01-08 19:28:19 +00003292 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003293 default: assert(0 && "Cannot load this type!");
3294 case MVT::i1:
3295 case MVT::i8: Opc = X86::MOV8rm; break;
3296 case MVT::i16: Opc = X86::MOV16rm; break;
3297 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003298 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003299 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003300 if (X86ScalarSSE) {
3301 Opc = X86::MOVSDrm;
3302 } else {
3303 Opc = X86::FLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003304 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003305 }
3306 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003307 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003308
Chris Lattner88c8a232005-01-07 07:49:41 +00003309 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003310 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003311 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3312 } else {
3313 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00003314
3315 SDOperand Chain = N.getOperand(0);
3316 SDOperand Address = N.getOperand(1);
3317 if (getRegPressure(Chain) > getRegPressure(Address)) {
3318 Select(Chain);
3319 SelectAddress(Address, AM);
3320 } else {
3321 SelectAddress(Address, AM);
3322 Select(Chain);
3323 }
3324
Chris Lattner88c8a232005-01-07 07:49:41 +00003325 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3326 }
3327 return Result;
Chris Lattnera36117b2005-05-14 06:52:07 +00003328 case X86ISD::FILD64m:
3329 // Make sure we generate both values.
3330 assert(Result != 1 && N.getValueType() == MVT::f64);
3331 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3332 assert(0 && "Load already emitted!?");
3333
3334 {
3335 X86AddressMode AM;
3336
3337 SDOperand Chain = N.getOperand(0);
3338 SDOperand Address = N.getOperand(1);
3339 if (getRegPressure(Chain) > getRegPressure(Address)) {
3340 Select(Chain);
3341 SelectAddress(Address, AM);
3342 } else {
3343 SelectAddress(Address, AM);
3344 Select(Chain);
3345 }
Chris Lattner67756e22005-07-29 00:40:01 +00003346
3347 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00003348 }
3349 return Result;
Chris Lattner67756e22005-07-29 00:40:01 +00003350
Chris Lattnere18a4c42005-01-15 05:22:24 +00003351 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3352 case ISD::ZEXTLOAD: {
3353 // Make sure we generate both values.
3354 if (Result != 1)
3355 ExprMap[N.getValue(1)] = 1; // Generate the token
3356 else
3357 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3358
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003359 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3360 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00003361 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003362 "Bad EXTLOAD!");
3363 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3364 CP->getIndex());
3365 return Result;
3366 }
3367
Chris Lattnere18a4c42005-01-15 05:22:24 +00003368 X86AddressMode AM;
3369 if (getRegPressure(Node->getOperand(0)) >
3370 getRegPressure(Node->getOperand(1))) {
3371 Select(Node->getOperand(0)); // chain
3372 SelectAddress(Node->getOperand(1), AM);
3373 } else {
3374 SelectAddress(Node->getOperand(1), AM);
3375 Select(Node->getOperand(0)); // chain
3376 }
3377
3378 switch (Node->getValueType(0)) {
3379 default: assert(0 && "Unknown type to sign extend to.");
3380 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00003381 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003382 "Bad EXTLOAD!");
3383 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3384 break;
3385 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003386 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003387 default:
3388 assert(0 && "Bad zero extend!");
3389 case MVT::i1:
3390 case MVT::i8:
3391 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3392 break;
3393 case MVT::i16:
3394 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3395 break;
3396 }
3397 break;
3398 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003399 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003400 "Bad zero extend!");
3401 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3402 break;
3403 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00003404 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003405 "Bad zero extend!");
3406 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3407 break;
3408 }
3409 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003410 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003411 case ISD::SEXTLOAD: {
3412 // Make sure we generate both values.
3413 if (Result != 1)
3414 ExprMap[N.getValue(1)] = 1; // Generate the token
3415 else
3416 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3417
3418 X86AddressMode AM;
3419 if (getRegPressure(Node->getOperand(0)) >
3420 getRegPressure(Node->getOperand(1))) {
3421 Select(Node->getOperand(0)); // chain
3422 SelectAddress(Node->getOperand(1), AM);
3423 } else {
3424 SelectAddress(Node->getOperand(1), AM);
3425 Select(Node->getOperand(0)); // chain
3426 }
3427
3428 switch (Node->getValueType(0)) {
3429 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3430 default: assert(0 && "Unknown type to sign extend to.");
3431 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003432 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003433 default:
3434 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3435 case MVT::i8:
3436 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3437 break;
3438 case MVT::i16:
3439 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3440 break;
3441 }
3442 break;
3443 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003444 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003445 "Cannot sign extend from bool!");
3446 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3447 break;
3448 }
3449 return Result;
3450 }
3451
Chris Lattner88c8a232005-01-07 07:49:41 +00003452 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00003453 // Generate both result values.
3454 if (Result != 1)
3455 ExprMap[N.getValue(1)] = 1; // Generate the token
3456 else
3457 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3458
3459 // FIXME: We are currently ignoring the requested alignment for handling
3460 // greater than the stack alignment. This will need to be revisited at some
3461 // point. Align = N.getOperand(2);
3462
3463 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3464 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3465 std::cerr << "Cannot allocate stack object with greater alignment than"
3466 << " the stack alignment yet!";
3467 abort();
3468 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003469
Chris Lattner88c8a232005-01-07 07:49:41 +00003470 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003471 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003472 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3473 .addImm(CN->getValue());
3474 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003475 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3476 Select(N.getOperand(0));
3477 Tmp1 = SelectExpr(N.getOperand(1));
3478 } else {
3479 Tmp1 = SelectExpr(N.getOperand(1));
3480 Select(N.getOperand(0));
3481 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003482
3483 // Subtract size from stack pointer, thereby allocating some space.
3484 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3485 }
3486
3487 // Put a pointer to the space into the result register, by copying the stack
3488 // pointer.
3489 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3490 return Result;
3491
Chris Lattner1b3520c2005-05-14 08:48:15 +00003492 case X86ISD::TAILCALL:
3493 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00003494 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003495 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00003496
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003497 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3498 isa<ExternalSymbolSDNode>(N.getOperand(1));
3499 unsigned Callee = 0;
3500 if (isDirect) {
3501 Select(N.getOperand(0));
3502 } else {
3503 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3504 Select(N.getOperand(0));
3505 Callee = SelectExpr(N.getOperand(1));
3506 } else {
3507 Callee = SelectExpr(N.getOperand(1));
3508 Select(N.getOperand(0));
3509 }
3510 }
3511
3512 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003513 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003514 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003515 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003516 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00003517 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003518
Chris Lattner1b3520c2005-05-14 08:48:15 +00003519 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003520 default: assert(0 && "Bad thing to pass in regs");
3521 case MVT::i1:
3522 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3523 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3524 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3525 }
3526 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00003527 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003528 default: assert(0 && "Bad thing to pass in regs");
3529 case MVT::i1:
3530 case MVT::i8:
3531 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3532 break;
3533 case MVT::i16:
3534 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3535 break;
3536 case MVT::i32:
3537 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3538 break;
3539 }
3540 }
3541
Chris Lattner88c8a232005-01-07 07:49:41 +00003542 if (GlobalAddressSDNode *GASD =
3543 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3544 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3545 } else if (ExternalSymbolSDNode *ESSDN =
3546 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3547 BuildMI(BB, X86::CALLpcrel32,
3548 1).addExternalSymbol(ESSDN->getSymbol(), true);
3549 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003550 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3551 Select(N.getOperand(0));
3552 Tmp1 = SelectExpr(N.getOperand(1));
3553 } else {
3554 Tmp1 = SelectExpr(N.getOperand(1));
3555 Select(N.getOperand(0));
3556 }
3557
Chris Lattner88c8a232005-01-07 07:49:41 +00003558 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3559 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003560
3561 // Get caller stack amount and amount the callee added to the stack pointer.
3562 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3563 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3564 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3565
3566 if (Node->getNumValues() != 1)
3567 switch (Node->getValueType(1)) {
3568 default: assert(0 && "Unknown value type for call result!");
3569 case MVT::Other: return 1;
3570 case MVT::i1:
3571 case MVT::i8:
3572 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3573 break;
3574 case MVT::i16:
3575 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3576 break;
3577 case MVT::i32:
3578 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3579 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3580 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3581 break;
3582 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00003583 if (X86ScalarSSE) {
3584 ContainsFPCode = true;
3585 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3586
3587 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3588 MachineFunction *F = BB->getParent();
3589 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3590 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3591 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3592 break;
3593 } else {
3594 ContainsFPCode = true;
3595 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3596 break;
3597 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003598 }
3599 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003600 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003601 case ISD::READPORT:
3602 // First, determine that the size of the operand falls within the acceptable
3603 // range for this architecture.
3604 //
3605 if (Node->getOperand(1).getValueType() != MVT::i16) {
3606 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3607 exit(1);
3608 }
3609
3610 // Make sure we generate both values.
3611 if (Result != 1) { // Generate the token
3612 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3613 assert(0 && "readport already emitted!?");
3614 } else
3615 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003616
Chris Lattner70ea07c2005-05-09 21:17:38 +00003617 Select(Node->getOperand(0)); // Select the chain.
3618
3619 // If the port is a single-byte constant, use the immediate form.
3620 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3621 if ((Port->getValue() & 255) == Port->getValue()) {
3622 switch (Node->getValueType(0)) {
3623 case MVT::i8:
3624 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3625 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3626 return Result;
3627 case MVT::i16:
3628 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3629 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3630 return Result;
3631 case MVT::i32:
3632 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3633 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3634 return Result;
3635 default: break;
3636 }
3637 }
3638
3639 // Now, move the I/O port address into the DX register and use the IN
3640 // instruction to get the input data.
3641 //
3642 Tmp1 = SelectExpr(Node->getOperand(1));
3643 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3644 switch (Node->getValueType(0)) {
3645 case MVT::i8:
3646 BuildMI(BB, X86::IN8rr, 0);
3647 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3648 return Result;
3649 case MVT::i16:
3650 BuildMI(BB, X86::IN16rr, 0);
3651 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3652 return Result;
3653 case MVT::i32:
3654 BuildMI(BB, X86::IN32rr, 0);
3655 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3656 return Result;
3657 default:
3658 std::cerr << "Cannot do input on this data type";
3659 exit(1);
3660 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003661
Chris Lattner88c8a232005-01-07 07:49:41 +00003662 }
3663
3664 return 0;
3665}
3666
Chris Lattner96113fd2005-01-17 19:25:26 +00003667/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3668/// load/op/store instruction. If successful return true.
3669bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3670 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3671 SDOperand Chain = Node->getOperand(0);
3672 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00003673 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00003674
3675 // The chain has to be a load, the stored value must be an integer binary
3676 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00003677 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00003678 MVT::isFloatingPoint(StVal.getValueType()))
3679 return false;
3680
Chris Lattnere86c9332005-01-17 22:10:42 +00003681 // Token chain must either be a factor node or the load to fold.
3682 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3683 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00003684
Chris Lattnere86c9332005-01-17 22:10:42 +00003685 SDOperand TheLoad;
3686
3687 // Check to see if there is a load from the same pointer that we're storing
3688 // to in either operand of the binop.
3689 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3690 StVal.getOperand(0).getOperand(1) == StPtr)
3691 TheLoad = StVal.getOperand(0);
3692 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3693 StVal.getOperand(1).getOperand(1) == StPtr)
3694 TheLoad = StVal.getOperand(1);
3695 else
3696 return false; // No matching load operand.
3697
3698 // We can only fold the load if there are no intervening side-effecting
3699 // operations. This means that the store uses the load as its token chain, or
3700 // there are only token factor nodes in between the store and load.
3701 if (Chain != TheLoad.getValue(1)) {
3702 // Okay, the other option is that we have a store referring to (possibly
3703 // nested) token factor nodes. For now, just try peeking through one level
3704 // of token factors to see if this is the case.
3705 bool ChainOk = false;
3706 if (Chain.getOpcode() == ISD::TokenFactor) {
3707 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3708 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3709 ChainOk = true;
3710 break;
3711 }
3712 }
3713
3714 if (!ChainOk) return false;
3715 }
3716
3717 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00003718 return false;
3719
3720 // Make sure that one of the operands of the binop is the load, and that the
3721 // load folds into the binop.
3722 if (((StVal.getOperand(0) != TheLoad ||
3723 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3724 (StVal.getOperand(1) != TheLoad ||
3725 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3726 return false;
3727
3728 // Finally, check to see if this is one of the ops we can handle!
3729 static const unsigned ADDTAB[] = {
3730 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3731 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3732 };
3733 static const unsigned SUBTAB[] = {
3734 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3735 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3736 };
3737 static const unsigned ANDTAB[] = {
3738 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3739 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3740 };
3741 static const unsigned ORTAB[] = {
3742 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3743 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3744 };
3745 static const unsigned XORTAB[] = {
3746 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3747 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3748 };
3749 static const unsigned SHLTAB[] = {
3750 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3751 /*Have to put the reg in CL*/0, 0, 0,
3752 };
3753 static const unsigned SARTAB[] = {
3754 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3755 /*Have to put the reg in CL*/0, 0, 0,
3756 };
3757 static const unsigned SHRTAB[] = {
3758 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3759 /*Have to put the reg in CL*/0, 0, 0,
3760 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00003761
Chris Lattner96113fd2005-01-17 19:25:26 +00003762 const unsigned *TabPtr = 0;
3763 switch (StVal.getOpcode()) {
3764 default:
3765 std::cerr << "CANNOT [mem] op= val: ";
3766 StVal.Val->dump(); std::cerr << "\n";
3767 case ISD::MUL:
3768 case ISD::SDIV:
3769 case ISD::UDIV:
3770 case ISD::SREM:
3771 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003772
Chris Lattner96113fd2005-01-17 19:25:26 +00003773 case ISD::ADD: TabPtr = ADDTAB; break;
3774 case ISD::SUB: TabPtr = SUBTAB; break;
3775 case ISD::AND: TabPtr = ANDTAB; break;
3776 case ISD:: OR: TabPtr = ORTAB; break;
3777 case ISD::XOR: TabPtr = XORTAB; break;
3778 case ISD::SHL: TabPtr = SHLTAB; break;
3779 case ISD::SRA: TabPtr = SARTAB; break;
3780 case ISD::SRL: TabPtr = SHRTAB; break;
3781 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003782
Chris Lattner96113fd2005-01-17 19:25:26 +00003783 // Handle: [mem] op= CST
3784 SDOperand Op0 = StVal.getOperand(0);
3785 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00003786 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00003787 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3788 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3789 default: break;
3790 case MVT::i1:
3791 case MVT::i8: Opc = TabPtr[0]; break;
3792 case MVT::i16: Opc = TabPtr[1]; break;
3793 case MVT::i32: Opc = TabPtr[2]; break;
3794 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003795
Chris Lattner96113fd2005-01-17 19:25:26 +00003796 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00003797 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3798 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003799 Select(Chain);
3800
Chris Lattner96113fd2005-01-17 19:25:26 +00003801 X86AddressMode AM;
3802 if (getRegPressure(TheLoad.getOperand(0)) >
3803 getRegPressure(TheLoad.getOperand(1))) {
3804 Select(TheLoad.getOperand(0));
3805 SelectAddress(TheLoad.getOperand(1), AM);
3806 } else {
3807 SelectAddress(TheLoad.getOperand(1), AM);
3808 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00003809 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003810
3811 if (StVal.getOpcode() == ISD::ADD) {
3812 if (CN->getValue() == 1) {
3813 switch (Op0.getValueType()) {
3814 default: break;
3815 case MVT::i8:
3816 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3817 return true;
3818 case MVT::i16: Opc = TabPtr[1];
3819 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3820 return true;
3821 case MVT::i32: Opc = TabPtr[2];
3822 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3823 return true;
3824 }
3825 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3826 switch (Op0.getValueType()) {
3827 default: break;
3828 case MVT::i8:
3829 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3830 return true;
3831 case MVT::i16: Opc = TabPtr[1];
3832 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3833 return true;
3834 case MVT::i32: Opc = TabPtr[2];
3835 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3836 return true;
3837 }
3838 }
3839 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003840
Chris Lattner96113fd2005-01-17 19:25:26 +00003841 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3842 return true;
3843 }
3844 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003845
Chris Lattner96113fd2005-01-17 19:25:26 +00003846 // If we have [mem] = V op [mem], try to turn it into:
3847 // [mem] = [mem] op V.
3848 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3849 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3850 StVal.getOpcode() != ISD::SRL)
3851 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003852
Chris Lattner96113fd2005-01-17 19:25:26 +00003853 if (Op0 != TheLoad) return false;
3854
3855 switch (Op0.getValueType()) {
3856 default: return false;
3857 case MVT::i1:
3858 case MVT::i8: Opc = TabPtr[3]; break;
3859 case MVT::i16: Opc = TabPtr[4]; break;
3860 case MVT::i32: Opc = TabPtr[5]; break;
3861 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003862
Chris Lattner479c7112005-01-18 17:35:28 +00003863 // Table entry doesn't exist?
3864 if (Opc == 0) return false;
3865
Chris Lattner78d30282005-01-18 03:51:59 +00003866 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3867 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003868 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00003869 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00003870
Chris Lattner96113fd2005-01-17 19:25:26 +00003871 X86AddressMode AM;
3872 SelectAddress(TheLoad.getOperand(1), AM);
3873 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00003874 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00003875 return true;
3876}
3877
Chris Lattnerdd66a412005-05-15 05:46:45 +00003878/// If node is a ret(tailcall) node, emit the specified tail call and return
3879/// true, otherwise return false.
3880///
3881/// FIXME: This whole thing should be a post-legalize optimization pass which
3882/// recognizes and transforms the dag. We don't want the selection phase doing
3883/// this stuff!!
3884///
3885bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3886 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3887
3888 SDOperand Chain = RetNode->getOperand(0);
3889
3890 // If this is a token factor node where one operand is a call, dig into it.
3891 SDOperand TokFactor;
3892 unsigned TokFactorOperand = 0;
3893 if (Chain.getOpcode() == ISD::TokenFactor) {
3894 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3895 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3896 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3897 TokFactorOperand = i;
3898 TokFactor = Chain;
3899 Chain = Chain.getOperand(i);
3900 break;
3901 }
3902 if (TokFactor.Val == 0) return false; // No call operand.
3903 }
3904
3905 // Skip the CALLSEQ_END node if present.
3906 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3907 Chain = Chain.getOperand(0);
3908
3909 // Is a tailcall the last control operation that occurs before the return?
3910 if (Chain.getOpcode() != X86ISD::TAILCALL)
3911 return false;
3912
3913 // If we return a value, is it the value produced by the call?
3914 if (RetNode->getNumOperands() > 1) {
3915 // Not returning the ret val of the call?
3916 if (Chain.Val->getNumValues() == 1 ||
3917 RetNode->getOperand(1) != Chain.getValue(1))
3918 return false;
3919
3920 if (RetNode->getNumOperands() > 2) {
3921 if (Chain.Val->getNumValues() == 2 ||
3922 RetNode->getOperand(2) != Chain.getValue(2))
3923 return false;
3924 }
3925 assert(RetNode->getNumOperands() <= 3);
3926 }
3927
3928 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3929 // For FastCC, this will always be > 0.
3930 unsigned CalleeCallArgAmt =
3931 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3932
3933 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3934 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3935 unsigned CalleeCallArgPopAmt =
3936 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3937
3938 // There are several cases we can handle here. First, if the caller and
3939 // callee are both CCC functions, we can tailcall if the callee takes <= the
3940 // number of argument bytes that the caller does.
3941 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3942 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3943 // Check to see if caller arg area size >= callee arg area size.
3944 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3945 //std::cerr << "CCC TAILCALL UNIMP!\n";
3946 // If TokFactor is non-null, emit all operands.
3947
3948 //EmitCCCToCCCTailCall(Chain.Val);
3949 //return true;
3950 }
3951 return false;
3952 }
3953
3954 // Second, if both are FastCC functions, we can always perform the tail call.
3955 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3956 // If TokFactor is non-null, emit all operands before the call.
3957 if (TokFactor.Val) {
3958 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3959 if (i != TokFactorOperand)
3960 Select(TokFactor.getOperand(i));
3961 }
3962
3963 EmitFastCCToFastCCTailCall(Chain.Val);
3964 return true;
3965 }
3966
3967 // We don't support mixed calls, due to issues with alignment. We could in
3968 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3969 // aligned (which depends on the number of arguments to the callee). TODO.
3970 return false;
3971}
3972
3973static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3974 SelectionDAG &DAG) {
3975 MVT::ValueType StoreVT;
3976 switch (Chain.getOpcode()) {
3977 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00003978 // If we found the start of the call sequence, we're done. We actually
3979 // strip off the CALLSEQ_START node, to avoid generating the
3980 // ADJCALLSTACKDOWN marker for the tail call.
3981 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003982 case ISD::TokenFactor: {
3983 std::vector<SDOperand> Ops;
3984 Ops.reserve(Chain.getNumOperands());
3985 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3986 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3987 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3988 }
3989 case ISD::STORE: // Normal store
3990 StoreVT = Chain.getOperand(1).getValueType();
3991 break;
3992 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00003993 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00003994 break;
3995 }
3996
3997 SDOperand OrigDest = Chain.getOperand(2);
3998 unsigned OrigOffset;
3999
4000 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4001 OrigOffset = 0;
4002 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
4003 } else {
4004 // We expect only (ESP+C)
4005 assert(OrigDest.getOpcode() == ISD::ADD &&
4006 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4007 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
4008 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
4009 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4010 }
4011
4012 // Compute the new offset from the incoming ESP value we wish to use.
4013 unsigned NewOffset = OrigOffset + Offset;
4014
4015 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4016 MachineFunction &MF = DAG.getMachineFunction();
4017 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4018 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4019
4020 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4021 DAG);
4022 if (Chain.getOpcode() == ISD::STORE)
4023 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4024 FIN);
4025 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4026 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00004027 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00004028}
4029
4030
4031/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4032/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4033/// tail call.
4034void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4035 unsigned CalleeCallArgSize =
4036 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4037 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4038
4039 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4040
4041 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4042 // indexes that are relative to the incoming ESP. If the incoming and
4043 // outgoing arg sizes are the same we will store to [InESP] instead of
4044 // [CurESP] and the ESP referenced will be relative to the incoming function
4045 // ESP.
4046 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4047 SDOperand AdjustedArgStores =
4048 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4049
4050 // Copy the return address of the caller into a virtual register so we don't
4051 // clobber it.
4052 SDOperand RetVal;
4053 if (ESPOffset) {
4054 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4055 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4056 RetValAddr, TheDAG->getSrcValue(NULL));
4057 SelectExpr(RetVal);
4058 }
4059
4060 // Codegen all of the argument stores.
4061 Select(AdjustedArgStores);
4062
4063 if (RetVal.Val) {
4064 // Emit a store of the saved ret value to the new location.
4065 MachineFunction &MF = TheDAG->getMachineFunction();
4066 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4067 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4068 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4069 RetVal, RetValAddr));
4070 }
4071
4072 // Get the destination value.
4073 SDOperand Callee = TailCallNode->getOperand(1);
4074 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4075 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00004076 unsigned CalleeReg = 0;
Chris Lattnerdd66a412005-05-15 05:46:45 +00004077 if (!isDirect) CalleeReg = SelectExpr(Callee);
4078
4079 unsigned RegOp1 = 0;
4080 unsigned RegOp2 = 0;
4081
4082 if (TailCallNode->getNumOperands() > 4) {
4083 // The first value is passed in (a part of) EAX, the second in EDX.
4084 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4085 if (TailCallNode->getNumOperands() > 5)
4086 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004087
Chris Lattnerdd66a412005-05-15 05:46:45 +00004088 switch (TailCallNode->getOperand(4).getValueType()) {
4089 default: assert(0 && "Bad thing to pass in regs");
4090 case MVT::i1:
4091 case MVT::i8:
4092 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4093 RegOp1 = X86::AL;
4094 break;
4095 case MVT::i16:
4096 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4097 RegOp1 = X86::AX;
4098 break;
4099 case MVT::i32:
4100 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4101 RegOp1 = X86::EAX;
4102 break;
4103 }
4104 if (RegOp2)
4105 switch (TailCallNode->getOperand(5).getValueType()) {
4106 default: assert(0 && "Bad thing to pass in regs");
4107 case MVT::i1:
4108 case MVT::i8:
4109 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4110 RegOp2 = X86::DL;
4111 break;
4112 case MVT::i16:
4113 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4114 RegOp2 = X86::DX;
4115 break;
4116 case MVT::i32:
4117 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4118 RegOp2 = X86::EDX;
4119 break;
4120 }
4121 }
4122
4123 // Adjust ESP.
4124 if (ESPOffset)
4125 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4126 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4127
4128 // TODO: handle jmp [mem]
4129 if (!isDirect) {
4130 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4131 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00004132 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00004133 } else {
4134 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4135 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4136 }
4137 // ADD IMPLICIT USE RegOp1/RegOp2's
4138}
4139
Chris Lattner96113fd2005-01-17 19:25:26 +00004140
Chris Lattner88c8a232005-01-07 07:49:41 +00004141void ISel::Select(SDOperand N) {
4142 unsigned Tmp1, Tmp2, Opc;
4143
Nate Begeman95210522005-03-24 04:39:54 +00004144 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00004145 return; // Already selected.
4146
Chris Lattner36f78482005-01-11 06:14:36 +00004147 SDNode *Node = N.Val;
4148
4149 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00004150 default:
Chris Lattner36f78482005-01-11 06:14:36 +00004151 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00004152 assert(0 && "Node not handled yet!");
4153 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00004154 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00004155 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004156 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00004157 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4158 Select(Node->getOperand(OneFirst));
4159 Select(Node->getOperand(!OneFirst));
4160 } else {
4161 std::vector<std::pair<unsigned, unsigned> > OpsP;
4162 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4163 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4164 std::sort(OpsP.begin(), OpsP.end());
4165 std::reverse(OpsP.begin(), OpsP.end());
4166 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4167 Select(Node->getOperand(OpsP[i].second));
4168 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00004169 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004170 case ISD::CopyToReg:
Chris Lattner2cfce682005-01-12 02:02:48 +00004171 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4172 Select(N.getOperand(0));
4173 Tmp1 = SelectExpr(N.getOperand(1));
4174 } else {
4175 Tmp1 = SelectExpr(N.getOperand(1));
4176 Select(N.getOperand(0));
4177 }
Chris Lattnere727af02005-01-13 20:50:02 +00004178 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00004179
Chris Lattner88c8a232005-01-07 07:49:41 +00004180 if (Tmp1 != Tmp2) {
4181 switch (N.getOperand(1).getValueType()) {
4182 default: assert(0 && "Invalid type for operation!");
4183 case MVT::i1:
4184 case MVT::i8: Opc = X86::MOV8rr; break;
4185 case MVT::i16: Opc = X86::MOV16rr; break;
4186 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004187 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004188 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00004189 if (X86ScalarSSE) {
4190 Opc = X86::MOVAPDrr;
4191 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004192 Opc = X86::FpMOV;
4193 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004194 }
4195 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004196 }
4197 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4198 }
4199 return;
4200 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00004201 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4202 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4203 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4204 if (EmitPotentialTailCall(Node))
4205 return;
4206
Chris Lattner88c8a232005-01-07 07:49:41 +00004207 switch (N.getNumOperands()) {
4208 default:
4209 assert(0 && "Unknown return instruction!");
4210 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00004211 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004212 N.getOperand(2).getValueType() == MVT::i32 &&
4213 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004214 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4215 Tmp1 = SelectExpr(N.getOperand(1));
4216 Tmp2 = SelectExpr(N.getOperand(2));
4217 } else {
4218 Tmp2 = SelectExpr(N.getOperand(2));
4219 Tmp1 = SelectExpr(N.getOperand(1));
4220 }
4221 Select(N.getOperand(0));
4222
Chris Lattner88c8a232005-01-07 07:49:41 +00004223 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4224 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00004225 break;
4226 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004227 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4228 Select(N.getOperand(0));
4229 Tmp1 = SelectExpr(N.getOperand(1));
4230 } else {
4231 Tmp1 = SelectExpr(N.getOperand(1));
4232 Select(N.getOperand(0));
4233 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004234 switch (N.getOperand(1).getValueType()) {
4235 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00004236 case MVT::f32:
4237 if (X86ScalarSSE) {
4238 // Spill the value to memory and reload it into top of stack.
4239 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4240 MachineFunction *F = BB->getParent();
4241 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4242 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4243 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4244 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004245 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004246 } else {
4247 assert(0 && "MVT::f32 only legal with scalar sse fp");
4248 abort();
4249 }
4250 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004251 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00004252 if (X86ScalarSSE) {
4253 // Spill the value to memory and reload it into top of stack.
4254 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4255 MachineFunction *F = BB->getParent();
4256 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4257 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4258 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4259 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004260 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004261 } else {
4262 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4263 }
4264 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004265 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00004266 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4267 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004268 }
4269 break;
4270 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004271 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004272 break;
4273 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004274 if (X86Lowering.getBytesToPopOnReturn() == 0)
4275 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4276 else
4277 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00004278 return;
4279 case ISD::BR: {
4280 Select(N.getOperand(0));
4281 MachineBasicBlock *Dest =
4282 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4283 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4284 return;
4285 }
4286
4287 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00004288 MachineBasicBlock *Dest =
4289 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004290
Chris Lattner88c8a232005-01-07 07:49:41 +00004291 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4292 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00004293 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4294 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4295 Select(N.getOperand(0));
4296 Tmp1 = SelectExpr(N.getOperand(1));
4297 } else {
4298 Tmp1 = SelectExpr(N.getOperand(1));
4299 Select(N.getOperand(0));
4300 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004301 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4302 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4303 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004304
Chris Lattner88c8a232005-01-07 07:49:41 +00004305 return;
4306 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004307
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004308 case ISD::LOAD:
4309 // If this load could be folded into the only using instruction, and if it
4310 // is safe to emit the instruction here, try to do so now.
4311 if (Node->hasNUsesOfValue(1, 0)) {
4312 SDOperand TheVal = N.getValue(0);
4313 SDNode *User = 0;
4314 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4315 assert(UI != Node->use_end() && "Didn't find use!");
4316 SDNode *UN = *UI;
4317 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4318 if (UN->getOperand(i) == TheVal) {
4319 User = UN;
4320 goto FoundIt;
4321 }
4322 }
4323 FoundIt:
4324 // Only handle unary operators right now.
4325 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00004326 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004327 SelectExpr(SDOperand(User, 0));
4328 return;
4329 }
4330 }
Chris Lattner28a205e2005-01-18 04:00:54 +00004331 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004332 SelectExpr(N);
4333 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00004334 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00004335 case ISD::EXTLOAD:
4336 case ISD::SEXTLOAD:
4337 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00004338 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner1b3520c2005-05-14 08:48:15 +00004339 case X86ISD::TAILCALL:
4340 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00004341 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00004342 SelectExpr(N);
4343 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004344 case ISD::CopyFromReg:
Chris Lattnera36117b2005-05-14 06:52:07 +00004345 case X86ISD::FILD64m:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004346 ExprMap.erase(N);
4347 SelectExpr(N.getValue(0));
4348 return;
Chris Lattner67756e22005-07-29 00:40:01 +00004349
Chris Lattner6dc60e82005-07-29 00:54:34 +00004350 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00004351 assert(N.getOperand(1).getValueType() == MVT::f64);
4352 X86AddressMode AM;
4353 Select(N.getOperand(0)); // Select the token chain
4354
4355 unsigned ValReg;
4356 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4357 ValReg = SelectExpr(N.getOperand(1));
4358 SelectAddress(N.getOperand(2), AM);
4359 } else {
4360 SelectAddress(N.getOperand(2), AM);
4361 ValReg = SelectExpr(N.getOperand(1));
4362 }
Chris Lattner6dc60e82005-07-29 00:54:34 +00004363
4364 // Change the floating point control register to use "round towards zero"
4365 // mode when truncating to an integer value.
4366 //
4367 MachineFunction *F = BB->getParent();
4368 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4369 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
4370
4371 // Load the old value of the high byte of the control word...
4372 unsigned HighPartOfCW = MakeReg(MVT::i8);
4373 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
4374 CWFrameIdx, 1);
4375
4376 // Set the high part to be round to zero...
4377 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
4378 CWFrameIdx, 1).addImm(12);
4379
4380 // Reload the modified control word now...
4381 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
4382
4383 // Restore the memory image of control word to original value
4384 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
4385 CWFrameIdx, 1).addReg(HighPartOfCW);
4386
Chris Lattner67756e22005-07-29 00:40:01 +00004387 addFullAddress(BuildMI(BB, X86::FISTP64m, 5), AM).addReg(ValReg);
Chris Lattner6dc60e82005-07-29 00:54:34 +00004388
4389 // Reload the original control word now.
4390 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00004391 return;
4392 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004393
Chris Lattner36db1ed2005-07-10 00:29:18 +00004394 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00004395 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00004396 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00004397 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4398 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4399 && "Unsupported TRUNCSTORE for this target!");
4400
4401 if (StoredTy == MVT::i16) {
4402 // FIXME: This is here just to allow testing. X86 doesn't really have a
4403 // TRUNCSTORE i16 operation, but this is required for targets that do not
4404 // have 16-bit integer registers. We occasionally disable 16-bit integer
4405 // registers to test the promotion code.
4406 Select(N.getOperand(0));
4407 Tmp1 = SelectExpr(N.getOperand(1));
4408 SelectAddress(N.getOperand(2), AM);
4409
4410 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4411 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4412 return;
4413 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004414
4415 // Store of constant bool?
4416 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4417 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4418 Select(N.getOperand(0));
4419 SelectAddress(N.getOperand(2), AM);
4420 } else {
4421 SelectAddress(N.getOperand(2), AM);
4422 Select(N.getOperand(0));
4423 }
4424 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4425 return;
4426 }
4427
4428 switch (StoredTy) {
4429 default: assert(0 && "Cannot truncstore this type!");
4430 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004431 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004432 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begeman8a093362005-07-06 18:59:04 +00004433 Opc = X86::FST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00004434 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00004435
Chris Lattnere18a4c42005-01-15 05:22:24 +00004436 std::vector<std::pair<unsigned, unsigned> > RP;
4437 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4438 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4439 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4440 std::sort(RP.begin(), RP.end());
4441
Chris Lattner80c5b972005-02-23 05:57:21 +00004442 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00004443 for (unsigned i = 0; i != 3; ++i)
4444 switch (RP[2-i].second) {
4445 default: assert(0 && "Unknown operand number!");
4446 case 0: Select(N.getOperand(0)); break;
4447 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4448 case 2: SelectAddress(N.getOperand(2), AM); break;
4449 }
4450
4451 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4452 return;
4453 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004454 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00004455 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00004456
4457 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4458 Opc = 0;
4459 switch (CN->getValueType(0)) {
4460 default: assert(0 && "Invalid type for operation!");
4461 case MVT::i1:
4462 case MVT::i8: Opc = X86::MOV8mi; break;
4463 case MVT::i16: Opc = X86::MOV16mi; break;
4464 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004465 }
4466 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004467 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4468 Select(N.getOperand(0));
4469 SelectAddress(N.getOperand(2), AM);
4470 } else {
4471 SelectAddress(N.getOperand(2), AM);
4472 Select(N.getOperand(0));
4473 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004474 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4475 return;
4476 }
Chris Lattneradcfc172005-04-21 19:03:24 +00004477 } else if (GlobalAddressSDNode *GA =
4478 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4479 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4480
4481 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4482 Select(N.getOperand(0));
4483 SelectAddress(N.getOperand(2), AM);
4484 } else {
4485 SelectAddress(N.getOperand(2), AM);
4486 Select(N.getOperand(0));
4487 }
Nate Begemana0b5e032005-07-15 00:38:55 +00004488 GlobalValue *GV = GA->getGlobal();
4489 // For Darwin, external and weak symbols are indirect, so we want to load
4490 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004491 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00004492 (GV->hasWeakLinkage() || GV->isExternal())) {
4493 Tmp1 = MakeReg(MVT::i32);
4494 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4495 .addGlobalAddress(GV, false, 0);
4496 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4497 } else {
4498 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4499 }
Chris Lattneradcfc172005-04-21 19:03:24 +00004500 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004501 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00004502
4503 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00004504 if (TryToFoldLoadOpStore(Node))
4505 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00004506
Chris Lattner88c8a232005-01-07 07:49:41 +00004507 switch (N.getOperand(1).getValueType()) {
4508 default: assert(0 && "Cannot store this type!");
4509 case MVT::i1:
4510 case MVT::i8: Opc = X86::MOV8mr; break;
4511 case MVT::i16: Opc = X86::MOV16mr; break;
4512 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004513 case MVT::f32: Opc = X86::MOVSSmr; break;
4514 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004515 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00004516
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004517 std::vector<std::pair<unsigned, unsigned> > RP;
4518 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4519 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4520 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4521 std::sort(RP.begin(), RP.end());
4522
Chris Lattner80c5b972005-02-23 05:57:21 +00004523 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004524 for (unsigned i = 0; i != 3; ++i)
4525 switch (RP[2-i].second) {
4526 default: assert(0 && "Unknown operand number!");
4527 case 0: Select(N.getOperand(0)); break;
4528 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00004529 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004530 }
4531
Chris Lattner88c8a232005-01-07 07:49:41 +00004532 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4533 return;
4534 }
Chris Lattner2dce7032005-05-12 23:24:06 +00004535 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004536 Select(N.getOperand(0));
4537 // Stack amount
4538 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4539 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4540 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00004541 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00004542 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004543 return;
Chris Lattner36f78482005-01-11 06:14:36 +00004544 case ISD::MEMSET: {
4545 Select(N.getOperand(0)); // Select the chain.
4546 unsigned Align =
4547 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4548 if (Align == 0) Align = 1;
4549
4550 // Turn the byte code into # iterations
4551 unsigned CountReg;
4552 unsigned Opcode;
4553 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4554 unsigned Val = ValC->getValue() & 255;
4555
4556 // If the value is a constant, then we can potentially use larger sets.
4557 switch (Align & 3) {
4558 case 2: // WORD aligned
4559 CountReg = MakeReg(MVT::i32);
4560 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4561 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4562 } else {
4563 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4564 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4565 }
4566 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4567 Opcode = X86::REP_STOSW;
4568 break;
4569 case 0: // DWORD aligned
4570 CountReg = MakeReg(MVT::i32);
4571 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4572 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4573 } else {
4574 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4575 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4576 }
4577 Val = (Val << 8) | Val;
4578 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4579 Opcode = X86::REP_STOSD;
4580 break;
4581 default: // BYTE aligned
4582 CountReg = SelectExpr(Node->getOperand(3));
4583 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4584 Opcode = X86::REP_STOSB;
4585 break;
4586 }
4587 } else {
4588 // If it's not a constant value we are storing, just fall back. We could
4589 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4590 unsigned ValReg = SelectExpr(Node->getOperand(2));
4591 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4592 CountReg = SelectExpr(Node->getOperand(3));
4593 Opcode = X86::REP_STOSB;
4594 }
4595
4596 // No matter what the alignment is, we put the source in ESI, the
4597 // destination in EDI, and the count in ECX.
4598 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4599 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4600 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4601 BuildMI(BB, Opcode, 0);
4602 return;
4603 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004604 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00004605 Select(N.getOperand(0)); // Select the chain.
4606 unsigned Align =
4607 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4608 if (Align == 0) Align = 1;
4609
4610 // Turn the byte code into # iterations
4611 unsigned CountReg;
4612 unsigned Opcode;
4613 switch (Align & 3) {
4614 case 2: // WORD aligned
4615 CountReg = MakeReg(MVT::i32);
4616 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4617 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4618 } else {
4619 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4620 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4621 }
4622 Opcode = X86::REP_MOVSW;
4623 break;
4624 case 0: // DWORD aligned
4625 CountReg = MakeReg(MVT::i32);
4626 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4627 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4628 } else {
4629 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4630 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4631 }
4632 Opcode = X86::REP_MOVSD;
4633 break;
4634 default: // BYTE aligned
4635 CountReg = SelectExpr(Node->getOperand(3));
4636 Opcode = X86::REP_MOVSB;
4637 break;
4638 }
4639
4640 // No matter what the alignment is, we put the source in ESI, the
4641 // destination in EDI, and the count in ECX.
4642 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4643 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4644 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4645 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4646 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4647 BuildMI(BB, Opcode, 0);
4648 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004649 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004650 case ISD::WRITEPORT:
4651 if (Node->getOperand(2).getValueType() != MVT::i16) {
4652 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4653 exit(1);
4654 }
4655 Select(Node->getOperand(0)); // Emit the chain.
4656
4657 Tmp1 = SelectExpr(Node->getOperand(1));
4658 switch (Node->getOperand(1).getValueType()) {
4659 case MVT::i8:
4660 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4661 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4662 break;
4663 case MVT::i16:
4664 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4665 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4666 break;
4667 case MVT::i32:
4668 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4669 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4670 break;
4671 default:
4672 std::cerr << "llvm.writeport: invalid data type for X86 target";
4673 exit(1);
4674 }
4675
4676 // If the port is a single-byte constant, use the immediate form.
4677 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4678 if ((CN->getValue() & 255) == CN->getValue()) {
4679 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4680 return;
4681 }
4682
4683 // Otherwise, move the I/O port address into the DX register.
4684 unsigned Reg = SelectExpr(Node->getOperand(2));
4685 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4686 BuildMI(BB, Opc, 0);
4687 return;
4688 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004689 assert(0 && "Should not be reached!");
4690}
4691
4692
4693/// createX86PatternInstructionSelector - This pass converts an LLVM function
4694/// into a machine code representation using pattern matching and a machine
4695/// description file.
4696///
4697FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004698 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00004699}