blob: 53c43614131b4bcc2413d8d4391a9e132ada78f1 [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 Gohman35893082008-09-18 23:23:44 +0000345 Opcode = I->getOpcode();
346 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000347 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000348 Opcode = C->getOpcode();
349 U = C;
350 }
Dan Gohman0586d912008-09-10 20:11:02 +0000351
Dan Gohman35893082008-09-18 23:23:44 +0000352 switch (Opcode) {
353 default: break;
354 case Instruction::BitCast:
355 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000356 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000357
358 case Instruction::IntToPtr:
359 // Look past no-op inttoptrs.
360 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000361 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000362 break;
Dan Gohman35893082008-09-18 23:23:44 +0000363
364 case Instruction::PtrToInt:
365 // Look past no-op ptrtoints.
366 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000367 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000368 break;
Dan Gohman35893082008-09-18 23:23:44 +0000369
370 case Instruction::Alloca: {
371 // Do static allocas.
372 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000373 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000374 if (SI != StaticAllocaMap.end()) {
375 AM.BaseType = X86AddressMode::FrameIndexBase;
376 AM.Base.FrameIndex = SI->second;
377 return true;
378 }
379 break;
Dan Gohman35893082008-09-18 23:23:44 +0000380 }
381
382 case Instruction::Add: {
383 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000384 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000385 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
386 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000387 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000388 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000389 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000390 }
Dan Gohman0586d912008-09-10 20:11:02 +0000391 }
Dan Gohman35893082008-09-18 23:23:44 +0000392 break;
393 }
394
395 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000396 X86AddressMode SavedAM = AM;
397
Dan Gohman35893082008-09-18 23:23:44 +0000398 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000399 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000400 unsigned IndexReg = AM.IndexReg;
401 unsigned Scale = AM.Scale;
402 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000403 // Iterate through the indices, folding what we can. Constants can be
404 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000405 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000406 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000407 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000408 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
409 const StructLayout *SL = TD.getStructLayout(STy);
410 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
411 Disp += SL->getElementOffset(Idx);
412 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000413 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman46510a72010-04-15 01:51:59 +0000414 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Dan Gohman35893082008-09-18 23:23:44 +0000415 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000416 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000417 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000418 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000419 (S == 1 || S == 2 || S == 4 || S == 8)) {
420 // Scaled-index addressing.
421 Scale = S;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000422 IndexReg = getRegForGEPIndex(Op).first;
Dan Gohman35893082008-09-18 23:23:44 +0000423 if (IndexReg == 0)
424 return false;
425 } else
426 // Unsupported.
427 goto unsupported_gep;
428 }
429 }
Dan Gohman09aae462008-09-26 20:04:15 +0000430 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000431 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000432 break;
Dan Gohman35893082008-09-18 23:23:44 +0000433 // Ok, the GEP indices were covered by constant-offset and scaled-index
434 // addressing. Update the address state and move on to examining the base.
435 AM.IndexReg = IndexReg;
436 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000437 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000438 if (X86SelectAddress(U->getOperand(0), AM))
439 return true;
440
441 // If we couldn't merge the sub value into this addr mode, revert back to
442 // our address and just match the value instead of completely failing.
443 AM = SavedAM;
444 break;
Dan Gohman35893082008-09-18 23:23:44 +0000445 unsupported_gep:
446 // Ok, the GEP indices weren't all covered.
447 break;
448 }
449 }
450
451 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000452 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000453 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000454 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000455 return false;
456
Dan Gohman97135e12008-09-26 19:15:30 +0000457 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000458 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000459 (AM.Base.Reg != 0 || AM.IndexReg != 0))
460 return false;
461
Dan Gohmane9865942009-02-23 22:03:08 +0000462 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000463 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000464 if (GVar->isThreadLocal())
465 return false;
466
Chris Lattnerff7727f2009-07-09 06:41:35 +0000467 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000468 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000469
Chris Lattner0d786dd2009-07-10 07:48:51 +0000470 // Allow the subtarget to classify the global.
471 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
472
473 // If this reference is relative to the pic base, set it now.
474 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000475 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000476 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000477 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000478
479 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000480 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000481 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000482 if (Subtarget->isPICStyleRIPRel()) {
483 // Use rip-relative addressing if we can. Above we verified that the
484 // base and index registers are unused.
485 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
486 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000487 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000488 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000489 return true;
490 }
491
Chris Lattner0d786dd2009-07-10 07:48:51 +0000492 // Ok, we need to do a load from a stub. If we've already loaded from this
493 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000494 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
495 unsigned LoadReg;
496 if (I != LocalValueMap.end() && I->second != 0) {
497 LoadReg = I->second;
498 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000499 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000500 unsigned Opc = 0;
501 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000502 X86AddressMode StubAM;
503 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000504 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000505 StubAM.GVOpFlags = GVFlags;
506
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000508 Opc = X86::MOV64rm;
509 RC = X86::GR64RegisterClass;
510
Chris Lattner0d786dd2009-07-10 07:48:51 +0000511 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000512 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000513 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000514 Opc = X86::MOV32rm;
515 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000516 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000517
518 LoadReg = createResultReg(RC);
519 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
520
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000521 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000522 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000523 }
Chris Lattner18c59872009-06-27 04:16:01 +0000524
Chris Lattnerff7727f2009-07-09 06:41:35 +0000525 // Now construct the final address. Note that the Disp, Scale,
526 // and Index values may already be set here.
527 AM.Base.Reg = LoadReg;
528 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000529 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000530 }
531
Dan Gohman97135e12008-09-26 19:15:30 +0000532 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000533 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000534 if (AM.Base.Reg == 0) {
535 AM.Base.Reg = getRegForValue(V);
536 return AM.Base.Reg != 0;
537 }
538 if (AM.IndexReg == 0) {
539 assert(AM.Scale == 1 && "Scale with no index!");
540 AM.IndexReg = getRegForValue(V);
541 return AM.IndexReg != 0;
542 }
543 }
544
545 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000546}
547
Chris Lattner0aa43de2009-07-10 05:33:42 +0000548/// X86SelectCallAddress - Attempt to fill in an address from the given value.
549///
Dan Gohman46510a72010-04-15 01:51:59 +0000550bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
551 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000552 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000553 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000554 Opcode = I->getOpcode();
555 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000556 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000557 Opcode = C->getOpcode();
558 U = C;
559 }
560
561 switch (Opcode) {
562 default: break;
563 case Instruction::BitCast:
564 // Look past bitcasts.
565 return X86SelectCallAddress(U->getOperand(0), AM);
566
567 case Instruction::IntToPtr:
568 // Look past no-op inttoptrs.
569 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
570 return X86SelectCallAddress(U->getOperand(0), AM);
571 break;
572
573 case Instruction::PtrToInt:
574 // Look past no-op ptrtoints.
575 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
576 return X86SelectCallAddress(U->getOperand(0), AM);
577 break;
578 }
579
580 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000581 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000582 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000583 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000584 return false;
585
586 // RIP-relative addresses can't have additional register operands.
587 if (Subtarget->isPICStyleRIPRel() &&
588 (AM.Base.Reg != 0 || AM.IndexReg != 0))
589 return false;
590
Chris Lattner754b7652009-07-10 05:48:03 +0000591 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000592 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000593 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000594 return false;
595
596 // Okay, we've committed to selecting this global. Set up the basic address.
597 AM.GV = GV;
598
Chris Lattnere6c07b52009-07-10 05:45:15 +0000599 // No ABI requires an extra load for anything other than DLLImport, which
600 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000601 if (Subtarget->isPICStyleRIPRel()) {
602 // Use rip-relative addressing if we can. Above we verified that the
603 // base and index registers are unused.
604 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
605 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000606 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000607 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
608 } else if (Subtarget->isPICStyleGOT()) {
609 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000610 }
611
Chris Lattner0aa43de2009-07-10 05:33:42 +0000612 return true;
613 }
614
615 // If all else fails, try to materialize the value in a register.
616 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
617 if (AM.Base.Reg == 0) {
618 AM.Base.Reg = getRegForValue(V);
619 return AM.Base.Reg != 0;
620 }
621 if (AM.IndexReg == 0) {
622 assert(AM.Scale == 1 && "Scale with no index!");
623 AM.IndexReg = getRegForValue(V);
624 return AM.IndexReg != 0;
625 }
626 }
627
628 return false;
629}
630
631
Owen Andersona3971df2008-09-04 07:08:58 +0000632/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000633bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000634 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000635 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000636 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000637
Dan Gohman0586d912008-09-10 20:11:02 +0000638 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000639 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000640 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000641
Chris Lattner438949a2008-10-15 05:30:52 +0000642 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000643}
644
Evan Cheng8b19e562008-09-03 06:44:39 +0000645/// X86SelectLoad - Select and emit code to implement load instructions.
646///
Dan Gohman46510a72010-04-15 01:51:59 +0000647bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000648 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000649 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000650 return false;
651
Dan Gohman0586d912008-09-10 20:11:02 +0000652 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000653 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000654 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000655
Evan Cheng0de588f2008-09-05 21:00:03 +0000656 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000657 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000658 UpdateValueMap(I, ResultReg);
659 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000660 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000661 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000662}
663
Owen Andersone50ed302009-08-10 22:56:29 +0000664static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000666 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 case MVT::i8: return X86::CMP8rr;
668 case MVT::i16: return X86::CMP16rr;
669 case MVT::i32: return X86::CMP32rr;
670 case MVT::i64: return X86::CMP64rr;
671 case MVT::f32: return X86::UCOMISSrr;
672 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000673 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000674}
675
Chris Lattner0e13c782008-10-15 04:13:29 +0000676/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
677/// of the comparison, return an opcode that works for the compare (e.g.
678/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000679static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000681 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000682 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 case MVT::i8: return X86::CMP8ri;
684 case MVT::i16: return X86::CMP16ri;
685 case MVT::i32: return X86::CMP32ri;
686 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000687 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
688 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000689 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000690 return X86::CMP64ri32;
691 return 0;
692 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000693}
694
Dan Gohman46510a72010-04-15 01:51:59 +0000695bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
696 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000697 unsigned Op0Reg = getRegForValue(Op0);
698 if (Op0Reg == 0) return false;
699
Chris Lattnerd53886b2008-10-15 05:18:04 +0000700 // Handle 'null' like i32/i64 0.
701 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000702 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000703
Chris Lattner9a08a612008-10-15 04:26:38 +0000704 // We have two options: compare with register or immediate. If the RHS of
705 // the compare is an immediate that we can fold into this compare, use
706 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000707 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000708 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000709 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000710 .addImm(Op1C->getSExtValue());
711 return true;
712 }
713 }
714
715 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
716 if (CompareOpc == 0) return false;
717
718 unsigned Op1Reg = getRegForValue(Op1);
719 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000720 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000721
722 return true;
723}
724
Dan Gohman46510a72010-04-15 01:51:59 +0000725bool X86FastISel::X86SelectCmp(const Instruction *I) {
726 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000727
Owen Andersone50ed302009-08-10 22:56:29 +0000728 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000729 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000730 return false;
731
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000732 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000733 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000734 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000735 switch (CI->getPredicate()) {
736 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000737 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
738 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000739
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000740 unsigned EReg = createResultReg(&X86::GR8RegClass);
741 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000742 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
743 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
744 BuildMI(MBB, DL,
745 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000746 UpdateValueMap(I, ResultReg);
747 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000748 }
749 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000750 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
751 return false;
752
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000753 unsigned NEReg = createResultReg(&X86::GR8RegClass);
754 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000755 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
756 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
757 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000758 UpdateValueMap(I, ResultReg);
759 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000760 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000761 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
762 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
763 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
764 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
765 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
766 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
767 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
768 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
769 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
770 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
771 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
772 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
773
774 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
775 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
776 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
777 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
778 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
779 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
780 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
781 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
782 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
783 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000784 default:
785 return false;
786 }
787
Dan Gohman46510a72010-04-15 01:51:59 +0000788 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000789 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000790 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000791
Chris Lattner9a08a612008-10-15 04:26:38 +0000792 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000793 if (!X86FastEmitCompare(Op0, Op1, VT))
794 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000795
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000796 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000797 UpdateValueMap(I, ResultReg);
798 return true;
799}
Evan Cheng8b19e562008-09-03 06:44:39 +0000800
Dan Gohman46510a72010-04-15 01:51:59 +0000801bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000802 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000803 if (I->getType()->isIntegerTy(8) &&
804 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000805 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000806 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000807 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000808 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000809 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000810 UpdateValueMap(I, ResultReg);
811 return true;
812 }
813
814 return false;
815}
816
Chris Lattner9a08a612008-10-15 04:26:38 +0000817
Dan Gohman46510a72010-04-15 01:51:59 +0000818bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000819 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000820 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000821 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000822 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
823 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
824
Dan Gohmand98d6202008-10-02 22:15:21 +0000825 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000826 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000827 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000828 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000829
Dan Gohmand98d6202008-10-02 22:15:21 +0000830 // Try to take advantage of fallthrough opportunities.
831 CmpInst::Predicate Predicate = CI->getPredicate();
832 if (MBB->isLayoutSuccessor(TrueMBB)) {
833 std::swap(TrueMBB, FalseMBB);
834 Predicate = CmpInst::getInversePredicate(Predicate);
835 }
836
Chris Lattner871d2462008-10-15 03:58:05 +0000837 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
838 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
839
Dan Gohmand98d6202008-10-02 22:15:21 +0000840 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000841 case CmpInst::FCMP_OEQ:
842 std::swap(TrueMBB, FalseMBB);
843 Predicate = CmpInst::FCMP_UNE;
844 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000845 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
846 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
847 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
848 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
849 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
850 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
851 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
852 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
853 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
854 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
855 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
856 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
857 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000858
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000859 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
860 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
861 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
862 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
863 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
864 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
865 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
866 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
867 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
868 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000869 default:
870 return false;
871 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000872
Dan Gohman46510a72010-04-15 01:51:59 +0000873 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000874 if (SwapArgs)
875 std::swap(Op0, Op1);
876
Chris Lattner9a08a612008-10-15 04:26:38 +0000877 // Emit a compare of the LHS and RHS, setting the flags.
878 if (!X86FastEmitCompare(Op0, Op1, VT))
879 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000880
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000881 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000882
883 if (Predicate == CmpInst::FCMP_UNE) {
884 // X86 requires a second branch to handle UNE (and OEQ,
885 // which is mapped to UNE above).
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000886 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000887 }
888
Dan Gohmand98d6202008-10-02 22:15:21 +0000889 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000890 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000891 return true;
892 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000893 } else if (ExtractValueInst *EI =
894 dyn_cast<ExtractValueInst>(BI->getCondition())) {
895 // Check to see if the branch instruction is from an "arithmetic with
896 // overflow" intrinsic. The main way these intrinsics are used is:
897 //
898 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
899 // %sum = extractvalue { i32, i1 } %t, 0
900 // %obit = extractvalue { i32, i1 } %t, 1
901 // br i1 %obit, label %overflow, label %normal
902 //
Dan Gohman653456c2009-01-07 00:15:08 +0000903 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000904 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000905 // looking for the SETO/SETB instruction. If an instruction modifies the
906 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
907 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000908 if (const IntrinsicInst *CI =
909 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000910 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
911 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
912 const MachineInstr *SetMI = 0;
913 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000914
Chris Lattnera9a42252009-04-12 07:36:01 +0000915 for (MachineBasicBlock::const_reverse_iterator
916 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
917 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000918
Evan Cheng1015ba72010-05-21 20:53:24 +0000919 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000920 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000921
Chris Lattnera9a42252009-04-12 07:36:01 +0000922 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
923 Reg = Src;
924 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000925 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000926
Chris Lattnera9a42252009-04-12 07:36:01 +0000927 SetMI = &MI;
928 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000929 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000930
Chris Lattnera9a42252009-04-12 07:36:01 +0000931 const TargetInstrDesc &TID = MI.getDesc();
932 if (TID.hasUnmodeledSideEffects() ||
933 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
934 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000935 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000936
937 if (SetMI) {
938 unsigned OpCode = SetMI->getOpcode();
939
940 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000941 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
942 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000943 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000944 FastEmitBranch(FalseMBB);
945 MBB->addSuccessor(TrueMBB);
946 return true;
947 }
Bill Wendling9a901322008-12-10 19:44:24 +0000948 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000949 }
950 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000951 }
952
953 // Otherwise do a clumsy setcc and re-test it.
954 unsigned OpReg = getRegForValue(BI->getCondition());
955 if (OpReg == 0) return false;
956
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000957 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000958 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000959 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000960 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000961 return true;
962}
963
Dan Gohman46510a72010-04-15 01:51:59 +0000964bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000965 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000966 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000967 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000968 CReg = X86::CL;
969 RC = &X86::GR8RegClass;
970 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000971 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
972 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
973 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000974 default: return false;
975 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000976 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000977 CReg = X86::CX;
978 RC = &X86::GR16RegClass;
979 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000980 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
981 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
982 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000983 default: return false;
984 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000985 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000986 CReg = X86::ECX;
987 RC = &X86::GR32RegClass;
988 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000989 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
990 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
991 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000992 default: return false;
993 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000994 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000995 CReg = X86::RCX;
996 RC = &X86::GR64RegClass;
997 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000998 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
999 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1000 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001001 default: return false;
1002 }
1003 } else {
1004 return false;
1005 }
1006
Owen Andersone50ed302009-08-10 22:56:29 +00001007 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001008 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001009 return false;
1010
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001011 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1012 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001013
1014 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001015 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001016 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001017 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001018 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001019 UpdateValueMap(I, ResultReg);
1020 return true;
1021 }
1022
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001023 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1024 if (Op1Reg == 0) return false;
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001025 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001026
1027 // The shift instruction uses X86::CL. If we defined a super-register
1028 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1029 // we're doing here.
1030 if (CReg != X86::CL)
Chris Lattner518bb532010-02-09 19:54:29 +00001031 BuildMI(MBB, DL, TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001032 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1033
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001034 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001035 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001036 UpdateValueMap(I, ResultReg);
1037 return true;
1038}
1039
Dan Gohman46510a72010-04-15 01:51:59 +00001040bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001041 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001043 return false;
1044
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001045 unsigned Opc = 0;
1046 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001048 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001049 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001051 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001052 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001053 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001054 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001055 RC = &X86::GR64RegClass;
1056 } else {
1057 return false;
1058 }
1059
1060 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1061 if (Op0Reg == 0) return false;
1062 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1063 if (Op1Reg == 0) return false;
1064 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1065 if (Op2Reg == 0) return false;
1066
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001067 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001068 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001069 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001070 UpdateValueMap(I, ResultReg);
1071 return true;
1072}
1073
Dan Gohman46510a72010-04-15 01:51:59 +00001074bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001075 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001076 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001077 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001078 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001079 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001080 unsigned OpReg = getRegForValue(V);
1081 if (OpReg == 0) return false;
1082 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001083 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001084 UpdateValueMap(I, ResultReg);
1085 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001086 }
1087 }
1088
1089 return false;
1090}
1091
Dan Gohman46510a72010-04-15 01:51:59 +00001092bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001093 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001094 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001095 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001096 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001097 unsigned OpReg = getRegForValue(V);
1098 if (OpReg == 0) return false;
1099 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001100 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001101 UpdateValueMap(I, ResultReg);
1102 return true;
1103 }
1104 }
1105 }
1106
1107 return false;
1108}
1109
Dan Gohman46510a72010-04-15 01:51:59 +00001110bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001111 if (Subtarget->is64Bit())
1112 // All other cases should be handled by the tblgen generated code.
1113 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001114 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1115 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001116
1117 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001119 // All other cases should be handled by the tblgen generated code.
1120 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001122 // All other cases should be handled by the tblgen generated code.
1123 return false;
1124
1125 unsigned InputReg = getRegForValue(I->getOperand(0));
1126 if (!InputReg)
1127 // Unhandled operand. Halt "fast" selection and bail.
1128 return false;
1129
Dan Gohman62417622009-04-27 16:33:14 +00001130 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001131 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1132 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001133 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001134 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001135 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001136
1137 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001139 CopyReg, /*Kill=*/true,
1140 X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001141 if (!ResultReg)
1142 return false;
1143
1144 UpdateValueMap(I, ResultReg);
1145 return true;
1146}
1147
Dan Gohman46510a72010-04-15 01:51:59 +00001148bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1149 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1150 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001151
Dan Gohman46510a72010-04-15 01:51:59 +00001152 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001153 switch (CI->getIntrinsicID()) {
1154 default: break;
1155 case Intrinsic::sadd_with_overflow:
1156 case Intrinsic::uadd_with_overflow:
1157 // Cheat a little. We know that the registers for "add" and "seto" are
1158 // allocated sequentially. However, we only keep track of the register
1159 // for "add" in the value map. Use extractvalue's index to get the
1160 // correct register for "seto".
1161 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1162 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001163 }
1164 }
1165
1166 return false;
1167}
1168
Dan Gohman46510a72010-04-15 01:51:59 +00001169bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001170 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001171 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001172 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001173 case Intrinsic::stackprotector: {
1174 // Emit code inline code to store the stack guard onto the stack.
1175 EVT PtrTy = TLI.getPointerTy();
1176
Eric Christopher551754c2010-04-16 23:37:20 +00001177 const Value *Op1 = I.getOperand(1); // The guard's value.
1178 const AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Eric Christopher07754c22010-03-18 20:27:26 +00001179
1180 // Grab the frame index.
1181 X86AddressMode AM;
1182 if (!X86SelectAddress(Slot, AM)) return false;
1183
Eric Christopher88dee302010-03-18 21:58:33 +00001184 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1185
Eric Christopher07754c22010-03-18 20:27:26 +00001186 return true;
1187 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001188 case Intrinsic::objectsize: {
Eric Christopher551754c2010-04-16 23:37:20 +00001189 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopherf27805b2010-03-11 06:20:22 +00001190 const Type *Ty = I.getCalledFunction()->getReturnType();
1191
1192 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1193
1194 EVT VT;
1195 if (!isTypeLegal(Ty, VT))
1196 return false;
1197
1198 unsigned OpC = 0;
1199 if (VT == MVT::i32)
1200 OpC = X86::MOV32ri;
1201 else if (VT == MVT::i64)
1202 OpC = X86::MOV64ri;
1203 else
1204 return false;
1205
1206 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1207 BuildMI(MBB, DL, TII.get(OpC), ResultReg).
1208 addImm(CI->getZExtValue() == 0 ? -1ULL : 0);
1209 UpdateValueMap(&I, ResultReg);
1210 return true;
1211 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001212 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001213 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001214 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001215 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001216 if (!X86SelectAddress(DI->getAddress(), AM))
1217 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001218 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001219 // FIXME may need to add RegState::Debug to any registers produced,
1220 // although ESP/EBP should be the only ones at the moment.
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001221 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1222 addMetadata(DI->getVariable());
1223 return true;
1224 }
Eric Christopher77f79892010-01-18 22:11:29 +00001225 case Intrinsic::trap: {
1226 BuildMI(MBB, DL, TII.get(X86::TRAP));
1227 return true;
1228 }
Bill Wendling52370a12008-12-09 02:42:50 +00001229 case Intrinsic::sadd_with_overflow:
1230 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001231 // Replace "add with overflow" intrinsics with an "add" instruction followed
1232 // by a seto/setc instruction. Later on, when the "extractvalue"
1233 // instructions are encountered, we use the fact that two registers were
1234 // created sequentially to get the correct registers for the "sum" and the
1235 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001236 const Function *Callee = I.getCalledFunction();
1237 const Type *RetTy =
1238 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1239
Owen Andersone50ed302009-08-10 22:56:29 +00001240 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001241 if (!isTypeLegal(RetTy, VT))
1242 return false;
1243
Eric Christopher551754c2010-04-16 23:37:20 +00001244 const Value *Op1 = I.getOperand(1);
1245 const Value *Op2 = I.getOperand(2);
Bill Wendling52370a12008-12-09 02:42:50 +00001246 unsigned Reg1 = getRegForValue(Op1);
1247 unsigned Reg2 = getRegForValue(Op2);
1248
1249 if (Reg1 == 0 || Reg2 == 0)
1250 // FIXME: Handle values *not* in registers.
1251 return false;
1252
1253 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001255 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001257 OpC = X86::ADD64rr;
1258 else
1259 return false;
1260
1261 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001262 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001263 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001264
Chris Lattner8d57b772009-04-12 07:51:14 +00001265 // If the add with overflow is an intra-block value then we just want to
1266 // create temporaries for it like normal. If it is a cross-block value then
1267 // UpdateValueMap will return the cross-block register used. Since we
1268 // *really* want the value to be live in the register pair known by
1269 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1270 // the cross block case. In the non-cross-block case, we should just make
1271 // another register for the value.
1272 if (DestReg1 != ResultReg)
1273 ResultReg = DestReg1+1;
1274 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001276
Chris Lattnera9a42252009-04-12 07:36:01 +00001277 unsigned Opc = X86::SETBr;
1278 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1279 Opc = X86::SETOr;
1280 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001281 return true;
1282 }
1283 }
1284}
1285
Dan Gohman46510a72010-04-15 01:51:59 +00001286bool X86FastISel::X86SelectCall(const Instruction *I) {
1287 const CallInst *CI = cast<CallInst>(I);
Eric Christopher551754c2010-04-16 23:37:20 +00001288 const Value *Callee = I->getOperand(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001289
1290 // Can't handle inline asm yet.
1291 if (isa<InlineAsm>(Callee))
1292 return false;
1293
Bill Wendling52370a12008-12-09 02:42:50 +00001294 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001295 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001296 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001297
Evan Chengf3d4efe2008-09-07 09:09:33 +00001298 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001299 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001300 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001301 if (CC != CallingConv::C &&
1302 CC != CallingConv::Fast &&
1303 CC != CallingConv::X86_FastCall)
1304 return false;
1305
Evan Cheng381993f2010-01-27 00:00:57 +00001306 // fastcc with -tailcallopt is intended to provide a guaranteed
1307 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001308 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001309 return false;
1310
Evan Chengf3d4efe2008-09-07 09:09:33 +00001311 // Let SDISel handle vararg functions.
1312 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1313 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1314 if (FTy->isVarArg())
1315 return false;
1316
1317 // Handle *simple* calls for now.
1318 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001319 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001320 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001322 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001323 return false;
1324
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001325 // Materialize callee address in a register. FIXME: GV address can be
1326 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001327 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001328 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001329 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001330 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001331 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001332 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001333 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001334 } else if (CalleeAM.Base.Reg != 0) {
1335 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001336 } else
1337 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001338
Evan Chengdebdea02008-09-08 17:15:42 +00001339 // Allow calls which produce i1 results.
1340 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 if (RetVT == MVT::i1) {
1342 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001343 AndToI1 = true;
1344 }
1345
Evan Chengf3d4efe2008-09-07 09:09:33 +00001346 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001347 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001348 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001349 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001350 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001351 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001352 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001353 ArgVTs.reserve(CS.arg_size());
1354 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001355 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001356 i != e; ++i) {
1357 unsigned Arg = getRegForValue(*i);
1358 if (Arg == 0)
1359 return false;
1360 ISD::ArgFlagsTy Flags;
1361 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001362 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001363 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001364 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001365 Flags.setZExt();
1366
1367 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001368 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1369 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1370 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1371 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001372 return false;
1373
1374 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001376 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001377 return false;
1378 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1379 Flags.setOrigAlign(OriginalAlignment);
1380
1381 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001382 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001383 ArgVTs.push_back(ArgVT);
1384 ArgFlags.push_back(Flags);
1385 }
1386
1387 // Analyze operands of the call, assigning locations to each operand.
1388 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001389 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001390 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1391
1392 // Get a count of how many bytes are to be pushed on the stack.
1393 unsigned NumBytes = CCInfo.getNextStackOffset();
1394
1395 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001396 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001397 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001398
Chris Lattner438949a2008-10-15 05:30:52 +00001399 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001400 // copies / loads.
1401 SmallVector<unsigned, 4> RegArgs;
1402 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1403 CCValAssign &VA = ArgLocs[i];
1404 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001405 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001406
1407 // Promote the value if needed.
1408 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001409 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001410 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001411 case CCValAssign::SExt: {
1412 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1413 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001414 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001415 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001416 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001417 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001418 }
1419 case CCValAssign::ZExt: {
1420 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1421 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001422 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001423 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001424 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001425 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001426 }
1427 case CCValAssign::AExt: {
1428 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1429 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001430 if (!Emitted)
1431 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001432 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001433 if (!Emitted)
1434 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1435 Arg, ArgVT, Arg);
1436
Chris Lattnera33649e2008-12-19 17:03:38 +00001437 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001438 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001439 break;
1440 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001441 case CCValAssign::BCvt: {
1442 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001443 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001444 assert(BC != 0 && "Failed to emit a bitcast!");
1445 Arg = BC;
1446 ArgVT = VA.getLocVT();
1447 break;
1448 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001449 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001450
1451 if (VA.isRegLoc()) {
1452 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1453 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001454 Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001455 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001456 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457 RegArgs.push_back(VA.getLocReg());
1458 } else {
1459 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001460 X86AddressMode AM;
1461 AM.Base.Reg = StackPtr;
1462 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001463 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001464
1465 // If this is a really simple value, emit this with the Value* version of
1466 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1467 // can cause us to reevaluate the argument.
1468 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1469 X86FastEmitStore(ArgVT, ArgVal, AM);
1470 else
1471 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001472 }
1473 }
1474
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001475 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1476 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001477 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001478 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001479 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001480 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC,
1481 DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001482 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001483 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001484 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001485
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001487 MachineInstrBuilder MIB;
1488 if (CalleeOp) {
1489 // Register-indirect call.
1490 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1491 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1492
1493 } else {
1494 // Direct call.
1495 assert(GV && "Not a direct call");
1496 unsigned CallOpc =
1497 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1498
1499 // See if we need any target-specific flags on the GV operand.
1500 unsigned char OpFlags = 0;
1501
1502 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1503 // external symbols most go through the PLT in PIC mode. If the symbol
1504 // has hidden or protected visibility, or if it is static or local, then
1505 // we don't need to use the PLT - we can directly call it.
1506 if (Subtarget->isTargetELF() &&
1507 TM.getRelocationModel() == Reloc::PIC_ &&
1508 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1509 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001510 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001511 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1512 Subtarget->getDarwinVers() < 9) {
1513 // PC-relative references to external symbols should go through $stub,
1514 // unless we're building with the leopard linker or later, which
1515 // automatically synthesizes these stubs.
1516 OpFlags = X86II::MO_DARWIN_STUB;
1517 }
1518
1519
1520 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1521 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001522
1523 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001524 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001525 MIB.addReg(X86::EBX);
1526
Evan Chengf3d4efe2008-09-07 09:09:33 +00001527 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001528 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1529 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001530
1531 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001532 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001533 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001534
1535 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001537 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001538 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001539 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1540
1541 // Copy all of the result registers out of their specified physreg.
1542 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001543 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001544 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1545 TargetRegisterClass *SrcRC = DstRC;
1546
1547 // If this is a call to a function that returns an fp value on the x87 fp
1548 // stack, but where we prefer to use the value in xmm registers, copy it
1549 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1550 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1551 RVLocs[0].getLocReg() == X86::ST1) &&
1552 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001554 SrcRC = X86::RSTRegisterClass;
1555 DstRC = X86::RFP80RegisterClass;
1556 }
1557
1558 unsigned ResultReg = createResultReg(DstRC);
1559 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001560 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001561 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001562 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001563 if (CopyVT != RVLocs[0].getValVT()) {
1564 // Round the F80 the right size, which also moves to the appropriate xmm
1565 // register. This is accomplished by storing the F80 value in memory and
1566 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001567 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001569 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001570 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001571 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001575 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001576 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 }
1578
Evan Chengdebdea02008-09-08 17:15:42 +00001579 if (AndToI1) {
1580 // Mask out all but lowest bit for some call which produces an i1.
1581 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001582 BuildMI(MBB, DL,
1583 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001584 ResultReg = AndResult;
1585 }
1586
Evan Chengf3d4efe2008-09-07 09:09:33 +00001587 UpdateValueMap(I, ResultReg);
1588 }
1589
1590 return true;
1591}
1592
1593
Dan Gohman99b21822008-08-28 23:21:34 +00001594bool
Dan Gohman46510a72010-04-15 01:51:59 +00001595X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001596 switch (I->getOpcode()) {
1597 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001598 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001599 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001600 case Instruction::Store:
1601 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001602 case Instruction::ICmp:
1603 case Instruction::FCmp:
1604 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001605 case Instruction::ZExt:
1606 return X86SelectZExt(I);
1607 case Instruction::Br:
1608 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001609 case Instruction::Call:
1610 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001611 case Instruction::LShr:
1612 case Instruction::AShr:
1613 case Instruction::Shl:
1614 return X86SelectShift(I);
1615 case Instruction::Select:
1616 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001617 case Instruction::Trunc:
1618 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001619 case Instruction::FPExt:
1620 return X86SelectFPExt(I);
1621 case Instruction::FPTrunc:
1622 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001623 case Instruction::ExtractValue:
1624 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001625 case Instruction::IntToPtr: // Deliberate fall-through.
1626 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001627 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1628 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001629 if (DstVT.bitsGT(SrcVT))
1630 return X86SelectZExt(I);
1631 if (DstVT.bitsLT(SrcVT))
1632 return X86SelectTrunc(I);
1633 unsigned Reg = getRegForValue(I->getOperand(0));
1634 if (Reg == 0) return false;
1635 UpdateValueMap(I, Reg);
1636 return true;
1637 }
Dan Gohman99b21822008-08-28 23:21:34 +00001638 }
1639
1640 return false;
1641}
1642
Dan Gohman46510a72010-04-15 01:51:59 +00001643unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001644 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001645 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001646 return false;
1647
1648 // Get opcode and regclass of the output for the given load instruction.
1649 unsigned Opc = 0;
1650 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001652 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001654 Opc = X86::MOV8rm;
1655 RC = X86::GR8RegisterClass;
1656 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001657 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001658 Opc = X86::MOV16rm;
1659 RC = X86::GR16RegisterClass;
1660 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001662 Opc = X86::MOV32rm;
1663 RC = X86::GR32RegisterClass;
1664 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001665 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001666 // Must be in x86-64 mode.
1667 Opc = X86::MOV64rm;
1668 RC = X86::GR64RegisterClass;
1669 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001670 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001671 if (Subtarget->hasSSE1()) {
1672 Opc = X86::MOVSSrm;
1673 RC = X86::FR32RegisterClass;
1674 } else {
1675 Opc = X86::LD_Fp32m;
1676 RC = X86::RFP32RegisterClass;
1677 }
1678 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001679 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001680 if (Subtarget->hasSSE2()) {
1681 Opc = X86::MOVSDrm;
1682 RC = X86::FR64RegisterClass;
1683 } else {
1684 Opc = X86::LD_Fp64m;
1685 RC = X86::RFP64RegisterClass;
1686 }
1687 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001688 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001689 // No f80 support yet.
1690 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001691 }
1692
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001693 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001694 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001695 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001696 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001698 Opc = X86::LEA32r;
1699 else
1700 Opc = X86::LEA64r;
1701 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001702 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001703 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001704 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001705 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001706 }
1707
Owen Anderson3b217c62008-09-06 01:11:01 +00001708 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001709 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001710 if (Align == 0) {
1711 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001712 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001713 }
Owen Anderson95267a12008-09-05 00:06:23 +00001714
Dan Gohman5396c992008-09-30 01:21:32 +00001715 // x86-32 PIC requires a PIC base register for constant pools.
1716 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001717 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001718 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001719 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1720 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1721 } else if (Subtarget->isPICStyleGOT()) {
1722 OpFlag = X86II::MO_GOTOFF;
1723 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1724 } else if (Subtarget->isPICStyleRIPRel() &&
1725 TM.getCodeModel() == CodeModel::Small) {
1726 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001727 }
Dan Gohman5396c992008-09-30 01:21:32 +00001728
1729 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001730 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001731 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001732 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1733 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001734
Owen Anderson95267a12008-09-05 00:06:23 +00001735 return ResultReg;
1736}
1737
Dan Gohman46510a72010-04-15 01:51:59 +00001738unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001739 // Fail on dynamic allocas. At this point, getRegForValue has already
1740 // checked its CSE maps, so if we're here trying to handle a dynamic
1741 // alloca, we're not going to succeed. X86SelectAddress has a
1742 // check for dynamic allocas, because it's called directly from
1743 // various places, but TargetMaterializeAlloca also needs a check
1744 // in order to avoid recursion between getRegForValue,
1745 // X86SelectAddrss, and TargetMaterializeAlloca.
1746 if (!StaticAllocaMap.count(C))
1747 return 0;
1748
Dan Gohman0586d912008-09-10 20:11:02 +00001749 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001750 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001751 return 0;
1752 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1753 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1754 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001755 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001756 return ResultReg;
1757}
1758
Evan Chengc3f44b02008-09-03 00:03:49 +00001759namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001760 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1761 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001762 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00001763 DenseMap<const AllocaInst *, int> &am,
1764 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001765#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00001766 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001767#endif
1768 ) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00001769 return new X86FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001770#ifndef NDEBUG
1771 , cil
1772#endif
1773 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001774 }
Dan Gohman99b21822008-08-28 23:21:34 +00001775}