blob: 9197bab552b69e90a21a6d01339dfe75e42dcf6f [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000025#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/Support/CallSite.h"
Dan Gohman35893082008-09-18 23:23:44 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000031
32using namespace llvm;
33
34class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000038
39 /// StackPtr - Register used as the stack pointer.
40 ///
41 unsigned StackPtr;
42
43 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
44 /// floating point ops.
45 /// When SSE is available, use it for f32 operations.
46 /// When SSE2 is available, use it for f64 operations.
47 bool X86ScalarSSEf64;
48 bool X86ScalarSSEf32;
49
Evan Cheng8b19e562008-09-03 06:44:39 +000050public:
Dan Gohman3df24e62008-09-03 23:12:08 +000051 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000052 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +000053 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000054 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +000055 DenseMap<const AllocaInst *, int> &am
56#ifndef NDEBUG
57 , SmallSet<Instruction*, 8> &cil
58#endif
59 )
60 : FastISel(mf, mmi, vm, bm, am
61#ifndef NDEBUG
62 , cil
63#endif
64 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000065 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000066 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
67 X86ScalarSSEf64 = Subtarget->hasSSE2();
68 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000069 }
Evan Chengc3f44b02008-09-03 00:03:49 +000070
Dan Gohman3df24e62008-09-03 23:12:08 +000071 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000072
Dan Gohman1adf1b02008-08-19 21:45:35 +000073#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000074
75private:
Chris Lattner9a08a612008-10-15 04:26:38 +000076 bool X86FastEmitCompare(Value *LHS, Value *RHS, MVT VT);
77
Dan Gohman0586d912008-09-10 20:11:02 +000078 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Evan Chengf3d4efe2008-09-07 09:09:33 +000080 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000081 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000082
83 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
84 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000085
Dan Gohman2ff7fd12008-09-19 22:16:54 +000086 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000087
Dan Gohman3df24e62008-09-03 23:12:08 +000088 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000089
90 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000091
92 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000093
94 bool X86SelectZExt(Instruction *I);
95
96 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000097
98 bool X86SelectShift(Instruction *I);
99
100 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000101
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000102 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000103
Dan Gohman78efce62008-09-10 21:02:08 +0000104 bool X86SelectFPExt(Instruction *I);
105 bool X86SelectFPTrunc(Instruction *I);
106
Evan Chengf3d4efe2008-09-07 09:09:33 +0000107 bool X86SelectCall(Instruction *I);
108
109 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
110
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000111 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000112 return getTargetMachine()->getInstrInfo();
113 }
114 const X86TargetMachine *getTargetMachine() const {
115 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000116 }
117
Dan Gohman0586d912008-09-10 20:11:02 +0000118 unsigned TargetMaterializeConstant(Constant *C);
119
120 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000121
122 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
123 /// computed in an SSE register, not on the X87 floating point stack.
124 bool isScalarFPTypeInSSEReg(MVT VT) const {
125 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
126 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
127 }
128
Dan Gohman9b66d732008-09-30 00:48:39 +0000129 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
130 bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000131};
Dan Gohman99b21822008-08-28 23:21:34 +0000132
Dan Gohman9b66d732008-09-30 00:48:39 +0000133bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
134 MVT &VT, bool AllowI1) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
136 if (VT == MVT::Other || !VT.isSimple())
137 // Unhandled type. Halt "fast" selection and bail.
138 return false;
139 if (VT == MVT::iPTR)
140 // Use pointer type.
141 VT = TLI.getPointerTy();
Dan Gohman9b66d732008-09-30 00:48:39 +0000142 // For now, require SSE/SSE2 for performing floating-point operations,
143 // since x87 requires additional work.
144 if (VT == MVT::f64 && !X86ScalarSSEf64)
145 return false;
146 if (VT == MVT::f32 && !X86ScalarSSEf32)
147 return false;
148 // Similarly, no f80 support yet.
149 if (VT == MVT::f80)
150 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151 // We only handle legal types. For example, on x86-32 the instruction
152 // selector contains all of the 64-bit instructions from x86-64,
153 // under the assumption that i64 won't be used if the target doesn't
154 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000155 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000156}
157
158#include "X86GenCallingConv.inc"
159
160/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
161/// convention.
162CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
163 if (Subtarget->is64Bit()) {
164 if (Subtarget->isTargetWin64())
165 return CC_X86_Win64_C;
166 else if (CC == CallingConv::Fast && isTaillCall)
167 return CC_X86_64_TailCall;
168 else
169 return CC_X86_64_C;
170 }
171
172 if (CC == CallingConv::X86_FastCall)
173 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174 else if (CC == CallingConv::Fast)
175 return CC_X86_32_FastCC;
176 else
177 return CC_X86_32_C;
178}
179
Evan Cheng0de588f2008-09-05 21:00:03 +0000180/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000181/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000182/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000183bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 unsigned &ResultReg) {
185 // Get opcode and regclass of the output for the given load instruction.
186 unsigned Opc = 0;
187 const TargetRegisterClass *RC = NULL;
188 switch (VT.getSimpleVT()) {
189 default: return false;
190 case MVT::i8:
191 Opc = X86::MOV8rm;
192 RC = X86::GR8RegisterClass;
193 break;
194 case MVT::i16:
195 Opc = X86::MOV16rm;
196 RC = X86::GR16RegisterClass;
197 break;
198 case MVT::i32:
199 Opc = X86::MOV32rm;
200 RC = X86::GR32RegisterClass;
201 break;
202 case MVT::i64:
203 // Must be in x86-64 mode.
204 Opc = X86::MOV64rm;
205 RC = X86::GR64RegisterClass;
206 break;
207 case MVT::f32:
208 if (Subtarget->hasSSE1()) {
209 Opc = X86::MOVSSrm;
210 RC = X86::FR32RegisterClass;
211 } else {
212 Opc = X86::LD_Fp32m;
213 RC = X86::RFP32RegisterClass;
214 }
215 break;
216 case MVT::f64:
217 if (Subtarget->hasSSE2()) {
218 Opc = X86::MOVSDrm;
219 RC = X86::FR64RegisterClass;
220 } else {
221 Opc = X86::LD_Fp64m;
222 RC = X86::RFP64RegisterClass;
223 }
224 break;
225 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000226 // No f80 support yet.
227 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 }
229
230 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
232 return true;
233}
234
Evan Chengf3d4efe2008-09-07 09:09:33 +0000235/// X86FastEmitStore - Emit a machine instruction to store a value Val of
236/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
237/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000238/// i.e. V. Return true if it is possible.
239bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000240X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000241 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000242 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000243 unsigned Opc = 0;
244 const TargetRegisterClass *RC = NULL;
245 switch (VT.getSimpleVT()) {
246 default: return false;
247 case MVT::i8:
248 Opc = X86::MOV8mr;
249 RC = X86::GR8RegisterClass;
250 break;
251 case MVT::i16:
252 Opc = X86::MOV16mr;
253 RC = X86::GR16RegisterClass;
254 break;
255 case MVT::i32:
256 Opc = X86::MOV32mr;
257 RC = X86::GR32RegisterClass;
258 break;
259 case MVT::i64:
260 // Must be in x86-64 mode.
261 Opc = X86::MOV64mr;
262 RC = X86::GR64RegisterClass;
263 break;
264 case MVT::f32:
265 if (Subtarget->hasSSE1()) {
266 Opc = X86::MOVSSmr;
267 RC = X86::FR32RegisterClass;
268 } else {
269 Opc = X86::ST_Fp32m;
270 RC = X86::RFP32RegisterClass;
271 }
272 break;
273 case MVT::f64:
274 if (Subtarget->hasSSE2()) {
275 Opc = X86::MOVSDmr;
276 RC = X86::FR64RegisterClass;
277 } else {
278 Opc = X86::ST_Fp64m;
279 RC = X86::RFP64RegisterClass;
280 }
281 break;
282 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000283 // No f80 support yet.
284 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000285 }
286
Evan Chengf3d4efe2008-09-07 09:09:33 +0000287 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000288 return true;
289}
290
Evan Cheng24e3a902008-09-08 06:35:17 +0000291/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
292/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
293/// ISD::SIGN_EXTEND).
294bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
295 unsigned Src, MVT SrcVT,
296 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000297 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
298
299 if (RR != 0) {
300 ResultReg = RR;
301 return true;
302 } else
303 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000304}
305
Dan Gohman0586d912008-09-10 20:11:02 +0000306/// X86SelectAddress - Attempt to fill in an address from the given value.
307///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000308bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000309 User *U;
310 unsigned Opcode = Instruction::UserOp1;
311 if (Instruction *I = dyn_cast<Instruction>(V)) {
312 Opcode = I->getOpcode();
313 U = I;
314 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
315 Opcode = C->getOpcode();
316 U = C;
317 }
Dan Gohman0586d912008-09-10 20:11:02 +0000318
Dan Gohman35893082008-09-18 23:23:44 +0000319 switch (Opcode) {
320 default: break;
321 case Instruction::BitCast:
322 // Look past bitcasts.
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::IntToPtr:
326 // Look past no-op inttoptrs.
327 if (TLI.getValueType(U->getOperand(0)->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::PtrToInt:
331 // Look past no-op ptrtoints.
332 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000333 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000334
335 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000336 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000337 // Do static allocas.
338 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000339 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000340 if (SI != StaticAllocaMap.end()) {
341 AM.BaseType = X86AddressMode::FrameIndexBase;
342 AM.Base.FrameIndex = SI->second;
343 return true;
344 }
345 break;
Dan Gohman35893082008-09-18 23:23:44 +0000346 }
347
348 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000349 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000350 // Adds of constants are common and easy enough.
351 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000352 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
353 // They have to fit in the 32-bit signed displacement field though.
354 if (isInt32(Disp)) {
355 AM.Disp = (uint32_t)Disp;
356 return X86SelectAddress(U->getOperand(0), AM, isCall);
357 }
Dan Gohman0586d912008-09-10 20:11:02 +0000358 }
Dan Gohman35893082008-09-18 23:23:44 +0000359 break;
360 }
361
362 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000363 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000364 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000365 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000366 unsigned IndexReg = AM.IndexReg;
367 unsigned Scale = AM.Scale;
368 gep_type_iterator GTI = gep_type_begin(U);
369 // Look at all but the last index. Constants can be folded,
370 // and one dynamic index can be handled, if the scale is supported.
371 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
372 i != e; ++i, ++GTI) {
373 Value *Op = *i;
374 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
375 const StructLayout *SL = TD.getStructLayout(STy);
376 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
377 Disp += SL->getElementOffset(Idx);
378 } else {
379 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
380 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
381 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000382 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000383 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000384 (!AM.GV ||
385 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000386 (S == 1 || S == 2 || S == 4 || S == 8)) {
387 // Scaled-index addressing.
388 Scale = S;
389 IndexReg = getRegForValue(Op);
390 if (IndexReg == 0)
391 return false;
392 } else
393 // Unsupported.
394 goto unsupported_gep;
395 }
396 }
Dan Gohman09aae462008-09-26 20:04:15 +0000397 // Check for displacement overflow.
398 if (!isInt32(Disp))
399 break;
Dan Gohman35893082008-09-18 23:23:44 +0000400 // Ok, the GEP indices were covered by constant-offset and scaled-index
401 // addressing. Update the address state and move on to examining the base.
402 AM.IndexReg = IndexReg;
403 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000404 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000405 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000406 unsupported_gep:
407 // Ok, the GEP indices weren't all covered.
408 break;
409 }
410 }
411
412 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000413 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000414 // Can't handle alternate code models yet.
415 if (TM.getCodeModel() != CodeModel::Default &&
416 TM.getCodeModel() != CodeModel::Small)
417 return false;
418
Dan Gohman97135e12008-09-26 19:15:30 +0000419 // RIP-relative addresses can't have additional register operands.
420 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
421 (AM.Base.Reg != 0 || AM.IndexReg != 0))
422 return false;
423
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000424 // Set up the basic address.
425 AM.GV = GV;
426 if (!isCall &&
427 TM.getRelocationModel() == Reloc::PIC_ &&
428 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000429 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000430
431 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000432 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
433 // Check to see if we've already materialized this
434 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000435 if (unsigned Reg = LocalValueMap[V]) {
436 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000437 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000438 return true;
439 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000440 // Issue load from stub if necessary.
441 unsigned Opc = 0;
442 const TargetRegisterClass *RC = NULL;
443 if (TLI.getPointerTy() == MVT::i32) {
444 Opc = X86::MOV32rm;
445 RC = X86::GR32RegisterClass;
446 } else {
447 Opc = X86::MOV64rm;
448 RC = X86::GR64RegisterClass;
449 }
Dan Gohman789ce772008-09-25 23:34:02 +0000450
451 X86AddressMode StubAM;
452 StubAM.Base.Reg = AM.Base.Reg;
453 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000454 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000455 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
456
457 // Now construct the final address. Note that the Disp, Scale,
458 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000459 AM.Base.Reg = ResultReg;
460 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000461
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000462 // Prevent loading GV stub multiple times in same MBB.
463 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000464 }
465 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000466 }
467
Dan Gohman97135e12008-09-26 19:15:30 +0000468 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000469 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000470 if (AM.Base.Reg == 0) {
471 AM.Base.Reg = getRegForValue(V);
472 return AM.Base.Reg != 0;
473 }
474 if (AM.IndexReg == 0) {
475 assert(AM.Scale == 1 && "Scale with no index!");
476 AM.IndexReg = getRegForValue(V);
477 return AM.IndexReg != 0;
478 }
479 }
480
481 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000482}
483
Owen Andersona3971df2008-09-04 07:08:58 +0000484/// X86SelectStore - Select and emit code to implement store instructions.
485bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000486 MVT VT;
487 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000488 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000489 unsigned Val = getRegForValue(I->getOperand(0));
490 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000491 // Unhandled operand. Halt "fast" selection and bail.
492 return false;
493
Dan Gohman0586d912008-09-10 20:11:02 +0000494 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000495 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000496 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000497
Dan Gohman0586d912008-09-10 20:11:02 +0000498 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000499}
500
Evan Cheng8b19e562008-09-03 06:44:39 +0000501/// X86SelectLoad - Select and emit code to implement load instructions.
502///
Dan Gohman3df24e62008-09-03 23:12:08 +0000503bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000504 MVT VT;
505 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000506 return false;
507
Dan Gohman0586d912008-09-10 20:11:02 +0000508 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000509 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000510 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000511
Evan Cheng0de588f2008-09-05 21:00:03 +0000512 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000513 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000514 UpdateValueMap(I, ResultReg);
515 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000516 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000517 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000518}
519
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000520static unsigned X86ChooseCmpOpcode(MVT VT) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000521 switch (VT.getSimpleVT()) {
522 case MVT::i8: return X86::CMP8rr;
523 case MVT::i16: return X86::CMP16rr;
524 case MVT::i32: return X86::CMP32rr;
525 case MVT::i64: return X86::CMP64rr;
526 case MVT::f32: return X86::UCOMISSrr;
527 case MVT::f64: return X86::UCOMISDrr;
528 default: break;
529 }
530 return 0;
531}
532
Chris Lattner0e13c782008-10-15 04:13:29 +0000533/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
534/// of the comparison, return an opcode that works for the compare (e.g.
535/// CMP32ri) otherwise return 0.
536static unsigned X86ChooseCmpImmediateOpcode(ConstantInt *RHSC) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000537 if (RHSC->getType() == Type::Int8Ty)
538 return X86::CMP8ri;
539 if (RHSC->getType() == Type::Int16Ty)
540 return X86::CMP16ri;
541 if (RHSC->getType() == Type::Int32Ty)
542 return X86::CMP32ri;
543
544 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
545 // field.
546 if (RHSC->getType() == Type::Int64Ty &&
547 (int)RHSC->getSExtValue() == RHSC->getSExtValue())
548 return X86::CMP64ri32;
549
550 // Otherwise, we can't fold the immediate into this comparison.
551 return 0;
552}
553
Chris Lattner9a08a612008-10-15 04:26:38 +0000554bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
555 unsigned Op0Reg = getRegForValue(Op0);
556 if (Op0Reg == 0) return false;
557
558 // We have two options: compare with register or immediate. If the RHS of
559 // the compare is an immediate that we can fold into this compare, use
560 // CMPri, otherwise use CMPrr.
561 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
562 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(Op1C)) {
563 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg)
564 .addImm(Op1C->getSExtValue());
565 return true;
566 }
567 }
568
569 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
570 if (CompareOpc == 0) return false;
571
572 unsigned Op1Reg = getRegForValue(Op1);
573 if (Op1Reg == 0) return false;
574 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
575
576 return true;
577}
578
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000579bool X86FastISel::X86SelectCmp(Instruction *I) {
580 CmpInst *CI = cast<CmpInst>(I);
581
Dan Gohman9b66d732008-09-30 00:48:39 +0000582 MVT VT;
583 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000584 return false;
585
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000586 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000587 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000588 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000589 switch (CI->getPredicate()) {
590 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000591 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
592 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000593
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000594 unsigned EReg = createResultReg(&X86::GR8RegClass);
595 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000596 BuildMI(MBB, TII.get(X86::SETEr), EReg);
597 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
598 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000599 UpdateValueMap(I, ResultReg);
600 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000601 }
602 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000603 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
604 return false;
605
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000606 unsigned NEReg = createResultReg(&X86::GR8RegClass);
607 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000608 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
609 BuildMI(MBB, TII.get(X86::SETPr), PReg);
610 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000611 UpdateValueMap(I, ResultReg);
612 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000613 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000614 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
615 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
616 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
617 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
618 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
619 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
620 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
621 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
622 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
623 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
624 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
625 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
626
627 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
628 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
629 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
630 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
631 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
632 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
633 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
634 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
635 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
636 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000637 default:
638 return false;
639 }
640
Chris Lattner9a08a612008-10-15 04:26:38 +0000641 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000642 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000643 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000644
Chris Lattner9a08a612008-10-15 04:26:38 +0000645 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000646 if (!X86FastEmitCompare(Op0, Op1, VT))
647 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000648
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000649 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000650 UpdateValueMap(I, ResultReg);
651 return true;
652}
Evan Cheng8b19e562008-09-03 06:44:39 +0000653
Dan Gohmand89ae992008-09-05 01:06:14 +0000654bool X86FastISel::X86SelectZExt(Instruction *I) {
655 // Special-case hack: The only i1 values we know how to produce currently
656 // set the upper bits of an i8 value to zero.
657 if (I->getType() == Type::Int8Ty &&
658 I->getOperand(0)->getType() == Type::Int1Ty) {
659 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000660 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000661 UpdateValueMap(I, ResultReg);
662 return true;
663 }
664
665 return false;
666}
667
Chris Lattner9a08a612008-10-15 04:26:38 +0000668
Dan Gohmand89ae992008-09-05 01:06:14 +0000669bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000670 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000671 // Handle a conditional branch.
672 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000673 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
674 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
675
Dan Gohmand98d6202008-10-02 22:15:21 +0000676 // Fold the common case of a conditional branch with a comparison.
677 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
678 if (CI->hasOneUse()) {
679 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000680
Dan Gohmand98d6202008-10-02 22:15:21 +0000681 // Try to take advantage of fallthrough opportunities.
682 CmpInst::Predicate Predicate = CI->getPredicate();
683 if (MBB->isLayoutSuccessor(TrueMBB)) {
684 std::swap(TrueMBB, FalseMBB);
685 Predicate = CmpInst::getInversePredicate(Predicate);
686 }
687
Chris Lattner871d2462008-10-15 03:58:05 +0000688 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
689 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
690
Dan Gohmand98d6202008-10-02 22:15:21 +0000691 switch (Predicate) {
Chris Lattner871d2462008-10-15 03:58:05 +0000692 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
693 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
694 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
695 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
696 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
697 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
698 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
699 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
700 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
701 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
702 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
703 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000704
Chris Lattner871d2462008-10-15 03:58:05 +0000705 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
706 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
707 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
708 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
709 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
710 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
711 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
712 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
713 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
714 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000715 default:
716 return false;
717 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000718
Chris Lattner709d8292008-10-15 04:02:26 +0000719 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
720 if (SwapArgs)
721 std::swap(Op0, Op1);
722
Chris Lattner9a08a612008-10-15 04:26:38 +0000723 // Emit a compare of the LHS and RHS, setting the flags.
724 if (!X86FastEmitCompare(Op0, Op1, VT))
725 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000726
Chris Lattner54aebde2008-10-15 03:47:17 +0000727 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000728 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000729 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000730 return true;
731 }
732 }
733
734 // Otherwise do a clumsy setcc and re-test it.
735 unsigned OpReg = getRegForValue(BI->getCondition());
736 if (OpReg == 0) return false;
737
738 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Dan Gohmand98d6202008-10-02 22:15:21 +0000739 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000740 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000741 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000742 return true;
743}
744
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000745bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000746 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000747 const TargetRegisterClass *RC = NULL;
748 if (I->getType() == Type::Int8Ty) {
749 CReg = X86::CL;
750 RC = &X86::GR8RegClass;
751 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000752 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
753 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
754 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000755 default: return false;
756 }
757 } else if (I->getType() == Type::Int16Ty) {
758 CReg = X86::CX;
759 RC = &X86::GR16RegClass;
760 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000761 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
762 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
763 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000764 default: return false;
765 }
766 } else if (I->getType() == Type::Int32Ty) {
767 CReg = X86::ECX;
768 RC = &X86::GR32RegClass;
769 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000770 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
771 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
772 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000773 default: return false;
774 }
775 } else if (I->getType() == Type::Int64Ty) {
776 CReg = X86::RCX;
777 RC = &X86::GR64RegClass;
778 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000779 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
780 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
781 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000782 default: return false;
783 }
784 } else {
785 return false;
786 }
787
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000788 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000789 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000790 return false;
791
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000792 unsigned Op0Reg = getRegForValue(I->getOperand(0));
793 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000794
795 // Fold immediate in shl(x,3).
796 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
797 unsigned ResultReg = createResultReg(RC);
798 BuildMI(MBB, TII.get(OpImm),
799 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
800 UpdateValueMap(I, ResultReg);
801 return true;
802 }
803
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000804 unsigned Op1Reg = getRegForValue(I->getOperand(1));
805 if (Op1Reg == 0) return false;
806 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000807
808 // The shift instruction uses X86::CL. If we defined a super-register
809 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
810 // we're doing here.
811 if (CReg != X86::CL)
812 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
813 .addReg(CReg).addImm(X86::SUBREG_8BIT);
814
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000815 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000816 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000817 UpdateValueMap(I, ResultReg);
818 return true;
819}
820
821bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000822 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000823 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000824 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000825
826 unsigned Opc = 0;
827 const TargetRegisterClass *RC = NULL;
828 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000829 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000830 RC = &X86::GR16RegClass;
831 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000832 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000833 RC = &X86::GR32RegClass;
834 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000835 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000836 RC = &X86::GR64RegClass;
837 } else {
838 return false;
839 }
840
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000841 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000842 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000843 return false;
844
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000845 unsigned Op0Reg = getRegForValue(I->getOperand(0));
846 if (Op0Reg == 0) return false;
847 unsigned Op1Reg = getRegForValue(I->getOperand(1));
848 if (Op1Reg == 0) return false;
849 unsigned Op2Reg = getRegForValue(I->getOperand(2));
850 if (Op2Reg == 0) return false;
851
852 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
853 unsigned ResultReg = createResultReg(RC);
854 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
855 UpdateValueMap(I, ResultReg);
856 return true;
857}
858
Dan Gohman78efce62008-09-10 21:02:08 +0000859bool X86FastISel::X86SelectFPExt(Instruction *I) {
860 if (Subtarget->hasSSE2()) {
861 if (I->getType() == Type::DoubleTy) {
862 Value *V = I->getOperand(0);
863 if (V->getType() == Type::FloatTy) {
864 unsigned OpReg = getRegForValue(V);
865 if (OpReg == 0) return false;
866 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
867 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
868 UpdateValueMap(I, ResultReg);
869 return true;
870 }
871 }
872 }
873
874 return false;
875}
876
877bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
878 if (Subtarget->hasSSE2()) {
879 if (I->getType() == Type::FloatTy) {
880 Value *V = I->getOperand(0);
881 if (V->getType() == Type::DoubleTy) {
882 unsigned OpReg = getRegForValue(V);
883 if (OpReg == 0) return false;
884 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
885 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
886 UpdateValueMap(I, ResultReg);
887 return true;
888 }
889 }
890 }
891
892 return false;
893}
894
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000895bool X86FastISel::X86SelectTrunc(Instruction *I) {
896 if (Subtarget->is64Bit())
897 // All other cases should be handled by the tblgen generated code.
898 return false;
899 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
900 MVT DstVT = TLI.getValueType(I->getType());
901 if (DstVT != MVT::i8)
902 // All other cases should be handled by the tblgen generated code.
903 return false;
904 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
905 // All other cases should be handled by the tblgen generated code.
906 return false;
907
908 unsigned InputReg = getRegForValue(I->getOperand(0));
909 if (!InputReg)
910 // Unhandled operand. Halt "fast" selection and bail.
911 return false;
912
913 // First issue a copy to GR16_ or GR32_.
914 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
915 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
916 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
917 unsigned CopyReg = createResultReg(CopyRC);
918 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
919
920 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000921 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000922 if (!ResultReg)
923 return false;
924
925 UpdateValueMap(I, ResultReg);
926 return true;
927}
928
Evan Chengf3d4efe2008-09-07 09:09:33 +0000929bool X86FastISel::X86SelectCall(Instruction *I) {
930 CallInst *CI = cast<CallInst>(I);
931 Value *Callee = I->getOperand(0);
932
933 // Can't handle inline asm yet.
934 if (isa<InlineAsm>(Callee))
935 return false;
936
937 // FIXME: Handle some intrinsics.
938 if (Function *F = CI->getCalledFunction()) {
939 if (F->isDeclaration() &&F->getIntrinsicID())
940 return false;
941 }
942
Evan Chengf3d4efe2008-09-07 09:09:33 +0000943 // Handle only C and fastcc calling conventions for now.
944 CallSite CS(CI);
945 unsigned CC = CS.getCallingConv();
946 if (CC != CallingConv::C &&
947 CC != CallingConv::Fast &&
948 CC != CallingConv::X86_FastCall)
949 return false;
950
951 // Let SDISel handle vararg functions.
952 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
953 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
954 if (FTy->isVarArg())
955 return false;
956
957 // Handle *simple* calls for now.
958 const Type *RetTy = CS.getType();
959 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000960 if (RetTy == Type::VoidTy)
961 RetVT = MVT::isVoid;
962 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000963 return false;
964
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000965 // Materialize callee address in a register. FIXME: GV address can be
966 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000967 X86AddressMode CalleeAM;
968 if (!X86SelectAddress(Callee, CalleeAM, true))
969 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000970 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000971 GlobalValue *GV = 0;
972 if (CalleeAM.Base.Reg != 0) {
973 assert(CalleeAM.GV == 0);
974 CalleeOp = CalleeAM.Base.Reg;
975 } else if (CalleeAM.GV != 0) {
976 assert(CalleeAM.GV != 0);
977 GV = CalleeAM.GV;
978 } else
979 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000980
Evan Chengdebdea02008-09-08 17:15:42 +0000981 // Allow calls which produce i1 results.
982 bool AndToI1 = false;
983 if (RetVT == MVT::i1) {
984 RetVT = MVT::i8;
985 AndToI1 = true;
986 }
987
Evan Chengf3d4efe2008-09-07 09:09:33 +0000988 // Deal with call operands first.
989 SmallVector<unsigned, 4> Args;
990 SmallVector<MVT, 4> ArgVTs;
991 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
992 Args.reserve(CS.arg_size());
993 ArgVTs.reserve(CS.arg_size());
994 ArgFlags.reserve(CS.arg_size());
995 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
996 i != e; ++i) {
997 unsigned Arg = getRegForValue(*i);
998 if (Arg == 0)
999 return false;
1000 ISD::ArgFlagsTy Flags;
1001 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001002 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001003 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001004 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001005 Flags.setZExt();
1006
1007 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001008 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1009 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1010 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1011 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001012 return false;
1013
1014 const Type *ArgTy = (*i)->getType();
1015 MVT ArgVT;
1016 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1017 return false;
1018 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1019 Flags.setOrigAlign(OriginalAlignment);
1020
1021 Args.push_back(Arg);
1022 ArgVTs.push_back(ArgVT);
1023 ArgFlags.push_back(Flags);
1024 }
1025
1026 // Analyze operands of the call, assigning locations to each operand.
1027 SmallVector<CCValAssign, 16> ArgLocs;
1028 CCState CCInfo(CC, false, TM, ArgLocs);
1029 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1030
1031 // Get a count of how many bytes are to be pushed on the stack.
1032 unsigned NumBytes = CCInfo.getNextStackOffset();
1033
1034 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001035 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1036 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001037
1038 // Process argumenet: walk the register/memloc assignments, inserting
1039 // copies / loads.
1040 SmallVector<unsigned, 4> RegArgs;
1041 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1042 CCValAssign &VA = ArgLocs[i];
1043 unsigned Arg = Args[VA.getValNo()];
1044 MVT ArgVT = ArgVTs[VA.getValNo()];
1045
1046 // Promote the value if needed.
1047 switch (VA.getLocInfo()) {
1048 default: assert(0 && "Unknown loc info!");
1049 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001050 case CCValAssign::SExt: {
1051 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1052 Arg, ArgVT, Arg);
1053 assert(Emitted && "Failed to emit a sext!");
1054 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001055 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001056 }
1057 case CCValAssign::ZExt: {
1058 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1059 Arg, ArgVT, Arg);
1060 assert(Emitted && "Failed to emit a zext!");
1061 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001062 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001063 }
1064 case CCValAssign::AExt: {
1065 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1066 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001067 if (!Emitted)
1068 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1069 Arg, ArgVT, Arg);
1070 if (!Emitted)
1071 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1072 Arg, ArgVT, Arg);
1073
Evan Cheng24e3a902008-09-08 06:35:17 +00001074 assert(Emitted && "Failed to emit a aext!");
1075 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001076 break;
1077 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001078 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001079
1080 if (VA.isRegLoc()) {
1081 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1082 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1083 Arg, RC, RC);
1084 assert(Emitted && "Failed to emit a copy instruction!");
1085 RegArgs.push_back(VA.getLocReg());
1086 } else {
1087 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001088 X86AddressMode AM;
1089 AM.Base.Reg = StackPtr;
1090 AM.Disp = LocMemOffset;
1091 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001092 }
1093 }
1094
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001095 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1096 // GOT pointer.
1097 if (!Subtarget->is64Bit() &&
1098 TM.getRelocationModel() == Reloc::PIC_ &&
1099 Subtarget->isPICStyleGOT()) {
1100 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001101 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001102 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1103 assert(Emitted && "Failed to emit a copy instruction!");
1104 }
1105
Evan Chengf3d4efe2008-09-07 09:09:33 +00001106 // Issue the call.
1107 unsigned CallOpc = CalleeOp
1108 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1109 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1110 MachineInstrBuilder MIB = CalleeOp
1111 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001112 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001113
1114 // Add an implicit use GOT pointer in EBX.
1115 if (!Subtarget->is64Bit() &&
1116 TM.getRelocationModel() == Reloc::PIC_ &&
1117 Subtarget->isPICStyleGOT())
1118 MIB.addReg(X86::EBX);
1119
Evan Chengf3d4efe2008-09-07 09:09:33 +00001120 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001121 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1122 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001123
1124 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001125 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1126 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001127
1128 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001129 if (RetVT.getSimpleVT() != MVT::isVoid) {
1130 SmallVector<CCValAssign, 16> RVLocs;
1131 CCState CCInfo(CC, false, TM, RVLocs);
1132 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1133
1134 // Copy all of the result registers out of their specified physreg.
1135 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1136 MVT CopyVT = RVLocs[0].getValVT();
1137 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1138 TargetRegisterClass *SrcRC = DstRC;
1139
1140 // If this is a call to a function that returns an fp value on the x87 fp
1141 // stack, but where we prefer to use the value in xmm registers, copy it
1142 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1143 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1144 RVLocs[0].getLocReg() == X86::ST1) &&
1145 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1146 CopyVT = MVT::f80;
1147 SrcRC = X86::RSTRegisterClass;
1148 DstRC = X86::RFP80RegisterClass;
1149 }
1150
1151 unsigned ResultReg = createResultReg(DstRC);
1152 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1153 RVLocs[0].getLocReg(), DstRC, SrcRC);
1154 assert(Emitted && "Failed to emit a copy instruction!");
1155 if (CopyVT != RVLocs[0].getValVT()) {
1156 // Round the F80 the right size, which also moves to the appropriate xmm
1157 // register. This is accomplished by storing the F80 value in memory and
1158 // then loading it back. Ewww...
1159 MVT ResVT = RVLocs[0].getValVT();
1160 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1161 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001162 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001163 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1164 DstRC = ResVT == MVT::f32
1165 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1166 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1167 ResultReg = createResultReg(DstRC);
1168 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1169 }
1170
Evan Chengdebdea02008-09-08 17:15:42 +00001171 if (AndToI1) {
1172 // Mask out all but lowest bit for some call which produces an i1.
1173 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1174 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1175 ResultReg = AndResult;
1176 }
1177
Evan Chengf3d4efe2008-09-07 09:09:33 +00001178 UpdateValueMap(I, ResultReg);
1179 }
1180
1181 return true;
1182}
1183
1184
Dan Gohman99b21822008-08-28 23:21:34 +00001185bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001186X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001187 switch (I->getOpcode()) {
1188 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001189 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001190 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001191 case Instruction::Store:
1192 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001193 case Instruction::ICmp:
1194 case Instruction::FCmp:
1195 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001196 case Instruction::ZExt:
1197 return X86SelectZExt(I);
1198 case Instruction::Br:
1199 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001200 case Instruction::Call:
1201 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001202 case Instruction::LShr:
1203 case Instruction::AShr:
1204 case Instruction::Shl:
1205 return X86SelectShift(I);
1206 case Instruction::Select:
1207 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001208 case Instruction::Trunc:
1209 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001210 case Instruction::FPExt:
1211 return X86SelectFPExt(I);
1212 case Instruction::FPTrunc:
1213 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001214 }
1215
1216 return false;
1217}
1218
Dan Gohman0586d912008-09-10 20:11:02 +00001219unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001220 MVT VT;
1221 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001222 return false;
1223
1224 // Get opcode and regclass of the output for the given load instruction.
1225 unsigned Opc = 0;
1226 const TargetRegisterClass *RC = NULL;
1227 switch (VT.getSimpleVT()) {
1228 default: return false;
1229 case MVT::i8:
1230 Opc = X86::MOV8rm;
1231 RC = X86::GR8RegisterClass;
1232 break;
1233 case MVT::i16:
1234 Opc = X86::MOV16rm;
1235 RC = X86::GR16RegisterClass;
1236 break;
1237 case MVT::i32:
1238 Opc = X86::MOV32rm;
1239 RC = X86::GR32RegisterClass;
1240 break;
1241 case MVT::i64:
1242 // Must be in x86-64 mode.
1243 Opc = X86::MOV64rm;
1244 RC = X86::GR64RegisterClass;
1245 break;
1246 case MVT::f32:
1247 if (Subtarget->hasSSE1()) {
1248 Opc = X86::MOVSSrm;
1249 RC = X86::FR32RegisterClass;
1250 } else {
1251 Opc = X86::LD_Fp32m;
1252 RC = X86::RFP32RegisterClass;
1253 }
1254 break;
1255 case MVT::f64:
1256 if (Subtarget->hasSSE2()) {
1257 Opc = X86::MOVSDrm;
1258 RC = X86::FR64RegisterClass;
1259 } else {
1260 Opc = X86::LD_Fp64m;
1261 RC = X86::RFP64RegisterClass;
1262 }
1263 break;
1264 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001265 // No f80 support yet.
1266 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001267 }
1268
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001269 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001270 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001271 X86AddressMode AM;
1272 if (X86SelectAddress(C, AM, false)) {
1273 if (TLI.getPointerTy() == MVT::i32)
1274 Opc = X86::LEA32r;
1275 else
1276 Opc = X86::LEA64r;
1277 unsigned ResultReg = createResultReg(RC);
1278 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001279 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001280 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001281 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001282 }
1283
Owen Anderson3b217c62008-09-06 01:11:01 +00001284 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001285 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001286 if (Align == 0) {
1287 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001288 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001289 Align = Log2_64(Align);
1290 }
Owen Anderson95267a12008-09-05 00:06:23 +00001291
Dan Gohman5396c992008-09-30 01:21:32 +00001292 // x86-32 PIC requires a PIC base register for constant pools.
1293 unsigned PICBase = 0;
1294 if (TM.getRelocationModel() == Reloc::PIC_ &&
1295 !Subtarget->is64Bit())
1296 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1297
1298 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001299 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001300 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001301 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1302 PICBase);
1303
Owen Anderson95267a12008-09-05 00:06:23 +00001304 return ResultReg;
1305}
1306
Dan Gohman0586d912008-09-10 20:11:02 +00001307unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001308 // Fail on dynamic allocas. At this point, getRegForValue has already
1309 // checked its CSE maps, so if we're here trying to handle a dynamic
1310 // alloca, we're not going to succeed. X86SelectAddress has a
1311 // check for dynamic allocas, because it's called directly from
1312 // various places, but TargetMaterializeAlloca also needs a check
1313 // in order to avoid recursion between getRegForValue,
1314 // X86SelectAddrss, and TargetMaterializeAlloca.
1315 if (!StaticAllocaMap.count(C))
1316 return 0;
1317
Dan Gohman0586d912008-09-10 20:11:02 +00001318 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001319 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001320 return 0;
1321 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1322 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1323 unsigned ResultReg = createResultReg(RC);
1324 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1325 return ResultReg;
1326}
1327
Evan Chengc3f44b02008-09-03 00:03:49 +00001328namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001329 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001330 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001331 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001332 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001333 DenseMap<const AllocaInst *, int> &am
1334#ifndef NDEBUG
1335 , SmallSet<Instruction*, 8> &cil
1336#endif
1337 ) {
1338 return new X86FastISel(mf, mmi, vm, bm, am
1339#ifndef NDEBUG
1340 , cil
1341#endif
1342 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001343 }
Dan Gohman99b21822008-08-28 23:21:34 +00001344}