blob: 457b74250c8a2d7d292fef2fe3cbb40bdafa9db7 [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"
Evan Chengef41ff62011-06-23 17:54:54 +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"
Dan Gohmane9865942009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000025#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000026#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000027#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000028#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000029#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000030#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000031#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000034#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000036#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000037#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000038using namespace llvm;
39
Chris Lattner087fcf32009-03-08 18:44:31 +000040namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000041
Evan Chengc3f44b02008-09-03 00:03:49 +000042class X86FastISel : public FastISel {
43 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
44 /// make the right decision when generating code for different targets.
45 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000046
47 /// StackPtr - Register used as the stack pointer.
48 ///
49 unsigned StackPtr;
50
Wesley Peckbf17cfa2010-11-23 03:31:01 +000051 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000052 /// floating point ops.
53 /// When SSE is available, use it for f32 operations.
54 /// When SSE2 is available, use it for f64 operations.
55 bool X86ScalarSSEf64;
56 bool X86ScalarSSEf32;
57
Evan Cheng8b19e562008-09-03 06:44:39 +000058public:
Dan Gohmana4160c32010-07-07 16:29:44 +000059 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000060 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000061 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +000062 X86ScalarSSEf64 = Subtarget->hasSSE2() || Subtarget->hasAVX();
63 X86ScalarSSEf32 = Subtarget->hasSSE1() || Subtarget->hasAVX();
Evan Cheng88e30412008-09-03 01:04:47 +000064 }
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Dan Gohman46510a72010-04-15 01:51:59 +000066 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000067
Chris Lattnerbeac75d2010-09-05 02:18:34 +000068 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
69 /// vreg is being provided by the specified load instruction. If possible,
70 /// try to fold the load as an operand to the instruction, returning true if
71 /// possible.
72 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
73 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000074
Dan Gohman1adf1b02008-08-19 21:45:35 +000075#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000076
77private:
Dan Gohman46510a72010-04-15 01:51:59 +000078 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000079
Owen Andersone50ed302009-08-10 22:56:29 +000080 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000081
Chris Lattnerb44101c2011-04-19 05:09:50 +000082 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
83 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000086 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000087
Dan Gohman46510a72010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000094
Dan Gohman84023e02010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectFPExt(const Instruction *I);
110 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
113 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000114
Eli Friedman25255cb2011-06-10 23:39:36 +0000115 bool DoSelectCall(const Instruction *I, const char *MemIntName);
116
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000117 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000118 return getTargetMachine()->getInstrInfo();
119 }
120 const X86TargetMachine *getTargetMachine() const {
121 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000122 }
123
Dan Gohman46510a72010-04-15 01:51:59 +0000124 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000125
Dan Gohman46510a72010-04-15 01:51:59 +0000126 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127
Eli Friedman2790ba82011-04-27 22:41:55 +0000128 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
129
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
131 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000132 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
134 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 }
136
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000137 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000138
Eli Friedmanc0883452011-05-20 22:21:04 +0000139 bool IsMemcpySmall(uint64_t Len);
140
Eli Friedmand5089a92011-04-27 01:45:07 +0000141 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
142 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000143};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000144
Chris Lattner087fcf32009-03-08 18:44:31 +0000145} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000146
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000147bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000148 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
149 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000150 // Unhandled type. Halt "fast" selection and bail.
151 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000152
153 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 // For now, require SSE/SSE2 for performing floating-point operations,
155 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000159 return false;
160 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000162 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000163 // We only handle legal types. For example, on x86-32 the instruction
164 // selector contains all of the 64-bit instructions from x86-64,
165 // under the assumption that i64 won't be used if the target doesn't
166 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000168}
169
170#include "X86GenCallingConv.inc"
171
Evan Cheng0de588f2008-09-05 21:00:03 +0000172/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000173/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000174/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000175bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000176 unsigned &ResultReg) {
177 // Get opcode and regclass of the output for the given load instruction.
178 unsigned Opc = 0;
179 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000181 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000182 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 Opc = X86::MOV8rm;
185 RC = X86::GR8RegisterClass;
186 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 Opc = X86::MOV16rm;
189 RC = X86::GR16RegisterClass;
190 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000192 Opc = X86::MOV32rm;
193 RC = X86::GR32RegisterClass;
194 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000196 // Must be in x86-64 mode.
197 Opc = X86::MOV64rm;
198 RC = X86::GR64RegisterClass;
199 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000201 if (X86ScalarSSEf32) {
202 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 RC = X86::FR32RegisterClass;
204 } else {
205 Opc = X86::LD_Fp32m;
206 RC = X86::RFP32RegisterClass;
207 }
208 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000210 if (X86ScalarSSEf64) {
211 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000212 RC = X86::FR64RegisterClass;
213 } else {
214 Opc = X86::LD_Fp64m;
215 RC = X86::RFP64RegisterClass;
216 }
217 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000219 // No f80 support yet.
220 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000221 }
222
223 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000224 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
225 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000226 return true;
227}
228
Evan Chengf3d4efe2008-09-07 09:09:33 +0000229/// X86FastEmitStore - Emit a machine instruction to store a value Val of
230/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
231/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000232/// i.e. V. Return true if it is possible.
233bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000234X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000235 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000236 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 switch (VT.getSimpleVT().SimpleTy) {
238 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000239 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000240 case MVT::i1: {
241 // Mask out all but lowest bit.
242 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000244 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
245 Val = AndResult;
246 }
247 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 case MVT::i8: Opc = X86::MOV8mr; break;
249 case MVT::i16: Opc = X86::MOV16mr; break;
250 case MVT::i32: Opc = X86::MOV32mr; break;
251 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
252 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000253 Opc = X86ScalarSSEf32 ?
254 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000257 Opc = X86ScalarSSEf64 ?
258 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000259 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000261
Dan Gohman84023e02010-07-10 09:00:22 +0000262 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
263 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 return true;
265}
266
Dan Gohman46510a72010-04-15 01:51:59 +0000267bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000268 const X86AddressMode &AM) {
269 // Handle 'null' like i32/i64 0.
270 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000271 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000272
Chris Lattner438949a2008-10-15 05:30:52 +0000273 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000274 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000275 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000276 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000278 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000279 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 case MVT::i8: Opc = X86::MOV8mi; break;
281 case MVT::i16: Opc = X86::MOV16mi; break;
282 case MVT::i32: Opc = X86::MOV32mi; break;
283 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000284 // Must be a 32-bit sign extended value.
285 if ((int)CI->getSExtValue() == CI->getSExtValue())
286 Opc = X86::MOV64mi32;
287 break;
288 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000289
Chris Lattner438949a2008-10-15 05:30:52 +0000290 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000291 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
292 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000293 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000295 return true;
296 }
297 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000298
Chris Lattner438949a2008-10-15 05:30:52 +0000299 unsigned ValReg = getRegForValue(Val);
300 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000301 return false;
302
Chris Lattner438949a2008-10-15 05:30:52 +0000303 return X86FastEmitStore(VT, ValReg, AM);
304}
305
Evan Cheng24e3a902008-09-08 06:35:17 +0000306/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
307/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
308/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000309bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
310 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000311 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000312 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
313 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000314
Owen Andersonac34a002008-09-11 19:44:55 +0000315 if (RR != 0) {
316 ResultReg = RR;
317 return true;
318 } else
319 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000320}
321
Dan Gohman0586d912008-09-10 20:11:02 +0000322/// X86SelectAddress - Attempt to fill in an address from the given value.
323///
Dan Gohman46510a72010-04-15 01:51:59 +0000324bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
325 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000326 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000327 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000328 // Don't walk into other basic blocks; it's possible we haven't
329 // visited them yet, so the instructions may not yet be assigned
330 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000331 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
332 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
333 Opcode = I->getOpcode();
334 U = I;
335 }
Dan Gohman46510a72010-04-15 01:51:59 +0000336 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000337 Opcode = C->getOpcode();
338 U = C;
339 }
Dan Gohman0586d912008-09-10 20:11:02 +0000340
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000341 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000342 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000343 // Fast instruction selection doesn't support the special
344 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000345 return false;
346
Dan Gohman35893082008-09-18 23:23:44 +0000347 switch (Opcode) {
348 default: break;
349 case Instruction::BitCast:
350 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000351 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000352
353 case Instruction::IntToPtr:
354 // Look past no-op inttoptrs.
355 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000356 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000357 break;
Dan Gohman35893082008-09-18 23:23:44 +0000358
359 case Instruction::PtrToInt:
360 // Look past no-op ptrtoints.
361 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000362 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000363 break;
Dan Gohman35893082008-09-18 23:23:44 +0000364
365 case Instruction::Alloca: {
366 // Do static allocas.
367 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000368 DenseMap<const AllocaInst*, int>::iterator SI =
369 FuncInfo.StaticAllocaMap.find(A);
370 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000371 AM.BaseType = X86AddressMode::FrameIndexBase;
372 AM.Base.FrameIndex = SI->second;
373 return true;
374 }
375 break;
Dan Gohman35893082008-09-18 23:23:44 +0000376 }
377
378 case Instruction::Add: {
379 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000380 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000381 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
382 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000383 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000384 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000385 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000386 }
Dan Gohman0586d912008-09-10 20:11:02 +0000387 }
Dan Gohman35893082008-09-18 23:23:44 +0000388 break;
389 }
390
391 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000392 X86AddressMode SavedAM = AM;
393
Dan Gohman35893082008-09-18 23:23:44 +0000394 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000395 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000396 unsigned IndexReg = AM.IndexReg;
397 unsigned Scale = AM.Scale;
398 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000399 // Iterate through the indices, folding what we can. Constants can be
400 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000401 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000402 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000403 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000404 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000405 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000406 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
407 continue;
408 }
Eric Christopher471e4222011-06-08 23:55:35 +0000409
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000410 // A array/variable index is always of the form i*S where S is the
411 // constant scale size. See if we can push the scale into immediates.
412 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
413 for (;;) {
414 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
415 // Constant-offset addressing.
416 Disp += CI->getSExtValue() * S;
417 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000418 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000419 if (isa<AddOperator>(Op) &&
420 (!isa<Instruction>(Op) ||
421 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
422 == FuncInfo.MBB) &&
423 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
424 // An add (in the same block) with a constant operand. Fold the
425 // constant.
426 ConstantInt *CI =
427 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
428 Disp += CI->getSExtValue() * S;
429 // Iterate on the other operand.
430 Op = cast<AddOperator>(Op)->getOperand(0);
431 continue;
432 }
433 if (IndexReg == 0 &&
434 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
435 (S == 1 || S == 2 || S == 4 || S == 8)) {
436 // Scaled-index addressing.
437 Scale = S;
438 IndexReg = getRegForGEPIndex(Op).first;
439 if (IndexReg == 0)
440 return false;
441 break;
442 }
443 // Unsupported.
444 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000445 }
446 }
Dan Gohman09aae462008-09-26 20:04:15 +0000447 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000448 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000449 break;
Dan Gohman35893082008-09-18 23:23:44 +0000450 // Ok, the GEP indices were covered by constant-offset and scaled-index
451 // addressing. Update the address state and move on to examining the base.
452 AM.IndexReg = IndexReg;
453 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000454 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000455 if (X86SelectAddress(U->getOperand(0), AM))
456 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000457
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000458 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000459 // our address and just match the value instead of completely failing.
460 AM = SavedAM;
461 break;
Dan Gohman35893082008-09-18 23:23:44 +0000462 unsupported_gep:
463 // Ok, the GEP indices weren't all covered.
464 break;
465 }
466 }
467
468 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000469 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000470 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000471 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000472 return false;
473
Dan Gohman46510a72010-04-15 01:51:59 +0000474 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000475 if (GVar->isThreadLocal())
476 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000477
Chris Lattner0a1c9972011-04-17 17:47:38 +0000478 // RIP-relative addresses can't have additional register operands, so if
479 // we've already folded stuff into the addressing mode, just force the
480 // global value into its own register, which we can use as the basereg.
481 if (!Subtarget->isPICStyleRIPRel() ||
482 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
483 // Okay, we've committed to selecting this global. Set up the address.
484 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000485
Chris Lattner0a1c9972011-04-17 17:47:38 +0000486 // Allow the subtarget to classify the global.
487 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000488
Chris Lattner0a1c9972011-04-17 17:47:38 +0000489 // If this reference is relative to the pic base, set it now.
490 if (isGlobalRelativeToPICBase(GVFlags)) {
491 // FIXME: How do we know Base.Reg is free??
492 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000493 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000494
495 // Unless the ABI requires an extra load, return a direct reference to
496 // the global.
497 if (!isGlobalStubReference(GVFlags)) {
498 if (Subtarget->isPICStyleRIPRel()) {
499 // Use rip-relative addressing if we can. Above we verified that the
500 // base and index registers are unused.
501 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
502 AM.Base.Reg = X86::RIP;
503 }
504 AM.GVOpFlags = GVFlags;
505 return true;
506 }
507
508 // Ok, we need to do a load from a stub. If we've already loaded from
509 // this stub, reuse the loaded pointer, otherwise emit the load now.
510 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
511 unsigned LoadReg;
512 if (I != LocalValueMap.end() && I->second != 0) {
513 LoadReg = I->second;
514 } else {
515 // Issue load from stub.
516 unsigned Opc = 0;
517 const TargetRegisterClass *RC = NULL;
518 X86AddressMode StubAM;
519 StubAM.Base.Reg = AM.Base.Reg;
520 StubAM.GV = GV;
521 StubAM.GVOpFlags = GVFlags;
522
523 // Prepare for inserting code in the local-value area.
524 SavePoint SaveInsertPt = enterLocalValueArea();
525
526 if (TLI.getPointerTy() == MVT::i64) {
527 Opc = X86::MOV64rm;
528 RC = X86::GR64RegisterClass;
529
530 if (Subtarget->isPICStyleRIPRel())
531 StubAM.Base.Reg = X86::RIP;
532 } else {
533 Opc = X86::MOV32rm;
534 RC = X86::GR32RegisterClass;
535 }
536
537 LoadReg = createResultReg(RC);
538 MachineInstrBuilder LoadMI =
539 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
540 addFullAddress(LoadMI, StubAM);
541
542 // Ok, back to normal mode.
543 leaveLocalValueArea(SaveInsertPt);
544
545 // Prevent loading GV stub multiple times in same MBB.
546 LocalValueMap[V] = LoadReg;
547 }
548
549 // Now construct the final address. Note that the Disp, Scale,
550 // and Index values may already be set here.
551 AM.Base.Reg = LoadReg;
552 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000553 return true;
554 }
Dan Gohman0586d912008-09-10 20:11:02 +0000555 }
556
Dan Gohman97135e12008-09-26 19:15:30 +0000557 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000558 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000559 if (AM.Base.Reg == 0) {
560 AM.Base.Reg = getRegForValue(V);
561 return AM.Base.Reg != 0;
562 }
563 if (AM.IndexReg == 0) {
564 assert(AM.Scale == 1 && "Scale with no index!");
565 AM.IndexReg = getRegForValue(V);
566 return AM.IndexReg != 0;
567 }
568 }
569
570 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000571}
572
Chris Lattner0aa43de2009-07-10 05:33:42 +0000573/// X86SelectCallAddress - Attempt to fill in an address from the given value.
574///
Dan Gohman46510a72010-04-15 01:51:59 +0000575bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
576 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000577 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000578 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000579 Opcode = I->getOpcode();
580 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000581 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000582 Opcode = C->getOpcode();
583 U = C;
584 }
585
586 switch (Opcode) {
587 default: break;
588 case Instruction::BitCast:
589 // Look past bitcasts.
590 return X86SelectCallAddress(U->getOperand(0), AM);
591
592 case Instruction::IntToPtr:
593 // Look past no-op inttoptrs.
594 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
595 return X86SelectCallAddress(U->getOperand(0), AM);
596 break;
597
598 case Instruction::PtrToInt:
599 // Look past no-op ptrtoints.
600 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
601 return X86SelectCallAddress(U->getOperand(0), AM);
602 break;
603 }
604
605 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000606 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000607 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000608 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000609 return false;
610
611 // RIP-relative addresses can't have additional register operands.
612 if (Subtarget->isPICStyleRIPRel() &&
613 (AM.Base.Reg != 0 || AM.IndexReg != 0))
614 return false;
615
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000616 // Can't handle DLLImport.
617 if (GV->hasDLLImportLinkage())
618 return false;
619
620 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000621 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000622 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000623 return false;
624
625 // Okay, we've committed to selecting this global. Set up the basic address.
626 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000627
Chris Lattnere6c07b52009-07-10 05:45:15 +0000628 // No ABI requires an extra load for anything other than DLLImport, which
629 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000630 if (Subtarget->isPICStyleRIPRel()) {
631 // Use rip-relative addressing if we can. Above we verified that the
632 // base and index registers are unused.
633 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
634 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000635 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000636 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
637 } else if (Subtarget->isPICStyleGOT()) {
638 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000639 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000640
Chris Lattner0aa43de2009-07-10 05:33:42 +0000641 return true;
642 }
643
644 // If all else fails, try to materialize the value in a register.
645 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
646 if (AM.Base.Reg == 0) {
647 AM.Base.Reg = getRegForValue(V);
648 return AM.Base.Reg != 0;
649 }
650 if (AM.IndexReg == 0) {
651 assert(AM.Scale == 1 && "Scale with no index!");
652 AM.IndexReg = getRegForValue(V);
653 return AM.IndexReg != 0;
654 }
655 }
656
657 return false;
658}
659
660
Owen Andersona3971df2008-09-04 07:08:58 +0000661/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000662bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000663 // Atomic stores need special handling.
664 if (cast<StoreInst>(I)->isAtomic())
665 return false;
666
Duncan Sands1440e8b2010-11-03 11:35:31 +0000667 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000668 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000669 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000670
Dan Gohman0586d912008-09-10 20:11:02 +0000671 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000672 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000673 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000674
Chris Lattner438949a2008-10-15 05:30:52 +0000675 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000676}
677
Dan Gohman84023e02010-07-10 09:00:22 +0000678/// X86SelectRet - Select and emit code to implement ret instructions.
679bool X86FastISel::X86SelectRet(const Instruction *I) {
680 const ReturnInst *Ret = cast<ReturnInst>(I);
681 const Function &F = *I->getParent()->getParent();
682
683 if (!FuncInfo.CanLowerReturn)
684 return false;
685
686 CallingConv::ID CC = F.getCallingConv();
687 if (CC != CallingConv::C &&
688 CC != CallingConv::Fast &&
689 CC != CallingConv::X86_FastCall)
690 return false;
691
692 if (Subtarget->isTargetWin64())
693 return false;
694
695 // Don't handle popping bytes on return for now.
696 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
697 ->getBytesToPopOnReturn() != 0)
698 return 0;
699
700 // fastcc with -tailcallopt is intended to provide a guaranteed
701 // tail call optimization. Fastisel doesn't know how to do that.
702 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
703 return false;
704
705 // Let SDISel handle vararg functions.
706 if (F.isVarArg())
707 return false;
708
709 if (Ret->getNumOperands() > 0) {
710 SmallVector<ISD::OutputArg, 4> Outs;
711 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
712 Outs, TLI);
713
714 // Analyze operands of the call, assigning locations to each operand.
715 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000716 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
717 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000718 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000719
720 const Value *RV = Ret->getOperand(0);
721 unsigned Reg = getRegForValue(RV);
722 if (Reg == 0)
723 return false;
724
725 // Only handle a single return value for now.
726 if (ValLocs.size() != 1)
727 return false;
728
729 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730
Dan Gohman84023e02010-07-10 09:00:22 +0000731 // Don't bother handling odd stuff for now.
732 if (VA.getLocInfo() != CCValAssign::Full)
733 return false;
734 // Only handle register returns for now.
735 if (!VA.isRegLoc())
736 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000737
738 // The calling-convention tables for x87 returns don't tell
739 // the whole story.
740 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
741 return false;
742
Eli Friedman22486c92011-05-18 23:13:10 +0000743 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000744 EVT SrcVT = TLI.getValueType(RV->getType());
745 EVT DstVT = VA.getValVT();
746 // Special handling for extended integers.
747 if (SrcVT != DstVT) {
748 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
749 return false;
750
751 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
752 return false;
753
754 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
755
756 if (SrcVT == MVT::i1) {
757 if (Outs[0].Flags.isSExt())
758 return false;
759 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
760 SrcVT = MVT::i8;
761 }
762 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
763 ISD::SIGN_EXTEND;
764 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
765 SrcReg, /*TODO: Kill=*/false);
766 }
767
768 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000769 unsigned DstReg = VA.getLocReg();
770 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000771 // Avoid a cross-class copy. This is very unlikely.
772 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000773 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000774 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
775 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000776
777 // Mark the register as live out of the function.
778 MRI.addLiveOut(VA.getLocReg());
779 }
780
781 // Now emit the RET.
782 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
783 return true;
784}
785
Evan Cheng8b19e562008-09-03 06:44:39 +0000786/// X86SelectLoad - Select and emit code to implement load instructions.
787///
Dan Gohman46510a72010-04-15 01:51:59 +0000788bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000789 // Atomic loads need special handling.
790 if (cast<LoadInst>(I)->isAtomic())
791 return false;
792
Duncan Sands1440e8b2010-11-03 11:35:31 +0000793 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000794 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000795 return false;
796
Dan Gohman0586d912008-09-10 20:11:02 +0000797 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000798 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000799 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000800
Evan Cheng0de588f2008-09-05 21:00:03 +0000801 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000802 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000803 UpdateValueMap(I, ResultReg);
804 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000805 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000806 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000807}
808
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000809static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000810 bool HasAVX = Subtarget->hasAVX();
811 bool X86ScalarSSEf32 = HasAVX || Subtarget->hasSSE1();
812 bool X86ScalarSSEf64 = HasAVX || Subtarget->hasSSE2();
813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000815 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 case MVT::i8: return X86::CMP8rr;
817 case MVT::i16: return X86::CMP16rr;
818 case MVT::i32: return X86::CMP32rr;
819 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000820 case MVT::f32:
821 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
822 case MVT::f64:
823 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000824 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000825}
826
Chris Lattner0e13c782008-10-15 04:13:29 +0000827/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
828/// of the comparison, return an opcode that works for the compare (e.g.
829/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000830static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000832 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000833 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 case MVT::i8: return X86::CMP8ri;
835 case MVT::i16: return X86::CMP16ri;
836 case MVT::i32: return X86::CMP32ri;
837 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000838 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
839 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000840 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000841 return X86::CMP64ri32;
842 return 0;
843 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000844}
845
Dan Gohman46510a72010-04-15 01:51:59 +0000846bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
847 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000848 unsigned Op0Reg = getRegForValue(Op0);
849 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000850
Chris Lattnerd53886b2008-10-15 05:18:04 +0000851 // Handle 'null' like i32/i64 0.
852 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000853 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854
Chris Lattner9a08a612008-10-15 04:26:38 +0000855 // We have two options: compare with register or immediate. If the RHS of
856 // the compare is an immediate that we can fold into this compare, use
857 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000858 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000859 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
861 .addReg(Op0Reg)
862 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000863 return true;
864 }
865 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000866
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000867 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000868 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000869
Chris Lattner9a08a612008-10-15 04:26:38 +0000870 unsigned Op1Reg = getRegForValue(Op1);
871 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
873 .addReg(Op0Reg)
874 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000875
Chris Lattner9a08a612008-10-15 04:26:38 +0000876 return true;
877}
878
Dan Gohman46510a72010-04-15 01:51:59 +0000879bool X86FastISel::X86SelectCmp(const Instruction *I) {
880 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000881
Duncan Sands1440e8b2010-11-03 11:35:31 +0000882 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000883 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000884 return false;
885
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000886 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000887 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000888 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000889 switch (CI->getPredicate()) {
890 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000891 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
892 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000893
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000894 unsigned EReg = createResultReg(&X86::GR8RegClass);
895 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000896 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
897 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
898 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000900 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000901 UpdateValueMap(I, ResultReg);
902 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000903 }
904 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000905 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
906 return false;
907
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000908 unsigned NEReg = createResultReg(&X86::GR8RegClass);
909 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
911 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000913 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000914 UpdateValueMap(I, ResultReg);
915 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000916 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000917 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
918 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
919 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
920 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
921 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
922 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
923 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
924 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
925 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
926 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
927 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
928 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000929
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000930 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
931 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
932 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
933 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
934 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
935 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
936 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
937 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
938 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
939 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000940 default:
941 return false;
942 }
943
Dan Gohman46510a72010-04-15 01:51:59 +0000944 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000945 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000946 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000947
Chris Lattner9a08a612008-10-15 04:26:38 +0000948 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000949 if (!X86FastEmitCompare(Op0, Op1, VT))
950 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000951
Dan Gohman84023e02010-07-10 09:00:22 +0000952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000953 UpdateValueMap(I, ResultReg);
954 return true;
955}
Evan Cheng8b19e562008-09-03 06:44:39 +0000956
Dan Gohman46510a72010-04-15 01:51:59 +0000957bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000958 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000959 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000960 return false;
961
962 EVT DstVT = TLI.getValueType(I->getType());
963 if (!TLI.isTypeLegal(DstVT))
964 return false;
965
966 unsigned ResultReg = getRegForValue(I->getOperand(0));
967 if (ResultReg == 0)
968 return false;
969
970 // Set the high bits to zero.
971 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
972 if (ResultReg == 0)
973 return false;
974
975 if (DstVT != MVT::i8) {
976 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
977 ResultReg, /*Kill=*/true);
978 if (ResultReg == 0)
979 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000980 }
981
Eli Friedman76927d732011-05-25 23:49:02 +0000982 UpdateValueMap(I, ResultReg);
983 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +0000984}
985
Chris Lattner9a08a612008-10-15 04:26:38 +0000986
Dan Gohman46510a72010-04-15 01:51:59 +0000987bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000988 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000989 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000990 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000991 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
992 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000993
Dan Gohman8bef7442010-08-21 02:32:36 +0000994 // Fold the common case of a conditional branch with a comparison
995 // in the same block (values defined on other blocks may not have
996 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000997 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000998 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000999 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001000
Dan Gohmand98d6202008-10-02 22:15:21 +00001001 // Try to take advantage of fallthrough opportunities.
1002 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001003 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001004 std::swap(TrueMBB, FalseMBB);
1005 Predicate = CmpInst::getInversePredicate(Predicate);
1006 }
1007
Chris Lattner871d2462008-10-15 03:58:05 +00001008 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1009 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1010
Dan Gohmand98d6202008-10-02 22:15:21 +00001011 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001012 case CmpInst::FCMP_OEQ:
1013 std::swap(TrueMBB, FalseMBB);
1014 Predicate = CmpInst::FCMP_UNE;
1015 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001016 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1017 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1018 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1019 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1020 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1021 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1022 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1023 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1024 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1025 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1026 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1027 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1028 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001029
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001030 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1031 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1032 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1033 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1034 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1035 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1036 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1037 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1038 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1039 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001040 default:
1041 return false;
1042 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001043
Dan Gohman46510a72010-04-15 01:51:59 +00001044 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001045 if (SwapArgs)
1046 std::swap(Op0, Op1);
1047
Chris Lattner9a08a612008-10-15 04:26:38 +00001048 // Emit a compare of the LHS and RHS, setting the flags.
1049 if (!X86FastEmitCompare(Op0, Op1, VT))
1050 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001051
Dan Gohman84023e02010-07-10 09:00:22 +00001052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1053 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001054
1055 if (Predicate == CmpInst::FCMP_UNE) {
1056 // X86 requires a second branch to handle UNE (and OEQ,
1057 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1059 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001060 }
1061
Stuart Hastings3bf91252010-06-17 22:43:56 +00001062 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001063 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001064 return true;
1065 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001066 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1067 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1068 // typically happen for _Bool and C++ bools.
1069 MVT SourceVT;
1070 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1071 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1072 unsigned TestOpc = 0;
1073 switch (SourceVT.SimpleTy) {
1074 default: break;
1075 case MVT::i8: TestOpc = X86::TEST8ri; break;
1076 case MVT::i16: TestOpc = X86::TEST16ri; break;
1077 case MVT::i32: TestOpc = X86::TEST32ri; break;
1078 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1079 }
1080 if (TestOpc) {
1081 unsigned OpReg = getRegForValue(TI->getOperand(0));
1082 if (OpReg == 0) return false;
1083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1084 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001085
Chris Lattnerc76d1212011-04-19 04:26:32 +00001086 unsigned JmpOpc = X86::JNE_4;
1087 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1088 std::swap(TrueMBB, FalseMBB);
1089 JmpOpc = X86::JE_4;
1090 }
Eric Christopher471e4222011-06-08 23:55:35 +00001091
Chris Lattnerc76d1212011-04-19 04:26:32 +00001092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001093 .addMBB(TrueMBB);
1094 FastEmitBranch(FalseMBB, DL);
1095 FuncInfo.MBB->addSuccessor(TrueMBB);
1096 return true;
1097 }
1098 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001099 }
1100
1101 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001102 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1103 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001104 unsigned OpReg = getRegForValue(BI->getCondition());
1105 if (OpReg == 0) return false;
1106
Eli Friedman547eb4f2011-04-27 01:34:27 +00001107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1108 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001109 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1110 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001111 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001112 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001113 return true;
1114}
1115
Dan Gohman46510a72010-04-15 01:51:59 +00001116bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001117 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001119 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001120 CReg = X86::CL;
1121 RC = &X86::GR8RegClass;
1122 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001123 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1124 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1125 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001126 default: return false;
1127 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001128 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001129 CReg = X86::CX;
1130 RC = &X86::GR16RegClass;
1131 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001132 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1133 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1134 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001135 default: return false;
1136 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001137 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001138 CReg = X86::ECX;
1139 RC = &X86::GR32RegClass;
1140 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001141 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1142 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1143 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001144 default: return false;
1145 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001146 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 CReg = X86::RCX;
1148 RC = &X86::GR64RegClass;
1149 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001150 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1151 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1152 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001153 default: return false;
1154 }
1155 } else {
1156 return false;
1157 }
1158
Duncan Sands1440e8b2010-11-03 11:35:31 +00001159 MVT VT;
1160 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001161 return false;
1162
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001163 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1164 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001165
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001166 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1167 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1169 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001170
1171 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001172 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001173 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1175 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001176 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001177
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1180 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001181 UpdateValueMap(I, ResultReg);
1182 return true;
1183}
1184
Dan Gohman46510a72010-04-15 01:51:59 +00001185bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001186 MVT VT;
1187 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001188 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189
Eric Christophere487b012010-09-29 23:00:29 +00001190 // We only use cmov here, if we don't have a cmov instruction bail.
1191 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001192
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001193 unsigned Opc = 0;
1194 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001195 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001196 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001198 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001199 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001201 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001202 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001203 RC = &X86::GR64RegClass;
1204 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001205 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001206 }
1207
1208 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1209 if (Op0Reg == 0) return false;
1210 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1211 if (Op1Reg == 0) return false;
1212 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1213 if (Op2Reg == 0) return false;
1214
Dan Gohman84023e02010-07-10 09:00:22 +00001215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1216 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001217 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1219 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001220 UpdateValueMap(I, ResultReg);
1221 return true;
1222}
1223
Dan Gohman46510a72010-04-15 01:51:59 +00001224bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001225 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001226 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001227 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001228 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001229 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001230 unsigned OpReg = getRegForValue(V);
1231 if (OpReg == 0) return false;
1232 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1234 TII.get(X86::CVTSS2SDrr), ResultReg)
1235 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001236 UpdateValueMap(I, ResultReg);
1237 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001238 }
1239 }
1240
1241 return false;
1242}
1243
Dan Gohman46510a72010-04-15 01:51:59 +00001244bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001245 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001246 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001247 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001248 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001249 unsigned OpReg = getRegForValue(V);
1250 if (OpReg == 0) return false;
1251 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1253 TII.get(X86::CVTSD2SSrr), ResultReg)
1254 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001255 UpdateValueMap(I, ResultReg);
1256 return true;
1257 }
1258 }
1259 }
1260
1261 return false;
1262}
1263
Dan Gohman46510a72010-04-15 01:51:59 +00001264bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001265 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1266 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001267
Eli Friedman76927d732011-05-25 23:49:02 +00001268 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001270 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001271 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001272 return false;
1273
1274 unsigned InputReg = getRegForValue(I->getOperand(0));
1275 if (!InputReg)
1276 // Unhandled operand. Halt "fast" selection and bail.
1277 return false;
1278
Eli Friedman76927d732011-05-25 23:49:02 +00001279 if (SrcVT == MVT::i8) {
1280 // Truncate from i8 to i1; no code needed.
1281 UpdateValueMap(I, InputReg);
1282 return true;
1283 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001284
Eli Friedman76927d732011-05-25 23:49:02 +00001285 if (!Subtarget->is64Bit()) {
1286 // If we're on x86-32; we can't extract an i8 from a general register.
1287 // First issue a copy to GR16_ABCD or GR32_ABCD.
1288 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1289 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
1290 unsigned CopyReg = createResultReg(CopyRC);
1291 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1292 CopyReg).addReg(InputReg);
1293 InputReg = CopyReg;
1294 }
1295
1296 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001298 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001299 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001300 if (!ResultReg)
1301 return false;
1302
1303 UpdateValueMap(I, ResultReg);
1304 return true;
1305}
1306
Eli Friedmanc0883452011-05-20 22:21:04 +00001307bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1308 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1309}
1310
Eli Friedmand5089a92011-04-27 01:45:07 +00001311bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1312 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001313
Eli Friedmand5089a92011-04-27 01:45:07 +00001314 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001315 if (!IsMemcpySmall(Len))
1316 return false;
1317
1318 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001319
1320 // We don't care about alignment here since we just emit integer accesses.
1321 while (Len) {
1322 MVT VT;
1323 if (Len >= 8 && i64Legal)
1324 VT = MVT::i64;
1325 else if (Len >= 4)
1326 VT = MVT::i32;
1327 else if (Len >= 2)
1328 VT = MVT::i16;
1329 else {
1330 assert(Len == 1);
1331 VT = MVT::i8;
1332 }
1333
1334 unsigned Reg;
1335 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1336 RV &= X86FastEmitStore(VT, Reg, DestAM);
1337 assert(RV && "Failed to emit load or store??");
1338
1339 unsigned Size = VT.getSizeInBits()/8;
1340 Len -= Size;
1341 DestAM.Disp += Size;
1342 SrcAM.Disp += Size;
1343 }
1344
1345 return true;
1346}
1347
Dan Gohman46510a72010-04-15 01:51:59 +00001348bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001349 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001350 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001351 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001352 case Intrinsic::memcpy: {
1353 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1354 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001355 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001356 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001357
Eli Friedman25255cb2011-06-10 23:39:36 +00001358 if (isa<ConstantInt>(MCI.getLength())) {
1359 // Small memcpy's are common enough that we want to do them
1360 // without a call if possible.
1361 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1362 if (IsMemcpySmall(Len)) {
1363 X86AddressMode DestAM, SrcAM;
1364 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1365 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1366 return false;
1367 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1368 return true;
1369 }
1370 }
Eric Christopher471e4222011-06-08 23:55:35 +00001371
Eli Friedman25255cb2011-06-10 23:39:36 +00001372 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1373 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001374 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001375
Eli Friedman25255cb2011-06-10 23:39:36 +00001376 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1377 return false;
1378
1379 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001380 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001381 case Intrinsic::memset: {
1382 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001383
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001384 if (MSI.isVolatile())
1385 return false;
1386
Eli Friedman25255cb2011-06-10 23:39:36 +00001387 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1388 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1389 return false;
1390
1391 if (MSI.getDestAddressSpace() > 255)
1392 return false;
1393
1394 return DoSelectCall(&I, "memset");
1395 }
Eric Christopher07754c22010-03-18 20:27:26 +00001396 case Intrinsic::stackprotector: {
1397 // Emit code inline code to store the stack guard onto the stack.
1398 EVT PtrTy = TLI.getPointerTy();
1399
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001400 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1401 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001402
1403 // Grab the frame index.
1404 X86AddressMode AM;
1405 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001406 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001407 return true;
1408 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001409 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001410 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001411 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001412 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001413 if (!X86SelectAddress(DI->getAddress(), AM))
1414 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001415 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001416 // FIXME may need to add RegState::Debug to any registers produced,
1417 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001418 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1419 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001420 return true;
1421 }
Eric Christopher77f79892010-01-18 22:11:29 +00001422 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001424 return true;
1425 }
Bill Wendling52370a12008-12-09 02:42:50 +00001426 case Intrinsic::sadd_with_overflow:
1427 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001428 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001429
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001430 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001431 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001432 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001433 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001434 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1435
Duncan Sands1440e8b2010-11-03 11:35:31 +00001436 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001437 if (!isTypeLegal(RetTy, VT))
1438 return false;
1439
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001440 const Value *Op1 = I.getArgOperand(0);
1441 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001442 unsigned Reg1 = getRegForValue(Op1);
1443 unsigned Reg2 = getRegForValue(Op2);
1444
1445 if (Reg1 == 0 || Reg2 == 0)
1446 // FIXME: Handle values *not* in registers.
1447 return false;
1448
1449 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001451 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001453 OpC = X86::ADD64rr;
1454 else
1455 return false;
1456
Eli Friedman482feb32011-05-16 21:06:17 +00001457 // The call to CreateRegs builds two sequential registers, to store the
1458 // both the the returned values.
1459 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001460 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1461 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnera9a42252009-04-12 07:36:01 +00001463 unsigned Opc = X86::SETBr;
1464 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1465 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001466 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1467
1468 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001469 return true;
1470 }
1471 }
1472}
1473
Dan Gohman46510a72010-04-15 01:51:59 +00001474bool X86FastISel::X86SelectCall(const Instruction *I) {
1475 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001476 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001477
1478 // Can't handle inline asm yet.
1479 if (isa<InlineAsm>(Callee))
1480 return false;
1481
Bill Wendling52370a12008-12-09 02:42:50 +00001482 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001483 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001484 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001485
Eli Friedman25255cb2011-06-10 23:39:36 +00001486 return DoSelectCall(I, 0);
1487}
1488
1489// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1490bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1491 const CallInst *CI = cast<CallInst>(I);
1492 const Value *Callee = CI->getCalledValue();
1493
Evan Chengf3d4efe2008-09-07 09:09:33 +00001494 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001495 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001496 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001497 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001498 CC != CallingConv::X86_FastCall)
1499 return false;
1500
Evan Cheng381993f2010-01-27 00:00:57 +00001501 // fastcc with -tailcallopt is intended to provide a guaranteed
1502 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001503 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001504 return false;
1505
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001506 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1507 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001508 bool isVarArg = FTy->isVarArg();
1509
1510 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1511 // x86-32. Special handling for x86-64 is implemented.
1512 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001513 return false;
1514
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001515 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001516 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
1517 GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001518 return false;
1519
Eli Friedman19515b42011-05-17 18:29:03 +00001520 // Check whether the function can return without sret-demotion.
1521 SmallVector<ISD::OutputArg, 4> Outs;
1522 SmallVector<uint64_t, 4> Offsets;
1523 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1524 Outs, TLI, &Offsets);
1525 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00001526 *FuncInfo.MF, FTy->isVarArg(),
1527 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001528 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001529 return false;
1530
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001531 // Materialize callee address in a register. FIXME: GV address can be
1532 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001533 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001534 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001535 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001536 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001537 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001538 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001539 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001540 } else if (CalleeAM.Base.Reg != 0) {
1541 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001542 } else
1543 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001544
Evan Chengf3d4efe2008-09-07 09:09:33 +00001545 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001546 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001547 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001548 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001549 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001550 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001551 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001552 ArgVTs.reserve(CS.arg_size());
1553 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001554 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001555 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001556 // If we're lowering a mem intrinsic instead of a regular call, skip the
1557 // last two arguments, which should not passed to the underlying functions.
1558 if (MemIntName && e-i <= 2)
1559 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001560 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001561 ISD::ArgFlagsTy Flags;
1562 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001563 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001564 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001565 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001566 Flags.setZExt();
1567
Eli Friedmanc0883452011-05-20 22:21:04 +00001568 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001569 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1570 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001571 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1572 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1573 if (!FrameAlign)
1574 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1575 Flags.setByVal();
1576 Flags.setByValSize(FrameSize);
1577 Flags.setByValAlign(FrameAlign);
1578 if (!IsMemcpySmall(FrameSize))
1579 return false;
1580 }
1581
1582 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1583 Flags.setInReg();
1584 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1585 Flags.setNest();
1586
Chris Lattnere03b8d32011-04-19 04:42:38 +00001587 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1588 // instruction. This is safe because it is common to all fastisel supported
1589 // calling conventions on x86.
1590 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1591 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1592 CI->getBitWidth() == 16) {
1593 if (Flags.isSExt())
1594 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1595 else
1596 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1597 }
1598 }
Eric Christopher471e4222011-06-08 23:55:35 +00001599
Chris Lattnerb44101c2011-04-19 05:09:50 +00001600 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001601
Chris Lattnerff009ad2011-04-19 05:15:59 +00001602 // Passing bools around ends up doing a trunc to i1 and passing it.
1603 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001604 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1605 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1606 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001607 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1608 ArgReg = getRegForValue(ArgVal);
1609 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001610
Chris Lattnerb44101c2011-04-19 05:09:50 +00001611 MVT ArgVT;
1612 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001613
Chris Lattnerb44101c2011-04-19 05:09:50 +00001614 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1615 ArgVal->hasOneUse(), 1);
1616 } else {
1617 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001618 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001619
Chris Lattnerff009ad2011-04-19 05:15:59 +00001620 if (ArgReg == 0) return false;
1621
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001622 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001623 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001624 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001625 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001626 if (ArgVT == MVT::x86mmx)
1627 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001628 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1629 Flags.setOrigAlign(OriginalAlignment);
1630
Chris Lattnerb44101c2011-04-19 05:09:50 +00001631 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001632 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001633 ArgVTs.push_back(ArgVT);
1634 ArgFlags.push_back(Flags);
1635 }
1636
1637 // Analyze operands of the call, assigning locations to each operand.
1638 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001639 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1640 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641
Dan Gohmand8acddd2010-06-01 21:09:47 +00001642 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001643 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001645
Duncan Sands45907662010-10-31 13:21:44 +00001646 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001647
1648 // Get a count of how many bytes are to be pushed on the stack.
1649 unsigned NumBytes = CCInfo.getNextStackOffset();
1650
1651 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001652 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1654 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001655
Chris Lattner438949a2008-10-15 05:30:52 +00001656 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001657 // copies / loads.
1658 SmallVector<unsigned, 4> RegArgs;
1659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1660 CCValAssign &VA = ArgLocs[i];
1661 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001662 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001663
Evan Chengf3d4efe2008-09-07 09:09:33 +00001664 // Promote the value if needed.
1665 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001666 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001667 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001668 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001669 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1670 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001671 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1672 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001673 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001674 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001675 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001676 }
1677 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001678 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1679 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001680 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1681 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001682 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001683 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001684 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001685 }
1686 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001687 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1688 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001689 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1690 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001691 if (!Emitted)
1692 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001693 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001694 if (!Emitted)
1695 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1696 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001697
Chris Lattnerc46ec642011-01-05 22:26:52 +00001698 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001699 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001700 break;
1701 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001702 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001703 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001704 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001705 assert(BC != 0 && "Failed to emit a bitcast!");
1706 Arg = BC;
1707 ArgVT = VA.getLocVT();
1708 break;
1709 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001710 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001711
Evan Chengf3d4efe2008-09-07 09:09:33 +00001712 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1714 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001715 RegArgs.push_back(VA.getLocReg());
1716 } else {
1717 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001718 X86AddressMode AM;
1719 AM.Base.Reg = StackPtr;
1720 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001721 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001722 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001723
Eli Friedmanc0883452011-05-20 22:21:04 +00001724 if (Flags.isByVal()) {
1725 X86AddressMode SrcAM;
1726 SrcAM.Base.Reg = Arg;
1727 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1728 assert(Res && "memcpy length already checked!"); (void)Res;
1729 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1730 // If this is a really simple value, emit this with the Value* version
1731 //of X86FastEmitStore. If it isn't simple, we don't want to do this,
1732 // as it can cause us to reevaluate the argument.
Chris Lattner241ab472008-10-15 05:38:32 +00001733 X86FastEmitStore(ArgVT, ArgVal, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001734 } else {
Chris Lattner241ab472008-10-15 05:38:32 +00001735 X86FastEmitStore(ArgVT, Arg, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001736 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001737 }
1738 }
1739
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001740 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001741 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001742 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001743 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001744 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1745 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001746 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001747
Eli Friedman37620462011-04-19 17:22:22 +00001748 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1749 // Count the number of XMM registers allocated.
1750 static const unsigned XMMArgRegs[] = {
1751 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1752 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1753 };
1754 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1755 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1756 X86::AL).addImm(NumXMMRegs);
1757 }
1758
Evan Chengf3d4efe2008-09-07 09:09:33 +00001759 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001760 MachineInstrBuilder MIB;
1761 if (CalleeOp) {
1762 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001763 unsigned CallOpc;
1764 if (Subtarget->isTargetWin64())
1765 CallOpc = X86::WINCALL64r;
1766 else if (Subtarget->is64Bit())
1767 CallOpc = X86::CALL64r;
1768 else
1769 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001770 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1771 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772
Chris Lattner51e8eab2009-07-09 06:34:26 +00001773 } else {
1774 // Direct call.
1775 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001776 unsigned CallOpc;
1777 if (Subtarget->isTargetWin64())
1778 CallOpc = X86::WINCALL64pcrel32;
1779 else if (Subtarget->is64Bit())
1780 CallOpc = X86::CALL64pcrel32;
1781 else
1782 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001783
Chris Lattner51e8eab2009-07-09 06:34:26 +00001784 // See if we need any target-specific flags on the GV operand.
1785 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001786
Chris Lattner51e8eab2009-07-09 06:34:26 +00001787 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1788 // external symbols most go through the PLT in PIC mode. If the symbol
1789 // has hidden or protected visibility, or if it is static or local, then
1790 // we don't need to use the PLT - we can directly call it.
1791 if (Subtarget->isTargetELF() &&
1792 TM.getRelocationModel() == Reloc::PIC_ &&
1793 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1794 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001795 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001796 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001797 (!Subtarget->getTargetTriple().isMacOSX() ||
1798 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001799 // PC-relative references to external symbols should go through $stub,
1800 // unless we're building with the leopard linker or later, which
1801 // automatically synthesizes these stubs.
1802 OpFlags = X86II::MO_DARWIN_STUB;
1803 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001804
1805
Eli Friedman25255cb2011-06-10 23:39:36 +00001806 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1807 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001808 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001809 else
1810 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001811 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001812
1813 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001814 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001815 MIB.addReg(X86::EBX);
1816
Eli Friedman37620462011-04-19 17:22:22 +00001817 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1818 MIB.addReg(X86::AL);
1819
Evan Chengf3d4efe2008-09-07 09:09:33 +00001820 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001821 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1822 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001823
1824 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001825 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001826 unsigned NumBytesCallee = 0;
1827 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1828 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001829 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001830 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001831
Eli Friedman19515b42011-05-17 18:29:03 +00001832 // Build info for return calling conv lowering code.
1833 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1834 SmallVector<ISD::InputArg, 32> Ins;
1835 SmallVector<EVT, 4> RetTys;
1836 ComputeValueVTs(TLI, I->getType(), RetTys);
1837 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1838 EVT VT = RetTys[i];
1839 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1840 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1841 for (unsigned j = 0; j != NumRegs; ++j) {
1842 ISD::InputArg MyFlags;
1843 MyFlags.VT = RegisterVT.getSimpleVT();
1844 MyFlags.Used = !CS.getInstruction()->use_empty();
1845 if (CS.paramHasAttr(0, Attribute::SExt))
1846 MyFlags.Flags.setSExt();
1847 if (CS.paramHasAttr(0, Attribute::ZExt))
1848 MyFlags.Flags.setZExt();
1849 if (CS.paramHasAttr(0, Attribute::InReg))
1850 MyFlags.Flags.setInReg();
1851 Ins.push_back(MyFlags);
1852 }
1853 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001854
Eli Friedman19515b42011-05-17 18:29:03 +00001855 // Now handle call return values.
1856 SmallVector<unsigned, 4> UsedRegs;
1857 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001858 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1859 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001860 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1861 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1862 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1863 EVT CopyVT = RVLocs[i].getValVT();
1864 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001865
Evan Chengf3d4efe2008-09-07 09:09:33 +00001866 // If this is a call to a function that returns an fp value on the x87 fp
1867 // stack, but where we prefer to use the value in xmm registers, copy it
1868 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001869 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001870 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001871 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001872 CopyVT = MVT::f80;
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001873 CopyReg = createResultReg(X86::RFP80RegisterClass);
1874 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1876 CopyReg);
1877 } else {
1878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1879 CopyReg).addReg(RVLocs[i].getLocReg());
1880 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001881 }
1882
Eli Friedman19515b42011-05-17 18:29:03 +00001883 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001884 // Round the F80 the right size, which also moves to the appropriate xmm
1885 // register. This is accomplished by storing the F80 value in memory and
1886 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001887 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001889 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001890 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001891 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1892 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001893 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001895 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001896 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001897 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001898 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001899
Eli Friedman19515b42011-05-17 18:29:03 +00001900 if (RVLocs.size())
1901 UpdateValueMap(I, ResultReg, RVLocs.size());
1902
Dan Gohmandb497122010-06-18 23:28:01 +00001903 // Set all unused physreg defs as dead.
1904 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1905
Evan Chengf3d4efe2008-09-07 09:09:33 +00001906 return true;
1907}
1908
1909
Dan Gohman99b21822008-08-28 23:21:34 +00001910bool
Dan Gohman46510a72010-04-15 01:51:59 +00001911X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001912 switch (I->getOpcode()) {
1913 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001914 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001915 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001916 case Instruction::Store:
1917 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001918 case Instruction::Ret:
1919 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001920 case Instruction::ICmp:
1921 case Instruction::FCmp:
1922 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001923 case Instruction::ZExt:
1924 return X86SelectZExt(I);
1925 case Instruction::Br:
1926 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001927 case Instruction::Call:
1928 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001929 case Instruction::LShr:
1930 case Instruction::AShr:
1931 case Instruction::Shl:
1932 return X86SelectShift(I);
1933 case Instruction::Select:
1934 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001935 case Instruction::Trunc:
1936 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001937 case Instruction::FPExt:
1938 return X86SelectFPExt(I);
1939 case Instruction::FPTrunc:
1940 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001941 case Instruction::IntToPtr: // Deliberate fall-through.
1942 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001943 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1944 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001945 if (DstVT.bitsGT(SrcVT))
1946 return X86SelectZExt(I);
1947 if (DstVT.bitsLT(SrcVT))
1948 return X86SelectTrunc(I);
1949 unsigned Reg = getRegForValue(I->getOperand(0));
1950 if (Reg == 0) return false;
1951 UpdateValueMap(I, Reg);
1952 return true;
1953 }
Dan Gohman99b21822008-08-28 23:21:34 +00001954 }
1955
1956 return false;
1957}
1958
Dan Gohman46510a72010-04-15 01:51:59 +00001959unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001960 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001961 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001962 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001963
Owen Anderson95267a12008-09-05 00:06:23 +00001964 // Get opcode and regclass of the output for the given load instruction.
1965 unsigned Opc = 0;
1966 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001967 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001968 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001970 Opc = X86::MOV8rm;
1971 RC = X86::GR8RegisterClass;
1972 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001974 Opc = X86::MOV16rm;
1975 RC = X86::GR16RegisterClass;
1976 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001978 Opc = X86::MOV32rm;
1979 RC = X86::GR32RegisterClass;
1980 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001982 // Must be in x86-64 mode.
1983 Opc = X86::MOV64rm;
1984 RC = X86::GR64RegisterClass;
1985 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001987 if (X86ScalarSSEf32) {
1988 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Owen Anderson95267a12008-09-05 00:06:23 +00001989 RC = X86::FR32RegisterClass;
1990 } else {
1991 Opc = X86::LD_Fp32m;
1992 RC = X86::RFP32RegisterClass;
1993 }
1994 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001996 if (X86ScalarSSEf64) {
1997 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Owen Anderson95267a12008-09-05 00:06:23 +00001998 RC = X86::FR64RegisterClass;
1999 } else {
2000 Opc = X86::LD_Fp64m;
2001 RC = X86::RFP64RegisterClass;
2002 }
2003 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002005 // No f80 support yet.
2006 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00002007 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002008
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002009 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002010 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002011 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002012 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002013 // If the expression is just a basereg, then we're done, otherwise we need
2014 // to emit an LEA.
2015 if (AM.BaseType == X86AddressMode::RegBase &&
2016 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2017 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002018
Chris Lattner685090f2011-04-17 17:12:08 +00002019 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002020 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002021 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2022 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002023 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002024 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002025 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002027
Owen Anderson3b217c62008-09-06 01:11:01 +00002028 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002029 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002030 if (Align == 0) {
2031 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002032 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002033 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002034
Dan Gohman5396c992008-09-30 01:21:32 +00002035 // x86-32 PIC requires a PIC base register for constant pools.
2036 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002037 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002038 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002039 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002040 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002041 } else if (Subtarget->isPICStyleGOT()) {
2042 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002043 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002044 } else if (Subtarget->isPICStyleRIPRel() &&
2045 TM.getCodeModel() == CodeModel::Small) {
2046 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002047 }
Dan Gohman5396c992008-09-30 01:21:32 +00002048
2049 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002050 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002051 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002052 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2053 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002054 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002055
Owen Anderson95267a12008-09-05 00:06:23 +00002056 return ResultReg;
2057}
2058
Dan Gohman46510a72010-04-15 01:51:59 +00002059unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002060 // Fail on dynamic allocas. At this point, getRegForValue has already
2061 // checked its CSE maps, so if we're here trying to handle a dynamic
2062 // alloca, we're not going to succeed. X86SelectAddress has a
2063 // check for dynamic allocas, because it's called directly from
2064 // various places, but TargetMaterializeAlloca also needs a check
2065 // in order to avoid recursion between getRegForValue,
2066 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002067 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002068 return 0;
2069
Dan Gohman0586d912008-09-10 20:11:02 +00002070 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002071 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002072 return 0;
2073 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2074 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2075 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002076 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2077 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002078 return ResultReg;
2079}
2080
Eli Friedman2790ba82011-04-27 22:41:55 +00002081unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2082 MVT VT;
2083 if (!isTypeLegal(CF->getType(), VT))
2084 return false;
2085
2086 // Get opcode and regclass for the given zero.
2087 unsigned Opc = 0;
2088 const TargetRegisterClass *RC = NULL;
2089 switch (VT.SimpleTy) {
2090 default: return false;
2091 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002092 if (X86ScalarSSEf32) {
2093 Opc = Subtarget->hasAVX() ? X86::VFsFLD0SS : X86::FsFLD0SS;
Eli Friedman2790ba82011-04-27 22:41:55 +00002094 RC = X86::FR32RegisterClass;
2095 } else {
2096 Opc = X86::LD_Fp032;
2097 RC = X86::RFP32RegisterClass;
2098 }
2099 break;
2100 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002101 if (X86ScalarSSEf64) {
2102 Opc = Subtarget->hasAVX() ? X86::VFsFLD0SD : X86::FsFLD0SD;
Eli Friedman2790ba82011-04-27 22:41:55 +00002103 RC = X86::FR64RegisterClass;
2104 } else {
2105 Opc = X86::LD_Fp064;
2106 RC = X86::RFP64RegisterClass;
2107 }
2108 break;
2109 case MVT::f80:
2110 // No f80 support yet.
2111 return false;
2112 }
2113
2114 unsigned ResultReg = createResultReg(RC);
2115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2116 return ResultReg;
2117}
2118
2119
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002120/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2121/// vreg is being provided by the specified load instruction. If possible,
2122/// try to fold the load as an operand to the instruction, returning true if
2123/// possible.
2124bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2125 const LoadInst *LI) {
2126 X86AddressMode AM;
2127 if (!X86SelectAddress(LI->getOperand(0), AM))
2128 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002129
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002130 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002131
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002132 unsigned Size = TD.getTypeAllocSize(LI->getType());
2133 unsigned Alignment = LI->getAlignment();
2134
2135 SmallVector<MachineOperand, 8> AddrOps;
2136 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002137
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002138 MachineInstr *Result =
2139 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2140 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002141
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002142 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002143 MI->eraseFromParent();
2144 return true;
2145}
2146
2147
Evan Chengc3f44b02008-09-03 00:03:49 +00002148namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002149 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2150 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002151 }
Dan Gohman99b21822008-08-28 23:21:34 +00002152}