LLVM API Documentation
00001 //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the X86-specific support for the FastISel class. Much 00011 // of the target-specific code is generated by tablegen in the file 00012 // X86GenFastISel.inc, which is #included here. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "X86.h" 00017 #include "X86InstrBuilder.h" 00018 #include "X86ISelLowering.h" 00019 #include "X86RegisterInfo.h" 00020 #include "X86Subtarget.h" 00021 #include "X86TargetMachine.h" 00022 #include "llvm/CallingConv.h" 00023 #include "llvm/DerivedTypes.h" 00024 #include "llvm/Instructions.h" 00025 #include "llvm/Intrinsics.h" 00026 #include "llvm/CodeGen/FastISel.h" 00027 #include "llvm/CodeGen/MachineConstantPool.h" 00028 #include "llvm/CodeGen/MachineFrameInfo.h" 00029 #include "llvm/CodeGen/MachineRegisterInfo.h" 00030 #include "llvm/Support/CallSite.h" 00031 #include "llvm/Support/GetElementPtrTypeIterator.h" 00032 00033 using namespace llvm; 00034 00035 class X86FastISel : public FastISel { 00036 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can 00037 /// make the right decision when generating code for different targets. 00038 const X86Subtarget *Subtarget; 00039 00040 /// StackPtr - Register used as the stack pointer. 00041 /// 00042 unsigned StackPtr; 00043 00044 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87 00045 /// floating point ops. 00046 /// When SSE is available, use it for f32 operations. 00047 /// When SSE2 is available, use it for f64 operations. 00048 bool X86ScalarSSEf64; 00049 bool X86ScalarSSEf32; 00050 00051 public: 00052 explicit X86FastISel(MachineFunction &mf, 00053 MachineModuleInfo *mmi, 00054 DwarfWriter *dw, 00055 DenseMap<const Value *, unsigned> &vm, 00056 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, 00057 DenseMap<const AllocaInst *, int> &am 00058 #ifndef NDEBUG 00059 , SmallSet<Instruction*, 8> &cil 00060 #endif 00061 ) 00062 : FastISel(mf, mmi, dw, vm, bm, am 00063 #ifndef NDEBUG 00064 , cil 00065 #endif 00066 ) { 00067 Subtarget = &TM.getSubtarget<X86Subtarget>(); 00068 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; 00069 X86ScalarSSEf64 = Subtarget->hasSSE2(); 00070 X86ScalarSSEf32 = Subtarget->hasSSE1(); 00071 } 00072 00073 virtual bool TargetSelectInstruction(Instruction *I); 00074 00075 #include "X86GenFastISel.inc" 00076 00077 private: 00078 bool X86FastEmitCompare(Value *LHS, Value *RHS, MVT VT); 00079 00080 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR); 00081 00082 bool X86FastEmitStore(MVT VT, Value *Val, 00083 const X86AddressMode &AM); 00084 bool X86FastEmitStore(MVT VT, unsigned Val, 00085 const X86AddressMode &AM); 00086 00087 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT, 00088 unsigned &ResultReg); 00089 00090 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall); 00091 00092 bool X86SelectLoad(Instruction *I); 00093 00094 bool X86SelectStore(Instruction *I); 00095 00096 bool X86SelectCmp(Instruction *I); 00097 00098 bool X86SelectZExt(Instruction *I); 00099 00100 bool X86SelectBranch(Instruction *I); 00101 00102 bool X86SelectShift(Instruction *I); 00103 00104 bool X86SelectSelect(Instruction *I); 00105 00106 bool X86SelectTrunc(Instruction *I); 00107 00108 bool X86SelectFPExt(Instruction *I); 00109 bool X86SelectFPTrunc(Instruction *I); 00110 00111 bool X86SelectExtractValue(Instruction *I); 00112 00113 bool X86VisitIntrinsicCall(CallInst &I, unsigned Intrinsic); 00114 bool X86SelectCall(Instruction *I); 00115 00116 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false); 00117 00118 const X86InstrInfo *getInstrInfo() const { 00119 return getTargetMachine()->getInstrInfo(); 00120 } 00121 const X86TargetMachine *getTargetMachine() const { 00122 return static_cast<const X86TargetMachine *>(&TM); 00123 } 00124 00125 unsigned TargetMaterializeConstant(Constant *C); 00126 00127 unsigned TargetMaterializeAlloca(AllocaInst *C); 00128 00129 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is 00130 /// computed in an SSE register, not on the X87 floating point stack. 00131 bool isScalarFPTypeInSSEReg(MVT VT) const { 00132 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2 00133 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1 00134 } 00135 00136 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false); 00137 }; 00138 00139 bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) { 00140 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true); 00141 if (VT == MVT::Other || !VT.isSimple()) 00142 // Unhandled type. Halt "fast" selection and bail. 00143 return false; 00144 00145 // For now, require SSE/SSE2 for performing floating-point operations, 00146 // since x87 requires additional work. 00147 if (VT == MVT::f64 && !X86ScalarSSEf64) 00148 return false; 00149 if (VT == MVT::f32 && !X86ScalarSSEf32) 00150 return false; 00151 // Similarly, no f80 support yet. 00152 if (VT == MVT::f80) 00153 return false; 00154 // We only handle legal types. For example, on x86-32 the instruction 00155 // selector contains all of the 64-bit instructions from x86-64, 00156 // under the assumption that i64 won't be used if the target doesn't 00157 // support it. 00158 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT); 00159 } 00160 00161 #include "X86GenCallingConv.inc" 00162 00163 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling 00164 /// convention. 00165 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) { 00166 if (Subtarget->is64Bit()) { 00167 if (Subtarget->isTargetWin64()) 00168 return CC_X86_Win64_C; 00169 else if (CC == CallingConv::Fast && isTaillCall) 00170 return CC_X86_64_TailCall; 00171 else 00172 return CC_X86_64_C; 00173 } 00174 00175 if (CC == CallingConv::X86_FastCall) 00176 return CC_X86_32_FastCall; 00177 else if (CC == CallingConv::Fast) 00178 return CC_X86_32_FastCC; 00179 else 00180 return CC_X86_32_C; 00181 } 00182 00183 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT. 00184 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV. 00185 /// Return true and the result register by reference if it is possible. 00186 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM, 00187 unsigned &ResultReg) { 00188 // Get opcode and regclass of the output for the given load instruction. 00189 unsigned Opc = 0; 00190 const TargetRegisterClass *RC = NULL; 00191 switch (VT.getSimpleVT()) { 00192 default: return false; 00193 case MVT::i8: 00194 Opc = X86::MOV8rm; 00195 RC = X86::GR8RegisterClass; 00196 break; 00197 case MVT::i16: 00198 Opc = X86::MOV16rm; 00199 RC = X86::GR16RegisterClass; 00200 break; 00201 case MVT::i32: 00202 Opc = X86::MOV32rm; 00203 RC = X86::GR32RegisterClass; 00204 break; 00205 case MVT::i64: 00206 // Must be in x86-64 mode. 00207 Opc = X86::MOV64rm; 00208 RC = X86::GR64RegisterClass; 00209 break; 00210 case MVT::f32: 00211 if (Subtarget->hasSSE1()) { 00212 Opc = X86::MOVSSrm; 00213 RC = X86::FR32RegisterClass; 00214 } else { 00215 Opc = X86::LD_Fp32m; 00216 RC = X86::RFP32RegisterClass; 00217 } 00218 break; 00219 case MVT::f64: 00220 if (Subtarget->hasSSE2()) { 00221 Opc = X86::MOVSDrm; 00222 RC = X86::FR64RegisterClass; 00223 } else { 00224 Opc = X86::LD_Fp64m; 00225 RC = X86::RFP64RegisterClass; 00226 } 00227 break; 00228 case MVT::f80: 00229 // No f80 support yet. 00230 return false; 00231 } 00232 00233 ResultReg = createResultReg(RC); 00234 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); 00235 return true; 00236 } 00237 00238 /// X86FastEmitStore - Emit a machine instruction to store a value Val of 00239 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr 00240 /// and a displacement offset, or a GlobalAddress, 00241 /// i.e. V. Return true if it is possible. 00242 bool 00243 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val, 00244 const X86AddressMode &AM) { 00245 // Get opcode and regclass of the output for the given store instruction. 00246 unsigned Opc = 0; 00247 switch (VT.getSimpleVT()) { 00248 case MVT::f80: // No f80 support yet. 00249 default: return false; 00250 case MVT::i8: Opc = X86::MOV8mr; break; 00251 case MVT::i16: Opc = X86::MOV16mr; break; 00252 case MVT::i32: Opc = X86::MOV32mr; break; 00253 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode. 00254 case MVT::f32: 00255 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m; 00256 break; 00257 case MVT::f64: 00258 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m; 00259 break; 00260 } 00261 00262 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val); 00263 return true; 00264 } 00265 00266 bool X86FastISel::X86FastEmitStore(MVT VT, Value *Val, 00267 const X86AddressMode &AM) { 00268 // Handle 'null' like i32/i64 0. 00269 if (isa<ConstantPointerNull>(Val)) 00270 Val = Constant::getNullValue(TD.getIntPtrType()); 00271 00272 // If this is a store of a simple constant, fold the constant into the store. 00273 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { 00274 unsigned Opc = 0; 00275 switch (VT.getSimpleVT()) { 00276 default: break; 00277 case MVT::i8: Opc = X86::MOV8mi; break; 00278 case MVT::i16: Opc = X86::MOV16mi; break; 00279 case MVT::i32: Opc = X86::MOV32mi; break; 00280 case MVT::i64: 00281 // Must be a 32-bit sign extended value. 00282 if ((int)CI->getSExtValue() == CI->getSExtValue()) 00283 Opc = X86::MOV64mi32; 00284 break; 00285 } 00286 00287 if (Opc) { 00288 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addImm(CI->getSExtValue()); 00289 return true; 00290 } 00291 } 00292 00293 unsigned ValReg = getRegForValue(Val); 00294 if (ValReg == 0) 00295 return false; 00296 00297 return X86FastEmitStore(VT, ValReg, AM); 00298 } 00299 00300 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of 00301 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g. 00302 /// ISD::SIGN_EXTEND). 00303 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, 00304 unsigned Src, MVT SrcVT, 00305 unsigned &ResultReg) { 00306 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src); 00307 00308 if (RR != 0) { 00309 ResultReg = RR; 00310 return true; 00311 } else 00312 return false; 00313 } 00314 00315 /// X86SelectAddress - Attempt to fill in an address from the given value. 00316 /// 00317 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) { 00318 User *U; 00319 unsigned Opcode = Instruction::UserOp1; 00320 if (Instruction *I = dyn_cast<Instruction>(V)) { 00321 Opcode = I->getOpcode(); 00322 U = I; 00323 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) { 00324 Opcode = C->getOpcode(); 00325 U = C; 00326 } 00327 00328 switch (Opcode) { 00329 default: break; 00330 case Instruction::BitCast: 00331 // Look past bitcasts. 00332 return X86SelectAddress(U->getOperand(0), AM, isCall); 00333 00334 case Instruction::IntToPtr: 00335 // Look past no-op inttoptrs. 00336 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 00337 return X86SelectAddress(U->getOperand(0), AM, isCall); 00338 break; 00339 00340 case Instruction::PtrToInt: 00341 // Look past no-op ptrtoints. 00342 if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) 00343 return X86SelectAddress(U->getOperand(0), AM, isCall); 00344 break; 00345 00346 case Instruction::Alloca: { 00347 if (isCall) break; 00348 // Do static allocas. 00349 const AllocaInst *A = cast<AllocaInst>(V); 00350 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A); 00351 if (SI != StaticAllocaMap.end()) { 00352 AM.BaseType = X86AddressMode::FrameIndexBase; 00353 AM.Base.FrameIndex = SI->second; 00354 return true; 00355 } 00356 break; 00357 } 00358 00359 case Instruction::Add: { 00360 if (isCall) break; 00361 // Adds of constants are common and easy enough. 00362 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { 00363 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue(); 00364 // They have to fit in the 32-bit signed displacement field though. 00365 if (isInt32(Disp)) { 00366 AM.Disp = (uint32_t)Disp; 00367 return X86SelectAddress(U->getOperand(0), AM, isCall); 00368 } 00369 } 00370 break; 00371 } 00372 00373 case Instruction::GetElementPtr: { 00374 if (isCall) break; 00375 // Pattern-match simple GEPs. 00376 uint64_t Disp = (int32_t)AM.Disp; 00377 unsigned IndexReg = AM.IndexReg; 00378 unsigned Scale = AM.Scale; 00379 gep_type_iterator GTI = gep_type_begin(U); 00380 // Iterate through the indices, folding what we can. Constants can be 00381 // folded, and one dynamic index can be handled, if the scale is supported. 00382 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); 00383 i != e; ++i, ++GTI) { 00384 Value *Op = *i; 00385 if (const StructType *STy = dyn_cast<StructType>(*GTI)) { 00386 const StructLayout *SL = TD.getStructLayout(STy); 00387 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 00388 Disp += SL->getElementOffset(Idx); 00389 } else { 00390 uint64_t S = TD.getTypePaddedSize(GTI.getIndexedType()); 00391 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 00392 // Constant-offset addressing. 00393 Disp += CI->getSExtValue() * S; 00394 } else if (IndexReg == 0 && 00395 (!AM.GV || 00396 !getTargetMachine()->symbolicAddressesAreRIPRel()) && 00397 (S == 1 || S == 2 || S == 4 || S == 8)) { 00398 // Scaled-index addressing. 00399 Scale = S; 00400 IndexReg = getRegForGEPIndex(Op); 00401 if (IndexReg == 0) 00402 return false; 00403 } else 00404 // Unsupported. 00405 goto unsupported_gep; 00406 } 00407 } 00408 // Check for displacement overflow. 00409 if (!isInt32(Disp)) 00410 break; 00411 // Ok, the GEP indices were covered by constant-offset and scaled-index 00412 // addressing. Update the address state and move on to examining the base. 00413 AM.IndexReg = IndexReg; 00414 AM.Scale = Scale; 00415 AM.Disp = (uint32_t)Disp; 00416 return X86SelectAddress(U->getOperand(0), AM, isCall); 00417 unsupported_gep: 00418 // Ok, the GEP indices weren't all covered. 00419 break; 00420 } 00421 } 00422 00423 // Handle constant address. 00424 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 00425 // Can't handle alternate code models yet. 00426 if (TM.getCodeModel() != CodeModel::Default && 00427 TM.getCodeModel() != CodeModel::Small) 00428 return false; 00429 00430 // RIP-relative addresses can't have additional register operands. 00431 if (getTargetMachine()->symbolicAddressesAreRIPRel() && 00432 (AM.Base.Reg != 0 || AM.IndexReg != 0)) 00433 return false; 00434 00435 // Set up the basic address. 00436 AM.GV = GV; 00437 if (!isCall && 00438 TM.getRelocationModel() == Reloc::PIC_ && 00439 !Subtarget->is64Bit()) 00440 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF); 00441 00442 // Emit an extra load if the ABI requires it. 00443 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) { 00444 // Check to see if we've already materialized this 00445 // value in a register in this block. 00446 if (unsigned Reg = LocalValueMap[V]) { 00447 AM.Base.Reg = Reg; 00448 AM.GV = 0; 00449 return true; 00450 } 00451 // Issue load from stub if necessary. 00452 unsigned Opc = 0; 00453 const TargetRegisterClass *RC = NULL; 00454 if (TLI.getPointerTy() == MVT::i32) { 00455 Opc = X86::MOV32rm; 00456 RC = X86::GR32RegisterClass; 00457 } else { 00458 Opc = X86::MOV64rm; 00459 RC = X86::GR64RegisterClass; 00460 } 00461 00462 X86AddressMode StubAM; 00463 StubAM.Base.Reg = AM.Base.Reg; 00464 StubAM.GV = AM.GV; 00465 unsigned ResultReg = createResultReg(RC); 00466 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM); 00467 00468 // Now construct the final address. Note that the Disp, Scale, 00469 // and Index values may already be set here. 00470 AM.Base.Reg = ResultReg; 00471 AM.GV = 0; 00472 00473 // Prevent loading GV stub multiple times in same MBB. 00474 LocalValueMap[V] = AM.Base.Reg; 00475 } 00476 return true; 00477 } 00478 00479 // If all else fails, try to materialize the value in a register. 00480 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) { 00481 if (AM.Base.Reg == 0) { 00482 AM.Base.Reg = getRegForValue(V); 00483 return AM.Base.Reg != 0; 00484 } 00485 if (AM.IndexReg == 0) { 00486 assert(AM.Scale == 1 && "Scale with no index!"); 00487 AM.IndexReg = getRegForValue(V); 00488 return AM.IndexReg != 0; 00489 } 00490 } 00491 00492 return false; 00493 } 00494 00495 /// X86SelectStore - Select and emit code to implement store instructions. 00496 bool X86FastISel::X86SelectStore(Instruction* I) { 00497 MVT VT; 00498 if (!isTypeLegal(I->getOperand(0)->getType(), VT)) 00499 return false; 00500 00501 X86AddressMode AM; 00502 if (!X86SelectAddress(I->getOperand(1), AM, false)) 00503 return false; 00504 00505 return X86FastEmitStore(VT, I->getOperand(0), AM); 00506 } 00507 00508 /// X86SelectLoad - Select and emit code to implement load instructions. 00509 /// 00510 bool X86FastISel::X86SelectLoad(Instruction *I) { 00511 MVT VT; 00512 if (!isTypeLegal(I->getType(), VT)) 00513 return false; 00514 00515 X86AddressMode AM; 00516 if (!X86SelectAddress(I->getOperand(0), AM, false)) 00517 return false; 00518 00519 unsigned ResultReg = 0; 00520 if (X86FastEmitLoad(VT, AM, ResultReg)) { 00521 UpdateValueMap(I, ResultReg); 00522 return true; 00523 } 00524 return false; 00525 } 00526 00527 static unsigned X86ChooseCmpOpcode(MVT VT) { 00528 switch (VT.getSimpleVT()) { 00529 default: return 0; 00530 case MVT::i8: return X86::CMP8rr; 00531 case MVT::i16: return X86::CMP16rr; 00532 case MVT::i32: return X86::CMP32rr; 00533 case MVT::i64: return X86::CMP64rr; 00534 case MVT::f32: return X86::UCOMISSrr; 00535 case MVT::f64: return X86::UCOMISDrr; 00536 } 00537 } 00538 00539 /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS 00540 /// of the comparison, return an opcode that works for the compare (e.g. 00541 /// CMP32ri) otherwise return 0. 00542 static unsigned X86ChooseCmpImmediateOpcode(MVT VT, ConstantInt *RHSC) { 00543 switch (VT.getSimpleVT()) { 00544 // Otherwise, we can't fold the immediate into this comparison. 00545 default: return 0; 00546 case MVT::i8: return X86::CMP8ri; 00547 case MVT::i16: return X86::CMP16ri; 00548 case MVT::i32: return X86::CMP32ri; 00549 case MVT::i64: 00550 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext 00551 // field. 00552 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue()) 00553 return X86::CMP64ri32; 00554 return 0; 00555 } 00556 } 00557 00558 bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) { 00559 unsigned Op0Reg = getRegForValue(Op0); 00560 if (Op0Reg == 0) return false; 00561 00562 // Handle 'null' like i32/i64 0. 00563 if (isa<ConstantPointerNull>(Op1)) 00564 Op1 = Constant::getNullValue(TD.getIntPtrType()); 00565 00566 // We have two options: compare with register or immediate. If the RHS of 00567 // the compare is an immediate that we can fold into this compare, use 00568 // CMPri, otherwise use CMPrr. 00569 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { 00570 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) { 00571 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg) 00572 .addImm(Op1C->getSExtValue()); 00573 return true; 00574 } 00575 } 00576 00577 unsigned CompareOpc = X86ChooseCmpOpcode(VT); 00578 if (CompareOpc == 0) return false; 00579 00580 unsigned Op1Reg = getRegForValue(Op1); 00581 if (Op1Reg == 0) return false; 00582 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg); 00583 00584 return true; 00585 } 00586 00587 bool X86FastISel::X86SelectCmp(Instruction *I) { 00588 CmpInst *CI = cast<CmpInst>(I); 00589 00590 MVT VT; 00591 if (!isTypeLegal(I->getOperand(0)->getType(), VT)) 00592 return false; 00593 00594 unsigned ResultReg = createResultReg(&X86::GR8RegClass); 00595 unsigned SetCCOpc; 00596 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0. 00597 switch (CI->getPredicate()) { 00598 case CmpInst::FCMP_OEQ: { 00599 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT)) 00600 return false; 00601 00602 unsigned EReg = createResultReg(&X86::GR8RegClass); 00603 unsigned NPReg = createResultReg(&X86::GR8RegClass); 00604 BuildMI(MBB, TII.get(X86::SETEr), EReg); 00605 BuildMI(MBB, TII.get(X86::SETNPr), NPReg); 00606 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg); 00607 UpdateValueMap(I, ResultReg); 00608 return true; 00609 } 00610 case CmpInst::FCMP_UNE: { 00611 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT)) 00612 return false; 00613 00614 unsigned NEReg = createResultReg(&X86::GR8RegClass); 00615 unsigned PReg = createResultReg(&X86::GR8RegClass); 00616 BuildMI(MBB, TII.get(X86::SETNEr), NEReg); 00617 BuildMI(MBB, TII.get(X86::SETPr), PReg); 00618 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg); 00619 UpdateValueMap(I, ResultReg); 00620 return true; 00621 } 00622 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break; 00623 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break; 00624 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break; 00625 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break; 00626 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break; 00627 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break; 00628 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break; 00629 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break; 00630 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break; 00631 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break; 00632 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break; 00633 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break; 00634 00635 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break; 00636 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break; 00637 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break; 00638 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break; 00639 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break; 00640 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break; 00641 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break; 00642 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break; 00643 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break; 00644 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break; 00645 default: 00646 return false; 00647 } 00648 00649 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); 00650 if (SwapArgs) 00651 std::swap(Op0, Op1); 00652 00653 // Emit a compare of Op0/Op1. 00654 if (!X86FastEmitCompare(Op0, Op1, VT)) 00655 return false; 00656 00657 BuildMI(MBB, TII.get(SetCCOpc), ResultReg); 00658 UpdateValueMap(I, ResultReg); 00659 return true; 00660 } 00661 00662 bool X86FastISel::X86SelectZExt(Instruction *I) { 00663 // Special-case hack: The only i1 values we know how to produce currently 00664 // set the upper bits of an i8 value to zero. 00665 if (I->getType() == Type::Int8Ty && 00666 I->getOperand(0)->getType() == Type::Int1Ty) { 00667 unsigned ResultReg = getRegForValue(I->getOperand(0)); 00668 if (ResultReg == 0) return false; 00669 UpdateValueMap(I, ResultReg); 00670 return true; 00671 } 00672 00673 return false; 00674 } 00675 00676 00677 bool X86FastISel::X86SelectBranch(Instruction *I) { 00678 // Unconditional branches are selected by tablegen-generated code. 00679 // Handle a conditional branch. 00680 BranchInst *BI = cast<BranchInst>(I); 00681 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)]; 00682 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)]; 00683 00684 // Fold the common case of a conditional branch with a comparison. 00685 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 00686 if (CI->hasOneUse()) { 00687 MVT VT = TLI.getValueType(CI->getOperand(0)->getType()); 00688 00689 // Try to take advantage of fallthrough opportunities. 00690 CmpInst::Predicate Predicate = CI->getPredicate(); 00691 if (MBB->isLayoutSuccessor(TrueMBB)) { 00692 std::swap(TrueMBB, FalseMBB); 00693 Predicate = CmpInst::getInversePredicate(Predicate); 00694 } 00695 00696 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0. 00697 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA" 00698 00699 switch (Predicate) { 00700 case CmpInst::FCMP_OEQ: 00701 std::swap(TrueMBB, FalseMBB); 00702 Predicate = CmpInst::FCMP_UNE; 00703 // FALL THROUGH 00704 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break; 00705 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break; 00706 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break; 00707 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break; 00708 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break; 00709 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break; 00710 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break; 00711 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break; 00712 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break; 00713 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break; 00714 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break; 00715 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break; 00716 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break; 00717 00718 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break; 00719 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break; 00720 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break; 00721 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break; 00722 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break; 00723 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break; 00724 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break; 00725 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break; 00726 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break; 00727 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break; 00728 default: 00729 return false; 00730 } 00731 00732 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); 00733 if (SwapArgs) 00734 std::swap(Op0, Op1); 00735 00736 // Emit a compare of the LHS and RHS, setting the flags. 00737 if (!X86FastEmitCompare(Op0, Op1, VT)) 00738 return false; 00739 00740 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB); 00741 00742 if (Predicate == CmpInst::FCMP_UNE) { 00743 // X86 requires a second branch to handle UNE (and OEQ, 00744 // which is mapped to UNE above). 00745 BuildMI(MBB, TII.get(X86::JP)).addMBB(TrueMBB); 00746 } 00747 00748 FastEmitBranch(FalseMBB); 00749 MBB->addSuccessor(TrueMBB); 00750 return true; 00751 } 00752 } else if (ExtractValueInst *EI = 00753 dyn_cast<ExtractValueInst>(BI->getCondition())) { 00754 // Check to see if the branch instruction is from an "arithmetic with 00755 // overflow" intrinsic. The main way these intrinsics are used is: 00756 // 00757 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2) 00758 // %sum = extractvalue { i32, i1 } %t, 0 00759 // %obit = extractvalue { i32, i1 } %t, 1 00760 // br i1 %obit, label %overflow, label %normal 00761 // 00762 // The %sum and %obit are converted in an ADD and a SETO/SETB before 00763 // reaching the branch. Therefore, we search backwards through the MBB 00764 // looking for the SETO/SETB instruction. If an instruction modifies the 00765 // EFLAGS register before we reach the SETO/SETB instruction, then we can't 00766 // convert the branch into a JO/JB instruction. 00767 00768 Value *Agg = EI->getAggregateOperand(); 00769 00770 if (CallInst *CI = dyn_cast<CallInst>(Agg)) { 00771 Function *F = CI->getCalledFunction(); 00772 00773 if (F && F->isDeclaration()) { 00774 switch (F->getIntrinsicID()) { 00775 default: break; 00776 case Intrinsic::sadd_with_overflow: 00777 case Intrinsic::uadd_with_overflow: { 00778 const MachineInstr *SetMI = 0; 00779 unsigned Reg = lookUpRegForValue(EI); 00780 00781 for (MachineBasicBlock::const_reverse_iterator 00782 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) { 00783 const MachineInstr &MI = *RI; 00784 00785 if (MI.modifiesRegister(Reg)) { 00786 unsigned Src, Dst, SrcSR, DstSR; 00787 00788 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) { 00789 Reg = Src; 00790 continue; 00791 } 00792 00793 SetMI = &MI; 00794 break; 00795 } 00796 00797 const TargetInstrDesc &TID = MI.getDesc(); 00798 const unsigned *ImpDefs = TID.getImplicitDefs(); 00799 00800 if (TID.hasUnmodeledSideEffects()) break; 00801 00802 bool ModifiesEFlags = false; 00803 00804 if (ImpDefs) { 00805 for (unsigned u = 0; ImpDefs[u]; ++u) 00806 if (ImpDefs[u] == X86::EFLAGS) { 00807 ModifiesEFlags = true; 00808 break; 00809 } 00810 } 00811 00812 if (ModifiesEFlags) break; 00813 } 00814 00815 if (SetMI) { 00816 unsigned OpCode = SetMI->getOpcode(); 00817 00818 if (OpCode == X86::SETOr || OpCode == X86::SETBr) { 00819 BuildMI(MBB, TII.get((OpCode == X86::SETOr) ? 00820 X86::JO : X86::JB)).addMBB(TrueMBB); 00821 FastEmitBranch(FalseMBB); 00822 MBB->addSuccessor(TrueMBB); 00823 return true; 00824 } 00825 } 00826 } 00827 } 00828 } 00829 } 00830 } 00831 00832 // Otherwise do a clumsy setcc and re-test it. 00833 unsigned OpReg = getRegForValue(BI->getCondition()); 00834 if (OpReg == 0) return false; 00835 00836 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg); 00837 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB); 00838 FastEmitBranch(FalseMBB); 00839 MBB->addSuccessor(TrueMBB); 00840 return true; 00841 } 00842 00843 bool X86FastISel::X86SelectShift(Instruction *I) { 00844 unsigned CReg = 0, OpReg = 0, OpImm = 0; 00845 const TargetRegisterClass *RC = NULL; 00846 if (I->getType() == Type::Int8Ty) { 00847 CReg = X86::CL; 00848 RC = &X86::GR8RegClass; 00849 switch (I->getOpcode()) { 00850 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break; 00851 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break; 00852 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break; 00853 default: return false; 00854 } 00855 } else if (I->getType() == Type::Int16Ty) { 00856 CReg = X86::CX; 00857 RC = &X86::GR16RegClass; 00858 switch (I->getOpcode()) { 00859 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break; 00860 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break; 00861 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break; 00862 default: return false; 00863 } 00864 } else if (I->getType() == Type::Int32Ty) { 00865 CReg = X86::ECX; 00866 RC = &X86::GR32RegClass; 00867 switch (I->getOpcode()) { 00868 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break; 00869 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break; 00870 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break; 00871 default: return false; 00872 } 00873 } else if (I->getType() == Type::Int64Ty) { 00874 CReg = X86::RCX; 00875 RC = &X86::GR64RegClass; 00876 switch (I->getOpcode()) { 00877 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break; 00878 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break; 00879 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break; 00880 default: return false; 00881 } 00882 } else { 00883 return false; 00884 } 00885 00886 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true); 00887 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT)) 00888 return false; 00889 00890 unsigned Op0Reg = getRegForValue(I->getOperand(0)); 00891 if (Op0Reg == 0) return false; 00892 00893 // Fold immediate in shl(x,3). 00894 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 00895 unsigned ResultReg = createResultReg(RC); 00896 BuildMI(MBB, TII.get(OpImm), 00897 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff); 00898 UpdateValueMap(I, ResultReg); 00899 return true; 00900 } 00901 00902 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 00903 if (Op1Reg == 0) return false; 00904 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC); 00905 00906 // The shift instruction uses X86::CL. If we defined a super-register 00907 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what 00908 // we're doing here. 00909 if (CReg != X86::CL) 00910 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL) 00911 .addReg(CReg).addImm(X86::SUBREG_8BIT); 00912 00913 unsigned ResultReg = createResultReg(RC); 00914 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg); 00915 UpdateValueMap(I, ResultReg); 00916 return true; 00917 } 00918 00919 bool X86FastISel::X86SelectSelect(Instruction *I) { 00920 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true); 00921 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT)) 00922 return false; 00923 00924 unsigned Opc = 0; 00925 const TargetRegisterClass *RC = NULL; 00926 if (VT.getSimpleVT() == MVT::i16) { 00927 Opc = X86::CMOVE16rr; 00928 RC = &X86::GR16RegClass; 00929 } else if (VT.getSimpleVT() == MVT::i32) { 00930 Opc = X86::CMOVE32rr; 00931 RC = &X86::GR32RegClass; 00932 } else if (VT.getSimpleVT() == MVT::i64) { 00933 Opc = X86::CMOVE64rr; 00934 RC = &X86::GR64RegClass; 00935 } else { 00936 return false; 00937 } 00938 00939 unsigned Op0Reg = getRegForValue(I->getOperand(0)); 00940 if (Op0Reg == 0) return false; 00941 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 00942 if (Op1Reg == 0) return false; 00943 unsigned Op2Reg = getRegForValue(I->getOperand(2)); 00944 if (Op2Reg == 0) return false; 00945 00946 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg); 00947 unsigned ResultReg = createResultReg(RC); 00948 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg); 00949 UpdateValueMap(I, ResultReg); 00950 return true; 00951 } 00952 00953 bool X86FastISel::X86SelectFPExt(Instruction *I) { 00954 // fpext from float to double. 00955 if (Subtarget->hasSSE2() && I->getType() == Type::DoubleTy) { 00956 Value *V = I->getOperand(0); 00957 if (V->getType() == Type::FloatTy) { 00958 unsigned OpReg = getRegForValue(V); 00959 if (OpReg == 0) return false; 00960 unsigned ResultReg = createResultReg(X86::FR64RegisterClass); 00961 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg); 00962 UpdateValueMap(I, ResultReg); 00963 return true; 00964 } 00965 } 00966 00967 return false; 00968 } 00969 00970 bool X86FastISel::X86SelectFPTrunc(Instruction *I) { 00971 if (Subtarget->hasSSE2()) { 00972 if (I->getType() == Type::FloatTy) { 00973 Value *V = I->getOperand(0); 00974 if (V->getType() == Type::DoubleTy) { 00975 unsigned OpReg = getRegForValue(V); 00976 if (OpReg == 0) return false; 00977 unsigned ResultReg = createResultReg(X86::FR32RegisterClass); 00978 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg); 00979 UpdateValueMap(I, ResultReg); 00980 return true; 00981 } 00982 } 00983 } 00984 00985 return false; 00986 } 00987 00988 bool X86FastISel::X86SelectTrunc(Instruction *I) { 00989 if (Subtarget->is64Bit()) 00990 // All other cases should be handled by the tblgen generated code. 00991 return false; 00992 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 00993 MVT DstVT = TLI.getValueType(I->getType()); 00994 if (DstVT != MVT::i8) 00995 // All other cases should be handled by the tblgen generated code. 00996 return false; 00997 if (SrcVT != MVT::i16 && SrcVT != MVT::i32) 00998 // All other cases should be handled by the tblgen generated code. 00999 return false; 01000 01001 unsigned InputReg = getRegForValue(I->getOperand(0)); 01002 if (!InputReg) 01003 // Unhandled operand. Halt "fast" selection and bail. 01004 return false; 01005 01006 // First issue a copy to GR16_ or GR32_. 01007 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_; 01008 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) 01009 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass; 01010 unsigned CopyReg = createResultReg(CopyRC); 01011 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg); 01012 01013 // Then issue an extract_subreg. 01014 unsigned ResultReg = FastEmitInst_extractsubreg(DstVT.getSimpleVT(), 01015 CopyReg, X86::SUBREG_8BIT); 01016 if (!ResultReg) 01017 return false; 01018 01019 UpdateValueMap(I, ResultReg); 01020 return true; 01021 } 01022 01023 bool X86FastISel::X86SelectExtractValue(Instruction *I) { 01024 ExtractValueInst *EI = cast<ExtractValueInst>(I); 01025 Value *Agg = EI->getAggregateOperand(); 01026 01027 if (CallInst *CI = dyn_cast<CallInst>(Agg)) { 01028 Function *F = CI->getCalledFunction(); 01029 01030 if (F && F->isDeclaration()) { 01031 switch (F->getIntrinsicID()) { 01032 default: break; 01033 case Intrinsic::sadd_with_overflow: 01034 case Intrinsic::uadd_with_overflow: 01035 // Cheat a little. We know that the registers for "add" and "seto" are 01036 // allocated sequentially. However, we only keep track of the register 01037 // for "add" in the value map. Use extractvalue's index to get the 01038 // correct register for "seto". 01039 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin()); 01040 return true; 01041 } 01042 } 01043 } 01044 01045 return false; 01046 } 01047 01048 bool X86FastISel::X86VisitIntrinsicCall(CallInst &I, unsigned Intrinsic) { 01049 // FIXME: Handle more intrinsics. 01050 switch (Intrinsic) { 01051 default: return false; 01052 case Intrinsic::sadd_with_overflow: 01053 case Intrinsic::uadd_with_overflow: { 01054 // Replace "add with overflow" intrinsics with an "add" instruction followed 01055 // by a seto/setc instruction. Later on, when the "extractvalue" 01056 // instructions are encountered, we use the fact that two registers were 01057 // created sequentially to get the correct registers for the "sum" and the 01058 // "overflow bit". 01059 MVT VT; 01060 const Function *Callee = I.getCalledFunction(); 01061 const Type *RetTy = 01062 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0)); 01063 01064 if (!isTypeLegal(RetTy, VT)) 01065 return false; 01066 01067 Value *Op1 = I.getOperand(1); 01068 Value *Op2 = I.getOperand(2); 01069 unsigned Reg1 = getRegForValue(Op1); 01070 unsigned Reg2 = getRegForValue(Op2); 01071 01072 if (Reg1 == 0 || Reg2 == 0) 01073 // FIXME: Handle values *not* in registers. 01074 return false; 01075 01076 unsigned OpC = 0; 01077 01078 if (VT == MVT::i32) 01079 OpC = X86::ADD32rr; 01080 else if (VT == MVT::i64) 01081 OpC = X86::ADD64rr; 01082 else 01083 return false; 01084 01085 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); 01086 BuildMI(MBB, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2); 01087 UpdateValueMap(&I, ResultReg); 01088 01089 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8)); 01090 BuildMI(MBB, TII.get((Intrinsic == Intrinsic::sadd_with_overflow) ? 01091 X86::SETOr : X86::SETBr), ResultReg); 01092 return true; 01093 } 01094 } 01095 } 01096 01097 bool X86FastISel::X86SelectCall(Instruction *I) { 01098 CallInst *CI = cast<CallInst>(I); 01099 Value *Callee = I->getOperand(0); 01100 01101 // Can't handle inline asm yet. 01102 if (isa<InlineAsm>(Callee)) 01103 return false; 01104 01105 // Handle intrinsic calls. 01106 if (Function *F = CI->getCalledFunction()) 01107 if (F->isDeclaration()) 01108 if (unsigned IID = F->getIntrinsicID()) 01109 return X86VisitIntrinsicCall(*CI, IID); 01110 01111 // Handle only C and fastcc calling conventions for now. 01112 CallSite CS(CI); 01113 unsigned CC = CS.getCallingConv(); 01114 if (CC != CallingConv::C && 01115 CC != CallingConv::Fast && 01116 CC != CallingConv::X86_FastCall) 01117 return false; 01118 01119 // Let SDISel handle vararg functions. 01120 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); 01121 const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); 01122 if (FTy->isVarArg()) 01123 return false; 01124 01125 // Handle *simple* calls for now. 01126 const Type *RetTy = CS.getType(); 01127 MVT RetVT; 01128 if (RetTy == Type::VoidTy) 01129 RetVT = MVT::isVoid; 01130 else if (!isTypeLegal(RetTy, RetVT, true)) 01131 return false; 01132 01133 // Materialize callee address in a register. FIXME: GV address can be 01134 // handled with a CALLpcrel32 instead. 01135 X86AddressMode CalleeAM; 01136 if (!X86SelectAddress(Callee, CalleeAM, true)) 01137 return false; 01138 unsigned CalleeOp = 0; 01139 GlobalValue *GV = 0; 01140 if (CalleeAM.Base.Reg != 0) { 01141 assert(CalleeAM.GV == 0); 01142 CalleeOp = CalleeAM.Base.Reg; 01143 } else if (CalleeAM.GV != 0) { 01144 assert(CalleeAM.GV != 0); 01145 GV = CalleeAM.GV; 01146 } else 01147 return false; 01148 01149 // Allow calls which produce i1 results. 01150 bool AndToI1 = false; 01151 if (RetVT == MVT::i1) { 01152 RetVT = MVT::i8; 01153 AndToI1 = true; 01154 } 01155 01156 // Deal with call operands first. 01157 SmallVector<Value*, 8> ArgVals; 01158 SmallVector<unsigned, 8> Args; 01159 SmallVector<MVT, 8> ArgVTs; 01160 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 01161 Args.reserve(CS.arg_size()); 01162 ArgVals.reserve(CS.arg_size()); 01163 ArgVTs.reserve(CS.arg_size()); 01164 ArgFlags.reserve(CS.arg_size()); 01165 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); 01166 i != e; ++i) { 01167 unsigned Arg = getRegForValue(*i); 01168 if (Arg == 0) 01169 return false; 01170 ISD::ArgFlagsTy Flags; 01171 unsigned AttrInd = i - CS.arg_begin() + 1; 01172 if (CS.paramHasAttr(AttrInd, Attribute::SExt)) 01173 Flags.setSExt(); 01174 if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) 01175 Flags.setZExt(); 01176 01177 // FIXME: Only handle *easy* calls for now. 01178 if (CS.paramHasAttr(AttrInd, Attribute::InReg) || 01179 CS.paramHasAttr(AttrInd, Attribute::StructRet) || 01180 CS.paramHasAttr(AttrInd, Attribute::Nest) || 01181 CS.paramHasAttr(AttrInd, Attribute::ByVal)) 01182 return false; 01183 01184 const Type *ArgTy = (*i)->getType(); 01185 MVT ArgVT; 01186 if (!isTypeLegal(ArgTy, ArgVT)) 01187 return false; 01188 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); 01189 Flags.setOrigAlign(OriginalAlignment); 01190 01191 Args.push_back(Arg); 01192 ArgVals.push_back(*i); 01193 ArgVTs.push_back(ArgVT); 01194 ArgFlags.push_back(Flags); 01195 } 01196 01197 // Analyze operands of the call, assigning locations to each operand. 01198 SmallVector<CCValAssign, 16> ArgLocs; 01199 CCState CCInfo(CC, false, TM, ArgLocs); 01200 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC)); 01201 01202 // Get a count of how many bytes are to be pushed on the stack. 01203 unsigned NumBytes = CCInfo.getNextStackOffset(); 01204 01205 // Issue CALLSEQ_START 01206 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode(); 01207 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes); 01208 01209 // Process argument: walk the register/memloc assignments, inserting 01210 // copies / loads. 01211 SmallVector<unsigned, 4> RegArgs; 01212 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 01213 CCValAssign &VA = ArgLocs[i]; 01214 unsigned Arg = Args[VA.getValNo()]; 01215 MVT ArgVT = ArgVTs[VA.getValNo()]; 01216 01217 // Promote the value if needed. 01218 switch (VA.getLocInfo()) { 01219 default: assert(0 && "Unknown loc info!"); 01220 case CCValAssign::Full: break; 01221 case CCValAssign::SExt: { 01222 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), 01223 Arg, ArgVT, Arg); 01224 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted; 01225 Emitted = true; 01226 ArgVT = VA.getLocVT(); 01227 break; 01228 } 01229 case CCValAssign::ZExt: { 01230 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), 01231 Arg, ArgVT, Arg); 01232 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted; 01233 Emitted = true; 01234 ArgVT = VA.getLocVT(); 01235 break; 01236 } 01237 case CCValAssign::AExt: { 01238 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), 01239 Arg, ArgVT, Arg); 01240 if (!Emitted) 01241 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), 01242 Arg, ArgVT, Arg); 01243 if (!Emitted) 01244 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), 01245 Arg, ArgVT, Arg); 01246 01247 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted; 01248 ArgVT = VA.getLocVT(); 01249 break; 01250 } 01251 } 01252 01253 if (VA.isRegLoc()) { 01254 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT); 01255 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(), 01256 Arg, RC, RC); 01257 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted; 01258 Emitted = true; 01259 RegArgs.push_back(VA.getLocReg()); 01260 } else { 01261 unsigned LocMemOffset = VA.getLocMemOffset(); 01262 X86AddressMode AM; 01263 AM.Base.Reg = StackPtr; 01264 AM.Disp = LocMemOffset; 01265 Value *ArgVal = ArgVals[VA.getValNo()]; 01266 01267 // If this is a really simple value, emit this with the Value* version of 01268 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it 01269 // can cause us to reevaluate the argument. 01270 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) 01271 X86FastEmitStore(ArgVT, ArgVal, AM); 01272 else 01273 X86FastEmitStore(ArgVT, Arg, AM); 01274 } 01275 } 01276 01277 // ELF / PIC requires GOT in the EBX register before function calls via PLT 01278 // GOT pointer. 01279 if (!Subtarget->is64Bit() && 01280 TM.getRelocationModel() == Reloc::PIC_ && 01281 Subtarget->isPICStyleGOT()) { 01282 TargetRegisterClass *RC = X86::GR32RegisterClass; 01283 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF); 01284 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC); 01285 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted; 01286 Emitted = true; 01287 } 01288 01289 // Issue the call. 01290 unsigned CallOpc = CalleeOp 01291 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r) 01292 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32); 01293 MachineInstrBuilder MIB = CalleeOp 01294 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp) 01295 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV); 01296 01297 // Add an implicit use GOT pointer in EBX. 01298 if (!Subtarget->is64Bit() && 01299 TM.getRelocationModel() == Reloc::PIC_ && 01300 Subtarget->isPICStyleGOT()) 01301 MIB.addReg(X86::EBX); 01302 01303 // Add implicit physical register uses to the call. 01304 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) 01305 MIB.addReg(RegArgs[i]); 01306 01307 // Issue CALLSEQ_END 01308 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode(); 01309 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0); 01310 01311 // Now handle call return value (if any). 01312 if (RetVT.getSimpleVT() != MVT::isVoid) { 01313 SmallVector<CCValAssign, 16> RVLocs; 01314 CCState CCInfo(CC, false, TM, RVLocs); 01315 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86); 01316 01317 // Copy all of the result registers out of their specified physreg. 01318 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!"); 01319 MVT CopyVT = RVLocs[0].getValVT(); 01320 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); 01321 TargetRegisterClass *SrcRC = DstRC; 01322 01323 // If this is a call to a function that returns an fp value on the x87 fp 01324 // stack, but where we prefer to use the value in xmm registers, copy it 01325 // out as F80 and use a truncate to move it from fp stack reg to xmm reg. 01326 if ((RVLocs[0].getLocReg() == X86::ST0 || 01327 RVLocs[0].getLocReg() == X86::ST1) && 01328 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) { 01329 CopyVT = MVT::f80; 01330 SrcRC = X86::RSTRegisterClass; 01331 DstRC = X86::RFP80RegisterClass; 01332 } 01333 01334 unsigned ResultReg = createResultReg(DstRC); 01335 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 01336 RVLocs[0].getLocReg(), DstRC, SrcRC); 01337 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted; 01338 Emitted = true; 01339 if (CopyVT != RVLocs[0].getValVT()) { 01340 // Round the F80 the right size, which also moves to the appropriate xmm 01341 // register. This is accomplished by storing the F80 value in memory and 01342 // then loading it back. Ewww... 01343 MVT ResVT = RVLocs[0].getValVT(); 01344 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64; 01345 unsigned MemSize = ResVT.getSizeInBits()/8; 01346 int FI = MFI.CreateStackObject(MemSize, MemSize); 01347 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg); 01348 DstRC = ResVT == MVT::f32 01349 ? X86::FR32RegisterClass : X86::FR64RegisterClass; 01350 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm; 01351 ResultReg = createResultReg(DstRC); 01352 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI); 01353 } 01354 01355 if (AndToI1) { 01356 // Mask out all but lowest bit for some call which produces an i1. 01357 unsigned AndResult = createResultReg(X86::GR8RegisterClass); 01358 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1); 01359 ResultReg = AndResult; 01360 } 01361 01362 UpdateValueMap(I, ResultReg); 01363 } 01364 01365 return true; 01366 } 01367 01368 01369 bool 01370 X86FastISel::TargetSelectInstruction(Instruction *I) { 01371 switch (I->getOpcode()) { 01372 default: break; 01373 case Instruction::Load: 01374 return X86SelectLoad(I); 01375 case Instruction::Store: 01376 return X86SelectStore(I); 01377 case Instruction::ICmp: 01378 case Instruction::FCmp: 01379 return X86SelectCmp(I); 01380 case Instruction::ZExt: 01381 return X86SelectZExt(I); 01382 case Instruction::Br: 01383 return X86SelectBranch(I); 01384 case Instruction::Call: 01385 return X86SelectCall(I); 01386 case Instruction::LShr: 01387 case Instruction::AShr: 01388 case Instruction::Shl: 01389 return X86SelectShift(I); 01390 case Instruction::Select: 01391 return X86SelectSelect(I); 01392 case Instruction::Trunc: 01393 return X86SelectTrunc(I); 01394 case Instruction::FPExt: 01395 return X86SelectFPExt(I); 01396 case Instruction::FPTrunc: 01397 return X86SelectFPTrunc(I); 01398 case Instruction::ExtractValue: 01399 return X86SelectExtractValue(I); 01400 } 01401 01402 return false; 01403 } 01404 01405 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) { 01406 MVT VT; 01407 if (!isTypeLegal(C->getType(), VT)) 01408 return false; 01409 01410 // Get opcode and regclass of the output for the given load instruction. 01411 unsigned Opc = 0; 01412 const TargetRegisterClass *RC = NULL; 01413 switch (VT.getSimpleVT()) { 01414 default: return false; 01415 case MVT::i8: 01416 Opc = X86::MOV8rm; 01417 RC = X86::GR8RegisterClass; 01418 break; 01419 case MVT::i16: 01420 Opc = X86::MOV16rm; 01421 RC = X86::GR16RegisterClass; 01422 break; 01423 case MVT::i32: 01424 Opc = X86::MOV32rm; 01425 RC = X86::GR32RegisterClass; 01426 break; 01427 case MVT::i64: 01428 // Must be in x86-64 mode. 01429 Opc = X86::MOV64rm; 01430 RC = X86::GR64RegisterClass; 01431 break; 01432 case MVT::f32: 01433 if (Subtarget->hasSSE1()) { 01434 Opc = X86::MOVSSrm; 01435 RC = X86::FR32RegisterClass; 01436 } else { 01437 Opc = X86::LD_Fp32m; 01438 RC = X86::RFP32RegisterClass; 01439 } 01440 break; 01441 case MVT::f64: 01442 if (Subtarget->hasSSE2()) { 01443 Opc = X86::MOVSDrm; 01444 RC = X86::FR64RegisterClass; 01445 } else { 01446 Opc = X86::LD_Fp64m; 01447 RC = X86::RFP64RegisterClass; 01448 } 01449 break; 01450 case MVT::f80: 01451 // No f80 support yet. 01452 return false; 01453 } 01454 01455 // Materialize addresses with LEA instructions. 01456 if (isa<GlobalValue>(C)) { 01457 X86AddressMode AM; 01458 if (X86SelectAddress(C, AM, false)) { 01459 if (TLI.getPointerTy() == MVT::i32) 01460 Opc = X86::LEA32r; 01461 else 01462 Opc = X86::LEA64r; 01463 unsigned ResultReg = createResultReg(RC); 01464 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); 01465 return ResultReg; 01466 } 01467 return 0; 01468 } 01469 01470 // MachineConstantPool wants an explicit alignment. 01471 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType()); 01472 if (Align == 0) { 01473 // Alignment of vector types. FIXME! 01474 Align = TD.getTypePaddedSize(C->getType()); 01475 Align = Log2_64(Align); 01476 } 01477 01478 // x86-32 PIC requires a PIC base register for constant pools. 01479 unsigned PICBase = 0; 01480 if (TM.getRelocationModel() == Reloc::PIC_ && 01481 !Subtarget->is64Bit()) 01482 PICBase = getInstrInfo()->getGlobalBaseReg(&MF); 01483 01484 // Create the load from the constant pool. 01485 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align); 01486 unsigned ResultReg = createResultReg(RC); 01487 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset, 01488 PICBase); 01489 01490 return ResultReg; 01491 } 01492 01493 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) { 01494 // Fail on dynamic allocas. At this point, getRegForValue has already 01495 // checked its CSE maps, so if we're here trying to handle a dynamic 01496 // alloca, we're not going to succeed. X86SelectAddress has a 01497 // check for dynamic allocas, because it's called directly from 01498 // various places, but TargetMaterializeAlloca also needs a check 01499 // in order to avoid recursion between getRegForValue, 01500 // X86SelectAddrss, and TargetMaterializeAlloca. 01501 if (!StaticAllocaMap.count(C)) 01502 return 0; 01503 01504 X86AddressMode AM; 01505 if (!X86SelectAddress(C, AM, false)) 01506 return 0; 01507 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r; 01508 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy()); 01509 unsigned ResultReg = createResultReg(RC); 01510 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); 01511 return ResultReg; 01512 } 01513 01514 namespace llvm { 01515 llvm::FastISel *X86::createFastISel(MachineFunction &mf, 01516 MachineModuleInfo *mmi, 01517 DwarfWriter *dw, 01518 DenseMap<const Value *, unsigned> &vm, 01519 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, 01520 DenseMap<const AllocaInst *, int> &am 01521 #ifndef NDEBUG 01522 , SmallSet<Instruction*, 8> &cil 01523 #endif 01524 ) { 01525 return new X86FastISel(mf, mmi, dw, vm, bm, am 01526 #ifndef NDEBUG 01527 , cil 01528 #endif 01529 ); 01530 } 01531 }