blob: 10fba2461edf122b10423cb36572bec948ba86ed [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
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000121 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000122
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.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000172CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
173 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174 if (Subtarget->is64Bit()) {
175 if (Subtarget->isTargetWin64())
176 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_64_C;
179 }
180
181 if (CC == CallingConv::X86_FastCall)
182 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000183 else if (CC == CallingConv::Fast)
184 return CC_X86_32_FastCC;
185 else
186 return CC_X86_32_C;
187}
188
Evan Cheng0de588f2008-09-05 21:00:03 +0000189/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000190/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000191/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000192bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 unsigned &ResultReg) {
194 // Get opcode and regclass of the output for the given load instruction.
195 unsigned Opc = 0;
196 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000198 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000199 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 Opc = X86::MOV8rm;
202 RC = X86::GR8RegisterClass;
203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 Opc = X86::MOV16rm;
206 RC = X86::GR16RegisterClass;
207 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 Opc = X86::MOV32rm;
210 RC = X86::GR32RegisterClass;
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 // Must be in x86-64 mode.
214 Opc = X86::MOV64rm;
215 RC = X86::GR64RegisterClass;
216 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 if (Subtarget->hasSSE1()) {
219 Opc = X86::MOVSSrm;
220 RC = X86::FR32RegisterClass;
221 } else {
222 Opc = X86::LD_Fp32m;
223 RC = X86::RFP32RegisterClass;
224 }
225 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000227 if (Subtarget->hasSSE2()) {
228 Opc = X86::MOVSDrm;
229 RC = X86::FR64RegisterClass;
230 } else {
231 Opc = X86::LD_Fp64m;
232 RC = X86::RFP64RegisterClass;
233 }
234 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000236 // No f80 support yet.
237 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000238 }
239
240 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000241 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 return true;
243}
244
Evan Chengf3d4efe2008-09-07 09:09:33 +0000245/// X86FastEmitStore - Emit a machine instruction to store a value Val of
246/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
247/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000248/// i.e. V. Return true if it is possible.
249bool
Owen Andersone50ed302009-08-10 22:56:29 +0000250X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000251 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000252 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 switch (VT.getSimpleVT().SimpleTy) {
255 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000257 case MVT::i1: {
258 // Mask out all but lowest bit.
259 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
260 BuildMI(MBB, DL,
261 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
262 Val = AndResult;
263 }
264 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 case MVT::i8: Opc = X86::MOV8mr; break;
266 case MVT::i16: Opc = X86::MOV16mr; break;
267 case MVT::i32: Opc = X86::MOV32mr; break;
268 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
269 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000270 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000271 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000273 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 }
Chris Lattner438949a2008-10-15 05:30:52 +0000276
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000277 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 return true;
279}
280
Owen Andersone50ed302009-08-10 22:56:29 +0000281bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000282 const X86AddressMode &AM) {
283 // Handle 'null' like i32/i64 0.
284 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000286
287 // If this is a store of a simple constant, fold the constant into the store.
288 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
289 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000290 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000292 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000293 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 case MVT::i8: Opc = X86::MOV8mi; break;
295 case MVT::i16: Opc = X86::MOV16mi; break;
296 case MVT::i32: Opc = X86::MOV32mi; break;
297 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000298 // Must be a 32-bit sign extended value.
299 if ((int)CI->getSExtValue() == CI->getSExtValue())
300 Opc = X86::MOV64mi32;
301 break;
302 }
303
304 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000305 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000306 .addImm(Signed ? CI->getSExtValue() :
307 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000308 return true;
309 }
310 }
311
312 unsigned ValReg = getRegForValue(Val);
313 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000314 return false;
315
316 return X86FastEmitStore(VT, ValReg, AM);
317}
318
Evan Cheng24e3a902008-09-08 06:35:17 +0000319/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
320/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
321/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000322bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
323 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000324 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000325 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
326
327 if (RR != 0) {
328 ResultReg = RR;
329 return true;
330 } else
331 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000332}
333
Dan Gohman0586d912008-09-10 20:11:02 +0000334/// X86SelectAddress - Attempt to fill in an address from the given value.
335///
Chris Lattner0aa43de2009-07-10 05:33:42 +0000336bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Duncan Sands12513882009-06-03 12:05:18 +0000337 User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000338 unsigned Opcode = Instruction::UserOp1;
339 if (Instruction *I = dyn_cast<Instruction>(V)) {
340 Opcode = I->getOpcode();
341 U = I;
342 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
343 Opcode = C->getOpcode();
344 U = C;
345 }
Dan Gohman0586d912008-09-10 20:11:02 +0000346
Dan Gohman35893082008-09-18 23:23:44 +0000347 switch (Opcode) {
348 default: break;
349 case Instruction::BitCast:
350 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000351 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000352
353 case Instruction::IntToPtr:
354 // Look past no-op inttoptrs.
355 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000356 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000357 break;
Dan Gohman35893082008-09-18 23:23:44 +0000358
359 case Instruction::PtrToInt:
360 // Look past no-op ptrtoints.
361 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000362 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000363 break;
Dan Gohman35893082008-09-18 23:23:44 +0000364
365 case Instruction::Alloca: {
366 // Do static allocas.
367 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000368 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000369 if (SI != StaticAllocaMap.end()) {
370 AM.BaseType = X86AddressMode::FrameIndexBase;
371 AM.Base.FrameIndex = SI->second;
372 return true;
373 }
374 break;
Dan Gohman35893082008-09-18 23:23:44 +0000375 }
376
377 case Instruction::Add: {
378 // Adds of constants are common and easy enough.
379 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000380 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
381 // They have to fit in the 32-bit signed displacement field though.
382 if (isInt32(Disp)) {
383 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000384 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000385 }
Dan Gohman0586d912008-09-10 20:11:02 +0000386 }
Dan Gohman35893082008-09-18 23:23:44 +0000387 break;
388 }
389
390 case Instruction::GetElementPtr: {
391 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000392 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000393 unsigned IndexReg = AM.IndexReg;
394 unsigned Scale = AM.Scale;
395 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000396 // Iterate through the indices, folding what we can. Constants can be
397 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000398 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
399 i != e; ++i, ++GTI) {
400 Value *Op = *i;
401 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
402 const StructLayout *SL = TD.getStructLayout(STy);
403 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
404 Disp += SL->getElementOffset(Idx);
405 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000406 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman35893082008-09-18 23:23:44 +0000407 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
408 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000409 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000410 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000411 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000412 (S == 1 || S == 2 || S == 4 || S == 8)) {
413 // Scaled-index addressing.
414 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000415 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000416 if (IndexReg == 0)
417 return false;
418 } else
419 // Unsupported.
420 goto unsupported_gep;
421 }
422 }
Dan Gohman09aae462008-09-26 20:04:15 +0000423 // Check for displacement overflow.
424 if (!isInt32(Disp))
425 break;
Dan Gohman35893082008-09-18 23:23:44 +0000426 // Ok, the GEP indices were covered by constant-offset and scaled-index
427 // addressing. Update the address state and move on to examining the base.
428 AM.IndexReg = IndexReg;
429 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000430 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000431 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000432 unsupported_gep:
433 // Ok, the GEP indices weren't all covered.
434 break;
435 }
436 }
437
438 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000439 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000440 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000441 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000442 return false;
443
Dan Gohman97135e12008-09-26 19:15:30 +0000444 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000445 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000446 (AM.Base.Reg != 0 || AM.IndexReg != 0))
447 return false;
448
Dan Gohmane9865942009-02-23 22:03:08 +0000449 // Can't handle TLS yet.
450 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
451 if (GVar->isThreadLocal())
452 return false;
453
Chris Lattnerff7727f2009-07-09 06:41:35 +0000454 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000455 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000456
Chris Lattner0d786dd2009-07-10 07:48:51 +0000457 // Allow the subtarget to classify the global.
458 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
459
460 // If this reference is relative to the pic base, set it now.
461 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000462 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000463 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000464 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000465
466 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000467 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000468 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000469 if (Subtarget->isPICStyleRIPRel()) {
470 // Use rip-relative addressing if we can. Above we verified that the
471 // base and index registers are unused.
472 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
473 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000474 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000475 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000476 return true;
477 }
478
Chris Lattner0d786dd2009-07-10 07:48:51 +0000479 // Ok, we need to do a load from a stub. If we've already loaded from this
480 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000481 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
482 unsigned LoadReg;
483 if (I != LocalValueMap.end() && I->second != 0) {
484 LoadReg = I->second;
485 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000486 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000487 unsigned Opc = 0;
488 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000489 X86AddressMode StubAM;
490 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000491 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000492 StubAM.GVOpFlags = GVFlags;
493
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000495 Opc = X86::MOV64rm;
496 RC = X86::GR64RegisterClass;
497
Chris Lattner0d786dd2009-07-10 07:48:51 +0000498 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000499 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000500 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000501 Opc = X86::MOV32rm;
502 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000503 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000504
505 LoadReg = createResultReg(RC);
506 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
507
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000508 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000509 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000510 }
Chris Lattner18c59872009-06-27 04:16:01 +0000511
Chris Lattnerff7727f2009-07-09 06:41:35 +0000512 // Now construct the final address. Note that the Disp, Scale,
513 // and Index values may already be set here.
514 AM.Base.Reg = LoadReg;
515 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000516 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000517 }
518
Dan Gohman97135e12008-09-26 19:15:30 +0000519 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000520 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000521 if (AM.Base.Reg == 0) {
522 AM.Base.Reg = getRegForValue(V);
523 return AM.Base.Reg != 0;
524 }
525 if (AM.IndexReg == 0) {
526 assert(AM.Scale == 1 && "Scale with no index!");
527 AM.IndexReg = getRegForValue(V);
528 return AM.IndexReg != 0;
529 }
530 }
531
532 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000533}
534
Chris Lattner0aa43de2009-07-10 05:33:42 +0000535/// X86SelectCallAddress - Attempt to fill in an address from the given value.
536///
537bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
538 User *U = NULL;
539 unsigned Opcode = Instruction::UserOp1;
540 if (Instruction *I = dyn_cast<Instruction>(V)) {
541 Opcode = I->getOpcode();
542 U = I;
543 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
544 Opcode = C->getOpcode();
545 U = C;
546 }
547
548 switch (Opcode) {
549 default: break;
550 case Instruction::BitCast:
551 // Look past bitcasts.
552 return X86SelectCallAddress(U->getOperand(0), AM);
553
554 case Instruction::IntToPtr:
555 // Look past no-op inttoptrs.
556 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
557 return X86SelectCallAddress(U->getOperand(0), AM);
558 break;
559
560 case Instruction::PtrToInt:
561 // Look past no-op ptrtoints.
562 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
563 return X86SelectCallAddress(U->getOperand(0), AM);
564 break;
565 }
566
567 // Handle constant address.
568 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
569 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000570 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000571 return false;
572
573 // RIP-relative addresses can't have additional register operands.
574 if (Subtarget->isPICStyleRIPRel() &&
575 (AM.Base.Reg != 0 || AM.IndexReg != 0))
576 return false;
577
Chris Lattner754b7652009-07-10 05:48:03 +0000578 // Can't handle TLS or DLLImport.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000579 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000580 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000581 return false;
582
583 // Okay, we've committed to selecting this global. Set up the basic address.
584 AM.GV = GV;
585
Chris Lattnere6c07b52009-07-10 05:45:15 +0000586 // No ABI requires an extra load for anything other than DLLImport, which
587 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000588 if (Subtarget->isPICStyleRIPRel()) {
589 // Use rip-relative addressing if we can. Above we verified that the
590 // base and index registers are unused.
591 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
592 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000593 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000594 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
595 } else if (Subtarget->isPICStyleGOT()) {
596 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000597 }
598
Chris Lattner0aa43de2009-07-10 05:33:42 +0000599 return true;
600 }
601
602 // If all else fails, try to materialize the value in a register.
603 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
604 if (AM.Base.Reg == 0) {
605 AM.Base.Reg = getRegForValue(V);
606 return AM.Base.Reg != 0;
607 }
608 if (AM.IndexReg == 0) {
609 assert(AM.Scale == 1 && "Scale with no index!");
610 AM.IndexReg = getRegForValue(V);
611 return AM.IndexReg != 0;
612 }
613 }
614
615 return false;
616}
617
618
Owen Andersona3971df2008-09-04 07:08:58 +0000619/// X86SelectStore - Select and emit code to implement store instructions.
620bool X86FastISel::X86SelectStore(Instruction* I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000621 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000622 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000623 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000624
Dan Gohman0586d912008-09-10 20:11:02 +0000625 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000626 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000627 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000628
Chris Lattner438949a2008-10-15 05:30:52 +0000629 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000630}
631
Evan Cheng8b19e562008-09-03 06:44:39 +0000632/// X86SelectLoad - Select and emit code to implement load instructions.
633///
Dan Gohman3df24e62008-09-03 23:12:08 +0000634bool X86FastISel::X86SelectLoad(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000635 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000636 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000637 return false;
638
Dan Gohman0586d912008-09-10 20:11:02 +0000639 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000640 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000641 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000642
Evan Cheng0de588f2008-09-05 21:00:03 +0000643 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000644 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000645 UpdateValueMap(I, ResultReg);
646 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000647 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000648 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000649}
650
Owen Andersone50ed302009-08-10 22:56:29 +0000651static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000653 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 case MVT::i8: return X86::CMP8rr;
655 case MVT::i16: return X86::CMP16rr;
656 case MVT::i32: return X86::CMP32rr;
657 case MVT::i64: return X86::CMP64rr;
658 case MVT::f32: return X86::UCOMISSrr;
659 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000660 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000661}
662
Chris Lattner0e13c782008-10-15 04:13:29 +0000663/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
664/// of the comparison, return an opcode that works for the compare (e.g.
665/// CMP32ri) otherwise return 0.
Owen Andersone50ed302009-08-10 22:56:29 +0000666static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000668 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000669 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 case MVT::i8: return X86::CMP8ri;
671 case MVT::i16: return X86::CMP16ri;
672 case MVT::i32: return X86::CMP32ri;
673 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000674 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
675 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000676 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000677 return X86::CMP64ri32;
678 return 0;
679 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000680}
681
Owen Andersone50ed302009-08-10 22:56:29 +0000682bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000683 unsigned Op0Reg = getRegForValue(Op0);
684 if (Op0Reg == 0) return false;
685
Chris Lattnerd53886b2008-10-15 05:18:04 +0000686 // Handle 'null' like i32/i64 0.
687 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000688 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000689
Chris Lattner9a08a612008-10-15 04:26:38 +0000690 // We have two options: compare with register or immediate. If the RHS of
691 // the compare is an immediate that we can fold into this compare, use
692 // CMPri, otherwise use CMPrr.
693 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000694 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000695 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000696 .addImm(Op1C->getSExtValue());
697 return true;
698 }
699 }
700
701 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
702 if (CompareOpc == 0) return false;
703
704 unsigned Op1Reg = getRegForValue(Op1);
705 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000706 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000707
708 return true;
709}
710
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000711bool X86FastISel::X86SelectCmp(Instruction *I) {
712 CmpInst *CI = cast<CmpInst>(I);
713
Owen Andersone50ed302009-08-10 22:56:29 +0000714 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000715 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000716 return false;
717
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000718 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000719 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000720 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000721 switch (CI->getPredicate()) {
722 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000723 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
724 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000725
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000726 unsigned EReg = createResultReg(&X86::GR8RegClass);
727 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000728 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
729 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
730 BuildMI(MBB, DL,
731 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000732 UpdateValueMap(I, ResultReg);
733 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000734 }
735 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000736 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
737 return false;
738
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000739 unsigned NEReg = createResultReg(&X86::GR8RegClass);
740 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000741 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
742 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
743 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000744 UpdateValueMap(I, ResultReg);
745 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000746 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000747 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
748 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
749 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
750 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
751 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
752 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
753 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
754 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
755 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
756 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
757 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
758 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
759
760 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
761 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
762 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
763 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
764 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
765 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
766 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
767 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
768 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
769 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000770 default:
771 return false;
772 }
773
Chris Lattner9a08a612008-10-15 04:26:38 +0000774 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000775 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000776 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000777
Chris Lattner9a08a612008-10-15 04:26:38 +0000778 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000779 if (!X86FastEmitCompare(Op0, Op1, VT))
780 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000781
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000782 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000783 UpdateValueMap(I, ResultReg);
784 return true;
785}
Evan Cheng8b19e562008-09-03 06:44:39 +0000786
Dan Gohmand89ae992008-09-05 01:06:14 +0000787bool X86FastISel::X86SelectZExt(Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000788 // Handle zero-extension from i1 to i8, which is common.
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000789 if (I->getType()->isInteger(8) &&
790 I->getOperand(0)->getType()->isInteger(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000791 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000792 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000793 // Set the high bits to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000795 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000796 UpdateValueMap(I, ResultReg);
797 return true;
798 }
799
800 return false;
801}
802
Chris Lattner9a08a612008-10-15 04:26:38 +0000803
Dan Gohmand89ae992008-09-05 01:06:14 +0000804bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000805 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000806 // Handle a conditional branch.
807 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000808 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
809 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
810
Dan Gohmand98d6202008-10-02 22:15:21 +0000811 // Fold the common case of a conditional branch with a comparison.
812 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
813 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000814 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000815
Dan Gohmand98d6202008-10-02 22:15:21 +0000816 // Try to take advantage of fallthrough opportunities.
817 CmpInst::Predicate Predicate = CI->getPredicate();
818 if (MBB->isLayoutSuccessor(TrueMBB)) {
819 std::swap(TrueMBB, FalseMBB);
820 Predicate = CmpInst::getInversePredicate(Predicate);
821 }
822
Chris Lattner871d2462008-10-15 03:58:05 +0000823 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
824 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
825
Dan Gohmand98d6202008-10-02 22:15:21 +0000826 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000827 case CmpInst::FCMP_OEQ:
828 std::swap(TrueMBB, FalseMBB);
829 Predicate = CmpInst::FCMP_UNE;
830 // FALL THROUGH
831 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner871d2462008-10-15 03:58:05 +0000832 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
833 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
834 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
835 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
836 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
837 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
838 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
839 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
840 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
841 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
842 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
843 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000844
Chris Lattner871d2462008-10-15 03:58:05 +0000845 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
846 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
847 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
848 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
849 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
850 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
851 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
852 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
853 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
854 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000855 default:
856 return false;
857 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000858
Chris Lattner709d8292008-10-15 04:02:26 +0000859 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
860 if (SwapArgs)
861 std::swap(Op0, Op1);
862
Chris Lattner9a08a612008-10-15 04:26:38 +0000863 // Emit a compare of the LHS and RHS, setting the flags.
864 if (!X86FastEmitCompare(Op0, Op1, VT))
865 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000866
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000867 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000868
869 if (Predicate == CmpInst::FCMP_UNE) {
870 // X86 requires a second branch to handle UNE (and OEQ,
871 // which is mapped to UNE above).
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000872 BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000873 }
874
Dan Gohmand98d6202008-10-02 22:15:21 +0000875 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000876 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000877 return true;
878 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000879 } else if (ExtractValueInst *EI =
880 dyn_cast<ExtractValueInst>(BI->getCondition())) {
881 // Check to see if the branch instruction is from an "arithmetic with
882 // overflow" intrinsic. The main way these intrinsics are used is:
883 //
884 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
885 // %sum = extractvalue { i32, i1 } %t, 0
886 // %obit = extractvalue { i32, i1 } %t, 1
887 // br i1 %obit, label %overflow, label %normal
888 //
Dan Gohman653456c2009-01-07 00:15:08 +0000889 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000890 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000891 // looking for the SETO/SETB instruction. If an instruction modifies the
892 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
893 // convert the branch into a JO/JB instruction.
Chris Lattnera9a42252009-04-12 07:36:01 +0000894 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
895 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
896 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
897 const MachineInstr *SetMI = 0;
898 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000899
Chris Lattnera9a42252009-04-12 07:36:01 +0000900 for (MachineBasicBlock::const_reverse_iterator
901 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
902 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000903
Chris Lattnera9a42252009-04-12 07:36:01 +0000904 if (MI.modifiesRegister(Reg)) {
905 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000906
Chris Lattnera9a42252009-04-12 07:36:01 +0000907 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
908 Reg = Src;
909 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000910 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000911
Chris Lattnera9a42252009-04-12 07:36:01 +0000912 SetMI = &MI;
913 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000914 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000915
Chris Lattnera9a42252009-04-12 07:36:01 +0000916 const TargetInstrDesc &TID = MI.getDesc();
917 if (TID.hasUnmodeledSideEffects() ||
918 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
919 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000920 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000921
922 if (SetMI) {
923 unsigned OpCode = SetMI->getOpcode();
924
925 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattner8d57b772009-04-12 07:51:14 +0000926 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ? X86::JO : X86::JB))
927 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000928 FastEmitBranch(FalseMBB);
929 MBB->addSuccessor(TrueMBB);
930 return true;
931 }
Bill Wendling9a901322008-12-10 19:44:24 +0000932 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000933 }
934 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000935 }
936
937 // Otherwise do a clumsy setcc and re-test it.
938 unsigned OpReg = getRegForValue(BI->getCondition());
939 if (OpReg == 0) return false;
940
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000941 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
942 BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000943 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000944 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000945 return true;
946}
947
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000948bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000949 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000950 const TargetRegisterClass *RC = NULL;
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000951 if (I->getType()->isInteger(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000952 CReg = X86::CL;
953 RC = &X86::GR8RegClass;
954 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000955 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
956 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
957 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000958 default: return false;
959 }
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000960 } else if (I->getType()->isInteger(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000961 CReg = X86::CX;
962 RC = &X86::GR16RegClass;
963 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000964 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
965 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
966 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000967 default: return false;
968 }
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000969 } else if (I->getType()->isInteger(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000970 CReg = X86::ECX;
971 RC = &X86::GR32RegClass;
972 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000973 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
974 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
975 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000976 default: return false;
977 }
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000978 } else if (I->getType()->isInteger(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000979 CReg = X86::RCX;
980 RC = &X86::GR64RegClass;
981 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000982 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
983 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
984 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000985 default: return false;
986 }
987 } else {
988 return false;
989 }
990
Owen Andersone50ed302009-08-10 22:56:29 +0000991 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000993 return false;
994
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000995 unsigned Op0Reg = getRegForValue(I->getOperand(0));
996 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000997
998 // Fold immediate in shl(x,3).
999 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
1000 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001001 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001002 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001003 UpdateValueMap(I, ResultReg);
1004 return true;
1005 }
1006
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001007 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1008 if (Op1Reg == 0) return false;
1009 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +00001010
1011 // The shift instruction uses X86::CL. If we defined a super-register
1012 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1013 // we're doing here.
1014 if (CReg != X86::CL)
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001015 BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001016 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1017
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001018 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001019 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001020 UpdateValueMap(I, ResultReg);
1021 return true;
1022}
1023
1024bool X86FastISel::X86SelectSelect(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001025 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001027 return false;
1028
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001029 unsigned Opc = 0;
1030 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001031 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001032 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001033 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001035 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001036 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001037 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001038 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001039 RC = &X86::GR64RegClass;
1040 } else {
1041 return false;
1042 }
1043
1044 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1045 if (Op0Reg == 0) return false;
1046 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1047 if (Op1Reg == 0) return false;
1048 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1049 if (Op2Reg == 0) return false;
1050
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001051 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001052 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001053 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001054 UpdateValueMap(I, ResultReg);
1055 return true;
1056}
1057
Dan Gohman78efce62008-09-10 21:02:08 +00001058bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001059 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001060 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001061 I->getType()->isDoubleTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001062 Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001063 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001064 unsigned OpReg = getRegForValue(V);
1065 if (OpReg == 0) return false;
1066 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001067 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001068 UpdateValueMap(I, ResultReg);
1069 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001070 }
1071 }
1072
1073 return false;
1074}
1075
1076bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
1077 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001078 if (I->getType()->isFloatTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001079 Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001080 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001081 unsigned OpReg = getRegForValue(V);
1082 if (OpReg == 0) return false;
1083 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001084 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001085 UpdateValueMap(I, ResultReg);
1086 return true;
1087 }
1088 }
1089 }
1090
1091 return false;
1092}
1093
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001094bool X86FastISel::X86SelectTrunc(Instruction *I) {
1095 if (Subtarget->is64Bit())
1096 // All other cases should be handled by the tblgen generated code.
1097 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001098 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1099 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001100
1101 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001103 // All other cases should be handled by the tblgen generated code.
1104 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001106 // All other cases should be handled by the tblgen generated code.
1107 return false;
1108
1109 unsigned InputReg = getRegForValue(I->getOperand(0));
1110 if (!InputReg)
1111 // Unhandled operand. Halt "fast" selection and bail.
1112 return false;
1113
Dan Gohman62417622009-04-27 16:33:14 +00001114 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1116 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001117 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001118 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001119 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001120
1121 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Evan Cheng536ab132009-01-22 09:10:11 +00001123 CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001124 if (!ResultReg)
1125 return false;
1126
1127 UpdateValueMap(I, ResultReg);
1128 return true;
1129}
1130
Bill Wendling52370a12008-12-09 02:42:50 +00001131bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1132 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1133 Value *Agg = EI->getAggregateOperand();
1134
Chris Lattnera9a42252009-04-12 07:36:01 +00001135 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
1136 switch (CI->getIntrinsicID()) {
1137 default: break;
1138 case Intrinsic::sadd_with_overflow:
1139 case Intrinsic::uadd_with_overflow:
1140 // Cheat a little. We know that the registers for "add" and "seto" are
1141 // allocated sequentially. However, we only keep track of the register
1142 // for "add" in the value map. Use extractvalue's index to get the
1143 // correct register for "seto".
1144 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1145 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001146 }
1147 }
1148
1149 return false;
1150}
1151
Chris Lattnera9a42252009-04-12 07:36:01 +00001152bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001153 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001154 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001155 default: return false;
1156 case Intrinsic::sadd_with_overflow:
1157 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001158 // Replace "add with overflow" intrinsics with an "add" instruction followed
1159 // by a seto/setc instruction. Later on, when the "extractvalue"
1160 // instructions are encountered, we use the fact that two registers were
1161 // created sequentially to get the correct registers for the "sum" and the
1162 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001163 const Function *Callee = I.getCalledFunction();
1164 const Type *RetTy =
1165 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1166
Owen Andersone50ed302009-08-10 22:56:29 +00001167 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001168 if (!isTypeLegal(RetTy, VT))
1169 return false;
1170
1171 Value *Op1 = I.getOperand(1);
1172 Value *Op2 = I.getOperand(2);
1173 unsigned Reg1 = getRegForValue(Op1);
1174 unsigned Reg2 = getRegForValue(Op2);
1175
1176 if (Reg1 == 0 || Reg2 == 0)
1177 // FIXME: Handle values *not* in registers.
1178 return false;
1179
1180 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001182 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001183 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001184 OpC = X86::ADD64rr;
1185 else
1186 return false;
1187
1188 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001189 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001190 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001191
Chris Lattner8d57b772009-04-12 07:51:14 +00001192 // If the add with overflow is an intra-block value then we just want to
1193 // create temporaries for it like normal. If it is a cross-block value then
1194 // UpdateValueMap will return the cross-block register used. Since we
1195 // *really* want the value to be live in the register pair known by
1196 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1197 // the cross block case. In the non-cross-block case, we should just make
1198 // another register for the value.
1199 if (DestReg1 != ResultReg)
1200 ResultReg = DestReg1+1;
1201 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001203
Chris Lattnera9a42252009-04-12 07:36:01 +00001204 unsigned Opc = X86::SETBr;
1205 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1206 Opc = X86::SETOr;
1207 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001208 return true;
1209 }
1210 }
1211}
1212
Evan Chengf3d4efe2008-09-07 09:09:33 +00001213bool X86FastISel::X86SelectCall(Instruction *I) {
1214 CallInst *CI = cast<CallInst>(I);
1215 Value *Callee = I->getOperand(0);
1216
1217 // Can't handle inline asm yet.
1218 if (isa<InlineAsm>(Callee))
1219 return false;
1220
Bill Wendling52370a12008-12-09 02:42:50 +00001221 // Handle intrinsic calls.
Chris Lattnera9a42252009-04-12 07:36:01 +00001222 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1223 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001224
Evan Chengf3d4efe2008-09-07 09:09:33 +00001225 // Handle only C and fastcc calling conventions for now.
1226 CallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001227 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001228 if (CC != CallingConv::C &&
1229 CC != CallingConv::Fast &&
1230 CC != CallingConv::X86_FastCall)
1231 return false;
1232
Dan Gohman7d04e4a2009-05-04 19:50:33 +00001233 // On X86, -tailcallopt changes the fastcc ABI. FastISel doesn't
1234 // handle this for now.
1235 if (CC == CallingConv::Fast && PerformTailCallOpt)
1236 return false;
1237
Evan Chengf3d4efe2008-09-07 09:09:33 +00001238 // Let SDISel handle vararg functions.
1239 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1240 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1241 if (FTy->isVarArg())
1242 return false;
1243
1244 // Handle *simple* calls for now.
1245 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001246 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001247 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001249 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001250 return false;
1251
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001252 // Materialize callee address in a register. FIXME: GV address can be
1253 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001254 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001255 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001256 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001257 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001258 GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001259 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001260 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001261 } else if (CalleeAM.Base.Reg != 0) {
1262 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001263 } else
1264 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001265
Evan Chengdebdea02008-09-08 17:15:42 +00001266 // Allow calls which produce i1 results.
1267 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 if (RetVT == MVT::i1) {
1269 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001270 AndToI1 = true;
1271 }
1272
Evan Chengf3d4efe2008-09-07 09:09:33 +00001273 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001274 SmallVector<Value*, 8> ArgVals;
1275 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001276 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001277 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001278 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001279 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001280 ArgVTs.reserve(CS.arg_size());
1281 ArgFlags.reserve(CS.arg_size());
1282 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1283 i != e; ++i) {
1284 unsigned Arg = getRegForValue(*i);
1285 if (Arg == 0)
1286 return false;
1287 ISD::ArgFlagsTy Flags;
1288 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001289 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001290 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001291 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001292 Flags.setZExt();
1293
1294 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001295 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1296 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1297 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1298 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001299 return false;
1300
1301 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001302 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001303 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001304 return false;
1305 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1306 Flags.setOrigAlign(OriginalAlignment);
1307
1308 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001309 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001310 ArgVTs.push_back(ArgVT);
1311 ArgFlags.push_back(Flags);
1312 }
1313
1314 // Analyze operands of the call, assigning locations to each operand.
1315 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001316 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001317 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1318
1319 // Get a count of how many bytes are to be pushed on the stack.
1320 unsigned NumBytes = CCInfo.getNextStackOffset();
1321
1322 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001323 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001324 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001325
Chris Lattner438949a2008-10-15 05:30:52 +00001326 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001327 // copies / loads.
1328 SmallVector<unsigned, 4> RegArgs;
1329 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1330 CCValAssign &VA = ArgLocs[i];
1331 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001332 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001333
1334 // Promote the value if needed.
1335 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001336 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001337 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001338 case CCValAssign::SExt: {
1339 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1340 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001341 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001342 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001343 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001344 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001345 }
1346 case CCValAssign::ZExt: {
1347 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1348 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001349 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001350 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001351 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001352 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001353 }
1354 case CCValAssign::AExt: {
1355 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1356 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001357 if (!Emitted)
1358 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001359 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001360 if (!Emitted)
1361 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1362 Arg, ArgVT, Arg);
1363
Chris Lattnera33649e2008-12-19 17:03:38 +00001364 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001365 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001366 break;
1367 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001368 case CCValAssign::BCvt: {
1369 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
1370 ISD::BIT_CONVERT, Arg);
1371 assert(BC != 0 && "Failed to emit a bitcast!");
1372 Arg = BC;
1373 ArgVT = VA.getLocVT();
1374 break;
1375 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001376 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001377
1378 if (VA.isRegLoc()) {
1379 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1380 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1381 Arg, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001382 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001383 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001384 RegArgs.push_back(VA.getLocReg());
1385 } else {
1386 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001387 X86AddressMode AM;
1388 AM.Base.Reg = StackPtr;
1389 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001390 Value *ArgVal = ArgVals[VA.getValNo()];
1391
1392 // If this is a really simple value, emit this with the Value* version of
1393 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1394 // can cause us to reevaluate the argument.
1395 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1396 X86FastEmitStore(ArgVT, ArgVal, AM);
1397 else
1398 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001399 }
1400 }
1401
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001402 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1403 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001404 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001405 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001406 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001407 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001408 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001409 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001410 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001411
Evan Chengf3d4efe2008-09-07 09:09:33 +00001412 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001413 MachineInstrBuilder MIB;
1414 if (CalleeOp) {
1415 // Register-indirect call.
1416 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1417 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1418
1419 } else {
1420 // Direct call.
1421 assert(GV && "Not a direct call");
1422 unsigned CallOpc =
1423 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1424
1425 // See if we need any target-specific flags on the GV operand.
1426 unsigned char OpFlags = 0;
1427
1428 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1429 // external symbols most go through the PLT in PIC mode. If the symbol
1430 // has hidden or protected visibility, or if it is static or local, then
1431 // we don't need to use the PLT - we can directly call it.
1432 if (Subtarget->isTargetELF() &&
1433 TM.getRelocationModel() == Reloc::PIC_ &&
1434 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1435 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001436 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001437 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1438 Subtarget->getDarwinVers() < 9) {
1439 // PC-relative references to external symbols should go through $stub,
1440 // unless we're building with the leopard linker or later, which
1441 // automatically synthesizes these stubs.
1442 OpFlags = X86II::MO_DARWIN_STUB;
1443 }
1444
1445
1446 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1447 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001448
1449 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001450 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001451 MIB.addReg(X86::EBX);
1452
Evan Chengf3d4efe2008-09-07 09:09:33 +00001453 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001454 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1455 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001456
1457 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001458 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001459 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001460
1461 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001463 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001464 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001465 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1466
1467 // Copy all of the result registers out of their specified physreg.
1468 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001470 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1471 TargetRegisterClass *SrcRC = DstRC;
1472
1473 // If this is a call to a function that returns an fp value on the x87 fp
1474 // stack, but where we prefer to use the value in xmm registers, copy it
1475 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1476 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1477 RVLocs[0].getLocReg() == X86::ST1) &&
1478 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001479 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001480 SrcRC = X86::RSTRegisterClass;
1481 DstRC = X86::RFP80RegisterClass;
1482 }
1483
1484 unsigned ResultReg = createResultReg(DstRC);
1485 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1486 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001487 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001488 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001489 if (CopyVT != RVLocs[0].getValVT()) {
1490 // Round the F80 the right size, which also moves to the appropriate xmm
1491 // register. This is accomplished by storing the F80 value in memory and
1492 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001493 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001494 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001495 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001496 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001497 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001499 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001501 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001502 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001503 }
1504
Evan Chengdebdea02008-09-08 17:15:42 +00001505 if (AndToI1) {
1506 // Mask out all but lowest bit for some call which produces an i1.
1507 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001508 BuildMI(MBB, DL,
1509 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001510 ResultReg = AndResult;
1511 }
1512
Evan Chengf3d4efe2008-09-07 09:09:33 +00001513 UpdateValueMap(I, ResultReg);
1514 }
1515
1516 return true;
1517}
1518
1519
Dan Gohman99b21822008-08-28 23:21:34 +00001520bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001521X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001522 switch (I->getOpcode()) {
1523 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001524 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001525 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001526 case Instruction::Store:
1527 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001528 case Instruction::ICmp:
1529 case Instruction::FCmp:
1530 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001531 case Instruction::ZExt:
1532 return X86SelectZExt(I);
1533 case Instruction::Br:
1534 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001535 case Instruction::Call:
1536 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001537 case Instruction::LShr:
1538 case Instruction::AShr:
1539 case Instruction::Shl:
1540 return X86SelectShift(I);
1541 case Instruction::Select:
1542 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001543 case Instruction::Trunc:
1544 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001545 case Instruction::FPExt:
1546 return X86SelectFPExt(I);
1547 case Instruction::FPTrunc:
1548 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001549 case Instruction::ExtractValue:
1550 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001551 case Instruction::IntToPtr: // Deliberate fall-through.
1552 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001553 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1554 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001555 if (DstVT.bitsGT(SrcVT))
1556 return X86SelectZExt(I);
1557 if (DstVT.bitsLT(SrcVT))
1558 return X86SelectTrunc(I);
1559 unsigned Reg = getRegForValue(I->getOperand(0));
1560 if (Reg == 0) return false;
1561 UpdateValueMap(I, Reg);
1562 return true;
1563 }
Dan Gohman99b21822008-08-28 23:21:34 +00001564 }
1565
1566 return false;
1567}
1568
Dan Gohman0586d912008-09-10 20:11:02 +00001569unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001570 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001571 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001572 return false;
1573
1574 // Get opcode and regclass of the output for the given load instruction.
1575 unsigned Opc = 0;
1576 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001578 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001580 Opc = X86::MOV8rm;
1581 RC = X86::GR8RegisterClass;
1582 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001584 Opc = X86::MOV16rm;
1585 RC = X86::GR16RegisterClass;
1586 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001587 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001588 Opc = X86::MOV32rm;
1589 RC = X86::GR32RegisterClass;
1590 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001591 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001592 // Must be in x86-64 mode.
1593 Opc = X86::MOV64rm;
1594 RC = X86::GR64RegisterClass;
1595 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001597 if (Subtarget->hasSSE1()) {
1598 Opc = X86::MOVSSrm;
1599 RC = X86::FR32RegisterClass;
1600 } else {
1601 Opc = X86::LD_Fp32m;
1602 RC = X86::RFP32RegisterClass;
1603 }
1604 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001605 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001606 if (Subtarget->hasSSE2()) {
1607 Opc = X86::MOVSDrm;
1608 RC = X86::FR64RegisterClass;
1609 } else {
1610 Opc = X86::LD_Fp64m;
1611 RC = X86::RFP64RegisterClass;
1612 }
1613 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001615 // No f80 support yet.
1616 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001617 }
1618
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001619 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001620 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001621 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001622 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001624 Opc = X86::LEA32r;
1625 else
1626 Opc = X86::LEA64r;
1627 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001628 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001629 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001630 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001631 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001632 }
1633
Owen Anderson3b217c62008-09-06 01:11:01 +00001634 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001635 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001636 if (Align == 0) {
1637 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001638 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001639 }
Owen Anderson95267a12008-09-05 00:06:23 +00001640
Dan Gohman5396c992008-09-30 01:21:32 +00001641 // x86-32 PIC requires a PIC base register for constant pools.
1642 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001643 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001644 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001645 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1646 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1647 } else if (Subtarget->isPICStyleGOT()) {
1648 OpFlag = X86II::MO_GOTOFF;
1649 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1650 } else if (Subtarget->isPICStyleRIPRel() &&
1651 TM.getCodeModel() == CodeModel::Small) {
1652 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001653 }
Dan Gohman5396c992008-09-30 01:21:32 +00001654
1655 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001656 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001657 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001658 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1659 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001660
Owen Anderson95267a12008-09-05 00:06:23 +00001661 return ResultReg;
1662}
1663
Dan Gohman0586d912008-09-10 20:11:02 +00001664unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001665 // Fail on dynamic allocas. At this point, getRegForValue has already
1666 // checked its CSE maps, so if we're here trying to handle a dynamic
1667 // alloca, we're not going to succeed. X86SelectAddress has a
1668 // check for dynamic allocas, because it's called directly from
1669 // various places, but TargetMaterializeAlloca also needs a check
1670 // in order to avoid recursion between getRegForValue,
1671 // X86SelectAddrss, and TargetMaterializeAlloca.
1672 if (!StaticAllocaMap.count(C))
1673 return 0;
1674
Dan Gohman0586d912008-09-10 20:11:02 +00001675 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001676 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001677 return 0;
1678 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1679 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1680 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001681 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001682 return ResultReg;
1683}
1684
Evan Chengc3f44b02008-09-03 00:03:49 +00001685namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001686 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001687 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +00001688 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001689 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001690 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001691 DenseMap<const AllocaInst *, int> &am
1692#ifndef NDEBUG
1693 , SmallSet<Instruction*, 8> &cil
1694#endif
1695 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001696 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001697#ifndef NDEBUG
1698 , cil
1699#endif
1700 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001701 }
Dan Gohman99b21822008-08-28 23:21:34 +00001702}