blob: 6bd8e27607ff92da5a1558070e970d114721382c [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
57 /// CALL/TAILCALL - These operations represent an abstract X86 call
58 /// instruction, which includes a bunch of information. In particular the
59 /// operands of these node are:
60 ///
61 /// #0 - The incoming token chain
62 /// #1 - The callee
63 /// #2 - The number of arg bytes the caller pushes on the stack.
64 /// #3 - The number of arg bytes the callee pops off the stack.
65 /// #4 - The value to pass in AL/AX/EAX (optional)
66 /// #5 - The value to pass in DL/DX/EDX (optional)
67 ///
68 /// The result values of these nodes are:
69 ///
70 /// #0 - The outgoing token chain
71 /// #1 - The first register result value (optional)
72 /// #2 - The second register result value (optional)
73 ///
74 /// The CALL vs TAILCALL distinction boils down to whether the callee is
75 /// known not to modify the caller's stack frame, as is standard with
76 /// LLVM.
77 CALL,
78 TAILCALL,
Chris Lattnera36117b2005-05-14 06:52:07 +000079 };
80 }
81}
82
Chris Lattner88c8a232005-01-07 07:49:41 +000083//===----------------------------------------------------------------------===//
84// X86TargetLowering - X86 Implementation of the TargetLowering interface
85namespace {
86 class X86TargetLowering : public TargetLowering {
87 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner9f59d282005-01-09 00:01:27 +000088 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattnerdd66a412005-05-15 05:46:45 +000089 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
90 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner88c8a232005-01-07 07:49:41 +000091 public:
92 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
93 // Set up the TargetLowering object.
Chris Lattnerc1f386c2005-01-17 00:00:33 +000094
Chris Lattner5011ff02005-05-13 22:46:57 +000095 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattnerc1f386c2005-01-17 00:00:33 +000096 setShiftAmountType(MVT::i8);
97 setSetCCResultType(MVT::i8);
Chris Lattner38fd9702005-04-07 19:41:46 +000098 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnerd8d30662005-01-19 03:36:30 +000099 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000100
101 // Set up the register classes.
Nate Begeman8a093362005-07-06 18:59:04 +0000102 // FIXME: Eliminate these two classes when legalize can handle promotions
103 // well.
104 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner88c8a232005-01-07 07:49:41 +0000105 addRegisterClass(MVT::i8, X86::R8RegisterClass);
106 addRegisterClass(MVT::i16, X86::R16RegisterClass);
107 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Nate Begeman8a093362005-07-06 18:59:04 +0000108
Chris Lattnera36117b2005-05-14 06:52:07 +0000109 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Chris Lattnera3a135a2005-04-09 03:22:37 +0000110 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000111 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000114 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
115 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
116 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner05ad4b82005-05-11 05:00:34 +0000117 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
118 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
119 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
120 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
121 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
122 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000123 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
124 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +0000125 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Nate Begeman8a093362005-07-06 18:59:04 +0000126
Chris Lattner6c6a39a2005-05-09 20:37:29 +0000127 setOperationAction(ISD::READIO , MVT::i1 , Expand);
128 setOperationAction(ISD::READIO , MVT::i8 , Expand);
129 setOperationAction(ISD::READIO , MVT::i16 , Expand);
130 setOperationAction(ISD::READIO , MVT::i32 , Expand);
131 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
132 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
133 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
134 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Nate Begeman8a093362005-07-06 18:59:04 +0000135
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000136 // These should be promoted to a larger select which is supported.
Nate Begeman8a093362005-07-06 18:59:04 +0000137 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000138 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman8a093362005-07-06 18:59:04 +0000139
140 if (X86ScalarSSE) {
141 // Set up the FP register classes.
142 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
143 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
144
145 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
146 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
147
148 // We don't support sin/cos/sqrt/fmod
149 setOperationAction(ISD::FSIN , MVT::f64, Expand);
150 setOperationAction(ISD::FCOS , MVT::f64, Expand);
151 setOperationAction(ISD::FABS , MVT::f64, Expand);
152 setOperationAction(ISD::FNEG , MVT::f64, Expand);
153 setOperationAction(ISD::SREM , MVT::f64, Expand);
154 setOperationAction(ISD::FSIN , MVT::f32, Expand);
155 setOperationAction(ISD::FCOS , MVT::f32, Expand);
156 setOperationAction(ISD::FABS , MVT::f32, Expand);
157 setOperationAction(ISD::FNEG , MVT::f32, Expand);
158 setOperationAction(ISD::SREM , MVT::f32, Expand);
159 } else {
160 // Set up the FP register classes.
161 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
162
163 if (!UnsafeFPMath) {
164 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
165 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
166 }
167
168 addLegalFPImmediate(+0.0); // FLD0
169 addLegalFPImmediate(+1.0); // FLD1
170 addLegalFPImmediate(-0.0); // FLD0/FCHS
171 addLegalFPImmediate(-1.0); // FLD1/FCHS
172 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000173 computeRegisterProperties();
Chris Lattner88c8a232005-01-07 07:49:41 +0000174 }
Nate Begeman8a093362005-07-06 18:59:04 +0000175
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000176 // Return the number of bytes that a function should pop when it returns (in
177 // addition to the space used by the return address).
178 //
179 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
180
Chris Lattnerdd66a412005-05-15 05:46:45 +0000181 // Return the number of bytes that the caller reserves for arguments passed
182 // to this function.
183 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
184
Chris Lattnera36117b2005-05-14 06:52:07 +0000185 /// LowerOperation - Provide custom lowering hooks for some operations.
186 ///
187 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
188
Chris Lattner88c8a232005-01-07 07:49:41 +0000189 /// LowerArguments - This hook must be implemented to indicate how we should
190 /// lower the arguments for the specified function, into the specified DAG.
191 virtual std::vector<SDOperand>
192 LowerArguments(Function &F, SelectionDAG &DAG);
193
194 /// LowerCallTo - This hook lowers an abstract call to a function into an
195 /// actual call.
Chris Lattnerb52e0412005-01-08 19:28:19 +0000196 virtual std::pair<SDOperand, SDOperand>
Chris Lattner36674a12005-05-12 19:56:45 +0000197 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000198 bool isTailCall, SDOperand Callee, ArgListTy &Args,
199 SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000200
Chris Lattnera7220852005-07-05 19:58:54 +0000201 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
202 Value *VAListV, SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000203 virtual std::pair<SDOperand,SDOperand>
Chris Lattnera7220852005-07-05 19:58:54 +0000204 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
205 const Type *ArgTy, SelectionDAG &DAG);
206
Chris Lattner9f59d282005-01-09 00:01:27 +0000207 virtual std::pair<SDOperand, SDOperand>
208 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
209 SelectionDAG &DAG);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000210
211 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
212
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000213 private:
214 // C Calling Convention implementation.
215 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
216 std::pair<SDOperand, SDOperand>
217 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000218 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000219 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
220
221 // Fast Calling Convention implementation.
222 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
223 std::pair<SDOperand, SDOperand>
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000224 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000225 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000226 };
227}
228
Chris Lattner88c8a232005-01-07 07:49:41 +0000229std::vector<SDOperand>
230X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000231 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
232 return LowerFastCCArguments(F, DAG);
233 return LowerCCCArguments(F, DAG);
234}
235
236std::pair<SDOperand, SDOperand>
237X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
238 bool isVarArg, unsigned CallingConv,
Chris Lattner2e77db62005-05-13 18:50:42 +0000239 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000240 SDOperand Callee, ArgListTy &Args,
241 SelectionDAG &DAG) {
242 assert((!isVarArg || CallingConv == CallingConv::C) &&
243 "Only C takes varargs!");
244 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000245 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
246 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000247}
248
249//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000250// C Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000251//===----------------------------------------------------------------------===//
252
253std::vector<SDOperand>
254X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000255 std::vector<SDOperand> ArgValues;
256
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000257 MachineFunction &MF = DAG.getMachineFunction();
258 MachineFrameInfo *MFI = MF.getFrameInfo();
259
Chris Lattner88c8a232005-01-07 07:49:41 +0000260 // Add DAG nodes to load the arguments... On entry to a function on the X86,
261 // the stack frame looks like this:
262 //
263 // [ESP] -- return address
264 // [ESP + 4] -- first argument (leftmost lexically)
265 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukmanc88330a2005-04-21 23:38:14 +0000266 // ...
Chris Lattner88c8a232005-01-07 07:49:41 +0000267 //
Chris Lattner88c8a232005-01-07 07:49:41 +0000268 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattner531f9e92005-03-15 04:54:21 +0000269 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000270 MVT::ValueType ObjectVT = getValueType(I->getType());
271 unsigned ArgIncrement = 4;
272 unsigned ObjSize;
273 switch (ObjectVT) {
274 default: assert(0 && "Unhandled argument type!");
275 case MVT::i1:
276 case MVT::i8: ObjSize = 1; break;
277 case MVT::i16: ObjSize = 2; break;
278 case MVT::i32: ObjSize = 4; break;
279 case MVT::i64: ObjSize = ArgIncrement = 8; break;
280 case MVT::f32: ObjSize = 4; break;
281 case MVT::f64: ObjSize = ArgIncrement = 8; break;
282 }
283 // Create the frame index object for this incoming parameter...
284 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000285
Chris Lattner88c8a232005-01-07 07:49:41 +0000286 // Create the SelectionDAG nodes corresponding to a load from this parameter
287 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
288
289 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
290 // dead loads.
291 SDOperand ArgValue;
292 if (!I->use_empty())
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000293 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
294 DAG.getSrcValue(NULL));
Chris Lattner88c8a232005-01-07 07:49:41 +0000295 else {
296 if (MVT::isInteger(ObjectVT))
297 ArgValue = DAG.getConstant(0, ObjectVT);
298 else
299 ArgValue = DAG.getConstantFP(0, ObjectVT);
300 }
301 ArgValues.push_back(ArgValue);
302
303 ArgOffset += ArgIncrement; // Move on to the next argument...
304 }
305
306 // If the function takes variable number of arguments, make a frame index for
307 // the start of the first vararg value... for expansion of llvm.va_start.
308 if (F.isVarArg())
309 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000310 ReturnAddrIndex = 0; // No return address slot generated yet.
311 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000312 BytesCallerReserves = ArgOffset;
Chris Lattnerb59006c2005-04-09 15:23:56 +0000313
314 // Finally, inform the code generator which regs we return values in.
315 switch (getValueType(F.getReturnType())) {
316 default: assert(0 && "Unknown type!");
317 case MVT::isVoid: break;
318 case MVT::i1:
319 case MVT::i8:
320 case MVT::i16:
321 case MVT::i32:
322 MF.addLiveOut(X86::EAX);
323 break;
324 case MVT::i64:
325 MF.addLiveOut(X86::EAX);
326 MF.addLiveOut(X86::EDX);
327 break;
328 case MVT::f32:
329 case MVT::f64:
330 MF.addLiveOut(X86::ST0);
331 break;
332 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000333 return ArgValues;
334}
335
Chris Lattnerb52e0412005-01-08 19:28:19 +0000336std::pair<SDOperand, SDOperand>
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000337X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000338 bool isVarArg, bool isTailCall,
339 SDOperand Callee, ArgListTy &Args,
340 SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000341 // Count how many bytes are to be pushed on the stack.
342 unsigned NumBytes = 0;
343
344 if (Args.empty()) {
345 // Save zero bytes.
Chris Lattner2dce7032005-05-12 23:24:06 +0000346 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000347 DAG.getConstant(0, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000348 } else {
349 for (unsigned i = 0, e = Args.size(); i != e; ++i)
350 switch (getValueType(Args[i].second)) {
351 default: assert(0 && "Unknown value type!");
352 case MVT::i1:
353 case MVT::i8:
354 case MVT::i16:
355 case MVT::i32:
356 case MVT::f32:
357 NumBytes += 4;
358 break;
359 case MVT::i64:
360 case MVT::f64:
361 NumBytes += 8;
362 break;
363 }
364
Chris Lattner2dce7032005-05-12 23:24:06 +0000365 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000366 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000367
368 // Arguments go on the stack in reverse order, as specified by the ABI.
369 unsigned ArgOffset = 0;
Chris Lattner720a62e2005-01-14 22:37:41 +0000370 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
371 DAG.getEntryNode());
Chris Lattnerc78776d2005-01-21 19:46:38 +0000372 std::vector<SDOperand> Stores;
373
Chris Lattner88c8a232005-01-07 07:49:41 +0000374 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000375 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
376 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
377
378 switch (getValueType(Args[i].second)) {
379 default: assert(0 && "Unexpected ValueType for argument!");
380 case MVT::i1:
381 case MVT::i8:
382 case MVT::i16:
383 // Promote the integer to 32 bits. If the input type is signed use a
384 // sign extend, otherwise use a zero extend.
385 if (Args[i].second->isSigned())
386 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
387 else
388 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
389
390 // FALL THROUGH
391 case MVT::i32:
392 case MVT::f32:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000393 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000394 Args[i].first, PtrOff,
395 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000396 ArgOffset += 4;
397 break;
398 case MVT::i64:
399 case MVT::f64:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000400 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000401 Args[i].first, PtrOff,
402 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000403 ArgOffset += 8;
404 break;
405 }
406 }
Chris Lattnerc78776d2005-01-21 19:46:38 +0000407 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner88c8a232005-01-07 07:49:41 +0000408 }
409
410 std::vector<MVT::ValueType> RetVals;
411 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner88c8a232005-01-07 07:49:41 +0000412 RetVals.push_back(MVT::Other);
413
Chris Lattner1b3520c2005-05-14 08:48:15 +0000414 // The result values produced have to be legal. Promote the result.
415 switch (RetTyVT) {
416 case MVT::isVoid: break;
417 default:
418 RetVals.push_back(RetTyVT);
419 break;
420 case MVT::i1:
421 case MVT::i8:
422 case MVT::i16:
423 RetVals.push_back(MVT::i32);
424 break;
425 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000426 if (X86ScalarSSE)
427 RetVals.push_back(MVT::f32);
428 else
429 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000430 break;
431 case MVT::i64:
432 RetVals.push_back(MVT::i32);
433 RetVals.push_back(MVT::i32);
434 break;
435 }
436 std::vector<SDOperand> Ops;
437 Ops.push_back(Chain);
438 Ops.push_back(Callee);
439 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
440 Ops.push_back(DAG.getConstant(0, getPointerTy()));
441 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
442 RetVals, Ops);
443 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
444
445 SDOperand ResultVal;
446 switch (RetTyVT) {
447 case MVT::isVoid: break;
448 default:
449 ResultVal = TheCall.getValue(1);
450 break;
451 case MVT::i1:
452 case MVT::i8:
453 case MVT::i16:
454 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
455 break;
456 case MVT::f32:
457 // FIXME: we would really like to remember that this FP_ROUND operation is
458 // okay to eliminate if we allow excess FP precision.
459 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
460 break;
461 case MVT::i64:
462 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
463 TheCall.getValue(2));
464 break;
465 }
466
467 return std::make_pair(ResultVal, Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000468}
469
Chris Lattnera7220852005-07-05 19:58:54 +0000470SDOperand
471X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
472 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000473 // vastart just stores the address of the VarArgsFrameIndex slot.
474 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnera7220852005-07-05 19:58:54 +0000475 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
476 DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000477}
478
Chris Lattnera7220852005-07-05 19:58:54 +0000479
480std::pair<SDOperand,SDOperand>
481X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
482 Value *VAListV, const Type *ArgTy,
483 SelectionDAG &DAG) {
Chris Lattner9f59d282005-01-09 00:01:27 +0000484 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnera7220852005-07-05 19:58:54 +0000485 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
486 VAListP, DAG.getSrcValue(VAListV));
487 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner91ae1292005-07-05 17:50:16 +0000488 DAG.getSrcValue(NULL));
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000489 unsigned Amt;
490 if (ArgVT == MVT::i32)
491 Amt = 4;
492 else {
493 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
494 "Other types should have been promoted for varargs!");
495 Amt = 8;
Chris Lattner9f59d282005-01-09 00:01:27 +0000496 }
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000497 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
498 DAG.getConstant(Amt, Val.getValueType()));
499 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera7220852005-07-05 19:58:54 +0000500 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000501 return std::make_pair(Result, Chain);
502}
Misha Brukmanc88330a2005-04-21 23:38:14 +0000503
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000504//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000505// Fast Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000506//===----------------------------------------------------------------------===//
507//
508// The X86 'fast' calling convention passes up to two integer arguments in
509// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
510// and requires that the callee pop its arguments off the stack (allowing proper
511// tail calls), and has the same return value conventions as C calling convs.
512//
Chris Lattner9b29fe22005-05-13 23:49:10 +0000513// This calling convention always arranges for the callee pop value to be 8n+4
514// bytes, which is needed for tail recursion elimination and stack alignment
515// reasons.
516//
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000517// Note that this can be enhanced in the future to pass fp vals in registers
518// (when we have a global fp allocator) and do other tricks.
519//
Chris Lattner0b17b452005-05-13 07:38:09 +0000520
521/// AddLiveIn - This helper function adds the specified physical register to the
522/// MachineFunction as a live in value. It also creates a corresponding virtual
523/// register for it.
524static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
525 TargetRegisterClass *RC) {
526 assert(RC->contains(PReg) && "Not the correct regclass!");
527 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
528 MF.addLiveIn(PReg, VReg);
529 return VReg;
530}
531
532
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000533std::vector<SDOperand>
534X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
535 std::vector<SDOperand> ArgValues;
536
537 MachineFunction &MF = DAG.getMachineFunction();
538 MachineFrameInfo *MFI = MF.getFrameInfo();
539
540 // Add DAG nodes to load the arguments... On entry to a function the stack
541 // frame looks like this:
542 //
543 // [ESP] -- return address
544 // [ESP + 4] -- first nonreg argument (leftmost lexically)
545 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
546 // ...
547 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
548
549 // Keep track of the number of integer regs passed so far. This can be either
550 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
551 // used).
552 unsigned NumIntRegs = 0;
553
554 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
555 MVT::ValueType ObjectVT = getValueType(I->getType());
556 unsigned ArgIncrement = 4;
557 unsigned ObjSize = 0;
558 SDOperand ArgValue;
559
560 switch (ObjectVT) {
561 default: assert(0 && "Unhandled argument type!");
562 case MVT::i1:
563 case MVT::i8:
564 if (NumIntRegs < 2) {
565 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000566 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
567 X86::R8RegisterClass);
568 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000569 DAG.setRoot(ArgValue.getValue(1));
570 }
571 ++NumIntRegs;
572 break;
573 }
574
575 ObjSize = 1;
576 break;
577 case MVT::i16:
578 if (NumIntRegs < 2) {
579 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000580 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
581 X86::R16RegisterClass);
582 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000583 DAG.setRoot(ArgValue.getValue(1));
584 }
585 ++NumIntRegs;
586 break;
587 }
588 ObjSize = 2;
589 break;
590 case MVT::i32:
591 if (NumIntRegs < 2) {
592 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000593 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
594 X86::R32RegisterClass);
595 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000596 DAG.setRoot(ArgValue.getValue(1));
597 }
598 ++NumIntRegs;
599 break;
600 }
601 ObjSize = 4;
602 break;
603 case MVT::i64:
604 if (NumIntRegs == 0) {
605 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000606 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
607 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000608
Chris Lattner0b17b452005-05-13 07:38:09 +0000609 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
610 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000611 DAG.setRoot(Hi.getValue(1));
612
613 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
614 }
615 NumIntRegs = 2;
616 break;
617 } else if (NumIntRegs == 1) {
618 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000619 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
620 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000621 DAG.setRoot(Low.getValue(1));
622
623 // Load the high part from memory.
624 // Create the frame index object for this incoming parameter...
625 int FI = MFI->CreateFixedObject(4, ArgOffset);
626 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
627 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
628 DAG.getSrcValue(NULL));
629 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
630 }
631 ArgOffset += 4;
632 NumIntRegs = 2;
633 break;
634 }
635 ObjSize = ArgIncrement = 8;
636 break;
637 case MVT::f32: ObjSize = 4; break;
638 case MVT::f64: ObjSize = ArgIncrement = 8; break;
639 }
640
641 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
642 // dead loads.
643 if (ObjSize && !I->use_empty()) {
644 // Create the frame index object for this incoming parameter...
645 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
646
647 // Create the SelectionDAG nodes corresponding to a load from this
648 // parameter.
649 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
650
651 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
652 DAG.getSrcValue(NULL));
653 } else if (ArgValue.Val == 0) {
654 if (MVT::isInteger(ObjectVT))
655 ArgValue = DAG.getConstant(0, ObjectVT);
656 else
657 ArgValue = DAG.getConstantFP(0, ObjectVT);
658 }
659 ArgValues.push_back(ArgValue);
660
661 if (ObjSize)
662 ArgOffset += ArgIncrement; // Move on to the next argument.
663 }
664
Chris Lattner9b29fe22005-05-13 23:49:10 +0000665 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
666 // arguments and the arguments after the retaddr has been pushed are aligned.
667 if ((ArgOffset & 7) == 0)
668 ArgOffset += 4;
669
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000670 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
671 ReturnAddrIndex = 0; // No return address slot generated yet.
672 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000673 BytesCallerReserves = 0;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000674
675 // Finally, inform the code generator which regs we return values in.
676 switch (getValueType(F.getReturnType())) {
677 default: assert(0 && "Unknown type!");
678 case MVT::isVoid: break;
679 case MVT::i1:
680 case MVT::i8:
681 case MVT::i16:
682 case MVT::i32:
683 MF.addLiveOut(X86::EAX);
684 break;
685 case MVT::i64:
686 MF.addLiveOut(X86::EAX);
687 MF.addLiveOut(X86::EDX);
688 break;
689 case MVT::f32:
690 case MVT::f64:
691 MF.addLiveOut(X86::ST0);
692 break;
693 }
694 return ArgValues;
695}
696
697std::pair<SDOperand, SDOperand>
698X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000699 bool isTailCall, SDOperand Callee,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000700 ArgListTy &Args, SelectionDAG &DAG) {
701 // Count how many bytes are to be pushed on the stack.
702 unsigned NumBytes = 0;
703
704 // Keep track of the number of integer regs passed so far. This can be either
705 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
706 // used).
707 unsigned NumIntRegs = 0;
708
709 for (unsigned i = 0, e = Args.size(); i != e; ++i)
710 switch (getValueType(Args[i].second)) {
711 default: assert(0 && "Unknown value type!");
712 case MVT::i1:
713 case MVT::i8:
714 case MVT::i16:
715 case MVT::i32:
716 if (NumIntRegs < 2) {
717 ++NumIntRegs;
718 break;
719 }
720 // fall through
721 case MVT::f32:
722 NumBytes += 4;
723 break;
724 case MVT::i64:
725 if (NumIntRegs == 0) {
726 NumIntRegs = 2;
727 break;
728 } else if (NumIntRegs == 1) {
729 NumIntRegs = 2;
730 NumBytes += 4;
731 break;
732 }
733
734 // fall through
735 case MVT::f64:
736 NumBytes += 8;
737 break;
738 }
739
Chris Lattner9b29fe22005-05-13 23:49:10 +0000740 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
741 // arguments and the arguments after the retaddr has been pushed are aligned.
742 if ((NumBytes & 7) == 0)
743 NumBytes += 4;
744
Chris Lattner2dce7032005-05-12 23:24:06 +0000745 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000746 DAG.getConstant(NumBytes, getPointerTy()));
747
748 // Arguments go on the stack in reverse order, as specified by the ABI.
749 unsigned ArgOffset = 0;
750 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
751 DAG.getEntryNode());
752 NumIntRegs = 0;
753 std::vector<SDOperand> Stores;
754 std::vector<SDOperand> RegValuesToPass;
755 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
756 switch (getValueType(Args[i].second)) {
757 default: assert(0 && "Unexpected ValueType for argument!");
758 case MVT::i1:
759 case MVT::i8:
760 case MVT::i16:
761 case MVT::i32:
762 if (NumIntRegs < 2) {
763 RegValuesToPass.push_back(Args[i].first);
764 ++NumIntRegs;
765 break;
766 }
767 // Fall through
768 case MVT::f32: {
769 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
770 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
771 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
772 Args[i].first, PtrOff,
773 DAG.getSrcValue(NULL)));
774 ArgOffset += 4;
775 break;
776 }
777 case MVT::i64:
778 if (NumIntRegs < 2) { // Can pass part of it in regs?
779 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
780 Args[i].first, DAG.getConstant(1, MVT::i32));
781 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
782 Args[i].first, DAG.getConstant(0, MVT::i32));
783 RegValuesToPass.push_back(Lo);
784 ++NumIntRegs;
785 if (NumIntRegs < 2) { // Pass both parts in regs?
786 RegValuesToPass.push_back(Hi);
787 ++NumIntRegs;
788 } else {
789 // Pass the high part in memory.
790 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
791 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
792 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner18b2c2f2005-05-14 12:03:10 +0000793 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000794 ArgOffset += 4;
795 }
796 break;
797 }
798 // Fall through
799 case MVT::f64:
800 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
801 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
802 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
803 Args[i].first, PtrOff,
804 DAG.getSrcValue(NULL)));
805 ArgOffset += 8;
806 break;
807 }
808 }
809 if (!Stores.empty())
810 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
811
Chris Lattner9b29fe22005-05-13 23:49:10 +0000812 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
813 // arguments and the arguments after the retaddr has been pushed are aligned.
814 if ((ArgOffset & 7) == 0)
815 ArgOffset += 4;
816
Chris Lattner1b3520c2005-05-14 08:48:15 +0000817 std::vector<MVT::ValueType> RetVals;
818 MVT::ValueType RetTyVT = getValueType(RetTy);
819
820 RetVals.push_back(MVT::Other);
821
822 // The result values produced have to be legal. Promote the result.
823 switch (RetTyVT) {
824 case MVT::isVoid: break;
825 default:
826 RetVals.push_back(RetTyVT);
827 break;
828 case MVT::i1:
829 case MVT::i8:
830 case MVT::i16:
831 RetVals.push_back(MVT::i32);
832 break;
833 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000834 if (X86ScalarSSE)
835 RetVals.push_back(MVT::f32);
836 else
837 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000838 break;
839 case MVT::i64:
840 RetVals.push_back(MVT::i32);
841 RetVals.push_back(MVT::i32);
842 break;
843 }
844
845 std::vector<SDOperand> Ops;
846 Ops.push_back(Chain);
847 Ops.push_back(Callee);
848 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
849 // Callee pops all arg values on the stack.
850 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
851
852 // Pass register arguments as needed.
853 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
854
855 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
856 RetVals, Ops);
857 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
858
859 SDOperand ResultVal;
860 switch (RetTyVT) {
861 case MVT::isVoid: break;
862 default:
863 ResultVal = TheCall.getValue(1);
864 break;
865 case MVT::i1:
866 case MVT::i8:
867 case MVT::i16:
868 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
869 break;
870 case MVT::f32:
871 // FIXME: we would really like to remember that this FP_ROUND operation is
872 // okay to eliminate if we allow excess FP precision.
873 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
874 break;
875 case MVT::i64:
876 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
877 TheCall.getValue(2));
878 break;
879 }
880
881 return std::make_pair(ResultVal, Chain);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000882}
883
Chris Lattnerdd66a412005-05-15 05:46:45 +0000884SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
885 if (ReturnAddrIndex == 0) {
886 // Set up a frame object for the return address.
887 MachineFunction &MF = DAG.getMachineFunction();
888 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
889 }
890
891 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
892}
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000893
894
Chris Lattner9f59d282005-01-09 00:01:27 +0000895
896std::pair<SDOperand, SDOperand> X86TargetLowering::
897LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
898 SelectionDAG &DAG) {
899 SDOperand Result;
900 if (Depth) // Depths > 0 not supported yet!
901 Result = DAG.getConstant(0, getPointerTy());
902 else {
Chris Lattnerdd66a412005-05-15 05:46:45 +0000903 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000904 if (!isFrameAddress)
905 // Just load the return address
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000906 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
907 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000908 else
909 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
910 DAG.getConstant(4, MVT::i32));
911 }
912 return std::make_pair(Result, Chain);
913}
Chris Lattner88c8a232005-01-07 07:49:41 +0000914
Chris Lattnera36117b2005-05-14 06:52:07 +0000915/// LowerOperation - Provide custom lowering hooks for some operations.
916///
917SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
918 switch (Op.getOpcode()) {
919 default: assert(0 && "Should not custom lower this!");
920 case ISD::SINT_TO_FP:
921 assert(Op.getValueType() == MVT::f64 &&
922 Op.getOperand(0).getValueType() == MVT::i64 &&
923 "Unknown SINT_TO_FP to lower!");
924 // We lower sint64->FP into a store to a temporary stack slot, followed by a
925 // FILD64m node.
926 MachineFunction &MF = DAG.getMachineFunction();
927 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
928 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
929 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
930 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
931 std::vector<MVT::ValueType> RTs;
932 RTs.push_back(MVT::f64);
933 RTs.push_back(MVT::Other);
934 std::vector<SDOperand> Ops;
935 Ops.push_back(Store);
936 Ops.push_back(StackSlot);
937 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
938 }
939}
940
941
942//===----------------------------------------------------------------------===//
943// Pattern Matcher Implementation
944//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +0000945
Chris Lattnera7acdda2005-01-18 01:06:26 +0000946namespace {
947 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
948 /// SDOperand's instead of register numbers for the leaves of the matched
949 /// tree.
950 struct X86ISelAddressMode {
951 enum {
952 RegBase,
953 FrameIndexBase,
954 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000955
Chris Lattnera7acdda2005-01-18 01:06:26 +0000956 struct { // This is really a union, discriminated by BaseType!
957 SDOperand Reg;
958 int FrameIndex;
959 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000960
Chris Lattnera7acdda2005-01-18 01:06:26 +0000961 unsigned Scale;
962 SDOperand IndexReg;
963 unsigned Disp;
964 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000965
Chris Lattnera7acdda2005-01-18 01:06:26 +0000966 X86ISelAddressMode()
967 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
968 }
969 };
970}
Chris Lattner88c8a232005-01-07 07:49:41 +0000971
972
973namespace {
974 Statistic<>
975 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
976
977 //===--------------------------------------------------------------------===//
978 /// ISel - X86 specific code to select X86 machine instructions for
979 /// SelectionDAG operations.
980 ///
981 class ISel : public SelectionDAGISel {
982 /// ContainsFPCode - Every instruction we select that uses or defines a FP
983 /// register should set this to true.
984 bool ContainsFPCode;
985
986 /// X86Lowering - This object fully describes how to lower LLVM code to an
987 /// X86-specific SelectionDAG.
988 X86TargetLowering X86Lowering;
989
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000990 /// RegPressureMap - This keeps an approximate count of the number of
991 /// registers required to evaluate each node in the graph.
992 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000993
994 /// ExprMap - As shared expressions are codegen'd, we keep track of which
995 /// vreg the value is produced in, so we only emit one copy of each compiled
996 /// tree.
997 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000998
Chris Lattnerdd66a412005-05-15 05:46:45 +0000999 /// TheDAG - The DAG being selected during Select* operations.
1000 SelectionDAG *TheDAG;
Nate Begemanf26625e2005-07-12 01:41:54 +00001001
1002 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
1003 /// make the right decision when generating code for different targets.
1004 const X86Subtarget *Subtarget;
Chris Lattner88c8a232005-01-07 07:49:41 +00001005 public:
1006 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanf26625e2005-07-12 01:41:54 +00001007 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner88c8a232005-01-07 07:49:41 +00001008 }
1009
Chris Lattnere1e844c2005-01-21 21:35:14 +00001010 virtual const char *getPassName() const {
1011 return "X86 Pattern Instruction Selection";
1012 }
1013
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001014 unsigned getRegPressure(SDOperand O) {
1015 return RegPressureMap[O.Val];
1016 }
1017 unsigned ComputeRegPressure(SDOperand O);
1018
Chris Lattner88c8a232005-01-07 07:49:41 +00001019 /// InstructionSelectBasicBlock - This callback is invoked by
1020 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +00001021 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +00001022
Chris Lattner0b17b452005-05-13 07:38:09 +00001023 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1024
Chris Lattner30607ec2005-01-25 20:03:11 +00001025 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1026 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +00001027 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +00001028 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +00001029 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001030 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +00001031 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner1d13a922005-01-10 22:10:13 +00001032 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1033 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +00001034 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001035
1036 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1037 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1038 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +00001039 bool EmitPotentialTailCall(SDNode *Node);
1040 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +00001041 void Select(SDOperand N);
1042 };
1043}
1044
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001045/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1046/// the main function.
1047static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1048 MachineFrameInfo *MFI) {
1049 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1050 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1051 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1052
1053 // Set the high part to be 64-bit precision.
1054 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1055 CWFrameIdx, 1).addImm(2);
1056
1057 // Reload the modified control word now.
1058 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1059}
1060
Chris Lattner0b17b452005-05-13 07:38:09 +00001061void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1062 // If this function has live-in values, emit the copies from pregs to vregs at
1063 // the top of the function, before anything else.
1064 MachineBasicBlock *BB = MF.begin();
1065 if (MF.livein_begin() != MF.livein_end()) {
1066 SSARegMap *RegMap = MF.getSSARegMap();
1067 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1068 E = MF.livein_end(); LI != E; ++LI) {
1069 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1070 if (RC == X86::R8RegisterClass) {
1071 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1072 } else if (RC == X86::R16RegisterClass) {
1073 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1074 } else if (RC == X86::R32RegisterClass) {
1075 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1076 } else if (RC == X86::RFPRegisterClass) {
1077 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begeman8a093362005-07-06 18:59:04 +00001078 } else if (RC == X86::RXMMRegisterClass) {
1079 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner0b17b452005-05-13 07:38:09 +00001080 } else {
1081 assert(0 && "Unknown regclass!");
1082 }
1083 }
1084 }
1085
1086
1087 // If this is main, emit special code for main.
1088 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1089 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1090}
1091
1092
Chris Lattner6fba62d62005-01-12 04:21:28 +00001093/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1094/// when it has created a SelectionDAG for us to codegen.
1095void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1096 // While we're doing this, keep track of whether we see any FP code for
1097 // FP_REG_KILL insertion.
1098 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001099 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001100
1101 // Scan the PHI nodes that already are inserted into this basic block. If any
1102 // of them is a PHI of a floating point value, we need to insert an
1103 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001104 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +00001105 if (BB != MF->begin())
1106 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1107 I != E; ++I) {
1108 assert(I->getOpcode() == X86::PHI &&
1109 "Isn't just PHI nodes?");
1110 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1111 X86::RFPRegisterClass) {
1112 ContainsFPCode = true;
1113 break;
1114 }
Chris Lattner6fba62d62005-01-12 04:21:28 +00001115 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001116
Chris Lattner6fba62d62005-01-12 04:21:28 +00001117 // Compute the RegPressureMap, which is an approximation for the number of
1118 // registers required to compute each node.
1119 ComputeRegPressure(DAG.getRoot());
1120
Chris Lattnerdd66a412005-05-15 05:46:45 +00001121 TheDAG = &DAG;
1122
Chris Lattner6fba62d62005-01-12 04:21:28 +00001123 // Codegen the basic block.
1124 Select(DAG.getRoot());
1125
Chris Lattnerdd66a412005-05-15 05:46:45 +00001126 TheDAG = 0;
1127
Chris Lattner6fba62d62005-01-12 04:21:28 +00001128 // Finally, look at all of the successors of this block. If any contain a PHI
1129 // node of FP type, we need to insert an FP_REG_KILL in this block.
1130 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1131 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1132 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1133 I != E && I->getOpcode() == X86::PHI; ++I) {
1134 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1135 X86::RFPRegisterClass) {
1136 ContainsFPCode = true;
1137 break;
1138 }
1139 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001140
Chris Lattner6972c312005-05-09 03:36:39 +00001141 // Final check, check LLVM BB's that are successors to the LLVM BB
1142 // corresponding to BB for FP PHI nodes.
1143 const BasicBlock *LLVMBB = BB->getBasicBlock();
1144 const PHINode *PN;
1145 if (!ContainsFPCode)
1146 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1147 SI != E && !ContainsFPCode; ++SI)
1148 for (BasicBlock::const_iterator II = SI->begin();
1149 (PN = dyn_cast<PHINode>(II)); ++II)
1150 if (PN->getType()->isFloatingPoint()) {
1151 ContainsFPCode = true;
1152 break;
1153 }
1154
1155
Chris Lattner6fba62d62005-01-12 04:21:28 +00001156 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1157 // only occurs due to the floating point stackifier not being aggressive
1158 // enough to handle arbitrary global stackification.
1159 //
1160 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1161 // defines a floating point virtual register.
1162 //
1163 // When the global register allocators (like linear scan) finally update live
1164 // variable analysis, we can keep floating point values in registers across
1165 // basic blocks. This will be a huge win, but we are waiting on the global
1166 // allocators before we can do this.
1167 //
Chris Lattner472a2652005-03-30 01:10:00 +00001168 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +00001169 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1170 ++NumFPKill;
1171 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001172
Chris Lattner6fba62d62005-01-12 04:21:28 +00001173 // Clear state used for selection.
1174 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001175 RegPressureMap.clear();
1176}
1177
1178
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001179// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1180// for the number of registers required to compute each node. This is basically
1181// computing a generalized form of the Sethi-Ullman number for each node.
1182unsigned ISel::ComputeRegPressure(SDOperand O) {
1183 SDNode *N = O.Val;
1184 unsigned &Result = RegPressureMap[N];
1185 if (Result) return Result;
1186
Chris Lattner8fea42b2005-01-11 03:37:59 +00001187 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1188 // higher fixed cost??
1189
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001190 if (N->getNumOperands() == 0) {
1191 Result = 1;
1192 } else {
1193 unsigned MaxRegUse = 0;
1194 unsigned NumExtraMaxRegUsers = 0;
1195 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1196 unsigned Regs;
1197 if (N->getOperand(i).getOpcode() == ISD::Constant)
1198 Regs = 0;
1199 else
1200 Regs = ComputeRegPressure(N->getOperand(i));
1201 if (Regs > MaxRegUse) {
1202 MaxRegUse = Regs;
1203 NumExtraMaxRegUsers = 0;
1204 } else if (Regs == MaxRegUse &&
1205 N->getOperand(i).getValueType() != MVT::Other) {
1206 ++NumExtraMaxRegUsers;
1207 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001208 }
Chris Lattnerca318ed2005-01-17 22:56:09 +00001209
1210 if (O.getOpcode() != ISD::TokenFactor)
1211 Result = MaxRegUse+NumExtraMaxRegUsers;
1212 else
Chris Lattnera5d137f2005-01-17 23:02:13 +00001213 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001214 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001215
Chris Lattner75bac9f2005-01-11 23:21:30 +00001216 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001217 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001218}
1219
Chris Lattner5b04f332005-01-20 16:50:16 +00001220/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1221/// The DAG cannot have cycles in it, by definition, so the visited set is not
1222/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1223/// reuse, so it prevents exponential cases.
1224///
1225static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1226 std::set<SDNode*> &Visited) {
1227 if (N == Op) return true; // Found it.
1228 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +00001229 if (Node->getNumOperands() == 0 || // Leaf?
1230 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +00001231 if (!Visited.insert(Node).second) return false; // Already visited?
1232
1233 // Recurse for the first N-1 operands.
1234 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1235 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1236 return true;
1237
1238 // Tail recurse for the last operand.
1239 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1240}
1241
Chris Lattnera7acdda2005-01-18 01:06:26 +00001242X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1243 X86AddressMode Result;
1244
1245 // If we need to emit two register operands, emit the one with the highest
1246 // register pressure first.
1247 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1248 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001249 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001250 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001251 std::set<SDNode*> Visited;
1252 EmitBaseThenIndex = true;
1253 // If Base ends up pointing to Index, we must emit index first. This is
1254 // because of the way we fold loads, we may end up doing bad things with
1255 // the folded add.
1256 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1257 EmitBaseThenIndex = false;
1258 } else {
1259 std::set<SDNode*> Visited;
1260 EmitBaseThenIndex = false;
1261 // If Base ends up pointing to Index, we must emit index first. This is
1262 // because of the way we fold loads, we may end up doing bad things with
1263 // the folded add.
1264 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1265 EmitBaseThenIndex = true;
1266 }
1267
1268 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001269 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1270 Result.IndexReg = SelectExpr(IAM.IndexReg);
1271 } else {
1272 Result.IndexReg = SelectExpr(IAM.IndexReg);
1273 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1274 }
Chris Lattner5b04f332005-01-20 16:50:16 +00001275
Chris Lattnera7acdda2005-01-18 01:06:26 +00001276 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1277 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1278 } else if (IAM.IndexReg.Val) {
1279 Result.IndexReg = SelectExpr(IAM.IndexReg);
1280 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001281
Chris Lattnera7acdda2005-01-18 01:06:26 +00001282 switch (IAM.BaseType) {
1283 case X86ISelAddressMode::RegBase:
1284 Result.BaseType = X86AddressMode::RegBase;
1285 break;
1286 case X86ISelAddressMode::FrameIndexBase:
1287 Result.BaseType = X86AddressMode::FrameIndexBase;
1288 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1289 break;
1290 default:
1291 assert(0 && "Unknown base type!");
1292 break;
1293 }
1294 Result.Scale = IAM.Scale;
1295 Result.Disp = IAM.Disp;
1296 Result.GV = IAM.GV;
1297 return Result;
1298}
1299
1300/// SelectAddress - Pattern match the maximal addressing mode for this node and
1301/// emit all of the leaf registers.
1302void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1303 X86ISelAddressMode IAM;
1304 MatchAddress(N, IAM);
1305 AM = SelectAddrExprs(IAM);
1306}
1307
1308/// MatchAddress - Add the specified node to the specified addressing mode,
1309/// returning true if it cannot be done. This just pattern matches for the
1310/// addressing mode, it does not cause any code to be emitted. For that, use
1311/// SelectAddress.
1312bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001313 switch (N.getOpcode()) {
1314 default: break;
1315 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +00001316 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1317 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +00001318 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1319 return false;
1320 }
1321 break;
1322 case ISD::GlobalAddress:
1323 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +00001324 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1325 // For Darwin, external and weak symbols are indirect, so we want to load
1326 // the value at address GV, not the value of GV itself. This means that
1327 // the GlobalAddress must be in the base or index register of the address,
1328 // not the GV offset field.
1329 if (Subtarget->getIndirectExternAndWeakGlobals() &&
1330 (GV->hasWeakLinkage() || GV->isExternal())) {
1331 break;
1332 } else {
1333 AM.GV = GV;
1334 return false;
1335 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001336 }
1337 break;
1338 case ISD::Constant:
1339 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1340 return false;
1341 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001342 // We might have folded the load into this shift, so don't regen the value
1343 // if so.
1344 if (ExprMap.count(N)) break;
1345
Chris Lattnera7acdda2005-01-18 01:06:26 +00001346 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +00001347 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1348 unsigned Val = CN->getValue();
1349 if (Val == 1 || Val == 2 || Val == 3) {
1350 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001351 SDOperand ShVal = N.Val->getOperand(0);
1352
1353 // Okay, we know that we have a scale by now. However, if the scaled
1354 // value is an add of something and a constant, we can fold the
1355 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001356 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001357 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001358 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001359 ConstantSDNode *AddVal =
1360 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1361 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +00001362 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001363 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001364 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001365 return false;
1366 }
1367 }
1368 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001369 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001370 // We might have folded the load into this mul, so don't regen the value if
1371 // so.
1372 if (ExprMap.count(N)) break;
1373
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001374 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +00001375 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1376 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001377 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1378 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1379 AM.Scale = unsigned(CN->getValue())-1;
1380
1381 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001382 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001383
1384 // Okay, we know that we have a scale by now. However, if the scaled
1385 // value is an add of something and a constant, we can fold the
1386 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001387 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001388 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001389 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001390 ConstantSDNode *AddVal =
1391 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1392 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00001393 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001394 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001395 }
1396
1397 AM.IndexReg = AM.Base.Reg = Reg;
1398 return false;
1399 }
1400 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001401
1402 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +00001403 // We might have folded the load into this mul, so don't regen the value if
1404 // so.
1405 if (ExprMap.count(N)) break;
1406
Chris Lattnera7acdda2005-01-18 01:06:26 +00001407 X86ISelAddressMode Backup = AM;
1408 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1409 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +00001410 return false;
1411 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001412 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1413 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +00001414 return false;
1415 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +00001416 break;
1417 }
1418 }
1419
Chris Lattner378262d2005-01-11 04:40:19 +00001420 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +00001421 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +00001422 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001423 if (AM.IndexReg.Val == 0) {
1424 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +00001425 AM.Scale = 1;
1426 return false;
1427 }
1428
1429 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +00001430 return true;
Chris Lattner378262d2005-01-11 04:40:19 +00001431 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001432
1433 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001434 AM.BaseType = X86ISelAddressMode::RegBase;
1435 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +00001436 return false;
1437}
1438
1439/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1440/// assuming that the temporary registers are in the 8-bit register class.
1441///
1442/// Tmp1 = setcc1
1443/// Tmp2 = setcc2
1444/// DestReg = logicalop Tmp1, Tmp2
1445///
1446static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1447 unsigned SetCC2, unsigned LogicalOp,
1448 unsigned DestReg) {
1449 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1450 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1451 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1452 BuildMI(BB, SetCC1, 0, Tmp1);
1453 BuildMI(BB, SetCC2, 0, Tmp2);
1454 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1455}
1456
1457/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1458/// condition codes match the specified SetCCOpcode. Note that some conditions
1459/// require multiple instructions to generate the correct value.
1460static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1461 ISD::CondCode SetCCOpcode, bool isFP) {
1462 unsigned Opc;
1463 if (!isFP) {
1464 switch (SetCCOpcode) {
1465 default: assert(0 && "Illegal integer SetCC!");
1466 case ISD::SETEQ: Opc = X86::SETEr; break;
1467 case ISD::SETGT: Opc = X86::SETGr; break;
1468 case ISD::SETGE: Opc = X86::SETGEr; break;
1469 case ISD::SETLT: Opc = X86::SETLr; break;
1470 case ISD::SETLE: Opc = X86::SETLEr; break;
1471 case ISD::SETNE: Opc = X86::SETNEr; break;
1472 case ISD::SETULT: Opc = X86::SETBr; break;
1473 case ISD::SETUGT: Opc = X86::SETAr; break;
1474 case ISD::SETULE: Opc = X86::SETBEr; break;
1475 case ISD::SETUGE: Opc = X86::SETAEr; break;
1476 }
1477 } else {
1478 // On a floating point condition, the flags are set as follows:
1479 // ZF PF CF op
1480 // 0 | 0 | 0 | X > Y
1481 // 0 | 0 | 1 | X < Y
1482 // 1 | 0 | 0 | X == Y
1483 // 1 | 1 | 1 | unordered
1484 //
1485 switch (SetCCOpcode) {
1486 default: assert(0 && "Invalid FP setcc!");
1487 case ISD::SETUEQ:
1488 case ISD::SETEQ:
1489 Opc = X86::SETEr; // True if ZF = 1
1490 break;
1491 case ISD::SETOGT:
1492 case ISD::SETGT:
1493 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1494 break;
1495 case ISD::SETOGE:
1496 case ISD::SETGE:
1497 Opc = X86::SETAEr; // True if CF = 0
1498 break;
1499 case ISD::SETULT:
1500 case ISD::SETLT:
1501 Opc = X86::SETBr; // True if CF = 1
1502 break;
1503 case ISD::SETULE:
1504 case ISD::SETLE:
1505 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1506 break;
1507 case ISD::SETONE:
1508 case ISD::SETNE:
1509 Opc = X86::SETNEr; // True if ZF = 0
1510 break;
1511 case ISD::SETUO:
1512 Opc = X86::SETPr; // True if PF = 1
1513 break;
1514 case ISD::SETO:
1515 Opc = X86::SETNPr; // True if PF = 0
1516 break;
1517 case ISD::SETOEQ: // !PF & ZF
1518 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1519 return;
1520 case ISD::SETOLT: // !PF & CF
1521 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1522 return;
1523 case ISD::SETOLE: // !PF & (CF || ZF)
1524 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1525 return;
1526 case ISD::SETUGT: // PF | (!ZF & !CF)
1527 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1528 return;
1529 case ISD::SETUGE: // PF | !CF
1530 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1531 return;
1532 case ISD::SETUNE: // PF | !ZF
1533 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1534 return;
1535 }
1536 }
1537 BuildMI(BB, Opc, 0, DestReg);
1538}
1539
1540
1541/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1542/// the Dest block if the Cond condition is true. If we cannot fold this
1543/// condition into the branch, return true.
1544///
Chris Lattner37ed2852005-01-11 04:06:27 +00001545bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1546 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001547 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1548 // B) using two conditional branches instead of one condbr, two setcc's, and
1549 // an or.
1550 if ((Cond.getOpcode() == ISD::OR ||
1551 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1552 // And and or set the flags for us, so there is no need to emit a TST of the
1553 // result. It is only safe to do this if there is only a single use of the
1554 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +00001555 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +00001556 SelectExpr(Cond);
1557 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1558 return false;
1559 }
1560
1561 // Codegen br not C -> JE.
1562 if (Cond.getOpcode() == ISD::XOR)
1563 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1564 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +00001565 unsigned CondR;
1566 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1567 Select(Chain);
1568 CondR = SelectExpr(Cond.Val->getOperand(0));
1569 } else {
1570 CondR = SelectExpr(Cond.Val->getOperand(0));
1571 Select(Chain);
1572 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001573 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1574 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1575 return false;
1576 }
1577
1578 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1579 if (SetCC == 0)
1580 return true; // Can only handle simple setcc's so far.
1581
1582 unsigned Opc;
1583
1584 // Handle integer conditions first.
1585 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1586 switch (SetCC->getCondition()) {
1587 default: assert(0 && "Illegal integer SetCC!");
1588 case ISD::SETEQ: Opc = X86::JE; break;
1589 case ISD::SETGT: Opc = X86::JG; break;
1590 case ISD::SETGE: Opc = X86::JGE; break;
1591 case ISD::SETLT: Opc = X86::JL; break;
1592 case ISD::SETLE: Opc = X86::JLE; break;
1593 case ISD::SETNE: Opc = X86::JNE; break;
1594 case ISD::SETULT: Opc = X86::JB; break;
1595 case ISD::SETUGT: Opc = X86::JA; break;
1596 case ISD::SETULE: Opc = X86::JBE; break;
1597 case ISD::SETUGE: Opc = X86::JAE; break;
1598 }
Chris Lattner37ed2852005-01-11 04:06:27 +00001599 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001600 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001601 BuildMI(BB, Opc, 1).addMBB(Dest);
1602 return false;
1603 }
1604
Chris Lattner88c8a232005-01-07 07:49:41 +00001605 unsigned Opc2 = 0; // Second branch if needed.
1606
1607 // On a floating point condition, the flags are set as follows:
1608 // ZF PF CF op
1609 // 0 | 0 | 0 | X > Y
1610 // 0 | 0 | 1 | X < Y
1611 // 1 | 0 | 0 | X == Y
1612 // 1 | 1 | 1 | unordered
1613 //
1614 switch (SetCC->getCondition()) {
1615 default: assert(0 && "Invalid FP setcc!");
1616 case ISD::SETUEQ:
1617 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1618 case ISD::SETOGT:
1619 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1620 case ISD::SETOGE:
1621 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1622 case ISD::SETULT:
1623 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1624 case ISD::SETULE:
1625 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1626 case ISD::SETONE:
1627 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1628 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1629 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1630 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1631 Opc = X86::JA; // ZF = 0 & CF = 0
1632 Opc2 = X86::JP; // PF = 1
1633 break;
1634 case ISD::SETUGE: // PF = 1 | CF = 0
1635 Opc = X86::JAE; // CF = 0
1636 Opc2 = X86::JP; // PF = 1
1637 break;
1638 case ISD::SETUNE: // PF = 1 | ZF = 0
1639 Opc = X86::JNE; // ZF = 0
1640 Opc2 = X86::JP; // PF = 1
1641 break;
1642 case ISD::SETOEQ: // PF = 0 & ZF = 1
1643 //X86::JNP, X86::JE
1644 //X86::AND8rr
1645 return true; // FIXME: Emit more efficient code for this branch.
1646 case ISD::SETOLT: // PF = 0 & CF = 1
1647 //X86::JNP, X86::JB
1648 //X86::AND8rr
1649 return true; // FIXME: Emit more efficient code for this branch.
1650 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1651 //X86::JNP, X86::JBE
1652 //X86::AND8rr
1653 return true; // FIXME: Emit more efficient code for this branch.
1654 }
1655
Chris Lattner37ed2852005-01-11 04:06:27 +00001656 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001657 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001658 BuildMI(BB, Opc, 1).addMBB(Dest);
1659 if (Opc2)
1660 BuildMI(BB, Opc2, 1).addMBB(Dest);
1661 return false;
1662}
1663
Chris Lattner1d13a922005-01-10 22:10:13 +00001664/// EmitSelectCC - Emit code into BB that performs a select operation between
1665/// the two registers RTrue and RFalse, generating a result into RDest. Return
1666/// true if the fold cannot be performed.
1667///
1668void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1669 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1670 enum Condition {
1671 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1672 NOT_SET
1673 } CondCode = NOT_SET;
1674
1675 static const unsigned CMOVTAB16[] = {
1676 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1677 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001678 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001679 };
1680 static const unsigned CMOVTAB32[] = {
1681 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1682 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001683 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001684 };
1685 static const unsigned CMOVTABFP[] = {
1686 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1687 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1688 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1689 };
Nate Begeman8a093362005-07-06 18:59:04 +00001690 static const unsigned SSE_CMOVTAB[] = {
1691 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
1692 2 /* CMPLESS */, 1 /* CMPLTSS */, /*missing*/0, /*missing*/0,
1693 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1694 };
Chris Lattner1d13a922005-01-10 22:10:13 +00001695
1696 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1697 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1698 switch (SetCC->getCondition()) {
1699 default: assert(0 && "Unknown integer comparison!");
1700 case ISD::SETEQ: CondCode = EQ; break;
1701 case ISD::SETGT: CondCode = GT; break;
1702 case ISD::SETGE: CondCode = GE; break;
1703 case ISD::SETLT: CondCode = LT; break;
1704 case ISD::SETLE: CondCode = LE; break;
1705 case ISD::SETNE: CondCode = NE; break;
1706 case ISD::SETULT: CondCode = B; break;
1707 case ISD::SETUGT: CondCode = A; break;
1708 case ISD::SETULE: CondCode = BE; break;
1709 case ISD::SETUGE: CondCode = AE; break;
1710 }
Nate Begeman8a093362005-07-06 18:59:04 +00001711 } else if (X86ScalarSSE) {
1712 switch (SetCC->getCondition()) {
1713 default: assert(0 && "Unknown scalar fp comparison!");
1714 case ISD::SETEQ: CondCode = EQ; break;
1715 case ISD::SETNE: CondCode = NE; break;
1716 case ISD::SETULT:
1717 case ISD::SETLT: CondCode = LT; break;
1718 case ISD::SETULE:
1719 case ISD::SETLE: CondCode = LE; break;
1720 case ISD::SETUGT:
1721 case ISD::SETGT: CondCode = GT; break;
1722 case ISD::SETUGE:
1723 case ISD::SETGE: CondCode = GE; break;
1724 }
Chris Lattner1d13a922005-01-10 22:10:13 +00001725 } else {
1726 // On a floating point condition, the flags are set as follows:
1727 // ZF PF CF op
1728 // 0 | 0 | 0 | X > Y
1729 // 0 | 0 | 1 | X < Y
1730 // 1 | 0 | 0 | X == Y
1731 // 1 | 1 | 1 | unordered
1732 //
1733 switch (SetCC->getCondition()) {
1734 default: assert(0 && "Unknown FP comparison!");
1735 case ISD::SETUEQ:
1736 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1737 case ISD::SETOGT:
1738 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1739 case ISD::SETOGE:
1740 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1741 case ISD::SETULT:
1742 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1743 case ISD::SETULE:
1744 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1745 case ISD::SETONE:
1746 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1747 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1748 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1749 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1750 case ISD::SETUGE: // PF = 1 | CF = 0
1751 case ISD::SETUNE: // PF = 1 | ZF = 0
1752 case ISD::SETOEQ: // PF = 0 & ZF = 1
1753 case ISD::SETOLT: // PF = 0 & CF = 1
1754 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1755 // We cannot emit this comparison as a single cmov.
1756 break;
1757 }
1758 }
1759 }
1760
Nate Begeman8a093362005-07-06 18:59:04 +00001761 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1762 // Others we will have to do the PowerPC thing and generate an MBB for the
1763 // true and false values and select between them with a PHI.
1764 if (X86ScalarSSE) {
1765 if (CondCode != NOT_SET) {
1766 unsigned CMPSOpc = (SVT == MVT::f64) ? X86::CMPSDrr : X86::CMPSSrr;
1767 unsigned CMPSImm = SSE_CMOVTAB[CondCode];
1768 // FIXME check for min
1769 // FIXME check for max
1770 // FIXME check for reverse
1771 unsigned LHS = SelectExpr(Cond.getOperand(0));
1772 unsigned RHS = SelectExpr(Cond.getOperand(1));
1773 // emit compare mask
1774 unsigned MaskReg = MakeReg(SVT);
1775 BuildMI(BB, CMPSOpc, 3, MaskReg).addReg(LHS).addReg(RHS).addImm(CMPSImm);
1776 // emit and with mask
1777 unsigned TrueMask = MakeReg(SVT);
1778 unsigned AndOpc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
1779 BuildMI(BB, AndOpc, 2, TrueMask).addReg(RTrue).addReg(MaskReg);
1780 // emit and with inverse mask
1781 unsigned FalseMask = MakeReg(SVT);
1782 unsigned AndnOpc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
1783 BuildMI(BB, AndnOpc, 2, FalseMask).addReg(RFalse).addReg(MaskReg);
1784 // emit or into dest reg
1785 unsigned OROpc = (SVT == MVT::f32) ? X86::ORPSrr : X86::ORPDrr;
1786 BuildMI(BB, OROpc, 2, RDest).addReg(TrueMask).addReg(FalseMask);
1787 return;
1788 } else {
1789 // do the test and branch thing
1790 // Get the condition into the zero flag.
1791 unsigned CondReg = SelectExpr(Cond);
1792 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1793
1794 // Create an iterator with which to insert the MBB for copying the false
1795 // value and the MBB to hold the PHI instruction for this SetCC.
1796 MachineBasicBlock *thisMBB = BB;
1797 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1798 ilist<MachineBasicBlock>::iterator It = BB;
1799 ++It;
1800
1801 // thisMBB:
1802 // ...
1803 // TrueVal = ...
1804 // cmpTY ccX, r1, r2
1805 // bCC sinkMBB
1806 // fallthrough --> copy0MBB
1807 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1808 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1809 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1810 MachineFunction *F = BB->getParent();
1811 F->getBasicBlockList().insert(It, copy0MBB);
1812 F->getBasicBlockList().insert(It, sinkMBB);
1813 // Update machine-CFG edges
1814 BB->addSuccessor(copy0MBB);
1815 BB->addSuccessor(sinkMBB);
1816
1817 // copy0MBB:
1818 // %FalseValue = ...
1819 // # fallthrough to sinkMBB
1820 BB = copy0MBB;
1821 // Update machine-CFG edges
1822 BB->addSuccessor(sinkMBB);
1823
1824 // sinkMBB:
1825 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1826 // ...
1827 BB = sinkMBB;
1828 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1829 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1830 }
1831 return;
1832 }
1833
Chris Lattner1d13a922005-01-10 22:10:13 +00001834 unsigned Opc = 0;
1835 if (CondCode != NOT_SET) {
1836 switch (SVT) {
1837 default: assert(0 && "Cannot select this type!");
1838 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1839 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001840 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001841 }
1842 }
Nate Begeman8a093362005-07-06 18:59:04 +00001843
Chris Lattner1d13a922005-01-10 22:10:13 +00001844 // Finally, if we weren't able to fold this, just emit the condition and test
1845 // it.
1846 if (CondCode == NOT_SET || Opc == 0) {
1847 // Get the condition into the zero flag.
1848 unsigned CondReg = SelectExpr(Cond);
1849 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1850
1851 switch (SVT) {
1852 default: assert(0 && "Cannot select this type!");
1853 case MVT::i16: Opc = X86::CMOVE16rr; break;
1854 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001855 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001856 }
1857 } else {
1858 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +00001859 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +00001860 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +00001861 }
1862 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1863}
1864
Chris Lattner3be6cd52005-01-17 01:34:14 +00001865void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001866 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +00001867 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1868 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001869 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001870 switch (RHS.getValueType()) {
1871 default: break;
1872 case MVT::i1:
1873 case MVT::i8: Opc = X86::CMP8mi; break;
1874 case MVT::i16: Opc = X86::CMP16mi; break;
1875 case MVT::i32: Opc = X86::CMP32mi; break;
1876 }
1877 if (Opc) {
1878 X86AddressMode AM;
1879 EmitFoldedLoad(LHS, AM);
1880 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1881 return;
1882 }
1883 }
1884
Chris Lattner88c8a232005-01-07 07:49:41 +00001885 switch (RHS.getValueType()) {
1886 default: break;
1887 case MVT::i1:
1888 case MVT::i8: Opc = X86::CMP8ri; break;
1889 case MVT::i16: Opc = X86::CMP16ri; break;
1890 case MVT::i32: Opc = X86::CMP32ri; break;
1891 }
1892 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001893 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00001894 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1895 return;
1896 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001897 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +00001898 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1899 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +00001900 unsigned Reg = SelectExpr(LHS);
1901 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1902 BuildMI(BB, X86::FNSTSW8r, 0);
1903 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001904 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001905 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001906 }
1907
Chris Lattner2cfce682005-01-12 02:02:48 +00001908 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001909 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001910 switch (RHS.getValueType()) {
1911 default: break;
1912 case MVT::i1:
1913 case MVT::i8: Opc = X86::CMP8mr; break;
1914 case MVT::i16: Opc = X86::CMP16mr; break;
1915 case MVT::i32: Opc = X86::CMP32mr; break;
1916 }
1917 if (Opc) {
1918 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001919 EmitFoldedLoad(LHS, AM);
1920 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001921 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1922 return;
1923 }
1924 }
1925
Chris Lattner88c8a232005-01-07 07:49:41 +00001926 switch (LHS.getValueType()) {
1927 default: assert(0 && "Cannot compare this value!");
1928 case MVT::i1:
1929 case MVT::i8: Opc = X86::CMP8rr; break;
1930 case MVT::i16: Opc = X86::CMP16rr; break;
1931 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001932 case MVT::f32: Opc = X86::UCOMISSrr; break;
1933 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001934 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001935 unsigned Tmp1, Tmp2;
1936 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1937 Tmp1 = SelectExpr(LHS);
1938 Tmp2 = SelectExpr(RHS);
1939 } else {
1940 Tmp2 = SelectExpr(RHS);
1941 Tmp1 = SelectExpr(LHS);
1942 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001943 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1944}
1945
Chris Lattner62b22422005-01-11 21:19:59 +00001946/// isFoldableLoad - Return true if this is a load instruction that can safely
1947/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001948bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1949 if (Op.getOpcode() == ISD::LOAD) {
1950 // FIXME: currently can't fold constant pool indexes.
1951 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1952 return false;
1953 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00001954 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001955 // FIXME: currently can't fold constant pool indexes.
1956 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1957 return false;
1958 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001959 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001960 }
Chris Lattner62b22422005-01-11 21:19:59 +00001961
1962 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001963 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1964 if (ExprMap.count(Op.getValue(1))) return false;
1965 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001966 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001967
Chris Lattnera56d29d2005-01-17 06:26:58 +00001968 // If there is not just one use of its value, we cannot fold.
1969 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1970
1971 // Finally, we cannot fold the load into the operation if this would induce a
1972 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1973 // operand of the operation we are folding the load into) can possible use the
1974 // chain node defined by the load.
1975 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1976 std::set<SDNode*> Visited;
1977 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1978 return false;
1979 }
1980 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001981}
1982
Chris Lattnera56d29d2005-01-17 06:26:58 +00001983
Chris Lattner62b22422005-01-11 21:19:59 +00001984/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1985/// and compute the address being loaded into AM.
1986void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1987 SDOperand Chain = Op.getOperand(0);
1988 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001989
Chris Lattner62b22422005-01-11 21:19:59 +00001990 if (getRegPressure(Chain) > getRegPressure(Address)) {
1991 Select(Chain);
1992 SelectAddress(Address, AM);
1993 } else {
1994 SelectAddress(Address, AM);
1995 Select(Chain);
1996 }
1997
1998 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001999 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2000 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00002001 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00002002 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00002003}
2004
Chris Lattner29f58192005-01-19 07:37:26 +00002005// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2006// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2007// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2008// return true.
2009bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002010 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2011 // good!
2012 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2013 std::swap(Op1, Op2); // Op1 is the SHL now.
2014 } else {
2015 return false; // No match
2016 }
2017
2018 SDOperand ShlVal = Op1.getOperand(0);
2019 SDOperand ShlAmt = Op1.getOperand(1);
2020 SDOperand ShrVal = Op2.getOperand(0);
2021 SDOperand ShrAmt = Op2.getOperand(1);
2022
Chris Lattner29f58192005-01-19 07:37:26 +00002023 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2024
Chris Lattner41fe2012005-01-19 06:18:43 +00002025 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2026 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2027 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00002028 if (SubCST->getValue() == RegSize) {
2029 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00002030 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00002031 if (ShrVal == ShlVal) {
2032 unsigned Reg, ShAmt;
2033 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2034 Reg = SelectExpr(ShrVal);
2035 ShAmt = SelectExpr(ShrAmt);
2036 } else {
2037 ShAmt = SelectExpr(ShrAmt);
2038 Reg = SelectExpr(ShrVal);
2039 }
2040 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2041 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2042 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2043 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2044 return true;
2045 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002046 unsigned AReg, BReg;
2047 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002048 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002049 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002050 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00002051 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002052 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002053 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00002054 unsigned ShAmt = SelectExpr(ShrAmt);
2055 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2056 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2057 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00002058 return true;
2059 }
2060 }
2061
Chris Lattnerde87d1462005-01-19 08:07:05 +00002062 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2063 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2064 if (SubCST->getValue() == RegSize) {
2065 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2066 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2067 if (ShrVal == ShlVal) {
2068 unsigned Reg, ShAmt;
2069 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2070 Reg = SelectExpr(ShrVal);
2071 ShAmt = SelectExpr(ShlAmt);
2072 } else {
2073 ShAmt = SelectExpr(ShlAmt);
2074 Reg = SelectExpr(ShrVal);
2075 }
2076 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2077 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2078 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2079 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2080 return true;
2081 } else if (RegSize != 8) {
2082 unsigned AReg, BReg;
2083 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00002084 AReg = SelectExpr(ShlVal);
2085 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002086 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00002087 BReg = SelectExpr(ShrVal);
2088 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002089 }
2090 unsigned ShAmt = SelectExpr(ShlAmt);
2091 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2092 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2093 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2094 return true;
2095 }
2096 }
Chris Lattner41fe2012005-01-19 06:18:43 +00002097
Chris Lattnerde87d1462005-01-19 08:07:05 +00002098 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2099 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2100 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2101 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2102 // (A >> 5) | (A << 27) --> ROR A, 5
2103 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2104 if (ShrVal == ShlVal) {
2105 unsigned Reg = SelectExpr(ShrVal);
2106 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2107 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2108 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2109 return true;
2110 } else if (RegSize != 8) {
2111 unsigned AReg, BReg;
2112 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002113 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002114 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002115 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002116 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002117 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002118 }
2119 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2120 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2121 .addImm(ShrCst->getValue());
2122 return true;
2123 }
2124 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002125
Chris Lattner41fe2012005-01-19 06:18:43 +00002126 return false;
2127}
2128
Chris Lattner88c8a232005-01-07 07:49:41 +00002129unsigned ISel::SelectExpr(SDOperand N) {
2130 unsigned Result;
2131 unsigned Tmp1, Tmp2, Tmp3;
2132 unsigned Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002133 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00002134 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002135
Chris Lattner720a62e2005-01-14 22:37:41 +00002136 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002137 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2138 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2139 // Just use the specified register as our input.
2140 return cast<RegSDNode>(Node)->getReg();
2141 }
Chris Lattner720a62e2005-01-14 22:37:41 +00002142 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002143
Chris Lattner62b22422005-01-11 21:19:59 +00002144 unsigned &Reg = ExprMap[N];
2145 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002146
Chris Lattnera31d4c72005-04-02 04:01:14 +00002147 switch (N.getOpcode()) {
2148 default:
Chris Lattner62b22422005-01-11 21:19:59 +00002149 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00002150 MakeReg(N.getValueType()) : 1;
2151 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00002152 case X86ISD::TAILCALL:
2153 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00002154 // If this is a call instruction, make sure to prepare ALL of the result
2155 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002156 ExprMap[N.getValue(0)] = 1;
2157 if (Node->getNumValues() > 1) {
2158 Result = MakeReg(Node->getValueType(1));
2159 ExprMap[N.getValue(1)] = Result;
2160 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00002161 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00002162 } else {
2163 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00002164 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00002165 break;
2166 case ISD::ADD_PARTS:
2167 case ISD::SUB_PARTS:
2168 case ISD::SHL_PARTS:
2169 case ISD::SRL_PARTS:
2170 case ISD::SRA_PARTS:
2171 Result = MakeReg(Node->getValueType(0));
2172 ExprMap[N.getValue(0)] = Result;
2173 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2174 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2175 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002176 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002177
Chris Lattner88c8a232005-01-07 07:49:41 +00002178 switch (N.getOpcode()) {
2179 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00002180 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00002181 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00002182 case ISD::FP_EXTEND:
2183 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
2184 Tmp1 = SelectExpr(N.getOperand(0));
2185 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2186 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002187 case ISD::CopyFromReg:
2188 Select(N.getOperand(0));
2189 if (Result == 1) {
2190 Reg = Result = ExprMap[N.getValue(0)] =
2191 MakeReg(N.getValue(0).getValueType());
2192 }
2193 switch (Node->getValueType(0)) {
2194 default: assert(0 && "Cannot CopyFromReg this!");
2195 case MVT::i1:
2196 case MVT::i8:
2197 BuildMI(BB, X86::MOV8rr, 1,
2198 Result).addReg(cast<RegSDNode>(Node)->getReg());
2199 return Result;
2200 case MVT::i16:
2201 BuildMI(BB, X86::MOV16rr, 1,
2202 Result).addReg(cast<RegSDNode>(Node)->getReg());
2203 return Result;
2204 case MVT::i32:
2205 BuildMI(BB, X86::MOV32rr, 1,
2206 Result).addReg(cast<RegSDNode>(Node)->getReg());
2207 return Result;
2208 }
2209
Chris Lattner88c8a232005-01-07 07:49:41 +00002210 case ISD::FrameIndex:
2211 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2212 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2213 return Result;
2214 case ISD::ConstantPool:
2215 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2216 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2217 return Result;
2218 case ISD::ConstantFP:
2219 ContainsFPCode = true;
2220 Tmp1 = Result; // Intermediate Register
2221 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2222 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2223 Tmp1 = MakeReg(MVT::f64);
2224
2225 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2226 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2227 BuildMI(BB, X86::FLD0, 0, Tmp1);
2228 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2229 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2230 BuildMI(BB, X86::FLD1, 0, Tmp1);
2231 else
2232 assert(0 && "Unexpected constant!");
2233 if (Tmp1 != Result)
2234 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2235 return Result;
2236 case ISD::Constant:
2237 switch (N.getValueType()) {
2238 default: assert(0 && "Cannot use constants of this type!");
2239 case MVT::i1:
2240 case MVT::i8: Opc = X86::MOV8ri; break;
2241 case MVT::i16: Opc = X86::MOV16ri; break;
2242 case MVT::i32: Opc = X86::MOV32ri; break;
2243 }
2244 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2245 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00002246 case ISD::UNDEF:
2247 if (Node->getValueType(0) == MVT::f64) {
2248 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2249 BuildMI(BB, X86::FLD0, 0, Result);
2250 } else {
2251 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2252 }
2253 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002254 case ISD::GlobalAddress: {
2255 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00002256 // For Darwin, external and weak symbols are indirect, so we want to load
2257 // the value at address GV, not the value of GV itself.
2258 if (Subtarget->getIndirectExternAndWeakGlobals() &&
2259 (GV->hasWeakLinkage() || GV->isExternal())) {
2260 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2261 .addGlobalAddress(GV, false, 0);
2262 } else {
2263 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2264 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002265 return Result;
2266 }
2267 case ISD::ExternalSymbol: {
2268 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2269 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2270 return Result;
2271 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002272 case ISD::ZERO_EXTEND: {
2273 int DestIs16 = N.getValueType() == MVT::i16;
2274 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00002275
2276 // FIXME: This hack is here for zero extension casts from bool to i8. This
2277 // would not be needed if bools were promoted by Legalize.
2278 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002279 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00002280 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2281 return Result;
2282 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002283
Chris Lattnera56d29d2005-01-17 06:26:58 +00002284 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002285 static const unsigned Opc[3] = {
2286 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2287 };
2288
2289 X86AddressMode AM;
2290 EmitFoldedLoad(N.getOperand(0), AM);
2291 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002292
Chris Lattnerb0eef822005-01-11 23:33:00 +00002293 return Result;
2294 }
2295
Chris Lattner88c8a232005-01-07 07:49:41 +00002296 static const unsigned Opc[3] = {
2297 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2298 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00002299 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002300 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2301 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002302 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002303 case ISD::SIGN_EXTEND: {
2304 int DestIs16 = N.getValueType() == MVT::i16;
2305 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2306
Chris Lattner282781c2005-01-09 18:52:44 +00002307 // FIXME: Legalize should promote bools to i8!
2308 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2309 "Sign extend from bool not implemented!");
2310
Chris Lattnera56d29d2005-01-17 06:26:58 +00002311 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002312 static const unsigned Opc[3] = {
2313 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2314 };
2315
2316 X86AddressMode AM;
2317 EmitFoldedLoad(N.getOperand(0), AM);
2318 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2319 return Result;
2320 }
2321
Chris Lattner88c8a232005-01-07 07:49:41 +00002322 static const unsigned Opc[3] = {
2323 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2324 };
2325 Tmp1 = SelectExpr(N.getOperand(0));
2326 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2327 return Result;
2328 }
2329 case ISD::TRUNCATE:
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00002330 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner14947c32005-01-18 20:05:56 +00002331 // FIXME: This should be performed by the DAGCombiner.
Chris Lattnera56d29d2005-01-17 06:26:58 +00002332 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00002333 switch (N.getValueType()) {
2334 default: assert(0 && "Unknown truncate!");
2335 case MVT::i1:
2336 case MVT::i8: Opc = X86::MOV8rm; break;
2337 case MVT::i16: Opc = X86::MOV16rm; break;
2338 }
2339 X86AddressMode AM;
2340 EmitFoldedLoad(N.getOperand(0), AM);
2341 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2342 return Result;
2343 }
2344
Chris Lattner88c8a232005-01-07 07:49:41 +00002345 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2346 // a move out of AX or AL.
2347 switch (N.getOperand(0).getValueType()) {
2348 default: assert(0 && "Unknown truncate!");
2349 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2350 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2351 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2352 }
2353 Tmp1 = SelectExpr(N.getOperand(0));
2354 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2355
2356 switch (N.getValueType()) {
2357 default: assert(0 && "Unknown truncate!");
2358 case MVT::i1:
2359 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2360 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2361 }
2362 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2363 return Result;
2364
Chris Lattner282781c2005-01-09 18:52:44 +00002365 case ISD::SINT_TO_FP:
2366 case ISD::UINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00002367 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2368 unsigned PromoteOpcode = 0;
2369
2370 // We can handle any sint to fp, and 8 and 16 uint to fp with the direct
2371 // sse conversion instructions.
2372 if (X86ScalarSSE) {
2373 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
2374 MVT::ValueType DstTy = N.getValueType();
2375 switch (SrcTy) {
2376 case MVT::i1:
2377 case MVT::i8:
2378 PromoteOpcode = (N.getOpcode() == ISD::UINT_TO_FP) ?
2379 X86::MOVZX32rr8 : X86::MOVSX32rr8;
2380 break;
2381 case MVT::i16:
2382 PromoteOpcode = (N.getOpcode() == ISD::UINT_TO_FP) ?
2383 X86::MOVZX32rr16 : X86::MOVSX32rr16;
2384 break;
2385 default:
2386 assert(N.getOpcode() != ISD::UINT_TO_FP);
2387 break;
2388 }
2389 if (PromoteOpcode) {
2390 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2391 Tmp1 = Tmp2;
2392 }
2393 Opc = (DstTy == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
2394 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2395 return Result;
2396 }
2397
Chris Lattner282781c2005-01-09 18:52:44 +00002398 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattnere44e6d12005-01-11 03:50:45 +00002399 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00002400
2401 // Promote the integer to a type supported by FLD. We do this because there
2402 // are no unsigned FLD instructions, so we must promote an unsigned value to
2403 // a larger signed value, then use FLD on the larger value.
2404 //
2405 MVT::ValueType PromoteType = MVT::Other;
2406 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner2e81f652005-07-07 17:12:53 +00002407 unsigned RealDestReg = Result;
Chris Lattner282781c2005-01-09 18:52:44 +00002408 switch (SrcTy) {
2409 case MVT::i1:
2410 case MVT::i8:
2411 // We don't have the facilities for directly loading byte sized data from
2412 // memory (even signed). Promote it to 16 bits.
2413 PromoteType = MVT::i16;
2414 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
2415 X86::MOVSX16rr8 : X86::MOVZX16rr8;
2416 break;
2417 case MVT::i16:
2418 if (Node->getOpcode() == ISD::UINT_TO_FP) {
2419 PromoteType = MVT::i32;
2420 PromoteOpcode = X86::MOVZX32rr16;
2421 }
2422 break;
2423 default:
2424 // Don't fild into the real destination.
2425 if (Node->getOpcode() == ISD::UINT_TO_FP)
2426 Result = MakeReg(Node->getValueType(0));
2427 break;
2428 }
2429
Chris Lattner282781c2005-01-09 18:52:44 +00002430 if (PromoteType != MVT::Other) {
2431 Tmp2 = MakeReg(PromoteType);
2432 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2433 SrcTy = PromoteType;
2434 Tmp1 = Tmp2;
2435 }
2436
2437 // Spill the integer to memory and reload it from there.
2438 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2439 MachineFunction *F = BB->getParent();
2440 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2441
2442 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00002443 case MVT::i32:
2444 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
2445 FrameIdx).addReg(Tmp1);
2446 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2447 break;
2448 case MVT::i16:
2449 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
2450 FrameIdx).addReg(Tmp1);
2451 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2452 break;
2453 default: break; // No promotion required.
2454 }
Chris Lattner2e81f652005-07-07 17:12:53 +00002455
2456 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
2457 // If this is a cast from uint -> double, we need to be careful when if
2458 // the "sign" bit is set. If so, we don't want to make a negative number,
2459 // we want to make a positive number. Emit code to add an offset if the
2460 // sign bit is set.
2461
2462 // Compute whether the sign bit is set by shifting the reg right 31 bits.
2463 unsigned IsNeg = MakeReg(MVT::i32);
2464 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
2465
2466 // Create a CP value that has the offset in one word and 0 in the other.
2467 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
2468 0x4f80000000000000ULL);
2469 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
2470 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
2471 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
2472 }
2473 return RealDestReg;
Chris Lattner282781c2005-01-09 18:52:44 +00002474 }
2475 case ISD::FP_TO_SINT:
2476 case ISD::FP_TO_UINT: {
2477 // FIXME: Most of this grunt work should be done by legalize!
2478 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2479
Nate Begeman8a093362005-07-06 18:59:04 +00002480 // If the target supports SSE2 and is performing FP operations in SSE regs
2481 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2482 // CVTSD2SI instructions.
2483 if (ISD::FP_TO_SINT == N.getOpcode() && X86ScalarSSE) {
2484 if (MVT::f32 == N.getOperand(0).getValueType()) {
2485 BuildMI(BB, X86::CVTSS2SIrr, 1, Result).addReg(Tmp1);
2486 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2487 BuildMI(BB, X86::CVTSD2SIrr, 1, Result).addReg(Tmp1);
2488 } else {
2489 assert(0 && "Not an f32 or f64?");
2490 abort();
2491 }
2492 return Result;
2493 }
2494
Chris Lattner282781c2005-01-09 18:52:44 +00002495 // Change the floating point control register to use "round towards zero"
2496 // mode when truncating to an integer value.
2497 //
2498 MachineFunction *F = BB->getParent();
2499 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2500 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2501
2502 // Load the old value of the high byte of the control word...
2503 unsigned HighPartOfCW = MakeReg(MVT::i8);
2504 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2505 CWFrameIdx, 1);
2506
2507 // Set the high part to be round to zero...
2508 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2509 CWFrameIdx, 1).addImm(12);
2510
2511 // Reload the modified control word now...
2512 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002513
Chris Lattner282781c2005-01-09 18:52:44 +00002514 // Restore the memory image of control word to original value
2515 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2516 CWFrameIdx, 1).addReg(HighPartOfCW);
2517
2518 // We don't have the facilities for directly storing byte sized data to
2519 // memory. Promote it to 16 bits. We also must promote unsigned values to
2520 // larger classes because we only have signed FP stores.
2521 MVT::ValueType StoreClass = Node->getValueType(0);
2522 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2523 switch (StoreClass) {
Chris Lattnere6266112005-05-09 05:33:18 +00002524 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002525 case MVT::i8: StoreClass = MVT::i16; break;
2526 case MVT::i16: StoreClass = MVT::i32; break;
2527 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner282781c2005-01-09 18:52:44 +00002528 default: assert(0 && "Unknown store class!");
2529 }
2530
2531 // Spill the integer to memory and reload it from there.
2532 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2533 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2534
2535 switch (StoreClass) {
2536 default: assert(0 && "Unknown store class!");
2537 case MVT::i16:
2538 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2539 break;
2540 case MVT::i32:
Chris Lattner66d34302005-01-09 19:49:59 +00002541 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002542 break;
Chris Lattner4ccd1f62005-05-09 18:37:02 +00002543 case MVT::i64:
2544 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2545 break; }
Chris Lattner282781c2005-01-09 18:52:44 +00002546
2547 switch (Node->getValueType(0)) {
2548 default:
2549 assert(0 && "Unknown integer type!");
Chris Lattner282781c2005-01-09 18:52:44 +00002550 case MVT::i32:
2551 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2552 break;
2553 case MVT::i16:
2554 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2555 break;
2556 case MVT::i8:
Chris Lattnere6266112005-05-09 05:33:18 +00002557 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002558 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2559 break;
2560 }
2561
2562 // Reload the original control word now.
2563 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2564 return Result;
2565 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002566 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00002567 Op0 = N.getOperand(0);
2568 Op1 = N.getOperand(1);
2569
Chris Lattner30607ec2005-01-25 20:03:11 +00002570 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00002571 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002572 goto FoldAdd;
2573 }
Chris Lattner62b22422005-01-11 21:19:59 +00002574
Chris Lattner30607ec2005-01-25 20:03:11 +00002575 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002576 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00002577 switch (N.getValueType()) {
2578 default: assert(0 && "Cannot add this type!");
2579 case MVT::i1:
2580 case MVT::i8: Opc = X86::ADD8rm; break;
2581 case MVT::i16: Opc = X86::ADD16rm; break;
2582 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002583 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002584 case MVT::f64:
2585 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00002586 if (X86ScalarSSE) {
2587 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2588 Opc = X86::ADDSDrm;
2589 } else {
2590 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2591 }
Chris Lattner30607ec2005-01-25 20:03:11 +00002592 break;
Chris Lattner62b22422005-01-11 21:19:59 +00002593 }
2594 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002595 EmitFoldedLoad(Op1, AM);
2596 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002597 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2598 return Result;
2599 }
2600
Chris Lattner88c8a232005-01-07 07:49:41 +00002601 // See if we can codegen this as an LEA to fold operations together.
2602 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00002603 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002604 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00002605 MatchAddress(N, AM);
2606 ExprMap[N] = Result;
2607
2608 // If this is not just an add, emit the LEA. For a simple add (like
2609 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2610 // leave this as LEA, then peephole it to 'ADD' after two address elim
2611 // happens.
2612 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2613 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2614 X86AddressMode XAM = SelectAddrExprs(AM);
2615 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2616 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002617 }
2618 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002619
Chris Lattner62b22422005-01-11 21:19:59 +00002620 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002621 Opc = 0;
2622 if (CN->getValue() == 1) { // add X, 1 -> inc X
2623 switch (N.getValueType()) {
2624 default: assert(0 && "Cannot integer add this type!");
2625 case MVT::i8: Opc = X86::INC8r; break;
2626 case MVT::i16: Opc = X86::INC16r; break;
2627 case MVT::i32: Opc = X86::INC32r; break;
2628 }
2629 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2630 switch (N.getValueType()) {
2631 default: assert(0 && "Cannot integer add this type!");
2632 case MVT::i8: Opc = X86::DEC8r; break;
2633 case MVT::i16: Opc = X86::DEC16r; break;
2634 case MVT::i32: Opc = X86::DEC32r; break;
2635 }
2636 }
2637
2638 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002639 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002640 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2641 return Result;
2642 }
2643
2644 switch (N.getValueType()) {
2645 default: assert(0 && "Cannot add this type!");
2646 case MVT::i8: Opc = X86::ADD8ri; break;
2647 case MVT::i16: Opc = X86::ADD16ri; break;
2648 case MVT::i32: Opc = X86::ADD32ri; break;
2649 }
2650 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002651 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002652 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2653 return Result;
2654 }
2655 }
2656
Chris Lattner88c8a232005-01-07 07:49:41 +00002657 switch (N.getValueType()) {
2658 default: assert(0 && "Cannot add this type!");
2659 case MVT::i8: Opc = X86::ADD8rr; break;
2660 case MVT::i16: Opc = X86::ADD16rr; break;
2661 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002662 case MVT::f32: Opc = X86::ADDSSrr; break;
2663 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002664 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002665
Chris Lattner62b22422005-01-11 21:19:59 +00002666 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2667 Tmp1 = SelectExpr(Op0);
2668 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002669 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002670 Tmp2 = SelectExpr(Op1);
2671 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002672 }
2673
Chris Lattner88c8a232005-01-07 07:49:41 +00002674 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2675 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00002676
Nate Begeman8a093362005-07-06 18:59:04 +00002677 case ISD::FSQRT:
2678 Tmp1 = SelectExpr(Node->getOperand(0));
2679 if (X86ScalarSSE) {
2680 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2681 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2682 } else {
2683 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2684 }
2685 return Result;
2686
2687 // FIXME:
2688 // Once we can spill 16 byte constants into the constant pool, we can
2689 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00002690 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00002691 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00002692 case ISD::FSIN:
2693 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00002694 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00002695 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00002696 switch (N.getOpcode()) {
2697 default: assert(0 && "Unreachable!");
2698 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2699 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00002700 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2701 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00002702 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00002703 return Result;
2704
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002705 case ISD::MULHU:
2706 switch (N.getValueType()) {
2707 default: assert(0 && "Unsupported VT!");
2708 case MVT::i8: Tmp2 = X86::MUL8r; break;
2709 case MVT::i16: Tmp2 = X86::MUL16r; break;
2710 case MVT::i32: Tmp2 = X86::MUL32r; break;
2711 }
2712 // FALL THROUGH
2713 case ISD::MULHS: {
2714 unsigned MovOpc, LowReg, HiReg;
2715 switch (N.getValueType()) {
2716 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00002717 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002718 MovOpc = X86::MOV8rr;
2719 LowReg = X86::AL;
2720 HiReg = X86::AH;
2721 Opc = X86::IMUL8r;
2722 break;
2723 case MVT::i16:
2724 MovOpc = X86::MOV16rr;
2725 LowReg = X86::AX;
2726 HiReg = X86::DX;
2727 Opc = X86::IMUL16r;
2728 break;
2729 case MVT::i32:
2730 MovOpc = X86::MOV32rr;
2731 LowReg = X86::EAX;
2732 HiReg = X86::EDX;
2733 Opc = X86::IMUL32r;
2734 break;
2735 }
2736 if (Node->getOpcode() != ISD::MULHS)
2737 Opc = Tmp2; // Get the MULHU opcode.
2738
2739 Op0 = Node->getOperand(0);
2740 Op1 = Node->getOperand(1);
2741 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2742 Tmp1 = SelectExpr(Op0);
2743 Tmp2 = SelectExpr(Op1);
2744 } else {
2745 Tmp2 = SelectExpr(Op1);
2746 Tmp1 = SelectExpr(Op0);
2747 }
2748
2749 // FIXME: Implement folding of loads into the memory operands here!
2750 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2751 BuildMI(BB, Opc, 1).addReg(Tmp2);
2752 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2753 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002754 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002755
Chris Lattner88c8a232005-01-07 07:49:41 +00002756 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00002757 case ISD::MUL:
2758 case ISD::AND:
2759 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00002760 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00002761 static const unsigned SUBTab[] = {
2762 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2763 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2764 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2765 };
Nate Begeman8a093362005-07-06 18:59:04 +00002766 static const unsigned SSE_SUBTab[] = {
2767 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2768 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2769 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2770 };
Chris Lattner62b22422005-01-11 21:19:59 +00002771 static const unsigned MULTab[] = {
2772 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2773 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2774 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2775 };
Nate Begeman8a093362005-07-06 18:59:04 +00002776 static const unsigned SSE_MULTab[] = {
2777 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2778 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2779 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2780 };
Chris Lattner62b22422005-01-11 21:19:59 +00002781 static const unsigned ANDTab[] = {
2782 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2783 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00002784 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00002785 };
2786 static const unsigned ORTab[] = {
2787 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2788 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2789 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2790 };
2791 static const unsigned XORTab[] = {
2792 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2793 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2794 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2795 };
2796
2797 Op0 = Node->getOperand(0);
2798 Op1 = Node->getOperand(1);
2799
Chris Lattner29f58192005-01-19 07:37:26 +00002800 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2801 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00002802 return Result;
2803
2804 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00002805 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2806 if (CN->isNullValue()) { // 0 - N -> neg N
2807 switch (N.getValueType()) {
2808 default: assert(0 && "Cannot sub this type!");
2809 case MVT::i1:
2810 case MVT::i8: Opc = X86::NEG8r; break;
2811 case MVT::i16: Opc = X86::NEG16r; break;
2812 case MVT::i32: Opc = X86::NEG32r; break;
2813 }
2814 Tmp1 = SelectExpr(N.getOperand(1));
2815 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2816 return Result;
2817 }
2818
Chris Lattner62b22422005-01-11 21:19:59 +00002819 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2820 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002821 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00002822 switch (N.getValueType()) {
2823 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002824 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00002825 case MVT::i8: Opc = X86::NOT8r; break;
2826 case MVT::i16: Opc = X86::NOT16r; break;
2827 case MVT::i32: Opc = X86::NOT32r; break;
2828 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002829 if (Opc) {
2830 Tmp1 = SelectExpr(Op0);
2831 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2832 return Result;
2833 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00002834 }
2835
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002836 // Fold common multiplies into LEA instructions.
2837 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2838 switch ((int)CN->getValue()) {
2839 default: break;
2840 case 3:
2841 case 5:
2842 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002843 // Remove N from exprmap so SelectAddress doesn't get confused.
2844 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002845 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002846 SelectAddress(N, AM);
2847 // Restore it to the map.
2848 ExprMap[N] = Result;
2849 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2850 return Result;
2851 }
2852 }
2853
Chris Lattner88c8a232005-01-07 07:49:41 +00002854 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00002855 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00002856 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00002857 case MVT::i8: Opc = 0; break;
2858 case MVT::i16: Opc = 1; break;
2859 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002860 }
Chris Lattner62b22422005-01-11 21:19:59 +00002861 switch (Node->getOpcode()) {
2862 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002863 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2864 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002865 case ISD::AND: Opc = ANDTab[Opc]; break;
2866 case ISD::OR: Opc = ORTab[Opc]; break;
2867 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002868 }
Chris Lattner62b22422005-01-11 21:19:59 +00002869 if (Opc) { // Can't fold MUL:i8 R, imm
2870 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002871 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2872 return Result;
2873 }
2874 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002875
Chris Lattner30607ec2005-01-25 20:03:11 +00002876 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner62b22422005-01-11 21:19:59 +00002877 if (Node->getOpcode() != ISD::SUB) {
2878 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002879 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00002880 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00002881 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00002882 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00002883 if (Op0.getOpcode() == ISD::EXTLOAD)
2884 Opc = X86::FSUBR32m;
2885 else
2886 Opc = X86::FSUBR64m;
2887
Chris Lattner62b22422005-01-11 21:19:59 +00002888 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002889 EmitFoldedLoad(Op0, AM);
2890 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00002891 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2892 return Result;
2893 }
2894 }
2895
Chris Lattner30607ec2005-01-25 20:03:11 +00002896 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002897 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00002898 switch (N.getValueType()) {
2899 default: assert(0 && "Cannot operate on this type!");
2900 case MVT::i1:
2901 case MVT::i8: Opc = 5; break;
2902 case MVT::i16: Opc = 6; break;
2903 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002904 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002905 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00002906 case MVT::f64:
2907 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
2908 "SSE load should have been promoted");
2909 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002910 }
2911 switch (Node->getOpcode()) {
2912 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002913 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2914 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002915 case ISD::AND: Opc = ANDTab[Opc]; break;
2916 case ISD::OR: Opc = ORTab[Opc]; break;
2917 case ISD::XOR: Opc = XORTab[Opc]; break;
2918 }
2919
2920 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002921 EmitFoldedLoad(Op1, AM);
2922 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002923 if (Opc) {
2924 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2925 } else {
2926 assert(Node->getOpcode() == ISD::MUL &&
2927 N.getValueType() == MVT::i8 && "Unexpected situation!");
2928 // Must use the MUL instruction, which forces use of AL.
2929 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2930 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2931 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2932 }
2933 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002934 }
Chris Lattner62b22422005-01-11 21:19:59 +00002935
2936 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2937 Tmp1 = SelectExpr(Op0);
2938 Tmp2 = SelectExpr(Op1);
2939 } else {
2940 Tmp2 = SelectExpr(Op1);
2941 Tmp1 = SelectExpr(Op0);
2942 }
2943
Chris Lattner88c8a232005-01-07 07:49:41 +00002944 switch (N.getValueType()) {
2945 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00002946 case MVT::i1:
2947 case MVT::i8: Opc = 10; break;
2948 case MVT::i16: Opc = 11; break;
2949 case MVT::i32: Opc = 12; break;
2950 case MVT::f32: Opc = 13; break;
2951 case MVT::f64: Opc = 14; break;
2952 }
2953 switch (Node->getOpcode()) {
2954 default: assert(0 && "Unreachable!");
Nate Begeman8a093362005-07-06 18:59:04 +00002955 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2956 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002957 case ISD::AND: Opc = ANDTab[Opc]; break;
2958 case ISD::OR: Opc = ORTab[Opc]; break;
2959 case ISD::XOR: Opc = XORTab[Opc]; break;
2960 }
2961 if (Opc) {
2962 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2963 } else {
2964 assert(Node->getOpcode() == ISD::MUL &&
2965 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00002966 // Must use the MUL instruction, which forces use of AL.
2967 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2968 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2969 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00002970 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002971 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00002972 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00002973 case ISD::ADD_PARTS:
2974 case ISD::SUB_PARTS: {
2975 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2976 "Not an i64 add/sub!");
2977 // Emit all of the operands.
2978 std::vector<unsigned> InVals;
2979 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2980 InVals.push_back(SelectExpr(N.getOperand(i)));
2981 if (N.getOpcode() == ISD::ADD_PARTS) {
2982 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2983 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2984 } else {
2985 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2986 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2987 }
2988 return Result+N.ResNo;
2989 }
2990
Chris Lattnera31d4c72005-04-02 04:01:14 +00002991 case ISD::SHL_PARTS:
2992 case ISD::SRA_PARTS:
2993 case ISD::SRL_PARTS: {
2994 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2995 "Not an i64 shift!");
2996 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2997 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2998 unsigned TmpReg = MakeReg(MVT::i32);
2999 if (N.getOpcode() == ISD::SRA_PARTS) {
3000 // If this is a SHR of a Long, then we need to do funny sign extension
3001 // stuff. TmpReg gets the value to use as the high-part if we are
3002 // shifting more than 32 bits.
3003 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
3004 } else {
3005 // Other shifts use a fixed zero value if the shift is more than 32 bits.
3006 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
3007 }
3008
3009 // Initialize CL with the shift amount.
3010 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
3011 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
3012
3013 unsigned TmpReg2 = MakeReg(MVT::i32);
3014 unsigned TmpReg3 = MakeReg(MVT::i32);
3015 if (N.getOpcode() == ISD::SHL_PARTS) {
3016 // TmpReg2 = shld inHi, inLo
3017 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
3018 .addReg(ShiftOpLo);
3019 // TmpReg3 = shl inLo, CL
3020 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003021
Chris Lattnera31d4c72005-04-02 04:01:14 +00003022 // Set the flags to indicate whether the shift was by more than 32 bits.
3023 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003024
Chris Lattnera31d4c72005-04-02 04:01:14 +00003025 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003026 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00003027 Result+1).addReg(TmpReg2).addReg(TmpReg3);
3028 // DestLo = (>32) ? TmpReg : TmpReg3;
3029 BuildMI(BB, X86::CMOVNE32rr, 2,
3030 Result).addReg(TmpReg3).addReg(TmpReg);
3031 } else {
3032 // TmpReg2 = shrd inLo, inHi
3033 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
3034 .addReg(ShiftOpHi);
3035 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00003036 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00003037 : X86::SHR32rCL, 1, TmpReg3)
3038 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003039
Chris Lattnera31d4c72005-04-02 04:01:14 +00003040 // Set the flags to indicate whether the shift was by more than 32 bits.
3041 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003042
Chris Lattnera31d4c72005-04-02 04:01:14 +00003043 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003044 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00003045 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003046
Chris Lattnera31d4c72005-04-02 04:01:14 +00003047 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003048 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00003049 Result+1).addReg(TmpReg3).addReg(TmpReg);
3050 }
3051 return Result+N.ResNo;
3052 }
3053
Chris Lattner88c8a232005-01-07 07:49:41 +00003054 case ISD::SELECT:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003055 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3056 Tmp2 = SelectExpr(N.getOperand(1));
3057 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner88c8a232005-01-07 07:49:41 +00003058 } else {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003059 Tmp3 = SelectExpr(N.getOperand(2));
3060 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88c8a232005-01-07 07:49:41 +00003061 }
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003062 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
3063 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003064
3065 case ISD::SDIV:
3066 case ISD::UDIV:
3067 case ISD::SREM:
3068 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003069 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3070 "We don't support this operator!");
3071
Chris Lattner60c23bd2005-04-13 03:29:53 +00003072 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00003073 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00003074 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00003075 // Check for reversed and unreversed DIV.
3076 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3077 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3078 Opc = X86::FDIVR32m;
3079 else
3080 Opc = X86::FDIVR64m;
3081 X86AddressMode AM;
3082 EmitFoldedLoad(N.getOperand(0), AM);
3083 Tmp1 = SelectExpr(N.getOperand(1));
3084 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3085 return Result;
3086 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3087 N.getOperand(1).getOpcode() == ISD::LOAD) {
3088 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3089 Opc = X86::FDIV32m;
3090 else
3091 Opc = X86::FDIV64m;
3092 X86AddressMode AM;
3093 EmitFoldedLoad(N.getOperand(1), AM);
3094 Tmp1 = SelectExpr(N.getOperand(0));
3095 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3096 return Result;
3097 }
3098 }
3099
Chris Lattner88c8a232005-01-07 07:49:41 +00003100 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3101 // FIXME: These special cases should be handled by the lowering impl!
3102 unsigned RHS = CN->getValue();
3103 bool isNeg = false;
3104 if ((int)RHS < 0) {
3105 isNeg = true;
3106 RHS = -RHS;
3107 }
3108 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
3109 unsigned Log = log2(RHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00003110 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3111 switch (N.getValueType()) {
3112 default: assert("Unknown type to signed divide!");
3113 case MVT::i8:
3114 SAROpc = X86::SAR8ri;
3115 SHROpc = X86::SHR8ri;
3116 ADDOpc = X86::ADD8rr;
3117 NEGOpc = X86::NEG8r;
3118 break;
3119 case MVT::i16:
3120 SAROpc = X86::SAR16ri;
3121 SHROpc = X86::SHR16ri;
3122 ADDOpc = X86::ADD16rr;
3123 NEGOpc = X86::NEG16r;
3124 break;
3125 case MVT::i32:
3126 SAROpc = X86::SAR32ri;
3127 SHROpc = X86::SHR32ri;
3128 ADDOpc = X86::ADD32rr;
3129 NEGOpc = X86::NEG32r;
3130 break;
3131 }
Chris Lattner7d387d22005-05-13 21:48:20 +00003132 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003133 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner79e9fa52005-05-13 21:50:27 +00003134 unsigned TmpReg;
3135 if (Log != 1) {
3136 TmpReg = MakeReg(N.getValueType());
3137 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3138 } else {
3139 TmpReg = Tmp1;
3140 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003141 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattner7d387d22005-05-13 21:48:20 +00003142 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner88c8a232005-01-07 07:49:41 +00003143 unsigned TmpReg3 = MakeReg(N.getValueType());
3144 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003145
Chris Lattner88c8a232005-01-07 07:49:41 +00003146 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3147 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3148 if (isNeg)
3149 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3150 return Result;
3151 }
3152 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00003153 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003154
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003155 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3156 Tmp1 = SelectExpr(N.getOperand(0));
3157 Tmp2 = SelectExpr(N.getOperand(1));
3158 } else {
3159 Tmp2 = SelectExpr(N.getOperand(1));
3160 Tmp1 = SelectExpr(N.getOperand(0));
3161 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003162
3163 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3164 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3165 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3166 switch (N.getValueType()) {
3167 default: assert(0 && "Cannot sdiv this type!");
3168 case MVT::i8:
3169 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3170 LoReg = X86::AL;
3171 HiReg = X86::AH;
3172 MovOpcode = X86::MOV8rr;
3173 ClrOpcode = X86::MOV8ri;
3174 SExtOpcode = X86::CBW;
3175 break;
3176 case MVT::i16:
3177 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3178 LoReg = X86::AX;
3179 HiReg = X86::DX;
3180 MovOpcode = X86::MOV16rr;
3181 ClrOpcode = X86::MOV16ri;
3182 SExtOpcode = X86::CWD;
3183 break;
3184 case MVT::i32:
3185 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00003186 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00003187 HiReg = X86::EDX;
3188 MovOpcode = X86::MOV32rr;
3189 ClrOpcode = X86::MOV32ri;
3190 SExtOpcode = X86::CDQ;
3191 break;
Nate Begeman8a093362005-07-06 18:59:04 +00003192 case MVT::f32:
3193 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3194 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003195 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003196 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003198 return Result;
3199 }
3200
3201 // Set up the low part.
3202 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3203
3204 if (isSigned) {
3205 // Sign extend the low part into the high part.
3206 BuildMI(BB, SExtOpcode, 0);
3207 } else {
3208 // Zero out the high part, effectively zero extending the input.
3209 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3210 }
3211
3212 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00003213 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003214
3215 // Get the result of the divide or rem.
3216 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3217 return Result;
3218 }
3219
3220 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003221 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00003222 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3223 switch (N.getValueType()) {
3224 default: assert(0 && "Cannot shift this type!");
3225 case MVT::i8: Opc = X86::ADD8rr; break;
3226 case MVT::i16: Opc = X86::ADD16rr; break;
3227 case MVT::i32: Opc = X86::ADD32rr; break;
3228 }
3229 Tmp1 = SelectExpr(N.getOperand(0));
3230 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3231 return Result;
3232 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003233
Chris Lattner88c8a232005-01-07 07:49:41 +00003234 switch (N.getValueType()) {
3235 default: assert(0 && "Cannot shift this type!");
3236 case MVT::i8: Opc = X86::SHL8ri; break;
3237 case MVT::i16: Opc = X86::SHL16ri; break;
3238 case MVT::i32: Opc = X86::SHL32ri; break;
3239 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003240 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003241 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3242 return Result;
3243 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003244
3245 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3246 Tmp1 = SelectExpr(N.getOperand(0));
3247 Tmp2 = SelectExpr(N.getOperand(1));
3248 } else {
3249 Tmp2 = SelectExpr(N.getOperand(1));
3250 Tmp1 = SelectExpr(N.getOperand(0));
3251 }
3252
Chris Lattner88c8a232005-01-07 07:49:41 +00003253 switch (N.getValueType()) {
3254 default: assert(0 && "Cannot shift this type!");
3255 case MVT::i8 : Opc = X86::SHL8rCL; break;
3256 case MVT::i16: Opc = X86::SHL16rCL; break;
3257 case MVT::i32: Opc = X86::SHL32rCL; break;
3258 }
3259 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3260 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3261 return Result;
3262 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003263 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3264 switch (N.getValueType()) {
3265 default: assert(0 && "Cannot shift this type!");
3266 case MVT::i8: Opc = X86::SHR8ri; break;
3267 case MVT::i16: Opc = X86::SHR16ri; break;
3268 case MVT::i32: Opc = X86::SHR32ri; break;
3269 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003270 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003271 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3272 return Result;
3273 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003274
3275 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3276 Tmp1 = SelectExpr(N.getOperand(0));
3277 Tmp2 = SelectExpr(N.getOperand(1));
3278 } else {
3279 Tmp2 = SelectExpr(N.getOperand(1));
3280 Tmp1 = SelectExpr(N.getOperand(0));
3281 }
3282
Chris Lattner88c8a232005-01-07 07:49:41 +00003283 switch (N.getValueType()) {
3284 default: assert(0 && "Cannot shift this type!");
3285 case MVT::i8 : Opc = X86::SHR8rCL; break;
3286 case MVT::i16: Opc = X86::SHR16rCL; break;
3287 case MVT::i32: Opc = X86::SHR32rCL; break;
3288 }
3289 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3290 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3291 return Result;
3292 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00003293 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3294 switch (N.getValueType()) {
3295 default: assert(0 && "Cannot shift this type!");
3296 case MVT::i8: Opc = X86::SAR8ri; break;
3297 case MVT::i16: Opc = X86::SAR16ri; break;
3298 case MVT::i32: Opc = X86::SAR32ri; break;
3299 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003300 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003301 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3302 return Result;
3303 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003304
3305 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3306 Tmp1 = SelectExpr(N.getOperand(0));
3307 Tmp2 = SelectExpr(N.getOperand(1));
3308 } else {
3309 Tmp2 = SelectExpr(N.getOperand(1));
3310 Tmp1 = SelectExpr(N.getOperand(0));
3311 }
3312
Chris Lattner88c8a232005-01-07 07:49:41 +00003313 switch (N.getValueType()) {
3314 default: assert(0 && "Cannot shift this type!");
3315 case MVT::i8 : Opc = X86::SAR8rCL; break;
3316 case MVT::i16: Opc = X86::SAR16rCL; break;
3317 case MVT::i32: Opc = X86::SAR32rCL; break;
3318 }
3319 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3320 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3321 return Result;
3322
3323 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00003324 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00003325 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3326 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3327 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003328 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003329 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00003330 if (Result != 1) { // Generate the token
3331 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3332 assert(0 && "Load already emitted!?");
3333 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00003334 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3335
Chris Lattnerb52e0412005-01-08 19:28:19 +00003336 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003337 default: assert(0 && "Cannot load this type!");
3338 case MVT::i1:
3339 case MVT::i8: Opc = X86::MOV8rm; break;
3340 case MVT::i16: Opc = X86::MOV16rm; break;
3341 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003342 case MVT::f32: Opc = X86::MOVSSrm; break;
3343 case MVT::f64:
3344 if (X86ScalarSSE) {
3345 Opc = X86::MOVSDrm;
3346 } else {
3347 Opc = X86::FLD64m;
3348 ContainsFPCode = true;
3349 }
3350 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003351 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003352
Chris Lattner88c8a232005-01-07 07:49:41 +00003353 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003354 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003355 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3356 } else {
3357 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00003358
3359 SDOperand Chain = N.getOperand(0);
3360 SDOperand Address = N.getOperand(1);
3361 if (getRegPressure(Chain) > getRegPressure(Address)) {
3362 Select(Chain);
3363 SelectAddress(Address, AM);
3364 } else {
3365 SelectAddress(Address, AM);
3366 Select(Chain);
3367 }
3368
Chris Lattner88c8a232005-01-07 07:49:41 +00003369 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3370 }
3371 return Result;
Chris Lattnera36117b2005-05-14 06:52:07 +00003372 case X86ISD::FILD64m:
3373 // Make sure we generate both values.
3374 assert(Result != 1 && N.getValueType() == MVT::f64);
3375 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3376 assert(0 && "Load already emitted!?");
3377
3378 {
3379 X86AddressMode AM;
3380
3381 SDOperand Chain = N.getOperand(0);
3382 SDOperand Address = N.getOperand(1);
3383 if (getRegPressure(Chain) > getRegPressure(Address)) {
3384 Select(Chain);
3385 SelectAddress(Address, AM);
3386 } else {
3387 SelectAddress(Address, AM);
3388 Select(Chain);
3389 }
3390
3391 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
3392 }
3393 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003394
3395 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3396 case ISD::ZEXTLOAD: {
3397 // Make sure we generate both values.
3398 if (Result != 1)
3399 ExprMap[N.getValue(1)] = 1; // Generate the token
3400 else
3401 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3402
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003403 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3404 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00003405 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003406 "Bad EXTLOAD!");
3407 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3408 CP->getIndex());
3409 return Result;
3410 }
3411
Chris Lattnere18a4c42005-01-15 05:22:24 +00003412 X86AddressMode AM;
3413 if (getRegPressure(Node->getOperand(0)) >
3414 getRegPressure(Node->getOperand(1))) {
3415 Select(Node->getOperand(0)); // chain
3416 SelectAddress(Node->getOperand(1), AM);
3417 } else {
3418 SelectAddress(Node->getOperand(1), AM);
3419 Select(Node->getOperand(0)); // chain
3420 }
3421
3422 switch (Node->getValueType(0)) {
3423 default: assert(0 && "Unknown type to sign extend to.");
3424 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00003425 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003426 "Bad EXTLOAD!");
3427 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3428 break;
3429 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003430 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003431 default:
3432 assert(0 && "Bad zero extend!");
3433 case MVT::i1:
3434 case MVT::i8:
3435 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3436 break;
3437 case MVT::i16:
3438 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3439 break;
3440 }
3441 break;
3442 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003443 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003444 "Bad zero extend!");
3445 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3446 break;
3447 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00003448 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003449 "Bad zero extend!");
3450 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3451 break;
3452 }
3453 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003454 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003455 case ISD::SEXTLOAD: {
3456 // Make sure we generate both values.
3457 if (Result != 1)
3458 ExprMap[N.getValue(1)] = 1; // Generate the token
3459 else
3460 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3461
3462 X86AddressMode AM;
3463 if (getRegPressure(Node->getOperand(0)) >
3464 getRegPressure(Node->getOperand(1))) {
3465 Select(Node->getOperand(0)); // chain
3466 SelectAddress(Node->getOperand(1), AM);
3467 } else {
3468 SelectAddress(Node->getOperand(1), AM);
3469 Select(Node->getOperand(0)); // chain
3470 }
3471
3472 switch (Node->getValueType(0)) {
3473 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3474 default: assert(0 && "Unknown type to sign extend to.");
3475 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003476 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003477 default:
3478 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3479 case MVT::i8:
3480 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3481 break;
3482 case MVT::i16:
3483 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3484 break;
3485 }
3486 break;
3487 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003488 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003489 "Cannot sign extend from bool!");
3490 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3491 break;
3492 }
3493 return Result;
3494 }
3495
Chris Lattner88c8a232005-01-07 07:49:41 +00003496 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00003497 // Generate both result values.
3498 if (Result != 1)
3499 ExprMap[N.getValue(1)] = 1; // Generate the token
3500 else
3501 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3502
3503 // FIXME: We are currently ignoring the requested alignment for handling
3504 // greater than the stack alignment. This will need to be revisited at some
3505 // point. Align = N.getOperand(2);
3506
3507 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3508 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3509 std::cerr << "Cannot allocate stack object with greater alignment than"
3510 << " the stack alignment yet!";
3511 abort();
3512 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003513
Chris Lattner88c8a232005-01-07 07:49:41 +00003514 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003515 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003516 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3517 .addImm(CN->getValue());
3518 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003519 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3520 Select(N.getOperand(0));
3521 Tmp1 = SelectExpr(N.getOperand(1));
3522 } else {
3523 Tmp1 = SelectExpr(N.getOperand(1));
3524 Select(N.getOperand(0));
3525 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003526
3527 // Subtract size from stack pointer, thereby allocating some space.
3528 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3529 }
3530
3531 // Put a pointer to the space into the result register, by copying the stack
3532 // pointer.
3533 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3534 return Result;
3535
Chris Lattner1b3520c2005-05-14 08:48:15 +00003536 case X86ISD::TAILCALL:
3537 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00003538 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003539 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00003540
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003541 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3542 isa<ExternalSymbolSDNode>(N.getOperand(1));
3543 unsigned Callee = 0;
3544 if (isDirect) {
3545 Select(N.getOperand(0));
3546 } else {
3547 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3548 Select(N.getOperand(0));
3549 Callee = SelectExpr(N.getOperand(1));
3550 } else {
3551 Callee = SelectExpr(N.getOperand(1));
3552 Select(N.getOperand(0));
3553 }
3554 }
3555
3556 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003557 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003558 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003559 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003560 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00003561 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003562
Chris Lattner1b3520c2005-05-14 08:48:15 +00003563 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003564 default: assert(0 && "Bad thing to pass in regs");
3565 case MVT::i1:
3566 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3567 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3568 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3569 }
3570 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00003571 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003572 default: assert(0 && "Bad thing to pass in regs");
3573 case MVT::i1:
3574 case MVT::i8:
3575 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3576 break;
3577 case MVT::i16:
3578 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3579 break;
3580 case MVT::i32:
3581 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3582 break;
3583 }
3584 }
3585
Chris Lattner88c8a232005-01-07 07:49:41 +00003586 if (GlobalAddressSDNode *GASD =
3587 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3588 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3589 } else if (ExternalSymbolSDNode *ESSDN =
3590 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3591 BuildMI(BB, X86::CALLpcrel32,
3592 1).addExternalSymbol(ESSDN->getSymbol(), true);
3593 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003594 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3595 Select(N.getOperand(0));
3596 Tmp1 = SelectExpr(N.getOperand(1));
3597 } else {
3598 Tmp1 = SelectExpr(N.getOperand(1));
3599 Select(N.getOperand(0));
3600 }
3601
Chris Lattner88c8a232005-01-07 07:49:41 +00003602 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3603 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003604
3605 // Get caller stack amount and amount the callee added to the stack pointer.
3606 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3607 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3608 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3609
3610 if (Node->getNumValues() != 1)
3611 switch (Node->getValueType(1)) {
3612 default: assert(0 && "Unknown value type for call result!");
3613 case MVT::Other: return 1;
3614 case MVT::i1:
3615 case MVT::i8:
3616 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3617 break;
3618 case MVT::i16:
3619 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3620 break;
3621 case MVT::i32:
3622 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3623 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3624 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3625 break;
3626 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00003627 if (X86ScalarSSE) {
3628 ContainsFPCode = true;
3629 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3630
3631 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3632 MachineFunction *F = BB->getParent();
3633 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3634 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3635 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3636 break;
3637 } else {
3638 ContainsFPCode = true;
3639 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3640 break;
3641 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003642 }
3643 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003644 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003645 case ISD::READPORT:
3646 // First, determine that the size of the operand falls within the acceptable
3647 // range for this architecture.
3648 //
3649 if (Node->getOperand(1).getValueType() != MVT::i16) {
3650 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3651 exit(1);
3652 }
3653
3654 // Make sure we generate both values.
3655 if (Result != 1) { // Generate the token
3656 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3657 assert(0 && "readport already emitted!?");
3658 } else
3659 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3660
3661 Select(Node->getOperand(0)); // Select the chain.
3662
3663 // If the port is a single-byte constant, use the immediate form.
3664 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3665 if ((Port->getValue() & 255) == Port->getValue()) {
3666 switch (Node->getValueType(0)) {
3667 case MVT::i8:
3668 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3669 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3670 return Result;
3671 case MVT::i16:
3672 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3673 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3674 return Result;
3675 case MVT::i32:
3676 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3677 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3678 return Result;
3679 default: break;
3680 }
3681 }
3682
3683 // Now, move the I/O port address into the DX register and use the IN
3684 // instruction to get the input data.
3685 //
3686 Tmp1 = SelectExpr(Node->getOperand(1));
3687 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3688 switch (Node->getValueType(0)) {
3689 case MVT::i8:
3690 BuildMI(BB, X86::IN8rr, 0);
3691 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3692 return Result;
3693 case MVT::i16:
3694 BuildMI(BB, X86::IN16rr, 0);
3695 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3696 return Result;
3697 case MVT::i32:
3698 BuildMI(BB, X86::IN32rr, 0);
3699 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3700 return Result;
3701 default:
3702 std::cerr << "Cannot do input on this data type";
3703 exit(1);
3704 }
3705
Chris Lattner88c8a232005-01-07 07:49:41 +00003706 }
3707
3708 return 0;
3709}
3710
Chris Lattner96113fd2005-01-17 19:25:26 +00003711/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3712/// load/op/store instruction. If successful return true.
3713bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3714 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3715 SDOperand Chain = Node->getOperand(0);
3716 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00003717 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00003718
3719 // The chain has to be a load, the stored value must be an integer binary
3720 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00003721 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00003722 MVT::isFloatingPoint(StVal.getValueType()))
3723 return false;
3724
Chris Lattnere86c9332005-01-17 22:10:42 +00003725 // Token chain must either be a factor node or the load to fold.
3726 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3727 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00003728
Chris Lattnere86c9332005-01-17 22:10:42 +00003729 SDOperand TheLoad;
3730
3731 // Check to see if there is a load from the same pointer that we're storing
3732 // to in either operand of the binop.
3733 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3734 StVal.getOperand(0).getOperand(1) == StPtr)
3735 TheLoad = StVal.getOperand(0);
3736 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3737 StVal.getOperand(1).getOperand(1) == StPtr)
3738 TheLoad = StVal.getOperand(1);
3739 else
3740 return false; // No matching load operand.
3741
3742 // We can only fold the load if there are no intervening side-effecting
3743 // operations. This means that the store uses the load as its token chain, or
3744 // there are only token factor nodes in between the store and load.
3745 if (Chain != TheLoad.getValue(1)) {
3746 // Okay, the other option is that we have a store referring to (possibly
3747 // nested) token factor nodes. For now, just try peeking through one level
3748 // of token factors to see if this is the case.
3749 bool ChainOk = false;
3750 if (Chain.getOpcode() == ISD::TokenFactor) {
3751 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3752 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3753 ChainOk = true;
3754 break;
3755 }
3756 }
3757
3758 if (!ChainOk) return false;
3759 }
3760
3761 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00003762 return false;
3763
3764 // Make sure that one of the operands of the binop is the load, and that the
3765 // load folds into the binop.
3766 if (((StVal.getOperand(0) != TheLoad ||
3767 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3768 (StVal.getOperand(1) != TheLoad ||
3769 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3770 return false;
3771
3772 // Finally, check to see if this is one of the ops we can handle!
3773 static const unsigned ADDTAB[] = {
3774 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3775 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3776 };
3777 static const unsigned SUBTAB[] = {
3778 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3779 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3780 };
3781 static const unsigned ANDTAB[] = {
3782 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3783 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3784 };
3785 static const unsigned ORTAB[] = {
3786 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3787 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3788 };
3789 static const unsigned XORTAB[] = {
3790 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3791 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3792 };
3793 static const unsigned SHLTAB[] = {
3794 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3795 /*Have to put the reg in CL*/0, 0, 0,
3796 };
3797 static const unsigned SARTAB[] = {
3798 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3799 /*Have to put the reg in CL*/0, 0, 0,
3800 };
3801 static const unsigned SHRTAB[] = {
3802 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3803 /*Have to put the reg in CL*/0, 0, 0,
3804 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00003805
Chris Lattner96113fd2005-01-17 19:25:26 +00003806 const unsigned *TabPtr = 0;
3807 switch (StVal.getOpcode()) {
3808 default:
3809 std::cerr << "CANNOT [mem] op= val: ";
3810 StVal.Val->dump(); std::cerr << "\n";
3811 case ISD::MUL:
3812 case ISD::SDIV:
3813 case ISD::UDIV:
3814 case ISD::SREM:
3815 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003816
Chris Lattner96113fd2005-01-17 19:25:26 +00003817 case ISD::ADD: TabPtr = ADDTAB; break;
3818 case ISD::SUB: TabPtr = SUBTAB; break;
3819 case ISD::AND: TabPtr = ANDTAB; break;
3820 case ISD:: OR: TabPtr = ORTAB; break;
3821 case ISD::XOR: TabPtr = XORTAB; break;
3822 case ISD::SHL: TabPtr = SHLTAB; break;
3823 case ISD::SRA: TabPtr = SARTAB; break;
3824 case ISD::SRL: TabPtr = SHRTAB; break;
3825 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003826
Chris Lattner96113fd2005-01-17 19:25:26 +00003827 // Handle: [mem] op= CST
3828 SDOperand Op0 = StVal.getOperand(0);
3829 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00003830 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00003831 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3832 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3833 default: break;
3834 case MVT::i1:
3835 case MVT::i8: Opc = TabPtr[0]; break;
3836 case MVT::i16: Opc = TabPtr[1]; break;
3837 case MVT::i32: Opc = TabPtr[2]; break;
3838 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003839
Chris Lattner96113fd2005-01-17 19:25:26 +00003840 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00003841 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3842 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003843 Select(Chain);
3844
Chris Lattner96113fd2005-01-17 19:25:26 +00003845 X86AddressMode AM;
3846 if (getRegPressure(TheLoad.getOperand(0)) >
3847 getRegPressure(TheLoad.getOperand(1))) {
3848 Select(TheLoad.getOperand(0));
3849 SelectAddress(TheLoad.getOperand(1), AM);
3850 } else {
3851 SelectAddress(TheLoad.getOperand(1), AM);
3852 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00003853 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003854
3855 if (StVal.getOpcode() == ISD::ADD) {
3856 if (CN->getValue() == 1) {
3857 switch (Op0.getValueType()) {
3858 default: break;
3859 case MVT::i8:
3860 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3861 return true;
3862 case MVT::i16: Opc = TabPtr[1];
3863 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3864 return true;
3865 case MVT::i32: Opc = TabPtr[2];
3866 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3867 return true;
3868 }
3869 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3870 switch (Op0.getValueType()) {
3871 default: break;
3872 case MVT::i8:
3873 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3874 return true;
3875 case MVT::i16: Opc = TabPtr[1];
3876 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3877 return true;
3878 case MVT::i32: Opc = TabPtr[2];
3879 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3880 return true;
3881 }
3882 }
3883 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003884
Chris Lattner96113fd2005-01-17 19:25:26 +00003885 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3886 return true;
3887 }
3888 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003889
Chris Lattner96113fd2005-01-17 19:25:26 +00003890 // If we have [mem] = V op [mem], try to turn it into:
3891 // [mem] = [mem] op V.
3892 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3893 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3894 StVal.getOpcode() != ISD::SRL)
3895 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003896
Chris Lattner96113fd2005-01-17 19:25:26 +00003897 if (Op0 != TheLoad) return false;
3898
3899 switch (Op0.getValueType()) {
3900 default: return false;
3901 case MVT::i1:
3902 case MVT::i8: Opc = TabPtr[3]; break;
3903 case MVT::i16: Opc = TabPtr[4]; break;
3904 case MVT::i32: Opc = TabPtr[5]; break;
3905 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003906
Chris Lattner479c7112005-01-18 17:35:28 +00003907 // Table entry doesn't exist?
3908 if (Opc == 0) return false;
3909
Chris Lattner78d30282005-01-18 03:51:59 +00003910 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3911 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003912 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00003913 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00003914
Chris Lattner96113fd2005-01-17 19:25:26 +00003915 X86AddressMode AM;
3916 SelectAddress(TheLoad.getOperand(1), AM);
3917 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00003918 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00003919 return true;
3920}
3921
Chris Lattnerdd66a412005-05-15 05:46:45 +00003922/// If node is a ret(tailcall) node, emit the specified tail call and return
3923/// true, otherwise return false.
3924///
3925/// FIXME: This whole thing should be a post-legalize optimization pass which
3926/// recognizes and transforms the dag. We don't want the selection phase doing
3927/// this stuff!!
3928///
3929bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3930 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3931
3932 SDOperand Chain = RetNode->getOperand(0);
3933
3934 // If this is a token factor node where one operand is a call, dig into it.
3935 SDOperand TokFactor;
3936 unsigned TokFactorOperand = 0;
3937 if (Chain.getOpcode() == ISD::TokenFactor) {
3938 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3939 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3940 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3941 TokFactorOperand = i;
3942 TokFactor = Chain;
3943 Chain = Chain.getOperand(i);
3944 break;
3945 }
3946 if (TokFactor.Val == 0) return false; // No call operand.
3947 }
3948
3949 // Skip the CALLSEQ_END node if present.
3950 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3951 Chain = Chain.getOperand(0);
3952
3953 // Is a tailcall the last control operation that occurs before the return?
3954 if (Chain.getOpcode() != X86ISD::TAILCALL)
3955 return false;
3956
3957 // If we return a value, is it the value produced by the call?
3958 if (RetNode->getNumOperands() > 1) {
3959 // Not returning the ret val of the call?
3960 if (Chain.Val->getNumValues() == 1 ||
3961 RetNode->getOperand(1) != Chain.getValue(1))
3962 return false;
3963
3964 if (RetNode->getNumOperands() > 2) {
3965 if (Chain.Val->getNumValues() == 2 ||
3966 RetNode->getOperand(2) != Chain.getValue(2))
3967 return false;
3968 }
3969 assert(RetNode->getNumOperands() <= 3);
3970 }
3971
3972 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3973 // For FastCC, this will always be > 0.
3974 unsigned CalleeCallArgAmt =
3975 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3976
3977 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3978 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3979 unsigned CalleeCallArgPopAmt =
3980 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3981
3982 // There are several cases we can handle here. First, if the caller and
3983 // callee are both CCC functions, we can tailcall if the callee takes <= the
3984 // number of argument bytes that the caller does.
3985 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3986 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3987 // Check to see if caller arg area size >= callee arg area size.
3988 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3989 //std::cerr << "CCC TAILCALL UNIMP!\n";
3990 // If TokFactor is non-null, emit all operands.
3991
3992 //EmitCCCToCCCTailCall(Chain.Val);
3993 //return true;
3994 }
3995 return false;
3996 }
3997
3998 // Second, if both are FastCC functions, we can always perform the tail call.
3999 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
4000 // If TokFactor is non-null, emit all operands before the call.
4001 if (TokFactor.Val) {
4002 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
4003 if (i != TokFactorOperand)
4004 Select(TokFactor.getOperand(i));
4005 }
4006
4007 EmitFastCCToFastCCTailCall(Chain.Val);
4008 return true;
4009 }
4010
4011 // We don't support mixed calls, due to issues with alignment. We could in
4012 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
4013 // aligned (which depends on the number of arguments to the callee). TODO.
4014 return false;
4015}
4016
4017static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
4018 SelectionDAG &DAG) {
4019 MVT::ValueType StoreVT;
4020 switch (Chain.getOpcode()) {
4021 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00004022 // If we found the start of the call sequence, we're done. We actually
4023 // strip off the CALLSEQ_START node, to avoid generating the
4024 // ADJCALLSTACKDOWN marker for the tail call.
4025 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00004026 case ISD::TokenFactor: {
4027 std::vector<SDOperand> Ops;
4028 Ops.reserve(Chain.getNumOperands());
4029 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
4030 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
4031 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
4032 }
4033 case ISD::STORE: // Normal store
4034 StoreVT = Chain.getOperand(1).getValueType();
4035 break;
4036 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00004037 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00004038 break;
4039 }
4040
4041 SDOperand OrigDest = Chain.getOperand(2);
4042 unsigned OrigOffset;
4043
4044 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4045 OrigOffset = 0;
4046 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
4047 } else {
4048 // We expect only (ESP+C)
4049 assert(OrigDest.getOpcode() == ISD::ADD &&
4050 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4051 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
4052 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
4053 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4054 }
4055
4056 // Compute the new offset from the incoming ESP value we wish to use.
4057 unsigned NewOffset = OrigOffset + Offset;
4058
4059 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4060 MachineFunction &MF = DAG.getMachineFunction();
4061 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4062 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4063
4064 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4065 DAG);
4066 if (Chain.getOpcode() == ISD::STORE)
4067 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4068 FIN);
4069 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4070 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00004071 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00004072}
4073
4074
4075/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4076/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4077/// tail call.
4078void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4079 unsigned CalleeCallArgSize =
4080 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4081 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4082
4083 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4084
4085 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4086 // indexes that are relative to the incoming ESP. If the incoming and
4087 // outgoing arg sizes are the same we will store to [InESP] instead of
4088 // [CurESP] and the ESP referenced will be relative to the incoming function
4089 // ESP.
4090 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4091 SDOperand AdjustedArgStores =
4092 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4093
4094 // Copy the return address of the caller into a virtual register so we don't
4095 // clobber it.
4096 SDOperand RetVal;
4097 if (ESPOffset) {
4098 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4099 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4100 RetValAddr, TheDAG->getSrcValue(NULL));
4101 SelectExpr(RetVal);
4102 }
4103
4104 // Codegen all of the argument stores.
4105 Select(AdjustedArgStores);
4106
4107 if (RetVal.Val) {
4108 // Emit a store of the saved ret value to the new location.
4109 MachineFunction &MF = TheDAG->getMachineFunction();
4110 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4111 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4112 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4113 RetVal, RetValAddr));
4114 }
4115
4116 // Get the destination value.
4117 SDOperand Callee = TailCallNode->getOperand(1);
4118 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4119 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00004120 unsigned CalleeReg = 0;
Chris Lattnerdd66a412005-05-15 05:46:45 +00004121 if (!isDirect) CalleeReg = SelectExpr(Callee);
4122
4123 unsigned RegOp1 = 0;
4124 unsigned RegOp2 = 0;
4125
4126 if (TailCallNode->getNumOperands() > 4) {
4127 // The first value is passed in (a part of) EAX, the second in EDX.
4128 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4129 if (TailCallNode->getNumOperands() > 5)
4130 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
4131
4132 switch (TailCallNode->getOperand(4).getValueType()) {
4133 default: assert(0 && "Bad thing to pass in regs");
4134 case MVT::i1:
4135 case MVT::i8:
4136 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4137 RegOp1 = X86::AL;
4138 break;
4139 case MVT::i16:
4140 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4141 RegOp1 = X86::AX;
4142 break;
4143 case MVT::i32:
4144 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4145 RegOp1 = X86::EAX;
4146 break;
4147 }
4148 if (RegOp2)
4149 switch (TailCallNode->getOperand(5).getValueType()) {
4150 default: assert(0 && "Bad thing to pass in regs");
4151 case MVT::i1:
4152 case MVT::i8:
4153 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4154 RegOp2 = X86::DL;
4155 break;
4156 case MVT::i16:
4157 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4158 RegOp2 = X86::DX;
4159 break;
4160 case MVT::i32:
4161 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4162 RegOp2 = X86::EDX;
4163 break;
4164 }
4165 }
4166
4167 // Adjust ESP.
4168 if (ESPOffset)
4169 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4170 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4171
4172 // TODO: handle jmp [mem]
4173 if (!isDirect) {
4174 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4175 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00004176 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00004177 } else {
4178 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4179 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4180 }
4181 // ADD IMPLICIT USE RegOp1/RegOp2's
4182}
4183
Chris Lattner96113fd2005-01-17 19:25:26 +00004184
Chris Lattner88c8a232005-01-07 07:49:41 +00004185void ISel::Select(SDOperand N) {
4186 unsigned Tmp1, Tmp2, Opc;
4187
Nate Begeman95210522005-03-24 04:39:54 +00004188 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00004189 return; // Already selected.
4190
Chris Lattner36f78482005-01-11 06:14:36 +00004191 SDNode *Node = N.Val;
4192
4193 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00004194 default:
Chris Lattner36f78482005-01-11 06:14:36 +00004195 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00004196 assert(0 && "Node not handled yet!");
4197 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00004198 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00004199 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004200 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00004201 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4202 Select(Node->getOperand(OneFirst));
4203 Select(Node->getOperand(!OneFirst));
4204 } else {
4205 std::vector<std::pair<unsigned, unsigned> > OpsP;
4206 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4207 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4208 std::sort(OpsP.begin(), OpsP.end());
4209 std::reverse(OpsP.begin(), OpsP.end());
4210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4211 Select(Node->getOperand(OpsP[i].second));
4212 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00004213 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004214 case ISD::CopyToReg:
Chris Lattner2cfce682005-01-12 02:02:48 +00004215 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4216 Select(N.getOperand(0));
4217 Tmp1 = SelectExpr(N.getOperand(1));
4218 } else {
4219 Tmp1 = SelectExpr(N.getOperand(1));
4220 Select(N.getOperand(0));
4221 }
Chris Lattnere727af02005-01-13 20:50:02 +00004222 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00004223
Chris Lattner88c8a232005-01-07 07:49:41 +00004224 if (Tmp1 != Tmp2) {
4225 switch (N.getOperand(1).getValueType()) {
4226 default: assert(0 && "Invalid type for operation!");
4227 case MVT::i1:
4228 case MVT::i8: Opc = X86::MOV8rr; break;
4229 case MVT::i16: Opc = X86::MOV16rr; break;
4230 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004231 case MVT::f32: Opc = X86::MOVAPSrr; break;
4232 case MVT::f64:
4233 if (X86ScalarSSE) {
4234 Opc = X86::MOVAPDrr;
4235 } else {
4236 Opc = X86::FpMOV;
4237 ContainsFPCode = true;
4238 }
4239 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004240 }
4241 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4242 }
4243 return;
4244 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00004245 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4246 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4247 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4248 if (EmitPotentialTailCall(Node))
4249 return;
4250
Chris Lattner88c8a232005-01-07 07:49:41 +00004251 switch (N.getNumOperands()) {
4252 default:
4253 assert(0 && "Unknown return instruction!");
4254 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00004255 assert(N.getOperand(1).getValueType() == MVT::i32 &&
4256 N.getOperand(2).getValueType() == MVT::i32 &&
4257 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004258 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4259 Tmp1 = SelectExpr(N.getOperand(1));
4260 Tmp2 = SelectExpr(N.getOperand(2));
4261 } else {
4262 Tmp2 = SelectExpr(N.getOperand(2));
4263 Tmp1 = SelectExpr(N.getOperand(1));
4264 }
4265 Select(N.getOperand(0));
4266
Chris Lattner88c8a232005-01-07 07:49:41 +00004267 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4268 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00004269 break;
4270 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004271 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4272 Select(N.getOperand(0));
4273 Tmp1 = SelectExpr(N.getOperand(1));
4274 } else {
4275 Tmp1 = SelectExpr(N.getOperand(1));
4276 Select(N.getOperand(0));
4277 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004278 switch (N.getOperand(1).getValueType()) {
4279 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00004280 case MVT::f32:
4281 if (X86ScalarSSE) {
4282 // Spill the value to memory and reload it into top of stack.
4283 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4284 MachineFunction *F = BB->getParent();
4285 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4286 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4287 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4288 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4289 ContainsFPCode = true;
4290 } else {
4291 assert(0 && "MVT::f32 only legal with scalar sse fp");
4292 abort();
4293 }
4294 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004295 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00004296 if (X86ScalarSSE) {
4297 // Spill the value to memory and reload it into top of stack.
4298 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4299 MachineFunction *F = BB->getParent();
4300 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4301 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4302 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4303 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4304 ContainsFPCode = true;
4305 } else {
4306 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4307 }
4308 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004309 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00004310 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4311 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004312 }
4313 break;
4314 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004315 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004316 break;
4317 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004318 if (X86Lowering.getBytesToPopOnReturn() == 0)
4319 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4320 else
4321 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00004322 return;
4323 case ISD::BR: {
4324 Select(N.getOperand(0));
4325 MachineBasicBlock *Dest =
4326 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4327 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4328 return;
4329 }
4330
4331 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00004332 MachineBasicBlock *Dest =
4333 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004334
Chris Lattner88c8a232005-01-07 07:49:41 +00004335 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4336 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00004337 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4338 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4339 Select(N.getOperand(0));
4340 Tmp1 = SelectExpr(N.getOperand(1));
4341 } else {
4342 Tmp1 = SelectExpr(N.getOperand(1));
4343 Select(N.getOperand(0));
4344 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004345 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4346 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4347 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004348
Chris Lattner88c8a232005-01-07 07:49:41 +00004349 return;
4350 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004351
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004352 case ISD::LOAD:
4353 // If this load could be folded into the only using instruction, and if it
4354 // is safe to emit the instruction here, try to do so now.
4355 if (Node->hasNUsesOfValue(1, 0)) {
4356 SDOperand TheVal = N.getValue(0);
4357 SDNode *User = 0;
4358 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4359 assert(UI != Node->use_end() && "Didn't find use!");
4360 SDNode *UN = *UI;
4361 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4362 if (UN->getOperand(i) == TheVal) {
4363 User = UN;
4364 goto FoundIt;
4365 }
4366 }
4367 FoundIt:
4368 // Only handle unary operators right now.
4369 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00004370 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004371 SelectExpr(SDOperand(User, 0));
4372 return;
4373 }
4374 }
Chris Lattner28a205e2005-01-18 04:00:54 +00004375 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004376 SelectExpr(N);
4377 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00004378 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00004379 case ISD::EXTLOAD:
4380 case ISD::SEXTLOAD:
4381 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00004382 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner1b3520c2005-05-14 08:48:15 +00004383 case X86ISD::TAILCALL:
4384 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00004385 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00004386 SelectExpr(N);
4387 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004388 case ISD::CopyFromReg:
Chris Lattnera36117b2005-05-14 06:52:07 +00004389 case X86ISD::FILD64m:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004390 ExprMap.erase(N);
4391 SelectExpr(N.getValue(0));
4392 return;
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:
4432 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
4433 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 }
4488 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
4489 AM).addGlobalAddress(GA->getGlobal());
4490 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004491 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00004492
4493 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00004494 if (TryToFoldLoadOpStore(Node))
4495 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00004496
Chris Lattner88c8a232005-01-07 07:49:41 +00004497 switch (N.getOperand(1).getValueType()) {
4498 default: assert(0 && "Cannot store this type!");
4499 case MVT::i1:
4500 case MVT::i8: Opc = X86::MOV8mr; break;
4501 case MVT::i16: Opc = X86::MOV16mr; break;
4502 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004503 case MVT::f32: Opc = X86::MOVSSmr; break;
4504 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004505 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00004506
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004507 std::vector<std::pair<unsigned, unsigned> > RP;
4508 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4509 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4510 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4511 std::sort(RP.begin(), RP.end());
4512
Chris Lattner80c5b972005-02-23 05:57:21 +00004513 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004514 for (unsigned i = 0; i != 3; ++i)
4515 switch (RP[2-i].second) {
4516 default: assert(0 && "Unknown operand number!");
4517 case 0: Select(N.getOperand(0)); break;
4518 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00004519 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004520 }
4521
Chris Lattner88c8a232005-01-07 07:49:41 +00004522 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4523 return;
4524 }
Chris Lattner2dce7032005-05-12 23:24:06 +00004525 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004526 Select(N.getOperand(0));
4527 // Stack amount
4528 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4529 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4530 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00004531 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00004532 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004533 return;
Chris Lattner36f78482005-01-11 06:14:36 +00004534 case ISD::MEMSET: {
4535 Select(N.getOperand(0)); // Select the chain.
4536 unsigned Align =
4537 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4538 if (Align == 0) Align = 1;
4539
4540 // Turn the byte code into # iterations
4541 unsigned CountReg;
4542 unsigned Opcode;
4543 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4544 unsigned Val = ValC->getValue() & 255;
4545
4546 // If the value is a constant, then we can potentially use larger sets.
4547 switch (Align & 3) {
4548 case 2: // WORD aligned
4549 CountReg = MakeReg(MVT::i32);
4550 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4551 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4552 } else {
4553 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4554 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4555 }
4556 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4557 Opcode = X86::REP_STOSW;
4558 break;
4559 case 0: // DWORD aligned
4560 CountReg = MakeReg(MVT::i32);
4561 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4562 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4563 } else {
4564 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4565 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4566 }
4567 Val = (Val << 8) | Val;
4568 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4569 Opcode = X86::REP_STOSD;
4570 break;
4571 default: // BYTE aligned
4572 CountReg = SelectExpr(Node->getOperand(3));
4573 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4574 Opcode = X86::REP_STOSB;
4575 break;
4576 }
4577 } else {
4578 // If it's not a constant value we are storing, just fall back. We could
4579 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4580 unsigned ValReg = SelectExpr(Node->getOperand(2));
4581 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4582 CountReg = SelectExpr(Node->getOperand(3));
4583 Opcode = X86::REP_STOSB;
4584 }
4585
4586 // No matter what the alignment is, we put the source in ESI, the
4587 // destination in EDI, and the count in ECX.
4588 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4589 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4590 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4591 BuildMI(BB, Opcode, 0);
4592 return;
4593 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004594 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00004595 Select(N.getOperand(0)); // Select the chain.
4596 unsigned Align =
4597 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4598 if (Align == 0) Align = 1;
4599
4600 // Turn the byte code into # iterations
4601 unsigned CountReg;
4602 unsigned Opcode;
4603 switch (Align & 3) {
4604 case 2: // WORD aligned
4605 CountReg = MakeReg(MVT::i32);
4606 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4607 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4608 } else {
4609 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4610 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4611 }
4612 Opcode = X86::REP_MOVSW;
4613 break;
4614 case 0: // DWORD 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()/4);
4618 } else {
4619 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4620 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4621 }
4622 Opcode = X86::REP_MOVSD;
4623 break;
4624 default: // BYTE aligned
4625 CountReg = SelectExpr(Node->getOperand(3));
4626 Opcode = X86::REP_MOVSB;
4627 break;
4628 }
4629
4630 // No matter what the alignment is, we put the source in ESI, the
4631 // destination in EDI, and the count in ECX.
4632 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4633 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4634 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4635 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4636 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4637 BuildMI(BB, Opcode, 0);
4638 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004639 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004640 case ISD::WRITEPORT:
4641 if (Node->getOperand(2).getValueType() != MVT::i16) {
4642 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4643 exit(1);
4644 }
4645 Select(Node->getOperand(0)); // Emit the chain.
4646
4647 Tmp1 = SelectExpr(Node->getOperand(1));
4648 switch (Node->getOperand(1).getValueType()) {
4649 case MVT::i8:
4650 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4651 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4652 break;
4653 case MVT::i16:
4654 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4655 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4656 break;
4657 case MVT::i32:
4658 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4659 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4660 break;
4661 default:
4662 std::cerr << "llvm.writeport: invalid data type for X86 target";
4663 exit(1);
4664 }
4665
4666 // If the port is a single-byte constant, use the immediate form.
4667 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4668 if ((CN->getValue() & 255) == CN->getValue()) {
4669 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4670 return;
4671 }
4672
4673 // Otherwise, move the I/O port address into the DX register.
4674 unsigned Reg = SelectExpr(Node->getOperand(2));
4675 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4676 BuildMI(BB, Opc, 0);
4677 return;
4678 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004679 assert(0 && "Should not be reached!");
4680}
4681
4682
4683/// createX86PatternInstructionSelector - This pass converts an LLVM function
4684/// into a machine code representation using pattern matching and a machine
4685/// description file.
4686///
4687FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004688 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00004689}