blob: e04e91a7acd4a52cdc450bf6fd185fca3ae6d609 [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
Chris Lattner160f6cc2008-10-15 05:07:36 +0000129 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000130};
Dan Gohman99b21822008-08-28 23:21:34 +0000131
Chris Lattner160f6cc2008-10-15 05:07:36 +0000132bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
133 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134 if (VT == MVT::Other || !VT.isSimple())
135 // Unhandled type. Halt "fast" selection and bail.
136 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000137
Dan Gohman9b66d732008-09-30 00:48:39 +0000138 // For now, require SSE/SSE2 for performing floating-point operations,
139 // since x87 requires additional work.
140 if (VT == MVT::f64 && !X86ScalarSSEf64)
141 return false;
142 if (VT == MVT::f32 && !X86ScalarSSEf32)
143 return false;
144 // Similarly, no f80 support yet.
145 if (VT == MVT::f80)
146 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // We only handle legal types. For example, on x86-32 the instruction
148 // selector contains all of the 64-bit instructions from x86-64,
149 // under the assumption that i64 won't be used if the target doesn't
150 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000151 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152}
153
154#include "X86GenCallingConv.inc"
155
156/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
157/// convention.
158CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
159 if (Subtarget->is64Bit()) {
160 if (Subtarget->isTargetWin64())
161 return CC_X86_Win64_C;
162 else if (CC == CallingConv::Fast && isTaillCall)
163 return CC_X86_64_TailCall;
164 else
165 return CC_X86_64_C;
166 }
167
168 if (CC == CallingConv::X86_FastCall)
169 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170 else if (CC == CallingConv::Fast)
171 return CC_X86_32_FastCC;
172 else
173 return CC_X86_32_C;
174}
175
Evan Cheng0de588f2008-09-05 21:00:03 +0000176/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000178/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000179bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000180 unsigned &ResultReg) {
181 // Get opcode and regclass of the output for the given load instruction.
182 unsigned Opc = 0;
183 const TargetRegisterClass *RC = NULL;
184 switch (VT.getSimpleVT()) {
185 default: return false;
186 case MVT::i8:
187 Opc = X86::MOV8rm;
188 RC = X86::GR8RegisterClass;
189 break;
190 case MVT::i16:
191 Opc = X86::MOV16rm;
192 RC = X86::GR16RegisterClass;
193 break;
194 case MVT::i32:
195 Opc = X86::MOV32rm;
196 RC = X86::GR32RegisterClass;
197 break;
198 case MVT::i64:
199 // Must be in x86-64 mode.
200 Opc = X86::MOV64rm;
201 RC = X86::GR64RegisterClass;
202 break;
203 case MVT::f32:
204 if (Subtarget->hasSSE1()) {
205 Opc = X86::MOVSSrm;
206 RC = X86::FR32RegisterClass;
207 } else {
208 Opc = X86::LD_Fp32m;
209 RC = X86::RFP32RegisterClass;
210 }
211 break;
212 case MVT::f64:
213 if (Subtarget->hasSSE2()) {
214 Opc = X86::MOVSDrm;
215 RC = X86::FR64RegisterClass;
216 } else {
217 Opc = X86::LD_Fp64m;
218 RC = X86::RFP64RegisterClass;
219 }
220 break;
221 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000222 // No f80 support yet.
223 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000224 }
225
226 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000227 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
228 return true;
229}
230
Evan Chengf3d4efe2008-09-07 09:09:33 +0000231/// X86FastEmitStore - Emit a machine instruction to store a value Val of
232/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
233/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000234/// i.e. V. Return true if it is possible.
235bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000236X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000237 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000238 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000239 unsigned Opc = 0;
240 const TargetRegisterClass *RC = NULL;
241 switch (VT.getSimpleVT()) {
242 default: return false;
243 case MVT::i8:
244 Opc = X86::MOV8mr;
245 RC = X86::GR8RegisterClass;
246 break;
247 case MVT::i16:
248 Opc = X86::MOV16mr;
249 RC = X86::GR16RegisterClass;
250 break;
251 case MVT::i32:
252 Opc = X86::MOV32mr;
253 RC = X86::GR32RegisterClass;
254 break;
255 case MVT::i64:
256 // Must be in x86-64 mode.
257 Opc = X86::MOV64mr;
258 RC = X86::GR64RegisterClass;
259 break;
260 case MVT::f32:
261 if (Subtarget->hasSSE1()) {
262 Opc = X86::MOVSSmr;
263 RC = X86::FR32RegisterClass;
264 } else {
265 Opc = X86::ST_Fp32m;
266 RC = X86::RFP32RegisterClass;
267 }
268 break;
269 case MVT::f64:
270 if (Subtarget->hasSSE2()) {
271 Opc = X86::MOVSDmr;
272 RC = X86::FR64RegisterClass;
273 } else {
274 Opc = X86::ST_Fp64m;
275 RC = X86::RFP64RegisterClass;
276 }
277 break;
278 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000279 // No f80 support yet.
280 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000281 }
282
Evan Chengf3d4efe2008-09-07 09:09:33 +0000283 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000284 return true;
285}
286
Evan Cheng24e3a902008-09-08 06:35:17 +0000287/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
288/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
289/// ISD::SIGN_EXTEND).
290bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
291 unsigned Src, MVT SrcVT,
292 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000293 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
294
295 if (RR != 0) {
296 ResultReg = RR;
297 return true;
298 } else
299 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000300}
301
Dan Gohman0586d912008-09-10 20:11:02 +0000302/// X86SelectAddress - Attempt to fill in an address from the given value.
303///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000304bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000305 User *U;
306 unsigned Opcode = Instruction::UserOp1;
307 if (Instruction *I = dyn_cast<Instruction>(V)) {
308 Opcode = I->getOpcode();
309 U = I;
310 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
311 Opcode = C->getOpcode();
312 U = C;
313 }
Dan Gohman0586d912008-09-10 20:11:02 +0000314
Dan Gohman35893082008-09-18 23:23:44 +0000315 switch (Opcode) {
316 default: break;
317 case Instruction::BitCast:
318 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000319 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000320
321 case Instruction::IntToPtr:
322 // Look past no-op inttoptrs.
323 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000324 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000325
326 case Instruction::PtrToInt:
327 // Look past no-op ptrtoints.
328 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000329 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000330
331 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000332 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000333 // Do static allocas.
334 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000335 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000336 if (SI != StaticAllocaMap.end()) {
337 AM.BaseType = X86AddressMode::FrameIndexBase;
338 AM.Base.FrameIndex = SI->second;
339 return true;
340 }
341 break;
Dan Gohman35893082008-09-18 23:23:44 +0000342 }
343
344 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000345 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000346 // Adds of constants are common and easy enough.
347 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000348 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
349 // They have to fit in the 32-bit signed displacement field though.
350 if (isInt32(Disp)) {
351 AM.Disp = (uint32_t)Disp;
352 return X86SelectAddress(U->getOperand(0), AM, isCall);
353 }
Dan Gohman0586d912008-09-10 20:11:02 +0000354 }
Dan Gohman35893082008-09-18 23:23:44 +0000355 break;
356 }
357
358 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000359 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000360 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000361 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000362 unsigned IndexReg = AM.IndexReg;
363 unsigned Scale = AM.Scale;
364 gep_type_iterator GTI = gep_type_begin(U);
365 // Look at all but the last index. Constants can be folded,
366 // and one dynamic index can be handled, if the scale is supported.
367 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
368 i != e; ++i, ++GTI) {
369 Value *Op = *i;
370 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
371 const StructLayout *SL = TD.getStructLayout(STy);
372 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
373 Disp += SL->getElementOffset(Idx);
374 } else {
375 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
376 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
377 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000378 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000379 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000380 (!AM.GV ||
381 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000382 (S == 1 || S == 2 || S == 4 || S == 8)) {
383 // Scaled-index addressing.
384 Scale = S;
385 IndexReg = getRegForValue(Op);
386 if (IndexReg == 0)
387 return false;
388 } else
389 // Unsupported.
390 goto unsupported_gep;
391 }
392 }
Dan Gohman09aae462008-09-26 20:04:15 +0000393 // Check for displacement overflow.
394 if (!isInt32(Disp))
395 break;
Dan Gohman35893082008-09-18 23:23:44 +0000396 // Ok, the GEP indices were covered by constant-offset and scaled-index
397 // addressing. Update the address state and move on to examining the base.
398 AM.IndexReg = IndexReg;
399 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000400 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000401 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000402 unsupported_gep:
403 // Ok, the GEP indices weren't all covered.
404 break;
405 }
406 }
407
408 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000409 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000410 // Can't handle alternate code models yet.
411 if (TM.getCodeModel() != CodeModel::Default &&
412 TM.getCodeModel() != CodeModel::Small)
413 return false;
414
Dan Gohman97135e12008-09-26 19:15:30 +0000415 // RIP-relative addresses can't have additional register operands.
416 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
417 (AM.Base.Reg != 0 || AM.IndexReg != 0))
418 return false;
419
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000420 // Set up the basic address.
421 AM.GV = GV;
422 if (!isCall &&
423 TM.getRelocationModel() == Reloc::PIC_ &&
424 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000425 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000426
427 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000428 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
429 // Check to see if we've already materialized this
430 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000431 if (unsigned Reg = LocalValueMap[V]) {
432 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000433 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000434 return true;
435 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000436 // Issue load from stub if necessary.
437 unsigned Opc = 0;
438 const TargetRegisterClass *RC = NULL;
439 if (TLI.getPointerTy() == MVT::i32) {
440 Opc = X86::MOV32rm;
441 RC = X86::GR32RegisterClass;
442 } else {
443 Opc = X86::MOV64rm;
444 RC = X86::GR64RegisterClass;
445 }
Dan Gohman789ce772008-09-25 23:34:02 +0000446
447 X86AddressMode StubAM;
448 StubAM.Base.Reg = AM.Base.Reg;
449 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000450 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000451 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
452
453 // Now construct the final address. Note that the Disp, Scale,
454 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000455 AM.Base.Reg = ResultReg;
456 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000457
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000458 // Prevent loading GV stub multiple times in same MBB.
459 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000460 }
461 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000462 }
463
Dan Gohman97135e12008-09-26 19:15:30 +0000464 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000465 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000466 if (AM.Base.Reg == 0) {
467 AM.Base.Reg = getRegForValue(V);
468 return AM.Base.Reg != 0;
469 }
470 if (AM.IndexReg == 0) {
471 assert(AM.Scale == 1 && "Scale with no index!");
472 AM.IndexReg = getRegForValue(V);
473 return AM.IndexReg != 0;
474 }
475 }
476
477 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000478}
479
Owen Andersona3971df2008-09-04 07:08:58 +0000480/// X86SelectStore - Select and emit code to implement store instructions.
481bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000482 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000483 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000484 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000485 unsigned Val = getRegForValue(I->getOperand(0));
486 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000487 // Unhandled operand. Halt "fast" selection and bail.
488 return false;
489
Dan Gohman0586d912008-09-10 20:11:02 +0000490 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000491 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000492 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000493
Dan Gohman0586d912008-09-10 20:11:02 +0000494 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000495}
496
Evan Cheng8b19e562008-09-03 06:44:39 +0000497/// X86SelectLoad - Select and emit code to implement load instructions.
498///
Dan Gohman3df24e62008-09-03 23:12:08 +0000499bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000500 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000501 if (!isTypeLegal(I->getType(), VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000502 return false;
503
Dan Gohman0586d912008-09-10 20:11:02 +0000504 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000505 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000506 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000507
Evan Cheng0de588f2008-09-05 21:00:03 +0000508 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000509 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000510 UpdateValueMap(I, ResultReg);
511 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000512 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000513 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000514}
515
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000516static unsigned X86ChooseCmpOpcode(MVT VT) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000517 switch (VT.getSimpleVT()) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000518 default: return 0;
519 case MVT::i8: return X86::CMP8rr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000520 case MVT::i16: return X86::CMP16rr;
521 case MVT::i32: return X86::CMP32rr;
522 case MVT::i64: return X86::CMP64rr;
523 case MVT::f32: return X86::UCOMISSrr;
524 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000525 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000526}
527
Chris Lattner0e13c782008-10-15 04:13:29 +0000528/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
529/// of the comparison, return an opcode that works for the compare (e.g.
530/// CMP32ri) otherwise return 0.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000531static unsigned X86ChooseCmpImmediateOpcode(MVT VT, ConstantInt *RHSC) {
532 switch (VT.getSimpleVT()) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000533 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000534 default: return 0;
535 case MVT::i8: return X86::CMP8ri;
536 case MVT::i16: return X86::CMP16ri;
537 case MVT::i32: return X86::CMP32ri;
538 case MVT::i64:
539 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
540 // field.
541 if (RHSC->getType() == Type::Int64Ty &&
542 (int)RHSC->getSExtValue() == RHSC->getSExtValue())
543 return X86::CMP64ri32;
544 return 0;
545 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000546}
547
Chris Lattner9a08a612008-10-15 04:26:38 +0000548bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
549 unsigned Op0Reg = getRegForValue(Op0);
550 if (Op0Reg == 0) return false;
551
Chris Lattnerd53886b2008-10-15 05:18:04 +0000552 // Handle 'null' like i32/i64 0.
553 if (isa<ConstantPointerNull>(Op1))
554 Op1 = Constant::getNullValue(TD.getIntPtrType());
555
Chris Lattner9a08a612008-10-15 04:26:38 +0000556 // We have two options: compare with register or immediate. If the RHS of
557 // the compare is an immediate that we can fold into this compare, use
558 // CMPri, otherwise use CMPrr.
559 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000560 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000561 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg)
562 .addImm(Op1C->getSExtValue());
563 return true;
564 }
565 }
566
567 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
568 if (CompareOpc == 0) return false;
569
570 unsigned Op1Reg = getRegForValue(Op1);
571 if (Op1Reg == 0) return false;
572 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
573
574 return true;
575}
576
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000577bool X86FastISel::X86SelectCmp(Instruction *I) {
578 CmpInst *CI = cast<CmpInst>(I);
579
Dan Gohman9b66d732008-09-30 00:48:39 +0000580 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000581 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000582 return false;
583
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000584 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000585 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000586 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000587 switch (CI->getPredicate()) {
588 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000589 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
590 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000591
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000592 unsigned EReg = createResultReg(&X86::GR8RegClass);
593 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000594 BuildMI(MBB, TII.get(X86::SETEr), EReg);
595 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
596 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000597 UpdateValueMap(I, ResultReg);
598 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000599 }
600 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000601 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
602 return false;
603
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000604 unsigned NEReg = createResultReg(&X86::GR8RegClass);
605 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000606 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
607 BuildMI(MBB, TII.get(X86::SETPr), PReg);
608 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000609 UpdateValueMap(I, ResultReg);
610 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000611 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000612 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
613 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
614 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
615 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
616 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
617 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
618 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
619 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
620 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
621 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
622 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
623 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
624
625 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
626 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
627 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
628 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
629 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
630 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
631 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
632 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
633 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
634 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000635 default:
636 return false;
637 }
638
Chris Lattner9a08a612008-10-15 04:26:38 +0000639 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000640 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000641 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000642
Chris Lattner9a08a612008-10-15 04:26:38 +0000643 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000644 if (!X86FastEmitCompare(Op0, Op1, VT))
645 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000646
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000647 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000648 UpdateValueMap(I, ResultReg);
649 return true;
650}
Evan Cheng8b19e562008-09-03 06:44:39 +0000651
Dan Gohmand89ae992008-09-05 01:06:14 +0000652bool X86FastISel::X86SelectZExt(Instruction *I) {
653 // Special-case hack: The only i1 values we know how to produce currently
654 // set the upper bits of an i8 value to zero.
655 if (I->getType() == Type::Int8Ty &&
656 I->getOperand(0)->getType() == Type::Int1Ty) {
657 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000658 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000659 UpdateValueMap(I, ResultReg);
660 return true;
661 }
662
663 return false;
664}
665
Chris Lattner9a08a612008-10-15 04:26:38 +0000666
Dan Gohmand89ae992008-09-05 01:06:14 +0000667bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000668 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000669 // Handle a conditional branch.
670 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000671 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
672 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
673
Dan Gohmand98d6202008-10-02 22:15:21 +0000674 // Fold the common case of a conditional branch with a comparison.
675 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
676 if (CI->hasOneUse()) {
677 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000678
Dan Gohmand98d6202008-10-02 22:15:21 +0000679 // Try to take advantage of fallthrough opportunities.
680 CmpInst::Predicate Predicate = CI->getPredicate();
681 if (MBB->isLayoutSuccessor(TrueMBB)) {
682 std::swap(TrueMBB, FalseMBB);
683 Predicate = CmpInst::getInversePredicate(Predicate);
684 }
685
Chris Lattner871d2462008-10-15 03:58:05 +0000686 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
687 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
688
Dan Gohmand98d6202008-10-02 22:15:21 +0000689 switch (Predicate) {
Chris Lattner871d2462008-10-15 03:58:05 +0000690 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
691 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
692 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
693 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
694 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
695 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
696 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
697 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
698 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
699 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
700 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
701 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000702
Chris Lattner871d2462008-10-15 03:58:05 +0000703 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
704 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
705 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
706 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
707 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
708 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
709 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
710 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
711 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
712 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000713 default:
714 return false;
715 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000716
Chris Lattner709d8292008-10-15 04:02:26 +0000717 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
718 if (SwapArgs)
719 std::swap(Op0, Op1);
720
Chris Lattner9a08a612008-10-15 04:26:38 +0000721 // Emit a compare of the LHS and RHS, setting the flags.
722 if (!X86FastEmitCompare(Op0, Op1, VT))
723 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000724
Chris Lattner54aebde2008-10-15 03:47:17 +0000725 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000726 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000727 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000728 return true;
729 }
730 }
731
732 // Otherwise do a clumsy setcc and re-test it.
733 unsigned OpReg = getRegForValue(BI->getCondition());
734 if (OpReg == 0) return false;
735
736 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Dan Gohmand98d6202008-10-02 22:15:21 +0000737 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000738 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000739 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000740 return true;
741}
742
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000743bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000744 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000745 const TargetRegisterClass *RC = NULL;
746 if (I->getType() == Type::Int8Ty) {
747 CReg = X86::CL;
748 RC = &X86::GR8RegClass;
749 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000750 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
751 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
752 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000753 default: return false;
754 }
755 } else if (I->getType() == Type::Int16Ty) {
756 CReg = X86::CX;
757 RC = &X86::GR16RegClass;
758 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000759 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
760 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
761 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000762 default: return false;
763 }
764 } else if (I->getType() == Type::Int32Ty) {
765 CReg = X86::ECX;
766 RC = &X86::GR32RegClass;
767 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000768 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
769 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
770 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000771 default: return false;
772 }
773 } else if (I->getType() == Type::Int64Ty) {
774 CReg = X86::RCX;
775 RC = &X86::GR64RegClass;
776 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000777 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
778 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
779 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000780 default: return false;
781 }
782 } else {
783 return false;
784 }
785
Chris Lattner160f6cc2008-10-15 05:07:36 +0000786 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
787 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000788 return false;
789
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000790 unsigned Op0Reg = getRegForValue(I->getOperand(0));
791 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000792
793 // Fold immediate in shl(x,3).
794 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
795 unsigned ResultReg = createResultReg(RC);
796 BuildMI(MBB, TII.get(OpImm),
797 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
798 UpdateValueMap(I, ResultReg);
799 return true;
800 }
801
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000802 unsigned Op1Reg = getRegForValue(I->getOperand(1));
803 if (Op1Reg == 0) return false;
804 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000805
806 // The shift instruction uses X86::CL. If we defined a super-register
807 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
808 // we're doing here.
809 if (CReg != X86::CL)
810 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
811 .addReg(CReg).addImm(X86::SUBREG_8BIT);
812
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000813 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000814 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000815 UpdateValueMap(I, ResultReg);
816 return true;
817}
818
819bool X86FastISel::X86SelectSelect(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000820 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
821 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
822 return false;
823
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000824 unsigned Opc = 0;
825 const TargetRegisterClass *RC = NULL;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000826 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +0000827 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000828 RC = &X86::GR16RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000829 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +0000830 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000831 RC = &X86::GR32RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000832 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +0000833 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000834 RC = &X86::GR64RegClass;
835 } else {
836 return false;
837 }
838
839 unsigned Op0Reg = getRegForValue(I->getOperand(0));
840 if (Op0Reg == 0) return false;
841 unsigned Op1Reg = getRegForValue(I->getOperand(1));
842 if (Op1Reg == 0) return false;
843 unsigned Op2Reg = getRegForValue(I->getOperand(2));
844 if (Op2Reg == 0) return false;
845
846 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
847 unsigned ResultReg = createResultReg(RC);
848 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
849 UpdateValueMap(I, ResultReg);
850 return true;
851}
852
Dan Gohman78efce62008-09-10 21:02:08 +0000853bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000854 // fpext from float to double.
855 if (Subtarget->hasSSE2() && I->getType() == Type::DoubleTy) {
856 Value *V = I->getOperand(0);
857 if (V->getType() == Type::FloatTy) {
858 unsigned OpReg = getRegForValue(V);
859 if (OpReg == 0) return false;
860 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
861 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
862 UpdateValueMap(I, ResultReg);
863 return true;
Dan Gohman78efce62008-09-10 21:02:08 +0000864 }
865 }
866
867 return false;
868}
869
870bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
871 if (Subtarget->hasSSE2()) {
872 if (I->getType() == Type::FloatTy) {
873 Value *V = I->getOperand(0);
874 if (V->getType() == Type::DoubleTy) {
875 unsigned OpReg = getRegForValue(V);
876 if (OpReg == 0) return false;
877 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
878 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
879 UpdateValueMap(I, ResultReg);
880 return true;
881 }
882 }
883 }
884
885 return false;
886}
887
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000888bool X86FastISel::X86SelectTrunc(Instruction *I) {
889 if (Subtarget->is64Bit())
890 // All other cases should be handled by the tblgen generated code.
891 return false;
892 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
893 MVT DstVT = TLI.getValueType(I->getType());
894 if (DstVT != MVT::i8)
895 // All other cases should be handled by the tblgen generated code.
896 return false;
897 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
898 // All other cases should be handled by the tblgen generated code.
899 return false;
900
901 unsigned InputReg = getRegForValue(I->getOperand(0));
902 if (!InputReg)
903 // Unhandled operand. Halt "fast" selection and bail.
904 return false;
905
906 // First issue a copy to GR16_ or GR32_.
907 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
908 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
909 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
910 unsigned CopyReg = createResultReg(CopyRC);
911 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
912
913 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000914 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000915 if (!ResultReg)
916 return false;
917
918 UpdateValueMap(I, ResultReg);
919 return true;
920}
921
Evan Chengf3d4efe2008-09-07 09:09:33 +0000922bool X86FastISel::X86SelectCall(Instruction *I) {
923 CallInst *CI = cast<CallInst>(I);
924 Value *Callee = I->getOperand(0);
925
926 // Can't handle inline asm yet.
927 if (isa<InlineAsm>(Callee))
928 return false;
929
930 // FIXME: Handle some intrinsics.
931 if (Function *F = CI->getCalledFunction()) {
932 if (F->isDeclaration() &&F->getIntrinsicID())
933 return false;
934 }
935
Evan Chengf3d4efe2008-09-07 09:09:33 +0000936 // Handle only C and fastcc calling conventions for now.
937 CallSite CS(CI);
938 unsigned CC = CS.getCallingConv();
939 if (CC != CallingConv::C &&
940 CC != CallingConv::Fast &&
941 CC != CallingConv::X86_FastCall)
942 return false;
943
944 // Let SDISel handle vararg functions.
945 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
946 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
947 if (FTy->isVarArg())
948 return false;
949
950 // Handle *simple* calls for now.
951 const Type *RetTy = CS.getType();
952 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000953 if (RetTy == Type::VoidTy)
954 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000955 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000956 return false;
957
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000958 // Materialize callee address in a register. FIXME: GV address can be
959 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000960 X86AddressMode CalleeAM;
961 if (!X86SelectAddress(Callee, CalleeAM, true))
962 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000963 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000964 GlobalValue *GV = 0;
965 if (CalleeAM.Base.Reg != 0) {
966 assert(CalleeAM.GV == 0);
967 CalleeOp = CalleeAM.Base.Reg;
968 } else if (CalleeAM.GV != 0) {
969 assert(CalleeAM.GV != 0);
970 GV = CalleeAM.GV;
971 } else
972 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000973
Evan Chengdebdea02008-09-08 17:15:42 +0000974 // Allow calls which produce i1 results.
975 bool AndToI1 = false;
976 if (RetVT == MVT::i1) {
977 RetVT = MVT::i8;
978 AndToI1 = true;
979 }
980
Evan Chengf3d4efe2008-09-07 09:09:33 +0000981 // Deal with call operands first.
982 SmallVector<unsigned, 4> Args;
983 SmallVector<MVT, 4> ArgVTs;
984 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
985 Args.reserve(CS.arg_size());
986 ArgVTs.reserve(CS.arg_size());
987 ArgFlags.reserve(CS.arg_size());
988 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
989 i != e; ++i) {
990 unsigned Arg = getRegForValue(*i);
991 if (Arg == 0)
992 return false;
993 ISD::ArgFlagsTy Flags;
994 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +0000995 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000996 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +0000997 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000998 Flags.setZExt();
999
1000 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001001 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1002 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1003 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1004 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001005 return false;
1006
1007 const Type *ArgTy = (*i)->getType();
1008 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001009 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001010 return false;
1011 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1012 Flags.setOrigAlign(OriginalAlignment);
1013
1014 Args.push_back(Arg);
1015 ArgVTs.push_back(ArgVT);
1016 ArgFlags.push_back(Flags);
1017 }
1018
1019 // Analyze operands of the call, assigning locations to each operand.
1020 SmallVector<CCValAssign, 16> ArgLocs;
1021 CCState CCInfo(CC, false, TM, ArgLocs);
1022 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1023
1024 // Get a count of how many bytes are to be pushed on the stack.
1025 unsigned NumBytes = CCInfo.getNextStackOffset();
1026
1027 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001028 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1029 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001030
1031 // Process argumenet: walk the register/memloc assignments, inserting
1032 // copies / loads.
1033 SmallVector<unsigned, 4> RegArgs;
1034 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1035 CCValAssign &VA = ArgLocs[i];
1036 unsigned Arg = Args[VA.getValNo()];
1037 MVT ArgVT = ArgVTs[VA.getValNo()];
1038
1039 // Promote the value if needed.
1040 switch (VA.getLocInfo()) {
1041 default: assert(0 && "Unknown loc info!");
1042 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001043 case CCValAssign::SExt: {
1044 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1045 Arg, ArgVT, Arg);
1046 assert(Emitted && "Failed to emit a sext!");
1047 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001048 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001049 }
1050 case CCValAssign::ZExt: {
1051 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1052 Arg, ArgVT, Arg);
1053 assert(Emitted && "Failed to emit a zext!");
1054 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001055 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001056 }
1057 case CCValAssign::AExt: {
1058 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1059 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001060 if (!Emitted)
1061 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001062 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001063 if (!Emitted)
1064 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1065 Arg, ArgVT, Arg);
1066
Evan Cheng24e3a902008-09-08 06:35:17 +00001067 assert(Emitted && "Failed to emit a aext!");
1068 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001069 break;
1070 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001071 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001072
1073 if (VA.isRegLoc()) {
1074 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1075 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1076 Arg, RC, RC);
1077 assert(Emitted && "Failed to emit a copy instruction!");
1078 RegArgs.push_back(VA.getLocReg());
1079 } else {
1080 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001081 X86AddressMode AM;
1082 AM.Base.Reg = StackPtr;
1083 AM.Disp = LocMemOffset;
1084 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001085 }
1086 }
1087
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001088 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1089 // GOT pointer.
1090 if (!Subtarget->is64Bit() &&
1091 TM.getRelocationModel() == Reloc::PIC_ &&
1092 Subtarget->isPICStyleGOT()) {
1093 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001094 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001095 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1096 assert(Emitted && "Failed to emit a copy instruction!");
1097 }
1098
Evan Chengf3d4efe2008-09-07 09:09:33 +00001099 // Issue the call.
1100 unsigned CallOpc = CalleeOp
1101 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1102 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1103 MachineInstrBuilder MIB = CalleeOp
1104 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001105 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001106
1107 // Add an implicit use GOT pointer in EBX.
1108 if (!Subtarget->is64Bit() &&
1109 TM.getRelocationModel() == Reloc::PIC_ &&
1110 Subtarget->isPICStyleGOT())
1111 MIB.addReg(X86::EBX);
1112
Evan Chengf3d4efe2008-09-07 09:09:33 +00001113 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001114 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1115 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001116
1117 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001118 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1119 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001120
1121 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001122 if (RetVT.getSimpleVT() != MVT::isVoid) {
1123 SmallVector<CCValAssign, 16> RVLocs;
1124 CCState CCInfo(CC, false, TM, RVLocs);
1125 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1126
1127 // Copy all of the result registers out of their specified physreg.
1128 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1129 MVT CopyVT = RVLocs[0].getValVT();
1130 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1131 TargetRegisterClass *SrcRC = DstRC;
1132
1133 // If this is a call to a function that returns an fp value on the x87 fp
1134 // stack, but where we prefer to use the value in xmm registers, copy it
1135 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1136 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1137 RVLocs[0].getLocReg() == X86::ST1) &&
1138 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1139 CopyVT = MVT::f80;
1140 SrcRC = X86::RSTRegisterClass;
1141 DstRC = X86::RFP80RegisterClass;
1142 }
1143
1144 unsigned ResultReg = createResultReg(DstRC);
1145 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1146 RVLocs[0].getLocReg(), DstRC, SrcRC);
1147 assert(Emitted && "Failed to emit a copy instruction!");
1148 if (CopyVT != RVLocs[0].getValVT()) {
1149 // Round the F80 the right size, which also moves to the appropriate xmm
1150 // register. This is accomplished by storing the F80 value in memory and
1151 // then loading it back. Ewww...
1152 MVT ResVT = RVLocs[0].getValVT();
1153 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1154 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001155 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001156 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1157 DstRC = ResVT == MVT::f32
1158 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1159 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1160 ResultReg = createResultReg(DstRC);
1161 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1162 }
1163
Evan Chengdebdea02008-09-08 17:15:42 +00001164 if (AndToI1) {
1165 // Mask out all but lowest bit for some call which produces an i1.
1166 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1167 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1168 ResultReg = AndResult;
1169 }
1170
Evan Chengf3d4efe2008-09-07 09:09:33 +00001171 UpdateValueMap(I, ResultReg);
1172 }
1173
1174 return true;
1175}
1176
1177
Dan Gohman99b21822008-08-28 23:21:34 +00001178bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001179X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001180 switch (I->getOpcode()) {
1181 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001182 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001183 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001184 case Instruction::Store:
1185 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001186 case Instruction::ICmp:
1187 case Instruction::FCmp:
1188 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001189 case Instruction::ZExt:
1190 return X86SelectZExt(I);
1191 case Instruction::Br:
1192 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001193 case Instruction::Call:
1194 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001195 case Instruction::LShr:
1196 case Instruction::AShr:
1197 case Instruction::Shl:
1198 return X86SelectShift(I);
1199 case Instruction::Select:
1200 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001201 case Instruction::Trunc:
1202 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001203 case Instruction::FPExt:
1204 return X86SelectFPExt(I);
1205 case Instruction::FPTrunc:
1206 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001207 }
1208
1209 return false;
1210}
1211
Dan Gohman0586d912008-09-10 20:11:02 +00001212unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001213 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001214 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001215 return false;
1216
1217 // Get opcode and regclass of the output for the given load instruction.
1218 unsigned Opc = 0;
1219 const TargetRegisterClass *RC = NULL;
1220 switch (VT.getSimpleVT()) {
1221 default: return false;
1222 case MVT::i8:
1223 Opc = X86::MOV8rm;
1224 RC = X86::GR8RegisterClass;
1225 break;
1226 case MVT::i16:
1227 Opc = X86::MOV16rm;
1228 RC = X86::GR16RegisterClass;
1229 break;
1230 case MVT::i32:
1231 Opc = X86::MOV32rm;
1232 RC = X86::GR32RegisterClass;
1233 break;
1234 case MVT::i64:
1235 // Must be in x86-64 mode.
1236 Opc = X86::MOV64rm;
1237 RC = X86::GR64RegisterClass;
1238 break;
1239 case MVT::f32:
1240 if (Subtarget->hasSSE1()) {
1241 Opc = X86::MOVSSrm;
1242 RC = X86::FR32RegisterClass;
1243 } else {
1244 Opc = X86::LD_Fp32m;
1245 RC = X86::RFP32RegisterClass;
1246 }
1247 break;
1248 case MVT::f64:
1249 if (Subtarget->hasSSE2()) {
1250 Opc = X86::MOVSDrm;
1251 RC = X86::FR64RegisterClass;
1252 } else {
1253 Opc = X86::LD_Fp64m;
1254 RC = X86::RFP64RegisterClass;
1255 }
1256 break;
1257 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001258 // No f80 support yet.
1259 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001260 }
1261
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001262 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001263 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001264 X86AddressMode AM;
1265 if (X86SelectAddress(C, AM, false)) {
1266 if (TLI.getPointerTy() == MVT::i32)
1267 Opc = X86::LEA32r;
1268 else
1269 Opc = X86::LEA64r;
1270 unsigned ResultReg = createResultReg(RC);
1271 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001272 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001273 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001274 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001275 }
1276
Owen Anderson3b217c62008-09-06 01:11:01 +00001277 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001278 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001279 if (Align == 0) {
1280 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001281 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001282 Align = Log2_64(Align);
1283 }
Owen Anderson95267a12008-09-05 00:06:23 +00001284
Dan Gohman5396c992008-09-30 01:21:32 +00001285 // x86-32 PIC requires a PIC base register for constant pools.
1286 unsigned PICBase = 0;
1287 if (TM.getRelocationModel() == Reloc::PIC_ &&
1288 !Subtarget->is64Bit())
1289 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1290
1291 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001292 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001293 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001294 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1295 PICBase);
1296
Owen Anderson95267a12008-09-05 00:06:23 +00001297 return ResultReg;
1298}
1299
Dan Gohman0586d912008-09-10 20:11:02 +00001300unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001301 // Fail on dynamic allocas. At this point, getRegForValue has already
1302 // checked its CSE maps, so if we're here trying to handle a dynamic
1303 // alloca, we're not going to succeed. X86SelectAddress has a
1304 // check for dynamic allocas, because it's called directly from
1305 // various places, but TargetMaterializeAlloca also needs a check
1306 // in order to avoid recursion between getRegForValue,
1307 // X86SelectAddrss, and TargetMaterializeAlloca.
1308 if (!StaticAllocaMap.count(C))
1309 return 0;
1310
Dan Gohman0586d912008-09-10 20:11:02 +00001311 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001312 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001313 return 0;
1314 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1315 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1316 unsigned ResultReg = createResultReg(RC);
1317 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1318 return ResultReg;
1319}
1320
Evan Chengc3f44b02008-09-03 00:03:49 +00001321namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001322 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001323 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001324 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001325 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001326 DenseMap<const AllocaInst *, int> &am
1327#ifndef NDEBUG
1328 , SmallSet<Instruction*, 8> &cil
1329#endif
1330 ) {
1331 return new X86FastISel(mf, mmi, vm, bm, am
1332#ifndef NDEBUG
1333 , cil
1334#endif
1335 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001336 }
Dan Gohman99b21822008-08-28 23:21:34 +00001337}