blob: 309a3f3f79864498ed0f0093c9dd3c352eb7dbbb [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) {
621 unsigned CReg = 0;
622 unsigned Opc = 0;
623 const TargetRegisterClass *RC = NULL;
624 if (I->getType() == Type::Int8Ty) {
625 CReg = X86::CL;
626 RC = &X86::GR8RegClass;
627 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000628 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000629 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000630 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000631 default: return false;
632 }
633 } else if (I->getType() == Type::Int16Ty) {
634 CReg = X86::CX;
635 RC = &X86::GR16RegClass;
636 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000637 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000638 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000639 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000640 default: return false;
641 }
642 } else if (I->getType() == Type::Int32Ty) {
643 CReg = X86::ECX;
644 RC = &X86::GR32RegClass;
645 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000646 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000647 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000648 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000649 default: return false;
650 }
651 } else if (I->getType() == Type::Int64Ty) {
652 CReg = X86::RCX;
653 RC = &X86::GR64RegClass;
654 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000655 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000656 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000657 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000658 default: return false;
659 }
660 } else {
661 return false;
662 }
663
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000664 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
665 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
666 return false;
667
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000668 unsigned Op0Reg = getRegForValue(I->getOperand(0));
669 if (Op0Reg == 0) return false;
670 unsigned Op1Reg = getRegForValue(I->getOperand(1));
671 if (Op1Reg == 0) return false;
672 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
673 unsigned ResultReg = createResultReg(RC);
674 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
675 UpdateValueMap(I, ResultReg);
676 return true;
677}
678
679bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000680 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000681 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000682 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000683
684 unsigned Opc = 0;
685 const TargetRegisterClass *RC = NULL;
686 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000687 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000688 RC = &X86::GR16RegClass;
689 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000690 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000691 RC = &X86::GR32RegClass;
692 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000693 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000694 RC = &X86::GR64RegClass;
695 } else {
696 return false;
697 }
698
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000699 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
700 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
701 return false;
702
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000703 unsigned Op0Reg = getRegForValue(I->getOperand(0));
704 if (Op0Reg == 0) return false;
705 unsigned Op1Reg = getRegForValue(I->getOperand(1));
706 if (Op1Reg == 0) return false;
707 unsigned Op2Reg = getRegForValue(I->getOperand(2));
708 if (Op2Reg == 0) return false;
709
710 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
711 unsigned ResultReg = createResultReg(RC);
712 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
713 UpdateValueMap(I, ResultReg);
714 return true;
715}
716
Dan Gohman78efce62008-09-10 21:02:08 +0000717bool X86FastISel::X86SelectFPExt(Instruction *I) {
718 if (Subtarget->hasSSE2()) {
719 if (I->getType() == Type::DoubleTy) {
720 Value *V = I->getOperand(0);
721 if (V->getType() == Type::FloatTy) {
722 unsigned OpReg = getRegForValue(V);
723 if (OpReg == 0) return false;
724 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
725 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
726 UpdateValueMap(I, ResultReg);
727 return true;
728 }
729 }
730 }
731
732 return false;
733}
734
735bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
736 if (Subtarget->hasSSE2()) {
737 if (I->getType() == Type::FloatTy) {
738 Value *V = I->getOperand(0);
739 if (V->getType() == Type::DoubleTy) {
740 unsigned OpReg = getRegForValue(V);
741 if (OpReg == 0) return false;
742 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
743 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
744 UpdateValueMap(I, ResultReg);
745 return true;
746 }
747 }
748 }
749
750 return false;
751}
752
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000753bool X86FastISel::X86SelectTrunc(Instruction *I) {
754 if (Subtarget->is64Bit())
755 // All other cases should be handled by the tblgen generated code.
756 return false;
757 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
758 MVT DstVT = TLI.getValueType(I->getType());
759 if (DstVT != MVT::i8)
760 // All other cases should be handled by the tblgen generated code.
761 return false;
762 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
763 // All other cases should be handled by the tblgen generated code.
764 return false;
765
766 unsigned InputReg = getRegForValue(I->getOperand(0));
767 if (!InputReg)
768 // Unhandled operand. Halt "fast" selection and bail.
769 return false;
770
771 // First issue a copy to GR16_ or GR32_.
772 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
773 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
774 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
775 unsigned CopyReg = createResultReg(CopyRC);
776 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
777
778 // Then issue an extract_subreg.
779 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
780 if (!ResultReg)
781 return false;
782
783 UpdateValueMap(I, ResultReg);
784 return true;
785}
786
Evan Chengf3d4efe2008-09-07 09:09:33 +0000787bool X86FastISel::X86SelectCall(Instruction *I) {
788 CallInst *CI = cast<CallInst>(I);
789 Value *Callee = I->getOperand(0);
790
791 // Can't handle inline asm yet.
792 if (isa<InlineAsm>(Callee))
793 return false;
794
795 // FIXME: Handle some intrinsics.
796 if (Function *F = CI->getCalledFunction()) {
797 if (F->isDeclaration() &&F->getIntrinsicID())
798 return false;
799 }
800
Evan Chengf3d4efe2008-09-07 09:09:33 +0000801 // Handle only C and fastcc calling conventions for now.
802 CallSite CS(CI);
803 unsigned CC = CS.getCallingConv();
804 if (CC != CallingConv::C &&
805 CC != CallingConv::Fast &&
806 CC != CallingConv::X86_FastCall)
807 return false;
808
809 // Let SDISel handle vararg functions.
810 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
811 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
812 if (FTy->isVarArg())
813 return false;
814
815 // Handle *simple* calls for now.
816 const Type *RetTy = CS.getType();
817 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000818 if (RetTy == Type::VoidTy)
819 RetVT = MVT::isVoid;
820 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000821 return false;
822
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000823 // Materialize callee address in a register. FIXME: GV address can be
824 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000825 X86AddressMode CalleeAM;
826 if (!X86SelectAddress(Callee, CalleeAM, true))
827 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000828 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000829 GlobalValue *GV = 0;
830 if (CalleeAM.Base.Reg != 0) {
831 assert(CalleeAM.GV == 0);
832 CalleeOp = CalleeAM.Base.Reg;
833 } else if (CalleeAM.GV != 0) {
834 assert(CalleeAM.GV != 0);
835 GV = CalleeAM.GV;
836 } else
837 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000838
Evan Chengdebdea02008-09-08 17:15:42 +0000839 // Allow calls which produce i1 results.
840 bool AndToI1 = false;
841 if (RetVT == MVT::i1) {
842 RetVT = MVT::i8;
843 AndToI1 = true;
844 }
845
Evan Chengf3d4efe2008-09-07 09:09:33 +0000846 // Deal with call operands first.
847 SmallVector<unsigned, 4> Args;
848 SmallVector<MVT, 4> ArgVTs;
849 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
850 Args.reserve(CS.arg_size());
851 ArgVTs.reserve(CS.arg_size());
852 ArgFlags.reserve(CS.arg_size());
853 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
854 i != e; ++i) {
855 unsigned Arg = getRegForValue(*i);
856 if (Arg == 0)
857 return false;
858 ISD::ArgFlagsTy Flags;
859 unsigned AttrInd = i - CS.arg_begin() + 1;
860 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
861 Flags.setSExt();
862 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
863 Flags.setZExt();
864
865 // FIXME: Only handle *easy* calls for now.
866 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
867 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
868 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
869 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
870 return false;
871
872 const Type *ArgTy = (*i)->getType();
873 MVT ArgVT;
874 if (!isTypeLegal(ArgTy, TLI, ArgVT))
875 return false;
876 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
877 Flags.setOrigAlign(OriginalAlignment);
878
879 Args.push_back(Arg);
880 ArgVTs.push_back(ArgVT);
881 ArgFlags.push_back(Flags);
882 }
883
884 // Analyze operands of the call, assigning locations to each operand.
885 SmallVector<CCValAssign, 16> ArgLocs;
886 CCState CCInfo(CC, false, TM, ArgLocs);
887 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
888
889 // Get a count of how many bytes are to be pushed on the stack.
890 unsigned NumBytes = CCInfo.getNextStackOffset();
891
892 // Issue CALLSEQ_START
893 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
894
895 // Process argumenet: walk the register/memloc assignments, inserting
896 // copies / loads.
897 SmallVector<unsigned, 4> RegArgs;
898 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
899 CCValAssign &VA = ArgLocs[i];
900 unsigned Arg = Args[VA.getValNo()];
901 MVT ArgVT = ArgVTs[VA.getValNo()];
902
903 // Promote the value if needed.
904 switch (VA.getLocInfo()) {
905 default: assert(0 && "Unknown loc info!");
906 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000907 case CCValAssign::SExt: {
908 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
909 Arg, ArgVT, Arg);
910 assert(Emitted && "Failed to emit a sext!");
911 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000912 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000913 }
914 case CCValAssign::ZExt: {
915 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
916 Arg, ArgVT, Arg);
917 assert(Emitted && "Failed to emit a zext!");
918 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000919 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000920 }
921 case CCValAssign::AExt: {
922 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
923 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000924 if (!Emitted)
925 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
926 Arg, ArgVT, Arg);
927 if (!Emitted)
928 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
929 Arg, ArgVT, Arg);
930
Evan Cheng24e3a902008-09-08 06:35:17 +0000931 assert(Emitted && "Failed to emit a aext!");
932 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000933 break;
934 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000935 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000936
937 if (VA.isRegLoc()) {
938 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
939 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
940 Arg, RC, RC);
941 assert(Emitted && "Failed to emit a copy instruction!");
942 RegArgs.push_back(VA.getLocReg());
943 } else {
944 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000945 X86AddressMode AM;
946 AM.Base.Reg = StackPtr;
947 AM.Disp = LocMemOffset;
948 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000949 }
950 }
951
952 // Issue the call.
953 unsigned CallOpc = CalleeOp
954 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
955 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
956 MachineInstrBuilder MIB = CalleeOp
957 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000958 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000959 // Add implicit physical register uses to the call.
960 while (!RegArgs.empty()) {
961 MIB.addReg(RegArgs.back());
962 RegArgs.pop_back();
963 }
964
965 // Issue CALLSEQ_END
966 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
967
968 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000969 if (RetVT.getSimpleVT() != MVT::isVoid) {
970 SmallVector<CCValAssign, 16> RVLocs;
971 CCState CCInfo(CC, false, TM, RVLocs);
972 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
973
974 // Copy all of the result registers out of their specified physreg.
975 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
976 MVT CopyVT = RVLocs[0].getValVT();
977 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
978 TargetRegisterClass *SrcRC = DstRC;
979
980 // If this is a call to a function that returns an fp value on the x87 fp
981 // stack, but where we prefer to use the value in xmm registers, copy it
982 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
983 if ((RVLocs[0].getLocReg() == X86::ST0 ||
984 RVLocs[0].getLocReg() == X86::ST1) &&
985 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
986 CopyVT = MVT::f80;
987 SrcRC = X86::RSTRegisterClass;
988 DstRC = X86::RFP80RegisterClass;
989 }
990
991 unsigned ResultReg = createResultReg(DstRC);
992 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
993 RVLocs[0].getLocReg(), DstRC, SrcRC);
994 assert(Emitted && "Failed to emit a copy instruction!");
995 if (CopyVT != RVLocs[0].getValVT()) {
996 // Round the F80 the right size, which also moves to the appropriate xmm
997 // register. This is accomplished by storing the F80 value in memory and
998 // then loading it back. Ewww...
999 MVT ResVT = RVLocs[0].getValVT();
1000 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1001 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001002 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001003 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1004 DstRC = ResVT == MVT::f32
1005 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1006 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1007 ResultReg = createResultReg(DstRC);
1008 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1009 }
1010
Evan Chengdebdea02008-09-08 17:15:42 +00001011 if (AndToI1) {
1012 // Mask out all but lowest bit for some call which produces an i1.
1013 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1014 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1015 ResultReg = AndResult;
1016 }
1017
Evan Chengf3d4efe2008-09-07 09:09:33 +00001018 UpdateValueMap(I, ResultReg);
1019 }
1020
1021 return true;
1022}
1023
1024
Dan Gohman99b21822008-08-28 23:21:34 +00001025bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001026X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001027 switch (I->getOpcode()) {
1028 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001029 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001030 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001031 case Instruction::Store:
1032 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001033 case Instruction::ICmp:
1034 case Instruction::FCmp:
1035 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001036 case Instruction::ZExt:
1037 return X86SelectZExt(I);
1038 case Instruction::Br:
1039 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001040 case Instruction::Call:
1041 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001042 case Instruction::LShr:
1043 case Instruction::AShr:
1044 case Instruction::Shl:
1045 return X86SelectShift(I);
1046 case Instruction::Select:
1047 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001048 case Instruction::Trunc:
1049 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001050 case Instruction::FPExt:
1051 return X86SelectFPExt(I);
1052 case Instruction::FPTrunc:
1053 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001054 }
1055
1056 return false;
1057}
1058
Dan Gohman0586d912008-09-10 20:11:02 +00001059unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001060 // Can't handle PIC-mode yet.
1061 if (TM.getRelocationModel() == Reloc::PIC_)
1062 return 0;
1063
Evan Cheng59fbc802008-09-09 01:26:59 +00001064 MVT VT;
1065 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001066 return false;
1067
1068 // Get opcode and regclass of the output for the given load instruction.
1069 unsigned Opc = 0;
1070 const TargetRegisterClass *RC = NULL;
1071 switch (VT.getSimpleVT()) {
1072 default: return false;
1073 case MVT::i8:
1074 Opc = X86::MOV8rm;
1075 RC = X86::GR8RegisterClass;
1076 break;
1077 case MVT::i16:
1078 Opc = X86::MOV16rm;
1079 RC = X86::GR16RegisterClass;
1080 break;
1081 case MVT::i32:
1082 Opc = X86::MOV32rm;
1083 RC = X86::GR32RegisterClass;
1084 break;
1085 case MVT::i64:
1086 // Must be in x86-64 mode.
1087 Opc = X86::MOV64rm;
1088 RC = X86::GR64RegisterClass;
1089 break;
1090 case MVT::f32:
1091 if (Subtarget->hasSSE1()) {
1092 Opc = X86::MOVSSrm;
1093 RC = X86::FR32RegisterClass;
1094 } else {
1095 Opc = X86::LD_Fp32m;
1096 RC = X86::RFP32RegisterClass;
1097 }
1098 break;
1099 case MVT::f64:
1100 if (Subtarget->hasSSE2()) {
1101 Opc = X86::MOVSDrm;
1102 RC = X86::FR64RegisterClass;
1103 } else {
1104 Opc = X86::LD_Fp64m;
1105 RC = X86::RFP64RegisterClass;
1106 }
1107 break;
1108 case MVT::f80:
1109 Opc = X86::LD_Fp80m;
1110 RC = X86::RFP80RegisterClass;
1111 break;
1112 }
1113
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001114 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001115 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001116 X86AddressMode AM;
1117 if (X86SelectAddress(C, AM, false)) {
1118 if (TLI.getPointerTy() == MVT::i32)
1119 Opc = X86::LEA32r;
1120 else
1121 Opc = X86::LEA64r;
1122 unsigned ResultReg = createResultReg(RC);
1123 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001124 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001125 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001126 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001127 }
1128
Owen Anderson3b217c62008-09-06 01:11:01 +00001129 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001130 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001131 if (Align == 0) {
1132 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001133 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001134 Align = Log2_64(Align);
1135 }
Owen Anderson95267a12008-09-05 00:06:23 +00001136
Dan Gohman0586d912008-09-10 20:11:02 +00001137 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001138 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001139 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001140 return ResultReg;
1141}
1142
Dan Gohman0586d912008-09-10 20:11:02 +00001143unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1144 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001145 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001146 return 0;
1147 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1148 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1149 unsigned ResultReg = createResultReg(RC);
1150 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1151 return ResultReg;
1152}
1153
Evan Chengc3f44b02008-09-03 00:03:49 +00001154namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001155 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1156 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001157 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1158 DenseMap<const AllocaInst *, int> &am) {
1159 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001160 }
Dan Gohman99b21822008-08-28 23:21:34 +00001161}