blob: 954a3a0640e2fb78b344071df6dd09a497128aeb [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
552 // We have two options: compare with register or immediate. If the RHS of
553 // the compare is an immediate that we can fold into this compare, use
554 // CMPri, otherwise use CMPrr.
555 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000556 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000557 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg)
558 .addImm(Op1C->getSExtValue());
559 return true;
560 }
561 }
562
563 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
564 if (CompareOpc == 0) return false;
565
566 unsigned Op1Reg = getRegForValue(Op1);
567 if (Op1Reg == 0) return false;
568 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
569
570 return true;
571}
572
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000573bool X86FastISel::X86SelectCmp(Instruction *I) {
574 CmpInst *CI = cast<CmpInst>(I);
575
Dan Gohman9b66d732008-09-30 00:48:39 +0000576 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000577 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000578 return false;
579
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000580 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000581 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000582 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000583 switch (CI->getPredicate()) {
584 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000585 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
586 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000587
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000588 unsigned EReg = createResultReg(&X86::GR8RegClass);
589 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000590 BuildMI(MBB, TII.get(X86::SETEr), EReg);
591 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
592 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000593 UpdateValueMap(I, ResultReg);
594 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000595 }
596 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000597 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
598 return false;
599
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000600 unsigned NEReg = createResultReg(&X86::GR8RegClass);
601 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000602 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
603 BuildMI(MBB, TII.get(X86::SETPr), PReg);
604 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000605 UpdateValueMap(I, ResultReg);
606 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000607 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000608 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
609 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
610 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
611 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
612 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
613 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
614 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
615 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
616 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
617 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
618 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
619 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
620
621 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
622 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
623 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
624 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
625 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
626 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
627 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
628 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
629 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
630 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000631 default:
632 return false;
633 }
634
Chris Lattner9a08a612008-10-15 04:26:38 +0000635 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000636 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000637 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000638
Chris Lattner9a08a612008-10-15 04:26:38 +0000639 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000640 if (!X86FastEmitCompare(Op0, Op1, VT))
641 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000642
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000643 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000644 UpdateValueMap(I, ResultReg);
645 return true;
646}
Evan Cheng8b19e562008-09-03 06:44:39 +0000647
Dan Gohmand89ae992008-09-05 01:06:14 +0000648bool X86FastISel::X86SelectZExt(Instruction *I) {
649 // Special-case hack: The only i1 values we know how to produce currently
650 // set the upper bits of an i8 value to zero.
651 if (I->getType() == Type::Int8Ty &&
652 I->getOperand(0)->getType() == Type::Int1Ty) {
653 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000654 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000655 UpdateValueMap(I, ResultReg);
656 return true;
657 }
658
659 return false;
660}
661
Chris Lattner9a08a612008-10-15 04:26:38 +0000662
Dan Gohmand89ae992008-09-05 01:06:14 +0000663bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000664 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000665 // Handle a conditional branch.
666 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000667 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
668 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
669
Dan Gohmand98d6202008-10-02 22:15:21 +0000670 // Fold the common case of a conditional branch with a comparison.
671 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
672 if (CI->hasOneUse()) {
673 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000674
Dan Gohmand98d6202008-10-02 22:15:21 +0000675 // Try to take advantage of fallthrough opportunities.
676 CmpInst::Predicate Predicate = CI->getPredicate();
677 if (MBB->isLayoutSuccessor(TrueMBB)) {
678 std::swap(TrueMBB, FalseMBB);
679 Predicate = CmpInst::getInversePredicate(Predicate);
680 }
681
Chris Lattner871d2462008-10-15 03:58:05 +0000682 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
683 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
684
Dan Gohmand98d6202008-10-02 22:15:21 +0000685 switch (Predicate) {
Chris Lattner871d2462008-10-15 03:58:05 +0000686 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
687 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
688 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
689 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
690 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
691 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
692 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
693 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
694 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
695 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
696 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
697 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000698
Chris Lattner871d2462008-10-15 03:58:05 +0000699 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
700 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
701 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
702 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
703 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
704 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
705 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
706 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
707 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
708 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000709 default:
710 return false;
711 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000712
Chris Lattner709d8292008-10-15 04:02:26 +0000713 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
714 if (SwapArgs)
715 std::swap(Op0, Op1);
716
Chris Lattner9a08a612008-10-15 04:26:38 +0000717 // Emit a compare of the LHS and RHS, setting the flags.
718 if (!X86FastEmitCompare(Op0, Op1, VT))
719 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000720
Chris Lattner54aebde2008-10-15 03:47:17 +0000721 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000722 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000723 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000724 return true;
725 }
726 }
727
728 // Otherwise do a clumsy setcc and re-test it.
729 unsigned OpReg = getRegForValue(BI->getCondition());
730 if (OpReg == 0) return false;
731
732 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Dan Gohmand98d6202008-10-02 22:15:21 +0000733 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000734 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000735 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000736 return true;
737}
738
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000739bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000740 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000741 const TargetRegisterClass *RC = NULL;
742 if (I->getType() == Type::Int8Ty) {
743 CReg = X86::CL;
744 RC = &X86::GR8RegClass;
745 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000746 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
747 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
748 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000749 default: return false;
750 }
751 } else if (I->getType() == Type::Int16Ty) {
752 CReg = X86::CX;
753 RC = &X86::GR16RegClass;
754 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000755 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
756 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
757 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000758 default: return false;
759 }
760 } else if (I->getType() == Type::Int32Ty) {
761 CReg = X86::ECX;
762 RC = &X86::GR32RegClass;
763 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000764 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
765 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
766 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000767 default: return false;
768 }
769 } else if (I->getType() == Type::Int64Ty) {
770 CReg = X86::RCX;
771 RC = &X86::GR64RegClass;
772 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000773 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
774 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
775 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000776 default: return false;
777 }
778 } else {
779 return false;
780 }
781
Chris Lattner160f6cc2008-10-15 05:07:36 +0000782 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
783 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000784 return false;
785
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000786 unsigned Op0Reg = getRegForValue(I->getOperand(0));
787 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000788
789 // Fold immediate in shl(x,3).
790 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
791 unsigned ResultReg = createResultReg(RC);
792 BuildMI(MBB, TII.get(OpImm),
793 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
794 UpdateValueMap(I, ResultReg);
795 return true;
796 }
797
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000798 unsigned Op1Reg = getRegForValue(I->getOperand(1));
799 if (Op1Reg == 0) return false;
800 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000801
802 // The shift instruction uses X86::CL. If we defined a super-register
803 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
804 // we're doing here.
805 if (CReg != X86::CL)
806 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
807 .addReg(CReg).addImm(X86::SUBREG_8BIT);
808
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000809 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000810 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000811 UpdateValueMap(I, ResultReg);
812 return true;
813}
814
815bool X86FastISel::X86SelectSelect(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000816 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
817 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
818 return false;
819
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000820 unsigned Opc = 0;
821 const TargetRegisterClass *RC = NULL;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000822 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +0000823 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000824 RC = &X86::GR16RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000825 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +0000826 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000827 RC = &X86::GR32RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000828 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +0000829 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000830 RC = &X86::GR64RegClass;
831 } else {
832 return false;
833 }
834
835 unsigned Op0Reg = getRegForValue(I->getOperand(0));
836 if (Op0Reg == 0) return false;
837 unsigned Op1Reg = getRegForValue(I->getOperand(1));
838 if (Op1Reg == 0) return false;
839 unsigned Op2Reg = getRegForValue(I->getOperand(2));
840 if (Op2Reg == 0) return false;
841
842 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
843 unsigned ResultReg = createResultReg(RC);
844 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
845 UpdateValueMap(I, ResultReg);
846 return true;
847}
848
Dan Gohman78efce62008-09-10 21:02:08 +0000849bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000850 // fpext from float to double.
851 if (Subtarget->hasSSE2() && I->getType() == Type::DoubleTy) {
852 Value *V = I->getOperand(0);
853 if (V->getType() == Type::FloatTy) {
854 unsigned OpReg = getRegForValue(V);
855 if (OpReg == 0) return false;
856 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
857 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
858 UpdateValueMap(I, ResultReg);
859 return true;
Dan Gohman78efce62008-09-10 21:02:08 +0000860 }
861 }
862
863 return false;
864}
865
866bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
867 if (Subtarget->hasSSE2()) {
868 if (I->getType() == Type::FloatTy) {
869 Value *V = I->getOperand(0);
870 if (V->getType() == Type::DoubleTy) {
871 unsigned OpReg = getRegForValue(V);
872 if (OpReg == 0) return false;
873 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
874 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
875 UpdateValueMap(I, ResultReg);
876 return true;
877 }
878 }
879 }
880
881 return false;
882}
883
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000884bool X86FastISel::X86SelectTrunc(Instruction *I) {
885 if (Subtarget->is64Bit())
886 // All other cases should be handled by the tblgen generated code.
887 return false;
888 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
889 MVT DstVT = TLI.getValueType(I->getType());
890 if (DstVT != MVT::i8)
891 // All other cases should be handled by the tblgen generated code.
892 return false;
893 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
894 // All other cases should be handled by the tblgen generated code.
895 return false;
896
897 unsigned InputReg = getRegForValue(I->getOperand(0));
898 if (!InputReg)
899 // Unhandled operand. Halt "fast" selection and bail.
900 return false;
901
902 // First issue a copy to GR16_ or GR32_.
903 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
904 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
905 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
906 unsigned CopyReg = createResultReg(CopyRC);
907 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
908
909 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000910 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000911 if (!ResultReg)
912 return false;
913
914 UpdateValueMap(I, ResultReg);
915 return true;
916}
917
Evan Chengf3d4efe2008-09-07 09:09:33 +0000918bool X86FastISel::X86SelectCall(Instruction *I) {
919 CallInst *CI = cast<CallInst>(I);
920 Value *Callee = I->getOperand(0);
921
922 // Can't handle inline asm yet.
923 if (isa<InlineAsm>(Callee))
924 return false;
925
926 // FIXME: Handle some intrinsics.
927 if (Function *F = CI->getCalledFunction()) {
928 if (F->isDeclaration() &&F->getIntrinsicID())
929 return false;
930 }
931
Evan Chengf3d4efe2008-09-07 09:09:33 +0000932 // Handle only C and fastcc calling conventions for now.
933 CallSite CS(CI);
934 unsigned CC = CS.getCallingConv();
935 if (CC != CallingConv::C &&
936 CC != CallingConv::Fast &&
937 CC != CallingConv::X86_FastCall)
938 return false;
939
940 // Let SDISel handle vararg functions.
941 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
942 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
943 if (FTy->isVarArg())
944 return false;
945
946 // Handle *simple* calls for now.
947 const Type *RetTy = CS.getType();
948 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000949 if (RetTy == Type::VoidTy)
950 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000951 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000952 return false;
953
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000954 // Materialize callee address in a register. FIXME: GV address can be
955 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000956 X86AddressMode CalleeAM;
957 if (!X86SelectAddress(Callee, CalleeAM, true))
958 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000959 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000960 GlobalValue *GV = 0;
961 if (CalleeAM.Base.Reg != 0) {
962 assert(CalleeAM.GV == 0);
963 CalleeOp = CalleeAM.Base.Reg;
964 } else if (CalleeAM.GV != 0) {
965 assert(CalleeAM.GV != 0);
966 GV = CalleeAM.GV;
967 } else
968 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000969
Evan Chengdebdea02008-09-08 17:15:42 +0000970 // Allow calls which produce i1 results.
971 bool AndToI1 = false;
972 if (RetVT == MVT::i1) {
973 RetVT = MVT::i8;
974 AndToI1 = true;
975 }
976
Evan Chengf3d4efe2008-09-07 09:09:33 +0000977 // Deal with call operands first.
978 SmallVector<unsigned, 4> Args;
979 SmallVector<MVT, 4> ArgVTs;
980 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
981 Args.reserve(CS.arg_size());
982 ArgVTs.reserve(CS.arg_size());
983 ArgFlags.reserve(CS.arg_size());
984 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
985 i != e; ++i) {
986 unsigned Arg = getRegForValue(*i);
987 if (Arg == 0)
988 return false;
989 ISD::ArgFlagsTy Flags;
990 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +0000991 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000992 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +0000993 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000994 Flags.setZExt();
995
996 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +0000997 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
998 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
999 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1000 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001001 return false;
1002
1003 const Type *ArgTy = (*i)->getType();
1004 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001005 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001006 return false;
1007 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1008 Flags.setOrigAlign(OriginalAlignment);
1009
1010 Args.push_back(Arg);
1011 ArgVTs.push_back(ArgVT);
1012 ArgFlags.push_back(Flags);
1013 }
1014
1015 // Analyze operands of the call, assigning locations to each operand.
1016 SmallVector<CCValAssign, 16> ArgLocs;
1017 CCState CCInfo(CC, false, TM, ArgLocs);
1018 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1019
1020 // Get a count of how many bytes are to be pushed on the stack.
1021 unsigned NumBytes = CCInfo.getNextStackOffset();
1022
1023 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001024 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1025 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001026
1027 // Process argumenet: walk the register/memloc assignments, inserting
1028 // copies / loads.
1029 SmallVector<unsigned, 4> RegArgs;
1030 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1031 CCValAssign &VA = ArgLocs[i];
1032 unsigned Arg = Args[VA.getValNo()];
1033 MVT ArgVT = ArgVTs[VA.getValNo()];
1034
1035 // Promote the value if needed.
1036 switch (VA.getLocInfo()) {
1037 default: assert(0 && "Unknown loc info!");
1038 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001039 case CCValAssign::SExt: {
1040 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1041 Arg, ArgVT, Arg);
1042 assert(Emitted && "Failed to emit a sext!");
1043 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001044 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001045 }
1046 case CCValAssign::ZExt: {
1047 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1048 Arg, ArgVT, Arg);
1049 assert(Emitted && "Failed to emit a zext!");
1050 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001051 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001052 }
1053 case CCValAssign::AExt: {
1054 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1055 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001056 if (!Emitted)
1057 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001058 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001059 if (!Emitted)
1060 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1061 Arg, ArgVT, Arg);
1062
Evan Cheng24e3a902008-09-08 06:35:17 +00001063 assert(Emitted && "Failed to emit a aext!");
1064 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001065 break;
1066 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001067 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001068
1069 if (VA.isRegLoc()) {
1070 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1071 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1072 Arg, RC, RC);
1073 assert(Emitted && "Failed to emit a copy instruction!");
1074 RegArgs.push_back(VA.getLocReg());
1075 } else {
1076 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001077 X86AddressMode AM;
1078 AM.Base.Reg = StackPtr;
1079 AM.Disp = LocMemOffset;
1080 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001081 }
1082 }
1083
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001084 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1085 // GOT pointer.
1086 if (!Subtarget->is64Bit() &&
1087 TM.getRelocationModel() == Reloc::PIC_ &&
1088 Subtarget->isPICStyleGOT()) {
1089 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001090 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001091 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1092 assert(Emitted && "Failed to emit a copy instruction!");
1093 }
1094
Evan Chengf3d4efe2008-09-07 09:09:33 +00001095 // Issue the call.
1096 unsigned CallOpc = CalleeOp
1097 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1098 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1099 MachineInstrBuilder MIB = CalleeOp
1100 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001101 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001102
1103 // Add an implicit use GOT pointer in EBX.
1104 if (!Subtarget->is64Bit() &&
1105 TM.getRelocationModel() == Reloc::PIC_ &&
1106 Subtarget->isPICStyleGOT())
1107 MIB.addReg(X86::EBX);
1108
Evan Chengf3d4efe2008-09-07 09:09:33 +00001109 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001110 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1111 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001112
1113 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001114 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1115 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001116
1117 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001118 if (RetVT.getSimpleVT() != MVT::isVoid) {
1119 SmallVector<CCValAssign, 16> RVLocs;
1120 CCState CCInfo(CC, false, TM, RVLocs);
1121 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1122
1123 // Copy all of the result registers out of their specified physreg.
1124 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1125 MVT CopyVT = RVLocs[0].getValVT();
1126 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1127 TargetRegisterClass *SrcRC = DstRC;
1128
1129 // If this is a call to a function that returns an fp value on the x87 fp
1130 // stack, but where we prefer to use the value in xmm registers, copy it
1131 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1132 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1133 RVLocs[0].getLocReg() == X86::ST1) &&
1134 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1135 CopyVT = MVT::f80;
1136 SrcRC = X86::RSTRegisterClass;
1137 DstRC = X86::RFP80RegisterClass;
1138 }
1139
1140 unsigned ResultReg = createResultReg(DstRC);
1141 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1142 RVLocs[0].getLocReg(), DstRC, SrcRC);
1143 assert(Emitted && "Failed to emit a copy instruction!");
1144 if (CopyVT != RVLocs[0].getValVT()) {
1145 // Round the F80 the right size, which also moves to the appropriate xmm
1146 // register. This is accomplished by storing the F80 value in memory and
1147 // then loading it back. Ewww...
1148 MVT ResVT = RVLocs[0].getValVT();
1149 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1150 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001151 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001152 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1153 DstRC = ResVT == MVT::f32
1154 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1155 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1156 ResultReg = createResultReg(DstRC);
1157 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1158 }
1159
Evan Chengdebdea02008-09-08 17:15:42 +00001160 if (AndToI1) {
1161 // Mask out all but lowest bit for some call which produces an i1.
1162 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1163 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1164 ResultReg = AndResult;
1165 }
1166
Evan Chengf3d4efe2008-09-07 09:09:33 +00001167 UpdateValueMap(I, ResultReg);
1168 }
1169
1170 return true;
1171}
1172
1173
Dan Gohman99b21822008-08-28 23:21:34 +00001174bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001175X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001176 switch (I->getOpcode()) {
1177 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001178 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001179 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001180 case Instruction::Store:
1181 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001182 case Instruction::ICmp:
1183 case Instruction::FCmp:
1184 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001185 case Instruction::ZExt:
1186 return X86SelectZExt(I);
1187 case Instruction::Br:
1188 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001189 case Instruction::Call:
1190 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001191 case Instruction::LShr:
1192 case Instruction::AShr:
1193 case Instruction::Shl:
1194 return X86SelectShift(I);
1195 case Instruction::Select:
1196 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001197 case Instruction::Trunc:
1198 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001199 case Instruction::FPExt:
1200 return X86SelectFPExt(I);
1201 case Instruction::FPTrunc:
1202 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001203 }
1204
1205 return false;
1206}
1207
Dan Gohman0586d912008-09-10 20:11:02 +00001208unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001209 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001210 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001211 return false;
1212
1213 // Get opcode and regclass of the output for the given load instruction.
1214 unsigned Opc = 0;
1215 const TargetRegisterClass *RC = NULL;
1216 switch (VT.getSimpleVT()) {
1217 default: return false;
1218 case MVT::i8:
1219 Opc = X86::MOV8rm;
1220 RC = X86::GR8RegisterClass;
1221 break;
1222 case MVT::i16:
1223 Opc = X86::MOV16rm;
1224 RC = X86::GR16RegisterClass;
1225 break;
1226 case MVT::i32:
1227 Opc = X86::MOV32rm;
1228 RC = X86::GR32RegisterClass;
1229 break;
1230 case MVT::i64:
1231 // Must be in x86-64 mode.
1232 Opc = X86::MOV64rm;
1233 RC = X86::GR64RegisterClass;
1234 break;
1235 case MVT::f32:
1236 if (Subtarget->hasSSE1()) {
1237 Opc = X86::MOVSSrm;
1238 RC = X86::FR32RegisterClass;
1239 } else {
1240 Opc = X86::LD_Fp32m;
1241 RC = X86::RFP32RegisterClass;
1242 }
1243 break;
1244 case MVT::f64:
1245 if (Subtarget->hasSSE2()) {
1246 Opc = X86::MOVSDrm;
1247 RC = X86::FR64RegisterClass;
1248 } else {
1249 Opc = X86::LD_Fp64m;
1250 RC = X86::RFP64RegisterClass;
1251 }
1252 break;
1253 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001254 // No f80 support yet.
1255 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001256 }
1257
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001258 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001259 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001260 X86AddressMode AM;
1261 if (X86SelectAddress(C, AM, false)) {
1262 if (TLI.getPointerTy() == MVT::i32)
1263 Opc = X86::LEA32r;
1264 else
1265 Opc = X86::LEA64r;
1266 unsigned ResultReg = createResultReg(RC);
1267 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001268 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001269 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001270 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001271 }
1272
Owen Anderson3b217c62008-09-06 01:11:01 +00001273 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001274 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001275 if (Align == 0) {
1276 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001277 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001278 Align = Log2_64(Align);
1279 }
Owen Anderson95267a12008-09-05 00:06:23 +00001280
Dan Gohman5396c992008-09-30 01:21:32 +00001281 // x86-32 PIC requires a PIC base register for constant pools.
1282 unsigned PICBase = 0;
1283 if (TM.getRelocationModel() == Reloc::PIC_ &&
1284 !Subtarget->is64Bit())
1285 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1286
1287 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001288 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001289 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001290 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1291 PICBase);
1292
Owen Anderson95267a12008-09-05 00:06:23 +00001293 return ResultReg;
1294}
1295
Dan Gohman0586d912008-09-10 20:11:02 +00001296unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001297 // Fail on dynamic allocas. At this point, getRegForValue has already
1298 // checked its CSE maps, so if we're here trying to handle a dynamic
1299 // alloca, we're not going to succeed. X86SelectAddress has a
1300 // check for dynamic allocas, because it's called directly from
1301 // various places, but TargetMaterializeAlloca also needs a check
1302 // in order to avoid recursion between getRegForValue,
1303 // X86SelectAddrss, and TargetMaterializeAlloca.
1304 if (!StaticAllocaMap.count(C))
1305 return 0;
1306
Dan Gohman0586d912008-09-10 20:11:02 +00001307 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001308 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001309 return 0;
1310 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1311 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1312 unsigned ResultReg = createResultReg(RC);
1313 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1314 return ResultReg;
1315}
1316
Evan Chengc3f44b02008-09-03 00:03:49 +00001317namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001318 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001319 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001320 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001321 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001322 DenseMap<const AllocaInst *, int> &am
1323#ifndef NDEBUG
1324 , SmallSet<Instruction*, 8> &cil
1325#endif
1326 ) {
1327 return new X86FastISel(mf, mmi, vm, bm, am
1328#ifndef NDEBUG
1329 , cil
1330#endif
1331 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001332 }
Dan Gohman99b21822008-08-28 23:21:34 +00001333}