blob: 040eb0d4f7796ce21d195e9e037d8fe9fc6bf785 [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.
382 if (unsigned Reg = LocalValueMap[V])
383 return Reg;
384 // Issue load from stub if necessary.
385 unsigned Opc = 0;
386 const TargetRegisterClass *RC = NULL;
387 if (TLI.getPointerTy() == MVT::i32) {
388 Opc = X86::MOV32rm;
389 RC = X86::GR32RegisterClass;
390 } else {
391 Opc = X86::MOV64rm;
392 RC = X86::GR64RegisterClass;
393 }
394 AM.Base.Reg = createResultReg(RC);
395 X86AddressMode LocalAM;
396 LocalAM.GV = GV;
397 addFullAddress(BuildMI(MBB, TII.get(Opc), AM.Base.Reg), LocalAM);
398 // Prevent loading GV stub multiple times in same MBB.
399 LocalValueMap[V] = AM.Base.Reg;
400 } else {
401 AM.GV = GV;
402 }
403 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000404 }
405
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000406 // If all else fails, just materialize the value in a register.
407 AM.Base.Reg = getRegForValue(V);
408 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000409}
410
Owen Andersona3971df2008-09-04 07:08:58 +0000411/// X86SelectStore - Select and emit code to implement store instructions.
412bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000413 MVT VT;
414 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000415 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000416 unsigned Val = getRegForValue(I->getOperand(0));
417 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000418 // Unhandled operand. Halt "fast" selection and bail.
419 return false;
420
Dan Gohman0586d912008-09-10 20:11:02 +0000421 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000422 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000423 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000424
Dan Gohman0586d912008-09-10 20:11:02 +0000425 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000426}
427
Evan Cheng8b19e562008-09-03 06:44:39 +0000428/// X86SelectLoad - Select and emit code to implement load instructions.
429///
Dan Gohman3df24e62008-09-03 23:12:08 +0000430bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000431 MVT VT;
432 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000433 return false;
434
Dan Gohman0586d912008-09-10 20:11:02 +0000435 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000436 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000437 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000438
Evan Cheng0de588f2008-09-05 21:00:03 +0000439 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000440 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000441 UpdateValueMap(I, ResultReg);
442 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000443 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000444 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000445}
446
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000447bool X86FastISel::X86SelectCmp(Instruction *I) {
448 CmpInst *CI = cast<CmpInst>(I);
449
Dan Gohman4f22bb02008-09-05 01:33:56 +0000450 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
451 if (!TLI.isTypeLegal(VT))
452 return false;
453
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000454 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000455 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000456 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000457 if (Op1Reg == 0) return false;
458
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000459 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000460 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000461 case MVT::i8: Opc = X86::CMP8rr; break;
462 case MVT::i16: Opc = X86::CMP16rr; break;
463 case MVT::i32: Opc = X86::CMP32rr; break;
464 case MVT::i64: Opc = X86::CMP64rr; break;
465 case MVT::f32: Opc = X86::UCOMISSrr; break;
466 case MVT::f64: Opc = X86::UCOMISDrr; break;
467 default: return false;
468 }
469
470 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
471 switch (CI->getPredicate()) {
472 case CmpInst::FCMP_OEQ: {
473 unsigned EReg = createResultReg(&X86::GR8RegClass);
474 unsigned NPReg = createResultReg(&X86::GR8RegClass);
475 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
476 BuildMI(MBB, TII.get(X86::SETEr), EReg);
477 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
478 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
479 break;
480 }
481 case CmpInst::FCMP_UNE: {
482 unsigned NEReg = createResultReg(&X86::GR8RegClass);
483 unsigned PReg = createResultReg(&X86::GR8RegClass);
484 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
485 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
486 BuildMI(MBB, TII.get(X86::SETPr), PReg);
487 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
488 break;
489 }
490 case CmpInst::FCMP_OGT:
491 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
492 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
493 break;
494 case CmpInst::FCMP_OGE:
495 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
496 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
497 break;
498 case CmpInst::FCMP_OLT:
499 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
500 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
501 break;
502 case CmpInst::FCMP_OLE:
503 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
504 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
505 break;
506 case CmpInst::FCMP_ONE:
507 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
508 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
509 break;
510 case CmpInst::FCMP_ORD:
511 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
512 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
513 break;
514 case CmpInst::FCMP_UNO:
515 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
517 break;
518 case CmpInst::FCMP_UEQ:
519 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
520 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
521 break;
522 case CmpInst::FCMP_UGT:
523 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
524 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
525 break;
526 case CmpInst::FCMP_UGE:
527 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
528 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
529 break;
530 case CmpInst::FCMP_ULT:
531 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
532 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
533 break;
534 case CmpInst::FCMP_ULE:
535 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
536 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
537 break;
538 case CmpInst::ICMP_EQ:
539 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
540 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
541 break;
542 case CmpInst::ICMP_NE:
543 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
544 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
545 break;
546 case CmpInst::ICMP_UGT:
547 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
548 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
549 break;
550 case CmpInst::ICMP_UGE:
551 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
552 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
553 break;
554 case CmpInst::ICMP_ULT:
555 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
557 break;
558 case CmpInst::ICMP_ULE:
559 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
560 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
561 break;
562 case CmpInst::ICMP_SGT:
563 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
564 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
565 break;
566 case CmpInst::ICMP_SGE:
567 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
568 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
569 break;
570 case CmpInst::ICMP_SLT:
571 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
573 break;
574 case CmpInst::ICMP_SLE:
575 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
577 break;
578 default:
579 return false;
580 }
581
582 UpdateValueMap(I, ResultReg);
583 return true;
584}
Evan Cheng8b19e562008-09-03 06:44:39 +0000585
Dan Gohmand89ae992008-09-05 01:06:14 +0000586bool X86FastISel::X86SelectZExt(Instruction *I) {
587 // Special-case hack: The only i1 values we know how to produce currently
588 // set the upper bits of an i8 value to zero.
589 if (I->getType() == Type::Int8Ty &&
590 I->getOperand(0)->getType() == Type::Int1Ty) {
591 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000592 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000593 UpdateValueMap(I, ResultReg);
594 return true;
595 }
596
597 return false;
598}
599
600bool X86FastISel::X86SelectBranch(Instruction *I) {
601 BranchInst *BI = cast<BranchInst>(I);
602 // Unconditional branches are selected by tablegen-generated code.
603 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000604 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000605 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
606 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
607
608 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
609 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
610 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
611
612 MBB->addSuccessor(TrueMBB);
613 MBB->addSuccessor(FalseMBB);
614
615 return true;
616}
617
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000618bool X86FastISel::X86SelectShift(Instruction *I) {
619 unsigned CReg = 0;
620 unsigned Opc = 0;
621 const TargetRegisterClass *RC = NULL;
622 if (I->getType() == Type::Int8Ty) {
623 CReg = X86::CL;
624 RC = &X86::GR8RegClass;
625 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000626 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000627 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000628 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000629 default: return false;
630 }
631 } else if (I->getType() == Type::Int16Ty) {
632 CReg = X86::CX;
633 RC = &X86::GR16RegClass;
634 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000635 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000636 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000637 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000638 default: return false;
639 }
640 } else if (I->getType() == Type::Int32Ty) {
641 CReg = X86::ECX;
642 RC = &X86::GR32RegClass;
643 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000644 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000645 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000646 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000647 default: return false;
648 }
649 } else if (I->getType() == Type::Int64Ty) {
650 CReg = X86::RCX;
651 RC = &X86::GR64RegClass;
652 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000653 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000654 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000655 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000656 default: return false;
657 }
658 } else {
659 return false;
660 }
661
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000662 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
663 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
664 return false;
665
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000666 unsigned Op0Reg = getRegForValue(I->getOperand(0));
667 if (Op0Reg == 0) return false;
668 unsigned Op1Reg = getRegForValue(I->getOperand(1));
669 if (Op1Reg == 0) return false;
670 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
671 unsigned ResultReg = createResultReg(RC);
672 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
673 UpdateValueMap(I, ResultReg);
674 return true;
675}
676
677bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000678 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000679 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000680 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000681
682 unsigned Opc = 0;
683 const TargetRegisterClass *RC = NULL;
684 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000685 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000686 RC = &X86::GR16RegClass;
687 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000688 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000689 RC = &X86::GR32RegClass;
690 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000691 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000692 RC = &X86::GR64RegClass;
693 } else {
694 return false;
695 }
696
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000697 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
698 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
699 return false;
700
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000701 unsigned Op0Reg = getRegForValue(I->getOperand(0));
702 if (Op0Reg == 0) return false;
703 unsigned Op1Reg = getRegForValue(I->getOperand(1));
704 if (Op1Reg == 0) return false;
705 unsigned Op2Reg = getRegForValue(I->getOperand(2));
706 if (Op2Reg == 0) return false;
707
708 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
709 unsigned ResultReg = createResultReg(RC);
710 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
711 UpdateValueMap(I, ResultReg);
712 return true;
713}
714
Dan Gohman78efce62008-09-10 21:02:08 +0000715bool X86FastISel::X86SelectFPExt(Instruction *I) {
716 if (Subtarget->hasSSE2()) {
717 if (I->getType() == Type::DoubleTy) {
718 Value *V = I->getOperand(0);
719 if (V->getType() == Type::FloatTy) {
720 unsigned OpReg = getRegForValue(V);
721 if (OpReg == 0) return false;
722 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
723 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
724 UpdateValueMap(I, ResultReg);
725 return true;
726 }
727 }
728 }
729
730 return false;
731}
732
733bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
734 if (Subtarget->hasSSE2()) {
735 if (I->getType() == Type::FloatTy) {
736 Value *V = I->getOperand(0);
737 if (V->getType() == Type::DoubleTy) {
738 unsigned OpReg = getRegForValue(V);
739 if (OpReg == 0) return false;
740 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
741 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
742 UpdateValueMap(I, ResultReg);
743 return true;
744 }
745 }
746 }
747
748 return false;
749}
750
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000751bool X86FastISel::X86SelectTrunc(Instruction *I) {
752 if (Subtarget->is64Bit())
753 // All other cases should be handled by the tblgen generated code.
754 return false;
755 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
756 MVT DstVT = TLI.getValueType(I->getType());
757 if (DstVT != MVT::i8)
758 // All other cases should be handled by the tblgen generated code.
759 return false;
760 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
761 // All other cases should be handled by the tblgen generated code.
762 return false;
763
764 unsigned InputReg = getRegForValue(I->getOperand(0));
765 if (!InputReg)
766 // Unhandled operand. Halt "fast" selection and bail.
767 return false;
768
769 // First issue a copy to GR16_ or GR32_.
770 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
771 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
772 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
773 unsigned CopyReg = createResultReg(CopyRC);
774 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
775
776 // Then issue an extract_subreg.
777 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
778 if (!ResultReg)
779 return false;
780
781 UpdateValueMap(I, ResultReg);
782 return true;
783}
784
Evan Chengf3d4efe2008-09-07 09:09:33 +0000785bool X86FastISel::X86SelectCall(Instruction *I) {
786 CallInst *CI = cast<CallInst>(I);
787 Value *Callee = I->getOperand(0);
788
789 // Can't handle inline asm yet.
790 if (isa<InlineAsm>(Callee))
791 return false;
792
793 // FIXME: Handle some intrinsics.
794 if (Function *F = CI->getCalledFunction()) {
795 if (F->isDeclaration() &&F->getIntrinsicID())
796 return false;
797 }
798
Evan Chengf3d4efe2008-09-07 09:09:33 +0000799 // Handle only C and fastcc calling conventions for now.
800 CallSite CS(CI);
801 unsigned CC = CS.getCallingConv();
802 if (CC != CallingConv::C &&
803 CC != CallingConv::Fast &&
804 CC != CallingConv::X86_FastCall)
805 return false;
806
807 // Let SDISel handle vararg functions.
808 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
809 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
810 if (FTy->isVarArg())
811 return false;
812
813 // Handle *simple* calls for now.
814 const Type *RetTy = CS.getType();
815 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000816 if (RetTy == Type::VoidTy)
817 RetVT = MVT::isVoid;
818 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000819 return false;
820
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000821 // Materialize callee address in a register. FIXME: GV address can be
822 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000823 X86AddressMode CalleeAM;
824 if (!X86SelectAddress(Callee, CalleeAM, true))
825 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000826 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000827 GlobalValue *GV = 0;
828 if (CalleeAM.Base.Reg != 0) {
829 assert(CalleeAM.GV == 0);
830 CalleeOp = CalleeAM.Base.Reg;
831 } else if (CalleeAM.GV != 0) {
832 assert(CalleeAM.GV != 0);
833 GV = CalleeAM.GV;
834 } else
835 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000836
Evan Chengdebdea02008-09-08 17:15:42 +0000837 // Allow calls which produce i1 results.
838 bool AndToI1 = false;
839 if (RetVT == MVT::i1) {
840 RetVT = MVT::i8;
841 AndToI1 = true;
842 }
843
Evan Chengf3d4efe2008-09-07 09:09:33 +0000844 // Deal with call operands first.
845 SmallVector<unsigned, 4> Args;
846 SmallVector<MVT, 4> ArgVTs;
847 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
848 Args.reserve(CS.arg_size());
849 ArgVTs.reserve(CS.arg_size());
850 ArgFlags.reserve(CS.arg_size());
851 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
852 i != e; ++i) {
853 unsigned Arg = getRegForValue(*i);
854 if (Arg == 0)
855 return false;
856 ISD::ArgFlagsTy Flags;
857 unsigned AttrInd = i - CS.arg_begin() + 1;
858 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
859 Flags.setSExt();
860 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
861 Flags.setZExt();
862
863 // FIXME: Only handle *easy* calls for now.
864 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
865 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
866 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
867 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
868 return false;
869
870 const Type *ArgTy = (*i)->getType();
871 MVT ArgVT;
872 if (!isTypeLegal(ArgTy, TLI, ArgVT))
873 return false;
874 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
875 Flags.setOrigAlign(OriginalAlignment);
876
877 Args.push_back(Arg);
878 ArgVTs.push_back(ArgVT);
879 ArgFlags.push_back(Flags);
880 }
881
882 // Analyze operands of the call, assigning locations to each operand.
883 SmallVector<CCValAssign, 16> ArgLocs;
884 CCState CCInfo(CC, false, TM, ArgLocs);
885 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
886
887 // Get a count of how many bytes are to be pushed on the stack.
888 unsigned NumBytes = CCInfo.getNextStackOffset();
889
890 // Issue CALLSEQ_START
891 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
892
893 // Process argumenet: walk the register/memloc assignments, inserting
894 // copies / loads.
895 SmallVector<unsigned, 4> RegArgs;
896 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
897 CCValAssign &VA = ArgLocs[i];
898 unsigned Arg = Args[VA.getValNo()];
899 MVT ArgVT = ArgVTs[VA.getValNo()];
900
901 // Promote the value if needed.
902 switch (VA.getLocInfo()) {
903 default: assert(0 && "Unknown loc info!");
904 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000905 case CCValAssign::SExt: {
906 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
907 Arg, ArgVT, Arg);
908 assert(Emitted && "Failed to emit a sext!");
909 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000910 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000911 }
912 case CCValAssign::ZExt: {
913 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
914 Arg, ArgVT, Arg);
915 assert(Emitted && "Failed to emit a zext!");
916 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000917 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000918 }
919 case CCValAssign::AExt: {
920 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
921 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000922 if (!Emitted)
923 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
924 Arg, ArgVT, Arg);
925 if (!Emitted)
926 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
927 Arg, ArgVT, Arg);
928
Evan Cheng24e3a902008-09-08 06:35:17 +0000929 assert(Emitted && "Failed to emit a aext!");
930 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000931 break;
932 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000933 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000934
935 if (VA.isRegLoc()) {
936 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
937 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
938 Arg, RC, RC);
939 assert(Emitted && "Failed to emit a copy instruction!");
940 RegArgs.push_back(VA.getLocReg());
941 } else {
942 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000943 X86AddressMode AM;
944 AM.Base.Reg = StackPtr;
945 AM.Disp = LocMemOffset;
946 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000947 }
948 }
949
950 // Issue the call.
951 unsigned CallOpc = CalleeOp
952 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
953 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
954 MachineInstrBuilder MIB = CalleeOp
955 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000956 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000957 // Add implicit physical register uses to the call.
958 while (!RegArgs.empty()) {
959 MIB.addReg(RegArgs.back());
960 RegArgs.pop_back();
961 }
962
963 // Issue CALLSEQ_END
964 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
965
966 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000967 if (RetVT.getSimpleVT() != MVT::isVoid) {
968 SmallVector<CCValAssign, 16> RVLocs;
969 CCState CCInfo(CC, false, TM, RVLocs);
970 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
971
972 // Copy all of the result registers out of their specified physreg.
973 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
974 MVT CopyVT = RVLocs[0].getValVT();
975 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
976 TargetRegisterClass *SrcRC = DstRC;
977
978 // If this is a call to a function that returns an fp value on the x87 fp
979 // stack, but where we prefer to use the value in xmm registers, copy it
980 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
981 if ((RVLocs[0].getLocReg() == X86::ST0 ||
982 RVLocs[0].getLocReg() == X86::ST1) &&
983 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
984 CopyVT = MVT::f80;
985 SrcRC = X86::RSTRegisterClass;
986 DstRC = X86::RFP80RegisterClass;
987 }
988
989 unsigned ResultReg = createResultReg(DstRC);
990 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
991 RVLocs[0].getLocReg(), DstRC, SrcRC);
992 assert(Emitted && "Failed to emit a copy instruction!");
993 if (CopyVT != RVLocs[0].getValVT()) {
994 // Round the F80 the right size, which also moves to the appropriate xmm
995 // register. This is accomplished by storing the F80 value in memory and
996 // then loading it back. Ewww...
997 MVT ResVT = RVLocs[0].getValVT();
998 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
999 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001000 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001001 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1002 DstRC = ResVT == MVT::f32
1003 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1004 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1005 ResultReg = createResultReg(DstRC);
1006 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1007 }
1008
Evan Chengdebdea02008-09-08 17:15:42 +00001009 if (AndToI1) {
1010 // Mask out all but lowest bit for some call which produces an i1.
1011 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1012 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1013 ResultReg = AndResult;
1014 }
1015
Evan Chengf3d4efe2008-09-07 09:09:33 +00001016 UpdateValueMap(I, ResultReg);
1017 }
1018
1019 return true;
1020}
1021
1022
Dan Gohman99b21822008-08-28 23:21:34 +00001023bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001024X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001025 switch (I->getOpcode()) {
1026 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001027 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001028 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001029 case Instruction::Store:
1030 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001031 case Instruction::ICmp:
1032 case Instruction::FCmp:
1033 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001034 case Instruction::ZExt:
1035 return X86SelectZExt(I);
1036 case Instruction::Br:
1037 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001038 case Instruction::Call:
1039 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001040 case Instruction::LShr:
1041 case Instruction::AShr:
1042 case Instruction::Shl:
1043 return X86SelectShift(I);
1044 case Instruction::Select:
1045 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001046 case Instruction::Trunc:
1047 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001048 case Instruction::FPExt:
1049 return X86SelectFPExt(I);
1050 case Instruction::FPTrunc:
1051 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001052 }
1053
1054 return false;
1055}
1056
Dan Gohman0586d912008-09-10 20:11:02 +00001057unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001058 // Can't handle PIC-mode yet.
1059 if (TM.getRelocationModel() == Reloc::PIC_)
1060 return 0;
1061
Evan Cheng59fbc802008-09-09 01:26:59 +00001062 MVT VT;
1063 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001064 return false;
1065
1066 // Get opcode and regclass of the output for the given load instruction.
1067 unsigned Opc = 0;
1068 const TargetRegisterClass *RC = NULL;
1069 switch (VT.getSimpleVT()) {
1070 default: return false;
1071 case MVT::i8:
1072 Opc = X86::MOV8rm;
1073 RC = X86::GR8RegisterClass;
1074 break;
1075 case MVT::i16:
1076 Opc = X86::MOV16rm;
1077 RC = X86::GR16RegisterClass;
1078 break;
1079 case MVT::i32:
1080 Opc = X86::MOV32rm;
1081 RC = X86::GR32RegisterClass;
1082 break;
1083 case MVT::i64:
1084 // Must be in x86-64 mode.
1085 Opc = X86::MOV64rm;
1086 RC = X86::GR64RegisterClass;
1087 break;
1088 case MVT::f32:
1089 if (Subtarget->hasSSE1()) {
1090 Opc = X86::MOVSSrm;
1091 RC = X86::FR32RegisterClass;
1092 } else {
1093 Opc = X86::LD_Fp32m;
1094 RC = X86::RFP32RegisterClass;
1095 }
1096 break;
1097 case MVT::f64:
1098 if (Subtarget->hasSSE2()) {
1099 Opc = X86::MOVSDrm;
1100 RC = X86::FR64RegisterClass;
1101 } else {
1102 Opc = X86::LD_Fp64m;
1103 RC = X86::RFP64RegisterClass;
1104 }
1105 break;
1106 case MVT::f80:
1107 Opc = X86::LD_Fp80m;
1108 RC = X86::RFP80RegisterClass;
1109 break;
1110 }
1111
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001112 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001113 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001114 X86AddressMode AM;
1115 if (X86SelectAddress(C, AM, false)) {
1116 if (TLI.getPointerTy() == MVT::i32)
1117 Opc = X86::LEA32r;
1118 else
1119 Opc = X86::LEA64r;
1120 unsigned ResultReg = createResultReg(RC);
1121 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001122 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001123 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001124 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001125 }
1126
Owen Anderson3b217c62008-09-06 01:11:01 +00001127 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001128 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001129 if (Align == 0) {
1130 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001131 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001132 Align = Log2_64(Align);
1133 }
Owen Anderson95267a12008-09-05 00:06:23 +00001134
Dan Gohman0586d912008-09-10 20:11:02 +00001135 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001136 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001137 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001138 return ResultReg;
1139}
1140
Dan Gohman0586d912008-09-10 20:11:02 +00001141unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1142 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001143 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001144 return 0;
1145 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1146 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1147 unsigned ResultReg = createResultReg(RC);
1148 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1149 return ResultReg;
1150}
1151
Evan Chengc3f44b02008-09-03 00:03:49 +00001152namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001153 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1154 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001155 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1156 DenseMap<const AllocaInst *, int> &am) {
1157 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001158 }
Dan Gohman99b21822008-08-28 23:21:34 +00001159}