1 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the SelectionDAGISel class, which is used as the common 10 // base class for SelectionDAG-based instruction selectors. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H 15 #define LLVM_CODEGEN_SELECTIONDAGISEL_H 16 17 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/CodeGen/SelectionDAG.h" 19 #include "llvm/IR/BasicBlock.h" 20 #include <memory> 21 22 namespace llvm { 23 class AAResults; 24 class AssumptionCache; 25 class TargetInstrInfo; 26 class TargetMachine; 27 class SelectionDAGBuilder; 28 class SDValue; 29 class MachineRegisterInfo; 30 class MachineFunction; 31 class OptimizationRemarkEmitter; 32 class TargetLowering; 33 class TargetLibraryInfo; 34 class FunctionLoweringInfo; 35 class SwiftErrorValueTracking; 36 class GCFunctionInfo; 37 class ScheduleDAGSDNodes; 38 39 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based 40 /// pattern-matching instruction selectors. 41 class SelectionDAGISel : public MachineFunctionPass { 42 public: 43 TargetMachine &TM; 44 const TargetLibraryInfo *LibInfo; 45 std::unique_ptr<FunctionLoweringInfo> FuncInfo; 46 SwiftErrorValueTracking *SwiftError; 47 MachineFunction *MF; 48 MachineRegisterInfo *RegInfo; 49 SelectionDAG *CurDAG; 50 std::unique_ptr<SelectionDAGBuilder> SDB; 51 AAResults *AA = nullptr; 52 AssumptionCache *AC = nullptr; 53 GCFunctionInfo *GFI = nullptr; 54 CodeGenOptLevel OptLevel; 55 const TargetInstrInfo *TII; 56 const TargetLowering *TLI; 57 bool FastISelFailed; 58 SmallPtrSet<const Instruction *, 4> ElidedArgCopyInstrs; 59 60 /// Current optimization remark emitter. 61 /// Used to report things like combines and FastISel failures. 62 std::unique_ptr<OptimizationRemarkEmitter> ORE; 63 64 /// True if the function currently processing is in the function printing list 65 /// (i.e. `-filter-print-funcs`). 66 /// This is primarily used by ISEL_DUMP, which spans in multiple member 67 /// functions. Storing the filter result here so that we only need to do the 68 /// filtering once. 69 bool MatchFilterFuncName = false; 70 71 explicit SelectionDAGISel(char &ID, TargetMachine &tm, 72 CodeGenOptLevel OL = CodeGenOptLevel::Default); 73 ~SelectionDAGISel() override; 74 75 const TargetLowering *getTargetLowering() const { return TLI; } 76 77 void getAnalysisUsage(AnalysisUsage &AU) const override; 78 79 bool runOnMachineFunction(MachineFunction &MF) override; 80 81 virtual void emitFunctionEntryCode() {} 82 83 /// PreprocessISelDAG - This hook allows targets to hack on the graph before 84 /// instruction selection starts. 85 virtual void PreprocessISelDAG() {} 86 87 /// PostprocessISelDAG() - This hook allows the target to hack on the graph 88 /// right after selection. 89 virtual void PostprocessISelDAG() {} 90 91 /// Main hook for targets to transform nodes into machine nodes. 92 virtual void Select(SDNode *N) = 0; 93 94 /// SelectInlineAsmMemoryOperand - Select the specified address as a target 95 /// addressing mode, according to the specified constraint. If this does 96 /// not match or is not implemented, return true. The resultant operands 97 /// (which will appear in the machine instruction) should be added to the 98 /// OutOps vector. 99 virtual bool 100 SelectInlineAsmMemoryOperand(const SDValue &Op, 101 InlineAsm::ConstraintCode ConstraintID, 102 std::vector<SDValue> &OutOps) { 103 return true; 104 } 105 106 /// IsProfitableToFold - Returns true if it's profitable to fold the specific 107 /// operand node N of U during instruction selection that starts at Root. 108 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const; 109 110 /// IsLegalToFold - Returns true if the specific operand node N of 111 /// U can be folded during instruction selection that starts at Root. 112 /// FIXME: This is a static member function because the MSP430/X86 113 /// targets, which uses it during isel. This could become a proper member. 114 static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, 115 CodeGenOptLevel OptLevel, 116 bool IgnoreChains = false); 117 118 static void InvalidateNodeId(SDNode *N); 119 static int getUninvalidatedNodeId(SDNode *N); 120 121 static void EnforceNodeIdInvariant(SDNode *N); 122 123 // Opcodes used by the DAG state machine: 124 enum BuiltinOpcodes { 125 OPC_Scope, 126 OPC_RecordNode, 127 OPC_RecordChild0, 128 OPC_RecordChild1, 129 OPC_RecordChild2, 130 OPC_RecordChild3, 131 OPC_RecordChild4, 132 OPC_RecordChild5, 133 OPC_RecordChild6, 134 OPC_RecordChild7, 135 OPC_RecordMemRef, 136 OPC_CaptureGlueInput, 137 OPC_MoveChild, 138 OPC_MoveChild0, 139 OPC_MoveChild1, 140 OPC_MoveChild2, 141 OPC_MoveChild3, 142 OPC_MoveChild4, 143 OPC_MoveChild5, 144 OPC_MoveChild6, 145 OPC_MoveChild7, 146 OPC_MoveSibling, 147 OPC_MoveSibling0, 148 OPC_MoveSibling1, 149 OPC_MoveSibling2, 150 OPC_MoveSibling3, 151 OPC_MoveSibling4, 152 OPC_MoveSibling5, 153 OPC_MoveSibling6, 154 OPC_MoveSibling7, 155 OPC_MoveParent, 156 OPC_CheckSame, 157 OPC_CheckChild0Same, 158 OPC_CheckChild1Same, 159 OPC_CheckChild2Same, 160 OPC_CheckChild3Same, 161 OPC_CheckPatternPredicate, 162 OPC_CheckPatternPredicate2, 163 OPC_CheckPredicate, 164 OPC_CheckPredicateWithOperands, 165 OPC_CheckOpcode, 166 OPC_SwitchOpcode, 167 OPC_CheckType, 168 // Space-optimized forms that implicitly encode VT. 169 OPC_CheckTypeI32, 170 OPC_CheckTypeI64, 171 OPC_CheckTypeRes, 172 OPC_SwitchType, 173 OPC_CheckChild0Type, 174 OPC_CheckChild1Type, 175 OPC_CheckChild2Type, 176 OPC_CheckChild3Type, 177 OPC_CheckChild4Type, 178 OPC_CheckChild5Type, 179 OPC_CheckChild6Type, 180 OPC_CheckChild7Type, 181 182 OPC_CheckChild0TypeI32, 183 OPC_CheckChild1TypeI32, 184 OPC_CheckChild2TypeI32, 185 OPC_CheckChild3TypeI32, 186 OPC_CheckChild4TypeI32, 187 OPC_CheckChild5TypeI32, 188 OPC_CheckChild6TypeI32, 189 OPC_CheckChild7TypeI32, 190 191 OPC_CheckChild0TypeI64, 192 OPC_CheckChild1TypeI64, 193 OPC_CheckChild2TypeI64, 194 OPC_CheckChild3TypeI64, 195 OPC_CheckChild4TypeI64, 196 OPC_CheckChild5TypeI64, 197 OPC_CheckChild6TypeI64, 198 OPC_CheckChild7TypeI64, 199 200 OPC_CheckInteger, 201 OPC_CheckChild0Integer, 202 OPC_CheckChild1Integer, 203 OPC_CheckChild2Integer, 204 OPC_CheckChild3Integer, 205 OPC_CheckChild4Integer, 206 OPC_CheckCondCode, 207 OPC_CheckChild2CondCode, 208 OPC_CheckValueType, 209 OPC_CheckComplexPat, 210 OPC_CheckAndImm, 211 OPC_CheckOrImm, 212 OPC_CheckImmAllOnesV, 213 OPC_CheckImmAllZerosV, 214 OPC_CheckFoldableChainNode, 215 216 OPC_EmitInteger, 217 // Space-optimized forms that implicitly encode integer VT. 218 OPC_EmitInteger8, 219 OPC_EmitInteger16, 220 OPC_EmitInteger32, 221 OPC_EmitInteger64, 222 OPC_EmitStringInteger, 223 // Space-optimized forms that implicitly encode integer VT. 224 OPC_EmitStringInteger32, 225 OPC_EmitRegister, 226 OPC_EmitRegister2, 227 OPC_EmitConvertToTarget, 228 OPC_EmitConvertToTarget0, 229 OPC_EmitConvertToTarget1, 230 OPC_EmitConvertToTarget2, 231 OPC_EmitConvertToTarget3, 232 OPC_EmitConvertToTarget4, 233 OPC_EmitConvertToTarget5, 234 OPC_EmitConvertToTarget6, 235 OPC_EmitConvertToTarget7, 236 OPC_EmitMergeInputChains, 237 OPC_EmitMergeInputChains1_0, 238 OPC_EmitMergeInputChains1_1, 239 OPC_EmitMergeInputChains1_2, 240 OPC_EmitCopyToReg, 241 OPC_EmitCopyToReg0, 242 OPC_EmitCopyToReg1, 243 OPC_EmitCopyToReg2, 244 OPC_EmitCopyToReg3, 245 OPC_EmitCopyToReg4, 246 OPC_EmitCopyToReg5, 247 OPC_EmitCopyToReg6, 248 OPC_EmitCopyToReg7, 249 OPC_EmitCopyToRegTwoByte, 250 OPC_EmitNodeXForm, 251 OPC_EmitNode, 252 // Space-optimized forms that implicitly encode number of result VTs. 253 OPC_EmitNode0, 254 OPC_EmitNode1, 255 OPC_EmitNode2, 256 // Space-optimized forms that implicitly encode EmitNodeInfo. 257 OPC_EmitNode0None, 258 OPC_EmitNode1None, 259 OPC_EmitNode2None, 260 OPC_EmitNode0Chain, 261 OPC_EmitNode1Chain, 262 OPC_EmitNode2Chain, 263 OPC_MorphNodeTo, 264 // Space-optimized forms that implicitly encode number of result VTs. 265 OPC_MorphNodeTo0, 266 OPC_MorphNodeTo1, 267 OPC_MorphNodeTo2, 268 // Space-optimized forms that implicitly encode EmitNodeInfo. 269 OPC_MorphNodeTo0None, 270 OPC_MorphNodeTo1None, 271 OPC_MorphNodeTo2None, 272 OPC_MorphNodeTo0Chain, 273 OPC_MorphNodeTo1Chain, 274 OPC_MorphNodeTo2Chain, 275 OPC_MorphNodeTo0GlueInput, 276 OPC_MorphNodeTo1GlueInput, 277 OPC_MorphNodeTo2GlueInput, 278 OPC_MorphNodeTo0GlueOutput, 279 OPC_MorphNodeTo1GlueOutput, 280 OPC_MorphNodeTo2GlueOutput, 281 OPC_CompleteMatch, 282 // Contains offset in table for pattern being selected 283 OPC_Coverage 284 }; 285 286 enum { 287 OPFL_None = 0, // Node has no chain or glue input and isn't variadic. 288 OPFL_Chain = 1, // Node has a chain input. 289 OPFL_GlueInput = 2, // Node has a glue input. 290 OPFL_GlueOutput = 4, // Node has a glue output. 291 OPFL_MemRefs = 8, // Node gets accumulated MemRefs. 292 OPFL_Variadic0 = 1<<4, // Node is variadic, root has 0 fixed inputs. 293 OPFL_Variadic1 = 2<<4, // Node is variadic, root has 1 fixed inputs. 294 OPFL_Variadic2 = 3<<4, // Node is variadic, root has 2 fixed inputs. 295 OPFL_Variadic3 = 4<<4, // Node is variadic, root has 3 fixed inputs. 296 OPFL_Variadic4 = 5<<4, // Node is variadic, root has 4 fixed inputs. 297 OPFL_Variadic5 = 6<<4, // Node is variadic, root has 5 fixed inputs. 298 OPFL_Variadic6 = 7<<4, // Node is variadic, root has 6 fixed inputs. 299 300 OPFL_VariadicInfo = OPFL_Variadic6 301 }; 302 303 /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the 304 /// number of fixed arity values that should be skipped when copying from the 305 /// root. 306 static inline int getNumFixedFromVariadicInfo(unsigned Flags) { 307 return ((Flags&OPFL_VariadicInfo) >> 4)-1; 308 } 309 310 311 protected: 312 /// DAGSize - Size of DAG being instruction selected. 313 /// 314 unsigned DAGSize = 0; 315 316 /// ReplaceUses - replace all uses of the old node F with the use 317 /// of the new node T. 318 void ReplaceUses(SDValue F, SDValue T) { 319 CurDAG->ReplaceAllUsesOfValueWith(F, T); 320 EnforceNodeIdInvariant(T.getNode()); 321 } 322 323 /// ReplaceUses - replace all uses of the old nodes F with the use 324 /// of the new nodes T. 325 void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) { 326 CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num); 327 for (unsigned i = 0; i < Num; ++i) 328 EnforceNodeIdInvariant(T[i].getNode()); 329 } 330 331 /// ReplaceUses - replace all uses of the old node F with the use 332 /// of the new node T. 333 void ReplaceUses(SDNode *F, SDNode *T) { 334 CurDAG->ReplaceAllUsesWith(F, T); 335 EnforceNodeIdInvariant(T); 336 } 337 338 /// Replace all uses of \c F with \c T, then remove \c F from the DAG. 339 void ReplaceNode(SDNode *F, SDNode *T) { 340 CurDAG->ReplaceAllUsesWith(F, T); 341 EnforceNodeIdInvariant(T); 342 CurDAG->RemoveDeadNode(F); 343 } 344 345 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated 346 /// by tblgen. Others should not call it. 347 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops, 348 const SDLoc &DL); 349 350 /// getPatternForIndex - Patterns selected by tablegen during ISEL 351 virtual StringRef getPatternForIndex(unsigned index) { 352 llvm_unreachable("Tblgen should generate the implementation of this!"); 353 } 354 355 /// getIncludePathForIndex - get the td source location of pattern instantiation 356 virtual StringRef getIncludePathForIndex(unsigned index) { 357 llvm_unreachable("Tblgen should generate the implementation of this!"); 358 } 359 360 bool shouldOptForSize(const MachineFunction *MF) const { 361 return CurDAG->shouldOptForSize(); 362 } 363 364 public: 365 // Calls to these predicates are generated by tblgen. 366 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS, 367 int64_t DesiredMaskS) const; 368 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS, 369 int64_t DesiredMaskS) const; 370 371 372 /// CheckPatternPredicate - This function is generated by tblgen in the 373 /// target. It runs the specified pattern predicate and returns true if it 374 /// succeeds or false if it fails. The number is a private implementation 375 /// detail to the code tblgen produces. 376 virtual bool CheckPatternPredicate(unsigned PredNo) const { 377 llvm_unreachable("Tblgen should generate the implementation of this!"); 378 } 379 380 /// CheckNodePredicate - This function is generated by tblgen in the target. 381 /// It runs node predicate number PredNo and returns true if it succeeds or 382 /// false if it fails. The number is a private implementation 383 /// detail to the code tblgen produces. 384 virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const { 385 llvm_unreachable("Tblgen should generate the implementation of this!"); 386 } 387 388 /// CheckNodePredicateWithOperands - This function is generated by tblgen in 389 /// the target. 390 /// It runs node predicate number PredNo and returns true if it succeeds or 391 /// false if it fails. The number is a private implementation detail to the 392 /// code tblgen produces. 393 virtual bool CheckNodePredicateWithOperands( 394 SDNode *N, unsigned PredNo, 395 const SmallVectorImpl<SDValue> &Operands) const { 396 llvm_unreachable("Tblgen should generate the implementation of this!"); 397 } 398 399 virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N, 400 unsigned PatternNo, 401 SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) { 402 llvm_unreachable("Tblgen should generate the implementation of this!"); 403 } 404 405 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) { 406 llvm_unreachable("Tblgen should generate this!"); 407 } 408 409 void SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable, 410 unsigned TableSize); 411 412 /// Return true if complex patterns for this target can mutate the 413 /// DAG. 414 virtual bool ComplexPatternFuncMutatesDAG() const { 415 return false; 416 } 417 418 /// Return whether the node may raise an FP exception. 419 bool mayRaiseFPException(SDNode *Node) const; 420 421 bool isOrEquivalentToAdd(const SDNode *N) const; 422 423 private: 424 425 // Calls to these functions are generated by tblgen. 426 void Select_INLINEASM(SDNode *N); 427 void Select_READ_REGISTER(SDNode *Op); 428 void Select_WRITE_REGISTER(SDNode *Op); 429 void Select_UNDEF(SDNode *N); 430 void CannotYetSelect(SDNode *N); 431 432 void Select_FREEZE(SDNode *N); 433 void Select_ARITH_FENCE(SDNode *N); 434 void Select_MEMBARRIER(SDNode *N); 435 436 void pushStackMapLiveVariable(SmallVectorImpl<SDValue> &Ops, SDValue Operand, 437 SDLoc DL); 438 void Select_STACKMAP(SDNode *N); 439 void Select_PATCHPOINT(SDNode *N); 440 441 void Select_JUMP_TABLE_DEBUG_INFO(SDNode *N); 442 443 private: 444 void DoInstructionSelection(); 445 SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList, 446 ArrayRef<SDValue> Ops, unsigned EmitNodeInfo); 447 448 /// Prepares the landing pad to take incoming values or do other EH 449 /// personality specific tasks. Returns true if the block should be 450 /// instruction selected, false if no code should be emitted for it. 451 bool PrepareEHLandingPad(); 452 453 // Mark and Report IPToState for each Block under AsynchEH 454 void reportIPToStateForBlocks(MachineFunction *Fn); 455 456 /// Perform instruction selection on all basic blocks in the function. 457 void SelectAllBasicBlocks(const Function &Fn); 458 459 /// Perform instruction selection on a single basic block, for 460 /// instructions between \p Begin and \p End. \p HadTailCall will be set 461 /// to true if a call in the block was translated as a tail call. 462 void SelectBasicBlock(BasicBlock::const_iterator Begin, 463 BasicBlock::const_iterator End, 464 bool &HadTailCall); 465 void FinishBasicBlock(); 466 467 void CodeGenAndEmitDAG(); 468 469 /// Generate instructions for lowering the incoming arguments of the 470 /// given function. 471 void LowerArguments(const Function &F); 472 473 void ComputeLiveOutVRegInfo(); 474 475 /// Create the scheduler. If a specific scheduler was specified 476 /// via the SchedulerRegistry, use it, otherwise select the 477 /// one preferred by the target. 478 /// 479 ScheduleDAGSDNodes *CreateScheduler(); 480 481 /// OpcodeOffset - This is a cache used to dispatch efficiently into isel 482 /// state machines that start with a OPC_SwitchOpcode node. 483 std::vector<unsigned> OpcodeOffset; 484 485 void UpdateChains(SDNode *NodeToMatch, SDValue InputChain, 486 SmallVectorImpl<SDNode *> &ChainNodesMatched, 487 bool isMorphNodeTo); 488 }; 489 490 } 491 492 #endif /* LLVM_CODEGEN_SELECTIONDAGISEL_H */ 493