blob: 0a3f5204868875b77c3416189e5f9db8fb5d3d4a [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:
Dan Gohman5af29c22008-09-26 01:39:32 +0000219 // No f80 support yet.
220 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000221 }
222
223 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000224 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
225 return true;
226}
227
Evan Chengf3d4efe2008-09-07 09:09:33 +0000228/// X86FastEmitStore - Emit a machine instruction to store a value Val of
229/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
230/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000231/// i.e. V. Return true if it is possible.
232bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000233X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000234 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000235 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000236 unsigned Opc = 0;
237 const TargetRegisterClass *RC = NULL;
238 switch (VT.getSimpleVT()) {
239 default: return false;
240 case MVT::i8:
241 Opc = X86::MOV8mr;
242 RC = X86::GR8RegisterClass;
243 break;
244 case MVT::i16:
245 Opc = X86::MOV16mr;
246 RC = X86::GR16RegisterClass;
247 break;
248 case MVT::i32:
249 Opc = X86::MOV32mr;
250 RC = X86::GR32RegisterClass;
251 break;
252 case MVT::i64:
253 // Must be in x86-64 mode.
254 Opc = X86::MOV64mr;
255 RC = X86::GR64RegisterClass;
256 break;
257 case MVT::f32:
258 if (Subtarget->hasSSE1()) {
259 Opc = X86::MOVSSmr;
260 RC = X86::FR32RegisterClass;
261 } else {
262 Opc = X86::ST_Fp32m;
263 RC = X86::RFP32RegisterClass;
264 }
265 break;
266 case MVT::f64:
267 if (Subtarget->hasSSE2()) {
268 Opc = X86::MOVSDmr;
269 RC = X86::FR64RegisterClass;
270 } else {
271 Opc = X86::ST_Fp64m;
272 RC = X86::RFP64RegisterClass;
273 }
274 break;
275 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000276 // No f80 support yet.
277 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 }
279
Evan Chengf3d4efe2008-09-07 09:09:33 +0000280 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000281 return true;
282}
283
Evan Cheng24e3a902008-09-08 06:35:17 +0000284/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
285/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
286/// ISD::SIGN_EXTEND).
287bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
288 unsigned Src, MVT SrcVT,
289 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000290 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
291
292 if (RR != 0) {
293 ResultReg = RR;
294 return true;
295 } else
296 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000297}
298
Dan Gohman0586d912008-09-10 20:11:02 +0000299/// X86SelectAddress - Attempt to fill in an address from the given value.
300///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000301bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000302 User *U;
303 unsigned Opcode = Instruction::UserOp1;
304 if (Instruction *I = dyn_cast<Instruction>(V)) {
305 Opcode = I->getOpcode();
306 U = I;
307 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
308 Opcode = C->getOpcode();
309 U = C;
310 }
Dan Gohman0586d912008-09-10 20:11:02 +0000311
Dan Gohman35893082008-09-18 23:23:44 +0000312 switch (Opcode) {
313 default: break;
314 case Instruction::BitCast:
315 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000316 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000317
318 case Instruction::IntToPtr:
319 // Look past no-op inttoptrs.
320 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000321 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000322
323 case Instruction::PtrToInt:
324 // Look past no-op ptrtoints.
325 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000326 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000327
328 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000329 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000330 // Do static allocas.
331 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000332 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
333 if (SI == StaticAllocaMap.end())
334 return false;
335 AM.BaseType = X86AddressMode::FrameIndexBase;
336 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000337 return true;
338 }
339
340 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000341 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000342 // Adds of constants are common and easy enough.
343 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
344 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000345 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000346 }
Dan Gohman35893082008-09-18 23:23:44 +0000347 break;
348 }
349
350 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000351 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000352 // Pattern-match simple GEPs.
353 uint64_t Disp = AM.Disp;
354 unsigned IndexReg = AM.IndexReg;
355 unsigned Scale = AM.Scale;
356 gep_type_iterator GTI = gep_type_begin(U);
357 // Look at all but the last index. Constants can be folded,
358 // and one dynamic index can be handled, if the scale is supported.
359 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
360 i != e; ++i, ++GTI) {
361 Value *Op = *i;
362 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
363 const StructLayout *SL = TD.getStructLayout(STy);
364 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
365 Disp += SL->getElementOffset(Idx);
366 } else {
367 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
368 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
369 // Constant-offset addressing.
370 Disp += CI->getZExtValue() * S;
371 } else if (IndexReg == 0 &&
372 (S == 1 || S == 2 || S == 4 || S == 8)) {
373 // Scaled-index addressing.
374 Scale = S;
375 IndexReg = getRegForValue(Op);
376 if (IndexReg == 0)
377 return false;
378 } else
379 // Unsupported.
380 goto unsupported_gep;
381 }
382 }
383 // Ok, the GEP indices were covered by constant-offset and scaled-index
384 // addressing. Update the address state and move on to examining the base.
385 AM.IndexReg = IndexReg;
386 AM.Scale = Scale;
387 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000388 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000389 unsupported_gep:
390 // Ok, the GEP indices weren't all covered.
391 break;
392 }
393 }
394
395 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000396 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000397 // Can't handle alternate code models yet.
398 if (TM.getCodeModel() != CodeModel::Default &&
399 TM.getCodeModel() != CodeModel::Small)
400 return false;
401
402 // Set up the basic address.
403 AM.GV = GV;
404 if (!isCall &&
405 TM.getRelocationModel() == Reloc::PIC_ &&
406 !Subtarget->is64Bit())
407 AM.Base.Reg = getGlobalBaseReg();
408
409 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000410 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
411 // Check to see if we've already materialized this
412 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000413 if (unsigned Reg = LocalValueMap[V]) {
414 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000415 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000416 return true;
417 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000418 // Issue load from stub if necessary.
419 unsigned Opc = 0;
420 const TargetRegisterClass *RC = NULL;
421 if (TLI.getPointerTy() == MVT::i32) {
422 Opc = X86::MOV32rm;
423 RC = X86::GR32RegisterClass;
424 } else {
425 Opc = X86::MOV64rm;
426 RC = X86::GR64RegisterClass;
427 }
Dan Gohman789ce772008-09-25 23:34:02 +0000428
429 X86AddressMode StubAM;
430 StubAM.Base.Reg = AM.Base.Reg;
431 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000432 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000433 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
434
435 // Now construct the final address. Note that the Disp, Scale,
436 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000437 AM.Base.Reg = ResultReg;
438 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000439
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000440 // Prevent loading GV stub multiple times in same MBB.
441 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000442 }
443 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000444 }
445
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000446 // If all else fails, just materialize the value in a register.
447 AM.Base.Reg = getRegForValue(V);
448 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000449}
450
Owen Andersona3971df2008-09-04 07:08:58 +0000451/// X86SelectStore - Select and emit code to implement store instructions.
452bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000453 MVT VT;
454 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000455 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000456 unsigned Val = getRegForValue(I->getOperand(0));
457 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000458 // Unhandled operand. Halt "fast" selection and bail.
459 return false;
460
Dan Gohman0586d912008-09-10 20:11:02 +0000461 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000462 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000463 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000464
Dan Gohman0586d912008-09-10 20:11:02 +0000465 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000466}
467
Evan Cheng8b19e562008-09-03 06:44:39 +0000468/// X86SelectLoad - Select and emit code to implement load instructions.
469///
Dan Gohman3df24e62008-09-03 23:12:08 +0000470bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000471 MVT VT;
472 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000473 return false;
474
Dan Gohman0586d912008-09-10 20:11:02 +0000475 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000476 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000477 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000478
Evan Cheng0de588f2008-09-05 21:00:03 +0000479 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000480 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000481 UpdateValueMap(I, ResultReg);
482 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000483 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000484 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000485}
486
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000487bool X86FastISel::X86SelectCmp(Instruction *I) {
488 CmpInst *CI = cast<CmpInst>(I);
489
Dan Gohman4f22bb02008-09-05 01:33:56 +0000490 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
491 if (!TLI.isTypeLegal(VT))
492 return false;
493
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000494 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000495 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000496 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000497 if (Op1Reg == 0) return false;
498
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000499 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000500 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000501 case MVT::i8: Opc = X86::CMP8rr; break;
502 case MVT::i16: Opc = X86::CMP16rr; break;
503 case MVT::i32: Opc = X86::CMP32rr; break;
504 case MVT::i64: Opc = X86::CMP64rr; break;
505 case MVT::f32: Opc = X86::UCOMISSrr; break;
506 case MVT::f64: Opc = X86::UCOMISDrr; break;
507 default: return false;
508 }
509
510 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
511 switch (CI->getPredicate()) {
512 case CmpInst::FCMP_OEQ: {
513 unsigned EReg = createResultReg(&X86::GR8RegClass);
514 unsigned NPReg = createResultReg(&X86::GR8RegClass);
515 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516 BuildMI(MBB, TII.get(X86::SETEr), EReg);
517 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
518 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
519 break;
520 }
521 case CmpInst::FCMP_UNE: {
522 unsigned NEReg = createResultReg(&X86::GR8RegClass);
523 unsigned PReg = createResultReg(&X86::GR8RegClass);
524 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
525 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
526 BuildMI(MBB, TII.get(X86::SETPr), PReg);
527 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
528 break;
529 }
530 case CmpInst::FCMP_OGT:
531 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
532 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
533 break;
534 case CmpInst::FCMP_OGE:
535 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
536 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
537 break;
538 case CmpInst::FCMP_OLT:
539 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
540 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
541 break;
542 case CmpInst::FCMP_OLE:
543 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
544 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
545 break;
546 case CmpInst::FCMP_ONE:
547 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
548 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
549 break;
550 case CmpInst::FCMP_ORD:
551 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
552 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
553 break;
554 case CmpInst::FCMP_UNO:
555 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
557 break;
558 case CmpInst::FCMP_UEQ:
559 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
560 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
561 break;
562 case CmpInst::FCMP_UGT:
563 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
564 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
565 break;
566 case CmpInst::FCMP_UGE:
567 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
568 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
569 break;
570 case CmpInst::FCMP_ULT:
571 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
573 break;
574 case CmpInst::FCMP_ULE:
575 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
577 break;
578 case CmpInst::ICMP_EQ:
579 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
580 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
581 break;
582 case CmpInst::ICMP_NE:
583 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
584 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
585 break;
586 case CmpInst::ICMP_UGT:
587 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
588 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
589 break;
590 case CmpInst::ICMP_UGE:
591 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
592 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
593 break;
594 case CmpInst::ICMP_ULT:
595 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
596 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
597 break;
598 case CmpInst::ICMP_ULE:
599 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
600 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
601 break;
602 case CmpInst::ICMP_SGT:
603 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
604 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
605 break;
606 case CmpInst::ICMP_SGE:
607 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
608 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
609 break;
610 case CmpInst::ICMP_SLT:
611 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
612 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
613 break;
614 case CmpInst::ICMP_SLE:
615 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
616 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
617 break;
618 default:
619 return false;
620 }
621
622 UpdateValueMap(I, ResultReg);
623 return true;
624}
Evan Cheng8b19e562008-09-03 06:44:39 +0000625
Dan Gohmand89ae992008-09-05 01:06:14 +0000626bool X86FastISel::X86SelectZExt(Instruction *I) {
627 // Special-case hack: The only i1 values we know how to produce currently
628 // set the upper bits of an i8 value to zero.
629 if (I->getType() == Type::Int8Ty &&
630 I->getOperand(0)->getType() == Type::Int1Ty) {
631 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000632 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000633 UpdateValueMap(I, ResultReg);
634 return true;
635 }
636
637 return false;
638}
639
640bool X86FastISel::X86SelectBranch(Instruction *I) {
641 BranchInst *BI = cast<BranchInst>(I);
642 // Unconditional branches are selected by tablegen-generated code.
643 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000644 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000645 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
646 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
647
648 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
649 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
650 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
651
652 MBB->addSuccessor(TrueMBB);
653 MBB->addSuccessor(FalseMBB);
654
655 return true;
656}
657
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000658bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000659 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000660 const TargetRegisterClass *RC = NULL;
661 if (I->getType() == Type::Int8Ty) {
662 CReg = X86::CL;
663 RC = &X86::GR8RegClass;
664 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000665 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
666 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
667 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000668 default: return false;
669 }
670 } else if (I->getType() == Type::Int16Ty) {
671 CReg = X86::CX;
672 RC = &X86::GR16RegClass;
673 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000674 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
675 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
676 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000677 default: return false;
678 }
679 } else if (I->getType() == Type::Int32Ty) {
680 CReg = X86::ECX;
681 RC = &X86::GR32RegClass;
682 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000683 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
684 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
685 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000686 default: return false;
687 }
688 } else if (I->getType() == Type::Int64Ty) {
689 CReg = X86::RCX;
690 RC = &X86::GR64RegClass;
691 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000692 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
693 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
694 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000695 default: return false;
696 }
697 } else {
698 return false;
699 }
700
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000701 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
702 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
703 return false;
704
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000705 unsigned Op0Reg = getRegForValue(I->getOperand(0));
706 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000707
708 // Fold immediate in shl(x,3).
709 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
710 unsigned ResultReg = createResultReg(RC);
711 BuildMI(MBB, TII.get(OpImm),
712 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
713 UpdateValueMap(I, ResultReg);
714 return true;
715 }
716
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000717 unsigned Op1Reg = getRegForValue(I->getOperand(1));
718 if (Op1Reg == 0) return false;
719 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
720 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000721 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000722 UpdateValueMap(I, ResultReg);
723 return true;
724}
725
726bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000727 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000728 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000729 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000730
731 unsigned Opc = 0;
732 const TargetRegisterClass *RC = NULL;
733 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000734 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000735 RC = &X86::GR16RegClass;
736 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000737 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000738 RC = &X86::GR32RegClass;
739 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000740 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000741 RC = &X86::GR64RegClass;
742 } else {
743 return false;
744 }
745
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000746 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
747 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
748 return false;
749
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000750 unsigned Op0Reg = getRegForValue(I->getOperand(0));
751 if (Op0Reg == 0) return false;
752 unsigned Op1Reg = getRegForValue(I->getOperand(1));
753 if (Op1Reg == 0) return false;
754 unsigned Op2Reg = getRegForValue(I->getOperand(2));
755 if (Op2Reg == 0) return false;
756
757 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
758 unsigned ResultReg = createResultReg(RC);
759 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
760 UpdateValueMap(I, ResultReg);
761 return true;
762}
763
Dan Gohman78efce62008-09-10 21:02:08 +0000764bool X86FastISel::X86SelectFPExt(Instruction *I) {
765 if (Subtarget->hasSSE2()) {
766 if (I->getType() == Type::DoubleTy) {
767 Value *V = I->getOperand(0);
768 if (V->getType() == Type::FloatTy) {
769 unsigned OpReg = getRegForValue(V);
770 if (OpReg == 0) return false;
771 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
772 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
773 UpdateValueMap(I, ResultReg);
774 return true;
775 }
776 }
777 }
778
779 return false;
780}
781
782bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
783 if (Subtarget->hasSSE2()) {
784 if (I->getType() == Type::FloatTy) {
785 Value *V = I->getOperand(0);
786 if (V->getType() == Type::DoubleTy) {
787 unsigned OpReg = getRegForValue(V);
788 if (OpReg == 0) return false;
789 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
790 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
791 UpdateValueMap(I, ResultReg);
792 return true;
793 }
794 }
795 }
796
797 return false;
798}
799
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000800bool X86FastISel::X86SelectTrunc(Instruction *I) {
801 if (Subtarget->is64Bit())
802 // All other cases should be handled by the tblgen generated code.
803 return false;
804 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
805 MVT DstVT = TLI.getValueType(I->getType());
806 if (DstVT != MVT::i8)
807 // All other cases should be handled by the tblgen generated code.
808 return false;
809 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
810 // All other cases should be handled by the tblgen generated code.
811 return false;
812
813 unsigned InputReg = getRegForValue(I->getOperand(0));
814 if (!InputReg)
815 // Unhandled operand. Halt "fast" selection and bail.
816 return false;
817
818 // First issue a copy to GR16_ or GR32_.
819 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
820 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
821 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
822 unsigned CopyReg = createResultReg(CopyRC);
823 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
824
825 // Then issue an extract_subreg.
826 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
827 if (!ResultReg)
828 return false;
829
830 UpdateValueMap(I, ResultReg);
831 return true;
832}
833
Evan Chengf3d4efe2008-09-07 09:09:33 +0000834bool X86FastISel::X86SelectCall(Instruction *I) {
835 CallInst *CI = cast<CallInst>(I);
836 Value *Callee = I->getOperand(0);
837
838 // Can't handle inline asm yet.
839 if (isa<InlineAsm>(Callee))
840 return false;
841
842 // FIXME: Handle some intrinsics.
843 if (Function *F = CI->getCalledFunction()) {
844 if (F->isDeclaration() &&F->getIntrinsicID())
845 return false;
846 }
847
Evan Chengf3d4efe2008-09-07 09:09:33 +0000848 // Handle only C and fastcc calling conventions for now.
849 CallSite CS(CI);
850 unsigned CC = CS.getCallingConv();
851 if (CC != CallingConv::C &&
852 CC != CallingConv::Fast &&
853 CC != CallingConv::X86_FastCall)
854 return false;
855
856 // Let SDISel handle vararg functions.
857 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
858 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
859 if (FTy->isVarArg())
860 return false;
861
862 // Handle *simple* calls for now.
863 const Type *RetTy = CS.getType();
864 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000865 if (RetTy == Type::VoidTy)
866 RetVT = MVT::isVoid;
867 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000868 return false;
869
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000870 // Materialize callee address in a register. FIXME: GV address can be
871 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000872 X86AddressMode CalleeAM;
873 if (!X86SelectAddress(Callee, CalleeAM, true))
874 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000875 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000876 GlobalValue *GV = 0;
877 if (CalleeAM.Base.Reg != 0) {
878 assert(CalleeAM.GV == 0);
879 CalleeOp = CalleeAM.Base.Reg;
880 } else if (CalleeAM.GV != 0) {
881 assert(CalleeAM.GV != 0);
882 GV = CalleeAM.GV;
883 } else
884 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000885
Evan Chengdebdea02008-09-08 17:15:42 +0000886 // Allow calls which produce i1 results.
887 bool AndToI1 = false;
888 if (RetVT == MVT::i1) {
889 RetVT = MVT::i8;
890 AndToI1 = true;
891 }
892
Evan Chengf3d4efe2008-09-07 09:09:33 +0000893 // Deal with call operands first.
894 SmallVector<unsigned, 4> Args;
895 SmallVector<MVT, 4> ArgVTs;
896 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
897 Args.reserve(CS.arg_size());
898 ArgVTs.reserve(CS.arg_size());
899 ArgFlags.reserve(CS.arg_size());
900 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
901 i != e; ++i) {
902 unsigned Arg = getRegForValue(*i);
903 if (Arg == 0)
904 return false;
905 ISD::ArgFlagsTy Flags;
906 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +0000907 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000908 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +0000909 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000910 Flags.setZExt();
911
912 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +0000913 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
914 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
915 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
916 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000917 return false;
918
919 const Type *ArgTy = (*i)->getType();
920 MVT ArgVT;
921 if (!isTypeLegal(ArgTy, TLI, ArgVT))
922 return false;
923 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
924 Flags.setOrigAlign(OriginalAlignment);
925
926 Args.push_back(Arg);
927 ArgVTs.push_back(ArgVT);
928 ArgFlags.push_back(Flags);
929 }
930
931 // Analyze operands of the call, assigning locations to each operand.
932 SmallVector<CCValAssign, 16> ArgLocs;
933 CCState CCInfo(CC, false, TM, ArgLocs);
934 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
935
936 // Get a count of how many bytes are to be pushed on the stack.
937 unsigned NumBytes = CCInfo.getNextStackOffset();
938
939 // Issue CALLSEQ_START
940 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
941
942 // Process argumenet: walk the register/memloc assignments, inserting
943 // copies / loads.
944 SmallVector<unsigned, 4> RegArgs;
945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
946 CCValAssign &VA = ArgLocs[i];
947 unsigned Arg = Args[VA.getValNo()];
948 MVT ArgVT = ArgVTs[VA.getValNo()];
949
950 // Promote the value if needed.
951 switch (VA.getLocInfo()) {
952 default: assert(0 && "Unknown loc info!");
953 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000954 case CCValAssign::SExt: {
955 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
956 Arg, ArgVT, Arg);
957 assert(Emitted && "Failed to emit a sext!");
958 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000959 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000960 }
961 case CCValAssign::ZExt: {
962 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
963 Arg, ArgVT, Arg);
964 assert(Emitted && "Failed to emit a zext!");
965 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000966 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000967 }
968 case CCValAssign::AExt: {
969 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
970 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000971 if (!Emitted)
972 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
973 Arg, ArgVT, Arg);
974 if (!Emitted)
975 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
976 Arg, ArgVT, Arg);
977
Evan Cheng24e3a902008-09-08 06:35:17 +0000978 assert(Emitted && "Failed to emit a aext!");
979 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000980 break;
981 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000982 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000983
984 if (VA.isRegLoc()) {
985 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
986 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
987 Arg, RC, RC);
988 assert(Emitted && "Failed to emit a copy instruction!");
989 RegArgs.push_back(VA.getLocReg());
990 } else {
991 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000992 X86AddressMode AM;
993 AM.Base.Reg = StackPtr;
994 AM.Disp = LocMemOffset;
995 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000996 }
997 }
998
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000999 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1000 // GOT pointer.
1001 if (!Subtarget->is64Bit() &&
1002 TM.getRelocationModel() == Reloc::PIC_ &&
1003 Subtarget->isPICStyleGOT()) {
1004 TargetRegisterClass *RC = X86::GR32RegisterClass;
1005 unsigned Base = getGlobalBaseReg();
1006 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1007 assert(Emitted && "Failed to emit a copy instruction!");
1008 }
1009
Evan Chengf3d4efe2008-09-07 09:09:33 +00001010 // Issue the call.
1011 unsigned CallOpc = CalleeOp
1012 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1013 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1014 MachineInstrBuilder MIB = CalleeOp
1015 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001016 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001017
1018 // Add an implicit use GOT pointer in EBX.
1019 if (!Subtarget->is64Bit() &&
1020 TM.getRelocationModel() == Reloc::PIC_ &&
1021 Subtarget->isPICStyleGOT())
1022 MIB.addReg(X86::EBX);
1023
Evan Chengf3d4efe2008-09-07 09:09:33 +00001024 // Add implicit physical register uses to the call.
1025 while (!RegArgs.empty()) {
1026 MIB.addReg(RegArgs.back());
1027 RegArgs.pop_back();
1028 }
1029
1030 // Issue CALLSEQ_END
1031 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1032
1033 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001034 if (RetVT.getSimpleVT() != MVT::isVoid) {
1035 SmallVector<CCValAssign, 16> RVLocs;
1036 CCState CCInfo(CC, false, TM, RVLocs);
1037 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1038
1039 // Copy all of the result registers out of their specified physreg.
1040 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1041 MVT CopyVT = RVLocs[0].getValVT();
1042 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1043 TargetRegisterClass *SrcRC = DstRC;
1044
1045 // If this is a call to a function that returns an fp value on the x87 fp
1046 // stack, but where we prefer to use the value in xmm registers, copy it
1047 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1048 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1049 RVLocs[0].getLocReg() == X86::ST1) &&
1050 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1051 CopyVT = MVT::f80;
1052 SrcRC = X86::RSTRegisterClass;
1053 DstRC = X86::RFP80RegisterClass;
1054 }
1055
1056 unsigned ResultReg = createResultReg(DstRC);
1057 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1058 RVLocs[0].getLocReg(), DstRC, SrcRC);
1059 assert(Emitted && "Failed to emit a copy instruction!");
1060 if (CopyVT != RVLocs[0].getValVT()) {
1061 // Round the F80 the right size, which also moves to the appropriate xmm
1062 // register. This is accomplished by storing the F80 value in memory and
1063 // then loading it back. Ewww...
1064 MVT ResVT = RVLocs[0].getValVT();
1065 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1066 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001067 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001068 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1069 DstRC = ResVT == MVT::f32
1070 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1071 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1072 ResultReg = createResultReg(DstRC);
1073 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1074 }
1075
Evan Chengdebdea02008-09-08 17:15:42 +00001076 if (AndToI1) {
1077 // Mask out all but lowest bit for some call which produces an i1.
1078 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1079 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1080 ResultReg = AndResult;
1081 }
1082
Evan Chengf3d4efe2008-09-07 09:09:33 +00001083 UpdateValueMap(I, ResultReg);
1084 }
1085
1086 return true;
1087}
1088
1089
Dan Gohman99b21822008-08-28 23:21:34 +00001090bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001091X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001092 switch (I->getOpcode()) {
1093 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001094 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001095 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001096 case Instruction::Store:
1097 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001098 case Instruction::ICmp:
1099 case Instruction::FCmp:
1100 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001101 case Instruction::ZExt:
1102 return X86SelectZExt(I);
1103 case Instruction::Br:
1104 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001105 case Instruction::Call:
1106 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001107 case Instruction::LShr:
1108 case Instruction::AShr:
1109 case Instruction::Shl:
1110 return X86SelectShift(I);
1111 case Instruction::Select:
1112 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001113 case Instruction::Trunc:
1114 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001115 case Instruction::FPExt:
1116 return X86SelectFPExt(I);
1117 case Instruction::FPTrunc:
1118 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001119 }
1120
1121 return false;
1122}
1123
Dan Gohman0586d912008-09-10 20:11:02 +00001124unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001125 MVT VT;
1126 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001127 return false;
1128
1129 // Get opcode and regclass of the output for the given load instruction.
1130 unsigned Opc = 0;
1131 const TargetRegisterClass *RC = NULL;
1132 switch (VT.getSimpleVT()) {
1133 default: return false;
1134 case MVT::i8:
1135 Opc = X86::MOV8rm;
1136 RC = X86::GR8RegisterClass;
1137 break;
1138 case MVT::i16:
1139 Opc = X86::MOV16rm;
1140 RC = X86::GR16RegisterClass;
1141 break;
1142 case MVT::i32:
1143 Opc = X86::MOV32rm;
1144 RC = X86::GR32RegisterClass;
1145 break;
1146 case MVT::i64:
1147 // Must be in x86-64 mode.
1148 Opc = X86::MOV64rm;
1149 RC = X86::GR64RegisterClass;
1150 break;
1151 case MVT::f32:
1152 if (Subtarget->hasSSE1()) {
1153 Opc = X86::MOVSSrm;
1154 RC = X86::FR32RegisterClass;
1155 } else {
1156 Opc = X86::LD_Fp32m;
1157 RC = X86::RFP32RegisterClass;
1158 }
1159 break;
1160 case MVT::f64:
1161 if (Subtarget->hasSSE2()) {
1162 Opc = X86::MOVSDrm;
1163 RC = X86::FR64RegisterClass;
1164 } else {
1165 Opc = X86::LD_Fp64m;
1166 RC = X86::RFP64RegisterClass;
1167 }
1168 break;
1169 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001170 // No f80 support yet.
1171 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001172 }
1173
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001174 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001175 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001176 X86AddressMode AM;
1177 if (X86SelectAddress(C, AM, false)) {
1178 if (TLI.getPointerTy() == MVT::i32)
1179 Opc = X86::LEA32r;
1180 else
1181 Opc = X86::LEA64r;
1182 unsigned ResultReg = createResultReg(RC);
1183 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001184 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001185 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001186 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001187 }
1188
Owen Anderson3b217c62008-09-06 01:11:01 +00001189 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001190 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001191 if (Align == 0) {
1192 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001193 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001194 Align = Log2_64(Align);
1195 }
Owen Anderson95267a12008-09-05 00:06:23 +00001196
Dan Gohman0586d912008-09-10 20:11:02 +00001197 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001198 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001199 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001200 return ResultReg;
1201}
1202
Dan Gohman0586d912008-09-10 20:11:02 +00001203unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1204 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001205 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001206 return 0;
1207 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1208 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1209 unsigned ResultReg = createResultReg(RC);
1210 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1211 return ResultReg;
1212}
1213
Evan Chengc3f44b02008-09-03 00:03:49 +00001214namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001215 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001216 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001217 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001218 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1219 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001220 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001221 }
Dan Gohman99b21822008-08-28 23:21:34 +00001222}