blob: 962ead10456d003d4aa6b9ddb30dfdcf75ff33e6 [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"
Dan Gohman84023e02010-07-10 09:00:22 +000026#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000028#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000029#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000032#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000035#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000036using namespace llvm;
37
Chris Lattner087fcf32009-03-08 18:44:31 +000038namespace {
39
Evan Chengc3f44b02008-09-03 00:03:49 +000040class X86FastISel : public FastISel {
41 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
42 /// make the right decision when generating code for different targets.
43 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000044
45 /// StackPtr - Register used as the stack pointer.
46 ///
47 unsigned StackPtr;
48
49 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
50 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Cheng8b19e562008-09-03 06:44:39 +000056public:
Dan Gohmana4160c32010-07-07 16:29:44 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000058 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000059 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
60 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman46510a72010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Chris Lattnerbeac75d2010-09-05 02:18:34 +000066 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
67 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
69 /// possible.
70 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
71 const LoadInst *LI);
72
Dan Gohman1adf1b02008-08-19 21:45:35 +000073#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000074
75private:
Dan Gohman46510a72010-04-15 01:51:59 +000076 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000077
Owen Andersone50ed302009-08-10 22:56:29 +000078 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000081 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000083 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000086 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000087
Dan Gohman46510a72010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000094
Dan Gohman84023e02010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectFPExt(const Instruction *I);
110 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
115 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000116
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000117 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000118 return getTargetMachine()->getInstrInfo();
119 }
120 const X86TargetMachine *getTargetMachine() const {
121 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000122 }
123
Dan Gohman46510a72010-04-15 01:51:59 +0000124 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000125
Dan Gohman46510a72010-04-15 01:51:59 +0000126 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127
128 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
129 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000130 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
132 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133 }
134
Owen Andersone50ed302009-08-10 22:56:29 +0000135 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000136};
Chris Lattner087fcf32009-03-08 18:44:31 +0000137
138} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000139
Owen Andersone50ed302009-08-10 22:56:29 +0000140bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000141 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000143 // Unhandled type. Halt "fast" selection and bail.
144 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000145
Dan Gohman9b66d732008-09-30 00:48:39 +0000146 // For now, require SSE/SSE2 for performing floating-point operations,
147 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000149 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000151 return false;
152 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000155 // We only handle legal types. For example, on x86-32 the instruction
156 // selector contains all of the 64-bit instructions from x86-64,
157 // under the assumption that i64 won't be used if the target doesn't
158 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000160}
161
162#include "X86GenCallingConv.inc"
163
Evan Cheng0de588f2008-09-05 21:00:03 +0000164/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000166/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000167bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000168 unsigned &ResultReg) {
169 // Get opcode and regclass of the output for the given load instruction.
170 unsigned Opc = 0;
171 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000173 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000174 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000176 Opc = X86::MOV8rm;
177 RC = X86::GR8RegisterClass;
178 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000180 Opc = X86::MOV16rm;
181 RC = X86::GR16RegisterClass;
182 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 Opc = X86::MOV32rm;
185 RC = X86::GR32RegisterClass;
186 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 // Must be in x86-64 mode.
189 Opc = X86::MOV64rm;
190 RC = X86::GR64RegisterClass;
191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 if (Subtarget->hasSSE1()) {
194 Opc = X86::MOVSSrm;
195 RC = X86::FR32RegisterClass;
196 } else {
197 Opc = X86::LD_Fp32m;
198 RC = X86::RFP32RegisterClass;
199 }
200 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 if (Subtarget->hasSSE2()) {
203 Opc = X86::MOVSDrm;
204 RC = X86::FR64RegisterClass;
205 } else {
206 Opc = X86::LD_Fp64m;
207 RC = X86::RFP64RegisterClass;
208 }
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000211 // No f80 support yet.
212 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 }
214
215 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000216 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
217 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 return true;
219}
220
Evan Chengf3d4efe2008-09-07 09:09:33 +0000221/// X86FastEmitStore - Emit a machine instruction to store a value Val of
222/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
223/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000224/// i.e. V. Return true if it is possible.
225bool
Owen Andersone50ed302009-08-10 22:56:29 +0000226X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000227 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000228 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 switch (VT.getSimpleVT().SimpleTy) {
231 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000232 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000233 case MVT::i1: {
234 // Mask out all but lowest bit.
235 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000237 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
238 Val = AndResult;
239 }
240 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 case MVT::i8: Opc = X86::MOV8mr; break;
242 case MVT::i16: Opc = X86::MOV16mr; break;
243 case MVT::i32: Opc = X86::MOV32mr; break;
244 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
245 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000246 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000247 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000249 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000250 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000251 }
Chris Lattner438949a2008-10-15 05:30:52 +0000252
Dan Gohman84023e02010-07-10 09:00:22 +0000253 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
254 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 return true;
256}
257
Dan Gohman46510a72010-04-15 01:51:59 +0000258bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000259 const X86AddressMode &AM) {
260 // Handle 'null' like i32/i64 0.
261 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000262 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000263
264 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000265 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000266 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000267 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000269 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000270 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 case MVT::i8: Opc = X86::MOV8mi; break;
272 case MVT::i16: Opc = X86::MOV16mi; break;
273 case MVT::i32: Opc = X86::MOV32mi; break;
274 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000275 // Must be a 32-bit sign extended value.
276 if ((int)CI->getSExtValue() == CI->getSExtValue())
277 Opc = X86::MOV64mi32;
278 break;
279 }
280
281 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000282 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
283 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000284 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000285 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000286 return true;
287 }
288 }
289
290 unsigned ValReg = getRegForValue(Val);
291 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000292 return false;
293
294 return X86FastEmitStore(VT, ValReg, AM);
295}
296
Evan Cheng24e3a902008-09-08 06:35:17 +0000297/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
298/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
299/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000300bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
301 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000302 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000303 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
304 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000305
306 if (RR != 0) {
307 ResultReg = RR;
308 return true;
309 } else
310 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000311}
312
Dan Gohman0586d912008-09-10 20:11:02 +0000313/// X86SelectAddress - Attempt to fill in an address from the given value.
314///
Dan Gohman46510a72010-04-15 01:51:59 +0000315bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
316 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000317 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000318 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000319 // Don't walk into other basic blocks; it's possible we haven't
320 // visited them yet, so the instructions may not yet be assigned
321 // virtual registers.
Dan Gohman84023e02010-07-10 09:00:22 +0000322 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000323 return false;
324
Dan Gohman35893082008-09-18 23:23:44 +0000325 Opcode = I->getOpcode();
326 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000327 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000328 Opcode = C->getOpcode();
329 U = C;
330 }
Dan Gohman0586d912008-09-10 20:11:02 +0000331
Chris Lattner868ee942010-06-15 19:08:40 +0000332 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
333 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000334 // Fast instruction selection doesn't support the special
335 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000336 return false;
337
Dan Gohman35893082008-09-18 23:23:44 +0000338 switch (Opcode) {
339 default: break;
340 case Instruction::BitCast:
341 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000342 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000343
344 case Instruction::IntToPtr:
345 // Look past no-op inttoptrs.
346 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000347 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000348 break;
Dan Gohman35893082008-09-18 23:23:44 +0000349
350 case Instruction::PtrToInt:
351 // Look past no-op ptrtoints.
352 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000353 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000354 break;
Dan Gohman35893082008-09-18 23:23:44 +0000355
356 case Instruction::Alloca: {
357 // Do static allocas.
358 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000359 DenseMap<const AllocaInst*, int>::iterator SI =
360 FuncInfo.StaticAllocaMap.find(A);
361 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000362 AM.BaseType = X86AddressMode::FrameIndexBase;
363 AM.Base.FrameIndex = SI->second;
364 return true;
365 }
366 break;
Dan Gohman35893082008-09-18 23:23:44 +0000367 }
368
369 case Instruction::Add: {
370 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000371 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000372 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
373 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000374 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000375 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000376 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000377 }
Dan Gohman0586d912008-09-10 20:11:02 +0000378 }
Dan Gohman35893082008-09-18 23:23:44 +0000379 break;
380 }
381
382 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000383 X86AddressMode SavedAM = AM;
384
Dan Gohman35893082008-09-18 23:23:44 +0000385 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000386 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000387 unsigned IndexReg = AM.IndexReg;
388 unsigned Scale = AM.Scale;
389 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000390 // Iterate through the indices, folding what we can. Constants can be
391 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000392 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000393 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000394 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000395 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
396 const StructLayout *SL = TD.getStructLayout(STy);
397 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
398 Disp += SL->getElementOffset(Idx);
399 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000400 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000401 SmallVector<const Value *, 4> Worklist;
402 Worklist.push_back(Op);
403 do {
404 Op = Worklist.pop_back_val();
405 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
406 // Constant-offset addressing.
407 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000408 } else if (isa<AddOperator>(Op) &&
409 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
410 // An add with a constant operand. Fold the constant.
411 ConstantInt *CI =
412 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
413 Disp += CI->getSExtValue() * S;
414 // Add the other operand back to the work list.
415 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000416 } else if (IndexReg == 0 &&
417 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
418 (S == 1 || S == 2 || S == 4 || S == 8)) {
419 // Scaled-index addressing.
420 Scale = S;
421 IndexReg = getRegForGEPIndex(Op).first;
422 if (IndexReg == 0)
423 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000424 } else
425 // Unsupported.
426 goto unsupported_gep;
427 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000428 }
429 }
Dan Gohman09aae462008-09-26 20:04:15 +0000430 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000431 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000432 break;
Dan Gohman35893082008-09-18 23:23:44 +0000433 // Ok, the GEP indices were covered by constant-offset and scaled-index
434 // addressing. Update the address state and move on to examining the base.
435 AM.IndexReg = IndexReg;
436 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000437 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000438 if (X86SelectAddress(U->getOperand(0), AM))
439 return true;
440
441 // If we couldn't merge the sub value into this addr mode, revert back to
442 // our address and just match the value instead of completely failing.
443 AM = SavedAM;
444 break;
Dan Gohman35893082008-09-18 23:23:44 +0000445 unsupported_gep:
446 // Ok, the GEP indices weren't all covered.
447 break;
448 }
449 }
450
451 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000452 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000453 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000454 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000455 return false;
456
Dan Gohman97135e12008-09-26 19:15:30 +0000457 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000458 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000459 (AM.Base.Reg != 0 || AM.IndexReg != 0))
460 return false;
461
Dan Gohmane9865942009-02-23 22:03:08 +0000462 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000463 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000464 if (GVar->isThreadLocal())
465 return false;
466
Chris Lattnerff7727f2009-07-09 06:41:35 +0000467 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000468 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000469
Chris Lattner0d786dd2009-07-10 07:48:51 +0000470 // Allow the subtarget to classify the global.
471 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
472
473 // If this reference is relative to the pic base, set it now.
474 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000475 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000476 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000477 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000478
479 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000480 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000481 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000482 if (Subtarget->isPICStyleRIPRel()) {
483 // Use rip-relative addressing if we can. Above we verified that the
484 // base and index registers are unused.
485 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
486 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000487 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000488 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000489 return true;
490 }
491
Chris Lattner0d786dd2009-07-10 07:48:51 +0000492 // Ok, we need to do a load from a stub. If we've already loaded from this
493 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000494 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
495 unsigned LoadReg;
496 if (I != LocalValueMap.end() && I->second != 0) {
497 LoadReg = I->second;
498 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000499 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000500 unsigned Opc = 0;
501 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000502 X86AddressMode StubAM;
503 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000504 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000505 StubAM.GVOpFlags = GVFlags;
506
Dan Gohman84023e02010-07-10 09:00:22 +0000507 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000508 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000509
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000511 Opc = X86::MOV64rm;
512 RC = X86::GR64RegisterClass;
513
Chris Lattner0d786dd2009-07-10 07:48:51 +0000514 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000515 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000516 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000517 Opc = X86::MOV32rm;
518 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000519 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000520
521 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000522 MachineInstrBuilder LoadMI =
523 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
524 addFullAddress(LoadMI, StubAM);
525
526 // Ok, back to normal mode.
527 leaveLocalValueArea(SaveInsertPt);
528
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000529 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000530 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000531 }
Chris Lattner18c59872009-06-27 04:16:01 +0000532
Chris Lattnerff7727f2009-07-09 06:41:35 +0000533 // Now construct the final address. Note that the Disp, Scale,
534 // and Index values may already be set here.
535 AM.Base.Reg = LoadReg;
536 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000537 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000538 }
539
Dan Gohman97135e12008-09-26 19:15:30 +0000540 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000541 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000542 if (AM.Base.Reg == 0) {
543 AM.Base.Reg = getRegForValue(V);
544 return AM.Base.Reg != 0;
545 }
546 if (AM.IndexReg == 0) {
547 assert(AM.Scale == 1 && "Scale with no index!");
548 AM.IndexReg = getRegForValue(V);
549 return AM.IndexReg != 0;
550 }
551 }
552
553 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000554}
555
Chris Lattner0aa43de2009-07-10 05:33:42 +0000556/// X86SelectCallAddress - Attempt to fill in an address from the given value.
557///
Dan Gohman46510a72010-04-15 01:51:59 +0000558bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
559 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000560 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000561 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000562 Opcode = I->getOpcode();
563 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000564 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000565 Opcode = C->getOpcode();
566 U = C;
567 }
568
569 switch (Opcode) {
570 default: break;
571 case Instruction::BitCast:
572 // Look past bitcasts.
573 return X86SelectCallAddress(U->getOperand(0), AM);
574
575 case Instruction::IntToPtr:
576 // Look past no-op inttoptrs.
577 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
578 return X86SelectCallAddress(U->getOperand(0), AM);
579 break;
580
581 case Instruction::PtrToInt:
582 // Look past no-op ptrtoints.
583 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
584 return X86SelectCallAddress(U->getOperand(0), AM);
585 break;
586 }
587
588 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000589 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000590 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000591 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000592 return false;
593
594 // RIP-relative addresses can't have additional register operands.
595 if (Subtarget->isPICStyleRIPRel() &&
596 (AM.Base.Reg != 0 || AM.IndexReg != 0))
597 return false;
598
Chris Lattner754b7652009-07-10 05:48:03 +0000599 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000600 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000601 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 return false;
603
604 // Okay, we've committed to selecting this global. Set up the basic address.
605 AM.GV = GV;
606
Chris Lattnere6c07b52009-07-10 05:45:15 +0000607 // No ABI requires an extra load for anything other than DLLImport, which
608 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000609 if (Subtarget->isPICStyleRIPRel()) {
610 // Use rip-relative addressing if we can. Above we verified that the
611 // base and index registers are unused.
612 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
613 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000614 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000615 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
616 } else if (Subtarget->isPICStyleGOT()) {
617 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000618 }
619
Chris Lattner0aa43de2009-07-10 05:33:42 +0000620 return true;
621 }
622
623 // If all else fails, try to materialize the value in a register.
624 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
625 if (AM.Base.Reg == 0) {
626 AM.Base.Reg = getRegForValue(V);
627 return AM.Base.Reg != 0;
628 }
629 if (AM.IndexReg == 0) {
630 assert(AM.Scale == 1 && "Scale with no index!");
631 AM.IndexReg = getRegForValue(V);
632 return AM.IndexReg != 0;
633 }
634 }
635
636 return false;
637}
638
639
Owen Andersona3971df2008-09-04 07:08:58 +0000640/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000641bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000642 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000643 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000644 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000645
Dan Gohman0586d912008-09-10 20:11:02 +0000646 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000647 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000648 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000649
Chris Lattner438949a2008-10-15 05:30:52 +0000650 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000651}
652
Dan Gohman84023e02010-07-10 09:00:22 +0000653/// X86SelectRet - Select and emit code to implement ret instructions.
654bool X86FastISel::X86SelectRet(const Instruction *I) {
655 const ReturnInst *Ret = cast<ReturnInst>(I);
656 const Function &F = *I->getParent()->getParent();
657
658 if (!FuncInfo.CanLowerReturn)
659 return false;
660
661 CallingConv::ID CC = F.getCallingConv();
662 if (CC != CallingConv::C &&
663 CC != CallingConv::Fast &&
664 CC != CallingConv::X86_FastCall)
665 return false;
666
667 if (Subtarget->isTargetWin64())
668 return false;
669
670 // Don't handle popping bytes on return for now.
671 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
672 ->getBytesToPopOnReturn() != 0)
673 return 0;
674
675 // fastcc with -tailcallopt is intended to provide a guaranteed
676 // tail call optimization. Fastisel doesn't know how to do that.
677 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
678 return false;
679
680 // Let SDISel handle vararg functions.
681 if (F.isVarArg())
682 return false;
683
684 if (Ret->getNumOperands() > 0) {
685 SmallVector<ISD::OutputArg, 4> Outs;
686 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
687 Outs, TLI);
688
689 // Analyze operands of the call, assigning locations to each operand.
690 SmallVector<CCValAssign, 16> ValLocs;
691 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000692 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000693
694 const Value *RV = Ret->getOperand(0);
695 unsigned Reg = getRegForValue(RV);
696 if (Reg == 0)
697 return false;
698
699 // Only handle a single return value for now.
700 if (ValLocs.size() != 1)
701 return false;
702
703 CCValAssign &VA = ValLocs[0];
704
705 // Don't bother handling odd stuff for now.
706 if (VA.getLocInfo() != CCValAssign::Full)
707 return false;
708 // Only handle register returns for now.
709 if (!VA.isRegLoc())
710 return false;
711 // TODO: For now, don't try to handle cases where getLocInfo()
712 // says Full but the types don't match.
713 if (VA.getValVT() != TLI.getValueType(RV->getType()))
714 return false;
715
716 // The calling-convention tables for x87 returns don't tell
717 // the whole story.
718 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
719 return false;
720
721 // Make the copy.
722 unsigned SrcReg = Reg + VA.getValNo();
723 unsigned DstReg = VA.getLocReg();
724 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000725 // Avoid a cross-class copy. This is very unlikely.
726 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000727 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000728 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
729 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000730
731 // Mark the register as live out of the function.
732 MRI.addLiveOut(VA.getLocReg());
733 }
734
735 // Now emit the RET.
736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
737 return true;
738}
739
Evan Cheng8b19e562008-09-03 06:44:39 +0000740/// X86SelectLoad - Select and emit code to implement load instructions.
741///
Dan Gohman46510a72010-04-15 01:51:59 +0000742bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000743 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000744 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000745 return false;
746
Dan Gohman0586d912008-09-10 20:11:02 +0000747 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000748 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000749 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000750
Evan Cheng0de588f2008-09-05 21:00:03 +0000751 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000752 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000753 UpdateValueMap(I, ResultReg);
754 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000755 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000756 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000757}
758
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000759static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000761 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 case MVT::i8: return X86::CMP8rr;
763 case MVT::i16: return X86::CMP16rr;
764 case MVT::i32: return X86::CMP32rr;
765 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000766 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
767 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000768 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000769}
770
Chris Lattner0e13c782008-10-15 04:13:29 +0000771/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
772/// of the comparison, return an opcode that works for the compare (e.g.
773/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000774static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000776 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000777 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 case MVT::i8: return X86::CMP8ri;
779 case MVT::i16: return X86::CMP16ri;
780 case MVT::i32: return X86::CMP32ri;
781 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000782 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
783 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000784 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000785 return X86::CMP64ri32;
786 return 0;
787 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000788}
789
Dan Gohman46510a72010-04-15 01:51:59 +0000790bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
791 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000792 unsigned Op0Reg = getRegForValue(Op0);
793 if (Op0Reg == 0) return false;
794
Chris Lattnerd53886b2008-10-15 05:18:04 +0000795 // Handle 'null' like i32/i64 0.
796 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000797 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000798
Chris Lattner9a08a612008-10-15 04:26:38 +0000799 // We have two options: compare with register or immediate. If the RHS of
800 // the compare is an immediate that we can fold into this compare, use
801 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000802 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000803 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000804 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
805 .addReg(Op0Reg)
806 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000807 return true;
808 }
809 }
810
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000811 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000812 if (CompareOpc == 0) return false;
813
814 unsigned Op1Reg = getRegForValue(Op1);
815 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000816 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
817 .addReg(Op0Reg)
818 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000819
820 return true;
821}
822
Dan Gohman46510a72010-04-15 01:51:59 +0000823bool X86FastISel::X86SelectCmp(const Instruction *I) {
824 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000825
Owen Andersone50ed302009-08-10 22:56:29 +0000826 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000827 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000828 return false;
829
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000830 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000831 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000832 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000833 switch (CI->getPredicate()) {
834 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000835 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
836 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000837
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000838 unsigned EReg = createResultReg(&X86::GR8RegClass);
839 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000840 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
841 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
842 TII.get(X86::SETNPr), NPReg);
843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000844 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000845 UpdateValueMap(I, ResultReg);
846 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000847 }
848 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000849 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
850 return false;
851
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000852 unsigned NEReg = createResultReg(&X86::GR8RegClass);
853 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000854 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
855 TII.get(X86::SETNEr), NEReg);
856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
857 TII.get(X86::SETPr), PReg);
858 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
859 TII.get(X86::OR8rr), ResultReg)
860 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000861 UpdateValueMap(I, ResultReg);
862 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000863 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000864 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
865 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
866 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
867 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
868 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
869 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
870 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
871 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
872 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
873 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
874 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
875 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
876
877 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
878 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
879 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
880 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
881 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
882 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
883 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
884 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
885 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
886 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000887 default:
888 return false;
889 }
890
Dan Gohman46510a72010-04-15 01:51:59 +0000891 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000892 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000893 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000894
Chris Lattner9a08a612008-10-15 04:26:38 +0000895 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000896 if (!X86FastEmitCompare(Op0, Op1, VT))
897 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000898
Dan Gohman84023e02010-07-10 09:00:22 +0000899 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000900 UpdateValueMap(I, ResultReg);
901 return true;
902}
Evan Cheng8b19e562008-09-03 06:44:39 +0000903
Dan Gohman46510a72010-04-15 01:51:59 +0000904bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000905 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000906 if (I->getType()->isIntegerTy(8) &&
907 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000908 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000909 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000910 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000911 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000912 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000913 UpdateValueMap(I, ResultReg);
914 return true;
915 }
916
917 return false;
918}
919
Chris Lattner9a08a612008-10-15 04:26:38 +0000920
Dan Gohman46510a72010-04-15 01:51:59 +0000921bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000922 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000923 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000924 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000925 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
926 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000927
Dan Gohman8bef7442010-08-21 02:32:36 +0000928 // Fold the common case of a conditional branch with a comparison
929 // in the same block (values defined on other blocks may not have
930 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000931 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000932 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000933 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000934
Dan Gohmand98d6202008-10-02 22:15:21 +0000935 // Try to take advantage of fallthrough opportunities.
936 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000937 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000938 std::swap(TrueMBB, FalseMBB);
939 Predicate = CmpInst::getInversePredicate(Predicate);
940 }
941
Chris Lattner871d2462008-10-15 03:58:05 +0000942 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
943 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
944
Dan Gohmand98d6202008-10-02 22:15:21 +0000945 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000946 case CmpInst::FCMP_OEQ:
947 std::swap(TrueMBB, FalseMBB);
948 Predicate = CmpInst::FCMP_UNE;
949 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000950 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
951 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
952 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
953 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
954 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
955 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
956 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
957 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
958 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
959 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
960 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
961 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
962 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000963
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000964 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
965 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
966 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
967 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
968 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
969 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
970 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
971 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
972 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
973 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000974 default:
975 return false;
976 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000977
Dan Gohman46510a72010-04-15 01:51:59 +0000978 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000979 if (SwapArgs)
980 std::swap(Op0, Op1);
981
Chris Lattner9a08a612008-10-15 04:26:38 +0000982 // Emit a compare of the LHS and RHS, setting the flags.
983 if (!X86FastEmitCompare(Op0, Op1, VT))
984 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000985
Dan Gohman84023e02010-07-10 09:00:22 +0000986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
987 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000988
989 if (Predicate == CmpInst::FCMP_UNE) {
990 // X86 requires a second branch to handle UNE (and OEQ,
991 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +0000992 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
993 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000994 }
995
Stuart Hastings3bf91252010-06-17 22:43:56 +0000996 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +0000997 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000998 return true;
999 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001000 } else if (ExtractValueInst *EI =
1001 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1002 // Check to see if the branch instruction is from an "arithmetic with
1003 // overflow" intrinsic. The main way these intrinsics are used is:
1004 //
1005 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1006 // %sum = extractvalue { i32, i1 } %t, 0
1007 // %obit = extractvalue { i32, i1 } %t, 1
1008 // br i1 %obit, label %overflow, label %normal
1009 //
Dan Gohman653456c2009-01-07 00:15:08 +00001010 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001011 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001012 // looking for the SETO/SETB instruction. If an instruction modifies the
1013 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1014 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001015 if (const IntrinsicInst *CI =
1016 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001017 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1018 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1019 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001020 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001021
Chris Lattnera9a42252009-04-12 07:36:01 +00001022 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001023 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1024 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001025 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001026
Evan Cheng1015ba72010-05-21 20:53:24 +00001027 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001028 if (MI.isCopy()) {
1029 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001030 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001031 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001032
Chris Lattnera9a42252009-04-12 07:36:01 +00001033 SetMI = &MI;
1034 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001035 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001036
Chris Lattnera9a42252009-04-12 07:36:01 +00001037 const TargetInstrDesc &TID = MI.getDesc();
1038 if (TID.hasUnmodeledSideEffects() ||
1039 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
1040 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001041 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001042
1043 if (SetMI) {
1044 unsigned OpCode = SetMI->getOpcode();
1045
1046 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1048 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001049 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001050 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001051 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001052 return true;
1053 }
Bill Wendling9a901322008-12-10 19:44:24 +00001054 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001055 }
1056 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001057 }
1058
1059 // Otherwise do a clumsy setcc and re-test it.
1060 unsigned OpReg = getRegForValue(BI->getCondition());
1061 if (OpReg == 0) return false;
1062
Dan Gohman84023e02010-07-10 09:00:22 +00001063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1064 .addReg(OpReg).addReg(OpReg);
1065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1066 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001067 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001068 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001069 return true;
1070}
1071
Dan Gohman46510a72010-04-15 01:51:59 +00001072bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001073 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001074 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001075 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001076 CReg = X86::CL;
1077 RC = &X86::GR8RegClass;
1078 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001079 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1080 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1081 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001082 default: return false;
1083 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001084 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001085 CReg = X86::CX;
1086 RC = &X86::GR16RegClass;
1087 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001088 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1089 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1090 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001091 default: return false;
1092 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001093 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001094 CReg = X86::ECX;
1095 RC = &X86::GR32RegClass;
1096 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001097 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1098 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1099 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001100 default: return false;
1101 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001102 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 CReg = X86::RCX;
1104 RC = &X86::GR64RegClass;
1105 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001106 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1107 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1108 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 default: return false;
1110 }
1111 } else {
1112 return false;
1113 }
1114
Owen Andersone50ed302009-08-10 22:56:29 +00001115 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001117 return false;
1118
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001119 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1120 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001121
1122 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001123 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001124 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001126 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001127 UpdateValueMap(I, ResultReg);
1128 return true;
1129 }
1130
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001131 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1132 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1134 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001135
1136 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001137 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001138 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001139 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1140 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001141 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001142
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001143 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1145 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001146 UpdateValueMap(I, ResultReg);
1147 return true;
1148}
1149
Dan Gohman46510a72010-04-15 01:51:59 +00001150bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001151 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001153 return false;
1154
Eric Christophere487b012010-09-29 23:00:29 +00001155 // We only use cmov here, if we don't have a cmov instruction bail.
1156 if (!Subtarget->hasCMov()) return false;
1157
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001158 unsigned Opc = 0;
1159 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001161 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001162 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001163 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001164 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001165 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001167 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001168 RC = &X86::GR64RegClass;
1169 } else {
1170 return false;
1171 }
1172
1173 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1174 if (Op0Reg == 0) return false;
1175 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1176 if (Op1Reg == 0) return false;
1177 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1178 if (Op2Reg == 0) return false;
1179
Dan Gohman84023e02010-07-10 09:00:22 +00001180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1181 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1184 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001185 UpdateValueMap(I, ResultReg);
1186 return true;
1187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001190 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001191 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001192 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001193 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001194 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001195 unsigned OpReg = getRegForValue(V);
1196 if (OpReg == 0) return false;
1197 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001198 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1199 TII.get(X86::CVTSS2SDrr), ResultReg)
1200 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001201 UpdateValueMap(I, ResultReg);
1202 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001203 }
1204 }
1205
1206 return false;
1207}
1208
Dan Gohman46510a72010-04-15 01:51:59 +00001209bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001210 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001211 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001212 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001213 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001214 unsigned OpReg = getRegForValue(V);
1215 if (OpReg == 0) return false;
1216 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1218 TII.get(X86::CVTSD2SSrr), ResultReg)
1219 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001220 UpdateValueMap(I, ResultReg);
1221 return true;
1222 }
1223 }
1224 }
1225
1226 return false;
1227}
1228
Dan Gohman46510a72010-04-15 01:51:59 +00001229bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001230 if (Subtarget->is64Bit())
1231 // All other cases should be handled by the tblgen generated code.
1232 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001233 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1234 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001235
1236 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001238 // All other cases should be handled by the tblgen generated code.
1239 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001241 // All other cases should be handled by the tblgen generated code.
1242 return false;
1243
1244 unsigned InputReg = getRegForValue(I->getOperand(0));
1245 if (!InputReg)
1246 // Unhandled operand. Halt "fast" selection and bail.
1247 return false;
1248
Dan Gohman62417622009-04-27 16:33:14 +00001249 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001251 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001252 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1254 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001255
1256 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001258 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001259 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001260 if (!ResultReg)
1261 return false;
1262
1263 UpdateValueMap(I, ResultReg);
1264 return true;
1265}
1266
Dan Gohman46510a72010-04-15 01:51:59 +00001267bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1268 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1269 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001270
Dan Gohman46510a72010-04-15 01:51:59 +00001271 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001272 switch (CI->getIntrinsicID()) {
1273 default: break;
1274 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001275 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001276 // Cheat a little. We know that the registers for "add" and "seto" are
1277 // allocated sequentially. However, we only keep track of the register
1278 // for "add" in the value map. Use extractvalue's index to get the
1279 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001280 unsigned OpReg = getRegForValue(Agg);
1281 if (OpReg == 0)
1282 return false;
1283 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001284 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001285 }
Dan Gohman84023e02010-07-10 09:00:22 +00001286 }
Bill Wendling52370a12008-12-09 02:42:50 +00001287 }
1288
1289 return false;
1290}
1291
Dan Gohman46510a72010-04-15 01:51:59 +00001292bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001293 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001294 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001295 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001296 case Intrinsic::stackprotector: {
1297 // Emit code inline code to store the stack guard onto the stack.
1298 EVT PtrTy = TLI.getPointerTy();
1299
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001300 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1301 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001302
1303 // Grab the frame index.
1304 X86AddressMode AM;
1305 if (!X86SelectAddress(Slot, AM)) return false;
1306
Eric Christopher88dee302010-03-18 21:58:33 +00001307 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1308
Eric Christopher07754c22010-03-18 20:27:26 +00001309 return true;
1310 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001311 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001312 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001313 const Type *Ty = I.getCalledFunction()->getReturnType();
1314
1315 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1316
1317 EVT VT;
1318 if (!isTypeLegal(Ty, VT))
1319 return false;
1320
1321 unsigned OpC = 0;
1322 if (VT == MVT::i32)
1323 OpC = X86::MOV32ri;
1324 else if (VT == MVT::i64)
1325 OpC = X86::MOV64ri;
1326 else
1327 return false;
1328
1329 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001331 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001332 UpdateValueMap(&I, ResultReg);
1333 return true;
1334 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001335 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001336 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001337 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001338 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001339 if (!X86SelectAddress(DI->getAddress(), AM))
1340 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001341 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001342 // FIXME may need to add RegState::Debug to any registers produced,
1343 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001344 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1345 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001346 return true;
1347 }
Eric Christopher77f79892010-01-18 22:11:29 +00001348 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001350 return true;
1351 }
Bill Wendling52370a12008-12-09 02:42:50 +00001352 case Intrinsic::sadd_with_overflow:
1353 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001354 // Replace "add with overflow" intrinsics with an "add" instruction followed
1355 // by a seto/setc instruction. Later on, when the "extractvalue"
1356 // instructions are encountered, we use the fact that two registers were
1357 // created sequentially to get the correct registers for the "sum" and the
1358 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001359 const Function *Callee = I.getCalledFunction();
1360 const Type *RetTy =
1361 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1362
Owen Andersone50ed302009-08-10 22:56:29 +00001363 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001364 if (!isTypeLegal(RetTy, VT))
1365 return false;
1366
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001367 const Value *Op1 = I.getArgOperand(0);
1368 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001369 unsigned Reg1 = getRegForValue(Op1);
1370 unsigned Reg2 = getRegForValue(Op2);
1371
1372 if (Reg1 == 0 || Reg2 == 0)
1373 // FIXME: Handle values *not* in registers.
1374 return false;
1375
1376 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001377 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001378 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001379 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001380 OpC = X86::ADD64rr;
1381 else
1382 return false;
1383
1384 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001385 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1386 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001387 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001388
Chris Lattner8d57b772009-04-12 07:51:14 +00001389 // If the add with overflow is an intra-block value then we just want to
1390 // create temporaries for it like normal. If it is a cross-block value then
1391 // UpdateValueMap will return the cross-block register used. Since we
1392 // *really* want the value to be live in the register pair known by
1393 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1394 // the cross block case. In the non-cross-block case, we should just make
1395 // another register for the value.
1396 if (DestReg1 != ResultReg)
1397 ResultReg = DestReg1+1;
1398 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001400
Chris Lattnera9a42252009-04-12 07:36:01 +00001401 unsigned Opc = X86::SETBr;
1402 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1403 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001404 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001405 return true;
1406 }
1407 }
1408}
1409
Dan Gohman46510a72010-04-15 01:51:59 +00001410bool X86FastISel::X86SelectCall(const Instruction *I) {
1411 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001412 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001413
1414 // Can't handle inline asm yet.
1415 if (isa<InlineAsm>(Callee))
1416 return false;
1417
Bill Wendling52370a12008-12-09 02:42:50 +00001418 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001419 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001420 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001421
Evan Chengf3d4efe2008-09-07 09:09:33 +00001422 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001423 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001424 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001425 if (CC != CallingConv::C &&
1426 CC != CallingConv::Fast &&
1427 CC != CallingConv::X86_FastCall)
1428 return false;
1429
Evan Cheng381993f2010-01-27 00:00:57 +00001430 // fastcc with -tailcallopt is intended to provide a guaranteed
1431 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001432 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001433 return false;
1434
Evan Chengf3d4efe2008-09-07 09:09:33 +00001435 // Let SDISel handle vararg functions.
1436 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1437 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1438 if (FTy->isVarArg())
1439 return false;
1440
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001441 // Fast-isel doesn't know about callee-pop yet.
1442 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1443 return false;
1444
Evan Chengf3d4efe2008-09-07 09:09:33 +00001445 // Handle *simple* calls for now.
1446 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001447 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001448 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001450 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001451 return false;
1452
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001453 // Materialize callee address in a register. FIXME: GV address can be
1454 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001455 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001456 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001457 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001458 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001459 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001460 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001461 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001462 } else if (CalleeAM.Base.Reg != 0) {
1463 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001464 } else
1465 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001466
Evan Chengdebdea02008-09-08 17:15:42 +00001467 // Allow calls which produce i1 results.
1468 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 if (RetVT == MVT::i1) {
1470 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001471 AndToI1 = true;
1472 }
1473
Evan Chengf3d4efe2008-09-07 09:09:33 +00001474 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001475 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001476 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001477 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001478 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001480 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001481 ArgVTs.reserve(CS.arg_size());
1482 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001483 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001484 i != e; ++i) {
1485 unsigned Arg = getRegForValue(*i);
1486 if (Arg == 0)
1487 return false;
1488 ISD::ArgFlagsTy Flags;
1489 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001490 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001491 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001492 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001493 Flags.setZExt();
1494
1495 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001496 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1497 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1498 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1499 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001500 return false;
1501
1502 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001503 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001504 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001505 return false;
1506 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1507 Flags.setOrigAlign(OriginalAlignment);
1508
1509 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001510 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 ArgVTs.push_back(ArgVT);
1512 ArgFlags.push_back(Flags);
1513 }
1514
1515 // Analyze operands of the call, assigning locations to each operand.
1516 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001517 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001518
1519 // Allocate shadow area for Win64
1520 if (Subtarget->isTargetWin64()) {
1521 CCInfo.AllocateStack(32, 8);
1522 }
1523
Duncan Sands45907662010-10-31 13:21:44 +00001524 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525
1526 // Get a count of how many bytes are to be pushed on the stack.
1527 unsigned NumBytes = CCInfo.getNextStackOffset();
1528
1529 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001530 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001531 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1532 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001533
Chris Lattner438949a2008-10-15 05:30:52 +00001534 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001535 // copies / loads.
1536 SmallVector<unsigned, 4> RegArgs;
1537 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1538 CCValAssign &VA = ArgLocs[i];
1539 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001540 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001541
1542 // Promote the value if needed.
1543 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001544 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001545 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001546 case CCValAssign::SExt: {
1547 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1548 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001549 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001550 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001551 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001552 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001553 }
1554 case CCValAssign::ZExt: {
1555 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1556 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001557 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001558 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001559 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001561 }
1562 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001563 // We don't handle MMX parameters yet.
1564 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1565 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001566 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1567 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001568 if (!Emitted)
1569 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001570 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001571 if (!Emitted)
1572 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1573 Arg, ArgVT, Arg);
1574
Chris Lattnera33649e2008-12-19 17:03:38 +00001575 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001576 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 break;
1578 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001579 case CCValAssign::BCvt: {
1580 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001581 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001582 assert(BC != 0 && "Failed to emit a bitcast!");
1583 Arg = BC;
1584 ArgVT = VA.getLocVT();
1585 break;
1586 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001587 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001588
1589 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001590 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1591 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001592 RegArgs.push_back(VA.getLocReg());
1593 } else {
1594 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001595 X86AddressMode AM;
1596 AM.Base.Reg = StackPtr;
1597 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001598 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001599
1600 // If this is a really simple value, emit this with the Value* version of
1601 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1602 // can cause us to reevaluate the argument.
1603 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1604 X86FastEmitStore(ArgVT, ArgVal, AM);
1605 else
1606 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001607 }
1608 }
1609
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001610 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1611 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001612 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001613 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001614 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1615 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001616 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001617
Evan Chengf3d4efe2008-09-07 09:09:33 +00001618 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001619 MachineInstrBuilder MIB;
1620 if (CalleeOp) {
1621 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001622 unsigned CallOpc;
1623 if (Subtarget->isTargetWin64())
1624 CallOpc = X86::WINCALL64r;
1625 else if (Subtarget->is64Bit())
1626 CallOpc = X86::CALL64r;
1627 else
1628 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001629 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1630 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001631
1632 } else {
1633 // Direct call.
1634 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001635 unsigned CallOpc;
1636 if (Subtarget->isTargetWin64())
1637 CallOpc = X86::WINCALL64pcrel32;
1638 else if (Subtarget->is64Bit())
1639 CallOpc = X86::CALL64pcrel32;
1640 else
1641 CallOpc = X86::CALLpcrel32;
Chris Lattner51e8eab2009-07-09 06:34:26 +00001642
1643 // See if we need any target-specific flags on the GV operand.
1644 unsigned char OpFlags = 0;
1645
1646 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1647 // external symbols most go through the PLT in PIC mode. If the symbol
1648 // has hidden or protected visibility, or if it is static or local, then
1649 // we don't need to use the PLT - we can directly call it.
1650 if (Subtarget->isTargetELF() &&
1651 TM.getRelocationModel() == Reloc::PIC_ &&
1652 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1653 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001654 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001655 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1656 Subtarget->getDarwinVers() < 9) {
1657 // PC-relative references to external symbols should go through $stub,
1658 // unless we're building with the leopard linker or later, which
1659 // automatically synthesizes these stubs.
1660 OpFlags = X86II::MO_DARWIN_STUB;
1661 }
1662
1663
Dan Gohman84023e02010-07-10 09:00:22 +00001664 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1665 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001666 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001667
1668 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001669 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001670 MIB.addReg(X86::EBX);
1671
Evan Chengf3d4efe2008-09-07 09:09:33 +00001672 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001673 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1674 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001675
1676 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001677 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1679 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001680
1681 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001682 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001683 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001684 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001685 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001686 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1687
1688 // Copy all of the result registers out of their specified physreg.
1689 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001690 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001691 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001692
1693 // If this is a call to a function that returns an fp value on the x87 fp
1694 // stack, but where we prefer to use the value in xmm registers, copy it
1695 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1696 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1697 RVLocs[0].getLocReg() == X86::ST1) &&
1698 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001700 DstRC = X86::RFP80RegisterClass;
1701 }
1702
1703 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001704 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1705 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001706 UsedRegs.push_back(RVLocs[0].getLocReg());
1707
Evan Chengf3d4efe2008-09-07 09:09:33 +00001708 if (CopyVT != RVLocs[0].getValVT()) {
1709 // Round the F80 the right size, which also moves to the appropriate xmm
1710 // register. This is accomplished by storing the F80 value in memory and
1711 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001712 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001714 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001715 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001716 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1717 TII.get(Opc)), FI)
1718 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001720 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001721 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001722 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001723 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1724 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001725 }
1726
Evan Chengdebdea02008-09-08 17:15:42 +00001727 if (AndToI1) {
1728 // Mask out all but lowest bit for some call which produces an i1.
1729 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001730 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001731 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001732 ResultReg = AndResult;
1733 }
1734
Evan Chengf3d4efe2008-09-07 09:09:33 +00001735 UpdateValueMap(I, ResultReg);
1736 }
1737
Dan Gohmandb497122010-06-18 23:28:01 +00001738 // Set all unused physreg defs as dead.
1739 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1740
Evan Chengf3d4efe2008-09-07 09:09:33 +00001741 return true;
1742}
1743
1744
Dan Gohman99b21822008-08-28 23:21:34 +00001745bool
Dan Gohman46510a72010-04-15 01:51:59 +00001746X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001747 switch (I->getOpcode()) {
1748 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001749 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001750 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001751 case Instruction::Store:
1752 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001753 case Instruction::Ret:
1754 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001755 case Instruction::ICmp:
1756 case Instruction::FCmp:
1757 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001758 case Instruction::ZExt:
1759 return X86SelectZExt(I);
1760 case Instruction::Br:
1761 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001762 case Instruction::Call:
1763 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001764 case Instruction::LShr:
1765 case Instruction::AShr:
1766 case Instruction::Shl:
1767 return X86SelectShift(I);
1768 case Instruction::Select:
1769 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001770 case Instruction::Trunc:
1771 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001772 case Instruction::FPExt:
1773 return X86SelectFPExt(I);
1774 case Instruction::FPTrunc:
1775 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001776 case Instruction::ExtractValue:
1777 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001778 case Instruction::IntToPtr: // Deliberate fall-through.
1779 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001780 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1781 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001782 if (DstVT.bitsGT(SrcVT))
1783 return X86SelectZExt(I);
1784 if (DstVT.bitsLT(SrcVT))
1785 return X86SelectTrunc(I);
1786 unsigned Reg = getRegForValue(I->getOperand(0));
1787 if (Reg == 0) return false;
1788 UpdateValueMap(I, Reg);
1789 return true;
1790 }
Dan Gohman99b21822008-08-28 23:21:34 +00001791 }
1792
1793 return false;
1794}
1795
Dan Gohman46510a72010-04-15 01:51:59 +00001796unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001797 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001798 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001799 return false;
1800
1801 // Get opcode and regclass of the output for the given load instruction.
1802 unsigned Opc = 0;
1803 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001805 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001807 Opc = X86::MOV8rm;
1808 RC = X86::GR8RegisterClass;
1809 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001811 Opc = X86::MOV16rm;
1812 RC = X86::GR16RegisterClass;
1813 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001815 Opc = X86::MOV32rm;
1816 RC = X86::GR32RegisterClass;
1817 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001819 // Must be in x86-64 mode.
1820 Opc = X86::MOV64rm;
1821 RC = X86::GR64RegisterClass;
1822 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001824 if (Subtarget->hasSSE1()) {
1825 Opc = X86::MOVSSrm;
1826 RC = X86::FR32RegisterClass;
1827 } else {
1828 Opc = X86::LD_Fp32m;
1829 RC = X86::RFP32RegisterClass;
1830 }
1831 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001833 if (Subtarget->hasSSE2()) {
1834 Opc = X86::MOVSDrm;
1835 RC = X86::FR64RegisterClass;
1836 } else {
1837 Opc = X86::LD_Fp64m;
1838 RC = X86::RFP64RegisterClass;
1839 }
1840 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001842 // No f80 support yet.
1843 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001844 }
1845
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001846 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001847 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001848 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001849 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001851 Opc = X86::LEA32r;
1852 else
1853 Opc = X86::LEA64r;
1854 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001855 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1856 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001857 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001858 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001859 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001860 }
1861
Owen Anderson3b217c62008-09-06 01:11:01 +00001862 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001863 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001864 if (Align == 0) {
1865 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001866 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001867 }
Owen Anderson95267a12008-09-05 00:06:23 +00001868
Dan Gohman5396c992008-09-30 01:21:32 +00001869 // x86-32 PIC requires a PIC base register for constant pools.
1870 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001871 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001872 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001873 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001874 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001875 } else if (Subtarget->isPICStyleGOT()) {
1876 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001877 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001878 } else if (Subtarget->isPICStyleRIPRel() &&
1879 TM.getCodeModel() == CodeModel::Small) {
1880 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001881 }
Dan Gohman5396c992008-09-30 01:21:32 +00001882
1883 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001884 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001885 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001886 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1887 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001888 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001889
Owen Anderson95267a12008-09-05 00:06:23 +00001890 return ResultReg;
1891}
1892
Dan Gohman46510a72010-04-15 01:51:59 +00001893unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001894 // Fail on dynamic allocas. At this point, getRegForValue has already
1895 // checked its CSE maps, so if we're here trying to handle a dynamic
1896 // alloca, we're not going to succeed. X86SelectAddress has a
1897 // check for dynamic allocas, because it's called directly from
1898 // various places, but TargetMaterializeAlloca also needs a check
1899 // in order to avoid recursion between getRegForValue,
1900 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001901 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001902 return 0;
1903
Dan Gohman0586d912008-09-10 20:11:02 +00001904 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001905 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001906 return 0;
1907 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1908 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1909 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001910 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1911 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001912 return ResultReg;
1913}
1914
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001915/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1916/// vreg is being provided by the specified load instruction. If possible,
1917/// try to fold the load as an operand to the instruction, returning true if
1918/// possible.
1919bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1920 const LoadInst *LI) {
1921 X86AddressMode AM;
1922 if (!X86SelectAddress(LI->getOperand(0), AM))
1923 return false;
1924
1925 X86InstrInfo &XII = (X86InstrInfo&)TII;
1926
1927 unsigned Size = TD.getTypeAllocSize(LI->getType());
1928 unsigned Alignment = LI->getAlignment();
1929
1930 SmallVector<MachineOperand, 8> AddrOps;
1931 AM.getFullAddress(AddrOps);
1932
1933 MachineInstr *Result =
1934 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1935 if (Result == 0) return false;
1936
1937 MI->getParent()->insert(MI, Result);
1938 MI->eraseFromParent();
1939 return true;
1940}
1941
1942
Evan Chengc3f44b02008-09-03 00:03:49 +00001943namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001944 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1945 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001946 }
Dan Gohman99b21822008-08-28 23:21:34 +00001947}