blob: 3d27ff32324aef79c55597dafddfa7d90df0a9eb [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
Dan Gohman2ff7fd12008-09-19 22:16:54 +000075 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000076
Dan Gohman3df24e62008-09-03 23:12:08 +000077 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000078
79 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000080
81 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000082
83 bool X86SelectZExt(Instruction *I);
84
85 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000086
87 bool X86SelectShift(Instruction *I);
88
89 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000090
Evan Cheng10a8d9c2008-09-07 08:47:42 +000091 bool X86SelectTrunc(Instruction *I);
92
Dan Gohman78efce62008-09-10 21:02:08 +000093 bool X86SelectFPExt(Instruction *I);
94 bool X86SelectFPTrunc(Instruction *I);
95
Evan Chengf3d4efe2008-09-07 09:09:33 +000096 bool X86SelectCall(Instruction *I);
97
98 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
99
Dan Gohman0586d912008-09-10 20:11:02 +0000100 unsigned TargetMaterializeConstant(Constant *C);
101
102 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000103
104 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
105 /// computed in an SSE register, not on the X87 floating point stack.
106 bool isScalarFPTypeInSSEReg(MVT VT) const {
107 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
108 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
109 }
110
Evan Chengc3f44b02008-09-03 00:03:49 +0000111};
Dan Gohman99b21822008-08-28 23:21:34 +0000112
Evan Chengdebdea02008-09-08 17:15:42 +0000113static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
114 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
116 if (VT == MVT::Other || !VT.isSimple())
117 // Unhandled type. Halt "fast" selection and bail.
118 return false;
119 if (VT == MVT::iPTR)
120 // Use pointer type.
121 VT = TLI.getPointerTy();
122 // We only handle legal types. For example, on x86-32 the instruction
123 // selector contains all of the 64-bit instructions from x86-64,
124 // under the assumption that i64 won't be used if the target doesn't
125 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000126 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127}
128
129#include "X86GenCallingConv.inc"
130
131/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
132/// convention.
133CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
134 if (Subtarget->is64Bit()) {
135 if (Subtarget->isTargetWin64())
136 return CC_X86_Win64_C;
137 else if (CC == CallingConv::Fast && isTaillCall)
138 return CC_X86_64_TailCall;
139 else
140 return CC_X86_64_C;
141 }
142
143 if (CC == CallingConv::X86_FastCall)
144 return CC_X86_32_FastCall;
145 else if (CC == CallingConv::Fast && isTaillCall)
146 return CC_X86_32_TailCall;
147 else if (CC == CallingConv::Fast)
148 return CC_X86_32_FastCC;
149 else
150 return CC_X86_32_C;
151}
152
Evan Cheng0de588f2008-09-05 21:00:03 +0000153/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000154/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000155/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000156bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000157 unsigned &ResultReg) {
158 // Get opcode and regclass of the output for the given load instruction.
159 unsigned Opc = 0;
160 const TargetRegisterClass *RC = NULL;
161 switch (VT.getSimpleVT()) {
162 default: return false;
163 case MVT::i8:
164 Opc = X86::MOV8rm;
165 RC = X86::GR8RegisterClass;
166 break;
167 case MVT::i16:
168 Opc = X86::MOV16rm;
169 RC = X86::GR16RegisterClass;
170 break;
171 case MVT::i32:
172 Opc = X86::MOV32rm;
173 RC = X86::GR32RegisterClass;
174 break;
175 case MVT::i64:
176 // Must be in x86-64 mode.
177 Opc = X86::MOV64rm;
178 RC = X86::GR64RegisterClass;
179 break;
180 case MVT::f32:
181 if (Subtarget->hasSSE1()) {
182 Opc = X86::MOVSSrm;
183 RC = X86::FR32RegisterClass;
184 } else {
185 Opc = X86::LD_Fp32m;
186 RC = X86::RFP32RegisterClass;
187 }
188 break;
189 case MVT::f64:
190 if (Subtarget->hasSSE2()) {
191 Opc = X86::MOVSDrm;
192 RC = X86::FR64RegisterClass;
193 } else {
194 Opc = X86::LD_Fp64m;
195 RC = X86::RFP64RegisterClass;
196 }
197 break;
198 case MVT::f80:
199 Opc = X86::LD_Fp80m;
200 RC = X86::RFP80RegisterClass;
201 break;
202 }
203
204 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
206 return true;
207}
208
Evan Chengf3d4efe2008-09-07 09:09:33 +0000209/// X86FastEmitStore - Emit a machine instruction to store a value Val of
210/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
211/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000212/// i.e. V. Return true if it is possible.
213bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000214X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000215 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000216 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000217 unsigned Opc = 0;
218 const TargetRegisterClass *RC = NULL;
219 switch (VT.getSimpleVT()) {
220 default: return false;
221 case MVT::i8:
222 Opc = X86::MOV8mr;
223 RC = X86::GR8RegisterClass;
224 break;
225 case MVT::i16:
226 Opc = X86::MOV16mr;
227 RC = X86::GR16RegisterClass;
228 break;
229 case MVT::i32:
230 Opc = X86::MOV32mr;
231 RC = X86::GR32RegisterClass;
232 break;
233 case MVT::i64:
234 // Must be in x86-64 mode.
235 Opc = X86::MOV64mr;
236 RC = X86::GR64RegisterClass;
237 break;
238 case MVT::f32:
239 if (Subtarget->hasSSE1()) {
240 Opc = X86::MOVSSmr;
241 RC = X86::FR32RegisterClass;
242 } else {
243 Opc = X86::ST_Fp32m;
244 RC = X86::RFP32RegisterClass;
245 }
246 break;
247 case MVT::f64:
248 if (Subtarget->hasSSE2()) {
249 Opc = X86::MOVSDmr;
250 RC = X86::FR64RegisterClass;
251 } else {
252 Opc = X86::ST_Fp64m;
253 RC = X86::RFP64RegisterClass;
254 }
255 break;
256 case MVT::f80:
257 Opc = X86::ST_FP80m;
258 RC = X86::RFP80RegisterClass;
259 break;
260 }
261
Evan Chengf3d4efe2008-09-07 09:09:33 +0000262 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000263 return true;
264}
265
Evan Cheng24e3a902008-09-08 06:35:17 +0000266/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
267/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
268/// ISD::SIGN_EXTEND).
269bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
270 unsigned Src, MVT SrcVT,
271 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000272 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
273
274 if (RR != 0) {
275 ResultReg = RR;
276 return true;
277 } else
278 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000279}
280
Dan Gohman0586d912008-09-10 20:11:02 +0000281/// X86SelectAddress - Attempt to fill in an address from the given value.
282///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000283bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000284 User *U;
285 unsigned Opcode = Instruction::UserOp1;
286 if (Instruction *I = dyn_cast<Instruction>(V)) {
287 Opcode = I->getOpcode();
288 U = I;
289 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
290 Opcode = C->getOpcode();
291 U = C;
292 }
Dan Gohman0586d912008-09-10 20:11:02 +0000293
Dan Gohman35893082008-09-18 23:23:44 +0000294 switch (Opcode) {
295 default: break;
296 case Instruction::BitCast:
297 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000298 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000299
300 case Instruction::IntToPtr:
301 // Look past no-op inttoptrs.
302 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000303 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000304
305 case Instruction::PtrToInt:
306 // Look past no-op ptrtoints.
307 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000308 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000309
310 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000311 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000312 // Do static allocas.
313 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000314 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
315 if (SI == StaticAllocaMap.end())
316 return false;
317 AM.BaseType = X86AddressMode::FrameIndexBase;
318 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000319 return true;
320 }
321
322 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000323 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000324 // Adds of constants are common and easy enough.
325 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
326 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000327 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000328 }
Dan Gohman35893082008-09-18 23:23:44 +0000329 break;
330 }
331
332 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000333 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000334 // Pattern-match simple GEPs.
335 uint64_t Disp = AM.Disp;
336 unsigned IndexReg = AM.IndexReg;
337 unsigned Scale = AM.Scale;
338 gep_type_iterator GTI = gep_type_begin(U);
339 // Look at all but the last index. Constants can be folded,
340 // and one dynamic index can be handled, if the scale is supported.
341 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
342 i != e; ++i, ++GTI) {
343 Value *Op = *i;
344 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
345 const StructLayout *SL = TD.getStructLayout(STy);
346 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
347 Disp += SL->getElementOffset(Idx);
348 } else {
349 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
350 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
351 // Constant-offset addressing.
352 Disp += CI->getZExtValue() * S;
353 } else if (IndexReg == 0 &&
354 (S == 1 || S == 2 || S == 4 || S == 8)) {
355 // Scaled-index addressing.
356 Scale = S;
357 IndexReg = getRegForValue(Op);
358 if (IndexReg == 0)
359 return false;
360 } else
361 // Unsupported.
362 goto unsupported_gep;
363 }
364 }
365 // Ok, the GEP indices were covered by constant-offset and scaled-index
366 // addressing. Update the address state and move on to examining the base.
367 AM.IndexReg = IndexReg;
368 AM.Scale = Scale;
369 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000370 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000371 unsupported_gep:
372 // Ok, the GEP indices weren't all covered.
373 break;
374 }
375 }
376
377 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000378 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
379 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
380 // Check to see if we've already materialized this
381 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000382 if (unsigned Reg = LocalValueMap[V]) {
383 AM.Base.Reg = Reg;
384 return true;
385 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000386 // Issue load from stub if necessary.
387 unsigned Opc = 0;
388 const TargetRegisterClass *RC = NULL;
389 if (TLI.getPointerTy() == MVT::i32) {
390 Opc = X86::MOV32rm;
391 RC = X86::GR32RegisterClass;
392 } else {
393 Opc = X86::MOV64rm;
394 RC = X86::GR64RegisterClass;
395 }
396 AM.Base.Reg = createResultReg(RC);
397 X86AddressMode LocalAM;
398 LocalAM.GV = GV;
399 addFullAddress(BuildMI(MBB, TII.get(Opc), AM.Base.Reg), LocalAM);
400 // Prevent loading GV stub multiple times in same MBB.
401 LocalValueMap[V] = AM.Base.Reg;
402 } else {
403 AM.GV = GV;
404 }
405 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000406 }
407
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000408 // If all else fails, just materialize the value in a register.
409 AM.Base.Reg = getRegForValue(V);
410 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000411}
412
Owen Andersona3971df2008-09-04 07:08:58 +0000413/// X86SelectStore - Select and emit code to implement store instructions.
414bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000415 MVT VT;
416 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000417 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000418 unsigned Val = getRegForValue(I->getOperand(0));
419 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000420 // Unhandled operand. Halt "fast" selection and bail.
421 return false;
422
Dan Gohman0586d912008-09-10 20:11:02 +0000423 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000424 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000425 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000426
Dan Gohman0586d912008-09-10 20:11:02 +0000427 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000428}
429
Evan Cheng8b19e562008-09-03 06:44:39 +0000430/// X86SelectLoad - Select and emit code to implement load instructions.
431///
Dan Gohman3df24e62008-09-03 23:12:08 +0000432bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000433 MVT VT;
434 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000435 return false;
436
Dan Gohman0586d912008-09-10 20:11:02 +0000437 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000438 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000439 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000440
Evan Cheng0de588f2008-09-05 21:00:03 +0000441 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000442 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000443 UpdateValueMap(I, ResultReg);
444 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000445 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000446 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000447}
448
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000449bool X86FastISel::X86SelectCmp(Instruction *I) {
450 CmpInst *CI = cast<CmpInst>(I);
451
Dan Gohman4f22bb02008-09-05 01:33:56 +0000452 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
453 if (!TLI.isTypeLegal(VT))
454 return false;
455
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000456 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000457 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000458 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000459 if (Op1Reg == 0) return false;
460
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000461 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000462 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000463 case MVT::i8: Opc = X86::CMP8rr; break;
464 case MVT::i16: Opc = X86::CMP16rr; break;
465 case MVT::i32: Opc = X86::CMP32rr; break;
466 case MVT::i64: Opc = X86::CMP64rr; break;
467 case MVT::f32: Opc = X86::UCOMISSrr; break;
468 case MVT::f64: Opc = X86::UCOMISDrr; break;
469 default: return false;
470 }
471
472 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
473 switch (CI->getPredicate()) {
474 case CmpInst::FCMP_OEQ: {
475 unsigned EReg = createResultReg(&X86::GR8RegClass);
476 unsigned NPReg = createResultReg(&X86::GR8RegClass);
477 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
478 BuildMI(MBB, TII.get(X86::SETEr), EReg);
479 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
480 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
481 break;
482 }
483 case CmpInst::FCMP_UNE: {
484 unsigned NEReg = createResultReg(&X86::GR8RegClass);
485 unsigned PReg = createResultReg(&X86::GR8RegClass);
486 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
487 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
488 BuildMI(MBB, TII.get(X86::SETPr), PReg);
489 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
490 break;
491 }
492 case CmpInst::FCMP_OGT:
493 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
494 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
495 break;
496 case CmpInst::FCMP_OGE:
497 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
498 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
499 break;
500 case CmpInst::FCMP_OLT:
501 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
502 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
503 break;
504 case CmpInst::FCMP_OLE:
505 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
506 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
507 break;
508 case CmpInst::FCMP_ONE:
509 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
510 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
511 break;
512 case CmpInst::FCMP_ORD:
513 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
514 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
515 break;
516 case CmpInst::FCMP_UNO:
517 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
518 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
519 break;
520 case CmpInst::FCMP_UEQ:
521 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
522 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
523 break;
524 case CmpInst::FCMP_UGT:
525 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
526 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
527 break;
528 case CmpInst::FCMP_UGE:
529 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
530 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
531 break;
532 case CmpInst::FCMP_ULT:
533 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
534 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
535 break;
536 case CmpInst::FCMP_ULE:
537 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
538 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
539 break;
540 case CmpInst::ICMP_EQ:
541 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
542 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
543 break;
544 case CmpInst::ICMP_NE:
545 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
546 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
547 break;
548 case CmpInst::ICMP_UGT:
549 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
550 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
551 break;
552 case CmpInst::ICMP_UGE:
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
555 break;
556 case CmpInst::ICMP_ULT:
557 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
558 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
559 break;
560 case CmpInst::ICMP_ULE:
561 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
562 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
563 break;
564 case CmpInst::ICMP_SGT:
565 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
566 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
567 break;
568 case CmpInst::ICMP_SGE:
569 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
570 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
571 break;
572 case CmpInst::ICMP_SLT:
573 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
574 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
575 break;
576 case CmpInst::ICMP_SLE:
577 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
578 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
579 break;
580 default:
581 return false;
582 }
583
584 UpdateValueMap(I, ResultReg);
585 return true;
586}
Evan Cheng8b19e562008-09-03 06:44:39 +0000587
Dan Gohmand89ae992008-09-05 01:06:14 +0000588bool X86FastISel::X86SelectZExt(Instruction *I) {
589 // Special-case hack: The only i1 values we know how to produce currently
590 // set the upper bits of an i8 value to zero.
591 if (I->getType() == Type::Int8Ty &&
592 I->getOperand(0)->getType() == Type::Int1Ty) {
593 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000594 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000595 UpdateValueMap(I, ResultReg);
596 return true;
597 }
598
599 return false;
600}
601
602bool X86FastISel::X86SelectBranch(Instruction *I) {
603 BranchInst *BI = cast<BranchInst>(I);
604 // Unconditional branches are selected by tablegen-generated code.
605 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000606 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000607 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
608 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
609
610 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
611 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
612 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
613
614 MBB->addSuccessor(TrueMBB);
615 MBB->addSuccessor(FalseMBB);
616
617 return true;
618}
619
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000620bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000621 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000622 const TargetRegisterClass *RC = NULL;
623 if (I->getType() == Type::Int8Ty) {
624 CReg = X86::CL;
625 RC = &X86::GR8RegClass;
626 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000627 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
628 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
629 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000630 default: return false;
631 }
632 } else if (I->getType() == Type::Int16Ty) {
633 CReg = X86::CX;
634 RC = &X86::GR16RegClass;
635 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000636 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
637 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
638 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000639 default: return false;
640 }
641 } else if (I->getType() == Type::Int32Ty) {
642 CReg = X86::ECX;
643 RC = &X86::GR32RegClass;
644 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000645 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
646 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
647 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000648 default: return false;
649 }
650 } else if (I->getType() == Type::Int64Ty) {
651 CReg = X86::RCX;
652 RC = &X86::GR64RegClass;
653 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000654 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
655 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
656 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000657 default: return false;
658 }
659 } else {
660 return false;
661 }
662
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000663 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
664 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
665 return false;
666
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000667 unsigned Op0Reg = getRegForValue(I->getOperand(0));
668 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000669
670 // Fold immediate in shl(x,3).
671 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
672 unsigned ResultReg = createResultReg(RC);
673 BuildMI(MBB, TII.get(OpImm),
674 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
675 UpdateValueMap(I, ResultReg);
676 return true;
677 }
678
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000679 unsigned Op1Reg = getRegForValue(I->getOperand(1));
680 if (Op1Reg == 0) return false;
681 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
682 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000683 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000684 UpdateValueMap(I, ResultReg);
685 return true;
686}
687
688bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000689 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000690 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000691 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000692
693 unsigned Opc = 0;
694 const TargetRegisterClass *RC = NULL;
695 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000696 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000697 RC = &X86::GR16RegClass;
698 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000699 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000700 RC = &X86::GR32RegClass;
701 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000702 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000703 RC = &X86::GR64RegClass;
704 } else {
705 return false;
706 }
707
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000708 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
709 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
710 return false;
711
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000712 unsigned Op0Reg = getRegForValue(I->getOperand(0));
713 if (Op0Reg == 0) return false;
714 unsigned Op1Reg = getRegForValue(I->getOperand(1));
715 if (Op1Reg == 0) return false;
716 unsigned Op2Reg = getRegForValue(I->getOperand(2));
717 if (Op2Reg == 0) return false;
718
719 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
720 unsigned ResultReg = createResultReg(RC);
721 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
722 UpdateValueMap(I, ResultReg);
723 return true;
724}
725
Dan Gohman78efce62008-09-10 21:02:08 +0000726bool X86FastISel::X86SelectFPExt(Instruction *I) {
727 if (Subtarget->hasSSE2()) {
728 if (I->getType() == Type::DoubleTy) {
729 Value *V = I->getOperand(0);
730 if (V->getType() == Type::FloatTy) {
731 unsigned OpReg = getRegForValue(V);
732 if (OpReg == 0) return false;
733 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
734 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
735 UpdateValueMap(I, ResultReg);
736 return true;
737 }
738 }
739 }
740
741 return false;
742}
743
744bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
745 if (Subtarget->hasSSE2()) {
746 if (I->getType() == Type::FloatTy) {
747 Value *V = I->getOperand(0);
748 if (V->getType() == Type::DoubleTy) {
749 unsigned OpReg = getRegForValue(V);
750 if (OpReg == 0) return false;
751 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
752 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
753 UpdateValueMap(I, ResultReg);
754 return true;
755 }
756 }
757 }
758
759 return false;
760}
761
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000762bool X86FastISel::X86SelectTrunc(Instruction *I) {
763 if (Subtarget->is64Bit())
764 // All other cases should be handled by the tblgen generated code.
765 return false;
766 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
767 MVT DstVT = TLI.getValueType(I->getType());
768 if (DstVT != MVT::i8)
769 // All other cases should be handled by the tblgen generated code.
770 return false;
771 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
772 // All other cases should be handled by the tblgen generated code.
773 return false;
774
775 unsigned InputReg = getRegForValue(I->getOperand(0));
776 if (!InputReg)
777 // Unhandled operand. Halt "fast" selection and bail.
778 return false;
779
780 // First issue a copy to GR16_ or GR32_.
781 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
782 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
783 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
784 unsigned CopyReg = createResultReg(CopyRC);
785 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
786
787 // Then issue an extract_subreg.
788 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
789 if (!ResultReg)
790 return false;
791
792 UpdateValueMap(I, ResultReg);
793 return true;
794}
795
Evan Chengf3d4efe2008-09-07 09:09:33 +0000796bool X86FastISel::X86SelectCall(Instruction *I) {
797 CallInst *CI = cast<CallInst>(I);
798 Value *Callee = I->getOperand(0);
799
800 // Can't handle inline asm yet.
801 if (isa<InlineAsm>(Callee))
802 return false;
803
804 // FIXME: Handle some intrinsics.
805 if (Function *F = CI->getCalledFunction()) {
806 if (F->isDeclaration() &&F->getIntrinsicID())
807 return false;
808 }
809
Evan Chengf3d4efe2008-09-07 09:09:33 +0000810 // Handle only C and fastcc calling conventions for now.
811 CallSite CS(CI);
812 unsigned CC = CS.getCallingConv();
813 if (CC != CallingConv::C &&
814 CC != CallingConv::Fast &&
815 CC != CallingConv::X86_FastCall)
816 return false;
817
818 // Let SDISel handle vararg functions.
819 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
820 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
821 if (FTy->isVarArg())
822 return false;
823
824 // Handle *simple* calls for now.
825 const Type *RetTy = CS.getType();
826 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000827 if (RetTy == Type::VoidTy)
828 RetVT = MVT::isVoid;
829 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000830 return false;
831
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000832 // Materialize callee address in a register. FIXME: GV address can be
833 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000834 X86AddressMode CalleeAM;
835 if (!X86SelectAddress(Callee, CalleeAM, true))
836 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000837 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000838 GlobalValue *GV = 0;
839 if (CalleeAM.Base.Reg != 0) {
840 assert(CalleeAM.GV == 0);
841 CalleeOp = CalleeAM.Base.Reg;
842 } else if (CalleeAM.GV != 0) {
843 assert(CalleeAM.GV != 0);
844 GV = CalleeAM.GV;
845 } else
846 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000847
Evan Chengdebdea02008-09-08 17:15:42 +0000848 // Allow calls which produce i1 results.
849 bool AndToI1 = false;
850 if (RetVT == MVT::i1) {
851 RetVT = MVT::i8;
852 AndToI1 = true;
853 }
854
Evan Chengf3d4efe2008-09-07 09:09:33 +0000855 // Deal with call operands first.
856 SmallVector<unsigned, 4> Args;
857 SmallVector<MVT, 4> ArgVTs;
858 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
859 Args.reserve(CS.arg_size());
860 ArgVTs.reserve(CS.arg_size());
861 ArgFlags.reserve(CS.arg_size());
862 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
863 i != e; ++i) {
864 unsigned Arg = getRegForValue(*i);
865 if (Arg == 0)
866 return false;
867 ISD::ArgFlagsTy Flags;
868 unsigned AttrInd = i - CS.arg_begin() + 1;
869 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
870 Flags.setSExt();
871 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
872 Flags.setZExt();
873
874 // FIXME: Only handle *easy* calls for now.
875 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
876 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
877 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
878 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
879 return false;
880
881 const Type *ArgTy = (*i)->getType();
882 MVT ArgVT;
883 if (!isTypeLegal(ArgTy, TLI, ArgVT))
884 return false;
885 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
886 Flags.setOrigAlign(OriginalAlignment);
887
888 Args.push_back(Arg);
889 ArgVTs.push_back(ArgVT);
890 ArgFlags.push_back(Flags);
891 }
892
893 // Analyze operands of the call, assigning locations to each operand.
894 SmallVector<CCValAssign, 16> ArgLocs;
895 CCState CCInfo(CC, false, TM, ArgLocs);
896 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
897
898 // Get a count of how many bytes are to be pushed on the stack.
899 unsigned NumBytes = CCInfo.getNextStackOffset();
900
901 // Issue CALLSEQ_START
902 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
903
904 // Process argumenet: walk the register/memloc assignments, inserting
905 // copies / loads.
906 SmallVector<unsigned, 4> RegArgs;
907 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
908 CCValAssign &VA = ArgLocs[i];
909 unsigned Arg = Args[VA.getValNo()];
910 MVT ArgVT = ArgVTs[VA.getValNo()];
911
912 // Promote the value if needed.
913 switch (VA.getLocInfo()) {
914 default: assert(0 && "Unknown loc info!");
915 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000916 case CCValAssign::SExt: {
917 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
918 Arg, ArgVT, Arg);
919 assert(Emitted && "Failed to emit a sext!");
920 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000921 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000922 }
923 case CCValAssign::ZExt: {
924 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
925 Arg, ArgVT, Arg);
926 assert(Emitted && "Failed to emit a zext!");
927 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000928 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000929 }
930 case CCValAssign::AExt: {
931 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
932 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000933 if (!Emitted)
934 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
935 Arg, ArgVT, Arg);
936 if (!Emitted)
937 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
938 Arg, ArgVT, Arg);
939
Evan Cheng24e3a902008-09-08 06:35:17 +0000940 assert(Emitted && "Failed to emit a aext!");
941 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000942 break;
943 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000944 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000945
946 if (VA.isRegLoc()) {
947 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
948 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
949 Arg, RC, RC);
950 assert(Emitted && "Failed to emit a copy instruction!");
951 RegArgs.push_back(VA.getLocReg());
952 } else {
953 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000954 X86AddressMode AM;
955 AM.Base.Reg = StackPtr;
956 AM.Disp = LocMemOffset;
957 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000958 }
959 }
960
961 // Issue the call.
962 unsigned CallOpc = CalleeOp
963 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
964 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
965 MachineInstrBuilder MIB = CalleeOp
966 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000967 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000968 // Add implicit physical register uses to the call.
969 while (!RegArgs.empty()) {
970 MIB.addReg(RegArgs.back());
971 RegArgs.pop_back();
972 }
973
974 // Issue CALLSEQ_END
975 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
976
977 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000978 if (RetVT.getSimpleVT() != MVT::isVoid) {
979 SmallVector<CCValAssign, 16> RVLocs;
980 CCState CCInfo(CC, false, TM, RVLocs);
981 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
982
983 // Copy all of the result registers out of their specified physreg.
984 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
985 MVT CopyVT = RVLocs[0].getValVT();
986 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
987 TargetRegisterClass *SrcRC = DstRC;
988
989 // If this is a call to a function that returns an fp value on the x87 fp
990 // stack, but where we prefer to use the value in xmm registers, copy it
991 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
992 if ((RVLocs[0].getLocReg() == X86::ST0 ||
993 RVLocs[0].getLocReg() == X86::ST1) &&
994 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
995 CopyVT = MVT::f80;
996 SrcRC = X86::RSTRegisterClass;
997 DstRC = X86::RFP80RegisterClass;
998 }
999
1000 unsigned ResultReg = createResultReg(DstRC);
1001 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1002 RVLocs[0].getLocReg(), DstRC, SrcRC);
1003 assert(Emitted && "Failed to emit a copy instruction!");
1004 if (CopyVT != RVLocs[0].getValVT()) {
1005 // Round the F80 the right size, which also moves to the appropriate xmm
1006 // register. This is accomplished by storing the F80 value in memory and
1007 // then loading it back. Ewww...
1008 MVT ResVT = RVLocs[0].getValVT();
1009 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1010 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001011 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001012 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1013 DstRC = ResVT == MVT::f32
1014 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1015 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1016 ResultReg = createResultReg(DstRC);
1017 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1018 }
1019
Evan Chengdebdea02008-09-08 17:15:42 +00001020 if (AndToI1) {
1021 // Mask out all but lowest bit for some call which produces an i1.
1022 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1023 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1024 ResultReg = AndResult;
1025 }
1026
Evan Chengf3d4efe2008-09-07 09:09:33 +00001027 UpdateValueMap(I, ResultReg);
1028 }
1029
1030 return true;
1031}
1032
1033
Dan Gohman99b21822008-08-28 23:21:34 +00001034bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001035X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001036 switch (I->getOpcode()) {
1037 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001038 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001039 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001040 case Instruction::Store:
1041 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001042 case Instruction::ICmp:
1043 case Instruction::FCmp:
1044 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001045 case Instruction::ZExt:
1046 return X86SelectZExt(I);
1047 case Instruction::Br:
1048 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001049 case Instruction::Call:
1050 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001051 case Instruction::LShr:
1052 case Instruction::AShr:
1053 case Instruction::Shl:
1054 return X86SelectShift(I);
1055 case Instruction::Select:
1056 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001057 case Instruction::Trunc:
1058 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001059 case Instruction::FPExt:
1060 return X86SelectFPExt(I);
1061 case Instruction::FPTrunc:
1062 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001063 }
1064
1065 return false;
1066}
1067
Dan Gohman0586d912008-09-10 20:11:02 +00001068unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001069 // Can't handle PIC-mode yet.
1070 if (TM.getRelocationModel() == Reloc::PIC_)
1071 return 0;
1072
Evan Cheng59fbc802008-09-09 01:26:59 +00001073 MVT VT;
1074 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001075 return false;
1076
1077 // Get opcode and regclass of the output for the given load instruction.
1078 unsigned Opc = 0;
1079 const TargetRegisterClass *RC = NULL;
1080 switch (VT.getSimpleVT()) {
1081 default: return false;
1082 case MVT::i8:
1083 Opc = X86::MOV8rm;
1084 RC = X86::GR8RegisterClass;
1085 break;
1086 case MVT::i16:
1087 Opc = X86::MOV16rm;
1088 RC = X86::GR16RegisterClass;
1089 break;
1090 case MVT::i32:
1091 Opc = X86::MOV32rm;
1092 RC = X86::GR32RegisterClass;
1093 break;
1094 case MVT::i64:
1095 // Must be in x86-64 mode.
1096 Opc = X86::MOV64rm;
1097 RC = X86::GR64RegisterClass;
1098 break;
1099 case MVT::f32:
1100 if (Subtarget->hasSSE1()) {
1101 Opc = X86::MOVSSrm;
1102 RC = X86::FR32RegisterClass;
1103 } else {
1104 Opc = X86::LD_Fp32m;
1105 RC = X86::RFP32RegisterClass;
1106 }
1107 break;
1108 case MVT::f64:
1109 if (Subtarget->hasSSE2()) {
1110 Opc = X86::MOVSDrm;
1111 RC = X86::FR64RegisterClass;
1112 } else {
1113 Opc = X86::LD_Fp64m;
1114 RC = X86::RFP64RegisterClass;
1115 }
1116 break;
1117 case MVT::f80:
1118 Opc = X86::LD_Fp80m;
1119 RC = X86::RFP80RegisterClass;
1120 break;
1121 }
1122
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001123 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001124 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001125 X86AddressMode AM;
1126 if (X86SelectAddress(C, AM, false)) {
1127 if (TLI.getPointerTy() == MVT::i32)
1128 Opc = X86::LEA32r;
1129 else
1130 Opc = X86::LEA64r;
1131 unsigned ResultReg = createResultReg(RC);
1132 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001133 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001134 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001135 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001136 }
1137
Owen Anderson3b217c62008-09-06 01:11:01 +00001138 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001139 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001140 if (Align == 0) {
1141 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001142 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001143 Align = Log2_64(Align);
1144 }
Owen Anderson95267a12008-09-05 00:06:23 +00001145
Dan Gohman0586d912008-09-10 20:11:02 +00001146 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001147 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001148 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001149 return ResultReg;
1150}
1151
Dan Gohman0586d912008-09-10 20:11:02 +00001152unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1153 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001154 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001155 return 0;
1156 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1157 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1158 unsigned ResultReg = createResultReg(RC);
1159 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1160 return ResultReg;
1161}
1162
Evan Chengc3f44b02008-09-03 00:03:49 +00001163namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001164 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1165 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001166 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1167 DenseMap<const AllocaInst *, int> &am) {
1168 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001169 }
Dan Gohman99b21822008-08-28 23:21:34 +00001170}