blob: c9f056b3685bfdfc084265bc71d4bef734f112d8 [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000025#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/Support/CallSite.h"
Dan Gohman35893082008-09-18 23:23:44 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000031
32using namespace llvm;
33
34class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000038
39 /// StackPtr - Register used as the stack pointer.
40 ///
41 unsigned StackPtr;
42
43 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
44 /// floating point ops.
45 /// When SSE is available, use it for f32 operations.
46 /// When SSE2 is available, use it for f64 operations.
47 bool X86ScalarSSEf64;
48 bool X86ScalarSSEf32;
49
Evan Cheng8b19e562008-09-03 06:44:39 +000050public:
Dan Gohman3df24e62008-09-03 23:12:08 +000051 explicit X86FastISel(MachineFunction &mf,
52 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000053 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
54 DenseMap<const AllocaInst *, int> &am)
55 : FastISel(mf, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000056 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000057 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
58 X86ScalarSSEf64 = Subtarget->hasSSE2();
59 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000060 }
Evan Chengc3f44b02008-09-03 00:03:49 +000061
Dan Gohman3df24e62008-09-03 23:12:08 +000062 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman1adf1b02008-08-19 21:45:35 +000064#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000065
66private:
Dan Gohman0586d912008-09-10 20:11:02 +000067 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000068
Evan Chengf3d4efe2008-09-07 09:09:33 +000069 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000070 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000071
72 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
73 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000074
Evan Cheng59fbc802008-09-09 01:26:59 +000075 bool X86SelectConstAddr(Value *V, unsigned &Op0,
76 bool isCall = false, bool inReg = false);
Evan Cheng8b19e562008-09-03 06:44:39 +000077
Dan Gohman0586d912008-09-10 20:11:02 +000078 bool X86SelectAddress(Value *V, X86AddressMode &AM);
79
Dan Gohman3df24e62008-09-03 23:12:08 +000080 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000081
82 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000083
84 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000085
86 bool X86SelectZExt(Instruction *I);
87
88 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000089
90 bool X86SelectShift(Instruction *I);
91
92 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000093
Evan Cheng10a8d9c2008-09-07 08:47:42 +000094 bool X86SelectTrunc(Instruction *I);
95
Dan Gohman78efce62008-09-10 21:02:08 +000096 bool X86SelectFPExt(Instruction *I);
97 bool X86SelectFPTrunc(Instruction *I);
98
Evan Chengf3d4efe2008-09-07 09:09:33 +000099 bool X86SelectCall(Instruction *I);
100
101 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
102
Dan Gohman0586d912008-09-10 20:11:02 +0000103 unsigned TargetMaterializeConstant(Constant *C);
104
105 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000106
107 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
108 /// computed in an SSE register, not on the X87 floating point stack.
109 bool isScalarFPTypeInSSEReg(MVT VT) const {
110 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
111 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
112 }
113
Evan Chengc3f44b02008-09-03 00:03:49 +0000114};
Dan Gohman99b21822008-08-28 23:21:34 +0000115
Evan Chengdebdea02008-09-08 17:15:42 +0000116static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
117 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
119 if (VT == MVT::Other || !VT.isSimple())
120 // Unhandled type. Halt "fast" selection and bail.
121 return false;
122 if (VT == MVT::iPTR)
123 // Use pointer type.
124 VT = TLI.getPointerTy();
125 // We only handle legal types. For example, on x86-32 the instruction
126 // selector contains all of the 64-bit instructions from x86-64,
127 // under the assumption that i64 won't be used if the target doesn't
128 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000129 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130}
131
132#include "X86GenCallingConv.inc"
133
134/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
135/// convention.
136CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
137 if (Subtarget->is64Bit()) {
138 if (Subtarget->isTargetWin64())
139 return CC_X86_Win64_C;
140 else if (CC == CallingConv::Fast && isTaillCall)
141 return CC_X86_64_TailCall;
142 else
143 return CC_X86_64_C;
144 }
145
146 if (CC == CallingConv::X86_FastCall)
147 return CC_X86_32_FastCall;
148 else if (CC == CallingConv::Fast && isTaillCall)
149 return CC_X86_32_TailCall;
150 else if (CC == CallingConv::Fast)
151 return CC_X86_32_FastCC;
152 else
153 return CC_X86_32_C;
154}
155
Evan Cheng0de588f2008-09-05 21:00:03 +0000156/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000157/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000158/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000159bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000160 unsigned &ResultReg) {
161 // Get opcode and regclass of the output for the given load instruction.
162 unsigned Opc = 0;
163 const TargetRegisterClass *RC = NULL;
164 switch (VT.getSimpleVT()) {
165 default: return false;
166 case MVT::i8:
167 Opc = X86::MOV8rm;
168 RC = X86::GR8RegisterClass;
169 break;
170 case MVT::i16:
171 Opc = X86::MOV16rm;
172 RC = X86::GR16RegisterClass;
173 break;
174 case MVT::i32:
175 Opc = X86::MOV32rm;
176 RC = X86::GR32RegisterClass;
177 break;
178 case MVT::i64:
179 // Must be in x86-64 mode.
180 Opc = X86::MOV64rm;
181 RC = X86::GR64RegisterClass;
182 break;
183 case MVT::f32:
184 if (Subtarget->hasSSE1()) {
185 Opc = X86::MOVSSrm;
186 RC = X86::FR32RegisterClass;
187 } else {
188 Opc = X86::LD_Fp32m;
189 RC = X86::RFP32RegisterClass;
190 }
191 break;
192 case MVT::f64:
193 if (Subtarget->hasSSE2()) {
194 Opc = X86::MOVSDrm;
195 RC = X86::FR64RegisterClass;
196 } else {
197 Opc = X86::LD_Fp64m;
198 RC = X86::RFP64RegisterClass;
199 }
200 break;
201 case MVT::f80:
202 Opc = X86::LD_Fp80m;
203 RC = X86::RFP80RegisterClass;
204 break;
205 }
206
207 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
209 return true;
210}
211
Evan Chengf3d4efe2008-09-07 09:09:33 +0000212/// X86FastEmitStore - Emit a machine instruction to store a value Val of
213/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
214/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000215/// i.e. V. Return true if it is possible.
216bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000217X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000218 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000219 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 unsigned Opc = 0;
221 const TargetRegisterClass *RC = NULL;
222 switch (VT.getSimpleVT()) {
223 default: return false;
224 case MVT::i8:
225 Opc = X86::MOV8mr;
226 RC = X86::GR8RegisterClass;
227 break;
228 case MVT::i16:
229 Opc = X86::MOV16mr;
230 RC = X86::GR16RegisterClass;
231 break;
232 case MVT::i32:
233 Opc = X86::MOV32mr;
234 RC = X86::GR32RegisterClass;
235 break;
236 case MVT::i64:
237 // Must be in x86-64 mode.
238 Opc = X86::MOV64mr;
239 RC = X86::GR64RegisterClass;
240 break;
241 case MVT::f32:
242 if (Subtarget->hasSSE1()) {
243 Opc = X86::MOVSSmr;
244 RC = X86::FR32RegisterClass;
245 } else {
246 Opc = X86::ST_Fp32m;
247 RC = X86::RFP32RegisterClass;
248 }
249 break;
250 case MVT::f64:
251 if (Subtarget->hasSSE2()) {
252 Opc = X86::MOVSDmr;
253 RC = X86::FR64RegisterClass;
254 } else {
255 Opc = X86::ST_Fp64m;
256 RC = X86::RFP64RegisterClass;
257 }
258 break;
259 case MVT::f80:
260 Opc = X86::ST_FP80m;
261 RC = X86::RFP80RegisterClass;
262 break;
263 }
264
Evan Chengf3d4efe2008-09-07 09:09:33 +0000265 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000266 return true;
267}
268
Evan Cheng24e3a902008-09-08 06:35:17 +0000269/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
270/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
271/// ISD::SIGN_EXTEND).
272bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
273 unsigned Src, MVT SrcVT,
274 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000275 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
276
277 if (RR != 0) {
278 ResultReg = RR;
279 return true;
280 } else
281 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000282}
283
Evan Cheng8b19e562008-09-03 06:44:39 +0000284/// X86SelectConstAddr - Select and emit code to materialize constant address.
285///
Evan Cheng59fbc802008-09-09 01:26:59 +0000286bool X86FastISel::X86SelectConstAddr(Value *V, unsigned &Op0,
287 bool isCall, bool inReg) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000288 // FIXME: Only GlobalAddress for now.
289 GlobalValue *GV = dyn_cast<GlobalValue>(V);
290 if (!GV)
291 return false;
292
Evan Chengf3d4efe2008-09-07 09:09:33 +0000293 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000294 // Issue load from stub if necessary.
295 unsigned Opc = 0;
296 const TargetRegisterClass *RC = NULL;
297 if (TLI.getPointerTy() == MVT::i32) {
298 Opc = X86::MOV32rm;
299 RC = X86::GR32RegisterClass;
300 } else {
301 Opc = X86::MOV64rm;
302 RC = X86::GR64RegisterClass;
303 }
304 Op0 = createResultReg(RC);
305 X86AddressMode AM;
306 AM.GV = GV;
307 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
Evan Cheng373d50a2008-09-04 06:18:33 +0000308 // Prevent loading GV stub multiple times in same MBB.
309 LocalValueMap[V] = Op0;
Evan Cheng59fbc802008-09-09 01:26:59 +0000310 } else if (inReg) {
311 unsigned Opc = 0;
312 const TargetRegisterClass *RC = NULL;
313 if (TLI.getPointerTy() == MVT::i32) {
314 Opc = X86::LEA32r;
315 RC = X86::GR32RegisterClass;
316 } else {
317 Opc = X86::LEA64r;
318 RC = X86::GR64RegisterClass;
319 }
320 Op0 = createResultReg(RC);
321 X86AddressMode AM;
322 AM.GV = GV;
323 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
324 // Prevent materializing GV address multiple times in same MBB.
325 LocalValueMap[V] = Op0;
Evan Cheng8b19e562008-09-03 06:44:39 +0000326 }
Evan Cheng59fbc802008-09-09 01:26:59 +0000327
Evan Cheng8b19e562008-09-03 06:44:39 +0000328 return true;
329}
330
Dan Gohman0586d912008-09-10 20:11:02 +0000331/// X86SelectAddress - Attempt to fill in an address from the given value.
332///
333bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Dan Gohman35893082008-09-18 23:23:44 +0000334 User *U;
335 unsigned Opcode = Instruction::UserOp1;
336 if (Instruction *I = dyn_cast<Instruction>(V)) {
337 Opcode = I->getOpcode();
338 U = I;
339 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
340 Opcode = C->getOpcode();
341 U = C;
342 }
Dan Gohman0586d912008-09-10 20:11:02 +0000343
Dan Gohman35893082008-09-18 23:23:44 +0000344 switch (Opcode) {
345 default: break;
346 case Instruction::BitCast:
347 // Look past bitcasts.
348 return X86SelectAddress(U->getOperand(0), AM);
349
350 case Instruction::IntToPtr:
351 // Look past no-op inttoptrs.
352 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
353 return X86SelectAddress(U->getOperand(0), AM);
354
355 case Instruction::PtrToInt:
356 // Look past no-op ptrtoints.
357 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
358 return X86SelectAddress(U->getOperand(0), AM);
359
360 case Instruction::Alloca: {
361 // Do static allocas.
362 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000363 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
364 if (SI == StaticAllocaMap.end())
365 return false;
366 AM.BaseType = X86AddressMode::FrameIndexBase;
367 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000368 return true;
369 }
370
371 case Instruction::Add: {
372 // Adds of constants are common and easy enough.
373 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
374 AM.Disp += CI->getZExtValue();
375 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman0586d912008-09-10 20:11:02 +0000376 }
Dan Gohman35893082008-09-18 23:23:44 +0000377 break;
378 }
379
380 case Instruction::GetElementPtr: {
381 // Pattern-match simple GEPs.
382 uint64_t Disp = AM.Disp;
383 unsigned IndexReg = AM.IndexReg;
384 unsigned Scale = AM.Scale;
385 gep_type_iterator GTI = gep_type_begin(U);
386 // Look at all but the last index. Constants can be folded,
387 // and one dynamic index can be handled, if the scale is supported.
388 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
389 i != e; ++i, ++GTI) {
390 Value *Op = *i;
391 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
392 const StructLayout *SL = TD.getStructLayout(STy);
393 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
394 Disp += SL->getElementOffset(Idx);
395 } else {
396 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
397 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
398 // Constant-offset addressing.
399 Disp += CI->getZExtValue() * S;
400 } else if (IndexReg == 0 &&
401 (S == 1 || S == 2 || S == 4 || S == 8)) {
402 // Scaled-index addressing.
403 Scale = S;
404 IndexReg = getRegForValue(Op);
405 if (IndexReg == 0)
406 return false;
407 } else
408 // Unsupported.
409 goto unsupported_gep;
410 }
411 }
412 // Ok, the GEP indices were covered by constant-offset and scaled-index
413 // addressing. Update the address state and move on to examining the base.
414 AM.IndexReg = IndexReg;
415 AM.Scale = Scale;
416 AM.Disp = Disp;
417 return X86SelectAddress(U->getOperand(0), AM);
418 unsupported_gep:
419 // Ok, the GEP indices weren't all covered.
420 break;
421 }
422 }
423
424 // Handle constant address.
425 // FIXME: If load type is something we can't handle, this can result in
426 // a dead stub load instruction.
427 if (isa<Constant>(V) && X86SelectConstAddr(V, AM.Base.Reg)) {
428 if (AM.Base.Reg == 0)
429 AM.GV = cast<GlobalValue>(V);
430 } else {
431 AM.Base.Reg = getRegForValue(V);
432 if (AM.Base.Reg == 0)
433 // Unhandled operand. Halt "fast" selection and bail.
434 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000435 }
436
437 return true;
438}
439
Owen Andersona3971df2008-09-04 07:08:58 +0000440/// X86SelectStore - Select and emit code to implement store instructions.
441bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000442 MVT VT;
443 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000444 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000445 unsigned Val = getRegForValue(I->getOperand(0));
446 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000447 // Unhandled operand. Halt "fast" selection and bail.
448 return false;
449
Dan Gohman0586d912008-09-10 20:11:02 +0000450 X86AddressMode AM;
451 if (!X86SelectAddress(I->getOperand(1), AM))
452 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000453
Dan Gohman0586d912008-09-10 20:11:02 +0000454 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000455}
456
Evan Cheng8b19e562008-09-03 06:44:39 +0000457/// X86SelectLoad - Select and emit code to implement load instructions.
458///
Dan Gohman3df24e62008-09-03 23:12:08 +0000459bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000460 MVT VT;
461 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000462 return false;
463
Dan Gohman0586d912008-09-10 20:11:02 +0000464 X86AddressMode AM;
465 if (!X86SelectAddress(I->getOperand(0), AM))
466 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000467
Evan Cheng0de588f2008-09-05 21:00:03 +0000468 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000469 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000470 UpdateValueMap(I, ResultReg);
471 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000472 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000473 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000474}
475
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000476bool X86FastISel::X86SelectCmp(Instruction *I) {
477 CmpInst *CI = cast<CmpInst>(I);
478
Dan Gohman4f22bb02008-09-05 01:33:56 +0000479 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
480 if (!TLI.isTypeLegal(VT))
481 return false;
482
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000483 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000484 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000485 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000486 if (Op1Reg == 0) return false;
487
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000488 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000489 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000490 case MVT::i8: Opc = X86::CMP8rr; break;
491 case MVT::i16: Opc = X86::CMP16rr; break;
492 case MVT::i32: Opc = X86::CMP32rr; break;
493 case MVT::i64: Opc = X86::CMP64rr; break;
494 case MVT::f32: Opc = X86::UCOMISSrr; break;
495 case MVT::f64: Opc = X86::UCOMISDrr; break;
496 default: return false;
497 }
498
499 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
500 switch (CI->getPredicate()) {
501 case CmpInst::FCMP_OEQ: {
502 unsigned EReg = createResultReg(&X86::GR8RegClass);
503 unsigned NPReg = createResultReg(&X86::GR8RegClass);
504 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
505 BuildMI(MBB, TII.get(X86::SETEr), EReg);
506 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
507 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
508 break;
509 }
510 case CmpInst::FCMP_UNE: {
511 unsigned NEReg = createResultReg(&X86::GR8RegClass);
512 unsigned PReg = createResultReg(&X86::GR8RegClass);
513 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
514 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
515 BuildMI(MBB, TII.get(X86::SETPr), PReg);
516 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
517 break;
518 }
519 case CmpInst::FCMP_OGT:
520 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
521 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
522 break;
523 case CmpInst::FCMP_OGE:
524 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
525 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
526 break;
527 case CmpInst::FCMP_OLT:
528 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
529 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
530 break;
531 case CmpInst::FCMP_OLE:
532 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
533 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
534 break;
535 case CmpInst::FCMP_ONE:
536 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
537 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
538 break;
539 case CmpInst::FCMP_ORD:
540 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
541 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
542 break;
543 case CmpInst::FCMP_UNO:
544 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
545 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
546 break;
547 case CmpInst::FCMP_UEQ:
548 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
549 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
550 break;
551 case CmpInst::FCMP_UGT:
552 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
553 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
554 break;
555 case CmpInst::FCMP_UGE:
556 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
557 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
558 break;
559 case CmpInst::FCMP_ULT:
560 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
561 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
562 break;
563 case CmpInst::FCMP_ULE:
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
566 break;
567 case CmpInst::ICMP_EQ:
568 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
569 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
570 break;
571 case CmpInst::ICMP_NE:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
573 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
574 break;
575 case CmpInst::ICMP_UGT:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
577 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
578 break;
579 case CmpInst::ICMP_UGE:
580 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
581 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
582 break;
583 case CmpInst::ICMP_ULT:
584 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
585 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
586 break;
587 case CmpInst::ICMP_ULE:
588 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
589 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
590 break;
591 case CmpInst::ICMP_SGT:
592 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
593 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
594 break;
595 case CmpInst::ICMP_SGE:
596 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
597 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
598 break;
599 case CmpInst::ICMP_SLT:
600 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
601 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
602 break;
603 case CmpInst::ICMP_SLE:
604 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
605 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
606 break;
607 default:
608 return false;
609 }
610
611 UpdateValueMap(I, ResultReg);
612 return true;
613}
Evan Cheng8b19e562008-09-03 06:44:39 +0000614
Dan Gohmand89ae992008-09-05 01:06:14 +0000615bool X86FastISel::X86SelectZExt(Instruction *I) {
616 // Special-case hack: The only i1 values we know how to produce currently
617 // set the upper bits of an i8 value to zero.
618 if (I->getType() == Type::Int8Ty &&
619 I->getOperand(0)->getType() == Type::Int1Ty) {
620 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000621 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000622 UpdateValueMap(I, ResultReg);
623 return true;
624 }
625
626 return false;
627}
628
629bool X86FastISel::X86SelectBranch(Instruction *I) {
630 BranchInst *BI = cast<BranchInst>(I);
631 // Unconditional branches are selected by tablegen-generated code.
632 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000633 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000634 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
635 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
636
637 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
638 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
639 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
640
641 MBB->addSuccessor(TrueMBB);
642 MBB->addSuccessor(FalseMBB);
643
644 return true;
645}
646
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000647bool X86FastISel::X86SelectShift(Instruction *I) {
648 unsigned CReg = 0;
649 unsigned Opc = 0;
650 const TargetRegisterClass *RC = NULL;
651 if (I->getType() == Type::Int8Ty) {
652 CReg = X86::CL;
653 RC = &X86::GR8RegClass;
654 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000655 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000656 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000657 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000658 default: return false;
659 }
660 } else if (I->getType() == Type::Int16Ty) {
661 CReg = X86::CX;
662 RC = &X86::GR16RegClass;
663 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000664 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000665 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000666 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000667 default: return false;
668 }
669 } else if (I->getType() == Type::Int32Ty) {
670 CReg = X86::ECX;
671 RC = &X86::GR32RegClass;
672 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000673 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000674 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000675 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000676 default: return false;
677 }
678 } else if (I->getType() == Type::Int64Ty) {
679 CReg = X86::RCX;
680 RC = &X86::GR64RegClass;
681 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000682 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000683 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000684 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000685 default: return false;
686 }
687 } else {
688 return false;
689 }
690
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000691 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
692 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
693 return false;
694
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000695 unsigned Op0Reg = getRegForValue(I->getOperand(0));
696 if (Op0Reg == 0) return false;
697 unsigned Op1Reg = getRegForValue(I->getOperand(1));
698 if (Op1Reg == 0) return false;
699 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
700 unsigned ResultReg = createResultReg(RC);
701 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
702 UpdateValueMap(I, ResultReg);
703 return true;
704}
705
706bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000707 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000708 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000709 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000710
711 unsigned Opc = 0;
712 const TargetRegisterClass *RC = NULL;
713 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000714 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000715 RC = &X86::GR16RegClass;
716 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000717 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000718 RC = &X86::GR32RegClass;
719 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000720 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000721 RC = &X86::GR64RegClass;
722 } else {
723 return false;
724 }
725
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000726 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
727 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
728 return false;
729
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000730 unsigned Op0Reg = getRegForValue(I->getOperand(0));
731 if (Op0Reg == 0) return false;
732 unsigned Op1Reg = getRegForValue(I->getOperand(1));
733 if (Op1Reg == 0) return false;
734 unsigned Op2Reg = getRegForValue(I->getOperand(2));
735 if (Op2Reg == 0) return false;
736
737 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
738 unsigned ResultReg = createResultReg(RC);
739 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
740 UpdateValueMap(I, ResultReg);
741 return true;
742}
743
Dan Gohman78efce62008-09-10 21:02:08 +0000744bool X86FastISel::X86SelectFPExt(Instruction *I) {
745 if (Subtarget->hasSSE2()) {
746 if (I->getType() == Type::DoubleTy) {
747 Value *V = I->getOperand(0);
748 if (V->getType() == Type::FloatTy) {
749 unsigned OpReg = getRegForValue(V);
750 if (OpReg == 0) return false;
751 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
752 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
753 UpdateValueMap(I, ResultReg);
754 return true;
755 }
756 }
757 }
758
759 return false;
760}
761
762bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
763 if (Subtarget->hasSSE2()) {
764 if (I->getType() == Type::FloatTy) {
765 Value *V = I->getOperand(0);
766 if (V->getType() == Type::DoubleTy) {
767 unsigned OpReg = getRegForValue(V);
768 if (OpReg == 0) return false;
769 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
770 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
771 UpdateValueMap(I, ResultReg);
772 return true;
773 }
774 }
775 }
776
777 return false;
778}
779
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000780bool X86FastISel::X86SelectTrunc(Instruction *I) {
781 if (Subtarget->is64Bit())
782 // All other cases should be handled by the tblgen generated code.
783 return false;
784 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
785 MVT DstVT = TLI.getValueType(I->getType());
786 if (DstVT != MVT::i8)
787 // All other cases should be handled by the tblgen generated code.
788 return false;
789 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
790 // All other cases should be handled by the tblgen generated code.
791 return false;
792
793 unsigned InputReg = getRegForValue(I->getOperand(0));
794 if (!InputReg)
795 // Unhandled operand. Halt "fast" selection and bail.
796 return false;
797
798 // First issue a copy to GR16_ or GR32_.
799 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
800 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
801 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
802 unsigned CopyReg = createResultReg(CopyRC);
803 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
804
805 // Then issue an extract_subreg.
806 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
807 if (!ResultReg)
808 return false;
809
810 UpdateValueMap(I, ResultReg);
811 return true;
812}
813
Evan Chengf3d4efe2008-09-07 09:09:33 +0000814bool X86FastISel::X86SelectCall(Instruction *I) {
815 CallInst *CI = cast<CallInst>(I);
816 Value *Callee = I->getOperand(0);
817
818 // Can't handle inline asm yet.
819 if (isa<InlineAsm>(Callee))
820 return false;
821
822 // FIXME: Handle some intrinsics.
823 if (Function *F = CI->getCalledFunction()) {
824 if (F->isDeclaration() &&F->getIntrinsicID())
825 return false;
826 }
827
Evan Chengf3d4efe2008-09-07 09:09:33 +0000828 // Handle only C and fastcc calling conventions for now.
829 CallSite CS(CI);
830 unsigned CC = CS.getCallingConv();
831 if (CC != CallingConv::C &&
832 CC != CallingConv::Fast &&
833 CC != CallingConv::X86_FastCall)
834 return false;
835
836 // Let SDISel handle vararg functions.
837 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
838 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
839 if (FTy->isVarArg())
840 return false;
841
842 // Handle *simple* calls for now.
843 const Type *RetTy = CS.getType();
844 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000845 if (RetTy == Type::VoidTy)
846 RetVT = MVT::isVoid;
847 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000848 return false;
849
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000850 // Materialize callee address in a register. FIXME: GV address can be
851 // handled with a CALLpcrel32 instead.
852 unsigned CalleeOp = 0;
853 if (!isa<Constant>(Callee) || !X86SelectConstAddr(Callee, CalleeOp, true)) {
854 CalleeOp = getRegForValue(Callee);
855 if (CalleeOp == 0)
856 // Unhandled operand. Halt "fast" selection and bail.
857 return false;
858 }
859
Evan Chengdebdea02008-09-08 17:15:42 +0000860 // Allow calls which produce i1 results.
861 bool AndToI1 = false;
862 if (RetVT == MVT::i1) {
863 RetVT = MVT::i8;
864 AndToI1 = true;
865 }
866
Evan Chengf3d4efe2008-09-07 09:09:33 +0000867 // Deal with call operands first.
868 SmallVector<unsigned, 4> Args;
869 SmallVector<MVT, 4> ArgVTs;
870 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
871 Args.reserve(CS.arg_size());
872 ArgVTs.reserve(CS.arg_size());
873 ArgFlags.reserve(CS.arg_size());
874 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
875 i != e; ++i) {
876 unsigned Arg = getRegForValue(*i);
877 if (Arg == 0)
878 return false;
879 ISD::ArgFlagsTy Flags;
880 unsigned AttrInd = i - CS.arg_begin() + 1;
881 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
882 Flags.setSExt();
883 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
884 Flags.setZExt();
885
886 // FIXME: Only handle *easy* calls for now.
887 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
888 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
889 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
890 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
891 return false;
892
893 const Type *ArgTy = (*i)->getType();
894 MVT ArgVT;
895 if (!isTypeLegal(ArgTy, TLI, ArgVT))
896 return false;
897 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
898 Flags.setOrigAlign(OriginalAlignment);
899
900 Args.push_back(Arg);
901 ArgVTs.push_back(ArgVT);
902 ArgFlags.push_back(Flags);
903 }
904
905 // Analyze operands of the call, assigning locations to each operand.
906 SmallVector<CCValAssign, 16> ArgLocs;
907 CCState CCInfo(CC, false, TM, ArgLocs);
908 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
909
910 // Get a count of how many bytes are to be pushed on the stack.
911 unsigned NumBytes = CCInfo.getNextStackOffset();
912
913 // Issue CALLSEQ_START
914 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
915
916 // Process argumenet: walk the register/memloc assignments, inserting
917 // copies / loads.
918 SmallVector<unsigned, 4> RegArgs;
919 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
920 CCValAssign &VA = ArgLocs[i];
921 unsigned Arg = Args[VA.getValNo()];
922 MVT ArgVT = ArgVTs[VA.getValNo()];
923
924 // Promote the value if needed.
925 switch (VA.getLocInfo()) {
926 default: assert(0 && "Unknown loc info!");
927 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000928 case CCValAssign::SExt: {
929 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
930 Arg, ArgVT, Arg);
931 assert(Emitted && "Failed to emit a sext!");
932 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000933 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000934 }
935 case CCValAssign::ZExt: {
936 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
937 Arg, ArgVT, Arg);
938 assert(Emitted && "Failed to emit a zext!");
939 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000940 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000941 }
942 case CCValAssign::AExt: {
943 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
944 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000945 if (!Emitted)
946 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
947 Arg, ArgVT, Arg);
948 if (!Emitted)
949 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
950 Arg, ArgVT, Arg);
951
Evan Cheng24e3a902008-09-08 06:35:17 +0000952 assert(Emitted && "Failed to emit a aext!");
953 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000954 break;
955 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000956 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000957
958 if (VA.isRegLoc()) {
959 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
960 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
961 Arg, RC, RC);
962 assert(Emitted && "Failed to emit a copy instruction!");
963 RegArgs.push_back(VA.getLocReg());
964 } else {
965 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000966 X86AddressMode AM;
967 AM.Base.Reg = StackPtr;
968 AM.Disp = LocMemOffset;
969 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000970 }
971 }
972
973 // Issue the call.
974 unsigned CallOpc = CalleeOp
975 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
976 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
977 MachineInstrBuilder MIB = CalleeOp
978 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
979 :BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(cast<GlobalValue>(Callee));
980 // Add implicit physical register uses to the call.
981 while (!RegArgs.empty()) {
982 MIB.addReg(RegArgs.back());
983 RegArgs.pop_back();
984 }
985
986 // Issue CALLSEQ_END
987 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
988
989 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000990 if (RetVT.getSimpleVT() != MVT::isVoid) {
991 SmallVector<CCValAssign, 16> RVLocs;
992 CCState CCInfo(CC, false, TM, RVLocs);
993 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
994
995 // Copy all of the result registers out of their specified physreg.
996 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
997 MVT CopyVT = RVLocs[0].getValVT();
998 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
999 TargetRegisterClass *SrcRC = DstRC;
1000
1001 // If this is a call to a function that returns an fp value on the x87 fp
1002 // stack, but where we prefer to use the value in xmm registers, copy it
1003 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1004 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1005 RVLocs[0].getLocReg() == X86::ST1) &&
1006 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1007 CopyVT = MVT::f80;
1008 SrcRC = X86::RSTRegisterClass;
1009 DstRC = X86::RFP80RegisterClass;
1010 }
1011
1012 unsigned ResultReg = createResultReg(DstRC);
1013 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1014 RVLocs[0].getLocReg(), DstRC, SrcRC);
1015 assert(Emitted && "Failed to emit a copy instruction!");
1016 if (CopyVT != RVLocs[0].getValVT()) {
1017 // Round the F80 the right size, which also moves to the appropriate xmm
1018 // register. This is accomplished by storing the F80 value in memory and
1019 // then loading it back. Ewww...
1020 MVT ResVT = RVLocs[0].getValVT();
1021 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1022 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001023 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001024 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1025 DstRC = ResVT == MVT::f32
1026 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1027 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1028 ResultReg = createResultReg(DstRC);
1029 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1030 }
1031
Evan Chengdebdea02008-09-08 17:15:42 +00001032 if (AndToI1) {
1033 // Mask out all but lowest bit for some call which produces an i1.
1034 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1035 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1036 ResultReg = AndResult;
1037 }
1038
Evan Chengf3d4efe2008-09-07 09:09:33 +00001039 UpdateValueMap(I, ResultReg);
1040 }
1041
1042 return true;
1043}
1044
1045
Dan Gohman99b21822008-08-28 23:21:34 +00001046bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001047X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001048 switch (I->getOpcode()) {
1049 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001050 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001051 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001052 case Instruction::Store:
1053 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001054 case Instruction::ICmp:
1055 case Instruction::FCmp:
1056 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001057 case Instruction::ZExt:
1058 return X86SelectZExt(I);
1059 case Instruction::Br:
1060 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001061 case Instruction::Call:
1062 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001063 case Instruction::LShr:
1064 case Instruction::AShr:
1065 case Instruction::Shl:
1066 return X86SelectShift(I);
1067 case Instruction::Select:
1068 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001069 case Instruction::Trunc:
1070 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001071 case Instruction::FPExt:
1072 return X86SelectFPExt(I);
1073 case Instruction::FPTrunc:
1074 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001075 }
1076
1077 return false;
1078}
1079
Dan Gohman0586d912008-09-10 20:11:02 +00001080unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001081 // Can't handle PIC-mode yet.
1082 if (TM.getRelocationModel() == Reloc::PIC_)
1083 return 0;
1084
Evan Cheng59fbc802008-09-09 01:26:59 +00001085 MVT VT;
1086 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001087 return false;
1088
1089 // Get opcode and regclass of the output for the given load instruction.
1090 unsigned Opc = 0;
1091 const TargetRegisterClass *RC = NULL;
1092 switch (VT.getSimpleVT()) {
1093 default: return false;
1094 case MVT::i8:
1095 Opc = X86::MOV8rm;
1096 RC = X86::GR8RegisterClass;
1097 break;
1098 case MVT::i16:
1099 Opc = X86::MOV16rm;
1100 RC = X86::GR16RegisterClass;
1101 break;
1102 case MVT::i32:
1103 Opc = X86::MOV32rm;
1104 RC = X86::GR32RegisterClass;
1105 break;
1106 case MVT::i64:
1107 // Must be in x86-64 mode.
1108 Opc = X86::MOV64rm;
1109 RC = X86::GR64RegisterClass;
1110 break;
1111 case MVT::f32:
1112 if (Subtarget->hasSSE1()) {
1113 Opc = X86::MOVSSrm;
1114 RC = X86::FR32RegisterClass;
1115 } else {
1116 Opc = X86::LD_Fp32m;
1117 RC = X86::RFP32RegisterClass;
1118 }
1119 break;
1120 case MVT::f64:
1121 if (Subtarget->hasSSE2()) {
1122 Opc = X86::MOVSDrm;
1123 RC = X86::FR64RegisterClass;
1124 } else {
1125 Opc = X86::LD_Fp64m;
1126 RC = X86::RFP64RegisterClass;
1127 }
1128 break;
1129 case MVT::f80:
1130 Opc = X86::LD_Fp80m;
1131 RC = X86::RFP80RegisterClass;
1132 break;
1133 }
1134
1135 unsigned ResultReg = createResultReg(RC);
1136 if (isa<GlobalValue>(C)) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001137 if (X86SelectConstAddr(C, ResultReg, false, true))
Owen Anderson95267a12008-09-05 00:06:23 +00001138 return ResultReg;
Evan Cheng0de588f2008-09-05 21:00:03 +00001139 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001140 }
1141
Owen Anderson3b217c62008-09-06 01:11:01 +00001142 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001143 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001144 if (Align == 0) {
1145 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001146 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001147 Align = Log2_64(Align);
1148 }
Owen Anderson95267a12008-09-05 00:06:23 +00001149
Dan Gohman0586d912008-09-10 20:11:02 +00001150 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Owen Anderson95267a12008-09-05 00:06:23 +00001151 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001152 return ResultReg;
1153}
1154
Dan Gohman0586d912008-09-10 20:11:02 +00001155unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1156 X86AddressMode AM;
1157 if (!X86SelectAddress(C, AM))
1158 return 0;
1159 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1160 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1161 unsigned ResultReg = createResultReg(RC);
1162 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1163 return ResultReg;
1164}
1165
Evan Chengc3f44b02008-09-03 00:03:49 +00001166namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001167 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1168 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001169 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1170 DenseMap<const AllocaInst *, int> &am) {
1171 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001172 }
Dan Gohman99b21822008-08-28 23:21:34 +00001173}