blob: a820c26ee35885977efc97237b56fe5e0679c4b1 [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
Dan Gohman2cc3aa42008-09-25 15:24:26 +000043 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
44 /// base register.
45 unsigned GlobalBaseReg;
46
Evan Chengf3d4efe2008-09-07 09:09:33 +000047 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
51 bool X86ScalarSSEf64;
52 bool X86ScalarSSEf32;
53
Evan Cheng8b19e562008-09-03 06:44:39 +000054public:
Dan Gohman3df24e62008-09-03 23:12:08 +000055 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000056 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +000057 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000058 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
59 DenseMap<const AllocaInst *, int> &am)
Dan Gohmand57dd5f2008-09-23 21:53:34 +000060 : FastISel(mf, mmi, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000062 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Dan Gohman2cc3aa42008-09-25 15:24:26 +000063 GlobalBaseReg = 0;
Evan Chengf3d4efe2008-09-07 09:09:33 +000064 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000066 }
Evan Chengc3f44b02008-09-03 00:03:49 +000067
Dan Gohman3df24e62008-09-03 23:12:08 +000068 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000069
Dan Gohman1adf1b02008-08-19 21:45:35 +000070#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000071
72private:
Dan Gohman0586d912008-09-10 20:11:02 +000073 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000074
Evan Chengf3d4efe2008-09-07 09:09:33 +000075 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000076 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000077
78 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
79 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman2ff7fd12008-09-19 22:16:54 +000081 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000082
Dan Gohman3df24e62008-09-03 23:12:08 +000083 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000084
85 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000086
87 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000088
89 bool X86SelectZExt(Instruction *I);
90
91 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000092
93 bool X86SelectShift(Instruction *I);
94
95 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000096
Evan Cheng10a8d9c2008-09-07 08:47:42 +000097 bool X86SelectTrunc(Instruction *I);
98
Dan Gohman78efce62008-09-10 21:02:08 +000099 bool X86SelectFPExt(Instruction *I);
100 bool X86SelectFPTrunc(Instruction *I);
101
Evan Chengf3d4efe2008-09-07 09:09:33 +0000102 bool X86SelectCall(Instruction *I);
103
104 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
105
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000106 unsigned getGlobalBaseReg();
107
108 const X86InstrInfo *getInstrInfo() const {
109 return static_cast<const X86InstrInfo *>(TM.getInstrInfo());
110 }
111
Dan Gohman0586d912008-09-10 20:11:02 +0000112 unsigned TargetMaterializeConstant(Constant *C);
113
114 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115
116 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
117 /// computed in an SSE register, not on the X87 floating point stack.
118 bool isScalarFPTypeInSSEReg(MVT VT) const {
119 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
120 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
121 }
122
Evan Chengc3f44b02008-09-03 00:03:49 +0000123};
Dan Gohman99b21822008-08-28 23:21:34 +0000124
Evan Chengdebdea02008-09-08 17:15:42 +0000125static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
126 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
128 if (VT == MVT::Other || !VT.isSimple())
129 // Unhandled type. Halt "fast" selection and bail.
130 return false;
131 if (VT == MVT::iPTR)
132 // Use pointer type.
133 VT = TLI.getPointerTy();
134 // We only handle legal types. For example, on x86-32 the instruction
135 // selector contains all of the 64-bit instructions from x86-64,
136 // under the assumption that i64 won't be used if the target doesn't
137 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000138 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139}
140
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000141/// getGlobalBaseReg - Return the the global base register. Output
142/// instructions required to initialize the global base register, if necessary.
143///
144unsigned X86FastISel::getGlobalBaseReg() {
145 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
146 if (!GlobalBaseReg)
147 GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
148 return GlobalBaseReg;
149}
150
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151#include "X86GenCallingConv.inc"
152
153/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
154/// convention.
155CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
156 if (Subtarget->is64Bit()) {
157 if (Subtarget->isTargetWin64())
158 return CC_X86_Win64_C;
159 else if (CC == CallingConv::Fast && isTaillCall)
160 return CC_X86_64_TailCall;
161 else
162 return CC_X86_64_C;
163 }
164
165 if (CC == CallingConv::X86_FastCall)
166 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167 else if (CC == CallingConv::Fast)
168 return CC_X86_32_FastCC;
169 else
170 return CC_X86_32_C;
171}
172
Evan Cheng0de588f2008-09-05 21:00:03 +0000173/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000176bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000177 unsigned &ResultReg) {
178 // Get opcode and regclass of the output for the given load instruction.
179 unsigned Opc = 0;
180 const TargetRegisterClass *RC = NULL;
181 switch (VT.getSimpleVT()) {
182 default: return false;
183 case MVT::i8:
184 Opc = X86::MOV8rm;
185 RC = X86::GR8RegisterClass;
186 break;
187 case MVT::i16:
188 Opc = X86::MOV16rm;
189 RC = X86::GR16RegisterClass;
190 break;
191 case MVT::i32:
192 Opc = X86::MOV32rm;
193 RC = X86::GR32RegisterClass;
194 break;
195 case MVT::i64:
196 // Must be in x86-64 mode.
197 Opc = X86::MOV64rm;
198 RC = X86::GR64RegisterClass;
199 break;
200 case MVT::f32:
201 if (Subtarget->hasSSE1()) {
202 Opc = X86::MOVSSrm;
203 RC = X86::FR32RegisterClass;
204 } else {
205 Opc = X86::LD_Fp32m;
206 RC = X86::RFP32RegisterClass;
207 }
208 break;
209 case MVT::f64:
210 if (Subtarget->hasSSE2()) {
211 Opc = X86::MOVSDrm;
212 RC = X86::FR64RegisterClass;
213 } else {
214 Opc = X86::LD_Fp64m;
215 RC = X86::RFP64RegisterClass;
216 }
217 break;
218 case MVT::f80:
219 Opc = X86::LD_Fp80m;
220 RC = X86::RFP80RegisterClass;
221 break;
222 }
223
224 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000225 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
226 return true;
227}
228
Evan Chengf3d4efe2008-09-07 09:09:33 +0000229/// X86FastEmitStore - Emit a machine instruction to store a value Val of
230/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
231/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000232/// i.e. V. Return true if it is possible.
233bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000234X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000235 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000236 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 unsigned Opc = 0;
238 const TargetRegisterClass *RC = NULL;
239 switch (VT.getSimpleVT()) {
240 default: return false;
241 case MVT::i8:
242 Opc = X86::MOV8mr;
243 RC = X86::GR8RegisterClass;
244 break;
245 case MVT::i16:
246 Opc = X86::MOV16mr;
247 RC = X86::GR16RegisterClass;
248 break;
249 case MVT::i32:
250 Opc = X86::MOV32mr;
251 RC = X86::GR32RegisterClass;
252 break;
253 case MVT::i64:
254 // Must be in x86-64 mode.
255 Opc = X86::MOV64mr;
256 RC = X86::GR64RegisterClass;
257 break;
258 case MVT::f32:
259 if (Subtarget->hasSSE1()) {
260 Opc = X86::MOVSSmr;
261 RC = X86::FR32RegisterClass;
262 } else {
263 Opc = X86::ST_Fp32m;
264 RC = X86::RFP32RegisterClass;
265 }
266 break;
267 case MVT::f64:
268 if (Subtarget->hasSSE2()) {
269 Opc = X86::MOVSDmr;
270 RC = X86::FR64RegisterClass;
271 } else {
272 Opc = X86::ST_Fp64m;
273 RC = X86::RFP64RegisterClass;
274 }
275 break;
276 case MVT::f80:
277 Opc = X86::ST_FP80m;
278 RC = X86::RFP80RegisterClass;
279 break;
280 }
281
Evan Chengf3d4efe2008-09-07 09:09:33 +0000282 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000283 return true;
284}
285
Evan Cheng24e3a902008-09-08 06:35:17 +0000286/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
287/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
288/// ISD::SIGN_EXTEND).
289bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
290 unsigned Src, MVT SrcVT,
291 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000292 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
293
294 if (RR != 0) {
295 ResultReg = RR;
296 return true;
297 } else
298 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000299}
300
Dan Gohman0586d912008-09-10 20:11:02 +0000301/// X86SelectAddress - Attempt to fill in an address from the given value.
302///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000303bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000304 User *U;
305 unsigned Opcode = Instruction::UserOp1;
306 if (Instruction *I = dyn_cast<Instruction>(V)) {
307 Opcode = I->getOpcode();
308 U = I;
309 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
310 Opcode = C->getOpcode();
311 U = C;
312 }
Dan Gohman0586d912008-09-10 20:11:02 +0000313
Dan Gohman35893082008-09-18 23:23:44 +0000314 switch (Opcode) {
315 default: break;
316 case Instruction::BitCast:
317 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000318 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000319
320 case Instruction::IntToPtr:
321 // Look past no-op inttoptrs.
322 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000323 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000324
325 case Instruction::PtrToInt:
326 // Look past no-op ptrtoints.
327 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000328 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000329
330 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000331 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000332 // Do static allocas.
333 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000334 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
335 if (SI == StaticAllocaMap.end())
336 return false;
337 AM.BaseType = X86AddressMode::FrameIndexBase;
338 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000339 return true;
340 }
341
342 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000343 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000344 // Adds of constants are common and easy enough.
345 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
346 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000347 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000348 }
Dan Gohman35893082008-09-18 23:23:44 +0000349 break;
350 }
351
352 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000353 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000354 // Pattern-match simple GEPs.
355 uint64_t Disp = AM.Disp;
356 unsigned IndexReg = AM.IndexReg;
357 unsigned Scale = AM.Scale;
358 gep_type_iterator GTI = gep_type_begin(U);
359 // Look at all but the last index. Constants can be folded,
360 // and one dynamic index can be handled, if the scale is supported.
361 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
362 i != e; ++i, ++GTI) {
363 Value *Op = *i;
364 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
365 const StructLayout *SL = TD.getStructLayout(STy);
366 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
367 Disp += SL->getElementOffset(Idx);
368 } else {
369 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
370 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
371 // Constant-offset addressing.
372 Disp += CI->getZExtValue() * S;
373 } else if (IndexReg == 0 &&
374 (S == 1 || S == 2 || S == 4 || S == 8)) {
375 // Scaled-index addressing.
376 Scale = S;
377 IndexReg = getRegForValue(Op);
378 if (IndexReg == 0)
379 return false;
380 } else
381 // Unsupported.
382 goto unsupported_gep;
383 }
384 }
385 // Ok, the GEP indices were covered by constant-offset and scaled-index
386 // addressing. Update the address state and move on to examining the base.
387 AM.IndexReg = IndexReg;
388 AM.Scale = Scale;
389 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000390 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000391 unsupported_gep:
392 // Ok, the GEP indices weren't all covered.
393 break;
394 }
395 }
396
397 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000398 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000399 // Can't handle alternate code models yet.
400 if (TM.getCodeModel() != CodeModel::Default &&
401 TM.getCodeModel() != CodeModel::Small)
402 return false;
403
404 // Set up the basic address.
405 AM.GV = GV;
406 if (!isCall &&
407 TM.getRelocationModel() == Reloc::PIC_ &&
408 !Subtarget->is64Bit())
409 AM.Base.Reg = getGlobalBaseReg();
410
411 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000412 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
413 // Check to see if we've already materialized this
414 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000415 if (unsigned Reg = LocalValueMap[V]) {
416 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000417 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000418 return true;
419 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000420 // Issue load from stub if necessary.
421 unsigned Opc = 0;
422 const TargetRegisterClass *RC = NULL;
423 if (TLI.getPointerTy() == MVT::i32) {
424 Opc = X86::MOV32rm;
425 RC = X86::GR32RegisterClass;
426 } else {
427 Opc = X86::MOV64rm;
428 RC = X86::GR64RegisterClass;
429 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000430 unsigned ResultReg = createResultReg(RC);
431 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
432 AM.Base.Reg = ResultReg;
433 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000434 // Prevent loading GV stub multiple times in same MBB.
435 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000436 }
437 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000438 }
439
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000440 // If all else fails, just materialize the value in a register.
441 AM.Base.Reg = getRegForValue(V);
442 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000443}
444
Owen Andersona3971df2008-09-04 07:08:58 +0000445/// X86SelectStore - Select and emit code to implement store instructions.
446bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000447 MVT VT;
448 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000449 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000450 unsigned Val = getRegForValue(I->getOperand(0));
451 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000452 // Unhandled operand. Halt "fast" selection and bail.
453 return false;
454
Dan Gohman0586d912008-09-10 20:11:02 +0000455 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000456 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000457 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000458
Dan Gohman0586d912008-09-10 20:11:02 +0000459 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000460}
461
Evan Cheng8b19e562008-09-03 06:44:39 +0000462/// X86SelectLoad - Select and emit code to implement load instructions.
463///
Dan Gohman3df24e62008-09-03 23:12:08 +0000464bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000465 MVT VT;
466 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000467 return false;
468
Dan Gohman0586d912008-09-10 20:11:02 +0000469 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000470 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000471 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000472
Evan Cheng0de588f2008-09-05 21:00:03 +0000473 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000474 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000475 UpdateValueMap(I, ResultReg);
476 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000477 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000478 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000479}
480
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000481bool X86FastISel::X86SelectCmp(Instruction *I) {
482 CmpInst *CI = cast<CmpInst>(I);
483
Dan Gohman4f22bb02008-09-05 01:33:56 +0000484 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
485 if (!TLI.isTypeLegal(VT))
486 return false;
487
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000488 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000489 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000490 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000491 if (Op1Reg == 0) return false;
492
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000493 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000494 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000495 case MVT::i8: Opc = X86::CMP8rr; break;
496 case MVT::i16: Opc = X86::CMP16rr; break;
497 case MVT::i32: Opc = X86::CMP32rr; break;
498 case MVT::i64: Opc = X86::CMP64rr; break;
499 case MVT::f32: Opc = X86::UCOMISSrr; break;
500 case MVT::f64: Opc = X86::UCOMISDrr; break;
501 default: return false;
502 }
503
504 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
505 switch (CI->getPredicate()) {
506 case CmpInst::FCMP_OEQ: {
507 unsigned EReg = createResultReg(&X86::GR8RegClass);
508 unsigned NPReg = createResultReg(&X86::GR8RegClass);
509 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
510 BuildMI(MBB, TII.get(X86::SETEr), EReg);
511 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
512 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
513 break;
514 }
515 case CmpInst::FCMP_UNE: {
516 unsigned NEReg = createResultReg(&X86::GR8RegClass);
517 unsigned PReg = createResultReg(&X86::GR8RegClass);
518 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
519 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
520 BuildMI(MBB, TII.get(X86::SETPr), PReg);
521 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
522 break;
523 }
524 case CmpInst::FCMP_OGT:
525 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
526 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
527 break;
528 case CmpInst::FCMP_OGE:
529 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
530 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
531 break;
532 case CmpInst::FCMP_OLT:
533 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
534 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
535 break;
536 case CmpInst::FCMP_OLE:
537 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
538 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
539 break;
540 case CmpInst::FCMP_ONE:
541 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
542 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
543 break;
544 case CmpInst::FCMP_ORD:
545 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
546 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
547 break;
548 case CmpInst::FCMP_UNO:
549 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
550 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
551 break;
552 case CmpInst::FCMP_UEQ:
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
555 break;
556 case CmpInst::FCMP_UGT:
557 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
558 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
559 break;
560 case CmpInst::FCMP_UGE:
561 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
562 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
563 break;
564 case CmpInst::FCMP_ULT:
565 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
566 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
567 break;
568 case CmpInst::FCMP_ULE:
569 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
570 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
571 break;
572 case CmpInst::ICMP_EQ:
573 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
574 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
575 break;
576 case CmpInst::ICMP_NE:
577 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
578 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
579 break;
580 case CmpInst::ICMP_UGT:
581 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
582 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
583 break;
584 case CmpInst::ICMP_UGE:
585 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
586 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
587 break;
588 case CmpInst::ICMP_ULT:
589 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
590 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
591 break;
592 case CmpInst::ICMP_ULE:
593 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
594 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
595 break;
596 case CmpInst::ICMP_SGT:
597 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
598 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
599 break;
600 case CmpInst::ICMP_SGE:
601 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
602 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
603 break;
604 case CmpInst::ICMP_SLT:
605 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
606 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
607 break;
608 case CmpInst::ICMP_SLE:
609 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
610 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
611 break;
612 default:
613 return false;
614 }
615
616 UpdateValueMap(I, ResultReg);
617 return true;
618}
Evan Cheng8b19e562008-09-03 06:44:39 +0000619
Dan Gohmand89ae992008-09-05 01:06:14 +0000620bool X86FastISel::X86SelectZExt(Instruction *I) {
621 // Special-case hack: The only i1 values we know how to produce currently
622 // set the upper bits of an i8 value to zero.
623 if (I->getType() == Type::Int8Ty &&
624 I->getOperand(0)->getType() == Type::Int1Ty) {
625 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000626 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000627 UpdateValueMap(I, ResultReg);
628 return true;
629 }
630
631 return false;
632}
633
634bool X86FastISel::X86SelectBranch(Instruction *I) {
635 BranchInst *BI = cast<BranchInst>(I);
636 // Unconditional branches are selected by tablegen-generated code.
637 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000638 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000639 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
640 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
641
642 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
643 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
644 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
645
646 MBB->addSuccessor(TrueMBB);
647 MBB->addSuccessor(FalseMBB);
648
649 return true;
650}
651
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000652bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000653 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000654 const TargetRegisterClass *RC = NULL;
655 if (I->getType() == Type::Int8Ty) {
656 CReg = X86::CL;
657 RC = &X86::GR8RegClass;
658 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000659 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
660 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
661 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000662 default: return false;
663 }
664 } else if (I->getType() == Type::Int16Ty) {
665 CReg = X86::CX;
666 RC = &X86::GR16RegClass;
667 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000668 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
669 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
670 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000671 default: return false;
672 }
673 } else if (I->getType() == Type::Int32Ty) {
674 CReg = X86::ECX;
675 RC = &X86::GR32RegClass;
676 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000677 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
678 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
679 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000680 default: return false;
681 }
682 } else if (I->getType() == Type::Int64Ty) {
683 CReg = X86::RCX;
684 RC = &X86::GR64RegClass;
685 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000686 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
687 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
688 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000689 default: return false;
690 }
691 } else {
692 return false;
693 }
694
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000695 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
696 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
697 return false;
698
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000699 unsigned Op0Reg = getRegForValue(I->getOperand(0));
700 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000701
702 // Fold immediate in shl(x,3).
703 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
704 unsigned ResultReg = createResultReg(RC);
705 BuildMI(MBB, TII.get(OpImm),
706 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
707 UpdateValueMap(I, ResultReg);
708 return true;
709 }
710
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000711 unsigned Op1Reg = getRegForValue(I->getOperand(1));
712 if (Op1Reg == 0) return false;
713 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
714 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000715 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000716 UpdateValueMap(I, ResultReg);
717 return true;
718}
719
720bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000721 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000722 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000723 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000724
725 unsigned Opc = 0;
726 const TargetRegisterClass *RC = NULL;
727 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000728 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000729 RC = &X86::GR16RegClass;
730 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000731 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000732 RC = &X86::GR32RegClass;
733 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000734 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000735 RC = &X86::GR64RegClass;
736 } else {
737 return false;
738 }
739
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000740 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
741 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
742 return false;
743
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000744 unsigned Op0Reg = getRegForValue(I->getOperand(0));
745 if (Op0Reg == 0) return false;
746 unsigned Op1Reg = getRegForValue(I->getOperand(1));
747 if (Op1Reg == 0) return false;
748 unsigned Op2Reg = getRegForValue(I->getOperand(2));
749 if (Op2Reg == 0) return false;
750
751 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
752 unsigned ResultReg = createResultReg(RC);
753 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
754 UpdateValueMap(I, ResultReg);
755 return true;
756}
757
Dan Gohman78efce62008-09-10 21:02:08 +0000758bool X86FastISel::X86SelectFPExt(Instruction *I) {
759 if (Subtarget->hasSSE2()) {
760 if (I->getType() == Type::DoubleTy) {
761 Value *V = I->getOperand(0);
762 if (V->getType() == Type::FloatTy) {
763 unsigned OpReg = getRegForValue(V);
764 if (OpReg == 0) return false;
765 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
766 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
767 UpdateValueMap(I, ResultReg);
768 return true;
769 }
770 }
771 }
772
773 return false;
774}
775
776bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
777 if (Subtarget->hasSSE2()) {
778 if (I->getType() == Type::FloatTy) {
779 Value *V = I->getOperand(0);
780 if (V->getType() == Type::DoubleTy) {
781 unsigned OpReg = getRegForValue(V);
782 if (OpReg == 0) return false;
783 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
784 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
785 UpdateValueMap(I, ResultReg);
786 return true;
787 }
788 }
789 }
790
791 return false;
792}
793
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000794bool X86FastISel::X86SelectTrunc(Instruction *I) {
795 if (Subtarget->is64Bit())
796 // All other cases should be handled by the tblgen generated code.
797 return false;
798 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
799 MVT DstVT = TLI.getValueType(I->getType());
800 if (DstVT != MVT::i8)
801 // All other cases should be handled by the tblgen generated code.
802 return false;
803 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
804 // All other cases should be handled by the tblgen generated code.
805 return false;
806
807 unsigned InputReg = getRegForValue(I->getOperand(0));
808 if (!InputReg)
809 // Unhandled operand. Halt "fast" selection and bail.
810 return false;
811
812 // First issue a copy to GR16_ or GR32_.
813 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
814 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
815 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
816 unsigned CopyReg = createResultReg(CopyRC);
817 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
818
819 // Then issue an extract_subreg.
820 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
821 if (!ResultReg)
822 return false;
823
824 UpdateValueMap(I, ResultReg);
825 return true;
826}
827
Evan Chengf3d4efe2008-09-07 09:09:33 +0000828bool X86FastISel::X86SelectCall(Instruction *I) {
829 CallInst *CI = cast<CallInst>(I);
830 Value *Callee = I->getOperand(0);
831
832 // Can't handle inline asm yet.
833 if (isa<InlineAsm>(Callee))
834 return false;
835
836 // FIXME: Handle some intrinsics.
837 if (Function *F = CI->getCalledFunction()) {
838 if (F->isDeclaration() &&F->getIntrinsicID())
839 return false;
840 }
841
Evan Chengf3d4efe2008-09-07 09:09:33 +0000842 // Handle only C and fastcc calling conventions for now.
843 CallSite CS(CI);
844 unsigned CC = CS.getCallingConv();
845 if (CC != CallingConv::C &&
846 CC != CallingConv::Fast &&
847 CC != CallingConv::X86_FastCall)
848 return false;
849
850 // Let SDISel handle vararg functions.
851 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
852 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
853 if (FTy->isVarArg())
854 return false;
855
856 // Handle *simple* calls for now.
857 const Type *RetTy = CS.getType();
858 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000859 if (RetTy == Type::VoidTy)
860 RetVT = MVT::isVoid;
861 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000862 return false;
863
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000864 // Materialize callee address in a register. FIXME: GV address can be
865 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000866 X86AddressMode CalleeAM;
867 if (!X86SelectAddress(Callee, CalleeAM, true))
868 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000869 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000870 GlobalValue *GV = 0;
871 if (CalleeAM.Base.Reg != 0) {
872 assert(CalleeAM.GV == 0);
873 CalleeOp = CalleeAM.Base.Reg;
874 } else if (CalleeAM.GV != 0) {
875 assert(CalleeAM.GV != 0);
876 GV = CalleeAM.GV;
877 } else
878 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000879
Evan Chengdebdea02008-09-08 17:15:42 +0000880 // Allow calls which produce i1 results.
881 bool AndToI1 = false;
882 if (RetVT == MVT::i1) {
883 RetVT = MVT::i8;
884 AndToI1 = true;
885 }
886
Evan Chengf3d4efe2008-09-07 09:09:33 +0000887 // Deal with call operands first.
888 SmallVector<unsigned, 4> Args;
889 SmallVector<MVT, 4> ArgVTs;
890 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
891 Args.reserve(CS.arg_size());
892 ArgVTs.reserve(CS.arg_size());
893 ArgFlags.reserve(CS.arg_size());
894 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
895 i != e; ++i) {
896 unsigned Arg = getRegForValue(*i);
897 if (Arg == 0)
898 return false;
899 ISD::ArgFlagsTy Flags;
900 unsigned AttrInd = i - CS.arg_begin() + 1;
901 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
902 Flags.setSExt();
903 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
904 Flags.setZExt();
905
906 // FIXME: Only handle *easy* calls for now.
907 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
908 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
909 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
910 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
911 return false;
912
913 const Type *ArgTy = (*i)->getType();
914 MVT ArgVT;
915 if (!isTypeLegal(ArgTy, TLI, ArgVT))
916 return false;
917 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
918 Flags.setOrigAlign(OriginalAlignment);
919
920 Args.push_back(Arg);
921 ArgVTs.push_back(ArgVT);
922 ArgFlags.push_back(Flags);
923 }
924
925 // Analyze operands of the call, assigning locations to each operand.
926 SmallVector<CCValAssign, 16> ArgLocs;
927 CCState CCInfo(CC, false, TM, ArgLocs);
928 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
929
930 // Get a count of how many bytes are to be pushed on the stack.
931 unsigned NumBytes = CCInfo.getNextStackOffset();
932
933 // Issue CALLSEQ_START
934 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
935
936 // Process argumenet: walk the register/memloc assignments, inserting
937 // copies / loads.
938 SmallVector<unsigned, 4> RegArgs;
939 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
940 CCValAssign &VA = ArgLocs[i];
941 unsigned Arg = Args[VA.getValNo()];
942 MVT ArgVT = ArgVTs[VA.getValNo()];
943
944 // Promote the value if needed.
945 switch (VA.getLocInfo()) {
946 default: assert(0 && "Unknown loc info!");
947 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000948 case CCValAssign::SExt: {
949 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
950 Arg, ArgVT, Arg);
951 assert(Emitted && "Failed to emit a sext!");
952 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000953 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000954 }
955 case CCValAssign::ZExt: {
956 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
957 Arg, ArgVT, Arg);
958 assert(Emitted && "Failed to emit a zext!");
959 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000960 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000961 }
962 case CCValAssign::AExt: {
963 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
964 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000965 if (!Emitted)
966 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
967 Arg, ArgVT, Arg);
968 if (!Emitted)
969 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
970 Arg, ArgVT, Arg);
971
Evan Cheng24e3a902008-09-08 06:35:17 +0000972 assert(Emitted && "Failed to emit a aext!");
973 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000974 break;
975 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000976 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000977
978 if (VA.isRegLoc()) {
979 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
980 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
981 Arg, RC, RC);
982 assert(Emitted && "Failed to emit a copy instruction!");
983 RegArgs.push_back(VA.getLocReg());
984 } else {
985 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000986 X86AddressMode AM;
987 AM.Base.Reg = StackPtr;
988 AM.Disp = LocMemOffset;
989 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000990 }
991 }
992
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000993 // ELF / PIC requires GOT in the EBX register before function calls via PLT
994 // GOT pointer.
995 if (!Subtarget->is64Bit() &&
996 TM.getRelocationModel() == Reloc::PIC_ &&
997 Subtarget->isPICStyleGOT()) {
998 TargetRegisterClass *RC = X86::GR32RegisterClass;
999 unsigned Base = getGlobalBaseReg();
1000 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1001 assert(Emitted && "Failed to emit a copy instruction!");
1002 }
1003
Evan Chengf3d4efe2008-09-07 09:09:33 +00001004 // Issue the call.
1005 unsigned CallOpc = CalleeOp
1006 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1007 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1008 MachineInstrBuilder MIB = CalleeOp
1009 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001010 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001011
1012 // Add an implicit use GOT pointer in EBX.
1013 if (!Subtarget->is64Bit() &&
1014 TM.getRelocationModel() == Reloc::PIC_ &&
1015 Subtarget->isPICStyleGOT())
1016 MIB.addReg(X86::EBX);
1017
Evan Chengf3d4efe2008-09-07 09:09:33 +00001018 // Add implicit physical register uses to the call.
1019 while (!RegArgs.empty()) {
1020 MIB.addReg(RegArgs.back());
1021 RegArgs.pop_back();
1022 }
1023
1024 // Issue CALLSEQ_END
1025 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1026
1027 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001028 if (RetVT.getSimpleVT() != MVT::isVoid) {
1029 SmallVector<CCValAssign, 16> RVLocs;
1030 CCState CCInfo(CC, false, TM, RVLocs);
1031 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1032
1033 // Copy all of the result registers out of their specified physreg.
1034 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1035 MVT CopyVT = RVLocs[0].getValVT();
1036 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1037 TargetRegisterClass *SrcRC = DstRC;
1038
1039 // If this is a call to a function that returns an fp value on the x87 fp
1040 // stack, but where we prefer to use the value in xmm registers, copy it
1041 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1042 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1043 RVLocs[0].getLocReg() == X86::ST1) &&
1044 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1045 CopyVT = MVT::f80;
1046 SrcRC = X86::RSTRegisterClass;
1047 DstRC = X86::RFP80RegisterClass;
1048 }
1049
1050 unsigned ResultReg = createResultReg(DstRC);
1051 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1052 RVLocs[0].getLocReg(), DstRC, SrcRC);
1053 assert(Emitted && "Failed to emit a copy instruction!");
1054 if (CopyVT != RVLocs[0].getValVT()) {
1055 // Round the F80 the right size, which also moves to the appropriate xmm
1056 // register. This is accomplished by storing the F80 value in memory and
1057 // then loading it back. Ewww...
1058 MVT ResVT = RVLocs[0].getValVT();
1059 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1060 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001061 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001062 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1063 DstRC = ResVT == MVT::f32
1064 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1065 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1066 ResultReg = createResultReg(DstRC);
1067 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1068 }
1069
Evan Chengdebdea02008-09-08 17:15:42 +00001070 if (AndToI1) {
1071 // Mask out all but lowest bit for some call which produces an i1.
1072 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1073 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1074 ResultReg = AndResult;
1075 }
1076
Evan Chengf3d4efe2008-09-07 09:09:33 +00001077 UpdateValueMap(I, ResultReg);
1078 }
1079
1080 return true;
1081}
1082
1083
Dan Gohman99b21822008-08-28 23:21:34 +00001084bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001085X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001086 switch (I->getOpcode()) {
1087 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001088 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001089 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001090 case Instruction::Store:
1091 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001092 case Instruction::ICmp:
1093 case Instruction::FCmp:
1094 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001095 case Instruction::ZExt:
1096 return X86SelectZExt(I);
1097 case Instruction::Br:
1098 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001099 case Instruction::Call:
1100 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001101 case Instruction::LShr:
1102 case Instruction::AShr:
1103 case Instruction::Shl:
1104 return X86SelectShift(I);
1105 case Instruction::Select:
1106 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001107 case Instruction::Trunc:
1108 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001109 case Instruction::FPExt:
1110 return X86SelectFPExt(I);
1111 case Instruction::FPTrunc:
1112 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001113 }
1114
1115 return false;
1116}
1117
Dan Gohman0586d912008-09-10 20:11:02 +00001118unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001119 MVT VT;
1120 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001121 return false;
1122
1123 // Get opcode and regclass of the output for the given load instruction.
1124 unsigned Opc = 0;
1125 const TargetRegisterClass *RC = NULL;
1126 switch (VT.getSimpleVT()) {
1127 default: return false;
1128 case MVT::i8:
1129 Opc = X86::MOV8rm;
1130 RC = X86::GR8RegisterClass;
1131 break;
1132 case MVT::i16:
1133 Opc = X86::MOV16rm;
1134 RC = X86::GR16RegisterClass;
1135 break;
1136 case MVT::i32:
1137 Opc = X86::MOV32rm;
1138 RC = X86::GR32RegisterClass;
1139 break;
1140 case MVT::i64:
1141 // Must be in x86-64 mode.
1142 Opc = X86::MOV64rm;
1143 RC = X86::GR64RegisterClass;
1144 break;
1145 case MVT::f32:
1146 if (Subtarget->hasSSE1()) {
1147 Opc = X86::MOVSSrm;
1148 RC = X86::FR32RegisterClass;
1149 } else {
1150 Opc = X86::LD_Fp32m;
1151 RC = X86::RFP32RegisterClass;
1152 }
1153 break;
1154 case MVT::f64:
1155 if (Subtarget->hasSSE2()) {
1156 Opc = X86::MOVSDrm;
1157 RC = X86::FR64RegisterClass;
1158 } else {
1159 Opc = X86::LD_Fp64m;
1160 RC = X86::RFP64RegisterClass;
1161 }
1162 break;
1163 case MVT::f80:
1164 Opc = X86::LD_Fp80m;
1165 RC = X86::RFP80RegisterClass;
1166 break;
1167 }
1168
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001169 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001170 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001171 X86AddressMode AM;
1172 if (X86SelectAddress(C, AM, false)) {
1173 if (TLI.getPointerTy() == MVT::i32)
1174 Opc = X86::LEA32r;
1175 else
1176 Opc = X86::LEA64r;
1177 unsigned ResultReg = createResultReg(RC);
1178 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001179 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001180 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001181 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001182 }
1183
Owen Anderson3b217c62008-09-06 01:11:01 +00001184 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001185 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001186 if (Align == 0) {
1187 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001188 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001189 Align = Log2_64(Align);
1190 }
Owen Anderson95267a12008-09-05 00:06:23 +00001191
Dan Gohman0586d912008-09-10 20:11:02 +00001192 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001193 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001194 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001195 return ResultReg;
1196}
1197
Dan Gohman0586d912008-09-10 20:11:02 +00001198unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1199 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001200 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001201 return 0;
1202 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1203 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1204 unsigned ResultReg = createResultReg(RC);
1205 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1206 return ResultReg;
1207}
1208
Evan Chengc3f44b02008-09-03 00:03:49 +00001209namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001210 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001211 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001212 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001213 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1214 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001215 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001216 }
Dan Gohman99b21822008-08-28 23:21:34 +00001217}