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