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