1 //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/CodeGen/FastISel.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/Support/CallSite.h"
30 #include "llvm/Support/GetElementPtrTypeIterator.h"
34 class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
39 /// StackPtr - Register used as the stack pointer.
43 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
44 /// floating point ops.
45 /// When SSE is available, use it for f32 operations.
46 /// When SSE2 is available, use it for f64 operations.
51 explicit X86FastISel(MachineFunction &mf,
52 MachineModuleInfo *mmi,
53 DenseMap<const Value *, unsigned> &vm,
54 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
55 DenseMap<const AllocaInst *, int> &am
57 , SmallSet<Instruction*, 8> &cil
60 : FastISel(mf, mmi, vm, bm, am
65 Subtarget = &TM.getSubtarget<X86Subtarget>();
66 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
67 X86ScalarSSEf64 = Subtarget->hasSSE2();
68 X86ScalarSSEf32 = Subtarget->hasSSE1();
71 virtual bool TargetSelectInstruction(Instruction *I);
73 #include "X86GenFastISel.inc"
76 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
78 bool X86FastEmitStore(MVT VT, unsigned Val,
79 const X86AddressMode &AM);
81 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
84 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
86 bool X86SelectLoad(Instruction *I);
88 bool X86SelectStore(Instruction *I);
90 bool X86SelectCmp(Instruction *I);
92 bool X86SelectZExt(Instruction *I);
94 bool X86SelectBranch(Instruction *I);
96 bool X86SelectShift(Instruction *I);
98 bool X86SelectSelect(Instruction *I);
100 bool X86SelectTrunc(Instruction *I);
102 unsigned X86ChooseCmpOpcode(MVT VT);
104 bool X86SelectFPExt(Instruction *I);
105 bool X86SelectFPTrunc(Instruction *I);
107 bool X86SelectCall(Instruction *I);
109 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
111 const X86InstrInfo *getInstrInfo() const {
112 return getTargetMachine()->getInstrInfo();
114 const X86TargetMachine *getTargetMachine() const {
115 return static_cast<const X86TargetMachine *>(&TM);
118 unsigned TargetMaterializeConstant(Constant *C);
120 unsigned TargetMaterializeAlloca(AllocaInst *C);
122 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
123 /// computed in an SSE register, not on the X87 floating point stack.
124 bool isScalarFPTypeInSSEReg(MVT VT) const {
125 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
126 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
129 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
130 bool AllowI1 = false);
133 bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
134 MVT &VT, bool AllowI1) {
135 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
136 if (VT == MVT::Other || !VT.isSimple())
137 // Unhandled type. Halt "fast" selection and bail.
141 VT = TLI.getPointerTy();
142 // For now, require SSE/SSE2 for performing floating-point operations,
143 // since x87 requires additional work.
144 if (VT == MVT::f64 && !X86ScalarSSEf64)
146 if (VT == MVT::f32 && !X86ScalarSSEf32)
148 // Similarly, no f80 support yet.
151 // We only handle legal types. For example, on x86-32 the instruction
152 // selector contains all of the 64-bit instructions from x86-64,
153 // under the assumption that i64 won't be used if the target doesn't
155 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
158 #include "X86GenCallingConv.inc"
160 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
162 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
163 if (Subtarget->is64Bit()) {
164 if (Subtarget->isTargetWin64())
165 return CC_X86_Win64_C;
166 else if (CC == CallingConv::Fast && isTaillCall)
167 return CC_X86_64_TailCall;
172 if (CC == CallingConv::X86_FastCall)
173 return CC_X86_32_FastCall;
174 else if (CC == CallingConv::Fast)
175 return CC_X86_32_FastCC;
180 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
181 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
182 /// Return true and the result register by reference if it is possible.
183 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
184 unsigned &ResultReg) {
185 // Get opcode and regclass of the output for the given load instruction.
187 const TargetRegisterClass *RC = NULL;
188 switch (VT.getSimpleVT()) {
189 default: return false;
192 RC = X86::GR8RegisterClass;
196 RC = X86::GR16RegisterClass;
200 RC = X86::GR32RegisterClass;
203 // Must be in x86-64 mode.
205 RC = X86::GR64RegisterClass;
208 if (Subtarget->hasSSE1()) {
210 RC = X86::FR32RegisterClass;
213 RC = X86::RFP32RegisterClass;
217 if (Subtarget->hasSSE2()) {
219 RC = X86::FR64RegisterClass;
222 RC = X86::RFP64RegisterClass;
226 // No f80 support yet.
230 ResultReg = createResultReg(RC);
231 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
235 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
236 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
237 /// and a displacement offset, or a GlobalAddress,
238 /// i.e. V. Return true if it is possible.
240 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
241 const X86AddressMode &AM) {
242 // Get opcode and regclass of the output for the given store instruction.
244 const TargetRegisterClass *RC = NULL;
245 switch (VT.getSimpleVT()) {
246 default: return false;
249 RC = X86::GR8RegisterClass;
253 RC = X86::GR16RegisterClass;
257 RC = X86::GR32RegisterClass;
260 // Must be in x86-64 mode.
262 RC = X86::GR64RegisterClass;
265 if (Subtarget->hasSSE1()) {
267 RC = X86::FR32RegisterClass;
270 RC = X86::RFP32RegisterClass;
274 if (Subtarget->hasSSE2()) {
276 RC = X86::FR64RegisterClass;
279 RC = X86::RFP64RegisterClass;
283 // No f80 support yet.
287 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
291 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
292 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
293 /// ISD::SIGN_EXTEND).
294 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
295 unsigned Src, MVT SrcVT,
296 unsigned &ResultReg) {
297 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
306 /// X86SelectAddress - Attempt to fill in an address from the given value.
308 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
310 unsigned Opcode = Instruction::UserOp1;
311 if (Instruction *I = dyn_cast<Instruction>(V)) {
312 Opcode = I->getOpcode();
314 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
315 Opcode = C->getOpcode();
321 case Instruction::BitCast:
322 // Look past bitcasts.
323 return X86SelectAddress(U->getOperand(0), AM, isCall);
325 case Instruction::IntToPtr:
326 // Look past no-op inttoptrs.
327 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
328 return X86SelectAddress(U->getOperand(0), AM, isCall);
330 case Instruction::PtrToInt:
331 // Look past no-op ptrtoints.
332 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
333 return X86SelectAddress(U->getOperand(0), AM, isCall);
335 case Instruction::Alloca: {
337 // Do static allocas.
338 const AllocaInst *A = cast<AllocaInst>(V);
339 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
340 if (SI != StaticAllocaMap.end()) {
341 AM.BaseType = X86AddressMode::FrameIndexBase;
342 AM.Base.FrameIndex = SI->second;
348 case Instruction::Add: {
350 // Adds of constants are common and easy enough.
351 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
352 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
353 // They have to fit in the 32-bit signed displacement field though.
355 AM.Disp = (uint32_t)Disp;
356 return X86SelectAddress(U->getOperand(0), AM, isCall);
362 case Instruction::GetElementPtr: {
364 // Pattern-match simple GEPs.
365 uint64_t Disp = (int32_t)AM.Disp;
366 unsigned IndexReg = AM.IndexReg;
367 unsigned Scale = AM.Scale;
368 gep_type_iterator GTI = gep_type_begin(U);
369 // Look at all but the last index. Constants can be folded,
370 // and one dynamic index can be handled, if the scale is supported.
371 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
372 i != e; ++i, ++GTI) {
374 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
375 const StructLayout *SL = TD.getStructLayout(STy);
376 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
377 Disp += SL->getElementOffset(Idx);
379 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
380 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
381 // Constant-offset addressing.
382 Disp += CI->getSExtValue() * S;
383 } else if (IndexReg == 0 &&
385 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
386 (S == 1 || S == 2 || S == 4 || S == 8)) {
387 // Scaled-index addressing.
389 IndexReg = getRegForValue(Op);
394 goto unsupported_gep;
397 // Check for displacement overflow.
400 // Ok, the GEP indices were covered by constant-offset and scaled-index
401 // addressing. Update the address state and move on to examining the base.
402 AM.IndexReg = IndexReg;
404 AM.Disp = (uint32_t)Disp;
405 return X86SelectAddress(U->getOperand(0), AM, isCall);
407 // Ok, the GEP indices weren't all covered.
412 // Handle constant address.
413 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
414 // Can't handle alternate code models yet.
415 if (TM.getCodeModel() != CodeModel::Default &&
416 TM.getCodeModel() != CodeModel::Small)
419 // RIP-relative addresses can't have additional register operands.
420 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
421 (AM.Base.Reg != 0 || AM.IndexReg != 0))
424 // Set up the basic address.
427 TM.getRelocationModel() == Reloc::PIC_ &&
428 !Subtarget->is64Bit())
429 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
431 // Emit an extra load if the ABI requires it.
432 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
433 // Check to see if we've already materialized this
434 // value in a register in this block.
435 if (unsigned Reg = LocalValueMap[V]) {
440 // Issue load from stub if necessary.
442 const TargetRegisterClass *RC = NULL;
443 if (TLI.getPointerTy() == MVT::i32) {
445 RC = X86::GR32RegisterClass;
448 RC = X86::GR64RegisterClass;
451 X86AddressMode StubAM;
452 StubAM.Base.Reg = AM.Base.Reg;
454 unsigned ResultReg = createResultReg(RC);
455 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
457 // Now construct the final address. Note that the Disp, Scale,
458 // and Index values may already be set here.
459 AM.Base.Reg = ResultReg;
462 // Prevent loading GV stub multiple times in same MBB.
463 LocalValueMap[V] = AM.Base.Reg;
468 // If all else fails, try to materialize the value in a register.
469 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
470 if (AM.Base.Reg == 0) {
471 AM.Base.Reg = getRegForValue(V);
472 return AM.Base.Reg != 0;
474 if (AM.IndexReg == 0) {
475 assert(AM.Scale == 1 && "Scale with no index!");
476 AM.IndexReg = getRegForValue(V);
477 return AM.IndexReg != 0;
484 /// X86SelectStore - Select and emit code to implement store instructions.
485 bool X86FastISel::X86SelectStore(Instruction* I) {
487 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
489 unsigned Val = getRegForValue(I->getOperand(0));
491 // Unhandled operand. Halt "fast" selection and bail.
495 if (!X86SelectAddress(I->getOperand(1), AM, false))
498 return X86FastEmitStore(VT, Val, AM);
501 /// X86SelectLoad - Select and emit code to implement load instructions.
503 bool X86FastISel::X86SelectLoad(Instruction *I) {
505 if (!isTypeLegal(I->getType(), TLI, VT))
509 if (!X86SelectAddress(I->getOperand(0), AM, false))
512 unsigned ResultReg = 0;
513 if (X86FastEmitLoad(VT, AM, ResultReg)) {
514 UpdateValueMap(I, ResultReg);
520 unsigned X86FastISel::X86ChooseCmpOpcode(MVT VT) {
521 switch (VT.getSimpleVT()) {
522 case MVT::i8: return X86::CMP8rr;
523 case MVT::i16: return X86::CMP16rr;
524 case MVT::i32: return X86::CMP32rr;
525 case MVT::i64: return X86::CMP64rr;
526 case MVT::f32: return X86::UCOMISSrr;
527 case MVT::f64: return X86::UCOMISDrr;
533 bool X86FastISel::X86SelectCmp(Instruction *I) {
534 CmpInst *CI = cast<CmpInst>(I);
537 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
540 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
541 if (Op0Reg == 0) return false;
542 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
543 if (Op1Reg == 0) return false;
545 unsigned Opc = X86ChooseCmpOpcode(VT);
547 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
548 switch (CI->getPredicate()) {
549 case CmpInst::FCMP_OEQ: {
550 unsigned EReg = createResultReg(&X86::GR8RegClass);
551 unsigned NPReg = createResultReg(&X86::GR8RegClass);
552 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
553 BuildMI(MBB, TII.get(X86::SETEr), EReg);
554 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
555 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
558 case CmpInst::FCMP_UNE: {
559 unsigned NEReg = createResultReg(&X86::GR8RegClass);
560 unsigned PReg = createResultReg(&X86::GR8RegClass);
561 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
562 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
563 BuildMI(MBB, TII.get(X86::SETPr), PReg);
564 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
567 case CmpInst::FCMP_OGT:
568 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
569 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
571 case CmpInst::FCMP_OGE:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
573 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
575 case CmpInst::FCMP_OLT:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
577 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
579 case CmpInst::FCMP_OLE:
580 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
581 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
583 case CmpInst::FCMP_ONE:
584 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
585 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
587 case CmpInst::FCMP_ORD:
588 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
589 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
591 case CmpInst::FCMP_UNO:
592 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
593 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
595 case CmpInst::FCMP_UEQ:
596 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
597 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
599 case CmpInst::FCMP_UGT:
600 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
601 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
603 case CmpInst::FCMP_UGE:
604 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
605 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
607 case CmpInst::FCMP_ULT:
608 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
609 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
611 case CmpInst::FCMP_ULE:
612 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
613 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
615 case CmpInst::ICMP_EQ:
616 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
617 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
619 case CmpInst::ICMP_NE:
620 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
621 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
623 case CmpInst::ICMP_UGT:
624 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
625 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
627 case CmpInst::ICMP_UGE:
628 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
629 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
631 case CmpInst::ICMP_ULT:
632 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
633 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
635 case CmpInst::ICMP_ULE:
636 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
637 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
639 case CmpInst::ICMP_SGT:
640 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
641 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
643 case CmpInst::ICMP_SGE:
644 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
645 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
647 case CmpInst::ICMP_SLT:
648 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
649 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
651 case CmpInst::ICMP_SLE:
652 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
653 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
659 UpdateValueMap(I, ResultReg);
663 bool X86FastISel::X86SelectZExt(Instruction *I) {
664 // Special-case hack: The only i1 values we know how to produce currently
665 // set the upper bits of an i8 value to zero.
666 if (I->getType() == Type::Int8Ty &&
667 I->getOperand(0)->getType() == Type::Int1Ty) {
668 unsigned ResultReg = getRegForValue(I->getOperand(0));
669 if (ResultReg == 0) return false;
670 UpdateValueMap(I, ResultReg);
677 bool X86FastISel::X86SelectBranch(Instruction *I) {
678 // Unconditional branches are selected by tablegen-generated code.
679 // Handle a conditional branch.
680 BranchInst *BI = cast<BranchInst>(I);
681 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
682 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
684 // Fold the common case of a conditional branch with a comparison.
685 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
686 if (CI->hasOneUse()) {
687 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
688 unsigned Opc = X86ChooseCmpOpcode(VT);
689 if (Opc == 0) return false;
691 // Try to take advantage of fallthrough opportunities.
692 CmpInst::Predicate Predicate = CI->getPredicate();
693 if (MBB->isLayoutSuccessor(TrueMBB)) {
694 std::swap(TrueMBB, FalseMBB);
695 Predicate = CmpInst::getInversePredicate(Predicate);
698 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
699 if (Op0Reg == 0) return false;
700 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
701 if (Op1Reg == 0) return false;
704 case CmpInst::FCMP_OGT:
705 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
706 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
708 case CmpInst::FCMP_OGE:
709 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
710 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
712 case CmpInst::FCMP_OLT:
713 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
714 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
716 case CmpInst::FCMP_OLE:
717 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
718 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
720 case CmpInst::FCMP_ONE:
721 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
722 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
724 case CmpInst::FCMP_ORD:
725 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
726 BuildMI(MBB, TII.get(X86::JNP)).addMBB(TrueMBB);
728 case CmpInst::FCMP_UNO:
729 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
730 BuildMI(MBB, TII.get(X86::JP)).addMBB(TrueMBB);
732 case CmpInst::FCMP_UEQ:
733 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
734 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
736 case CmpInst::FCMP_UGT:
737 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
738 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
740 case CmpInst::FCMP_UGE:
741 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
742 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
744 case CmpInst::FCMP_ULT:
745 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
746 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
748 case CmpInst::FCMP_ULE:
749 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
750 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
752 case CmpInst::ICMP_EQ:
753 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
754 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
756 case CmpInst::ICMP_NE:
757 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
758 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
760 case CmpInst::ICMP_UGT:
761 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
762 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
764 case CmpInst::ICMP_UGE:
765 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
766 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
768 case CmpInst::ICMP_ULT:
769 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
770 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
772 case CmpInst::ICMP_ULE:
773 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
774 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
776 case CmpInst::ICMP_SGT:
777 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
778 BuildMI(MBB, TII.get(X86::JG)).addMBB(TrueMBB);
780 case CmpInst::ICMP_SGE:
781 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
782 BuildMI(MBB, TII.get(X86::JGE)).addMBB(TrueMBB);
784 case CmpInst::ICMP_SLT:
785 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
786 BuildMI(MBB, TII.get(X86::JL)).addMBB(TrueMBB);
788 case CmpInst::ICMP_SLE:
789 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
790 BuildMI(MBB, TII.get(X86::JLE)).addMBB(TrueMBB);
795 FastEmitBranch(FalseMBB);
796 MBB->addSuccessor(TrueMBB);
801 // Otherwise do a clumsy setcc and re-test it.
802 unsigned OpReg = getRegForValue(BI->getCondition());
803 if (OpReg == 0) return false;
805 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
807 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
808 FastEmitBranch(FalseMBB);
809 MBB->addSuccessor(TrueMBB);
814 bool X86FastISel::X86SelectShift(Instruction *I) {
815 unsigned CReg = 0, OpReg = 0, OpImm = 0;
816 const TargetRegisterClass *RC = NULL;
817 if (I->getType() == Type::Int8Ty) {
819 RC = &X86::GR8RegClass;
820 switch (I->getOpcode()) {
821 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
822 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
823 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
824 default: return false;
826 } else if (I->getType() == Type::Int16Ty) {
828 RC = &X86::GR16RegClass;
829 switch (I->getOpcode()) {
830 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
831 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
832 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
833 default: return false;
835 } else if (I->getType() == Type::Int32Ty) {
837 RC = &X86::GR32RegClass;
838 switch (I->getOpcode()) {
839 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
840 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
841 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
842 default: return false;
844 } else if (I->getType() == Type::Int64Ty) {
846 RC = &X86::GR64RegClass;
847 switch (I->getOpcode()) {
848 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
849 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
850 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
851 default: return false;
857 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
858 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
861 unsigned Op0Reg = getRegForValue(I->getOperand(0));
862 if (Op0Reg == 0) return false;
864 // Fold immediate in shl(x,3).
865 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
866 unsigned ResultReg = createResultReg(RC);
867 BuildMI(MBB, TII.get(OpImm),
868 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
869 UpdateValueMap(I, ResultReg);
873 unsigned Op1Reg = getRegForValue(I->getOperand(1));
874 if (Op1Reg == 0) return false;
875 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
877 // The shift instruction uses X86::CL. If we defined a super-register
878 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
881 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
882 .addReg(CReg).addImm(X86::SUBREG_8BIT);
884 unsigned ResultReg = createResultReg(RC);
885 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
886 UpdateValueMap(I, ResultReg);
890 bool X86FastISel::X86SelectSelect(Instruction *I) {
891 const Type *Ty = I->getType();
892 if (isa<PointerType>(Ty))
893 Ty = TD.getIntPtrType();
896 const TargetRegisterClass *RC = NULL;
897 if (Ty == Type::Int16Ty) {
898 Opc = X86::CMOVE16rr;
899 RC = &X86::GR16RegClass;
900 } else if (Ty == Type::Int32Ty) {
901 Opc = X86::CMOVE32rr;
902 RC = &X86::GR32RegClass;
903 } else if (Ty == Type::Int64Ty) {
904 Opc = X86::CMOVE64rr;
905 RC = &X86::GR64RegClass;
910 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
911 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
914 unsigned Op0Reg = getRegForValue(I->getOperand(0));
915 if (Op0Reg == 0) return false;
916 unsigned Op1Reg = getRegForValue(I->getOperand(1));
917 if (Op1Reg == 0) return false;
918 unsigned Op2Reg = getRegForValue(I->getOperand(2));
919 if (Op2Reg == 0) return false;
921 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
922 unsigned ResultReg = createResultReg(RC);
923 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
924 UpdateValueMap(I, ResultReg);
928 bool X86FastISel::X86SelectFPExt(Instruction *I) {
929 if (Subtarget->hasSSE2()) {
930 if (I->getType() == Type::DoubleTy) {
931 Value *V = I->getOperand(0);
932 if (V->getType() == Type::FloatTy) {
933 unsigned OpReg = getRegForValue(V);
934 if (OpReg == 0) return false;
935 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
936 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
937 UpdateValueMap(I, ResultReg);
946 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
947 if (Subtarget->hasSSE2()) {
948 if (I->getType() == Type::FloatTy) {
949 Value *V = I->getOperand(0);
950 if (V->getType() == Type::DoubleTy) {
951 unsigned OpReg = getRegForValue(V);
952 if (OpReg == 0) return false;
953 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
954 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
955 UpdateValueMap(I, ResultReg);
964 bool X86FastISel::X86SelectTrunc(Instruction *I) {
965 if (Subtarget->is64Bit())
966 // All other cases should be handled by the tblgen generated code.
968 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
969 MVT DstVT = TLI.getValueType(I->getType());
970 if (DstVT != MVT::i8)
971 // All other cases should be handled by the tblgen generated code.
973 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
974 // All other cases should be handled by the tblgen generated code.
977 unsigned InputReg = getRegForValue(I->getOperand(0));
979 // Unhandled operand. Halt "fast" selection and bail.
982 // First issue a copy to GR16_ or GR32_.
983 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
984 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
985 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
986 unsigned CopyReg = createResultReg(CopyRC);
987 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
989 // Then issue an extract_subreg.
990 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
994 UpdateValueMap(I, ResultReg);
998 bool X86FastISel::X86SelectCall(Instruction *I) {
999 CallInst *CI = cast<CallInst>(I);
1000 Value *Callee = I->getOperand(0);
1002 // Can't handle inline asm yet.
1003 if (isa<InlineAsm>(Callee))
1006 // FIXME: Handle some intrinsics.
1007 if (Function *F = CI->getCalledFunction()) {
1008 if (F->isDeclaration() &&F->getIntrinsicID())
1012 // Handle only C and fastcc calling conventions for now.
1014 unsigned CC = CS.getCallingConv();
1015 if (CC != CallingConv::C &&
1016 CC != CallingConv::Fast &&
1017 CC != CallingConv::X86_FastCall)
1020 // Let SDISel handle vararg functions.
1021 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1022 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1023 if (FTy->isVarArg())
1026 // Handle *simple* calls for now.
1027 const Type *RetTy = CS.getType();
1029 if (RetTy == Type::VoidTy)
1030 RetVT = MVT::isVoid;
1031 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
1034 // Materialize callee address in a register. FIXME: GV address can be
1035 // handled with a CALLpcrel32 instead.
1036 X86AddressMode CalleeAM;
1037 if (!X86SelectAddress(Callee, CalleeAM, true))
1039 unsigned CalleeOp = 0;
1040 GlobalValue *GV = 0;
1041 if (CalleeAM.Base.Reg != 0) {
1042 assert(CalleeAM.GV == 0);
1043 CalleeOp = CalleeAM.Base.Reg;
1044 } else if (CalleeAM.GV != 0) {
1045 assert(CalleeAM.GV != 0);
1050 // Allow calls which produce i1 results.
1051 bool AndToI1 = false;
1052 if (RetVT == MVT::i1) {
1057 // Deal with call operands first.
1058 SmallVector<unsigned, 4> Args;
1059 SmallVector<MVT, 4> ArgVTs;
1060 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1061 Args.reserve(CS.arg_size());
1062 ArgVTs.reserve(CS.arg_size());
1063 ArgFlags.reserve(CS.arg_size());
1064 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1066 unsigned Arg = getRegForValue(*i);
1069 ISD::ArgFlagsTy Flags;
1070 unsigned AttrInd = i - CS.arg_begin() + 1;
1071 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1073 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1076 // FIXME: Only handle *easy* calls for now.
1077 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1078 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1079 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1080 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1083 const Type *ArgTy = (*i)->getType();
1085 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1087 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1088 Flags.setOrigAlign(OriginalAlignment);
1090 Args.push_back(Arg);
1091 ArgVTs.push_back(ArgVT);
1092 ArgFlags.push_back(Flags);
1095 // Analyze operands of the call, assigning locations to each operand.
1096 SmallVector<CCValAssign, 16> ArgLocs;
1097 CCState CCInfo(CC, false, TM, ArgLocs);
1098 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1100 // Get a count of how many bytes are to be pushed on the stack.
1101 unsigned NumBytes = CCInfo.getNextStackOffset();
1103 // Issue CALLSEQ_START
1104 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1105 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
1107 // Process argumenet: walk the register/memloc assignments, inserting
1109 SmallVector<unsigned, 4> RegArgs;
1110 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1111 CCValAssign &VA = ArgLocs[i];
1112 unsigned Arg = Args[VA.getValNo()];
1113 MVT ArgVT = ArgVTs[VA.getValNo()];
1115 // Promote the value if needed.
1116 switch (VA.getLocInfo()) {
1117 default: assert(0 && "Unknown loc info!");
1118 case CCValAssign::Full: break;
1119 case CCValAssign::SExt: {
1120 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1122 assert(Emitted && "Failed to emit a sext!");
1123 ArgVT = VA.getLocVT();
1126 case CCValAssign::ZExt: {
1127 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1129 assert(Emitted && "Failed to emit a zext!");
1130 ArgVT = VA.getLocVT();
1133 case CCValAssign::AExt: {
1134 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1137 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1140 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1143 assert(Emitted && "Failed to emit a aext!");
1144 ArgVT = VA.getLocVT();
1149 if (VA.isRegLoc()) {
1150 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1151 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1153 assert(Emitted && "Failed to emit a copy instruction!");
1154 RegArgs.push_back(VA.getLocReg());
1156 unsigned LocMemOffset = VA.getLocMemOffset();
1158 AM.Base.Reg = StackPtr;
1159 AM.Disp = LocMemOffset;
1160 X86FastEmitStore(ArgVT, Arg, AM);
1164 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1166 if (!Subtarget->is64Bit() &&
1167 TM.getRelocationModel() == Reloc::PIC_ &&
1168 Subtarget->isPICStyleGOT()) {
1169 TargetRegisterClass *RC = X86::GR32RegisterClass;
1170 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
1171 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1172 assert(Emitted && "Failed to emit a copy instruction!");
1176 unsigned CallOpc = CalleeOp
1177 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1178 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1179 MachineInstrBuilder MIB = CalleeOp
1180 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
1181 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
1183 // Add an implicit use GOT pointer in EBX.
1184 if (!Subtarget->is64Bit() &&
1185 TM.getRelocationModel() == Reloc::PIC_ &&
1186 Subtarget->isPICStyleGOT())
1187 MIB.addReg(X86::EBX);
1189 // Add implicit physical register uses to the call.
1190 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1191 MIB.addReg(RegArgs[i]);
1193 // Issue CALLSEQ_END
1194 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1195 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
1197 // Now handle call return value (if any).
1198 if (RetVT.getSimpleVT() != MVT::isVoid) {
1199 SmallVector<CCValAssign, 16> RVLocs;
1200 CCState CCInfo(CC, false, TM, RVLocs);
1201 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1203 // Copy all of the result registers out of their specified physreg.
1204 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1205 MVT CopyVT = RVLocs[0].getValVT();
1206 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1207 TargetRegisterClass *SrcRC = DstRC;
1209 // If this is a call to a function that returns an fp value on the x87 fp
1210 // stack, but where we prefer to use the value in xmm registers, copy it
1211 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1212 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1213 RVLocs[0].getLocReg() == X86::ST1) &&
1214 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1216 SrcRC = X86::RSTRegisterClass;
1217 DstRC = X86::RFP80RegisterClass;
1220 unsigned ResultReg = createResultReg(DstRC);
1221 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1222 RVLocs[0].getLocReg(), DstRC, SrcRC);
1223 assert(Emitted && "Failed to emit a copy instruction!");
1224 if (CopyVT != RVLocs[0].getValVT()) {
1225 // Round the F80 the right size, which also moves to the appropriate xmm
1226 // register. This is accomplished by storing the F80 value in memory and
1227 // then loading it back. Ewww...
1228 MVT ResVT = RVLocs[0].getValVT();
1229 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1230 unsigned MemSize = ResVT.getSizeInBits()/8;
1231 int FI = MFI.CreateStackObject(MemSize, MemSize);
1232 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1233 DstRC = ResVT == MVT::f32
1234 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1235 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1236 ResultReg = createResultReg(DstRC);
1237 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1241 // Mask out all but lowest bit for some call which produces an i1.
1242 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1243 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1244 ResultReg = AndResult;
1247 UpdateValueMap(I, ResultReg);
1255 X86FastISel::TargetSelectInstruction(Instruction *I) {
1256 switch (I->getOpcode()) {
1258 case Instruction::Load:
1259 return X86SelectLoad(I);
1260 case Instruction::Store:
1261 return X86SelectStore(I);
1262 case Instruction::ICmp:
1263 case Instruction::FCmp:
1264 return X86SelectCmp(I);
1265 case Instruction::ZExt:
1266 return X86SelectZExt(I);
1267 case Instruction::Br:
1268 return X86SelectBranch(I);
1269 case Instruction::Call:
1270 return X86SelectCall(I);
1271 case Instruction::LShr:
1272 case Instruction::AShr:
1273 case Instruction::Shl:
1274 return X86SelectShift(I);
1275 case Instruction::Select:
1276 return X86SelectSelect(I);
1277 case Instruction::Trunc:
1278 return X86SelectTrunc(I);
1279 case Instruction::FPExt:
1280 return X86SelectFPExt(I);
1281 case Instruction::FPTrunc:
1282 return X86SelectFPTrunc(I);
1288 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
1290 if (!isTypeLegal(C->getType(), TLI, VT))
1293 // Get opcode and regclass of the output for the given load instruction.
1295 const TargetRegisterClass *RC = NULL;
1296 switch (VT.getSimpleVT()) {
1297 default: return false;
1300 RC = X86::GR8RegisterClass;
1304 RC = X86::GR16RegisterClass;
1308 RC = X86::GR32RegisterClass;
1311 // Must be in x86-64 mode.
1313 RC = X86::GR64RegisterClass;
1316 if (Subtarget->hasSSE1()) {
1318 RC = X86::FR32RegisterClass;
1320 Opc = X86::LD_Fp32m;
1321 RC = X86::RFP32RegisterClass;
1325 if (Subtarget->hasSSE2()) {
1327 RC = X86::FR64RegisterClass;
1329 Opc = X86::LD_Fp64m;
1330 RC = X86::RFP64RegisterClass;
1334 // No f80 support yet.
1338 // Materialize addresses with LEA instructions.
1339 if (isa<GlobalValue>(C)) {
1341 if (X86SelectAddress(C, AM, false)) {
1342 if (TLI.getPointerTy() == MVT::i32)
1346 unsigned ResultReg = createResultReg(RC);
1347 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1353 // MachineConstantPool wants an explicit alignment.
1354 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
1356 // Alignment of vector types. FIXME!
1357 Align = TD.getABITypeSize(C->getType());
1358 Align = Log2_64(Align);
1361 // x86-32 PIC requires a PIC base register for constant pools.
1362 unsigned PICBase = 0;
1363 if (TM.getRelocationModel() == Reloc::PIC_ &&
1364 !Subtarget->is64Bit())
1365 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1367 // Create the load from the constant pool.
1368 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
1369 unsigned ResultReg = createResultReg(RC);
1370 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1376 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1377 // Fail on dynamic allocas. At this point, getRegForValue has already
1378 // checked its CSE maps, so if we're here trying to handle a dynamic
1379 // alloca, we're not going to succeed. X86SelectAddress has a
1380 // check for dynamic allocas, because it's called directly from
1381 // various places, but TargetMaterializeAlloca also needs a check
1382 // in order to avoid recursion between getRegForValue,
1383 // X86SelectAddrss, and TargetMaterializeAlloca.
1384 if (!StaticAllocaMap.count(C))
1388 if (!X86SelectAddress(C, AM, false))
1390 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1391 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1392 unsigned ResultReg = createResultReg(RC);
1393 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1398 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1399 MachineModuleInfo *mmi,
1400 DenseMap<const Value *, unsigned> &vm,
1401 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1402 DenseMap<const AllocaInst *, int> &am
1404 , SmallSet<Instruction*, 8> &cil
1407 return new X86FastISel(mf, mmi, vm, bm, am