LLVM API Documentation
00001 //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel 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 implements the SelectionDAGISel class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "isel" 00015 #include "llvm/CodeGen/SelectionDAGISel.h" 00016 #include "SelectionDAGBuild.h" 00017 #include "llvm/ADT/BitVector.h" 00018 #include "llvm/Analysis/AliasAnalysis.h" 00019 #include "llvm/Constants.h" 00020 #include "llvm/CallingConv.h" 00021 #include "llvm/DerivedTypes.h" 00022 #include "llvm/Function.h" 00023 #include "llvm/GlobalVariable.h" 00024 #include "llvm/InlineAsm.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Intrinsics.h" 00027 #include "llvm/IntrinsicInst.h" 00028 #include "llvm/CodeGen/FastISel.h" 00029 #include "llvm/CodeGen/GCStrategy.h" 00030 #include "llvm/CodeGen/GCMetadata.h" 00031 #include "llvm/CodeGen/MachineFunction.h" 00032 #include "llvm/CodeGen/MachineFrameInfo.h" 00033 #include "llvm/CodeGen/MachineInstrBuilder.h" 00034 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00035 #include "llvm/CodeGen/MachineModuleInfo.h" 00036 #include "llvm/CodeGen/MachineRegisterInfo.h" 00037 #include "llvm/CodeGen/ScheduleDAG.h" 00038 #include "llvm/CodeGen/SchedulerRegistry.h" 00039 #include "llvm/CodeGen/SelectionDAG.h" 00040 #include "llvm/Target/TargetRegisterInfo.h" 00041 #include "llvm/Target/TargetData.h" 00042 #include "llvm/Target/TargetFrameInfo.h" 00043 #include "llvm/Target/TargetInstrInfo.h" 00044 #include "llvm/Target/TargetLowering.h" 00045 #include "llvm/Target/TargetMachine.h" 00046 #include "llvm/Target/TargetOptions.h" 00047 #include "llvm/Support/Compiler.h" 00048 #include "llvm/Support/Debug.h" 00049 #include "llvm/Support/MathExtras.h" 00050 #include "llvm/Support/Timer.h" 00051 #include <algorithm> 00052 using namespace llvm; 00053 00054 static cl::opt<bool> 00055 EnableValueProp("enable-value-prop", cl::Hidden); 00056 static cl::opt<bool> 00057 EnableLegalizeTypes("enable-legalize-types", cl::Hidden); 00058 static cl::opt<bool> 00059 EnableFastISelVerbose("fast-isel-verbose", cl::Hidden, 00060 cl::desc("Enable verbose messages in the experimental \"fast\" " 00061 "instruction selector")); 00062 static cl::opt<bool> 00063 EnableFastISelAbort("fast-isel-abort", cl::Hidden, 00064 cl::desc("Enable abort calls when \"fast\" instruction fails")); 00065 static cl::opt<bool> 00066 SchedLiveInCopies("schedule-livein-copies", 00067 cl::desc("Schedule copies of livein registers"), 00068 cl::init(false)); 00069 00070 #ifndef NDEBUG 00071 static cl::opt<bool> 00072 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden, 00073 cl::desc("Pop up a window to show dags before the first " 00074 "dag combine pass")); 00075 static cl::opt<bool> 00076 ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden, 00077 cl::desc("Pop up a window to show dags before legalize types")); 00078 static cl::opt<bool> 00079 ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, 00080 cl::desc("Pop up a window to show dags before legalize")); 00081 static cl::opt<bool> 00082 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden, 00083 cl::desc("Pop up a window to show dags before the second " 00084 "dag combine pass")); 00085 static cl::opt<bool> 00086 ViewISelDAGs("view-isel-dags", cl::Hidden, 00087 cl::desc("Pop up a window to show isel dags as they are selected")); 00088 static cl::opt<bool> 00089 ViewSchedDAGs("view-sched-dags", cl::Hidden, 00090 cl::desc("Pop up a window to show sched dags as they are processed")); 00091 static cl::opt<bool> 00092 ViewSUnitDAGs("view-sunit-dags", cl::Hidden, 00093 cl::desc("Pop up a window to show SUnit dags after they are processed")); 00094 #else 00095 static const bool ViewDAGCombine1 = false, 00096 ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false, 00097 ViewDAGCombine2 = false, 00098 ViewISelDAGs = false, ViewSchedDAGs = false, 00099 ViewSUnitDAGs = false; 00100 #endif 00101 00102 //===---------------------------------------------------------------------===// 00103 /// 00104 /// RegisterScheduler class - Track the registration of instruction schedulers. 00105 /// 00106 //===---------------------------------------------------------------------===// 00107 MachinePassRegistry RegisterScheduler::Registry; 00108 00109 //===---------------------------------------------------------------------===// 00110 /// 00111 /// ISHeuristic command line option for instruction schedulers. 00112 /// 00113 //===---------------------------------------------------------------------===// 00114 static cl::opt<RegisterScheduler::FunctionPassCtor, false, 00115 RegisterPassParser<RegisterScheduler> > 00116 ISHeuristic("pre-RA-sched", 00117 cl::init(&createDefaultScheduler), 00118 cl::desc("Instruction schedulers available (before register" 00119 " allocation):")); 00120 00121 static RegisterScheduler 00122 defaultListDAGScheduler("default", " Best scheduler for the target", 00123 createDefaultScheduler); 00124 00125 namespace llvm { 00126 //===--------------------------------------------------------------------===// 00127 /// createDefaultScheduler - This creates an instruction scheduler appropriate 00128 /// for the target. 00129 ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS, 00130 SelectionDAG *DAG, 00131 MachineBasicBlock *BB, 00132 bool Fast) { 00133 TargetLowering &TLI = IS->getTargetLowering(); 00134 00135 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) { 00136 return createTDListDAGScheduler(IS, DAG, BB, Fast); 00137 } else { 00138 assert(TLI.getSchedulingPreference() == 00139 TargetLowering::SchedulingForRegPressure && "Unknown sched type!"); 00140 return createBURRListDAGScheduler(IS, DAG, BB, Fast); 00141 } 00142 } 00143 } 00144 00145 // EmitInstrWithCustomInserter - This method should be implemented by targets 00146 // that mark instructions with the 'usesCustomDAGSchedInserter' flag. These 00147 // instructions are special in various ways, which require special support to 00148 // insert. The specified MachineInstr is created but not inserted into any 00149 // basic blocks, and the scheduler passes ownership of it to this method. 00150 MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 00151 MachineBasicBlock *MBB) { 00152 cerr << "If a target marks an instruction with " 00153 << "'usesCustomDAGSchedInserter', it must implement " 00154 << "TargetLowering::EmitInstrWithCustomInserter!\n"; 00155 abort(); 00156 return 0; 00157 } 00158 00159 /// EmitLiveInCopy - Emit a copy for a live in physical register. If the 00160 /// physical register has only a single copy use, then coalesced the copy 00161 /// if possible. 00162 static void EmitLiveInCopy(MachineBasicBlock *MBB, 00163 MachineBasicBlock::iterator &InsertPos, 00164 unsigned VirtReg, unsigned PhysReg, 00165 const TargetRegisterClass *RC, 00166 DenseMap<MachineInstr*, unsigned> &CopyRegMap, 00167 const MachineRegisterInfo &MRI, 00168 const TargetRegisterInfo &TRI, 00169 const TargetInstrInfo &TII) { 00170 unsigned NumUses = 0; 00171 MachineInstr *UseMI = NULL; 00172 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg), 00173 UE = MRI.use_end(); UI != UE; ++UI) { 00174 UseMI = &*UI; 00175 if (++NumUses > 1) 00176 break; 00177 } 00178 00179 // If the number of uses is not one, or the use is not a move instruction, 00180 // don't coalesce. Also, only coalesce away a virtual register to virtual 00181 // register copy. 00182 bool Coalesced = false; 00183 unsigned SrcReg, DstReg; 00184 if (NumUses == 1 && 00185 TII.isMoveInstr(*UseMI, SrcReg, DstReg) && 00186 TargetRegisterInfo::isVirtualRegister(DstReg)) { 00187 VirtReg = DstReg; 00188 Coalesced = true; 00189 } 00190 00191 // Now find an ideal location to insert the copy. 00192 MachineBasicBlock::iterator Pos = InsertPos; 00193 while (Pos != MBB->begin()) { 00194 MachineInstr *PrevMI = prior(Pos); 00195 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI); 00196 // copyRegToReg might emit multiple instructions to do a copy. 00197 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second; 00198 if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg)) 00199 // This is what the BB looks like right now: 00200 // r1024 = mov r0 00201 // ... 00202 // r1 = mov r1024 00203 // 00204 // We want to insert "r1025 = mov r1". Inserting this copy below the 00205 // move to r1024 makes it impossible for that move to be coalesced. 00206 // 00207 // r1025 = mov r1 00208 // r1024 = mov r0 00209 // ... 00210 // r1 = mov 1024 00211 // r2 = mov 1025 00212 break; // Woot! Found a good location. 00213 --Pos; 00214 } 00215 00216 TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC); 00217 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg)); 00218 if (Coalesced) { 00219 if (&*InsertPos == UseMI) ++InsertPos; 00220 MBB->erase(UseMI); 00221 } 00222 } 00223 00224 /// EmitLiveInCopies - If this is the first basic block in the function, 00225 /// and if it has live ins that need to be copied into vregs, emit the 00226 /// copies into the block. 00227 static void EmitLiveInCopies(MachineBasicBlock *EntryMBB, 00228 const MachineRegisterInfo &MRI, 00229 const TargetRegisterInfo &TRI, 00230 const TargetInstrInfo &TII) { 00231 if (SchedLiveInCopies) { 00232 // Emit the copies at a heuristically-determined location in the block. 00233 DenseMap<MachineInstr*, unsigned> CopyRegMap; 00234 MachineBasicBlock::iterator InsertPos = EntryMBB->begin(); 00235 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(), 00236 E = MRI.livein_end(); LI != E; ++LI) 00237 if (LI->second) { 00238 const TargetRegisterClass *RC = MRI.getRegClass(LI->second); 00239 EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first, 00240 RC, CopyRegMap, MRI, TRI, TII); 00241 } 00242 } else { 00243 // Emit the copies into the top of the block. 00244 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(), 00245 E = MRI.livein_end(); LI != E; ++LI) 00246 if (LI->second) { 00247 const TargetRegisterClass *RC = MRI.getRegClass(LI->second); 00248 TII.copyRegToReg(*EntryMBB, EntryMBB->begin(), 00249 LI->second, LI->first, RC, RC); 00250 } 00251 } 00252 } 00253 00254 //===----------------------------------------------------------------------===// 00255 // SelectionDAGISel code 00256 //===----------------------------------------------------------------------===// 00257 00258 SelectionDAGISel::SelectionDAGISel(TargetLowering &tli, bool fast) : 00259 FunctionPass(&ID), TLI(tli), 00260 FuncInfo(new FunctionLoweringInfo(TLI)), 00261 CurDAG(new SelectionDAG(TLI, *FuncInfo)), 00262 SDL(new SelectionDAGLowering(*CurDAG, TLI, *FuncInfo)), 00263 GFI(), 00264 Fast(fast), 00265 DAGSize(0) 00266 {} 00267 00268 SelectionDAGISel::~SelectionDAGISel() { 00269 delete SDL; 00270 delete CurDAG; 00271 delete FuncInfo; 00272 } 00273 00274 unsigned SelectionDAGISel::MakeReg(MVT VT) { 00275 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); 00276 } 00277 00278 void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { 00279 AU.addRequired<AliasAnalysis>(); 00280 AU.addRequired<GCModuleInfo>(); 00281 AU.setPreservesAll(); 00282 } 00283 00284 bool SelectionDAGISel::runOnFunction(Function &Fn) { 00285 // Do some sanity-checking on the command-line options. 00286 assert((!EnableFastISelVerbose || EnableFastISel) && 00287 "-fast-isel-verbose requires -fast-isel"); 00288 assert((!EnableFastISelAbort || EnableFastISel) && 00289 "-fast-isel-abort requires -fast-isel"); 00290 00291 // Get alias analysis for load/store combining. 00292 AA = &getAnalysis<AliasAnalysis>(); 00293 00294 TargetMachine &TM = TLI.getTargetMachine(); 00295 MachineFunction &MF = MachineFunction::construct(&Fn, TM); 00296 const MachineRegisterInfo &MRI = MF.getRegInfo(); 00297 const TargetInstrInfo &TII = *TM.getInstrInfo(); 00298 const TargetRegisterInfo &TRI = *TM.getRegisterInfo(); 00299 00300 if (MF.getFunction()->hasGC()) 00301 GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF.getFunction()); 00302 else 00303 GFI = 0; 00304 RegInfo = &MF.getRegInfo(); 00305 DOUT << "\n\n\n=== " << Fn.getName() << "\n"; 00306 00307 FuncInfo->set(Fn, MF, EnableFastISel); 00308 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>(); 00309 CurDAG->init(MF, MMI); 00310 SDL->init(GFI, *AA); 00311 00312 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) 00313 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator())) 00314 // Mark landing pad. 00315 FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); 00316 00317 SelectAllBasicBlocks(Fn, MF, MMI); 00318 00319 // If the first basic block in the function has live ins that need to be 00320 // copied into vregs, emit the copies into the top of the block before 00321 // emitting the code for the block. 00322 EmitLiveInCopies(MF.begin(), MRI, TRI, TII); 00323 00324 // Add function live-ins to entry block live-in set. 00325 for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(), 00326 E = RegInfo->livein_end(); I != E; ++I) 00327 MF.begin()->addLiveIn(I->first); 00328 00329 #ifndef NDEBUG 00330 assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() && 00331 "Not all catch info was assigned to a landing pad!"); 00332 #endif 00333 00334 FuncInfo->clear(); 00335 00336 return true; 00337 } 00338 00339 static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB, 00340 MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { 00341 for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I) 00342 if (EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) { 00343 // Apply the catch info to DestBB. 00344 AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]); 00345 #ifndef NDEBUG 00346 if (!FLI.MBBMap[SrcBB]->isLandingPad()) 00347 FLI.CatchInfoFound.insert(EHSel); 00348 #endif 00349 } 00350 } 00351 00352 /// IsFixedFrameObjectWithPosOffset - Check if object is a fixed frame object and 00353 /// whether object offset >= 0. 00354 static bool 00355 IsFixedFrameObjectWithPosOffset(MachineFrameInfo * MFI, SDValue Op) { 00356 if (!isa<FrameIndexSDNode>(Op)) return false; 00357 00358 FrameIndexSDNode * FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op); 00359 int FrameIdx = FrameIdxNode->getIndex(); 00360 return MFI->isFixedObjectIndex(FrameIdx) && 00361 MFI->getObjectOffset(FrameIdx) >= 0; 00362 } 00363 00364 /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could 00365 /// possibly be overwritten when lowering the outgoing arguments in a tail 00366 /// call. Currently the implementation of this call is very conservative and 00367 /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with 00368 /// virtual registers would be overwritten by direct lowering. 00369 static bool IsPossiblyOverwrittenArgumentOfTailCall(SDValue Op, 00370 MachineFrameInfo * MFI) { 00371 RegisterSDNode * OpReg = NULL; 00372 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS || 00373 (Op.getOpcode()== ISD::CopyFromReg && 00374 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) && 00375 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) || 00376 (Op.getOpcode() == ISD::LOAD && 00377 IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(1))) || 00378 (Op.getOpcode() == ISD::MERGE_VALUES && 00379 Op.getOperand(Op.getResNo()).getOpcode() == ISD::LOAD && 00380 IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(Op.getResNo()). 00381 getOperand(1)))) 00382 return true; 00383 return false; 00384 } 00385 00386 /// CheckDAGForTailCallsAndFixThem - This Function looks for CALL nodes in the 00387 /// DAG and fixes their tailcall attribute operand. 00388 static void CheckDAGForTailCallsAndFixThem(SelectionDAG &DAG, 00389 TargetLowering& TLI) { 00390 SDNode * Ret = NULL; 00391 SDValue Terminator = DAG.getRoot(); 00392 00393 // Find RET node. 00394 if (Terminator.getOpcode() == ISD::RET) { 00395 Ret = Terminator.getNode(); 00396 } 00397 00398 // Fix tail call attribute of CALL nodes. 00399 for (SelectionDAG::allnodes_iterator BE = DAG.allnodes_begin(), 00400 BI = DAG.allnodes_end(); BI != BE; ) { 00401 --BI; 00402 if (CallSDNode *TheCall = dyn_cast<CallSDNode>(BI)) { 00403 SDValue OpRet(Ret, 0); 00404 SDValue OpCall(BI, 0); 00405 bool isMarkedTailCall = TheCall->isTailCall(); 00406 // If CALL node has tail call attribute set to true and the call is not 00407 // eligible (no RET or the target rejects) the attribute is fixed to 00408 // false. The TargetLowering::IsEligibleForTailCallOptimization function 00409 // must correctly identify tail call optimizable calls. 00410 if (!isMarkedTailCall) continue; 00411 if (Ret==NULL || 00412 !TLI.IsEligibleForTailCallOptimization(TheCall, OpRet, DAG)) { 00413 // Not eligible. Mark CALL node as non tail call. Note that we 00414 // can modify the call node in place since calls are not CSE'd. 00415 TheCall->setNotTailCall(); 00416 } else { 00417 // Look for tail call clobbered arguments. Emit a series of 00418 // copyto/copyfrom virtual register nodes to protect them. 00419 SmallVector<SDValue, 32> Ops; 00420 SDValue Chain = TheCall->getChain(), InFlag; 00421 Ops.push_back(Chain); 00422 Ops.push_back(TheCall->getCallee()); 00423 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; ++i) { 00424 SDValue Arg = TheCall->getArg(i); 00425 bool isByVal = TheCall->getArgFlags(i).isByVal(); 00426 MachineFunction &MF = DAG.getMachineFunction(); 00427 MachineFrameInfo *MFI = MF.getFrameInfo(); 00428 if (!isByVal && 00429 IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) { 00430 MVT VT = Arg.getValueType(); 00431 unsigned VReg = MF.getRegInfo(). 00432 createVirtualRegister(TLI.getRegClassFor(VT)); 00433 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag); 00434 InFlag = Chain.getValue(1); 00435 Arg = DAG.getCopyFromReg(Chain, VReg, VT, InFlag); 00436 Chain = Arg.getValue(1); 00437 InFlag = Arg.getValue(2); 00438 } 00439 Ops.push_back(Arg); 00440 Ops.push_back(TheCall->getArgFlagsVal(i)); 00441 } 00442 // Link in chain of CopyTo/CopyFromReg. 00443 Ops[0] = Chain; 00444 DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size()); 00445 } 00446 } 00447 } 00448 } 00449 00450 void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, 00451 BasicBlock::iterator Begin, 00452 BasicBlock::iterator End) { 00453 SDL->setCurrentBasicBlock(BB); 00454 00455 MachineModuleInfo *MMI = CurDAG->getMachineModuleInfo(); 00456 00457 if (MMI && BB->isLandingPad()) { 00458 // Add a label to mark the beginning of the landing pad. Deletion of the 00459 // landing pad can thus be detected via the MachineModuleInfo. 00460 unsigned LabelID = MMI->addLandingPad(BB); 00461 CurDAG->setRoot(CurDAG->getLabel(ISD::EH_LABEL, 00462 CurDAG->getEntryNode(), LabelID)); 00463 00464 // Mark exception register as live in. 00465 unsigned Reg = TLI.getExceptionAddressRegister(); 00466 if (Reg) BB->addLiveIn(Reg); 00467 00468 // Mark exception selector register as live in. 00469 Reg = TLI.getExceptionSelectorRegister(); 00470 if (Reg) BB->addLiveIn(Reg); 00471 00472 // FIXME: Hack around an exception handling flaw (PR1508): the personality 00473 // function and list of typeids logically belong to the invoke (or, if you 00474 // like, the basic block containing the invoke), and need to be associated 00475 // with it in the dwarf exception handling tables. Currently however the 00476 // information is provided by an intrinsic (eh.selector) that can be moved 00477 // to unexpected places by the optimizers: if the unwind edge is critical, 00478 // then breaking it can result in the intrinsics being in the successor of 00479 // the landing pad, not the landing pad itself. This results in exceptions 00480 // not being caught because no typeids are associated with the invoke. 00481 // This may not be the only way things can go wrong, but it is the only way 00482 // we try to work around for the moment. 00483 BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator()); 00484 00485 if (Br && Br->isUnconditional()) { // Critical edge? 00486 BasicBlock::iterator I, E; 00487 for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I) 00488 if (isa<EHSelectorInst>(I)) 00489 break; 00490 00491 if (I == E) 00492 // No catch info found - try to extract some from the successor. 00493 copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo); 00494 } 00495 } 00496 00497 // Lower all of the non-terminator instructions. 00498 for (BasicBlock::iterator I = Begin; I != End; ++I) 00499 if (!isa<TerminatorInst>(I)) 00500 SDL->visit(*I); 00501 00502 // Ensure that all instructions which are used outside of their defining 00503 // blocks are available as virtual registers. Invoke is handled elsewhere. 00504 for (BasicBlock::iterator I = Begin; I != End; ++I) 00505 if (!I->use_empty() && !isa<PHINode>(I) && !isa<InvokeInst>(I)) { 00506 DenseMap<const Value*,unsigned>::iterator VMI =FuncInfo->ValueMap.find(I); 00507 if (VMI != FuncInfo->ValueMap.end()) 00508 SDL->CopyValueToVirtualRegister(I, VMI->second); 00509 } 00510 00511 // Handle PHI nodes in successor blocks. 00512 if (End == LLVMBB->end()) { 00513 HandlePHINodesInSuccessorBlocks(LLVMBB); 00514 00515 // Lower the terminator after the copies are emitted. 00516 SDL->visit(*LLVMBB->getTerminator()); 00517 } 00518 00519 // Make sure the root of the DAG is up-to-date. 00520 CurDAG->setRoot(SDL->getControlRoot()); 00521 00522 // Check whether calls in this block are real tail calls. Fix up CALL nodes 00523 // with correct tailcall attribute so that the target can rely on the tailcall 00524 // attribute indicating whether the call is really eligible for tail call 00525 // optimization. 00526 if (PerformTailCallOpt) 00527 CheckDAGForTailCallsAndFixThem(*CurDAG, TLI); 00528 00529 // Final step, emit the lowered DAG as machine code. 00530 CodeGenAndEmitDAG(); 00531 SDL->clear(); 00532 } 00533 00534 void SelectionDAGISel::ComputeLiveOutVRegInfo() { 00535 SmallPtrSet<SDNode*, 128> VisitedNodes; 00536 SmallVector<SDNode*, 128> Worklist; 00537 00538 Worklist.push_back(CurDAG->getRoot().getNode()); 00539 00540 APInt Mask; 00541 APInt KnownZero; 00542 APInt KnownOne; 00543 00544 while (!Worklist.empty()) { 00545 SDNode *N = Worklist.back(); 00546 Worklist.pop_back(); 00547 00548 // If we've already seen this node, ignore it. 00549 if (!VisitedNodes.insert(N)) 00550 continue; 00551 00552 // Otherwise, add all chain operands to the worklist. 00553 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 00554 if (N->getOperand(i).getValueType() == MVT::Other) 00555 Worklist.push_back(N->getOperand(i).getNode()); 00556 00557 // If this is a CopyToReg with a vreg dest, process it. 00558 if (N->getOpcode() != ISD::CopyToReg) 00559 continue; 00560 00561 unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 00562 if (!TargetRegisterInfo::isVirtualRegister(DestReg)) 00563 continue; 00564 00565 // Ignore non-scalar or non-integer values. 00566 SDValue Src = N->getOperand(2); 00567 MVT SrcVT = Src.getValueType(); 00568 if (!SrcVT.isInteger() || SrcVT.isVector()) 00569 continue; 00570 00571 unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src); 00572 Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits()); 00573 CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne); 00574 00575 // Only install this information if it tells us something. 00576 if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) { 00577 DestReg -= TargetRegisterInfo::FirstVirtualRegister; 00578 FunctionLoweringInfo &FLI = CurDAG->getFunctionLoweringInfo(); 00579 if (DestReg >= FLI.LiveOutRegInfo.size()) 00580 FLI.LiveOutRegInfo.resize(DestReg+1); 00581 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[DestReg]; 00582 LOI.NumSignBits = NumSignBits; 00583 LOI.KnownOne = NumSignBits; 00584 LOI.KnownZero = NumSignBits; 00585 } 00586 } 00587 } 00588 00589 void SelectionDAGISel::CodeGenAndEmitDAG() { 00590 std::string GroupName; 00591 if (TimePassesIsEnabled) 00592 GroupName = "Instruction Selection and Scheduling"; 00593 std::string BlockName; 00594 if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs || 00595 ViewDAGCombine2 || ViewISelDAGs || ViewSchedDAGs || ViewSUnitDAGs) 00596 BlockName = CurDAG->getMachineFunction().getFunction()->getName() + ':' + 00597 BB->getBasicBlock()->getName(); 00598 00599 DOUT << "Initial selection DAG:\n"; 00600 DEBUG(CurDAG->dump()); 00601 00602 if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName); 00603 00604 // Run the DAG combiner in pre-legalize mode. 00605 if (TimePassesIsEnabled) { 00606 NamedRegionTimer T("DAG Combining 1", GroupName); 00607 CurDAG->Combine(false, *AA, Fast); 00608 } else { 00609 CurDAG->Combine(false, *AA, Fast); 00610 } 00611 00612 DOUT << "Optimized lowered selection DAG:\n"; 00613 DEBUG(CurDAG->dump()); 00614 00615 // Second step, hack on the DAG until it only uses operations and types that 00616 // the target supports. 00617 if (EnableLegalizeTypes) {// Enable this some day. 00618 if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " + 00619 BlockName); 00620 00621 if (TimePassesIsEnabled) { 00622 NamedRegionTimer T("Type Legalization", GroupName); 00623 CurDAG->LegalizeTypes(); 00624 } else { 00625 CurDAG->LegalizeTypes(); 00626 } 00627 00628 DOUT << "Type-legalized selection DAG:\n"; 00629 DEBUG(CurDAG->dump()); 00630 00631 // TODO: enable a dag combine pass here. 00632 } 00633 00634 if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName); 00635 00636 if (TimePassesIsEnabled) { 00637 NamedRegionTimer T("DAG Legalization", GroupName); 00638 CurDAG->Legalize(); 00639 } else { 00640 CurDAG->Legalize(); 00641 } 00642 00643 DOUT << "Legalized selection DAG:\n"; 00644 DEBUG(CurDAG->dump()); 00645 00646 if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName); 00647 00648 // Run the DAG combiner in post-legalize mode. 00649 if (TimePassesIsEnabled) { 00650 NamedRegionTimer T("DAG Combining 2", GroupName); 00651 CurDAG->Combine(true, *AA, Fast); 00652 } else { 00653 CurDAG->Combine(true, *AA, Fast); 00654 } 00655 00656 DOUT << "Optimized legalized selection DAG:\n"; 00657 DEBUG(CurDAG->dump()); 00658 00659 if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName); 00660 00661 if (!Fast && EnableValueProp) 00662 ComputeLiveOutVRegInfo(); 00663 00664 // Third, instruction select all of the operations to machine code, adding the 00665 // code to the MachineBasicBlock. 00666 if (TimePassesIsEnabled) { 00667 NamedRegionTimer T("Instruction Selection", GroupName); 00668 InstructionSelect(); 00669 } else { 00670 InstructionSelect(); 00671 } 00672 00673 DOUT << "Selected selection DAG:\n"; 00674 DEBUG(CurDAG->dump()); 00675 00676 if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName); 00677 00678 // Schedule machine code. 00679 ScheduleDAG *Scheduler; 00680 if (TimePassesIsEnabled) { 00681 NamedRegionTimer T("Instruction Scheduling", GroupName); 00682 Scheduler = Schedule(); 00683 } else { 00684 Scheduler = Schedule(); 00685 } 00686 00687 if (ViewSUnitDAGs) Scheduler->viewGraph(); 00688 00689 // Emit machine code to BB. This can change 'BB' to the last block being 00690 // inserted into. 00691 if (TimePassesIsEnabled) { 00692 NamedRegionTimer T("Instruction Creation", GroupName); 00693 BB = Scheduler->EmitSchedule(); 00694 } else { 00695 BB = Scheduler->EmitSchedule(); 00696 } 00697 00698 // Free the scheduler state. 00699 if (TimePassesIsEnabled) { 00700 NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName); 00701 delete Scheduler; 00702 } else { 00703 delete Scheduler; 00704 } 00705 00706 DOUT << "Selected machine code:\n"; 00707 DEBUG(BB->dump()); 00708 } 00709 00710 void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn, MachineFunction &MF, 00711 MachineModuleInfo *MMI) { 00712 // Initialize the Fast-ISel state, if needed. 00713 FastISel *FastIS = 0; 00714 if (EnableFastISel) 00715 FastIS = TLI.createFastISel(*FuncInfo->MF, MMI, 00716 FuncInfo->ValueMap, 00717 FuncInfo->MBBMap, 00718 FuncInfo->StaticAllocaMap); 00719 00720 // Iterate over all basic blocks in the function. 00721 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { 00722 BasicBlock *LLVMBB = &*I; 00723 BB = FuncInfo->MBBMap[LLVMBB]; 00724 00725 BasicBlock::iterator const Begin = LLVMBB->begin(); 00726 BasicBlock::iterator const End = LLVMBB->end(); 00727 BasicBlock::iterator BI = Begin; 00728 00729 // Lower any arguments needed in this block if this is the entry block. 00730 bool SuppressFastISel = false; 00731 if (LLVMBB == &Fn.getEntryBlock()) { 00732 LowerArguments(LLVMBB); 00733 00734 // If any of the arguments has the byval attribute, forgo 00735 // fast-isel in the entry block. 00736 if (FastIS) { 00737 unsigned j = 1; 00738 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); 00739 I != E; ++I, ++j) 00740 if (Fn.paramHasAttr(j, Attribute::ByVal)) { 00741 if (EnableFastISelVerbose || EnableFastISelAbort) 00742 cerr << "FastISel skips entry block due to byval argument\n"; 00743 SuppressFastISel = true; 00744 break; 00745 } 00746 } 00747 } 00748 00749 // Before doing SelectionDAG ISel, see if FastISel has been requested. 00750 // FastISel doesn't support EH landing pads, which require special handling. 00751 if (FastIS && !SuppressFastISel && !BB->isLandingPad()) { 00752 // Emit code for any incoming arguments. This must happen before 00753 // beginning FastISel on the entry block. 00754 if (LLVMBB == &Fn.getEntryBlock()) { 00755 CurDAG->setRoot(SDL->getControlRoot()); 00756 CodeGenAndEmitDAG(); 00757 SDL->clear(); 00758 } 00759 FastIS->startNewBlock(BB); 00760 // Do FastISel on as many instructions as possible. 00761 for (; BI != End; ++BI) { 00762 // Just before the terminator instruction, insert instructions to 00763 // feed PHI nodes in successor blocks. 00764 if (isa<TerminatorInst>(BI)) 00765 if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) { 00766 if (EnableFastISelVerbose || EnableFastISelAbort) { 00767 cerr << "FastISel miss: "; 00768 BI->dump(); 00769 } 00770 if (EnableFastISelAbort) 00771 assert(0 && "FastISel didn't handle a PHI in a successor"); 00772 break;