blob: 6c68f205a61597e041b77de196fc81e9ea99650f [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000033#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000034using namespace llvm;
35
Chris Lattner087fcf32009-03-08 18:44:31 +000036namespace {
37
Evan Chengc3f44b02008-09-03 00:03:49 +000038class X86FastISel : public FastISel {
39 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
40 /// make the right decision when generating code for different targets.
41 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000042
43 /// StackPtr - Register used as the stack pointer.
44 ///
45 unsigned StackPtr;
46
47 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
51 bool X86ScalarSSEf64;
52 bool X86ScalarSSEf32;
53
Evan Cheng8b19e562008-09-03 06:44:39 +000054public:
Dan Gohman3df24e62008-09-03 23:12:08 +000055 explicit X86FastISel(MachineFunction &mf,
56 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000057 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +000058 DenseMap<const AllocaInst *, int> &am,
59 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +000060#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +000061 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +000062#endif
63 )
Dan Gohmanf81eca02010-04-22 20:46:50 +000064 : FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +000065#ifndef NDEBUG
66 , cil
67#endif
68 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000069 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000070 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
71 X86ScalarSSEf64 = Subtarget->hasSSE2();
72 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000073 }
Evan Chengc3f44b02008-09-03 00:03:49 +000074
Dan Gohman46510a72010-04-15 01:51:59 +000075 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000076
Dan Gohman1adf1b02008-08-19 21:45:35 +000077#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000078
79private:
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000081
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000083
Dan Gohman46510a72010-04-15 01:51:59 +000084 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000085 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000087 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000088
Owen Andersone50ed302009-08-10 22:56:29 +000089 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000090 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
93 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000094
Dan Gohman46510a72010-04-15 01:51:59 +000095 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000096
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectFPExt(const Instruction *I);
112 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
117 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000119 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000120
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000122 return getTargetMachine()->getInstrInfo();
123 }
124 const X86TargetMachine *getTargetMachine() const {
125 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000126 }
127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000129
Dan Gohman46510a72010-04-15 01:51:59 +0000130 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000131
132 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
133 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000134 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
136 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 }
138
Owen Andersone50ed302009-08-10 22:56:29 +0000139 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000140};
Chris Lattner087fcf32009-03-08 18:44:31 +0000141
142} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000143
Owen Andersone50ed302009-08-10 22:56:29 +0000144bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000145 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // Unhandled type. Halt "fast" selection and bail.
148 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000149
Dan Gohman9b66d732008-09-30 00:48:39 +0000150 // For now, require SSE/SSE2 for performing floating-point operations,
151 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000153 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 return false;
156 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000159 // We only handle legal types. For example, on x86-32 the instruction
160 // selector contains all of the 64-bit instructions from x86-64,
161 // under the assumption that i64 won't be used if the target doesn't
162 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164}
165
166#include "X86GenCallingConv.inc"
167
168/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
169/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000170CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
171 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000172 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000173 if (CC == CallingConv::GHC)
174 return CC_X86_64_GHC;
175 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_64_C;
179 }
180
181 if (CC == CallingConv::X86_FastCall)
182 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000183 else if (CC == CallingConv::X86_ThisCall)
184 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000185 else if (CC == CallingConv::Fast)
186 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000187 else if (CC == CallingConv::GHC)
188 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000189 else
190 return CC_X86_32_C;
191}
192
Evan Cheng0de588f2008-09-05 21:00:03 +0000193/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000194/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000195/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000196bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 unsigned &ResultReg) {
198 // Get opcode and regclass of the output for the given load instruction.
199 unsigned Opc = 0;
200 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000203 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 Opc = X86::MOV8rm;
206 RC = X86::GR8RegisterClass;
207 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 Opc = X86::MOV16rm;
210 RC = X86::GR16RegisterClass;
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 Opc = X86::MOV32rm;
214 RC = X86::GR32RegisterClass;
215 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000217 // Must be in x86-64 mode.
218 Opc = X86::MOV64rm;
219 RC = X86::GR64RegisterClass;
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 if (Subtarget->hasSSE1()) {
223 Opc = X86::MOVSSrm;
224 RC = X86::FR32RegisterClass;
225 } else {
226 Opc = X86::LD_Fp32m;
227 RC = X86::RFP32RegisterClass;
228 }
229 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 if (Subtarget->hasSSE2()) {
232 Opc = X86::MOVSDrm;
233 RC = X86::FR64RegisterClass;
234 } else {
235 Opc = X86::LD_Fp64m;
236 RC = X86::RFP64RegisterClass;
237 }
238 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000240 // No f80 support yet.
241 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 }
243
244 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000245 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 return true;
247}
248
Evan Chengf3d4efe2008-09-07 09:09:33 +0000249/// X86FastEmitStore - Emit a machine instruction to store a value Val of
250/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
251/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000252/// i.e. V. Return true if it is possible.
253bool
Owen Andersone50ed302009-08-10 22:56:29 +0000254X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000255 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000256 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 switch (VT.getSimpleVT().SimpleTy) {
259 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000261 case MVT::i1: {
262 // Mask out all but lowest bit.
263 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
264 BuildMI(MBB, DL,
265 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
266 Val = AndResult;
267 }
268 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 case MVT::i8: Opc = X86::MOV8mr; break;
270 case MVT::i16: Opc = X86::MOV16mr; break;
271 case MVT::i32: Opc = X86::MOV32mr; break;
272 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
273 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000274 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000277 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000279 }
Chris Lattner438949a2008-10-15 05:30:52 +0000280
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000281 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000282 return true;
283}
284
Dan Gohman46510a72010-04-15 01:51:59 +0000285bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000286 const X86AddressMode &AM) {
287 // Handle 'null' like i32/i64 0.
288 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000289 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000290
291 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000292 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000293 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000296 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000297 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 case MVT::i8: Opc = X86::MOV8mi; break;
299 case MVT::i16: Opc = X86::MOV16mi; break;
300 case MVT::i32: Opc = X86::MOV32mi; break;
301 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000302 // Must be a 32-bit sign extended value.
303 if ((int)CI->getSExtValue() == CI->getSExtValue())
304 Opc = X86::MOV64mi32;
305 break;
306 }
307
308 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000309 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000310 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000311 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000312 return true;
313 }
314 }
315
316 unsigned ValReg = getRegForValue(Val);
317 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000318 return false;
319
320 return X86FastEmitStore(VT, ValReg, AM);
321}
322
Evan Cheng24e3a902008-09-08 06:35:17 +0000323/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
324/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
325/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000326bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
327 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000328 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000329 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
330 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000331
332 if (RR != 0) {
333 ResultReg = RR;
334 return true;
335 } else
336 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000337}
338
Dan Gohman0586d912008-09-10 20:11:02 +0000339/// X86SelectAddress - Attempt to fill in an address from the given value.
340///
Dan Gohman46510a72010-04-15 01:51:59 +0000341bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
342 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000343 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000344 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000345 // Don't walk into other basic blocks; it's possible we haven't
346 // visited them yet, so the instructions may not yet be assigned
347 // virtual registers.
348 if (MBBMap[I->getParent()] != MBB)
349 return false;
350
Dan Gohman35893082008-09-18 23:23:44 +0000351 Opcode = I->getOpcode();
352 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000353 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000354 Opcode = C->getOpcode();
355 U = C;
356 }
Dan Gohman0586d912008-09-10 20:11:02 +0000357
Chris Lattner868ee942010-06-15 19:08:40 +0000358 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
359 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000360 // Fast instruction selection doesn't support the special
361 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000362 return false;
363
Dan Gohman35893082008-09-18 23:23:44 +0000364 switch (Opcode) {
365 default: break;
366 case Instruction::BitCast:
367 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000368 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000369
370 case Instruction::IntToPtr:
371 // Look past no-op inttoptrs.
372 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000373 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000374 break;
Dan Gohman35893082008-09-18 23:23:44 +0000375
376 case Instruction::PtrToInt:
377 // Look past no-op ptrtoints.
378 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000379 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000380 break;
Dan Gohman35893082008-09-18 23:23:44 +0000381
382 case Instruction::Alloca: {
383 // Do static allocas.
384 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000385 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000386 if (SI != StaticAllocaMap.end()) {
387 AM.BaseType = X86AddressMode::FrameIndexBase;
388 AM.Base.FrameIndex = SI->second;
389 return true;
390 }
391 break;
Dan Gohman35893082008-09-18 23:23:44 +0000392 }
393
394 case Instruction::Add: {
395 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000396 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000397 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
398 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000399 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000400 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000401 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000402 }
Dan Gohman0586d912008-09-10 20:11:02 +0000403 }
Dan Gohman35893082008-09-18 23:23:44 +0000404 break;
405 }
406
407 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000408 X86AddressMode SavedAM = AM;
409
Dan Gohman35893082008-09-18 23:23:44 +0000410 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000411 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000412 unsigned IndexReg = AM.IndexReg;
413 unsigned Scale = AM.Scale;
414 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000415 // Iterate through the indices, folding what we can. Constants can be
416 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000417 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000418 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000419 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000420 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
421 const StructLayout *SL = TD.getStructLayout(STy);
422 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
423 Disp += SL->getElementOffset(Idx);
424 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000425 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman46510a72010-04-15 01:51:59 +0000426 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Dan Gohman35893082008-09-18 23:23:44 +0000427 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000428 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000429 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000430 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000431 (S == 1 || S == 2 || S == 4 || S == 8)) {
432 // Scaled-index addressing.
433 Scale = S;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000434 IndexReg = getRegForGEPIndex(Op).first;
Dan Gohman35893082008-09-18 23:23:44 +0000435 if (IndexReg == 0)
436 return false;
437 } else
438 // Unsupported.
439 goto unsupported_gep;
440 }
441 }
Dan Gohman09aae462008-09-26 20:04:15 +0000442 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000443 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000444 break;
Dan Gohman35893082008-09-18 23:23:44 +0000445 // Ok, the GEP indices were covered by constant-offset and scaled-index
446 // addressing. Update the address state and move on to examining the base.
447 AM.IndexReg = IndexReg;
448 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000449 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000450 if (X86SelectAddress(U->getOperand(0), AM))
451 return true;
452
453 // If we couldn't merge the sub value into this addr mode, revert back to
454 // our address and just match the value instead of completely failing.
455 AM = SavedAM;
456 break;
Dan Gohman35893082008-09-18 23:23:44 +0000457 unsupported_gep:
458 // Ok, the GEP indices weren't all covered.
459 break;
460 }
461 }
462
463 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000464 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000465 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000466 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000467 return false;
468
Dan Gohman97135e12008-09-26 19:15:30 +0000469 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000470 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000471 (AM.Base.Reg != 0 || AM.IndexReg != 0))
472 return false;
473
Dan Gohmane9865942009-02-23 22:03:08 +0000474 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000475 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000476 if (GVar->isThreadLocal())
477 return false;
478
Chris Lattnerff7727f2009-07-09 06:41:35 +0000479 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000480 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000481
Chris Lattner0d786dd2009-07-10 07:48:51 +0000482 // Allow the subtarget to classify the global.
483 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
484
485 // If this reference is relative to the pic base, set it now.
486 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000487 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000488 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000489 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000490
491 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000492 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000493 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000494 if (Subtarget->isPICStyleRIPRel()) {
495 // Use rip-relative addressing if we can. Above we verified that the
496 // base and index registers are unused.
497 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
498 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000499 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000500 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000501 return true;
502 }
503
Chris Lattner0d786dd2009-07-10 07:48:51 +0000504 // Ok, we need to do a load from a stub. If we've already loaded from this
505 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000506 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
507 unsigned LoadReg;
508 if (I != LocalValueMap.end() && I->second != 0) {
509 LoadReg = I->second;
510 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000511 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000512 unsigned Opc = 0;
513 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000514 X86AddressMode StubAM;
515 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000516 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000517 StubAM.GVOpFlags = GVFlags;
518
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000520 Opc = X86::MOV64rm;
521 RC = X86::GR64RegisterClass;
522
Chris Lattner0d786dd2009-07-10 07:48:51 +0000523 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000524 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000525 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000526 Opc = X86::MOV32rm;
527 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000528 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000529
530 LoadReg = createResultReg(RC);
531 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
532
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000533 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000534 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000535 }
Chris Lattner18c59872009-06-27 04:16:01 +0000536
Chris Lattnerff7727f2009-07-09 06:41:35 +0000537 // Now construct the final address. Note that the Disp, Scale,
538 // and Index values may already be set here.
539 AM.Base.Reg = LoadReg;
540 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000541 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000542 }
543
Dan Gohman97135e12008-09-26 19:15:30 +0000544 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000545 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000546 if (AM.Base.Reg == 0) {
547 AM.Base.Reg = getRegForValue(V);
548 return AM.Base.Reg != 0;
549 }
550 if (AM.IndexReg == 0) {
551 assert(AM.Scale == 1 && "Scale with no index!");
552 AM.IndexReg = getRegForValue(V);
553 return AM.IndexReg != 0;
554 }
555 }
556
557 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000558}
559
Chris Lattner0aa43de2009-07-10 05:33:42 +0000560/// X86SelectCallAddress - Attempt to fill in an address from the given value.
561///
Dan Gohman46510a72010-04-15 01:51:59 +0000562bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
563 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000564 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000565 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566 Opcode = I->getOpcode();
567 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000568 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000569 Opcode = C->getOpcode();
570 U = C;
571 }
572
573 switch (Opcode) {
574 default: break;
575 case Instruction::BitCast:
576 // Look past bitcasts.
577 return X86SelectCallAddress(U->getOperand(0), AM);
578
579 case Instruction::IntToPtr:
580 // Look past no-op inttoptrs.
581 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
582 return X86SelectCallAddress(U->getOperand(0), AM);
583 break;
584
585 case Instruction::PtrToInt:
586 // Look past no-op ptrtoints.
587 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
588 return X86SelectCallAddress(U->getOperand(0), AM);
589 break;
590 }
591
592 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000593 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000594 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000595 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000596 return false;
597
598 // RIP-relative addresses can't have additional register operands.
599 if (Subtarget->isPICStyleRIPRel() &&
600 (AM.Base.Reg != 0 || AM.IndexReg != 0))
601 return false;
602
Chris Lattner754b7652009-07-10 05:48:03 +0000603 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000604 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000605 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000606 return false;
607
608 // Okay, we've committed to selecting this global. Set up the basic address.
609 AM.GV = GV;
610
Chris Lattnere6c07b52009-07-10 05:45:15 +0000611 // No ABI requires an extra load for anything other than DLLImport, which
612 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000613 if (Subtarget->isPICStyleRIPRel()) {
614 // Use rip-relative addressing if we can. Above we verified that the
615 // base and index registers are unused.
616 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
617 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000618 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000619 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
620 } else if (Subtarget->isPICStyleGOT()) {
621 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000622 }
623
Chris Lattner0aa43de2009-07-10 05:33:42 +0000624 return true;
625 }
626
627 // If all else fails, try to materialize the value in a register.
628 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
629 if (AM.Base.Reg == 0) {
630 AM.Base.Reg = getRegForValue(V);
631 return AM.Base.Reg != 0;
632 }
633 if (AM.IndexReg == 0) {
634 assert(AM.Scale == 1 && "Scale with no index!");
635 AM.IndexReg = getRegForValue(V);
636 return AM.IndexReg != 0;
637 }
638 }
639
640 return false;
641}
642
643
Owen Andersona3971df2008-09-04 07:08:58 +0000644/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000645bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000646 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000647 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000648 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000649
Dan Gohman0586d912008-09-10 20:11:02 +0000650 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000651 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000652 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000653
Chris Lattner438949a2008-10-15 05:30:52 +0000654 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000655}
656
Evan Cheng8b19e562008-09-03 06:44:39 +0000657/// X86SelectLoad - Select and emit code to implement load instructions.
658///
Dan Gohman46510a72010-04-15 01:51:59 +0000659bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000660 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000661 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000662 return false;
663
Dan Gohman0586d912008-09-10 20:11:02 +0000664 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000665 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000666 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000667
Evan Cheng0de588f2008-09-05 21:00:03 +0000668 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000669 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000670 UpdateValueMap(I, ResultReg);
671 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000672 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000673 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000674}
675
Owen Andersone50ed302009-08-10 22:56:29 +0000676static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000678 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 case MVT::i8: return X86::CMP8rr;
680 case MVT::i16: return X86::CMP16rr;
681 case MVT::i32: return X86::CMP32rr;
682 case MVT::i64: return X86::CMP64rr;
683 case MVT::f32: return X86::UCOMISSrr;
684 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000685 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000686}
687
Chris Lattner0e13c782008-10-15 04:13:29 +0000688/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
689/// of the comparison, return an opcode that works for the compare (e.g.
690/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000691static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000693 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000694 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 case MVT::i8: return X86::CMP8ri;
696 case MVT::i16: return X86::CMP16ri;
697 case MVT::i32: return X86::CMP32ri;
698 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000699 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
700 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000701 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000702 return X86::CMP64ri32;
703 return 0;
704 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000705}
706
Dan Gohman46510a72010-04-15 01:51:59 +0000707bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
708 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000709 unsigned Op0Reg = getRegForValue(Op0);
710 if (Op0Reg == 0) return false;
711
Chris Lattnerd53886b2008-10-15 05:18:04 +0000712 // Handle 'null' like i32/i64 0.
713 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000714 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000715
Chris Lattner9a08a612008-10-15 04:26:38 +0000716 // We have two options: compare with register or immediate. If the RHS of
717 // the compare is an immediate that we can fold into this compare, use
718 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000719 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000720 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000721 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000722 .addImm(Op1C->getSExtValue());
723 return true;
724 }
725 }
726
727 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
728 if (CompareOpc == 0) return false;
729
730 unsigned Op1Reg = getRegForValue(Op1);
731 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000732 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000733
734 return true;
735}
736
Dan Gohman46510a72010-04-15 01:51:59 +0000737bool X86FastISel::X86SelectCmp(const Instruction *I) {
738 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000739
Owen Andersone50ed302009-08-10 22:56:29 +0000740 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000741 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000742 return false;
743
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000744 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000745 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000746 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000747 switch (CI->getPredicate()) {
748 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000749 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
750 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000751
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000752 unsigned EReg = createResultReg(&X86::GR8RegClass);
753 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000754 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
755 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
756 BuildMI(MBB, DL,
757 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000758 UpdateValueMap(I, ResultReg);
759 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000760 }
761 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000762 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
763 return false;
764
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000765 unsigned NEReg = createResultReg(&X86::GR8RegClass);
766 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000767 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
768 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
769 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000770 UpdateValueMap(I, ResultReg);
771 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000772 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000773 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
774 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
775 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
776 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
777 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
778 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
779 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
780 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
781 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
782 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
783 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
784 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
785
786 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
787 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
788 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
789 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
790 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
791 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
792 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
793 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
794 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
795 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000796 default:
797 return false;
798 }
799
Dan Gohman46510a72010-04-15 01:51:59 +0000800 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000801 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000802 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000803
Chris Lattner9a08a612008-10-15 04:26:38 +0000804 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000805 if (!X86FastEmitCompare(Op0, Op1, VT))
806 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000807
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000808 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000809 UpdateValueMap(I, ResultReg);
810 return true;
811}
Evan Cheng8b19e562008-09-03 06:44:39 +0000812
Dan Gohman46510a72010-04-15 01:51:59 +0000813bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000814 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000815 if (I->getType()->isIntegerTy(8) &&
816 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000817 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000818 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000819 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000820 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000821 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000822 UpdateValueMap(I, ResultReg);
823 return true;
824 }
825
826 return false;
827}
828
Chris Lattner9a08a612008-10-15 04:26:38 +0000829
Dan Gohman46510a72010-04-15 01:51:59 +0000830bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000831 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000832 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000833 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000834 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
835 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
836
Dan Gohmand98d6202008-10-02 22:15:21 +0000837 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000838 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000839 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000840 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000841
Dan Gohmand98d6202008-10-02 22:15:21 +0000842 // Try to take advantage of fallthrough opportunities.
843 CmpInst::Predicate Predicate = CI->getPredicate();
844 if (MBB->isLayoutSuccessor(TrueMBB)) {
845 std::swap(TrueMBB, FalseMBB);
846 Predicate = CmpInst::getInversePredicate(Predicate);
847 }
848
Chris Lattner871d2462008-10-15 03:58:05 +0000849 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
850 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
851
Dan Gohmand98d6202008-10-02 22:15:21 +0000852 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000853 case CmpInst::FCMP_OEQ:
854 std::swap(TrueMBB, FalseMBB);
855 Predicate = CmpInst::FCMP_UNE;
856 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000857 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
858 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
859 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
860 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
861 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
862 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
863 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
864 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
865 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
866 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
867 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
868 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
869 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000870
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000871 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
872 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
873 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
874 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
875 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
876 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
877 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
878 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
879 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
880 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000881 default:
882 return false;
883 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000884
Dan Gohman46510a72010-04-15 01:51:59 +0000885 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000886 if (SwapArgs)
887 std::swap(Op0, Op1);
888
Chris Lattner9a08a612008-10-15 04:26:38 +0000889 // Emit a compare of the LHS and RHS, setting the flags.
890 if (!X86FastEmitCompare(Op0, Op1, VT))
891 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000892
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000893 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000894
895 if (Predicate == CmpInst::FCMP_UNE) {
896 // X86 requires a second branch to handle UNE (and OEQ,
897 // which is mapped to UNE above).
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000898 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000899 }
900
Stuart Hastings3bf91252010-06-17 22:43:56 +0000901 FastEmitBranch(FalseMBB, DL);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000902 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000903 return true;
904 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000905 } else if (ExtractValueInst *EI =
906 dyn_cast<ExtractValueInst>(BI->getCondition())) {
907 // Check to see if the branch instruction is from an "arithmetic with
908 // overflow" intrinsic. The main way these intrinsics are used is:
909 //
910 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
911 // %sum = extractvalue { i32, i1 } %t, 0
912 // %obit = extractvalue { i32, i1 } %t, 1
913 // br i1 %obit, label %overflow, label %normal
914 //
Dan Gohman653456c2009-01-07 00:15:08 +0000915 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000916 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000917 // looking for the SETO/SETB instruction. If an instruction modifies the
918 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
919 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000920 if (const IntrinsicInst *CI =
921 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000922 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
923 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
924 const MachineInstr *SetMI = 0;
925 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000926
Chris Lattnera9a42252009-04-12 07:36:01 +0000927 for (MachineBasicBlock::const_reverse_iterator
928 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
929 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000930
Evan Cheng1015ba72010-05-21 20:53:24 +0000931 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000932 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000933
Chris Lattnera9a42252009-04-12 07:36:01 +0000934 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
935 Reg = Src;
936 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000937 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000938
Chris Lattnera9a42252009-04-12 07:36:01 +0000939 SetMI = &MI;
940 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000941 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000942
Chris Lattnera9a42252009-04-12 07:36:01 +0000943 const TargetInstrDesc &TID = MI.getDesc();
944 if (TID.hasUnmodeledSideEffects() ||
945 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
946 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000947 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000948
949 if (SetMI) {
950 unsigned OpCode = SetMI->getOpcode();
951
952 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000953 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
954 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000955 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000956 FastEmitBranch(FalseMBB, DL);
Chris Lattnera9a42252009-04-12 07:36:01 +0000957 MBB->addSuccessor(TrueMBB);
958 return true;
959 }
Bill Wendling9a901322008-12-10 19:44:24 +0000960 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000961 }
962 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000963 }
964
965 // Otherwise do a clumsy setcc and re-test it.
966 unsigned OpReg = getRegForValue(BI->getCondition());
967 if (OpReg == 0) return false;
968
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000969 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000970 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000971 FastEmitBranch(FalseMBB, DL);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000972 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000973 return true;
974}
975
Dan Gohman46510a72010-04-15 01:51:59 +0000976bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000977 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000978 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000979 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000980 CReg = X86::CL;
981 RC = &X86::GR8RegClass;
982 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000983 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
984 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
985 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000986 default: return false;
987 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000988 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000989 CReg = X86::CX;
990 RC = &X86::GR16RegClass;
991 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000992 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
993 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
994 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000995 default: return false;
996 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000997 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000998 CReg = X86::ECX;
999 RC = &X86::GR32RegClass;
1000 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001001 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1002 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1003 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001004 default: return false;
1005 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001006 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001007 CReg = X86::RCX;
1008 RC = &X86::GR64RegClass;
1009 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001010 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1011 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1012 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001013 default: return false;
1014 }
1015 } else {
1016 return false;
1017 }
1018
Owen Andersone50ed302009-08-10 22:56:29 +00001019 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001021 return false;
1022
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001023 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1024 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001025
1026 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001027 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001028 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001029 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001030 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001031 UpdateValueMap(I, ResultReg);
1032 return true;
1033 }
1034
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001035 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1036 if (Op1Reg == 0) return false;
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001037 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001038
1039 // The shift instruction uses X86::CL. If we defined a super-register
1040 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1041 // we're doing here.
1042 if (CReg != X86::CL)
Chris Lattner518bb532010-02-09 19:54:29 +00001043 BuildMI(MBB, DL, TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001044 .addReg(CReg).addImm(X86::sub_8bit);
Dan Gohman145b8282008-10-07 21:50:36 +00001045
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001046 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001047 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001048 UpdateValueMap(I, ResultReg);
1049 return true;
1050}
1051
Dan Gohman46510a72010-04-15 01:51:59 +00001052bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001053 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001054 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001055 return false;
1056
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001057 unsigned Opc = 0;
1058 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001060 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001061 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001063 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001064 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001066 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001067 RC = &X86::GR64RegClass;
1068 } else {
1069 return false;
1070 }
1071
1072 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1073 if (Op0Reg == 0) return false;
1074 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1075 if (Op1Reg == 0) return false;
1076 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1077 if (Op2Reg == 0) return false;
1078
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001079 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001080 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001081 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001082 UpdateValueMap(I, ResultReg);
1083 return true;
1084}
1085
Dan Gohman46510a72010-04-15 01:51:59 +00001086bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001087 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001088 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001089 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001090 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001091 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001092 unsigned OpReg = getRegForValue(V);
1093 if (OpReg == 0) return false;
1094 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001095 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001096 UpdateValueMap(I, ResultReg);
1097 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001098 }
1099 }
1100
1101 return false;
1102}
1103
Dan Gohman46510a72010-04-15 01:51:59 +00001104bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001105 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001106 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001107 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001108 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001109 unsigned OpReg = getRegForValue(V);
1110 if (OpReg == 0) return false;
1111 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001112 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001113 UpdateValueMap(I, ResultReg);
1114 return true;
1115 }
1116 }
1117 }
1118
1119 return false;
1120}
1121
Dan Gohman46510a72010-04-15 01:51:59 +00001122bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001123 if (Subtarget->is64Bit())
1124 // All other cases should be handled by the tblgen generated code.
1125 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001126 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1127 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001128
1129 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001131 // All other cases should be handled by the tblgen generated code.
1132 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001133 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001134 // All other cases should be handled by the tblgen generated code.
1135 return false;
1136
1137 unsigned InputReg = getRegForValue(I->getOperand(0));
1138 if (!InputReg)
1139 // Unhandled operand. Halt "fast" selection and bail.
1140 return false;
1141
Dan Gohman62417622009-04-27 16:33:14 +00001142 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1144 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001145 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001146 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001147 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001148
1149 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001151 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001152 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001153 if (!ResultReg)
1154 return false;
1155
1156 UpdateValueMap(I, ResultReg);
1157 return true;
1158}
1159
Dan Gohman46510a72010-04-15 01:51:59 +00001160bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1161 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1162 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001163
Dan Gohman46510a72010-04-15 01:51:59 +00001164 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001165 switch (CI->getIntrinsicID()) {
1166 default: break;
1167 case Intrinsic::sadd_with_overflow:
1168 case Intrinsic::uadd_with_overflow:
1169 // Cheat a little. We know that the registers for "add" and "seto" are
1170 // allocated sequentially. However, we only keep track of the register
1171 // for "add" in the value map. Use extractvalue's index to get the
1172 // correct register for "seto".
1173 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1174 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001175 }
1176 }
1177
1178 return false;
1179}
1180
Dan Gohman46510a72010-04-15 01:51:59 +00001181bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001182 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001183 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001184 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001185 case Intrinsic::stackprotector: {
1186 // Emit code inline code to store the stack guard onto the stack.
1187 EVT PtrTy = TLI.getPointerTy();
1188
Eric Christopher551754c2010-04-16 23:37:20 +00001189 const Value *Op1 = I.getOperand(1); // The guard's value.
1190 const AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Eric Christopher07754c22010-03-18 20:27:26 +00001191
1192 // Grab the frame index.
1193 X86AddressMode AM;
1194 if (!X86SelectAddress(Slot, AM)) return false;
1195
Eric Christopher88dee302010-03-18 21:58:33 +00001196 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1197
Eric Christopher07754c22010-03-18 20:27:26 +00001198 return true;
1199 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001200 case Intrinsic::objectsize: {
Eric Christopher551754c2010-04-16 23:37:20 +00001201 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopherf27805b2010-03-11 06:20:22 +00001202 const Type *Ty = I.getCalledFunction()->getReturnType();
1203
1204 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1205
1206 EVT VT;
1207 if (!isTypeLegal(Ty, VT))
1208 return false;
1209
1210 unsigned OpC = 0;
1211 if (VT == MVT::i32)
1212 OpC = X86::MOV32ri;
1213 else if (VT == MVT::i64)
1214 OpC = X86::MOV64ri;
1215 else
1216 return false;
1217
1218 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1219 BuildMI(MBB, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001220 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001221 UpdateValueMap(&I, ResultReg);
1222 return true;
1223 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001224 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001225 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001226 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001227 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001228 if (!X86SelectAddress(DI->getAddress(), AM))
1229 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001230 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001231 // FIXME may need to add RegState::Debug to any registers produced,
1232 // although ESP/EBP should be the only ones at the moment.
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001233 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1234 addMetadata(DI->getVariable());
1235 return true;
1236 }
Eric Christopher77f79892010-01-18 22:11:29 +00001237 case Intrinsic::trap: {
1238 BuildMI(MBB, DL, TII.get(X86::TRAP));
1239 return true;
1240 }
Bill Wendling52370a12008-12-09 02:42:50 +00001241 case Intrinsic::sadd_with_overflow:
1242 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001243 // Replace "add with overflow" intrinsics with an "add" instruction followed
1244 // by a seto/setc instruction. Later on, when the "extractvalue"
1245 // instructions are encountered, we use the fact that two registers were
1246 // created sequentially to get the correct registers for the "sum" and the
1247 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001248 const Function *Callee = I.getCalledFunction();
1249 const Type *RetTy =
1250 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1251
Owen Andersone50ed302009-08-10 22:56:29 +00001252 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001253 if (!isTypeLegal(RetTy, VT))
1254 return false;
1255
Eric Christopher551754c2010-04-16 23:37:20 +00001256 const Value *Op1 = I.getOperand(1);
1257 const Value *Op2 = I.getOperand(2);
Bill Wendling52370a12008-12-09 02:42:50 +00001258 unsigned Reg1 = getRegForValue(Op1);
1259 unsigned Reg2 = getRegForValue(Op2);
1260
1261 if (Reg1 == 0 || Reg2 == 0)
1262 // FIXME: Handle values *not* in registers.
1263 return false;
1264
1265 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001267 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001269 OpC = X86::ADD64rr;
1270 else
1271 return false;
1272
1273 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001274 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001275 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001276
Chris Lattner8d57b772009-04-12 07:51:14 +00001277 // If the add with overflow is an intra-block value then we just want to
1278 // create temporaries for it like normal. If it is a cross-block value then
1279 // UpdateValueMap will return the cross-block register used. Since we
1280 // *really* want the value to be live in the register pair known by
1281 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1282 // the cross block case. In the non-cross-block case, we should just make
1283 // another register for the value.
1284 if (DestReg1 != ResultReg)
1285 ResultReg = DestReg1+1;
1286 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001287 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001288
Chris Lattnera9a42252009-04-12 07:36:01 +00001289 unsigned Opc = X86::SETBr;
1290 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1291 Opc = X86::SETOr;
1292 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001293 return true;
1294 }
1295 }
1296}
1297
Dan Gohman46510a72010-04-15 01:51:59 +00001298bool X86FastISel::X86SelectCall(const Instruction *I) {
1299 const CallInst *CI = cast<CallInst>(I);
Eric Christopher551754c2010-04-16 23:37:20 +00001300 const Value *Callee = I->getOperand(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001301
1302 // Can't handle inline asm yet.
1303 if (isa<InlineAsm>(Callee))
1304 return false;
1305
Bill Wendling52370a12008-12-09 02:42:50 +00001306 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001307 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001308 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001309
Evan Chengf3d4efe2008-09-07 09:09:33 +00001310 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001311 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001312 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001313 if (CC != CallingConv::C &&
1314 CC != CallingConv::Fast &&
1315 CC != CallingConv::X86_FastCall)
1316 return false;
1317
Evan Cheng381993f2010-01-27 00:00:57 +00001318 // fastcc with -tailcallopt is intended to provide a guaranteed
1319 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001320 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001321 return false;
1322
Evan Chengf3d4efe2008-09-07 09:09:33 +00001323 // Let SDISel handle vararg functions.
1324 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1325 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1326 if (FTy->isVarArg())
1327 return false;
1328
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001329 // Fast-isel doesn't know about callee-pop yet.
1330 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1331 return false;
1332
Evan Chengf3d4efe2008-09-07 09:09:33 +00001333 // Handle *simple* calls for now.
1334 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001335 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001336 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001338 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001339 return false;
1340
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001341 // Materialize callee address in a register. FIXME: GV address can be
1342 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001343 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001344 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001345 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001346 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001347 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001348 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001349 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001350 } else if (CalleeAM.Base.Reg != 0) {
1351 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001352 } else
1353 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001354
Evan Chengdebdea02008-09-08 17:15:42 +00001355 // Allow calls which produce i1 results.
1356 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001357 if (RetVT == MVT::i1) {
1358 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001359 AndToI1 = true;
1360 }
1361
Evan Chengf3d4efe2008-09-07 09:09:33 +00001362 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001363 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001364 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001365 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001366 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001367 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001368 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001369 ArgVTs.reserve(CS.arg_size());
1370 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001371 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001372 i != e; ++i) {
1373 unsigned Arg = getRegForValue(*i);
1374 if (Arg == 0)
1375 return false;
1376 ISD::ArgFlagsTy Flags;
1377 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001378 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001379 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001380 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001381 Flags.setZExt();
1382
1383 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001384 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1385 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1386 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1387 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001388 return false;
1389
1390 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001391 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001392 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001393 return false;
1394 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1395 Flags.setOrigAlign(OriginalAlignment);
1396
1397 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001398 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001399 ArgVTs.push_back(ArgVT);
1400 ArgFlags.push_back(Flags);
1401 }
1402
1403 // Analyze operands of the call, assigning locations to each operand.
1404 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001405 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001406
1407 // Allocate shadow area for Win64
1408 if (Subtarget->isTargetWin64()) {
1409 CCInfo.AllocateStack(32, 8);
1410 }
1411
Evan Chengf3d4efe2008-09-07 09:09:33 +00001412 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1413
1414 // Get a count of how many bytes are to be pushed on the stack.
1415 unsigned NumBytes = CCInfo.getNextStackOffset();
1416
1417 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001418 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001419 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001420
Chris Lattner438949a2008-10-15 05:30:52 +00001421 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001422 // copies / loads.
1423 SmallVector<unsigned, 4> RegArgs;
1424 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1425 CCValAssign &VA = ArgLocs[i];
1426 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001427 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001428
1429 // Promote the value if needed.
1430 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001431 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001432 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001433 case CCValAssign::SExt: {
1434 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1435 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001436 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001437 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001438 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001439 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001440 }
1441 case CCValAssign::ZExt: {
1442 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1443 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001444 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001445 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001446 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001447 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001448 }
1449 case CCValAssign::AExt: {
1450 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1451 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001452 if (!Emitted)
1453 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001454 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001455 if (!Emitted)
1456 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1457 Arg, ArgVT, Arg);
1458
Chris Lattnera33649e2008-12-19 17:03:38 +00001459 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001460 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001461 break;
1462 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001463 case CCValAssign::BCvt: {
1464 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001465 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001466 assert(BC != 0 && "Failed to emit a bitcast!");
1467 Arg = BC;
1468 ArgVT = VA.getLocVT();
1469 break;
1470 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001471 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001472
1473 if (VA.isRegLoc()) {
1474 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1475 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001476 Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001477 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001478 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 RegArgs.push_back(VA.getLocReg());
1480 } else {
1481 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001482 X86AddressMode AM;
1483 AM.Base.Reg = StackPtr;
1484 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001485 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001486
1487 // If this is a really simple value, emit this with the Value* version of
1488 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1489 // can cause us to reevaluate the argument.
1490 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1491 X86FastEmitStore(ArgVT, ArgVal, AM);
1492 else
1493 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001494 }
1495 }
1496
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001497 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1498 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001499 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001500 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001501 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001502 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC,
1503 DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001504 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001505 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001506 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001507
Evan Chengf3d4efe2008-09-07 09:09:33 +00001508 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001509 MachineInstrBuilder MIB;
1510 if (CalleeOp) {
1511 // Register-indirect call.
1512 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1513 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1514
1515 } else {
1516 // Direct call.
1517 assert(GV && "Not a direct call");
1518 unsigned CallOpc =
1519 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1520
1521 // See if we need any target-specific flags on the GV operand.
1522 unsigned char OpFlags = 0;
1523
1524 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1525 // external symbols most go through the PLT in PIC mode. If the symbol
1526 // has hidden or protected visibility, or if it is static or local, then
1527 // we don't need to use the PLT - we can directly call it.
1528 if (Subtarget->isTargetELF() &&
1529 TM.getRelocationModel() == Reloc::PIC_ &&
1530 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1531 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001532 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001533 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1534 Subtarget->getDarwinVers() < 9) {
1535 // PC-relative references to external symbols should go through $stub,
1536 // unless we're building with the leopard linker or later, which
1537 // automatically synthesizes these stubs.
1538 OpFlags = X86II::MO_DARWIN_STUB;
1539 }
1540
1541
1542 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1543 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001544
1545 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001546 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001547 MIB.addReg(X86::EBX);
1548
Evan Chengf3d4efe2008-09-07 09:09:33 +00001549 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001550 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1551 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001552
1553 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001554 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001555 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001556
1557 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001558 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001561 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1563
1564 // Copy all of the result registers out of their specified physreg.
1565 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001566 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001567 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1568 TargetRegisterClass *SrcRC = DstRC;
1569
1570 // If this is a call to a function that returns an fp value on the x87 fp
1571 // stack, but where we prefer to use the value in xmm registers, copy it
1572 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1573 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1574 RVLocs[0].getLocReg() == X86::ST1) &&
1575 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 SrcRC = X86::RSTRegisterClass;
1578 DstRC = X86::RFP80RegisterClass;
1579 }
1580
1581 unsigned ResultReg = createResultReg(DstRC);
1582 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001583 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001584 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001585 Emitted = true;
Dan Gohmandb497122010-06-18 23:28:01 +00001586 UsedRegs.push_back(RVLocs[0].getLocReg());
1587
Evan Chengf3d4efe2008-09-07 09:09:33 +00001588 if (CopyVT != RVLocs[0].getValVT()) {
1589 // Round the F80 the right size, which also moves to the appropriate xmm
1590 // register. This is accomplished by storing the F80 value in memory and
1591 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001592 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001594 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001595 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001596 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001598 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001600 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001601 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001602 }
1603
Evan Chengdebdea02008-09-08 17:15:42 +00001604 if (AndToI1) {
1605 // Mask out all but lowest bit for some call which produces an i1.
1606 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001607 BuildMI(MBB, DL,
1608 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001609 ResultReg = AndResult;
1610 }
1611
Evan Chengf3d4efe2008-09-07 09:09:33 +00001612 UpdateValueMap(I, ResultReg);
1613 }
1614
Dan Gohmandb497122010-06-18 23:28:01 +00001615 // Set all unused physreg defs as dead.
1616 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1617
Evan Chengf3d4efe2008-09-07 09:09:33 +00001618 return true;
1619}
1620
1621
Dan Gohman99b21822008-08-28 23:21:34 +00001622bool
Dan Gohman46510a72010-04-15 01:51:59 +00001623X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001624 switch (I->getOpcode()) {
1625 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001626 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001627 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001628 case Instruction::Store:
1629 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001630 case Instruction::ICmp:
1631 case Instruction::FCmp:
1632 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001633 case Instruction::ZExt:
1634 return X86SelectZExt(I);
1635 case Instruction::Br:
1636 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001637 case Instruction::Call:
1638 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001639 case Instruction::LShr:
1640 case Instruction::AShr:
1641 case Instruction::Shl:
1642 return X86SelectShift(I);
1643 case Instruction::Select:
1644 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001645 case Instruction::Trunc:
1646 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001647 case Instruction::FPExt:
1648 return X86SelectFPExt(I);
1649 case Instruction::FPTrunc:
1650 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001651 case Instruction::ExtractValue:
1652 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001653 case Instruction::IntToPtr: // Deliberate fall-through.
1654 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001655 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1656 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001657 if (DstVT.bitsGT(SrcVT))
1658 return X86SelectZExt(I);
1659 if (DstVT.bitsLT(SrcVT))
1660 return X86SelectTrunc(I);
1661 unsigned Reg = getRegForValue(I->getOperand(0));
1662 if (Reg == 0) return false;
1663 UpdateValueMap(I, Reg);
1664 return true;
1665 }
Dan Gohman99b21822008-08-28 23:21:34 +00001666 }
1667
1668 return false;
1669}
1670
Dan Gohman46510a72010-04-15 01:51:59 +00001671unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001672 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001673 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001674 return false;
1675
1676 // Get opcode and regclass of the output for the given load instruction.
1677 unsigned Opc = 0;
1678 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001679 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001680 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001681 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001682 Opc = X86::MOV8rm;
1683 RC = X86::GR8RegisterClass;
1684 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001686 Opc = X86::MOV16rm;
1687 RC = X86::GR16RegisterClass;
1688 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001690 Opc = X86::MOV32rm;
1691 RC = X86::GR32RegisterClass;
1692 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001694 // Must be in x86-64 mode.
1695 Opc = X86::MOV64rm;
1696 RC = X86::GR64RegisterClass;
1697 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001699 if (Subtarget->hasSSE1()) {
1700 Opc = X86::MOVSSrm;
1701 RC = X86::FR32RegisterClass;
1702 } else {
1703 Opc = X86::LD_Fp32m;
1704 RC = X86::RFP32RegisterClass;
1705 }
1706 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001708 if (Subtarget->hasSSE2()) {
1709 Opc = X86::MOVSDrm;
1710 RC = X86::FR64RegisterClass;
1711 } else {
1712 Opc = X86::LD_Fp64m;
1713 RC = X86::RFP64RegisterClass;
1714 }
1715 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001717 // No f80 support yet.
1718 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001719 }
1720
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001721 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001722 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001723 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001724 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001726 Opc = X86::LEA32r;
1727 else
1728 Opc = X86::LEA64r;
1729 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001730 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001731 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001732 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001733 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001734 }
1735
Owen Anderson3b217c62008-09-06 01:11:01 +00001736 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001737 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001738 if (Align == 0) {
1739 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001740 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001741 }
Owen Anderson95267a12008-09-05 00:06:23 +00001742
Dan Gohman5396c992008-09-30 01:21:32 +00001743 // x86-32 PIC requires a PIC base register for constant pools.
1744 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001745 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001746 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001747 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1748 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1749 } else if (Subtarget->isPICStyleGOT()) {
1750 OpFlag = X86II::MO_GOTOFF;
1751 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1752 } else if (Subtarget->isPICStyleRIPRel() &&
1753 TM.getCodeModel() == CodeModel::Small) {
1754 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001755 }
Dan Gohman5396c992008-09-30 01:21:32 +00001756
1757 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001758 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001759 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001760 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1761 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001762
Owen Anderson95267a12008-09-05 00:06:23 +00001763 return ResultReg;
1764}
1765
Dan Gohman46510a72010-04-15 01:51:59 +00001766unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001767 // Fail on dynamic allocas. At this point, getRegForValue has already
1768 // checked its CSE maps, so if we're here trying to handle a dynamic
1769 // alloca, we're not going to succeed. X86SelectAddress has a
1770 // check for dynamic allocas, because it's called directly from
1771 // various places, but TargetMaterializeAlloca also needs a check
1772 // in order to avoid recursion between getRegForValue,
1773 // X86SelectAddrss, and TargetMaterializeAlloca.
1774 if (!StaticAllocaMap.count(C))
1775 return 0;
1776
Dan Gohman0586d912008-09-10 20:11:02 +00001777 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001778 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001779 return 0;
1780 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1781 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1782 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001783 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001784 return ResultReg;
1785}
1786
Evan Chengc3f44b02008-09-03 00:03:49 +00001787namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001788 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1789 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001790 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00001791 DenseMap<const AllocaInst *, int> &am,
1792 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001793#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00001794 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001795#endif
1796 ) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00001797 return new X86FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001798#ifndef NDEBUG
1799 , cil
1800#endif
1801 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001802 }
Dan Gohman99b21822008-08-28 23:21:34 +00001803}