LLVM API Documentation
00001 ///===-- FastISel.cpp - Implementation of the FastISel class --------------===// 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 contains the implementation of the FastISel class. 00011 // 00012 // "Fast" instruction selection is designed to emit very poor code quickly. 00013 // Also, it is not designed to be able to do much lowering, so most illegal 00014 // types (e.g. i64 on 32-bit targets) and operations are not supported. It is 00015 // also not intended to be able to do much optimization, except in a few cases 00016 // where doing optimizations reduces overall compile time. For example, folding 00017 // constants into immediate fields is often done, because it's cheap and it 00018 // reduces the number of instructions later phases have to examine. 00019 // 00020 // "Fast" instruction selection is able to fail gracefully and transfer 00021 // control to the SelectionDAG selector for operations that it doesn't 00022 // support. In many cases, this allows us to avoid duplicating a lot of 00023 // the complicated lowering logic that SelectionDAG currently has. 00024 // 00025 // The intended use for "fast" instruction selection is "-O0" mode 00026 // compilation, where the quality of the generated code is irrelevant when 00027 // weighed against the speed at which the code can be generated. Also, 00028 // at -O0, the LLVM optimizers are not running, and this makes the 00029 // compile time of codegen a much higher portion of the overall compile 00030 // time. Despite its limitations, "fast" instruction selection is able to 00031 // handle enough code on its own to provide noticeable overall speedups 00032 // in -O0 compiles. 00033 // 00034 // Basic operations are supported in a target-independent way, by reading 00035 // the same instruction descriptions that the SelectionDAG selector reads, 00036 // and identifying simple arithmetic operations that can be directly selected 00037 // from simple operators. More complicated operations currently require 00038 // target-specific code. 00039 // 00040 //===----------------------------------------------------------------------===// 00041 00042 #include "llvm/Function.h" 00043 #include "llvm/GlobalVariable.h" 00044 #include "llvm/Instructions.h" 00045 #include "llvm/IntrinsicInst.h" 00046 #include "llvm/CodeGen/FastISel.h" 00047 #include "llvm/CodeGen/MachineInstrBuilder.h" 00048 #include "llvm/CodeGen/MachineModuleInfo.h" 00049 #include "llvm/CodeGen/MachineRegisterInfo.h" 00050 #include "llvm/CodeGen/DwarfWriter.h" 00051 #include "llvm/Analysis/DebugInfo.h" 00052 #include "llvm/Target/TargetData.h" 00053 #include "llvm/Target/TargetInstrInfo.h" 00054 #include "llvm/Target/TargetLowering.h" 00055 #include "llvm/Target/TargetMachine.h" 00056 #include "SelectionDAGBuild.h" 00057 using namespace llvm; 00058 00059 unsigned FastISel::getRegForValue(Value *V) { 00060 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT(); 00061 00062 // Ignore illegal types. We must do this before looking up the value 00063 // in ValueMap because Arguments are given virtual registers regardless 00064 // of whether FastISel can handle them. 00065 if (!TLI.isTypeLegal(VT)) { 00066 // Promote MVT::i1 to a legal type though, because it's common and easy. 00067 if (VT == MVT::i1) 00068 VT = TLI.getTypeToTransformTo(VT).getSimpleVT(); 00069 else 00070 return 0; 00071 } 00072 00073 // Look up the value to see if we already have a register for it. We 00074 // cache values defined by Instructions across blocks, and other values 00075 // only locally. This is because Instructions already have the SSA 00076 // def-dominatess-use requirement enforced. 00077 if (ValueMap.count(V)) 00078 return ValueMap[V]; 00079 unsigned Reg = LocalValueMap[V]; 00080 if (Reg != 0) 00081 return Reg; 00082 00083 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 00084 if (CI->getValue().getActiveBits() <= 64) 00085 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); 00086 } else if (isa<AllocaInst>(V)) { 00087 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); 00088 } else if (isa<ConstantPointerNull>(V)) { 00089 // Translate this as an integer zero so that it can be 00090 // local-CSE'd with actual integer zeros. 00091 Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType())); 00092 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 00093 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); 00094 00095 if (!Reg) { 00096 const APFloat &Flt = CF->getValueAPF(); 00097 MVT IntVT = TLI.getPointerTy(); 00098 00099 uint64_t x[2]; 00100 uint32_t IntBitWidth = IntVT.getSizeInBits(); 00101 bool isExact; 00102 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, 00103 APFloat::rmTowardZero, &isExact); 00104 if (isExact) { 00105 APInt IntVal(IntBitWidth, 2, x); 00106 00107 unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal)); 00108 if (IntegerReg != 0) 00109 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg); 00110 } 00111 } 00112 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 00113 if (!SelectOperator(CE, CE->getOpcode())) return 0; 00114 Reg = LocalValueMap[CE]; 00115 } else if (isa<UndefValue>(V)) { 00116 Reg = createResultReg(TLI.getRegClassFor(VT)); 00117 BuildMI(MBB, DL, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg); 00118 } 00119 00120 // If target-independent code couldn't handle the value, give target-specific 00121 // code a try. 00122 if (!Reg && isa<Constant>(V)) 00123 Reg = TargetMaterializeConstant(cast<Constant>(V)); 00124 00125 // Don't cache constant materializations in the general ValueMap. 00126 // To do so would require tracking what uses they dominate. 00127 if (Reg != 0) 00128 LocalValueMap[V] = Reg; 00129 return Reg; 00130 } 00131 00132 unsigned FastISel::lookUpRegForValue(Value *V) { 00133 // Look up the value to see if we already have a register for it. We 00134 // cache values defined by Instructions across blocks, and other values 00135 // only locally. This is because Instructions already have the SSA 00136 // def-dominatess-use requirement enforced. 00137 if (ValueMap.count(V)) 00138 return ValueMap[V]; 00139 return LocalValueMap[V]; 00140 } 00141 00142 /// UpdateValueMap - Update the value map to include the new mapping for this 00143 /// instruction, or insert an extra copy to get the result in a previous 00144 /// determined register. 00145 /// NOTE: This is only necessary because we might select a block that uses 00146 /// a value before we select the block that defines the value. It might be 00147 /// possible to fix this by selecting blocks in reverse postorder. 00148 void FastISel::UpdateValueMap(Value* I, unsigned Reg) { 00149 if (!isa<Instruction>(I)) { 00150 LocalValueMap[I] = Reg; 00151 return; 00152 } 00153 if (!ValueMap.count(I)) 00154 ValueMap[I] = Reg; 00155 else 00156 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I], 00157 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg)); 00158 } 00159 00160 unsigned FastISel::getRegForGEPIndex(Value *Idx) { 00161 unsigned IdxN = getRegForValue(Idx); 00162 if (IdxN == 0) 00163 // Unhandled operand. Halt "fast" selection and bail. 00164 return 0; 00165 00166 // If the index is smaller or larger than intptr_t, truncate or extend it. 00167 MVT PtrVT = TLI.getPointerTy(); 00168 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false); 00169 if (IdxVT.bitsLT(PtrVT)) 00170 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(), 00171 ISD::SIGN_EXTEND, IdxN); 00172 else if (IdxVT.bitsGT(PtrVT)) 00173 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(), 00174 ISD::TRUNCATE, IdxN); 00175 return IdxN; 00176 } 00177 00178 /// SelectBinaryOp - Select and emit code for a binary operator instruction, 00179 /// which has an opcode which directly corresponds to the given ISD opcode. 00180 /// 00181 bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) { 00182 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); 00183 if (VT == MVT::Other || !VT.isSimple()) 00184 // Unhandled type. Halt "fast" selection and bail. 00185 return false; 00186 00187 // We only handle legal types. For example, on x86-32 the instruction 00188 // selector contains all of the 64-bit instructions from x86-64, 00189 // under the assumption that i64 won't be used if the target doesn't 00190 // support it. 00191 if (!TLI.isTypeLegal(VT)) { 00192 // MVT::i1 is special. Allow AND, OR, or XOR because they 00193 // don't require additional zeroing, which makes them easy. 00194 if (VT == MVT::i1 && 00195 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || 00196 ISDOpcode == ISD::XOR)) 00197 VT = TLI.getTypeToTransformTo(VT); 00198 else 00199 return false; 00200 } 00201 00202 unsigned Op0 = getRegForValue(I->getOperand(0)); 00203 if (Op0 == 0) 00204 // Unhandled operand. Halt "fast" selection and bail. 00205 return false; 00206 00207 // Check if the second operand is a constant and handle it appropriately. 00208 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 00209 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(), 00210 ISDOpcode, Op0, CI->getZExtValue()); 00211 if (ResultReg != 0) { 00212 // We successfully emitted code for the given LLVM Instruction. 00213 UpdateValueMap(I, ResultReg); 00214 return true; 00215 } 00216 } 00217 00218 // Check if the second operand is a constant float. 00219 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { 00220 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), 00221 ISDOpcode, Op0, CF); 00222 if (ResultReg != 0) { 00223 // We successfully emitted code for the given LLVM Instruction. 00224 UpdateValueMap(I, ResultReg); 00225 return true; 00226 } 00227 } 00228 00229 unsigned Op1 = getRegForValue(I->getOperand(1)); 00230 if (Op1 == 0) 00231 // Unhandled operand. Halt "fast" selection and bail. 00232 return false; 00233 00234 // Now we have both operands in registers. Emit the instruction. 00235 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), 00236 ISDOpcode, Op0, Op1); 00237 if (ResultReg == 0) 00238 // Target-specific code wasn't able to find a machine opcode for 00239 // the given ISD opcode and type. Halt "fast" selection and bail. 00240 return false; 00241 00242 // We successfully emitted code for the given LLVM Instruction. 00243 UpdateValueMap(I, ResultReg); 00244 return true; 00245 } 00246 00247 bool FastISel::SelectGetElementPtr(User *I) { 00248 unsigned N = getRegForValue(I->getOperand(0)); 00249 if (N == 0) 00250 // Unhandled operand. Halt "fast" selection and bail. 00251 return false; 00252 00253 const Type *Ty = I->getOperand(0)->getType(); 00254 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT(); 00255 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); 00256 OI != E; ++OI) { 00257 Value *Idx = *OI; 00258 if (const StructType *StTy = dyn_cast<StructType>(Ty)) { 00259 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); 00260 if (Field) { 00261 // N = N + Offset 00262 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); 00263 // FIXME: This can be optimized by combining the add with a 00264 // subsequent one. 00265 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); 00266 if (N == 0) 00267 // Unhandled operand. Halt "fast" selection and bail. 00268 return false; 00269 } 00270 Ty = StTy->getElementType(Field); 00271 } else { 00272 Ty = cast<SequentialType>(Ty)->getElementType(); 00273 00274 // If this is a constant subscript, handle it quickly. 00275 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { 00276 if (CI->getZExtValue() == 0) continue; 00277 uint64_t Offs = 00278 TD.getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); 00279 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); 00280 if (N == 0) 00281 // Unhandled operand. Halt "fast" selection and bail. 00282 return false; 00283 continue; 00284 } 00285 00286 // N = N + Idx * ElementSize; 00287 uint64_t ElementSize = TD.getTypePaddedSize(Ty); 00288 unsigned IdxN = getRegForGEPIndex(Idx); 00289 if (IdxN == 0) 00290 // Unhandled operand. Halt "fast" selection and bail. 00291 return false; 00292 00293 if (ElementSize != 1) { 00294 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); 00295 if (IdxN == 0) 00296 // Unhandled operand. Halt "fast" selection and bail. 00297 return false; 00298 } 00299 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN); 00300 if (N == 0) 00301 // Unhandled operand. Halt "fast" selection and bail. 00302 return false; 00303 } 00304 } 00305 00306 // We successfully emitted code for the given LLVM Instruction. 00307 UpdateValueMap(I, N); 00308 return true; 00309 } 00310 00311 bool FastISel::SelectCall(User *I) { 00312 Function *F = cast<CallInst>(I)->getCalledFunction(); 00313 if (!F) return false; 00314 00315 unsigned IID = F->getIntrinsicID(); 00316 switch (IID) { 00317 default: break; 00318 case Intrinsic::dbg_stoppoint: { 00319 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I); 00320 if (DW && DW->ValidDebugInfo(SPI->getContext())) { 00321 DICompileUnit CU(cast<GlobalVariable>(SPI->getContext())); 00322 unsigned SrcFile = DW->RecordSource(CU.getDirectory(), 00323 CU.getFilename()); 00324 unsigned Line = SPI->getLine(); 00325 unsigned Col = SPI->getColumn(); 00326 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile); 00327 unsigned Idx = MF.getOrCreateDebugLocID(SrcFile, Line, Col); 00328 setCurDebugLoc(DebugLoc::get(Idx)); 00329 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL); 00330 BuildMI(MBB, DL, II).addImm(ID); 00331 } 00332 return true; 00333 } 00334 case Intrinsic::dbg_region_start: { 00335 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I); 00336 if (DW && DW->ValidDebugInfo(RSI->getContext())) { 00337 unsigned ID = 00338 DW->RecordRegionStart(cast<GlobalVariable>(RSI->getContext())); 00339 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL); 00340 BuildMI(MBB, DL, II).addImm(ID); 00341 } 00342 return true; 00343 } 00344 case Intrinsic::dbg_region_end: { 00345 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I); 00346 if (DW && DW->ValidDebugInfo(REI->getContext())) { 00347 unsigned ID = 00348 DW->RecordRegionEnd(cast<GlobalVariable>(REI->getContext())); 00349 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL); 00350 BuildMI(MBB, DL, II).addImm(ID); 00351 } 00352 return true; 00353 } 00354 case Intrinsic::dbg_func_start: { 00355 if (!DW) return true; 00356 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I); 00357 Value *SP = FSI->getSubprogram(); 00358 00359 if (DW->ValidDebugInfo(SP)) { 00360 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is what 00361 // (most?) gdb expects. 00362 DISubprogram Subprogram(cast<GlobalVariable>(SP)); 00363 DICompileUnit CompileUnit = Subprogram.getCompileUnit(); 00364 unsigned SrcFile = DW->RecordSource(CompileUnit.getDirectory(), 00365 CompileUnit.getFilename()); 00366 00367 // Record the source line but does not create a label for the normal 00368 // function start. It will be emitted at asm emission time. However, 00369 // create a label if this is a beginning of inlined function. 00370 unsigned Line = Subprogram.getLineNumber(); 00371 unsigned LabelID = DW->RecordSourceLine(Line, 0, SrcFile); 00372 setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(SrcFile, Line, 0))); 00373 00374 if (DW->getRecordSourceLineCount() != 1) { 00375 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL); 00376 BuildMI(MBB, DL, II).addImm(LabelID); 00377 } 00378 } 00379 00380 return true; 00381 } 00382 case Intrinsic::dbg_declare: { 00383 DbgDeclareInst *DI = cast<DbgDeclareInst>(I); 00384 Value *Variable = DI->getVariable(); 00385 if (DW && DW->ValidDebugInfo(Variable)) { 00386 // Determine the address of the declared object. 00387 Value *Address = DI->getAddress(); 00388 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) 00389 Address = BCI->getOperand(0); 00390 AllocaInst *AI = dyn_cast<AllocaInst>(Address); 00391 // Don't handle byval struct arguments, for example. 00392 if (!AI) break; 00393 DenseMap<const AllocaInst*, int>::iterator SI = 00394 StaticAllocaMap.find(AI); 00395 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!"); 00396 int FI = SI->second; 00397 00398 // Determine the debug globalvariable. 00399 GlobalValue *GV = cast<GlobalVariable>(Variable); 00400 00401 // Build the DECLARE instruction. 00402 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE); 00403 BuildMI(MBB, DL, II).addFrameIndex(FI).addGlobalAddress(GV); 00404 } 00405 return true; 00406 } 00407 case Intrinsic::eh_exception: { 00408 MVT VT = TLI.getValueType(I->getType()); 00409 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) { 00410 default: break; 00411 case TargetLowering::Expand: { 00412 if (!MBB->isLandingPad()) { 00413 // FIXME: Mark exception register as live in. Hack for PR1508. 00414 unsigned Reg = TLI.getExceptionAddressRegister(); 00415 if (Reg) MBB->addLiveIn(Reg); 00416 } 00417 unsigned Reg = TLI.getExceptionAddressRegister(); 00418 const TargetRegisterClass *RC = TLI.getRegClassFor(VT); 00419 unsigned ResultReg = createResultReg(RC); 00420 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00421 Reg, RC, RC); 00422 assert(InsertedCopy && "Can't copy address registers!"); 00423 InsertedCopy = InsertedCopy; 00424 UpdateValueMap(I, ResultReg); 00425 return true; 00426 } 00427 } 00428 break; 00429 } 00430 case Intrinsic::eh_selector_i32: 00431 case Intrinsic::eh_selector_i64: { 00432 MVT VT = TLI.getValueType(I->getType()); 00433 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) { 00434 default: break; 00435 case TargetLowering::Expand: { 00436 MVT VT = (IID == Intrinsic::eh_selector_i32 ? 00437 MVT::i32 : MVT::i64); 00438 00439 if (MMI) { 00440 if (MBB->isLandingPad()) 00441 AddCatchInfo(*cast<CallInst>(I), MMI, MBB); 00442 else { 00443 #ifndef NDEBUG 00444 CatchInfoLost.insert(cast<CallInst>(I)); 00445 #endif 00446 // FIXME: Mark exception selector register as live in. Hack for PR1508. 00447 unsigned Reg = TLI.getExceptionSelectorRegister(); 00448 if (Reg) MBB->addLiveIn(Reg); 00449 } 00450 00451 unsigned Reg = TLI.getExceptionSelectorRegister(); 00452 const TargetRegisterClass *RC = TLI.getRegClassFor(VT); 00453 unsigned ResultReg = createResultReg(RC); 00454 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00455 Reg, RC, RC); 00456 assert(InsertedCopy && "Can't copy address registers!"); 00457 InsertedCopy = InsertedCopy; 00458 UpdateValueMap(I, ResultReg); 00459 } else { 00460 unsigned ResultReg = 00461 getRegForValue(Constant::getNullValue(I->getType())); 00462 UpdateValueMap(I, ResultReg); 00463 } 00464 return true; 00465 } 00466 } 00467 break; 00468 } 00469 } 00470 return false; 00471 } 00472 00473 bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) { 00474 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 00475 MVT DstVT = TLI.getValueType(I->getType()); 00476 00477 if (SrcVT == MVT::Other || !SrcVT.isSimple() || 00478 DstVT == MVT::Other || !DstVT.isSimple() || 00479 !TLI.isTypeLegal(DstVT)) 00480 // Unhandled type. Halt "fast" selection and bail. 00481 return false; 00482 00483 // Check if the source operand is legal. Or as a special case, 00484 // it may be i1 if we're doing zero-extension because that's 00485 // trivially easy and somewhat common. 00486 if (!TLI.isTypeLegal(SrcVT)) { 00487 if (SrcVT == MVT::i1 && Opcode == ISD::ZERO_EXTEND) 00488 SrcVT = TLI.getTypeToTransformTo(SrcVT); 00489 else 00490 // Unhandled type. Halt "fast" selection and bail. 00491 return false; 00492 } 00493 00494 unsigned InputReg = getRegForValue(I->getOperand(0)); 00495 if (!InputReg) 00496 // Unhandled operand. Halt "fast" selection and bail. 00497 return false; 00498 00499 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), 00500 DstVT.getSimpleVT(), 00501 Opcode, 00502 InputReg); 00503 if (!ResultReg) 00504 return false; 00505 00506 UpdateValueMap(I, ResultReg); 00507 return true; 00508 } 00509 00510 bool FastISel::SelectBitCast(User *I) { 00511 // If the bitcast doesn't change the type, just use the operand value. 00512 if (I->getType() == I->getOperand(0)->getType()) { 00513 unsigned Reg = getRegForValue(I->getOperand(0)); 00514 if (Reg == 0) 00515 return false; 00516 UpdateValueMap(I, Reg); 00517 return true; 00518 } 00519 00520 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators. 00521 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 00522 MVT DstVT = TLI.getValueType(I->getType()); 00523 00524 if (SrcVT == MVT::Other || !SrcVT.isSimple() || 00525 DstVT == MVT::Other || !DstVT.isSimple() || 00526 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) 00527 // Unhandled type. Halt "fast" selection and bail. 00528 return false; 00529 00530 unsigned Op0 = getRegForValue(I->getOperand(0)); 00531 if (Op0 == 0) 00532 // Unhandled operand. Halt "fast" selection and bail. 00533 return false; 00534 00535 // First, try to perform the bitcast by inserting a reg-reg copy. 00536 unsigned ResultReg = 0; 00537 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { 00538 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); 00539 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); 00540 ResultReg = createResultReg(DstClass); 00541 00542 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00543 Op0, DstClass, SrcClass); 00544 if (!InsertedCopy) 00545 ResultReg = 0; 00546 } 00547 00548 // If the reg-reg copy failed, select a BIT_CONVERT opcode. 00549 if (!ResultReg) 00550 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), 00551 ISD::BIT_CONVERT, Op0); 00552 00553 if (!ResultReg) 00554 return false; 00555 00556 UpdateValueMap(I, ResultReg); 00557 return true; 00558 } 00559 00560 bool 00561 FastISel::SelectInstruction(Instruction *I) { 00562 return SelectOperator(I, I->getOpcode()); 00563 } 00564 00565 /// FastEmitBranch - Emit an unconditional branch to the given block, 00566 /// unless it is the immediate (fall-through) successor, and update 00567 /// the CFG. 00568 void 00569 FastISel::FastEmitBranch(MachineBasicBlock *MSucc) { 00570 MachineFunction::iterator NextMBB = 00571 next(MachineFunction::iterator(MBB)); 00572 00573 if (MBB->isLayoutSuccessor(MSucc)) { 00574 // The unconditional fall-through case, which needs no instructions. 00575 } else { 00576 // The unconditional branch case. 00577 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); 00578 } 00579 MBB->addSuccessor(MSucc); 00580 } 00581 00582 bool 00583 FastISel::SelectOperator(User *I, unsigned Opcode) { 00584 switch (Opcode) { 00585 case Instruction::Add: { 00586 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD; 00587 return SelectBinaryOp(I, Opc); 00588 } 00589 case Instruction::Sub: { 00590 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB; 00591 return SelectBinaryOp(I, Opc); 00592 } 00593 case Instruction::Mul: { 00594 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL; 00595 return SelectBinaryOp(I, Opc); 00596 } 00597 case Instruction::SDiv: 00598 return SelectBinaryOp(I, ISD::SDIV); 00599 case Instruction::UDiv: 00600 return SelectBinaryOp(I, ISD::UDIV); 00601 case Instruction::FDiv: 00602 return SelectBinaryOp(I, ISD::FDIV); 00603 case Instruction::SRem: 00604 return SelectBinaryOp(I, ISD::SREM); 00605 case Instruction::URem: 00606 return SelectBinaryOp(I, ISD::UREM); 00607 case Instruction::FRem: 00608 return SelectBinaryOp(I, ISD::FREM); 00609 case Instruction::Shl: 00610 return SelectBinaryOp(I, ISD::SHL); 00611 case Instruction::LShr: 00612 return SelectBinaryOp(I, ISD::SRL); 00613 case Instruction::AShr: 00614 return SelectBinaryOp(I, ISD::SRA); 00615 case Instruction::And: 00616 return SelectBinaryOp(I, ISD::AND); 00617 case Instruction::Or: 00618 return SelectBinaryOp(I, ISD::OR); 00619 case Instruction::Xor: 00620 return SelectBinaryOp(I, ISD::XOR); 00621 00622 case Instruction::GetElementPtr: 00623 return SelectGetElementPtr(I); 00624 00625 case Instruction::Br: { 00626 BranchInst *BI = cast<BranchInst>(I); 00627 00628 if (BI->isUnconditional()) { 00629 BasicBlock *LLVMSucc = BI->getSuccessor(0); 00630 MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; 00631 FastEmitBranch(MSucc); 00632 return true; 00633 } 00634 00635 // Conditional branches are not handed yet. 00636 // Halt "fast" selection and bail. 00637 return false; 00638 } 00639 00640 case Instruction::Unreachable: 00641 // Nothing to emit. 00642 return true; 00643 00644 case Instruction::PHI: 00645 // PHI nodes are already emitted. 00646 return true; 00647 00648 case Instruction::Alloca: 00649 // FunctionLowering has the static-sized case covered. 00650 if (StaticAllocaMap.count(cast<AllocaInst>(I))) 00651 return true; 00652 00653 // Dynamic-sized alloca is not handled yet. 00654 return false; 00655 00656 case Instruction::Call: 00657 return SelectCall(I); 00658 00659 case Instruction::BitCast: 00660 return SelectBitCast(I); 00661 00662 case Instruction::FPToSI: 00663 return SelectCast(I, ISD::FP_TO_SINT); 00664 case Instruction::ZExt: 00665 return SelectCast(I, ISD::ZERO_EXTEND); 00666 case Instruction::SExt: 00667 return SelectCast(I, ISD::SIGN_EXTEND); 00668 case Instruction::Trunc: 00669 return SelectCast(I, ISD::TRUNCATE); 00670 case Instruction::SIToFP: 00671 return SelectCast(I, ISD::SINT_TO_FP); 00672 00673 case Instruction::IntToPtr: // Deliberate fall-through. 00674 case Instruction::PtrToInt: { 00675 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 00676 MVT DstVT = TLI.getValueType(I->getType()); 00677 if (DstVT.bitsGT(SrcVT)) 00678 return SelectCast(I, ISD::ZERO_EXTEND); 00679 if (DstVT.bitsLT(SrcVT)) 00680 return SelectCast(I, ISD::TRUNCATE); 00681 unsigned Reg = getRegForValue(I->getOperand(0)); 00682 if (Reg == 0) return false; 00683 UpdateValueMap(I, Reg); 00684 return true; 00685 } 00686 00687 default: 00688 // Unhandled instruction. Halt "fast" selection and bail. 00689 return false; 00690 } 00691 } 00692 00693 FastISel::FastISel(MachineFunction &mf, 00694 MachineModuleInfo *mmi, 00695 DwarfWriter *dw, 00696 DenseMap<const Value *, unsigned> &vm, 00697 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, 00698 DenseMap<const AllocaInst *, int> &am 00699 #ifndef NDEBUG 00700 , SmallSet<Instruction*, 8> &cil 00701 #endif 00702 ) 00703 : MBB(0), 00704 ValueMap(vm), 00705 MBBMap(bm), 00706 StaticAllocaMap(am), 00707 #ifndef NDEBUG 00708 CatchInfoLost(cil), 00709 #endif 00710 MF(mf), 00711 MMI(mmi), 00712 DW(dw), 00713 MRI(MF.getRegInfo()), 00714 MFI(*MF.getFrameInfo()), 00715 MCP(*MF.getConstantPool()), 00716 TM(MF.getTarget()), 00717 TD(*TM.getTargetData()), 00718 TII(*TM.getInstrInfo()), 00719 TLI(*TM.getTargetLowering()) { 00720 } 00721 00722 FastISel::~FastISel() {} 00723 00724 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, 00725 ISD::NodeType) { 00726 return 0; 00727 } 00728 00729 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType, 00730 ISD::NodeType, unsigned /*Op0*/) { 00731 return 0; 00732 } 00733 00734 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, 00735 ISD::NodeType, unsigned /*Op0*/, 00736 unsigned /*Op0*/) { 00737 return 0; 00738 } 00739 00740 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType, 00741 ISD::NodeType, uint64_t /*Imm*/) { 00742 return 0; 00743 } 00744 00745 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType, 00746 ISD::NodeType, ConstantFP * /*FPImm*/) { 00747 return 0; 00748 } 00749 00750 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType, 00751 ISD::NodeType, unsigned /*Op0*/, 00752 uint64_t /*Imm*/) { 00753 return 0; 00754 } 00755 00756 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType, 00757 ISD::NodeType, unsigned /*Op0*/, 00758 ConstantFP * /*FPImm*/) { 00759 return 0; 00760 } 00761 00762 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType, 00763 ISD::NodeType, 00764 unsigned /*Op0*/, unsigned /*Op1*/, 00765 uint64_t /*Imm*/) { 00766 return 0; 00767 } 00768 00769 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries 00770 /// to emit an instruction with an immediate operand using FastEmit_ri. 00771 /// If that fails, it materializes the immediate into a register and try 00772 /// FastEmit_rr instead. 00773 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode, 00774 unsigned Op0, uint64_t Imm, 00775 MVT::SimpleValueType ImmType) { 00776 // First check if immediate type is legal. If not, we can't use the ri form. 00777 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm); 00778 if (ResultReg != 0) 00779 return ResultReg; 00780 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); 00781 if (MaterialReg == 0) 00782 return 0; 00783 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); 00784 } 00785 00786 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries 00787 /// to emit an instruction with a floating-point immediate operand using 00788 /// FastEmit_rf. If that fails, it materializes the immediate into a register 00789 /// and try FastEmit_rr instead. 00790 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode, 00791 unsigned Op0, ConstantFP *FPImm, 00792 MVT::SimpleValueType ImmType) { 00793 // First check if immediate type is legal. If not, we can't use the rf form. 00794 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm); 00795 if (ResultReg != 0) 00796 return ResultReg; 00797 00798 // Materialize the constant in a register. 00799 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm); 00800 if (MaterialReg == 0) { 00801 // If the target doesn't have a way to directly enter a floating-point 00802 // value into a register, use an alternate approach. 00803 // TODO: The current approach only supports floating-point constants 00804 // that can be constructed by conversion from integer values. This should 00805 // be replaced by code that creates a load from a constant-pool entry, 00806 // which will require some target-specific work. 00807 const APFloat &Flt = FPImm->getValueAPF(); 00808 MVT IntVT = TLI.getPointerTy(); 00809 00810 uint64_t x[2]; 00811 uint32_t IntBitWidth = IntVT.getSizeInBits(); 00812 bool isExact; 00813 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, 00814 APFloat::rmTowardZero, &isExact); 00815 if (!isExact) 00816 return 0; 00817 APInt IntVal(IntBitWidth, 2, x); 00818 00819 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(), 00820 ISD::Constant, IntVal.getZExtValue()); 00821 if (IntegerReg == 0) 00822 return 0; 00823 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT, 00824 ISD::SINT_TO_FP, IntegerReg); 00825 if (MaterialReg == 0) 00826 return 0; 00827 } 00828 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); 00829 } 00830 00831 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { 00832 return MRI.createVirtualRegister(RC); 00833 } 00834 00835 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, 00836 const TargetRegisterClass* RC) { 00837 unsigned ResultReg = createResultReg(RC); 00838 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00839 00840 BuildMI(MBB, DL, II, ResultReg); 00841 return ResultReg; 00842 } 00843 00844 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, 00845 const TargetRegisterClass *RC, 00846 unsigned Op0) { 00847 unsigned ResultReg = createResultReg(RC); 00848 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00849 00850 if (II.getNumDefs() >= 1) 00851 BuildMI(MBB, DL, II, ResultReg).addReg(Op0); 00852 else { 00853 BuildMI(MBB, DL, II).addReg(Op0); 00854 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00855 II.ImplicitDefs[0], RC, RC); 00856 if (!InsertedCopy) 00857 ResultReg = 0; 00858 } 00859 00860 return ResultReg; 00861 } 00862 00863 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, 00864 const TargetRegisterClass *RC, 00865 unsigned Op0, unsigned Op1) { 00866 unsigned ResultReg = createResultReg(RC); 00867 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00868 00869 if (II.getNumDefs() >= 1) 00870 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1); 00871 else { 00872 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1); 00873 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00874 II.ImplicitDefs[0], RC, RC); 00875 if (!InsertedCopy) 00876 ResultReg = 0; 00877 } 00878 return ResultReg; 00879 } 00880 00881 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, 00882 const TargetRegisterClass *RC, 00883 unsigned Op0, uint64_t Imm) { 00884 unsigned ResultReg = createResultReg(RC); 00885 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00886 00887 if (II.getNumDefs() >= 1) 00888 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm); 00889 else { 00890 BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm); 00891 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00892 II.ImplicitDefs[0], RC, RC); 00893 if (!InsertedCopy) 00894 ResultReg = 0; 00895 } 00896 return ResultReg; 00897 } 00898 00899 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, 00900 const TargetRegisterClass *RC, 00901 unsigned Op0, ConstantFP *FPImm) { 00902 unsigned ResultReg = createResultReg(RC); 00903 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00904 00905 if (II.getNumDefs() >= 1) 00906 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm); 00907 else { 00908 BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm); 00909 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00910 II.ImplicitDefs[0], RC, RC); 00911 if (!InsertedCopy) 00912 ResultReg = 0; 00913 } 00914 return ResultReg; 00915 } 00916 00917 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, 00918 const TargetRegisterClass *RC, 00919 unsigned Op0, unsigned Op1, uint64_t Imm) { 00920 unsigned ResultReg = createResultReg(RC); 00921 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00922 00923 if (II.getNumDefs() >= 1) 00924 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); 00925 else { 00926 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm); 00927 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00928 II.ImplicitDefs[0], RC, RC); 00929 if (!InsertedCopy) 00930 ResultReg = 0; 00931 } 00932 return ResultReg; 00933 } 00934 00935 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, 00936 const TargetRegisterClass *RC, 00937 uint64_t Imm) { 00938 unsigned ResultReg = createResultReg(RC); 00939 const TargetInstrDesc &II = TII.get(MachineInstOpcode); 00940 00941 if (II.getNumDefs() >= 1) 00942 BuildMI(MBB, DL, II, ResultReg).addImm(Imm); 00943 else { 00944 BuildMI(MBB, DL, II).addImm(Imm); 00945 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00946 II.ImplicitDefs[0], RC, RC); 00947 if (!InsertedCopy) 00948 ResultReg = 0; 00949 } 00950 return ResultReg; 00951 } 00952 00953 unsigned FastISel::FastEmitInst_extractsubreg(MVT::SimpleValueType RetVT, 00954 unsigned Op0, uint32_t Idx) { 00955 const TargetRegisterClass* RC = MRI.getRegClass(Op0); 00956 00957 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 00958 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG); 00959 00960 if (II.getNumDefs() >= 1) 00961 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx); 00962 else { 00963 BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx); 00964 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, 00965 II.ImplicitDefs[0], RC, RC); 00966 if (!InsertedCopy) 00967 ResultReg = 0; 00968 } 00969 return ResultReg; 00970 }