blob: 5e9a39f0562672256ee83f2901fc2e5f75d8483a [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;
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 Opc = X86::MOV8rm;
200 RC = X86::GR8RegisterClass;
201 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 Opc = X86::MOV16rm;
204 RC = X86::GR16RegisterClass;
205 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 Opc = X86::MOV32rm;
208 RC = X86::GR32RegisterClass;
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 // Must be in x86-64 mode.
212 Opc = X86::MOV64rm;
213 RC = X86::GR64RegisterClass;
214 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 if (Subtarget->hasSSE1()) {
217 Opc = X86::MOVSSrm;
218 RC = X86::FR32RegisterClass;
219 } else {
220 Opc = X86::LD_Fp32m;
221 RC = X86::RFP32RegisterClass;
222 }
223 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000225 if (Subtarget->hasSSE2()) {
226 Opc = X86::MOVSDrm;
227 RC = X86::FR64RegisterClass;
228 } else {
229 Opc = X86::LD_Fp64m;
230 RC = X86::RFP64RegisterClass;
231 }
232 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000234 // No f80 support yet.
235 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000236 }
237
238 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000239 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000240 return true;
241}
242
Evan Chengf3d4efe2008-09-07 09:09:33 +0000243/// X86FastEmitStore - Emit a machine instruction to store a value Val of
244/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
245/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000246/// i.e. V. Return true if it is possible.
247bool
Owen Andersone50ed302009-08-10 22:56:29 +0000248X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000249 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000250 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000251 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 switch (VT.getSimpleVT().SimpleTy) {
253 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000254 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 case MVT::i8: Opc = X86::MOV8mr; break;
256 case MVT::i16: Opc = X86::MOV16mr; break;
257 case MVT::i32: Opc = X86::MOV32mr; break;
258 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
259 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000260 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000261 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000263 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000265 }
Chris Lattner438949a2008-10-15 05:30:52 +0000266
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000267 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000268 return true;
269}
270
Owen Andersone50ed302009-08-10 22:56:29 +0000271bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000272 const X86AddressMode &AM) {
273 // Handle 'null' like i32/i64 0.
274 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000275 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000276
277 // If this is a store of a simple constant, fold the constant into the store.
278 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
279 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000281 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 case MVT::i8: Opc = X86::MOV8mi; break;
283 case MVT::i16: Opc = X86::MOV16mi; break;
284 case MVT::i32: Opc = X86::MOV32mi; break;
285 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000286 // Must be a 32-bit sign extended value.
287 if ((int)CI->getSExtValue() == CI->getSExtValue())
288 Opc = X86::MOV64mi32;
289 break;
290 }
291
292 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000293 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
294 .addImm(CI->getSExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000295 return true;
296 }
297 }
298
299 unsigned ValReg = getRegForValue(Val);
300 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000301 return false;
302
303 return X86FastEmitStore(VT, ValReg, AM);
304}
305
Evan Cheng24e3a902008-09-08 06:35:17 +0000306/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
307/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
308/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000309bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
310 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000311 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000312 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
313
314 if (RR != 0) {
315 ResultReg = RR;
316 return true;
317 } else
318 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000319}
320
Dan Gohman0586d912008-09-10 20:11:02 +0000321/// X86SelectAddress - Attempt to fill in an address from the given value.
322///
Chris Lattner0aa43de2009-07-10 05:33:42 +0000323bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Duncan Sands12513882009-06-03 12:05:18 +0000324 User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000325 unsigned Opcode = Instruction::UserOp1;
326 if (Instruction *I = dyn_cast<Instruction>(V)) {
327 Opcode = I->getOpcode();
328 U = I;
329 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
330 Opcode = C->getOpcode();
331 U = C;
332 }
Dan Gohman0586d912008-09-10 20:11:02 +0000333
Dan Gohman35893082008-09-18 23:23:44 +0000334 switch (Opcode) {
335 default: break;
336 case Instruction::BitCast:
337 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000338 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000339
340 case Instruction::IntToPtr:
341 // Look past no-op inttoptrs.
342 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000343 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000344 break;
Dan Gohman35893082008-09-18 23:23:44 +0000345
346 case Instruction::PtrToInt:
347 // Look past no-op ptrtoints.
348 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000349 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000350 break;
Dan Gohman35893082008-09-18 23:23:44 +0000351
352 case Instruction::Alloca: {
353 // Do static allocas.
354 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000355 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000356 if (SI != StaticAllocaMap.end()) {
357 AM.BaseType = X86AddressMode::FrameIndexBase;
358 AM.Base.FrameIndex = SI->second;
359 return true;
360 }
361 break;
Dan Gohman35893082008-09-18 23:23:44 +0000362 }
363
364 case Instruction::Add: {
365 // Adds of constants are common and easy enough.
366 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000367 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
368 // They have to fit in the 32-bit signed displacement field though.
369 if (isInt32(Disp)) {
370 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000371 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000372 }
Dan Gohman0586d912008-09-10 20:11:02 +0000373 }
Dan Gohman35893082008-09-18 23:23:44 +0000374 break;
375 }
376
377 case Instruction::GetElementPtr: {
378 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000379 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000380 unsigned IndexReg = AM.IndexReg;
381 unsigned Scale = AM.Scale;
382 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000383 // Iterate through the indices, folding what we can. Constants can be
384 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000385 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
386 i != e; ++i, ++GTI) {
387 Value *Op = *i;
388 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
389 const StructLayout *SL = TD.getStructLayout(STy);
390 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
391 Disp += SL->getElementOffset(Idx);
392 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000393 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman35893082008-09-18 23:23:44 +0000394 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
395 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000396 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000397 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000398 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000399 (S == 1 || S == 2 || S == 4 || S == 8)) {
400 // Scaled-index addressing.
401 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000402 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000403 if (IndexReg == 0)
404 return false;
405 } else
406 // Unsupported.
407 goto unsupported_gep;
408 }
409 }
Dan Gohman09aae462008-09-26 20:04:15 +0000410 // Check for displacement overflow.
411 if (!isInt32(Disp))
412 break;
Dan Gohman35893082008-09-18 23:23:44 +0000413 // Ok, the GEP indices were covered by constant-offset and scaled-index
414 // addressing. Update the address state and move on to examining the base.
415 AM.IndexReg = IndexReg;
416 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000417 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000418 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000419 unsupported_gep:
420 // Ok, the GEP indices weren't all covered.
421 break;
422 }
423 }
424
425 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000426 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000427 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000428 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000429 return false;
430
Dan Gohman97135e12008-09-26 19:15:30 +0000431 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000432 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000433 (AM.Base.Reg != 0 || AM.IndexReg != 0))
434 return false;
435
Dan Gohmane9865942009-02-23 22:03:08 +0000436 // Can't handle TLS yet.
437 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
438 if (GVar->isThreadLocal())
439 return false;
440
Chris Lattnerff7727f2009-07-09 06:41:35 +0000441 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000442 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000443
Chris Lattner0d786dd2009-07-10 07:48:51 +0000444 // Allow the subtarget to classify the global.
445 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
446
447 // If this reference is relative to the pic base, set it now.
448 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000449 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000450 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000451 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000452
453 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000454 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000455 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000456 if (Subtarget->isPICStyleRIPRel()) {
457 // Use rip-relative addressing if we can. Above we verified that the
458 // base and index registers are unused.
459 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
460 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000461 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000462 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000463 return true;
464 }
465
Chris Lattner0d786dd2009-07-10 07:48:51 +0000466 // Ok, we need to do a load from a stub. If we've already loaded from this
467 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000468 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
469 unsigned LoadReg;
470 if (I != LocalValueMap.end() && I->second != 0) {
471 LoadReg = I->second;
472 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000473 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000474 unsigned Opc = 0;
475 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000476 X86AddressMode StubAM;
477 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000478 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000479 StubAM.GVOpFlags = GVFlags;
480
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000482 Opc = X86::MOV64rm;
483 RC = X86::GR64RegisterClass;
484
Chris Lattner0d786dd2009-07-10 07:48:51 +0000485 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000486 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000487 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000488 Opc = X86::MOV32rm;
489 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000490 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000491
492 LoadReg = createResultReg(RC);
493 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
494
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000495 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000496 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000497 }
Chris Lattner18c59872009-06-27 04:16:01 +0000498
Chris Lattnerff7727f2009-07-09 06:41:35 +0000499 // Now construct the final address. Note that the Disp, Scale,
500 // and Index values may already be set here.
501 AM.Base.Reg = LoadReg;
502 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000503 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000504 }
505
Dan Gohman97135e12008-09-26 19:15:30 +0000506 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000507 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000508 if (AM.Base.Reg == 0) {
509 AM.Base.Reg = getRegForValue(V);
510 return AM.Base.Reg != 0;
511 }
512 if (AM.IndexReg == 0) {
513 assert(AM.Scale == 1 && "Scale with no index!");
514 AM.IndexReg = getRegForValue(V);
515 return AM.IndexReg != 0;
516 }
517 }
518
519 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000520}
521
Chris Lattner0aa43de2009-07-10 05:33:42 +0000522/// X86SelectCallAddress - Attempt to fill in an address from the given value.
523///
524bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
525 User *U = NULL;
526 unsigned Opcode = Instruction::UserOp1;
527 if (Instruction *I = dyn_cast<Instruction>(V)) {
528 Opcode = I->getOpcode();
529 U = I;
530 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
531 Opcode = C->getOpcode();
532 U = C;
533 }
534
535 switch (Opcode) {
536 default: break;
537 case Instruction::BitCast:
538 // Look past bitcasts.
539 return X86SelectCallAddress(U->getOperand(0), AM);
540
541 case Instruction::IntToPtr:
542 // Look past no-op inttoptrs.
543 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
544 return X86SelectCallAddress(U->getOperand(0), AM);
545 break;
546
547 case Instruction::PtrToInt:
548 // Look past no-op ptrtoints.
549 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
550 return X86SelectCallAddress(U->getOperand(0), AM);
551 break;
552 }
553
554 // Handle constant address.
555 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
556 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000557 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000558 return false;
559
560 // RIP-relative addresses can't have additional register operands.
561 if (Subtarget->isPICStyleRIPRel() &&
562 (AM.Base.Reg != 0 || AM.IndexReg != 0))
563 return false;
564
Chris Lattner754b7652009-07-10 05:48:03 +0000565 // Can't handle TLS or DLLImport.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000567 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000568 return false;
569
570 // Okay, we've committed to selecting this global. Set up the basic address.
571 AM.GV = GV;
572
Chris Lattnere6c07b52009-07-10 05:45:15 +0000573 // No ABI requires an extra load for anything other than DLLImport, which
574 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000575 if (Subtarget->isPICStyleRIPRel()) {
576 // Use rip-relative addressing if we can. Above we verified that the
577 // base and index registers are unused.
578 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
579 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000580 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000581 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
582 } else if (Subtarget->isPICStyleGOT()) {
583 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000584 }
585
Chris Lattner0aa43de2009-07-10 05:33:42 +0000586 return true;
587 }
588
589 // If all else fails, try to materialize the value in a register.
590 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
591 if (AM.Base.Reg == 0) {
592 AM.Base.Reg = getRegForValue(V);
593 return AM.Base.Reg != 0;
594 }
595 if (AM.IndexReg == 0) {
596 assert(AM.Scale == 1 && "Scale with no index!");
597 AM.IndexReg = getRegForValue(V);
598 return AM.IndexReg != 0;
599 }
600 }
601
602 return false;
603}
604
605
Owen Andersona3971df2008-09-04 07:08:58 +0000606/// X86SelectStore - Select and emit code to implement store instructions.
607bool X86FastISel::X86SelectStore(Instruction* I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000608 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000609 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000610 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000611
Dan Gohman0586d912008-09-10 20:11:02 +0000612 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000613 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000614 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000615
Chris Lattner438949a2008-10-15 05:30:52 +0000616 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000617}
618
Evan Cheng8b19e562008-09-03 06:44:39 +0000619/// X86SelectLoad - Select and emit code to implement load instructions.
620///
Dan Gohman3df24e62008-09-03 23:12:08 +0000621bool X86FastISel::X86SelectLoad(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000622 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000623 if (!isTypeLegal(I->getType(), VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000624 return false;
625
Dan Gohman0586d912008-09-10 20:11:02 +0000626 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000627 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000628 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000629
Evan Cheng0de588f2008-09-05 21:00:03 +0000630 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000631 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000632 UpdateValueMap(I, ResultReg);
633 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000634 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000635 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000636}
637
Owen Andersone50ed302009-08-10 22:56:29 +0000638static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000640 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 case MVT::i8: return X86::CMP8rr;
642 case MVT::i16: return X86::CMP16rr;
643 case MVT::i32: return X86::CMP32rr;
644 case MVT::i64: return X86::CMP64rr;
645 case MVT::f32: return X86::UCOMISSrr;
646 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000647 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000648}
649
Chris Lattner0e13c782008-10-15 04:13:29 +0000650/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
651/// of the comparison, return an opcode that works for the compare (e.g.
652/// CMP32ri) otherwise return 0.
Owen Andersone50ed302009-08-10 22:56:29 +0000653static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000655 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000656 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 case MVT::i8: return X86::CMP8ri;
658 case MVT::i16: return X86::CMP16ri;
659 case MVT::i32: return X86::CMP32ri;
660 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000661 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
662 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000663 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000664 return X86::CMP64ri32;
665 return 0;
666 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000667}
668
Owen Andersone50ed302009-08-10 22:56:29 +0000669bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000670 unsigned Op0Reg = getRegForValue(Op0);
671 if (Op0Reg == 0) return false;
672
Chris Lattnerd53886b2008-10-15 05:18:04 +0000673 // Handle 'null' like i32/i64 0.
674 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000675 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000676
Chris Lattner9a08a612008-10-15 04:26:38 +0000677 // We have two options: compare with register or immediate. If the RHS of
678 // the compare is an immediate that we can fold into this compare, use
679 // CMPri, otherwise use CMPrr.
680 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000681 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000682 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000683 .addImm(Op1C->getSExtValue());
684 return true;
685 }
686 }
687
688 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
689 if (CompareOpc == 0) return false;
690
691 unsigned Op1Reg = getRegForValue(Op1);
692 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000693 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000694
695 return true;
696}
697
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000698bool X86FastISel::X86SelectCmp(Instruction *I) {
699 CmpInst *CI = cast<CmpInst>(I);
700
Owen Andersone50ed302009-08-10 22:56:29 +0000701 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000702 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000703 return false;
704
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000705 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000706 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000707 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000708 switch (CI->getPredicate()) {
709 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000710 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
711 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000712
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000713 unsigned EReg = createResultReg(&X86::GR8RegClass);
714 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000715 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
716 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
717 BuildMI(MBB, DL,
718 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000719 UpdateValueMap(I, ResultReg);
720 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000721 }
722 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000723 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
724 return false;
725
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000726 unsigned NEReg = createResultReg(&X86::GR8RegClass);
727 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000728 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
729 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
730 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000731 UpdateValueMap(I, ResultReg);
732 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000733 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000734 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
735 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
736 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
737 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
738 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
739 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
740 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
741 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
742 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
743 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
744 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
745 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
746
747 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
748 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
749 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
750 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
751 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
752 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
753 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
754 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
755 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
756 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000757 default:
758 return false;
759 }
760
Chris Lattner9a08a612008-10-15 04:26:38 +0000761 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000762 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000763 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000764
Chris Lattner9a08a612008-10-15 04:26:38 +0000765 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000766 if (!X86FastEmitCompare(Op0, Op1, VT))
767 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000768
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000769 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000770 UpdateValueMap(I, ResultReg);
771 return true;
772}
Evan Cheng8b19e562008-09-03 06:44:39 +0000773
Dan Gohmand89ae992008-09-05 01:06:14 +0000774bool X86FastISel::X86SelectZExt(Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000775 // Handle zero-extension from i1 to i8, which is common.
Owen Anderson1d0be152009-08-13 21:58:54 +0000776 if (I->getType() == Type::getInt8Ty(I->getContext()) &&
777 I->getOperand(0)->getType() == Type::getInt1Ty(I->getContext())) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000778 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000779 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000780 // Set the high bits to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000782 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000783 UpdateValueMap(I, ResultReg);
784 return true;
785 }
786
787 return false;
788}
789
Chris Lattner9a08a612008-10-15 04:26:38 +0000790
Dan Gohmand89ae992008-09-05 01:06:14 +0000791bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000792 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000793 // Handle a conditional branch.
794 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000795 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
796 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
797
Dan Gohmand98d6202008-10-02 22:15:21 +0000798 // Fold the common case of a conditional branch with a comparison.
799 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
800 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000801 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000802
Dan Gohmand98d6202008-10-02 22:15:21 +0000803 // Try to take advantage of fallthrough opportunities.
804 CmpInst::Predicate Predicate = CI->getPredicate();
805 if (MBB->isLayoutSuccessor(TrueMBB)) {
806 std::swap(TrueMBB, FalseMBB);
807 Predicate = CmpInst::getInversePredicate(Predicate);
808 }
809
Chris Lattner871d2462008-10-15 03:58:05 +0000810 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
811 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
812
Dan Gohmand98d6202008-10-02 22:15:21 +0000813 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000814 case CmpInst::FCMP_OEQ:
815 std::swap(TrueMBB, FalseMBB);
816 Predicate = CmpInst::FCMP_UNE;
817 // FALL THROUGH
818 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner871d2462008-10-15 03:58:05 +0000819 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
820 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
821 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
822 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
823 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
824 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
825 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
826 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
827 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
828 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
829 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
830 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000831
Chris Lattner871d2462008-10-15 03:58:05 +0000832 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
833 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
834 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
835 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
836 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
837 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
838 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
839 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
840 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
841 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000842 default:
843 return false;
844 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000845
Chris Lattner709d8292008-10-15 04:02:26 +0000846 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
847 if (SwapArgs)
848 std::swap(Op0, Op1);
849
Chris Lattner9a08a612008-10-15 04:26:38 +0000850 // Emit a compare of the LHS and RHS, setting the flags.
851 if (!X86FastEmitCompare(Op0, Op1, VT))
852 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000853
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000854 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000855
856 if (Predicate == CmpInst::FCMP_UNE) {
857 // X86 requires a second branch to handle UNE (and OEQ,
858 // which is mapped to UNE above).
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000859 BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000860 }
861
Dan Gohmand98d6202008-10-02 22:15:21 +0000862 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000863 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000864 return true;
865 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000866 } else if (ExtractValueInst *EI =
867 dyn_cast<ExtractValueInst>(BI->getCondition())) {
868 // Check to see if the branch instruction is from an "arithmetic with
869 // overflow" intrinsic. The main way these intrinsics are used is:
870 //
871 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
872 // %sum = extractvalue { i32, i1 } %t, 0
873 // %obit = extractvalue { i32, i1 } %t, 1
874 // br i1 %obit, label %overflow, label %normal
875 //
Dan Gohman653456c2009-01-07 00:15:08 +0000876 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000877 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000878 // looking for the SETO/SETB instruction. If an instruction modifies the
879 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
880 // convert the branch into a JO/JB instruction.
Chris Lattnera9a42252009-04-12 07:36:01 +0000881 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
882 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
883 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
884 const MachineInstr *SetMI = 0;
885 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000886
Chris Lattnera9a42252009-04-12 07:36:01 +0000887 for (MachineBasicBlock::const_reverse_iterator
888 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
889 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000890
Chris Lattnera9a42252009-04-12 07:36:01 +0000891 if (MI.modifiesRegister(Reg)) {
892 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000893
Chris Lattnera9a42252009-04-12 07:36:01 +0000894 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
895 Reg = Src;
896 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000897 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000898
Chris Lattnera9a42252009-04-12 07:36:01 +0000899 SetMI = &MI;
900 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000901 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000902
Chris Lattnera9a42252009-04-12 07:36:01 +0000903 const TargetInstrDesc &TID = MI.getDesc();
904 if (TID.hasUnmodeledSideEffects() ||
905 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
906 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000907 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000908
909 if (SetMI) {
910 unsigned OpCode = SetMI->getOpcode();
911
912 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattner8d57b772009-04-12 07:51:14 +0000913 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ? X86::JO : X86::JB))
914 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000915 FastEmitBranch(FalseMBB);
916 MBB->addSuccessor(TrueMBB);
917 return true;
918 }
Bill Wendling9a901322008-12-10 19:44:24 +0000919 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000920 }
921 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000922 }
923
924 // Otherwise do a clumsy setcc and re-test it.
925 unsigned OpReg = getRegForValue(BI->getCondition());
926 if (OpReg == 0) return false;
927
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000928 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
929 BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000930 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000931 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000932 return true;
933}
934
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000935bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000936 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000937 const TargetRegisterClass *RC = NULL;
Owen Anderson1d0be152009-08-13 21:58:54 +0000938 if (I->getType() == Type::getInt8Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000939 CReg = X86::CL;
940 RC = &X86::GR8RegClass;
941 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000942 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
943 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
944 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000945 default: return false;
946 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000947 } else if (I->getType() == Type::getInt16Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000948 CReg = X86::CX;
949 RC = &X86::GR16RegClass;
950 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000951 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
952 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
953 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000954 default: return false;
955 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000956 } else if (I->getType() == Type::getInt32Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000957 CReg = X86::ECX;
958 RC = &X86::GR32RegClass;
959 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000960 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
961 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
962 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000963 default: return false;
964 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000965 } else if (I->getType() == Type::getInt64Ty(I->getContext())) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000966 CReg = X86::RCX;
967 RC = &X86::GR64RegClass;
968 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000969 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
970 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
971 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000972 default: return false;
973 }
974 } else {
975 return false;
976 }
977
Owen Andersone50ed302009-08-10 22:56:29 +0000978 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000980 return false;
981
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000982 unsigned Op0Reg = getRegForValue(I->getOperand(0));
983 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000984
985 // Fold immediate in shl(x,3).
986 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
987 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000988 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +0000989 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +0000990 UpdateValueMap(I, ResultReg);
991 return true;
992 }
993
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000994 unsigned Op1Reg = getRegForValue(I->getOperand(1));
995 if (Op1Reg == 0) return false;
996 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000997
998 // The shift instruction uses X86::CL. If we defined a super-register
999 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1000 // we're doing here.
1001 if (CReg != X86::CL)
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001002 BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001003 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1004
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001005 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001006 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001007 UpdateValueMap(I, ResultReg);
1008 return true;
1009}
1010
1011bool X86FastISel::X86SelectSelect(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001012 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001013 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001014 return false;
1015
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001016 unsigned Opc = 0;
1017 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001019 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001020 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001022 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001023 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001025 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001026 RC = &X86::GR64RegClass;
1027 } else {
1028 return false;
1029 }
1030
1031 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1032 if (Op0Reg == 0) return false;
1033 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1034 if (Op1Reg == 0) return false;
1035 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1036 if (Op2Reg == 0) return false;
1037
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001038 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001039 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001040 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001041 UpdateValueMap(I, ResultReg);
1042 return true;
1043}
1044
Dan Gohman78efce62008-09-10 21:02:08 +00001045bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001046 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001047 if (Subtarget->hasSSE2() &&
1048 I->getType() == Type::getDoubleTy(I->getContext())) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001049 Value *V = I->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001050 if (V->getType() == Type::getFloatTy(I->getContext())) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001051 unsigned OpReg = getRegForValue(V);
1052 if (OpReg == 0) return false;
1053 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001054 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001055 UpdateValueMap(I, ResultReg);
1056 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001057 }
1058 }
1059
1060 return false;
1061}
1062
1063bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
1064 if (Subtarget->hasSSE2()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001065 if (I->getType() == Type::getFloatTy(I->getContext())) {
Dan Gohman78efce62008-09-10 21:02:08 +00001066 Value *V = I->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001067 if (V->getType() == Type::getDoubleTy(I->getContext())) {
Dan Gohman78efce62008-09-10 21:02:08 +00001068 unsigned OpReg = getRegForValue(V);
1069 if (OpReg == 0) return false;
1070 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001071 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001072 UpdateValueMap(I, ResultReg);
1073 return true;
1074 }
1075 }
1076 }
1077
1078 return false;
1079}
1080
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001081bool X86FastISel::X86SelectTrunc(Instruction *I) {
1082 if (Subtarget->is64Bit())
1083 // All other cases should be handled by the tblgen generated code.
1084 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001085 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1086 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001087
1088 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001090 // All other cases should be handled by the tblgen generated code.
1091 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001093 // All other cases should be handled by the tblgen generated code.
1094 return false;
1095
1096 unsigned InputReg = getRegForValue(I->getOperand(0));
1097 if (!InputReg)
1098 // Unhandled operand. Halt "fast" selection and bail.
1099 return false;
1100
Dan Gohman62417622009-04-27 16:33:14 +00001101 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1103 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001104 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001105 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001106 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001107
1108 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001109 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Evan Cheng536ab132009-01-22 09:10:11 +00001110 CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001111 if (!ResultReg)
1112 return false;
1113
1114 UpdateValueMap(I, ResultReg);
1115 return true;
1116}
1117
Bill Wendling52370a12008-12-09 02:42:50 +00001118bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1119 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1120 Value *Agg = EI->getAggregateOperand();
1121
Chris Lattnera9a42252009-04-12 07:36:01 +00001122 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
1123 switch (CI->getIntrinsicID()) {
1124 default: break;
1125 case Intrinsic::sadd_with_overflow:
1126 case Intrinsic::uadd_with_overflow:
1127 // Cheat a little. We know that the registers for "add" and "seto" are
1128 // allocated sequentially. However, we only keep track of the register
1129 // for "add" in the value map. Use extractvalue's index to get the
1130 // correct register for "seto".
1131 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1132 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001133 }
1134 }
1135
1136 return false;
1137}
1138
Chris Lattnera9a42252009-04-12 07:36:01 +00001139bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001140 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001141 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001142 default: return false;
1143 case Intrinsic::sadd_with_overflow:
1144 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001145 // Replace "add with overflow" intrinsics with an "add" instruction followed
1146 // by a seto/setc instruction. Later on, when the "extractvalue"
1147 // instructions are encountered, we use the fact that two registers were
1148 // created sequentially to get the correct registers for the "sum" and the
1149 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001150 const Function *Callee = I.getCalledFunction();
1151 const Type *RetTy =
1152 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1153
Owen Andersone50ed302009-08-10 22:56:29 +00001154 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001155 if (!isTypeLegal(RetTy, VT))
1156 return false;
1157
1158 Value *Op1 = I.getOperand(1);
1159 Value *Op2 = I.getOperand(2);
1160 unsigned Reg1 = getRegForValue(Op1);
1161 unsigned Reg2 = getRegForValue(Op2);
1162
1163 if (Reg1 == 0 || Reg2 == 0)
1164 // FIXME: Handle values *not* in registers.
1165 return false;
1166
1167 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001168 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001169 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001170 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001171 OpC = X86::ADD64rr;
1172 else
1173 return false;
1174
1175 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001176 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001177 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001178
Chris Lattner8d57b772009-04-12 07:51:14 +00001179 // If the add with overflow is an intra-block value then we just want to
1180 // create temporaries for it like normal. If it is a cross-block value then
1181 // UpdateValueMap will return the cross-block register used. Since we
1182 // *really* want the value to be live in the register pair known by
1183 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1184 // the cross block case. In the non-cross-block case, we should just make
1185 // another register for the value.
1186 if (DestReg1 != ResultReg)
1187 ResultReg = DestReg1+1;
1188 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001189 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001190
Chris Lattnera9a42252009-04-12 07:36:01 +00001191 unsigned Opc = X86::SETBr;
1192 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1193 Opc = X86::SETOr;
1194 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001195 return true;
1196 }
1197 }
1198}
1199
Evan Chengf3d4efe2008-09-07 09:09:33 +00001200bool X86FastISel::X86SelectCall(Instruction *I) {
1201 CallInst *CI = cast<CallInst>(I);
1202 Value *Callee = I->getOperand(0);
1203
1204 // Can't handle inline asm yet.
1205 if (isa<InlineAsm>(Callee))
1206 return false;
1207
Bill Wendling52370a12008-12-09 02:42:50 +00001208 // Handle intrinsic calls.
Chris Lattnera9a42252009-04-12 07:36:01 +00001209 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1210 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001211
Evan Chengf3d4efe2008-09-07 09:09:33 +00001212 // Handle only C and fastcc calling conventions for now.
1213 CallSite CS(CI);
1214 unsigned CC = CS.getCallingConv();
1215 if (CC != CallingConv::C &&
1216 CC != CallingConv::Fast &&
1217 CC != CallingConv::X86_FastCall)
1218 return false;
1219
Dan Gohman7d04e4a2009-05-04 19:50:33 +00001220 // On X86, -tailcallopt changes the fastcc ABI. FastISel doesn't
1221 // handle this for now.
1222 if (CC == CallingConv::Fast && PerformTailCallOpt)
1223 return false;
1224
Evan Chengf3d4efe2008-09-07 09:09:33 +00001225 // Let SDISel handle vararg functions.
1226 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1227 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1228 if (FTy->isVarArg())
1229 return false;
1230
1231 // Handle *simple* calls for now.
1232 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001233 EVT RetVT;
Owen Anderson1d0be152009-08-13 21:58:54 +00001234 if (RetTy == Type::getVoidTy(I->getContext()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001236 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001237 return false;
1238
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001239 // Materialize callee address in a register. FIXME: GV address can be
1240 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001241 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001242 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001243 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001244 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001245 GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001246 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001247 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001248 } else if (CalleeAM.Base.Reg != 0) {
1249 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001250 } else
1251 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001252
Evan Chengdebdea02008-09-08 17:15:42 +00001253 // Allow calls which produce i1 results.
1254 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (RetVT == MVT::i1) {
1256 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001257 AndToI1 = true;
1258 }
1259
Evan Chengf3d4efe2008-09-07 09:09:33 +00001260 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001261 SmallVector<Value*, 8> ArgVals;
1262 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001263 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001264 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001265 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001266 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001267 ArgVTs.reserve(CS.arg_size());
1268 ArgFlags.reserve(CS.arg_size());
1269 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1270 i != e; ++i) {
1271 unsigned Arg = getRegForValue(*i);
1272 if (Arg == 0)
1273 return false;
1274 ISD::ArgFlagsTy Flags;
1275 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001276 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001277 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001278 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001279 Flags.setZExt();
1280
1281 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001282 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1283 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1284 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1285 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001286 return false;
1287
1288 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001289 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001290 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001291 return false;
1292 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1293 Flags.setOrigAlign(OriginalAlignment);
1294
1295 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001296 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001297 ArgVTs.push_back(ArgVT);
1298 ArgFlags.push_back(Flags);
1299 }
1300
1301 // Analyze operands of the call, assigning locations to each operand.
1302 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001303 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001304 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1305
1306 // Get a count of how many bytes are to be pushed on the stack.
1307 unsigned NumBytes = CCInfo.getNextStackOffset();
1308
1309 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001310 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001311 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001312
Chris Lattner438949a2008-10-15 05:30:52 +00001313 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001314 // copies / loads.
1315 SmallVector<unsigned, 4> RegArgs;
1316 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1317 CCValAssign &VA = ArgLocs[i];
1318 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001319 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001320
1321 // Promote the value if needed.
1322 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001323 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001324 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001325 case CCValAssign::SExt: {
1326 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1327 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001328 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001329 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001330 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001331 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001332 }
1333 case CCValAssign::ZExt: {
1334 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1335 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001336 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001337 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001338 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001339 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001340 }
1341 case CCValAssign::AExt: {
1342 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1343 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001344 if (!Emitted)
1345 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001346 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001347 if (!Emitted)
1348 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1349 Arg, ArgVT, Arg);
1350
Chris Lattnera33649e2008-12-19 17:03:38 +00001351 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001352 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001353 break;
1354 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001355 case CCValAssign::BCvt: {
1356 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
1357 ISD::BIT_CONVERT, Arg);
1358 assert(BC != 0 && "Failed to emit a bitcast!");
1359 Arg = BC;
1360 ArgVT = VA.getLocVT();
1361 break;
1362 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001363 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001364
1365 if (VA.isRegLoc()) {
1366 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1367 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1368 Arg, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001369 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001370 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001371 RegArgs.push_back(VA.getLocReg());
1372 } else {
1373 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001374 X86AddressMode AM;
1375 AM.Base.Reg = StackPtr;
1376 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001377 Value *ArgVal = ArgVals[VA.getValNo()];
1378
1379 // If this is a really simple value, emit this with the Value* version of
1380 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1381 // can cause us to reevaluate the argument.
1382 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1383 X86FastEmitStore(ArgVT, ArgVal, AM);
1384 else
1385 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001386 }
1387 }
1388
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001389 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1390 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001391 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001392 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001393 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001394 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001395 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001396 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001397 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001398
Evan Chengf3d4efe2008-09-07 09:09:33 +00001399 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001400 MachineInstrBuilder MIB;
1401 if (CalleeOp) {
1402 // Register-indirect call.
1403 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1404 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1405
1406 } else {
1407 // Direct call.
1408 assert(GV && "Not a direct call");
1409 unsigned CallOpc =
1410 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1411
1412 // See if we need any target-specific flags on the GV operand.
1413 unsigned char OpFlags = 0;
1414
1415 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1416 // external symbols most go through the PLT in PIC mode. If the symbol
1417 // has hidden or protected visibility, or if it is static or local, then
1418 // we don't need to use the PLT - we can directly call it.
1419 if (Subtarget->isTargetELF() &&
1420 TM.getRelocationModel() == Reloc::PIC_ &&
1421 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1422 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001423 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001424 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1425 Subtarget->getDarwinVers() < 9) {
1426 // PC-relative references to external symbols should go through $stub,
1427 // unless we're building with the leopard linker or later, which
1428 // automatically synthesizes these stubs.
1429 OpFlags = X86II::MO_DARWIN_STUB;
1430 }
1431
1432
1433 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1434 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001435
1436 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001437 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001438 MIB.addReg(X86::EBX);
1439
Evan Chengf3d4efe2008-09-07 09:09:33 +00001440 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001441 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1442 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001443
1444 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001445 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001446 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001447
1448 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001450 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001451 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001452 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1453
1454 // Copy all of the result registers out of their specified physreg.
1455 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001456 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1458 TargetRegisterClass *SrcRC = DstRC;
1459
1460 // If this is a call to a function that returns an fp value on the x87 fp
1461 // stack, but where we prefer to use the value in xmm registers, copy it
1462 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1463 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1464 RVLocs[0].getLocReg() == X86::ST1) &&
1465 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001467 SrcRC = X86::RSTRegisterClass;
1468 DstRC = X86::RFP80RegisterClass;
1469 }
1470
1471 unsigned ResultReg = createResultReg(DstRC);
1472 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1473 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001474 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001475 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001476 if (CopyVT != RVLocs[0].getValVT()) {
1477 // Round the F80 the right size, which also moves to the appropriate xmm
1478 // register. This is accomplished by storing the F80 value in memory and
1479 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001480 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001482 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001483 int FI = MFI.CreateStackObject(MemSize, MemSize);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001484 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001487 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001488 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001489 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001490 }
1491
Evan Chengdebdea02008-09-08 17:15:42 +00001492 if (AndToI1) {
1493 // Mask out all but lowest bit for some call which produces an i1.
1494 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001495 BuildMI(MBB, DL,
1496 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001497 ResultReg = AndResult;
1498 }
1499
Evan Chengf3d4efe2008-09-07 09:09:33 +00001500 UpdateValueMap(I, ResultReg);
1501 }
1502
1503 return true;
1504}
1505
1506
Dan Gohman99b21822008-08-28 23:21:34 +00001507bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001508X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001509 switch (I->getOpcode()) {
1510 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001511 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001512 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001513 case Instruction::Store:
1514 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001515 case Instruction::ICmp:
1516 case Instruction::FCmp:
1517 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001518 case Instruction::ZExt:
1519 return X86SelectZExt(I);
1520 case Instruction::Br:
1521 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001522 case Instruction::Call:
1523 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001524 case Instruction::LShr:
1525 case Instruction::AShr:
1526 case Instruction::Shl:
1527 return X86SelectShift(I);
1528 case Instruction::Select:
1529 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001530 case Instruction::Trunc:
1531 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001532 case Instruction::FPExt:
1533 return X86SelectFPExt(I);
1534 case Instruction::FPTrunc:
1535 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001536 case Instruction::ExtractValue:
1537 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001538 case Instruction::IntToPtr: // Deliberate fall-through.
1539 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001540 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1541 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001542 if (DstVT.bitsGT(SrcVT))
1543 return X86SelectZExt(I);
1544 if (DstVT.bitsLT(SrcVT))
1545 return X86SelectTrunc(I);
1546 unsigned Reg = getRegForValue(I->getOperand(0));
1547 if (Reg == 0) return false;
1548 UpdateValueMap(I, Reg);
1549 return true;
1550 }
Dan Gohman99b21822008-08-28 23:21:34 +00001551 }
1552
1553 return false;
1554}
1555
Dan Gohman0586d912008-09-10 20:11:02 +00001556unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001557 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001558 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001559 return false;
1560
1561 // Get opcode and regclass of the output for the given load instruction.
1562 unsigned Opc = 0;
1563 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001565 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001567 Opc = X86::MOV8rm;
1568 RC = X86::GR8RegisterClass;
1569 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001571 Opc = X86::MOV16rm;
1572 RC = X86::GR16RegisterClass;
1573 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001575 Opc = X86::MOV32rm;
1576 RC = X86::GR32RegisterClass;
1577 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001579 // Must be in x86-64 mode.
1580 Opc = X86::MOV64rm;
1581 RC = X86::GR64RegisterClass;
1582 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001584 if (Subtarget->hasSSE1()) {
1585 Opc = X86::MOVSSrm;
1586 RC = X86::FR32RegisterClass;
1587 } else {
1588 Opc = X86::LD_Fp32m;
1589 RC = X86::RFP32RegisterClass;
1590 }
1591 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001593 if (Subtarget->hasSSE2()) {
1594 Opc = X86::MOVSDrm;
1595 RC = X86::FR64RegisterClass;
1596 } else {
1597 Opc = X86::LD_Fp64m;
1598 RC = X86::RFP64RegisterClass;
1599 }
1600 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001602 // No f80 support yet.
1603 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001604 }
1605
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001606 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001607 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001608 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001609 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001611 Opc = X86::LEA32r;
1612 else
1613 Opc = X86::LEA64r;
1614 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001615 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001616 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001617 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001618 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001619 }
1620
Owen Anderson3b217c62008-09-06 01:11:01 +00001621 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001622 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001623 if (Align == 0) {
1624 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001625 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001626 }
Owen Anderson95267a12008-09-05 00:06:23 +00001627
Dan Gohman5396c992008-09-30 01:21:32 +00001628 // x86-32 PIC requires a PIC base register for constant pools.
1629 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001630 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001631 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001632 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1633 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1634 } else if (Subtarget->isPICStyleGOT()) {
1635 OpFlag = X86II::MO_GOTOFF;
1636 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1637 } else if (Subtarget->isPICStyleRIPRel() &&
1638 TM.getCodeModel() == CodeModel::Small) {
1639 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001640 }
Dan Gohman5396c992008-09-30 01:21:32 +00001641
1642 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001643 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001644 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001645 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1646 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001647
Owen Anderson95267a12008-09-05 00:06:23 +00001648 return ResultReg;
1649}
1650
Dan Gohman0586d912008-09-10 20:11:02 +00001651unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001652 // Fail on dynamic allocas. At this point, getRegForValue has already
1653 // checked its CSE maps, so if we're here trying to handle a dynamic
1654 // alloca, we're not going to succeed. X86SelectAddress has a
1655 // check for dynamic allocas, because it's called directly from
1656 // various places, but TargetMaterializeAlloca also needs a check
1657 // in order to avoid recursion between getRegForValue,
1658 // X86SelectAddrss, and TargetMaterializeAlloca.
1659 if (!StaticAllocaMap.count(C))
1660 return 0;
1661
Dan Gohman0586d912008-09-10 20:11:02 +00001662 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001663 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001664 return 0;
1665 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1666 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1667 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001668 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001669 return ResultReg;
1670}
1671
Evan Chengc3f44b02008-09-03 00:03:49 +00001672namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001673 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001674 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +00001675 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001676 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001677 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001678 DenseMap<const AllocaInst *, int> &am
1679#ifndef NDEBUG
1680 , SmallSet<Instruction*, 8> &cil
1681#endif
1682 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001683 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001684#ifndef NDEBUG
1685 , cil
1686#endif
1687 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001688 }
Dan Gohman99b21822008-08-28 23:21:34 +00001689}