blob: 44657ecd1c84b6531b1ba083984d3a0103345b22 [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:
Dan Gohman0586d912008-09-10 20:11:02 +000076 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000077
Evan Chengf3d4efe2008-09-07 09:09:33 +000078 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000079 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000080
81 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
82 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000083
Dan Gohman2ff7fd12008-09-19 22:16:54 +000084 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000085
Dan Gohman3df24e62008-09-03 23:12:08 +000086 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000087
88 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000089
90 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000091
92 bool X86SelectZExt(Instruction *I);
93
94 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000095
96 bool X86SelectShift(Instruction *I);
97
98 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000099
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000100 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000101
102 unsigned X86ChooseCmpOpcode(MVT VT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000103
Dan Gohman78efce62008-09-10 21:02:08 +0000104 bool X86SelectFPExt(Instruction *I);
105 bool X86SelectFPTrunc(Instruction *I);
106
Evan Chengf3d4efe2008-09-07 09:09:33 +0000107 bool X86SelectCall(Instruction *I);
108
109 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
110
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000111 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000112 return getTargetMachine()->getInstrInfo();
113 }
114 const X86TargetMachine *getTargetMachine() const {
115 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000116 }
117
Dan Gohman0586d912008-09-10 20:11:02 +0000118 unsigned TargetMaterializeConstant(Constant *C);
119
120 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000121
122 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
123 /// computed in an SSE register, not on the X87 floating point stack.
124 bool isScalarFPTypeInSSEReg(MVT VT) const {
125 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
126 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
127 }
128
Dan Gohman9b66d732008-09-30 00:48:39 +0000129 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
130 bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000131};
Dan Gohman99b21822008-08-28 23:21:34 +0000132
Dan Gohman9b66d732008-09-30 00:48:39 +0000133bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
134 MVT &VT, bool AllowI1) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
136 if (VT == MVT::Other || !VT.isSimple())
137 // Unhandled type. Halt "fast" selection and bail.
138 return false;
139 if (VT == MVT::iPTR)
140 // Use pointer type.
141 VT = TLI.getPointerTy();
Dan Gohman9b66d732008-09-30 00:48:39 +0000142 // For now, require SSE/SSE2 for performing floating-point operations,
143 // since x87 requires additional work.
144 if (VT == MVT::f64 && !X86ScalarSSEf64)
145 return false;
146 if (VT == MVT::f32 && !X86ScalarSSEf32)
147 return false;
148 // Similarly, no f80 support yet.
149 if (VT == MVT::f80)
150 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151 // We only handle legal types. For example, on x86-32 the instruction
152 // selector contains all of the 64-bit instructions from x86-64,
153 // under the assumption that i64 won't be used if the target doesn't
154 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000155 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000156}
157
158#include "X86GenCallingConv.inc"
159
160/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
161/// convention.
162CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
163 if (Subtarget->is64Bit()) {
164 if (Subtarget->isTargetWin64())
165 return CC_X86_Win64_C;
166 else if (CC == CallingConv::Fast && isTaillCall)
167 return CC_X86_64_TailCall;
168 else
169 return CC_X86_64_C;
170 }
171
172 if (CC == CallingConv::X86_FastCall)
173 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174 else if (CC == CallingConv::Fast)
175 return CC_X86_32_FastCC;
176 else
177 return CC_X86_32_C;
178}
179
Evan Cheng0de588f2008-09-05 21:00:03 +0000180/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000181/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000182/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000183bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 unsigned &ResultReg) {
185 // Get opcode and regclass of the output for the given load instruction.
186 unsigned Opc = 0;
187 const TargetRegisterClass *RC = NULL;
188 switch (VT.getSimpleVT()) {
189 default: return false;
190 case MVT::i8:
191 Opc = X86::MOV8rm;
192 RC = X86::GR8RegisterClass;
193 break;
194 case MVT::i16:
195 Opc = X86::MOV16rm;
196 RC = X86::GR16RegisterClass;
197 break;
198 case MVT::i32:
199 Opc = X86::MOV32rm;
200 RC = X86::GR32RegisterClass;
201 break;
202 case MVT::i64:
203 // Must be in x86-64 mode.
204 Opc = X86::MOV64rm;
205 RC = X86::GR64RegisterClass;
206 break;
207 case MVT::f32:
208 if (Subtarget->hasSSE1()) {
209 Opc = X86::MOVSSrm;
210 RC = X86::FR32RegisterClass;
211 } else {
212 Opc = X86::LD_Fp32m;
213 RC = X86::RFP32RegisterClass;
214 }
215 break;
216 case MVT::f64:
217 if (Subtarget->hasSSE2()) {
218 Opc = X86::MOVSDrm;
219 RC = X86::FR64RegisterClass;
220 } else {
221 Opc = X86::LD_Fp64m;
222 RC = X86::RFP64RegisterClass;
223 }
224 break;
225 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000226 // No f80 support yet.
227 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 }
229
230 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
232 return true;
233}
234
Evan Chengf3d4efe2008-09-07 09:09:33 +0000235/// X86FastEmitStore - Emit a machine instruction to store a value Val of
236/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
237/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000238/// i.e. V. Return true if it is possible.
239bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000240X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000241 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000242 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000243 unsigned Opc = 0;
244 const TargetRegisterClass *RC = NULL;
245 switch (VT.getSimpleVT()) {
246 default: return false;
247 case MVT::i8:
248 Opc = X86::MOV8mr;
249 RC = X86::GR8RegisterClass;
250 break;
251 case MVT::i16:
252 Opc = X86::MOV16mr;
253 RC = X86::GR16RegisterClass;
254 break;
255 case MVT::i32:
256 Opc = X86::MOV32mr;
257 RC = X86::GR32RegisterClass;
258 break;
259 case MVT::i64:
260 // Must be in x86-64 mode.
261 Opc = X86::MOV64mr;
262 RC = X86::GR64RegisterClass;
263 break;
264 case MVT::f32:
265 if (Subtarget->hasSSE1()) {
266 Opc = X86::MOVSSmr;
267 RC = X86::FR32RegisterClass;
268 } else {
269 Opc = X86::ST_Fp32m;
270 RC = X86::RFP32RegisterClass;
271 }
272 break;
273 case MVT::f64:
274 if (Subtarget->hasSSE2()) {
275 Opc = X86::MOVSDmr;
276 RC = X86::FR64RegisterClass;
277 } else {
278 Opc = X86::ST_Fp64m;
279 RC = X86::RFP64RegisterClass;
280 }
281 break;
282 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000283 // No f80 support yet.
284 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000285 }
286
Evan Chengf3d4efe2008-09-07 09:09:33 +0000287 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000288 return true;
289}
290
Evan Cheng24e3a902008-09-08 06:35:17 +0000291/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
292/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
293/// ISD::SIGN_EXTEND).
294bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
295 unsigned Src, MVT SrcVT,
296 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000297 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
298
299 if (RR != 0) {
300 ResultReg = RR;
301 return true;
302 } else
303 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000304}
305
Dan Gohman0586d912008-09-10 20:11:02 +0000306/// X86SelectAddress - Attempt to fill in an address from the given value.
307///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000308bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000309 User *U;
310 unsigned Opcode = Instruction::UserOp1;
311 if (Instruction *I = dyn_cast<Instruction>(V)) {
312 Opcode = I->getOpcode();
313 U = I;
314 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
315 Opcode = C->getOpcode();
316 U = C;
317 }
Dan Gohman0586d912008-09-10 20:11:02 +0000318
Dan Gohman35893082008-09-18 23:23:44 +0000319 switch (Opcode) {
320 default: break;
321 case Instruction::BitCast:
322 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000323 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000324
325 case Instruction::IntToPtr:
326 // Look past no-op inttoptrs.
327 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000328 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000329
330 case Instruction::PtrToInt:
331 // Look past no-op ptrtoints.
332 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000333 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000334
335 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000336 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000337 // Do static allocas.
338 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000339 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000340 if (SI != StaticAllocaMap.end()) {
341 AM.BaseType = X86AddressMode::FrameIndexBase;
342 AM.Base.FrameIndex = SI->second;
343 return true;
344 }
345 break;
Dan Gohman35893082008-09-18 23:23:44 +0000346 }
347
348 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000349 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000350 // Adds of constants are common and easy enough.
351 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000352 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
353 // They have to fit in the 32-bit signed displacement field though.
354 if (isInt32(Disp)) {
355 AM.Disp = (uint32_t)Disp;
356 return X86SelectAddress(U->getOperand(0), AM, isCall);
357 }
Dan Gohman0586d912008-09-10 20:11:02 +0000358 }
Dan Gohman35893082008-09-18 23:23:44 +0000359 break;
360 }
361
362 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000363 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000364 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000365 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000366 unsigned IndexReg = AM.IndexReg;
367 unsigned Scale = AM.Scale;
368 gep_type_iterator GTI = gep_type_begin(U);
369 // Look at all but the last index. Constants can be folded,
370 // and one dynamic index can be handled, if the scale is supported.
371 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
372 i != e; ++i, ++GTI) {
373 Value *Op = *i;
374 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
375 const StructLayout *SL = TD.getStructLayout(STy);
376 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
377 Disp += SL->getElementOffset(Idx);
378 } else {
379 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
380 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
381 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000382 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000383 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000384 (!AM.GV ||
385 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000386 (S == 1 || S == 2 || S == 4 || S == 8)) {
387 // Scaled-index addressing.
388 Scale = S;
389 IndexReg = getRegForValue(Op);
390 if (IndexReg == 0)
391 return false;
392 } else
393 // Unsupported.
394 goto unsupported_gep;
395 }
396 }
Dan Gohman09aae462008-09-26 20:04:15 +0000397 // Check for displacement overflow.
398 if (!isInt32(Disp))
399 break;
Dan Gohman35893082008-09-18 23:23:44 +0000400 // Ok, the GEP indices were covered by constant-offset and scaled-index
401 // addressing. Update the address state and move on to examining the base.
402 AM.IndexReg = IndexReg;
403 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000404 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000405 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000406 unsupported_gep:
407 // Ok, the GEP indices weren't all covered.
408 break;
409 }
410 }
411
412 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000413 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000414 // Can't handle alternate code models yet.
415 if (TM.getCodeModel() != CodeModel::Default &&
416 TM.getCodeModel() != CodeModel::Small)
417 return false;
418
Dan Gohman97135e12008-09-26 19:15:30 +0000419 // RIP-relative addresses can't have additional register operands.
420 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
421 (AM.Base.Reg != 0 || AM.IndexReg != 0))
422 return false;
423
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000424 // Set up the basic address.
425 AM.GV = GV;
426 if (!isCall &&
427 TM.getRelocationModel() == Reloc::PIC_ &&
428 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000429 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000430
431 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000432 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
433 // Check to see if we've already materialized this
434 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000435 if (unsigned Reg = LocalValueMap[V]) {
436 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000437 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000438 return true;
439 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000440 // Issue load from stub if necessary.
441 unsigned Opc = 0;
442 const TargetRegisterClass *RC = NULL;
443 if (TLI.getPointerTy() == MVT::i32) {
444 Opc = X86::MOV32rm;
445 RC = X86::GR32RegisterClass;
446 } else {
447 Opc = X86::MOV64rm;
448 RC = X86::GR64RegisterClass;
449 }
Dan Gohman789ce772008-09-25 23:34:02 +0000450
451 X86AddressMode StubAM;
452 StubAM.Base.Reg = AM.Base.Reg;
453 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000454 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000455 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
456
457 // Now construct the final address. Note that the Disp, Scale,
458 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000459 AM.Base.Reg = ResultReg;
460 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000461
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000462 // Prevent loading GV stub multiple times in same MBB.
463 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000464 }
465 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000466 }
467
Dan Gohman97135e12008-09-26 19:15:30 +0000468 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000469 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000470 if (AM.Base.Reg == 0) {
471 AM.Base.Reg = getRegForValue(V);
472 return AM.Base.Reg != 0;
473 }
474 if (AM.IndexReg == 0) {
475 assert(AM.Scale == 1 && "Scale with no index!");
476 AM.IndexReg = getRegForValue(V);
477 return AM.IndexReg != 0;
478 }
479 }
480
481 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000482}
483
Owen Andersona3971df2008-09-04 07:08:58 +0000484/// X86SelectStore - Select and emit code to implement store instructions.
485bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000486 MVT VT;
487 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000488 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000489 unsigned Val = getRegForValue(I->getOperand(0));
490 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000491 // Unhandled operand. Halt "fast" selection and bail.
492 return false;
493
Dan Gohman0586d912008-09-10 20:11:02 +0000494 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000495 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000496 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000497
Dan Gohman0586d912008-09-10 20:11:02 +0000498 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000499}
500
Evan Cheng8b19e562008-09-03 06:44:39 +0000501/// X86SelectLoad - Select and emit code to implement load instructions.
502///
Dan Gohman3df24e62008-09-03 23:12:08 +0000503bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000504 MVT VT;
505 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000506 return false;
507
Dan Gohman0586d912008-09-10 20:11:02 +0000508 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000509 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000510 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000511
Evan Cheng0de588f2008-09-05 21:00:03 +0000512 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000513 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000514 UpdateValueMap(I, ResultReg);
515 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000516 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000517 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000518}
519
Dan Gohmand98d6202008-10-02 22:15:21 +0000520unsigned X86FastISel::X86ChooseCmpOpcode(MVT VT) {
521 switch (VT.getSimpleVT()) {
522 case MVT::i8: return X86::CMP8rr;
523 case MVT::i16: return X86::CMP16rr;
524 case MVT::i32: return X86::CMP32rr;
525 case MVT::i64: return X86::CMP64rr;
526 case MVT::f32: return X86::UCOMISSrr;
527 case MVT::f64: return X86::UCOMISDrr;
528 default: break;
529 }
530 return 0;
531}
532
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000533bool X86FastISel::X86SelectCmp(Instruction *I) {
534 CmpInst *CI = cast<CmpInst>(I);
535
Dan Gohman9b66d732008-09-30 00:48:39 +0000536 MVT VT;
537 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000538 return false;
539
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000540 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000541 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000542 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000543 if (Op1Reg == 0) return false;
544
Dan Gohmand98d6202008-10-02 22:15:21 +0000545 unsigned Opc = X86ChooseCmpOpcode(VT);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000546
547 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000548 unsigned SetCCOpc;
549
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000550 switch (CI->getPredicate()) {
551 case CmpInst::FCMP_OEQ: {
552 unsigned EReg = createResultReg(&X86::GR8RegClass);
553 unsigned NPReg = createResultReg(&X86::GR8RegClass);
554 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
555 BuildMI(MBB, TII.get(X86::SETEr), EReg);
556 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
557 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000558 UpdateValueMap(I, ResultReg);
559 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000560 }
561 case CmpInst::FCMP_UNE: {
562 unsigned NEReg = createResultReg(&X86::GR8RegClass);
563 unsigned PReg = createResultReg(&X86::GR8RegClass);
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
566 BuildMI(MBB, TII.get(X86::SETPr), PReg);
567 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000568 UpdateValueMap(I, ResultReg);
569 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000570 }
571 case CmpInst::FCMP_OGT:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000573 SetCCOpc = X86::SETAr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000574 break;
575 case CmpInst::FCMP_OGE:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000577 SetCCOpc = X86::SETAEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000578 break;
579 case CmpInst::FCMP_OLT:
580 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000581 SetCCOpc = X86::SETAr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000582 break;
583 case CmpInst::FCMP_OLE:
584 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000585 SetCCOpc = X86::SETAEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000586 break;
587 case CmpInst::FCMP_ONE:
588 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000589 SetCCOpc = X86::SETNEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000590 break;
591 case CmpInst::FCMP_ORD:
592 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000593 SetCCOpc = X86::SETNPr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000594 break;
595 case CmpInst::FCMP_UNO:
596 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000597 SetCCOpc = X86::SETPr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000598 break;
599 case CmpInst::FCMP_UEQ:
600 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000601 SetCCOpc = X86::SETEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000602 break;
603 case CmpInst::FCMP_UGT:
604 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000605 SetCCOpc = X86::SETBr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000606 break;
607 case CmpInst::FCMP_UGE:
608 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000609 SetCCOpc = X86::SETBEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000610 break;
611 case CmpInst::FCMP_ULT:
612 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000613 SetCCOpc = X86::SETBr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000614 break;
615 case CmpInst::FCMP_ULE:
616 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000617 SetCCOpc = X86::SETBEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000618 break;
619 case CmpInst::ICMP_EQ:
620 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000621 SetCCOpc = X86::SETEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000622 break;
623 case CmpInst::ICMP_NE:
624 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000625 SetCCOpc = X86::SETNEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000626 break;
627 case CmpInst::ICMP_UGT:
628 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000629 SetCCOpc = X86::SETAr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000630 break;
631 case CmpInst::ICMP_UGE:
632 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000633 SetCCOpc = X86::SETAEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000634 break;
635 case CmpInst::ICMP_ULT:
636 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000637 SetCCOpc = X86::SETBr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000638 break;
639 case CmpInst::ICMP_ULE:
640 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000641 SetCCOpc = X86::SETBEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000642 break;
643 case CmpInst::ICMP_SGT:
644 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000645 SetCCOpc = X86::SETGr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000646 break;
647 case CmpInst::ICMP_SGE:
648 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000649 SetCCOpc = X86::SETGEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000650 break;
651 case CmpInst::ICMP_SLT:
652 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000653 SetCCOpc = X86::SETLr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000654 break;
655 case CmpInst::ICMP_SLE:
656 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000657 SetCCOpc = X86::SETLEr;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000658 break;
659 default:
660 return false;
661 }
662
Chris Lattner54aebde2008-10-15 03:47:17 +0000663 if (SetCCOpc)
664 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
665
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000666 UpdateValueMap(I, ResultReg);
667 return true;
668}
Evan Cheng8b19e562008-09-03 06:44:39 +0000669
Dan Gohmand89ae992008-09-05 01:06:14 +0000670bool X86FastISel::X86SelectZExt(Instruction *I) {
671 // Special-case hack: The only i1 values we know how to produce currently
672 // set the upper bits of an i8 value to zero.
673 if (I->getType() == Type::Int8Ty &&
674 I->getOperand(0)->getType() == Type::Int1Ty) {
675 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000676 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000677 UpdateValueMap(I, ResultReg);
678 return true;
679 }
680
681 return false;
682}
683
684bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000685 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000686 // Handle a conditional branch.
687 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000688 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
689 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
690
Dan Gohmand98d6202008-10-02 22:15:21 +0000691 // Fold the common case of a conditional branch with a comparison.
692 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
693 if (CI->hasOneUse()) {
694 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
695 unsigned Opc = X86ChooseCmpOpcode(VT);
696 if (Opc == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000697
Dan Gohmand98d6202008-10-02 22:15:21 +0000698 // Try to take advantage of fallthrough opportunities.
699 CmpInst::Predicate Predicate = CI->getPredicate();
700 if (MBB->isLayoutSuccessor(TrueMBB)) {
701 std::swap(TrueMBB, FalseMBB);
702 Predicate = CmpInst::getInversePredicate(Predicate);
703 }
704
705 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
706 if (Op0Reg == 0) return false;
707 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
708 if (Op1Reg == 0) return false;
709
Chris Lattner54aebde2008-10-15 03:47:17 +0000710 unsigned BranchOpc;
Dan Gohmand98d6202008-10-02 22:15:21 +0000711 switch (Predicate) {
712 case CmpInst::FCMP_OGT:
713 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000714 BranchOpc = X86::JA;
Dan Gohmand98d6202008-10-02 22:15:21 +0000715 break;
716 case CmpInst::FCMP_OGE:
717 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000718 BranchOpc = X86::JAE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000719 break;
720 case CmpInst::FCMP_OLT:
721 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000722 BranchOpc = X86::JA;
Dan Gohmand98d6202008-10-02 22:15:21 +0000723 break;
724 case CmpInst::FCMP_OLE:
725 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000726 BranchOpc = X86::JAE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000727 break;
728 case CmpInst::FCMP_ONE:
729 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000730 BranchOpc = X86::JNE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000731 break;
732 case CmpInst::FCMP_ORD:
733 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000734 BranchOpc = X86::JNP;
Dan Gohmand98d6202008-10-02 22:15:21 +0000735 break;
736 case CmpInst::FCMP_UNO:
737 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000738 BranchOpc = X86::JP;
Dan Gohmand98d6202008-10-02 22:15:21 +0000739 break;
740 case CmpInst::FCMP_UEQ:
741 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000742 BranchOpc = X86::JE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000743 break;
744 case CmpInst::FCMP_UGT:
745 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000746 BranchOpc = X86::JB;
Dan Gohmand98d6202008-10-02 22:15:21 +0000747 break;
748 case CmpInst::FCMP_UGE:
749 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000750 BranchOpc = X86::JBE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000751 break;
752 case CmpInst::FCMP_ULT:
753 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000754 BranchOpc = X86::JB;
Dan Gohmand98d6202008-10-02 22:15:21 +0000755 break;
756 case CmpInst::FCMP_ULE:
757 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000758 BranchOpc = X86::JBE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000759 break;
760 case CmpInst::ICMP_EQ:
761 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000762 BranchOpc = X86::JE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000763 break;
764 case CmpInst::ICMP_NE:
765 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000766 BranchOpc = X86::JNE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000767 break;
768 case CmpInst::ICMP_UGT:
769 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000770 BranchOpc = X86::JA;
Dan Gohmand98d6202008-10-02 22:15:21 +0000771 break;
772 case CmpInst::ICMP_UGE:
773 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000774 BranchOpc = X86::JAE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000775 break;
776 case CmpInst::ICMP_ULT:
777 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000778 BranchOpc = X86::JB;
Dan Gohmand98d6202008-10-02 22:15:21 +0000779 break;
780 case CmpInst::ICMP_ULE:
781 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000782 BranchOpc = X86::JBE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000783 break;
784 case CmpInst::ICMP_SGT:
785 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000786 BranchOpc = X86::JG;
Dan Gohmand98d6202008-10-02 22:15:21 +0000787 break;
788 case CmpInst::ICMP_SGE:
789 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000790 BranchOpc = X86::JGE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000791 break;
792 case CmpInst::ICMP_SLT:
793 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000794 BranchOpc = X86::JL;
Dan Gohmand98d6202008-10-02 22:15:21 +0000795 break;
796 case CmpInst::ICMP_SLE:
797 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000798 BranchOpc = X86::JLE;
Dan Gohmand98d6202008-10-02 22:15:21 +0000799 break;
800 default:
801 return false;
802 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000803
804 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000805 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000806 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000807 return true;
808 }
809 }
810
811 // Otherwise do a clumsy setcc and re-test it.
812 unsigned OpReg = getRegForValue(BI->getCondition());
813 if (OpReg == 0) return false;
814
815 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
816
817 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000818 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000819 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000820
821 return true;
822}
823
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000824bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000825 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000826 const TargetRegisterClass *RC = NULL;
827 if (I->getType() == Type::Int8Ty) {
828 CReg = X86::CL;
829 RC = &X86::GR8RegClass;
830 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000831 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
832 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
833 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000834 default: return false;
835 }
836 } else if (I->getType() == Type::Int16Ty) {
837 CReg = X86::CX;
838 RC = &X86::GR16RegClass;
839 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000840 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
841 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
842 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000843 default: return false;
844 }
845 } else if (I->getType() == Type::Int32Ty) {
846 CReg = X86::ECX;
847 RC = &X86::GR32RegClass;
848 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000849 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
850 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
851 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000852 default: return false;
853 }
854 } else if (I->getType() == Type::Int64Ty) {
855 CReg = X86::RCX;
856 RC = &X86::GR64RegClass;
857 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000858 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
859 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
860 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000861 default: return false;
862 }
863 } else {
864 return false;
865 }
866
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000867 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000868 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000869 return false;
870
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000871 unsigned Op0Reg = getRegForValue(I->getOperand(0));
872 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000873
874 // Fold immediate in shl(x,3).
875 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
876 unsigned ResultReg = createResultReg(RC);
877 BuildMI(MBB, TII.get(OpImm),
878 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
879 UpdateValueMap(I, ResultReg);
880 return true;
881 }
882
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000883 unsigned Op1Reg = getRegForValue(I->getOperand(1));
884 if (Op1Reg == 0) return false;
885 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000886
887 // The shift instruction uses X86::CL. If we defined a super-register
888 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
889 // we're doing here.
890 if (CReg != X86::CL)
891 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
892 .addReg(CReg).addImm(X86::SUBREG_8BIT);
893
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000894 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000895 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000896 UpdateValueMap(I, ResultReg);
897 return true;
898}
899
900bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000901 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000902 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000903 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000904
905 unsigned Opc = 0;
906 const TargetRegisterClass *RC = NULL;
907 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000908 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000909 RC = &X86::GR16RegClass;
910 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000911 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000912 RC = &X86::GR32RegClass;
913 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000914 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000915 RC = &X86::GR64RegClass;
916 } else {
917 return false;
918 }
919
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000920 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000921 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000922 return false;
923
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000924 unsigned Op0Reg = getRegForValue(I->getOperand(0));
925 if (Op0Reg == 0) return false;
926 unsigned Op1Reg = getRegForValue(I->getOperand(1));
927 if (Op1Reg == 0) return false;
928 unsigned Op2Reg = getRegForValue(I->getOperand(2));
929 if (Op2Reg == 0) return false;
930
931 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
932 unsigned ResultReg = createResultReg(RC);
933 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
934 UpdateValueMap(I, ResultReg);
935 return true;
936}
937
Dan Gohman78efce62008-09-10 21:02:08 +0000938bool X86FastISel::X86SelectFPExt(Instruction *I) {
939 if (Subtarget->hasSSE2()) {
940 if (I->getType() == Type::DoubleTy) {
941 Value *V = I->getOperand(0);
942 if (V->getType() == Type::FloatTy) {
943 unsigned OpReg = getRegForValue(V);
944 if (OpReg == 0) return false;
945 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
946 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
947 UpdateValueMap(I, ResultReg);
948 return true;
949 }
950 }
951 }
952
953 return false;
954}
955
956bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
957 if (Subtarget->hasSSE2()) {
958 if (I->getType() == Type::FloatTy) {
959 Value *V = I->getOperand(0);
960 if (V->getType() == Type::DoubleTy) {
961 unsigned OpReg = getRegForValue(V);
962 if (OpReg == 0) return false;
963 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
964 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
965 UpdateValueMap(I, ResultReg);
966 return true;
967 }
968 }
969 }
970
971 return false;
972}
973
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000974bool X86FastISel::X86SelectTrunc(Instruction *I) {
975 if (Subtarget->is64Bit())
976 // All other cases should be handled by the tblgen generated code.
977 return false;
978 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
979 MVT DstVT = TLI.getValueType(I->getType());
980 if (DstVT != MVT::i8)
981 // All other cases should be handled by the tblgen generated code.
982 return false;
983 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
984 // All other cases should be handled by the tblgen generated code.
985 return false;
986
987 unsigned InputReg = getRegForValue(I->getOperand(0));
988 if (!InputReg)
989 // Unhandled operand. Halt "fast" selection and bail.
990 return false;
991
992 // First issue a copy to GR16_ or GR32_.
993 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
994 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
995 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
996 unsigned CopyReg = createResultReg(CopyRC);
997 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
998
999 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +00001000 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001001 if (!ResultReg)
1002 return false;
1003
1004 UpdateValueMap(I, ResultReg);
1005 return true;
1006}
1007
Evan Chengf3d4efe2008-09-07 09:09:33 +00001008bool X86FastISel::X86SelectCall(Instruction *I) {
1009 CallInst *CI = cast<CallInst>(I);
1010 Value *Callee = I->getOperand(0);
1011
1012 // Can't handle inline asm yet.
1013 if (isa<InlineAsm>(Callee))
1014 return false;
1015
1016 // FIXME: Handle some intrinsics.
1017 if (Function *F = CI->getCalledFunction()) {
1018 if (F->isDeclaration() &&F->getIntrinsicID())
1019 return false;
1020 }
1021
Evan Chengf3d4efe2008-09-07 09:09:33 +00001022 // Handle only C and fastcc calling conventions for now.
1023 CallSite CS(CI);
1024 unsigned CC = CS.getCallingConv();
1025 if (CC != CallingConv::C &&
1026 CC != CallingConv::Fast &&
1027 CC != CallingConv::X86_FastCall)
1028 return false;
1029
1030 // Let SDISel handle vararg functions.
1031 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1032 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1033 if (FTy->isVarArg())
1034 return false;
1035
1036 // Handle *simple* calls for now.
1037 const Type *RetTy = CS.getType();
1038 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001039 if (RetTy == Type::VoidTy)
1040 RetVT = MVT::isVoid;
1041 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001042 return false;
1043
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001044 // Materialize callee address in a register. FIXME: GV address can be
1045 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001046 X86AddressMode CalleeAM;
1047 if (!X86SelectAddress(Callee, CalleeAM, true))
1048 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001049 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001050 GlobalValue *GV = 0;
1051 if (CalleeAM.Base.Reg != 0) {
1052 assert(CalleeAM.GV == 0);
1053 CalleeOp = CalleeAM.Base.Reg;
1054 } else if (CalleeAM.GV != 0) {
1055 assert(CalleeAM.GV != 0);
1056 GV = CalleeAM.GV;
1057 } else
1058 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001059
Evan Chengdebdea02008-09-08 17:15:42 +00001060 // Allow calls which produce i1 results.
1061 bool AndToI1 = false;
1062 if (RetVT == MVT::i1) {
1063 RetVT = MVT::i8;
1064 AndToI1 = true;
1065 }
1066
Evan Chengf3d4efe2008-09-07 09:09:33 +00001067 // Deal with call operands first.
1068 SmallVector<unsigned, 4> Args;
1069 SmallVector<MVT, 4> ArgVTs;
1070 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1071 Args.reserve(CS.arg_size());
1072 ArgVTs.reserve(CS.arg_size());
1073 ArgFlags.reserve(CS.arg_size());
1074 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1075 i != e; ++i) {
1076 unsigned Arg = getRegForValue(*i);
1077 if (Arg == 0)
1078 return false;
1079 ISD::ArgFlagsTy Flags;
1080 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001081 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001082 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001083 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001084 Flags.setZExt();
1085
1086 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001087 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1088 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1089 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1090 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001091 return false;
1092
1093 const Type *ArgTy = (*i)->getType();
1094 MVT ArgVT;
1095 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1096 return false;
1097 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1098 Flags.setOrigAlign(OriginalAlignment);
1099
1100 Args.push_back(Arg);
1101 ArgVTs.push_back(ArgVT);
1102 ArgFlags.push_back(Flags);
1103 }
1104
1105 // Analyze operands of the call, assigning locations to each operand.
1106 SmallVector<CCValAssign, 16> ArgLocs;
1107 CCState CCInfo(CC, false, TM, ArgLocs);
1108 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1109
1110 // Get a count of how many bytes are to be pushed on the stack.
1111 unsigned NumBytes = CCInfo.getNextStackOffset();
1112
1113 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001114 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1115 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001116
1117 // Process argumenet: walk the register/memloc assignments, inserting
1118 // copies / loads.
1119 SmallVector<unsigned, 4> RegArgs;
1120 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1121 CCValAssign &VA = ArgLocs[i];
1122 unsigned Arg = Args[VA.getValNo()];
1123 MVT ArgVT = ArgVTs[VA.getValNo()];
1124
1125 // Promote the value if needed.
1126 switch (VA.getLocInfo()) {
1127 default: assert(0 && "Unknown loc info!");
1128 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001129 case CCValAssign::SExt: {
1130 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1131 Arg, ArgVT, Arg);
1132 assert(Emitted && "Failed to emit a sext!");
1133 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001134 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001135 }
1136 case CCValAssign::ZExt: {
1137 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1138 Arg, ArgVT, Arg);
1139 assert(Emitted && "Failed to emit a zext!");
1140 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001141 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001142 }
1143 case CCValAssign::AExt: {
1144 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1145 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001146 if (!Emitted)
1147 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1148 Arg, ArgVT, Arg);
1149 if (!Emitted)
1150 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1151 Arg, ArgVT, Arg);
1152
Evan Cheng24e3a902008-09-08 06:35:17 +00001153 assert(Emitted && "Failed to emit a aext!");
1154 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001155 break;
1156 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001157 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001158
1159 if (VA.isRegLoc()) {
1160 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1161 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1162 Arg, RC, RC);
1163 assert(Emitted && "Failed to emit a copy instruction!");
1164 RegArgs.push_back(VA.getLocReg());
1165 } else {
1166 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001167 X86AddressMode AM;
1168 AM.Base.Reg = StackPtr;
1169 AM.Disp = LocMemOffset;
1170 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001171 }
1172 }
1173
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001174 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1175 // GOT pointer.
1176 if (!Subtarget->is64Bit() &&
1177 TM.getRelocationModel() == Reloc::PIC_ &&
1178 Subtarget->isPICStyleGOT()) {
1179 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001180 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001181 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1182 assert(Emitted && "Failed to emit a copy instruction!");
1183 }
1184
Evan Chengf3d4efe2008-09-07 09:09:33 +00001185 // Issue the call.
1186 unsigned CallOpc = CalleeOp
1187 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1188 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1189 MachineInstrBuilder MIB = CalleeOp
1190 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001191 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001192
1193 // Add an implicit use GOT pointer in EBX.
1194 if (!Subtarget->is64Bit() &&
1195 TM.getRelocationModel() == Reloc::PIC_ &&
1196 Subtarget->isPICStyleGOT())
1197 MIB.addReg(X86::EBX);
1198
Evan Chengf3d4efe2008-09-07 09:09:33 +00001199 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001200 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1201 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001202
1203 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001204 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1205 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001206
1207 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001208 if (RetVT.getSimpleVT() != MVT::isVoid) {
1209 SmallVector<CCValAssign, 16> RVLocs;
1210 CCState CCInfo(CC, false, TM, RVLocs);
1211 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1212
1213 // Copy all of the result registers out of their specified physreg.
1214 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1215 MVT CopyVT = RVLocs[0].getValVT();
1216 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1217 TargetRegisterClass *SrcRC = DstRC;
1218
1219 // If this is a call to a function that returns an fp value on the x87 fp
1220 // stack, but where we prefer to use the value in xmm registers, copy it
1221 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1222 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1223 RVLocs[0].getLocReg() == X86::ST1) &&
1224 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1225 CopyVT = MVT::f80;
1226 SrcRC = X86::RSTRegisterClass;
1227 DstRC = X86::RFP80RegisterClass;
1228 }
1229
1230 unsigned ResultReg = createResultReg(DstRC);
1231 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1232 RVLocs[0].getLocReg(), DstRC, SrcRC);
1233 assert(Emitted && "Failed to emit a copy instruction!");
1234 if (CopyVT != RVLocs[0].getValVT()) {
1235 // Round the F80 the right size, which also moves to the appropriate xmm
1236 // register. This is accomplished by storing the F80 value in memory and
1237 // then loading it back. Ewww...
1238 MVT ResVT = RVLocs[0].getValVT();
1239 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1240 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001241 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001242 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1243 DstRC = ResVT == MVT::f32
1244 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1245 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1246 ResultReg = createResultReg(DstRC);
1247 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1248 }
1249
Evan Chengdebdea02008-09-08 17:15:42 +00001250 if (AndToI1) {
1251 // Mask out all but lowest bit for some call which produces an i1.
1252 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1253 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1254 ResultReg = AndResult;
1255 }
1256
Evan Chengf3d4efe2008-09-07 09:09:33 +00001257 UpdateValueMap(I, ResultReg);
1258 }
1259
1260 return true;
1261}
1262
1263
Dan Gohman99b21822008-08-28 23:21:34 +00001264bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001265X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001266 switch (I->getOpcode()) {
1267 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001268 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001269 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001270 case Instruction::Store:
1271 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001272 case Instruction::ICmp:
1273 case Instruction::FCmp:
1274 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001275 case Instruction::ZExt:
1276 return X86SelectZExt(I);
1277 case Instruction::Br:
1278 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001279 case Instruction::Call:
1280 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001281 case Instruction::LShr:
1282 case Instruction::AShr:
1283 case Instruction::Shl:
1284 return X86SelectShift(I);
1285 case Instruction::Select:
1286 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001287 case Instruction::Trunc:
1288 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001289 case Instruction::FPExt:
1290 return X86SelectFPExt(I);
1291 case Instruction::FPTrunc:
1292 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001293 }
1294
1295 return false;
1296}
1297
Dan Gohman0586d912008-09-10 20:11:02 +00001298unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001299 MVT VT;
1300 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001301 return false;
1302
1303 // Get opcode and regclass of the output for the given load instruction.
1304 unsigned Opc = 0;
1305 const TargetRegisterClass *RC = NULL;
1306 switch (VT.getSimpleVT()) {
1307 default: return false;
1308 case MVT::i8:
1309 Opc = X86::MOV8rm;
1310 RC = X86::GR8RegisterClass;
1311 break;
1312 case MVT::i16:
1313 Opc = X86::MOV16rm;
1314 RC = X86::GR16RegisterClass;
1315 break;
1316 case MVT::i32:
1317 Opc = X86::MOV32rm;
1318 RC = X86::GR32RegisterClass;
1319 break;
1320 case MVT::i64:
1321 // Must be in x86-64 mode.
1322 Opc = X86::MOV64rm;
1323 RC = X86::GR64RegisterClass;
1324 break;
1325 case MVT::f32:
1326 if (Subtarget->hasSSE1()) {
1327 Opc = X86::MOVSSrm;
1328 RC = X86::FR32RegisterClass;
1329 } else {
1330 Opc = X86::LD_Fp32m;
1331 RC = X86::RFP32RegisterClass;
1332 }
1333 break;
1334 case MVT::f64:
1335 if (Subtarget->hasSSE2()) {
1336 Opc = X86::MOVSDrm;
1337 RC = X86::FR64RegisterClass;
1338 } else {
1339 Opc = X86::LD_Fp64m;
1340 RC = X86::RFP64RegisterClass;
1341 }
1342 break;
1343 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001344 // No f80 support yet.
1345 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001346 }
1347
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001348 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001349 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001350 X86AddressMode AM;
1351 if (X86SelectAddress(C, AM, false)) {
1352 if (TLI.getPointerTy() == MVT::i32)
1353 Opc = X86::LEA32r;
1354 else
1355 Opc = X86::LEA64r;
1356 unsigned ResultReg = createResultReg(RC);
1357 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001358 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001359 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001360 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001361 }
1362
Owen Anderson3b217c62008-09-06 01:11:01 +00001363 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001364 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001365 if (Align == 0) {
1366 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001367 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001368 Align = Log2_64(Align);
1369 }
Owen Anderson95267a12008-09-05 00:06:23 +00001370
Dan Gohman5396c992008-09-30 01:21:32 +00001371 // x86-32 PIC requires a PIC base register for constant pools.
1372 unsigned PICBase = 0;
1373 if (TM.getRelocationModel() == Reloc::PIC_ &&
1374 !Subtarget->is64Bit())
1375 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1376
1377 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001378 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001379 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001380 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1381 PICBase);
1382
Owen Anderson95267a12008-09-05 00:06:23 +00001383 return ResultReg;
1384}
1385
Dan Gohman0586d912008-09-10 20:11:02 +00001386unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001387 // Fail on dynamic allocas. At this point, getRegForValue has already
1388 // checked its CSE maps, so if we're here trying to handle a dynamic
1389 // alloca, we're not going to succeed. X86SelectAddress has a
1390 // check for dynamic allocas, because it's called directly from
1391 // various places, but TargetMaterializeAlloca also needs a check
1392 // in order to avoid recursion between getRegForValue,
1393 // X86SelectAddrss, and TargetMaterializeAlloca.
1394 if (!StaticAllocaMap.count(C))
1395 return 0;
1396
Dan Gohman0586d912008-09-10 20:11:02 +00001397 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001398 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001399 return 0;
1400 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1401 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1402 unsigned ResultReg = createResultReg(RC);
1403 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1404 return ResultReg;
1405}
1406
Evan Chengc3f44b02008-09-03 00:03:49 +00001407namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001408 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001409 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001410 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001411 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001412 DenseMap<const AllocaInst *, int> &am
1413#ifndef NDEBUG
1414 , SmallSet<Instruction*, 8> &cil
1415#endif
1416 ) {
1417 return new X86FastISel(mf, mmi, vm, bm, am
1418#ifndef NDEBUG
1419 , cil
1420#endif
1421 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001422 }
Dan Gohman99b21822008-08-28 23:21:34 +00001423}