blob: 159d3197f34d53449ebf27b58b53f30b934c9f14 [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000025#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/Support/CallSite.h"
Dan Gohman35893082008-09-18 23:23:44 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000031
32using namespace llvm;
33
34class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000038
39 /// StackPtr - Register used as the stack pointer.
40 ///
41 unsigned StackPtr;
42
Dan Gohman2cc3aa42008-09-25 15:24:26 +000043 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
44 /// base register.
45 unsigned GlobalBaseReg;
46
Evan Chengf3d4efe2008-09-07 09:09:33 +000047 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
51 bool X86ScalarSSEf64;
52 bool X86ScalarSSEf32;
53
Evan Cheng8b19e562008-09-03 06:44:39 +000054public:
Dan Gohman3df24e62008-09-03 23:12:08 +000055 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000056 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +000057 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000058 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
59 DenseMap<const AllocaInst *, int> &am)
Dan Gohmand57dd5f2008-09-23 21:53:34 +000060 : FastISel(mf, mmi, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000062 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Dan Gohman2cc3aa42008-09-25 15:24:26 +000063 GlobalBaseReg = 0;
Evan Chengf3d4efe2008-09-07 09:09:33 +000064 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000066 }
Evan Chengc3f44b02008-09-03 00:03:49 +000067
Dan Gohman3df24e62008-09-03 23:12:08 +000068 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000069
Dan Gohman1adf1b02008-08-19 21:45:35 +000070#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000071
72private:
Dan Gohman0586d912008-09-10 20:11:02 +000073 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000074
Evan Chengf3d4efe2008-09-07 09:09:33 +000075 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000076 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000077
78 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
79 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman2ff7fd12008-09-19 22:16:54 +000081 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000082
Dan Gohman3df24e62008-09-03 23:12:08 +000083 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000084
85 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000086
87 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000088
89 bool X86SelectZExt(Instruction *I);
90
91 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000092
93 bool X86SelectShift(Instruction *I);
94
95 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000096
Evan Cheng10a8d9c2008-09-07 08:47:42 +000097 bool X86SelectTrunc(Instruction *I);
98
Dan Gohman78efce62008-09-10 21:02:08 +000099 bool X86SelectFPExt(Instruction *I);
100 bool X86SelectFPTrunc(Instruction *I);
101
Evan Chengf3d4efe2008-09-07 09:09:33 +0000102 bool X86SelectCall(Instruction *I);
103
104 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
105
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000106 unsigned getGlobalBaseReg();
107
108 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000109 return getTargetMachine()->getInstrInfo();
110 }
111 const X86TargetMachine *getTargetMachine() const {
112 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000113 }
114
Dan Gohman0586d912008-09-10 20:11:02 +0000115 unsigned TargetMaterializeConstant(Constant *C);
116
117 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118
119 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
120 /// computed in an SSE register, not on the X87 floating point stack.
121 bool isScalarFPTypeInSSEReg(MVT VT) const {
122 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
123 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
124 }
125
Evan Chengc3f44b02008-09-03 00:03:49 +0000126};
Dan Gohman99b21822008-08-28 23:21:34 +0000127
Evan Chengdebdea02008-09-08 17:15:42 +0000128static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
129 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
131 if (VT == MVT::Other || !VT.isSimple())
132 // Unhandled type. Halt "fast" selection and bail.
133 return false;
134 if (VT == MVT::iPTR)
135 // Use pointer type.
136 VT = TLI.getPointerTy();
137 // We only handle legal types. For example, on x86-32 the instruction
138 // selector contains all of the 64-bit instructions from x86-64,
139 // under the assumption that i64 won't be used if the target doesn't
140 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000141 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000142}
143
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000144/// getGlobalBaseReg - Return the the global base register. Output
145/// instructions required to initialize the global base register, if necessary.
146///
147unsigned X86FastISel::getGlobalBaseReg() {
148 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
149 if (!GlobalBaseReg)
150 GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
151 return GlobalBaseReg;
152}
153
Evan Chengf3d4efe2008-09-07 09:09:33 +0000154#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))) {
348 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000349 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000350 }
Dan Gohman35893082008-09-18 23:23:44 +0000351 break;
352 }
353
354 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000355 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000356 // Pattern-match simple GEPs.
357 uint64_t Disp = AM.Disp;
358 unsigned IndexReg = AM.IndexReg;
359 unsigned Scale = AM.Scale;
360 gep_type_iterator GTI = gep_type_begin(U);
361 // Look at all but the last index. Constants can be folded,
362 // and one dynamic index can be handled, if the scale is supported.
363 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
364 i != e; ++i, ++GTI) {
365 Value *Op = *i;
366 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
367 const StructLayout *SL = TD.getStructLayout(STy);
368 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
369 Disp += SL->getElementOffset(Idx);
370 } else {
371 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
372 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
373 // Constant-offset addressing.
374 Disp += CI->getZExtValue() * S;
375 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000376 (!AM.GV ||
377 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000378 (S == 1 || S == 2 || S == 4 || S == 8)) {
379 // Scaled-index addressing.
380 Scale = S;
381 IndexReg = getRegForValue(Op);
382 if (IndexReg == 0)
383 return false;
384 } else
385 // Unsupported.
386 goto unsupported_gep;
387 }
388 }
389 // Ok, the GEP indices were covered by constant-offset and scaled-index
390 // addressing. Update the address state and move on to examining the base.
391 AM.IndexReg = IndexReg;
392 AM.Scale = Scale;
393 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000394 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000395 unsupported_gep:
396 // Ok, the GEP indices weren't all covered.
397 break;
398 }
399 }
400
401 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000402 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000403 // Can't handle alternate code models yet.
404 if (TM.getCodeModel() != CodeModel::Default &&
405 TM.getCodeModel() != CodeModel::Small)
406 return false;
407
Dan Gohman97135e12008-09-26 19:15:30 +0000408 // RIP-relative addresses can't have additional register operands.
409 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
410 (AM.Base.Reg != 0 || AM.IndexReg != 0))
411 return false;
412
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000413 // Set up the basic address.
414 AM.GV = GV;
415 if (!isCall &&
416 TM.getRelocationModel() == Reloc::PIC_ &&
417 !Subtarget->is64Bit())
418 AM.Base.Reg = getGlobalBaseReg();
419
420 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000421 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
422 // Check to see if we've already materialized this
423 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000424 if (unsigned Reg = LocalValueMap[V]) {
425 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000426 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000427 return true;
428 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000429 // Issue load from stub if necessary.
430 unsigned Opc = 0;
431 const TargetRegisterClass *RC = NULL;
432 if (TLI.getPointerTy() == MVT::i32) {
433 Opc = X86::MOV32rm;
434 RC = X86::GR32RegisterClass;
435 } else {
436 Opc = X86::MOV64rm;
437 RC = X86::GR64RegisterClass;
438 }
Dan Gohman789ce772008-09-25 23:34:02 +0000439
440 X86AddressMode StubAM;
441 StubAM.Base.Reg = AM.Base.Reg;
442 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000443 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000444 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
445
446 // Now construct the final address. Note that the Disp, Scale,
447 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000448 AM.Base.Reg = ResultReg;
449 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000450
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000451 // Prevent loading GV stub multiple times in same MBB.
452 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000453 }
454 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000455 }
456
Dan Gohman97135e12008-09-26 19:15:30 +0000457 // If all else fails, try to materialize the value in a register.
458 if (!AM.GV && getTargetMachine()->symbolicAddressesAreRIPRel()) {
459 if (AM.Base.Reg == 0) {
460 AM.Base.Reg = getRegForValue(V);
461 return AM.Base.Reg != 0;
462 }
463 if (AM.IndexReg == 0) {
464 assert(AM.Scale == 1 && "Scale with no index!");
465 AM.IndexReg = getRegForValue(V);
466 return AM.IndexReg != 0;
467 }
468 }
469
470 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000471}
472
Owen Andersona3971df2008-09-04 07:08:58 +0000473/// X86SelectStore - Select and emit code to implement store instructions.
474bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000475 MVT VT;
476 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000477 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000478 unsigned Val = getRegForValue(I->getOperand(0));
479 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000480 // Unhandled operand. Halt "fast" selection and bail.
481 return false;
482
Dan Gohman0586d912008-09-10 20:11:02 +0000483 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000484 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000485 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000486
Dan Gohman0586d912008-09-10 20:11:02 +0000487 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000488}
489
Evan Cheng8b19e562008-09-03 06:44:39 +0000490/// X86SelectLoad - Select and emit code to implement load instructions.
491///
Dan Gohman3df24e62008-09-03 23:12:08 +0000492bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000493 MVT VT;
494 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000495 return false;
496
Dan Gohman0586d912008-09-10 20:11:02 +0000497 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000498 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000499 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000500
Evan Cheng0de588f2008-09-05 21:00:03 +0000501 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000502 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000503 UpdateValueMap(I, ResultReg);
504 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000505 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000506 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000507}
508
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000509bool X86FastISel::X86SelectCmp(Instruction *I) {
510 CmpInst *CI = cast<CmpInst>(I);
511
Dan Gohman4f22bb02008-09-05 01:33:56 +0000512 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
513 if (!TLI.isTypeLegal(VT))
514 return false;
515
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000516 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000517 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000518 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000519 if (Op1Reg == 0) return false;
520
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000521 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000522 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000523 case MVT::i8: Opc = X86::CMP8rr; break;
524 case MVT::i16: Opc = X86::CMP16rr; break;
525 case MVT::i32: Opc = X86::CMP32rr; break;
526 case MVT::i64: Opc = X86::CMP64rr; break;
527 case MVT::f32: Opc = X86::UCOMISSrr; break;
528 case MVT::f64: Opc = X86::UCOMISDrr; break;
529 default: return false;
530 }
531
532 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
533 switch (CI->getPredicate()) {
534 case CmpInst::FCMP_OEQ: {
535 unsigned EReg = createResultReg(&X86::GR8RegClass);
536 unsigned NPReg = createResultReg(&X86::GR8RegClass);
537 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
538 BuildMI(MBB, TII.get(X86::SETEr), EReg);
539 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
540 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
541 break;
542 }
543 case CmpInst::FCMP_UNE: {
544 unsigned NEReg = createResultReg(&X86::GR8RegClass);
545 unsigned PReg = createResultReg(&X86::GR8RegClass);
546 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
547 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
548 BuildMI(MBB, TII.get(X86::SETPr), PReg);
549 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
550 break;
551 }
552 case CmpInst::FCMP_OGT:
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
555 break;
556 case CmpInst::FCMP_OGE:
557 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
558 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
559 break;
560 case CmpInst::FCMP_OLT:
561 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
562 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
563 break;
564 case CmpInst::FCMP_OLE:
565 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
566 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
567 break;
568 case CmpInst::FCMP_ONE:
569 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
570 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
571 break;
572 case CmpInst::FCMP_ORD:
573 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
574 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
575 break;
576 case CmpInst::FCMP_UNO:
577 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
578 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
579 break;
580 case CmpInst::FCMP_UEQ:
581 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
582 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
583 break;
584 case CmpInst::FCMP_UGT:
585 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
586 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
587 break;
588 case CmpInst::FCMP_UGE:
589 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
590 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
591 break;
592 case CmpInst::FCMP_ULT:
593 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
594 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
595 break;
596 case CmpInst::FCMP_ULE:
597 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
598 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
599 break;
600 case CmpInst::ICMP_EQ:
601 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
602 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
603 break;
604 case CmpInst::ICMP_NE:
605 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
606 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
607 break;
608 case CmpInst::ICMP_UGT:
609 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
610 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
611 break;
612 case CmpInst::ICMP_UGE:
613 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
614 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
615 break;
616 case CmpInst::ICMP_ULT:
617 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
618 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
619 break;
620 case CmpInst::ICMP_ULE:
621 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
622 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
623 break;
624 case CmpInst::ICMP_SGT:
625 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
626 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
627 break;
628 case CmpInst::ICMP_SGE:
629 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
630 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
631 break;
632 case CmpInst::ICMP_SLT:
633 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
634 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
635 break;
636 case CmpInst::ICMP_SLE:
637 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
638 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
639 break;
640 default:
641 return false;
642 }
643
644 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
662bool X86FastISel::X86SelectBranch(Instruction *I) {
663 BranchInst *BI = cast<BranchInst>(I);
664 // Unconditional branches are selected by tablegen-generated code.
665 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000666 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000667 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
668 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
669
670 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
671 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
672 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
673
674 MBB->addSuccessor(TrueMBB);
675 MBB->addSuccessor(FalseMBB);
676
677 return true;
678}
679
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000680bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000681 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000682 const TargetRegisterClass *RC = NULL;
683 if (I->getType() == Type::Int8Ty) {
684 CReg = X86::CL;
685 RC = &X86::GR8RegClass;
686 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000687 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
688 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
689 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000690 default: return false;
691 }
692 } else if (I->getType() == Type::Int16Ty) {
693 CReg = X86::CX;
694 RC = &X86::GR16RegClass;
695 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000696 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
697 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
698 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000699 default: return false;
700 }
701 } else if (I->getType() == Type::Int32Ty) {
702 CReg = X86::ECX;
703 RC = &X86::GR32RegClass;
704 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000705 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
706 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
707 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000708 default: return false;
709 }
710 } else if (I->getType() == Type::Int64Ty) {
711 CReg = X86::RCX;
712 RC = &X86::GR64RegClass;
713 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000714 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
715 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
716 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000717 default: return false;
718 }
719 } else {
720 return false;
721 }
722
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000723 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
724 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
725 return false;
726
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000727 unsigned Op0Reg = getRegForValue(I->getOperand(0));
728 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000729
730 // Fold immediate in shl(x,3).
731 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
732 unsigned ResultReg = createResultReg(RC);
733 BuildMI(MBB, TII.get(OpImm),
734 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
735 UpdateValueMap(I, ResultReg);
736 return true;
737 }
738
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000739 unsigned Op1Reg = getRegForValue(I->getOperand(1));
740 if (Op1Reg == 0) return false;
741 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
742 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000743 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000744 UpdateValueMap(I, ResultReg);
745 return true;
746}
747
748bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000749 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000750 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000751 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000752
753 unsigned Opc = 0;
754 const TargetRegisterClass *RC = NULL;
755 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000756 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000757 RC = &X86::GR16RegClass;
758 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000759 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000760 RC = &X86::GR32RegClass;
761 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000762 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000763 RC = &X86::GR64RegClass;
764 } else {
765 return false;
766 }
767
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000768 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
769 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
770 return false;
771
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000772 unsigned Op0Reg = getRegForValue(I->getOperand(0));
773 if (Op0Reg == 0) return false;
774 unsigned Op1Reg = getRegForValue(I->getOperand(1));
775 if (Op1Reg == 0) return false;
776 unsigned Op2Reg = getRegForValue(I->getOperand(2));
777 if (Op2Reg == 0) return false;
778
779 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
780 unsigned ResultReg = createResultReg(RC);
781 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
782 UpdateValueMap(I, ResultReg);
783 return true;
784}
785
Dan Gohman78efce62008-09-10 21:02:08 +0000786bool X86FastISel::X86SelectFPExt(Instruction *I) {
787 if (Subtarget->hasSSE2()) {
788 if (I->getType() == Type::DoubleTy) {
789 Value *V = I->getOperand(0);
790 if (V->getType() == Type::FloatTy) {
791 unsigned OpReg = getRegForValue(V);
792 if (OpReg == 0) return false;
793 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
794 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
795 UpdateValueMap(I, ResultReg);
796 return true;
797 }
798 }
799 }
800
801 return false;
802}
803
804bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
805 if (Subtarget->hasSSE2()) {
806 if (I->getType() == Type::FloatTy) {
807 Value *V = I->getOperand(0);
808 if (V->getType() == Type::DoubleTy) {
809 unsigned OpReg = getRegForValue(V);
810 if (OpReg == 0) return false;
811 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
812 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
813 UpdateValueMap(I, ResultReg);
814 return true;
815 }
816 }
817 }
818
819 return false;
820}
821
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000822bool X86FastISel::X86SelectTrunc(Instruction *I) {
823 if (Subtarget->is64Bit())
824 // All other cases should be handled by the tblgen generated code.
825 return false;
826 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
827 MVT DstVT = TLI.getValueType(I->getType());
828 if (DstVT != MVT::i8)
829 // All other cases should be handled by the tblgen generated code.
830 return false;
831 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
832 // All other cases should be handled by the tblgen generated code.
833 return false;
834
835 unsigned InputReg = getRegForValue(I->getOperand(0));
836 if (!InputReg)
837 // Unhandled operand. Halt "fast" selection and bail.
838 return false;
839
840 // First issue a copy to GR16_ or GR32_.
841 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
842 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
843 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
844 unsigned CopyReg = createResultReg(CopyRC);
845 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
846
847 // Then issue an extract_subreg.
848 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
849 if (!ResultReg)
850 return false;
851
852 UpdateValueMap(I, ResultReg);
853 return true;
854}
855
Evan Chengf3d4efe2008-09-07 09:09:33 +0000856bool X86FastISel::X86SelectCall(Instruction *I) {
857 CallInst *CI = cast<CallInst>(I);
858 Value *Callee = I->getOperand(0);
859
860 // Can't handle inline asm yet.
861 if (isa<InlineAsm>(Callee))
862 return false;
863
864 // FIXME: Handle some intrinsics.
865 if (Function *F = CI->getCalledFunction()) {
866 if (F->isDeclaration() &&F->getIntrinsicID())
867 return false;
868 }
869
Evan Chengf3d4efe2008-09-07 09:09:33 +0000870 // Handle only C and fastcc calling conventions for now.
871 CallSite CS(CI);
872 unsigned CC = CS.getCallingConv();
873 if (CC != CallingConv::C &&
874 CC != CallingConv::Fast &&
875 CC != CallingConv::X86_FastCall)
876 return false;
877
878 // Let SDISel handle vararg functions.
879 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
880 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
881 if (FTy->isVarArg())
882 return false;
883
884 // Handle *simple* calls for now.
885 const Type *RetTy = CS.getType();
886 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000887 if (RetTy == Type::VoidTy)
888 RetVT = MVT::isVoid;
889 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000890 return false;
891
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000892 // Materialize callee address in a register. FIXME: GV address can be
893 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000894 X86AddressMode CalleeAM;
895 if (!X86SelectAddress(Callee, CalleeAM, true))
896 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000897 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000898 GlobalValue *GV = 0;
899 if (CalleeAM.Base.Reg != 0) {
900 assert(CalleeAM.GV == 0);
901 CalleeOp = CalleeAM.Base.Reg;
902 } else if (CalleeAM.GV != 0) {
903 assert(CalleeAM.GV != 0);
904 GV = CalleeAM.GV;
905 } else
906 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000907
Evan Chengdebdea02008-09-08 17:15:42 +0000908 // Allow calls which produce i1 results.
909 bool AndToI1 = false;
910 if (RetVT == MVT::i1) {
911 RetVT = MVT::i8;
912 AndToI1 = true;
913 }
914
Evan Chengf3d4efe2008-09-07 09:09:33 +0000915 // Deal with call operands first.
916 SmallVector<unsigned, 4> Args;
917 SmallVector<MVT, 4> ArgVTs;
918 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
919 Args.reserve(CS.arg_size());
920 ArgVTs.reserve(CS.arg_size());
921 ArgFlags.reserve(CS.arg_size());
922 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
923 i != e; ++i) {
924 unsigned Arg = getRegForValue(*i);
925 if (Arg == 0)
926 return false;
927 ISD::ArgFlagsTy Flags;
928 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +0000929 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000930 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +0000931 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000932 Flags.setZExt();
933
934 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +0000935 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
936 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
937 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
938 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000939 return false;
940
941 const Type *ArgTy = (*i)->getType();
942 MVT ArgVT;
943 if (!isTypeLegal(ArgTy, TLI, ArgVT))
944 return false;
945 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
946 Flags.setOrigAlign(OriginalAlignment);
947
948 Args.push_back(Arg);
949 ArgVTs.push_back(ArgVT);
950 ArgFlags.push_back(Flags);
951 }
952
953 // Analyze operands of the call, assigning locations to each operand.
954 SmallVector<CCValAssign, 16> ArgLocs;
955 CCState CCInfo(CC, false, TM, ArgLocs);
956 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
957
958 // Get a count of how many bytes are to be pushed on the stack.
959 unsigned NumBytes = CCInfo.getNextStackOffset();
960
961 // Issue CALLSEQ_START
962 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
963
964 // Process argumenet: walk the register/memloc assignments, inserting
965 // copies / loads.
966 SmallVector<unsigned, 4> RegArgs;
967 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
968 CCValAssign &VA = ArgLocs[i];
969 unsigned Arg = Args[VA.getValNo()];
970 MVT ArgVT = ArgVTs[VA.getValNo()];
971
972 // Promote the value if needed.
973 switch (VA.getLocInfo()) {
974 default: assert(0 && "Unknown loc info!");
975 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000976 case CCValAssign::SExt: {
977 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
978 Arg, ArgVT, Arg);
979 assert(Emitted && "Failed to emit a sext!");
980 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000981 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000982 }
983 case CCValAssign::ZExt: {
984 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
985 Arg, ArgVT, Arg);
986 assert(Emitted && "Failed to emit a zext!");
987 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000988 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000989 }
990 case CCValAssign::AExt: {
991 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
992 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000993 if (!Emitted)
994 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
995 Arg, ArgVT, Arg);
996 if (!Emitted)
997 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
998 Arg, ArgVT, Arg);
999
Evan Cheng24e3a902008-09-08 06:35:17 +00001000 assert(Emitted && "Failed to emit a aext!");
1001 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001002 break;
1003 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001004 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001005
1006 if (VA.isRegLoc()) {
1007 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1008 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1009 Arg, RC, RC);
1010 assert(Emitted && "Failed to emit a copy instruction!");
1011 RegArgs.push_back(VA.getLocReg());
1012 } else {
1013 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001014 X86AddressMode AM;
1015 AM.Base.Reg = StackPtr;
1016 AM.Disp = LocMemOffset;
1017 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001018 }
1019 }
1020
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001021 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1022 // GOT pointer.
1023 if (!Subtarget->is64Bit() &&
1024 TM.getRelocationModel() == Reloc::PIC_ &&
1025 Subtarget->isPICStyleGOT()) {
1026 TargetRegisterClass *RC = X86::GR32RegisterClass;
1027 unsigned Base = getGlobalBaseReg();
1028 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1029 assert(Emitted && "Failed to emit a copy instruction!");
1030 }
1031
Evan Chengf3d4efe2008-09-07 09:09:33 +00001032 // Issue the call.
1033 unsigned CallOpc = CalleeOp
1034 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1035 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1036 MachineInstrBuilder MIB = CalleeOp
1037 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001038 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001039
1040 // Add an implicit use GOT pointer in EBX.
1041 if (!Subtarget->is64Bit() &&
1042 TM.getRelocationModel() == Reloc::PIC_ &&
1043 Subtarget->isPICStyleGOT())
1044 MIB.addReg(X86::EBX);
1045
Evan Chengf3d4efe2008-09-07 09:09:33 +00001046 // Add implicit physical register uses to the call.
1047 while (!RegArgs.empty()) {
1048 MIB.addReg(RegArgs.back());
1049 RegArgs.pop_back();
1050 }
1051
1052 // Issue CALLSEQ_END
1053 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1054
1055 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001056 if (RetVT.getSimpleVT() != MVT::isVoid) {
1057 SmallVector<CCValAssign, 16> RVLocs;
1058 CCState CCInfo(CC, false, TM, RVLocs);
1059 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1060
1061 // Copy all of the result registers out of their specified physreg.
1062 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1063 MVT CopyVT = RVLocs[0].getValVT();
1064 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1065 TargetRegisterClass *SrcRC = DstRC;
1066
1067 // If this is a call to a function that returns an fp value on the x87 fp
1068 // stack, but where we prefer to use the value in xmm registers, copy it
1069 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1070 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1071 RVLocs[0].getLocReg() == X86::ST1) &&
1072 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1073 CopyVT = MVT::f80;
1074 SrcRC = X86::RSTRegisterClass;
1075 DstRC = X86::RFP80RegisterClass;
1076 }
1077
1078 unsigned ResultReg = createResultReg(DstRC);
1079 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1080 RVLocs[0].getLocReg(), DstRC, SrcRC);
1081 assert(Emitted && "Failed to emit a copy instruction!");
1082 if (CopyVT != RVLocs[0].getValVT()) {
1083 // Round the F80 the right size, which also moves to the appropriate xmm
1084 // register. This is accomplished by storing the F80 value in memory and
1085 // then loading it back. Ewww...
1086 MVT ResVT = RVLocs[0].getValVT();
1087 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1088 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001089 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001090 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1091 DstRC = ResVT == MVT::f32
1092 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1093 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1094 ResultReg = createResultReg(DstRC);
1095 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1096 }
1097
Evan Chengdebdea02008-09-08 17:15:42 +00001098 if (AndToI1) {
1099 // Mask out all but lowest bit for some call which produces an i1.
1100 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1101 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1102 ResultReg = AndResult;
1103 }
1104
Evan Chengf3d4efe2008-09-07 09:09:33 +00001105 UpdateValueMap(I, ResultReg);
1106 }
1107
1108 return true;
1109}
1110
1111
Dan Gohman99b21822008-08-28 23:21:34 +00001112bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001113X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001114 switch (I->getOpcode()) {
1115 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001116 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001117 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001118 case Instruction::Store:
1119 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001120 case Instruction::ICmp:
1121 case Instruction::FCmp:
1122 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001123 case Instruction::ZExt:
1124 return X86SelectZExt(I);
1125 case Instruction::Br:
1126 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001127 case Instruction::Call:
1128 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001129 case Instruction::LShr:
1130 case Instruction::AShr:
1131 case Instruction::Shl:
1132 return X86SelectShift(I);
1133 case Instruction::Select:
1134 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001135 case Instruction::Trunc:
1136 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001137 case Instruction::FPExt:
1138 return X86SelectFPExt(I);
1139 case Instruction::FPTrunc:
1140 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001141 }
1142
1143 return false;
1144}
1145
Dan Gohman0586d912008-09-10 20:11:02 +00001146unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001147 MVT VT;
1148 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001149 return false;
1150
1151 // Get opcode and regclass of the output for the given load instruction.
1152 unsigned Opc = 0;
1153 const TargetRegisterClass *RC = NULL;
1154 switch (VT.getSimpleVT()) {
1155 default: return false;
1156 case MVT::i8:
1157 Opc = X86::MOV8rm;
1158 RC = X86::GR8RegisterClass;
1159 break;
1160 case MVT::i16:
1161 Opc = X86::MOV16rm;
1162 RC = X86::GR16RegisterClass;
1163 break;
1164 case MVT::i32:
1165 Opc = X86::MOV32rm;
1166 RC = X86::GR32RegisterClass;
1167 break;
1168 case MVT::i64:
1169 // Must be in x86-64 mode.
1170 Opc = X86::MOV64rm;
1171 RC = X86::GR64RegisterClass;
1172 break;
1173 case MVT::f32:
1174 if (Subtarget->hasSSE1()) {
1175 Opc = X86::MOVSSrm;
1176 RC = X86::FR32RegisterClass;
1177 } else {
1178 Opc = X86::LD_Fp32m;
1179 RC = X86::RFP32RegisterClass;
1180 }
1181 break;
1182 case MVT::f64:
1183 if (Subtarget->hasSSE2()) {
1184 Opc = X86::MOVSDrm;
1185 RC = X86::FR64RegisterClass;
1186 } else {
1187 Opc = X86::LD_Fp64m;
1188 RC = X86::RFP64RegisterClass;
1189 }
1190 break;
1191 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001192 // No f80 support yet.
1193 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001194 }
1195
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001196 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001197 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001198 X86AddressMode AM;
1199 if (X86SelectAddress(C, AM, false)) {
1200 if (TLI.getPointerTy() == MVT::i32)
1201 Opc = X86::LEA32r;
1202 else
1203 Opc = X86::LEA64r;
1204 unsigned ResultReg = createResultReg(RC);
1205 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001206 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001207 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001208 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001209 }
1210
Owen Anderson3b217c62008-09-06 01:11:01 +00001211 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001212 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001213 if (Align == 0) {
1214 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001215 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001216 Align = Log2_64(Align);
1217 }
Owen Anderson95267a12008-09-05 00:06:23 +00001218
Dan Gohman0586d912008-09-10 20:11:02 +00001219 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001220 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001221 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001222 return ResultReg;
1223}
1224
Dan Gohman0586d912008-09-10 20:11:02 +00001225unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1226 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001227 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001228 return 0;
1229 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1230 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1231 unsigned ResultReg = createResultReg(RC);
1232 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1233 return ResultReg;
1234}
1235
Evan Chengc3f44b02008-09-03 00:03:49 +00001236namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001237 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001238 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001239 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001240 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1241 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001242 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001243 }
Dan Gohman99b21822008-08-28 23:21:34 +00001244}