blob: a184c3995dded8734e51062916668d1c323c1d92 [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"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000025#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000026#include "llvm/IntrinsicInst.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000033#include "llvm/Support/GetElementPtrTypeIterator.h"
Dan Gohman7d04e4a2009-05-04 19:50:33 +000034#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000035using namespace llvm;
36
Chris Lattner087fcf32009-03-08 18:44:31 +000037namespace {
38
Evan Chengc3f44b02008-09-03 00:03:49 +000039class X86FastISel : public FastISel {
40 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
41 /// make the right decision when generating code for different targets.
42 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000043
44 /// StackPtr - Register used as the stack pointer.
45 ///
46 unsigned StackPtr;
47
48 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
49 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Cheng8b19e562008-09-03 06:44:39 +000055public:
Dan Gohman3df24e62008-09-03 23:12:08 +000056 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000057 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +000058 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +000059 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000060 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +000061 DenseMap<const AllocaInst *, int> &am
62#ifndef NDEBUG
63 , SmallSet<Instruction*, 8> &cil
64#endif
65 )
Devang Patel83489bb2009-01-13 00:35:13 +000066 : FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +000067#ifndef NDEBUG
68 , cil
69#endif
70 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000071 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000072 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
73 X86ScalarSSEf64 = Subtarget->hasSSE2();
74 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000075 }
Evan Chengc3f44b02008-09-03 00:03:49 +000076
Dan Gohman3df24e62008-09-03 23:12:08 +000077 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000078
Dan Gohman1adf1b02008-08-19 21:45:35 +000079#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000080
81private:
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitCompare(Value *LHS, Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000085
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000087 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000088 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000089 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000090
Owen Andersone50ed302009-08-10 22:56:29 +000091 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000092 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000093
Chris Lattner0aa43de2009-07-10 05:33:42 +000094 bool X86SelectAddress(Value *V, X86AddressMode &AM);
95 bool X86SelectCallAddress(Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000096
Dan Gohman3df24e62008-09-03 23:12:08 +000097 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000098
99 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000100
101 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000102
103 bool X86SelectZExt(Instruction *I);
104
105 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000106
107 bool X86SelectShift(Instruction *I);
108
109 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000110
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000111 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000112
Dan Gohman78efce62008-09-10 21:02:08 +0000113 bool X86SelectFPExt(Instruction *I);
114 bool X86SelectFPTrunc(Instruction *I);
115
Bill Wendling52370a12008-12-09 02:42:50 +0000116 bool X86SelectExtractValue(Instruction *I);
117
Chris Lattnera9a42252009-04-12 07:36:01 +0000118 bool X86VisitIntrinsicCall(IntrinsicInst &I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119 bool X86SelectCall(Instruction *I);
120
121 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
122
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000123 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000124 return getTargetMachine()->getInstrInfo();
125 }
126 const X86TargetMachine *getTargetMachine() const {
127 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000128 }
129
Dan Gohman0586d912008-09-10 20:11:02 +0000130 unsigned TargetMaterializeConstant(Constant *C);
131
132 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133
134 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
135 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000136 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
138 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139 }
140
Owen Andersone50ed302009-08-10 22:56:29 +0000141 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000142};
Chris Lattner087fcf32009-03-08 18:44:31 +0000143
144} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000145
Owen Andersone50ed302009-08-10 22:56:29 +0000146bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000147 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000149 // Unhandled type. Halt "fast" selection and bail.
150 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000151
Dan Gohman9b66d732008-09-30 00:48:39 +0000152 // For now, require SSE/SSE2 for performing floating-point operations,
153 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 return false;
158 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000160 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000161 // We only handle legal types. For example, on x86-32 the instruction
162 // selector contains all of the 64-bit instructions from x86-64,
163 // under the assumption that i64 won't be used if the target doesn't
164 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166}
167
168#include "X86GenCallingConv.inc"
169
170/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
171/// convention.
172CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
173 if (Subtarget->is64Bit()) {
174 if (Subtarget->isTargetWin64())
175 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 else
177 return CC_X86_64_C;
178 }
179
180 if (CC == CallingConv::X86_FastCall)
181 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000182 else if (CC == CallingConv::Fast)
183 return CC_X86_32_FastCC;
184 else
185 return CC_X86_32_C;
186}
187
Evan Cheng0de588f2008-09-05 21:00:03 +0000188/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000189/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000190/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000191bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000192 unsigned &ResultReg) {
193 // Get opcode and regclass of the output for the given load instruction.
194 unsigned Opc = 0;
195 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000198 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 Opc = X86::MOV8rm;
201 RC = X86::GR8RegisterClass;
202 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 Opc = X86::MOV16rm;
205 RC = X86::GR16RegisterClass;
206 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 Opc = X86::MOV32rm;
209 RC = X86::GR32RegisterClass;
210 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000212 // Must be in x86-64 mode.
213 Opc = X86::MOV64rm;
214 RC = X86::GR64RegisterClass;
215 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000217 if (Subtarget->hasSSE1()) {
218 Opc = X86::MOVSSrm;
219 RC = X86::FR32RegisterClass;
220 } else {
221 Opc = X86::LD_Fp32m;
222 RC = X86::RFP32RegisterClass;
223 }
224 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000226 if (Subtarget->hasSSE2()) {
227 Opc = X86::MOVSDrm;
228 RC = X86::FR64RegisterClass;
229 } else {
230 Opc = X86::LD_Fp64m;
231 RC = X86::RFP64RegisterClass;
232 }
233 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000235 // No f80 support yet.
236 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 }
238
239 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000240 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000241 return true;
242}
243
Evan Chengf3d4efe2008-09-07 09:09:33 +0000244/// X86FastEmitStore - Emit a machine instruction to store a value Val of
245/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
246/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000247/// i.e. V. Return true if it is possible.
248bool
Owen Andersone50ed302009-08-10 22:56:29 +0000249X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000250 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000251 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000252 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 switch (VT.getSimpleVT().SimpleTy) {
254 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000256 case MVT::i1: {
257 // Mask out all but lowest bit.
258 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
259 BuildMI(MBB, DL,
260 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
261 Val = AndResult;
262 }
263 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 case MVT::i8: Opc = X86::MOV8mr; break;
265 case MVT::i16: Opc = X86::MOV16mr; break;
266 case MVT::i32: Opc = X86::MOV32mr; break;
267 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
268 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000269 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000270 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000272 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000273 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 }
Chris Lattner438949a2008-10-15 05:30:52 +0000275
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000276 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 return true;
278}
279
Owen Andersone50ed302009-08-10 22:56:29 +0000280bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000281 const X86AddressMode &AM) {
282 // Handle 'null' like i32/i64 0.
283 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000284 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000285
286 // If this is a store of a simple constant, fold the constant into the store.
287 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
288 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000289 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000291 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000292 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 case MVT::i8: Opc = X86::MOV8mi; break;
294 case MVT::i16: Opc = X86::MOV16mi; break;
295 case MVT::i32: Opc = X86::MOV32mi; break;
296 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000297 // Must be a 32-bit sign extended value.
298 if ((int)CI->getSExtValue() == CI->getSExtValue())
299 Opc = X86::MOV64mi32;
300 break;
301 }
302
303 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000304 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000305 .addImm(Signed ? CI->getSExtValue() :
306 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000307 return true;
308 }
309 }
310
311 unsigned ValReg = getRegForValue(Val);
312 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000313 return false;
314
315 return X86FastEmitStore(VT, ValReg, AM);
316}
317
Evan Cheng24e3a902008-09-08 06:35:17 +0000318/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
319/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
320/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000321bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
322 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000323 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000324 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
325
326 if (RR != 0) {
327 ResultReg = RR;
328 return true;
329 } else
330 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000331}
332
Dan Gohman0586d912008-09-10 20:11:02 +0000333/// X86SelectAddress - Attempt to fill in an address from the given value.
334///
Chris Lattner0aa43de2009-07-10 05:33:42 +0000335bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Duncan Sands12513882009-06-03 12:05:18 +0000336 User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000337 unsigned Opcode = Instruction::UserOp1;
338 if (Instruction *I = dyn_cast<Instruction>(V)) {
339 Opcode = I->getOpcode();
340 U = I;
341 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
342 Opcode = C->getOpcode();
343 U = C;
344 }
Dan Gohman0586d912008-09-10 20:11:02 +0000345
Dan Gohman35893082008-09-18 23:23:44 +0000346 switch (Opcode) {
347 default: break;
348 case Instruction::BitCast:
349 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000350 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000351
352 case Instruction::IntToPtr:
353 // Look past no-op inttoptrs.
354 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000355 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000356 break;
Dan Gohman35893082008-09-18 23:23:44 +0000357
358 case Instruction::PtrToInt:
359 // Look past no-op ptrtoints.
360 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000361 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000362 break;
Dan Gohman35893082008-09-18 23:23:44 +0000363
364 case Instruction::Alloca: {
365 // Do static allocas.
366 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000367 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000368 if (SI != StaticAllocaMap.end()) {
369 AM.BaseType = X86AddressMode::FrameIndexBase;
370 AM.Base.FrameIndex = SI->second;
371 return true;
372 }
373 break;
Dan Gohman35893082008-09-18 23:23:44 +0000374 }
375
376 case Instruction::Add: {
377 // Adds of constants are common and easy enough.
378 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000379 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
380 // They have to fit in the 32-bit signed displacement field though.
381 if (isInt32(Disp)) {
382 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000383 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000384 }
Dan Gohman0586d912008-09-10 20:11:02 +0000385 }
Dan Gohman35893082008-09-18 23:23:44 +0000386 break;
387 }
388
389 case Instruction::GetElementPtr: {
390 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000391 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000392 unsigned IndexReg = AM.IndexReg;
393 unsigned Scale = AM.Scale;
394 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000395 // Iterate through the indices, folding what we can. Constants can be
396 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000397 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
398 i != e; ++i, ++GTI) {
399 Value *Op = *i;
400 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
401 const StructLayout *SL = TD.getStructLayout(STy);
402 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
403 Disp += SL->getElementOffset(Idx);
404 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000405 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman35893082008-09-18 23:23:44 +0000406 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
407 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000408 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000409 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000410 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000411 (S == 1 || S == 2 || S == 4 || S == 8)) {
412 // Scaled-index addressing.
413 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000414 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000415 if (IndexReg == 0)
416 return false;
417 } else
418 // Unsupported.
419 goto unsupported_gep;
420 }
421 }
Dan Gohman09aae462008-09-26 20:04:15 +0000422 // Check for displacement overflow.
423 if (!isInt32(Disp))
424 break;
Dan Gohman35893082008-09-18 23:23:44 +0000425 // Ok, the GEP indices were covered by constant-offset and scaled-index
426 // addressing. Update the address state and move on to examining the base.
427 AM.IndexReg = IndexReg;
428 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000429 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000430 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000431 unsupported_gep:
432 // Ok, the GEP indices weren't all covered.
433 break;
434 }
435 }
436
437 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000438 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000439 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000440 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000441 return false;
442
Dan Gohman97135e12008-09-26 19:15:30 +0000443 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000444 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000445 (AM.Base.Reg != 0 || AM.IndexReg != 0))
446 return false;
447
Dan Gohmane9865942009-02-23 22:03:08 +0000448 // Can't handle TLS yet.
449 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
450 if (GVar->isThreadLocal())
451 return false;
452
Chris Lattnerff7727f2009-07-09 06:41:35 +0000453 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000454 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000455
Chris Lattner0d786dd2009-07-10 07:48:51 +0000456 // Allow the subtarget to classify the global.
457 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
458
459 // If this reference is relative to the pic base, set it now.
460 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000461 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000462 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000463 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000464
465 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000466 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000467 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000468 if (Subtarget->isPICStyleRIPRel()) {
469 // Use rip-relative addressing if we can. Above we verified that the
470 // base and index registers are unused.
471 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
472 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000473 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000474 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000475 return true;
476 }
477
Chris Lattner0d786dd2009-07-10 07:48:51 +0000478 // Ok, we need to do a load from a stub. If we've already loaded from this
479 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000480 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
481 unsigned LoadReg;
482 if (I != LocalValueMap.end() && I->second != 0) {
483 LoadReg = I->second;
484 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000485 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000486 unsigned Opc = 0;
487 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000488 X86AddressMode StubAM;
489 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000490 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000491 StubAM.GVOpFlags = GVFlags;
492
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000494 Opc = X86::MOV64rm;
495 RC = X86::GR64RegisterClass;
496
Chris Lattner0d786dd2009-07-10 07:48:51 +0000497 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000498 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000499 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000500 Opc = X86::MOV32rm;
501 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000502 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000503
504 LoadReg = createResultReg(RC);
505 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
506
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000507 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000508 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000509 }
Chris Lattner18c59872009-06-27 04:16:01 +0000510
Chris Lattnerff7727f2009-07-09 06:41:35 +0000511 // Now construct the final address. Note that the Disp, Scale,
512 // and Index values may already be set here.
513 AM.Base.Reg = LoadReg;
514 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000515 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000516 }
517
Dan Gohman97135e12008-09-26 19:15:30 +0000518 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000519 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000520 if (AM.Base.Reg == 0) {
521 AM.Base.Reg = getRegForValue(V);
522 return AM.Base.Reg != 0;
523 }
524 if (AM.IndexReg == 0) {
525 assert(AM.Scale == 1 && "Scale with no index!");
526 AM.IndexReg = getRegForValue(V);
527 return AM.IndexReg != 0;
528 }
529 }
530
531 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000532}
533
Chris Lattner0aa43de2009-07-10 05:33:42 +0000534/// X86SelectCallAddress - Attempt to fill in an address from the given value.
535///
536bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
537 User *U = NULL;
538 unsigned Opcode = Instruction::UserOp1;
539 if (Instruction *I = dyn_cast<Instruction>(V)) {
540 Opcode = I->getOpcode();
541 U = I;
542 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
543 Opcode = C->getOpcode();
544 U = C;
545 }
546
547 switch (Opcode) {
548 default: break;
549 case Instruction::BitCast:
550 // Look past bitcasts.
551 return X86SelectCallAddress(U->getOperand(0), AM);
552
553 case Instruction::IntToPtr:
554 // Look past no-op inttoptrs.
555 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
556 return X86SelectCallAddress(U->getOperand(0), AM);
557 break;
558
559 case Instruction::PtrToInt:
560 // Look past no-op ptrtoints.
561 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
562 return X86SelectCallAddress(U->getOperand(0), AM);
563 break;
564 }
565
566 // Handle constant address.
567 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
568 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000569 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570 return false;
571
572 // RIP-relative addresses can't have additional register operands.
573 if (Subtarget->isPICStyleRIPRel() &&
574 (AM.Base.Reg != 0 || AM.IndexReg != 0))
575 return false;
576
Chris Lattner754b7652009-07-10 05:48:03 +0000577 // Can't handle TLS or DLLImport.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000578 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000579 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000580 return false;
581
582 // Okay, we've committed to selecting this global. Set up the basic address.
583 AM.GV = GV;
584
Chris Lattnere6c07b52009-07-10 05:45:15 +0000585 // No ABI requires an extra load for anything other than DLLImport, which
586 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000587 if (Subtarget->isPICStyleRIPRel()) {
588 // Use rip-relative addressing if we can. Above we verified that the
589 // base and index registers are unused.
590 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
591 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000592 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000593 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
594 } else if (Subtarget->isPICStyleGOT()) {
595 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000596 }
597
Chris Lattner0aa43de2009-07-10 05:33:42 +0000598 return true;
599 }
600
601 // If all else fails, try to materialize the value in a register.
602 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
603 if (AM.Base.Reg == 0) {
604 AM.Base.Reg = getRegForValue(V);
605 return AM.Base.Reg != 0;
606 }
607 if (AM.IndexReg == 0) {
608 assert(AM.Scale == 1 && "Scale with no index!");
609 AM.IndexReg = getRegForValue(V);
610 return AM.IndexReg != 0;
611 }
612 }
613
614 return false;
615}
616
617
Owen Andersona3971df2008-09-04 07:08:58 +0000618/// X86SelectStore - Select and emit code to implement store instructions.
619bool X86FastISel::X86SelectStore(Instruction* I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000620 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000621 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000622 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000623
Dan Gohman0586d912008-09-10 20:11:02 +0000624 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000625 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000626 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000627
Chris Lattner438949a2008-10-15 05:30:52 +0000628 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000629}
630
Evan Cheng8b19e562008-09-03 06:44:39 +0000631/// X86SelectLoad - Select and emit code to implement load instructions.
632///
Dan Gohman3df24e62008-09-03 23:12:08 +0000633bool X86FastISel::X86SelectLoad(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000634 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000635 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000636 return false;
637
Dan Gohman0586d912008-09-10 20:11:02 +0000638 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000639 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000640 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000641
Evan Cheng0de588f2008-09-05 21:00:03 +0000642 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000643 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000644 UpdateValueMap(I, ResultReg);
645 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000646 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000647 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000648}
649
Owen Andersone50ed302009-08-10 22:56:29 +0000650static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000652 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 case MVT::i8: return X86::CMP8rr;
654 case MVT::i16: return X86::CMP16rr;
655 case MVT::i32: return X86::CMP32rr;
656 case MVT::i64: return X86::CMP64rr;
657 case MVT::f32: return X86::UCOMISSrr;
658 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000659 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000660}
661
Chris Lattner0e13c782008-10-15 04:13:29 +0000662/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
663/// of the comparison, return an opcode that works for the compare (e.g.
664/// CMP32ri) otherwise return 0.
Owen Andersone50ed302009-08-10 22:56:29 +0000665static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000667 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000668 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 case MVT::i8: return X86::CMP8ri;
670 case MVT::i16: return X86::CMP16ri;
671 case MVT::i32: return X86::CMP32ri;
672 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000673 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
674 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000675 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000676 return X86::CMP64ri32;
677 return 0;
678 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000679}
680
Owen Andersone50ed302009-08-10 22:56:29 +0000681bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000682 unsigned Op0Reg = getRegForValue(Op0);
683 if (Op0Reg == 0) return false;
684
Chris Lattnerd53886b2008-10-15 05:18:04 +0000685 // Handle 'null' like i32/i64 0.
686 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000687 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000688
Chris Lattner9a08a612008-10-15 04:26:38 +0000689 // We have two options: compare with register or immediate. If the RHS of
690 // the compare is an immediate that we can fold into this compare, use
691 // CMPri, otherwise use CMPrr.
692 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000693 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000694 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000695 .addImm(Op1C->getSExtValue());
696 return true;
697 }
698 }
699
700 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
701 if (CompareOpc == 0) return false;
702
703 unsigned Op1Reg = getRegForValue(Op1);
704 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000705 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000706
707 return true;
708}
709
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000710bool X86FastISel::X86SelectCmp(Instruction *I) {
711 CmpInst *CI = cast<CmpInst>(I);
712
Owen Andersone50ed302009-08-10 22:56:29 +0000713 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000714 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000715 return false;
716
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000717 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000718 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000719 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000720 switch (CI->getPredicate()) {
721 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000722 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
723 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000724
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000725 unsigned EReg = createResultReg(&X86::GR8RegClass);
726 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000727 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
728 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
729 BuildMI(MBB, DL,
730 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000731 UpdateValueMap(I, ResultReg);
732 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000733 }
734 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000735 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
736 return false;
737
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000738 unsigned NEReg = createResultReg(&X86::GR8RegClass);
739 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000740 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
741 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
742 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000743 UpdateValueMap(I, ResultReg);
744 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000745 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000746 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
747 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
748 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
749 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
750 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
751 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
752 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
753 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
754 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
755 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
756 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
757 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
758
759 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
760 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
761 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
762 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
763 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
764 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
765 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
766 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
767 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
768 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000769 default:
770 return false;
771 }
772
Chris Lattner9a08a612008-10-15 04:26:38 +0000773 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000774 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000775 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000776
Chris Lattner9a08a612008-10-15 04:26:38 +0000777 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000778 if (!X86FastEmitCompare(Op0, Op1, VT))
779 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000780
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000781 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000782 UpdateValueMap(I, ResultReg);
783 return true;
784}
Evan Cheng8b19e562008-09-03 06:44:39 +0000785
Dan Gohmand89ae992008-09-05 01:06:14 +0000786bool X86FastISel::X86SelectZExt(Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000787 // Handle zero-extension from i1 to i8, which is common.
Owen Anderson1d0be152009-08-13 21:58:54 +0000788 if (I->getType() == Type::getInt8Ty(I->getContext()) &&
789 I->getOperand(0)->getType() == Type::getInt1Ty(I->getContext())) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000790 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000791 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000792 // Set the high bits to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000794 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000795 UpdateValueMap(I, ResultReg);
796 return true;
797 }
798
799 return false;
800}
801
Chris Lattner9a08a612008-10-15 04:26:38 +0000802
Dan Gohmand89ae992008-09-05 01:06:14 +0000803bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000804 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000805 // Handle a conditional branch.
806 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000807 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
808 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
809
Dan Gohmand98d6202008-10-02 22:15:21 +0000810 // Fold the common case of a conditional branch with a comparison.
811 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
812 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000813 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000814
Dan Gohmand98d6202008-10-02 22:15:21 +0000815 // Try to take advantage of fallthrough opportunities.
816 CmpInst::Predicate Predicate = CI->getPredicate();
817 if (MBB->isLayoutSuccessor(TrueMBB)) {
818 std::swap(TrueMBB, FalseMBB);
819 Predicate = CmpInst::getInversePredicate(Predicate);
820 }
821
Chris Lattner871d2462008-10-15 03:58:05 +0000822 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
823 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
824
Dan Gohmand98d6202008-10-02 22:15:21 +0000825 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000826 case CmpInst::FCMP_OEQ:
827 std::swap(TrueMBB, FalseMBB);
828 Predicate = CmpInst::FCMP_UNE;
829 // FALL THROUGH
830 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner871d2462008-10-15 03:58:05 +0000831 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
832 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
833 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
834 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
835 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
836 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
837 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
838 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
839 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
840 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
841 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
842 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000843
Chris Lattner871d2462008-10-15 03:58:05 +0000844 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
845 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
846 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
847 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
848 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
849 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
850 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
851 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
852 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
853 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000854 default:
855 return false;
856 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000857
Chris Lattner709d8292008-10-15 04:02:26 +0000858 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
859 if (SwapArgs)
860 std::swap(Op0, Op1);
861
Chris Lattner9a08a612008-10-15 04:26:38 +0000862 // Emit a compare of the LHS and RHS, setting the flags.
863 if (!X86FastEmitCompare(Op0, Op1, VT))
864 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000865
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000866 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000867
868 if (Predicate == CmpInst::FCMP_UNE) {
869 // X86 requires a second branch to handle UNE (and OEQ,
870 // which is mapped to UNE above).
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000871 BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000872 }
873
Dan Gohmand98d6202008-10-02 22:15:21 +0000874 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000875 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000876 return true;
877 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000878 } else if (ExtractValueInst *EI =
879 dyn_cast<ExtractValueInst>(BI->getCondition())) {
880 // Check to see if the branch instruction is from an "arithmetic with
881 // overflow" intrinsic. The main way these intrinsics are used is:
882 //
883 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
884 // %sum = extractvalue { i32, i1 } %t, 0
885 // %obit = extractvalue { i32, i1 } %t, 1
886 // br i1 %obit, label %overflow, label %normal
887 //
Dan Gohman653456c2009-01-07 00:15:08 +0000888 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000889 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000890 // looking for the SETO/SETB instruction. If an instruction modifies the
891 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
892 // convert the branch into a JO/JB instruction.
Chris Lattnera9a42252009-04-12 07:36:01 +0000893 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
894 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
895 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
896 const MachineInstr *SetMI = 0;
897 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000898
Chris Lattnera9a42252009-04-12 07:36:01 +0000899 for (MachineBasicBlock::const_reverse_iterator
900 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
901 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000902
Chris Lattnera9a42252009-04-12 07:36:01 +0000903 if (MI.modifiesRegister(Reg)) {
904 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000905
Chris Lattnera9a42252009-04-12 07:36:01 +0000906 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
907 Reg = Src;
908 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000909 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000910
Chris Lattnera9a42252009-04-12 07:36:01 +0000911 SetMI = &MI;
912 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000913 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000914
Chris Lattnera9a42252009-04-12 07:36:01 +0000915 const TargetInstrDesc &TID = MI.getDesc();
916 if (TID.hasUnmodeledSideEffects() ||
917 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
918 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000919 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000920
921 if (SetMI) {
922 unsigned OpCode = SetMI->getOpcode();
923
924 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattner8d57b772009-04-12 07:51:14 +0000925 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ? X86::JO : X86::JB))
926 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000927 FastEmitBranch(FalseMBB);
928 MBB->addSuccessor(TrueMBB);
929 return true;
930 }
Bill Wendling9a901322008-12-10 19:44:24 +0000931 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000932 }
933 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000934 }
935
936 // Otherwise do a clumsy setcc and re-test it.
937 unsigned OpReg = getRegForValue(BI->getCondition());
938 if (OpReg == 0) return false;
939
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000940 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
941 BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000942 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000943 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000944 return true;
945}
946
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000947bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000948 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000949 const TargetRegisterClass *RC = NULL;
Owen Anderson1d0be152009-08-13 21:58:54 +0000950 if (I->getType() == Type::getInt8Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000951 CReg = X86::CL;
952 RC = &X86::GR8RegClass;
953 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000954 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
955 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
956 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000957 default: return false;
958 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000959 } else if (I->getType() == Type::getInt16Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000960 CReg = X86::CX;
961 RC = &X86::GR16RegClass;
962 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000963 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
964 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
965 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000966 default: return false;
967 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000968 } else if (I->getType() == Type::getInt32Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000969 CReg = X86::ECX;
970 RC = &X86::GR32RegClass;
971 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000972 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
973 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
974 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000975 default: return false;
976 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000977 } else if (I->getType() == Type::getInt64Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000978 CReg = X86::RCX;
979 RC = &X86::GR64RegClass;
980 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000981 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
982 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
983 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000984 default: return false;
985 }
986 } else {
987 return false;
988 }
989
Owen Andersone50ed302009-08-10 22:56:29 +0000990 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000992 return false;
993
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000994 unsigned Op0Reg = getRegForValue(I->getOperand(0));
995 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000996
997 // Fold immediate in shl(x,3).
998 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
999 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001000 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001001 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001002 UpdateValueMap(I, ResultReg);
1003 return true;
1004 }
1005
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001006 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1007 if (Op1Reg == 0) return false;
1008 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +00001009
1010 // The shift instruction uses X86::CL. If we defined a super-register
1011 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1012 // we're doing here.
1013 if (CReg != X86::CL)
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001014 BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001015 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1016
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001017 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001018 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001019 UpdateValueMap(I, ResultReg);
1020 return true;
1021}
1022
1023bool X86FastISel::X86SelectSelect(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001024 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001026 return false;
1027
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001028 unsigned Opc = 0;
1029 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001031 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001032 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001034 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001035 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001037 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001038 RC = &X86::GR64RegClass;
1039 } else {
1040 return false;
1041 }
1042
1043 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1044 if (Op0Reg == 0) return false;
1045 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1046 if (Op1Reg == 0) return false;
1047 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1048 if (Op2Reg == 0) return false;
1049
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001050 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001051 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001052 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001053 UpdateValueMap(I, ResultReg);
1054 return true;
1055}
1056
Dan Gohman78efce62008-09-10 21:02:08 +00001057bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001058 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001059 if (Subtarget->hasSSE2() &&
1060 I->getType() == Type::getDoubleTy(I->getContext())) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001061 Value *V = I->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001062 if (V->getType() == Type::getFloatTy(I->getContext())) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001063 unsigned OpReg = getRegForValue(V);
1064 if (OpReg == 0) return false;
1065 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001066 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001067 UpdateValueMap(I, ResultReg);
1068 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001069 }
1070 }
1071
1072 return false;
1073}
1074
1075bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
1076 if (Subtarget->hasSSE2()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001077 if (I->getType() == Type::getFloatTy(I->getContext())) {
Dan Gohman78efce62008-09-10 21:02:08 +00001078 Value *V = I->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001079 if (V->getType() == Type::getDoubleTy(I->getContext())) {
Dan Gohman78efce62008-09-10 21:02:08 +00001080 unsigned OpReg = getRegForValue(V);
1081 if (OpReg == 0) return false;
1082 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001083 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001084 UpdateValueMap(I, ResultReg);
1085 return true;
1086 }
1087 }
1088 }
1089
1090 return false;
1091}
1092
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001093bool X86FastISel::X86SelectTrunc(Instruction *I) {
1094 if (Subtarget->is64Bit())
1095 // All other cases should be handled by the tblgen generated code.
1096 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001097 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1098 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001099
1100 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001102 // All other cases should be handled by the tblgen generated code.
1103 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001105 // All other cases should be handled by the tblgen generated code.
1106 return false;
1107
1108 unsigned InputReg = getRegForValue(I->getOperand(0));
1109 if (!InputReg)
1110 // Unhandled operand. Halt "fast" selection and bail.
1111 return false;
1112
Dan Gohman62417622009-04-27 16:33:14 +00001113 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001114 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1115 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001116 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001117 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001118 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001119
1120 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Evan Cheng536ab132009-01-22 09:10:11 +00001122 CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001123 if (!ResultReg)
1124 return false;
1125
1126 UpdateValueMap(I, ResultReg);
1127 return true;
1128}
1129
Bill Wendling52370a12008-12-09 02:42:50 +00001130bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1131 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1132 Value *Agg = EI->getAggregateOperand();
1133
Chris Lattnera9a42252009-04-12 07:36:01 +00001134 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
1135 switch (CI->getIntrinsicID()) {
1136 default: break;
1137 case Intrinsic::sadd_with_overflow:
1138 case Intrinsic::uadd_with_overflow:
1139 // Cheat a little. We know that the registers for "add" and "seto" are
1140 // allocated sequentially. However, we only keep track of the register
1141 // for "add" in the value map. Use extractvalue's index to get the
1142 // correct register for "seto".
1143 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1144 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001145 }
1146 }
1147
1148 return false;
1149}
1150
Chris Lattnera9a42252009-04-12 07:36:01 +00001151bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001152 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001153 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001154 default: return false;
1155 case Intrinsic::sadd_with_overflow:
1156 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001157 // Replace "add with overflow" intrinsics with an "add" instruction followed
1158 // by a seto/setc instruction. Later on, when the "extractvalue"
1159 // instructions are encountered, we use the fact that two registers were
1160 // created sequentially to get the correct registers for the "sum" and the
1161 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001162 const Function *Callee = I.getCalledFunction();
1163 const Type *RetTy =
1164 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1165
Owen Andersone50ed302009-08-10 22:56:29 +00001166 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001167 if (!isTypeLegal(RetTy, VT))
1168 return false;
1169
1170 Value *Op1 = I.getOperand(1);
1171 Value *Op2 = I.getOperand(2);
1172 unsigned Reg1 = getRegForValue(Op1);
1173 unsigned Reg2 = getRegForValue(Op2);
1174
1175 if (Reg1 == 0 || Reg2 == 0)
1176 // FIXME: Handle values *not* in registers.
1177 return false;
1178
1179 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001180 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001181 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001183 OpC = X86::ADD64rr;
1184 else
1185 return false;
1186
1187 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001188 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001189 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001190
Chris Lattner8d57b772009-04-12 07:51:14 +00001191 // If the add with overflow is an intra-block value then we just want to
1192 // create temporaries for it like normal. If it is a cross-block value then
1193 // UpdateValueMap will return the cross-block register used. Since we
1194 // *really* want the value to be live in the register pair known by
1195 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1196 // the cross block case. In the non-cross-block case, we should just make
1197 // another register for the value.
1198 if (DestReg1 != ResultReg)
1199 ResultReg = DestReg1+1;
1200 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001202
Chris Lattnera9a42252009-04-12 07:36:01 +00001203 unsigned Opc = X86::SETBr;
1204 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1205 Opc = X86::SETOr;
1206 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001207 return true;
1208 }
1209 }
1210}
1211
Evan Chengf3d4efe2008-09-07 09:09:33 +00001212bool X86FastISel::X86SelectCall(Instruction *I) {
1213 CallInst *CI = cast<CallInst>(I);
1214 Value *Callee = I->getOperand(0);
1215
1216 // Can't handle inline asm yet.
1217 if (isa<InlineAsm>(Callee))
1218 return false;
1219
Bill Wendling52370a12008-12-09 02:42:50 +00001220 // Handle intrinsic calls.
Chris Lattnera9a42252009-04-12 07:36:01 +00001221 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1222 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001223
Evan Chengf3d4efe2008-09-07 09:09:33 +00001224 // Handle only C and fastcc calling conventions for now.
1225 CallSite CS(CI);
1226 unsigned CC = CS.getCallingConv();
1227 if (CC != CallingConv::C &&
1228 CC != CallingConv::Fast &&
1229 CC != CallingConv::X86_FastCall)
1230 return false;
1231
Dan Gohman7d04e4a2009-05-04 19:50:33 +00001232 // On X86, -tailcallopt changes the fastcc ABI. FastISel doesn't
1233 // handle this for now.
1234 if (CC == CallingConv::Fast && PerformTailCallOpt)
1235 return false;
1236
Evan Chengf3d4efe2008-09-07 09:09:33 +00001237 // Let SDISel handle vararg functions.
1238 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1239 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1240 if (FTy->isVarArg())
1241 return false;
1242
1243 // Handle *simple* calls for now.
1244 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001245 EVT RetVT;
Owen Anderson1d0be152009-08-13 21:58:54 +00001246 if (RetTy == Type::getVoidTy(I->getContext()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001248 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001249 return false;
1250
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001251 // Materialize callee address in a register. FIXME: GV address can be
1252 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001253 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001254 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001255 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001256 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001257 GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001258 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001259 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001260 } else if (CalleeAM.Base.Reg != 0) {
1261 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001262 } else
1263 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001264
Evan Chengdebdea02008-09-08 17:15:42 +00001265 // Allow calls which produce i1 results.
1266 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 if (RetVT == MVT::i1) {
1268 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001269 AndToI1 = true;
1270 }
1271
Evan Chengf3d4efe2008-09-07 09:09:33 +00001272 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001273 SmallVector<Value*, 8> ArgVals;
1274 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001275 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001276 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001277 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001278 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001279 ArgVTs.reserve(CS.arg_size());
1280 ArgFlags.reserve(CS.arg_size());
1281 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1282 i != e; ++i) {
1283 unsigned Arg = getRegForValue(*i);
1284 if (Arg == 0)
1285 return false;
1286 ISD::ArgFlagsTy Flags;
1287 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001288 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001289 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001290 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001291 Flags.setZExt();
1292
1293 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001294 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1295 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1296 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1297 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001298 return false;
1299
1300 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001301 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001302 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001303 return false;
1304 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1305 Flags.setOrigAlign(OriginalAlignment);
1306
1307 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001308 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001309 ArgVTs.push_back(ArgVT);
1310 ArgFlags.push_back(Flags);
1311 }
1312
1313 // Analyze operands of the call, assigning locations to each operand.
1314 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001315 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001316 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1317
1318 // Get a count of how many bytes are to be pushed on the stack.
1319 unsigned NumBytes = CCInfo.getNextStackOffset();
1320
1321 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001322 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001323 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001324
Chris Lattner438949a2008-10-15 05:30:52 +00001325 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001326 // copies / loads.
1327 SmallVector<unsigned, 4> RegArgs;
1328 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1329 CCValAssign &VA = ArgLocs[i];
1330 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001331 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001332
1333 // Promote the value if needed.
1334 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001335 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001336 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001337 case CCValAssign::SExt: {
1338 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1339 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001340 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001341 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001342 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001343 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001344 }
1345 case CCValAssign::ZExt: {
1346 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1347 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001348 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001349 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001350 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001351 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001352 }
1353 case CCValAssign::AExt: {
1354 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1355 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001356 if (!Emitted)
1357 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001358 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001359 if (!Emitted)
1360 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1361 Arg, ArgVT, Arg);
1362
Chris Lattnera33649e2008-12-19 17:03:38 +00001363 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001364 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001365 break;
1366 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001367 case CCValAssign::BCvt: {
1368 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
1369 ISD::BIT_CONVERT, Arg);
1370 assert(BC != 0 && "Failed to emit a bitcast!");
1371 Arg = BC;
1372 ArgVT = VA.getLocVT();
1373 break;
1374 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001375 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001376
1377 if (VA.isRegLoc()) {
1378 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1379 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1380 Arg, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001381 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001382 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001383 RegArgs.push_back(VA.getLocReg());
1384 } else {
1385 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001386 X86AddressMode AM;
1387 AM.Base.Reg = StackPtr;
1388 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001389 Value *ArgVal = ArgVals[VA.getValNo()];
1390
1391 // If this is a really simple value, emit this with the Value* version of
1392 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1393 // can cause us to reevaluate the argument.
1394 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1395 X86FastEmitStore(ArgVT, ArgVal, AM);
1396 else
1397 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001398 }
1399 }
1400
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001401 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1402 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001403 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001404 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001405 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001406 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001407 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001408 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001409 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001410
Evan Chengf3d4efe2008-09-07 09:09:33 +00001411 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001412 MachineInstrBuilder MIB;
1413 if (CalleeOp) {
1414 // Register-indirect call.
1415 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1416 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1417
1418 } else {
1419 // Direct call.
1420 assert(GV && "Not a direct call");
1421 unsigned CallOpc =
1422 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1423
1424 // See if we need any target-specific flags on the GV operand.
1425 unsigned char OpFlags = 0;
1426
1427 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1428 // external symbols most go through the PLT in PIC mode. If the symbol
1429 // has hidden or protected visibility, or if it is static or local, then
1430 // we don't need to use the PLT - we can directly call it.
1431 if (Subtarget->isTargetELF() &&
1432 TM.getRelocationModel() == Reloc::PIC_ &&
1433 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1434 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001435 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001436 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1437 Subtarget->getDarwinVers() < 9) {
1438 // PC-relative references to external symbols should go through $stub,
1439 // unless we're building with the leopard linker or later, which
1440 // automatically synthesizes these stubs.
1441 OpFlags = X86II::MO_DARWIN_STUB;
1442 }
1443
1444
1445 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1446 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001447
1448 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001449 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001450 MIB.addReg(X86::EBX);
1451
Evan Chengf3d4efe2008-09-07 09:09:33 +00001452 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001453 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1454 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001455
1456 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001457 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001458 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001459
1460 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001462 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001463 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001464 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1465
1466 // Copy all of the result registers out of their specified physreg.
1467 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001468 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001469 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1470 TargetRegisterClass *SrcRC = DstRC;
1471
1472 // If this is a call to a function that returns an fp value on the x87 fp
1473 // stack, but where we prefer to use the value in xmm registers, copy it
1474 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1475 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1476 RVLocs[0].getLocReg() == X86::ST1) &&
1477 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 SrcRC = X86::RSTRegisterClass;
1480 DstRC = X86::RFP80RegisterClass;
1481 }
1482
1483 unsigned ResultReg = createResultReg(DstRC);
1484 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1485 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001486 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001487 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001488 if (CopyVT != RVLocs[0].getValVT()) {
1489 // Round the F80 the right size, which also moves to the appropriate xmm
1490 // register. This is accomplished by storing the F80 value in memory and
1491 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001492 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001494 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001495 int FI = MFI.CreateStackObject(MemSize, MemSize);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001496 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001497 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001498 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001499 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001500 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001501 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001502 }
1503
Evan Chengdebdea02008-09-08 17:15:42 +00001504 if (AndToI1) {
1505 // Mask out all but lowest bit for some call which produces an i1.
1506 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001507 BuildMI(MBB, DL,
1508 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001509 ResultReg = AndResult;
1510 }
1511
Evan Chengf3d4efe2008-09-07 09:09:33 +00001512 UpdateValueMap(I, ResultReg);
1513 }
1514
1515 return true;
1516}
1517
1518
Dan Gohman99b21822008-08-28 23:21:34 +00001519bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001520X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001521 switch (I->getOpcode()) {
1522 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001523 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001524 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001525 case Instruction::Store:
1526 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001527 case Instruction::ICmp:
1528 case Instruction::FCmp:
1529 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001530 case Instruction::ZExt:
1531 return X86SelectZExt(I);
1532 case Instruction::Br:
1533 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001534 case Instruction::Call:
1535 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001536 case Instruction::LShr:
1537 case Instruction::AShr:
1538 case Instruction::Shl:
1539 return X86SelectShift(I);
1540 case Instruction::Select:
1541 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001542 case Instruction::Trunc:
1543 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001544 case Instruction::FPExt:
1545 return X86SelectFPExt(I);
1546 case Instruction::FPTrunc:
1547 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001548 case Instruction::ExtractValue:
1549 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001550 case Instruction::IntToPtr: // Deliberate fall-through.
1551 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001552 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1553 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001554 if (DstVT.bitsGT(SrcVT))
1555 return X86SelectZExt(I);
1556 if (DstVT.bitsLT(SrcVT))
1557 return X86SelectTrunc(I);
1558 unsigned Reg = getRegForValue(I->getOperand(0));
1559 if (Reg == 0) return false;
1560 UpdateValueMap(I, Reg);
1561 return true;
1562 }
Dan Gohman99b21822008-08-28 23:21:34 +00001563 }
1564
1565 return false;
1566}
1567
Dan Gohman0586d912008-09-10 20:11:02 +00001568unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001569 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001570 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001571 return false;
1572
1573 // Get opcode and regclass of the output for the given load instruction.
1574 unsigned Opc = 0;
1575 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001577 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001579 Opc = X86::MOV8rm;
1580 RC = X86::GR8RegisterClass;
1581 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001583 Opc = X86::MOV16rm;
1584 RC = X86::GR16RegisterClass;
1585 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001587 Opc = X86::MOV32rm;
1588 RC = X86::GR32RegisterClass;
1589 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001591 // Must be in x86-64 mode.
1592 Opc = X86::MOV64rm;
1593 RC = X86::GR64RegisterClass;
1594 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001596 if (Subtarget->hasSSE1()) {
1597 Opc = X86::MOVSSrm;
1598 RC = X86::FR32RegisterClass;
1599 } else {
1600 Opc = X86::LD_Fp32m;
1601 RC = X86::RFP32RegisterClass;
1602 }
1603 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001605 if (Subtarget->hasSSE2()) {
1606 Opc = X86::MOVSDrm;
1607 RC = X86::FR64RegisterClass;
1608 } else {
1609 Opc = X86::LD_Fp64m;
1610 RC = X86::RFP64RegisterClass;
1611 }
1612 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001614 // No f80 support yet.
1615 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001616 }
1617
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001618 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001619 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001620 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001621 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001623 Opc = X86::LEA32r;
1624 else
1625 Opc = X86::LEA64r;
1626 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001627 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001628 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001629 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001630 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001631 }
1632
Owen Anderson3b217c62008-09-06 01:11:01 +00001633 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001634 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001635 if (Align == 0) {
1636 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001637 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001638 }
Owen Anderson95267a12008-09-05 00:06:23 +00001639
Dan Gohman5396c992008-09-30 01:21:32 +00001640 // x86-32 PIC requires a PIC base register for constant pools.
1641 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001642 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001643 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001644 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1645 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1646 } else if (Subtarget->isPICStyleGOT()) {
1647 OpFlag = X86II::MO_GOTOFF;
1648 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1649 } else if (Subtarget->isPICStyleRIPRel() &&
1650 TM.getCodeModel() == CodeModel::Small) {
1651 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001652 }
Dan Gohman5396c992008-09-30 01:21:32 +00001653
1654 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001655 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001656 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001657 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1658 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001659
Owen Anderson95267a12008-09-05 00:06:23 +00001660 return ResultReg;
1661}
1662
Dan Gohman0586d912008-09-10 20:11:02 +00001663unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001664 // Fail on dynamic allocas. At this point, getRegForValue has already
1665 // checked its CSE maps, so if we're here trying to handle a dynamic
1666 // alloca, we're not going to succeed. X86SelectAddress has a
1667 // check for dynamic allocas, because it's called directly from
1668 // various places, but TargetMaterializeAlloca also needs a check
1669 // in order to avoid recursion between getRegForValue,
1670 // X86SelectAddrss, and TargetMaterializeAlloca.
1671 if (!StaticAllocaMap.count(C))
1672 return 0;
1673
Dan Gohman0586d912008-09-10 20:11:02 +00001674 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001675 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001676 return 0;
1677 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1678 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1679 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001680 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001681 return ResultReg;
1682}
1683
Evan Chengc3f44b02008-09-03 00:03:49 +00001684namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001685 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001686 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +00001687 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001688 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001689 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001690 DenseMap<const AllocaInst *, int> &am
1691#ifndef NDEBUG
1692 , SmallSet<Instruction*, 8> &cil
1693#endif
1694 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001695 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001696#ifndef NDEBUG
1697 , cil
1698#endif
1699 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001700 }
Dan Gohman99b21822008-08-28 23:21:34 +00001701}