1*0b57cec5SDimitry Andric //===- SelectionDAGISel.cpp - Implement the SelectionDAGISel class --------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This implements the SelectionDAGISel class. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGISel.h" 14*0b57cec5SDimitry Andric #include "ScheduleDAGSDNodes.h" 15*0b57cec5SDimitry Andric #include "SelectionDAGBuilder.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/None.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 23*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 24*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 25*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 26*0b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 27*0b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 28*0b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h" 29*0b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 30*0b57cec5SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 31*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 32*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 33*0b57cec5SDimitry Andric #include "llvm/CodeGen/FastISel.h" 34*0b57cec5SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h" 35*0b57cec5SDimitry Andric #include "llvm/CodeGen/GCMetadata.h" 36*0b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 37*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 38*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 39*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 40*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 41*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 42*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 43*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 44*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 45*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 46*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachinePassRegistry.h" 47*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 48*0b57cec5SDimitry Andric #include "llvm/CodeGen/SchedulerRegistry.h" 49*0b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 50*0b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 51*0b57cec5SDimitry Andric #include "llvm/CodeGen/StackProtector.h" 52*0b57cec5SDimitry Andric #include "llvm/CodeGen/SwiftErrorValueTracking.h" 53*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 54*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 55*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 56*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 57*0b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h" 58*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 59*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 60*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 61*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 62*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 63*0b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 64*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 65*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 66*0b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h" 67*0b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h" 68*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 69*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 70*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 71*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 72*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 73*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 74*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 75*0b57cec5SDimitry Andric #include "llvm/IR/User.h" 76*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 77*0b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h" 78*0b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 79*0b57cec5SDimitry Andric #include "llvm/Pass.h" 80*0b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h" 81*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 82*0b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 83*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 84*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 85*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 86*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 87*0b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h" 88*0b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h" 89*0b57cec5SDimitry Andric #include "llvm/Support/Timer.h" 90*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 91*0b57cec5SDimitry Andric #include "llvm/Target/TargetIntrinsicInfo.h" 92*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 93*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 94*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 95*0b57cec5SDimitry Andric #include <algorithm> 96*0b57cec5SDimitry Andric #include <cassert> 97*0b57cec5SDimitry Andric #include <cstdint> 98*0b57cec5SDimitry Andric #include <iterator> 99*0b57cec5SDimitry Andric #include <limits> 100*0b57cec5SDimitry Andric #include <memory> 101*0b57cec5SDimitry Andric #include <string> 102*0b57cec5SDimitry Andric #include <utility> 103*0b57cec5SDimitry Andric #include <vector> 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric using namespace llvm; 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric #define DEBUG_TYPE "isel" 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on"); 110*0b57cec5SDimitry Andric STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected"); 111*0b57cec5SDimitry Andric STATISTIC(NumFastIselBlocks, "Number of blocks selected entirely by fast isel"); 112*0b57cec5SDimitry Andric STATISTIC(NumDAGBlocks, "Number of blocks selected using DAG"); 113*0b57cec5SDimitry Andric STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path"); 114*0b57cec5SDimitry Andric STATISTIC(NumEntryBlocks, "Number of entry blocks encountered"); 115*0b57cec5SDimitry Andric STATISTIC(NumFastIselFailLowerArguments, 116*0b57cec5SDimitry Andric "Number of entry blocks where fast isel failed to lower arguments"); 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric static cl::opt<int> EnableFastISelAbort( 119*0b57cec5SDimitry Andric "fast-isel-abort", cl::Hidden, 120*0b57cec5SDimitry Andric cl::desc("Enable abort calls when \"fast\" instruction selection " 121*0b57cec5SDimitry Andric "fails to lower an instruction: 0 disable the abort, 1 will " 122*0b57cec5SDimitry Andric "abort but for args, calls and terminators, 2 will also " 123*0b57cec5SDimitry Andric "abort for argument lowering, and 3 will never fallback " 124*0b57cec5SDimitry Andric "to SelectionDAG.")); 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric static cl::opt<bool> EnableFastISelFallbackReport( 127*0b57cec5SDimitry Andric "fast-isel-report-on-fallback", cl::Hidden, 128*0b57cec5SDimitry Andric cl::desc("Emit a diagnostic when \"fast\" instruction selection " 129*0b57cec5SDimitry Andric "falls back to SelectionDAG.")); 130*0b57cec5SDimitry Andric 131*0b57cec5SDimitry Andric static cl::opt<bool> 132*0b57cec5SDimitry Andric UseMBPI("use-mbpi", 133*0b57cec5SDimitry Andric cl::desc("use Machine Branch Probability Info"), 134*0b57cec5SDimitry Andric cl::init(true), cl::Hidden); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric #ifndef NDEBUG 137*0b57cec5SDimitry Andric static cl::opt<std::string> 138*0b57cec5SDimitry Andric FilterDAGBasicBlockName("filter-view-dags", cl::Hidden, 139*0b57cec5SDimitry Andric cl::desc("Only display the basic block whose name " 140*0b57cec5SDimitry Andric "matches this for all view-*-dags options")); 141*0b57cec5SDimitry Andric static cl::opt<bool> 142*0b57cec5SDimitry Andric ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden, 143*0b57cec5SDimitry Andric cl::desc("Pop up a window to show dags before the first " 144*0b57cec5SDimitry Andric "dag combine pass")); 145*0b57cec5SDimitry Andric static cl::opt<bool> 146*0b57cec5SDimitry Andric ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden, 147*0b57cec5SDimitry Andric cl::desc("Pop up a window to show dags before legalize types")); 148*0b57cec5SDimitry Andric static cl::opt<bool> 149*0b57cec5SDimitry Andric ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, 150*0b57cec5SDimitry Andric cl::desc("Pop up a window to show dags before legalize")); 151*0b57cec5SDimitry Andric static cl::opt<bool> 152*0b57cec5SDimitry Andric ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden, 153*0b57cec5SDimitry Andric cl::desc("Pop up a window to show dags before the second " 154*0b57cec5SDimitry Andric "dag combine pass")); 155*0b57cec5SDimitry Andric static cl::opt<bool> 156*0b57cec5SDimitry Andric ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden, 157*0b57cec5SDimitry Andric cl::desc("Pop up a window to show dags before the post legalize types" 158*0b57cec5SDimitry Andric " dag combine pass")); 159*0b57cec5SDimitry Andric static cl::opt<bool> 160*0b57cec5SDimitry Andric ViewISelDAGs("view-isel-dags", cl::Hidden, 161*0b57cec5SDimitry Andric cl::desc("Pop up a window to show isel dags as they are selected")); 162*0b57cec5SDimitry Andric static cl::opt<bool> 163*0b57cec5SDimitry Andric ViewSchedDAGs("view-sched-dags", cl::Hidden, 164*0b57cec5SDimitry Andric cl::desc("Pop up a window to show sched dags as they are processed")); 165*0b57cec5SDimitry Andric static cl::opt<bool> 166*0b57cec5SDimitry Andric ViewSUnitDAGs("view-sunit-dags", cl::Hidden, 167*0b57cec5SDimitry Andric cl::desc("Pop up a window to show SUnit dags after they are processed")); 168*0b57cec5SDimitry Andric #else 169*0b57cec5SDimitry Andric static const bool ViewDAGCombine1 = false, 170*0b57cec5SDimitry Andric ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false, 171*0b57cec5SDimitry Andric ViewDAGCombine2 = false, 172*0b57cec5SDimitry Andric ViewDAGCombineLT = false, 173*0b57cec5SDimitry Andric ViewISelDAGs = false, ViewSchedDAGs = false, 174*0b57cec5SDimitry Andric ViewSUnitDAGs = false; 175*0b57cec5SDimitry Andric #endif 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 178*0b57cec5SDimitry Andric /// 179*0b57cec5SDimitry Andric /// RegisterScheduler class - Track the registration of instruction schedulers. 180*0b57cec5SDimitry Andric /// 181*0b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 182*0b57cec5SDimitry Andric MachinePassRegistry<RegisterScheduler::FunctionPassCtor> 183*0b57cec5SDimitry Andric RegisterScheduler::Registry; 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 186*0b57cec5SDimitry Andric /// 187*0b57cec5SDimitry Andric /// ISHeuristic command line option for instruction schedulers. 188*0b57cec5SDimitry Andric /// 189*0b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 190*0b57cec5SDimitry Andric static cl::opt<RegisterScheduler::FunctionPassCtor, false, 191*0b57cec5SDimitry Andric RegisterPassParser<RegisterScheduler>> 192*0b57cec5SDimitry Andric ISHeuristic("pre-RA-sched", 193*0b57cec5SDimitry Andric cl::init(&createDefaultScheduler), cl::Hidden, 194*0b57cec5SDimitry Andric cl::desc("Instruction schedulers available (before register" 195*0b57cec5SDimitry Andric " allocation):")); 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric static RegisterScheduler 198*0b57cec5SDimitry Andric defaultListDAGScheduler("default", "Best scheduler for the target", 199*0b57cec5SDimitry Andric createDefaultScheduler); 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric namespace llvm { 202*0b57cec5SDimitry Andric 203*0b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 204*0b57cec5SDimitry Andric /// This class is used by SelectionDAGISel to temporarily override 205*0b57cec5SDimitry Andric /// the optimization level on a per-function basis. 206*0b57cec5SDimitry Andric class OptLevelChanger { 207*0b57cec5SDimitry Andric SelectionDAGISel &IS; 208*0b57cec5SDimitry Andric CodeGenOpt::Level SavedOptLevel; 209*0b57cec5SDimitry Andric bool SavedFastISel; 210*0b57cec5SDimitry Andric 211*0b57cec5SDimitry Andric public: 212*0b57cec5SDimitry Andric OptLevelChanger(SelectionDAGISel &ISel, 213*0b57cec5SDimitry Andric CodeGenOpt::Level NewOptLevel) : IS(ISel) { 214*0b57cec5SDimitry Andric SavedOptLevel = IS.OptLevel; 215*0b57cec5SDimitry Andric if (NewOptLevel == SavedOptLevel) 216*0b57cec5SDimitry Andric return; 217*0b57cec5SDimitry Andric IS.OptLevel = NewOptLevel; 218*0b57cec5SDimitry Andric IS.TM.setOptLevel(NewOptLevel); 219*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nChanging optimization level for Function " 220*0b57cec5SDimitry Andric << IS.MF->getFunction().getName() << "\n"); 221*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tBefore: -O" << SavedOptLevel << " ; After: -O" 222*0b57cec5SDimitry Andric << NewOptLevel << "\n"); 223*0b57cec5SDimitry Andric SavedFastISel = IS.TM.Options.EnableFastISel; 224*0b57cec5SDimitry Andric if (NewOptLevel == CodeGenOpt::None) { 225*0b57cec5SDimitry Andric IS.TM.setFastISel(IS.TM.getO0WantsFastISel()); 226*0b57cec5SDimitry Andric LLVM_DEBUG( 227*0b57cec5SDimitry Andric dbgs() << "\tFastISel is " 228*0b57cec5SDimitry Andric << (IS.TM.Options.EnableFastISel ? "enabled" : "disabled") 229*0b57cec5SDimitry Andric << "\n"); 230*0b57cec5SDimitry Andric } 231*0b57cec5SDimitry Andric } 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric ~OptLevelChanger() { 234*0b57cec5SDimitry Andric if (IS.OptLevel == SavedOptLevel) 235*0b57cec5SDimitry Andric return; 236*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nRestoring optimization level for Function " 237*0b57cec5SDimitry Andric << IS.MF->getFunction().getName() << "\n"); 238*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tBefore: -O" << IS.OptLevel << " ; After: -O" 239*0b57cec5SDimitry Andric << SavedOptLevel << "\n"); 240*0b57cec5SDimitry Andric IS.OptLevel = SavedOptLevel; 241*0b57cec5SDimitry Andric IS.TM.setOptLevel(SavedOptLevel); 242*0b57cec5SDimitry Andric IS.TM.setFastISel(SavedFastISel); 243*0b57cec5SDimitry Andric } 244*0b57cec5SDimitry Andric }; 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 247*0b57cec5SDimitry Andric /// createDefaultScheduler - This creates an instruction scheduler appropriate 248*0b57cec5SDimitry Andric /// for the target. 249*0b57cec5SDimitry Andric ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS, 250*0b57cec5SDimitry Andric CodeGenOpt::Level OptLevel) { 251*0b57cec5SDimitry Andric const TargetLowering *TLI = IS->TLI; 252*0b57cec5SDimitry Andric const TargetSubtargetInfo &ST = IS->MF->getSubtarget(); 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric // Try first to see if the Target has its own way of selecting a scheduler 255*0b57cec5SDimitry Andric if (auto *SchedulerCtor = ST.getDAGScheduler(OptLevel)) { 256*0b57cec5SDimitry Andric return SchedulerCtor(IS, OptLevel); 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric if (OptLevel == CodeGenOpt::None || 260*0b57cec5SDimitry Andric (ST.enableMachineScheduler() && ST.enableMachineSchedDefaultSched()) || 261*0b57cec5SDimitry Andric TLI->getSchedulingPreference() == Sched::Source) 262*0b57cec5SDimitry Andric return createSourceListDAGScheduler(IS, OptLevel); 263*0b57cec5SDimitry Andric if (TLI->getSchedulingPreference() == Sched::RegPressure) 264*0b57cec5SDimitry Andric return createBURRListDAGScheduler(IS, OptLevel); 265*0b57cec5SDimitry Andric if (TLI->getSchedulingPreference() == Sched::Hybrid) 266*0b57cec5SDimitry Andric return createHybridListDAGScheduler(IS, OptLevel); 267*0b57cec5SDimitry Andric if (TLI->getSchedulingPreference() == Sched::VLIW) 268*0b57cec5SDimitry Andric return createVLIWDAGScheduler(IS, OptLevel); 269*0b57cec5SDimitry Andric assert(TLI->getSchedulingPreference() == Sched::ILP && 270*0b57cec5SDimitry Andric "Unknown sched type!"); 271*0b57cec5SDimitry Andric return createILPListDAGScheduler(IS, OptLevel); 272*0b57cec5SDimitry Andric } 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric } // end namespace llvm 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric // EmitInstrWithCustomInserter - This method should be implemented by targets 277*0b57cec5SDimitry Andric // that mark instructions with the 'usesCustomInserter' flag. These 278*0b57cec5SDimitry Andric // instructions are special in various ways, which require special support to 279*0b57cec5SDimitry Andric // insert. The specified MachineInstr is created but not inserted into any 280*0b57cec5SDimitry Andric // basic blocks, and this method is called to expand it into a sequence of 281*0b57cec5SDimitry Andric // instructions, potentially also creating new basic blocks and control flow. 282*0b57cec5SDimitry Andric // When new basic blocks are inserted and the edges from MBB to its successors 283*0b57cec5SDimitry Andric // are modified, the method should insert pairs of <OldSucc, NewSucc> into the 284*0b57cec5SDimitry Andric // DenseMap. 285*0b57cec5SDimitry Andric MachineBasicBlock * 286*0b57cec5SDimitry Andric TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 287*0b57cec5SDimitry Andric MachineBasicBlock *MBB) const { 288*0b57cec5SDimitry Andric #ifndef NDEBUG 289*0b57cec5SDimitry Andric dbgs() << "If a target marks an instruction with " 290*0b57cec5SDimitry Andric "'usesCustomInserter', it must implement " 291*0b57cec5SDimitry Andric "TargetLowering::EmitInstrWithCustomInserter!"; 292*0b57cec5SDimitry Andric #endif 293*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 294*0b57cec5SDimitry Andric } 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric void TargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 297*0b57cec5SDimitry Andric SDNode *Node) const { 298*0b57cec5SDimitry Andric assert(!MI.hasPostISelHook() && 299*0b57cec5SDimitry Andric "If a target marks an instruction with 'hasPostISelHook', " 300*0b57cec5SDimitry Andric "it must implement TargetLowering::AdjustInstrPostInstrSelection!"); 301*0b57cec5SDimitry Andric } 302*0b57cec5SDimitry Andric 303*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 304*0b57cec5SDimitry Andric // SelectionDAGISel code 305*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, 308*0b57cec5SDimitry Andric CodeGenOpt::Level OL) : 309*0b57cec5SDimitry Andric MachineFunctionPass(ID), TM(tm), 310*0b57cec5SDimitry Andric FuncInfo(new FunctionLoweringInfo()), 311*0b57cec5SDimitry Andric SwiftError(new SwiftErrorValueTracking()), 312*0b57cec5SDimitry Andric CurDAG(new SelectionDAG(tm, OL)), 313*0b57cec5SDimitry Andric SDB(new SelectionDAGBuilder(*CurDAG, *FuncInfo, *SwiftError, OL)), 314*0b57cec5SDimitry Andric AA(), GFI(), 315*0b57cec5SDimitry Andric OptLevel(OL), 316*0b57cec5SDimitry Andric DAGSize(0) { 317*0b57cec5SDimitry Andric initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); 318*0b57cec5SDimitry Andric initializeBranchProbabilityInfoWrapperPassPass( 319*0b57cec5SDimitry Andric *PassRegistry::getPassRegistry()); 320*0b57cec5SDimitry Andric initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); 321*0b57cec5SDimitry Andric initializeTargetLibraryInfoWrapperPassPass( 322*0b57cec5SDimitry Andric *PassRegistry::getPassRegistry()); 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric SelectionDAGISel::~SelectionDAGISel() { 326*0b57cec5SDimitry Andric delete SDB; 327*0b57cec5SDimitry Andric delete CurDAG; 328*0b57cec5SDimitry Andric delete FuncInfo; 329*0b57cec5SDimitry Andric delete SwiftError; 330*0b57cec5SDimitry Andric } 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { 333*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None) 334*0b57cec5SDimitry Andric AU.addRequired<AAResultsWrapperPass>(); 335*0b57cec5SDimitry Andric AU.addRequired<GCModuleInfo>(); 336*0b57cec5SDimitry Andric AU.addRequired<StackProtector>(); 337*0b57cec5SDimitry Andric AU.addPreserved<GCModuleInfo>(); 338*0b57cec5SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 339*0b57cec5SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 340*0b57cec5SDimitry Andric if (UseMBPI && OptLevel != CodeGenOpt::None) 341*0b57cec5SDimitry Andric AU.addRequired<BranchProbabilityInfoWrapperPass>(); 342*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 343*0b57cec5SDimitry Andric } 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric /// SplitCriticalSideEffectEdges - Look for critical edges with a PHI value that 346*0b57cec5SDimitry Andric /// may trap on it. In this case we have to split the edge so that the path 347*0b57cec5SDimitry Andric /// through the predecessor block that doesn't go to the phi block doesn't 348*0b57cec5SDimitry Andric /// execute the possibly trapping instruction. If available, we pass domtree 349*0b57cec5SDimitry Andric /// and loop info to be updated when we split critical edges. This is because 350*0b57cec5SDimitry Andric /// SelectionDAGISel preserves these analyses. 351*0b57cec5SDimitry Andric /// This is required for correctness, so it must be done at -O0. 352*0b57cec5SDimitry Andric /// 353*0b57cec5SDimitry Andric static void SplitCriticalSideEffectEdges(Function &Fn, DominatorTree *DT, 354*0b57cec5SDimitry Andric LoopInfo *LI) { 355*0b57cec5SDimitry Andric // Loop for blocks with phi nodes. 356*0b57cec5SDimitry Andric for (BasicBlock &BB : Fn) { 357*0b57cec5SDimitry Andric PHINode *PN = dyn_cast<PHINode>(BB.begin()); 358*0b57cec5SDimitry Andric if (!PN) continue; 359*0b57cec5SDimitry Andric 360*0b57cec5SDimitry Andric ReprocessBlock: 361*0b57cec5SDimitry Andric // For each block with a PHI node, check to see if any of the input values 362*0b57cec5SDimitry Andric // are potentially trapping constant expressions. Constant expressions are 363*0b57cec5SDimitry Andric // the only potentially trapping value that can occur as the argument to a 364*0b57cec5SDimitry Andric // PHI. 365*0b57cec5SDimitry Andric for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I)); ++I) 366*0b57cec5SDimitry Andric for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 367*0b57cec5SDimitry Andric ConstantExpr *CE = dyn_cast<ConstantExpr>(PN->getIncomingValue(i)); 368*0b57cec5SDimitry Andric if (!CE || !CE->canTrap()) continue; 369*0b57cec5SDimitry Andric 370*0b57cec5SDimitry Andric // The only case we have to worry about is when the edge is critical. 371*0b57cec5SDimitry Andric // Since this block has a PHI Node, we assume it has multiple input 372*0b57cec5SDimitry Andric // edges: check to see if the pred has multiple successors. 373*0b57cec5SDimitry Andric BasicBlock *Pred = PN->getIncomingBlock(i); 374*0b57cec5SDimitry Andric if (Pred->getTerminator()->getNumSuccessors() == 1) 375*0b57cec5SDimitry Andric continue; 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric // Okay, we have to split this edge. 378*0b57cec5SDimitry Andric SplitCriticalEdge( 379*0b57cec5SDimitry Andric Pred->getTerminator(), GetSuccessorNumber(Pred, &BB), 380*0b57cec5SDimitry Andric CriticalEdgeSplittingOptions(DT, LI).setMergeIdenticalEdges()); 381*0b57cec5SDimitry Andric goto ReprocessBlock; 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric } 385*0b57cec5SDimitry Andric 386*0b57cec5SDimitry Andric static void computeUsesMSVCFloatingPoint(const Triple &TT, const Function &F, 387*0b57cec5SDimitry Andric MachineModuleInfo &MMI) { 388*0b57cec5SDimitry Andric // Only needed for MSVC 389*0b57cec5SDimitry Andric if (!TT.isWindowsMSVCEnvironment()) 390*0b57cec5SDimitry Andric return; 391*0b57cec5SDimitry Andric 392*0b57cec5SDimitry Andric // If it's already set, nothing to do. 393*0b57cec5SDimitry Andric if (MMI.usesMSVCFloatingPoint()) 394*0b57cec5SDimitry Andric return; 395*0b57cec5SDimitry Andric 396*0b57cec5SDimitry Andric for (const Instruction &I : instructions(F)) { 397*0b57cec5SDimitry Andric if (I.getType()->isFPOrFPVectorTy()) { 398*0b57cec5SDimitry Andric MMI.setUsesMSVCFloatingPoint(true); 399*0b57cec5SDimitry Andric return; 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric for (const auto &Op : I.operands()) { 402*0b57cec5SDimitry Andric if (Op->getType()->isFPOrFPVectorTy()) { 403*0b57cec5SDimitry Andric MMI.setUsesMSVCFloatingPoint(true); 404*0b57cec5SDimitry Andric return; 405*0b57cec5SDimitry Andric } 406*0b57cec5SDimitry Andric } 407*0b57cec5SDimitry Andric } 408*0b57cec5SDimitry Andric } 409*0b57cec5SDimitry Andric 410*0b57cec5SDimitry Andric bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) { 411*0b57cec5SDimitry Andric // If we already selected that function, we do not need to run SDISel. 412*0b57cec5SDimitry Andric if (mf.getProperties().hasProperty( 413*0b57cec5SDimitry Andric MachineFunctionProperties::Property::Selected)) 414*0b57cec5SDimitry Andric return false; 415*0b57cec5SDimitry Andric // Do some sanity-checking on the command-line options. 416*0b57cec5SDimitry Andric assert((!EnableFastISelAbort || TM.Options.EnableFastISel) && 417*0b57cec5SDimitry Andric "-fast-isel-abort > 0 requires -fast-isel"); 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric const Function &Fn = mf.getFunction(); 420*0b57cec5SDimitry Andric MF = &mf; 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andric // Reset the target options before resetting the optimization 423*0b57cec5SDimitry Andric // level below. 424*0b57cec5SDimitry Andric // FIXME: This is a horrible hack and should be processed via 425*0b57cec5SDimitry Andric // codegen looking at the optimization level explicitly when 426*0b57cec5SDimitry Andric // it wants to look at it. 427*0b57cec5SDimitry Andric TM.resetTargetOptions(Fn); 428*0b57cec5SDimitry Andric // Reset OptLevel to None for optnone functions. 429*0b57cec5SDimitry Andric CodeGenOpt::Level NewOptLevel = OptLevel; 430*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None && skipFunction(Fn)) 431*0b57cec5SDimitry Andric NewOptLevel = CodeGenOpt::None; 432*0b57cec5SDimitry Andric OptLevelChanger OLC(*this, NewOptLevel); 433*0b57cec5SDimitry Andric 434*0b57cec5SDimitry Andric TII = MF->getSubtarget().getInstrInfo(); 435*0b57cec5SDimitry Andric TLI = MF->getSubtarget().getTargetLowering(); 436*0b57cec5SDimitry Andric RegInfo = &MF->getRegInfo(); 437*0b57cec5SDimitry Andric LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 438*0b57cec5SDimitry Andric GFI = Fn.hasGC() ? &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn) : nullptr; 439*0b57cec5SDimitry Andric ORE = make_unique<OptimizationRemarkEmitter>(&Fn); 440*0b57cec5SDimitry Andric auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 441*0b57cec5SDimitry Andric DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; 442*0b57cec5SDimitry Andric auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>(); 443*0b57cec5SDimitry Andric LoopInfo *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n"); 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric SplitCriticalSideEffectEdges(const_cast<Function &>(Fn), DT, LI); 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric CurDAG->init(*MF, *ORE, this, LibInfo, 450*0b57cec5SDimitry Andric getAnalysisIfAvailable<LegacyDivergenceAnalysis>()); 451*0b57cec5SDimitry Andric FuncInfo->set(Fn, *MF, CurDAG); 452*0b57cec5SDimitry Andric SwiftError->setFunction(*MF); 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric // Now get the optional analyzes if we want to. 455*0b57cec5SDimitry Andric // This is based on the possibly changed OptLevel (after optnone is taken 456*0b57cec5SDimitry Andric // into account). That's unfortunate but OK because it just means we won't 457*0b57cec5SDimitry Andric // ask for passes that have been required anyway. 458*0b57cec5SDimitry Andric 459*0b57cec5SDimitry Andric if (UseMBPI && OptLevel != CodeGenOpt::None) 460*0b57cec5SDimitry Andric FuncInfo->BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); 461*0b57cec5SDimitry Andric else 462*0b57cec5SDimitry Andric FuncInfo->BPI = nullptr; 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None) 465*0b57cec5SDimitry Andric AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 466*0b57cec5SDimitry Andric else 467*0b57cec5SDimitry Andric AA = nullptr; 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric SDB->init(GFI, AA, LibInfo); 470*0b57cec5SDimitry Andric 471*0b57cec5SDimitry Andric MF->setHasInlineAsm(false); 472*0b57cec5SDimitry Andric 473*0b57cec5SDimitry Andric FuncInfo->SplitCSR = false; 474*0b57cec5SDimitry Andric 475*0b57cec5SDimitry Andric // We split CSR if the target supports it for the given function 476*0b57cec5SDimitry Andric // and the function has only return exits. 477*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None && TLI->supportSplitCSR(MF)) { 478*0b57cec5SDimitry Andric FuncInfo->SplitCSR = true; 479*0b57cec5SDimitry Andric 480*0b57cec5SDimitry Andric // Collect all the return blocks. 481*0b57cec5SDimitry Andric for (const BasicBlock &BB : Fn) { 482*0b57cec5SDimitry Andric if (!succ_empty(&BB)) 483*0b57cec5SDimitry Andric continue; 484*0b57cec5SDimitry Andric 485*0b57cec5SDimitry Andric const Instruction *Term = BB.getTerminator(); 486*0b57cec5SDimitry Andric if (isa<UnreachableInst>(Term) || isa<ReturnInst>(Term)) 487*0b57cec5SDimitry Andric continue; 488*0b57cec5SDimitry Andric 489*0b57cec5SDimitry Andric // Bail out if the exit block is not Return nor Unreachable. 490*0b57cec5SDimitry Andric FuncInfo->SplitCSR = false; 491*0b57cec5SDimitry Andric break; 492*0b57cec5SDimitry Andric } 493*0b57cec5SDimitry Andric } 494*0b57cec5SDimitry Andric 495*0b57cec5SDimitry Andric MachineBasicBlock *EntryMBB = &MF->front(); 496*0b57cec5SDimitry Andric if (FuncInfo->SplitCSR) 497*0b57cec5SDimitry Andric // This performs initialization so lowering for SplitCSR will be correct. 498*0b57cec5SDimitry Andric TLI->initializeSplitCSR(EntryMBB); 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric SelectAllBasicBlocks(Fn); 501*0b57cec5SDimitry Andric if (FastISelFailed && EnableFastISelFallbackReport) { 502*0b57cec5SDimitry Andric DiagnosticInfoISelFallback DiagFallback(Fn); 503*0b57cec5SDimitry Andric Fn.getContext().diagnose(DiagFallback); 504*0b57cec5SDimitry Andric } 505*0b57cec5SDimitry Andric 506*0b57cec5SDimitry Andric // Replace forward-declared registers with the registers containing 507*0b57cec5SDimitry Andric // the desired value. 508*0b57cec5SDimitry Andric // Note: it is important that this happens **before** the call to 509*0b57cec5SDimitry Andric // EmitLiveInCopies, since implementations can skip copies of unused 510*0b57cec5SDimitry Andric // registers. If we don't apply the reg fixups before, some registers may 511*0b57cec5SDimitry Andric // appear as unused and will be skipped, resulting in bad MI. 512*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF->getRegInfo(); 513*0b57cec5SDimitry Andric for (DenseMap<unsigned, unsigned>::iterator I = FuncInfo->RegFixups.begin(), 514*0b57cec5SDimitry Andric E = FuncInfo->RegFixups.end(); 515*0b57cec5SDimitry Andric I != E; ++I) { 516*0b57cec5SDimitry Andric unsigned From = I->first; 517*0b57cec5SDimitry Andric unsigned To = I->second; 518*0b57cec5SDimitry Andric // If To is also scheduled to be replaced, find what its ultimate 519*0b57cec5SDimitry Andric // replacement is. 520*0b57cec5SDimitry Andric while (true) { 521*0b57cec5SDimitry Andric DenseMap<unsigned, unsigned>::iterator J = FuncInfo->RegFixups.find(To); 522*0b57cec5SDimitry Andric if (J == E) 523*0b57cec5SDimitry Andric break; 524*0b57cec5SDimitry Andric To = J->second; 525*0b57cec5SDimitry Andric } 526*0b57cec5SDimitry Andric // Make sure the new register has a sufficiently constrained register class. 527*0b57cec5SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(From) && 528*0b57cec5SDimitry Andric TargetRegisterInfo::isVirtualRegister(To)) 529*0b57cec5SDimitry Andric MRI.constrainRegClass(To, MRI.getRegClass(From)); 530*0b57cec5SDimitry Andric // Replace it. 531*0b57cec5SDimitry Andric 532*0b57cec5SDimitry Andric // Replacing one register with another won't touch the kill flags. 533*0b57cec5SDimitry Andric // We need to conservatively clear the kill flags as a kill on the old 534*0b57cec5SDimitry Andric // register might dominate existing uses of the new register. 535*0b57cec5SDimitry Andric if (!MRI.use_empty(To)) 536*0b57cec5SDimitry Andric MRI.clearKillFlags(From); 537*0b57cec5SDimitry Andric MRI.replaceRegWith(From, To); 538*0b57cec5SDimitry Andric } 539*0b57cec5SDimitry Andric 540*0b57cec5SDimitry Andric // If the first basic block in the function has live ins that need to be 541*0b57cec5SDimitry Andric // copied into vregs, emit the copies into the top of the block before 542*0b57cec5SDimitry Andric // emitting the code for the block. 543*0b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo(); 544*0b57cec5SDimitry Andric RegInfo->EmitLiveInCopies(EntryMBB, TRI, *TII); 545*0b57cec5SDimitry Andric 546*0b57cec5SDimitry Andric // Insert copies in the entry block and the return blocks. 547*0b57cec5SDimitry Andric if (FuncInfo->SplitCSR) { 548*0b57cec5SDimitry Andric SmallVector<MachineBasicBlock*, 4> Returns; 549*0b57cec5SDimitry Andric // Collect all the return blocks. 550*0b57cec5SDimitry Andric for (MachineBasicBlock &MBB : mf) { 551*0b57cec5SDimitry Andric if (!MBB.succ_empty()) 552*0b57cec5SDimitry Andric continue; 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric MachineBasicBlock::iterator Term = MBB.getFirstTerminator(); 555*0b57cec5SDimitry Andric if (Term != MBB.end() && Term->isReturn()) { 556*0b57cec5SDimitry Andric Returns.push_back(&MBB); 557*0b57cec5SDimitry Andric continue; 558*0b57cec5SDimitry Andric } 559*0b57cec5SDimitry Andric } 560*0b57cec5SDimitry Andric TLI->insertCopiesSplitCSR(EntryMBB, Returns); 561*0b57cec5SDimitry Andric } 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric DenseMap<unsigned, unsigned> LiveInMap; 564*0b57cec5SDimitry Andric if (!FuncInfo->ArgDbgValues.empty()) 565*0b57cec5SDimitry Andric for (std::pair<unsigned, unsigned> LI : RegInfo->liveins()) 566*0b57cec5SDimitry Andric if (LI.second) 567*0b57cec5SDimitry Andric LiveInMap.insert(LI); 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric // Insert DBG_VALUE instructions for function arguments to the entry block. 570*0b57cec5SDimitry Andric for (unsigned i = 0, e = FuncInfo->ArgDbgValues.size(); i != e; ++i) { 571*0b57cec5SDimitry Andric MachineInstr *MI = FuncInfo->ArgDbgValues[e-i-1]; 572*0b57cec5SDimitry Andric bool hasFI = MI->getOperand(0).isFI(); 573*0b57cec5SDimitry Andric Register Reg = 574*0b57cec5SDimitry Andric hasFI ? TRI.getFrameRegister(*MF) : MI->getOperand(0).getReg(); 575*0b57cec5SDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(Reg)) 576*0b57cec5SDimitry Andric EntryMBB->insert(EntryMBB->begin(), MI); 577*0b57cec5SDimitry Andric else { 578*0b57cec5SDimitry Andric MachineInstr *Def = RegInfo->getVRegDef(Reg); 579*0b57cec5SDimitry Andric if (Def) { 580*0b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = Def; 581*0b57cec5SDimitry Andric // FIXME: VR def may not be in entry block. 582*0b57cec5SDimitry Andric Def->getParent()->insert(std::next(InsertPos), MI); 583*0b57cec5SDimitry Andric } else 584*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Dropping debug info for dead vreg" 585*0b57cec5SDimitry Andric << TargetRegisterInfo::virtReg2Index(Reg) << "\n"); 586*0b57cec5SDimitry Andric } 587*0b57cec5SDimitry Andric 588*0b57cec5SDimitry Andric // If Reg is live-in then update debug info to track its copy in a vreg. 589*0b57cec5SDimitry Andric DenseMap<unsigned, unsigned>::iterator LDI = LiveInMap.find(Reg); 590*0b57cec5SDimitry Andric if (LDI != LiveInMap.end()) { 591*0b57cec5SDimitry Andric assert(!hasFI && "There's no handling of frame pointer updating here yet " 592*0b57cec5SDimitry Andric "- add if needed"); 593*0b57cec5SDimitry Andric MachineInstr *Def = RegInfo->getVRegDef(LDI->second); 594*0b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = Def; 595*0b57cec5SDimitry Andric const MDNode *Variable = MI->getDebugVariable(); 596*0b57cec5SDimitry Andric const MDNode *Expr = MI->getDebugExpression(); 597*0b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 598*0b57cec5SDimitry Andric bool IsIndirect = MI->isIndirectDebugValue(); 599*0b57cec5SDimitry Andric if (IsIndirect) 600*0b57cec5SDimitry Andric assert(MI->getOperand(1).getImm() == 0 && 601*0b57cec5SDimitry Andric "DBG_VALUE with nonzero offset"); 602*0b57cec5SDimitry Andric assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) && 603*0b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 604*0b57cec5SDimitry Andric // Def is never a terminator here, so it is ok to increment InsertPos. 605*0b57cec5SDimitry Andric BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE), 606*0b57cec5SDimitry Andric IsIndirect, LDI->second, Variable, Expr); 607*0b57cec5SDimitry Andric 608*0b57cec5SDimitry Andric // If this vreg is directly copied into an exported register then 609*0b57cec5SDimitry Andric // that COPY instructions also need DBG_VALUE, if it is the only 610*0b57cec5SDimitry Andric // user of LDI->second. 611*0b57cec5SDimitry Andric MachineInstr *CopyUseMI = nullptr; 612*0b57cec5SDimitry Andric for (MachineRegisterInfo::use_instr_iterator 613*0b57cec5SDimitry Andric UI = RegInfo->use_instr_begin(LDI->second), 614*0b57cec5SDimitry Andric E = RegInfo->use_instr_end(); UI != E; ) { 615*0b57cec5SDimitry Andric MachineInstr *UseMI = &*(UI++); 616*0b57cec5SDimitry Andric if (UseMI->isDebugValue()) continue; 617*0b57cec5SDimitry Andric if (UseMI->isCopy() && !CopyUseMI && UseMI->getParent() == EntryMBB) { 618*0b57cec5SDimitry Andric CopyUseMI = UseMI; continue; 619*0b57cec5SDimitry Andric } 620*0b57cec5SDimitry Andric // Otherwise this is another use or second copy use. 621*0b57cec5SDimitry Andric CopyUseMI = nullptr; break; 622*0b57cec5SDimitry Andric } 623*0b57cec5SDimitry Andric if (CopyUseMI) { 624*0b57cec5SDimitry Andric // Use MI's debug location, which describes where Variable was 625*0b57cec5SDimitry Andric // declared, rather than whatever is attached to CopyUseMI. 626*0b57cec5SDimitry Andric MachineInstr *NewMI = 627*0b57cec5SDimitry Andric BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect, 628*0b57cec5SDimitry Andric CopyUseMI->getOperand(0).getReg(), Variable, Expr); 629*0b57cec5SDimitry Andric MachineBasicBlock::iterator Pos = CopyUseMI; 630*0b57cec5SDimitry Andric EntryMBB->insertAfter(Pos, NewMI); 631*0b57cec5SDimitry Andric } 632*0b57cec5SDimitry Andric } 633*0b57cec5SDimitry Andric } 634*0b57cec5SDimitry Andric 635*0b57cec5SDimitry Andric // Determine if there are any calls in this machine function. 636*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF->getFrameInfo(); 637*0b57cec5SDimitry Andric for (const auto &MBB : *MF) { 638*0b57cec5SDimitry Andric if (MFI.hasCalls() && MF->hasInlineAsm()) 639*0b57cec5SDimitry Andric break; 640*0b57cec5SDimitry Andric 641*0b57cec5SDimitry Andric for (const auto &MI : MBB) { 642*0b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(MI.getOpcode()); 643*0b57cec5SDimitry Andric if ((MCID.isCall() && !MCID.isReturn()) || 644*0b57cec5SDimitry Andric MI.isStackAligningInlineAsm()) { 645*0b57cec5SDimitry Andric MFI.setHasCalls(true); 646*0b57cec5SDimitry Andric } 647*0b57cec5SDimitry Andric if (MI.isInlineAsm()) { 648*0b57cec5SDimitry Andric MF->setHasInlineAsm(true); 649*0b57cec5SDimitry Andric } 650*0b57cec5SDimitry Andric } 651*0b57cec5SDimitry Andric } 652*0b57cec5SDimitry Andric 653*0b57cec5SDimitry Andric // Determine if there is a call to setjmp in the machine function. 654*0b57cec5SDimitry Andric MF->setExposesReturnsTwice(Fn.callsFunctionThatReturnsTwice()); 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric // Determine if floating point is used for msvc 657*0b57cec5SDimitry Andric computeUsesMSVCFloatingPoint(TM.getTargetTriple(), Fn, MF->getMMI()); 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric // Replace forward-declared registers with the registers containing 660*0b57cec5SDimitry Andric // the desired value. 661*0b57cec5SDimitry Andric for (DenseMap<unsigned, unsigned>::iterator 662*0b57cec5SDimitry Andric I = FuncInfo->RegFixups.begin(), E = FuncInfo->RegFixups.end(); 663*0b57cec5SDimitry Andric I != E; ++I) { 664*0b57cec5SDimitry Andric unsigned From = I->first; 665*0b57cec5SDimitry Andric unsigned To = I->second; 666*0b57cec5SDimitry Andric // If To is also scheduled to be replaced, find what its ultimate 667*0b57cec5SDimitry Andric // replacement is. 668*0b57cec5SDimitry Andric while (true) { 669*0b57cec5SDimitry Andric DenseMap<unsigned, unsigned>::iterator J = FuncInfo->RegFixups.find(To); 670*0b57cec5SDimitry Andric if (J == E) break; 671*0b57cec5SDimitry Andric To = J->second; 672*0b57cec5SDimitry Andric } 673*0b57cec5SDimitry Andric // Make sure the new register has a sufficiently constrained register class. 674*0b57cec5SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(From) && 675*0b57cec5SDimitry Andric TargetRegisterInfo::isVirtualRegister(To)) 676*0b57cec5SDimitry Andric MRI.constrainRegClass(To, MRI.getRegClass(From)); 677*0b57cec5SDimitry Andric // Replace it. 678*0b57cec5SDimitry Andric 679*0b57cec5SDimitry Andric 680*0b57cec5SDimitry Andric // Replacing one register with another won't touch the kill flags. 681*0b57cec5SDimitry Andric // We need to conservatively clear the kill flags as a kill on the old 682*0b57cec5SDimitry Andric // register might dominate existing uses of the new register. 683*0b57cec5SDimitry Andric if (!MRI.use_empty(To)) 684*0b57cec5SDimitry Andric MRI.clearKillFlags(From); 685*0b57cec5SDimitry Andric MRI.replaceRegWith(From, To); 686*0b57cec5SDimitry Andric } 687*0b57cec5SDimitry Andric 688*0b57cec5SDimitry Andric TLI->finalizeLowering(*MF); 689*0b57cec5SDimitry Andric 690*0b57cec5SDimitry Andric // Release function-specific state. SDB and CurDAG are already cleared 691*0b57cec5SDimitry Andric // at this point. 692*0b57cec5SDimitry Andric FuncInfo->clear(); 693*0b57cec5SDimitry Andric 694*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** MachineFunction at end of ISel ***\n"); 695*0b57cec5SDimitry Andric LLVM_DEBUG(MF->print(dbgs())); 696*0b57cec5SDimitry Andric 697*0b57cec5SDimitry Andric return true; 698*0b57cec5SDimitry Andric } 699*0b57cec5SDimitry Andric 700*0b57cec5SDimitry Andric static void reportFastISelFailure(MachineFunction &MF, 701*0b57cec5SDimitry Andric OptimizationRemarkEmitter &ORE, 702*0b57cec5SDimitry Andric OptimizationRemarkMissed &R, 703*0b57cec5SDimitry Andric bool ShouldAbort) { 704*0b57cec5SDimitry Andric // Print the function name explicitly if we don't have a debug location (which 705*0b57cec5SDimitry Andric // makes the diagnostic less useful) or if we're going to emit a raw error. 706*0b57cec5SDimitry Andric if (!R.getLocation().isValid() || ShouldAbort) 707*0b57cec5SDimitry Andric R << (" (in function: " + MF.getName() + ")").str(); 708*0b57cec5SDimitry Andric 709*0b57cec5SDimitry Andric if (ShouldAbort) 710*0b57cec5SDimitry Andric report_fatal_error(R.getMsg()); 711*0b57cec5SDimitry Andric 712*0b57cec5SDimitry Andric ORE.emit(R); 713*0b57cec5SDimitry Andric } 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric void SelectionDAGISel::SelectBasicBlock(BasicBlock::const_iterator Begin, 716*0b57cec5SDimitry Andric BasicBlock::const_iterator End, 717*0b57cec5SDimitry Andric bool &HadTailCall) { 718*0b57cec5SDimitry Andric // Allow creating illegal types during DAG building for the basic block. 719*0b57cec5SDimitry Andric CurDAG->NewNodesMustHaveLegalTypes = false; 720*0b57cec5SDimitry Andric 721*0b57cec5SDimitry Andric // Lower the instructions. If a call is emitted as a tail call, cease emitting 722*0b57cec5SDimitry Andric // nodes for this block. 723*0b57cec5SDimitry Andric for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I) { 724*0b57cec5SDimitry Andric if (!ElidedArgCopyInstrs.count(&*I)) 725*0b57cec5SDimitry Andric SDB->visit(*I); 726*0b57cec5SDimitry Andric } 727*0b57cec5SDimitry Andric 728*0b57cec5SDimitry Andric // Make sure the root of the DAG is up-to-date. 729*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getControlRoot()); 730*0b57cec5SDimitry Andric HadTailCall = SDB->HasTailCall; 731*0b57cec5SDimitry Andric SDB->resolveOrClearDbgInfo(); 732*0b57cec5SDimitry Andric SDB->clear(); 733*0b57cec5SDimitry Andric 734*0b57cec5SDimitry Andric // Final step, emit the lowered DAG as machine code. 735*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 736*0b57cec5SDimitry Andric } 737*0b57cec5SDimitry Andric 738*0b57cec5SDimitry Andric void SelectionDAGISel::ComputeLiveOutVRegInfo() { 739*0b57cec5SDimitry Andric SmallPtrSet<SDNode*, 16> VisitedNodes; 740*0b57cec5SDimitry Andric SmallVector<SDNode*, 128> Worklist; 741*0b57cec5SDimitry Andric 742*0b57cec5SDimitry Andric Worklist.push_back(CurDAG->getRoot().getNode()); 743*0b57cec5SDimitry Andric 744*0b57cec5SDimitry Andric KnownBits Known; 745*0b57cec5SDimitry Andric 746*0b57cec5SDimitry Andric do { 747*0b57cec5SDimitry Andric SDNode *N = Worklist.pop_back_val(); 748*0b57cec5SDimitry Andric 749*0b57cec5SDimitry Andric // If we've already seen this node, ignore it. 750*0b57cec5SDimitry Andric if (!VisitedNodes.insert(N).second) 751*0b57cec5SDimitry Andric continue; 752*0b57cec5SDimitry Andric 753*0b57cec5SDimitry Andric // Otherwise, add all chain operands to the worklist. 754*0b57cec5SDimitry Andric for (const SDValue &Op : N->op_values()) 755*0b57cec5SDimitry Andric if (Op.getValueType() == MVT::Other) 756*0b57cec5SDimitry Andric Worklist.push_back(Op.getNode()); 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric // If this is a CopyToReg with a vreg dest, process it. 759*0b57cec5SDimitry Andric if (N->getOpcode() != ISD::CopyToReg) 760*0b57cec5SDimitry Andric continue; 761*0b57cec5SDimitry Andric 762*0b57cec5SDimitry Andric unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 763*0b57cec5SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DestReg)) 764*0b57cec5SDimitry Andric continue; 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric // Ignore non-integer values. 767*0b57cec5SDimitry Andric SDValue Src = N->getOperand(2); 768*0b57cec5SDimitry Andric EVT SrcVT = Src.getValueType(); 769*0b57cec5SDimitry Andric if (!SrcVT.isInteger()) 770*0b57cec5SDimitry Andric continue; 771*0b57cec5SDimitry Andric 772*0b57cec5SDimitry Andric unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src); 773*0b57cec5SDimitry Andric Known = CurDAG->computeKnownBits(Src); 774*0b57cec5SDimitry Andric FuncInfo->AddLiveOutRegInfo(DestReg, NumSignBits, Known); 775*0b57cec5SDimitry Andric } while (!Worklist.empty()); 776*0b57cec5SDimitry Andric } 777*0b57cec5SDimitry Andric 778*0b57cec5SDimitry Andric void SelectionDAGISel::CodeGenAndEmitDAG() { 779*0b57cec5SDimitry Andric StringRef GroupName = "sdag"; 780*0b57cec5SDimitry Andric StringRef GroupDescription = "Instruction Selection and Scheduling"; 781*0b57cec5SDimitry Andric std::string BlockName; 782*0b57cec5SDimitry Andric bool MatchFilterBB = false; (void)MatchFilterBB; 783*0b57cec5SDimitry Andric #ifndef NDEBUG 784*0b57cec5SDimitry Andric TargetTransformInfo &TTI = 785*0b57cec5SDimitry Andric getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*FuncInfo->Fn); 786*0b57cec5SDimitry Andric #endif 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric // Pre-type legalization allow creation of any node types. 789*0b57cec5SDimitry Andric CurDAG->NewNodesMustHaveLegalTypes = false; 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric #ifndef NDEBUG 792*0b57cec5SDimitry Andric MatchFilterBB = (FilterDAGBasicBlockName.empty() || 793*0b57cec5SDimitry Andric FilterDAGBasicBlockName == 794*0b57cec5SDimitry Andric FuncInfo->MBB->getBasicBlock()->getName()); 795*0b57cec5SDimitry Andric #endif 796*0b57cec5SDimitry Andric #ifdef NDEBUG 797*0b57cec5SDimitry Andric if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs || 798*0b57cec5SDimitry Andric ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs || 799*0b57cec5SDimitry Andric ViewSUnitDAGs) 800*0b57cec5SDimitry Andric #endif 801*0b57cec5SDimitry Andric { 802*0b57cec5SDimitry Andric BlockName = 803*0b57cec5SDimitry Andric (MF->getName() + ":" + FuncInfo->MBB->getBasicBlock()->getName()).str(); 804*0b57cec5SDimitry Andric } 805*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Initial selection DAG: " 806*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 807*0b57cec5SDimitry Andric << "'\n"; 808*0b57cec5SDimitry Andric CurDAG->dump()); 809*0b57cec5SDimitry Andric 810*0b57cec5SDimitry Andric if (ViewDAGCombine1 && MatchFilterBB) 811*0b57cec5SDimitry Andric CurDAG->viewGraph("dag-combine1 input for " + BlockName); 812*0b57cec5SDimitry Andric 813*0b57cec5SDimitry Andric // Run the DAG combiner in pre-legalize mode. 814*0b57cec5SDimitry Andric { 815*0b57cec5SDimitry Andric NamedRegionTimer T("combine1", "DAG Combining 1", GroupName, 816*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 817*0b57cec5SDimitry Andric CurDAG->Combine(BeforeLegalizeTypes, AA, OptLevel); 818*0b57cec5SDimitry Andric } 819*0b57cec5SDimitry Andric 820*0b57cec5SDimitry Andric #ifndef NDEBUG 821*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 822*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 823*0b57cec5SDimitry Andric #endif 824*0b57cec5SDimitry Andric 825*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimized lowered selection DAG: " 826*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 827*0b57cec5SDimitry Andric << "'\n"; 828*0b57cec5SDimitry Andric CurDAG->dump()); 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric // Second step, hack on the DAG until it only uses operations and types that 831*0b57cec5SDimitry Andric // the target supports. 832*0b57cec5SDimitry Andric if (ViewLegalizeTypesDAGs && MatchFilterBB) 833*0b57cec5SDimitry Andric CurDAG->viewGraph("legalize-types input for " + BlockName); 834*0b57cec5SDimitry Andric 835*0b57cec5SDimitry Andric bool Changed; 836*0b57cec5SDimitry Andric { 837*0b57cec5SDimitry Andric NamedRegionTimer T("legalize_types", "Type Legalization", GroupName, 838*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 839*0b57cec5SDimitry Andric Changed = CurDAG->LegalizeTypes(); 840*0b57cec5SDimitry Andric } 841*0b57cec5SDimitry Andric 842*0b57cec5SDimitry Andric #ifndef NDEBUG 843*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 844*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 845*0b57cec5SDimitry Andric #endif 846*0b57cec5SDimitry Andric 847*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Type-legalized selection DAG: " 848*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 849*0b57cec5SDimitry Andric << "'\n"; 850*0b57cec5SDimitry Andric CurDAG->dump()); 851*0b57cec5SDimitry Andric 852*0b57cec5SDimitry Andric // Only allow creation of legal node types. 853*0b57cec5SDimitry Andric CurDAG->NewNodesMustHaveLegalTypes = true; 854*0b57cec5SDimitry Andric 855*0b57cec5SDimitry Andric if (Changed) { 856*0b57cec5SDimitry Andric if (ViewDAGCombineLT && MatchFilterBB) 857*0b57cec5SDimitry Andric CurDAG->viewGraph("dag-combine-lt input for " + BlockName); 858*0b57cec5SDimitry Andric 859*0b57cec5SDimitry Andric // Run the DAG combiner in post-type-legalize mode. 860*0b57cec5SDimitry Andric { 861*0b57cec5SDimitry Andric NamedRegionTimer T("combine_lt", "DAG Combining after legalize types", 862*0b57cec5SDimitry Andric GroupName, GroupDescription, TimePassesIsEnabled); 863*0b57cec5SDimitry Andric CurDAG->Combine(AfterLegalizeTypes, AA, OptLevel); 864*0b57cec5SDimitry Andric } 865*0b57cec5SDimitry Andric 866*0b57cec5SDimitry Andric #ifndef NDEBUG 867*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 868*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 869*0b57cec5SDimitry Andric #endif 870*0b57cec5SDimitry Andric 871*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimized type-legalized selection DAG: " 872*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 873*0b57cec5SDimitry Andric << "'\n"; 874*0b57cec5SDimitry Andric CurDAG->dump()); 875*0b57cec5SDimitry Andric } 876*0b57cec5SDimitry Andric 877*0b57cec5SDimitry Andric { 878*0b57cec5SDimitry Andric NamedRegionTimer T("legalize_vec", "Vector Legalization", GroupName, 879*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 880*0b57cec5SDimitry Andric Changed = CurDAG->LegalizeVectors(); 881*0b57cec5SDimitry Andric } 882*0b57cec5SDimitry Andric 883*0b57cec5SDimitry Andric if (Changed) { 884*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Vector-legalized selection DAG: " 885*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 886*0b57cec5SDimitry Andric << "'\n"; 887*0b57cec5SDimitry Andric CurDAG->dump()); 888*0b57cec5SDimitry Andric 889*0b57cec5SDimitry Andric { 890*0b57cec5SDimitry Andric NamedRegionTimer T("legalize_types2", "Type Legalization 2", GroupName, 891*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 892*0b57cec5SDimitry Andric CurDAG->LegalizeTypes(); 893*0b57cec5SDimitry Andric } 894*0b57cec5SDimitry Andric 895*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Vector/type-legalized selection DAG: " 896*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 897*0b57cec5SDimitry Andric << "'\n"; 898*0b57cec5SDimitry Andric CurDAG->dump()); 899*0b57cec5SDimitry Andric 900*0b57cec5SDimitry Andric if (ViewDAGCombineLT && MatchFilterBB) 901*0b57cec5SDimitry Andric CurDAG->viewGraph("dag-combine-lv input for " + BlockName); 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric // Run the DAG combiner in post-type-legalize mode. 904*0b57cec5SDimitry Andric { 905*0b57cec5SDimitry Andric NamedRegionTimer T("combine_lv", "DAG Combining after legalize vectors", 906*0b57cec5SDimitry Andric GroupName, GroupDescription, TimePassesIsEnabled); 907*0b57cec5SDimitry Andric CurDAG->Combine(AfterLegalizeVectorOps, AA, OptLevel); 908*0b57cec5SDimitry Andric } 909*0b57cec5SDimitry Andric 910*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimized vector-legalized selection DAG: " 911*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 912*0b57cec5SDimitry Andric << "'\n"; 913*0b57cec5SDimitry Andric CurDAG->dump()); 914*0b57cec5SDimitry Andric 915*0b57cec5SDimitry Andric #ifndef NDEBUG 916*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 917*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 918*0b57cec5SDimitry Andric #endif 919*0b57cec5SDimitry Andric } 920*0b57cec5SDimitry Andric 921*0b57cec5SDimitry Andric if (ViewLegalizeDAGs && MatchFilterBB) 922*0b57cec5SDimitry Andric CurDAG->viewGraph("legalize input for " + BlockName); 923*0b57cec5SDimitry Andric 924*0b57cec5SDimitry Andric { 925*0b57cec5SDimitry Andric NamedRegionTimer T("legalize", "DAG Legalization", GroupName, 926*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 927*0b57cec5SDimitry Andric CurDAG->Legalize(); 928*0b57cec5SDimitry Andric } 929*0b57cec5SDimitry Andric 930*0b57cec5SDimitry Andric #ifndef NDEBUG 931*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 932*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 933*0b57cec5SDimitry Andric #endif 934*0b57cec5SDimitry Andric 935*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalized selection DAG: " 936*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 937*0b57cec5SDimitry Andric << "'\n"; 938*0b57cec5SDimitry Andric CurDAG->dump()); 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric if (ViewDAGCombine2 && MatchFilterBB) 941*0b57cec5SDimitry Andric CurDAG->viewGraph("dag-combine2 input for " + BlockName); 942*0b57cec5SDimitry Andric 943*0b57cec5SDimitry Andric // Run the DAG combiner in post-legalize mode. 944*0b57cec5SDimitry Andric { 945*0b57cec5SDimitry Andric NamedRegionTimer T("combine2", "DAG Combining 2", GroupName, 946*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 947*0b57cec5SDimitry Andric CurDAG->Combine(AfterLegalizeDAG, AA, OptLevel); 948*0b57cec5SDimitry Andric } 949*0b57cec5SDimitry Andric 950*0b57cec5SDimitry Andric #ifndef NDEBUG 951*0b57cec5SDimitry Andric if (TTI.hasBranchDivergence()) 952*0b57cec5SDimitry Andric CurDAG->VerifyDAGDiverence(); 953*0b57cec5SDimitry Andric #endif 954*0b57cec5SDimitry Andric 955*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimized legalized selection DAG: " 956*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 957*0b57cec5SDimitry Andric << "'\n"; 958*0b57cec5SDimitry Andric CurDAG->dump()); 959*0b57cec5SDimitry Andric 960*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None) 961*0b57cec5SDimitry Andric ComputeLiveOutVRegInfo(); 962*0b57cec5SDimitry Andric 963*0b57cec5SDimitry Andric if (ViewISelDAGs && MatchFilterBB) 964*0b57cec5SDimitry Andric CurDAG->viewGraph("isel input for " + BlockName); 965*0b57cec5SDimitry Andric 966*0b57cec5SDimitry Andric // Third, instruction select all of the operations to machine code, adding the 967*0b57cec5SDimitry Andric // code to the MachineBasicBlock. 968*0b57cec5SDimitry Andric { 969*0b57cec5SDimitry Andric NamedRegionTimer T("isel", "Instruction Selection", GroupName, 970*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 971*0b57cec5SDimitry Andric DoInstructionSelection(); 972*0b57cec5SDimitry Andric } 973*0b57cec5SDimitry Andric 974*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Selected selection DAG: " 975*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 976*0b57cec5SDimitry Andric << "'\n"; 977*0b57cec5SDimitry Andric CurDAG->dump()); 978*0b57cec5SDimitry Andric 979*0b57cec5SDimitry Andric if (ViewSchedDAGs && MatchFilterBB) 980*0b57cec5SDimitry Andric CurDAG->viewGraph("scheduler input for " + BlockName); 981*0b57cec5SDimitry Andric 982*0b57cec5SDimitry Andric // Schedule machine code. 983*0b57cec5SDimitry Andric ScheduleDAGSDNodes *Scheduler = CreateScheduler(); 984*0b57cec5SDimitry Andric { 985*0b57cec5SDimitry Andric NamedRegionTimer T("sched", "Instruction Scheduling", GroupName, 986*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 987*0b57cec5SDimitry Andric Scheduler->Run(CurDAG, FuncInfo->MBB); 988*0b57cec5SDimitry Andric } 989*0b57cec5SDimitry Andric 990*0b57cec5SDimitry Andric if (ViewSUnitDAGs && MatchFilterBB) 991*0b57cec5SDimitry Andric Scheduler->viewGraph(); 992*0b57cec5SDimitry Andric 993*0b57cec5SDimitry Andric // Emit machine code to BB. This can change 'BB' to the last block being 994*0b57cec5SDimitry Andric // inserted into. 995*0b57cec5SDimitry Andric MachineBasicBlock *FirstMBB = FuncInfo->MBB, *LastMBB; 996*0b57cec5SDimitry Andric { 997*0b57cec5SDimitry Andric NamedRegionTimer T("emit", "Instruction Creation", GroupName, 998*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 999*0b57cec5SDimitry Andric 1000*0b57cec5SDimitry Andric // FuncInfo->InsertPt is passed by reference and set to the end of the 1001*0b57cec5SDimitry Andric // scheduled instructions. 1002*0b57cec5SDimitry Andric LastMBB = FuncInfo->MBB = Scheduler->EmitSchedule(FuncInfo->InsertPt); 1003*0b57cec5SDimitry Andric } 1004*0b57cec5SDimitry Andric 1005*0b57cec5SDimitry Andric // If the block was split, make sure we update any references that are used to 1006*0b57cec5SDimitry Andric // update PHI nodes later on. 1007*0b57cec5SDimitry Andric if (FirstMBB != LastMBB) 1008*0b57cec5SDimitry Andric SDB->UpdateSplitBlock(FirstMBB, LastMBB); 1009*0b57cec5SDimitry Andric 1010*0b57cec5SDimitry Andric // Free the scheduler state. 1011*0b57cec5SDimitry Andric { 1012*0b57cec5SDimitry Andric NamedRegionTimer T("cleanup", "Instruction Scheduling Cleanup", GroupName, 1013*0b57cec5SDimitry Andric GroupDescription, TimePassesIsEnabled); 1014*0b57cec5SDimitry Andric delete Scheduler; 1015*0b57cec5SDimitry Andric } 1016*0b57cec5SDimitry Andric 1017*0b57cec5SDimitry Andric // Free the SelectionDAG state, now that we're finished with it. 1018*0b57cec5SDimitry Andric CurDAG->clear(); 1019*0b57cec5SDimitry Andric } 1020*0b57cec5SDimitry Andric 1021*0b57cec5SDimitry Andric namespace { 1022*0b57cec5SDimitry Andric 1023*0b57cec5SDimitry Andric /// ISelUpdater - helper class to handle updates of the instruction selection 1024*0b57cec5SDimitry Andric /// graph. 1025*0b57cec5SDimitry Andric class ISelUpdater : public SelectionDAG::DAGUpdateListener { 1026*0b57cec5SDimitry Andric SelectionDAG::allnodes_iterator &ISelPosition; 1027*0b57cec5SDimitry Andric 1028*0b57cec5SDimitry Andric public: 1029*0b57cec5SDimitry Andric ISelUpdater(SelectionDAG &DAG, SelectionDAG::allnodes_iterator &isp) 1030*0b57cec5SDimitry Andric : SelectionDAG::DAGUpdateListener(DAG), ISelPosition(isp) {} 1031*0b57cec5SDimitry Andric 1032*0b57cec5SDimitry Andric /// NodeDeleted - Handle nodes deleted from the graph. If the node being 1033*0b57cec5SDimitry Andric /// deleted is the current ISelPosition node, update ISelPosition. 1034*0b57cec5SDimitry Andric /// 1035*0b57cec5SDimitry Andric void NodeDeleted(SDNode *N, SDNode *E) override { 1036*0b57cec5SDimitry Andric if (ISelPosition == SelectionDAG::allnodes_iterator(N)) 1037*0b57cec5SDimitry Andric ++ISelPosition; 1038*0b57cec5SDimitry Andric } 1039*0b57cec5SDimitry Andric }; 1040*0b57cec5SDimitry Andric 1041*0b57cec5SDimitry Andric } // end anonymous namespace 1042*0b57cec5SDimitry Andric 1043*0b57cec5SDimitry Andric // This function is used to enforce the topological node id property 1044*0b57cec5SDimitry Andric // property leveraged during Instruction selection. Before selection all 1045*0b57cec5SDimitry Andric // nodes are given a non-negative id such that all nodes have a larger id than 1046*0b57cec5SDimitry Andric // their operands. As this holds transitively we can prune checks that a node N 1047*0b57cec5SDimitry Andric // is a predecessor of M another by not recursively checking through M's 1048*0b57cec5SDimitry Andric // operands if N's ID is larger than M's ID. This is significantly improves 1049*0b57cec5SDimitry Andric // performance of for various legality checks (e.g. IsLegalToFold / 1050*0b57cec5SDimitry Andric // UpdateChains). 1051*0b57cec5SDimitry Andric 1052*0b57cec5SDimitry Andric // However, when we fuse multiple nodes into a single node 1053*0b57cec5SDimitry Andric // during selection we may induce a predecessor relationship between inputs and 1054*0b57cec5SDimitry Andric // outputs of distinct nodes being merged violating the topological property. 1055*0b57cec5SDimitry Andric // Should a fused node have a successor which has yet to be selected, our 1056*0b57cec5SDimitry Andric // legality checks would be incorrect. To avoid this we mark all unselected 1057*0b57cec5SDimitry Andric // sucessor nodes, i.e. id != -1 as invalid for pruning by bit-negating (x => 1058*0b57cec5SDimitry Andric // (-(x+1))) the ids and modify our pruning check to ignore negative Ids of M. 1059*0b57cec5SDimitry Andric // We use bit-negation to more clearly enforce that node id -1 can only be 1060*0b57cec5SDimitry Andric // achieved by selected nodes). As the conversion is reversable the original Id, 1061*0b57cec5SDimitry Andric // topological pruning can still be leveraged when looking for unselected nodes. 1062*0b57cec5SDimitry Andric // This method is call internally in all ISel replacement calls. 1063*0b57cec5SDimitry Andric void SelectionDAGISel::EnforceNodeIdInvariant(SDNode *Node) { 1064*0b57cec5SDimitry Andric SmallVector<SDNode *, 4> Nodes; 1065*0b57cec5SDimitry Andric Nodes.push_back(Node); 1066*0b57cec5SDimitry Andric 1067*0b57cec5SDimitry Andric while (!Nodes.empty()) { 1068*0b57cec5SDimitry Andric SDNode *N = Nodes.pop_back_val(); 1069*0b57cec5SDimitry Andric for (auto *U : N->uses()) { 1070*0b57cec5SDimitry Andric auto UId = U->getNodeId(); 1071*0b57cec5SDimitry Andric if (UId > 0) { 1072*0b57cec5SDimitry Andric InvalidateNodeId(U); 1073*0b57cec5SDimitry Andric Nodes.push_back(U); 1074*0b57cec5SDimitry Andric } 1075*0b57cec5SDimitry Andric } 1076*0b57cec5SDimitry Andric } 1077*0b57cec5SDimitry Andric } 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric // InvalidateNodeId - As discusses in EnforceNodeIdInvariant, mark a 1080*0b57cec5SDimitry Andric // NodeId with the equivalent node id which is invalid for topological 1081*0b57cec5SDimitry Andric // pruning. 1082*0b57cec5SDimitry Andric void SelectionDAGISel::InvalidateNodeId(SDNode *N) { 1083*0b57cec5SDimitry Andric int InvalidId = -(N->getNodeId() + 1); 1084*0b57cec5SDimitry Andric N->setNodeId(InvalidId); 1085*0b57cec5SDimitry Andric } 1086*0b57cec5SDimitry Andric 1087*0b57cec5SDimitry Andric // getUninvalidatedNodeId - get original uninvalidated node id. 1088*0b57cec5SDimitry Andric int SelectionDAGISel::getUninvalidatedNodeId(SDNode *N) { 1089*0b57cec5SDimitry Andric int Id = N->getNodeId(); 1090*0b57cec5SDimitry Andric if (Id < -1) 1091*0b57cec5SDimitry Andric return -(Id + 1); 1092*0b57cec5SDimitry Andric return Id; 1093*0b57cec5SDimitry Andric } 1094*0b57cec5SDimitry Andric 1095*0b57cec5SDimitry Andric void SelectionDAGISel::DoInstructionSelection() { 1096*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "===== Instruction selection begins: " 1097*0b57cec5SDimitry Andric << printMBBReference(*FuncInfo->MBB) << " '" 1098*0b57cec5SDimitry Andric << FuncInfo->MBB->getName() << "'\n"); 1099*0b57cec5SDimitry Andric 1100*0b57cec5SDimitry Andric PreprocessISelDAG(); 1101*0b57cec5SDimitry Andric 1102*0b57cec5SDimitry Andric // Select target instructions for the DAG. 1103*0b57cec5SDimitry Andric { 1104*0b57cec5SDimitry Andric // Number all nodes with a topological order and set DAGSize. 1105*0b57cec5SDimitry Andric DAGSize = CurDAG->AssignTopologicalOrder(); 1106*0b57cec5SDimitry Andric 1107*0b57cec5SDimitry Andric // Create a dummy node (which is not added to allnodes), that adds 1108*0b57cec5SDimitry Andric // a reference to the root node, preventing it from being deleted, 1109*0b57cec5SDimitry Andric // and tracking any changes of the root. 1110*0b57cec5SDimitry Andric HandleSDNode Dummy(CurDAG->getRoot()); 1111*0b57cec5SDimitry Andric SelectionDAG::allnodes_iterator ISelPosition (CurDAG->getRoot().getNode()); 1112*0b57cec5SDimitry Andric ++ISelPosition; 1113*0b57cec5SDimitry Andric 1114*0b57cec5SDimitry Andric // Make sure that ISelPosition gets properly updated when nodes are deleted 1115*0b57cec5SDimitry Andric // in calls made from this function. 1116*0b57cec5SDimitry Andric ISelUpdater ISU(*CurDAG, ISelPosition); 1117*0b57cec5SDimitry Andric 1118*0b57cec5SDimitry Andric // The AllNodes list is now topological-sorted. Visit the 1119*0b57cec5SDimitry Andric // nodes by starting at the end of the list (the root of the 1120*0b57cec5SDimitry Andric // graph) and preceding back toward the beginning (the entry 1121*0b57cec5SDimitry Andric // node). 1122*0b57cec5SDimitry Andric while (ISelPosition != CurDAG->allnodes_begin()) { 1123*0b57cec5SDimitry Andric SDNode *Node = &*--ISelPosition; 1124*0b57cec5SDimitry Andric // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes, 1125*0b57cec5SDimitry Andric // but there are currently some corner cases that it misses. Also, this 1126*0b57cec5SDimitry Andric // makes it theoretically possible to disable the DAGCombiner. 1127*0b57cec5SDimitry Andric if (Node->use_empty()) 1128*0b57cec5SDimitry Andric continue; 1129*0b57cec5SDimitry Andric 1130*0b57cec5SDimitry Andric #ifndef NDEBUG 1131*0b57cec5SDimitry Andric SmallVector<SDNode *, 4> Nodes; 1132*0b57cec5SDimitry Andric Nodes.push_back(Node); 1133*0b57cec5SDimitry Andric 1134*0b57cec5SDimitry Andric while (!Nodes.empty()) { 1135*0b57cec5SDimitry Andric auto N = Nodes.pop_back_val(); 1136*0b57cec5SDimitry Andric if (N->getOpcode() == ISD::TokenFactor || N->getNodeId() < 0) 1137*0b57cec5SDimitry Andric continue; 1138*0b57cec5SDimitry Andric for (const SDValue &Op : N->op_values()) { 1139*0b57cec5SDimitry Andric if (Op->getOpcode() == ISD::TokenFactor) 1140*0b57cec5SDimitry Andric Nodes.push_back(Op.getNode()); 1141*0b57cec5SDimitry Andric else { 1142*0b57cec5SDimitry Andric // We rely on topological ordering of node ids for checking for 1143*0b57cec5SDimitry Andric // cycles when fusing nodes during selection. All unselected nodes 1144*0b57cec5SDimitry Andric // successors of an already selected node should have a negative id. 1145*0b57cec5SDimitry Andric // This assertion will catch such cases. If this assertion triggers 1146*0b57cec5SDimitry Andric // it is likely you using DAG-level Value/Node replacement functions 1147*0b57cec5SDimitry Andric // (versus equivalent ISEL replacement) in backend-specific 1148*0b57cec5SDimitry Andric // selections. See comment in EnforceNodeIdInvariant for more 1149*0b57cec5SDimitry Andric // details. 1150*0b57cec5SDimitry Andric assert(Op->getNodeId() != -1 && 1151*0b57cec5SDimitry Andric "Node has already selected predecessor node"); 1152*0b57cec5SDimitry Andric } 1153*0b57cec5SDimitry Andric } 1154*0b57cec5SDimitry Andric } 1155*0b57cec5SDimitry Andric #endif 1156*0b57cec5SDimitry Andric 1157*0b57cec5SDimitry Andric // When we are using non-default rounding modes or FP exception behavior 1158*0b57cec5SDimitry Andric // FP operations are represented by StrictFP pseudo-operations. For 1159*0b57cec5SDimitry Andric // targets that do not (yet) understand strict FP operations directly, 1160*0b57cec5SDimitry Andric // we convert them to normal FP opcodes instead at this point. This 1161*0b57cec5SDimitry Andric // will allow them to be handled by existing target-specific instruction 1162*0b57cec5SDimitry Andric // selectors. 1163*0b57cec5SDimitry Andric if (Node->isStrictFPOpcode() && 1164*0b57cec5SDimitry Andric (TLI->getOperationAction(Node->getOpcode(), Node->getValueType(0)) 1165*0b57cec5SDimitry Andric != TargetLowering::Legal)) 1166*0b57cec5SDimitry Andric Node = CurDAG->mutateStrictFPToFP(Node); 1167*0b57cec5SDimitry Andric 1168*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nISEL: Starting selection on root node: "; 1169*0b57cec5SDimitry Andric Node->dump(CurDAG)); 1170*0b57cec5SDimitry Andric 1171*0b57cec5SDimitry Andric Select(Node); 1172*0b57cec5SDimitry Andric } 1173*0b57cec5SDimitry Andric 1174*0b57cec5SDimitry Andric CurDAG->setRoot(Dummy.getValue()); 1175*0b57cec5SDimitry Andric } 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n===== Instruction selection ends:\n"); 1178*0b57cec5SDimitry Andric 1179*0b57cec5SDimitry Andric PostprocessISelDAG(); 1180*0b57cec5SDimitry Andric } 1181*0b57cec5SDimitry Andric 1182*0b57cec5SDimitry Andric static bool hasExceptionPointerOrCodeUser(const CatchPadInst *CPI) { 1183*0b57cec5SDimitry Andric for (const User *U : CPI->users()) { 1184*0b57cec5SDimitry Andric if (const IntrinsicInst *EHPtrCall = dyn_cast<IntrinsicInst>(U)) { 1185*0b57cec5SDimitry Andric Intrinsic::ID IID = EHPtrCall->getIntrinsicID(); 1186*0b57cec5SDimitry Andric if (IID == Intrinsic::eh_exceptionpointer || 1187*0b57cec5SDimitry Andric IID == Intrinsic::eh_exceptioncode) 1188*0b57cec5SDimitry Andric return true; 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric } 1191*0b57cec5SDimitry Andric return false; 1192*0b57cec5SDimitry Andric } 1193*0b57cec5SDimitry Andric 1194*0b57cec5SDimitry Andric // wasm.landingpad.index intrinsic is for associating a landing pad index number 1195*0b57cec5SDimitry Andric // with a catchpad instruction. Retrieve the landing pad index in the intrinsic 1196*0b57cec5SDimitry Andric // and store the mapping in the function. 1197*0b57cec5SDimitry Andric static void mapWasmLandingPadIndex(MachineBasicBlock *MBB, 1198*0b57cec5SDimitry Andric const CatchPadInst *CPI) { 1199*0b57cec5SDimitry Andric MachineFunction *MF = MBB->getParent(); 1200*0b57cec5SDimitry Andric // In case of single catch (...), we don't emit LSDA, so we don't need 1201*0b57cec5SDimitry Andric // this information. 1202*0b57cec5SDimitry Andric bool IsSingleCatchAllClause = 1203*0b57cec5SDimitry Andric CPI->getNumArgOperands() == 1 && 1204*0b57cec5SDimitry Andric cast<Constant>(CPI->getArgOperand(0))->isNullValue(); 1205*0b57cec5SDimitry Andric if (!IsSingleCatchAllClause) { 1206*0b57cec5SDimitry Andric // Create a mapping from landing pad label to landing pad index. 1207*0b57cec5SDimitry Andric bool IntrFound = false; 1208*0b57cec5SDimitry Andric for (const User *U : CPI->users()) { 1209*0b57cec5SDimitry Andric if (const auto *Call = dyn_cast<IntrinsicInst>(U)) { 1210*0b57cec5SDimitry Andric Intrinsic::ID IID = Call->getIntrinsicID(); 1211*0b57cec5SDimitry Andric if (IID == Intrinsic::wasm_landingpad_index) { 1212*0b57cec5SDimitry Andric Value *IndexArg = Call->getArgOperand(1); 1213*0b57cec5SDimitry Andric int Index = cast<ConstantInt>(IndexArg)->getZExtValue(); 1214*0b57cec5SDimitry Andric MF->setWasmLandingPadIndex(MBB, Index); 1215*0b57cec5SDimitry Andric IntrFound = true; 1216*0b57cec5SDimitry Andric break; 1217*0b57cec5SDimitry Andric } 1218*0b57cec5SDimitry Andric } 1219*0b57cec5SDimitry Andric } 1220*0b57cec5SDimitry Andric assert(IntrFound && "wasm.landingpad.index intrinsic not found!"); 1221*0b57cec5SDimitry Andric (void)IntrFound; 1222*0b57cec5SDimitry Andric } 1223*0b57cec5SDimitry Andric } 1224*0b57cec5SDimitry Andric 1225*0b57cec5SDimitry Andric /// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and 1226*0b57cec5SDimitry Andric /// do other setup for EH landing-pad blocks. 1227*0b57cec5SDimitry Andric bool SelectionDAGISel::PrepareEHLandingPad() { 1228*0b57cec5SDimitry Andric MachineBasicBlock *MBB = FuncInfo->MBB; 1229*0b57cec5SDimitry Andric const Constant *PersonalityFn = FuncInfo->Fn->getPersonalityFn(); 1230*0b57cec5SDimitry Andric const BasicBlock *LLVMBB = MBB->getBasicBlock(); 1231*0b57cec5SDimitry Andric const TargetRegisterClass *PtrRC = 1232*0b57cec5SDimitry Andric TLI->getRegClassFor(TLI->getPointerTy(CurDAG->getDataLayout())); 1233*0b57cec5SDimitry Andric 1234*0b57cec5SDimitry Andric auto Pers = classifyEHPersonality(PersonalityFn); 1235*0b57cec5SDimitry Andric 1236*0b57cec5SDimitry Andric // Catchpads have one live-in register, which typically holds the exception 1237*0b57cec5SDimitry Andric // pointer or code. 1238*0b57cec5SDimitry Andric if (isFuncletEHPersonality(Pers)) { 1239*0b57cec5SDimitry Andric if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) { 1240*0b57cec5SDimitry Andric if (hasExceptionPointerOrCodeUser(CPI)) { 1241*0b57cec5SDimitry Andric // Get or create the virtual register to hold the pointer or code. Mark 1242*0b57cec5SDimitry Andric // the live in physreg and copy into the vreg. 1243*0b57cec5SDimitry Andric MCPhysReg EHPhysReg = TLI->getExceptionPointerRegister(PersonalityFn); 1244*0b57cec5SDimitry Andric assert(EHPhysReg && "target lacks exception pointer register"); 1245*0b57cec5SDimitry Andric MBB->addLiveIn(EHPhysReg); 1246*0b57cec5SDimitry Andric unsigned VReg = FuncInfo->getCatchPadExceptionPointerVReg(CPI, PtrRC); 1247*0b57cec5SDimitry Andric BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), 1248*0b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), VReg) 1249*0b57cec5SDimitry Andric .addReg(EHPhysReg, RegState::Kill); 1250*0b57cec5SDimitry Andric } 1251*0b57cec5SDimitry Andric } 1252*0b57cec5SDimitry Andric return true; 1253*0b57cec5SDimitry Andric } 1254*0b57cec5SDimitry Andric 1255*0b57cec5SDimitry Andric // Add a label to mark the beginning of the landing pad. Deletion of the 1256*0b57cec5SDimitry Andric // landing pad can thus be detected via the MachineModuleInfo. 1257*0b57cec5SDimitry Andric MCSymbol *Label = MF->addLandingPad(MBB); 1258*0b57cec5SDimitry Andric 1259*0b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::EH_LABEL); 1260*0b57cec5SDimitry Andric BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II) 1261*0b57cec5SDimitry Andric .addSym(Label); 1262*0b57cec5SDimitry Andric 1263*0b57cec5SDimitry Andric if (Pers == EHPersonality::Wasm_CXX) { 1264*0b57cec5SDimitry Andric if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) 1265*0b57cec5SDimitry Andric mapWasmLandingPadIndex(MBB, CPI); 1266*0b57cec5SDimitry Andric } else { 1267*0b57cec5SDimitry Andric // Assign the call site to the landing pad's begin label. 1268*0b57cec5SDimitry Andric MF->setCallSiteLandingPad(Label, SDB->LPadToCallSiteMap[MBB]); 1269*0b57cec5SDimitry Andric // Mark exception register as live in. 1270*0b57cec5SDimitry Andric if (unsigned Reg = TLI->getExceptionPointerRegister(PersonalityFn)) 1271*0b57cec5SDimitry Andric FuncInfo->ExceptionPointerVirtReg = MBB->addLiveIn(Reg, PtrRC); 1272*0b57cec5SDimitry Andric // Mark exception selector register as live in. 1273*0b57cec5SDimitry Andric if (unsigned Reg = TLI->getExceptionSelectorRegister(PersonalityFn)) 1274*0b57cec5SDimitry Andric FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC); 1275*0b57cec5SDimitry Andric } 1276*0b57cec5SDimitry Andric 1277*0b57cec5SDimitry Andric return true; 1278*0b57cec5SDimitry Andric } 1279*0b57cec5SDimitry Andric 1280*0b57cec5SDimitry Andric /// isFoldedOrDeadInstruction - Return true if the specified instruction is 1281*0b57cec5SDimitry Andric /// side-effect free and is either dead or folded into a generated instruction. 1282*0b57cec5SDimitry Andric /// Return false if it needs to be emitted. 1283*0b57cec5SDimitry Andric static bool isFoldedOrDeadInstruction(const Instruction *I, 1284*0b57cec5SDimitry Andric FunctionLoweringInfo *FuncInfo) { 1285*0b57cec5SDimitry Andric return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded. 1286*0b57cec5SDimitry Andric !I->isTerminator() && // Terminators aren't folded. 1287*0b57cec5SDimitry Andric !isa<DbgInfoIntrinsic>(I) && // Debug instructions aren't folded. 1288*0b57cec5SDimitry Andric !I->isEHPad() && // EH pad instructions aren't folded. 1289*0b57cec5SDimitry Andric !FuncInfo->isExportedInst(I); // Exported instrs must be computed. 1290*0b57cec5SDimitry Andric } 1291*0b57cec5SDimitry Andric 1292*0b57cec5SDimitry Andric /// Collect llvm.dbg.declare information. This is done after argument lowering 1293*0b57cec5SDimitry Andric /// in case the declarations refer to arguments. 1294*0b57cec5SDimitry Andric static void processDbgDeclares(FunctionLoweringInfo *FuncInfo) { 1295*0b57cec5SDimitry Andric MachineFunction *MF = FuncInfo->MF; 1296*0b57cec5SDimitry Andric const DataLayout &DL = MF->getDataLayout(); 1297*0b57cec5SDimitry Andric for (const BasicBlock &BB : *FuncInfo->Fn) { 1298*0b57cec5SDimitry Andric for (const Instruction &I : BB) { 1299*0b57cec5SDimitry Andric const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(&I); 1300*0b57cec5SDimitry Andric if (!DI) 1301*0b57cec5SDimitry Andric continue; 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric assert(DI->getVariable() && "Missing variable"); 1304*0b57cec5SDimitry Andric assert(DI->getDebugLoc() && "Missing location"); 1305*0b57cec5SDimitry Andric const Value *Address = DI->getAddress(); 1306*0b57cec5SDimitry Andric if (!Address) 1307*0b57cec5SDimitry Andric continue; 1308*0b57cec5SDimitry Andric 1309*0b57cec5SDimitry Andric // Look through casts and constant offset GEPs. These mostly come from 1310*0b57cec5SDimitry Andric // inalloca. 1311*0b57cec5SDimitry Andric APInt Offset(DL.getTypeSizeInBits(Address->getType()), 0); 1312*0b57cec5SDimitry Andric Address = Address->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric // Check if the variable is a static alloca or a byval or inalloca 1315*0b57cec5SDimitry Andric // argument passed in memory. If it is not, then we will ignore this 1316*0b57cec5SDimitry Andric // intrinsic and handle this during isel like dbg.value. 1317*0b57cec5SDimitry Andric int FI = std::numeric_limits<int>::max(); 1318*0b57cec5SDimitry Andric if (const auto *AI = dyn_cast<AllocaInst>(Address)) { 1319*0b57cec5SDimitry Andric auto SI = FuncInfo->StaticAllocaMap.find(AI); 1320*0b57cec5SDimitry Andric if (SI != FuncInfo->StaticAllocaMap.end()) 1321*0b57cec5SDimitry Andric FI = SI->second; 1322*0b57cec5SDimitry Andric } else if (const auto *Arg = dyn_cast<Argument>(Address)) 1323*0b57cec5SDimitry Andric FI = FuncInfo->getArgumentFrameIndex(Arg); 1324*0b57cec5SDimitry Andric 1325*0b57cec5SDimitry Andric if (FI == std::numeric_limits<int>::max()) 1326*0b57cec5SDimitry Andric continue; 1327*0b57cec5SDimitry Andric 1328*0b57cec5SDimitry Andric DIExpression *Expr = DI->getExpression(); 1329*0b57cec5SDimitry Andric if (Offset.getBoolValue()) 1330*0b57cec5SDimitry Andric Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, 1331*0b57cec5SDimitry Andric Offset.getZExtValue()); 1332*0b57cec5SDimitry Andric MF->setVariableDbgInfo(DI->getVariable(), Expr, FI, DI->getDebugLoc()); 1333*0b57cec5SDimitry Andric } 1334*0b57cec5SDimitry Andric } 1335*0b57cec5SDimitry Andric } 1336*0b57cec5SDimitry Andric 1337*0b57cec5SDimitry Andric void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) { 1338*0b57cec5SDimitry Andric FastISelFailed = false; 1339*0b57cec5SDimitry Andric // Initialize the Fast-ISel state, if needed. 1340*0b57cec5SDimitry Andric FastISel *FastIS = nullptr; 1341*0b57cec5SDimitry Andric if (TM.Options.EnableFastISel) { 1342*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Enabling fast-isel\n"); 1343*0b57cec5SDimitry Andric FastIS = TLI->createFastISel(*FuncInfo, LibInfo); 1344*0b57cec5SDimitry Andric } 1345*0b57cec5SDimitry Andric 1346*0b57cec5SDimitry Andric ReversePostOrderTraversal<const Function*> RPOT(&Fn); 1347*0b57cec5SDimitry Andric 1348*0b57cec5SDimitry Andric // Lower arguments up front. An RPO iteration always visits the entry block 1349*0b57cec5SDimitry Andric // first. 1350*0b57cec5SDimitry Andric assert(*RPOT.begin() == &Fn.getEntryBlock()); 1351*0b57cec5SDimitry Andric ++NumEntryBlocks; 1352*0b57cec5SDimitry Andric 1353*0b57cec5SDimitry Andric // Set up FuncInfo for ISel. Entry blocks never have PHIs. 1354*0b57cec5SDimitry Andric FuncInfo->MBB = FuncInfo->MBBMap[&Fn.getEntryBlock()]; 1355*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->begin(); 1356*0b57cec5SDimitry Andric 1357*0b57cec5SDimitry Andric CurDAG->setFunctionLoweringInfo(FuncInfo); 1358*0b57cec5SDimitry Andric 1359*0b57cec5SDimitry Andric if (!FastIS) { 1360*0b57cec5SDimitry Andric LowerArguments(Fn); 1361*0b57cec5SDimitry Andric } else { 1362*0b57cec5SDimitry Andric // See if fast isel can lower the arguments. 1363*0b57cec5SDimitry Andric FastIS->startNewBlock(); 1364*0b57cec5SDimitry Andric if (!FastIS->lowerArguments()) { 1365*0b57cec5SDimitry Andric FastISelFailed = true; 1366*0b57cec5SDimitry Andric // Fast isel failed to lower these arguments 1367*0b57cec5SDimitry Andric ++NumFastIselFailLowerArguments; 1368*0b57cec5SDimitry Andric 1369*0b57cec5SDimitry Andric OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1370*0b57cec5SDimitry Andric Fn.getSubprogram(), 1371*0b57cec5SDimitry Andric &Fn.getEntryBlock()); 1372*0b57cec5SDimitry Andric R << "FastISel didn't lower all arguments: " 1373*0b57cec5SDimitry Andric << ore::NV("Prototype", Fn.getType()); 1374*0b57cec5SDimitry Andric reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 1); 1375*0b57cec5SDimitry Andric 1376*0b57cec5SDimitry Andric // Use SelectionDAG argument lowering 1377*0b57cec5SDimitry Andric LowerArguments(Fn); 1378*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getControlRoot()); 1379*0b57cec5SDimitry Andric SDB->clear(); 1380*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1381*0b57cec5SDimitry Andric } 1382*0b57cec5SDimitry Andric 1383*0b57cec5SDimitry Andric // If we inserted any instructions at the beginning, make a note of 1384*0b57cec5SDimitry Andric // where they are, so we can be sure to emit subsequent instructions 1385*0b57cec5SDimitry Andric // after them. 1386*0b57cec5SDimitry Andric if (FuncInfo->InsertPt != FuncInfo->MBB->begin()) 1387*0b57cec5SDimitry Andric FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt)); 1388*0b57cec5SDimitry Andric else 1389*0b57cec5SDimitry Andric FastIS->setLastLocalValue(nullptr); 1390*0b57cec5SDimitry Andric } 1391*0b57cec5SDimitry Andric 1392*0b57cec5SDimitry Andric bool Inserted = SwiftError->createEntriesInEntryBlock(SDB->getCurDebugLoc()); 1393*0b57cec5SDimitry Andric 1394*0b57cec5SDimitry Andric if (FastIS && Inserted) 1395*0b57cec5SDimitry Andric FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt)); 1396*0b57cec5SDimitry Andric 1397*0b57cec5SDimitry Andric processDbgDeclares(FuncInfo); 1398*0b57cec5SDimitry Andric 1399*0b57cec5SDimitry Andric // Iterate over all basic blocks in the function. 1400*0b57cec5SDimitry Andric StackProtector &SP = getAnalysis<StackProtector>(); 1401*0b57cec5SDimitry Andric for (const BasicBlock *LLVMBB : RPOT) { 1402*0b57cec5SDimitry Andric if (OptLevel != CodeGenOpt::None) { 1403*0b57cec5SDimitry Andric bool AllPredsVisited = true; 1404*0b57cec5SDimitry Andric for (const_pred_iterator PI = pred_begin(LLVMBB), PE = pred_end(LLVMBB); 1405*0b57cec5SDimitry Andric PI != PE; ++PI) { 1406*0b57cec5SDimitry Andric if (!FuncInfo->VisitedBBs.count(*PI)) { 1407*0b57cec5SDimitry Andric AllPredsVisited = false; 1408*0b57cec5SDimitry Andric break; 1409*0b57cec5SDimitry Andric } 1410*0b57cec5SDimitry Andric } 1411*0b57cec5SDimitry Andric 1412*0b57cec5SDimitry Andric if (AllPredsVisited) { 1413*0b57cec5SDimitry Andric for (const PHINode &PN : LLVMBB->phis()) 1414*0b57cec5SDimitry Andric FuncInfo->ComputePHILiveOutRegInfo(&PN); 1415*0b57cec5SDimitry Andric } else { 1416*0b57cec5SDimitry Andric for (const PHINode &PN : LLVMBB->phis()) 1417*0b57cec5SDimitry Andric FuncInfo->InvalidatePHILiveOutRegInfo(&PN); 1418*0b57cec5SDimitry Andric } 1419*0b57cec5SDimitry Andric 1420*0b57cec5SDimitry Andric FuncInfo->VisitedBBs.insert(LLVMBB); 1421*0b57cec5SDimitry Andric } 1422*0b57cec5SDimitry Andric 1423*0b57cec5SDimitry Andric BasicBlock::const_iterator const Begin = 1424*0b57cec5SDimitry Andric LLVMBB->getFirstNonPHI()->getIterator(); 1425*0b57cec5SDimitry Andric BasicBlock::const_iterator const End = LLVMBB->end(); 1426*0b57cec5SDimitry Andric BasicBlock::const_iterator BI = End; 1427*0b57cec5SDimitry Andric 1428*0b57cec5SDimitry Andric FuncInfo->MBB = FuncInfo->MBBMap[LLVMBB]; 1429*0b57cec5SDimitry Andric if (!FuncInfo->MBB) 1430*0b57cec5SDimitry Andric continue; // Some blocks like catchpads have no code or MBB. 1431*0b57cec5SDimitry Andric 1432*0b57cec5SDimitry Andric // Insert new instructions after any phi or argument setup code. 1433*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1434*0b57cec5SDimitry Andric 1435*0b57cec5SDimitry Andric // Setup an EH landing-pad block. 1436*0b57cec5SDimitry Andric FuncInfo->ExceptionPointerVirtReg = 0; 1437*0b57cec5SDimitry Andric FuncInfo->ExceptionSelectorVirtReg = 0; 1438*0b57cec5SDimitry Andric if (LLVMBB->isEHPad()) 1439*0b57cec5SDimitry Andric if (!PrepareEHLandingPad()) 1440*0b57cec5SDimitry Andric continue; 1441*0b57cec5SDimitry Andric 1442*0b57cec5SDimitry Andric // Before doing SelectionDAG ISel, see if FastISel has been requested. 1443*0b57cec5SDimitry Andric if (FastIS) { 1444*0b57cec5SDimitry Andric if (LLVMBB != &Fn.getEntryBlock()) 1445*0b57cec5SDimitry Andric FastIS->startNewBlock(); 1446*0b57cec5SDimitry Andric 1447*0b57cec5SDimitry Andric unsigned NumFastIselRemaining = std::distance(Begin, End); 1448*0b57cec5SDimitry Andric 1449*0b57cec5SDimitry Andric // Pre-assign swifterror vregs. 1450*0b57cec5SDimitry Andric SwiftError->preassignVRegs(FuncInfo->MBB, Begin, End); 1451*0b57cec5SDimitry Andric 1452*0b57cec5SDimitry Andric // Do FastISel on as many instructions as possible. 1453*0b57cec5SDimitry Andric for (; BI != Begin; --BI) { 1454*0b57cec5SDimitry Andric const Instruction *Inst = &*std::prev(BI); 1455*0b57cec5SDimitry Andric 1456*0b57cec5SDimitry Andric // If we no longer require this instruction, skip it. 1457*0b57cec5SDimitry Andric if (isFoldedOrDeadInstruction(Inst, FuncInfo) || 1458*0b57cec5SDimitry Andric ElidedArgCopyInstrs.count(Inst)) { 1459*0b57cec5SDimitry Andric --NumFastIselRemaining; 1460*0b57cec5SDimitry Andric continue; 1461*0b57cec5SDimitry Andric } 1462*0b57cec5SDimitry Andric 1463*0b57cec5SDimitry Andric // Bottom-up: reset the insert pos at the top, after any local-value 1464*0b57cec5SDimitry Andric // instructions. 1465*0b57cec5SDimitry Andric FastIS->recomputeInsertPt(); 1466*0b57cec5SDimitry Andric 1467*0b57cec5SDimitry Andric // Try to select the instruction with FastISel. 1468*0b57cec5SDimitry Andric if (FastIS->selectInstruction(Inst)) { 1469*0b57cec5SDimitry Andric --NumFastIselRemaining; 1470*0b57cec5SDimitry Andric ++NumFastIselSuccess; 1471*0b57cec5SDimitry Andric // If fast isel succeeded, skip over all the folded instructions, and 1472*0b57cec5SDimitry Andric // then see if there is a load right before the selected instructions. 1473*0b57cec5SDimitry Andric // Try to fold the load if so. 1474*0b57cec5SDimitry Andric const Instruction *BeforeInst = Inst; 1475*0b57cec5SDimitry Andric while (BeforeInst != &*Begin) { 1476*0b57cec5SDimitry Andric BeforeInst = &*std::prev(BasicBlock::const_iterator(BeforeInst)); 1477*0b57cec5SDimitry Andric if (!isFoldedOrDeadInstruction(BeforeInst, FuncInfo)) 1478*0b57cec5SDimitry Andric break; 1479*0b57cec5SDimitry Andric } 1480*0b57cec5SDimitry Andric if (BeforeInst != Inst && isa<LoadInst>(BeforeInst) && 1481*0b57cec5SDimitry Andric BeforeInst->hasOneUse() && 1482*0b57cec5SDimitry Andric FastIS->tryToFoldLoad(cast<LoadInst>(BeforeInst), Inst)) { 1483*0b57cec5SDimitry Andric // If we succeeded, don't re-select the load. 1484*0b57cec5SDimitry Andric BI = std::next(BasicBlock::const_iterator(BeforeInst)); 1485*0b57cec5SDimitry Andric --NumFastIselRemaining; 1486*0b57cec5SDimitry Andric ++NumFastIselSuccess; 1487*0b57cec5SDimitry Andric } 1488*0b57cec5SDimitry Andric continue; 1489*0b57cec5SDimitry Andric } 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric FastISelFailed = true; 1492*0b57cec5SDimitry Andric 1493*0b57cec5SDimitry Andric // Then handle certain instructions as single-LLVM-Instruction blocks. 1494*0b57cec5SDimitry Andric // We cannot separate out GCrelocates to their own blocks since we need 1495*0b57cec5SDimitry Andric // to keep track of gc-relocates for a particular gc-statepoint. This is 1496*0b57cec5SDimitry Andric // done by SelectionDAGBuilder::LowerAsSTATEPOINT, called before 1497*0b57cec5SDimitry Andric // visitGCRelocate. 1498*0b57cec5SDimitry Andric if (isa<CallInst>(Inst) && !isStatepoint(Inst) && !isGCRelocate(Inst) && 1499*0b57cec5SDimitry Andric !isGCResult(Inst)) { 1500*0b57cec5SDimitry Andric OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1501*0b57cec5SDimitry Andric Inst->getDebugLoc(), LLVMBB); 1502*0b57cec5SDimitry Andric 1503*0b57cec5SDimitry Andric R << "FastISel missed call"; 1504*0b57cec5SDimitry Andric 1505*0b57cec5SDimitry Andric if (R.isEnabled() || EnableFastISelAbort) { 1506*0b57cec5SDimitry Andric std::string InstStrStorage; 1507*0b57cec5SDimitry Andric raw_string_ostream InstStr(InstStrStorage); 1508*0b57cec5SDimitry Andric InstStr << *Inst; 1509*0b57cec5SDimitry Andric 1510*0b57cec5SDimitry Andric R << ": " << InstStr.str(); 1511*0b57cec5SDimitry Andric } 1512*0b57cec5SDimitry Andric 1513*0b57cec5SDimitry Andric reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 2); 1514*0b57cec5SDimitry Andric 1515*0b57cec5SDimitry Andric if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy() && 1516*0b57cec5SDimitry Andric !Inst->use_empty()) { 1517*0b57cec5SDimitry Andric unsigned &R = FuncInfo->ValueMap[Inst]; 1518*0b57cec5SDimitry Andric if (!R) 1519*0b57cec5SDimitry Andric R = FuncInfo->CreateRegs(Inst); 1520*0b57cec5SDimitry Andric } 1521*0b57cec5SDimitry Andric 1522*0b57cec5SDimitry Andric bool HadTailCall = false; 1523*0b57cec5SDimitry Andric MachineBasicBlock::iterator SavedInsertPt = FuncInfo->InsertPt; 1524*0b57cec5SDimitry Andric SelectBasicBlock(Inst->getIterator(), BI, HadTailCall); 1525*0b57cec5SDimitry Andric 1526*0b57cec5SDimitry Andric // If the call was emitted as a tail call, we're done with the block. 1527*0b57cec5SDimitry Andric // We also need to delete any previously emitted instructions. 1528*0b57cec5SDimitry Andric if (HadTailCall) { 1529*0b57cec5SDimitry Andric FastIS->removeDeadCode(SavedInsertPt, FuncInfo->MBB->end()); 1530*0b57cec5SDimitry Andric --BI; 1531*0b57cec5SDimitry Andric break; 1532*0b57cec5SDimitry Andric } 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric // Recompute NumFastIselRemaining as Selection DAG instruction 1535*0b57cec5SDimitry Andric // selection may have handled the call, input args, etc. 1536*0b57cec5SDimitry Andric unsigned RemainingNow = std::distance(Begin, BI); 1537*0b57cec5SDimitry Andric NumFastIselFailures += NumFastIselRemaining - RemainingNow; 1538*0b57cec5SDimitry Andric NumFastIselRemaining = RemainingNow; 1539*0b57cec5SDimitry Andric continue; 1540*0b57cec5SDimitry Andric } 1541*0b57cec5SDimitry Andric 1542*0b57cec5SDimitry Andric OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1543*0b57cec5SDimitry Andric Inst->getDebugLoc(), LLVMBB); 1544*0b57cec5SDimitry Andric 1545*0b57cec5SDimitry Andric bool ShouldAbort = EnableFastISelAbort; 1546*0b57cec5SDimitry Andric if (Inst->isTerminator()) { 1547*0b57cec5SDimitry Andric // Use a different message for terminator misses. 1548*0b57cec5SDimitry Andric R << "FastISel missed terminator"; 1549*0b57cec5SDimitry Andric // Don't abort for terminator unless the level is really high 1550*0b57cec5SDimitry Andric ShouldAbort = (EnableFastISelAbort > 2); 1551*0b57cec5SDimitry Andric } else { 1552*0b57cec5SDimitry Andric R << "FastISel missed"; 1553*0b57cec5SDimitry Andric } 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric if (R.isEnabled() || EnableFastISelAbort) { 1556*0b57cec5SDimitry Andric std::string InstStrStorage; 1557*0b57cec5SDimitry Andric raw_string_ostream InstStr(InstStrStorage); 1558*0b57cec5SDimitry Andric InstStr << *Inst; 1559*0b57cec5SDimitry Andric R << ": " << InstStr.str(); 1560*0b57cec5SDimitry Andric } 1561*0b57cec5SDimitry Andric 1562*0b57cec5SDimitry Andric reportFastISelFailure(*MF, *ORE, R, ShouldAbort); 1563*0b57cec5SDimitry Andric 1564*0b57cec5SDimitry Andric NumFastIselFailures += NumFastIselRemaining; 1565*0b57cec5SDimitry Andric break; 1566*0b57cec5SDimitry Andric } 1567*0b57cec5SDimitry Andric 1568*0b57cec5SDimitry Andric FastIS->recomputeInsertPt(); 1569*0b57cec5SDimitry Andric } 1570*0b57cec5SDimitry Andric 1571*0b57cec5SDimitry Andric if (SP.shouldEmitSDCheck(*LLVMBB)) { 1572*0b57cec5SDimitry Andric bool FunctionBasedInstrumentation = 1573*0b57cec5SDimitry Andric TLI->getSSPStackGuardCheck(*Fn.getParent()); 1574*0b57cec5SDimitry Andric SDB->SPDescriptor.initialize(LLVMBB, FuncInfo->MBBMap[LLVMBB], 1575*0b57cec5SDimitry Andric FunctionBasedInstrumentation); 1576*0b57cec5SDimitry Andric } 1577*0b57cec5SDimitry Andric 1578*0b57cec5SDimitry Andric if (Begin != BI) 1579*0b57cec5SDimitry Andric ++NumDAGBlocks; 1580*0b57cec5SDimitry Andric else 1581*0b57cec5SDimitry Andric ++NumFastIselBlocks; 1582*0b57cec5SDimitry Andric 1583*0b57cec5SDimitry Andric if (Begin != BI) { 1584*0b57cec5SDimitry Andric // Run SelectionDAG instruction selection on the remainder of the block 1585*0b57cec5SDimitry Andric // not handled by FastISel. If FastISel is not run, this is the entire 1586*0b57cec5SDimitry Andric // block. 1587*0b57cec5SDimitry Andric bool HadTailCall; 1588*0b57cec5SDimitry Andric SelectBasicBlock(Begin, BI, HadTailCall); 1589*0b57cec5SDimitry Andric 1590*0b57cec5SDimitry Andric // But if FastISel was run, we already selected some of the block. 1591*0b57cec5SDimitry Andric // If we emitted a tail-call, we need to delete any previously emitted 1592*0b57cec5SDimitry Andric // instruction that follows it. 1593*0b57cec5SDimitry Andric if (HadTailCall && FuncInfo->InsertPt != FuncInfo->MBB->end()) 1594*0b57cec5SDimitry Andric FastIS->removeDeadCode(FuncInfo->InsertPt, FuncInfo->MBB->end()); 1595*0b57cec5SDimitry Andric } 1596*0b57cec5SDimitry Andric 1597*0b57cec5SDimitry Andric if (FastIS) 1598*0b57cec5SDimitry Andric FastIS->finishBasicBlock(); 1599*0b57cec5SDimitry Andric FinishBasicBlock(); 1600*0b57cec5SDimitry Andric FuncInfo->PHINodesToUpdate.clear(); 1601*0b57cec5SDimitry Andric ElidedArgCopyInstrs.clear(); 1602*0b57cec5SDimitry Andric } 1603*0b57cec5SDimitry Andric 1604*0b57cec5SDimitry Andric SP.copyToMachineFrameInfo(MF->getFrameInfo()); 1605*0b57cec5SDimitry Andric 1606*0b57cec5SDimitry Andric SwiftError->propagateVRegs(); 1607*0b57cec5SDimitry Andric 1608*0b57cec5SDimitry Andric delete FastIS; 1609*0b57cec5SDimitry Andric SDB->clearDanglingDebugInfo(); 1610*0b57cec5SDimitry Andric SDB->SPDescriptor.resetPerFunctionState(); 1611*0b57cec5SDimitry Andric } 1612*0b57cec5SDimitry Andric 1613*0b57cec5SDimitry Andric /// Given that the input MI is before a partial terminator sequence TSeq, return 1614*0b57cec5SDimitry Andric /// true if M + TSeq also a partial terminator sequence. 1615*0b57cec5SDimitry Andric /// 1616*0b57cec5SDimitry Andric /// A Terminator sequence is a sequence of MachineInstrs which at this point in 1617*0b57cec5SDimitry Andric /// lowering copy vregs into physical registers, which are then passed into 1618*0b57cec5SDimitry Andric /// terminator instructors so we can satisfy ABI constraints. A partial 1619*0b57cec5SDimitry Andric /// terminator sequence is an improper subset of a terminator sequence (i.e. it 1620*0b57cec5SDimitry Andric /// may be the whole terminator sequence). 1621*0b57cec5SDimitry Andric static bool MIIsInTerminatorSequence(const MachineInstr &MI) { 1622*0b57cec5SDimitry Andric // If we do not have a copy or an implicit def, we return true if and only if 1623*0b57cec5SDimitry Andric // MI is a debug value. 1624*0b57cec5SDimitry Andric if (!MI.isCopy() && !MI.isImplicitDef()) 1625*0b57cec5SDimitry Andric // Sometimes DBG_VALUE MI sneak in between the copies from the vregs to the 1626*0b57cec5SDimitry Andric // physical registers if there is debug info associated with the terminator 1627*0b57cec5SDimitry Andric // of our mbb. We want to include said debug info in our terminator 1628*0b57cec5SDimitry Andric // sequence, so we return true in that case. 1629*0b57cec5SDimitry Andric return MI.isDebugValue(); 1630*0b57cec5SDimitry Andric 1631*0b57cec5SDimitry Andric // We have left the terminator sequence if we are not doing one of the 1632*0b57cec5SDimitry Andric // following: 1633*0b57cec5SDimitry Andric // 1634*0b57cec5SDimitry Andric // 1. Copying a vreg into a physical register. 1635*0b57cec5SDimitry Andric // 2. Copying a vreg into a vreg. 1636*0b57cec5SDimitry Andric // 3. Defining a register via an implicit def. 1637*0b57cec5SDimitry Andric 1638*0b57cec5SDimitry Andric // OPI should always be a register definition... 1639*0b57cec5SDimitry Andric MachineInstr::const_mop_iterator OPI = MI.operands_begin(); 1640*0b57cec5SDimitry Andric if (!OPI->isReg() || !OPI->isDef()) 1641*0b57cec5SDimitry Andric return false; 1642*0b57cec5SDimitry Andric 1643*0b57cec5SDimitry Andric // Defining any register via an implicit def is always ok. 1644*0b57cec5SDimitry Andric if (MI.isImplicitDef()) 1645*0b57cec5SDimitry Andric return true; 1646*0b57cec5SDimitry Andric 1647*0b57cec5SDimitry Andric // Grab the copy source... 1648*0b57cec5SDimitry Andric MachineInstr::const_mop_iterator OPI2 = OPI; 1649*0b57cec5SDimitry Andric ++OPI2; 1650*0b57cec5SDimitry Andric assert(OPI2 != MI.operands_end() 1651*0b57cec5SDimitry Andric && "Should have a copy implying we should have 2 arguments."); 1652*0b57cec5SDimitry Andric 1653*0b57cec5SDimitry Andric // Make sure that the copy dest is not a vreg when the copy source is a 1654*0b57cec5SDimitry Andric // physical register. 1655*0b57cec5SDimitry Andric if (!OPI2->isReg() || 1656*0b57cec5SDimitry Andric (!TargetRegisterInfo::isPhysicalRegister(OPI->getReg()) && 1657*0b57cec5SDimitry Andric TargetRegisterInfo::isPhysicalRegister(OPI2->getReg()))) 1658*0b57cec5SDimitry Andric return false; 1659*0b57cec5SDimitry Andric 1660*0b57cec5SDimitry Andric return true; 1661*0b57cec5SDimitry Andric } 1662*0b57cec5SDimitry Andric 1663*0b57cec5SDimitry Andric /// Find the split point at which to splice the end of BB into its success stack 1664*0b57cec5SDimitry Andric /// protector check machine basic block. 1665*0b57cec5SDimitry Andric /// 1666*0b57cec5SDimitry Andric /// On many platforms, due to ABI constraints, terminators, even before register 1667*0b57cec5SDimitry Andric /// allocation, use physical registers. This creates an issue for us since 1668*0b57cec5SDimitry Andric /// physical registers at this point can not travel across basic 1669*0b57cec5SDimitry Andric /// blocks. Luckily, selectiondag always moves physical registers into vregs 1670*0b57cec5SDimitry Andric /// when they enter functions and moves them through a sequence of copies back 1671*0b57cec5SDimitry Andric /// into the physical registers right before the terminator creating a 1672*0b57cec5SDimitry Andric /// ``Terminator Sequence''. This function is searching for the beginning of the 1673*0b57cec5SDimitry Andric /// terminator sequence so that we can ensure that we splice off not just the 1674*0b57cec5SDimitry Andric /// terminator, but additionally the copies that move the vregs into the 1675*0b57cec5SDimitry Andric /// physical registers. 1676*0b57cec5SDimitry Andric static MachineBasicBlock::iterator 1677*0b57cec5SDimitry Andric FindSplitPointForStackProtector(MachineBasicBlock *BB) { 1678*0b57cec5SDimitry Andric MachineBasicBlock::iterator SplitPoint = BB->getFirstTerminator(); 1679*0b57cec5SDimitry Andric // 1680*0b57cec5SDimitry Andric if (SplitPoint == BB->begin()) 1681*0b57cec5SDimitry Andric return SplitPoint; 1682*0b57cec5SDimitry Andric 1683*0b57cec5SDimitry Andric MachineBasicBlock::iterator Start = BB->begin(); 1684*0b57cec5SDimitry Andric MachineBasicBlock::iterator Previous = SplitPoint; 1685*0b57cec5SDimitry Andric --Previous; 1686*0b57cec5SDimitry Andric 1687*0b57cec5SDimitry Andric while (MIIsInTerminatorSequence(*Previous)) { 1688*0b57cec5SDimitry Andric SplitPoint = Previous; 1689*0b57cec5SDimitry Andric if (Previous == Start) 1690*0b57cec5SDimitry Andric break; 1691*0b57cec5SDimitry Andric --Previous; 1692*0b57cec5SDimitry Andric } 1693*0b57cec5SDimitry Andric 1694*0b57cec5SDimitry Andric return SplitPoint; 1695*0b57cec5SDimitry Andric } 1696*0b57cec5SDimitry Andric 1697*0b57cec5SDimitry Andric void 1698*0b57cec5SDimitry Andric SelectionDAGISel::FinishBasicBlock() { 1699*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Total amount of phi nodes to update: " 1700*0b57cec5SDimitry Andric << FuncInfo->PHINodesToUpdate.size() << "\n"; 1701*0b57cec5SDimitry Andric for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; 1702*0b57cec5SDimitry Andric ++i) dbgs() 1703*0b57cec5SDimitry Andric << "Node " << i << " : (" << FuncInfo->PHINodesToUpdate[i].first 1704*0b57cec5SDimitry Andric << ", " << FuncInfo->PHINodesToUpdate[i].second << ")\n"); 1705*0b57cec5SDimitry Andric 1706*0b57cec5SDimitry Andric // Next, now that we know what the last MBB the LLVM BB expanded is, update 1707*0b57cec5SDimitry Andric // PHI nodes in successors. 1708*0b57cec5SDimitry Andric for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) { 1709*0b57cec5SDimitry Andric MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first); 1710*0b57cec5SDimitry Andric assert(PHI->isPHI() && 1711*0b57cec5SDimitry Andric "This is not a machine PHI node that we are updating!"); 1712*0b57cec5SDimitry Andric if (!FuncInfo->MBB->isSuccessor(PHI->getParent())) 1713*0b57cec5SDimitry Andric continue; 1714*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB); 1715*0b57cec5SDimitry Andric } 1716*0b57cec5SDimitry Andric 1717*0b57cec5SDimitry Andric // Handle stack protector. 1718*0b57cec5SDimitry Andric if (SDB->SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) { 1719*0b57cec5SDimitry Andric // The target provides a guard check function. There is no need to 1720*0b57cec5SDimitry Andric // generate error handling code or to split current basic block. 1721*0b57cec5SDimitry Andric MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB(); 1722*0b57cec5SDimitry Andric 1723*0b57cec5SDimitry Andric // Add load and check to the basicblock. 1724*0b57cec5SDimitry Andric FuncInfo->MBB = ParentMBB; 1725*0b57cec5SDimitry Andric FuncInfo->InsertPt = 1726*0b57cec5SDimitry Andric FindSplitPointForStackProtector(ParentMBB); 1727*0b57cec5SDimitry Andric SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB); 1728*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1729*0b57cec5SDimitry Andric SDB->clear(); 1730*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1731*0b57cec5SDimitry Andric 1732*0b57cec5SDimitry Andric // Clear the Per-BB State. 1733*0b57cec5SDimitry Andric SDB->SPDescriptor.resetPerBBState(); 1734*0b57cec5SDimitry Andric } else if (SDB->SPDescriptor.shouldEmitStackProtector()) { 1735*0b57cec5SDimitry Andric MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB(); 1736*0b57cec5SDimitry Andric MachineBasicBlock *SuccessMBB = SDB->SPDescriptor.getSuccessMBB(); 1737*0b57cec5SDimitry Andric 1738*0b57cec5SDimitry Andric // Find the split point to split the parent mbb. At the same time copy all 1739*0b57cec5SDimitry Andric // physical registers used in the tail of parent mbb into virtual registers 1740*0b57cec5SDimitry Andric // before the split point and back into physical registers after the split 1741*0b57cec5SDimitry Andric // point. This prevents us needing to deal with Live-ins and many other 1742*0b57cec5SDimitry Andric // register allocation issues caused by us splitting the parent mbb. The 1743*0b57cec5SDimitry Andric // register allocator will clean up said virtual copies later on. 1744*0b57cec5SDimitry Andric MachineBasicBlock::iterator SplitPoint = 1745*0b57cec5SDimitry Andric FindSplitPointForStackProtector(ParentMBB); 1746*0b57cec5SDimitry Andric 1747*0b57cec5SDimitry Andric // Splice the terminator of ParentMBB into SuccessMBB. 1748*0b57cec5SDimitry Andric SuccessMBB->splice(SuccessMBB->end(), ParentMBB, 1749*0b57cec5SDimitry Andric SplitPoint, 1750*0b57cec5SDimitry Andric ParentMBB->end()); 1751*0b57cec5SDimitry Andric 1752*0b57cec5SDimitry Andric // Add compare/jump on neq/jump to the parent BB. 1753*0b57cec5SDimitry Andric FuncInfo->MBB = ParentMBB; 1754*0b57cec5SDimitry Andric FuncInfo->InsertPt = ParentMBB->end(); 1755*0b57cec5SDimitry Andric SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB); 1756*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1757*0b57cec5SDimitry Andric SDB->clear(); 1758*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1759*0b57cec5SDimitry Andric 1760*0b57cec5SDimitry Andric // CodeGen Failure MBB if we have not codegened it yet. 1761*0b57cec5SDimitry Andric MachineBasicBlock *FailureMBB = SDB->SPDescriptor.getFailureMBB(); 1762*0b57cec5SDimitry Andric if (FailureMBB->empty()) { 1763*0b57cec5SDimitry Andric FuncInfo->MBB = FailureMBB; 1764*0b57cec5SDimitry Andric FuncInfo->InsertPt = FailureMBB->end(); 1765*0b57cec5SDimitry Andric SDB->visitSPDescriptorFailure(SDB->SPDescriptor); 1766*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1767*0b57cec5SDimitry Andric SDB->clear(); 1768*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1769*0b57cec5SDimitry Andric } 1770*0b57cec5SDimitry Andric 1771*0b57cec5SDimitry Andric // Clear the Per-BB State. 1772*0b57cec5SDimitry Andric SDB->SPDescriptor.resetPerBBState(); 1773*0b57cec5SDimitry Andric } 1774*0b57cec5SDimitry Andric 1775*0b57cec5SDimitry Andric // Lower each BitTestBlock. 1776*0b57cec5SDimitry Andric for (auto &BTB : SDB->SL->BitTestCases) { 1777*0b57cec5SDimitry Andric // Lower header first, if it wasn't already lowered 1778*0b57cec5SDimitry Andric if (!BTB.Emitted) { 1779*0b57cec5SDimitry Andric // Set the current basic block to the mbb we wish to insert the code into 1780*0b57cec5SDimitry Andric FuncInfo->MBB = BTB.Parent; 1781*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1782*0b57cec5SDimitry Andric // Emit the code 1783*0b57cec5SDimitry Andric SDB->visitBitTestHeader(BTB, FuncInfo->MBB); 1784*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1785*0b57cec5SDimitry Andric SDB->clear(); 1786*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1787*0b57cec5SDimitry Andric } 1788*0b57cec5SDimitry Andric 1789*0b57cec5SDimitry Andric BranchProbability UnhandledProb = BTB.Prob; 1790*0b57cec5SDimitry Andric for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) { 1791*0b57cec5SDimitry Andric UnhandledProb -= BTB.Cases[j].ExtraProb; 1792*0b57cec5SDimitry Andric // Set the current basic block to the mbb we wish to insert the code into 1793*0b57cec5SDimitry Andric FuncInfo->MBB = BTB.Cases[j].ThisBB; 1794*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1795*0b57cec5SDimitry Andric // Emit the code 1796*0b57cec5SDimitry Andric 1797*0b57cec5SDimitry Andric // If all cases cover a contiguous range, it is not necessary to jump to 1798*0b57cec5SDimitry Andric // the default block after the last bit test fails. This is because the 1799*0b57cec5SDimitry Andric // range check during bit test header creation has guaranteed that every 1800*0b57cec5SDimitry Andric // case here doesn't go outside the range. In this case, there is no need 1801*0b57cec5SDimitry Andric // to perform the last bit test, as it will always be true. Instead, make 1802*0b57cec5SDimitry Andric // the second-to-last bit-test fall through to the target of the last bit 1803*0b57cec5SDimitry Andric // test, and delete the last bit test. 1804*0b57cec5SDimitry Andric 1805*0b57cec5SDimitry Andric MachineBasicBlock *NextMBB; 1806*0b57cec5SDimitry Andric if (BTB.ContiguousRange && j + 2 == ej) { 1807*0b57cec5SDimitry Andric // Second-to-last bit-test with contiguous range: fall through to the 1808*0b57cec5SDimitry Andric // target of the final bit test. 1809*0b57cec5SDimitry Andric NextMBB = BTB.Cases[j + 1].TargetBB; 1810*0b57cec5SDimitry Andric } else if (j + 1 == ej) { 1811*0b57cec5SDimitry Andric // For the last bit test, fall through to Default. 1812*0b57cec5SDimitry Andric NextMBB = BTB.Default; 1813*0b57cec5SDimitry Andric } else { 1814*0b57cec5SDimitry Andric // Otherwise, fall through to the next bit test. 1815*0b57cec5SDimitry Andric NextMBB = BTB.Cases[j + 1].ThisBB; 1816*0b57cec5SDimitry Andric } 1817*0b57cec5SDimitry Andric 1818*0b57cec5SDimitry Andric SDB->visitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j], 1819*0b57cec5SDimitry Andric FuncInfo->MBB); 1820*0b57cec5SDimitry Andric 1821*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1822*0b57cec5SDimitry Andric SDB->clear(); 1823*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1824*0b57cec5SDimitry Andric 1825*0b57cec5SDimitry Andric if (BTB.ContiguousRange && j + 2 == ej) { 1826*0b57cec5SDimitry Andric // Since we're not going to use the final bit test, remove it. 1827*0b57cec5SDimitry Andric BTB.Cases.pop_back(); 1828*0b57cec5SDimitry Andric break; 1829*0b57cec5SDimitry Andric } 1830*0b57cec5SDimitry Andric } 1831*0b57cec5SDimitry Andric 1832*0b57cec5SDimitry Andric // Update PHI Nodes 1833*0b57cec5SDimitry Andric for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size(); 1834*0b57cec5SDimitry Andric pi != pe; ++pi) { 1835*0b57cec5SDimitry Andric MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first); 1836*0b57cec5SDimitry Andric MachineBasicBlock *PHIBB = PHI->getParent(); 1837*0b57cec5SDimitry Andric assert(PHI->isPHI() && 1838*0b57cec5SDimitry Andric "This is not a machine PHI node that we are updating!"); 1839*0b57cec5SDimitry Andric // This is "default" BB. We have two jumps to it. From "header" BB and 1840*0b57cec5SDimitry Andric // from last "case" BB, unless the latter was skipped. 1841*0b57cec5SDimitry Andric if (PHIBB == BTB.Default) { 1842*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(BTB.Parent); 1843*0b57cec5SDimitry Andric if (!BTB.ContiguousRange) { 1844*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second) 1845*0b57cec5SDimitry Andric .addMBB(BTB.Cases.back().ThisBB); 1846*0b57cec5SDimitry Andric } 1847*0b57cec5SDimitry Andric } 1848*0b57cec5SDimitry Andric // One of "cases" BB. 1849*0b57cec5SDimitry Andric for (unsigned j = 0, ej = BTB.Cases.size(); 1850*0b57cec5SDimitry Andric j != ej; ++j) { 1851*0b57cec5SDimitry Andric MachineBasicBlock* cBB = BTB.Cases[j].ThisBB; 1852*0b57cec5SDimitry Andric if (cBB->isSuccessor(PHIBB)) 1853*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(cBB); 1854*0b57cec5SDimitry Andric } 1855*0b57cec5SDimitry Andric } 1856*0b57cec5SDimitry Andric } 1857*0b57cec5SDimitry Andric SDB->SL->BitTestCases.clear(); 1858*0b57cec5SDimitry Andric 1859*0b57cec5SDimitry Andric // If the JumpTable record is filled in, then we need to emit a jump table. 1860*0b57cec5SDimitry Andric // Updating the PHI nodes is tricky in this case, since we need to determine 1861*0b57cec5SDimitry Andric // whether the PHI is a successor of the range check MBB or the jump table MBB 1862*0b57cec5SDimitry Andric for (unsigned i = 0, e = SDB->SL->JTCases.size(); i != e; ++i) { 1863*0b57cec5SDimitry Andric // Lower header first, if it wasn't already lowered 1864*0b57cec5SDimitry Andric if (!SDB->SL->JTCases[i].first.Emitted) { 1865*0b57cec5SDimitry Andric // Set the current basic block to the mbb we wish to insert the code into 1866*0b57cec5SDimitry Andric FuncInfo->MBB = SDB->SL->JTCases[i].first.HeaderBB; 1867*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1868*0b57cec5SDimitry Andric // Emit the code 1869*0b57cec5SDimitry Andric SDB->visitJumpTableHeader(SDB->SL->JTCases[i].second, 1870*0b57cec5SDimitry Andric SDB->SL->JTCases[i].first, FuncInfo->MBB); 1871*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1872*0b57cec5SDimitry Andric SDB->clear(); 1873*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1874*0b57cec5SDimitry Andric } 1875*0b57cec5SDimitry Andric 1876*0b57cec5SDimitry Andric // Set the current basic block to the mbb we wish to insert the code into 1877*0b57cec5SDimitry Andric FuncInfo->MBB = SDB->SL->JTCases[i].second.MBB; 1878*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1879*0b57cec5SDimitry Andric // Emit the code 1880*0b57cec5SDimitry Andric SDB->visitJumpTable(SDB->SL->JTCases[i].second); 1881*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1882*0b57cec5SDimitry Andric SDB->clear(); 1883*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1884*0b57cec5SDimitry Andric 1885*0b57cec5SDimitry Andric // Update PHI Nodes 1886*0b57cec5SDimitry Andric for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size(); 1887*0b57cec5SDimitry Andric pi != pe; ++pi) { 1888*0b57cec5SDimitry Andric MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first); 1889*0b57cec5SDimitry Andric MachineBasicBlock *PHIBB = PHI->getParent(); 1890*0b57cec5SDimitry Andric assert(PHI->isPHI() && 1891*0b57cec5SDimitry Andric "This is not a machine PHI node that we are updating!"); 1892*0b57cec5SDimitry Andric // "default" BB. We can go there only from header BB. 1893*0b57cec5SDimitry Andric if (PHIBB == SDB->SL->JTCases[i].second.Default) 1894*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second) 1895*0b57cec5SDimitry Andric .addMBB(SDB->SL->JTCases[i].first.HeaderBB); 1896*0b57cec5SDimitry Andric // JT BB. Just iterate over successors here 1897*0b57cec5SDimitry Andric if (FuncInfo->MBB->isSuccessor(PHIBB)) 1898*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(FuncInfo->MBB); 1899*0b57cec5SDimitry Andric } 1900*0b57cec5SDimitry Andric } 1901*0b57cec5SDimitry Andric SDB->SL->JTCases.clear(); 1902*0b57cec5SDimitry Andric 1903*0b57cec5SDimitry Andric // If we generated any switch lowering information, build and codegen any 1904*0b57cec5SDimitry Andric // additional DAGs necessary. 1905*0b57cec5SDimitry Andric for (unsigned i = 0, e = SDB->SL->SwitchCases.size(); i != e; ++i) { 1906*0b57cec5SDimitry Andric // Set the current basic block to the mbb we wish to insert the code into 1907*0b57cec5SDimitry Andric FuncInfo->MBB = SDB->SL->SwitchCases[i].ThisBB; 1908*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1909*0b57cec5SDimitry Andric 1910*0b57cec5SDimitry Andric // Determine the unique successors. 1911*0b57cec5SDimitry Andric SmallVector<MachineBasicBlock *, 2> Succs; 1912*0b57cec5SDimitry Andric Succs.push_back(SDB->SL->SwitchCases[i].TrueBB); 1913*0b57cec5SDimitry Andric if (SDB->SL->SwitchCases[i].TrueBB != SDB->SL->SwitchCases[i].FalseBB) 1914*0b57cec5SDimitry Andric Succs.push_back(SDB->SL->SwitchCases[i].FalseBB); 1915*0b57cec5SDimitry Andric 1916*0b57cec5SDimitry Andric // Emit the code. Note that this could result in FuncInfo->MBB being split. 1917*0b57cec5SDimitry Andric SDB->visitSwitchCase(SDB->SL->SwitchCases[i], FuncInfo->MBB); 1918*0b57cec5SDimitry Andric CurDAG->setRoot(SDB->getRoot()); 1919*0b57cec5SDimitry Andric SDB->clear(); 1920*0b57cec5SDimitry Andric CodeGenAndEmitDAG(); 1921*0b57cec5SDimitry Andric 1922*0b57cec5SDimitry Andric // Remember the last block, now that any splitting is done, for use in 1923*0b57cec5SDimitry Andric // populating PHI nodes in successors. 1924*0b57cec5SDimitry Andric MachineBasicBlock *ThisBB = FuncInfo->MBB; 1925*0b57cec5SDimitry Andric 1926*0b57cec5SDimitry Andric // Handle any PHI nodes in successors of this chunk, as if we were coming 1927*0b57cec5SDimitry Andric // from the original BB before switch expansion. Note that PHI nodes can 1928*0b57cec5SDimitry Andric // occur multiple times in PHINodesToUpdate. We have to be very careful to 1929*0b57cec5SDimitry Andric // handle them the right number of times. 1930*0b57cec5SDimitry Andric for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 1931*0b57cec5SDimitry Andric FuncInfo->MBB = Succs[i]; 1932*0b57cec5SDimitry Andric FuncInfo->InsertPt = FuncInfo->MBB->end(); 1933*0b57cec5SDimitry Andric // FuncInfo->MBB may have been removed from the CFG if a branch was 1934*0b57cec5SDimitry Andric // constant folded. 1935*0b57cec5SDimitry Andric if (ThisBB->isSuccessor(FuncInfo->MBB)) { 1936*0b57cec5SDimitry Andric for (MachineBasicBlock::iterator 1937*0b57cec5SDimitry Andric MBBI = FuncInfo->MBB->begin(), MBBE = FuncInfo->MBB->end(); 1938*0b57cec5SDimitry Andric MBBI != MBBE && MBBI->isPHI(); ++MBBI) { 1939*0b57cec5SDimitry Andric MachineInstrBuilder PHI(*MF, MBBI); 1940*0b57cec5SDimitry Andric // This value for this PHI node is recorded in PHINodesToUpdate. 1941*0b57cec5SDimitry Andric for (unsigned pn = 0; ; ++pn) { 1942*0b57cec5SDimitry Andric assert(pn != FuncInfo->PHINodesToUpdate.size() && 1943*0b57cec5SDimitry Andric "Didn't find PHI entry!"); 1944*0b57cec5SDimitry Andric if (FuncInfo->PHINodesToUpdate[pn].first == PHI) { 1945*0b57cec5SDimitry Andric PHI.addReg(FuncInfo->PHINodesToUpdate[pn].second).addMBB(ThisBB); 1946*0b57cec5SDimitry Andric break; 1947*0b57cec5SDimitry Andric } 1948*0b57cec5SDimitry Andric } 1949*0b57cec5SDimitry Andric } 1950*0b57cec5SDimitry Andric } 1951*0b57cec5SDimitry Andric } 1952*0b57cec5SDimitry Andric } 1953*0b57cec5SDimitry Andric SDB->SL->SwitchCases.clear(); 1954*0b57cec5SDimitry Andric } 1955*0b57cec5SDimitry Andric 1956*0b57cec5SDimitry Andric /// Create the scheduler. If a specific scheduler was specified 1957*0b57cec5SDimitry Andric /// via the SchedulerRegistry, use it, otherwise select the 1958*0b57cec5SDimitry Andric /// one preferred by the target. 1959*0b57cec5SDimitry Andric /// 1960*0b57cec5SDimitry Andric ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() { 1961*0b57cec5SDimitry Andric return ISHeuristic(this, OptLevel); 1962*0b57cec5SDimitry Andric } 1963*0b57cec5SDimitry Andric 1964*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1965*0b57cec5SDimitry Andric // Helper functions used by the generated instruction selector. 1966*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1967*0b57cec5SDimitry Andric // Calls to these methods are generated by tblgen. 1968*0b57cec5SDimitry Andric 1969*0b57cec5SDimitry Andric /// CheckAndMask - The isel is trying to match something like (and X, 255). If 1970*0b57cec5SDimitry Andric /// the dag combiner simplified the 255, we still want to match. RHS is the 1971*0b57cec5SDimitry Andric /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value 1972*0b57cec5SDimitry Andric /// specified in the .td file (e.g. 255). 1973*0b57cec5SDimitry Andric bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS, 1974*0b57cec5SDimitry Andric int64_t DesiredMaskS) const { 1975*0b57cec5SDimitry Andric const APInt &ActualMask = RHS->getAPIntValue(); 1976*0b57cec5SDimitry Andric const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); 1977*0b57cec5SDimitry Andric 1978*0b57cec5SDimitry Andric // If the actual mask exactly matches, success! 1979*0b57cec5SDimitry Andric if (ActualMask == DesiredMask) 1980*0b57cec5SDimitry Andric return true; 1981*0b57cec5SDimitry Andric 1982*0b57cec5SDimitry Andric // If the actual AND mask is allowing unallowed bits, this doesn't match. 1983*0b57cec5SDimitry Andric if (!ActualMask.isSubsetOf(DesiredMask)) 1984*0b57cec5SDimitry Andric return false; 1985*0b57cec5SDimitry Andric 1986*0b57cec5SDimitry Andric // Otherwise, the DAG Combiner may have proven that the value coming in is 1987*0b57cec5SDimitry Andric // either already zero or is not demanded. Check for known zero input bits. 1988*0b57cec5SDimitry Andric APInt NeededMask = DesiredMask & ~ActualMask; 1989*0b57cec5SDimitry Andric if (CurDAG->MaskedValueIsZero(LHS, NeededMask)) 1990*0b57cec5SDimitry Andric return true; 1991*0b57cec5SDimitry Andric 1992*0b57cec5SDimitry Andric // TODO: check to see if missing bits are just not demanded. 1993*0b57cec5SDimitry Andric 1994*0b57cec5SDimitry Andric // Otherwise, this pattern doesn't match. 1995*0b57cec5SDimitry Andric return false; 1996*0b57cec5SDimitry Andric } 1997*0b57cec5SDimitry Andric 1998*0b57cec5SDimitry Andric /// CheckOrMask - The isel is trying to match something like (or X, 255). If 1999*0b57cec5SDimitry Andric /// the dag combiner simplified the 255, we still want to match. RHS is the 2000*0b57cec5SDimitry Andric /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value 2001*0b57cec5SDimitry Andric /// specified in the .td file (e.g. 255). 2002*0b57cec5SDimitry Andric bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS, 2003*0b57cec5SDimitry Andric int64_t DesiredMaskS) const { 2004*0b57cec5SDimitry Andric const APInt &ActualMask = RHS->getAPIntValue(); 2005*0b57cec5SDimitry Andric const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); 2006*0b57cec5SDimitry Andric 2007*0b57cec5SDimitry Andric // If the actual mask exactly matches, success! 2008*0b57cec5SDimitry Andric if (ActualMask == DesiredMask) 2009*0b57cec5SDimitry Andric return true; 2010*0b57cec5SDimitry Andric 2011*0b57cec5SDimitry Andric // If the actual AND mask is allowing unallowed bits, this doesn't match. 2012*0b57cec5SDimitry Andric if (!ActualMask.isSubsetOf(DesiredMask)) 2013*0b57cec5SDimitry Andric return false; 2014*0b57cec5SDimitry Andric 2015*0b57cec5SDimitry Andric // Otherwise, the DAG Combiner may have proven that the value coming in is 2016*0b57cec5SDimitry Andric // either already zero or is not demanded. Check for known zero input bits. 2017*0b57cec5SDimitry Andric APInt NeededMask = DesiredMask & ~ActualMask; 2018*0b57cec5SDimitry Andric KnownBits Known = CurDAG->computeKnownBits(LHS); 2019*0b57cec5SDimitry Andric 2020*0b57cec5SDimitry Andric // If all the missing bits in the or are already known to be set, match! 2021*0b57cec5SDimitry Andric if (NeededMask.isSubsetOf(Known.One)) 2022*0b57cec5SDimitry Andric return true; 2023*0b57cec5SDimitry Andric 2024*0b57cec5SDimitry Andric // TODO: check to see if missing bits are just not demanded. 2025*0b57cec5SDimitry Andric 2026*0b57cec5SDimitry Andric // Otherwise, this pattern doesn't match. 2027*0b57cec5SDimitry Andric return false; 2028*0b57cec5SDimitry Andric } 2029*0b57cec5SDimitry Andric 2030*0b57cec5SDimitry Andric /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated 2031*0b57cec5SDimitry Andric /// by tblgen. Others should not call it. 2032*0b57cec5SDimitry Andric void SelectionDAGISel::SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops, 2033*0b57cec5SDimitry Andric const SDLoc &DL) { 2034*0b57cec5SDimitry Andric std::vector<SDValue> InOps; 2035*0b57cec5SDimitry Andric std::swap(InOps, Ops); 2036*0b57cec5SDimitry Andric 2037*0b57cec5SDimitry Andric Ops.push_back(InOps[InlineAsm::Op_InputChain]); // 0 2038*0b57cec5SDimitry Andric Ops.push_back(InOps[InlineAsm::Op_AsmString]); // 1 2039*0b57cec5SDimitry Andric Ops.push_back(InOps[InlineAsm::Op_MDNode]); // 2, !srcloc 2040*0b57cec5SDimitry Andric Ops.push_back(InOps[InlineAsm::Op_ExtraInfo]); // 3 (SideEffect, AlignStack) 2041*0b57cec5SDimitry Andric 2042*0b57cec5SDimitry Andric unsigned i = InlineAsm::Op_FirstOperand, e = InOps.size(); 2043*0b57cec5SDimitry Andric if (InOps[e-1].getValueType() == MVT::Glue) 2044*0b57cec5SDimitry Andric --e; // Don't process a glue operand if it is here. 2045*0b57cec5SDimitry Andric 2046*0b57cec5SDimitry Andric while (i != e) { 2047*0b57cec5SDimitry Andric unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue(); 2048*0b57cec5SDimitry Andric if (!InlineAsm::isMemKind(Flags)) { 2049*0b57cec5SDimitry Andric // Just skip over this operand, copying the operands verbatim. 2050*0b57cec5SDimitry Andric Ops.insert(Ops.end(), InOps.begin()+i, 2051*0b57cec5SDimitry Andric InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1); 2052*0b57cec5SDimitry Andric i += InlineAsm::getNumOperandRegisters(Flags) + 1; 2053*0b57cec5SDimitry Andric } else { 2054*0b57cec5SDimitry Andric assert(InlineAsm::getNumOperandRegisters(Flags) == 1 && 2055*0b57cec5SDimitry Andric "Memory operand with multiple values?"); 2056*0b57cec5SDimitry Andric 2057*0b57cec5SDimitry Andric unsigned TiedToOperand; 2058*0b57cec5SDimitry Andric if (InlineAsm::isUseOperandTiedToDef(Flags, TiedToOperand)) { 2059*0b57cec5SDimitry Andric // We need the constraint ID from the operand this is tied to. 2060*0b57cec5SDimitry Andric unsigned CurOp = InlineAsm::Op_FirstOperand; 2061*0b57cec5SDimitry Andric Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue(); 2062*0b57cec5SDimitry Andric for (; TiedToOperand; --TiedToOperand) { 2063*0b57cec5SDimitry Andric CurOp += InlineAsm::getNumOperandRegisters(Flags)+1; 2064*0b57cec5SDimitry Andric Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue(); 2065*0b57cec5SDimitry Andric } 2066*0b57cec5SDimitry Andric } 2067*0b57cec5SDimitry Andric 2068*0b57cec5SDimitry Andric // Otherwise, this is a memory operand. Ask the target to select it. 2069*0b57cec5SDimitry Andric std::vector<SDValue> SelOps; 2070*0b57cec5SDimitry Andric unsigned ConstraintID = InlineAsm::getMemoryConstraintID(Flags); 2071*0b57cec5SDimitry Andric if (SelectInlineAsmMemoryOperand(InOps[i+1], ConstraintID, SelOps)) 2072*0b57cec5SDimitry Andric report_fatal_error("Could not match memory address. Inline asm" 2073*0b57cec5SDimitry Andric " failure!"); 2074*0b57cec5SDimitry Andric 2075*0b57cec5SDimitry Andric // Add this to the output node. 2076*0b57cec5SDimitry Andric unsigned NewFlags = 2077*0b57cec5SDimitry Andric InlineAsm::getFlagWord(InlineAsm::Kind_Mem, SelOps.size()); 2078*0b57cec5SDimitry Andric NewFlags = InlineAsm::getFlagWordForMem(NewFlags, ConstraintID); 2079*0b57cec5SDimitry Andric Ops.push_back(CurDAG->getTargetConstant(NewFlags, DL, MVT::i32)); 2080*0b57cec5SDimitry Andric Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); 2081*0b57cec5SDimitry Andric i += 2; 2082*0b57cec5SDimitry Andric } 2083*0b57cec5SDimitry Andric } 2084*0b57cec5SDimitry Andric 2085*0b57cec5SDimitry Andric // Add the glue input back if present. 2086*0b57cec5SDimitry Andric if (e != InOps.size()) 2087*0b57cec5SDimitry Andric Ops.push_back(InOps.back()); 2088*0b57cec5SDimitry Andric } 2089*0b57cec5SDimitry Andric 2090*0b57cec5SDimitry Andric /// findGlueUse - Return use of MVT::Glue value produced by the specified 2091*0b57cec5SDimitry Andric /// SDNode. 2092*0b57cec5SDimitry Andric /// 2093*0b57cec5SDimitry Andric static SDNode *findGlueUse(SDNode *N) { 2094*0b57cec5SDimitry Andric unsigned FlagResNo = N->getNumValues()-1; 2095*0b57cec5SDimitry Andric for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 2096*0b57cec5SDimitry Andric SDUse &Use = I.getUse(); 2097*0b57cec5SDimitry Andric if (Use.getResNo() == FlagResNo) 2098*0b57cec5SDimitry Andric return Use.getUser(); 2099*0b57cec5SDimitry Andric } 2100*0b57cec5SDimitry Andric return nullptr; 2101*0b57cec5SDimitry Andric } 2102*0b57cec5SDimitry Andric 2103*0b57cec5SDimitry Andric /// findNonImmUse - Return true if "Def" is a predecessor of "Root" via a path 2104*0b57cec5SDimitry Andric /// beyond "ImmedUse". We may ignore chains as they are checked separately. 2105*0b57cec5SDimitry Andric static bool findNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse, 2106*0b57cec5SDimitry Andric bool IgnoreChains) { 2107*0b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 16> Visited; 2108*0b57cec5SDimitry Andric SmallVector<const SDNode *, 16> WorkList; 2109*0b57cec5SDimitry Andric // Only check if we have non-immediate uses of Def. 2110*0b57cec5SDimitry Andric if (ImmedUse->isOnlyUserOf(Def)) 2111*0b57cec5SDimitry Andric return false; 2112*0b57cec5SDimitry Andric 2113*0b57cec5SDimitry Andric // We don't care about paths to Def that go through ImmedUse so mark it 2114*0b57cec5SDimitry Andric // visited and mark non-def operands as used. 2115*0b57cec5SDimitry Andric Visited.insert(ImmedUse); 2116*0b57cec5SDimitry Andric for (const SDValue &Op : ImmedUse->op_values()) { 2117*0b57cec5SDimitry Andric SDNode *N = Op.getNode(); 2118*0b57cec5SDimitry Andric // Ignore chain deps (they are validated by 2119*0b57cec5SDimitry Andric // HandleMergeInputChains) and immediate uses 2120*0b57cec5SDimitry Andric if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def) 2121*0b57cec5SDimitry Andric continue; 2122*0b57cec5SDimitry Andric if (!Visited.insert(N).second) 2123*0b57cec5SDimitry Andric continue; 2124*0b57cec5SDimitry Andric WorkList.push_back(N); 2125*0b57cec5SDimitry Andric } 2126*0b57cec5SDimitry Andric 2127*0b57cec5SDimitry Andric // Initialize worklist to operands of Root. 2128*0b57cec5SDimitry Andric if (Root != ImmedUse) { 2129*0b57cec5SDimitry Andric for (const SDValue &Op : Root->op_values()) { 2130*0b57cec5SDimitry Andric SDNode *N = Op.getNode(); 2131*0b57cec5SDimitry Andric // Ignore chains (they are validated by HandleMergeInputChains) 2132*0b57cec5SDimitry Andric if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def) 2133*0b57cec5SDimitry Andric continue; 2134*0b57cec5SDimitry Andric if (!Visited.insert(N).second) 2135*0b57cec5SDimitry Andric continue; 2136*0b57cec5SDimitry Andric WorkList.push_back(N); 2137*0b57cec5SDimitry Andric } 2138*0b57cec5SDimitry Andric } 2139*0b57cec5SDimitry Andric 2140*0b57cec5SDimitry Andric return SDNode::hasPredecessorHelper(Def, Visited, WorkList, 0, true); 2141*0b57cec5SDimitry Andric } 2142*0b57cec5SDimitry Andric 2143*0b57cec5SDimitry Andric /// IsProfitableToFold - Returns true if it's profitable to fold the specific 2144*0b57cec5SDimitry Andric /// operand node N of U during instruction selection that starts at Root. 2145*0b57cec5SDimitry Andric bool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U, 2146*0b57cec5SDimitry Andric SDNode *Root) const { 2147*0b57cec5SDimitry Andric if (OptLevel == CodeGenOpt::None) return false; 2148*0b57cec5SDimitry Andric return N.hasOneUse(); 2149*0b57cec5SDimitry Andric } 2150*0b57cec5SDimitry Andric 2151*0b57cec5SDimitry Andric /// IsLegalToFold - Returns true if the specific operand node N of 2152*0b57cec5SDimitry Andric /// U can be folded during instruction selection that starts at Root. 2153*0b57cec5SDimitry Andric bool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, 2154*0b57cec5SDimitry Andric CodeGenOpt::Level OptLevel, 2155*0b57cec5SDimitry Andric bool IgnoreChains) { 2156*0b57cec5SDimitry Andric if (OptLevel == CodeGenOpt::None) return false; 2157*0b57cec5SDimitry Andric 2158*0b57cec5SDimitry Andric // If Root use can somehow reach N through a path that that doesn't contain 2159*0b57cec5SDimitry Andric // U then folding N would create a cycle. e.g. In the following 2160*0b57cec5SDimitry Andric // diagram, Root can reach N through X. If N is folded into Root, then 2161*0b57cec5SDimitry Andric // X is both a predecessor and a successor of U. 2162*0b57cec5SDimitry Andric // 2163*0b57cec5SDimitry Andric // [N*] // 2164*0b57cec5SDimitry Andric // ^ ^ // 2165*0b57cec5SDimitry Andric // / \ // 2166*0b57cec5SDimitry Andric // [U*] [X]? // 2167*0b57cec5SDimitry Andric // ^ ^ // 2168*0b57cec5SDimitry Andric // \ / // 2169*0b57cec5SDimitry Andric // \ / // 2170*0b57cec5SDimitry Andric // [Root*] // 2171*0b57cec5SDimitry Andric // 2172*0b57cec5SDimitry Andric // * indicates nodes to be folded together. 2173*0b57cec5SDimitry Andric // 2174*0b57cec5SDimitry Andric // If Root produces glue, then it gets (even more) interesting. Since it 2175*0b57cec5SDimitry Andric // will be "glued" together with its glue use in the scheduler, we need to 2176*0b57cec5SDimitry Andric // check if it might reach N. 2177*0b57cec5SDimitry Andric // 2178*0b57cec5SDimitry Andric // [N*] // 2179*0b57cec5SDimitry Andric // ^ ^ // 2180*0b57cec5SDimitry Andric // / \ // 2181*0b57cec5SDimitry Andric // [U*] [X]? // 2182*0b57cec5SDimitry Andric // ^ ^ // 2183*0b57cec5SDimitry Andric // \ \ // 2184*0b57cec5SDimitry Andric // \ | // 2185*0b57cec5SDimitry Andric // [Root*] | // 2186*0b57cec5SDimitry Andric // ^ | // 2187*0b57cec5SDimitry Andric // f | // 2188*0b57cec5SDimitry Andric // | / // 2189*0b57cec5SDimitry Andric // [Y] / // 2190*0b57cec5SDimitry Andric // ^ / // 2191*0b57cec5SDimitry Andric // f / // 2192*0b57cec5SDimitry Andric // | / // 2193*0b57cec5SDimitry Andric // [GU] // 2194*0b57cec5SDimitry Andric // 2195*0b57cec5SDimitry Andric // If GU (glue use) indirectly reaches N (the load), and Root folds N 2196*0b57cec5SDimitry Andric // (call it Fold), then X is a predecessor of GU and a successor of 2197*0b57cec5SDimitry Andric // Fold. But since Fold and GU are glued together, this will create 2198*0b57cec5SDimitry Andric // a cycle in the scheduling graph. 2199*0b57cec5SDimitry Andric 2200*0b57cec5SDimitry Andric // If the node has glue, walk down the graph to the "lowest" node in the 2201*0b57cec5SDimitry Andric // glueged set. 2202*0b57cec5SDimitry Andric EVT VT = Root->getValueType(Root->getNumValues()-1); 2203*0b57cec5SDimitry Andric while (VT == MVT::Glue) { 2204*0b57cec5SDimitry Andric SDNode *GU = findGlueUse(Root); 2205*0b57cec5SDimitry Andric if (!GU) 2206*0b57cec5SDimitry Andric break; 2207*0b57cec5SDimitry Andric Root = GU; 2208*0b57cec5SDimitry Andric VT = Root->getValueType(Root->getNumValues()-1); 2209*0b57cec5SDimitry Andric 2210*0b57cec5SDimitry Andric // If our query node has a glue result with a use, we've walked up it. If 2211*0b57cec5SDimitry Andric // the user (which has already been selected) has a chain or indirectly uses 2212*0b57cec5SDimitry Andric // the chain, HandleMergeInputChains will not consider it. Because of 2213*0b57cec5SDimitry Andric // this, we cannot ignore chains in this predicate. 2214*0b57cec5SDimitry Andric IgnoreChains = false; 2215*0b57cec5SDimitry Andric } 2216*0b57cec5SDimitry Andric 2217*0b57cec5SDimitry Andric return !findNonImmUse(Root, N.getNode(), U, IgnoreChains); 2218*0b57cec5SDimitry Andric } 2219*0b57cec5SDimitry Andric 2220*0b57cec5SDimitry Andric void SelectionDAGISel::Select_INLINEASM(SDNode *N, bool Branch) { 2221*0b57cec5SDimitry Andric SDLoc DL(N); 2222*0b57cec5SDimitry Andric 2223*0b57cec5SDimitry Andric std::vector<SDValue> Ops(N->op_begin(), N->op_end()); 2224*0b57cec5SDimitry Andric SelectInlineAsmMemoryOperands(Ops, DL); 2225*0b57cec5SDimitry Andric 2226*0b57cec5SDimitry Andric const EVT VTs[] = {MVT::Other, MVT::Glue}; 2227*0b57cec5SDimitry Andric SDValue New = CurDAG->getNode(Branch ? ISD::INLINEASM_BR : ISD::INLINEASM, DL, VTs, Ops); 2228*0b57cec5SDimitry Andric New->setNodeId(-1); 2229*0b57cec5SDimitry Andric ReplaceUses(N, New.getNode()); 2230*0b57cec5SDimitry Andric CurDAG->RemoveDeadNode(N); 2231*0b57cec5SDimitry Andric } 2232*0b57cec5SDimitry Andric 2233*0b57cec5SDimitry Andric void SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) { 2234*0b57cec5SDimitry Andric SDLoc dl(Op); 2235*0b57cec5SDimitry Andric MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(Op->getOperand(1)); 2236*0b57cec5SDimitry Andric const MDString *RegStr = dyn_cast<MDString>(MD->getMD()->getOperand(0)); 2237*0b57cec5SDimitry Andric unsigned Reg = 2238*0b57cec5SDimitry Andric TLI->getRegisterByName(RegStr->getString().data(), Op->getValueType(0), 2239*0b57cec5SDimitry Andric *CurDAG); 2240*0b57cec5SDimitry Andric SDValue New = CurDAG->getCopyFromReg( 2241*0b57cec5SDimitry Andric Op->getOperand(0), dl, Reg, Op->getValueType(0)); 2242*0b57cec5SDimitry Andric New->setNodeId(-1); 2243*0b57cec5SDimitry Andric ReplaceUses(Op, New.getNode()); 2244*0b57cec5SDimitry Andric CurDAG->RemoveDeadNode(Op); 2245*0b57cec5SDimitry Andric } 2246*0b57cec5SDimitry Andric 2247*0b57cec5SDimitry Andric void SelectionDAGISel::Select_WRITE_REGISTER(SDNode *Op) { 2248*0b57cec5SDimitry Andric SDLoc dl(Op); 2249*0b57cec5SDimitry Andric MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(Op->getOperand(1)); 2250*0b57cec5SDimitry Andric const MDString *RegStr = dyn_cast<MDString>(MD->getMD()->getOperand(0)); 2251*0b57cec5SDimitry Andric unsigned Reg = TLI->getRegisterByName(RegStr->getString().data(), 2252*0b57cec5SDimitry Andric Op->getOperand(2).getValueType(), 2253*0b57cec5SDimitry Andric *CurDAG); 2254*0b57cec5SDimitry Andric SDValue New = CurDAG->getCopyToReg( 2255*0b57cec5SDimitry Andric Op->getOperand(0), dl, Reg, Op->getOperand(2)); 2256*0b57cec5SDimitry Andric New->setNodeId(-1); 2257*0b57cec5SDimitry Andric ReplaceUses(Op, New.getNode()); 2258*0b57cec5SDimitry Andric CurDAG->RemoveDeadNode(Op); 2259*0b57cec5SDimitry Andric } 2260*0b57cec5SDimitry Andric 2261*0b57cec5SDimitry Andric void SelectionDAGISel::Select_UNDEF(SDNode *N) { 2262*0b57cec5SDimitry Andric CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0)); 2263*0b57cec5SDimitry Andric } 2264*0b57cec5SDimitry Andric 2265*0b57cec5SDimitry Andric /// GetVBR - decode a vbr encoding whose top bit is set. 2266*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline uint64_t 2267*0b57cec5SDimitry Andric GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) { 2268*0b57cec5SDimitry Andric assert(Val >= 128 && "Not a VBR"); 2269*0b57cec5SDimitry Andric Val &= 127; // Remove first vbr bit. 2270*0b57cec5SDimitry Andric 2271*0b57cec5SDimitry Andric unsigned Shift = 7; 2272*0b57cec5SDimitry Andric uint64_t NextBits; 2273*0b57cec5SDimitry Andric do { 2274*0b57cec5SDimitry Andric NextBits = MatcherTable[Idx++]; 2275*0b57cec5SDimitry Andric Val |= (NextBits&127) << Shift; 2276*0b57cec5SDimitry Andric Shift += 7; 2277*0b57cec5SDimitry Andric } while (NextBits & 128); 2278*0b57cec5SDimitry Andric 2279*0b57cec5SDimitry Andric return Val; 2280*0b57cec5SDimitry Andric } 2281*0b57cec5SDimitry Andric 2282*0b57cec5SDimitry Andric /// When a match is complete, this method updates uses of interior chain results 2283*0b57cec5SDimitry Andric /// to use the new results. 2284*0b57cec5SDimitry Andric void SelectionDAGISel::UpdateChains( 2285*0b57cec5SDimitry Andric SDNode *NodeToMatch, SDValue InputChain, 2286*0b57cec5SDimitry Andric SmallVectorImpl<SDNode *> &ChainNodesMatched, bool isMorphNodeTo) { 2287*0b57cec5SDimitry Andric SmallVector<SDNode*, 4> NowDeadNodes; 2288*0b57cec5SDimitry Andric 2289*0b57cec5SDimitry Andric // Now that all the normal results are replaced, we replace the chain and 2290*0b57cec5SDimitry Andric // glue results if present. 2291*0b57cec5SDimitry Andric if (!ChainNodesMatched.empty()) { 2292*0b57cec5SDimitry Andric assert(InputChain.getNode() && 2293*0b57cec5SDimitry Andric "Matched input chains but didn't produce a chain"); 2294*0b57cec5SDimitry Andric // Loop over all of the nodes we matched that produced a chain result. 2295*0b57cec5SDimitry Andric // Replace all the chain results with the final chain we ended up with. 2296*0b57cec5SDimitry Andric for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) { 2297*0b57cec5SDimitry Andric SDNode *ChainNode = ChainNodesMatched[i]; 2298*0b57cec5SDimitry Andric // If ChainNode is null, it's because we replaced it on a previous 2299*0b57cec5SDimitry Andric // iteration and we cleared it out of the map. Just skip it. 2300*0b57cec5SDimitry Andric if (!ChainNode) 2301*0b57cec5SDimitry Andric continue; 2302*0b57cec5SDimitry Andric 2303*0b57cec5SDimitry Andric assert(ChainNode->getOpcode() != ISD::DELETED_NODE && 2304*0b57cec5SDimitry Andric "Deleted node left in chain"); 2305*0b57cec5SDimitry Andric 2306*0b57cec5SDimitry Andric // Don't replace the results of the root node if we're doing a 2307*0b57cec5SDimitry Andric // MorphNodeTo. 2308*0b57cec5SDimitry Andric if (ChainNode == NodeToMatch && isMorphNodeTo) 2309*0b57cec5SDimitry Andric continue; 2310*0b57cec5SDimitry Andric 2311*0b57cec5SDimitry Andric SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1); 2312*0b57cec5SDimitry Andric if (ChainVal.getValueType() == MVT::Glue) 2313*0b57cec5SDimitry Andric ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2); 2314*0b57cec5SDimitry Andric assert(ChainVal.getValueType() == MVT::Other && "Not a chain?"); 2315*0b57cec5SDimitry Andric SelectionDAG::DAGNodeDeletedListener NDL( 2316*0b57cec5SDimitry Andric *CurDAG, [&](SDNode *N, SDNode *E) { 2317*0b57cec5SDimitry Andric std::replace(ChainNodesMatched.begin(), ChainNodesMatched.end(), N, 2318*0b57cec5SDimitry Andric static_cast<SDNode *>(nullptr)); 2319*0b57cec5SDimitry Andric }); 2320*0b57cec5SDimitry Andric if (ChainNode->getOpcode() != ISD::TokenFactor) 2321*0b57cec5SDimitry Andric ReplaceUses(ChainVal, InputChain); 2322*0b57cec5SDimitry Andric 2323*0b57cec5SDimitry Andric // If the node became dead and we haven't already seen it, delete it. 2324*0b57cec5SDimitry Andric if (ChainNode != NodeToMatch && ChainNode->use_empty() && 2325*0b57cec5SDimitry Andric !std::count(NowDeadNodes.begin(), NowDeadNodes.end(), ChainNode)) 2326*0b57cec5SDimitry Andric NowDeadNodes.push_back(ChainNode); 2327*0b57cec5SDimitry Andric } 2328*0b57cec5SDimitry Andric } 2329*0b57cec5SDimitry Andric 2330*0b57cec5SDimitry Andric if (!NowDeadNodes.empty()) 2331*0b57cec5SDimitry Andric CurDAG->RemoveDeadNodes(NowDeadNodes); 2332*0b57cec5SDimitry Andric 2333*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "ISEL: Match complete!\n"); 2334*0b57cec5SDimitry Andric } 2335*0b57cec5SDimitry Andric 2336*0b57cec5SDimitry Andric /// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains 2337*0b57cec5SDimitry Andric /// operation for when the pattern matched at least one node with a chains. The 2338*0b57cec5SDimitry Andric /// input vector contains a list of all of the chained nodes that we match. We 2339*0b57cec5SDimitry Andric /// must determine if this is a valid thing to cover (i.e. matching it won't 2340*0b57cec5SDimitry Andric /// induce cycles in the DAG) and if so, creating a TokenFactor node. that will 2341*0b57cec5SDimitry Andric /// be used as the input node chain for the generated nodes. 2342*0b57cec5SDimitry Andric static SDValue 2343*0b57cec5SDimitry Andric HandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched, 2344*0b57cec5SDimitry Andric SelectionDAG *CurDAG) { 2345*0b57cec5SDimitry Andric 2346*0b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 16> Visited; 2347*0b57cec5SDimitry Andric SmallVector<const SDNode *, 8> Worklist; 2348*0b57cec5SDimitry Andric SmallVector<SDValue, 3> InputChains; 2349*0b57cec5SDimitry Andric unsigned int Max = 8192; 2350*0b57cec5SDimitry Andric 2351*0b57cec5SDimitry Andric // Quick exit on trivial merge. 2352*0b57cec5SDimitry Andric if (ChainNodesMatched.size() == 1) 2353*0b57cec5SDimitry Andric return ChainNodesMatched[0]->getOperand(0); 2354*0b57cec5SDimitry Andric 2355*0b57cec5SDimitry Andric // Add chains that aren't already added (internal). Peek through 2356*0b57cec5SDimitry Andric // token factors. 2357*0b57cec5SDimitry Andric std::function<void(const SDValue)> AddChains = [&](const SDValue V) { 2358*0b57cec5SDimitry Andric if (V.getValueType() != MVT::Other) 2359*0b57cec5SDimitry Andric return; 2360*0b57cec5SDimitry Andric if (V->getOpcode() == ISD::EntryToken) 2361*0b57cec5SDimitry Andric return; 2362*0b57cec5SDimitry Andric if (!Visited.insert(V.getNode()).second) 2363*0b57cec5SDimitry Andric return; 2364*0b57cec5SDimitry Andric if (V->getOpcode() == ISD::TokenFactor) { 2365*0b57cec5SDimitry Andric for (const SDValue &Op : V->op_values()) 2366*0b57cec5SDimitry Andric AddChains(Op); 2367*0b57cec5SDimitry Andric } else 2368*0b57cec5SDimitry Andric InputChains.push_back(V); 2369*0b57cec5SDimitry Andric }; 2370*0b57cec5SDimitry Andric 2371*0b57cec5SDimitry Andric for (auto *N : ChainNodesMatched) { 2372*0b57cec5SDimitry Andric Worklist.push_back(N); 2373*0b57cec5SDimitry Andric Visited.insert(N); 2374*0b57cec5SDimitry Andric } 2375*0b57cec5SDimitry Andric 2376*0b57cec5SDimitry Andric while (!Worklist.empty()) 2377*0b57cec5SDimitry Andric AddChains(Worklist.pop_back_val()->getOperand(0)); 2378*0b57cec5SDimitry Andric 2379*0b57cec5SDimitry Andric // Skip the search if there are no chain dependencies. 2380*0b57cec5SDimitry Andric if (InputChains.size() == 0) 2381*0b57cec5SDimitry Andric return CurDAG->getEntryNode(); 2382*0b57cec5SDimitry Andric 2383*0b57cec5SDimitry Andric // If one of these chains is a successor of input, we must have a 2384*0b57cec5SDimitry Andric // node that is both the predecessor and successor of the 2385*0b57cec5SDimitry Andric // to-be-merged nodes. Fail. 2386*0b57cec5SDimitry Andric Visited.clear(); 2387*0b57cec5SDimitry Andric for (SDValue V : InputChains) 2388*0b57cec5SDimitry Andric Worklist.push_back(V.getNode()); 2389*0b57cec5SDimitry Andric 2390*0b57cec5SDimitry Andric for (auto *N : ChainNodesMatched) 2391*0b57cec5SDimitry Andric if (SDNode::hasPredecessorHelper(N, Visited, Worklist, Max, true)) 2392*0b57cec5SDimitry Andric return SDValue(); 2393*0b57cec5SDimitry Andric 2394*0b57cec5SDimitry Andric // Return merged chain. 2395*0b57cec5SDimitry Andric if (InputChains.size() == 1) 2396*0b57cec5SDimitry Andric return InputChains[0]; 2397*0b57cec5SDimitry Andric return CurDAG->getNode(ISD::TokenFactor, SDLoc(ChainNodesMatched[0]), 2398*0b57cec5SDimitry Andric MVT::Other, InputChains); 2399*0b57cec5SDimitry Andric } 2400*0b57cec5SDimitry Andric 2401*0b57cec5SDimitry Andric /// MorphNode - Handle morphing a node in place for the selector. 2402*0b57cec5SDimitry Andric SDNode *SelectionDAGISel:: 2403*0b57cec5SDimitry Andric MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList, 2404*0b57cec5SDimitry Andric ArrayRef<SDValue> Ops, unsigned EmitNodeInfo) { 2405*0b57cec5SDimitry Andric // It is possible we're using MorphNodeTo to replace a node with no 2406*0b57cec5SDimitry Andric // normal results with one that has a normal result (or we could be 2407*0b57cec5SDimitry Andric // adding a chain) and the input could have glue and chains as well. 2408*0b57cec5SDimitry Andric // In this case we need to shift the operands down. 2409*0b57cec5SDimitry Andric // FIXME: This is a horrible hack and broken in obscure cases, no worse 2410*0b57cec5SDimitry Andric // than the old isel though. 2411*0b57cec5SDimitry Andric int OldGlueResultNo = -1, OldChainResultNo = -1; 2412*0b57cec5SDimitry Andric 2413*0b57cec5SDimitry Andric unsigned NTMNumResults = Node->getNumValues(); 2414*0b57cec5SDimitry Andric if (Node->getValueType(NTMNumResults-1) == MVT::Glue) { 2415*0b57cec5SDimitry Andric OldGlueResultNo = NTMNumResults-1; 2416*0b57cec5SDimitry Andric if (NTMNumResults != 1 && 2417*0b57cec5SDimitry Andric Node->getValueType(NTMNumResults-2) == MVT::Other) 2418*0b57cec5SDimitry Andric OldChainResultNo = NTMNumResults-2; 2419*0b57cec5SDimitry Andric } else if (Node->getValueType(NTMNumResults-1) == MVT::Other) 2420*0b57cec5SDimitry Andric OldChainResultNo = NTMNumResults-1; 2421*0b57cec5SDimitry Andric 2422*0b57cec5SDimitry Andric // Call the underlying SelectionDAG routine to do the transmogrification. Note 2423*0b57cec5SDimitry Andric // that this deletes operands of the old node that become dead. 2424*0b57cec5SDimitry Andric SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops); 2425*0b57cec5SDimitry Andric 2426*0b57cec5SDimitry Andric // MorphNodeTo can operate in two ways: if an existing node with the 2427*0b57cec5SDimitry Andric // specified operands exists, it can just return it. Otherwise, it 2428*0b57cec5SDimitry Andric // updates the node in place to have the requested operands. 2429*0b57cec5SDimitry Andric if (Res == Node) { 2430*0b57cec5SDimitry Andric // If we updated the node in place, reset the node ID. To the isel, 2431*0b57cec5SDimitry Andric // this should be just like a newly allocated machine node. 2432*0b57cec5SDimitry Andric Res->setNodeId(-1); 2433*0b57cec5SDimitry Andric } 2434*0b57cec5SDimitry Andric 2435*0b57cec5SDimitry Andric unsigned ResNumResults = Res->getNumValues(); 2436*0b57cec5SDimitry Andric // Move the glue if needed. 2437*0b57cec5SDimitry Andric if ((EmitNodeInfo & OPFL_GlueOutput) && OldGlueResultNo != -1 && 2438*0b57cec5SDimitry Andric (unsigned)OldGlueResultNo != ResNumResults-1) 2439*0b57cec5SDimitry Andric ReplaceUses(SDValue(Node, OldGlueResultNo), 2440*0b57cec5SDimitry Andric SDValue(Res, ResNumResults - 1)); 2441*0b57cec5SDimitry Andric 2442*0b57cec5SDimitry Andric if ((EmitNodeInfo & OPFL_GlueOutput) != 0) 2443*0b57cec5SDimitry Andric --ResNumResults; 2444*0b57cec5SDimitry Andric 2445*0b57cec5SDimitry Andric // Move the chain reference if needed. 2446*0b57cec5SDimitry Andric if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 && 2447*0b57cec5SDimitry Andric (unsigned)OldChainResultNo != ResNumResults-1) 2448*0b57cec5SDimitry Andric ReplaceUses(SDValue(Node, OldChainResultNo), 2449*0b57cec5SDimitry Andric SDValue(Res, ResNumResults - 1)); 2450*0b57cec5SDimitry Andric 2451*0b57cec5SDimitry Andric // Otherwise, no replacement happened because the node already exists. Replace 2452*0b57cec5SDimitry Andric // Uses of the old node with the new one. 2453*0b57cec5SDimitry Andric if (Res != Node) { 2454*0b57cec5SDimitry Andric ReplaceNode(Node, Res); 2455*0b57cec5SDimitry Andric } else { 2456*0b57cec5SDimitry Andric EnforceNodeIdInvariant(Res); 2457*0b57cec5SDimitry Andric } 2458*0b57cec5SDimitry Andric 2459*0b57cec5SDimitry Andric return Res; 2460*0b57cec5SDimitry Andric } 2461*0b57cec5SDimitry Andric 2462*0b57cec5SDimitry Andric /// CheckSame - Implements OP_CheckSame. 2463*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2464*0b57cec5SDimitry Andric CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2465*0b57cec5SDimitry Andric SDValue N, 2466*0b57cec5SDimitry Andric const SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes) { 2467*0b57cec5SDimitry Andric // Accept if it is exactly the same as a previously recorded node. 2468*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 2469*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid CheckSame"); 2470*0b57cec5SDimitry Andric return N == RecordedNodes[RecNo].first; 2471*0b57cec5SDimitry Andric } 2472*0b57cec5SDimitry Andric 2473*0b57cec5SDimitry Andric /// CheckChildSame - Implements OP_CheckChildXSame. 2474*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2475*0b57cec5SDimitry Andric CheckChildSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2476*0b57cec5SDimitry Andric SDValue N, 2477*0b57cec5SDimitry Andric const SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes, 2478*0b57cec5SDimitry Andric unsigned ChildNo) { 2479*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2480*0b57cec5SDimitry Andric return false; // Match fails if out of range child #. 2481*0b57cec5SDimitry Andric return ::CheckSame(MatcherTable, MatcherIndex, N.getOperand(ChildNo), 2482*0b57cec5SDimitry Andric RecordedNodes); 2483*0b57cec5SDimitry Andric } 2484*0b57cec5SDimitry Andric 2485*0b57cec5SDimitry Andric /// CheckPatternPredicate - Implements OP_CheckPatternPredicate. 2486*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2487*0b57cec5SDimitry Andric CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2488*0b57cec5SDimitry Andric const SelectionDAGISel &SDISel) { 2489*0b57cec5SDimitry Andric return SDISel.CheckPatternPredicate(MatcherTable[MatcherIndex++]); 2490*0b57cec5SDimitry Andric } 2491*0b57cec5SDimitry Andric 2492*0b57cec5SDimitry Andric /// CheckNodePredicate - Implements OP_CheckNodePredicate. 2493*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2494*0b57cec5SDimitry Andric CheckNodePredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2495*0b57cec5SDimitry Andric const SelectionDAGISel &SDISel, SDNode *N) { 2496*0b57cec5SDimitry Andric return SDISel.CheckNodePredicate(N, MatcherTable[MatcherIndex++]); 2497*0b57cec5SDimitry Andric } 2498*0b57cec5SDimitry Andric 2499*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2500*0b57cec5SDimitry Andric CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2501*0b57cec5SDimitry Andric SDNode *N) { 2502*0b57cec5SDimitry Andric uint16_t Opc = MatcherTable[MatcherIndex++]; 2503*0b57cec5SDimitry Andric Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 2504*0b57cec5SDimitry Andric return N->getOpcode() == Opc; 2505*0b57cec5SDimitry Andric } 2506*0b57cec5SDimitry Andric 2507*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2508*0b57cec5SDimitry Andric CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, 2509*0b57cec5SDimitry Andric const TargetLowering *TLI, const DataLayout &DL) { 2510*0b57cec5SDimitry Andric MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 2511*0b57cec5SDimitry Andric if (N.getValueType() == VT) return true; 2512*0b57cec5SDimitry Andric 2513*0b57cec5SDimitry Andric // Handle the case when VT is iPTR. 2514*0b57cec5SDimitry Andric return VT == MVT::iPTR && N.getValueType() == TLI->getPointerTy(DL); 2515*0b57cec5SDimitry Andric } 2516*0b57cec5SDimitry Andric 2517*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2518*0b57cec5SDimitry Andric CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2519*0b57cec5SDimitry Andric SDValue N, const TargetLowering *TLI, const DataLayout &DL, 2520*0b57cec5SDimitry Andric unsigned ChildNo) { 2521*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2522*0b57cec5SDimitry Andric return false; // Match fails if out of range child #. 2523*0b57cec5SDimitry Andric return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI, 2524*0b57cec5SDimitry Andric DL); 2525*0b57cec5SDimitry Andric } 2526*0b57cec5SDimitry Andric 2527*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2528*0b57cec5SDimitry Andric CheckCondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2529*0b57cec5SDimitry Andric SDValue N) { 2530*0b57cec5SDimitry Andric return cast<CondCodeSDNode>(N)->get() == 2531*0b57cec5SDimitry Andric (ISD::CondCode)MatcherTable[MatcherIndex++]; 2532*0b57cec5SDimitry Andric } 2533*0b57cec5SDimitry Andric 2534*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2535*0b57cec5SDimitry Andric CheckChild2CondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2536*0b57cec5SDimitry Andric SDValue N) { 2537*0b57cec5SDimitry Andric if (2 >= N.getNumOperands()) 2538*0b57cec5SDimitry Andric return false; 2539*0b57cec5SDimitry Andric return ::CheckCondCode(MatcherTable, MatcherIndex, N.getOperand(2)); 2540*0b57cec5SDimitry Andric } 2541*0b57cec5SDimitry Andric 2542*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2543*0b57cec5SDimitry Andric CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2544*0b57cec5SDimitry Andric SDValue N, const TargetLowering *TLI, const DataLayout &DL) { 2545*0b57cec5SDimitry Andric MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 2546*0b57cec5SDimitry Andric if (cast<VTSDNode>(N)->getVT() == VT) 2547*0b57cec5SDimitry Andric return true; 2548*0b57cec5SDimitry Andric 2549*0b57cec5SDimitry Andric // Handle the case when VT is iPTR. 2550*0b57cec5SDimitry Andric return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI->getPointerTy(DL); 2551*0b57cec5SDimitry Andric } 2552*0b57cec5SDimitry Andric 2553*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2554*0b57cec5SDimitry Andric CheckInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2555*0b57cec5SDimitry Andric SDValue N) { 2556*0b57cec5SDimitry Andric int64_t Val = MatcherTable[MatcherIndex++]; 2557*0b57cec5SDimitry Andric if (Val & 128) 2558*0b57cec5SDimitry Andric Val = GetVBR(Val, MatcherTable, MatcherIndex); 2559*0b57cec5SDimitry Andric 2560*0b57cec5SDimitry Andric ConstantSDNode *C = dyn_cast<ConstantSDNode>(N); 2561*0b57cec5SDimitry Andric return C && C->getSExtValue() == Val; 2562*0b57cec5SDimitry Andric } 2563*0b57cec5SDimitry Andric 2564*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2565*0b57cec5SDimitry Andric CheckChildInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2566*0b57cec5SDimitry Andric SDValue N, unsigned ChildNo) { 2567*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2568*0b57cec5SDimitry Andric return false; // Match fails if out of range child #. 2569*0b57cec5SDimitry Andric return ::CheckInteger(MatcherTable, MatcherIndex, N.getOperand(ChildNo)); 2570*0b57cec5SDimitry Andric } 2571*0b57cec5SDimitry Andric 2572*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2573*0b57cec5SDimitry Andric CheckAndImm(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2574*0b57cec5SDimitry Andric SDValue N, const SelectionDAGISel &SDISel) { 2575*0b57cec5SDimitry Andric int64_t Val = MatcherTable[MatcherIndex++]; 2576*0b57cec5SDimitry Andric if (Val & 128) 2577*0b57cec5SDimitry Andric Val = GetVBR(Val, MatcherTable, MatcherIndex); 2578*0b57cec5SDimitry Andric 2579*0b57cec5SDimitry Andric if (N->getOpcode() != ISD::AND) return false; 2580*0b57cec5SDimitry Andric 2581*0b57cec5SDimitry Andric ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2582*0b57cec5SDimitry Andric return C && SDISel.CheckAndMask(N.getOperand(0), C, Val); 2583*0b57cec5SDimitry Andric } 2584*0b57cec5SDimitry Andric 2585*0b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE static inline bool 2586*0b57cec5SDimitry Andric CheckOrImm(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2587*0b57cec5SDimitry Andric SDValue N, const SelectionDAGISel &SDISel) { 2588*0b57cec5SDimitry Andric int64_t Val = MatcherTable[MatcherIndex++]; 2589*0b57cec5SDimitry Andric if (Val & 128) 2590*0b57cec5SDimitry Andric Val = GetVBR(Val, MatcherTable, MatcherIndex); 2591*0b57cec5SDimitry Andric 2592*0b57cec5SDimitry Andric if (N->getOpcode() != ISD::OR) return false; 2593*0b57cec5SDimitry Andric 2594*0b57cec5SDimitry Andric ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2595*0b57cec5SDimitry Andric return C && SDISel.CheckOrMask(N.getOperand(0), C, Val); 2596*0b57cec5SDimitry Andric } 2597*0b57cec5SDimitry Andric 2598*0b57cec5SDimitry Andric /// IsPredicateKnownToFail - If we know how and can do so without pushing a 2599*0b57cec5SDimitry Andric /// scope, evaluate the current node. If the current predicate is known to 2600*0b57cec5SDimitry Andric /// fail, set Result=true and return anything. If the current predicate is 2601*0b57cec5SDimitry Andric /// known to pass, set Result=false and return the MatcherIndex to continue 2602*0b57cec5SDimitry Andric /// with. If the current predicate is unknown, set Result=false and return the 2603*0b57cec5SDimitry Andric /// MatcherIndex to continue with. 2604*0b57cec5SDimitry Andric static unsigned IsPredicateKnownToFail(const unsigned char *Table, 2605*0b57cec5SDimitry Andric unsigned Index, SDValue N, 2606*0b57cec5SDimitry Andric bool &Result, 2607*0b57cec5SDimitry Andric const SelectionDAGISel &SDISel, 2608*0b57cec5SDimitry Andric SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes) { 2609*0b57cec5SDimitry Andric switch (Table[Index++]) { 2610*0b57cec5SDimitry Andric default: 2611*0b57cec5SDimitry Andric Result = false; 2612*0b57cec5SDimitry Andric return Index-1; // Could not evaluate this predicate. 2613*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckSame: 2614*0b57cec5SDimitry Andric Result = !::CheckSame(Table, Index, N, RecordedNodes); 2615*0b57cec5SDimitry Andric return Index; 2616*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild0Same: 2617*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild1Same: 2618*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild2Same: 2619*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild3Same: 2620*0b57cec5SDimitry Andric Result = !::CheckChildSame(Table, Index, N, RecordedNodes, 2621*0b57cec5SDimitry Andric Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same); 2622*0b57cec5SDimitry Andric return Index; 2623*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckPatternPredicate: 2624*0b57cec5SDimitry Andric Result = !::CheckPatternPredicate(Table, Index, SDISel); 2625*0b57cec5SDimitry Andric return Index; 2626*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckPredicate: 2627*0b57cec5SDimitry Andric Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode()); 2628*0b57cec5SDimitry Andric return Index; 2629*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckOpcode: 2630*0b57cec5SDimitry Andric Result = !::CheckOpcode(Table, Index, N.getNode()); 2631*0b57cec5SDimitry Andric return Index; 2632*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckType: 2633*0b57cec5SDimitry Andric Result = !::CheckType(Table, Index, N, SDISel.TLI, 2634*0b57cec5SDimitry Andric SDISel.CurDAG->getDataLayout()); 2635*0b57cec5SDimitry Andric return Index; 2636*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckTypeRes: { 2637*0b57cec5SDimitry Andric unsigned Res = Table[Index++]; 2638*0b57cec5SDimitry Andric Result = !::CheckType(Table, Index, N.getValue(Res), SDISel.TLI, 2639*0b57cec5SDimitry Andric SDISel.CurDAG->getDataLayout()); 2640*0b57cec5SDimitry Andric return Index; 2641*0b57cec5SDimitry Andric } 2642*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild0Type: 2643*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild1Type: 2644*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild2Type: 2645*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild3Type: 2646*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild4Type: 2647*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild5Type: 2648*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild6Type: 2649*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild7Type: 2650*0b57cec5SDimitry Andric Result = !::CheckChildType( 2651*0b57cec5SDimitry Andric Table, Index, N, SDISel.TLI, SDISel.CurDAG->getDataLayout(), 2652*0b57cec5SDimitry Andric Table[Index - 1] - SelectionDAGISel::OPC_CheckChild0Type); 2653*0b57cec5SDimitry Andric return Index; 2654*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckCondCode: 2655*0b57cec5SDimitry Andric Result = !::CheckCondCode(Table, Index, N); 2656*0b57cec5SDimitry Andric return Index; 2657*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild2CondCode: 2658*0b57cec5SDimitry Andric Result = !::CheckChild2CondCode(Table, Index, N); 2659*0b57cec5SDimitry Andric return Index; 2660*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckValueType: 2661*0b57cec5SDimitry Andric Result = !::CheckValueType(Table, Index, N, SDISel.TLI, 2662*0b57cec5SDimitry Andric SDISel.CurDAG->getDataLayout()); 2663*0b57cec5SDimitry Andric return Index; 2664*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckInteger: 2665*0b57cec5SDimitry Andric Result = !::CheckInteger(Table, Index, N); 2666*0b57cec5SDimitry Andric return Index; 2667*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild0Integer: 2668*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild1Integer: 2669*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild2Integer: 2670*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild3Integer: 2671*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckChild4Integer: 2672*0b57cec5SDimitry Andric Result = !::CheckChildInteger(Table, Index, N, 2673*0b57cec5SDimitry Andric Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Integer); 2674*0b57cec5SDimitry Andric return Index; 2675*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckAndImm: 2676*0b57cec5SDimitry Andric Result = !::CheckAndImm(Table, Index, N, SDISel); 2677*0b57cec5SDimitry Andric return Index; 2678*0b57cec5SDimitry Andric case SelectionDAGISel::OPC_CheckOrImm: 2679*0b57cec5SDimitry Andric Result = !::CheckOrImm(Table, Index, N, SDISel); 2680*0b57cec5SDimitry Andric return Index; 2681*0b57cec5SDimitry Andric } 2682*0b57cec5SDimitry Andric } 2683*0b57cec5SDimitry Andric 2684*0b57cec5SDimitry Andric namespace { 2685*0b57cec5SDimitry Andric 2686*0b57cec5SDimitry Andric struct MatchScope { 2687*0b57cec5SDimitry Andric /// FailIndex - If this match fails, this is the index to continue with. 2688*0b57cec5SDimitry Andric unsigned FailIndex; 2689*0b57cec5SDimitry Andric 2690*0b57cec5SDimitry Andric /// NodeStack - The node stack when the scope was formed. 2691*0b57cec5SDimitry Andric SmallVector<SDValue, 4> NodeStack; 2692*0b57cec5SDimitry Andric 2693*0b57cec5SDimitry Andric /// NumRecordedNodes - The number of recorded nodes when the scope was formed. 2694*0b57cec5SDimitry Andric unsigned NumRecordedNodes; 2695*0b57cec5SDimitry Andric 2696*0b57cec5SDimitry Andric /// NumMatchedMemRefs - The number of matched memref entries. 2697*0b57cec5SDimitry Andric unsigned NumMatchedMemRefs; 2698*0b57cec5SDimitry Andric 2699*0b57cec5SDimitry Andric /// InputChain/InputGlue - The current chain/glue 2700*0b57cec5SDimitry Andric SDValue InputChain, InputGlue; 2701*0b57cec5SDimitry Andric 2702*0b57cec5SDimitry Andric /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty. 2703*0b57cec5SDimitry Andric bool HasChainNodesMatched; 2704*0b57cec5SDimitry Andric }; 2705*0b57cec5SDimitry Andric 2706*0b57cec5SDimitry Andric /// \A DAG update listener to keep the matching state 2707*0b57cec5SDimitry Andric /// (i.e. RecordedNodes and MatchScope) uptodate if the target is allowed to 2708*0b57cec5SDimitry Andric /// change the DAG while matching. X86 addressing mode matcher is an example 2709*0b57cec5SDimitry Andric /// for this. 2710*0b57cec5SDimitry Andric class MatchStateUpdater : public SelectionDAG::DAGUpdateListener 2711*0b57cec5SDimitry Andric { 2712*0b57cec5SDimitry Andric SDNode **NodeToMatch; 2713*0b57cec5SDimitry Andric SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes; 2714*0b57cec5SDimitry Andric SmallVectorImpl<MatchScope> &MatchScopes; 2715*0b57cec5SDimitry Andric 2716*0b57cec5SDimitry Andric public: 2717*0b57cec5SDimitry Andric MatchStateUpdater(SelectionDAG &DAG, SDNode **NodeToMatch, 2718*0b57cec5SDimitry Andric SmallVectorImpl<std::pair<SDValue, SDNode *>> &RN, 2719*0b57cec5SDimitry Andric SmallVectorImpl<MatchScope> &MS) 2720*0b57cec5SDimitry Andric : SelectionDAG::DAGUpdateListener(DAG), NodeToMatch(NodeToMatch), 2721*0b57cec5SDimitry Andric RecordedNodes(RN), MatchScopes(MS) {} 2722*0b57cec5SDimitry Andric 2723*0b57cec5SDimitry Andric void NodeDeleted(SDNode *N, SDNode *E) override { 2724*0b57cec5SDimitry Andric // Some early-returns here to avoid the search if we deleted the node or 2725*0b57cec5SDimitry Andric // if the update comes from MorphNodeTo (MorphNodeTo is the last thing we 2726*0b57cec5SDimitry Andric // do, so it's unnecessary to update matching state at that point). 2727*0b57cec5SDimitry Andric // Neither of these can occur currently because we only install this 2728*0b57cec5SDimitry Andric // update listener during matching a complex patterns. 2729*0b57cec5SDimitry Andric if (!E || E->isMachineOpcode()) 2730*0b57cec5SDimitry Andric return; 2731*0b57cec5SDimitry Andric // Check if NodeToMatch was updated. 2732*0b57cec5SDimitry Andric if (N == *NodeToMatch) 2733*0b57cec5SDimitry Andric *NodeToMatch = E; 2734*0b57cec5SDimitry Andric // Performing linear search here does not matter because we almost never 2735*0b57cec5SDimitry Andric // run this code. You'd have to have a CSE during complex pattern 2736*0b57cec5SDimitry Andric // matching. 2737*0b57cec5SDimitry Andric for (auto &I : RecordedNodes) 2738*0b57cec5SDimitry Andric if (I.first.getNode() == N) 2739*0b57cec5SDimitry Andric I.first.setNode(E); 2740*0b57cec5SDimitry Andric 2741*0b57cec5SDimitry Andric for (auto &I : MatchScopes) 2742*0b57cec5SDimitry Andric for (auto &J : I.NodeStack) 2743*0b57cec5SDimitry Andric if (J.getNode() == N) 2744*0b57cec5SDimitry Andric J.setNode(E); 2745*0b57cec5SDimitry Andric } 2746*0b57cec5SDimitry Andric }; 2747*0b57cec5SDimitry Andric 2748*0b57cec5SDimitry Andric } // end anonymous namespace 2749*0b57cec5SDimitry Andric 2750*0b57cec5SDimitry Andric void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch, 2751*0b57cec5SDimitry Andric const unsigned char *MatcherTable, 2752*0b57cec5SDimitry Andric unsigned TableSize) { 2753*0b57cec5SDimitry Andric // FIXME: Should these even be selected? Handle these cases in the caller? 2754*0b57cec5SDimitry Andric switch (NodeToMatch->getOpcode()) { 2755*0b57cec5SDimitry Andric default: 2756*0b57cec5SDimitry Andric break; 2757*0b57cec5SDimitry Andric case ISD::EntryToken: // These nodes remain the same. 2758*0b57cec5SDimitry Andric case ISD::BasicBlock: 2759*0b57cec5SDimitry Andric case ISD::Register: 2760*0b57cec5SDimitry Andric case ISD::RegisterMask: 2761*0b57cec5SDimitry Andric case ISD::HANDLENODE: 2762*0b57cec5SDimitry Andric case ISD::MDNODE_SDNODE: 2763*0b57cec5SDimitry Andric case ISD::TargetConstant: 2764*0b57cec5SDimitry Andric case ISD::TargetConstantFP: 2765*0b57cec5SDimitry Andric case ISD::TargetConstantPool: 2766*0b57cec5SDimitry Andric case ISD::TargetFrameIndex: 2767*0b57cec5SDimitry Andric case ISD::TargetExternalSymbol: 2768*0b57cec5SDimitry Andric case ISD::MCSymbol: 2769*0b57cec5SDimitry Andric case ISD::TargetBlockAddress: 2770*0b57cec5SDimitry Andric case ISD::TargetJumpTable: 2771*0b57cec5SDimitry Andric case ISD::TargetGlobalTLSAddress: 2772*0b57cec5SDimitry Andric case ISD::TargetGlobalAddress: 2773*0b57cec5SDimitry Andric case ISD::TokenFactor: 2774*0b57cec5SDimitry Andric case ISD::CopyFromReg: 2775*0b57cec5SDimitry Andric case ISD::CopyToReg: 2776*0b57cec5SDimitry Andric case ISD::EH_LABEL: 2777*0b57cec5SDimitry Andric case ISD::ANNOTATION_LABEL: 2778*0b57cec5SDimitry Andric case ISD::LIFETIME_START: 2779*0b57cec5SDimitry Andric case ISD::LIFETIME_END: 2780*0b57cec5SDimitry Andric NodeToMatch->setNodeId(-1); // Mark selected. 2781*0b57cec5SDimitry Andric return; 2782*0b57cec5SDimitry Andric case ISD::AssertSext: 2783*0b57cec5SDimitry Andric case ISD::AssertZext: 2784*0b57cec5SDimitry Andric ReplaceUses(SDValue(NodeToMatch, 0), NodeToMatch->getOperand(0)); 2785*0b57cec5SDimitry Andric CurDAG->RemoveDeadNode(NodeToMatch); 2786*0b57cec5SDimitry Andric return; 2787*0b57cec5SDimitry Andric case ISD::INLINEASM: 2788*0b57cec5SDimitry Andric case ISD::INLINEASM_BR: 2789*0b57cec5SDimitry Andric Select_INLINEASM(NodeToMatch, 2790*0b57cec5SDimitry Andric NodeToMatch->getOpcode() == ISD::INLINEASM_BR); 2791*0b57cec5SDimitry Andric return; 2792*0b57cec5SDimitry Andric case ISD::READ_REGISTER: 2793*0b57cec5SDimitry Andric Select_READ_REGISTER(NodeToMatch); 2794*0b57cec5SDimitry Andric return; 2795*0b57cec5SDimitry Andric case ISD::WRITE_REGISTER: 2796*0b57cec5SDimitry Andric Select_WRITE_REGISTER(NodeToMatch); 2797*0b57cec5SDimitry Andric return; 2798*0b57cec5SDimitry Andric case ISD::UNDEF: 2799*0b57cec5SDimitry Andric Select_UNDEF(NodeToMatch); 2800*0b57cec5SDimitry Andric return; 2801*0b57cec5SDimitry Andric } 2802*0b57cec5SDimitry Andric 2803*0b57cec5SDimitry Andric assert(!NodeToMatch->isMachineOpcode() && "Node already selected!"); 2804*0b57cec5SDimitry Andric 2805*0b57cec5SDimitry Andric // Set up the node stack with NodeToMatch as the only node on the stack. 2806*0b57cec5SDimitry Andric SmallVector<SDValue, 8> NodeStack; 2807*0b57cec5SDimitry Andric SDValue N = SDValue(NodeToMatch, 0); 2808*0b57cec5SDimitry Andric NodeStack.push_back(N); 2809*0b57cec5SDimitry Andric 2810*0b57cec5SDimitry Andric // MatchScopes - Scopes used when matching, if a match failure happens, this 2811*0b57cec5SDimitry Andric // indicates where to continue checking. 2812*0b57cec5SDimitry Andric SmallVector<MatchScope, 8> MatchScopes; 2813*0b57cec5SDimitry Andric 2814*0b57cec5SDimitry Andric // RecordedNodes - This is the set of nodes that have been recorded by the 2815*0b57cec5SDimitry Andric // state machine. The second value is the parent of the node, or null if the 2816*0b57cec5SDimitry Andric // root is recorded. 2817*0b57cec5SDimitry Andric SmallVector<std::pair<SDValue, SDNode*>, 8> RecordedNodes; 2818*0b57cec5SDimitry Andric 2819*0b57cec5SDimitry Andric // MatchedMemRefs - This is the set of MemRef's we've seen in the input 2820*0b57cec5SDimitry Andric // pattern. 2821*0b57cec5SDimitry Andric SmallVector<MachineMemOperand*, 2> MatchedMemRefs; 2822*0b57cec5SDimitry Andric 2823*0b57cec5SDimitry Andric // These are the current input chain and glue for use when generating nodes. 2824*0b57cec5SDimitry Andric // Various Emit operations change these. For example, emitting a copytoreg 2825*0b57cec5SDimitry Andric // uses and updates these. 2826*0b57cec5SDimitry Andric SDValue InputChain, InputGlue; 2827*0b57cec5SDimitry Andric 2828*0b57cec5SDimitry Andric // ChainNodesMatched - If a pattern matches nodes that have input/output 2829*0b57cec5SDimitry Andric // chains, the OPC_EmitMergeInputChains operation is emitted which indicates 2830*0b57cec5SDimitry Andric // which ones they are. The result is captured into this list so that we can 2831*0b57cec5SDimitry Andric // update the chain results when the pattern is complete. 2832*0b57cec5SDimitry Andric SmallVector<SDNode*, 3> ChainNodesMatched; 2833*0b57cec5SDimitry Andric 2834*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "ISEL: Starting pattern match\n"); 2835*0b57cec5SDimitry Andric 2836*0b57cec5SDimitry Andric // Determine where to start the interpreter. Normally we start at opcode #0, 2837*0b57cec5SDimitry Andric // but if the state machine starts with an OPC_SwitchOpcode, then we 2838*0b57cec5SDimitry Andric // accelerate the first lookup (which is guaranteed to be hot) with the 2839*0b57cec5SDimitry Andric // OpcodeOffset table. 2840*0b57cec5SDimitry Andric unsigned MatcherIndex = 0; 2841*0b57cec5SDimitry Andric 2842*0b57cec5SDimitry Andric if (!OpcodeOffset.empty()) { 2843*0b57cec5SDimitry Andric // Already computed the OpcodeOffset table, just index into it. 2844*0b57cec5SDimitry Andric if (N.getOpcode() < OpcodeOffset.size()) 2845*0b57cec5SDimitry Andric MatcherIndex = OpcodeOffset[N.getOpcode()]; 2846*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Initial Opcode index to " << MatcherIndex << "\n"); 2847*0b57cec5SDimitry Andric 2848*0b57cec5SDimitry Andric } else if (MatcherTable[0] == OPC_SwitchOpcode) { 2849*0b57cec5SDimitry Andric // Otherwise, the table isn't computed, but the state machine does start 2850*0b57cec5SDimitry Andric // with an OPC_SwitchOpcode instruction. Populate the table now, since this 2851*0b57cec5SDimitry Andric // is the first time we're selecting an instruction. 2852*0b57cec5SDimitry Andric unsigned Idx = 1; 2853*0b57cec5SDimitry Andric while (true) { 2854*0b57cec5SDimitry Andric // Get the size of this case. 2855*0b57cec5SDimitry Andric unsigned CaseSize = MatcherTable[Idx++]; 2856*0b57cec5SDimitry Andric if (CaseSize & 128) 2857*0b57cec5SDimitry Andric CaseSize = GetVBR(CaseSize, MatcherTable, Idx); 2858*0b57cec5SDimitry Andric if (CaseSize == 0) break; 2859*0b57cec5SDimitry Andric 2860*0b57cec5SDimitry Andric // Get the opcode, add the index to the table. 2861*0b57cec5SDimitry Andric uint16_t Opc = MatcherTable[Idx++]; 2862*0b57cec5SDimitry Andric Opc |= (unsigned short)MatcherTable[Idx++] << 8; 2863*0b57cec5SDimitry Andric if (Opc >= OpcodeOffset.size()) 2864*0b57cec5SDimitry Andric OpcodeOffset.resize((Opc+1)*2); 2865*0b57cec5SDimitry Andric OpcodeOffset[Opc] = Idx; 2866*0b57cec5SDimitry Andric Idx += CaseSize; 2867*0b57cec5SDimitry Andric } 2868*0b57cec5SDimitry Andric 2869*0b57cec5SDimitry Andric // Okay, do the lookup for the first opcode. 2870*0b57cec5SDimitry Andric if (N.getOpcode() < OpcodeOffset.size()) 2871*0b57cec5SDimitry Andric MatcherIndex = OpcodeOffset[N.getOpcode()]; 2872*0b57cec5SDimitry Andric } 2873*0b57cec5SDimitry Andric 2874*0b57cec5SDimitry Andric while (true) { 2875*0b57cec5SDimitry Andric assert(MatcherIndex < TableSize && "Invalid index"); 2876*0b57cec5SDimitry Andric #ifndef NDEBUG 2877*0b57cec5SDimitry Andric unsigned CurrentOpcodeIndex = MatcherIndex; 2878*0b57cec5SDimitry Andric #endif 2879*0b57cec5SDimitry Andric BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++]; 2880*0b57cec5SDimitry Andric switch (Opcode) { 2881*0b57cec5SDimitry Andric case OPC_Scope: { 2882*0b57cec5SDimitry Andric // Okay, the semantics of this operation are that we should push a scope 2883*0b57cec5SDimitry Andric // then evaluate the first child. However, pushing a scope only to have 2884*0b57cec5SDimitry Andric // the first check fail (which then pops it) is inefficient. If we can 2885*0b57cec5SDimitry Andric // determine immediately that the first check (or first several) will 2886*0b57cec5SDimitry Andric // immediately fail, don't even bother pushing a scope for them. 2887*0b57cec5SDimitry Andric unsigned FailIndex; 2888*0b57cec5SDimitry Andric 2889*0b57cec5SDimitry Andric while (true) { 2890*0b57cec5SDimitry Andric unsigned NumToSkip = MatcherTable[MatcherIndex++]; 2891*0b57cec5SDimitry Andric if (NumToSkip & 128) 2892*0b57cec5SDimitry Andric NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex); 2893*0b57cec5SDimitry Andric // Found the end of the scope with no match. 2894*0b57cec5SDimitry Andric if (NumToSkip == 0) { 2895*0b57cec5SDimitry Andric FailIndex = 0; 2896*0b57cec5SDimitry Andric break; 2897*0b57cec5SDimitry Andric } 2898*0b57cec5SDimitry Andric 2899*0b57cec5SDimitry Andric FailIndex = MatcherIndex+NumToSkip; 2900*0b57cec5SDimitry Andric 2901*0b57cec5SDimitry Andric unsigned MatcherIndexOfPredicate = MatcherIndex; 2902*0b57cec5SDimitry Andric (void)MatcherIndexOfPredicate; // silence warning. 2903*0b57cec5SDimitry Andric 2904*0b57cec5SDimitry Andric // If we can't evaluate this predicate without pushing a scope (e.g. if 2905*0b57cec5SDimitry Andric // it is a 'MoveParent') or if the predicate succeeds on this node, we 2906*0b57cec5SDimitry Andric // push the scope and evaluate the full predicate chain. 2907*0b57cec5SDimitry Andric bool Result; 2908*0b57cec5SDimitry Andric MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N, 2909*0b57cec5SDimitry Andric Result, *this, RecordedNodes); 2910*0b57cec5SDimitry Andric if (!Result) 2911*0b57cec5SDimitry Andric break; 2912*0b57cec5SDimitry Andric 2913*0b57cec5SDimitry Andric LLVM_DEBUG( 2914*0b57cec5SDimitry Andric dbgs() << " Skipped scope entry (due to false predicate) at " 2915*0b57cec5SDimitry Andric << "index " << MatcherIndexOfPredicate << ", continuing at " 2916*0b57cec5SDimitry Andric << FailIndex << "\n"); 2917*0b57cec5SDimitry Andric ++NumDAGIselRetries; 2918*0b57cec5SDimitry Andric 2919*0b57cec5SDimitry Andric // Otherwise, we know that this case of the Scope is guaranteed to fail, 2920*0b57cec5SDimitry Andric // move to the next case. 2921*0b57cec5SDimitry Andric MatcherIndex = FailIndex; 2922*0b57cec5SDimitry Andric } 2923*0b57cec5SDimitry Andric 2924*0b57cec5SDimitry Andric // If the whole scope failed to match, bail. 2925*0b57cec5SDimitry Andric if (FailIndex == 0) break; 2926*0b57cec5SDimitry Andric 2927*0b57cec5SDimitry Andric // Push a MatchScope which indicates where to go if the first child fails 2928*0b57cec5SDimitry Andric // to match. 2929*0b57cec5SDimitry Andric MatchScope NewEntry; 2930*0b57cec5SDimitry Andric NewEntry.FailIndex = FailIndex; 2931*0b57cec5SDimitry Andric NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end()); 2932*0b57cec5SDimitry Andric NewEntry.NumRecordedNodes = RecordedNodes.size(); 2933*0b57cec5SDimitry Andric NewEntry.NumMatchedMemRefs = MatchedMemRefs.size(); 2934*0b57cec5SDimitry Andric NewEntry.InputChain = InputChain; 2935*0b57cec5SDimitry Andric NewEntry.InputGlue = InputGlue; 2936*0b57cec5SDimitry Andric NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty(); 2937*0b57cec5SDimitry Andric MatchScopes.push_back(NewEntry); 2938*0b57cec5SDimitry Andric continue; 2939*0b57cec5SDimitry Andric } 2940*0b57cec5SDimitry Andric case OPC_RecordNode: { 2941*0b57cec5SDimitry Andric // Remember this node, it may end up being an operand in the pattern. 2942*0b57cec5SDimitry Andric SDNode *Parent = nullptr; 2943*0b57cec5SDimitry Andric if (NodeStack.size() > 1) 2944*0b57cec5SDimitry Andric Parent = NodeStack[NodeStack.size()-2].getNode(); 2945*0b57cec5SDimitry Andric RecordedNodes.push_back(std::make_pair(N, Parent)); 2946*0b57cec5SDimitry Andric continue; 2947*0b57cec5SDimitry Andric } 2948*0b57cec5SDimitry Andric 2949*0b57cec5SDimitry Andric case OPC_RecordChild0: case OPC_RecordChild1: 2950*0b57cec5SDimitry Andric case OPC_RecordChild2: case OPC_RecordChild3: 2951*0b57cec5SDimitry Andric case OPC_RecordChild4: case OPC_RecordChild5: 2952*0b57cec5SDimitry Andric case OPC_RecordChild6: case OPC_RecordChild7: { 2953*0b57cec5SDimitry Andric unsigned ChildNo = Opcode-OPC_RecordChild0; 2954*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2955*0b57cec5SDimitry Andric break; // Match fails if out of range child #. 2956*0b57cec5SDimitry Andric 2957*0b57cec5SDimitry Andric RecordedNodes.push_back(std::make_pair(N->getOperand(ChildNo), 2958*0b57cec5SDimitry Andric N.getNode())); 2959*0b57cec5SDimitry Andric continue; 2960*0b57cec5SDimitry Andric } 2961*0b57cec5SDimitry Andric case OPC_RecordMemRef: 2962*0b57cec5SDimitry Andric if (auto *MN = dyn_cast<MemSDNode>(N)) 2963*0b57cec5SDimitry Andric MatchedMemRefs.push_back(MN->getMemOperand()); 2964*0b57cec5SDimitry Andric else { 2965*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Expected MemSDNode "; N->dump(CurDAG); 2966*0b57cec5SDimitry Andric dbgs() << '\n'); 2967*0b57cec5SDimitry Andric } 2968*0b57cec5SDimitry Andric 2969*0b57cec5SDimitry Andric continue; 2970*0b57cec5SDimitry Andric 2971*0b57cec5SDimitry Andric case OPC_CaptureGlueInput: 2972*0b57cec5SDimitry Andric // If the current node has an input glue, capture it in InputGlue. 2973*0b57cec5SDimitry Andric if (N->getNumOperands() != 0 && 2974*0b57cec5SDimitry Andric N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) 2975*0b57cec5SDimitry Andric InputGlue = N->getOperand(N->getNumOperands()-1); 2976*0b57cec5SDimitry Andric continue; 2977*0b57cec5SDimitry Andric 2978*0b57cec5SDimitry Andric case OPC_MoveChild: { 2979*0b57cec5SDimitry Andric unsigned ChildNo = MatcherTable[MatcherIndex++]; 2980*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2981*0b57cec5SDimitry Andric break; // Match fails if out of range child #. 2982*0b57cec5SDimitry Andric N = N.getOperand(ChildNo); 2983*0b57cec5SDimitry Andric NodeStack.push_back(N); 2984*0b57cec5SDimitry Andric continue; 2985*0b57cec5SDimitry Andric } 2986*0b57cec5SDimitry Andric 2987*0b57cec5SDimitry Andric case OPC_MoveChild0: case OPC_MoveChild1: 2988*0b57cec5SDimitry Andric case OPC_MoveChild2: case OPC_MoveChild3: 2989*0b57cec5SDimitry Andric case OPC_MoveChild4: case OPC_MoveChild5: 2990*0b57cec5SDimitry Andric case OPC_MoveChild6: case OPC_MoveChild7: { 2991*0b57cec5SDimitry Andric unsigned ChildNo = Opcode-OPC_MoveChild0; 2992*0b57cec5SDimitry Andric if (ChildNo >= N.getNumOperands()) 2993*0b57cec5SDimitry Andric break; // Match fails if out of range child #. 2994*0b57cec5SDimitry Andric N = N.getOperand(ChildNo); 2995*0b57cec5SDimitry Andric NodeStack.push_back(N); 2996*0b57cec5SDimitry Andric continue; 2997*0b57cec5SDimitry Andric } 2998*0b57cec5SDimitry Andric 2999*0b57cec5SDimitry Andric case OPC_MoveParent: 3000*0b57cec5SDimitry Andric // Pop the current node off the NodeStack. 3001*0b57cec5SDimitry Andric NodeStack.pop_back(); 3002*0b57cec5SDimitry Andric assert(!NodeStack.empty() && "Node stack imbalance!"); 3003*0b57cec5SDimitry Andric N = NodeStack.back(); 3004*0b57cec5SDimitry Andric continue; 3005*0b57cec5SDimitry Andric 3006*0b57cec5SDimitry Andric case OPC_CheckSame: 3007*0b57cec5SDimitry Andric if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break; 3008*0b57cec5SDimitry Andric continue; 3009*0b57cec5SDimitry Andric 3010*0b57cec5SDimitry Andric case OPC_CheckChild0Same: case OPC_CheckChild1Same: 3011*0b57cec5SDimitry Andric case OPC_CheckChild2Same: case OPC_CheckChild3Same: 3012*0b57cec5SDimitry Andric if (!::CheckChildSame(MatcherTable, MatcherIndex, N, RecordedNodes, 3013*0b57cec5SDimitry Andric Opcode-OPC_CheckChild0Same)) 3014*0b57cec5SDimitry Andric break; 3015*0b57cec5SDimitry Andric continue; 3016*0b57cec5SDimitry Andric 3017*0b57cec5SDimitry Andric case OPC_CheckPatternPredicate: 3018*0b57cec5SDimitry Andric if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this)) break; 3019*0b57cec5SDimitry Andric continue; 3020*0b57cec5SDimitry Andric case OPC_CheckPredicate: 3021*0b57cec5SDimitry Andric if (!::CheckNodePredicate(MatcherTable, MatcherIndex, *this, 3022*0b57cec5SDimitry Andric N.getNode())) 3023*0b57cec5SDimitry Andric break; 3024*0b57cec5SDimitry Andric continue; 3025*0b57cec5SDimitry Andric case OPC_CheckPredicateWithOperands: { 3026*0b57cec5SDimitry Andric unsigned OpNum = MatcherTable[MatcherIndex++]; 3027*0b57cec5SDimitry Andric SmallVector<SDValue, 8> Operands; 3028*0b57cec5SDimitry Andric 3029*0b57cec5SDimitry Andric for (unsigned i = 0; i < OpNum; ++i) 3030*0b57cec5SDimitry Andric Operands.push_back(RecordedNodes[MatcherTable[MatcherIndex++]].first); 3031*0b57cec5SDimitry Andric 3032*0b57cec5SDimitry Andric unsigned PredNo = MatcherTable[MatcherIndex++]; 3033*0b57cec5SDimitry Andric if (!CheckNodePredicateWithOperands(N.getNode(), PredNo, Operands)) 3034*0b57cec5SDimitry Andric break; 3035*0b57cec5SDimitry Andric continue; 3036*0b57cec5SDimitry Andric } 3037*0b57cec5SDimitry Andric case OPC_CheckComplexPat: { 3038*0b57cec5SDimitry Andric unsigned CPNum = MatcherTable[MatcherIndex++]; 3039*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3040*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat"); 3041*0b57cec5SDimitry Andric 3042*0b57cec5SDimitry Andric // If target can modify DAG during matching, keep the matching state 3043*0b57cec5SDimitry Andric // consistent. 3044*0b57cec5SDimitry Andric std::unique_ptr<MatchStateUpdater> MSU; 3045*0b57cec5SDimitry Andric if (ComplexPatternFuncMutatesDAG()) 3046*0b57cec5SDimitry Andric MSU.reset(new MatchStateUpdater(*CurDAG, &NodeToMatch, RecordedNodes, 3047*0b57cec5SDimitry Andric MatchScopes)); 3048*0b57cec5SDimitry Andric 3049*0b57cec5SDimitry Andric if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo].second, 3050*0b57cec5SDimitry Andric RecordedNodes[RecNo].first, CPNum, 3051*0b57cec5SDimitry Andric RecordedNodes)) 3052*0b57cec5SDimitry Andric break; 3053*0b57cec5SDimitry Andric continue; 3054*0b57cec5SDimitry Andric } 3055*0b57cec5SDimitry Andric case OPC_CheckOpcode: 3056*0b57cec5SDimitry Andric if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break; 3057*0b57cec5SDimitry Andric continue; 3058*0b57cec5SDimitry Andric 3059*0b57cec5SDimitry Andric case OPC_CheckType: 3060*0b57cec5SDimitry Andric if (!::CheckType(MatcherTable, MatcherIndex, N, TLI, 3061*0b57cec5SDimitry Andric CurDAG->getDataLayout())) 3062*0b57cec5SDimitry Andric break; 3063*0b57cec5SDimitry Andric continue; 3064*0b57cec5SDimitry Andric 3065*0b57cec5SDimitry Andric case OPC_CheckTypeRes: { 3066*0b57cec5SDimitry Andric unsigned Res = MatcherTable[MatcherIndex++]; 3067*0b57cec5SDimitry Andric if (!::CheckType(MatcherTable, MatcherIndex, N.getValue(Res), TLI, 3068*0b57cec5SDimitry Andric CurDAG->getDataLayout())) 3069*0b57cec5SDimitry Andric break; 3070*0b57cec5SDimitry Andric continue; 3071*0b57cec5SDimitry Andric } 3072*0b57cec5SDimitry Andric 3073*0b57cec5SDimitry Andric case OPC_SwitchOpcode: { 3074*0b57cec5SDimitry Andric unsigned CurNodeOpcode = N.getOpcode(); 3075*0b57cec5SDimitry Andric unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart; 3076*0b57cec5SDimitry Andric unsigned CaseSize; 3077*0b57cec5SDimitry Andric while (true) { 3078*0b57cec5SDimitry Andric // Get the size of this case. 3079*0b57cec5SDimitry Andric CaseSize = MatcherTable[MatcherIndex++]; 3080*0b57cec5SDimitry Andric if (CaseSize & 128) 3081*0b57cec5SDimitry Andric CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex); 3082*0b57cec5SDimitry Andric if (CaseSize == 0) break; 3083*0b57cec5SDimitry Andric 3084*0b57cec5SDimitry Andric uint16_t Opc = MatcherTable[MatcherIndex++]; 3085*0b57cec5SDimitry Andric Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 3086*0b57cec5SDimitry Andric 3087*0b57cec5SDimitry Andric // If the opcode matches, then we will execute this case. 3088*0b57cec5SDimitry Andric if (CurNodeOpcode == Opc) 3089*0b57cec5SDimitry Andric break; 3090*0b57cec5SDimitry Andric 3091*0b57cec5SDimitry Andric // Otherwise, skip over this case. 3092*0b57cec5SDimitry Andric MatcherIndex += CaseSize; 3093*0b57cec5SDimitry Andric } 3094*0b57cec5SDimitry Andric 3095*0b57cec5SDimitry Andric // If no cases matched, bail out. 3096*0b57cec5SDimitry Andric if (CaseSize == 0) break; 3097*0b57cec5SDimitry Andric 3098*0b57cec5SDimitry Andric // Otherwise, execute the case we found. 3099*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " OpcodeSwitch from " << SwitchStart << " to " 3100*0b57cec5SDimitry Andric << MatcherIndex << "\n"); 3101*0b57cec5SDimitry Andric continue; 3102*0b57cec5SDimitry Andric } 3103*0b57cec5SDimitry Andric 3104*0b57cec5SDimitry Andric case OPC_SwitchType: { 3105*0b57cec5SDimitry Andric MVT CurNodeVT = N.getSimpleValueType(); 3106*0b57cec5SDimitry Andric unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart; 3107*0b57cec5SDimitry Andric unsigned CaseSize; 3108*0b57cec5SDimitry Andric while (true) { 3109*0b57cec5SDimitry Andric // Get the size of this case. 3110*0b57cec5SDimitry Andric CaseSize = MatcherTable[MatcherIndex++]; 3111*0b57cec5SDimitry Andric if (CaseSize & 128) 3112*0b57cec5SDimitry Andric CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex); 3113*0b57cec5SDimitry Andric if (CaseSize == 0) break; 3114*0b57cec5SDimitry Andric 3115*0b57cec5SDimitry Andric MVT CaseVT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3116*0b57cec5SDimitry Andric if (CaseVT == MVT::iPTR) 3117*0b57cec5SDimitry Andric CaseVT = TLI->getPointerTy(CurDAG->getDataLayout()); 3118*0b57cec5SDimitry Andric 3119*0b57cec5SDimitry Andric // If the VT matches, then we will execute this case. 3120*0b57cec5SDimitry Andric if (CurNodeVT == CaseVT) 3121*0b57cec5SDimitry Andric break; 3122*0b57cec5SDimitry Andric 3123*0b57cec5SDimitry Andric // Otherwise, skip over this case. 3124*0b57cec5SDimitry Andric MatcherIndex += CaseSize; 3125*0b57cec5SDimitry Andric } 3126*0b57cec5SDimitry Andric 3127*0b57cec5SDimitry Andric // If no cases matched, bail out. 3128*0b57cec5SDimitry Andric if (CaseSize == 0) break; 3129*0b57cec5SDimitry Andric 3130*0b57cec5SDimitry Andric // Otherwise, execute the case we found. 3131*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " TypeSwitch[" << EVT(CurNodeVT).getEVTString() 3132*0b57cec5SDimitry Andric << "] from " << SwitchStart << " to " << MatcherIndex 3133*0b57cec5SDimitry Andric << '\n'); 3134*0b57cec5SDimitry Andric continue; 3135*0b57cec5SDimitry Andric } 3136*0b57cec5SDimitry Andric case OPC_CheckChild0Type: case OPC_CheckChild1Type: 3137*0b57cec5SDimitry Andric case OPC_CheckChild2Type: case OPC_CheckChild3Type: 3138*0b57cec5SDimitry Andric case OPC_CheckChild4Type: case OPC_CheckChild5Type: 3139*0b57cec5SDimitry Andric case OPC_CheckChild6Type: case OPC_CheckChild7Type: 3140*0b57cec5SDimitry Andric if (!::CheckChildType(MatcherTable, MatcherIndex, N, TLI, 3141*0b57cec5SDimitry Andric CurDAG->getDataLayout(), 3142*0b57cec5SDimitry Andric Opcode - OPC_CheckChild0Type)) 3143*0b57cec5SDimitry Andric break; 3144*0b57cec5SDimitry Andric continue; 3145*0b57cec5SDimitry Andric case OPC_CheckCondCode: 3146*0b57cec5SDimitry Andric if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break; 3147*0b57cec5SDimitry Andric continue; 3148*0b57cec5SDimitry Andric case OPC_CheckChild2CondCode: 3149*0b57cec5SDimitry Andric if (!::CheckChild2CondCode(MatcherTable, MatcherIndex, N)) break; 3150*0b57cec5SDimitry Andric continue; 3151*0b57cec5SDimitry Andric case OPC_CheckValueType: 3152*0b57cec5SDimitry Andric if (!::CheckValueType(MatcherTable, MatcherIndex, N, TLI, 3153*0b57cec5SDimitry Andric CurDAG->getDataLayout())) 3154*0b57cec5SDimitry Andric break; 3155*0b57cec5SDimitry Andric continue; 3156*0b57cec5SDimitry Andric case OPC_CheckInteger: 3157*0b57cec5SDimitry Andric if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break; 3158*0b57cec5SDimitry Andric continue; 3159*0b57cec5SDimitry Andric case OPC_CheckChild0Integer: case OPC_CheckChild1Integer: 3160*0b57cec5SDimitry Andric case OPC_CheckChild2Integer: case OPC_CheckChild3Integer: 3161*0b57cec5SDimitry Andric case OPC_CheckChild4Integer: 3162*0b57cec5SDimitry Andric if (!::CheckChildInteger(MatcherTable, MatcherIndex, N, 3163*0b57cec5SDimitry Andric Opcode-OPC_CheckChild0Integer)) break; 3164*0b57cec5SDimitry Andric continue; 3165*0b57cec5SDimitry Andric case OPC_CheckAndImm: 3166*0b57cec5SDimitry Andric if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break; 3167*0b57cec5SDimitry Andric continue; 3168*0b57cec5SDimitry Andric case OPC_CheckOrImm: 3169*0b57cec5SDimitry Andric if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break; 3170*0b57cec5SDimitry Andric continue; 3171*0b57cec5SDimitry Andric case OPC_CheckImmAllOnesV: 3172*0b57cec5SDimitry Andric if (!ISD::isBuildVectorAllOnes(N.getNode())) break; 3173*0b57cec5SDimitry Andric continue; 3174*0b57cec5SDimitry Andric case OPC_CheckImmAllZerosV: 3175*0b57cec5SDimitry Andric if (!ISD::isBuildVectorAllZeros(N.getNode())) break; 3176*0b57cec5SDimitry Andric continue; 3177*0b57cec5SDimitry Andric 3178*0b57cec5SDimitry Andric case OPC_CheckFoldableChainNode: { 3179*0b57cec5SDimitry Andric assert(NodeStack.size() != 1 && "No parent node"); 3180*0b57cec5SDimitry Andric // Verify that all intermediate nodes between the root and this one have 3181*0b57cec5SDimitry Andric // a single use. 3182*0b57cec5SDimitry Andric bool HasMultipleUses = false; 3183*0b57cec5SDimitry Andric for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i) 3184*0b57cec5SDimitry Andric if (!NodeStack[i].getNode()->hasOneUse()) { 3185*0b57cec5SDimitry Andric HasMultipleUses = true; 3186*0b57cec5SDimitry Andric break; 3187*0b57cec5SDimitry Andric } 3188*0b57cec5SDimitry Andric if (HasMultipleUses) break; 3189*0b57cec5SDimitry Andric 3190*0b57cec5SDimitry Andric // Check to see that the target thinks this is profitable to fold and that 3191*0b57cec5SDimitry Andric // we can fold it without inducing cycles in the graph. 3192*0b57cec5SDimitry Andric if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(), 3193*0b57cec5SDimitry Andric NodeToMatch) || 3194*0b57cec5SDimitry Andric !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(), 3195*0b57cec5SDimitry Andric NodeToMatch, OptLevel, 3196*0b57cec5SDimitry Andric true/*We validate our own chains*/)) 3197*0b57cec5SDimitry Andric break; 3198*0b57cec5SDimitry Andric 3199*0b57cec5SDimitry Andric continue; 3200*0b57cec5SDimitry Andric } 3201*0b57cec5SDimitry Andric case OPC_EmitInteger: { 3202*0b57cec5SDimitry Andric MVT::SimpleValueType VT = 3203*0b57cec5SDimitry Andric (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3204*0b57cec5SDimitry Andric int64_t Val = MatcherTable[MatcherIndex++]; 3205*0b57cec5SDimitry Andric if (Val & 128) 3206*0b57cec5SDimitry Andric Val = GetVBR(Val, MatcherTable, MatcherIndex); 3207*0b57cec5SDimitry Andric RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3208*0b57cec5SDimitry Andric CurDAG->getTargetConstant(Val, SDLoc(NodeToMatch), 3209*0b57cec5SDimitry Andric VT), nullptr)); 3210*0b57cec5SDimitry Andric continue; 3211*0b57cec5SDimitry Andric } 3212*0b57cec5SDimitry Andric case OPC_EmitRegister: { 3213*0b57cec5SDimitry Andric MVT::SimpleValueType VT = 3214*0b57cec5SDimitry Andric (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3215*0b57cec5SDimitry Andric unsigned RegNo = MatcherTable[MatcherIndex++]; 3216*0b57cec5SDimitry Andric RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3217*0b57cec5SDimitry Andric CurDAG->getRegister(RegNo, VT), nullptr)); 3218*0b57cec5SDimitry Andric continue; 3219*0b57cec5SDimitry Andric } 3220*0b57cec5SDimitry Andric case OPC_EmitRegister2: { 3221*0b57cec5SDimitry Andric // For targets w/ more than 256 register names, the register enum 3222*0b57cec5SDimitry Andric // values are stored in two bytes in the matcher table (just like 3223*0b57cec5SDimitry Andric // opcodes). 3224*0b57cec5SDimitry Andric MVT::SimpleValueType VT = 3225*0b57cec5SDimitry Andric (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3226*0b57cec5SDimitry Andric unsigned RegNo = MatcherTable[MatcherIndex++]; 3227*0b57cec5SDimitry Andric RegNo |= MatcherTable[MatcherIndex++] << 8; 3228*0b57cec5SDimitry Andric RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3229*0b57cec5SDimitry Andric CurDAG->getRegister(RegNo, VT), nullptr)); 3230*0b57cec5SDimitry Andric continue; 3231*0b57cec5SDimitry Andric } 3232*0b57cec5SDimitry Andric 3233*0b57cec5SDimitry Andric case OPC_EmitConvertToTarget: { 3234*0b57cec5SDimitry Andric // Convert from IMM/FPIMM to target version. 3235*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3236*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitConvertToTarget"); 3237*0b57cec5SDimitry Andric SDValue Imm = RecordedNodes[RecNo].first; 3238*0b57cec5SDimitry Andric 3239*0b57cec5SDimitry Andric if (Imm->getOpcode() == ISD::Constant) { 3240*0b57cec5SDimitry Andric const ConstantInt *Val=cast<ConstantSDNode>(Imm)->getConstantIntValue(); 3241*0b57cec5SDimitry Andric Imm = CurDAG->getTargetConstant(*Val, SDLoc(NodeToMatch), 3242*0b57cec5SDimitry Andric Imm.getValueType()); 3243*0b57cec5SDimitry Andric } else if (Imm->getOpcode() == ISD::ConstantFP) { 3244*0b57cec5SDimitry Andric const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue(); 3245*0b57cec5SDimitry Andric Imm = CurDAG->getTargetConstantFP(*Val, SDLoc(NodeToMatch), 3246*0b57cec5SDimitry Andric Imm.getValueType()); 3247*0b57cec5SDimitry Andric } 3248*0b57cec5SDimitry Andric 3249*0b57cec5SDimitry Andric RecordedNodes.push_back(std::make_pair(Imm, RecordedNodes[RecNo].second)); 3250*0b57cec5SDimitry Andric continue; 3251*0b57cec5SDimitry Andric } 3252*0b57cec5SDimitry Andric 3253*0b57cec5SDimitry Andric case OPC_EmitMergeInputChains1_0: // OPC_EmitMergeInputChains, 1, 0 3254*0b57cec5SDimitry Andric case OPC_EmitMergeInputChains1_1: // OPC_EmitMergeInputChains, 1, 1 3255*0b57cec5SDimitry Andric case OPC_EmitMergeInputChains1_2: { // OPC_EmitMergeInputChains, 1, 2 3256*0b57cec5SDimitry Andric // These are space-optimized forms of OPC_EmitMergeInputChains. 3257*0b57cec5SDimitry Andric assert(!InputChain.getNode() && 3258*0b57cec5SDimitry Andric "EmitMergeInputChains should be the first chain producing node"); 3259*0b57cec5SDimitry Andric assert(ChainNodesMatched.empty() && 3260*0b57cec5SDimitry Andric "Should only have one EmitMergeInputChains per match"); 3261*0b57cec5SDimitry Andric 3262*0b57cec5SDimitry Andric // Read all of the chained nodes. 3263*0b57cec5SDimitry Andric unsigned RecNo = Opcode - OPC_EmitMergeInputChains1_0; 3264*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains"); 3265*0b57cec5SDimitry Andric ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode()); 3266*0b57cec5SDimitry Andric 3267*0b57cec5SDimitry Andric // FIXME: What if other value results of the node have uses not matched 3268*0b57cec5SDimitry Andric // by this pattern? 3269*0b57cec5SDimitry Andric if (ChainNodesMatched.back() != NodeToMatch && 3270*0b57cec5SDimitry Andric !RecordedNodes[RecNo].first.hasOneUse()) { 3271*0b57cec5SDimitry Andric ChainNodesMatched.clear(); 3272*0b57cec5SDimitry Andric break; 3273*0b57cec5SDimitry Andric } 3274*0b57cec5SDimitry Andric 3275*0b57cec5SDimitry Andric // Merge the input chains if they are not intra-pattern references. 3276*0b57cec5SDimitry Andric InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG); 3277*0b57cec5SDimitry Andric 3278*0b57cec5SDimitry Andric if (!InputChain.getNode()) 3279*0b57cec5SDimitry Andric break; // Failed to merge. 3280*0b57cec5SDimitry Andric continue; 3281*0b57cec5SDimitry Andric } 3282*0b57cec5SDimitry Andric 3283*0b57cec5SDimitry Andric case OPC_EmitMergeInputChains: { 3284*0b57cec5SDimitry Andric assert(!InputChain.getNode() && 3285*0b57cec5SDimitry Andric "EmitMergeInputChains should be the first chain producing node"); 3286*0b57cec5SDimitry Andric // This node gets a list of nodes we matched in the input that have 3287*0b57cec5SDimitry Andric // chains. We want to token factor all of the input chains to these nodes 3288*0b57cec5SDimitry Andric // together. However, if any of the input chains is actually one of the 3289*0b57cec5SDimitry Andric // nodes matched in this pattern, then we have an intra-match reference. 3290*0b57cec5SDimitry Andric // Ignore these because the newly token factored chain should not refer to 3291*0b57cec5SDimitry Andric // the old nodes. 3292*0b57cec5SDimitry Andric unsigned NumChains = MatcherTable[MatcherIndex++]; 3293*0b57cec5SDimitry Andric assert(NumChains != 0 && "Can't TF zero chains"); 3294*0b57cec5SDimitry Andric 3295*0b57cec5SDimitry Andric assert(ChainNodesMatched.empty() && 3296*0b57cec5SDimitry Andric "Should only have one EmitMergeInputChains per match"); 3297*0b57cec5SDimitry Andric 3298*0b57cec5SDimitry Andric // Read all of the chained nodes. 3299*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumChains; ++i) { 3300*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3301*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains"); 3302*0b57cec5SDimitry Andric ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode()); 3303*0b57cec5SDimitry Andric 3304*0b57cec5SDimitry Andric // FIXME: What if other value results of the node have uses not matched 3305*0b57cec5SDimitry Andric // by this pattern? 3306*0b57cec5SDimitry Andric if (ChainNodesMatched.back() != NodeToMatch && 3307*0b57cec5SDimitry Andric !RecordedNodes[RecNo].first.hasOneUse()) { 3308*0b57cec5SDimitry Andric ChainNodesMatched.clear(); 3309*0b57cec5SDimitry Andric break; 3310*0b57cec5SDimitry Andric } 3311*0b57cec5SDimitry Andric } 3312*0b57cec5SDimitry Andric 3313*0b57cec5SDimitry Andric // If the inner loop broke out, the match fails. 3314*0b57cec5SDimitry Andric if (ChainNodesMatched.empty()) 3315*0b57cec5SDimitry Andric break; 3316*0b57cec5SDimitry Andric 3317*0b57cec5SDimitry Andric // Merge the input chains if they are not intra-pattern references. 3318*0b57cec5SDimitry Andric InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG); 3319*0b57cec5SDimitry Andric 3320*0b57cec5SDimitry Andric if (!InputChain.getNode()) 3321*0b57cec5SDimitry Andric break; // Failed to merge. 3322*0b57cec5SDimitry Andric 3323*0b57cec5SDimitry Andric continue; 3324*0b57cec5SDimitry Andric } 3325*0b57cec5SDimitry Andric 3326*0b57cec5SDimitry Andric case OPC_EmitCopyToReg: { 3327*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3328*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg"); 3329*0b57cec5SDimitry Andric unsigned DestPhysReg = MatcherTable[MatcherIndex++]; 3330*0b57cec5SDimitry Andric 3331*0b57cec5SDimitry Andric if (!InputChain.getNode()) 3332*0b57cec5SDimitry Andric InputChain = CurDAG->getEntryNode(); 3333*0b57cec5SDimitry Andric 3334*0b57cec5SDimitry Andric InputChain = CurDAG->getCopyToReg(InputChain, SDLoc(NodeToMatch), 3335*0b57cec5SDimitry Andric DestPhysReg, RecordedNodes[RecNo].first, 3336*0b57cec5SDimitry Andric InputGlue); 3337*0b57cec5SDimitry Andric 3338*0b57cec5SDimitry Andric InputGlue = InputChain.getValue(1); 3339*0b57cec5SDimitry Andric continue; 3340*0b57cec5SDimitry Andric } 3341*0b57cec5SDimitry Andric 3342*0b57cec5SDimitry Andric case OPC_EmitNodeXForm: { 3343*0b57cec5SDimitry Andric unsigned XFormNo = MatcherTable[MatcherIndex++]; 3344*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3345*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitNodeXForm"); 3346*0b57cec5SDimitry Andric SDValue Res = RunSDNodeXForm(RecordedNodes[RecNo].first, XFormNo); 3347*0b57cec5SDimitry Andric RecordedNodes.push_back(std::pair<SDValue,SDNode*>(Res, nullptr)); 3348*0b57cec5SDimitry Andric continue; 3349*0b57cec5SDimitry Andric } 3350*0b57cec5SDimitry Andric case OPC_Coverage: { 3351*0b57cec5SDimitry Andric // This is emitted right before MorphNode/EmitNode. 3352*0b57cec5SDimitry Andric // So it should be safe to assume that this node has been selected 3353*0b57cec5SDimitry Andric unsigned index = MatcherTable[MatcherIndex++]; 3354*0b57cec5SDimitry Andric index |= (MatcherTable[MatcherIndex++] << 8); 3355*0b57cec5SDimitry Andric dbgs() << "COVERED: " << getPatternForIndex(index) << "\n"; 3356*0b57cec5SDimitry Andric dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n"; 3357*0b57cec5SDimitry Andric continue; 3358*0b57cec5SDimitry Andric } 3359*0b57cec5SDimitry Andric 3360*0b57cec5SDimitry Andric case OPC_EmitNode: case OPC_MorphNodeTo: 3361*0b57cec5SDimitry Andric case OPC_EmitNode0: case OPC_EmitNode1: case OPC_EmitNode2: 3362*0b57cec5SDimitry Andric case OPC_MorphNodeTo0: case OPC_MorphNodeTo1: case OPC_MorphNodeTo2: { 3363*0b57cec5SDimitry Andric uint16_t TargetOpc = MatcherTable[MatcherIndex++]; 3364*0b57cec5SDimitry Andric TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 3365*0b57cec5SDimitry Andric unsigned EmitNodeInfo = MatcherTable[MatcherIndex++]; 3366*0b57cec5SDimitry Andric // Get the result VT list. 3367*0b57cec5SDimitry Andric unsigned NumVTs; 3368*0b57cec5SDimitry Andric // If this is one of the compressed forms, get the number of VTs based 3369*0b57cec5SDimitry Andric // on the Opcode. Otherwise read the next byte from the table. 3370*0b57cec5SDimitry Andric if (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2) 3371*0b57cec5SDimitry Andric NumVTs = Opcode - OPC_MorphNodeTo0; 3372*0b57cec5SDimitry Andric else if (Opcode >= OPC_EmitNode0 && Opcode <= OPC_EmitNode2) 3373*0b57cec5SDimitry Andric NumVTs = Opcode - OPC_EmitNode0; 3374*0b57cec5SDimitry Andric else 3375*0b57cec5SDimitry Andric NumVTs = MatcherTable[MatcherIndex++]; 3376*0b57cec5SDimitry Andric SmallVector<EVT, 4> VTs; 3377*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumVTs; ++i) { 3378*0b57cec5SDimitry Andric MVT::SimpleValueType VT = 3379*0b57cec5SDimitry Andric (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3380*0b57cec5SDimitry Andric if (VT == MVT::iPTR) 3381*0b57cec5SDimitry Andric VT = TLI->getPointerTy(CurDAG->getDataLayout()).SimpleTy; 3382*0b57cec5SDimitry Andric VTs.push_back(VT); 3383*0b57cec5SDimitry Andric } 3384*0b57cec5SDimitry Andric 3385*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_Chain) 3386*0b57cec5SDimitry Andric VTs.push_back(MVT::Other); 3387*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_GlueOutput) 3388*0b57cec5SDimitry Andric VTs.push_back(MVT::Glue); 3389*0b57cec5SDimitry Andric 3390*0b57cec5SDimitry Andric // This is hot code, so optimize the two most common cases of 1 and 2 3391*0b57cec5SDimitry Andric // results. 3392*0b57cec5SDimitry Andric SDVTList VTList; 3393*0b57cec5SDimitry Andric if (VTs.size() == 1) 3394*0b57cec5SDimitry Andric VTList = CurDAG->getVTList(VTs[0]); 3395*0b57cec5SDimitry Andric else if (VTs.size() == 2) 3396*0b57cec5SDimitry Andric VTList = CurDAG->getVTList(VTs[0], VTs[1]); 3397*0b57cec5SDimitry Andric else 3398*0b57cec5SDimitry Andric VTList = CurDAG->getVTList(VTs); 3399*0b57cec5SDimitry Andric 3400*0b57cec5SDimitry Andric // Get the operand list. 3401*0b57cec5SDimitry Andric unsigned NumOps = MatcherTable[MatcherIndex++]; 3402*0b57cec5SDimitry Andric SmallVector<SDValue, 8> Ops; 3403*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumOps; ++i) { 3404*0b57cec5SDimitry Andric unsigned RecNo = MatcherTable[MatcherIndex++]; 3405*0b57cec5SDimitry Andric if (RecNo & 128) 3406*0b57cec5SDimitry Andric RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex); 3407*0b57cec5SDimitry Andric 3408*0b57cec5SDimitry Andric assert(RecNo < RecordedNodes.size() && "Invalid EmitNode"); 3409*0b57cec5SDimitry Andric Ops.push_back(RecordedNodes[RecNo].first); 3410*0b57cec5SDimitry Andric } 3411*0b57cec5SDimitry Andric 3412*0b57cec5SDimitry Andric // If there are variadic operands to add, handle them now. 3413*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_VariadicInfo) { 3414*0b57cec5SDimitry Andric // Determine the start index to copy from. 3415*0b57cec5SDimitry Andric unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo); 3416*0b57cec5SDimitry Andric FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0; 3417*0b57cec5SDimitry Andric assert(NodeToMatch->getNumOperands() >= FirstOpToCopy && 3418*0b57cec5SDimitry Andric "Invalid variadic node"); 3419*0b57cec5SDimitry Andric // Copy all of the variadic operands, not including a potential glue 3420*0b57cec5SDimitry Andric // input. 3421*0b57cec5SDimitry Andric for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands(); 3422*0b57cec5SDimitry Andric i != e; ++i) { 3423*0b57cec5SDimitry Andric SDValue V = NodeToMatch->getOperand(i); 3424*0b57cec5SDimitry Andric if (V.getValueType() == MVT::Glue) break; 3425*0b57cec5SDimitry Andric Ops.push_back(V); 3426*0b57cec5SDimitry Andric } 3427*0b57cec5SDimitry Andric } 3428*0b57cec5SDimitry Andric 3429*0b57cec5SDimitry Andric // If this has chain/glue inputs, add them. 3430*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_Chain) 3431*0b57cec5SDimitry Andric Ops.push_back(InputChain); 3432*0b57cec5SDimitry Andric if ((EmitNodeInfo & OPFL_GlueInput) && InputGlue.getNode() != nullptr) 3433*0b57cec5SDimitry Andric Ops.push_back(InputGlue); 3434*0b57cec5SDimitry Andric 3435*0b57cec5SDimitry Andric // Create the node. 3436*0b57cec5SDimitry Andric MachineSDNode *Res = nullptr; 3437*0b57cec5SDimitry Andric bool IsMorphNodeTo = Opcode == OPC_MorphNodeTo || 3438*0b57cec5SDimitry Andric (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2); 3439*0b57cec5SDimitry Andric if (!IsMorphNodeTo) { 3440*0b57cec5SDimitry Andric // If this is a normal EmitNode command, just create the new node and 3441*0b57cec5SDimitry Andric // add the results to the RecordedNodes list. 3442*0b57cec5SDimitry Andric Res = CurDAG->getMachineNode(TargetOpc, SDLoc(NodeToMatch), 3443*0b57cec5SDimitry Andric VTList, Ops); 3444*0b57cec5SDimitry Andric 3445*0b57cec5SDimitry Andric // Add all the non-glue/non-chain results to the RecordedNodes list. 3446*0b57cec5SDimitry Andric for (unsigned i = 0, e = VTs.size(); i != e; ++i) { 3447*0b57cec5SDimitry Andric if (VTs[i] == MVT::Other || VTs[i] == MVT::Glue) break; 3448*0b57cec5SDimitry Andric RecordedNodes.push_back(std::pair<SDValue,SDNode*>(SDValue(Res, i), 3449*0b57cec5SDimitry Andric nullptr)); 3450*0b57cec5SDimitry Andric } 3451*0b57cec5SDimitry Andric } else { 3452*0b57cec5SDimitry Andric assert(NodeToMatch->getOpcode() != ISD::DELETED_NODE && 3453*0b57cec5SDimitry Andric "NodeToMatch was removed partway through selection"); 3454*0b57cec5SDimitry Andric SelectionDAG::DAGNodeDeletedListener NDL(*CurDAG, [&](SDNode *N, 3455*0b57cec5SDimitry Andric SDNode *E) { 3456*0b57cec5SDimitry Andric CurDAG->salvageDebugInfo(*N); 3457*0b57cec5SDimitry Andric auto &Chain = ChainNodesMatched; 3458*0b57cec5SDimitry Andric assert((!E || !is_contained(Chain, N)) && 3459*0b57cec5SDimitry Andric "Chain node replaced during MorphNode"); 3460*0b57cec5SDimitry Andric Chain.erase(std::remove(Chain.begin(), Chain.end(), N), Chain.end()); 3461*0b57cec5SDimitry Andric }); 3462*0b57cec5SDimitry Andric Res = cast<MachineSDNode>(MorphNode(NodeToMatch, TargetOpc, VTList, 3463*0b57cec5SDimitry Andric Ops, EmitNodeInfo)); 3464*0b57cec5SDimitry Andric } 3465*0b57cec5SDimitry Andric 3466*0b57cec5SDimitry Andric // If the node had chain/glue results, update our notion of the current 3467*0b57cec5SDimitry Andric // chain and glue. 3468*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_GlueOutput) { 3469*0b57cec5SDimitry Andric InputGlue = SDValue(Res, VTs.size()-1); 3470*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_Chain) 3471*0b57cec5SDimitry Andric InputChain = SDValue(Res, VTs.size()-2); 3472*0b57cec5SDimitry Andric } else if (EmitNodeInfo & OPFL_Chain) 3473*0b57cec5SDimitry Andric InputChain = SDValue(Res, VTs.size()-1); 3474*0b57cec5SDimitry Andric 3475*0b57cec5SDimitry Andric // If the OPFL_MemRefs glue is set on this node, slap all of the 3476*0b57cec5SDimitry Andric // accumulated memrefs onto it. 3477*0b57cec5SDimitry Andric // 3478*0b57cec5SDimitry Andric // FIXME: This is vastly incorrect for patterns with multiple outputs 3479*0b57cec5SDimitry Andric // instructions that access memory and for ComplexPatterns that match 3480*0b57cec5SDimitry Andric // loads. 3481*0b57cec5SDimitry Andric if (EmitNodeInfo & OPFL_MemRefs) { 3482*0b57cec5SDimitry Andric // Only attach load or store memory operands if the generated 3483*0b57cec5SDimitry Andric // instruction may load or store. 3484*0b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(TargetOpc); 3485*0b57cec5SDimitry Andric bool mayLoad = MCID.mayLoad(); 3486*0b57cec5SDimitry Andric bool mayStore = MCID.mayStore(); 3487*0b57cec5SDimitry Andric 3488*0b57cec5SDimitry Andric // We expect to have relatively few of these so just filter them into a 3489*0b57cec5SDimitry Andric // temporary buffer so that we can easily add them to the instruction. 3490*0b57cec5SDimitry Andric SmallVector<MachineMemOperand *, 4> FilteredMemRefs; 3491*0b57cec5SDimitry Andric for (MachineMemOperand *MMO : MatchedMemRefs) { 3492*0b57cec5SDimitry Andric if (MMO->isLoad()) { 3493*0b57cec5SDimitry Andric if (mayLoad) 3494*0b57cec5SDimitry Andric FilteredMemRefs.push_back(MMO); 3495*0b57cec5SDimitry Andric } else if (MMO->isStore()) { 3496*0b57cec5SDimitry Andric if (mayStore) 3497*0b57cec5SDimitry Andric FilteredMemRefs.push_back(MMO); 3498*0b57cec5SDimitry Andric } else { 3499*0b57cec5SDimitry Andric FilteredMemRefs.push_back(MMO); 3500*0b57cec5SDimitry Andric } 3501*0b57cec5SDimitry Andric } 3502*0b57cec5SDimitry Andric 3503*0b57cec5SDimitry Andric CurDAG->setNodeMemRefs(Res, FilteredMemRefs); 3504*0b57cec5SDimitry Andric } 3505*0b57cec5SDimitry Andric 3506*0b57cec5SDimitry Andric LLVM_DEBUG(if (!MatchedMemRefs.empty() && Res->memoperands_empty()) dbgs() 3507*0b57cec5SDimitry Andric << " Dropping mem operands\n"; 3508*0b57cec5SDimitry Andric dbgs() << " " << (IsMorphNodeTo ? "Morphed" : "Created") 3509*0b57cec5SDimitry Andric << " node: "; 3510*0b57cec5SDimitry Andric Res->dump(CurDAG);); 3511*0b57cec5SDimitry Andric 3512*0b57cec5SDimitry Andric // If this was a MorphNodeTo then we're completely done! 3513*0b57cec5SDimitry Andric if (IsMorphNodeTo) { 3514*0b57cec5SDimitry Andric // Update chain uses. 3515*0b57cec5SDimitry Andric UpdateChains(Res, InputChain, ChainNodesMatched, true); 3516*0b57cec5SDimitry Andric return; 3517*0b57cec5SDimitry Andric } 3518*0b57cec5SDimitry Andric continue; 3519*0b57cec5SDimitry Andric } 3520*0b57cec5SDimitry Andric 3521*0b57cec5SDimitry Andric case OPC_CompleteMatch: { 3522*0b57cec5SDimitry Andric // The match has been completed, and any new nodes (if any) have been 3523*0b57cec5SDimitry Andric // created. Patch up references to the matched dag to use the newly 3524*0b57cec5SDimitry Andric // created nodes. 3525*0b57cec5SDimitry Andric unsigned NumResults = MatcherTable[MatcherIndex++]; 3526*0b57cec5SDimitry Andric 3527*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumResults; ++i) { 3528*0b57cec5SDimitry Andric unsigned ResSlot = MatcherTable[MatcherIndex++]; 3529*0b57cec5SDimitry Andric if (ResSlot & 128) 3530*0b57cec5SDimitry Andric ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex); 3531*0b57cec5SDimitry Andric 3532*0b57cec5SDimitry Andric assert(ResSlot < RecordedNodes.size() && "Invalid CompleteMatch"); 3533*0b57cec5SDimitry Andric SDValue Res = RecordedNodes[ResSlot].first; 3534*0b57cec5SDimitry Andric 3535*0b57cec5SDimitry Andric assert(i < NodeToMatch->getNumValues() && 3536*0b57cec5SDimitry Andric NodeToMatch->getValueType(i) != MVT::Other && 3537*0b57cec5SDimitry Andric NodeToMatch->getValueType(i) != MVT::Glue && 3538*0b57cec5SDimitry Andric "Invalid number of results to complete!"); 3539*0b57cec5SDimitry Andric assert((NodeToMatch->getValueType(i) == Res.getValueType() || 3540*0b57cec5SDimitry Andric NodeToMatch->getValueType(i) == MVT::iPTR || 3541*0b57cec5SDimitry Andric Res.getValueType() == MVT::iPTR || 3542*0b57cec5SDimitry Andric NodeToMatch->getValueType(i).getSizeInBits() == 3543*0b57cec5SDimitry Andric Res.getValueSizeInBits()) && 3544*0b57cec5SDimitry Andric "invalid replacement"); 3545*0b57cec5SDimitry Andric ReplaceUses(SDValue(NodeToMatch, i), Res); 3546*0b57cec5SDimitry Andric } 3547*0b57cec5SDimitry Andric 3548*0b57cec5SDimitry Andric // Update chain uses. 3549*0b57cec5SDimitry Andric UpdateChains(NodeToMatch, InputChain, ChainNodesMatched, false); 3550*0b57cec5SDimitry Andric 3551*0b57cec5SDimitry Andric // If the root node defines glue, we need to update it to the glue result. 3552*0b57cec5SDimitry Andric // TODO: This never happens in our tests and I think it can be removed / 3553*0b57cec5SDimitry Andric // replaced with an assert, but if we do it this the way the change is 3554*0b57cec5SDimitry Andric // NFC. 3555*0b57cec5SDimitry Andric if (NodeToMatch->getValueType(NodeToMatch->getNumValues() - 1) == 3556*0b57cec5SDimitry Andric MVT::Glue && 3557*0b57cec5SDimitry Andric InputGlue.getNode()) 3558*0b57cec5SDimitry Andric ReplaceUses(SDValue(NodeToMatch, NodeToMatch->getNumValues() - 1), 3559*0b57cec5SDimitry Andric InputGlue); 3560*0b57cec5SDimitry Andric 3561*0b57cec5SDimitry Andric assert(NodeToMatch->use_empty() && 3562*0b57cec5SDimitry Andric "Didn't replace all uses of the node?"); 3563*0b57cec5SDimitry Andric CurDAG->RemoveDeadNode(NodeToMatch); 3564*0b57cec5SDimitry Andric 3565*0b57cec5SDimitry Andric return; 3566*0b57cec5SDimitry Andric } 3567*0b57cec5SDimitry Andric } 3568*0b57cec5SDimitry Andric 3569*0b57cec5SDimitry Andric // If the code reached this point, then the match failed. See if there is 3570*0b57cec5SDimitry Andric // another child to try in the current 'Scope', otherwise pop it until we 3571*0b57cec5SDimitry Andric // find a case to check. 3572*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Match failed at index " << CurrentOpcodeIndex 3573*0b57cec5SDimitry Andric << "\n"); 3574*0b57cec5SDimitry Andric ++NumDAGIselRetries; 3575*0b57cec5SDimitry Andric while (true) { 3576*0b57cec5SDimitry Andric if (MatchScopes.empty()) { 3577*0b57cec5SDimitry Andric CannotYetSelect(NodeToMatch); 3578*0b57cec5SDimitry Andric return; 3579*0b57cec5SDimitry Andric } 3580*0b57cec5SDimitry Andric 3581*0b57cec5SDimitry Andric // Restore the interpreter state back to the point where the scope was 3582*0b57cec5SDimitry Andric // formed. 3583*0b57cec5SDimitry Andric MatchScope &LastScope = MatchScopes.back(); 3584*0b57cec5SDimitry Andric RecordedNodes.resize(LastScope.NumRecordedNodes); 3585*0b57cec5SDimitry Andric NodeStack.clear(); 3586*0b57cec5SDimitry Andric NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end()); 3587*0b57cec5SDimitry Andric N = NodeStack.back(); 3588*0b57cec5SDimitry Andric 3589*0b57cec5SDimitry Andric if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size()) 3590*0b57cec5SDimitry Andric MatchedMemRefs.resize(LastScope.NumMatchedMemRefs); 3591*0b57cec5SDimitry Andric MatcherIndex = LastScope.FailIndex; 3592*0b57cec5SDimitry Andric 3593*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Continuing at " << MatcherIndex << "\n"); 3594*0b57cec5SDimitry Andric 3595*0b57cec5SDimitry Andric InputChain = LastScope.InputChain; 3596*0b57cec5SDimitry Andric InputGlue = LastScope.InputGlue; 3597*0b57cec5SDimitry Andric if (!LastScope.HasChainNodesMatched) 3598*0b57cec5SDimitry Andric ChainNodesMatched.clear(); 3599*0b57cec5SDimitry Andric 3600*0b57cec5SDimitry Andric // Check to see what the offset is at the new MatcherIndex. If it is zero 3601*0b57cec5SDimitry Andric // we have reached the end of this scope, otherwise we have another child 3602*0b57cec5SDimitry Andric // in the current scope to try. 3603*0b57cec5SDimitry Andric unsigned NumToSkip = MatcherTable[MatcherIndex++]; 3604*0b57cec5SDimitry Andric if (NumToSkip & 128) 3605*0b57cec5SDimitry Andric NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex); 3606*0b57cec5SDimitry Andric 3607*0b57cec5SDimitry Andric // If we have another child in this scope to match, update FailIndex and 3608*0b57cec5SDimitry Andric // try it. 3609*0b57cec5SDimitry Andric if (NumToSkip != 0) { 3610*0b57cec5SDimitry Andric LastScope.FailIndex = MatcherIndex+NumToSkip; 3611*0b57cec5SDimitry Andric break; 3612*0b57cec5SDimitry Andric } 3613*0b57cec5SDimitry Andric 3614*0b57cec5SDimitry Andric // End of this scope, pop it and try the next child in the containing 3615*0b57cec5SDimitry Andric // scope. 3616*0b57cec5SDimitry Andric MatchScopes.pop_back(); 3617*0b57cec5SDimitry Andric } 3618*0b57cec5SDimitry Andric } 3619*0b57cec5SDimitry Andric } 3620*0b57cec5SDimitry Andric 3621*0b57cec5SDimitry Andric bool SelectionDAGISel::isOrEquivalentToAdd(const SDNode *N) const { 3622*0b57cec5SDimitry Andric assert(N->getOpcode() == ISD::OR && "Unexpected opcode"); 3623*0b57cec5SDimitry Andric auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3624*0b57cec5SDimitry Andric if (!C) 3625*0b57cec5SDimitry Andric return false; 3626*0b57cec5SDimitry Andric 3627*0b57cec5SDimitry Andric // Detect when "or" is used to add an offset to a stack object. 3628*0b57cec5SDimitry Andric if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) { 3629*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF->getFrameInfo(); 3630*0b57cec5SDimitry Andric unsigned A = MFI.getObjectAlignment(FN->getIndex()); 3631*0b57cec5SDimitry Andric assert(isPowerOf2_32(A) && "Unexpected alignment"); 3632*0b57cec5SDimitry Andric int32_t Off = C->getSExtValue(); 3633*0b57cec5SDimitry Andric // If the alleged offset fits in the zero bits guaranteed by 3634*0b57cec5SDimitry Andric // the alignment, then this or is really an add. 3635*0b57cec5SDimitry Andric return (Off >= 0) && (((A - 1) & Off) == unsigned(Off)); 3636*0b57cec5SDimitry Andric } 3637*0b57cec5SDimitry Andric return false; 3638*0b57cec5SDimitry Andric } 3639*0b57cec5SDimitry Andric 3640*0b57cec5SDimitry Andric void SelectionDAGISel::CannotYetSelect(SDNode *N) { 3641*0b57cec5SDimitry Andric std::string msg; 3642*0b57cec5SDimitry Andric raw_string_ostream Msg(msg); 3643*0b57cec5SDimitry Andric Msg << "Cannot select: "; 3644*0b57cec5SDimitry Andric 3645*0b57cec5SDimitry Andric if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN && 3646*0b57cec5SDimitry Andric N->getOpcode() != ISD::INTRINSIC_WO_CHAIN && 3647*0b57cec5SDimitry Andric N->getOpcode() != ISD::INTRINSIC_VOID) { 3648*0b57cec5SDimitry Andric N->printrFull(Msg, CurDAG); 3649*0b57cec5SDimitry Andric Msg << "\nIn function: " << MF->getName(); 3650*0b57cec5SDimitry Andric } else { 3651*0b57cec5SDimitry Andric bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other; 3652*0b57cec5SDimitry Andric unsigned iid = 3653*0b57cec5SDimitry Andric cast<ConstantSDNode>(N->getOperand(HasInputChain))->getZExtValue(); 3654*0b57cec5SDimitry Andric if (iid < Intrinsic::num_intrinsics) 3655*0b57cec5SDimitry Andric Msg << "intrinsic %" << Intrinsic::getName((Intrinsic::ID)iid, None); 3656*0b57cec5SDimitry Andric else if (const TargetIntrinsicInfo *TII = TM.getIntrinsicInfo()) 3657*0b57cec5SDimitry Andric Msg << "target intrinsic %" << TII->getName(iid); 3658*0b57cec5SDimitry Andric else 3659*0b57cec5SDimitry Andric Msg << "unknown intrinsic #" << iid; 3660*0b57cec5SDimitry Andric } 3661*0b57cec5SDimitry Andric report_fatal_error(Msg.str()); 3662*0b57cec5SDimitry Andric } 3663*0b57cec5SDimitry Andric 3664*0b57cec5SDimitry Andric char SelectionDAGISel::ID = 0; 3665