blob: 82f5d3a50deb2ac71ca13d79674e34af6baf9011 [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;
62 X86ScalarSSEf64 = Subtarget->hasSSE2();
63 X86ScalarSSEf32 = Subtarget->hasSSE1();
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
Duncan Sands1440e8b2010-11-03 11:35:31 +0000137 bool isTypeLegal(const 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
Duncan Sands1440e8b2010-11-03 11:35:31 +0000147bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
148 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:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 if (Subtarget->hasSSE1()) {
202 Opc = X86::MOVSSrm;
203 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:
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 if (Subtarget->hasSSE2()) {
211 Opc = X86::MOVSDrm;
212 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:
Chris Lattner438949a2008-10-15 05:30:52 +0000253 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000254 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000256 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000258 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259
Dan Gohman84023e02010-07-10 09:00:22 +0000260 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
261 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 return true;
263}
264
Dan Gohman46510a72010-04-15 01:51:59 +0000265bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000266 const X86AddressMode &AM) {
267 // Handle 'null' like i32/i64 0.
268 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000269 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000270
Chris Lattner438949a2008-10-15 05:30:52 +0000271 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000272 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000273 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000274 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000276 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000277 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 case MVT::i8: Opc = X86::MOV8mi; break;
279 case MVT::i16: Opc = X86::MOV16mi; break;
280 case MVT::i32: Opc = X86::MOV32mi; break;
281 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000282 // Must be a 32-bit sign extended value.
283 if ((int)CI->getSExtValue() == CI->getSExtValue())
284 Opc = X86::MOV64mi32;
285 break;
286 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000287
Chris Lattner438949a2008-10-15 05:30:52 +0000288 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000289 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
290 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000291 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000292 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000293 return true;
294 }
295 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000296
Chris Lattner438949a2008-10-15 05:30:52 +0000297 unsigned ValReg = getRegForValue(Val);
298 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000299 return false;
300
Chris Lattner438949a2008-10-15 05:30:52 +0000301 return X86FastEmitStore(VT, ValReg, AM);
302}
303
Evan Cheng24e3a902008-09-08 06:35:17 +0000304/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
305/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
306/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000307bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
308 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000309 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000310 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
311 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312
Owen Andersonac34a002008-09-11 19:44:55 +0000313 if (RR != 0) {
314 ResultReg = RR;
315 return true;
316 } else
317 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000318}
319
Dan Gohman0586d912008-09-10 20:11:02 +0000320/// X86SelectAddress - Attempt to fill in an address from the given value.
321///
Dan Gohman46510a72010-04-15 01:51:59 +0000322bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
323 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000324 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000325 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000326 // Don't walk into other basic blocks; it's possible we haven't
327 // visited them yet, so the instructions may not yet be assigned
328 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000329 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
330 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
331 Opcode = I->getOpcode();
332 U = I;
333 }
Dan Gohman46510a72010-04-15 01:51:59 +0000334 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000335 Opcode = C->getOpcode();
336 U = C;
337 }
Dan Gohman0586d912008-09-10 20:11:02 +0000338
Chris Lattner868ee942010-06-15 19:08:40 +0000339 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
340 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000341 // Fast instruction selection doesn't support the special
342 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000343 return false;
344
Dan Gohman35893082008-09-18 23:23:44 +0000345 switch (Opcode) {
346 default: break;
347 case Instruction::BitCast:
348 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000349 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000350
351 case Instruction::IntToPtr:
352 // Look past no-op inttoptrs.
353 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000354 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000355 break;
Dan Gohman35893082008-09-18 23:23:44 +0000356
357 case Instruction::PtrToInt:
358 // Look past no-op ptrtoints.
359 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000360 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000361 break;
Dan Gohman35893082008-09-18 23:23:44 +0000362
363 case Instruction::Alloca: {
364 // Do static allocas.
365 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000366 DenseMap<const AllocaInst*, int>::iterator SI =
367 FuncInfo.StaticAllocaMap.find(A);
368 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000369 AM.BaseType = X86AddressMode::FrameIndexBase;
370 AM.Base.FrameIndex = SI->second;
371 return true;
372 }
373 break;
Dan Gohman35893082008-09-18 23:23:44 +0000374 }
375
376 case Instruction::Add: {
377 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000378 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000379 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
380 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000381 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000382 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000383 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000384 }
Dan Gohman0586d912008-09-10 20:11:02 +0000385 }
Dan Gohman35893082008-09-18 23:23:44 +0000386 break;
387 }
388
389 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000390 X86AddressMode SavedAM = AM;
391
Dan Gohman35893082008-09-18 23:23:44 +0000392 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000393 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000394 unsigned IndexReg = AM.IndexReg;
395 unsigned Scale = AM.Scale;
396 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000397 // Iterate through the indices, folding what we can. Constants can be
398 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000399 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000400 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000401 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000402 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
403 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000404 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
405 continue;
406 }
Eric Christopher471e4222011-06-08 23:55:35 +0000407
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000408 // A array/variable index is always of the form i*S where S is the
409 // constant scale size. See if we can push the scale into immediates.
410 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
411 for (;;) {
412 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
413 // Constant-offset addressing.
414 Disp += CI->getSExtValue() * S;
415 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000416 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000417 if (isa<AddOperator>(Op) &&
418 (!isa<Instruction>(Op) ||
419 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
420 == FuncInfo.MBB) &&
421 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
422 // An add (in the same block) with a constant operand. Fold the
423 // constant.
424 ConstantInt *CI =
425 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
426 Disp += CI->getSExtValue() * S;
427 // Iterate on the other operand.
428 Op = cast<AddOperator>(Op)->getOperand(0);
429 continue;
430 }
431 if (IndexReg == 0 &&
432 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
433 (S == 1 || S == 2 || S == 4 || S == 8)) {
434 // Scaled-index addressing.
435 Scale = S;
436 IndexReg = getRegForGEPIndex(Op).first;
437 if (IndexReg == 0)
438 return false;
439 break;
440 }
441 // Unsupported.
442 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000443 }
444 }
Dan Gohman09aae462008-09-26 20:04:15 +0000445 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000446 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000447 break;
Dan Gohman35893082008-09-18 23:23:44 +0000448 // Ok, the GEP indices were covered by constant-offset and scaled-index
449 // addressing. Update the address state and move on to examining the base.
450 AM.IndexReg = IndexReg;
451 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000452 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000453 if (X86SelectAddress(U->getOperand(0), AM))
454 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000455
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000456 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000457 // our address and just match the value instead of completely failing.
458 AM = SavedAM;
459 break;
Dan Gohman35893082008-09-18 23:23:44 +0000460 unsupported_gep:
461 // Ok, the GEP indices weren't all covered.
462 break;
463 }
464 }
465
466 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000467 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000468 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000469 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000470 return false;
471
Dan Gohman46510a72010-04-15 01:51:59 +0000472 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000473 if (GVar->isThreadLocal())
474 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000475
Chris Lattner0a1c9972011-04-17 17:47:38 +0000476 // RIP-relative addresses can't have additional register operands, so if
477 // we've already folded stuff into the addressing mode, just force the
478 // global value into its own register, which we can use as the basereg.
479 if (!Subtarget->isPICStyleRIPRel() ||
480 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
481 // Okay, we've committed to selecting this global. Set up the address.
482 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000483
Chris Lattner0a1c9972011-04-17 17:47:38 +0000484 // Allow the subtarget to classify the global.
485 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000486
Chris Lattner0a1c9972011-04-17 17:47:38 +0000487 // If this reference is relative to the pic base, set it now.
488 if (isGlobalRelativeToPICBase(GVFlags)) {
489 // FIXME: How do we know Base.Reg is free??
490 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000491 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000492
493 // Unless the ABI requires an extra load, return a direct reference to
494 // the global.
495 if (!isGlobalStubReference(GVFlags)) {
496 if (Subtarget->isPICStyleRIPRel()) {
497 // Use rip-relative addressing if we can. Above we verified that the
498 // base and index registers are unused.
499 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
500 AM.Base.Reg = X86::RIP;
501 }
502 AM.GVOpFlags = GVFlags;
503 return true;
504 }
505
506 // Ok, we need to do a load from a stub. If we've already loaded from
507 // this stub, reuse the loaded pointer, otherwise emit the load now.
508 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
509 unsigned LoadReg;
510 if (I != LocalValueMap.end() && I->second != 0) {
511 LoadReg = I->second;
512 } else {
513 // Issue load from stub.
514 unsigned Opc = 0;
515 const TargetRegisterClass *RC = NULL;
516 X86AddressMode StubAM;
517 StubAM.Base.Reg = AM.Base.Reg;
518 StubAM.GV = GV;
519 StubAM.GVOpFlags = GVFlags;
520
521 // Prepare for inserting code in the local-value area.
522 SavePoint SaveInsertPt = enterLocalValueArea();
523
524 if (TLI.getPointerTy() == MVT::i64) {
525 Opc = X86::MOV64rm;
526 RC = X86::GR64RegisterClass;
527
528 if (Subtarget->isPICStyleRIPRel())
529 StubAM.Base.Reg = X86::RIP;
530 } else {
531 Opc = X86::MOV32rm;
532 RC = X86::GR32RegisterClass;
533 }
534
535 LoadReg = createResultReg(RC);
536 MachineInstrBuilder LoadMI =
537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
538 addFullAddress(LoadMI, StubAM);
539
540 // Ok, back to normal mode.
541 leaveLocalValueArea(SaveInsertPt);
542
543 // Prevent loading GV stub multiple times in same MBB.
544 LocalValueMap[V] = LoadReg;
545 }
546
547 // Now construct the final address. Note that the Disp, Scale,
548 // and Index values may already be set here.
549 AM.Base.Reg = LoadReg;
550 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000551 return true;
552 }
Dan Gohman0586d912008-09-10 20:11:02 +0000553 }
554
Dan Gohman97135e12008-09-26 19:15:30 +0000555 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000556 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000557 if (AM.Base.Reg == 0) {
558 AM.Base.Reg = getRegForValue(V);
559 return AM.Base.Reg != 0;
560 }
561 if (AM.IndexReg == 0) {
562 assert(AM.Scale == 1 && "Scale with no index!");
563 AM.IndexReg = getRegForValue(V);
564 return AM.IndexReg != 0;
565 }
566 }
567
568 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000569}
570
Chris Lattner0aa43de2009-07-10 05:33:42 +0000571/// X86SelectCallAddress - Attempt to fill in an address from the given value.
572///
Dan Gohman46510a72010-04-15 01:51:59 +0000573bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
574 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000575 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000576 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000577 Opcode = I->getOpcode();
578 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000579 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000580 Opcode = C->getOpcode();
581 U = C;
582 }
583
584 switch (Opcode) {
585 default: break;
586 case Instruction::BitCast:
587 // Look past bitcasts.
588 return X86SelectCallAddress(U->getOperand(0), AM);
589
590 case Instruction::IntToPtr:
591 // Look past no-op inttoptrs.
592 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
593 return X86SelectCallAddress(U->getOperand(0), AM);
594 break;
595
596 case Instruction::PtrToInt:
597 // Look past no-op ptrtoints.
598 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
599 return X86SelectCallAddress(U->getOperand(0), AM);
600 break;
601 }
602
603 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000604 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000605 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000606 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000607 return false;
608
609 // RIP-relative addresses can't have additional register operands.
610 if (Subtarget->isPICStyleRIPRel() &&
611 (AM.Base.Reg != 0 || AM.IndexReg != 0))
612 return false;
613
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000614 // Can't handle DLLImport.
615 if (GV->hasDLLImportLinkage())
616 return false;
617
618 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000619 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000620 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000621 return false;
622
623 // Okay, we've committed to selecting this global. Set up the basic address.
624 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000625
Chris Lattnere6c07b52009-07-10 05:45:15 +0000626 // No ABI requires an extra load for anything other than DLLImport, which
627 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000628 if (Subtarget->isPICStyleRIPRel()) {
629 // Use rip-relative addressing if we can. Above we verified that the
630 // base and index registers are unused.
631 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
632 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000633 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000634 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
635 } else if (Subtarget->isPICStyleGOT()) {
636 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000637 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000638
Chris Lattner0aa43de2009-07-10 05:33:42 +0000639 return true;
640 }
641
642 // If all else fails, try to materialize the value in a register.
643 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
644 if (AM.Base.Reg == 0) {
645 AM.Base.Reg = getRegForValue(V);
646 return AM.Base.Reg != 0;
647 }
648 if (AM.IndexReg == 0) {
649 assert(AM.Scale == 1 && "Scale with no index!");
650 AM.IndexReg = getRegForValue(V);
651 return AM.IndexReg != 0;
652 }
653 }
654
655 return false;
656}
657
658
Owen Andersona3971df2008-09-04 07:08:58 +0000659/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000660bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000661 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000662 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000663 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000664
Dan Gohman0586d912008-09-10 20:11:02 +0000665 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000666 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000667 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000668
Chris Lattner438949a2008-10-15 05:30:52 +0000669 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000670}
671
Dan Gohman84023e02010-07-10 09:00:22 +0000672/// X86SelectRet - Select and emit code to implement ret instructions.
673bool X86FastISel::X86SelectRet(const Instruction *I) {
674 const ReturnInst *Ret = cast<ReturnInst>(I);
675 const Function &F = *I->getParent()->getParent();
676
677 if (!FuncInfo.CanLowerReturn)
678 return false;
679
680 CallingConv::ID CC = F.getCallingConv();
681 if (CC != CallingConv::C &&
682 CC != CallingConv::Fast &&
683 CC != CallingConv::X86_FastCall)
684 return false;
685
686 if (Subtarget->isTargetWin64())
687 return false;
688
689 // Don't handle popping bytes on return for now.
690 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
691 ->getBytesToPopOnReturn() != 0)
692 return 0;
693
694 // fastcc with -tailcallopt is intended to provide a guaranteed
695 // tail call optimization. Fastisel doesn't know how to do that.
696 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
697 return false;
698
699 // Let SDISel handle vararg functions.
700 if (F.isVarArg())
701 return false;
702
703 if (Ret->getNumOperands() > 0) {
704 SmallVector<ISD::OutputArg, 4> Outs;
705 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
706 Outs, TLI);
707
708 // Analyze operands of the call, assigning locations to each operand.
709 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000710 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
711 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000712 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000713
714 const Value *RV = Ret->getOperand(0);
715 unsigned Reg = getRegForValue(RV);
716 if (Reg == 0)
717 return false;
718
719 // Only handle a single return value for now.
720 if (ValLocs.size() != 1)
721 return false;
722
723 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000724
Dan Gohman84023e02010-07-10 09:00:22 +0000725 // Don't bother handling odd stuff for now.
726 if (VA.getLocInfo() != CCValAssign::Full)
727 return false;
728 // Only handle register returns for now.
729 if (!VA.isRegLoc())
730 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000731
732 // The calling-convention tables for x87 returns don't tell
733 // the whole story.
734 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
735 return false;
736
Eli Friedman22486c92011-05-18 23:13:10 +0000737 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000738 EVT SrcVT = TLI.getValueType(RV->getType());
739 EVT DstVT = VA.getValVT();
740 // Special handling for extended integers.
741 if (SrcVT != DstVT) {
742 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
743 return false;
744
745 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
746 return false;
747
748 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
749
750 if (SrcVT == MVT::i1) {
751 if (Outs[0].Flags.isSExt())
752 return false;
753 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
754 SrcVT = MVT::i8;
755 }
756 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
757 ISD::SIGN_EXTEND;
758 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
759 SrcReg, /*TODO: Kill=*/false);
760 }
761
762 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000763 unsigned DstReg = VA.getLocReg();
764 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000765 // Avoid a cross-class copy. This is very unlikely.
766 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000767 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
769 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000770
771 // Mark the register as live out of the function.
772 MRI.addLiveOut(VA.getLocReg());
773 }
774
775 // Now emit the RET.
776 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
777 return true;
778}
779
Evan Cheng8b19e562008-09-03 06:44:39 +0000780/// X86SelectLoad - Select and emit code to implement load instructions.
781///
Dan Gohman46510a72010-04-15 01:51:59 +0000782bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000783 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000784 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000785 return false;
786
Dan Gohman0586d912008-09-10 20:11:02 +0000787 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000788 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000789 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000790
Evan Cheng0de588f2008-09-05 21:00:03 +0000791 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000792 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000793 UpdateValueMap(I, ResultReg);
794 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000795 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000796 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000797}
798
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000799static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000801 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 case MVT::i8: return X86::CMP8rr;
803 case MVT::i16: return X86::CMP16rr;
804 case MVT::i32: return X86::CMP32rr;
805 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000806 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
807 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000808 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000809}
810
Chris Lattner0e13c782008-10-15 04:13:29 +0000811/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
812/// of the comparison, return an opcode that works for the compare (e.g.
813/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000814static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000816 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000817 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 case MVT::i8: return X86::CMP8ri;
819 case MVT::i16: return X86::CMP16ri;
820 case MVT::i32: return X86::CMP32ri;
821 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000822 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
823 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000824 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000825 return X86::CMP64ri32;
826 return 0;
827 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000828}
829
Dan Gohman46510a72010-04-15 01:51:59 +0000830bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
831 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000832 unsigned Op0Reg = getRegForValue(Op0);
833 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000834
Chris Lattnerd53886b2008-10-15 05:18:04 +0000835 // Handle 'null' like i32/i64 0.
836 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000837 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000838
Chris Lattner9a08a612008-10-15 04:26:38 +0000839 // We have two options: compare with register or immediate. If the RHS of
840 // the compare is an immediate that we can fold into this compare, use
841 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000842 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000843 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
845 .addReg(Op0Reg)
846 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000847 return true;
848 }
849 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000850
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000851 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000852 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000853
Chris Lattner9a08a612008-10-15 04:26:38 +0000854 unsigned Op1Reg = getRegForValue(Op1);
855 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
857 .addReg(Op0Reg)
858 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000859
Chris Lattner9a08a612008-10-15 04:26:38 +0000860 return true;
861}
862
Dan Gohman46510a72010-04-15 01:51:59 +0000863bool X86FastISel::X86SelectCmp(const Instruction *I) {
864 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000865
Duncan Sands1440e8b2010-11-03 11:35:31 +0000866 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000867 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000868 return false;
869
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000870 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000871 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000872 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000873 switch (CI->getPredicate()) {
874 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000875 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
876 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000877
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000878 unsigned EReg = createResultReg(&X86::GR8RegClass);
879 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000880 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
881 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
882 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000884 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000885 UpdateValueMap(I, ResultReg);
886 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000887 }
888 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000889 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
890 return false;
891
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000892 unsigned NEReg = createResultReg(&X86::GR8RegClass);
893 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000894 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
896 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000897 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000898 UpdateValueMap(I, ResultReg);
899 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000900 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000901 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
902 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
903 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
904 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
905 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
906 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
907 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
908 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
909 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
910 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
911 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
912 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000913
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000914 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
915 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
916 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
917 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
918 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
919 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
920 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
921 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
922 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
923 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000924 default:
925 return false;
926 }
927
Dan Gohman46510a72010-04-15 01:51:59 +0000928 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000929 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000930 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000931
Chris Lattner9a08a612008-10-15 04:26:38 +0000932 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000933 if (!X86FastEmitCompare(Op0, Op1, VT))
934 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000935
Dan Gohman84023e02010-07-10 09:00:22 +0000936 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000937 UpdateValueMap(I, ResultReg);
938 return true;
939}
Evan Cheng8b19e562008-09-03 06:44:39 +0000940
Dan Gohman46510a72010-04-15 01:51:59 +0000941bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000942 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000943 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000944 return false;
945
946 EVT DstVT = TLI.getValueType(I->getType());
947 if (!TLI.isTypeLegal(DstVT))
948 return false;
949
950 unsigned ResultReg = getRegForValue(I->getOperand(0));
951 if (ResultReg == 0)
952 return false;
953
954 // Set the high bits to zero.
955 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
956 if (ResultReg == 0)
957 return false;
958
959 if (DstVT != MVT::i8) {
960 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
961 ResultReg, /*Kill=*/true);
962 if (ResultReg == 0)
963 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000964 }
965
Eli Friedman76927d732011-05-25 23:49:02 +0000966 UpdateValueMap(I, ResultReg);
967 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +0000968}
969
Chris Lattner9a08a612008-10-15 04:26:38 +0000970
Dan Gohman46510a72010-04-15 01:51:59 +0000971bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000972 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000973 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000974 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000975 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
976 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000977
Dan Gohman8bef7442010-08-21 02:32:36 +0000978 // Fold the common case of a conditional branch with a comparison
979 // in the same block (values defined on other blocks may not have
980 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000981 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000982 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000983 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000984
Dan Gohmand98d6202008-10-02 22:15:21 +0000985 // Try to take advantage of fallthrough opportunities.
986 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000987 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000988 std::swap(TrueMBB, FalseMBB);
989 Predicate = CmpInst::getInversePredicate(Predicate);
990 }
991
Chris Lattner871d2462008-10-15 03:58:05 +0000992 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
993 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
994
Dan Gohmand98d6202008-10-02 22:15:21 +0000995 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000996 case CmpInst::FCMP_OEQ:
997 std::swap(TrueMBB, FalseMBB);
998 Predicate = CmpInst::FCMP_UNE;
999 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001000 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1001 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1002 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1003 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1004 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1005 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1006 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1007 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1008 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1009 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1010 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1011 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1012 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001013
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001014 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1015 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1016 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1017 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1018 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1019 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1020 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1021 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1022 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1023 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001024 default:
1025 return false;
1026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001027
Dan Gohman46510a72010-04-15 01:51:59 +00001028 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001029 if (SwapArgs)
1030 std::swap(Op0, Op1);
1031
Chris Lattner9a08a612008-10-15 04:26:38 +00001032 // Emit a compare of the LHS and RHS, setting the flags.
1033 if (!X86FastEmitCompare(Op0, Op1, VT))
1034 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001035
Dan Gohman84023e02010-07-10 09:00:22 +00001036 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1037 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001038
1039 if (Predicate == CmpInst::FCMP_UNE) {
1040 // X86 requires a second branch to handle UNE (and OEQ,
1041 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1043 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001044 }
1045
Stuart Hastings3bf91252010-06-17 22:43:56 +00001046 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001047 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001048 return true;
1049 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001050 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1051 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1052 // typically happen for _Bool and C++ bools.
1053 MVT SourceVT;
1054 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1055 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1056 unsigned TestOpc = 0;
1057 switch (SourceVT.SimpleTy) {
1058 default: break;
1059 case MVT::i8: TestOpc = X86::TEST8ri; break;
1060 case MVT::i16: TestOpc = X86::TEST16ri; break;
1061 case MVT::i32: TestOpc = X86::TEST32ri; break;
1062 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1063 }
1064 if (TestOpc) {
1065 unsigned OpReg = getRegForValue(TI->getOperand(0));
1066 if (OpReg == 0) return false;
1067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1068 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001069
Chris Lattnerc76d1212011-04-19 04:26:32 +00001070 unsigned JmpOpc = X86::JNE_4;
1071 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1072 std::swap(TrueMBB, FalseMBB);
1073 JmpOpc = X86::JE_4;
1074 }
Eric Christopher471e4222011-06-08 23:55:35 +00001075
Chris Lattnerc76d1212011-04-19 04:26:32 +00001076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001077 .addMBB(TrueMBB);
1078 FastEmitBranch(FalseMBB, DL);
1079 FuncInfo.MBB->addSuccessor(TrueMBB);
1080 return true;
1081 }
1082 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001083 }
1084
1085 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001086 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1087 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001088 unsigned OpReg = getRegForValue(BI->getCondition());
1089 if (OpReg == 0) return false;
1090
Eli Friedman547eb4f2011-04-27 01:34:27 +00001091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1092 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1094 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001095 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001096 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001097 return true;
1098}
1099
Dan Gohman46510a72010-04-15 01:51:59 +00001100bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001101 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001102 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001103 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001104 CReg = X86::CL;
1105 RC = &X86::GR8RegClass;
1106 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001107 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1108 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1109 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001110 default: return false;
1111 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001112 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001113 CReg = X86::CX;
1114 RC = &X86::GR16RegClass;
1115 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001116 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1117 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1118 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001119 default: return false;
1120 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001121 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001122 CReg = X86::ECX;
1123 RC = &X86::GR32RegClass;
1124 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001125 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1126 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1127 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001128 default: return false;
1129 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001130 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001131 CReg = X86::RCX;
1132 RC = &X86::GR64RegClass;
1133 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001134 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1135 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1136 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001137 default: return false;
1138 }
1139 } else {
1140 return false;
1141 }
1142
Duncan Sands1440e8b2010-11-03 11:35:31 +00001143 MVT VT;
1144 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001145 return false;
1146
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1148 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001149
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001150 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1151 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001152 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1153 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001154
1155 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001156 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001157 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1159 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001160 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001161
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001162 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1164 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001165 UpdateValueMap(I, ResultReg);
1166 return true;
1167}
1168
Dan Gohman46510a72010-04-15 01:51:59 +00001169bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001170 MVT VT;
1171 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001172 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001173
Eric Christophere487b012010-09-29 23:00:29 +00001174 // We only use cmov here, if we don't have a cmov instruction bail.
1175 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001176
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001177 unsigned Opc = 0;
1178 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001179 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001180 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001181 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001182 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001183 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001184 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001185 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001186 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001187 RC = &X86::GR64RegClass;
1188 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001190 }
1191
1192 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1193 if (Op0Reg == 0) return false;
1194 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1195 if (Op1Reg == 0) return false;
1196 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1197 if (Op2Reg == 0) return false;
1198
Dan Gohman84023e02010-07-10 09:00:22 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1200 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001201 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001202 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1203 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001204 UpdateValueMap(I, ResultReg);
1205 return true;
1206}
1207
Dan Gohman46510a72010-04-15 01:51:59 +00001208bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001209 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001210 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001211 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001212 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001213 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001214 unsigned OpReg = getRegForValue(V);
1215 if (OpReg == 0) return false;
1216 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1218 TII.get(X86::CVTSS2SDrr), ResultReg)
1219 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001220 UpdateValueMap(I, ResultReg);
1221 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001222 }
1223 }
1224
1225 return false;
1226}
1227
Dan Gohman46510a72010-04-15 01:51:59 +00001228bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001229 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001230 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001231 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001232 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001233 unsigned OpReg = getRegForValue(V);
1234 if (OpReg == 0) return false;
1235 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1237 TII.get(X86::CVTSD2SSrr), ResultReg)
1238 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001239 UpdateValueMap(I, ResultReg);
1240 return true;
1241 }
1242 }
1243 }
1244
1245 return false;
1246}
1247
Dan Gohman46510a72010-04-15 01:51:59 +00001248bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001249 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1250 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251
Eli Friedman76927d732011-05-25 23:49:02 +00001252 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001254 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001255 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001256 return false;
1257
1258 unsigned InputReg = getRegForValue(I->getOperand(0));
1259 if (!InputReg)
1260 // Unhandled operand. Halt "fast" selection and bail.
1261 return false;
1262
Eli Friedman76927d732011-05-25 23:49:02 +00001263 if (SrcVT == MVT::i8) {
1264 // Truncate from i8 to i1; no code needed.
1265 UpdateValueMap(I, InputReg);
1266 return true;
1267 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001268
Eli Friedman76927d732011-05-25 23:49:02 +00001269 if (!Subtarget->is64Bit()) {
1270 // If we're on x86-32; we can't extract an i8 from a general register.
1271 // First issue a copy to GR16_ABCD or GR32_ABCD.
1272 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1273 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
1274 unsigned CopyReg = createResultReg(CopyRC);
1275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1276 CopyReg).addReg(InputReg);
1277 InputReg = CopyReg;
1278 }
1279
1280 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001282 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001283 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001284 if (!ResultReg)
1285 return false;
1286
1287 UpdateValueMap(I, ResultReg);
1288 return true;
1289}
1290
Eli Friedmanc0883452011-05-20 22:21:04 +00001291bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1292 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1293}
1294
Eli Friedmand5089a92011-04-27 01:45:07 +00001295bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1296 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001297
Eli Friedmand5089a92011-04-27 01:45:07 +00001298 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001299 if (!IsMemcpySmall(Len))
1300 return false;
1301
1302 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001303
1304 // We don't care about alignment here since we just emit integer accesses.
1305 while (Len) {
1306 MVT VT;
1307 if (Len >= 8 && i64Legal)
1308 VT = MVT::i64;
1309 else if (Len >= 4)
1310 VT = MVT::i32;
1311 else if (Len >= 2)
1312 VT = MVT::i16;
1313 else {
1314 assert(Len == 1);
1315 VT = MVT::i8;
1316 }
1317
1318 unsigned Reg;
1319 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1320 RV &= X86FastEmitStore(VT, Reg, DestAM);
1321 assert(RV && "Failed to emit load or store??");
1322
1323 unsigned Size = VT.getSizeInBits()/8;
1324 Len -= Size;
1325 DestAM.Disp += Size;
1326 SrcAM.Disp += Size;
1327 }
1328
1329 return true;
1330}
1331
Dan Gohman46510a72010-04-15 01:51:59 +00001332bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001333 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001334 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001335 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001336 case Intrinsic::memcpy: {
1337 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1338 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001339 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001340 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001341
Eli Friedman25255cb2011-06-10 23:39:36 +00001342 if (isa<ConstantInt>(MCI.getLength())) {
1343 // Small memcpy's are common enough that we want to do them
1344 // without a call if possible.
1345 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1346 if (IsMemcpySmall(Len)) {
1347 X86AddressMode DestAM, SrcAM;
1348 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1349 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1350 return false;
1351 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1352 return true;
1353 }
1354 }
Eric Christopher471e4222011-06-08 23:55:35 +00001355
Eli Friedman25255cb2011-06-10 23:39:36 +00001356 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1357 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001358 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001359
Eli Friedman25255cb2011-06-10 23:39:36 +00001360 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1361 return false;
1362
1363 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001364 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001365 case Intrinsic::memset: {
1366 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001367
Eli Friedman25255cb2011-06-10 23:39:36 +00001368 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1369 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1370 return false;
1371
1372 if (MSI.getDestAddressSpace() > 255)
1373 return false;
1374
1375 return DoSelectCall(&I, "memset");
1376 }
Eric Christopher07754c22010-03-18 20:27:26 +00001377 case Intrinsic::stackprotector: {
1378 // Emit code inline code to store the stack guard onto the stack.
1379 EVT PtrTy = TLI.getPointerTy();
1380
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001381 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1382 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001383
1384 // Grab the frame index.
1385 X86AddressMode AM;
1386 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001387 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001388 return true;
1389 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001390 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001391 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001392 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001393 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001394 if (!X86SelectAddress(DI->getAddress(), AM))
1395 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001396 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001397 // FIXME may need to add RegState::Debug to any registers produced,
1398 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001399 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1400 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001401 return true;
1402 }
Eric Christopher77f79892010-01-18 22:11:29 +00001403 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001404 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001405 return true;
1406 }
Bill Wendling52370a12008-12-09 02:42:50 +00001407 case Intrinsic::sadd_with_overflow:
1408 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001409 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001410
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001411 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001412 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001413 const Function *Callee = I.getCalledFunction();
1414 const Type *RetTy =
1415 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1416
Duncan Sands1440e8b2010-11-03 11:35:31 +00001417 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001418 if (!isTypeLegal(RetTy, VT))
1419 return false;
1420
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001421 const Value *Op1 = I.getArgOperand(0);
1422 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001423 unsigned Reg1 = getRegForValue(Op1);
1424 unsigned Reg2 = getRegForValue(Op2);
1425
1426 if (Reg1 == 0 || Reg2 == 0)
1427 // FIXME: Handle values *not* in registers.
1428 return false;
1429
1430 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001432 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001433 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001434 OpC = X86::ADD64rr;
1435 else
1436 return false;
1437
Eli Friedman482feb32011-05-16 21:06:17 +00001438 // The call to CreateRegs builds two sequential registers, to store the
1439 // both the the returned values.
1440 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1442 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001443
Chris Lattnera9a42252009-04-12 07:36:01 +00001444 unsigned Opc = X86::SETBr;
1445 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1446 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1448
1449 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001450 return true;
1451 }
1452 }
1453}
1454
Dan Gohman46510a72010-04-15 01:51:59 +00001455bool X86FastISel::X86SelectCall(const Instruction *I) {
1456 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001457 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001458
1459 // Can't handle inline asm yet.
1460 if (isa<InlineAsm>(Callee))
1461 return false;
1462
Bill Wendling52370a12008-12-09 02:42:50 +00001463 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001464 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001465 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001466
Eli Friedman25255cb2011-06-10 23:39:36 +00001467 return DoSelectCall(I, 0);
1468}
1469
1470// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1471bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1472 const CallInst *CI = cast<CallInst>(I);
1473 const Value *Callee = CI->getCalledValue();
1474
Evan Chengf3d4efe2008-09-07 09:09:33 +00001475 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001476 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001477 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001478 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 CC != CallingConv::X86_FastCall)
1480 return false;
1481
Evan Cheng381993f2010-01-27 00:00:57 +00001482 // fastcc with -tailcallopt is intended to provide a guaranteed
1483 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001484 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001485 return false;
1486
Evan Chengf3d4efe2008-09-07 09:09:33 +00001487 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1488 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001489 bool isVarArg = FTy->isVarArg();
1490
1491 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1492 // x86-32. Special handling for x86-64 is implemented.
1493 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001494 return false;
1495
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001496 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001497 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
1498 GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001499 return false;
1500
Eli Friedman19515b42011-05-17 18:29:03 +00001501 // Check whether the function can return without sret-demotion.
1502 SmallVector<ISD::OutputArg, 4> Outs;
1503 SmallVector<uint64_t, 4> Offsets;
1504 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1505 Outs, TLI, &Offsets);
1506 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00001507 *FuncInfo.MF, FTy->isVarArg(),
1508 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001509 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001510 return false;
1511
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001512 // Materialize callee address in a register. FIXME: GV address can be
1513 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001514 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001515 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001516 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001517 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001518 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001519 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001520 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001521 } else if (CalleeAM.Base.Reg != 0) {
1522 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001523 } else
1524 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001525
Evan Chengf3d4efe2008-09-07 09:09:33 +00001526 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001527 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001528 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001529 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001530 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001531 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001532 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001533 ArgVTs.reserve(CS.arg_size());
1534 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001535 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001536 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001537 // If we're lowering a mem intrinsic instead of a regular call, skip the
1538 // last two arguments, which should not passed to the underlying functions.
1539 if (MemIntName && e-i <= 2)
1540 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001541 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 ISD::ArgFlagsTy Flags;
1543 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001544 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001545 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001546 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001547 Flags.setZExt();
1548
Eli Friedmanc0883452011-05-20 22:21:04 +00001549 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
1550 const PointerType *Ty = cast<PointerType>(ArgVal->getType());
1551 const Type *ElementTy = Ty->getElementType();
1552 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1553 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1554 if (!FrameAlign)
1555 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1556 Flags.setByVal();
1557 Flags.setByValSize(FrameSize);
1558 Flags.setByValAlign(FrameAlign);
1559 if (!IsMemcpySmall(FrameSize))
1560 return false;
1561 }
1562
1563 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1564 Flags.setInReg();
1565 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1566 Flags.setNest();
1567
Chris Lattnere03b8d32011-04-19 04:42:38 +00001568 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1569 // instruction. This is safe because it is common to all fastisel supported
1570 // calling conventions on x86.
1571 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1572 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1573 CI->getBitWidth() == 16) {
1574 if (Flags.isSExt())
1575 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1576 else
1577 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1578 }
1579 }
Eric Christopher471e4222011-06-08 23:55:35 +00001580
Chris Lattnerb44101c2011-04-19 05:09:50 +00001581 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001582
Chris Lattnerff009ad2011-04-19 05:15:59 +00001583 // Passing bools around ends up doing a trunc to i1 and passing it.
1584 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001585 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1586 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1587 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001588 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1589 ArgReg = getRegForValue(ArgVal);
1590 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001591
Chris Lattnerb44101c2011-04-19 05:09:50 +00001592 MVT ArgVT;
1593 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001594
Chris Lattnerb44101c2011-04-19 05:09:50 +00001595 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1596 ArgVal->hasOneUse(), 1);
1597 } else {
1598 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001599 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001600
Chris Lattnerff009ad2011-04-19 05:15:59 +00001601 if (ArgReg == 0) return false;
1602
Chris Lattnere03b8d32011-04-19 04:42:38 +00001603 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001604 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001605 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001606 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001607 if (ArgVT == MVT::x86mmx)
1608 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001609 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1610 Flags.setOrigAlign(OriginalAlignment);
1611
Chris Lattnerb44101c2011-04-19 05:09:50 +00001612 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001613 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001614 ArgVTs.push_back(ArgVT);
1615 ArgFlags.push_back(Flags);
1616 }
1617
1618 // Analyze operands of the call, assigning locations to each operand.
1619 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001620 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1621 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001622
Dan Gohmand8acddd2010-06-01 21:09:47 +00001623 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001624 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001625 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001626
Duncan Sands45907662010-10-31 13:21:44 +00001627 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001628
1629 // Get a count of how many bytes are to be pushed on the stack.
1630 unsigned NumBytes = CCInfo.getNextStackOffset();
1631
1632 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001633 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001634 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1635 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001636
Chris Lattner438949a2008-10-15 05:30:52 +00001637 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001638 // copies / loads.
1639 SmallVector<unsigned, 4> RegArgs;
1640 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1641 CCValAssign &VA = ArgLocs[i];
1642 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001643 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644
Evan Chengf3d4efe2008-09-07 09:09:33 +00001645 // Promote the value if needed.
1646 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001647 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001648 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001649 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001650 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1651 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001652 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1653 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001654 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001655 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001656 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001657 }
1658 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001659 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1660 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001661 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1662 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001663 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001664 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001665 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001666 }
1667 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001668 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1669 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001670 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1671 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001672 if (!Emitted)
1673 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001674 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001675 if (!Emitted)
1676 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1677 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001678
Chris Lattnerc46ec642011-01-05 22:26:52 +00001679 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001680 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001681 break;
1682 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001683 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001684 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001685 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001686 assert(BC != 0 && "Failed to emit a bitcast!");
1687 Arg = BC;
1688 ArgVT = VA.getLocVT();
1689 break;
1690 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001691 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692
Evan Chengf3d4efe2008-09-07 09:09:33 +00001693 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1695 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001696 RegArgs.push_back(VA.getLocReg());
1697 } else {
1698 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001699 X86AddressMode AM;
1700 AM.Base.Reg = StackPtr;
1701 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001702 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001703 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001704
Eli Friedmanc0883452011-05-20 22:21:04 +00001705 if (Flags.isByVal()) {
1706 X86AddressMode SrcAM;
1707 SrcAM.Base.Reg = Arg;
1708 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1709 assert(Res && "memcpy length already checked!"); (void)Res;
1710 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1711 // If this is a really simple value, emit this with the Value* version
1712 //of X86FastEmitStore. If it isn't simple, we don't want to do this,
1713 // as it can cause us to reevaluate the argument.
Chris Lattner241ab472008-10-15 05:38:32 +00001714 X86FastEmitStore(ArgVT, ArgVal, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001715 } else {
Chris Lattner241ab472008-10-15 05:38:32 +00001716 X86FastEmitStore(ArgVT, Arg, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001717 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001718 }
1719 }
1720
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001721 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001722 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001723 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001724 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1726 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001727 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001728
Eli Friedman37620462011-04-19 17:22:22 +00001729 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1730 // Count the number of XMM registers allocated.
1731 static const unsigned XMMArgRegs[] = {
1732 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1733 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1734 };
1735 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1737 X86::AL).addImm(NumXMMRegs);
1738 }
1739
Evan Chengf3d4efe2008-09-07 09:09:33 +00001740 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001741 MachineInstrBuilder MIB;
1742 if (CalleeOp) {
1743 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001744 unsigned CallOpc;
1745 if (Subtarget->isTargetWin64())
1746 CallOpc = X86::WINCALL64r;
1747 else if (Subtarget->is64Bit())
1748 CallOpc = X86::CALL64r;
1749 else
1750 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001751 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1752 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001753
Chris Lattner51e8eab2009-07-09 06:34:26 +00001754 } else {
1755 // Direct call.
1756 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001757 unsigned CallOpc;
1758 if (Subtarget->isTargetWin64())
1759 CallOpc = X86::WINCALL64pcrel32;
1760 else if (Subtarget->is64Bit())
1761 CallOpc = X86::CALL64pcrel32;
1762 else
1763 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001764
Chris Lattner51e8eab2009-07-09 06:34:26 +00001765 // See if we need any target-specific flags on the GV operand.
1766 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001767
Chris Lattner51e8eab2009-07-09 06:34:26 +00001768 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1769 // external symbols most go through the PLT in PIC mode. If the symbol
1770 // has hidden or protected visibility, or if it is static or local, then
1771 // we don't need to use the PLT - we can directly call it.
1772 if (Subtarget->isTargetELF() &&
1773 TM.getRelocationModel() == Reloc::PIC_ &&
1774 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1775 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001776 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001777 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001778 (!Subtarget->getTargetTriple().isMacOSX() ||
1779 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001780 // PC-relative references to external symbols should go through $stub,
1781 // unless we're building with the leopard linker or later, which
1782 // automatically synthesizes these stubs.
1783 OpFlags = X86II::MO_DARWIN_STUB;
1784 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001785
1786
Eli Friedman25255cb2011-06-10 23:39:36 +00001787 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1788 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001789 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001790 else
1791 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001792 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001793
1794 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001795 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001796 MIB.addReg(X86::EBX);
1797
Eli Friedman37620462011-04-19 17:22:22 +00001798 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1799 MIB.addReg(X86::AL);
1800
Evan Chengf3d4efe2008-09-07 09:09:33 +00001801 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001802 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1803 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001804
1805 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001806 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001807 unsigned NumBytesCallee = 0;
1808 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1809 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001810 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001811 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001812
Eli Friedman19515b42011-05-17 18:29:03 +00001813 // Build info for return calling conv lowering code.
1814 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1815 SmallVector<ISD::InputArg, 32> Ins;
1816 SmallVector<EVT, 4> RetTys;
1817 ComputeValueVTs(TLI, I->getType(), RetTys);
1818 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1819 EVT VT = RetTys[i];
1820 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1821 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1822 for (unsigned j = 0; j != NumRegs; ++j) {
1823 ISD::InputArg MyFlags;
1824 MyFlags.VT = RegisterVT.getSimpleVT();
1825 MyFlags.Used = !CS.getInstruction()->use_empty();
1826 if (CS.paramHasAttr(0, Attribute::SExt))
1827 MyFlags.Flags.setSExt();
1828 if (CS.paramHasAttr(0, Attribute::ZExt))
1829 MyFlags.Flags.setZExt();
1830 if (CS.paramHasAttr(0, Attribute::InReg))
1831 MyFlags.Flags.setInReg();
1832 Ins.push_back(MyFlags);
1833 }
1834 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001835
Eli Friedman19515b42011-05-17 18:29:03 +00001836 // Now handle call return values.
1837 SmallVector<unsigned, 4> UsedRegs;
1838 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001839 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1840 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001841 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1842 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1843 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1844 EVT CopyVT = RVLocs[i].getValVT();
1845 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001846
Evan Chengf3d4efe2008-09-07 09:09:33 +00001847 // If this is a call to a function that returns an fp value on the x87 fp
1848 // stack, but where we prefer to use the value in xmm registers, copy it
1849 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001850 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1851 RVLocs[i].getLocReg() == X86::ST1) &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001852 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 CopyVT = MVT::f80;
Eli Friedman19515b42011-05-17 18:29:03 +00001854 CopyReg = createResultReg(X86::RFP80RegisterClass);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001855 }
1856
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eli Friedman19515b42011-05-17 18:29:03 +00001858 CopyReg).addReg(RVLocs[i].getLocReg());
1859 UsedRegs.push_back(RVLocs[i].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001860
Eli Friedman19515b42011-05-17 18:29:03 +00001861 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001862 // Round the F80 the right size, which also moves to the appropriate xmm
1863 // register. This is accomplished by storing the F80 value in memory and
1864 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001865 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001867 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001868 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001869 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1870 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001871 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001872 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001873 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001874 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001875 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001876 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001877
Eli Friedman19515b42011-05-17 18:29:03 +00001878 if (RVLocs.size())
1879 UpdateValueMap(I, ResultReg, RVLocs.size());
1880
Dan Gohmandb497122010-06-18 23:28:01 +00001881 // Set all unused physreg defs as dead.
1882 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1883
Evan Chengf3d4efe2008-09-07 09:09:33 +00001884 return true;
1885}
1886
1887
Dan Gohman99b21822008-08-28 23:21:34 +00001888bool
Dan Gohman46510a72010-04-15 01:51:59 +00001889X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001890 switch (I->getOpcode()) {
1891 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001892 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001893 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001894 case Instruction::Store:
1895 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001896 case Instruction::Ret:
1897 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001898 case Instruction::ICmp:
1899 case Instruction::FCmp:
1900 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001901 case Instruction::ZExt:
1902 return X86SelectZExt(I);
1903 case Instruction::Br:
1904 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001905 case Instruction::Call:
1906 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001907 case Instruction::LShr:
1908 case Instruction::AShr:
1909 case Instruction::Shl:
1910 return X86SelectShift(I);
1911 case Instruction::Select:
1912 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001913 case Instruction::Trunc:
1914 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001915 case Instruction::FPExt:
1916 return X86SelectFPExt(I);
1917 case Instruction::FPTrunc:
1918 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001919 case Instruction::IntToPtr: // Deliberate fall-through.
1920 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001921 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1922 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001923 if (DstVT.bitsGT(SrcVT))
1924 return X86SelectZExt(I);
1925 if (DstVT.bitsLT(SrcVT))
1926 return X86SelectTrunc(I);
1927 unsigned Reg = getRegForValue(I->getOperand(0));
1928 if (Reg == 0) return false;
1929 UpdateValueMap(I, Reg);
1930 return true;
1931 }
Dan Gohman99b21822008-08-28 23:21:34 +00001932 }
1933
1934 return false;
1935}
1936
Dan Gohman46510a72010-04-15 01:51:59 +00001937unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001938 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001939 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001940 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001941
Owen Anderson95267a12008-09-05 00:06:23 +00001942 // Get opcode and regclass of the output for the given load instruction.
1943 unsigned Opc = 0;
1944 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001945 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001946 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001948 Opc = X86::MOV8rm;
1949 RC = X86::GR8RegisterClass;
1950 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001952 Opc = X86::MOV16rm;
1953 RC = X86::GR16RegisterClass;
1954 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001956 Opc = X86::MOV32rm;
1957 RC = X86::GR32RegisterClass;
1958 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001960 // Must be in x86-64 mode.
1961 Opc = X86::MOV64rm;
1962 RC = X86::GR64RegisterClass;
1963 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001965 if (Subtarget->hasSSE1()) {
1966 Opc = X86::MOVSSrm;
1967 RC = X86::FR32RegisterClass;
1968 } else {
1969 Opc = X86::LD_Fp32m;
1970 RC = X86::RFP32RegisterClass;
1971 }
1972 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001974 if (Subtarget->hasSSE2()) {
1975 Opc = X86::MOVSDrm;
1976 RC = X86::FR64RegisterClass;
1977 } else {
1978 Opc = X86::LD_Fp64m;
1979 RC = X86::RFP64RegisterClass;
1980 }
1981 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001983 // No f80 support yet.
1984 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001985 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001986
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001987 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001988 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001989 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001990 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001991 // If the expression is just a basereg, then we're done, otherwise we need
1992 // to emit an LEA.
1993 if (AM.BaseType == X86AddressMode::RegBase &&
1994 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1995 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00001996
Chris Lattner685090f2011-04-17 17:12:08 +00001997 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001998 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001999 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2000 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002001 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002002 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002003 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002004 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002005
Owen Anderson3b217c62008-09-06 01:11:01 +00002006 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002007 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002008 if (Align == 0) {
2009 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002010 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002011 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002012
Dan Gohman5396c992008-09-30 01:21:32 +00002013 // x86-32 PIC requires a PIC base register for constant pools.
2014 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002015 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002016 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002017 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002018 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002019 } else if (Subtarget->isPICStyleGOT()) {
2020 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002021 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002022 } else if (Subtarget->isPICStyleRIPRel() &&
2023 TM.getCodeModel() == CodeModel::Small) {
2024 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002025 }
Dan Gohman5396c992008-09-30 01:21:32 +00002026
2027 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002028 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002029 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002030 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2031 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002032 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002033
Owen Anderson95267a12008-09-05 00:06:23 +00002034 return ResultReg;
2035}
2036
Dan Gohman46510a72010-04-15 01:51:59 +00002037unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002038 // Fail on dynamic allocas. At this point, getRegForValue has already
2039 // checked its CSE maps, so if we're here trying to handle a dynamic
2040 // alloca, we're not going to succeed. X86SelectAddress has a
2041 // check for dynamic allocas, because it's called directly from
2042 // various places, but TargetMaterializeAlloca also needs a check
2043 // in order to avoid recursion between getRegForValue,
2044 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002045 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002046 return 0;
2047
Dan Gohman0586d912008-09-10 20:11:02 +00002048 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002049 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002050 return 0;
2051 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2052 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2053 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002054 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2055 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002056 return ResultReg;
2057}
2058
Eli Friedman2790ba82011-04-27 22:41:55 +00002059unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2060 MVT VT;
2061 if (!isTypeLegal(CF->getType(), VT))
2062 return false;
2063
2064 // Get opcode and regclass for the given zero.
2065 unsigned Opc = 0;
2066 const TargetRegisterClass *RC = NULL;
2067 switch (VT.SimpleTy) {
2068 default: return false;
2069 case MVT::f32:
2070 if (Subtarget->hasSSE1()) {
2071 Opc = X86::FsFLD0SS;
2072 RC = X86::FR32RegisterClass;
2073 } else {
2074 Opc = X86::LD_Fp032;
2075 RC = X86::RFP32RegisterClass;
2076 }
2077 break;
2078 case MVT::f64:
2079 if (Subtarget->hasSSE2()) {
2080 Opc = X86::FsFLD0SD;
2081 RC = X86::FR64RegisterClass;
2082 } else {
2083 Opc = X86::LD_Fp064;
2084 RC = X86::RFP64RegisterClass;
2085 }
2086 break;
2087 case MVT::f80:
2088 // No f80 support yet.
2089 return false;
2090 }
2091
2092 unsigned ResultReg = createResultReg(RC);
2093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2094 return ResultReg;
2095}
2096
2097
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002098/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2099/// vreg is being provided by the specified load instruction. If possible,
2100/// try to fold the load as an operand to the instruction, returning true if
2101/// possible.
2102bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2103 const LoadInst *LI) {
2104 X86AddressMode AM;
2105 if (!X86SelectAddress(LI->getOperand(0), AM))
2106 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002107
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002108 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002110 unsigned Size = TD.getTypeAllocSize(LI->getType());
2111 unsigned Alignment = LI->getAlignment();
2112
2113 SmallVector<MachineOperand, 8> AddrOps;
2114 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002115
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002116 MachineInstr *Result =
2117 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2118 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002119
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002120 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002121 MI->eraseFromParent();
2122 return true;
2123}
2124
2125
Evan Chengc3f44b02008-09-03 00:03:49 +00002126namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002127 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2128 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002129 }
Dan Gohman99b21822008-08-28 23:21:34 +00002130}