LLVM API Documentation

SelectionDAGISel.cpp
Go to the documentation of this file.
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 #include "llvm/CodeGen/SelectionDAGISel.h"
00015 #include "ScheduleDAGSDNodes.h"
00016 #include "SelectionDAGBuilder.h"
00017 #include "llvm/ADT/PostOrderIterator.h"
00018 #include "llvm/ADT/Statistic.h"
00019 #include "llvm/Analysis/AliasAnalysis.h"
00020 #include "llvm/Analysis/BranchProbabilityInfo.h"
00021 #include "llvm/Analysis/CFG.h"
00022 #include "llvm/CodeGen/FastISel.h"
00023 #include "llvm/CodeGen/FunctionLoweringInfo.h"
00024 #include "llvm/CodeGen/GCMetadata.h"
00025 #include "llvm/CodeGen/GCStrategy.h"
00026 #include "llvm/CodeGen/MachineFrameInfo.h"
00027 #include "llvm/CodeGen/MachineFunction.h"
00028 #include "llvm/CodeGen/MachineInstrBuilder.h"
00029 #include "llvm/CodeGen/MachineModuleInfo.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
00032 #include "llvm/CodeGen/SchedulerRegistry.h"
00033 #include "llvm/CodeGen/SelectionDAG.h"
00034 #include "llvm/IR/Constants.h"
00035 #include "llvm/IR/DebugInfo.h"
00036 #include "llvm/IR/Function.h"
00037 #include "llvm/IR/InlineAsm.h"
00038 #include "llvm/IR/Instructions.h"
00039 #include "llvm/IR/IntrinsicInst.h"
00040 #include "llvm/IR/Intrinsics.h"
00041 #include "llvm/IR/LLVMContext.h"
00042 #include "llvm/IR/Module.h"
00043 #include "llvm/Support/Compiler.h"
00044 #include "llvm/Support/Debug.h"
00045 #include "llvm/Support/ErrorHandling.h"
00046 #include "llvm/Support/Timer.h"
00047 #include "llvm/Support/raw_ostream.h"
00048 #include "llvm/Target/TargetInstrInfo.h"
00049 #include "llvm/Target/TargetIntrinsicInfo.h"
00050 #include "llvm/Target/TargetLibraryInfo.h"
00051 #include "llvm/Target/TargetLowering.h"
00052 #include "llvm/Target/TargetMachine.h"
00053 #include "llvm/Target/TargetOptions.h"
00054 #include "llvm/Target/TargetRegisterInfo.h"
00055 #include "llvm/Target/TargetSubtargetInfo.h"
00056 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00057 #include <algorithm>
00058 using namespace llvm;
00059 
00060 #define DEBUG_TYPE "isel"
00061 
00062 STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on");
00063 STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected");
00064 STATISTIC(NumFastIselBlocks, "Number of blocks selected entirely by fast isel");
00065 STATISTIC(NumDAGBlocks, "Number of blocks selected using DAG");
00066 STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path");
00067 STATISTIC(NumEntryBlocks, "Number of entry blocks encountered");
00068 STATISTIC(NumFastIselFailLowerArguments,
00069           "Number of entry blocks where fast isel failed to lower arguments");
00070 
00071 #ifndef NDEBUG
00072 static cl::opt<bool>
00073 EnableFastISelVerbose2("fast-isel-verbose2", cl::Hidden,
00074           cl::desc("Enable extra verbose messages in the \"fast\" "
00075                    "instruction selector"));
00076 
00077   // Terminators
00078 STATISTIC(NumFastIselFailRet,"Fast isel fails on Ret");
00079 STATISTIC(NumFastIselFailBr,"Fast isel fails on Br");
00080 STATISTIC(NumFastIselFailSwitch,"Fast isel fails on Switch");
00081 STATISTIC(NumFastIselFailIndirectBr,"Fast isel fails on IndirectBr");
00082 STATISTIC(NumFastIselFailInvoke,"Fast isel fails on Invoke");
00083 STATISTIC(NumFastIselFailResume,"Fast isel fails on Resume");
00084 STATISTIC(NumFastIselFailUnreachable,"Fast isel fails on Unreachable");
00085 
00086   // Standard binary operators...
00087 STATISTIC(NumFastIselFailAdd,"Fast isel fails on Add");
00088 STATISTIC(NumFastIselFailFAdd,"Fast isel fails on FAdd");
00089 STATISTIC(NumFastIselFailSub,"Fast isel fails on Sub");
00090 STATISTIC(NumFastIselFailFSub,"Fast isel fails on FSub");
00091 STATISTIC(NumFastIselFailMul,"Fast isel fails on Mul");
00092 STATISTIC(NumFastIselFailFMul,"Fast isel fails on FMul");
00093 STATISTIC(NumFastIselFailUDiv,"Fast isel fails on UDiv");
00094 STATISTIC(NumFastIselFailSDiv,"Fast isel fails on SDiv");
00095 STATISTIC(NumFastIselFailFDiv,"Fast isel fails on FDiv");
00096 STATISTIC(NumFastIselFailURem,"Fast isel fails on URem");
00097 STATISTIC(NumFastIselFailSRem,"Fast isel fails on SRem");
00098 STATISTIC(NumFastIselFailFRem,"Fast isel fails on FRem");
00099 
00100   // Logical operators...
00101 STATISTIC(NumFastIselFailAnd,"Fast isel fails on And");
00102 STATISTIC(NumFastIselFailOr,"Fast isel fails on Or");
00103 STATISTIC(NumFastIselFailXor,"Fast isel fails on Xor");
00104 
00105   // Memory instructions...
00106 STATISTIC(NumFastIselFailAlloca,"Fast isel fails on Alloca");
00107 STATISTIC(NumFastIselFailLoad,"Fast isel fails on Load");
00108 STATISTIC(NumFastIselFailStore,"Fast isel fails on Store");
00109 STATISTIC(NumFastIselFailAtomicCmpXchg,"Fast isel fails on AtomicCmpXchg");
00110 STATISTIC(NumFastIselFailAtomicRMW,"Fast isel fails on AtomicRWM");
00111 STATISTIC(NumFastIselFailFence,"Fast isel fails on Frence");
00112 STATISTIC(NumFastIselFailGetElementPtr,"Fast isel fails on GetElementPtr");
00113 
00114   // Convert instructions...
00115 STATISTIC(NumFastIselFailTrunc,"Fast isel fails on Trunc");
00116 STATISTIC(NumFastIselFailZExt,"Fast isel fails on ZExt");
00117 STATISTIC(NumFastIselFailSExt,"Fast isel fails on SExt");
00118 STATISTIC(NumFastIselFailFPTrunc,"Fast isel fails on FPTrunc");
00119 STATISTIC(NumFastIselFailFPExt,"Fast isel fails on FPExt");
00120 STATISTIC(NumFastIselFailFPToUI,"Fast isel fails on FPToUI");
00121 STATISTIC(NumFastIselFailFPToSI,"Fast isel fails on FPToSI");
00122 STATISTIC(NumFastIselFailUIToFP,"Fast isel fails on UIToFP");
00123 STATISTIC(NumFastIselFailSIToFP,"Fast isel fails on SIToFP");
00124 STATISTIC(NumFastIselFailIntToPtr,"Fast isel fails on IntToPtr");
00125 STATISTIC(NumFastIselFailPtrToInt,"Fast isel fails on PtrToInt");
00126 STATISTIC(NumFastIselFailBitCast,"Fast isel fails on BitCast");
00127 
00128   // Other instructions...
00129 STATISTIC(NumFastIselFailICmp,"Fast isel fails on ICmp");
00130 STATISTIC(NumFastIselFailFCmp,"Fast isel fails on FCmp");
00131 STATISTIC(NumFastIselFailPHI,"Fast isel fails on PHI");
00132 STATISTIC(NumFastIselFailSelect,"Fast isel fails on Select");
00133 STATISTIC(NumFastIselFailCall,"Fast isel fails on Call");
00134 STATISTIC(NumFastIselFailShl,"Fast isel fails on Shl");
00135 STATISTIC(NumFastIselFailLShr,"Fast isel fails on LShr");
00136 STATISTIC(NumFastIselFailAShr,"Fast isel fails on AShr");
00137 STATISTIC(NumFastIselFailVAArg,"Fast isel fails on VAArg");
00138 STATISTIC(NumFastIselFailExtractElement,"Fast isel fails on ExtractElement");
00139 STATISTIC(NumFastIselFailInsertElement,"Fast isel fails on InsertElement");
00140 STATISTIC(NumFastIselFailShuffleVector,"Fast isel fails on ShuffleVector");
00141 STATISTIC(NumFastIselFailExtractValue,"Fast isel fails on ExtractValue");
00142 STATISTIC(NumFastIselFailInsertValue,"Fast isel fails on InsertValue");
00143 STATISTIC(NumFastIselFailLandingPad,"Fast isel fails on LandingPad");
00144 
00145 // Intrinsic instructions...
00146 STATISTIC(NumFastIselFailIntrinsicCall, "Fast isel fails on Intrinsic call");
00147 STATISTIC(NumFastIselFailSAddWithOverflow,
00148           "Fast isel fails on sadd.with.overflow");
00149 STATISTIC(NumFastIselFailUAddWithOverflow,
00150           "Fast isel fails on uadd.with.overflow");
00151 STATISTIC(NumFastIselFailSSubWithOverflow,
00152           "Fast isel fails on ssub.with.overflow");
00153 STATISTIC(NumFastIselFailUSubWithOverflow,
00154           "Fast isel fails on usub.with.overflow");
00155 STATISTIC(NumFastIselFailSMulWithOverflow,
00156           "Fast isel fails on smul.with.overflow");
00157 STATISTIC(NumFastIselFailUMulWithOverflow,
00158           "Fast isel fails on umul.with.overflow");
00159 STATISTIC(NumFastIselFailFrameaddress, "Fast isel fails on Frameaddress");
00160 STATISTIC(NumFastIselFailSqrt, "Fast isel fails on sqrt call");
00161 STATISTIC(NumFastIselFailStackMap, "Fast isel fails on StackMap call");
00162 STATISTIC(NumFastIselFailPatchPoint, "Fast isel fails on PatchPoint call");
00163 #endif
00164 
00165 static cl::opt<bool>
00166 EnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
00167           cl::desc("Enable verbose messages in the \"fast\" "
00168                    "instruction selector"));
00169 static cl::opt<bool>
00170 EnableFastISelAbort("fast-isel-abort", cl::Hidden,
00171           cl::desc("Enable abort calls when \"fast\" instruction selection "
00172                    "fails to lower an instruction"));
00173 static cl::opt<bool>
00174 EnableFastISelAbortArgs("fast-isel-abort-args", cl::Hidden,
00175           cl::desc("Enable abort calls when \"fast\" instruction selection "
00176                    "fails to lower a formal argument"));
00177 
00178 static cl::opt<bool>
00179 UseMBPI("use-mbpi",
00180         cl::desc("use Machine Branch Probability Info"),
00181         cl::init(true), cl::Hidden);
00182 
00183 #ifndef NDEBUG
00184 static cl::opt<bool>
00185 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
00186           cl::desc("Pop up a window to show dags before the first "
00187                    "dag combine pass"));
00188 static cl::opt<bool>
00189 ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
00190           cl::desc("Pop up a window to show dags before legalize types"));
00191 static cl::opt<bool>
00192 ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
00193           cl::desc("Pop up a window to show dags before legalize"));
00194 static cl::opt<bool>
00195 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
00196           cl::desc("Pop up a window to show dags before the second "
00197                    "dag combine pass"));
00198 static cl::opt<bool>
00199 ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
00200           cl::desc("Pop up a window to show dags before the post legalize types"
00201                    " dag combine pass"));
00202 static cl::opt<bool>
00203 ViewISelDAGs("view-isel-dags", cl::Hidden,
00204           cl::desc("Pop up a window to show isel dags as they are selected"));
00205 static cl::opt<bool>
00206 ViewSchedDAGs("view-sched-dags", cl::Hidden,
00207           cl::desc("Pop up a window to show sched dags as they are processed"));
00208 static cl::opt<bool>
00209 ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
00210       cl::desc("Pop up a window to show SUnit dags after they are processed"));
00211 #else
00212 static const bool ViewDAGCombine1 = false,
00213                   ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
00214                   ViewDAGCombine2 = false,
00215                   ViewDAGCombineLT = false,
00216                   ViewISelDAGs = false, ViewSchedDAGs = false,
00217                   ViewSUnitDAGs = false;
00218 #endif
00219 
00220 //===---------------------------------------------------------------------===//
00221 ///
00222 /// RegisterScheduler class - Track the registration of instruction schedulers.
00223 ///
00224 //===---------------------------------------------------------------------===//
00225 MachinePassRegistry RegisterScheduler::Registry;
00226 
00227 //===---------------------------------------------------------------------===//
00228 ///
00229 /// ISHeuristic command line option for instruction schedulers.
00230 ///
00231 //===---------------------------------------------------------------------===//
00232 static cl::opt<RegisterScheduler::FunctionPassCtor, false,
00233                RegisterPassParser<RegisterScheduler> >
00234 ISHeuristic("pre-RA-sched",
00235             cl::init(&createDefaultScheduler), cl::Hidden,
00236             cl::desc("Instruction schedulers available (before register"
00237                      " allocation):"));
00238 
00239 static RegisterScheduler
00240 defaultListDAGScheduler("default", "Best scheduler for the target",
00241                         createDefaultScheduler);
00242 
00243 namespace llvm {
00244   //===--------------------------------------------------------------------===//
00245   /// \brief This class is used by SelectionDAGISel to temporarily override
00246   /// the optimization level on a per-function basis.
00247   class OptLevelChanger {
00248     SelectionDAGISel &IS;
00249     CodeGenOpt::Level SavedOptLevel;
00250     bool SavedFastISel;
00251 
00252   public:
00253     OptLevelChanger(SelectionDAGISel &ISel,
00254                     CodeGenOpt::Level NewOptLevel) : IS(ISel) {
00255       SavedOptLevel = IS.OptLevel;
00256       if (NewOptLevel == SavedOptLevel)
00257         return;
00258       IS.OptLevel = NewOptLevel;
00259       IS.TM.setOptLevel(NewOptLevel);
00260       SavedFastISel = IS.TM.Options.EnableFastISel;
00261       if (NewOptLevel == CodeGenOpt::None)
00262         IS.TM.setFastISel(true);
00263       DEBUG(dbgs() << "\nChanging optimization level for Function "
00264             << IS.MF->getFunction()->getName() << "\n");
00265       DEBUG(dbgs() << "\tBefore: -O" << SavedOptLevel
00266             << " ; After: -O" << NewOptLevel << "\n");
00267     }
00268 
00269     ~OptLevelChanger() {
00270       if (IS.OptLevel == SavedOptLevel)
00271         return;
00272       DEBUG(dbgs() << "\nRestoring optimization level for Function "
00273             << IS.MF->getFunction()->getName() << "\n");
00274       DEBUG(dbgs() << "\tBefore: -O" << IS.OptLevel
00275             << " ; After: -O" << SavedOptLevel << "\n");
00276       IS.OptLevel = SavedOptLevel;
00277       IS.TM.setOptLevel(SavedOptLevel);
00278       IS.TM.setFastISel(SavedFastISel);
00279     }
00280   };
00281 
00282   //===--------------------------------------------------------------------===//
00283   /// createDefaultScheduler - This creates an instruction scheduler appropriate
00284   /// for the target.
00285   ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
00286                                              CodeGenOpt::Level OptLevel) {
00287     const TargetLowering *TLI = IS->getTargetLowering();
00288     const TargetSubtargetInfo &ST = IS->TM.getSubtarget<TargetSubtargetInfo>();
00289 
00290     if (OptLevel == CodeGenOpt::None || ST.useMachineScheduler() ||
00291         TLI->getSchedulingPreference() == Sched::Source)
00292       return createSourceListDAGScheduler(IS, OptLevel);
00293     if (TLI->getSchedulingPreference() == Sched::RegPressure)
00294       return createBURRListDAGScheduler(IS, OptLevel);
00295     if (TLI->getSchedulingPreference() == Sched::Hybrid)
00296       return createHybridListDAGScheduler(IS, OptLevel);
00297     if (TLI->getSchedulingPreference() == Sched::VLIW)
00298       return createVLIWDAGScheduler(IS, OptLevel);
00299     assert(TLI->getSchedulingPreference() == Sched::ILP &&
00300            "Unknown sched type!");
00301     return createILPListDAGScheduler(IS, OptLevel);
00302   }
00303 }
00304 
00305 // EmitInstrWithCustomInserter - This method should be implemented by targets
00306 // that mark instructions with the 'usesCustomInserter' flag.  These
00307 // instructions are special in various ways, which require special support to
00308 // insert.  The specified MachineInstr is created but not inserted into any
00309 // basic blocks, and this method is called to expand it into a sequence of
00310 // instructions, potentially also creating new basic blocks and control flow.
00311 // When new basic blocks are inserted and the edges from MBB to its successors
00312 // are modified, the method should insert pairs of <OldSucc, NewSucc> into the
00313 // DenseMap.
00314 MachineBasicBlock *
00315 TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
00316                                             MachineBasicBlock *MBB) const {
00317 #ifndef NDEBUG
00318   dbgs() << "If a target marks an instruction with "
00319           "'usesCustomInserter', it must implement "
00320           "TargetLowering::EmitInstrWithCustomInserter!";
00321 #endif
00322   llvm_unreachable(nullptr);
00323 }
00324 
00325 void TargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
00326                                                    SDNode *Node) const {
00327   assert(!MI->hasPostISelHook() &&
00328          "If a target marks an instruction with 'hasPostISelHook', "
00329          "it must implement TargetLowering::AdjustInstrPostInstrSelection!");
00330 }
00331 
00332 //===----------------------------------------------------------------------===//
00333 // SelectionDAGISel code
00334 //===----------------------------------------------------------------------===//
00335 
00336 SelectionDAGISel::SelectionDAGISel(TargetMachine &tm,
00337                                    CodeGenOpt::Level OL) :
00338   MachineFunctionPass(ID), TM(tm),
00339   FuncInfo(new FunctionLoweringInfo(TM)),
00340   CurDAG(new SelectionDAG(tm, OL)),
00341   SDB(new SelectionDAGBuilder(*CurDAG, *FuncInfo, OL)),
00342   GFI(),
00343   OptLevel(OL),
00344   DAGSize(0) {
00345     initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
00346     initializeAliasAnalysisAnalysisGroup(*PassRegistry::getPassRegistry());
00347     initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
00348     initializeTargetLibraryInfoPass(*PassRegistry::getPassRegistry());
00349   }
00350 
00351 SelectionDAGISel::~SelectionDAGISel() {
00352   delete SDB;
00353   delete CurDAG;
00354   delete FuncInfo;
00355 }
00356 
00357 void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
00358   AU.addRequired<AliasAnalysis>();
00359   AU.addPreserved<AliasAnalysis>();
00360   AU.addRequired<GCModuleInfo>();
00361   AU.addPreserved<GCModuleInfo>();
00362   AU.addRequired<TargetLibraryInfo>();
00363   if (UseMBPI && OptLevel != CodeGenOpt::None)
00364     AU.addRequired<BranchProbabilityInfo>();
00365   MachineFunctionPass::getAnalysisUsage(AU);
00366 }
00367 
00368 /// SplitCriticalSideEffectEdges - Look for critical edges with a PHI value that
00369 /// may trap on it.  In this case we have to split the edge so that the path
00370 /// through the predecessor block that doesn't go to the phi block doesn't
00371 /// execute the possibly trapping instruction.
00372 ///
00373 /// This is required for correctness, so it must be done at -O0.
00374 ///
00375 static void SplitCriticalSideEffectEdges(Function &Fn, Pass *SDISel) {
00376   // Loop for blocks with phi nodes.
00377   for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
00378     PHINode *PN = dyn_cast<PHINode>(BB->begin());
00379     if (!PN) continue;
00380 
00381   ReprocessBlock:
00382     // For each block with a PHI node, check to see if any of the input values
00383     // are potentially trapping constant expressions.  Constant expressions are
00384     // the only potentially trapping value that can occur as the argument to a
00385     // PHI.
00386     for (BasicBlock::iterator I = BB->begin(); (PN = dyn_cast<PHINode>(I)); ++I)
00387       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00388         ConstantExpr *CE = dyn_cast<ConstantExpr>(PN->getIncomingValue(i));
00389         if (!CE || !CE->canTrap()) continue;
00390 
00391         // The only case we have to worry about is when the edge is critical.
00392         // Since this block has a PHI Node, we assume it has multiple input
00393         // edges: check to see if the pred has multiple successors.
00394         BasicBlock *Pred = PN->getIncomingBlock(i);
00395         if (Pred->getTerminator()->getNumSuccessors() == 1)
00396           continue;
00397 
00398         // Okay, we have to split this edge.
00399         SplitCriticalEdge(Pred->getTerminator(),
00400                           GetSuccessorNumber(Pred, BB), SDISel, true);
00401         goto ReprocessBlock;
00402       }
00403   }
00404 }
00405 
00406 bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
00407   // Do some sanity-checking on the command-line options.
00408   assert((!EnableFastISelVerbose || TM.Options.EnableFastISel) &&
00409          "-fast-isel-verbose requires -fast-isel");
00410   assert((!EnableFastISelAbort || TM.Options.EnableFastISel) &&
00411          "-fast-isel-abort requires -fast-isel");
00412 
00413   const Function &Fn = *mf.getFunction();
00414   const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
00415   const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
00416   const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
00417 
00418   MF = &mf;
00419   RegInfo = &MF->getRegInfo();
00420   AA = &getAnalysis<AliasAnalysis>();
00421   LibInfo = &getAnalysis<TargetLibraryInfo>();
00422   GFI = Fn.hasGC() ? &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn) : nullptr;
00423 
00424   TM.resetTargetOptions(MF);
00425 
00426   // Reset OptLevel to None for optnone functions.
00427   CodeGenOpt::Level NewOptLevel = OptLevel;
00428   if (Fn.hasFnAttribute(Attribute::OptimizeNone))
00429     NewOptLevel = CodeGenOpt::None;
00430   OptLevelChanger OLC(*this, NewOptLevel);
00431 
00432   DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n");
00433 
00434   SplitCriticalSideEffectEdges(const_cast<Function&>(Fn), this);
00435 
00436   CurDAG->init(*MF, TLI);
00437   FuncInfo->set(Fn, *MF, CurDAG);
00438 
00439   if (UseMBPI && OptLevel != CodeGenOpt::None)
00440     FuncInfo->BPI = &getAnalysis<BranchProbabilityInfo>();
00441   else
00442     FuncInfo->BPI = nullptr;
00443 
00444   SDB->init(GFI, *AA, LibInfo);
00445 
00446   MF->setHasInlineAsm(false);
00447 
00448   SelectAllBasicBlocks(Fn);
00449 
00450   // If the first basic block in the function has live ins that need to be
00451   // copied into vregs, emit the copies into the top of the block before
00452   // emitting the code for the block.
00453   MachineBasicBlock *EntryMBB = MF->begin();
00454   RegInfo->EmitLiveInCopies(EntryMBB, TRI, TII);
00455 
00456   DenseMap<unsigned, unsigned> LiveInMap;
00457   if (!FuncInfo->ArgDbgValues.empty())
00458     for (MachineRegisterInfo::livein_iterator LI = RegInfo->livein_begin(),
00459            E = RegInfo->livein_end(); LI != E; ++LI)
00460       if (LI->second)
00461         LiveInMap.insert(std::make_pair(LI->first, LI->second));
00462 
00463   // Insert DBG_VALUE instructions for function arguments to the entry block.
00464   for (unsigned i = 0, e = FuncInfo->ArgDbgValues.size(); i != e; ++i) {
00465     MachineInstr *MI = FuncInfo->ArgDbgValues[e-i-1];
00466     bool hasFI = MI->getOperand(0).isFI();
00467     unsigned Reg =
00468         hasFI ? TRI.getFrameRegister(*MF) : MI->getOperand(0).getReg();
00469     if (TargetRegisterInfo::isPhysicalRegister(Reg))
00470       EntryMBB->insert(EntryMBB->begin(), MI);
00471     else {
00472       MachineInstr *Def = RegInfo->getVRegDef(Reg);
00473       if (Def) {
00474         MachineBasicBlock::iterator InsertPos = Def;
00475         // FIXME: VR def may not be in entry block.
00476         Def->getParent()->insert(std::next(InsertPos), MI);
00477       } else
00478         DEBUG(dbgs() << "Dropping debug info for dead vreg"
00479               << TargetRegisterInfo::virtReg2Index(Reg) << "\n");
00480     }
00481 
00482     // If Reg is live-in then update debug info to track its copy in a vreg.
00483     DenseMap<unsigned, unsigned>::iterator LDI = LiveInMap.find(Reg);
00484     if (LDI != LiveInMap.end()) {
00485       assert(!hasFI && "There's no handling of frame pointer updating here yet "
00486                        "- add if needed");
00487       MachineInstr *Def = RegInfo->getVRegDef(LDI->second);
00488       MachineBasicBlock::iterator InsertPos = Def;
00489       const MDNode *Variable =
00490         MI->getOperand(MI->getNumOperands()-1).getMetadata();
00491       bool IsIndirect = MI->isIndirectDebugValue();
00492       unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
00493       // Def is never a terminator here, so it is ok to increment InsertPos.
00494       BuildMI(*EntryMBB, ++InsertPos, MI->getDebugLoc(),
00495               TII.get(TargetOpcode::DBG_VALUE),
00496               IsIndirect,
00497               LDI->second, Offset, Variable);
00498 
00499       // If this vreg is directly copied into an exported register then
00500       // that COPY instructions also need DBG_VALUE, if it is the only
00501       // user of LDI->second.
00502       MachineInstr *CopyUseMI = nullptr;
00503       for (MachineRegisterInfo::use_instr_iterator
00504            UI = RegInfo->use_instr_begin(LDI->second),
00505            E = RegInfo->use_instr_end(); UI != E; ) {
00506         MachineInstr *UseMI = &*(UI++);
00507         if (UseMI->isDebugValue()) continue;
00508         if (UseMI->isCopy() && !CopyUseMI && UseMI->getParent() == EntryMBB) {
00509           CopyUseMI = UseMI; continue;
00510         }
00511         // Otherwise this is another use or second copy use.
00512         CopyUseMI = nullptr; break;
00513       }
00514       if (CopyUseMI) {
00515         MachineInstr *NewMI =
00516           BuildMI(*MF, CopyUseMI->getDebugLoc(),
00517                   TII.get(TargetOpcode::DBG_VALUE),
00518                   IsIndirect,
00519                   CopyUseMI->getOperand(0).getReg(),
00520                   Offset, Variable);
00521         MachineBasicBlock::iterator Pos = CopyUseMI;
00522         EntryMBB->insertAfter(Pos, NewMI);
00523       }
00524     }
00525   }
00526 
00527   // Determine if there are any calls in this machine function.
00528   MachineFrameInfo *MFI = MF->getFrameInfo();
00529   for (const auto &MBB : *MF) {
00530     if (MFI->hasCalls() && MF->hasInlineAsm())
00531       break;
00532 
00533     for (const auto &MI : MBB) {
00534       const MCInstrDesc &MCID =
00535           TM.getSubtargetImpl()->getInstrInfo()->get(MI.getOpcode());
00536       if ((MCID.isCall() && !MCID.isReturn()) ||
00537           MI.isStackAligningInlineAsm()) {
00538         MFI->setHasCalls(true);
00539       }
00540       if (MI.isInlineAsm()) {
00541         MF->setHasInlineAsm(true);
00542       }
00543     }
00544   }
00545 
00546   // Determine if there is a call to setjmp in the machine function.
00547   MF->setExposesReturnsTwice(Fn.callsFunctionThatReturnsTwice());
00548 
00549   // Replace forward-declared registers with the registers containing
00550   // the desired value.
00551   MachineRegisterInfo &MRI = MF->getRegInfo();
00552   for (DenseMap<unsigned, unsigned>::iterator
00553        I = FuncInfo->RegFixups.begin(), E = FuncInfo->RegFixups.end();
00554        I != E; ++I) {
00555     unsigned From = I->first;
00556     unsigned To = I->second;
00557     // If To is also scheduled to be replaced, find what its ultimate
00558     // replacement is.
00559     for (;;) {
00560       DenseMap<unsigned, unsigned>::iterator J = FuncInfo->RegFixups.find(To);
00561       if (J == E) break;
00562       To = J->second;
00563     }
00564     // Make sure the new register has a sufficiently constrained register class.
00565     if (TargetRegisterInfo::isVirtualRegister(From) &&
00566         TargetRegisterInfo::isVirtualRegister(To))
00567       MRI.constrainRegClass(To, MRI.getRegClass(From));
00568     // Replace it.
00569     MRI.replaceRegWith(From, To);
00570   }
00571 
00572   // Freeze the set of reserved registers now that MachineFrameInfo has been
00573   // set up. All the information required by getReservedRegs() should be
00574   // available now.
00575   MRI.freezeReservedRegs(*MF);
00576 
00577   // Release function-specific state. SDB and CurDAG are already cleared
00578   // at this point.
00579   FuncInfo->clear();
00580 
00581   DEBUG(dbgs() << "*** MachineFunction at end of ISel ***\n");
00582   DEBUG(MF->print(dbgs()));
00583 
00584   return true;
00585 }
00586 
00587 void SelectionDAGISel::SelectBasicBlock(BasicBlock::const_iterator Begin,
00588                                         BasicBlock::const_iterator End,
00589                                         bool &HadTailCall) {
00590   // Lower all of the non-terminator instructions. If a call is emitted
00591   // as a tail call, cease emitting nodes for this block. Terminators
00592   // are handled below.
00593   for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I)
00594     SDB->visit(*I);
00595 
00596   // Make sure the root of the DAG is up-to-date.
00597   CurDAG->setRoot(SDB->getControlRoot());
00598   HadTailCall = SDB->HasTailCall;
00599   SDB->clear();
00600 
00601   // Final step, emit the lowered DAG as machine code.
00602   CodeGenAndEmitDAG();
00603 }
00604 
00605 void SelectionDAGISel::ComputeLiveOutVRegInfo() {
00606   SmallPtrSet<SDNode*, 128> VisitedNodes;
00607   SmallVector<SDNode*, 128> Worklist;
00608 
00609   Worklist.push_back(CurDAG->getRoot().getNode());
00610 
00611   APInt KnownZero;
00612   APInt KnownOne;
00613 
00614   do {
00615     SDNode *N = Worklist.pop_back_val();
00616 
00617     // If we've already seen this node, ignore it.
00618     if (!VisitedNodes.insert(N))
00619       continue;
00620 
00621     // Otherwise, add all chain operands to the worklist.
00622     for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
00623       if (N->getOperand(i).getValueType() == MVT::Other)
00624         Worklist.push_back(N->getOperand(i).getNode());
00625 
00626     // If this is a CopyToReg with a vreg dest, process it.
00627     if (N->getOpcode() != ISD::CopyToReg)
00628       continue;
00629 
00630     unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
00631     if (!TargetRegisterInfo::isVirtualRegister(DestReg))
00632       continue;
00633 
00634     // Ignore non-scalar or non-integer values.
00635     SDValue Src = N->getOperand(2);
00636     EVT SrcVT = Src.getValueType();
00637     if (!SrcVT.isInteger() || SrcVT.isVector())
00638       continue;
00639 
00640     unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
00641     CurDAG->computeKnownBits(Src, KnownZero, KnownOne);
00642     FuncInfo->AddLiveOutRegInfo(DestReg, NumSignBits, KnownZero, KnownOne);
00643   } while (!Worklist.empty());
00644 }
00645 
00646 void SelectionDAGISel::CodeGenAndEmitDAG() {
00647   std::string GroupName;
00648   if (TimePassesIsEnabled)
00649     GroupName = "Instruction Selection and Scheduling";
00650   std::string BlockName;
00651   int BlockNumber = -1;
00652   (void)BlockNumber;
00653 #ifdef NDEBUG
00654   if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
00655       ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
00656       ViewSUnitDAGs)
00657 #endif
00658   {
00659     BlockNumber = FuncInfo->MBB->getNumber();
00660     BlockName = MF->getName().str() + ":" +
00661                 FuncInfo->MBB->getBasicBlock()->getName().str();
00662   }
00663   DEBUG(dbgs() << "Initial selection DAG: BB#" << BlockNumber
00664         << " '" << BlockName << "'\n"; CurDAG->dump());
00665 
00666   if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
00667 
00668   // Run the DAG combiner in pre-legalize mode.
00669   {
00670     NamedRegionTimer T("DAG Combining 1", GroupName, TimePassesIsEnabled);
00671     CurDAG->Combine(BeforeLegalizeTypes, *AA, OptLevel);
00672   }
00673 
00674   DEBUG(dbgs() << "Optimized lowered selection DAG: BB#" << BlockNumber
00675         << " '" << BlockName << "'\n"; CurDAG->dump());
00676 
00677   // Second step, hack on the DAG until it only uses operations and types that
00678   // the target supports.
00679   if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
00680                                                BlockName);
00681 
00682   bool Changed;
00683   {
00684     NamedRegionTimer T("Type Legalization", GroupName, TimePassesIsEnabled);
00685     Changed = CurDAG->LegalizeTypes();
00686   }
00687 
00688   DEBUG(dbgs() << "Type-legalized selection DAG: BB#" << BlockNumber
00689         << " '" << BlockName << "'\n"; CurDAG->dump());
00690 
00691   CurDAG->NewNodesMustHaveLegalTypes = true;
00692 
00693   if (Changed) {
00694     if (ViewDAGCombineLT)
00695       CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
00696 
00697     // Run the DAG combiner in post-type-legalize mode.
00698     {
00699       NamedRegionTimer T("DAG Combining after legalize types", GroupName,
00700                          TimePassesIsEnabled);
00701       CurDAG->Combine(AfterLegalizeTypes, *AA, OptLevel);
00702     }
00703 
00704     DEBUG(dbgs() << "Optimized type-legalized selection DAG: BB#" << BlockNumber
00705           << " '" << BlockName << "'\n"; CurDAG->dump());
00706 
00707   }
00708 
00709   {
00710     NamedRegionTimer T("Vector Legalization", GroupName, TimePassesIsEnabled);
00711     Changed = CurDAG->LegalizeVectors();
00712   }
00713 
00714   if (Changed) {
00715     {
00716       NamedRegionTimer T("Type Legalization 2", GroupName, TimePassesIsEnabled);
00717       CurDAG->LegalizeTypes();
00718     }
00719 
00720     if (ViewDAGCombineLT)
00721       CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
00722 
00723     // Run the DAG combiner in post-type-legalize mode.
00724     {
00725       NamedRegionTimer T("DAG Combining after legalize vectors", GroupName,
00726                          TimePassesIsEnabled);
00727       CurDAG->Combine(AfterLegalizeVectorOps, *AA, OptLevel);
00728     }
00729 
00730     DEBUG(dbgs() << "Optimized vector-legalized selection DAG: BB#"
00731           << BlockNumber << " '" << BlockName << "'\n"; CurDAG->dump());
00732   }
00733 
00734   if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
00735 
00736   {
00737     NamedRegionTimer T("DAG Legalization", GroupName, TimePassesIsEnabled);
00738     CurDAG->Legalize();
00739   }
00740 
00741   DEBUG(dbgs() << "Legalized selection DAG: BB#" << BlockNumber
00742         << " '" << BlockName << "'\n"; CurDAG->dump());
00743 
00744   if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
00745 
00746   // Run the DAG combiner in post-legalize mode.
00747   {
00748     NamedRegionTimer T("DAG Combining 2", GroupName, TimePassesIsEnabled);
00749     CurDAG->Combine(AfterLegalizeDAG, *AA, OptLevel);
00750   }
00751 
00752   DEBUG(dbgs() << "Optimized legalized selection DAG: BB#" << BlockNumber
00753         << " '" << BlockName << "'\n"; CurDAG->dump());
00754 
00755   if (OptLevel != CodeGenOpt::None)
00756     ComputeLiveOutVRegInfo();
00757 
00758   if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
00759 
00760   // Third, instruction select all of the operations to machine code, adding the
00761   // code to the MachineBasicBlock.
00762   {
00763     NamedRegionTimer T("Instruction Selection", GroupName, TimePassesIsEnabled);
00764     DoInstructionSelection();
00765   }
00766 
00767   DEBUG(dbgs() << "Selected selection DAG: BB#" << BlockNumber
00768         << " '" << BlockName << "'\n"; CurDAG->dump());
00769 
00770   if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
00771 
00772   // Schedule machine code.
00773   ScheduleDAGSDNodes *Scheduler = CreateScheduler();
00774   {
00775     NamedRegionTimer T("Instruction Scheduling", GroupName,
00776                        TimePassesIsEnabled);
00777     Scheduler->Run(CurDAG, FuncInfo->MBB);
00778   }
00779 
00780   if (ViewSUnitDAGs) Scheduler->viewGraph();
00781 
00782   // Emit machine code to BB.  This can change 'BB' to the last block being
00783   // inserted into.
00784   MachineBasicBlock *FirstMBB = FuncInfo->MBB, *LastMBB;
00785   {
00786     NamedRegionTimer T("Instruction Creation", GroupName, TimePassesIsEnabled);
00787 
00788     // FuncInfo->InsertPt is passed by reference and set to the end of the
00789     // scheduled instructions.
00790     LastMBB = FuncInfo->MBB = Scheduler->EmitSchedule(FuncInfo->InsertPt);
00791   }
00792 
00793   // If the block was split, make sure we update any references that are used to
00794   // update PHI nodes later on.
00795   if (FirstMBB != LastMBB)
00796     SDB->UpdateSplitBlock(FirstMBB, LastMBB);
00797 
00798   // Free the scheduler state.
00799   {
00800     NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName,
00801                        TimePassesIsEnabled);
00802     delete Scheduler;
00803   }
00804 
00805   // Free the SelectionDAG state, now that we're finished with it.
00806   CurDAG->clear();
00807 }
00808 
00809 namespace {
00810 /// ISelUpdater - helper class to handle updates of the instruction selection
00811 /// graph.
00812 class ISelUpdater : public SelectionDAG::DAGUpdateListener {
00813   SelectionDAG::allnodes_iterator &ISelPosition;
00814 public:
00815   ISelUpdater(SelectionDAG &DAG, SelectionDAG::allnodes_iterator &isp)
00816     : SelectionDAG::DAGUpdateListener(DAG), ISelPosition(isp) {}
00817 
00818   /// NodeDeleted - Handle nodes deleted from the graph. If the node being
00819   /// deleted is the current ISelPosition node, update ISelPosition.
00820   ///
00821   void NodeDeleted(SDNode *N, SDNode *E) override {
00822     if (ISelPosition == SelectionDAG::allnodes_iterator(N))
00823       ++ISelPosition;
00824   }
00825 };
00826 } // end anonymous namespace
00827 
00828 void SelectionDAGISel::DoInstructionSelection() {
00829   DEBUG(dbgs() << "===== Instruction selection begins: BB#"
00830         << FuncInfo->MBB->getNumber()
00831         << " '" << FuncInfo->MBB->getName() << "'\n");
00832 
00833   PreprocessISelDAG();
00834 
00835   // Select target instructions for the DAG.
00836   {
00837     // Number all nodes with a topological order and set DAGSize.
00838     DAGSize = CurDAG->AssignTopologicalOrder();
00839 
00840     // Create a dummy node (which is not added to allnodes), that adds
00841     // a reference to the root node, preventing it from being deleted,
00842     // and tracking any changes of the root.
00843     HandleSDNode Dummy(CurDAG->getRoot());
00844     SelectionDAG::allnodes_iterator ISelPosition (CurDAG->getRoot().getNode());
00845     ++ISelPosition;
00846 
00847     // Make sure that ISelPosition gets properly updated when nodes are deleted
00848     // in calls made from this function.
00849     ISelUpdater ISU(*CurDAG, ISelPosition);
00850 
00851     // The AllNodes list is now topological-sorted. Visit the
00852     // nodes by starting at the end of the list (the root of the
00853     // graph) and preceding back toward the beginning (the entry
00854     // node).
00855     while (ISelPosition != CurDAG->allnodes_begin()) {
00856       SDNode *Node = --ISelPosition;
00857       // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
00858       // but there are currently some corner cases that it misses. Also, this
00859       // makes it theoretically possible to disable the DAGCombiner.
00860       if (Node->use_empty())
00861         continue;
00862 
00863       SDNode *ResNode = Select(Node);
00864 
00865       // FIXME: This is pretty gross.  'Select' should be changed to not return
00866       // anything at all and this code should be nuked with a tactical strike.
00867 
00868       // If node should not be replaced, continue with the next one.
00869       if (ResNode == Node || Node->getOpcode() == ISD::DELETED_NODE)
00870         continue;
00871       // Replace node.
00872       if (ResNode) {
00873         ReplaceUses(Node, ResNode);
00874       }
00875 
00876       // If after the replacement this node is not used any more,
00877       // remove this dead node.
00878       if (Node->use_empty()) // Don't delete EntryToken, etc.
00879         CurDAG->RemoveDeadNode(Node);
00880     }
00881 
00882     CurDAG->setRoot(Dummy.getValue());
00883   }
00884 
00885   DEBUG(dbgs() << "===== Instruction selection ends:\n");
00886 
00887   PostprocessISelDAG();
00888 }
00889 
00890 /// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and
00891 /// do other setup for EH landing-pad blocks.
00892 void SelectionDAGISel::PrepareEHLandingPad() {
00893   MachineBasicBlock *MBB = FuncInfo->MBB;
00894 
00895   // Add a label to mark the beginning of the landing pad.  Deletion of the
00896   // landing pad can thus be detected via the MachineModuleInfo.
00897   MCSymbol *Label = MF->getMMI().addLandingPad(MBB);
00898 
00899   // Assign the call site to the landing pad's begin label.
00900   MF->getMMI().setCallSiteLandingPad(Label, SDB->LPadToCallSiteMap[MBB]);
00901 
00902   const MCInstrDesc &II =
00903       TM.getSubtargetImpl()->getInstrInfo()->get(TargetOpcode::EH_LABEL);
00904   BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II)
00905     .addSym(Label);
00906 
00907   // Mark exception register as live in.
00908   const TargetLowering *TLI = getTargetLowering();
00909   const TargetRegisterClass *PtrRC = TLI->getRegClassFor(TLI->getPointerTy());
00910   if (unsigned Reg = TLI->getExceptionPointerRegister())
00911     FuncInfo->ExceptionPointerVirtReg = MBB->addLiveIn(Reg, PtrRC);
00912 
00913   // Mark exception selector register as live in.
00914   if (unsigned Reg = TLI->getExceptionSelectorRegister())
00915     FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC);
00916 }
00917 
00918 /// isFoldedOrDeadInstruction - Return true if the specified instruction is
00919 /// side-effect free and is either dead or folded into a generated instruction.
00920 /// Return false if it needs to be emitted.
00921 static bool isFoldedOrDeadInstruction(const Instruction *I,
00922                                       FunctionLoweringInfo *FuncInfo) {
00923   return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded.
00924          !isa<TerminatorInst>(I) && // Terminators aren't folded.
00925          !isa<DbgInfoIntrinsic>(I) &&  // Debug instructions aren't folded.
00926          !isa<LandingPadInst>(I) &&    // Landingpad instructions aren't folded.
00927          !FuncInfo->isExportedInst(I); // Exported instrs must be computed.
00928 }
00929 
00930 #ifndef NDEBUG
00931 // Collect per Instruction statistics for fast-isel misses.  Only those
00932 // instructions that cause the bail are accounted for.  It does not account for
00933 // instructions higher in the block.  Thus, summing the per instructions stats
00934 // will not add up to what is reported by NumFastIselFailures.
00935 static void collectFailStats(const Instruction *I) {
00936   switch (I->getOpcode()) {
00937   default: assert (0 && "<Invalid operator> ");
00938 
00939   // Terminators
00940   case Instruction::Ret:         NumFastIselFailRet++; return;
00941   case Instruction::Br:          NumFastIselFailBr++; return;
00942   case Instruction::Switch:      NumFastIselFailSwitch++; return;
00943   case Instruction::IndirectBr:  NumFastIselFailIndirectBr++; return;
00944   case Instruction::Invoke:      NumFastIselFailInvoke++; return;
00945   case Instruction::Resume:      NumFastIselFailResume++; return;
00946   case Instruction::Unreachable: NumFastIselFailUnreachable++; return;
00947 
00948   // Standard binary operators...
00949   case Instruction::Add:  NumFastIselFailAdd++; return;
00950   case Instruction::FAdd: NumFastIselFailFAdd++; return;
00951   case Instruction::Sub:  NumFastIselFailSub++; return;
00952   case Instruction::FSub: NumFastIselFailFSub++; return;
00953   case Instruction::Mul:  NumFastIselFailMul++; return;
00954   case Instruction::FMul: NumFastIselFailFMul++; return;
00955   case Instruction::UDiv: NumFastIselFailUDiv++; return;
00956   case Instruction::SDiv: NumFastIselFailSDiv++; return;
00957   case Instruction::FDiv: NumFastIselFailFDiv++; return;
00958   case Instruction::URem: NumFastIselFailURem++; return;
00959   case Instruction::SRem: NumFastIselFailSRem++; return;
00960   case Instruction::FRem: NumFastIselFailFRem++; return;
00961 
00962   // Logical operators...
00963   case Instruction::And: NumFastIselFailAnd++; return;
00964   case Instruction::Or:  NumFastIselFailOr++; return;
00965   case Instruction::Xor: NumFastIselFailXor++; return;
00966 
00967   // Memory instructions...
00968   case Instruction::Alloca:        NumFastIselFailAlloca++; return;
00969   case Instruction::Load:          NumFastIselFailLoad++; return;
00970   case Instruction::Store:         NumFastIselFailStore++; return;
00971   case Instruction::AtomicCmpXchg: NumFastIselFailAtomicCmpXchg++; return;
00972   case Instruction::AtomicRMW:     NumFastIselFailAtomicRMW++; return;
00973   case Instruction::Fence:         NumFastIselFailFence++; return;
00974   case Instruction::GetElementPtr: NumFastIselFailGetElementPtr++; return;
00975 
00976   // Convert instructions...
00977   case Instruction::Trunc:    NumFastIselFailTrunc++; return;
00978   case Instruction::ZExt:     NumFastIselFailZExt++; return;
00979   case Instruction::SExt:     NumFastIselFailSExt++; return;
00980   case Instruction::FPTrunc:  NumFastIselFailFPTrunc++; return;
00981   case Instruction::FPExt:    NumFastIselFailFPExt++; return;
00982   case Instruction::FPToUI:   NumFastIselFailFPToUI++; return;
00983   case Instruction::FPToSI:   NumFastIselFailFPToSI++; return;
00984   case Instruction::UIToFP:   NumFastIselFailUIToFP++; return;
00985   case Instruction::SIToFP:   NumFastIselFailSIToFP++; return;
00986   case Instruction::IntToPtr: NumFastIselFailIntToPtr++; return;
00987   case Instruction::PtrToInt: NumFastIselFailPtrToInt++; return;
00988   case Instruction::BitCast:  NumFastIselFailBitCast++; return;
00989 
00990   // Other instructions...
00991   case Instruction::ICmp:           NumFastIselFailICmp++; return;
00992   case Instruction::FCmp:           NumFastIselFailFCmp++; return;
00993   case Instruction::PHI:            NumFastIselFailPHI++; return;
00994   case Instruction::Select:         NumFastIselFailSelect++; return;
00995   case Instruction::Call: {
00996     if (auto const *Intrinsic = dyn_cast<IntrinsicInst>(I)) {
00997       switch (Intrinsic->getIntrinsicID()) {
00998       default:
00999         NumFastIselFailIntrinsicCall++; return;
01000       case Intrinsic::sadd_with_overflow:
01001         NumFastIselFailSAddWithOverflow++; return;
01002       case Intrinsic::uadd_with_overflow:
01003         NumFastIselFailUAddWithOverflow++; return;
01004       case Intrinsic::ssub_with_overflow:
01005         NumFastIselFailSSubWithOverflow++; return;
01006       case Intrinsic::usub_with_overflow:
01007         NumFastIselFailUSubWithOverflow++; return;
01008       case Intrinsic::smul_with_overflow:
01009         NumFastIselFailSMulWithOverflow++; return;
01010       case Intrinsic::umul_with_overflow:
01011         NumFastIselFailUMulWithOverflow++; return;
01012       case Intrinsic::frameaddress:
01013         NumFastIselFailFrameaddress++; return;
01014       case Intrinsic::sqrt:
01015           NumFastIselFailSqrt++; return;
01016       case Intrinsic::experimental_stackmap:
01017         NumFastIselFailStackMap++; return;
01018       case Intrinsic::experimental_patchpoint_void: // fall-through
01019       case Intrinsic::experimental_patchpoint_i64:
01020         NumFastIselFailPatchPoint++; return;
01021       }
01022     }
01023     NumFastIselFailCall++;
01024     return;
01025   }
01026   case Instruction::Shl:            NumFastIselFailShl++; return;
01027   case Instruction::LShr:           NumFastIselFailLShr++; return;
01028   case Instruction::AShr:           NumFastIselFailAShr++; return;
01029   case Instruction::VAArg:          NumFastIselFailVAArg++; return;
01030   case Instruction::ExtractElement: NumFastIselFailExtractElement++; return;
01031   case Instruction::InsertElement:  NumFastIselFailInsertElement++; return;
01032   case Instruction::ShuffleVector:  NumFastIselFailShuffleVector++; return;
01033   case Instruction::ExtractValue:   NumFastIselFailExtractValue++; return;
01034   case Instruction::InsertValue:    NumFastIselFailInsertValue++; return;
01035   case Instruction::LandingPad:     NumFastIselFailLandingPad++; return;
01036   }
01037 }
01038 #endif
01039 
01040 void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
01041   // Initialize the Fast-ISel state, if needed.
01042   FastISel *FastIS = nullptr;
01043   if (TM.Options.EnableFastISel)
01044     FastIS = getTargetLowering()->createFastISel(*FuncInfo, LibInfo);
01045 
01046   // Iterate over all basic blocks in the function.
01047   ReversePostOrderTraversal<const Function*> RPOT(&Fn);
01048   for (ReversePostOrderTraversal<const Function*>::rpo_iterator
01049        I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
01050     const BasicBlock *LLVMBB = *I;
01051 
01052     if (OptLevel != CodeGenOpt::None) {
01053       bool AllPredsVisited = true;
01054       for (const_pred_iterator PI = pred_begin(LLVMBB), PE = pred_end(LLVMBB);
01055            PI != PE; ++PI) {
01056         if (!FuncInfo->VisitedBBs.count(*PI)) {
01057           AllPredsVisited = false;
01058           break;
01059         }
01060       }
01061 
01062       if (AllPredsVisited) {
01063         for (BasicBlock::const_iterator I = LLVMBB->begin();
01064              const PHINode *PN = dyn_cast<PHINode>(I); ++I)
01065           FuncInfo->ComputePHILiveOutRegInfo(PN);
01066       } else {
01067         for (BasicBlock::const_iterator I = LLVMBB->begin();
01068              const PHINode *PN = dyn_cast<PHINode>(I); ++I)
01069           FuncInfo->InvalidatePHILiveOutRegInfo(PN);
01070       }
01071 
01072       FuncInfo->VisitedBBs.insert(LLVMBB);
01073     }
01074 
01075     BasicBlock::const_iterator const Begin = LLVMBB->getFirstNonPHI();
01076     BasicBlock::const_iterator const End = LLVMBB->end();
01077     BasicBlock::const_iterator BI = End;
01078 
01079     FuncInfo->MBB = FuncInfo->MBBMap[LLVMBB];
01080     FuncInfo->InsertPt = FuncInfo->MBB->getFirstNonPHI();
01081 
01082     // Setup an EH landing-pad block.
01083     FuncInfo->ExceptionPointerVirtReg = 0;
01084     FuncInfo->ExceptionSelectorVirtReg = 0;
01085     if (FuncInfo->MBB->isLandingPad())
01086       PrepareEHLandingPad();
01087 
01088     // Before doing SelectionDAG ISel, see if FastISel has been requested.
01089     if (FastIS) {
01090       FastIS->startNewBlock();
01091 
01092       // Emit code for any incoming arguments. This must happen before
01093       // beginning FastISel on the entry block.
01094       if (LLVMBB == &Fn.getEntryBlock()) {
01095         ++NumEntryBlocks;
01096 
01097         // Lower any arguments needed in this block if this is the entry block.
01098         if (!FastIS->lowerArguments()) {
01099           // Fast isel failed to lower these arguments
01100           ++NumFastIselFailLowerArguments;
01101           if (EnableFastISelAbortArgs)
01102             llvm_unreachable("FastISel didn't lower all arguments");
01103 
01104           // Use SelectionDAG argument lowering
01105           LowerArguments(Fn);
01106           CurDAG->setRoot(SDB->getControlRoot());
01107           SDB->clear();
01108           CodeGenAndEmitDAG();
01109         }
01110 
01111         // If we inserted any instructions at the beginning, make a note of
01112         // where they are, so we can be sure to emit subsequent instructions
01113         // after them.
01114         if (FuncInfo->InsertPt != FuncInfo->MBB->begin())
01115           FastIS->setLastLocalValue(std::prev(FuncInfo->InsertPt));
01116         else
01117           FastIS->setLastLocalValue(nullptr);
01118       }
01119 
01120       unsigned NumFastIselRemaining = std::distance(Begin, End);
01121       // Do FastISel on as many instructions as possible.
01122       for (; BI != Begin; --BI) {
01123         const Instruction *Inst = std::prev(BI);
01124 
01125         // If we no longer require this instruction, skip it.
01126         if (isFoldedOrDeadInstruction(Inst, FuncInfo)) {
01127           --NumFastIselRemaining;
01128           continue;
01129         }
01130 
01131         // Bottom-up: reset the insert pos at the top, after any local-value
01132         // instructions.
01133         FastIS->recomputeInsertPt();
01134 
01135         // Try to select the instruction with FastISel.
01136         if (FastIS->selectInstruction(Inst)) {
01137           --NumFastIselRemaining;
01138           ++NumFastIselSuccess;
01139           // If fast isel succeeded, skip over all the folded instructions, and
01140           // then see if there is a load right before the selected instructions.
01141           // Try to fold the load if so.
01142           const Instruction *BeforeInst = Inst;
01143           while (BeforeInst != Begin) {
01144             BeforeInst = std::prev(BasicBlock::const_iterator(BeforeInst));
01145             if (!isFoldedOrDeadInstruction(BeforeInst, FuncInfo))
01146               break;
01147           }
01148           if (BeforeInst != Inst && isa<LoadInst>(BeforeInst) &&
01149               BeforeInst->hasOneUse() &&
01150               FastIS->tryToFoldLoad(cast<LoadInst>(BeforeInst), Inst)) {
01151             // If we succeeded, don't re-select the load.
01152             BI = std::next(BasicBlock::const_iterator(BeforeInst));
01153             --NumFastIselRemaining;
01154             ++NumFastIselSuccess;
01155           }
01156           continue;
01157         }
01158 
01159 #ifndef NDEBUG
01160         if (EnableFastISelVerbose2)
01161           collectFailStats(Inst);
01162 #endif
01163 
01164         // Then handle certain instructions as single-LLVM-Instruction blocks.
01165         if (isa<CallInst>(Inst)) {
01166 
01167           if (EnableFastISelVerbose || EnableFastISelAbort) {
01168             dbgs() << "FastISel missed call: ";
01169             Inst->dump();
01170           }
01171 
01172           if (!Inst->getType()->isVoidTy() && !Inst->use_empty()) {
01173             unsigned &R = FuncInfo->ValueMap[Inst];
01174             if (!R)
01175               R = FuncInfo->CreateRegs(Inst->getType());
01176           }
01177 
01178           bool HadTailCall = false;
01179           MachineBasicBlock::iterator SavedInsertPt = FuncInfo->InsertPt;
01180           SelectBasicBlock(Inst, BI, HadTailCall);
01181 
01182           // If the call was emitted as a tail call, we're done with the block.
01183           // We also need to delete any previously emitted instructions.
01184           if (HadTailCall) {
01185             FastIS->removeDeadCode(SavedInsertPt, FuncInfo->MBB->end());
01186             --BI;
01187             break;
01188           }
01189 
01190           // Recompute NumFastIselRemaining as Selection DAG instruction
01191           // selection may have handled the call, input args, etc.
01192           unsigned RemainingNow = std::distance(Begin, BI);
01193           NumFastIselFailures += NumFastIselRemaining - RemainingNow;
01194           NumFastIselRemaining = RemainingNow;
01195           continue;
01196         }
01197 
01198         if (isa<TerminatorInst>(Inst) && !isa<BranchInst>(Inst)) {
01199           // Don't abort, and use a different message for terminator misses.
01200           NumFastIselFailures += NumFastIselRemaining;
01201           if (EnableFastISelVerbose || EnableFastISelAbort) {
01202             dbgs() << "FastISel missed terminator: ";
01203             Inst->dump();
01204           }
01205         } else {
01206           NumFastIselFailures += NumFastIselRemaining;
01207           if (EnableFastISelVerbose || EnableFastISelAbort) {
01208             dbgs() << "FastISel miss: ";
01209             Inst->dump();
01210           }
01211           if (EnableFastISelAbort)
01212             // The "fast" selector couldn't handle something and bailed.
01213             // For the purpose of debugging, just abort.
01214             llvm_unreachable("FastISel didn't select the entire block");
01215         }
01216         break;
01217       }
01218 
01219       FastIS->recomputeInsertPt();
01220     } else {
01221       // Lower any arguments needed in this block if this is the entry block.
01222       if (LLVMBB == &Fn.getEntryBlock()) {
01223         ++NumEntryBlocks;
01224         LowerArguments(Fn);
01225       }
01226     }
01227 
01228     if (Begin != BI)
01229       ++NumDAGBlocks;
01230     else
01231       ++NumFastIselBlocks;
01232 
01233     if (Begin != BI) {
01234       // Run SelectionDAG instruction selection on the remainder of the block
01235       // not handled by FastISel. If FastISel is not run, this is the entire
01236       // block.
01237       bool HadTailCall;
01238       SelectBasicBlock(Begin, BI, HadTailCall);
01239     }
01240 
01241     FinishBasicBlock();
01242     FuncInfo->PHINodesToUpdate.clear();
01243   }
01244 
01245   delete FastIS;
01246   SDB->clearDanglingDebugInfo();
01247   SDB->SPDescriptor.resetPerFunctionState();
01248 }
01249 
01250 /// Given that the input MI is before a partial terminator sequence TSeq, return
01251 /// true if M + TSeq also a partial terminator sequence.
01252 ///
01253 /// A Terminator sequence is a sequence of MachineInstrs which at this point in
01254 /// lowering copy vregs into physical registers, which are then passed into
01255 /// terminator instructors so we can satisfy ABI constraints. A partial
01256 /// terminator sequence is an improper subset of a terminator sequence (i.e. it
01257 /// may be the whole terminator sequence).
01258 static bool MIIsInTerminatorSequence(const MachineInstr *MI) {
01259   // If we do not have a copy or an implicit def, we return true if and only if
01260   // MI is a debug value.
01261   if (!MI->isCopy() && !MI->isImplicitDef())
01262     // Sometimes DBG_VALUE MI sneak in between the copies from the vregs to the
01263     // physical registers if there is debug info associated with the terminator
01264     // of our mbb. We want to include said debug info in our terminator
01265     // sequence, so we return true in that case.
01266     return MI->isDebugValue();
01267 
01268   // We have left the terminator sequence if we are not doing one of the
01269   // following:
01270   //
01271   // 1. Copying a vreg into a physical register.
01272   // 2. Copying a vreg into a vreg.
01273   // 3. Defining a register via an implicit def.
01274 
01275   // OPI should always be a register definition...
01276   MachineInstr::const_mop_iterator OPI = MI->operands_begin();
01277   if (!OPI->isReg() || !OPI->isDef())
01278     return false;
01279 
01280   // Defining any register via an implicit def is always ok.
01281   if (MI->isImplicitDef())
01282     return true;
01283 
01284   // Grab the copy source...
01285   MachineInstr::const_mop_iterator OPI2 = OPI;
01286   ++OPI2;
01287   assert(OPI2 != MI->operands_end()
01288          && "Should have a copy implying we should have 2 arguments.");
01289 
01290   // Make sure that the copy dest is not a vreg when the copy source is a
01291   // physical register.
01292   if (!OPI2->isReg() ||
01293       (!TargetRegisterInfo::isPhysicalRegister(OPI->getReg()) &&
01294        TargetRegisterInfo::isPhysicalRegister(OPI2->getReg())))
01295     return false;
01296 
01297   return true;
01298 }
01299 
01300 /// Find the split point at which to splice the end of BB into its success stack
01301 /// protector check machine basic block.
01302 ///
01303 /// On many platforms, due to ABI constraints, terminators, even before register
01304 /// allocation, use physical registers. This creates an issue for us since
01305 /// physical registers at this point can not travel across basic
01306 /// blocks. Luckily, selectiondag always moves physical registers into vregs
01307 /// when they enter functions and moves them through a sequence of copies back
01308 /// into the physical registers right before the terminator creating a
01309 /// ``Terminator Sequence''. This function is searching for the beginning of the
01310 /// terminator sequence so that we can ensure that we splice off not just the
01311 /// terminator, but additionally the copies that move the vregs into the
01312 /// physical registers.
01313 static MachineBasicBlock::iterator
01314 FindSplitPointForStackProtector(MachineBasicBlock *BB, DebugLoc DL) {
01315   MachineBasicBlock::iterator SplitPoint = BB->getFirstTerminator();
01316   //
01317   if (SplitPoint == BB->begin())
01318     return SplitPoint;
01319 
01320   MachineBasicBlock::iterator Start = BB->begin();
01321   MachineBasicBlock::iterator Previous = SplitPoint;
01322   --Previous;
01323 
01324   while (MIIsInTerminatorSequence(Previous)) {
01325     SplitPoint = Previous;
01326     if (Previous == Start)
01327       break;
01328     --Previous;
01329   }
01330 
01331   return SplitPoint;
01332 }
01333 
01334 void
01335 SelectionDAGISel::FinishBasicBlock() {
01336 
01337   DEBUG(dbgs() << "Total amount of phi nodes to update: "
01338                << FuncInfo->PHINodesToUpdate.size() << "\n";
01339         for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i)
01340           dbgs() << "Node " << i << " : ("
01341                  << FuncInfo->PHINodesToUpdate[i].first
01342                  << ", " << FuncInfo->PHINodesToUpdate[i].second << ")\n");
01343 
01344   const bool MustUpdatePHINodes = SDB->SwitchCases.empty() &&
01345                                   SDB->JTCases.empty() &&
01346                                   SDB->BitTestCases.empty();
01347 
01348   // Next, now that we know what the last MBB the LLVM BB expanded is, update
01349   // PHI nodes in successors.
01350   if (MustUpdatePHINodes) {
01351     for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
01352       MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first);
01353       assert(PHI->isPHI() &&
01354              "This is not a machine PHI node that we are updating!");
01355       if (!FuncInfo->MBB->isSuccessor(PHI->getParent()))
01356         continue;
01357       PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB);
01358     }
01359   }
01360 
01361   // Handle stack protector.
01362   if (SDB->SPDescriptor.shouldEmitStackProtector()) {
01363     MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB();
01364     MachineBasicBlock *SuccessMBB = SDB->SPDescriptor.getSuccessMBB();
01365 
01366     // Find the split point to split the parent mbb. At the same time copy all
01367     // physical registers used in the tail of parent mbb into virtual registers
01368     // before the split point and back into physical registers after the split
01369     // point. This prevents us needing to deal with Live-ins and many other
01370     // register allocation issues caused by us splitting the parent mbb. The
01371     // register allocator will clean up said virtual copies later on.
01372     MachineBasicBlock::iterator SplitPoint =
01373       FindSplitPointForStackProtector(ParentMBB, SDB->getCurDebugLoc());
01374 
01375     // Splice the terminator of ParentMBB into SuccessMBB.
01376     SuccessMBB->splice(SuccessMBB->end(), ParentMBB,
01377                        SplitPoint,
01378                        ParentMBB->end());
01379 
01380     // Add compare/jump on neq/jump to the parent BB.
01381     FuncInfo->MBB = ParentMBB;
01382     FuncInfo->InsertPt = ParentMBB->end();
01383     SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
01384     CurDAG->setRoot(SDB->getRoot());
01385     SDB->clear();
01386     CodeGenAndEmitDAG();
01387 
01388     // CodeGen Failure MBB if we have not codegened it yet.
01389     MachineBasicBlock *FailureMBB = SDB->SPDescriptor.getFailureMBB();
01390     if (!FailureMBB->size()) {
01391       FuncInfo->MBB = FailureMBB;
01392       FuncInfo->InsertPt = FailureMBB->end();
01393       SDB->visitSPDescriptorFailure(SDB->SPDescriptor);
01394       CurDAG->setRoot(SDB->getRoot());
01395       SDB->clear();
01396       CodeGenAndEmitDAG();
01397     }
01398 
01399     // Clear the Per-BB State.
01400     SDB->SPDescriptor.resetPerBBState();
01401   }
01402 
01403   // If we updated PHI Nodes, return early.
01404   if (MustUpdatePHINodes)
01405     return;
01406 
01407   for (unsigned i = 0, e = SDB->BitTestCases.size(); i != e; ++i) {
01408     // Lower header first, if it wasn't already lowered
01409     if (!SDB->BitTestCases[i].Emitted) {
01410       // Set the current basic block to the mbb we wish to insert the code into
01411       FuncInfo->MBB = SDB->BitTestCases[i].Parent;
01412       FuncInfo->InsertPt = FuncInfo->MBB->end();
01413       // Emit the code
01414       SDB->visitBitTestHeader(SDB->BitTestCases[i], FuncInfo->MBB);
01415       CurDAG->setRoot(SDB->getRoot());
01416       SDB->clear();
01417       CodeGenAndEmitDAG();
01418     }
01419 
01420     uint32_t UnhandledWeight = 0;
01421     for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size(); j != ej; ++j)
01422       UnhandledWeight += SDB->BitTestCases[i].Cases[j].ExtraWeight;
01423 
01424     for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size(); j != ej; ++j) {
01425       UnhandledWeight -= SDB->BitTestCases[i].Cases[j].ExtraWeight;
01426       // Set the current basic block to the mbb we wish to insert the code into
01427       FuncInfo->MBB = SDB->BitTestCases[i].Cases[j].ThisBB;
01428       FuncInfo->InsertPt = FuncInfo->MBB->end();
01429       // Emit the code
01430       if (j+1 != ej)
01431         SDB->visitBitTestCase(SDB->BitTestCases[i],
01432                               SDB->BitTestCases[i].Cases[j+1].ThisBB,
01433                               UnhandledWeight,
01434                               SDB->BitTestCases[i].Reg,
01435                               SDB->BitTestCases[i].Cases[j],
01436                               FuncInfo->MBB);
01437       else
01438         SDB->visitBitTestCase(SDB->BitTestCases[i],
01439                               SDB->BitTestCases[i].Default,
01440                               UnhandledWeight,
01441                               SDB->BitTestCases[i].Reg,
01442                               SDB->BitTestCases[i].Cases[j],
01443                               FuncInfo->MBB);
01444 
01445 
01446       CurDAG->setRoot(SDB->getRoot());
01447       SDB->clear();
01448       CodeGenAndEmitDAG();
01449     }
01450 
01451     // Update PHI Nodes
01452     for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
01453          pi != pe; ++pi) {
01454       MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first);
01455       MachineBasicBlock *PHIBB = PHI->getParent();
01456       assert(PHI->isPHI() &&
01457              "This is not a machine PHI node that we are updating!");
01458       // This is "default" BB. We have two jumps to it. From "header" BB and
01459       // from last "case" BB.
01460       if (PHIBB == SDB->BitTestCases[i].Default)
01461         PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second)
01462            .addMBB(SDB->BitTestCases[i].Parent)
01463            .addReg(FuncInfo->PHINodesToUpdate[pi].second)
01464            .addMBB(SDB->BitTestCases[i].Cases.back().ThisBB);
01465       // One of "cases" BB.
01466       for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size();
01467            j != ej; ++j) {
01468         MachineBasicBlock* cBB = SDB->BitTestCases[i].Cases[j].ThisBB;
01469         if (cBB->isSuccessor(PHIBB))
01470           PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(cBB);
01471       }
01472     }
01473   }
01474   SDB->BitTestCases.clear();
01475 
01476   // If the JumpTable record is filled in, then we need to emit a jump table.
01477   // Updating the PHI nodes is tricky in this case, since we need to determine
01478   // whether the PHI is a successor of the range check MBB or the jump table MBB
01479   for (unsigned i = 0, e = SDB->JTCases.size(); i != e; ++i) {
01480     // Lower header first, if it wasn't already lowered
01481     if (!SDB->JTCases[i].first.Emitted) {
01482       // Set the current basic block to the mbb we wish to insert the code into
01483       FuncInfo->MBB = SDB->JTCases[i].first.HeaderBB;
01484       FuncInfo->InsertPt = FuncInfo->MBB->end();
01485       // Emit the code
01486       SDB->visitJumpTableHeader(SDB->JTCases[i].second, SDB->JTCases[i].first,
01487                                 FuncInfo->MBB);
01488       CurDAG->setRoot(SDB->getRoot());
01489       SDB->clear();
01490       CodeGenAndEmitDAG();
01491     }
01492 
01493     // Set the current basic block to the mbb we wish to insert the code into
01494     FuncInfo->MBB = SDB->JTCases[i].second.MBB;
01495     FuncInfo->InsertPt = FuncInfo->MBB->end();
01496     // Emit the code
01497     SDB->visitJumpTable(SDB->JTCases[i].second);
01498     CurDAG->setRoot(SDB->getRoot());
01499     SDB->clear();
01500     CodeGenAndEmitDAG();
01501 
01502     // Update PHI Nodes
01503     for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
01504          pi != pe; ++pi) {
01505       MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first);
01506       MachineBasicBlock *PHIBB = PHI->getParent();
01507       assert(PHI->isPHI() &&
01508              "This is not a machine PHI node that we are updating!");
01509       // "default" BB. We can go there only from header BB.
01510       if (PHIBB == SDB->JTCases[i].second.Default)
01511         PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second)
01512            .addMBB(SDB->JTCases[i].first.HeaderBB);
01513       // JT BB. Just iterate over successors here
01514       if (FuncInfo->MBB->isSuccessor(PHIBB))
01515         PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(FuncInfo->MBB);
01516     }
01517   }
01518   SDB->JTCases.clear();
01519 
01520   // If the switch block involved a branch to one of the actual successors, we
01521   // need to update PHI nodes in that block.
01522   for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
01523     MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first);
01524     assert(PHI->isPHI() &&
01525            "This is not a machine PHI node that we are updating!");
01526     if (FuncInfo->MBB->isSuccessor(PHI->getParent()))
01527       PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB);
01528   }
01529 
01530   // If we generated any switch lowering information, build and codegen any
01531   // additional DAGs necessary.
01532   for (unsigned i = 0, e = SDB->SwitchCases.size(); i != e; ++i) {
01533     // Set the current basic block to the mbb we wish to insert the code into
01534     FuncInfo->MBB = SDB->SwitchCases[i].ThisBB;
01535     FuncInfo->InsertPt = FuncInfo->MBB->end();
01536 
01537     // Determine the unique successors.
01538     SmallVector<MachineBasicBlock *, 2> Succs;
01539     Succs.push_back(SDB->SwitchCases[i].TrueBB);
01540     if (SDB->SwitchCases[i].TrueBB != SDB->SwitchCases[i].FalseBB)
01541       Succs.push_back(SDB->SwitchCases[i].FalseBB);
01542 
01543     // Emit the code. Note that this could result in FuncInfo->MBB being split.
01544     SDB->visitSwitchCase(SDB->SwitchCases[i], FuncInfo->MBB);
01545     CurDAG->setRoot(SDB->getRoot());
01546     SDB->clear();
01547     CodeGenAndEmitDAG();
01548 
01549     // Remember the last block, now that any splitting is done, for use in
01550     // populating PHI nodes in successors.
01551     MachineBasicBlock *ThisBB = FuncInfo->MBB;
01552 
01553     // Handle any PHI nodes in successors of this chunk, as if we were coming
01554     // from the original BB before switch expansion.  Note that PHI nodes can
01555     // occur multiple times in PHINodesToUpdate.  We have to be very careful to
01556     // handle them the right number of times.
01557     for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
01558       FuncInfo->MBB = Succs[i];
01559       FuncInfo->InsertPt = FuncInfo->MBB->end();
01560       // FuncInfo->MBB may have been removed from the CFG if a branch was
01561       // constant folded.
01562       if (ThisBB->isSuccessor(FuncInfo->MBB)) {
01563         for (MachineBasicBlock::iterator
01564              MBBI = FuncInfo->MBB->begin(), MBBE = FuncInfo->MBB->end();
01565              MBBI != MBBE && MBBI->isPHI(); ++MBBI) {
01566           MachineInstrBuilder PHI(*MF, MBBI);
01567           // This value for this PHI node is recorded in PHINodesToUpdate.
01568           for (unsigned pn = 0; ; ++pn) {
01569             assert(pn != FuncInfo->PHINodesToUpdate.size() &&
01570                    "Didn't find PHI entry!");
01571             if (FuncInfo->PHINodesToUpdate[pn].first == PHI) {
01572               PHI.addReg(FuncInfo->PHINodesToUpdate[pn].second).addMBB(ThisBB);
01573               break;
01574             }
01575           }
01576         }
01577       }
01578     }
01579   }
01580   SDB->SwitchCases.clear();
01581 }
01582 
01583 
01584 /// Create the scheduler. If a specific scheduler was specified
01585 /// via the SchedulerRegistry, use it, otherwise select the
01586 /// one preferred by the target.
01587 ///
01588 ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
01589   RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
01590 
01591   if (!Ctor) {
01592     Ctor = ISHeuristic;
01593     RegisterScheduler::setDefault(Ctor);
01594   }
01595 
01596   return Ctor(this, OptLevel);
01597 }
01598 
01599 //===----------------------------------------------------------------------===//
01600 // Helper functions used by the generated instruction selector.
01601 //===----------------------------------------------------------------------===//
01602 // Calls to these methods are generated by tblgen.
01603 
01604 /// CheckAndMask - The isel is trying to match something like (and X, 255).  If
01605 /// the dag combiner simplified the 255, we still want to match.  RHS is the
01606 /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
01607 /// specified in the .td file (e.g. 255).
01608 bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
01609                                     int64_t DesiredMaskS) const {
01610   const APInt &ActualMask = RHS->getAPIntValue();
01611   const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
01612 
01613   // If the actual mask exactly matches, success!
01614   if (ActualMask == DesiredMask)
01615     return true;
01616 
01617   // If the actual AND mask is allowing unallowed bits, this doesn't match.
01618   if (ActualMask.intersects(~DesiredMask))
01619     return false;
01620 
01621   // Otherwise, the DAG Combiner may have proven that the value coming in is
01622   // either already zero or is not demanded.  Check for known zero input bits.
01623   APInt NeededMask = DesiredMask & ~ActualMask;
01624   if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
01625     return true;
01626 
01627   // TODO: check to see if missing bits are just not demanded.
01628 
01629   // Otherwise, this pattern doesn't match.
01630   return false;
01631 }
01632 
01633 /// CheckOrMask - The isel is trying to match something like (or X, 255).  If
01634 /// the dag combiner simplified the 255, we still want to match.  RHS is the
01635 /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
01636 /// specified in the .td file (e.g. 255).
01637 bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
01638                                    int64_t DesiredMaskS) const {
01639   const APInt &ActualMask = RHS->getAPIntValue();
01640   const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
01641 
01642   // If the actual mask exactly matches, success!
01643   if (ActualMask == DesiredMask)
01644     return true;
01645 
01646   // If the actual AND mask is allowing unallowed bits, this doesn't match.
01647   if (ActualMask.intersects(~DesiredMask))
01648     return false;
01649 
01650   // Otherwise, the DAG Combiner may have proven that the value coming in is
01651   // either already zero or is not demanded.  Check for known zero input bits.
01652   APInt NeededMask = DesiredMask & ~ActualMask;
01653 
01654   APInt KnownZero, KnownOne;
01655   CurDAG->computeKnownBits(LHS, KnownZero, KnownOne);
01656 
01657   // If all the missing bits in the or are already known to be set, match!
01658   if ((NeededMask & KnownOne) == NeededMask)
01659     return true;
01660 
01661   // TODO: check to see if missing bits are just not demanded.
01662 
01663   // Otherwise, this pattern doesn't match.
01664   return false;
01665 }
01666 
01667 
01668 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
01669 /// by tblgen.  Others should not call it.
01670 void SelectionDAGISel::
01671 SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
01672   std::vector<SDValue> InOps;
01673   std::swap(InOps, Ops);
01674 
01675   Ops.push_back(InOps[InlineAsm::Op_InputChain]); // 0
01676   Ops.push_back(InOps[InlineAsm::Op_AsmString]);  // 1
01677   Ops.push_back(InOps[InlineAsm::Op_MDNode]);     // 2, !srcloc
01678   Ops.push_back(InOps[InlineAsm::Op_ExtraInfo]);  // 3 (SideEffect, AlignStack)
01679 
01680   unsigned i = InlineAsm::Op_FirstOperand, e = InOps.size();
01681   if (InOps[e-1].getValueType() == MVT::Glue)
01682     --e;  // Don't process a glue operand if it is here.
01683 
01684   while (i != e) {
01685     unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
01686     if (!InlineAsm::isMemKind(Flags)) {
01687       // Just skip over this operand, copying the operands verbatim.
01688       Ops.insert(Ops.end(), InOps.begin()+i,
01689                  InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
01690       i += InlineAsm::getNumOperandRegisters(Flags) + 1;
01691     } else {
01692       assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
01693              "Memory operand with multiple values?");
01694       // Otherwise, this is a memory operand.  Ask the target to select it.
01695       std::vector<SDValue> SelOps;
01696       if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps))
01697         report_fatal_error("Could not match memory address.  Inline asm"
01698                            " failure!");
01699 
01700       // Add this to the output node.
01701       unsigned NewFlags =
01702         InlineAsm::getFlagWord(InlineAsm::Kind_Mem, SelOps.size());
01703       Ops.push_back(CurDAG->getTargetConstant(NewFlags, MVT::i32));
01704       Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
01705       i += 2;
01706     }
01707   }
01708 
01709   // Add the glue input back if present.
01710   if (e != InOps.size())
01711     Ops.push_back(InOps.back());
01712 }
01713 
01714 /// findGlueUse - Return use of MVT::Glue value produced by the specified
01715 /// SDNode.
01716 ///
01717 static SDNode *findGlueUse(SDNode *N) {
01718   unsigned FlagResNo = N->getNumValues()-1;
01719   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
01720     SDUse &Use = I.getUse();
01721     if (Use.getResNo() == FlagResNo)
01722       return Use.getUser();
01723   }
01724   return nullptr;
01725 }
01726 
01727 /// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
01728 /// This function recursively traverses up the operand chain, ignoring
01729 /// certain nodes.
01730 static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
01731                           SDNode *Root, SmallPtrSetImpl<SDNode*> &Visited,
01732                           bool IgnoreChains) {
01733   // The NodeID's are given uniques ID's where a node ID is guaranteed to be
01734   // greater than all of its (recursive) operands.  If we scan to a point where
01735   // 'use' is smaller than the node we're scanning for, then we know we will
01736   // never find it.
01737   //
01738   // The Use may be -1 (unassigned) if it is a newly allocated node.  This can
01739   // happen because we scan down to newly selected nodes in the case of glue
01740   // uses.
01741   if ((Use->getNodeId() < Def->getNodeId() && Use->getNodeId() != -1))
01742     return false;
01743 
01744   // Don't revisit nodes if we already scanned it and didn't fail, we know we
01745   // won't fail if we scan it again.
01746   if (!Visited.insert(Use))
01747     return false;
01748 
01749   for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
01750     // Ignore chain uses, they are validated by HandleMergeInputChains.
01751     if (Use->getOperand(i).getValueType() == MVT::Other && IgnoreChains)
01752       continue;
01753 
01754     SDNode *N = Use->getOperand(i).getNode();
01755     if (N == Def) {
01756       if (Use == ImmedUse || Use == Root)
01757         continue;  // We are not looking for immediate use.
01758       assert(N != Root);
01759       return true;
01760     }
01761 
01762     // Traverse up the operand chain.
01763     if (findNonImmUse(N, Def, ImmedUse, Root, Visited, IgnoreChains))
01764       return true;
01765   }
01766   return false;
01767 }
01768 
01769 /// IsProfitableToFold - Returns true if it's profitable to fold the specific
01770 /// operand node N of U during instruction selection that starts at Root.
01771 bool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
01772                                           SDNode *Root) const {
01773   if (OptLevel == CodeGenOpt::None) return false;
01774   return N.hasOneUse();
01775 }
01776 
01777 /// IsLegalToFold - Returns true if the specific operand node N of
01778 /// U can be folded during instruction selection that starts at Root.
01779 bool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
01780                                      CodeGenOpt::Level OptLevel,
01781                                      bool IgnoreChains) {
01782   if (OptLevel == CodeGenOpt::None) return false;
01783 
01784   // If Root use can somehow reach N through a path that that doesn't contain
01785   // U then folding N would create a cycle. e.g. In the following
01786   // diagram, Root can reach N through X. If N is folded into into Root, then
01787   // X is both a predecessor and a successor of U.
01788   //
01789   //          [N*]           //
01790   //         ^   ^           //
01791   //        /     \          //
01792   //      [U*]    [X]?       //
01793   //        ^     ^          //
01794   //         \   /           //
01795   //          \ /            //
01796   //         [Root*]         //
01797   //
01798   // * indicates nodes to be folded together.
01799   //
01800   // If Root produces glue, then it gets (even more) interesting. Since it
01801   // will be "glued" together with its glue use in the scheduler, we need to
01802   // check if it might reach N.
01803   //
01804   //          [N*]           //
01805   //         ^   ^           //
01806   //        /     \          //
01807   //      [U*]    [X]?       //
01808   //        ^       ^        //
01809   //         \       \       //
01810   //          \      |       //
01811   //         [Root*] |       //
01812   //          ^      |       //
01813   //          f      |       //
01814   //          |      /       //
01815   //         [Y]    /        //
01816   //           ^   /         //
01817   //           f  /          //
01818   //           | /           //
01819   //          [GU]           //
01820   //
01821   // If GU (glue use) indirectly reaches N (the load), and Root folds N
01822   // (call it Fold), then X is a predecessor of GU and a successor of
01823   // Fold. But since Fold and GU are glued together, this will create
01824   // a cycle in the scheduling graph.
01825 
01826   // If the node has glue, walk down the graph to the "lowest" node in the
01827   // glueged set.
01828   EVT VT = Root->getValueType(Root->getNumValues()-1);
01829   while (VT == MVT::Glue) {
01830     SDNode *GU = findGlueUse(Root);
01831     if (!GU)
01832       break;
01833     Root = GU;
01834     VT = Root->getValueType(Root->getNumValues()-1);
01835 
01836     // If our query node has a glue result with a use, we've walked up it.  If
01837     // the user (which has already been selected) has a chain or indirectly uses
01838     // the chain, our WalkChainUsers predicate will not consider it.  Because of
01839     // this, we cannot ignore chains in this predicate.
01840     IgnoreChains = false;
01841   }
01842 
01843 
01844   SmallPtrSet<SDNode*, 16> Visited;
01845   return !findNonImmUse(Root, N.getNode(), U, Root, Visited, IgnoreChains);
01846 }
01847 
01848 SDNode *SelectionDAGISel::Select_INLINEASM(SDNode *N) {
01849   std::vector<SDValue> Ops(N->op_begin(), N->op_end());
01850   SelectInlineAsmMemoryOperands(Ops);
01851 
01852   EVT VTs[] = { MVT::Other, MVT::Glue };
01853   SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N), VTs, Ops);
01854   New->setNodeId(-1);
01855   return New.getNode();
01856 }
01857 
01858 SDNode
01859 *SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) {
01860   SDLoc dl(Op);
01861   MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(Op->getOperand(0));
01862   const MDString *RegStr = dyn_cast<MDString>(MD->getMD()->getOperand(0));
01863   unsigned Reg = getTargetLowering()->getRegisterByName(
01864                  RegStr->getString().data(), Op->getValueType(0));
01865   SDValue New = CurDAG->getCopyFromReg(
01866                         CurDAG->getEntryNode(), dl, Reg, Op->getValueType(0));
01867   New->setNodeId(-1);
01868   return New.getNode();
01869 }
01870 
01871 SDNode
01872 *SelectionDAGISel::Select_WRITE_REGISTER(SDNode *Op) {
01873   SDLoc dl(Op);
01874   MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(Op->getOperand(1));
01875   const MDString *RegStr = dyn_cast<MDString>(MD->getMD()->getOperand(0));
01876   unsigned Reg = getTargetLowering()->getRegisterByName(
01877                  RegStr->getString().data(), Op->getOperand(2).getValueType());
01878   SDValue New = CurDAG->getCopyToReg(
01879                         CurDAG->getEntryNode(), dl, Reg, Op->getOperand(2));
01880   New->setNodeId(-1);
01881   return New.getNode();
01882 }
01883 
01884 
01885 
01886 SDNode *SelectionDAGISel::Select_UNDEF(SDNode *N) {
01887   return CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF,N->getValueType(0));
01888 }
01889 
01890 /// GetVBR - decode a vbr encoding whose top bit is set.
01891 LLVM_ATTRIBUTE_ALWAYS_INLINE static uint64_t
01892 GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) {
01893   assert(Val >= 128 && "Not a VBR");
01894   Val &= 127;  // Remove first vbr bit.
01895 
01896   unsigned Shift = 7;
01897   uint64_t NextBits;
01898   do {
01899     NextBits = MatcherTable[Idx++];
01900     Val |= (NextBits&127) << Shift;
01901     Shift += 7;
01902   } while (NextBits & 128);
01903 
01904   return Val;
01905 }
01906 
01907 
01908 /// UpdateChainsAndGlue - When a match is complete, this method updates uses of
01909 /// interior glue and chain results to use the new glue and chain results.
01910 void SelectionDAGISel::
01911 UpdateChainsAndGlue(SDNode *NodeToMatch, SDValue InputChain,
01912                     const SmallVectorImpl<SDNode*> &ChainNodesMatched,
01913                     SDValue InputGlue,
01914                     const SmallVectorImpl<SDNode*> &GlueResultNodesMatched,
01915                     bool isMorphNodeTo) {
01916   SmallVector<SDNode*, 4> NowDeadNodes;
01917 
01918   // Now that all the normal results are replaced, we replace the chain and
01919   // glue results if present.
01920   if (!ChainNodesMatched.empty()) {
01921     assert(InputChain.getNode() &&
01922            "Matched input chains but didn't produce a chain");
01923     // Loop over all of the nodes we matched that produced a chain result.
01924     // Replace all the chain results with the final chain we ended up with.
01925     for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
01926       SDNode *ChainNode = ChainNodesMatched[i];
01927 
01928       // If this node was already deleted, don't look at it.
01929       if (ChainNode->getOpcode() == ISD::DELETED_NODE)
01930         continue;
01931 
01932       // Don't replace the results of the root node if we're doing a
01933       // MorphNodeTo.
01934       if (ChainNode == NodeToMatch && isMorphNodeTo)
01935         continue;
01936 
01937       SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
01938       if (ChainVal.getValueType() == MVT::Glue)
01939         ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
01940       assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
01941       CurDAG->ReplaceAllUsesOfValueWith(ChainVal, InputChain);
01942 
01943       // If the node became dead and we haven't already seen it, delete it.
01944       if (ChainNode->use_empty() &&
01945           !std::count(NowDeadNodes.begin(), NowDeadNodes.end(), ChainNode))
01946         NowDeadNodes.push_back(ChainNode);
01947     }
01948   }
01949 
01950   // If the result produces glue, update any glue results in the matched
01951   // pattern with the glue result.
01952   if (InputGlue.getNode()) {
01953     // Handle any interior nodes explicitly marked.
01954     for (unsigned i = 0, e = GlueResultNodesMatched.size(); i != e; ++i) {
01955       SDNode *FRN = GlueResultNodesMatched[i];
01956 
01957       // If this node was already deleted, don't look at it.
01958       if (FRN->getOpcode() == ISD::DELETED_NODE)
01959         continue;
01960 
01961       assert(FRN->getValueType(FRN->getNumValues()-1) == MVT::Glue &&
01962              "Doesn't have a glue result");
01963       CurDAG->ReplaceAllUsesOfValueWith(SDValue(FRN, FRN->getNumValues()-1),
01964                                         InputGlue);
01965 
01966       // If the node became dead and we haven't already seen it, delete it.
01967       if (FRN->use_empty() &&
01968           !std::count(NowDeadNodes.begin(), NowDeadNodes.end(), FRN))
01969         NowDeadNodes.push_back(FRN);
01970     }
01971   }
01972 
01973   if (!NowDeadNodes.empty())
01974     CurDAG->RemoveDeadNodes(NowDeadNodes);
01975 
01976   DEBUG(dbgs() << "ISEL: Match complete!\n");
01977 }
01978 
01979 enum ChainResult {
01980   CR_Simple,
01981   CR_InducesCycle,
01982   CR_LeadsToInteriorNode
01983 };
01984 
01985 /// WalkChainUsers - Walk down the users of the specified chained node that is
01986 /// part of the pattern we're matching, looking at all of the users we find.
01987 /// This determines whether something is an interior node, whether we have a
01988 /// non-pattern node in between two pattern nodes (which prevent folding because
01989 /// it would induce a cycle) and whether we have a TokenFactor node sandwiched
01990 /// between pattern nodes (in which case the TF becomes part of the pattern).
01991 ///
01992 /// The walk we do here is guaranteed to be small because we quickly get down to
01993 /// already selected nodes "below" us.
01994 static ChainResult
01995 WalkChainUsers(const SDNode *ChainedNode,
01996                SmallVectorImpl<SDNode*> &ChainedNodesInPattern,
01997                SmallVectorImpl<SDNode*> &InteriorChainedNodes) {
01998   ChainResult Result = CR_Simple;
01999 
02000   for (SDNode::use_iterator UI = ChainedNode->use_begin(),
02001          E = ChainedNode->use_end(); UI != E; ++UI) {
02002     // Make sure the use is of the chain, not some other value we produce.
02003     if (UI.getUse().getValueType() != MVT::Other) continue;
02004 
02005     SDNode *User = *UI;
02006 
02007     if (User->getOpcode() == ISD::HANDLENODE)  // Root of the graph.
02008       continue;
02009 
02010     // If we see an already-selected machine node, then we've gone beyond the
02011     // pattern that we're selecting down into the already selected chunk of the
02012     // DAG.
02013     unsigned UserOpcode = User->getOpcode();
02014     if (User->isMachineOpcode() ||
02015         UserOpcode == ISD::CopyToReg ||
02016         UserOpcode == ISD::CopyFromReg ||
02017         UserOpcode == ISD::INLINEASM ||
02018         UserOpcode == ISD::EH_LABEL ||
02019         UserOpcode == ISD::LIFETIME_START ||
02020         UserOpcode == ISD::LIFETIME_END) {
02021       // If their node ID got reset to -1 then they've already been selected.
02022       // Treat them like a MachineOpcode.
02023       if (User->getNodeId() == -1)
02024         continue;
02025     }
02026 
02027     // If we have a TokenFactor, we handle it specially.
02028     if (User->getOpcode() != ISD::TokenFactor) {
02029       // If the node isn't a token factor and isn't part of our pattern, then it
02030       // must be a random chained node in between two nodes we're selecting.
02031       // This happens when we have something like:
02032       //   x = load ptr
02033       //   call
02034       //   y = x+4
02035       //   store y -> ptr
02036       // Because we structurally match the load/store as a read/modify/write,
02037       // but the call is chained between them.  We cannot fold in this case
02038       // because it would induce a cycle in the graph.
02039       if (!std::count(ChainedNodesInPattern.begin(),
02040                       ChainedNodesInPattern.end(), User))
02041         return CR_InducesCycle;
02042 
02043       // Otherwise we found a node that is part of our pattern.  For example in:
02044       //   x = load ptr
02045       //   y = x+4
02046       //   store y -> ptr
02047       // This would happen when we're scanning down from the load and see the
02048       // store as a user.  Record that there is a use of ChainedNode that is
02049       // part of the pattern and keep scanning uses.
02050       Result = CR_LeadsToInteriorNode;
02051       InteriorChainedNodes.push_back(User);
02052       continue;
02053     }
02054 
02055     // If we found a TokenFactor, there are two cases to consider: first if the
02056     // TokenFactor is just hanging "below" the pattern we're matching (i.e. no
02057     // uses of the TF are in our pattern) we just want to ignore it.  Second,
02058     // the TokenFactor can be sandwiched in between two chained nodes, like so:
02059     //     [Load chain]
02060     //         ^
02061     //         |
02062     //       [Load]
02063     //       ^    ^
02064     //       |    \                    DAG's like cheese
02065     //      /       \                       do you?
02066     //     /         |
02067     // [TokenFactor] [Op]
02068     //     ^          ^
02069     //     |          |
02070     //      \        /
02071     //       \      /
02072     //       [Store]
02073     //
02074     // In this case, the TokenFactor becomes part of our match and we rewrite it
02075     // as a new TokenFactor.
02076     //
02077     // To distinguish these two cases, do a recursive walk down the uses.
02078     switch (WalkChainUsers(User, ChainedNodesInPattern, InteriorChainedNodes)) {
02079     case CR_Simple:
02080       // If the uses of the TokenFactor are just already-selected nodes, ignore
02081       // it, it is "below" our pattern.
02082       continue;
02083     case CR_InducesCycle:
02084       // If the uses of the TokenFactor lead to nodes that are not part of our
02085       // pattern that are not selected, folding would turn this into a cycle,
02086       // bail out now.
02087       return CR_InducesCycle;
02088     case CR_LeadsToInteriorNode:
02089       break;  // Otherwise, keep processing.
02090     }
02091 
02092     // Okay, we know we're in the interesting interior case.  The TokenFactor
02093     // is now going to be considered part of the pattern so that we rewrite its
02094     // uses (it may have uses that are not part of the pattern) with the
02095     // ultimate chain result of the generated code.  We will also add its chain
02096     // inputs as inputs to the ultimate TokenFactor we create.
02097     Result = CR_LeadsToInteriorNode;
02098     ChainedNodesInPattern.push_back(User);
02099     InteriorChainedNodes.push_back(User);
02100     continue;
02101   }
02102 
02103   return Result;
02104 }
02105 
02106 /// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains
02107 /// operation for when the pattern matched at least one node with a chains.  The
02108 /// input vector contains a list of all of the chained nodes that we match.  We
02109 /// must determine if this is a valid thing to cover (i.e. matching it won't
02110 /// induce cycles in the DAG) and if so, creating a TokenFactor node. that will
02111 /// be used as the input node chain for the generated nodes.
02112 static SDValue
02113 HandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched,
02114                        SelectionDAG *CurDAG) {
02115   // Walk all of the chained nodes we've matched, recursively scanning down the
02116   // users of the chain result. This adds any TokenFactor nodes that are caught
02117   // in between chained nodes to the chained and interior nodes list.
02118   SmallVector<SDNode*, 3> InteriorChainedNodes;
02119   for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
02120     if (WalkChainUsers(ChainNodesMatched[i], ChainNodesMatched,
02121                        InteriorChainedNodes) == CR_InducesCycle)
02122       return SDValue(); // Would induce a cycle.
02123   }
02124 
02125   // Okay, we have walked all the matched nodes and collected TokenFactor nodes
02126   // that we are interested in.  Form our input TokenFactor node.
02127   SmallVector<SDValue, 3> InputChains;
02128   for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
02129     // Add the input chain of this node to the InputChains list (which will be
02130     // the operands of the generated TokenFactor) if it's not an interior node.
02131     SDNode *N = ChainNodesMatched[i];
02132     if (N->getOpcode() != ISD::TokenFactor) {
02133       if (std::count(InteriorChainedNodes.begin(),InteriorChainedNodes.end(),N))
02134         continue;
02135 
02136       // Otherwise, add the input chain.
02137       SDValue InChain = ChainNodesMatched[i]->getOperand(0);
02138       assert(InChain.getValueType() == MVT::Other && "Not a chain");
02139       InputChains.push_back(InChain);
02140       continue;
02141     }
02142 
02143     // If we have a token factor, we want to add all inputs of the token factor
02144     // that are not part of the pattern we're matching.
02145     for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
02146       if (!std::count(ChainNodesMatched.begin(), ChainNodesMatched.end(),
02147                       N->getOperand(op).getNode()))
02148         InputChains.push_back(N->getOperand(op));
02149     }
02150   }
02151 
02152   if (InputChains.size() == 1)
02153     return InputChains[0];
02154   return CurDAG->getNode(ISD::TokenFactor, SDLoc(ChainNodesMatched[0]),
02155                          MVT::Other, InputChains);
02156 }
02157 
02158 /// MorphNode - Handle morphing a node in place for the selector.
02159 SDNode *SelectionDAGISel::
02160 MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
02161           ArrayRef<SDValue> Ops, unsigned EmitNodeInfo) {
02162   // It is possible we're using MorphNodeTo to replace a node with no
02163   // normal results with one that has a normal result (or we could be
02164   // adding a chain) and the input could have glue and chains as well.
02165   // In this case we need to shift the operands down.
02166   // FIXME: This is a horrible hack and broken in obscure cases, no worse
02167   // than the old isel though.
02168   int OldGlueResultNo = -1, OldChainResultNo = -1;
02169 
02170   unsigned NTMNumResults = Node->getNumValues();
02171   if (Node->getValueType(NTMNumResults-1) == MVT::Glue) {
02172     OldGlueResultNo = NTMNumResults-1;
02173     if (NTMNumResults != 1 &&
02174         Node->getValueType(NTMNumResults-2) == MVT::Other)
02175       OldChainResultNo = NTMNumResults-2;
02176   } else if (Node->getValueType(NTMNumResults-1) == MVT::Other)
02177     OldChainResultNo = NTMNumResults-1;
02178 
02179   // Call the underlying SelectionDAG routine to do the transmogrification. Note
02180   // that this deletes operands of the old node that become dead.
02181   SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops);
02182 
02183   // MorphNodeTo can operate in two ways: if an existing node with the
02184   // specified operands exists, it can just return it.  Otherwise, it
02185   // updates the node in place to have the requested operands.
02186   if (Res == Node) {
02187     // If we updated the node in place, reset the node ID.  To the isel,
02188     // this should be just like a newly allocated machine node.
02189     Res->setNodeId(-1);
02190   }
02191 
02192   unsigned ResNumResults = Res->getNumValues();
02193   // Move the glue if needed.
02194   if ((EmitNodeInfo & OPFL_GlueOutput) && OldGlueResultNo != -1 &&
02195       (unsigned)OldGlueResultNo != ResNumResults-1)
02196     CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldGlueResultNo),
02197                                       SDValue(Res, ResNumResults-1));
02198 
02199   if ((EmitNodeInfo & OPFL_GlueOutput) != 0)
02200     --ResNumResults;
02201 
02202   // Move the chain reference if needed.
02203   if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 &&
02204       (unsigned)OldChainResultNo != ResNumResults-1)
02205     CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldChainResultNo),
02206                                       SDValue(Res, ResNumResults-1));
02207 
02208   // Otherwise, no replacement happened because the node already exists. Replace
02209   // Uses of the old node with the new one.
02210   if (Res != Node)
02211     CurDAG->ReplaceAllUsesWith(Node, Res);
02212 
02213   return Res;
02214 }
02215 
02216 /// CheckSame - Implements OP_CheckSame.
02217 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02218 CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02219           SDValue N,
02220           const SmallVectorImpl<std::pair<SDValue, SDNode*> > &RecordedNodes) {
02221   // Accept if it is exactly the same as a previously recorded node.
02222   unsigned RecNo = MatcherTable[MatcherIndex++];
02223   assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
02224   return N == RecordedNodes[RecNo].first;
02225 }
02226 
02227 /// CheckChildSame - Implements OP_CheckChildXSame.
02228 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02229 CheckChildSame(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02230              SDValue N,
02231              const SmallVectorImpl<std::pair<SDValue, SDNode*> > &RecordedNodes,
02232              unsigned ChildNo) {
02233   if (ChildNo >= N.getNumOperands())
02234     return false;  // Match fails if out of range child #.
02235   return ::CheckSame(MatcherTable, MatcherIndex, N.getOperand(ChildNo),
02236                      RecordedNodes);
02237 }
02238 
02239 /// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
02240 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02241 CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02242                       const SelectionDAGISel &SDISel) {
02243   return SDISel.CheckPatternPredicate(MatcherTable[MatcherIndex++]);
02244 }
02245 
02246 /// CheckNodePredicate - Implements OP_CheckNodePredicate.
02247 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02248 CheckNodePredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02249                    const SelectionDAGISel &SDISel, SDNode *N) {
02250   return SDISel.CheckNodePredicate(N, MatcherTable[MatcherIndex++]);
02251 }
02252 
02253 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02254 CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02255             SDNode *N) {
02256   uint16_t Opc = MatcherTable[MatcherIndex++];
02257   Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
02258   return N->getOpcode() == Opc;
02259 }
02260 
02261 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02262 CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02263           SDValue N, const TargetLowering *TLI) {
02264   MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02265   if (N.getValueType() == VT) return true;
02266 
02267   // Handle the case when VT is iPTR.
02268   return VT == MVT::iPTR && N.getValueType() == TLI->getPointerTy();
02269 }
02270 
02271 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02272 CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02273                SDValue N, const TargetLowering *TLI, unsigned ChildNo) {
02274   if (ChildNo >= N.getNumOperands())
02275     return false;  // Match fails if out of range child #.
02276   return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI);
02277 }
02278 
02279 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02280 CheckCondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02281               SDValue N) {
02282   return cast<CondCodeSDNode>(N)->get() ==
02283       (ISD::CondCode)MatcherTable[MatcherIndex++];
02284 }
02285 
02286 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02287 CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02288                SDValue N, const TargetLowering *TLI) {
02289   MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02290   if (cast<VTSDNode>(N)->getVT() == VT)
02291     return true;
02292 
02293   // Handle the case when VT is iPTR.
02294   return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI->getPointerTy();
02295 }
02296 
02297 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02298 CheckInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02299              SDValue N) {
02300   int64_t Val = MatcherTable[MatcherIndex++];
02301   if (Val & 128)
02302     Val = GetVBR(Val, MatcherTable, MatcherIndex);
02303 
02304   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
02305   return C && C->getSExtValue() == Val;
02306 }
02307 
02308 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02309 CheckChildInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02310                   SDValue N, unsigned ChildNo) {
02311   if (ChildNo >= N.getNumOperands())
02312     return false;  // Match fails if out of range child #.
02313   return ::CheckInteger(MatcherTable, MatcherIndex, N.getOperand(ChildNo));
02314 }
02315 
02316 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02317 CheckAndImm(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02318             SDValue N, const SelectionDAGISel &SDISel) {
02319   int64_t Val = MatcherTable[MatcherIndex++];
02320   if (Val & 128)
02321     Val = GetVBR(Val, MatcherTable, MatcherIndex);
02322 
02323   if (N->getOpcode() != ISD::AND) return false;
02324 
02325   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
02326   return C && SDISel.CheckAndMask(N.getOperand(0), C, Val);
02327 }
02328 
02329 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
02330 CheckOrImm(const unsigned char *MatcherTable, unsigned &MatcherIndex,
02331            SDValue N, const SelectionDAGISel &SDISel) {
02332   int64_t Val = MatcherTable[MatcherIndex++];
02333   if (Val & 128)
02334     Val = GetVBR(Val, MatcherTable, MatcherIndex);
02335 
02336   if (N->getOpcode() != ISD::OR) return false;
02337 
02338   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
02339   return C && SDISel.CheckOrMask(N.getOperand(0), C, Val);
02340 }
02341 
02342 /// IsPredicateKnownToFail - If we know how and can do so without pushing a
02343 /// scope, evaluate the current node.  If the current predicate is known to
02344 /// fail, set Result=true and return anything.  If the current predicate is
02345 /// known to pass, set Result=false and return the MatcherIndex to continue
02346 /// with.  If the current predicate is unknown, set Result=false and return the
02347 /// MatcherIndex to continue with.
02348 static unsigned IsPredicateKnownToFail(const unsigned char *Table,
02349                                        unsigned Index, SDValue N,
02350                                        bool &Result,
02351                                        const SelectionDAGISel &SDISel,
02352                  SmallVectorImpl<std::pair<SDValue, SDNode*> > &RecordedNodes) {
02353   switch (Table[Index++]) {
02354   default:
02355     Result = false;
02356     return Index-1;  // Could not evaluate this predicate.
02357   case SelectionDAGISel::OPC_CheckSame:
02358     Result = !::CheckSame(Table, Index, N, RecordedNodes);
02359     return Index;
02360   case SelectionDAGISel::OPC_CheckChild0Same:
02361   case SelectionDAGISel::OPC_CheckChild1Same:
02362   case SelectionDAGISel::OPC_CheckChild2Same:
02363   case SelectionDAGISel::OPC_CheckChild3Same:
02364     Result = !::CheckChildSame(Table, Index, N, RecordedNodes,
02365                         Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same);
02366     return Index;
02367   case SelectionDAGISel::OPC_CheckPatternPredicate:
02368     Result = !::CheckPatternPredicate(Table, Index, SDISel);
02369     return Index;
02370   case SelectionDAGISel::OPC_CheckPredicate:
02371     Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode());
02372     return Index;
02373   case SelectionDAGISel::OPC_CheckOpcode:
02374     Result = !::CheckOpcode(Table, Index, N.getNode());
02375     return Index;
02376   case SelectionDAGISel::OPC_CheckType:
02377     Result = !::CheckType(Table, Index, N, SDISel.getTargetLowering());
02378     return Index;
02379   case SelectionDAGISel::OPC_CheckChild0Type:
02380   case SelectionDAGISel::OPC_CheckChild1Type:
02381   case SelectionDAGISel::OPC_CheckChild2Type:
02382   case SelectionDAGISel::OPC_CheckChild3Type:
02383   case SelectionDAGISel::OPC_CheckChild4Type:
02384   case SelectionDAGISel::OPC_CheckChild5Type:
02385   case SelectionDAGISel::OPC_CheckChild6Type:
02386   case SelectionDAGISel::OPC_CheckChild7Type:
02387     Result = !::CheckChildType(Table, Index, N, SDISel.getTargetLowering(),
02388                         Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Type);
02389     return Index;
02390   case SelectionDAGISel::OPC_CheckCondCode:
02391     Result = !::CheckCondCode(Table, Index, N);
02392     return Index;
02393   case SelectionDAGISel::OPC_CheckValueType:
02394     Result = !::CheckValueType(Table, Index, N, SDISel.getTargetLowering());
02395     return Index;
02396   case SelectionDAGISel::OPC_CheckInteger:
02397     Result = !::CheckInteger(Table, Index, N);
02398     return Index;
02399   case SelectionDAGISel::OPC_CheckChild0Integer:
02400   case SelectionDAGISel::OPC_CheckChild1Integer:
02401   case SelectionDAGISel::OPC_CheckChild2Integer:
02402   case SelectionDAGISel::OPC_CheckChild3Integer:
02403   case SelectionDAGISel::OPC_CheckChild4Integer:
02404     Result = !::CheckChildInteger(Table, Index, N,
02405                      Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Integer);
02406     return Index;
02407   case SelectionDAGISel::OPC_CheckAndImm:
02408     Result = !::CheckAndImm(Table, Index, N, SDISel);
02409     return Index;
02410   case SelectionDAGISel::OPC_CheckOrImm:
02411     Result = !::CheckOrImm(Table, Index, N, SDISel);
02412     return Index;
02413   }
02414 }
02415 
02416 namespace {
02417 
02418 struct MatchScope {
02419   /// FailIndex - If this match fails, this is the index to continue with.
02420   unsigned FailIndex;
02421 
02422   /// NodeStack - The node stack when the scope was formed.
02423   SmallVector<SDValue, 4> NodeStack;
02424 
02425   /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
02426   unsigned NumRecordedNodes;
02427 
02428   /// NumMatchedMemRefs - The number of matched memref entries.
02429   unsigned NumMatchedMemRefs;
02430 
02431   /// InputChain/InputGlue - The current chain/glue
02432   SDValue InputChain, InputGlue;
02433 
02434   /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
02435   bool HasChainNodesMatched, HasGlueResultNodesMatched;
02436 };
02437 
02438 }
02439 
02440 SDNode *SelectionDAGISel::
02441 SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
02442                  unsigned TableSize) {
02443   // FIXME: Should these even be selected?  Handle these cases in the caller?
02444   switch (NodeToMatch->getOpcode()) {
02445   default:
02446     break;
02447   case ISD::EntryToken:       // These nodes remain the same.
02448   case ISD::BasicBlock:
02449   case ISD::Register:
02450   case ISD::RegisterMask:
02451   case ISD::HANDLENODE:
02452   case ISD::MDNODE_SDNODE:
02453   case ISD::TargetConstant:
02454   case ISD::TargetConstantFP:
02455   case ISD::TargetConstantPool:
02456   case ISD::TargetFrameIndex:
02457   case ISD::TargetExternalSymbol:
02458   case ISD::TargetBlockAddress:
02459   case ISD::TargetJumpTable:
02460   case ISD::TargetGlobalTLSAddress:
02461   case ISD::TargetGlobalAddress:
02462   case ISD::TokenFactor:
02463   case ISD::CopyFromReg:
02464   case ISD::CopyToReg:
02465   case ISD::EH_LABEL:
02466   case ISD::LIFETIME_START:
02467   case ISD::LIFETIME_END:
02468     NodeToMatch->setNodeId(-1); // Mark selected.
02469     return nullptr;
02470   case ISD::AssertSext:
02471   case ISD::AssertZext:
02472     CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, 0),
02473                                       NodeToMatch->getOperand(0));
02474     return nullptr;
02475   case ISD::INLINEASM: return Select_INLINEASM(NodeToMatch);
02476   case ISD::READ_REGISTER: return Select_READ_REGISTER(NodeToMatch);
02477   case ISD::WRITE_REGISTER: return Select_WRITE_REGISTER(NodeToMatch);
02478   case ISD::UNDEF:     return Select_UNDEF(NodeToMatch);
02479   }
02480 
02481   assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
02482 
02483   // Set up the node stack with NodeToMatch as the only node on the stack.
02484   SmallVector<SDValue, 8> NodeStack;
02485   SDValue N = SDValue(NodeToMatch, 0);
02486   NodeStack.push_back(N);
02487 
02488   // MatchScopes - Scopes used when matching, if a match failure happens, this
02489   // indicates where to continue checking.
02490   SmallVector<MatchScope, 8> MatchScopes;
02491 
02492   // RecordedNodes - This is the set of nodes that have been recorded by the
02493   // state machine.  The second value is the parent of the node, or null if the
02494   // root is recorded.
02495   SmallVector<std::pair<SDValue, SDNode*>, 8> RecordedNodes;
02496 
02497   // MatchedMemRefs - This is the set of MemRef's we've seen in the input
02498   // pattern.
02499   SmallVector<MachineMemOperand*, 2> MatchedMemRefs;
02500 
02501   // These are the current input chain and glue for use when generating nodes.
02502   // Various Emit operations change these.  For example, emitting a copytoreg
02503   // uses and updates these.
02504   SDValue InputChain, InputGlue;
02505 
02506   // ChainNodesMatched - If a pattern matches nodes that have input/output
02507   // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
02508   // which ones they are.  The result is captured into this list so that we can
02509   // update the chain results when the pattern is complete.
02510   SmallVector<SDNode*, 3> ChainNodesMatched;
02511   SmallVector<SDNode*, 3> GlueResultNodesMatched;
02512 
02513   DEBUG(dbgs() << "ISEL: Starting pattern match on root node: ";
02514         NodeToMatch->dump(CurDAG);
02515         dbgs() << '\n');
02516 
02517   // Determine where to start the interpreter.  Normally we start at opcode #0,
02518   // but if the state machine starts with an OPC_SwitchOpcode, then we
02519   // accelerate the first lookup (which is guaranteed to be hot) with the
02520   // OpcodeOffset table.
02521   unsigned MatcherIndex = 0;
02522 
02523   if (!OpcodeOffset.empty()) {
02524     // Already computed the OpcodeOffset table, just index into it.
02525     if (N.getOpcode() < OpcodeOffset.size())
02526       MatcherIndex = OpcodeOffset[N.getOpcode()];
02527     DEBUG(dbgs() << "  Initial Opcode index to " << MatcherIndex << "\n");
02528 
02529   } else if (MatcherTable[0] == OPC_SwitchOpcode) {
02530     // Otherwise, the table isn't computed, but the state machine does start
02531     // with an OPC_SwitchOpcode instruction.  Populate the table now, since this
02532     // is the first time we're selecting an instruction.
02533     unsigned Idx = 1;
02534     while (1) {
02535       // Get the size of this case.
02536       unsigned CaseSize = MatcherTable[Idx++];
02537       if (CaseSize & 128)
02538         CaseSize = GetVBR(CaseSize, MatcherTable, Idx);
02539       if (CaseSize == 0) break;
02540 
02541       // Get the opcode, add the index to the table.
02542       uint16_t Opc = MatcherTable[Idx++];
02543       Opc |= (unsigned short)MatcherTable[Idx++] << 8;
02544       if (Opc >= OpcodeOffset.size())
02545         OpcodeOffset.resize((Opc+1)*2);
02546       OpcodeOffset[Opc] = Idx;
02547       Idx += CaseSize;
02548     }
02549 
02550     // Okay, do the lookup for the first opcode.
02551     if (N.getOpcode() < OpcodeOffset.size())
02552       MatcherIndex = OpcodeOffset[N.getOpcode()];
02553   }
02554 
02555   while (1) {
02556     assert(MatcherIndex < TableSize && "Invalid index");
02557 #ifndef NDEBUG
02558     unsigned CurrentOpcodeIndex = MatcherIndex;
02559 #endif
02560     BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++];
02561     switch (Opcode) {
02562     case OPC_Scope: {
02563       // Okay, the semantics of this operation are that we should push a scope
02564       // then evaluate the first child.  However, pushing a scope only to have
02565       // the first check fail (which then pops it) is inefficient.  If we can
02566       // determine immediately that the first check (or first several) will
02567       // immediately fail, don't even bother pushing a scope for them.
02568       unsigned FailIndex;
02569 
02570       while (1) {
02571         unsigned NumToSkip = MatcherTable[MatcherIndex++];
02572         if (NumToSkip & 128)
02573           NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
02574         // Found the end of the scope with no match.
02575         if (NumToSkip == 0) {
02576           FailIndex = 0;
02577           break;
02578         }
02579 
02580         FailIndex = MatcherIndex+NumToSkip;
02581 
02582         unsigned MatcherIndexOfPredicate = MatcherIndex;
02583         (void)MatcherIndexOfPredicate; // silence warning.
02584 
02585         // If we can't evaluate this predicate without pushing a scope (e.g. if
02586         // it is a 'MoveParent') or if the predicate succeeds on this node, we
02587         // push the scope and evaluate the full predicate chain.
02588         bool Result;
02589         MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N,
02590                                               Result, *this, RecordedNodes);
02591         if (!Result)
02592           break;
02593 
02594         DEBUG(dbgs() << "  Skipped scope entry (due to false predicate) at "
02595                      << "index " << MatcherIndexOfPredicate
02596                      << ", continuing at " << FailIndex << "\n");
02597         ++NumDAGIselRetries;
02598 
02599         // Otherwise, we know that this case of the Scope is guaranteed to fail,
02600         // move to the next case.
02601         MatcherIndex = FailIndex;
02602       }
02603 
02604       // If the whole scope failed to match, bail.
02605       if (FailIndex == 0) break;
02606 
02607       // Push a MatchScope which indicates where to go if the first child fails
02608       // to match.
02609       MatchScope NewEntry;
02610       NewEntry.FailIndex = FailIndex;
02611       NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end());
02612       NewEntry.NumRecordedNodes = RecordedNodes.size();
02613       NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
02614       NewEntry.InputChain = InputChain;
02615       NewEntry.InputGlue = InputGlue;
02616       NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
02617       NewEntry.HasGlueResultNodesMatched = !GlueResultNodesMatched.empty();
02618       MatchScopes.push_back(NewEntry);
02619       continue;
02620     }
02621     case OPC_RecordNode: {
02622       // Remember this node, it may end up being an operand in the pattern.
02623       SDNode *Parent = nullptr;
02624       if (NodeStack.size() > 1)
02625         Parent = NodeStack[NodeStack.size()-2].getNode();
02626       RecordedNodes.push_back(std::make_pair(N, Parent));
02627       continue;
02628     }
02629 
02630     case OPC_RecordChild0: case OPC_RecordChild1:
02631     case OPC_RecordChild2: case OPC_RecordChild3:
02632     case OPC_RecordChild4: case OPC_RecordChild5:
02633     case OPC_RecordChild6: case OPC_RecordChild7: {
02634       unsigned ChildNo = Opcode-OPC_RecordChild0;
02635       if (ChildNo >= N.getNumOperands())
02636         break;  // Match fails if out of range child #.
02637 
02638       RecordedNodes.push_back(std::make_pair(N->getOperand(ChildNo),
02639                                              N.getNode()));
02640       continue;
02641     }
02642     case OPC_RecordMemRef:
02643       MatchedMemRefs.push_back(cast<MemSDNode>(N)->getMemOperand());
02644       continue;
02645 
02646     case OPC_CaptureGlueInput:
02647       // If the current node has an input glue, capture it in InputGlue.
02648       if (N->getNumOperands() != 0 &&
02649           N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue)
02650         InputGlue = N->getOperand(N->getNumOperands()-1);
02651       continue;
02652 
02653     case OPC_MoveChild: {
02654       unsigned ChildNo = MatcherTable[MatcherIndex++];
02655       if (ChildNo >= N.getNumOperands())
02656         break;  // Match fails if out of range child #.
02657       N = N.getOperand(ChildNo);
02658       NodeStack.push_back(N);
02659       continue;
02660     }
02661 
02662     case OPC_MoveParent:
02663       // Pop the current node off the NodeStack.
02664       NodeStack.pop_back();
02665       assert(!NodeStack.empty() && "Node stack imbalance!");
02666       N = NodeStack.back();
02667       continue;
02668 
02669     case OPC_CheckSame:
02670       if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break;
02671       continue;
02672 
02673     case OPC_CheckChild0Same: case OPC_CheckChild1Same:
02674     case OPC_CheckChild2Same: case OPC_CheckChild3Same:
02675       if (!::CheckChildSame(MatcherTable, MatcherIndex, N, RecordedNodes,
02676                             Opcode-OPC_CheckChild0Same))
02677         break;
02678       continue;
02679 
02680     case OPC_CheckPatternPredicate:
02681       if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this)) break;
02682       continue;
02683     case OPC_CheckPredicate:
02684       if (!::CheckNodePredicate(MatcherTable, MatcherIndex, *this,
02685                                 N.getNode()))
02686         break;
02687       continue;
02688     case OPC_CheckComplexPat: {
02689       unsigned CPNum = MatcherTable[MatcherIndex++];
02690       unsigned RecNo = MatcherTable[MatcherIndex++];
02691       assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
02692       if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo].second,
02693                                RecordedNodes[RecNo].first, CPNum,
02694                                RecordedNodes))
02695         break;
02696       continue;
02697     }
02698     case OPC_CheckOpcode:
02699       if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break;
02700       continue;
02701 
02702     case OPC_CheckType:
02703       if (!::CheckType(MatcherTable, MatcherIndex, N, getTargetLowering()))
02704         break;
02705       continue;
02706 
02707     case OPC_SwitchOpcode: {
02708       unsigned CurNodeOpcode = N.getOpcode();
02709       unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
02710       unsigned CaseSize;
02711       while (1) {
02712         // Get the size of this case.
02713         CaseSize = MatcherTable[MatcherIndex++];
02714         if (CaseSize & 128)
02715           CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
02716         if (CaseSize == 0) break;
02717 
02718         uint16_t Opc = MatcherTable[MatcherIndex++];
02719         Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
02720 
02721         // If the opcode matches, then we will execute this case.
02722         if (CurNodeOpcode == Opc)
02723           break;
02724 
02725         // Otherwise, skip over this case.
02726         MatcherIndex += CaseSize;
02727       }
02728 
02729       // If no cases matched, bail out.
02730       if (CaseSize == 0) break;
02731 
02732       // Otherwise, execute the case we found.
02733       DEBUG(dbgs() << "  OpcodeSwitch from " << SwitchStart
02734                    << " to " << MatcherIndex << "\n");
02735       continue;
02736     }
02737 
02738     case OPC_SwitchType: {
02739       MVT CurNodeVT = N.getSimpleValueType();
02740       unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
02741       unsigned CaseSize;
02742       while (1) {
02743         // Get the size of this case.
02744         CaseSize = MatcherTable[MatcherIndex++];
02745         if (CaseSize & 128)
02746           CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
02747         if (CaseSize == 0) break;
02748 
02749         MVT CaseVT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02750         if (CaseVT == MVT::iPTR)
02751           CaseVT = getTargetLowering()->getPointerTy();
02752 
02753         // If the VT matches, then we will execute this case.
02754         if (CurNodeVT == CaseVT)
02755           break;
02756 
02757         // Otherwise, skip over this case.
02758         MatcherIndex += CaseSize;
02759       }
02760 
02761       // If no cases matched, bail out.
02762       if (CaseSize == 0) break;
02763 
02764       // Otherwise, execute the case we found.
02765       DEBUG(dbgs() << "  TypeSwitch[" << EVT(CurNodeVT).getEVTString()
02766                    << "] from " << SwitchStart << " to " << MatcherIndex<<'\n');
02767       continue;
02768     }
02769     case OPC_CheckChild0Type: case OPC_CheckChild1Type:
02770     case OPC_CheckChild2Type: case OPC_CheckChild3Type:
02771     case OPC_CheckChild4Type: case OPC_CheckChild5Type:
02772     case OPC_CheckChild6Type: case OPC_CheckChild7Type:
02773       if (!::CheckChildType(MatcherTable, MatcherIndex, N, getTargetLowering(),
02774                             Opcode-OPC_CheckChild0Type))
02775         break;
02776       continue;
02777     case OPC_CheckCondCode:
02778       if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break;
02779       continue;
02780     case OPC_CheckValueType:
02781       if (!::CheckValueType(MatcherTable, MatcherIndex, N, getTargetLowering()))
02782         break;
02783       continue;
02784     case OPC_CheckInteger:
02785       if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break;
02786       continue;
02787     case OPC_CheckChild0Integer: case OPC_CheckChild1Integer:
02788     case OPC_CheckChild2Integer: case OPC_CheckChild3Integer:
02789     case OPC_CheckChild4Integer:
02790       if (!::CheckChildInteger(MatcherTable, MatcherIndex, N,
02791                                Opcode-OPC_CheckChild0Integer)) break;
02792       continue;
02793     case OPC_CheckAndImm:
02794       if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break;
02795       continue;
02796     case OPC_CheckOrImm:
02797       if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break;
02798       continue;
02799 
02800     case OPC_CheckFoldableChainNode: {
02801       assert(NodeStack.size() != 1 && "No parent node");
02802       // Verify that all intermediate nodes between the root and this one have
02803       // a single use.
02804       bool HasMultipleUses = false;
02805       for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i)
02806         if (!NodeStack[i].hasOneUse()) {
02807           HasMultipleUses = true;
02808           break;
02809         }
02810       if (HasMultipleUses) break;
02811 
02812       // Check to see that the target thinks this is profitable to fold and that
02813       // we can fold it without inducing cycles in the graph.
02814       if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
02815                               NodeToMatch) ||
02816           !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
02817                          NodeToMatch, OptLevel,
02818                          true/*We validate our own chains*/))
02819         break;
02820 
02821       continue;
02822     }
02823     case OPC_EmitInteger: {
02824       MVT::SimpleValueType VT =
02825         (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02826       int64_t Val = MatcherTable[MatcherIndex++];
02827       if (Val & 128)
02828         Val = GetVBR(Val, MatcherTable, MatcherIndex);
02829       RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
02830                               CurDAG->getTargetConstant(Val, VT), nullptr));
02831       continue;
02832     }
02833     case OPC_EmitRegister: {
02834       MVT::SimpleValueType VT =
02835         (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02836       unsigned RegNo = MatcherTable[MatcherIndex++];
02837       RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
02838                               CurDAG->getRegister(RegNo, VT), nullptr));
02839       continue;
02840     }
02841     case OPC_EmitRegister2: {
02842       // For targets w/ more than 256 register names, the register enum
02843       // values are stored in two bytes in the matcher table (just like
02844       // opcodes).
02845       MVT::SimpleValueType VT =
02846         (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02847       unsigned RegNo = MatcherTable[MatcherIndex++];
02848       RegNo |= MatcherTable[MatcherIndex++] << 8;
02849       RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
02850                               CurDAG->getRegister(RegNo, VT), nullptr));
02851       continue;
02852     }
02853 
02854     case OPC_EmitConvertToTarget:  {
02855       // Convert from IMM/FPIMM to target version.
02856       unsigned RecNo = MatcherTable[MatcherIndex++];
02857       assert(RecNo < RecordedNodes.size() && "Invalid EmitConvertToTarget");
02858       SDValue Imm = RecordedNodes[RecNo].first;
02859 
02860       if (Imm->getOpcode() == ISD::Constant) {
02861         const ConstantInt *Val=cast<ConstantSDNode>(Imm)->getConstantIntValue();
02862         Imm = CurDAG->getConstant(*Val, Imm.getValueType(), true);
02863       } else if (Imm->getOpcode() == ISD::ConstantFP) {
02864         const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
02865         Imm = CurDAG->getConstantFP(*Val, Imm.getValueType(), true);
02866       }
02867 
02868       RecordedNodes.push_back(std::make_pair(Imm, RecordedNodes[RecNo].second));
02869       continue;
02870     }
02871 
02872     case OPC_EmitMergeInputChains1_0:    // OPC_EmitMergeInputChains, 1, 0
02873     case OPC_EmitMergeInputChains1_1: {  // OPC_EmitMergeInputChains, 1, 1
02874       // These are space-optimized forms of OPC_EmitMergeInputChains.
02875       assert(!InputChain.getNode() &&
02876              "EmitMergeInputChains should be the first chain producing node");
02877       assert(ChainNodesMatched.empty() &&
02878              "Should only have one EmitMergeInputChains per match");
02879 
02880       // Read all of the chained nodes.
02881       unsigned RecNo = Opcode == OPC_EmitMergeInputChains1_1;
02882       assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
02883       ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
02884 
02885       // FIXME: What if other value results of the node have uses not matched
02886       // by this pattern?
02887       if (ChainNodesMatched.back() != NodeToMatch &&
02888           !RecordedNodes[RecNo].first.hasOneUse()) {
02889         ChainNodesMatched.clear();
02890         break;
02891       }
02892 
02893       // Merge the input chains if they are not intra-pattern references.
02894       InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
02895 
02896       if (!InputChain.getNode())
02897         break;  // Failed to merge.
02898       continue;
02899     }
02900 
02901     case OPC_EmitMergeInputChains: {
02902       assert(!InputChain.getNode() &&
02903              "EmitMergeInputChains should be the first chain producing node");
02904       // This node gets a list of nodes we matched in the input that have
02905       // chains.  We want to token factor all of the input chains to these nodes
02906       // together.  However, if any of the input chains is actually one of the
02907       // nodes matched in this pattern, then we have an intra-match reference.
02908       // Ignore these because the newly token factored chain should not refer to
02909       // the old nodes.
02910       unsigned NumChains = MatcherTable[MatcherIndex++];
02911       assert(NumChains != 0 && "Can't TF zero chains");
02912 
02913       assert(ChainNodesMatched.empty() &&
02914              "Should only have one EmitMergeInputChains per match");
02915 
02916       // Read all of the chained nodes.
02917       for (unsigned i = 0; i != NumChains; ++i) {
02918         unsigned RecNo = MatcherTable[MatcherIndex++];
02919         assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
02920         ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
02921 
02922         // FIXME: What if other value results of the node have uses not matched
02923         // by this pattern?
02924         if (ChainNodesMatched.back() != NodeToMatch &&
02925             !RecordedNodes[RecNo].first.hasOneUse()) {
02926           ChainNodesMatched.clear();
02927           break;
02928         }
02929       }
02930 
02931       // If the inner loop broke out, the match fails.
02932       if (ChainNodesMatched.empty())
02933         break;
02934 
02935       // Merge the input chains if they are not intra-pattern references.
02936       InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
02937 
02938       if (!InputChain.getNode())
02939         break;  // Failed to merge.
02940 
02941       continue;
02942     }
02943 
02944     case OPC_EmitCopyToReg: {
02945       unsigned RecNo = MatcherTable[MatcherIndex++];
02946       assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg");
02947       unsigned DestPhysReg = MatcherTable[MatcherIndex++];
02948 
02949       if (!InputChain.getNode())
02950         InputChain = CurDAG->getEntryNode();
02951 
02952       InputChain = CurDAG->getCopyToReg(InputChain, SDLoc(NodeToMatch),
02953                                         DestPhysReg, RecordedNodes[RecNo].first,
02954                                         InputGlue);
02955 
02956       InputGlue = InputChain.getValue(1);
02957       continue;
02958     }
02959 
02960     case OPC_EmitNodeXForm: {
02961       unsigned XFormNo = MatcherTable[MatcherIndex++];
02962       unsigned RecNo = MatcherTable[MatcherIndex++];
02963       assert(RecNo < RecordedNodes.size() && "Invalid EmitNodeXForm");
02964       SDValue Res = RunSDNodeXForm(RecordedNodes[RecNo].first, XFormNo);
02965       RecordedNodes.push_back(std::pair<SDValue,SDNode*>(Res, nullptr));
02966       continue;
02967     }
02968 
02969     case OPC_EmitNode:
02970     case OPC_MorphNodeTo: {
02971       uint16_t TargetOpc = MatcherTable[MatcherIndex++];
02972       TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
02973       unsigned EmitNodeInfo = MatcherTable[MatcherIndex++];
02974       // Get the result VT list.
02975       unsigned NumVTs = MatcherTable[MatcherIndex++];
02976       SmallVector<EVT, 4> VTs;
02977       for (unsigned i = 0; i != NumVTs; ++i) {
02978         MVT::SimpleValueType VT =
02979           (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
02980         if (VT == MVT::iPTR) VT = getTargetLowering()->getPointerTy().SimpleTy;
02981         VTs.push_back(VT);
02982       }
02983 
02984       if (EmitNodeInfo & OPFL_Chain)
02985         VTs.push_back(MVT::Other);
02986       if (EmitNodeInfo & OPFL_GlueOutput)
02987         VTs.push_back(MVT::Glue);
02988 
02989       // This is hot code, so optimize the two most common cases of 1 and 2
02990       // results.
02991       SDVTList VTList;
02992       if (VTs.size() == 1)
02993         VTList = CurDAG->getVTList(VTs[0]);
02994       else if (VTs.size() == 2)
02995         VTList = CurDAG->getVTList(VTs[0], VTs[1]);
02996       else
02997         VTList = CurDAG->getVTList(VTs);
02998 
02999       // Get the operand list.
03000       unsigned NumOps = MatcherTable[MatcherIndex++];
03001       SmallVector<SDValue, 8> Ops;
03002       for (unsigned i = 0; i != NumOps; ++i) {
03003         unsigned RecNo = MatcherTable[MatcherIndex++];
03004         if (RecNo & 128)
03005           RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
03006 
03007         assert(RecNo < RecordedNodes.size() && "Invalid EmitNode");
03008         Ops.push_back(RecordedNodes[RecNo].first);
03009       }
03010 
03011       // If there are variadic operands to add, handle them now.
03012       if (EmitNodeInfo & OPFL_VariadicInfo) {
03013         // Determine the start index to copy from.
03014         unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
03015         FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
03016         assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
03017                "Invalid variadic node");
03018         // Copy all of the variadic operands, not including a potential glue
03019         // input.
03020         for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
03021              i != e; ++i) {
03022           SDValue V = NodeToMatch->getOperand(i);
03023           if (V.getValueType() == MVT::Glue) break;
03024           Ops.push_back(V);
03025         }
03026       }
03027 
03028       // If this has chain/glue inputs, add them.
03029       if (EmitNodeInfo & OPFL_Chain)
03030         Ops.push_back(InputChain);
03031       if ((EmitNodeInfo & OPFL_GlueInput) && InputGlue.getNode() != nullptr)
03032         Ops.push_back(InputGlue);
03033 
03034       // Create the node.
03035       SDNode *Res = nullptr;
03036       if (Opcode != OPC_MorphNodeTo) {
03037         // If this is a normal EmitNode command, just create the new node and
03038         // add the results to the RecordedNodes list.
03039         Res = CurDAG->getMachineNode(TargetOpc, SDLoc(NodeToMatch),
03040                                      VTList, Ops);
03041 
03042         // Add all the non-glue/non-chain results to the RecordedNodes list.
03043         for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
03044           if (VTs[i] == MVT::Other || VTs[i] == MVT::Glue) break;
03045           RecordedNodes.push_back(std::pair<SDValue,SDNode*>(SDValue(Res, i),
03046                                                              nullptr));
03047         }
03048 
03049       } else if (NodeToMatch->getOpcode() != ISD::DELETED_NODE) {
03050         Res = MorphNode(NodeToMatch, TargetOpc, VTList, Ops, EmitNodeInfo);
03051       } else {
03052         // NodeToMatch was eliminated by CSE when the target changed the DAG.
03053         // We will visit the equivalent node later.
03054         DEBUG(dbgs() << "Node was eliminated by CSE\n");
03055         return nullptr;
03056       }
03057 
03058       // If the node had chain/glue results, update our notion of the current
03059       // chain and glue.
03060       if (EmitNodeInfo & OPFL_GlueOutput) {
03061         InputGlue = SDValue(Res, VTs.size()-1);
03062         if (EmitNodeInfo & OPFL_Chain)
03063           InputChain = SDValue(Res, VTs.size()-2);
03064       } else if (EmitNodeInfo & OPFL_Chain)
03065         InputChain = SDValue(Res, VTs.size()-1);
03066 
03067       // If the OPFL_MemRefs glue is set on this node, slap all of the
03068       // accumulated memrefs onto it.
03069       //
03070       // FIXME: This is vastly incorrect for patterns with multiple outputs
03071       // instructions that access memory and for ComplexPatterns that match
03072       // loads.
03073       if (EmitNodeInfo & OPFL_MemRefs) {
03074         // Only attach load or store memory operands if the generated
03075         // instruction may load or store.
03076         const MCInstrDesc &MCID =
03077             TM.getSubtargetImpl()->getInstrInfo()->get(TargetOpc);
03078         bool mayLoad = MCID.mayLoad();
03079         bool mayStore = MCID.mayStore();
03080 
03081         unsigned NumMemRefs = 0;
03082         for (SmallVectorImpl<MachineMemOperand *>::const_iterator I =
03083                MatchedMemRefs.begin(), E = MatchedMemRefs.end(); I != E; ++I) {
03084           if ((*I)->isLoad()) {
03085             if (mayLoad)
03086               ++NumMemRefs;
03087           } else if ((*I)->isStore()) {
03088             if (mayStore)
03089               ++NumMemRefs;
03090           } else {
03091             ++NumMemRefs;
03092           }
03093         }
03094 
03095         MachineSDNode::mmo_iterator MemRefs =
03096           MF->allocateMemRefsArray(NumMemRefs);
03097 
03098         MachineSDNode::mmo_iterator MemRefsPos = MemRefs;
03099         for (SmallVectorImpl<MachineMemOperand *>::const_iterator I =
03100                MatchedMemRefs.begin(), E = MatchedMemRefs.end(); I != E; ++I) {
03101           if ((*I)->isLoad()) {
03102             if (mayLoad)
03103               *MemRefsPos++ = *I;
03104           } else if ((*I)->isStore()) {
03105             if (mayStore)
03106               *MemRefsPos++ = *I;
03107           } else {
03108             *MemRefsPos++ = *I;
03109           }
03110         }
03111 
03112         cast<MachineSDNode>(Res)
03113           ->setMemRefs(MemRefs, MemRefs + NumMemRefs);
03114       }
03115 
03116       DEBUG(dbgs() << "  "
03117                    << (Opcode == OPC_MorphNodeTo ? "Morphed" : "Created")
03118                    << " node: "; Res->dump(CurDAG); dbgs() << "\n");
03119 
03120       // If this was a MorphNodeTo then we're completely done!
03121       if (Opcode == OPC_MorphNodeTo) {
03122         // Update chain and glue uses.
03123         UpdateChainsAndGlue(NodeToMatch, InputChain, ChainNodesMatched,
03124                             InputGlue, GlueResultNodesMatched, true);
03125         return Res;
03126       }
03127 
03128       continue;
03129     }
03130 
03131     case OPC_MarkGlueResults: {
03132       unsigned NumNodes = MatcherTable[MatcherIndex++];
03133 
03134       // Read and remember all the glue-result nodes.
03135       for (unsigned i = 0; i != NumNodes; ++i) {
03136         unsigned RecNo = MatcherTable[MatcherIndex++];
03137         if (RecNo & 128)
03138           RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
03139 
03140         assert(RecNo < RecordedNodes.size() && "Invalid MarkGlueResults");
03141         GlueResultNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
03142       }
03143       continue;
03144     }
03145 
03146     case OPC_CompleteMatch: {
03147       // The match has been completed, and any new nodes (if any) have been
03148       // created.  Patch up references to the matched dag to use the newly
03149       // created nodes.
03150       unsigned NumResults = MatcherTable[MatcherIndex++];
03151 
03152       for (unsigned i = 0; i != NumResults; ++i) {
03153         unsigned ResSlot = MatcherTable[MatcherIndex++];
03154         if (ResSlot & 128)
03155           ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex);
03156 
03157         assert(ResSlot < RecordedNodes.size() && "Invalid CompleteMatch");
03158         SDValue Res = RecordedNodes[ResSlot].first;
03159 
03160         assert(i < NodeToMatch->getNumValues() &&
03161                NodeToMatch->getValueType(i) != MVT::Other &&
03162                NodeToMatch->getValueType(i) != MVT::Glue &&
03163                "Invalid number of results to complete!");
03164         assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
03165                 NodeToMatch->getValueType(i) == MVT::iPTR ||
03166                 Res.getValueType() == MVT::iPTR ||
03167                 NodeToMatch->getValueType(i).getSizeInBits() ==
03168                     Res.getValueType().getSizeInBits()) &&
03169                "invalid replacement");
03170         CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, i), Res);
03171       }
03172 
03173       // If the root node defines glue, add it to the glue nodes to update list.
03174       if (NodeToMatch->getValueType(NodeToMatch->getNumValues()-1) == MVT::Glue)
03175         GlueResultNodesMatched.push_back(NodeToMatch);
03176 
03177       // Update chain and glue uses.
03178       UpdateChainsAndGlue(NodeToMatch, InputChain, ChainNodesMatched,
03179                           InputGlue, GlueResultNodesMatched, false);
03180 
03181       assert(NodeToMatch->use_empty() &&
03182              "Didn't replace all uses of the node?");
03183 
03184       // FIXME: We just return here, which interacts correctly with SelectRoot
03185       // above.  We should fix this to not return an SDNode* anymore.
03186       return nullptr;
03187     }
03188     }
03189 
03190     // If the code reached this point, then the match failed.  See if there is
03191     // another child to try in the current 'Scope', otherwise pop it until we
03192     // find a case to check.
03193     DEBUG(dbgs() << "  Match failed at index " << CurrentOpcodeIndex << "\n");
03194     ++NumDAGIselRetries;
03195     while (1) {
03196       if (MatchScopes.empty()) {
03197         CannotYetSelect(NodeToMatch);
03198         return nullptr;
03199       }
03200 
03201       // Restore the interpreter state back to the point where the scope was
03202       // formed.
03203       MatchScope &LastScope = MatchScopes.back();
03204       RecordedNodes.resize(LastScope.NumRecordedNodes);
03205       NodeStack.clear();
03206       NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end());
03207       N = NodeStack.back();
03208 
03209       if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
03210         MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
03211       MatcherIndex = LastScope.FailIndex;
03212 
03213       DEBUG(dbgs() << "  Continuing at " << MatcherIndex << "\n");
03214 
03215       InputChain = LastScope.InputChain;
03216       InputGlue = LastScope.InputGlue;
03217       if (!LastScope.HasChainNodesMatched)
03218         ChainNodesMatched.clear();
03219       if (!LastScope.HasGlueResultNodesMatched)
03220         GlueResultNodesMatched.clear();
03221 
03222       // Check to see what the offset is at the new MatcherIndex.  If it is zero
03223       // we have reached the end of this scope, otherwise we have another child
03224       // in the current scope to try.
03225       unsigned NumToSkip = MatcherTable[MatcherIndex++];
03226       if (NumToSkip & 128)
03227         NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
03228 
03229       // If we have another child in this scope to match, update FailIndex and
03230       // try it.
03231       if (NumToSkip != 0) {
03232         LastScope.FailIndex = MatcherIndex+NumToSkip;
03233         break;
03234       }
03235 
03236       // End of this scope, pop it and try the next child in the containing
03237       // scope.
03238       MatchScopes.pop_back();
03239     }
03240   }
03241 }
03242 
03243 
03244 
03245 void SelectionDAGISel::CannotYetSelect(SDNode *N) {
03246   std::string msg;
03247   raw_string_ostream Msg(msg);
03248   Msg << "Cannot select: ";
03249 
03250   if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN &&
03251       N->getOpcode() != ISD::INTRINSIC_WO_CHAIN &&
03252       N->getOpcode() != ISD::INTRINSIC_VOID) {
03253     N->printrFull(Msg, CurDAG);
03254     Msg << "\nIn function: " << MF->getName();
03255   } else {
03256     bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other;
03257     unsigned iid =
03258       cast<ConstantSDNode>(N->getOperand(HasInputChain))->getZExtValue();
03259     if (iid < Intrinsic::num_intrinsics)
03260       Msg << "intrinsic %" << Intrinsic::getName((Intrinsic::ID)iid);
03261     else if (const TargetIntrinsicInfo *TII = TM.getIntrinsicInfo())
03262       Msg << "target intrinsic %" << TII->getName(iid);
03263     else
03264       Msg << "unknown intrinsic #" << iid;
03265   }
03266   report_fatal_error(Msg.str());
03267 }
03268 
03269 char SelectionDAGISel::ID = 0;