blob: cdcbee6bc1626dd3696133173be06fabb32f06e2 [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 Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000027#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000033#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000034#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000035using namespace llvm;
36
Chris Lattner087fcf32009-03-08 18:44:31 +000037namespace {
38
Evan Chengc3f44b02008-09-03 00:03:49 +000039class X86FastISel : public FastISel {
40 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
41 /// make the right decision when generating code for different targets.
42 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000043
44 /// StackPtr - Register used as the stack pointer.
45 ///
46 unsigned StackPtr;
47
48 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
49 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Cheng8b19e562008-09-03 06:44:39 +000055public:
Dan Gohmana4160c32010-07-07 16:29:44 +000056 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000057 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000058 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
59 X86ScalarSSEf64 = Subtarget->hasSSE2();
60 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000061 }
Evan Chengc3f44b02008-09-03 00:03:49 +000062
Dan Gohman46510a72010-04-15 01:51:59 +000063 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman1adf1b02008-08-19 21:45:35 +000065#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000066
67private:
Dan Gohman46510a72010-04-15 01:51:59 +000068 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000069
Owen Andersone50ed302009-08-10 22:56:29 +000070 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000071
Dan Gohman46510a72010-04-15 01:51:59 +000072 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000073 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000074 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000075 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000076
Owen Andersone50ed302009-08-10 22:56:29 +000077 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000078 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
81 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000082
Dan Gohman46510a72010-04-15 01:51:59 +000083 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000084
Dan Gohman46510a72010-04-15 01:51:59 +000085 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000088
Dan Gohman46510a72010-04-15 01:51:59 +000089 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000094
Dan Gohman46510a72010-04-15 01:51:59 +000095 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000096
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectFPExt(const Instruction *I);
100 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
105 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000106
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000107 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000108
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000109 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000110 return getTargetMachine()->getInstrInfo();
111 }
112 const X86TargetMachine *getTargetMachine() const {
113 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000114 }
115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000117
Dan Gohman46510a72010-04-15 01:51:59 +0000118 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119
120 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
121 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000122 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
124 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000125 }
126
Owen Andersone50ed302009-08-10 22:56:29 +0000127 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000128};
Chris Lattner087fcf32009-03-08 18:44:31 +0000129
130} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000131
Owen Andersone50ed302009-08-10 22:56:29 +0000132bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000133 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 // Unhandled type. Halt "fast" selection and bail.
136 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000137
Dan Gohman9b66d732008-09-30 00:48:39 +0000138 // For now, require SSE/SSE2 for performing floating-point operations,
139 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000141 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000143 return false;
144 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000146 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // We only handle legal types. For example, on x86-32 the instruction
148 // selector contains all of the 64-bit instructions from x86-64,
149 // under the assumption that i64 won't be used if the target doesn't
150 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152}
153
154#include "X86GenCallingConv.inc"
155
156/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
157/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000158CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
159 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000160 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000161 if (CC == CallingConv::GHC)
162 return CC_X86_64_GHC;
163 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165 else
166 return CC_X86_64_C;
167 }
168
169 if (CC == CallingConv::X86_FastCall)
170 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000171 else if (CC == CallingConv::X86_ThisCall)
172 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000173 else if (CC == CallingConv::Fast)
174 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000175 else if (CC == CallingConv::GHC)
176 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_32_C;
179}
180
Evan Cheng0de588f2008-09-05 21:00:03 +0000181/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000182/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000183/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000184bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 unsigned &ResultReg) {
186 // Get opcode and regclass of the output for the given load instruction.
187 unsigned Opc = 0;
188 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000190 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000191 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 Opc = X86::MOV8rm;
194 RC = X86::GR8RegisterClass;
195 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 Opc = X86::MOV16rm;
198 RC = X86::GR16RegisterClass;
199 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 Opc = X86::MOV32rm;
202 RC = X86::GR32RegisterClass;
203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 // Must be in x86-64 mode.
206 Opc = X86::MOV64rm;
207 RC = X86::GR64RegisterClass;
208 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 if (Subtarget->hasSSE1()) {
211 Opc = X86::MOVSSrm;
212 RC = X86::FR32RegisterClass;
213 } else {
214 Opc = X86::LD_Fp32m;
215 RC = X86::RFP32RegisterClass;
216 }
217 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 if (Subtarget->hasSSE2()) {
220 Opc = X86::MOVSDrm;
221 RC = X86::FR64RegisterClass;
222 } else {
223 Opc = X86::LD_Fp64m;
224 RC = X86::RFP64RegisterClass;
225 }
226 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000228 // No f80 support yet.
229 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 }
231
232 ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000233 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
234 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000235 return true;
236}
237
Evan Chengf3d4efe2008-09-07 09:09:33 +0000238/// X86FastEmitStore - Emit a machine instruction to store a value Val of
239/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
240/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000241/// i.e. V. Return true if it is possible.
242bool
Owen Andersone50ed302009-08-10 22:56:29 +0000243X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000244 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000245 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 switch (VT.getSimpleVT().SimpleTy) {
248 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000250 case MVT::i1: {
251 // Mask out all but lowest bit.
252 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000254 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
255 Val = AndResult;
256 }
257 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 case MVT::i8: Opc = X86::MOV8mr; break;
259 case MVT::i16: Opc = X86::MOV16mr; break;
260 case MVT::i32: Opc = X86::MOV32mr; break;
261 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
262 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000263 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000266 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000268 }
Chris Lattner438949a2008-10-15 05:30:52 +0000269
Dan Gohmaneabaed22010-07-07 16:47:08 +0000270 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
271 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000272 return true;
273}
274
Dan Gohman46510a72010-04-15 01:51:59 +0000275bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000276 const X86AddressMode &AM) {
277 // Handle 'null' like i32/i64 0.
278 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000279 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000280
281 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000282 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000283 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000284 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000286 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000287 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 case MVT::i8: Opc = X86::MOV8mi; break;
289 case MVT::i16: Opc = X86::MOV16mi; break;
290 case MVT::i32: Opc = X86::MOV32mi; break;
291 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000292 // Must be a 32-bit sign extended value.
293 if ((int)CI->getSExtValue() == CI->getSExtValue())
294 Opc = X86::MOV64mi32;
295 break;
296 }
297
298 if (Opc) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000299 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
300 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000301 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000302 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000303 return true;
304 }
305 }
306
307 unsigned ValReg = getRegForValue(Val);
308 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000309 return false;
310
311 return X86FastEmitStore(VT, ValReg, AM);
312}
313
Evan Cheng24e3a902008-09-08 06:35:17 +0000314/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
315/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
316/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000317bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
318 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000319 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000320 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
321 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000322
323 if (RR != 0) {
324 ResultReg = RR;
325 return true;
326 } else
327 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000328}
329
Dan Gohman0586d912008-09-10 20:11:02 +0000330/// X86SelectAddress - Attempt to fill in an address from the given value.
331///
Dan Gohman46510a72010-04-15 01:51:59 +0000332bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
333 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000334 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000335 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000336 // Don't walk into other basic blocks; it's possible we haven't
337 // visited them yet, so the instructions may not yet be assigned
338 // virtual registers.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000339 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000340 return false;
341
Dan Gohman35893082008-09-18 23:23:44 +0000342 Opcode = I->getOpcode();
343 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000344 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000345 Opcode = C->getOpcode();
346 U = C;
347 }
Dan Gohman0586d912008-09-10 20:11:02 +0000348
Chris Lattner868ee942010-06-15 19:08:40 +0000349 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
350 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000351 // Fast instruction selection doesn't support the special
352 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000353 return false;
354
Dan Gohman35893082008-09-18 23:23:44 +0000355 switch (Opcode) {
356 default: break;
357 case Instruction::BitCast:
358 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000359 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000360
361 case Instruction::IntToPtr:
362 // Look past no-op inttoptrs.
363 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000364 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000365 break;
Dan Gohman35893082008-09-18 23:23:44 +0000366
367 case Instruction::PtrToInt:
368 // Look past no-op ptrtoints.
369 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000370 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000371 break;
Dan Gohman35893082008-09-18 23:23:44 +0000372
373 case Instruction::Alloca: {
374 // Do static allocas.
375 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000376 DenseMap<const AllocaInst*, int>::iterator SI =
377 FuncInfo.StaticAllocaMap.find(A);
378 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000379 AM.BaseType = X86AddressMode::FrameIndexBase;
380 AM.Base.FrameIndex = SI->second;
381 return true;
382 }
383 break;
Dan Gohman35893082008-09-18 23:23:44 +0000384 }
385
386 case Instruction::Add: {
387 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000388 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000389 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
390 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000391 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000392 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000393 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000394 }
Dan Gohman0586d912008-09-10 20:11:02 +0000395 }
Dan Gohman35893082008-09-18 23:23:44 +0000396 break;
397 }
398
399 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000400 X86AddressMode SavedAM = AM;
401
Dan Gohman35893082008-09-18 23:23:44 +0000402 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000403 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000404 unsigned IndexReg = AM.IndexReg;
405 unsigned Scale = AM.Scale;
406 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000407 // Iterate through the indices, folding what we can. Constants can be
408 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000409 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000410 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000411 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000412 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
413 const StructLayout *SL = TD.getStructLayout(STy);
414 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
415 Disp += SL->getElementOffset(Idx);
416 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000417 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000418 SmallVector<const Value *, 4> Worklist;
419 Worklist.push_back(Op);
420 do {
421 Op = Worklist.pop_back_val();
422 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
423 // Constant-offset addressing.
424 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000425 } else if (isa<AddOperator>(Op) &&
426 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
427 // An add with a constant operand. Fold the constant.
428 ConstantInt *CI =
429 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
430 Disp += CI->getSExtValue() * S;
431 // Add the other operand back to the work list.
432 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000433 } else if (IndexReg == 0 &&
434 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
435 (S == 1 || S == 2 || S == 4 || S == 8)) {
436 // Scaled-index addressing.
437 Scale = S;
438 IndexReg = getRegForGEPIndex(Op).first;
439 if (IndexReg == 0)
440 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000441 } else
442 // Unsupported.
443 goto unsupported_gep;
444 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000445 }
446 }
Dan Gohman09aae462008-09-26 20:04:15 +0000447 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000448 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000449 break;
Dan Gohman35893082008-09-18 23:23:44 +0000450 // Ok, the GEP indices were covered by constant-offset and scaled-index
451 // addressing. Update the address state and move on to examining the base.
452 AM.IndexReg = IndexReg;
453 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000454 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000455 if (X86SelectAddress(U->getOperand(0), AM))
456 return true;
457
458 // If we couldn't merge the sub value into this addr mode, revert back to
459 // our address and just match the value instead of completely failing.
460 AM = SavedAM;
461 break;
Dan Gohman35893082008-09-18 23:23:44 +0000462 unsupported_gep:
463 // Ok, the GEP indices weren't all covered.
464 break;
465 }
466 }
467
468 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000469 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000470 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000471 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000472 return false;
473
Dan Gohman97135e12008-09-26 19:15:30 +0000474 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000475 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000476 (AM.Base.Reg != 0 || AM.IndexReg != 0))
477 return false;
478
Dan Gohmane9865942009-02-23 22:03:08 +0000479 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000480 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000481 if (GVar->isThreadLocal())
482 return false;
483
Chris Lattnerff7727f2009-07-09 06:41:35 +0000484 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000485 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000486
Chris Lattner0d786dd2009-07-10 07:48:51 +0000487 // Allow the subtarget to classify the global.
488 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
489
490 // If this reference is relative to the pic base, set it now.
491 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000492 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000493 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000494 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000495
496 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000497 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000498 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000499 if (Subtarget->isPICStyleRIPRel()) {
500 // Use rip-relative addressing if we can. Above we verified that the
501 // base and index registers are unused.
502 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
503 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000504 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000505 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000506 return true;
507 }
508
Chris Lattner0d786dd2009-07-10 07:48:51 +0000509 // Ok, we need to do a load from a stub. If we've already loaded from this
510 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000511 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
512 unsigned LoadReg;
513 if (I != LocalValueMap.end() && I->second != 0) {
514 LoadReg = I->second;
515 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000516 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000517 unsigned Opc = 0;
518 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000519 X86AddressMode StubAM;
520 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000521 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000522 StubAM.GVOpFlags = GVFlags;
523
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000525 Opc = X86::MOV64rm;
526 RC = X86::GR64RegisterClass;
527
Chris Lattner0d786dd2009-07-10 07:48:51 +0000528 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000529 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000530 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000531 Opc = X86::MOV32rm;
532 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000533 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000534
535 LoadReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000536 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
537 DL, TII.get(Opc), LoadReg), StubAM);
Chris Lattnerff7727f2009-07-09 06:41:35 +0000538
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000539 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000540 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000541 }
Chris Lattner18c59872009-06-27 04:16:01 +0000542
Chris Lattnerff7727f2009-07-09 06:41:35 +0000543 // Now construct the final address. Note that the Disp, Scale,
544 // and Index values may already be set here.
545 AM.Base.Reg = LoadReg;
546 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000547 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000548 }
549
Dan Gohman97135e12008-09-26 19:15:30 +0000550 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000551 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000552 if (AM.Base.Reg == 0) {
553 AM.Base.Reg = getRegForValue(V);
554 return AM.Base.Reg != 0;
555 }
556 if (AM.IndexReg == 0) {
557 assert(AM.Scale == 1 && "Scale with no index!");
558 AM.IndexReg = getRegForValue(V);
559 return AM.IndexReg != 0;
560 }
561 }
562
563 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000564}
565
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566/// X86SelectCallAddress - Attempt to fill in an address from the given value.
567///
Dan Gohman46510a72010-04-15 01:51:59 +0000568bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
569 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000571 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000572 Opcode = I->getOpcode();
573 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000574 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000575 Opcode = C->getOpcode();
576 U = C;
577 }
578
579 switch (Opcode) {
580 default: break;
581 case Instruction::BitCast:
582 // Look past bitcasts.
583 return X86SelectCallAddress(U->getOperand(0), AM);
584
585 case Instruction::IntToPtr:
586 // Look past no-op inttoptrs.
587 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
588 return X86SelectCallAddress(U->getOperand(0), AM);
589 break;
590
591 case Instruction::PtrToInt:
592 // Look past no-op ptrtoints.
593 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
594 return X86SelectCallAddress(U->getOperand(0), AM);
595 break;
596 }
597
598 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000599 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000601 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 return false;
603
604 // RIP-relative addresses can't have additional register operands.
605 if (Subtarget->isPICStyleRIPRel() &&
606 (AM.Base.Reg != 0 || AM.IndexReg != 0))
607 return false;
608
Chris Lattner754b7652009-07-10 05:48:03 +0000609 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000610 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000611 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000612 return false;
613
614 // Okay, we've committed to selecting this global. Set up the basic address.
615 AM.GV = GV;
616
Chris Lattnere6c07b52009-07-10 05:45:15 +0000617 // No ABI requires an extra load for anything other than DLLImport, which
618 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000619 if (Subtarget->isPICStyleRIPRel()) {
620 // Use rip-relative addressing if we can. Above we verified that the
621 // base and index registers are unused.
622 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
623 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000624 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000625 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
626 } else if (Subtarget->isPICStyleGOT()) {
627 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000628 }
629
Chris Lattner0aa43de2009-07-10 05:33:42 +0000630 return true;
631 }
632
633 // If all else fails, try to materialize the value in a register.
634 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
635 if (AM.Base.Reg == 0) {
636 AM.Base.Reg = getRegForValue(V);
637 return AM.Base.Reg != 0;
638 }
639 if (AM.IndexReg == 0) {
640 assert(AM.Scale == 1 && "Scale with no index!");
641 AM.IndexReg = getRegForValue(V);
642 return AM.IndexReg != 0;
643 }
644 }
645
646 return false;
647}
648
649
Owen Andersona3971df2008-09-04 07:08:58 +0000650/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000651bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000652 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000653 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000654 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000655
Dan Gohman0586d912008-09-10 20:11:02 +0000656 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000657 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000658 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000659
Chris Lattner438949a2008-10-15 05:30:52 +0000660 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000661}
662
Evan Cheng8b19e562008-09-03 06:44:39 +0000663/// X86SelectLoad - Select and emit code to implement load instructions.
664///
Dan Gohman46510a72010-04-15 01:51:59 +0000665bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000666 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000667 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000668 return false;
669
Dan Gohman0586d912008-09-10 20:11:02 +0000670 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000671 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000672 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000673
Evan Cheng0de588f2008-09-05 21:00:03 +0000674 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000675 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000676 UpdateValueMap(I, ResultReg);
677 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000678 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000679 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000680}
681
Owen Andersone50ed302009-08-10 22:56:29 +0000682static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000684 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 case MVT::i8: return X86::CMP8rr;
686 case MVT::i16: return X86::CMP16rr;
687 case MVT::i32: return X86::CMP32rr;
688 case MVT::i64: return X86::CMP64rr;
689 case MVT::f32: return X86::UCOMISSrr;
690 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000691 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000692}
693
Chris Lattner0e13c782008-10-15 04:13:29 +0000694/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
695/// of the comparison, return an opcode that works for the compare (e.g.
696/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000697static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000699 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000700 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 case MVT::i8: return X86::CMP8ri;
702 case MVT::i16: return X86::CMP16ri;
703 case MVT::i32: return X86::CMP32ri;
704 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000705 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
706 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000707 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000708 return X86::CMP64ri32;
709 return 0;
710 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000711}
712
Dan Gohman46510a72010-04-15 01:51:59 +0000713bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
714 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000715 unsigned Op0Reg = getRegForValue(Op0);
716 if (Op0Reg == 0) return false;
717
Chris Lattnerd53886b2008-10-15 05:18:04 +0000718 // Handle 'null' like i32/i64 0.
719 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000720 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000721
Chris Lattner9a08a612008-10-15 04:26:38 +0000722 // We have two options: compare with register or immediate. If the RHS of
723 // the compare is an immediate that we can fold into this compare, use
724 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000725 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000726 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000727 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
728 .addReg(Op0Reg)
729 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000730 return true;
731 }
732 }
733
734 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
735 if (CompareOpc == 0) return false;
736
737 unsigned Op1Reg = getRegForValue(Op1);
738 if (Op1Reg == 0) return false;
Dan Gohmaneabaed22010-07-07 16:47:08 +0000739 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
740 .addReg(Op0Reg)
741 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000742
743 return true;
744}
745
Dan Gohman46510a72010-04-15 01:51:59 +0000746bool X86FastISel::X86SelectCmp(const Instruction *I) {
747 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000748
Owen Andersone50ed302009-08-10 22:56:29 +0000749 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000750 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000751 return false;
752
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000753 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000754 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000755 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000756 switch (CI->getPredicate()) {
757 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000758 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
759 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000760
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000761 unsigned EReg = createResultReg(&X86::GR8RegClass);
762 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
764 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
765 TII.get(X86::SETNPr), NPReg);
766 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000767 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000768 UpdateValueMap(I, ResultReg);
769 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000770 }
771 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000772 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
773 return false;
774
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000775 unsigned NEReg = createResultReg(&X86::GR8RegClass);
776 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
778 TII.get(X86::SETNEr), NEReg);
779 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
780 TII.get(X86::SETPr), PReg);
781 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
782 TII.get(X86::OR8rr), ResultReg)
783 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000784 UpdateValueMap(I, ResultReg);
785 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000786 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000787 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
788 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
789 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
790 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
791 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
792 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
793 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
794 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
795 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
796 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
797 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
798 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
799
800 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
801 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
802 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
803 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
804 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
805 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
806 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
807 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
808 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
809 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000810 default:
811 return false;
812 }
813
Dan Gohman46510a72010-04-15 01:51:59 +0000814 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000815 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000816 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000817
Chris Lattner9a08a612008-10-15 04:26:38 +0000818 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000819 if (!X86FastEmitCompare(Op0, Op1, VT))
820 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000821
Dan Gohmaneabaed22010-07-07 16:47:08 +0000822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000823 UpdateValueMap(I, ResultReg);
824 return true;
825}
Evan Cheng8b19e562008-09-03 06:44:39 +0000826
Dan Gohman46510a72010-04-15 01:51:59 +0000827bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000828 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000829 if (I->getType()->isIntegerTy(8) &&
830 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000831 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000832 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000833 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000834 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000835 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000836 UpdateValueMap(I, ResultReg);
837 return true;
838 }
839
840 return false;
841}
842
Chris Lattner9a08a612008-10-15 04:26:38 +0000843
Dan Gohman46510a72010-04-15 01:51:59 +0000844bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000845 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000846 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000847 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000848 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
849 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000850
Dan Gohmand98d6202008-10-02 22:15:21 +0000851 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000852 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000853 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000854 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000855
Dan Gohmand98d6202008-10-02 22:15:21 +0000856 // Try to take advantage of fallthrough opportunities.
857 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmaneabaed22010-07-07 16:47:08 +0000858 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000859 std::swap(TrueMBB, FalseMBB);
860 Predicate = CmpInst::getInversePredicate(Predicate);
861 }
862
Chris Lattner871d2462008-10-15 03:58:05 +0000863 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
864 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
865
Dan Gohmand98d6202008-10-02 22:15:21 +0000866 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000867 case CmpInst::FCMP_OEQ:
868 std::swap(TrueMBB, FalseMBB);
869 Predicate = CmpInst::FCMP_UNE;
870 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000871 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
872 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
873 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
874 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
875 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
876 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
877 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
878 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
879 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
880 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
881 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
882 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
883 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000884
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000885 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
886 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
887 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
888 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
889 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
890 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
891 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
892 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
893 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
894 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000895 default:
896 return false;
897 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000898
Dan Gohman46510a72010-04-15 01:51:59 +0000899 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000900 if (SwapArgs)
901 std::swap(Op0, Op1);
902
Chris Lattner9a08a612008-10-15 04:26:38 +0000903 // Emit a compare of the LHS and RHS, setting the flags.
904 if (!X86FastEmitCompare(Op0, Op1, VT))
905 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000906
Dan Gohmaneabaed22010-07-07 16:47:08 +0000907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
908 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000909
910 if (Predicate == CmpInst::FCMP_UNE) {
911 // X86 requires a second branch to handle UNE (and OEQ,
912 // which is mapped to UNE above).
Dan Gohmaneabaed22010-07-07 16:47:08 +0000913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
914 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000915 }
916
Stuart Hastings3bf91252010-06-17 22:43:56 +0000917 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000918 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000919 return true;
920 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000921 } else if (ExtractValueInst *EI =
922 dyn_cast<ExtractValueInst>(BI->getCondition())) {
923 // Check to see if the branch instruction is from an "arithmetic with
924 // overflow" intrinsic. The main way these intrinsics are used is:
925 //
926 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
927 // %sum = extractvalue { i32, i1 } %t, 0
928 // %obit = extractvalue { i32, i1 } %t, 1
929 // br i1 %obit, label %overflow, label %normal
930 //
Dan Gohman653456c2009-01-07 00:15:08 +0000931 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000932 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000933 // looking for the SETO/SETB instruction. If an instruction modifies the
934 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
935 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000936 if (const IntrinsicInst *CI =
937 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000938 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
939 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
940 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +0000941 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000942
Chris Lattnera9a42252009-04-12 07:36:01 +0000943 for (MachineBasicBlock::const_reverse_iterator
Dan Gohmaneabaed22010-07-07 16:47:08 +0000944 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
945 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000946 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000947
Evan Cheng1015ba72010-05-21 20:53:24 +0000948 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000949 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000950
Chris Lattnera9a42252009-04-12 07:36:01 +0000951 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
952 Reg = Src;
953 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000954 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000955
Chris Lattnera9a42252009-04-12 07:36:01 +0000956 SetMI = &MI;
957 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000958 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000959
Chris Lattnera9a42252009-04-12 07:36:01 +0000960 const TargetInstrDesc &TID = MI.getDesc();
961 if (TID.hasUnmodeledSideEffects() ||
962 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
963 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000964 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000965
966 if (SetMI) {
967 unsigned OpCode = SetMI->getOpcode();
968
969 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000970 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
971 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000972 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000973 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000974 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000975 return true;
976 }
Bill Wendling9a901322008-12-10 19:44:24 +0000977 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000978 }
979 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000980 }
981
982 // Otherwise do a clumsy setcc and re-test it.
983 unsigned OpReg = getRegForValue(BI->getCondition());
984 if (OpReg == 0) return false;
985
Dan Gohmaneabaed22010-07-07 16:47:08 +0000986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
987 .addReg(OpReg).addReg(OpReg);
988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
989 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000990 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000991 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000992 return true;
993}
994
Dan Gohman46510a72010-04-15 01:51:59 +0000995bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000996 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000997 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000998 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000999 CReg = X86::CL;
1000 RC = &X86::GR8RegClass;
1001 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001002 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1003 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1004 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001005 default: return false;
1006 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001007 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001008 CReg = X86::CX;
1009 RC = &X86::GR16RegClass;
1010 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001011 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1012 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1013 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001014 default: return false;
1015 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001016 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001017 CReg = X86::ECX;
1018 RC = &X86::GR32RegClass;
1019 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001020 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1021 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1022 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001023 default: return false;
1024 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001025 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001026 CReg = X86::RCX;
1027 RC = &X86::GR64RegClass;
1028 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001029 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1030 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1031 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001032 default: return false;
1033 }
1034 } else {
1035 return false;
1036 }
1037
Owen Andersone50ed302009-08-10 22:56:29 +00001038 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001040 return false;
1041
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001042 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1043 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001044
1045 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001046 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001047 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001049 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001050 UpdateValueMap(I, ResultReg);
1051 return true;
1052 }
1053
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001054 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1055 if (Op1Reg == 0) return false;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001056 TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1057 CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001058
1059 // The shift instruction uses X86::CL. If we defined a super-register
1060 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1061 // we're doing here.
1062 if (CReg != X86::CL)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1064 TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001065 .addReg(CReg).addImm(X86::sub_8bit);
Dan Gohman145b8282008-10-07 21:50:36 +00001066
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001067 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1069 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001070 UpdateValueMap(I, ResultReg);
1071 return true;
1072}
1073
Dan Gohman46510a72010-04-15 01:51:59 +00001074bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001075 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001077 return false;
1078
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001079 unsigned Opc = 0;
1080 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001082 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001083 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001085 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001086 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001088 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001089 RC = &X86::GR64RegClass;
1090 } else {
1091 return false;
1092 }
1093
1094 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1095 if (Op0Reg == 0) return false;
1096 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1097 if (Op1Reg == 0) return false;
1098 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1099 if (Op2Reg == 0) return false;
1100
Dan Gohmaneabaed22010-07-07 16:47:08 +00001101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1102 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1105 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001106 UpdateValueMap(I, ResultReg);
1107 return true;
1108}
1109
Dan Gohman46510a72010-04-15 01:51:59 +00001110bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001111 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001112 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001113 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001114 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001115 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001116 unsigned OpReg = getRegForValue(V);
1117 if (OpReg == 0) return false;
1118 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1120 TII.get(X86::CVTSS2SDrr), ResultReg)
1121 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001122 UpdateValueMap(I, ResultReg);
1123 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001124 }
1125 }
1126
1127 return false;
1128}
1129
Dan Gohman46510a72010-04-15 01:51:59 +00001130bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001131 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001132 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001133 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001134 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001135 unsigned OpReg = getRegForValue(V);
1136 if (OpReg == 0) return false;
1137 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1139 TII.get(X86::CVTSD2SSrr), ResultReg)
1140 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001141 UpdateValueMap(I, ResultReg);
1142 return true;
1143 }
1144 }
1145 }
1146
1147 return false;
1148}
1149
Dan Gohman46510a72010-04-15 01:51:59 +00001150bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001151 if (Subtarget->is64Bit())
1152 // All other cases should be handled by the tblgen generated code.
1153 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001154 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1155 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001156
1157 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001159 // All other cases should be handled by the tblgen generated code.
1160 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001161 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001162 // All other cases should be handled by the tblgen generated code.
1163 return false;
1164
1165 unsigned InputReg = getRegForValue(I->getOperand(0));
1166 if (!InputReg)
1167 // Unhandled operand. Halt "fast" selection and bail.
1168 return false;
1169
Dan Gohman62417622009-04-27 16:33:14 +00001170 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1172 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001173 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001174 unsigned CopyReg = createResultReg(CopyRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CopyOpc), CopyReg)
1176 .addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001177
1178 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001180 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001181 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001182 if (!ResultReg)
1183 return false;
1184
1185 UpdateValueMap(I, ResultReg);
1186 return true;
1187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1190 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1191 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001192
Dan Gohman46510a72010-04-15 01:51:59 +00001193 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001194 switch (CI->getIntrinsicID()) {
1195 default: break;
1196 case Intrinsic::sadd_with_overflow:
1197 case Intrinsic::uadd_with_overflow:
1198 // Cheat a little. We know that the registers for "add" and "seto" are
1199 // allocated sequentially. However, we only keep track of the register
1200 // for "add" in the value map. Use extractvalue's index to get the
1201 // correct register for "seto".
1202 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1203 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001204 }
1205 }
1206
1207 return false;
1208}
1209
Dan Gohman46510a72010-04-15 01:51:59 +00001210bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001211 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001212 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001213 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001214 case Intrinsic::stackprotector: {
1215 // Emit code inline code to store the stack guard onto the stack.
1216 EVT PtrTy = TLI.getPointerTy();
1217
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001218 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1219 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001220
1221 // Grab the frame index.
1222 X86AddressMode AM;
1223 if (!X86SelectAddress(Slot, AM)) return false;
1224
Eric Christopher88dee302010-03-18 21:58:33 +00001225 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1226
Eric Christopher07754c22010-03-18 20:27:26 +00001227 return true;
1228 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001229 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001230 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001231 const Type *Ty = I.getCalledFunction()->getReturnType();
1232
1233 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1234
1235 EVT VT;
1236 if (!isTypeLegal(Ty, VT))
1237 return false;
1238
1239 unsigned OpC = 0;
1240 if (VT == MVT::i32)
1241 OpC = X86::MOV32ri;
1242 else if (VT == MVT::i64)
1243 OpC = X86::MOV64ri;
1244 else
1245 return false;
1246
1247 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +00001248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001249 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001250 UpdateValueMap(&I, ResultReg);
1251 return true;
1252 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001253 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001254 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001255 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001256 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001257 if (!X86SelectAddress(DI->getAddress(), AM))
1258 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001259 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001260 // FIXME may need to add RegState::Debug to any registers produced,
1261 // although ESP/EBP should be the only ones at the moment.
Dan Gohmaneabaed22010-07-07 16:47:08 +00001262 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1263 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001264 return true;
1265 }
Eric Christopher77f79892010-01-18 22:11:29 +00001266 case Intrinsic::trap: {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001268 return true;
1269 }
Bill Wendling52370a12008-12-09 02:42:50 +00001270 case Intrinsic::sadd_with_overflow:
1271 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001272 // Replace "add with overflow" intrinsics with an "add" instruction followed
1273 // by a seto/setc instruction. Later on, when the "extractvalue"
1274 // instructions are encountered, we use the fact that two registers were
1275 // created sequentially to get the correct registers for the "sum" and the
1276 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001277 const Function *Callee = I.getCalledFunction();
1278 const Type *RetTy =
1279 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1280
Owen Andersone50ed302009-08-10 22:56:29 +00001281 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001282 if (!isTypeLegal(RetTy, VT))
1283 return false;
1284
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001285 const Value *Op1 = I.getArgOperand(0);
1286 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001287 unsigned Reg1 = getRegForValue(Op1);
1288 unsigned Reg2 = getRegForValue(Op2);
1289
1290 if (Reg1 == 0 || Reg2 == 0)
1291 // FIXME: Handle values *not* in registers.
1292 return false;
1293
1294 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001296 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001298 OpC = X86::ADD64rr;
1299 else
1300 return false;
1301
1302 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +00001303 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1304 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001305 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001306
Chris Lattner8d57b772009-04-12 07:51:14 +00001307 // If the add with overflow is an intra-block value then we just want to
1308 // create temporaries for it like normal. If it is a cross-block value then
1309 // UpdateValueMap will return the cross-block register used. Since we
1310 // *really* want the value to be live in the register pair known by
1311 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1312 // the cross block case. In the non-cross-block case, we should just make
1313 // another register for the value.
1314 if (DestReg1 != ResultReg)
1315 ResultReg = DestReg1+1;
1316 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001318
Chris Lattnera9a42252009-04-12 07:36:01 +00001319 unsigned Opc = X86::SETBr;
1320 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1321 Opc = X86::SETOr;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001323 return true;
1324 }
1325 }
1326}
1327
Dan Gohman46510a72010-04-15 01:51:59 +00001328bool X86FastISel::X86SelectCall(const Instruction *I) {
1329 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001330 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001331
1332 // Can't handle inline asm yet.
1333 if (isa<InlineAsm>(Callee))
1334 return false;
1335
Bill Wendling52370a12008-12-09 02:42:50 +00001336 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001337 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001338 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001339
Evan Chengf3d4efe2008-09-07 09:09:33 +00001340 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001341 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001342 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001343 if (CC != CallingConv::C &&
1344 CC != CallingConv::Fast &&
1345 CC != CallingConv::X86_FastCall)
1346 return false;
1347
Evan Cheng381993f2010-01-27 00:00:57 +00001348 // fastcc with -tailcallopt is intended to provide a guaranteed
1349 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001350 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001351 return false;
1352
Evan Chengf3d4efe2008-09-07 09:09:33 +00001353 // Let SDISel handle vararg functions.
1354 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1355 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1356 if (FTy->isVarArg())
1357 return false;
1358
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001359 // Fast-isel doesn't know about callee-pop yet.
1360 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1361 return false;
1362
Evan Chengf3d4efe2008-09-07 09:09:33 +00001363 // Handle *simple* calls for now.
1364 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001365 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001366 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001368 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001369 return false;
1370
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001371 // Materialize callee address in a register. FIXME: GV address can be
1372 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001373 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001374 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001375 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001376 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001377 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001378 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001379 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001380 } else if (CalleeAM.Base.Reg != 0) {
1381 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001382 } else
1383 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001384
Evan Chengdebdea02008-09-08 17:15:42 +00001385 // Allow calls which produce i1 results.
1386 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 if (RetVT == MVT::i1) {
1388 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001389 AndToI1 = true;
1390 }
1391
Evan Chengf3d4efe2008-09-07 09:09:33 +00001392 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001393 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001394 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001395 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001396 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001397 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001398 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001399 ArgVTs.reserve(CS.arg_size());
1400 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001401 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001402 i != e; ++i) {
1403 unsigned Arg = getRegForValue(*i);
1404 if (Arg == 0)
1405 return false;
1406 ISD::ArgFlagsTy Flags;
1407 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001408 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001409 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001410 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001411 Flags.setZExt();
1412
1413 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001414 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1415 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1416 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1417 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001418 return false;
1419
1420 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001421 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001422 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001423 return false;
1424 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1425 Flags.setOrigAlign(OriginalAlignment);
1426
1427 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001428 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001429 ArgVTs.push_back(ArgVT);
1430 ArgFlags.push_back(Flags);
1431 }
1432
1433 // Analyze operands of the call, assigning locations to each operand.
1434 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001435 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001436
1437 // Allocate shadow area for Win64
1438 if (Subtarget->isTargetWin64()) {
1439 CCInfo.AllocateStack(32, 8);
1440 }
1441
Evan Chengf3d4efe2008-09-07 09:09:33 +00001442 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1443
1444 // Get a count of how many bytes are to be pushed on the stack.
1445 unsigned NumBytes = CCInfo.getNextStackOffset();
1446
1447 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001448 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohmaneabaed22010-07-07 16:47:08 +00001449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1450 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001451
Chris Lattner438949a2008-10-15 05:30:52 +00001452 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001453 // copies / loads.
1454 SmallVector<unsigned, 4> RegArgs;
1455 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1456 CCValAssign &VA = ArgLocs[i];
1457 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001458 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001459
1460 // Promote the value if needed.
1461 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001462 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001463 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001464 case CCValAssign::SExt: {
1465 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1466 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001467 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001468 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001469 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001470 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001471 }
1472 case CCValAssign::ZExt: {
1473 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1474 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001475 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001476 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001477 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001478 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001479 }
1480 case CCValAssign::AExt: {
1481 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1482 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001483 if (!Emitted)
1484 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001485 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001486 if (!Emitted)
1487 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1488 Arg, ArgVT, Arg);
1489
Chris Lattnera33649e2008-12-19 17:03:38 +00001490 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001491 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001492 break;
1493 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001494 case CCValAssign::BCvt: {
1495 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001496 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001497 assert(BC != 0 && "Failed to emit a bitcast!");
1498 Arg = BC;
1499 ArgVT = VA.getLocVT();
1500 break;
1501 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001502 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001503
1504 if (VA.isRegLoc()) {
1505 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001506 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1507 VA.getLocReg(), Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001508 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001509 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001510 RegArgs.push_back(VA.getLocReg());
1511 } else {
1512 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001513 X86AddressMode AM;
1514 AM.Base.Reg = StackPtr;
1515 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001516 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001517
1518 // If this is a really simple value, emit this with the Value* version of
1519 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1520 // can cause us to reevaluate the argument.
1521 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1522 X86FastEmitStore(ArgVT, ArgVal, AM);
1523 else
1524 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525 }
1526 }
1527
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001528 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1529 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001530 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001531 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohmana4160c32010-07-07 16:29:44 +00001532 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001533 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1534 X86::EBX, Base, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001535 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001536 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001537 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001538
Evan Chengf3d4efe2008-09-07 09:09:33 +00001539 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001540 MachineInstrBuilder MIB;
1541 if (CalleeOp) {
1542 // Register-indirect call.
1543 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001544 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1545 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001546
1547 } else {
1548 // Direct call.
1549 assert(GV && "Not a direct call");
1550 unsigned CallOpc =
1551 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1552
1553 // See if we need any target-specific flags on the GV operand.
1554 unsigned char OpFlags = 0;
1555
1556 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1557 // external symbols most go through the PLT in PIC mode. If the symbol
1558 // has hidden or protected visibility, or if it is static or local, then
1559 // we don't need to use the PLT - we can directly call it.
1560 if (Subtarget->isTargetELF() &&
1561 TM.getRelocationModel() == Reloc::PIC_ &&
1562 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1563 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001564 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001565 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1566 Subtarget->getDarwinVers() < 9) {
1567 // PC-relative references to external symbols should go through $stub,
1568 // unless we're building with the leopard linker or later, which
1569 // automatically synthesizes these stubs.
1570 OpFlags = X86II::MO_DARWIN_STUB;
1571 }
1572
1573
Dan Gohmaneabaed22010-07-07 16:47:08 +00001574 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1575 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001576 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001577
1578 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001579 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001580 MIB.addReg(X86::EBX);
1581
Evan Chengf3d4efe2008-09-07 09:09:33 +00001582 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001583 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1584 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001585
1586 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001587 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohmaneabaed22010-07-07 16:47:08 +00001588 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1589 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001590
1591 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001592 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001594 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001595 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001596 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1597
1598 // Copy all of the result registers out of their specified physreg.
1599 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001600 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001601 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1602 TargetRegisterClass *SrcRC = DstRC;
1603
1604 // If this is a call to a function that returns an fp value on the x87 fp
1605 // stack, but where we prefer to use the value in xmm registers, copy it
1606 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1607 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1608 RVLocs[0].getLocReg() == X86::ST1) &&
1609 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001611 SrcRC = X86::RSTRegisterClass;
1612 DstRC = X86::RFP80RegisterClass;
1613 }
1614
1615 unsigned ResultReg = createResultReg(DstRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001616 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt, ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001617 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001618 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001619 Emitted = true;
Dan Gohmandb497122010-06-18 23:28:01 +00001620 UsedRegs.push_back(RVLocs[0].getLocReg());
1621
Evan Chengf3d4efe2008-09-07 09:09:33 +00001622 if (CopyVT != RVLocs[0].getValVT()) {
1623 // Round the F80 the right size, which also moves to the appropriate xmm
1624 // register. This is accomplished by storing the F80 value in memory and
1625 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001628 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001629 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001630 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1631 TII.get(Opc)), FI)
1632 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001634 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001635 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001636 ResultReg = createResultReg(DstRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001637 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1638 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001639 }
1640
Evan Chengdebdea02008-09-08 17:15:42 +00001641 if (AndToI1) {
1642 // Mask out all but lowest bit for some call which produces an i1.
1643 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001644 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001645 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001646 ResultReg = AndResult;
1647 }
1648
Evan Chengf3d4efe2008-09-07 09:09:33 +00001649 UpdateValueMap(I, ResultReg);
1650 }
1651
Dan Gohmandb497122010-06-18 23:28:01 +00001652 // Set all unused physreg defs as dead.
1653 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1654
Evan Chengf3d4efe2008-09-07 09:09:33 +00001655 return true;
1656}
1657
1658
Dan Gohman99b21822008-08-28 23:21:34 +00001659bool
Dan Gohman46510a72010-04-15 01:51:59 +00001660X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001661 switch (I->getOpcode()) {
1662 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001663 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001664 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001665 case Instruction::Store:
1666 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001667 case Instruction::ICmp:
1668 case Instruction::FCmp:
1669 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001670 case Instruction::ZExt:
1671 return X86SelectZExt(I);
1672 case Instruction::Br:
1673 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001674 case Instruction::Call:
1675 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001676 case Instruction::LShr:
1677 case Instruction::AShr:
1678 case Instruction::Shl:
1679 return X86SelectShift(I);
1680 case Instruction::Select:
1681 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001682 case Instruction::Trunc:
1683 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001684 case Instruction::FPExt:
1685 return X86SelectFPExt(I);
1686 case Instruction::FPTrunc:
1687 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001688 case Instruction::ExtractValue:
1689 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001690 case Instruction::IntToPtr: // Deliberate fall-through.
1691 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001692 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1693 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001694 if (DstVT.bitsGT(SrcVT))
1695 return X86SelectZExt(I);
1696 if (DstVT.bitsLT(SrcVT))
1697 return X86SelectTrunc(I);
1698 unsigned Reg = getRegForValue(I->getOperand(0));
1699 if (Reg == 0) return false;
1700 UpdateValueMap(I, Reg);
1701 return true;
1702 }
Dan Gohman99b21822008-08-28 23:21:34 +00001703 }
1704
1705 return false;
1706}
1707
Dan Gohman46510a72010-04-15 01:51:59 +00001708unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001709 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001710 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001711 return false;
1712
1713 // Get opcode and regclass of the output for the given load instruction.
1714 unsigned Opc = 0;
1715 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001717 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001719 Opc = X86::MOV8rm;
1720 RC = X86::GR8RegisterClass;
1721 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001723 Opc = X86::MOV16rm;
1724 RC = X86::GR16RegisterClass;
1725 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001727 Opc = X86::MOV32rm;
1728 RC = X86::GR32RegisterClass;
1729 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001731 // Must be in x86-64 mode.
1732 Opc = X86::MOV64rm;
1733 RC = X86::GR64RegisterClass;
1734 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001736 if (Subtarget->hasSSE1()) {
1737 Opc = X86::MOVSSrm;
1738 RC = X86::FR32RegisterClass;
1739 } else {
1740 Opc = X86::LD_Fp32m;
1741 RC = X86::RFP32RegisterClass;
1742 }
1743 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001745 if (Subtarget->hasSSE2()) {
1746 Opc = X86::MOVSDrm;
1747 RC = X86::FR64RegisterClass;
1748 } else {
1749 Opc = X86::LD_Fp64m;
1750 RC = X86::RFP64RegisterClass;
1751 }
1752 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001754 // No f80 support yet.
1755 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001756 }
1757
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001758 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001759 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001760 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001761 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001763 Opc = X86::LEA32r;
1764 else
1765 Opc = X86::LEA64r;
1766 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001767 addLeaAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1768 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001769 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001770 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001771 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001772 }
1773
Owen Anderson3b217c62008-09-06 01:11:01 +00001774 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001775 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001776 if (Align == 0) {
1777 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001778 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001779 }
Owen Anderson95267a12008-09-05 00:06:23 +00001780
Dan Gohman5396c992008-09-30 01:21:32 +00001781 // x86-32 PIC requires a PIC base register for constant pools.
1782 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001783 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001784 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001785 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001786 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001787 } else if (Subtarget->isPICStyleGOT()) {
1788 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001789 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001790 } else if (Subtarget->isPICStyleRIPRel() &&
1791 TM.getCodeModel() == CodeModel::Small) {
1792 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001793 }
Dan Gohman5396c992008-09-30 01:21:32 +00001794
1795 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001796 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001797 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001798 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1799 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001800 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001801
Owen Anderson95267a12008-09-05 00:06:23 +00001802 return ResultReg;
1803}
1804
Dan Gohman46510a72010-04-15 01:51:59 +00001805unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001806 // Fail on dynamic allocas. At this point, getRegForValue has already
1807 // checked its CSE maps, so if we're here trying to handle a dynamic
1808 // alloca, we're not going to succeed. X86SelectAddress has a
1809 // check for dynamic allocas, because it's called directly from
1810 // various places, but TargetMaterializeAlloca also needs a check
1811 // in order to avoid recursion between getRegForValue,
1812 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001813 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001814 return 0;
1815
Dan Gohman0586d912008-09-10 20:11:02 +00001816 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001817 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001818 return 0;
1819 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1820 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1821 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001822 addLeaAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1823 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001824 return ResultReg;
1825}
1826
Evan Chengc3f44b02008-09-03 00:03:49 +00001827namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001828 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1829 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001830 }
Dan Gohman99b21822008-08-28 23:21:34 +00001831}