File indexing completed on 2026-05-10 08:43:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H
0015 #define LLVM_CODEGEN_SELECTIONDAGISEL_H
0016
0017 #include "llvm/Analysis/AliasAnalysis.h"
0018 #include "llvm/CodeGen/MachineFunctionPass.h"
0019 #include "llvm/CodeGen/MachinePassManager.h"
0020 #include "llvm/CodeGen/SelectionDAG.h"
0021 #include "llvm/IR/BasicBlock.h"
0022 #include <memory>
0023
0024 namespace llvm {
0025 class AAResults;
0026 class AssumptionCache;
0027 class TargetInstrInfo;
0028 class TargetMachine;
0029 class SSPLayoutInfo;
0030 class SelectionDAGBuilder;
0031 class SDValue;
0032 class MachineRegisterInfo;
0033 class MachineFunction;
0034 class OptimizationRemarkEmitter;
0035 class TargetLowering;
0036 class TargetLibraryInfo;
0037 class TargetTransformInfo;
0038 class FunctionLoweringInfo;
0039 class SwiftErrorValueTracking;
0040 class GCFunctionInfo;
0041 class ScheduleDAGSDNodes;
0042
0043
0044
0045 class SelectionDAGISel {
0046 public:
0047 TargetMachine &TM;
0048 const TargetLibraryInfo *LibInfo;
0049 std::unique_ptr<FunctionLoweringInfo> FuncInfo;
0050 SwiftErrorValueTracking *SwiftError;
0051 MachineFunction *MF;
0052 MachineModuleInfo *MMI;
0053 MachineRegisterInfo *RegInfo;
0054 SelectionDAG *CurDAG;
0055 std::unique_ptr<SelectionDAGBuilder> SDB;
0056 mutable std::optional<BatchAAResults> BatchAA;
0057 AssumptionCache *AC = nullptr;
0058 GCFunctionInfo *GFI = nullptr;
0059 SSPLayoutInfo *SP = nullptr;
0060 #if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
0061 TargetTransformInfo *TTI = nullptr;
0062 #endif
0063 CodeGenOptLevel OptLevel;
0064 const TargetInstrInfo *TII;
0065 const TargetLowering *TLI;
0066 bool FastISelFailed;
0067 SmallPtrSet<const Instruction *, 4> ElidedArgCopyInstrs;
0068
0069
0070
0071 std::unique_ptr<OptimizationRemarkEmitter> ORE;
0072
0073
0074
0075
0076
0077
0078 bool MatchFilterFuncName = false;
0079 StringRef FuncName;
0080
0081 explicit SelectionDAGISel(TargetMachine &tm,
0082 CodeGenOptLevel OL = CodeGenOptLevel::Default);
0083 virtual ~SelectionDAGISel();
0084
0085
0086 BatchAAResults *getBatchAA() const {
0087 if (BatchAA.has_value())
0088 return &BatchAA.value();
0089 return nullptr;
0090 }
0091
0092 const TargetLowering *getTargetLowering() const { return TLI; }
0093
0094 void initializeAnalysisResults(MachineFunctionAnalysisManager &MFAM);
0095 void initializeAnalysisResults(MachineFunctionPass &MFP);
0096
0097 virtual bool runOnMachineFunction(MachineFunction &mf);
0098
0099 virtual void emitFunctionEntryCode() {}
0100
0101
0102
0103 virtual void PreprocessISelDAG() {}
0104
0105
0106
0107 virtual void PostprocessISelDAG() {}
0108
0109
0110 virtual void Select(SDNode *N) = 0;
0111
0112
0113
0114
0115
0116
0117 virtual bool
0118 SelectInlineAsmMemoryOperand(const SDValue &Op,
0119 InlineAsm::ConstraintCode ConstraintID,
0120 std::vector<SDValue> &OutOps) {
0121 return true;
0122 }
0123
0124
0125
0126 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
0127
0128
0129
0130
0131
0132 static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
0133 CodeGenOptLevel OptLevel,
0134 bool IgnoreChains = false);
0135
0136 static void InvalidateNodeId(SDNode *N);
0137 static int getUninvalidatedNodeId(SDNode *N);
0138
0139 static void EnforceNodeIdInvariant(SDNode *N);
0140
0141
0142 enum BuiltinOpcodes {
0143 OPC_Scope,
0144 OPC_RecordNode,
0145 OPC_RecordChild0,
0146 OPC_RecordChild1,
0147 OPC_RecordChild2,
0148 OPC_RecordChild3,
0149 OPC_RecordChild4,
0150 OPC_RecordChild5,
0151 OPC_RecordChild6,
0152 OPC_RecordChild7,
0153 OPC_RecordMemRef,
0154 OPC_CaptureGlueInput,
0155 OPC_MoveChild,
0156 OPC_MoveChild0,
0157 OPC_MoveChild1,
0158 OPC_MoveChild2,
0159 OPC_MoveChild3,
0160 OPC_MoveChild4,
0161 OPC_MoveChild5,
0162 OPC_MoveChild6,
0163 OPC_MoveChild7,
0164 OPC_MoveSibling,
0165 OPC_MoveSibling0,
0166 OPC_MoveSibling1,
0167 OPC_MoveSibling2,
0168 OPC_MoveSibling3,
0169 OPC_MoveSibling4,
0170 OPC_MoveSibling5,
0171 OPC_MoveSibling6,
0172 OPC_MoveSibling7,
0173 OPC_MoveParent,
0174 OPC_CheckSame,
0175 OPC_CheckChild0Same,
0176 OPC_CheckChild1Same,
0177 OPC_CheckChild2Same,
0178 OPC_CheckChild3Same,
0179 OPC_CheckPatternPredicate,
0180 OPC_CheckPatternPredicate0,
0181 OPC_CheckPatternPredicate1,
0182 OPC_CheckPatternPredicate2,
0183 OPC_CheckPatternPredicate3,
0184 OPC_CheckPatternPredicate4,
0185 OPC_CheckPatternPredicate5,
0186 OPC_CheckPatternPredicate6,
0187 OPC_CheckPatternPredicate7,
0188 OPC_CheckPatternPredicateTwoByte,
0189 OPC_CheckPredicate,
0190 OPC_CheckPredicate0,
0191 OPC_CheckPredicate1,
0192 OPC_CheckPredicate2,
0193 OPC_CheckPredicate3,
0194 OPC_CheckPredicate4,
0195 OPC_CheckPredicate5,
0196 OPC_CheckPredicate6,
0197 OPC_CheckPredicate7,
0198 OPC_CheckPredicateWithOperands,
0199 OPC_CheckOpcode,
0200 OPC_SwitchOpcode,
0201 OPC_CheckType,
0202
0203 OPC_CheckTypeI32,
0204 OPC_CheckTypeI64,
0205 OPC_CheckTypeRes,
0206 OPC_SwitchType,
0207 OPC_CheckChild0Type,
0208 OPC_CheckChild1Type,
0209 OPC_CheckChild2Type,
0210 OPC_CheckChild3Type,
0211 OPC_CheckChild4Type,
0212 OPC_CheckChild5Type,
0213 OPC_CheckChild6Type,
0214 OPC_CheckChild7Type,
0215
0216 OPC_CheckChild0TypeI32,
0217 OPC_CheckChild1TypeI32,
0218 OPC_CheckChild2TypeI32,
0219 OPC_CheckChild3TypeI32,
0220 OPC_CheckChild4TypeI32,
0221 OPC_CheckChild5TypeI32,
0222 OPC_CheckChild6TypeI32,
0223 OPC_CheckChild7TypeI32,
0224
0225 OPC_CheckChild0TypeI64,
0226 OPC_CheckChild1TypeI64,
0227 OPC_CheckChild2TypeI64,
0228 OPC_CheckChild3TypeI64,
0229 OPC_CheckChild4TypeI64,
0230 OPC_CheckChild5TypeI64,
0231 OPC_CheckChild6TypeI64,
0232 OPC_CheckChild7TypeI64,
0233
0234 OPC_CheckInteger,
0235 OPC_CheckChild0Integer,
0236 OPC_CheckChild1Integer,
0237 OPC_CheckChild2Integer,
0238 OPC_CheckChild3Integer,
0239 OPC_CheckChild4Integer,
0240 OPC_CheckCondCode,
0241 OPC_CheckChild2CondCode,
0242 OPC_CheckValueType,
0243 OPC_CheckComplexPat,
0244 OPC_CheckComplexPat0,
0245 OPC_CheckComplexPat1,
0246 OPC_CheckComplexPat2,
0247 OPC_CheckComplexPat3,
0248 OPC_CheckComplexPat4,
0249 OPC_CheckComplexPat5,
0250 OPC_CheckComplexPat6,
0251 OPC_CheckComplexPat7,
0252 OPC_CheckAndImm,
0253 OPC_CheckOrImm,
0254 OPC_CheckImmAllOnesV,
0255 OPC_CheckImmAllZerosV,
0256 OPC_CheckFoldableChainNode,
0257
0258 OPC_EmitInteger,
0259
0260 OPC_EmitInteger8,
0261 OPC_EmitInteger16,
0262 OPC_EmitInteger32,
0263 OPC_EmitInteger64,
0264 OPC_EmitStringInteger,
0265
0266 OPC_EmitStringInteger32,
0267 OPC_EmitRegister,
0268 OPC_EmitRegisterI32,
0269 OPC_EmitRegisterI64,
0270 OPC_EmitRegister2,
0271 OPC_EmitConvertToTarget,
0272 OPC_EmitConvertToTarget0,
0273 OPC_EmitConvertToTarget1,
0274 OPC_EmitConvertToTarget2,
0275 OPC_EmitConvertToTarget3,
0276 OPC_EmitConvertToTarget4,
0277 OPC_EmitConvertToTarget5,
0278 OPC_EmitConvertToTarget6,
0279 OPC_EmitConvertToTarget7,
0280 OPC_EmitMergeInputChains,
0281 OPC_EmitMergeInputChains1_0,
0282 OPC_EmitMergeInputChains1_1,
0283 OPC_EmitMergeInputChains1_2,
0284 OPC_EmitCopyToReg,
0285 OPC_EmitCopyToReg0,
0286 OPC_EmitCopyToReg1,
0287 OPC_EmitCopyToReg2,
0288 OPC_EmitCopyToReg3,
0289 OPC_EmitCopyToReg4,
0290 OPC_EmitCopyToReg5,
0291 OPC_EmitCopyToReg6,
0292 OPC_EmitCopyToReg7,
0293 OPC_EmitCopyToRegTwoByte,
0294 OPC_EmitNodeXForm,
0295 OPC_EmitNode,
0296
0297 OPC_EmitNode0,
0298 OPC_EmitNode1,
0299 OPC_EmitNode2,
0300
0301 OPC_EmitNode0None,
0302 OPC_EmitNode1None,
0303 OPC_EmitNode2None,
0304 OPC_EmitNode0Chain,
0305 OPC_EmitNode1Chain,
0306 OPC_EmitNode2Chain,
0307 OPC_MorphNodeTo,
0308
0309 OPC_MorphNodeTo0,
0310 OPC_MorphNodeTo1,
0311 OPC_MorphNodeTo2,
0312
0313 OPC_MorphNodeTo0None,
0314 OPC_MorphNodeTo1None,
0315 OPC_MorphNodeTo2None,
0316 OPC_MorphNodeTo0Chain,
0317 OPC_MorphNodeTo1Chain,
0318 OPC_MorphNodeTo2Chain,
0319 OPC_MorphNodeTo0GlueInput,
0320 OPC_MorphNodeTo1GlueInput,
0321 OPC_MorphNodeTo2GlueInput,
0322 OPC_MorphNodeTo0GlueOutput,
0323 OPC_MorphNodeTo1GlueOutput,
0324 OPC_MorphNodeTo2GlueOutput,
0325 OPC_CompleteMatch,
0326
0327 OPC_Coverage
0328 };
0329
0330 enum {
0331 OPFL_None = 0,
0332 OPFL_Chain = 1,
0333 OPFL_GlueInput = 2,
0334 OPFL_GlueOutput = 4,
0335 OPFL_MemRefs = 8,
0336 OPFL_Variadic0 = 1<<4,
0337 OPFL_Variadic1 = 2<<4,
0338 OPFL_Variadic2 = 3<<4,
0339 OPFL_Variadic3 = 4<<4,
0340 OPFL_Variadic4 = 5<<4,
0341 OPFL_Variadic5 = 6<<4,
0342 OPFL_Variadic6 = 7<<4,
0343
0344 OPFL_VariadicInfo = OPFL_Variadic6
0345 };
0346
0347
0348
0349
0350 static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
0351 return ((Flags&OPFL_VariadicInfo) >> 4)-1;
0352 }
0353
0354
0355 protected:
0356
0357
0358 unsigned DAGSize = 0;
0359
0360
0361
0362 void ReplaceUses(SDValue F, SDValue T) {
0363 CurDAG->ReplaceAllUsesOfValueWith(F, T);
0364 EnforceNodeIdInvariant(T.getNode());
0365 }
0366
0367
0368
0369 void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
0370 CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num);
0371 for (unsigned i = 0; i < Num; ++i)
0372 EnforceNodeIdInvariant(T[i].getNode());
0373 }
0374
0375
0376
0377 void ReplaceUses(SDNode *F, SDNode *T) {
0378 CurDAG->ReplaceAllUsesWith(F, T);
0379 EnforceNodeIdInvariant(T);
0380 }
0381
0382
0383 void ReplaceNode(SDNode *F, SDNode *T) {
0384 CurDAG->ReplaceAllUsesWith(F, T);
0385 EnforceNodeIdInvariant(T);
0386 CurDAG->RemoveDeadNode(F);
0387 }
0388
0389
0390
0391 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops,
0392 const SDLoc &DL);
0393
0394
0395 virtual StringRef getPatternForIndex(unsigned index) {
0396 llvm_unreachable("Tblgen should generate the implementation of this!");
0397 }
0398
0399
0400 virtual StringRef getIncludePathForIndex(unsigned index) {
0401 llvm_unreachable("Tblgen should generate the implementation of this!");
0402 }
0403
0404 bool shouldOptForSize(const MachineFunction *MF) const {
0405 return CurDAG->shouldOptForSize();
0406 }
0407
0408 public:
0409
0410 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
0411 int64_t DesiredMaskS) const;
0412 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
0413 int64_t DesiredMaskS) const;
0414
0415
0416
0417
0418
0419
0420 virtual bool CheckPatternPredicate(unsigned PredNo) const {
0421 llvm_unreachable("Tblgen should generate the implementation of this!");
0422 }
0423
0424
0425
0426
0427
0428 virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
0429 llvm_unreachable("Tblgen should generate the implementation of this!");
0430 }
0431
0432
0433
0434
0435
0436
0437 virtual bool CheckNodePredicateWithOperands(
0438 SDNode *N, unsigned PredNo,
0439 const SmallVectorImpl<SDValue> &Operands) const {
0440 llvm_unreachable("Tblgen should generate the implementation of this!");
0441 }
0442
0443 virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
0444 unsigned PatternNo,
0445 SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {
0446 llvm_unreachable("Tblgen should generate the implementation of this!");
0447 }
0448
0449 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
0450 llvm_unreachable("Tblgen should generate this!");
0451 }
0452
0453 void SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
0454 unsigned TableSize);
0455
0456
0457
0458 virtual bool ComplexPatternFuncMutatesDAG() const {
0459 return false;
0460 }
0461
0462
0463 bool mayRaiseFPException(SDNode *Node) const;
0464
0465 bool isOrEquivalentToAdd(const SDNode *N) const;
0466
0467 private:
0468
0469
0470 void Select_INLINEASM(SDNode *N);
0471 void Select_READ_REGISTER(SDNode *Op);
0472 void Select_WRITE_REGISTER(SDNode *Op);
0473 void Select_UNDEF(SDNode *N);
0474 void Select_FAKE_USE(SDNode *N);
0475 void CannotYetSelect(SDNode *N);
0476
0477 void Select_FREEZE(SDNode *N);
0478 void Select_ARITH_FENCE(SDNode *N);
0479 void Select_MEMBARRIER(SDNode *N);
0480
0481 void Select_CONVERGENCECTRL_ANCHOR(SDNode *N);
0482 void Select_CONVERGENCECTRL_ENTRY(SDNode *N);
0483 void Select_CONVERGENCECTRL_LOOP(SDNode *N);
0484
0485 void pushStackMapLiveVariable(SmallVectorImpl<SDValue> &Ops, SDValue Operand,
0486 SDLoc DL);
0487 void Select_STACKMAP(SDNode *N);
0488 void Select_PATCHPOINT(SDNode *N);
0489
0490 void Select_JUMP_TABLE_DEBUG_INFO(SDNode *N);
0491
0492 private:
0493 void DoInstructionSelection();
0494 SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
0495 ArrayRef<SDValue> Ops, unsigned EmitNodeInfo);
0496
0497
0498
0499
0500 bool PrepareEHLandingPad();
0501
0502
0503 void reportIPToStateForBlocks(MachineFunction *Fn);
0504
0505
0506 void SelectAllBasicBlocks(const Function &Fn);
0507
0508
0509
0510
0511 void SelectBasicBlock(BasicBlock::const_iterator Begin,
0512 BasicBlock::const_iterator End,
0513 bool &HadTailCall);
0514 void FinishBasicBlock();
0515
0516 void CodeGenAndEmitDAG();
0517
0518
0519
0520 void LowerArguments(const Function &F);
0521
0522 void ComputeLiveOutVRegInfo();
0523
0524
0525
0526
0527
0528 ScheduleDAGSDNodes *CreateScheduler();
0529
0530
0531
0532 std::vector<unsigned> OpcodeOffset;
0533
0534 void UpdateChains(SDNode *NodeToMatch, SDValue InputChain,
0535 SmallVectorImpl<SDNode *> &ChainNodesMatched,
0536 bool isMorphNodeTo);
0537 };
0538
0539 class SelectionDAGISelLegacy : public MachineFunctionPass {
0540 std::unique_ptr<SelectionDAGISel> Selector;
0541
0542 public:
0543 SelectionDAGISelLegacy(char &ID, std::unique_ptr<SelectionDAGISel> S);
0544
0545 ~SelectionDAGISelLegacy() override = default;
0546
0547 void getAnalysisUsage(AnalysisUsage &AU) const override;
0548
0549 bool runOnMachineFunction(MachineFunction &MF) override;
0550 };
0551
0552 class SelectionDAGISelPass : public PassInfoMixin<SelectionDAGISelPass> {
0553 std::unique_ptr<SelectionDAGISel> Selector;
0554
0555 protected:
0556 SelectionDAGISelPass(std::unique_ptr<SelectionDAGISel> Selector)
0557 : Selector(std::move(Selector)) {}
0558
0559 public:
0560 PreservedAnalyses run(MachineFunction &MF,
0561 MachineFunctionAnalysisManager &MFAM);
0562 static bool isRequired() { return true; }
0563 };
0564 }
0565
0566 #endif