LLVM API Documentation
00001 //===-- SelectionDAGBuild.h - Selection-DAG building ----------------------===// 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 implements routines for translating from LLVM IR into SelectionDAG IR. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef SELECTIONDAGBUILD_H 00015 #define SELECTIONDAGBUILD_H 00016 00017 #include "llvm/Constants.h" 00018 #include "llvm/ADT/APInt.h" 00019 #include "llvm/ADT/DenseMap.h" 00020 #ifndef NDEBUG 00021 #include "llvm/ADT/SmallSet.h" 00022 #endif 00023 #include "llvm/CodeGen/SelectionDAGNodes.h" 00024 #include "llvm/CodeGen/ValueTypes.h" 00025 #include "llvm/Support/CallSite.h" 00026 #include <vector> 00027 #include <set> 00028 00029 namespace llvm { 00030 00031 class AliasAnalysis; 00032 class AllocaInst; 00033 class BasicBlock; 00034 class BitCastInst; 00035 class BranchInst; 00036 class CallInst; 00037 class ExtractElementInst; 00038 class ExtractValueInst; 00039 class FCmpInst; 00040 class FPExtInst; 00041 class FPToSIInst; 00042 class FPToUIInst; 00043 class FPTruncInst; 00044 class FreeInst; 00045 class Function; 00046 class GetElementPtrInst; 00047 class GCFunctionInfo; 00048 class ICmpInst; 00049 class IntToPtrInst; 00050 class InvokeInst; 00051 class InsertElementInst; 00052 class InsertValueInst; 00053 class Instruction; 00054 class LoadInst; 00055 class MachineBasicBlock; 00056 class MachineFunction; 00057 class MachineInstr; 00058 class MachineModuleInfo; 00059 class MachineRegisterInfo; 00060 class MallocInst; 00061 class PHINode; 00062 class PtrToIntInst; 00063 class ReturnInst; 00064 class SDISelAsmOperandInfo; 00065 class SExtInst; 00066 class SelectInst; 00067 class ShuffleVectorInst; 00068 class SIToFPInst; 00069 class StoreInst; 00070 class SwitchInst; 00071 class TargetData; 00072 class TargetLowering; 00073 class TruncInst; 00074 class UIToFPInst; 00075 class UnreachableInst; 00076 class UnwindInst; 00077 class VICmpInst; 00078 class VFCmpInst; 00079 class VAArgInst; 00080 class ZExtInst; 00081 00082 //===--------------------------------------------------------------------===// 00083 /// FunctionLoweringInfo - This contains information that is global to a 00084 /// function that is used when lowering a region of the function. 00085 /// 00086 class FunctionLoweringInfo { 00087 public: 00088 TargetLowering &TLI; 00089 Function *Fn; 00090 MachineFunction *MF; 00091 MachineRegisterInfo *RegInfo; 00092 00093 explicit FunctionLoweringInfo(TargetLowering &TLI); 00094 00095 /// set - Initialize this FunctionLoweringInfo with the given Function 00096 /// and its associated MachineFunction. 00097 /// 00098 void set(Function &Fn, MachineFunction &MF, SelectionDAG &DAG, 00099 bool EnableFastISel); 00100 00101 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. 00102 DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap; 00103 00104 /// ValueMap - Since we emit code for the function a basic block at a time, 00105 /// we must remember which virtual registers hold the values for 00106 /// cross-basic-block values. 00107 DenseMap<const Value*, unsigned> ValueMap; 00108 00109 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in 00110 /// the entry block. This allows the allocas to be efficiently referenced 00111 /// anywhere in the function. 00112 DenseMap<const AllocaInst*, int> StaticAllocaMap; 00113 00114 #ifndef NDEBUG 00115 SmallSet<Instruction*, 8> CatchInfoLost; 00116 SmallSet<Instruction*, 8> CatchInfoFound; 00117 #endif 00118 00119 unsigned MakeReg(MVT VT); 00120 00121 /// isExportedInst - Return true if the specified value is an instruction 00122 /// exported from its block. 00123 bool isExportedInst(const Value *V) { 00124 return ValueMap.count(V); 00125 } 00126 00127 unsigned CreateRegForValue(const Value *V); 00128 00129 unsigned InitializeRegForValue(const Value *V) { 00130 unsigned &R = ValueMap[V]; 00131 assert(R == 0 && "Already initialized this value register!"); 00132 return R = CreateRegForValue(V); 00133 } 00134 00135 struct LiveOutInfo { 00136 unsigned NumSignBits; 00137 APInt KnownOne, KnownZero; 00138 LiveOutInfo() : NumSignBits(0) {} 00139 }; 00140 00141 /// LiveOutRegInfo - Information about live out vregs, indexed by their 00142 /// register number offset by 'FirstVirtualRegister'. 00143 std::vector<LiveOutInfo> LiveOutRegInfo; 00144 00145 /// clear - Clear out all the function-specific state. This returns this 00146 /// FunctionLoweringInfo to an empty state, ready to be used for a 00147 /// different function. 00148 void clear() { 00149 MBBMap.clear(); 00150 ValueMap.clear(); 00151 StaticAllocaMap.clear(); 00152 #ifndef NDEBUG 00153 CatchInfoLost.clear(); 00154 CatchInfoFound.clear(); 00155 #endif 00156 LiveOutRegInfo.clear(); 00157 } 00158 }; 00159 00160 //===----------------------------------------------------------------------===// 00161 /// SelectionDAGLowering - This is the common target-independent lowering 00162 /// implementation that is parameterized by a TargetLowering object. 00163 /// Also, targets can overload any lowering method. 00164 /// 00165 class SelectionDAGLowering { 00166 MachineBasicBlock *CurMBB; 00167 00168 /// CurDebugLoc - current file + line number. Changes as we build the DAG. 00169 DebugLoc CurDebugLoc; 00170 00171 DenseMap<const Value*, SDValue> NodeMap; 00172 00173 /// PendingLoads - Loads are not emitted to the program immediately. We bunch 00174 /// them up and then emit token factor nodes when possible. This allows us to 00175 /// get simple disambiguation between loads without worrying about alias 00176 /// analysis. 00177 SmallVector<SDValue, 8> PendingLoads; 00178 00179 /// PendingExports - CopyToReg nodes that copy values to virtual registers 00180 /// for export to other blocks need to be emitted before any terminator 00181 /// instruction, but they have no other ordering requirements. We bunch them 00182 /// up and the emit a single tokenfactor for them just before terminator 00183 /// instructions. 00184 SmallVector<SDValue, 8> PendingExports; 00185 00186 /// Case - A struct to record the Value for a switch case, and the 00187 /// case's target basic block. 00188 struct Case { 00189 Constant* Low; 00190 Constant* High; 00191 MachineBasicBlock* BB; 00192 00193 Case() : Low(0), High(0), BB(0) { } 00194 Case(Constant* low, Constant* high, MachineBasicBlock* bb) : 00195 Low(low), High(high), BB(bb) { } 00196 uint64_t size() const { 00197 uint64_t rHigh = cast<ConstantInt>(High)->getSExtValue(); 00198 uint64_t rLow = cast<ConstantInt>(Low)->getSExtValue(); 00199 return (rHigh - rLow + 1ULL); 00200 } 00201 }; 00202 00203 struct CaseBits { 00204 uint64_t Mask; 00205 MachineBasicBlock* BB; 00206 unsigned Bits; 00207 00208 CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits): 00209 Mask(mask), BB(bb), Bits(bits) { } 00210 }; 00211 00212 typedef std::vector<Case> CaseVector; 00213 typedef std::vector<CaseBits> CaseBitsVector; 00214 typedef CaseVector::iterator CaseItr; 00215 typedef std::pair<CaseItr, CaseItr> CaseRange; 00216 00217 /// CaseRec - A struct with ctor used in lowering switches to a binary tree 00218 /// of conditional branches. 00219 struct CaseRec { 00220 CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) : 00221 CaseBB(bb), LT(lt), GE(ge), Range(r) {} 00222 00223 /// CaseBB - The MBB in which to emit the compare and branch 00224 MachineBasicBlock *CaseBB; 00225 /// LT, GE - If nonzero, we know the current case value must be less-than or 00226 /// greater-than-or-equal-to these Constants. 00227 Constant *LT; 00228 Constant *GE; 00229 /// Range - A pair of iterators representing the range of case values to be 00230 /// processed at this point in the binary search tree. 00231 CaseRange Range; 00232 }; 00233 00234 typedef std::vector<CaseRec> CaseRecVector; 00235 00236 /// The comparison function for sorting the switch case values in the vector. 00237 /// WARNING: Case ranges should be disjoint! 00238 struct CaseCmp { 00239 bool operator () (const Case& C1, const Case& C2) { 00240 assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); 00241 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); 00242 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); 00243 return CI1->getValue().slt(CI2->getValue()); 00244 } 00245 }; 00246 00247 struct CaseBitsCmp { 00248 bool operator () (const CaseBits& C1, const CaseBits& C2) { 00249 return C1.Bits > C2.Bits; 00250 } 00251 }; 00252 00253 size_t Clusterify(CaseVector& Cases, const SwitchInst &SI); 00254 00255 /// CaseBlock - This structure is used to communicate between SDLowering and 00256 /// SDISel for the code generation of additional basic blocks needed by multi- 00257 /// case switch statements. 00258 struct CaseBlock { 00259 CaseBlock(ISD::CondCode cc, Value *cmplhs, Value *cmprhs, Value *cmpmiddle, 00260 MachineBasicBlock *truebb, MachineBasicBlock *falsebb, 00261 MachineBasicBlock *me) 00262 : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), 00263 TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {} 00264 // CC - the condition code to use for the case block's setcc node 00265 ISD::CondCode CC; 00266 // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. 00267 // Emit by default LHS op RHS. MHS is used for range comparisons: 00268 // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). 00269 Value *CmpLHS, *CmpMHS, *CmpRHS; 00270 // TrueBB/FalseBB - the block to branch to if the setcc is true/false. 00271 MachineBasicBlock *TrueBB, *FalseBB; 00272 // ThisBB - the block into which to emit the code for the setcc and branches 00273 MachineBasicBlock *ThisBB; 00274 }; 00275 struct JumpTable { 00276 JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, 00277 MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} 00278 00279 /// Reg - the virtual register containing the index of the jump table entry 00280 //. to jump to. 00281 unsigned Reg; 00282 /// JTI - the JumpTableIndex for this jump table in the function. 00283 unsigned JTI; 00284 /// MBB - the MBB into which to emit the code for the indirect jump. 00285 MachineBasicBlock *MBB; 00286 /// Default - the MBB of the default bb, which is a successor of the range 00287 /// check MBB. This is when updating PHI nodes in successors. 00288 MachineBasicBlock *Default; 00289 }; 00290 struct JumpTableHeader { 00291 JumpTableHeader(APInt F, APInt L, Value* SV, MachineBasicBlock* H, 00292 bool E = false): 00293 First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} 00294 APInt First; 00295 APInt Last; 00296 Value *SValue; 00297 MachineBasicBlock *HeaderBB; 00298 bool Emitted; 00299 }; 00300 typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; 00301 00302 struct BitTestCase { 00303 BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr): 00304 Mask(M), ThisBB(T), TargetBB(Tr) { } 00305 uint64_t Mask; 00306 MachineBasicBlock* ThisBB; 00307 MachineBasicBlock* TargetBB; 00308 }; 00309 00310 typedef SmallVector<BitTestCase, 3> BitTestInfo; 00311 00312 struct BitTestBlock { 00313 BitTestBlock(APInt F, APInt R, Value* SV, 00314 unsigned Rg, bool E, 00315 MachineBasicBlock* P, MachineBasicBlock* D, 00316 const BitTestInfo& C): 00317 First(F), Range(R), SValue(SV), Reg(Rg), Emitted(E), 00318 Parent(P), Default(D), Cases(C) { } 00319 APInt First; 00320 APInt Range; 00321 Value *SValue; 00322 unsigned Reg; 00323 bool Emitted; 00324 MachineBasicBlock *Parent; 00325 MachineBasicBlock *Default; 00326 BitTestInfo Cases; 00327 }; 00328 00329 public: 00330 // TLI - This is information that describes the available target features we 00331 // need for lowering. This indicates when operations are unavailable, 00332 // implemented with a libcall, etc. 00333 TargetLowering &TLI; 00334 SelectionDAG &DAG; 00335 const TargetData *TD; 00336 AliasAnalysis *AA; 00337 00338 /// SwitchCases - Vector of CaseBlock structures used to communicate 00339 /// SwitchInst code generation information. 00340 std::vector<CaseBlock> SwitchCases; 00341 /// JTCases - Vector of JumpTable structures used to communicate 00342 /// SwitchInst code generation information. 00343 std::vector<JumpTableBlock> JTCases; 00344 /// BitTestCases - Vector of BitTestBlock structures used to communicate 00345 /// SwitchInst code generation information. 00346 std::vector<BitTestBlock> BitTestCases; 00347 00348 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; 00349 00350 // Emit PHI-node-operand constants only once even if used by multiple 00351 // PHI nodes. 00352 DenseMap<Constant*, unsigned> ConstantsOut; 00353 00354 /// FuncInfo - Information about the function as a whole. 00355 /// 00356 FunctionLoweringInfo &FuncInfo; 00357 00358 /// GFI - Garbage collection metadata for the function. 00359 GCFunctionInfo *GFI; 00360 00361 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, 00362 FunctionLoweringInfo &funcinfo) 00363 : CurDebugLoc(DebugLoc::getUnknownLoc()), 00364 TLI(tli), DAG(dag), FuncInfo(funcinfo) { 00365 } 00366 00367 void init(GCFunctionInfo *gfi, AliasAnalysis &aa); 00368 00369 /// clear - Clear out the curret SelectionDAG and the associated 00370 /// state and prepare this SelectionDAGLowering object to be used 00371 /// for a new block. This doesn't clear out information about 00372 /// additional blocks that are needed to complete switch lowering 00373 /// or PHI node updating; that information is cleared out as it is 00374 /// consumed. 00375 void clear(); 00376 00377 /// getRoot - Return the current virtual root of the Selection DAG, 00378 /// flushing any PendingLoad items. This must be done before emitting 00379 /// a store or any other node that may need to be ordered after any 00380 /// prior load instructions. 00381 /// 00382 SDValue getRoot(); 00383 00384 /// getControlRoot - Similar to getRoot, but instead of flushing all the 00385 /// PendingLoad items, flush all the PendingExports items. It is necessary 00386 /// to do this before emitting a terminator instruction. 00387 /// 00388 SDValue getControlRoot(); 00389 00390 DebugLoc getCurDebugLoc() const { return CurDebugLoc; } 00391 00392 void CopyValueToVirtualRegister(Value *V, unsigned Reg); 00393 00394 void visit(Instruction &I); 00395 00396 void visit(unsigned Opcode, User &I); 00397 00398 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } 00399 00400 SDValue getValue(const Value *V); 00401 00402 void setValue(const Value *V, SDValue NewN) { 00403 SDValue &N = NodeMap[V]; 00404 assert(N.getNode() == 0 && "Already set a value for this node!"); 00405 N = NewN; 00406 } 00407 00408 void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, 00409 std::set<unsigned> &OutputRegs, 00410 std::set<unsigned> &InputRegs); 00411 00412 void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB, 00413 MachineBasicBlock *FBB, MachineBasicBlock *CurBB, 00414 unsigned Opc); 00415 void EmitBranchForMergedCondition(Value *Cond, MachineBasicBlock *TBB, 00416 MachineBasicBlock *FBB, 00417 MachineBasicBlock *CurBB); 00418 bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); 00419 bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB); 00420 void ExportFromCurrentBlock(Value *V); 00421 void LowerCallTo(CallSite CS, SDValue Callee, bool IsTailCall, 00422 MachineBasicBlock *LandingPad = NULL); 00423 00424 private: 00425 // Terminator instructions. 00426 void visitRet(ReturnInst &I); 00427 void visitBr(BranchInst &I); 00428 void visitSwitch(SwitchInst &I); 00429 void visitUnreachable(UnreachableInst &I) { /* noop */ } 00430 00431 // Helpers for visitSwitch 00432 bool handleSmallSwitchRange(CaseRec& CR, 00433 CaseRecVector& WorkList, 00434 Value* SV, 00435 MachineBasicBlock* Default); 00436 bool handleJTSwitchCase(CaseRec& CR, 00437 CaseRecVector& WorkList, 00438 Value* SV, 00439 MachineBasicBlock* Default); 00440 bool handleBTSplitSwitchCase(CaseRec& CR, 00441 CaseRecVector& WorkList, 00442 Value* SV, 00443 MachineBasicBlock* Default); 00444 bool handleBitTestsSwitchCase(CaseRec& CR, 00445 CaseRecVector& WorkList, 00446 Value* SV, 00447 MachineBasicBlock* Default); 00448 public: 00449 void visitSwitchCase(CaseBlock &CB); 00450 void visitBitTestHeader(BitTestBlock &B); 00451 void visitBitTestCase(MachineBasicBlock* NextMBB, 00452 unsigned Reg, 00453 BitTestCase &B); 00454 void visitJumpTable(JumpTable &JT); 00455 void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH); 00456 00457 private: 00458 // These all get lowered before this pass. 00459 void visitInvoke(InvokeInst &I); 00460 void visitUnwind(UnwindInst &I); 00461 00462 void visitBinary(User &I, unsigned OpCode); 00463 void visitShift(User &I, unsigned Opcode); 00464 void visitAdd(User &I); 00465 void visitSub(User &I); 00466 void visitMul(User &I); 00467 void visitURem(User &I) { visitBinary(I, ISD::UREM); } 00468 void visitSRem(User &I) { visitBinary(I, ISD::SREM); } 00469 void visitFRem(User &I) { visitBinary(I, ISD::FREM); } 00470 void visitUDiv(User &I) { visitBinary(I, ISD::UDIV); } 00471 void visitSDiv(User &I) { visitBinary(I, ISD::SDIV); } 00472 void visitFDiv(User &I) { visitBinary(I, ISD::FDIV); } 00473 void visitAnd (User &I) { visitBinary(I, ISD::AND); } 00474 void visitOr (User &I) { visitBinary(I, ISD::OR); } 00475 void visitXor (User &I) { visitBinary(I, ISD::XOR); } 00476 void visitShl (User &I) { visitShift(I, ISD::SHL); } 00477 void visitLShr(User &I) { visitShift(I, ISD::SRL); } 00478 void visitAShr(User &I) { visitShift(I, ISD::SRA); } 00479 void visitICmp(User &I); 00480 void visitFCmp(User &I); 00481 void visitVICmp(User &I); 00482 void visitVFCmp(User &I); 00483 // Visit the conversion instructions 00484 void visitTrunc(User &I); 00485 void visitZExt(User &I); 00486 void visitSExt(User &I); 00487 void visitFPTrunc(User &I); 00488 void visitFPExt(User &I); 00489 void visitFPToUI(User &I); 00490 void visitFPToSI(User &I); 00491 void visitUIToFP(User &I); 00492 void visitSIToFP(User &I); 00493 void visitPtrToInt(User &I); 00494 void visitIntToPtr(User &I); 00495 void visitBitCast(User &I); 00496 00497 void visitExtractElement(User &I); 00498 void visitInsertElement(User &I); 00499 void visitShuffleVector(User &I); 00500 00501 void visitExtractValue(ExtractValueInst &I); 00502 void visitInsertValue(InsertValueInst &I); 00503 00504 void visitGetElementPtr(User &I); 00505 void visitSelect(User &I); 00506 00507 void visitMalloc(MallocInst &I); 00508 void visitFree(FreeInst &I); 00509 void visitAlloca(AllocaInst &I); 00510 void visitLoad(LoadInst &I); 00511 void visitStore(StoreInst &I); 00512 void visitPHI(PHINode &I) { } // PHI nodes are handled specially. 00513 void visitCall(CallInst &I); 00514 void visitInlineAsm(CallSite CS); 00515 const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); 00516 void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic); 00517 00518 void visitPow(CallInst &I); 00519 void visitExp2(CallInst &I); 00520 void visitExp(CallInst &I); 00521 void visitLog(CallInst &I); 00522 void visitLog2(CallInst &I); 00523 void visitLog10(CallInst &I); 00524 00525 void visitVAStart(CallInst &I); 00526 void visitVAArg(VAArgInst &I); 00527 void visitVAEnd(CallInst &I); 00528 void visitVACopy(CallInst &I); 00529 00530 void visitUserOp1(Instruction &I) { 00531 assert(0 && "UserOp1 should not exist at instruction selection time!"); 00532 abort(); 00533 } 00534 void visitUserOp2(Instruction &I) { 00535 assert(0 && "UserOp2 should not exist at instruction selection time!"); 00536 abort(); 00537 } 00538 00539 const char *implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op); 00540 const char *implVisitAluOverflow(CallInst &I, ISD::NodeType Op); 00541 00542 void setCurDebugLoc(DebugLoc dl) { CurDebugLoc = dl; } 00543 }; 00544 00545 /// AddCatchInfo - Extract the personality and type infos from an eh.selector 00546 /// call, and add them to the specified machine basic block. 00547 void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, 00548 MachineBasicBlock *MBB); 00549 00550 } // end namespace llvm 00551 00552 #endif