blob: 6733f80d501f82cdde8daef3a5d0067e0e272913 [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"
Owen Anderson95267a12008-09-05 00:06:23 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000033#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000034using namespace llvm;
35
Chris Lattner087fcf32009-03-08 18:44:31 +000036namespace {
37
Evan Chengc3f44b02008-09-03 00:03:49 +000038class X86FastISel : public FastISel {
39 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
40 /// make the right decision when generating code for different targets.
41 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000042
43 /// StackPtr - Register used as the stack pointer.
44 ///
45 unsigned StackPtr;
46
47 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
51 bool X86ScalarSSEf64;
52 bool X86ScalarSSEf32;
53
Evan Cheng8b19e562008-09-03 06:44:39 +000054public:
Dan Gohman3df24e62008-09-03 23:12:08 +000055 explicit X86FastISel(MachineFunction &mf,
56 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000057 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +000058 DenseMap<const AllocaInst *, int> &am,
59 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +000060#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +000061 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +000062#endif
63 )
Dan Gohmanf81eca02010-04-22 20:46:50 +000064 : FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +000065#ifndef NDEBUG
66 , cil
67#endif
68 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000069 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000070 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
71 X86ScalarSSEf64 = Subtarget->hasSSE2();
72 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000073 }
Evan Chengc3f44b02008-09-03 00:03:49 +000074
Dan Gohman46510a72010-04-15 01:51:59 +000075 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000076
Dan Gohman1adf1b02008-08-19 21:45:35 +000077#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000078
79private:
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000081
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000083
Dan Gohman46510a72010-04-15 01:51:59 +000084 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000085 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000087 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000088
Owen Andersone50ed302009-08-10 22:56:29 +000089 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000090 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
93 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000094
Dan Gohman46510a72010-04-15 01:51:59 +000095 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000096
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectFPExt(const Instruction *I);
112 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
117 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000119 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000120
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000122 return getTargetMachine()->getInstrInfo();
123 }
124 const X86TargetMachine *getTargetMachine() const {
125 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000126 }
127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000129
Dan Gohman46510a72010-04-15 01:51:59 +0000130 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000131
132 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
133 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000134 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
136 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 }
138
Owen Andersone50ed302009-08-10 22:56:29 +0000139 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000140};
Chris Lattner087fcf32009-03-08 18:44:31 +0000141
142} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000143
Owen Andersone50ed302009-08-10 22:56:29 +0000144bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000145 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // Unhandled type. Halt "fast" selection and bail.
148 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000149
Dan Gohman9b66d732008-09-30 00:48:39 +0000150 // For now, require SSE/SSE2 for performing floating-point operations,
151 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000153 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 return false;
156 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000159 // We only handle legal types. For example, on x86-32 the instruction
160 // selector contains all of the 64-bit instructions from x86-64,
161 // under the assumption that i64 won't be used if the target doesn't
162 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164}
165
166#include "X86GenCallingConv.inc"
167
168/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
169/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000170CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
171 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000172 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000173 if (CC == CallingConv::GHC)
174 return CC_X86_64_GHC;
175 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_64_C;
179 }
180
181 if (CC == CallingConv::X86_FastCall)
182 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000183 else if (CC == CallingConv::X86_ThisCall)
184 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000185 else if (CC == CallingConv::Fast)
186 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000187 else if (CC == CallingConv::GHC)
188 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000189 else
190 return CC_X86_32_C;
191}
192
Evan Cheng0de588f2008-09-05 21:00:03 +0000193/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000194/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000195/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000196bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 unsigned &ResultReg) {
198 // Get opcode and regclass of the output for the given load instruction.
199 unsigned Opc = 0;
200 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000203 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 Opc = X86::MOV8rm;
206 RC = X86::GR8RegisterClass;
207 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 Opc = X86::MOV16rm;
210 RC = X86::GR16RegisterClass;
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 Opc = X86::MOV32rm;
214 RC = X86::GR32RegisterClass;
215 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000217 // Must be in x86-64 mode.
218 Opc = X86::MOV64rm;
219 RC = X86::GR64RegisterClass;
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 if (Subtarget->hasSSE1()) {
223 Opc = X86::MOVSSrm;
224 RC = X86::FR32RegisterClass;
225 } else {
226 Opc = X86::LD_Fp32m;
227 RC = X86::RFP32RegisterClass;
228 }
229 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 if (Subtarget->hasSSE2()) {
232 Opc = X86::MOVSDrm;
233 RC = X86::FR64RegisterClass;
234 } else {
235 Opc = X86::LD_Fp64m;
236 RC = X86::RFP64RegisterClass;
237 }
238 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000240 // No f80 support yet.
241 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 }
243
244 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000245 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 return true;
247}
248
Evan Chengf3d4efe2008-09-07 09:09:33 +0000249/// X86FastEmitStore - Emit a machine instruction to store a value Val of
250/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
251/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000252/// i.e. V. Return true if it is possible.
253bool
Owen Andersone50ed302009-08-10 22:56:29 +0000254X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000255 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000256 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 switch (VT.getSimpleVT().SimpleTy) {
259 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000261 case MVT::i1: {
262 // Mask out all but lowest bit.
263 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
264 BuildMI(MBB, DL,
265 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
266 Val = AndResult;
267 }
268 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 case MVT::i8: Opc = X86::MOV8mr; break;
270 case MVT::i16: Opc = X86::MOV16mr; break;
271 case MVT::i32: Opc = X86::MOV32mr; break;
272 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
273 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000274 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000277 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000279 }
Chris Lattner438949a2008-10-15 05:30:52 +0000280
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000281 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000282 return true;
283}
284
Dan Gohman46510a72010-04-15 01:51:59 +0000285bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000286 const X86AddressMode &AM) {
287 // Handle 'null' like i32/i64 0.
288 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000289 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000290
291 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000292 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000293 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000296 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000297 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 case MVT::i8: Opc = X86::MOV8mi; break;
299 case MVT::i16: Opc = X86::MOV16mi; break;
300 case MVT::i32: Opc = X86::MOV32mi; break;
301 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000302 // Must be a 32-bit sign extended value.
303 if ((int)CI->getSExtValue() == CI->getSExtValue())
304 Opc = X86::MOV64mi32;
305 break;
306 }
307
308 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000309 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000310 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000311 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000312 return true;
313 }
314 }
315
316 unsigned ValReg = getRegForValue(Val);
317 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000318 return false;
319
320 return X86FastEmitStore(VT, ValReg, AM);
321}
322
Evan Cheng24e3a902008-09-08 06:35:17 +0000323/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
324/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
325/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000326bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
327 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000328 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000329 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
330 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000331
332 if (RR != 0) {
333 ResultReg = RR;
334 return true;
335 } else
336 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000337}
338
Dan Gohman0586d912008-09-10 20:11:02 +0000339/// X86SelectAddress - Attempt to fill in an address from the given value.
340///
Dan Gohman46510a72010-04-15 01:51:59 +0000341bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
342 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000343 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000344 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000345 // Don't walk into other basic blocks; it's possible we haven't
346 // visited them yet, so the instructions may not yet be assigned
347 // virtual registers.
348 if (MBBMap[I->getParent()] != MBB)
349 return false;
350
Dan Gohman35893082008-09-18 23:23:44 +0000351 Opcode = I->getOpcode();
352 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000353 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000354 Opcode = C->getOpcode();
355 U = C;
356 }
Dan Gohman0586d912008-09-10 20:11:02 +0000357
Chris Lattner868ee942010-06-15 19:08:40 +0000358 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
359 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000360 // Fast instruction selection doesn't support the special
361 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000362 return false;
363
Dan Gohman35893082008-09-18 23:23:44 +0000364 switch (Opcode) {
365 default: break;
366 case Instruction::BitCast:
367 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000368 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000369
370 case Instruction::IntToPtr:
371 // Look past no-op inttoptrs.
372 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000373 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000374 break;
Dan Gohman35893082008-09-18 23:23:44 +0000375
376 case Instruction::PtrToInt:
377 // Look past no-op ptrtoints.
378 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000379 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000380 break;
Dan Gohman35893082008-09-18 23:23:44 +0000381
382 case Instruction::Alloca: {
383 // Do static allocas.
384 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000385 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000386 if (SI != StaticAllocaMap.end()) {
387 AM.BaseType = X86AddressMode::FrameIndexBase;
388 AM.Base.FrameIndex = SI->second;
389 return true;
390 }
391 break;
Dan Gohman35893082008-09-18 23:23:44 +0000392 }
393
394 case Instruction::Add: {
395 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000396 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000397 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
398 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000399 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000400 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000401 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000402 }
Dan Gohman0586d912008-09-10 20:11:02 +0000403 }
Dan Gohman35893082008-09-18 23:23:44 +0000404 break;
405 }
406
407 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000408 X86AddressMode SavedAM = AM;
409
Dan Gohman35893082008-09-18 23:23:44 +0000410 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000411 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000412 unsigned IndexReg = AM.IndexReg;
413 unsigned Scale = AM.Scale;
414 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000415 // Iterate through the indices, folding what we can. Constants can be
416 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000417 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000418 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000419 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000420 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
421 const StructLayout *SL = TD.getStructLayout(STy);
422 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
423 Disp += SL->getElementOffset(Idx);
424 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000425 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000426 SmallVector<const Value *, 4> Worklist;
427 Worklist.push_back(Op);
428 do {
429 Op = Worklist.pop_back_val();
430 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
431 // Constant-offset addressing.
432 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000433 } else if (isa<AddOperator>(Op) &&
434 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
435 // An add with a constant operand. Fold the constant.
436 ConstantInt *CI =
437 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
438 Disp += CI->getSExtValue() * S;
439 // Add the other operand back to the work list.
440 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000441 } else if (IndexReg == 0 &&
442 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
443 (S == 1 || S == 2 || S == 4 || S == 8)) {
444 // Scaled-index addressing.
445 Scale = S;
446 IndexReg = getRegForGEPIndex(Op).first;
447 if (IndexReg == 0)
448 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000449 } else
450 // Unsupported.
451 goto unsupported_gep;
452 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000453 }
454 }
Dan Gohman09aae462008-09-26 20:04:15 +0000455 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000456 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000457 break;
Dan Gohman35893082008-09-18 23:23:44 +0000458 // Ok, the GEP indices were covered by constant-offset and scaled-index
459 // addressing. Update the address state and move on to examining the base.
460 AM.IndexReg = IndexReg;
461 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000462 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000463 if (X86SelectAddress(U->getOperand(0), AM))
464 return true;
465
466 // If we couldn't merge the sub value into this addr mode, revert back to
467 // our address and just match the value instead of completely failing.
468 AM = SavedAM;
469 break;
Dan Gohman35893082008-09-18 23:23:44 +0000470 unsupported_gep:
471 // Ok, the GEP indices weren't all covered.
472 break;
473 }
474 }
475
476 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000477 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000478 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000479 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000480 return false;
481
Dan Gohman97135e12008-09-26 19:15:30 +0000482 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000483 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000484 (AM.Base.Reg != 0 || AM.IndexReg != 0))
485 return false;
486
Dan Gohmane9865942009-02-23 22:03:08 +0000487 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000488 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000489 if (GVar->isThreadLocal())
490 return false;
491
Chris Lattnerff7727f2009-07-09 06:41:35 +0000492 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000493 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000494
Chris Lattner0d786dd2009-07-10 07:48:51 +0000495 // Allow the subtarget to classify the global.
496 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
497
498 // If this reference is relative to the pic base, set it now.
499 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000500 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000501 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000502 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000503
504 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000505 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000506 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000507 if (Subtarget->isPICStyleRIPRel()) {
508 // Use rip-relative addressing if we can. Above we verified that the
509 // base and index registers are unused.
510 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
511 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000512 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000513 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000514 return true;
515 }
516
Chris Lattner0d786dd2009-07-10 07:48:51 +0000517 // Ok, we need to do a load from a stub. If we've already loaded from this
518 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000519 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
520 unsigned LoadReg;
521 if (I != LocalValueMap.end() && I->second != 0) {
522 LoadReg = I->second;
523 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000524 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000525 unsigned Opc = 0;
526 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000527 X86AddressMode StubAM;
528 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000529 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000530 StubAM.GVOpFlags = GVFlags;
531
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000533 Opc = X86::MOV64rm;
534 RC = X86::GR64RegisterClass;
535
Chris Lattner0d786dd2009-07-10 07:48:51 +0000536 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000537 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000538 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000539 Opc = X86::MOV32rm;
540 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000541 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000542
543 LoadReg = createResultReg(RC);
544 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
545
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000546 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000547 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000548 }
Chris Lattner18c59872009-06-27 04:16:01 +0000549
Chris Lattnerff7727f2009-07-09 06:41:35 +0000550 // Now construct the final address. Note that the Disp, Scale,
551 // and Index values may already be set here.
552 AM.Base.Reg = LoadReg;
553 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000554 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000555 }
556
Dan Gohman97135e12008-09-26 19:15:30 +0000557 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000558 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000559 if (AM.Base.Reg == 0) {
560 AM.Base.Reg = getRegForValue(V);
561 return AM.Base.Reg != 0;
562 }
563 if (AM.IndexReg == 0) {
564 assert(AM.Scale == 1 && "Scale with no index!");
565 AM.IndexReg = getRegForValue(V);
566 return AM.IndexReg != 0;
567 }
568 }
569
570 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000571}
572
Chris Lattner0aa43de2009-07-10 05:33:42 +0000573/// X86SelectCallAddress - Attempt to fill in an address from the given value.
574///
Dan Gohman46510a72010-04-15 01:51:59 +0000575bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
576 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000577 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000578 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000579 Opcode = I->getOpcode();
580 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000581 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000582 Opcode = C->getOpcode();
583 U = C;
584 }
585
586 switch (Opcode) {
587 default: break;
588 case Instruction::BitCast:
589 // Look past bitcasts.
590 return X86SelectCallAddress(U->getOperand(0), AM);
591
592 case Instruction::IntToPtr:
593 // Look past no-op inttoptrs.
594 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
595 return X86SelectCallAddress(U->getOperand(0), AM);
596 break;
597
598 case Instruction::PtrToInt:
599 // Look past no-op ptrtoints.
600 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
601 return X86SelectCallAddress(U->getOperand(0), AM);
602 break;
603 }
604
605 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000606 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000607 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000608 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000609 return false;
610
611 // RIP-relative addresses can't have additional register operands.
612 if (Subtarget->isPICStyleRIPRel() &&
613 (AM.Base.Reg != 0 || AM.IndexReg != 0))
614 return false;
615
Chris Lattner754b7652009-07-10 05:48:03 +0000616 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000617 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000618 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000619 return false;
620
621 // Okay, we've committed to selecting this global. Set up the basic address.
622 AM.GV = GV;
623
Chris Lattnere6c07b52009-07-10 05:45:15 +0000624 // No ABI requires an extra load for anything other than DLLImport, which
625 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000626 if (Subtarget->isPICStyleRIPRel()) {
627 // Use rip-relative addressing if we can. Above we verified that the
628 // base and index registers are unused.
629 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
630 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000631 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000632 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
633 } else if (Subtarget->isPICStyleGOT()) {
634 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000635 }
636
Chris Lattner0aa43de2009-07-10 05:33:42 +0000637 return true;
638 }
639
640 // If all else fails, try to materialize the value in a register.
641 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
642 if (AM.Base.Reg == 0) {
643 AM.Base.Reg = getRegForValue(V);
644 return AM.Base.Reg != 0;
645 }
646 if (AM.IndexReg == 0) {
647 assert(AM.Scale == 1 && "Scale with no index!");
648 AM.IndexReg = getRegForValue(V);
649 return AM.IndexReg != 0;
650 }
651 }
652
653 return false;
654}
655
656
Owen Andersona3971df2008-09-04 07:08:58 +0000657/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000658bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000659 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000660 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000661 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000662
Dan Gohman0586d912008-09-10 20:11:02 +0000663 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000664 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000665 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000666
Chris Lattner438949a2008-10-15 05:30:52 +0000667 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000668}
669
Evan Cheng8b19e562008-09-03 06:44:39 +0000670/// X86SelectLoad - Select and emit code to implement load instructions.
671///
Dan Gohman46510a72010-04-15 01:51:59 +0000672bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000673 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000674 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000675 return false;
676
Dan Gohman0586d912008-09-10 20:11:02 +0000677 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000678 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000679 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000680
Evan Cheng0de588f2008-09-05 21:00:03 +0000681 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000682 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000683 UpdateValueMap(I, ResultReg);
684 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000685 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000686 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000687}
688
Owen Andersone50ed302009-08-10 22:56:29 +0000689static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000691 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 case MVT::i8: return X86::CMP8rr;
693 case MVT::i16: return X86::CMP16rr;
694 case MVT::i32: return X86::CMP32rr;
695 case MVT::i64: return X86::CMP64rr;
696 case MVT::f32: return X86::UCOMISSrr;
697 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000698 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000699}
700
Chris Lattner0e13c782008-10-15 04:13:29 +0000701/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
702/// of the comparison, return an opcode that works for the compare (e.g.
703/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000704static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000706 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000707 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 case MVT::i8: return X86::CMP8ri;
709 case MVT::i16: return X86::CMP16ri;
710 case MVT::i32: return X86::CMP32ri;
711 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000712 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
713 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000714 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000715 return X86::CMP64ri32;
716 return 0;
717 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000718}
719
Dan Gohman46510a72010-04-15 01:51:59 +0000720bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
721 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000722 unsigned Op0Reg = getRegForValue(Op0);
723 if (Op0Reg == 0) return false;
724
Chris Lattnerd53886b2008-10-15 05:18:04 +0000725 // Handle 'null' like i32/i64 0.
726 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000727 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000728
Chris Lattner9a08a612008-10-15 04:26:38 +0000729 // We have two options: compare with register or immediate. If the RHS of
730 // the compare is an immediate that we can fold into this compare, use
731 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000732 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000733 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000734 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000735 .addImm(Op1C->getSExtValue());
736 return true;
737 }
738 }
739
740 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
741 if (CompareOpc == 0) return false;
742
743 unsigned Op1Reg = getRegForValue(Op1);
744 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000745 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000746
747 return true;
748}
749
Dan Gohman46510a72010-04-15 01:51:59 +0000750bool X86FastISel::X86SelectCmp(const Instruction *I) {
751 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000752
Owen Andersone50ed302009-08-10 22:56:29 +0000753 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000754 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000755 return false;
756
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000757 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000758 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000759 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000760 switch (CI->getPredicate()) {
761 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000762 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
763 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000764
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000765 unsigned EReg = createResultReg(&X86::GR8RegClass);
766 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000767 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
768 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
769 BuildMI(MBB, DL,
770 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000771 UpdateValueMap(I, ResultReg);
772 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000773 }
774 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000775 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
776 return false;
777
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000778 unsigned NEReg = createResultReg(&X86::GR8RegClass);
779 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000780 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
781 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
782 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000783 UpdateValueMap(I, ResultReg);
784 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000785 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000786 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
787 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
788 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
789 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
790 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
791 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
792 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
793 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
794 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
795 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
796 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
797 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
798
799 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
800 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
801 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
802 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
803 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
804 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
805 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
806 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
807 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
808 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000809 default:
810 return false;
811 }
812
Dan Gohman46510a72010-04-15 01:51:59 +0000813 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000814 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000815 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000816
Chris Lattner9a08a612008-10-15 04:26:38 +0000817 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000818 if (!X86FastEmitCompare(Op0, Op1, VT))
819 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000820
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000821 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000822 UpdateValueMap(I, ResultReg);
823 return true;
824}
Evan Cheng8b19e562008-09-03 06:44:39 +0000825
Dan Gohman46510a72010-04-15 01:51:59 +0000826bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000827 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000828 if (I->getType()->isIntegerTy(8) &&
829 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000830 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000831 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000832 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000833 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000834 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000835 UpdateValueMap(I, ResultReg);
836 return true;
837 }
838
839 return false;
840}
841
Chris Lattner9a08a612008-10-15 04:26:38 +0000842
Dan Gohman46510a72010-04-15 01:51:59 +0000843bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000844 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000845 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000846 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000847 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
848 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
849
Dan Gohmand98d6202008-10-02 22:15:21 +0000850 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000851 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000852 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000853 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000854
Dan Gohmand98d6202008-10-02 22:15:21 +0000855 // Try to take advantage of fallthrough opportunities.
856 CmpInst::Predicate Predicate = CI->getPredicate();
857 if (MBB->isLayoutSuccessor(TrueMBB)) {
858 std::swap(TrueMBB, FalseMBB);
859 Predicate = CmpInst::getInversePredicate(Predicate);
860 }
861
Chris Lattner871d2462008-10-15 03:58:05 +0000862 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
863 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
864
Dan Gohmand98d6202008-10-02 22:15:21 +0000865 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000866 case CmpInst::FCMP_OEQ:
867 std::swap(TrueMBB, FalseMBB);
868 Predicate = CmpInst::FCMP_UNE;
869 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000870 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
871 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
872 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
873 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
874 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
875 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
876 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
877 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
878 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
879 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
880 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
881 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
882 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000883
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000884 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
885 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
886 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
887 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
888 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
889 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
890 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
891 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
892 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
893 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000894 default:
895 return false;
896 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000897
Dan Gohman46510a72010-04-15 01:51:59 +0000898 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000899 if (SwapArgs)
900 std::swap(Op0, Op1);
901
Chris Lattner9a08a612008-10-15 04:26:38 +0000902 // Emit a compare of the LHS and RHS, setting the flags.
903 if (!X86FastEmitCompare(Op0, Op1, VT))
904 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000905
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000906 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000907
908 if (Predicate == CmpInst::FCMP_UNE) {
909 // X86 requires a second branch to handle UNE (and OEQ,
910 // which is mapped to UNE above).
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000911 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000912 }
913
Stuart Hastings3bf91252010-06-17 22:43:56 +0000914 FastEmitBranch(FalseMBB, DL);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000915 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000916 return true;
917 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000918 } else if (ExtractValueInst *EI =
919 dyn_cast<ExtractValueInst>(BI->getCondition())) {
920 // Check to see if the branch instruction is from an "arithmetic with
921 // overflow" intrinsic. The main way these intrinsics are used is:
922 //
923 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
924 // %sum = extractvalue { i32, i1 } %t, 0
925 // %obit = extractvalue { i32, i1 } %t, 1
926 // br i1 %obit, label %overflow, label %normal
927 //
Dan Gohman653456c2009-01-07 00:15:08 +0000928 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000929 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000930 // looking for the SETO/SETB instruction. If an instruction modifies the
931 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
932 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000933 if (const IntrinsicInst *CI =
934 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000935 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
936 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
937 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +0000938 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000939
Chris Lattnera9a42252009-04-12 07:36:01 +0000940 for (MachineBasicBlock::const_reverse_iterator
941 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
942 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000943
Evan Cheng1015ba72010-05-21 20:53:24 +0000944 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000945 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000946
Chris Lattnera9a42252009-04-12 07:36:01 +0000947 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
948 Reg = Src;
949 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000950 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000951
Chris Lattnera9a42252009-04-12 07:36:01 +0000952 SetMI = &MI;
953 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000954 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000955
Chris Lattnera9a42252009-04-12 07:36:01 +0000956 const TargetInstrDesc &TID = MI.getDesc();
957 if (TID.hasUnmodeledSideEffects() ||
958 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
959 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000960 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000961
962 if (SetMI) {
963 unsigned OpCode = SetMI->getOpcode();
964
965 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000966 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
967 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000968 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000969 FastEmitBranch(FalseMBB, DL);
Chris Lattnera9a42252009-04-12 07:36:01 +0000970 MBB->addSuccessor(TrueMBB);
971 return true;
972 }
Bill Wendling9a901322008-12-10 19:44:24 +0000973 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000974 }
975 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000976 }
977
978 // Otherwise do a clumsy setcc and re-test it.
979 unsigned OpReg = getRegForValue(BI->getCondition());
980 if (OpReg == 0) return false;
981
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000982 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000983 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000984 FastEmitBranch(FalseMBB, DL);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000985 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000986 return true;
987}
988
Dan Gohman46510a72010-04-15 01:51:59 +0000989bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000990 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000991 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000992 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000993 CReg = X86::CL;
994 RC = &X86::GR8RegClass;
995 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000996 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
997 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
998 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000999 default: return false;
1000 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001001 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001002 CReg = X86::CX;
1003 RC = &X86::GR16RegClass;
1004 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001005 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1006 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1007 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001008 default: return false;
1009 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001010 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001011 CReg = X86::ECX;
1012 RC = &X86::GR32RegClass;
1013 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001014 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1015 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1016 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001017 default: return false;
1018 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001019 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001020 CReg = X86::RCX;
1021 RC = &X86::GR64RegClass;
1022 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001023 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1024 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1025 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001026 default: return false;
1027 }
1028 } else {
1029 return false;
1030 }
1031
Owen Andersone50ed302009-08-10 22:56:29 +00001032 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001034 return false;
1035
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001036 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1037 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001038
1039 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001040 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001041 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001042 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001043 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001044 UpdateValueMap(I, ResultReg);
1045 return true;
1046 }
1047
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001048 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1049 if (Op1Reg == 0) return false;
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001050 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001051
1052 // The shift instruction uses X86::CL. If we defined a super-register
1053 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1054 // we're doing here.
1055 if (CReg != X86::CL)
Chris Lattner518bb532010-02-09 19:54:29 +00001056 BuildMI(MBB, DL, TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001057 .addReg(CReg).addImm(X86::sub_8bit);
Dan Gohman145b8282008-10-07 21:50:36 +00001058
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001059 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001060 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001061 UpdateValueMap(I, ResultReg);
1062 return true;
1063}
1064
Dan Gohman46510a72010-04-15 01:51:59 +00001065bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001066 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001068 return false;
1069
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001070 unsigned Opc = 0;
1071 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001073 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001074 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001076 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001077 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001079 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001080 RC = &X86::GR64RegClass;
1081 } else {
1082 return false;
1083 }
1084
1085 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1086 if (Op0Reg == 0) return false;
1087 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1088 if (Op1Reg == 0) return false;
1089 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1090 if (Op2Reg == 0) return false;
1091
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001092 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001093 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001094 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001095 UpdateValueMap(I, ResultReg);
1096 return true;
1097}
1098
Dan Gohman46510a72010-04-15 01:51:59 +00001099bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001100 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001101 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001102 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001103 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001104 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001105 unsigned OpReg = getRegForValue(V);
1106 if (OpReg == 0) return false;
1107 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001108 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001109 UpdateValueMap(I, ResultReg);
1110 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001111 }
1112 }
1113
1114 return false;
1115}
1116
Dan Gohman46510a72010-04-15 01:51:59 +00001117bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001118 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001119 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001120 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001121 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001122 unsigned OpReg = getRegForValue(V);
1123 if (OpReg == 0) return false;
1124 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001125 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001126 UpdateValueMap(I, ResultReg);
1127 return true;
1128 }
1129 }
1130 }
1131
1132 return false;
1133}
1134
Dan Gohman46510a72010-04-15 01:51:59 +00001135bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001136 if (Subtarget->is64Bit())
1137 // All other cases should be handled by the tblgen generated code.
1138 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001139 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1140 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001141
1142 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001144 // All other cases should be handled by the tblgen generated code.
1145 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001146 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001147 // All other cases should be handled by the tblgen generated code.
1148 return false;
1149
1150 unsigned InputReg = getRegForValue(I->getOperand(0));
1151 if (!InputReg)
1152 // Unhandled operand. Halt "fast" selection and bail.
1153 return false;
1154
Dan Gohman62417622009-04-27 16:33:14 +00001155 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001156 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1157 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001158 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001159 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001160 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001161
1162 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001163 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001164 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001165 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001166 if (!ResultReg)
1167 return false;
1168
1169 UpdateValueMap(I, ResultReg);
1170 return true;
1171}
1172
Dan Gohman46510a72010-04-15 01:51:59 +00001173bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1174 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1175 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001176
Dan Gohman46510a72010-04-15 01:51:59 +00001177 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001178 switch (CI->getIntrinsicID()) {
1179 default: break;
1180 case Intrinsic::sadd_with_overflow:
1181 case Intrinsic::uadd_with_overflow:
1182 // Cheat a little. We know that the registers for "add" and "seto" are
1183 // allocated sequentially. However, we only keep track of the register
1184 // for "add" in the value map. Use extractvalue's index to get the
1185 // correct register for "seto".
1186 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1187 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001188 }
1189 }
1190
1191 return false;
1192}
1193
Dan Gohman46510a72010-04-15 01:51:59 +00001194bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001195 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001196 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001197 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001198 case Intrinsic::stackprotector: {
1199 // Emit code inline code to store the stack guard onto the stack.
1200 EVT PtrTy = TLI.getPointerTy();
1201
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001202 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1203 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001204
1205 // Grab the frame index.
1206 X86AddressMode AM;
1207 if (!X86SelectAddress(Slot, AM)) return false;
1208
Eric Christopher88dee302010-03-18 21:58:33 +00001209 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1210
Eric Christopher07754c22010-03-18 20:27:26 +00001211 return true;
1212 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001213 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001214 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001215 const Type *Ty = I.getCalledFunction()->getReturnType();
1216
1217 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1218
1219 EVT VT;
1220 if (!isTypeLegal(Ty, VT))
1221 return false;
1222
1223 unsigned OpC = 0;
1224 if (VT == MVT::i32)
1225 OpC = X86::MOV32ri;
1226 else if (VT == MVT::i64)
1227 OpC = X86::MOV64ri;
1228 else
1229 return false;
1230
1231 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1232 BuildMI(MBB, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001233 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001234 UpdateValueMap(&I, ResultReg);
1235 return true;
1236 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001237 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001238 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001239 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001240 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001241 if (!X86SelectAddress(DI->getAddress(), AM))
1242 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001243 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001244 // FIXME may need to add RegState::Debug to any registers produced,
1245 // although ESP/EBP should be the only ones at the moment.
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001246 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1247 addMetadata(DI->getVariable());
1248 return true;
1249 }
Eric Christopher77f79892010-01-18 22:11:29 +00001250 case Intrinsic::trap: {
1251 BuildMI(MBB, DL, TII.get(X86::TRAP));
1252 return true;
1253 }
Bill Wendling52370a12008-12-09 02:42:50 +00001254 case Intrinsic::sadd_with_overflow:
1255 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001256 // Replace "add with overflow" intrinsics with an "add" instruction followed
1257 // by a seto/setc instruction. Later on, when the "extractvalue"
1258 // instructions are encountered, we use the fact that two registers were
1259 // created sequentially to get the correct registers for the "sum" and the
1260 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001261 const Function *Callee = I.getCalledFunction();
1262 const Type *RetTy =
1263 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1264
Owen Andersone50ed302009-08-10 22:56:29 +00001265 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001266 if (!isTypeLegal(RetTy, VT))
1267 return false;
1268
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001269 const Value *Op1 = I.getArgOperand(0);
1270 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001271 unsigned Reg1 = getRegForValue(Op1);
1272 unsigned Reg2 = getRegForValue(Op2);
1273
1274 if (Reg1 == 0 || Reg2 == 0)
1275 // FIXME: Handle values *not* in registers.
1276 return false;
1277
1278 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001280 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001282 OpC = X86::ADD64rr;
1283 else
1284 return false;
1285
1286 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001287 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001288 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001289
Chris Lattner8d57b772009-04-12 07:51:14 +00001290 // If the add with overflow is an intra-block value then we just want to
1291 // create temporaries for it like normal. If it is a cross-block value then
1292 // UpdateValueMap will return the cross-block register used. Since we
1293 // *really* want the value to be live in the register pair known by
1294 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1295 // the cross block case. In the non-cross-block case, we should just make
1296 // another register for the value.
1297 if (DestReg1 != ResultReg)
1298 ResultReg = DestReg1+1;
1299 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001300 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001301
Chris Lattnera9a42252009-04-12 07:36:01 +00001302 unsigned Opc = X86::SETBr;
1303 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1304 Opc = X86::SETOr;
1305 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001306 return true;
1307 }
1308 }
1309}
1310
Dan Gohman46510a72010-04-15 01:51:59 +00001311bool X86FastISel::X86SelectCall(const Instruction *I) {
1312 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001313 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001314
1315 // Can't handle inline asm yet.
1316 if (isa<InlineAsm>(Callee))
1317 return false;
1318
Bill Wendling52370a12008-12-09 02:42:50 +00001319 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001320 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001321 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001322
Evan Chengf3d4efe2008-09-07 09:09:33 +00001323 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001324 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001325 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001326 if (CC != CallingConv::C &&
1327 CC != CallingConv::Fast &&
1328 CC != CallingConv::X86_FastCall)
1329 return false;
1330
Evan Cheng381993f2010-01-27 00:00:57 +00001331 // fastcc with -tailcallopt is intended to provide a guaranteed
1332 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001333 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001334 return false;
1335
Evan Chengf3d4efe2008-09-07 09:09:33 +00001336 // Let SDISel handle vararg functions.
1337 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1338 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1339 if (FTy->isVarArg())
1340 return false;
1341
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001342 // Fast-isel doesn't know about callee-pop yet.
1343 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1344 return false;
1345
Evan Chengf3d4efe2008-09-07 09:09:33 +00001346 // Handle *simple* calls for now.
1347 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001348 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001349 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001351 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001352 return false;
1353
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001354 // Materialize callee address in a register. FIXME: GV address can be
1355 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001356 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001357 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001358 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001359 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001360 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001361 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001362 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001363 } else if (CalleeAM.Base.Reg != 0) {
1364 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001365 } else
1366 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001367
Evan Chengdebdea02008-09-08 17:15:42 +00001368 // Allow calls which produce i1 results.
1369 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001370 if (RetVT == MVT::i1) {
1371 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001372 AndToI1 = true;
1373 }
1374
Evan Chengf3d4efe2008-09-07 09:09:33 +00001375 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001376 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001377 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001378 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001379 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001380 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001381 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001382 ArgVTs.reserve(CS.arg_size());
1383 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001384 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001385 i != e; ++i) {
1386 unsigned Arg = getRegForValue(*i);
1387 if (Arg == 0)
1388 return false;
1389 ISD::ArgFlagsTy Flags;
1390 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001391 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001392 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001393 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001394 Flags.setZExt();
1395
1396 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001397 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1398 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1399 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1400 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001401 return false;
1402
1403 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001404 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001405 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001406 return false;
1407 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1408 Flags.setOrigAlign(OriginalAlignment);
1409
1410 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001411 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001412 ArgVTs.push_back(ArgVT);
1413 ArgFlags.push_back(Flags);
1414 }
1415
1416 // Analyze operands of the call, assigning locations to each operand.
1417 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001418 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001419
1420 // Allocate shadow area for Win64
1421 if (Subtarget->isTargetWin64()) {
1422 CCInfo.AllocateStack(32, 8);
1423 }
1424
Evan Chengf3d4efe2008-09-07 09:09:33 +00001425 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1426
1427 // Get a count of how many bytes are to be pushed on the stack.
1428 unsigned NumBytes = CCInfo.getNextStackOffset();
1429
1430 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001431 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001432 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001433
Chris Lattner438949a2008-10-15 05:30:52 +00001434 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001435 // copies / loads.
1436 SmallVector<unsigned, 4> RegArgs;
1437 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1438 CCValAssign &VA = ArgLocs[i];
1439 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001440 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001441
1442 // Promote the value if needed.
1443 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001444 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001445 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001446 case CCValAssign::SExt: {
1447 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1448 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001449 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001450 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001451 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001452 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001453 }
1454 case CCValAssign::ZExt: {
1455 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1456 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001457 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001458 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001459 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001460 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001461 }
1462 case CCValAssign::AExt: {
1463 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1464 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001465 if (!Emitted)
1466 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001467 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001468 if (!Emitted)
1469 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1470 Arg, ArgVT, Arg);
1471
Chris Lattnera33649e2008-12-19 17:03:38 +00001472 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001473 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001474 break;
1475 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001476 case CCValAssign::BCvt: {
1477 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001478 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001479 assert(BC != 0 && "Failed to emit a bitcast!");
1480 Arg = BC;
1481 ArgVT = VA.getLocVT();
1482 break;
1483 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001484 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001485
1486 if (VA.isRegLoc()) {
1487 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1488 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001489 Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001490 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001491 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001492 RegArgs.push_back(VA.getLocReg());
1493 } else {
1494 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001495 X86AddressMode AM;
1496 AM.Base.Reg = StackPtr;
1497 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001498 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001499
1500 // If this is a really simple value, emit this with the Value* version of
1501 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1502 // can cause us to reevaluate the argument.
1503 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1504 X86FastEmitStore(ArgVT, ArgVal, AM);
1505 else
1506 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001507 }
1508 }
1509
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001510 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1511 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001512 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001513 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001514 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001515 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC,
1516 DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001517 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001518 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001519 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001520
Evan Chengf3d4efe2008-09-07 09:09:33 +00001521 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001522 MachineInstrBuilder MIB;
1523 if (CalleeOp) {
1524 // Register-indirect call.
1525 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1526 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1527
1528 } else {
1529 // Direct call.
1530 assert(GV && "Not a direct call");
1531 unsigned CallOpc =
1532 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1533
1534 // See if we need any target-specific flags on the GV operand.
1535 unsigned char OpFlags = 0;
1536
1537 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1538 // external symbols most go through the PLT in PIC mode. If the symbol
1539 // has hidden or protected visibility, or if it is static or local, then
1540 // we don't need to use the PLT - we can directly call it.
1541 if (Subtarget->isTargetELF() &&
1542 TM.getRelocationModel() == Reloc::PIC_ &&
1543 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1544 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001545 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001546 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1547 Subtarget->getDarwinVers() < 9) {
1548 // PC-relative references to external symbols should go through $stub,
1549 // unless we're building with the leopard linker or later, which
1550 // automatically synthesizes these stubs.
1551 OpFlags = X86II::MO_DARWIN_STUB;
1552 }
1553
1554
1555 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1556 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001557
1558 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001559 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001560 MIB.addReg(X86::EBX);
1561
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001563 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1564 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001565
1566 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001567 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001568 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001569
1570 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001571 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001574 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001575 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1576
1577 // Copy all of the result registers out of their specified physreg.
1578 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001579 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001580 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1581 TargetRegisterClass *SrcRC = DstRC;
1582
1583 // If this is a call to a function that returns an fp value on the x87 fp
1584 // stack, but where we prefer to use the value in xmm registers, copy it
1585 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1586 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1587 RVLocs[0].getLocReg() == X86::ST1) &&
1588 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001590 SrcRC = X86::RSTRegisterClass;
1591 DstRC = X86::RFP80RegisterClass;
1592 }
1593
1594 unsigned ResultReg = createResultReg(DstRC);
1595 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001596 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001597 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001598 Emitted = true;
Dan Gohmandb497122010-06-18 23:28:01 +00001599 UsedRegs.push_back(RVLocs[0].getLocReg());
1600
Evan Chengf3d4efe2008-09-07 09:09:33 +00001601 if (CopyVT != RVLocs[0].getValVT()) {
1602 // Round the F80 the right size, which also moves to the appropriate xmm
1603 // register. This is accomplished by storing the F80 value in memory and
1604 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001607 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001608 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001609 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001611 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001613 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001614 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001615 }
1616
Evan Chengdebdea02008-09-08 17:15:42 +00001617 if (AndToI1) {
1618 // Mask out all but lowest bit for some call which produces an i1.
1619 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001620 BuildMI(MBB, DL,
1621 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001622 ResultReg = AndResult;
1623 }
1624
Evan Chengf3d4efe2008-09-07 09:09:33 +00001625 UpdateValueMap(I, ResultReg);
1626 }
1627
Dan Gohmandb497122010-06-18 23:28:01 +00001628 // Set all unused physreg defs as dead.
1629 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1630
Evan Chengf3d4efe2008-09-07 09:09:33 +00001631 return true;
1632}
1633
1634
Dan Gohman99b21822008-08-28 23:21:34 +00001635bool
Dan Gohman46510a72010-04-15 01:51:59 +00001636X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001637 switch (I->getOpcode()) {
1638 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001639 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001640 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001641 case Instruction::Store:
1642 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001643 case Instruction::ICmp:
1644 case Instruction::FCmp:
1645 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001646 case Instruction::ZExt:
1647 return X86SelectZExt(I);
1648 case Instruction::Br:
1649 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001650 case Instruction::Call:
1651 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001652 case Instruction::LShr:
1653 case Instruction::AShr:
1654 case Instruction::Shl:
1655 return X86SelectShift(I);
1656 case Instruction::Select:
1657 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001658 case Instruction::Trunc:
1659 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001660 case Instruction::FPExt:
1661 return X86SelectFPExt(I);
1662 case Instruction::FPTrunc:
1663 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001664 case Instruction::ExtractValue:
1665 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001666 case Instruction::IntToPtr: // Deliberate fall-through.
1667 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001668 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1669 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001670 if (DstVT.bitsGT(SrcVT))
1671 return X86SelectZExt(I);
1672 if (DstVT.bitsLT(SrcVT))
1673 return X86SelectTrunc(I);
1674 unsigned Reg = getRegForValue(I->getOperand(0));
1675 if (Reg == 0) return false;
1676 UpdateValueMap(I, Reg);
1677 return true;
1678 }
Dan Gohman99b21822008-08-28 23:21:34 +00001679 }
1680
1681 return false;
1682}
1683
Dan Gohman46510a72010-04-15 01:51:59 +00001684unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001685 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001686 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001687 return false;
1688
1689 // Get opcode and regclass of the output for the given load instruction.
1690 unsigned Opc = 0;
1691 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001693 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001695 Opc = X86::MOV8rm;
1696 RC = X86::GR8RegisterClass;
1697 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001699 Opc = X86::MOV16rm;
1700 RC = X86::GR16RegisterClass;
1701 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001703 Opc = X86::MOV32rm;
1704 RC = X86::GR32RegisterClass;
1705 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001707 // Must be in x86-64 mode.
1708 Opc = X86::MOV64rm;
1709 RC = X86::GR64RegisterClass;
1710 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001712 if (Subtarget->hasSSE1()) {
1713 Opc = X86::MOVSSrm;
1714 RC = X86::FR32RegisterClass;
1715 } else {
1716 Opc = X86::LD_Fp32m;
1717 RC = X86::RFP32RegisterClass;
1718 }
1719 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001721 if (Subtarget->hasSSE2()) {
1722 Opc = X86::MOVSDrm;
1723 RC = X86::FR64RegisterClass;
1724 } else {
1725 Opc = X86::LD_Fp64m;
1726 RC = X86::RFP64RegisterClass;
1727 }
1728 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001730 // No f80 support yet.
1731 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001732 }
1733
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001734 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001735 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001736 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001737 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001739 Opc = X86::LEA32r;
1740 else
1741 Opc = X86::LEA64r;
1742 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001743 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001744 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001745 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001746 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001747 }
1748
Owen Anderson3b217c62008-09-06 01:11:01 +00001749 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001750 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001751 if (Align == 0) {
1752 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001753 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001754 }
Owen Anderson95267a12008-09-05 00:06:23 +00001755
Dan Gohman5396c992008-09-30 01:21:32 +00001756 // x86-32 PIC requires a PIC base register for constant pools.
1757 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001758 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001759 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001760 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1761 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1762 } else if (Subtarget->isPICStyleGOT()) {
1763 OpFlag = X86II::MO_GOTOFF;
1764 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1765 } else if (Subtarget->isPICStyleRIPRel() &&
1766 TM.getCodeModel() == CodeModel::Small) {
1767 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001768 }
Dan Gohman5396c992008-09-30 01:21:32 +00001769
1770 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001771 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001772 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001773 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1774 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001775
Owen Anderson95267a12008-09-05 00:06:23 +00001776 return ResultReg;
1777}
1778
Dan Gohman46510a72010-04-15 01:51:59 +00001779unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001780 // Fail on dynamic allocas. At this point, getRegForValue has already
1781 // checked its CSE maps, so if we're here trying to handle a dynamic
1782 // alloca, we're not going to succeed. X86SelectAddress has a
1783 // check for dynamic allocas, because it's called directly from
1784 // various places, but TargetMaterializeAlloca also needs a check
1785 // in order to avoid recursion between getRegForValue,
1786 // X86SelectAddrss, and TargetMaterializeAlloca.
1787 if (!StaticAllocaMap.count(C))
1788 return 0;
1789
Dan Gohman0586d912008-09-10 20:11:02 +00001790 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001791 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001792 return 0;
1793 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1794 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1795 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001796 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001797 return ResultReg;
1798}
1799
Evan Chengc3f44b02008-09-03 00:03:49 +00001800namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001801 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1802 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001803 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00001804 DenseMap<const AllocaInst *, int> &am,
1805 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001806#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00001807 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001808#endif
1809 ) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00001810 return new X86FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001811#ifndef NDEBUG
1812 , cil
1813#endif
1814 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001815 }
Dan Gohman99b21822008-08-28 23:21:34 +00001816}