blob: 953ba18212437f2c2af22d7c3c8d3f4b0a6c3d5a [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;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000183 else if (CC == CallingConv::Fast)
184 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000185 else if (CC == CallingConv::GHC)
186 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000187 else
188 return CC_X86_32_C;
189}
190
Evan Cheng0de588f2008-09-05 21:00:03 +0000191/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000192/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000193/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000194bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 unsigned &ResultReg) {
196 // Get opcode and regclass of the output for the given load instruction.
197 unsigned Opc = 0;
198 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000201 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 Opc = X86::MOV8rm;
204 RC = X86::GR8RegisterClass;
205 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 Opc = X86::MOV16rm;
208 RC = X86::GR16RegisterClass;
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 Opc = X86::MOV32rm;
212 RC = X86::GR32RegisterClass;
213 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 // Must be in x86-64 mode.
216 Opc = X86::MOV64rm;
217 RC = X86::GR64RegisterClass;
218 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 if (Subtarget->hasSSE1()) {
221 Opc = X86::MOVSSrm;
222 RC = X86::FR32RegisterClass;
223 } else {
224 Opc = X86::LD_Fp32m;
225 RC = X86::RFP32RegisterClass;
226 }
227 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 if (Subtarget->hasSSE2()) {
230 Opc = X86::MOVSDrm;
231 RC = X86::FR64RegisterClass;
232 } else {
233 Opc = X86::LD_Fp64m;
234 RC = X86::RFP64RegisterClass;
235 }
236 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000238 // No f80 support yet.
239 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000240 }
241
242 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000243 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 return true;
245}
246
Evan Chengf3d4efe2008-09-07 09:09:33 +0000247/// X86FastEmitStore - Emit a machine instruction to store a value Val of
248/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
249/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000250/// i.e. V. Return true if it is possible.
251bool
Owen Andersone50ed302009-08-10 22:56:29 +0000252X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000253 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000254 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 switch (VT.getSimpleVT().SimpleTy) {
257 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000258 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000259 case MVT::i1: {
260 // Mask out all but lowest bit.
261 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
262 BuildMI(MBB, DL,
263 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
264 Val = AndResult;
265 }
266 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 case MVT::i8: Opc = X86::MOV8mr; break;
268 case MVT::i16: Opc = X86::MOV16mr; break;
269 case MVT::i32: Opc = X86::MOV32mr; break;
270 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
271 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000272 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000273 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000275 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000276 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 }
Chris Lattner438949a2008-10-15 05:30:52 +0000278
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000279 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000280 return true;
281}
282
Dan Gohman46510a72010-04-15 01:51:59 +0000283bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000284 const X86AddressMode &AM) {
285 // Handle 'null' like i32/i64 0.
286 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000287 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000288
289 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000290 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000291 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000292 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000294 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000295 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 case MVT::i8: Opc = X86::MOV8mi; break;
297 case MVT::i16: Opc = X86::MOV16mi; break;
298 case MVT::i32: Opc = X86::MOV32mi; break;
299 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000300 // Must be a 32-bit sign extended value.
301 if ((int)CI->getSExtValue() == CI->getSExtValue())
302 Opc = X86::MOV64mi32;
303 break;
304 }
305
306 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000307 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000308 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000309 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000310 return true;
311 }
312 }
313
314 unsigned ValReg = getRegForValue(Val);
315 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000316 return false;
317
318 return X86FastEmitStore(VT, ValReg, AM);
319}
320
Evan Cheng24e3a902008-09-08 06:35:17 +0000321/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
322/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
323/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000324bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
325 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000326 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
328 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000329
330 if (RR != 0) {
331 ResultReg = RR;
332 return true;
333 } else
334 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000335}
336
Dan Gohman0586d912008-09-10 20:11:02 +0000337/// X86SelectAddress - Attempt to fill in an address from the given value.
338///
Dan Gohman46510a72010-04-15 01:51:59 +0000339bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
340 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000341 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000342 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000343 Opcode = I->getOpcode();
344 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000345 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000346 Opcode = C->getOpcode();
347 U = C;
348 }
Dan Gohman0586d912008-09-10 20:11:02 +0000349
Dan Gohman35893082008-09-18 23:23:44 +0000350 switch (Opcode) {
351 default: break;
352 case Instruction::BitCast:
353 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000354 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000355
356 case Instruction::IntToPtr:
357 // Look past no-op inttoptrs.
358 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000359 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000360 break;
Dan Gohman35893082008-09-18 23:23:44 +0000361
362 case Instruction::PtrToInt:
363 // Look past no-op ptrtoints.
364 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000365 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000366 break;
Dan Gohman35893082008-09-18 23:23:44 +0000367
368 case Instruction::Alloca: {
369 // Do static allocas.
370 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000371 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000372 if (SI != StaticAllocaMap.end()) {
373 AM.BaseType = X86AddressMode::FrameIndexBase;
374 AM.Base.FrameIndex = SI->second;
375 return true;
376 }
377 break;
Dan Gohman35893082008-09-18 23:23:44 +0000378 }
379
380 case Instruction::Add: {
381 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000382 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000383 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
384 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000385 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000386 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000387 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000388 }
Dan Gohman0586d912008-09-10 20:11:02 +0000389 }
Dan Gohman35893082008-09-18 23:23:44 +0000390 break;
391 }
392
393 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000394 X86AddressMode SavedAM = AM;
395
Dan Gohman35893082008-09-18 23:23:44 +0000396 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000397 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000398 unsigned IndexReg = AM.IndexReg;
399 unsigned Scale = AM.Scale;
400 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000401 // Iterate through the indices, folding what we can. Constants can be
402 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000403 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000404 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000405 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000406 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
407 const StructLayout *SL = TD.getStructLayout(STy);
408 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
409 Disp += SL->getElementOffset(Idx);
410 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000411 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman46510a72010-04-15 01:51:59 +0000412 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Dan Gohman35893082008-09-18 23:23:44 +0000413 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000414 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000415 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000416 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000417 (S == 1 || S == 2 || S == 4 || S == 8)) {
418 // Scaled-index addressing.
419 Scale = S;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000420 IndexReg = getRegForGEPIndex(Op).first;
Dan Gohman35893082008-09-18 23:23:44 +0000421 if (IndexReg == 0)
422 return false;
423 } else
424 // Unsupported.
425 goto unsupported_gep;
426 }
427 }
Dan Gohman09aae462008-09-26 20:04:15 +0000428 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000429 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000430 break;
Dan Gohman35893082008-09-18 23:23:44 +0000431 // Ok, the GEP indices were covered by constant-offset and scaled-index
432 // addressing. Update the address state and move on to examining the base.
433 AM.IndexReg = IndexReg;
434 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000435 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000436 if (X86SelectAddress(U->getOperand(0), AM))
437 return true;
438
439 // If we couldn't merge the sub value into this addr mode, revert back to
440 // our address and just match the value instead of completely failing.
441 AM = SavedAM;
442 break;
Dan Gohman35893082008-09-18 23:23:44 +0000443 unsupported_gep:
444 // Ok, the GEP indices weren't all covered.
445 break;
446 }
447 }
448
449 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000450 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000451 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000452 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000453 return false;
454
Dan Gohman97135e12008-09-26 19:15:30 +0000455 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000456 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000457 (AM.Base.Reg != 0 || AM.IndexReg != 0))
458 return false;
459
Dan Gohmane9865942009-02-23 22:03:08 +0000460 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000461 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000462 if (GVar->isThreadLocal())
463 return false;
464
Chris Lattnerff7727f2009-07-09 06:41:35 +0000465 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000466 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000467
Chris Lattner0d786dd2009-07-10 07:48:51 +0000468 // Allow the subtarget to classify the global.
469 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
470
471 // If this reference is relative to the pic base, set it now.
472 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000473 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000474 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000475 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000476
477 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000478 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000479 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000480 if (Subtarget->isPICStyleRIPRel()) {
481 // Use rip-relative addressing if we can. Above we verified that the
482 // base and index registers are unused.
483 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
484 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000485 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000486 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000487 return true;
488 }
489
Chris Lattner0d786dd2009-07-10 07:48:51 +0000490 // Ok, we need to do a load from a stub. If we've already loaded from this
491 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000492 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
493 unsigned LoadReg;
494 if (I != LocalValueMap.end() && I->second != 0) {
495 LoadReg = I->second;
496 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000497 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000498 unsigned Opc = 0;
499 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000500 X86AddressMode StubAM;
501 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000502 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000503 StubAM.GVOpFlags = GVFlags;
504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000506 Opc = X86::MOV64rm;
507 RC = X86::GR64RegisterClass;
508
Chris Lattner0d786dd2009-07-10 07:48:51 +0000509 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000510 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000511 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000512 Opc = X86::MOV32rm;
513 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000514 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000515
516 LoadReg = createResultReg(RC);
517 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
518
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000519 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000520 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000521 }
Chris Lattner18c59872009-06-27 04:16:01 +0000522
Chris Lattnerff7727f2009-07-09 06:41:35 +0000523 // Now construct the final address. Note that the Disp, Scale,
524 // and Index values may already be set here.
525 AM.Base.Reg = LoadReg;
526 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000527 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000528 }
529
Dan Gohman97135e12008-09-26 19:15:30 +0000530 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000531 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000532 if (AM.Base.Reg == 0) {
533 AM.Base.Reg = getRegForValue(V);
534 return AM.Base.Reg != 0;
535 }
536 if (AM.IndexReg == 0) {
537 assert(AM.Scale == 1 && "Scale with no index!");
538 AM.IndexReg = getRegForValue(V);
539 return AM.IndexReg != 0;
540 }
541 }
542
543 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000544}
545
Chris Lattner0aa43de2009-07-10 05:33:42 +0000546/// X86SelectCallAddress - Attempt to fill in an address from the given value.
547///
Dan Gohman46510a72010-04-15 01:51:59 +0000548bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
549 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000550 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000551 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000552 Opcode = I->getOpcode();
553 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000554 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000555 Opcode = C->getOpcode();
556 U = C;
557 }
558
559 switch (Opcode) {
560 default: break;
561 case Instruction::BitCast:
562 // Look past bitcasts.
563 return X86SelectCallAddress(U->getOperand(0), AM);
564
565 case Instruction::IntToPtr:
566 // Look past no-op inttoptrs.
567 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
568 return X86SelectCallAddress(U->getOperand(0), AM);
569 break;
570
571 case Instruction::PtrToInt:
572 // Look past no-op ptrtoints.
573 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
574 return X86SelectCallAddress(U->getOperand(0), AM);
575 break;
576 }
577
578 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000579 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000580 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000581 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000582 return false;
583
584 // RIP-relative addresses can't have additional register operands.
585 if (Subtarget->isPICStyleRIPRel() &&
586 (AM.Base.Reg != 0 || AM.IndexReg != 0))
587 return false;
588
Chris Lattner754b7652009-07-10 05:48:03 +0000589 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000590 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000591 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000592 return false;
593
594 // Okay, we've committed to selecting this global. Set up the basic address.
595 AM.GV = GV;
596
Chris Lattnere6c07b52009-07-10 05:45:15 +0000597 // No ABI requires an extra load for anything other than DLLImport, which
598 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000599 if (Subtarget->isPICStyleRIPRel()) {
600 // Use rip-relative addressing if we can. Above we verified that the
601 // base and index registers are unused.
602 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
603 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000604 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000605 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
606 } else if (Subtarget->isPICStyleGOT()) {
607 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000608 }
609
Chris Lattner0aa43de2009-07-10 05:33:42 +0000610 return true;
611 }
612
613 // If all else fails, try to materialize the value in a register.
614 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
615 if (AM.Base.Reg == 0) {
616 AM.Base.Reg = getRegForValue(V);
617 return AM.Base.Reg != 0;
618 }
619 if (AM.IndexReg == 0) {
620 assert(AM.Scale == 1 && "Scale with no index!");
621 AM.IndexReg = getRegForValue(V);
622 return AM.IndexReg != 0;
623 }
624 }
625
626 return false;
627}
628
629
Owen Andersona3971df2008-09-04 07:08:58 +0000630/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000631bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000632 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000633 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000634 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000635
Dan Gohman0586d912008-09-10 20:11:02 +0000636 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000637 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000638 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000639
Chris Lattner438949a2008-10-15 05:30:52 +0000640 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000641}
642
Evan Cheng8b19e562008-09-03 06:44:39 +0000643/// X86SelectLoad - Select and emit code to implement load instructions.
644///
Dan Gohman46510a72010-04-15 01:51:59 +0000645bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000646 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000647 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000648 return false;
649
Dan Gohman0586d912008-09-10 20:11:02 +0000650 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000651 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000652 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000653
Evan Cheng0de588f2008-09-05 21:00:03 +0000654 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000655 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000656 UpdateValueMap(I, ResultReg);
657 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000658 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000659 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000660}
661
Owen Andersone50ed302009-08-10 22:56:29 +0000662static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000664 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 case MVT::i8: return X86::CMP8rr;
666 case MVT::i16: return X86::CMP16rr;
667 case MVT::i32: return X86::CMP32rr;
668 case MVT::i64: return X86::CMP64rr;
669 case MVT::f32: return X86::UCOMISSrr;
670 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000671 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000672}
673
Chris Lattner0e13c782008-10-15 04:13:29 +0000674/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
675/// of the comparison, return an opcode that works for the compare (e.g.
676/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000677static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000679 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000680 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 case MVT::i8: return X86::CMP8ri;
682 case MVT::i16: return X86::CMP16ri;
683 case MVT::i32: return X86::CMP32ri;
684 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000685 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
686 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000687 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000688 return X86::CMP64ri32;
689 return 0;
690 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000691}
692
Dan Gohman46510a72010-04-15 01:51:59 +0000693bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
694 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000695 unsigned Op0Reg = getRegForValue(Op0);
696 if (Op0Reg == 0) return false;
697
Chris Lattnerd53886b2008-10-15 05:18:04 +0000698 // Handle 'null' like i32/i64 0.
699 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000700 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000701
Chris Lattner9a08a612008-10-15 04:26:38 +0000702 // We have two options: compare with register or immediate. If the RHS of
703 // the compare is an immediate that we can fold into this compare, use
704 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000705 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000706 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000707 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000708 .addImm(Op1C->getSExtValue());
709 return true;
710 }
711 }
712
713 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
714 if (CompareOpc == 0) return false;
715
716 unsigned Op1Reg = getRegForValue(Op1);
717 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000718 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000719
720 return true;
721}
722
Dan Gohman46510a72010-04-15 01:51:59 +0000723bool X86FastISel::X86SelectCmp(const Instruction *I) {
724 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000725
Owen Andersone50ed302009-08-10 22:56:29 +0000726 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000727 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000728 return false;
729
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000730 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000731 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000732 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000733 switch (CI->getPredicate()) {
734 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000735 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
736 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000737
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000738 unsigned EReg = createResultReg(&X86::GR8RegClass);
739 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000740 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
741 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
742 BuildMI(MBB, DL,
743 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000744 UpdateValueMap(I, ResultReg);
745 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000746 }
747 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000748 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
749 return false;
750
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000751 unsigned NEReg = createResultReg(&X86::GR8RegClass);
752 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000753 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
754 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
755 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000756 UpdateValueMap(I, ResultReg);
757 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000758 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000759 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
760 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
761 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
762 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
763 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
764 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
765 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
766 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
767 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
768 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
769 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
770 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
771
772 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
773 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
774 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
775 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
776 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
777 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
778 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
779 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
780 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
781 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000782 default:
783 return false;
784 }
785
Dan Gohman46510a72010-04-15 01:51:59 +0000786 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000787 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000788 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000789
Chris Lattner9a08a612008-10-15 04:26:38 +0000790 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000791 if (!X86FastEmitCompare(Op0, Op1, VT))
792 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000793
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000794 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000795 UpdateValueMap(I, ResultReg);
796 return true;
797}
Evan Cheng8b19e562008-09-03 06:44:39 +0000798
Dan Gohman46510a72010-04-15 01:51:59 +0000799bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000800 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000801 if (I->getType()->isIntegerTy(8) &&
802 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000803 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000804 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000805 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000806 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000807 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000808 UpdateValueMap(I, ResultReg);
809 return true;
810 }
811
812 return false;
813}
814
Chris Lattner9a08a612008-10-15 04:26:38 +0000815
Dan Gohman46510a72010-04-15 01:51:59 +0000816bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000817 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000818 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000819 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000820 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
821 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
822
Dan Gohmand98d6202008-10-02 22:15:21 +0000823 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000824 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000825 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000826 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000827
Dan Gohmand98d6202008-10-02 22:15:21 +0000828 // Try to take advantage of fallthrough opportunities.
829 CmpInst::Predicate Predicate = CI->getPredicate();
830 if (MBB->isLayoutSuccessor(TrueMBB)) {
831 std::swap(TrueMBB, FalseMBB);
832 Predicate = CmpInst::getInversePredicate(Predicate);
833 }
834
Chris Lattner871d2462008-10-15 03:58:05 +0000835 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
836 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
837
Dan Gohmand98d6202008-10-02 22:15:21 +0000838 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000839 case CmpInst::FCMP_OEQ:
840 std::swap(TrueMBB, FalseMBB);
841 Predicate = CmpInst::FCMP_UNE;
842 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000843 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
844 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
845 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
846 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
847 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
848 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
849 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
850 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
851 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
852 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
853 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
854 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
855 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000856
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000857 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
858 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
859 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
860 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
861 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
862 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
863 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
864 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
865 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
866 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000867 default:
868 return false;
869 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000870
Dan Gohman46510a72010-04-15 01:51:59 +0000871 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000872 if (SwapArgs)
873 std::swap(Op0, Op1);
874
Chris Lattner9a08a612008-10-15 04:26:38 +0000875 // Emit a compare of the LHS and RHS, setting the flags.
876 if (!X86FastEmitCompare(Op0, Op1, VT))
877 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000878
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000879 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000880
881 if (Predicate == CmpInst::FCMP_UNE) {
882 // X86 requires a second branch to handle UNE (and OEQ,
883 // which is mapped to UNE above).
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000884 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000885 }
886
Dan Gohmand98d6202008-10-02 22:15:21 +0000887 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000888 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000889 return true;
890 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000891 } else if (ExtractValueInst *EI =
892 dyn_cast<ExtractValueInst>(BI->getCondition())) {
893 // Check to see if the branch instruction is from an "arithmetic with
894 // overflow" intrinsic. The main way these intrinsics are used is:
895 //
896 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
897 // %sum = extractvalue { i32, i1 } %t, 0
898 // %obit = extractvalue { i32, i1 } %t, 1
899 // br i1 %obit, label %overflow, label %normal
900 //
Dan Gohman653456c2009-01-07 00:15:08 +0000901 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000902 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000903 // looking for the SETO/SETB instruction. If an instruction modifies the
904 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
905 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000906 if (const IntrinsicInst *CI =
907 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000908 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
909 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
910 const MachineInstr *SetMI = 0;
911 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000912
Chris Lattnera9a42252009-04-12 07:36:01 +0000913 for (MachineBasicBlock::const_reverse_iterator
914 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
915 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000916
Chris Lattnera9a42252009-04-12 07:36:01 +0000917 if (MI.modifiesRegister(Reg)) {
918 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000919
Chris Lattnera9a42252009-04-12 07:36:01 +0000920 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
921 Reg = Src;
922 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000923 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000924
Chris Lattnera9a42252009-04-12 07:36:01 +0000925 SetMI = &MI;
926 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000927 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000928
Chris Lattnera9a42252009-04-12 07:36:01 +0000929 const TargetInstrDesc &TID = MI.getDesc();
930 if (TID.hasUnmodeledSideEffects() ||
931 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
932 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000933 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000934
935 if (SetMI) {
936 unsigned OpCode = SetMI->getOpcode();
937
938 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000939 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
940 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000941 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000942 FastEmitBranch(FalseMBB);
943 MBB->addSuccessor(TrueMBB);
944 return true;
945 }
Bill Wendling9a901322008-12-10 19:44:24 +0000946 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000947 }
948 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000949 }
950
951 // Otherwise do a clumsy setcc and re-test it.
952 unsigned OpReg = getRegForValue(BI->getCondition());
953 if (OpReg == 0) return false;
954
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000955 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000956 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000957 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000958 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000959 return true;
960}
961
Dan Gohman46510a72010-04-15 01:51:59 +0000962bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000963 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000964 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000965 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000966 CReg = X86::CL;
967 RC = &X86::GR8RegClass;
968 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000969 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
970 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
971 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000972 default: return false;
973 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000974 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000975 CReg = X86::CX;
976 RC = &X86::GR16RegClass;
977 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000978 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
979 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
980 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000981 default: return false;
982 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000983 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000984 CReg = X86::ECX;
985 RC = &X86::GR32RegClass;
986 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000987 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
988 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
989 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000990 default: return false;
991 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000992 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000993 CReg = X86::RCX;
994 RC = &X86::GR64RegClass;
995 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000996 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
997 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
998 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000999 default: return false;
1000 }
1001 } else {
1002 return false;
1003 }
1004
Owen Andersone50ed302009-08-10 22:56:29 +00001005 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001006 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001007 return false;
1008
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001009 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1010 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001011
1012 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001013 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001014 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001015 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001016 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001017 UpdateValueMap(I, ResultReg);
1018 return true;
1019 }
1020
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001021 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1022 if (Op1Reg == 0) return false;
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001023 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001024
1025 // The shift instruction uses X86::CL. If we defined a super-register
1026 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1027 // we're doing here.
1028 if (CReg != X86::CL)
Chris Lattner518bb532010-02-09 19:54:29 +00001029 BuildMI(MBB, DL, TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001030 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1031
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001032 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001033 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001034 UpdateValueMap(I, ResultReg);
1035 return true;
1036}
1037
Dan Gohman46510a72010-04-15 01:51:59 +00001038bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001039 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001041 return false;
1042
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001043 unsigned Opc = 0;
1044 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001045 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001046 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001047 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001048 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001049 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001050 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001052 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001053 RC = &X86::GR64RegClass;
1054 } else {
1055 return false;
1056 }
1057
1058 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1059 if (Op0Reg == 0) return false;
1060 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1061 if (Op1Reg == 0) return false;
1062 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1063 if (Op2Reg == 0) return false;
1064
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001065 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001066 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001067 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001068 UpdateValueMap(I, ResultReg);
1069 return true;
1070}
1071
Dan Gohman46510a72010-04-15 01:51:59 +00001072bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001073 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001074 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001075 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001076 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001077 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001078 unsigned OpReg = getRegForValue(V);
1079 if (OpReg == 0) return false;
1080 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001081 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001082 UpdateValueMap(I, ResultReg);
1083 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001084 }
1085 }
1086
1087 return false;
1088}
1089
Dan Gohman46510a72010-04-15 01:51:59 +00001090bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001091 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001092 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001093 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001094 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001095 unsigned OpReg = getRegForValue(V);
1096 if (OpReg == 0) return false;
1097 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001098 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001099 UpdateValueMap(I, ResultReg);
1100 return true;
1101 }
1102 }
1103 }
1104
1105 return false;
1106}
1107
Dan Gohman46510a72010-04-15 01:51:59 +00001108bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001109 if (Subtarget->is64Bit())
1110 // All other cases should be handled by the tblgen generated code.
1111 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001112 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1113 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001114
1115 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001117 // All other cases should be handled by the tblgen generated code.
1118 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001120 // All other cases should be handled by the tblgen generated code.
1121 return false;
1122
1123 unsigned InputReg = getRegForValue(I->getOperand(0));
1124 if (!InputReg)
1125 // Unhandled operand. Halt "fast" selection and bail.
1126 return false;
1127
Dan Gohman62417622009-04-27 16:33:14 +00001128 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001129 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1130 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001131 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001132 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001133 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001134
1135 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001137 CopyReg, /*Kill=*/true,
1138 X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001139 if (!ResultReg)
1140 return false;
1141
1142 UpdateValueMap(I, ResultReg);
1143 return true;
1144}
1145
Dan Gohman46510a72010-04-15 01:51:59 +00001146bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1147 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1148 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001149
Dan Gohman46510a72010-04-15 01:51:59 +00001150 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001151 switch (CI->getIntrinsicID()) {
1152 default: break;
1153 case Intrinsic::sadd_with_overflow:
1154 case Intrinsic::uadd_with_overflow:
1155 // Cheat a little. We know that the registers for "add" and "seto" are
1156 // allocated sequentially. However, we only keep track of the register
1157 // for "add" in the value map. Use extractvalue's index to get the
1158 // correct register for "seto".
1159 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1160 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001161 }
1162 }
1163
1164 return false;
1165}
1166
Dan Gohman46510a72010-04-15 01:51:59 +00001167bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001168 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001169 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001170 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001171 case Intrinsic::stackprotector: {
1172 // Emit code inline code to store the stack guard onto the stack.
1173 EVT PtrTy = TLI.getPointerTy();
1174
Eric Christopher551754c2010-04-16 23:37:20 +00001175 const Value *Op1 = I.getOperand(1); // The guard's value.
1176 const AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Eric Christopher07754c22010-03-18 20:27:26 +00001177
1178 // Grab the frame index.
1179 X86AddressMode AM;
1180 if (!X86SelectAddress(Slot, AM)) return false;
1181
Eric Christopher88dee302010-03-18 21:58:33 +00001182 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1183
Eric Christopher07754c22010-03-18 20:27:26 +00001184 return true;
1185 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001186 case Intrinsic::objectsize: {
Eric Christopher551754c2010-04-16 23:37:20 +00001187 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopherf27805b2010-03-11 06:20:22 +00001188 const Type *Ty = I.getCalledFunction()->getReturnType();
1189
1190 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1191
1192 EVT VT;
1193 if (!isTypeLegal(Ty, VT))
1194 return false;
1195
1196 unsigned OpC = 0;
1197 if (VT == MVT::i32)
1198 OpC = X86::MOV32ri;
1199 else if (VT == MVT::i64)
1200 OpC = X86::MOV64ri;
1201 else
1202 return false;
1203
1204 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1205 BuildMI(MBB, DL, TII.get(OpC), ResultReg).
1206 addImm(CI->getZExtValue() == 0 ? -1ULL : 0);
1207 UpdateValueMap(&I, ResultReg);
1208 return true;
1209 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001210 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001211 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001212 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001213 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001214 if (!X86SelectAddress(DI->getAddress(), AM))
1215 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001216 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001217 // FIXME may need to add RegState::Debug to any registers produced,
1218 // although ESP/EBP should be the only ones at the moment.
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001219 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1220 addMetadata(DI->getVariable());
1221 return true;
1222 }
Eric Christopher77f79892010-01-18 22:11:29 +00001223 case Intrinsic::trap: {
1224 BuildMI(MBB, DL, TII.get(X86::TRAP));
1225 return true;
1226 }
Bill Wendling52370a12008-12-09 02:42:50 +00001227 case Intrinsic::sadd_with_overflow:
1228 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001229 // Replace "add with overflow" intrinsics with an "add" instruction followed
1230 // by a seto/setc instruction. Later on, when the "extractvalue"
1231 // instructions are encountered, we use the fact that two registers were
1232 // created sequentially to get the correct registers for the "sum" and the
1233 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001234 const Function *Callee = I.getCalledFunction();
1235 const Type *RetTy =
1236 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1237
Owen Andersone50ed302009-08-10 22:56:29 +00001238 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001239 if (!isTypeLegal(RetTy, VT))
1240 return false;
1241
Eric Christopher551754c2010-04-16 23:37:20 +00001242 const Value *Op1 = I.getOperand(1);
1243 const Value *Op2 = I.getOperand(2);
Bill Wendling52370a12008-12-09 02:42:50 +00001244 unsigned Reg1 = getRegForValue(Op1);
1245 unsigned Reg2 = getRegForValue(Op2);
1246
1247 if (Reg1 == 0 || Reg2 == 0)
1248 // FIXME: Handle values *not* in registers.
1249 return false;
1250
1251 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001253 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001255 OpC = X86::ADD64rr;
1256 else
1257 return false;
1258
1259 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001260 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001261 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001262
Chris Lattner8d57b772009-04-12 07:51:14 +00001263 // If the add with overflow is an intra-block value then we just want to
1264 // create temporaries for it like normal. If it is a cross-block value then
1265 // UpdateValueMap will return the cross-block register used. Since we
1266 // *really* want the value to be live in the register pair known by
1267 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1268 // the cross block case. In the non-cross-block case, we should just make
1269 // another register for the value.
1270 if (DestReg1 != ResultReg)
1271 ResultReg = DestReg1+1;
1272 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001274
Chris Lattnera9a42252009-04-12 07:36:01 +00001275 unsigned Opc = X86::SETBr;
1276 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1277 Opc = X86::SETOr;
1278 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001279 return true;
1280 }
1281 }
1282}
1283
Dan Gohman46510a72010-04-15 01:51:59 +00001284bool X86FastISel::X86SelectCall(const Instruction *I) {
1285 const CallInst *CI = cast<CallInst>(I);
Eric Christopher551754c2010-04-16 23:37:20 +00001286 const Value *Callee = I->getOperand(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001287
1288 // Can't handle inline asm yet.
1289 if (isa<InlineAsm>(Callee))
1290 return false;
1291
Bill Wendling52370a12008-12-09 02:42:50 +00001292 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001293 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001294 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001295
Evan Chengf3d4efe2008-09-07 09:09:33 +00001296 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001297 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001298 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001299 if (CC != CallingConv::C &&
1300 CC != CallingConv::Fast &&
1301 CC != CallingConv::X86_FastCall)
1302 return false;
1303
Evan Cheng381993f2010-01-27 00:00:57 +00001304 // fastcc with -tailcallopt is intended to provide a guaranteed
1305 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001306 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001307 return false;
1308
Evan Chengf3d4efe2008-09-07 09:09:33 +00001309 // Let SDISel handle vararg functions.
1310 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1311 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1312 if (FTy->isVarArg())
1313 return false;
1314
1315 // Handle *simple* calls for now.
1316 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001317 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001318 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001320 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001321 return false;
1322
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001323 // Materialize callee address in a register. FIXME: GV address can be
1324 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001325 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001326 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001327 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001328 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001329 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001330 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001331 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001332 } else if (CalleeAM.Base.Reg != 0) {
1333 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001334 } else
1335 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001336
Evan Chengdebdea02008-09-08 17:15:42 +00001337 // Allow calls which produce i1 results.
1338 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001339 if (RetVT == MVT::i1) {
1340 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001341 AndToI1 = true;
1342 }
1343
Evan Chengf3d4efe2008-09-07 09:09:33 +00001344 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001345 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001346 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001347 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001348 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001349 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001350 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001351 ArgVTs.reserve(CS.arg_size());
1352 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001353 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001354 i != e; ++i) {
1355 unsigned Arg = getRegForValue(*i);
1356 if (Arg == 0)
1357 return false;
1358 ISD::ArgFlagsTy Flags;
1359 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001360 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001361 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001362 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001363 Flags.setZExt();
1364
1365 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001366 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1367 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1368 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1369 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001370 return false;
1371
1372 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001373 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001374 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001375 return false;
1376 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1377 Flags.setOrigAlign(OriginalAlignment);
1378
1379 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001380 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001381 ArgVTs.push_back(ArgVT);
1382 ArgFlags.push_back(Flags);
1383 }
1384
1385 // Analyze operands of the call, assigning locations to each operand.
1386 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001387 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001388 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1389
1390 // Get a count of how many bytes are to be pushed on the stack.
1391 unsigned NumBytes = CCInfo.getNextStackOffset();
1392
1393 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001394 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001395 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001396
Chris Lattner438949a2008-10-15 05:30:52 +00001397 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001398 // copies / loads.
1399 SmallVector<unsigned, 4> RegArgs;
1400 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1401 CCValAssign &VA = ArgLocs[i];
1402 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001403 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001404
1405 // Promote the value if needed.
1406 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001407 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001408 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001409 case CCValAssign::SExt: {
1410 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1411 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001412 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001413 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001414 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001415 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001416 }
1417 case CCValAssign::ZExt: {
1418 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1419 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001420 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001421 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001422 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001423 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001424 }
1425 case CCValAssign::AExt: {
1426 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1427 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001428 if (!Emitted)
1429 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001430 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001431 if (!Emitted)
1432 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1433 Arg, ArgVT, Arg);
1434
Chris Lattnera33649e2008-12-19 17:03:38 +00001435 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001436 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001437 break;
1438 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001439 case CCValAssign::BCvt: {
1440 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001441 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001442 assert(BC != 0 && "Failed to emit a bitcast!");
1443 Arg = BC;
1444 ArgVT = VA.getLocVT();
1445 break;
1446 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001447 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001448
1449 if (VA.isRegLoc()) {
1450 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1451 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001452 Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001453 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001454 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001455 RegArgs.push_back(VA.getLocReg());
1456 } else {
1457 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001458 X86AddressMode AM;
1459 AM.Base.Reg = StackPtr;
1460 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001461 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001462
1463 // If this is a really simple value, emit this with the Value* version of
1464 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1465 // can cause us to reevaluate the argument.
1466 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1467 X86FastEmitStore(ArgVT, ArgVal, AM);
1468 else
1469 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001470 }
1471 }
1472
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001473 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1474 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001475 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001476 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001477 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001478 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC,
1479 DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001480 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001481 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001482 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001483
Evan Chengf3d4efe2008-09-07 09:09:33 +00001484 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001485 MachineInstrBuilder MIB;
1486 if (CalleeOp) {
1487 // Register-indirect call.
1488 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1489 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1490
1491 } else {
1492 // Direct call.
1493 assert(GV && "Not a direct call");
1494 unsigned CallOpc =
1495 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1496
1497 // See if we need any target-specific flags on the GV operand.
1498 unsigned char OpFlags = 0;
1499
1500 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1501 // external symbols most go through the PLT in PIC mode. If the symbol
1502 // has hidden or protected visibility, or if it is static or local, then
1503 // we don't need to use the PLT - we can directly call it.
1504 if (Subtarget->isTargetELF() &&
1505 TM.getRelocationModel() == Reloc::PIC_ &&
1506 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1507 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001508 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001509 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1510 Subtarget->getDarwinVers() < 9) {
1511 // PC-relative references to external symbols should go through $stub,
1512 // unless we're building with the leopard linker or later, which
1513 // automatically synthesizes these stubs.
1514 OpFlags = X86II::MO_DARWIN_STUB;
1515 }
1516
1517
1518 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1519 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001520
1521 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001522 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001523 MIB.addReg(X86::EBX);
1524
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001526 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1527 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001528
1529 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001530 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001531 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001532
1533 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001535 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001536 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001537 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1538
1539 // Copy all of the result registers out of their specified physreg.
1540 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001541 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1543 TargetRegisterClass *SrcRC = DstRC;
1544
1545 // If this is a call to a function that returns an fp value on the x87 fp
1546 // stack, but where we prefer to use the value in xmm registers, copy it
1547 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1548 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1549 RVLocs[0].getLocReg() == X86::ST1) &&
1550 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001552 SrcRC = X86::RSTRegisterClass;
1553 DstRC = X86::RFP80RegisterClass;
1554 }
1555
1556 unsigned ResultReg = createResultReg(DstRC);
1557 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001558 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001559 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001560 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001561 if (CopyVT != RVLocs[0].getValVT()) {
1562 // Round the F80 the right size, which also moves to the appropriate xmm
1563 // register. This is accomplished by storing the F80 value in memory and
1564 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001565 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001567 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001568 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001569 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001571 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001574 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001575 }
1576
Evan Chengdebdea02008-09-08 17:15:42 +00001577 if (AndToI1) {
1578 // Mask out all but lowest bit for some call which produces an i1.
1579 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001580 BuildMI(MBB, DL,
1581 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001582 ResultReg = AndResult;
1583 }
1584
Evan Chengf3d4efe2008-09-07 09:09:33 +00001585 UpdateValueMap(I, ResultReg);
1586 }
1587
1588 return true;
1589}
1590
1591
Dan Gohman99b21822008-08-28 23:21:34 +00001592bool
Dan Gohman46510a72010-04-15 01:51:59 +00001593X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001594 switch (I->getOpcode()) {
1595 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001596 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001597 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001598 case Instruction::Store:
1599 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001600 case Instruction::ICmp:
1601 case Instruction::FCmp:
1602 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001603 case Instruction::ZExt:
1604 return X86SelectZExt(I);
1605 case Instruction::Br:
1606 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001607 case Instruction::Call:
1608 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001609 case Instruction::LShr:
1610 case Instruction::AShr:
1611 case Instruction::Shl:
1612 return X86SelectShift(I);
1613 case Instruction::Select:
1614 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001615 case Instruction::Trunc:
1616 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001617 case Instruction::FPExt:
1618 return X86SelectFPExt(I);
1619 case Instruction::FPTrunc:
1620 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001621 case Instruction::ExtractValue:
1622 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001623 case Instruction::IntToPtr: // Deliberate fall-through.
1624 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001625 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1626 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001627 if (DstVT.bitsGT(SrcVT))
1628 return X86SelectZExt(I);
1629 if (DstVT.bitsLT(SrcVT))
1630 return X86SelectTrunc(I);
1631 unsigned Reg = getRegForValue(I->getOperand(0));
1632 if (Reg == 0) return false;
1633 UpdateValueMap(I, Reg);
1634 return true;
1635 }
Dan Gohman99b21822008-08-28 23:21:34 +00001636 }
1637
1638 return false;
1639}
1640
Dan Gohman46510a72010-04-15 01:51:59 +00001641unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001642 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001643 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001644 return false;
1645
1646 // Get opcode and regclass of the output for the given load instruction.
1647 unsigned Opc = 0;
1648 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001649 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001650 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001652 Opc = X86::MOV8rm;
1653 RC = X86::GR8RegisterClass;
1654 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001655 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001656 Opc = X86::MOV16rm;
1657 RC = X86::GR16RegisterClass;
1658 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001660 Opc = X86::MOV32rm;
1661 RC = X86::GR32RegisterClass;
1662 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001664 // Must be in x86-64 mode.
1665 Opc = X86::MOV64rm;
1666 RC = X86::GR64RegisterClass;
1667 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001668 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001669 if (Subtarget->hasSSE1()) {
1670 Opc = X86::MOVSSrm;
1671 RC = X86::FR32RegisterClass;
1672 } else {
1673 Opc = X86::LD_Fp32m;
1674 RC = X86::RFP32RegisterClass;
1675 }
1676 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001678 if (Subtarget->hasSSE2()) {
1679 Opc = X86::MOVSDrm;
1680 RC = X86::FR64RegisterClass;
1681 } else {
1682 Opc = X86::LD_Fp64m;
1683 RC = X86::RFP64RegisterClass;
1684 }
1685 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001686 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001687 // No f80 support yet.
1688 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001689 }
1690
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001691 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001692 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001693 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001694 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001696 Opc = X86::LEA32r;
1697 else
1698 Opc = X86::LEA64r;
1699 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001700 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001701 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001702 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001703 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001704 }
1705
Owen Anderson3b217c62008-09-06 01:11:01 +00001706 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001707 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001708 if (Align == 0) {
1709 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001710 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001711 }
Owen Anderson95267a12008-09-05 00:06:23 +00001712
Dan Gohman5396c992008-09-30 01:21:32 +00001713 // x86-32 PIC requires a PIC base register for constant pools.
1714 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001715 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001716 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001717 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1718 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1719 } else if (Subtarget->isPICStyleGOT()) {
1720 OpFlag = X86II::MO_GOTOFF;
1721 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1722 } else if (Subtarget->isPICStyleRIPRel() &&
1723 TM.getCodeModel() == CodeModel::Small) {
1724 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001725 }
Dan Gohman5396c992008-09-30 01:21:32 +00001726
1727 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001728 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001729 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001730 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1731 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001732
Owen Anderson95267a12008-09-05 00:06:23 +00001733 return ResultReg;
1734}
1735
Dan Gohman46510a72010-04-15 01:51:59 +00001736unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001737 // Fail on dynamic allocas. At this point, getRegForValue has already
1738 // checked its CSE maps, so if we're here trying to handle a dynamic
1739 // alloca, we're not going to succeed. X86SelectAddress has a
1740 // check for dynamic allocas, because it's called directly from
1741 // various places, but TargetMaterializeAlloca also needs a check
1742 // in order to avoid recursion between getRegForValue,
1743 // X86SelectAddrss, and TargetMaterializeAlloca.
1744 if (!StaticAllocaMap.count(C))
1745 return 0;
1746
Dan Gohman0586d912008-09-10 20:11:02 +00001747 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001748 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001749 return 0;
1750 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1751 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1752 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001753 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001754 return ResultReg;
1755}
1756
Evan Chengc3f44b02008-09-03 00:03:49 +00001757namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001758 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1759 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001760 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00001761 DenseMap<const AllocaInst *, int> &am,
1762 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001763#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00001764 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001765#endif
1766 ) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00001767 return new X86FastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001768#ifndef NDEBUG
1769 , cil
1770#endif
1771 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001772 }
Dan Gohman99b21822008-08-28 23:21:34 +00001773}