17330f729Sjoerg //===- SelectionDAGBuilder.h - Selection-DAG building -----------*- C++ -*-===// 27330f729Sjoerg // 37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information. 57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67330f729Sjoerg // 77330f729Sjoerg //===----------------------------------------------------------------------===// 87330f729Sjoerg // 97330f729Sjoerg // This implements routines for translating from LLVM IR into SelectionDAG IR. 107330f729Sjoerg // 117330f729Sjoerg //===----------------------------------------------------------------------===// 127330f729Sjoerg 137330f729Sjoerg #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 147330f729Sjoerg #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 157330f729Sjoerg 167330f729Sjoerg #include "StatepointLowering.h" 177330f729Sjoerg #include "llvm/ADT/ArrayRef.h" 187330f729Sjoerg #include "llvm/ADT/DenseMap.h" 197330f729Sjoerg #include "llvm/ADT/MapVector.h" 207330f729Sjoerg #include "llvm/ADT/SmallVector.h" 217330f729Sjoerg #include "llvm/CodeGen/ISDOpcodes.h" 227330f729Sjoerg #include "llvm/CodeGen/SelectionDAGNodes.h" 237330f729Sjoerg #include "llvm/CodeGen/SwitchLoweringUtils.h" 247330f729Sjoerg #include "llvm/CodeGen/TargetLowering.h" 257330f729Sjoerg #include "llvm/CodeGen/ValueTypes.h" 267330f729Sjoerg #include "llvm/IR/DebugLoc.h" 277330f729Sjoerg #include "llvm/IR/Instruction.h" 287330f729Sjoerg #include "llvm/Support/BranchProbability.h" 297330f729Sjoerg #include "llvm/Support/CodeGen.h" 307330f729Sjoerg #include "llvm/Support/ErrorHandling.h" 317330f729Sjoerg #include "llvm/Support/MachineValueType.h" 327330f729Sjoerg #include <algorithm> 337330f729Sjoerg #include <cassert> 347330f729Sjoerg #include <cstdint> 357330f729Sjoerg #include <utility> 367330f729Sjoerg #include <vector> 377330f729Sjoerg 387330f729Sjoerg namespace llvm { 397330f729Sjoerg 40*82d56013Sjoerg class AAResults; 417330f729Sjoerg class AllocaInst; 427330f729Sjoerg class AtomicCmpXchgInst; 437330f729Sjoerg class AtomicRMWInst; 447330f729Sjoerg class BasicBlock; 457330f729Sjoerg class BranchInst; 467330f729Sjoerg class CallInst; 477330f729Sjoerg class CallBrInst; 487330f729Sjoerg class CatchPadInst; 497330f729Sjoerg class CatchReturnInst; 507330f729Sjoerg class CatchSwitchInst; 517330f729Sjoerg class CleanupPadInst; 527330f729Sjoerg class CleanupReturnInst; 537330f729Sjoerg class Constant; 547330f729Sjoerg class ConstrainedFPIntrinsic; 557330f729Sjoerg class DbgValueInst; 567330f729Sjoerg class DataLayout; 577330f729Sjoerg class DIExpression; 587330f729Sjoerg class DILocalVariable; 597330f729Sjoerg class DILocation; 607330f729Sjoerg class FenceInst; 617330f729Sjoerg class FunctionLoweringInfo; 627330f729Sjoerg class GCFunctionInfo; 637330f729Sjoerg class GCRelocateInst; 647330f729Sjoerg class GCResultInst; 65*82d56013Sjoerg class GCStatepointInst; 667330f729Sjoerg class IndirectBrInst; 677330f729Sjoerg class InvokeInst; 687330f729Sjoerg class LandingPadInst; 697330f729Sjoerg class LLVMContext; 707330f729Sjoerg class LoadInst; 717330f729Sjoerg class MachineBasicBlock; 727330f729Sjoerg class PHINode; 737330f729Sjoerg class ResumeInst; 747330f729Sjoerg class ReturnInst; 757330f729Sjoerg class SDDbgValue; 76*82d56013Sjoerg class SelectionDAG; 777330f729Sjoerg class StoreInst; 787330f729Sjoerg class SwiftErrorValueTracking; 797330f729Sjoerg class SwitchInst; 807330f729Sjoerg class TargetLibraryInfo; 817330f729Sjoerg class TargetMachine; 827330f729Sjoerg class Type; 837330f729Sjoerg class VAArgInst; 847330f729Sjoerg class UnreachableInst; 857330f729Sjoerg class Use; 867330f729Sjoerg class User; 877330f729Sjoerg class Value; 887330f729Sjoerg 897330f729Sjoerg //===----------------------------------------------------------------------===// 907330f729Sjoerg /// SelectionDAGBuilder - This is the common target-independent lowering 917330f729Sjoerg /// implementation that is parameterized by a TargetLowering object. 927330f729Sjoerg /// 937330f729Sjoerg class SelectionDAGBuilder { 947330f729Sjoerg /// The current instruction being visited. 957330f729Sjoerg const Instruction *CurInst = nullptr; 967330f729Sjoerg 977330f729Sjoerg DenseMap<const Value*, SDValue> NodeMap; 987330f729Sjoerg 997330f729Sjoerg /// Maps argument value for unused arguments. This is used 1007330f729Sjoerg /// to preserve debug information for incoming arguments. 1017330f729Sjoerg DenseMap<const Value*, SDValue> UnusedArgNodeMap; 1027330f729Sjoerg 1037330f729Sjoerg /// Helper type for DanglingDebugInfoMap. 1047330f729Sjoerg class DanglingDebugInfo { 1057330f729Sjoerg const DbgValueInst* DI = nullptr; 1067330f729Sjoerg DebugLoc dl; 1077330f729Sjoerg unsigned SDNodeOrder = 0; 1087330f729Sjoerg 1097330f729Sjoerg public: 1107330f729Sjoerg DanglingDebugInfo() = default; DanglingDebugInfo(const DbgValueInst * di,DebugLoc DL,unsigned SDNO)1117330f729Sjoerg DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) 1127330f729Sjoerg : DI(di), dl(std::move(DL)), SDNodeOrder(SDNO) {} 1137330f729Sjoerg getDI()1147330f729Sjoerg const DbgValueInst* getDI() { return DI; } getdl()1157330f729Sjoerg DebugLoc getdl() { return dl; } getSDNodeOrder()1167330f729Sjoerg unsigned getSDNodeOrder() { return SDNodeOrder; } 1177330f729Sjoerg }; 1187330f729Sjoerg 1197330f729Sjoerg /// Helper type for DanglingDebugInfoMap. 1207330f729Sjoerg typedef std::vector<DanglingDebugInfo> DanglingDebugInfoVector; 1217330f729Sjoerg 1227330f729Sjoerg /// Keeps track of dbg_values for which we have not yet seen the referent. 1237330f729Sjoerg /// We defer handling these until we do see it. 1247330f729Sjoerg MapVector<const Value*, DanglingDebugInfoVector> DanglingDebugInfoMap; 1257330f729Sjoerg 1267330f729Sjoerg public: 1277330f729Sjoerg /// Loads are not emitted to the program immediately. We bunch them up and 1287330f729Sjoerg /// then emit token factor nodes when possible. This allows us to get simple 1297330f729Sjoerg /// disambiguation between loads without worrying about alias analysis. 1307330f729Sjoerg SmallVector<SDValue, 8> PendingLoads; 1317330f729Sjoerg 1327330f729Sjoerg /// State used while lowering a statepoint sequence (gc_statepoint, 1337330f729Sjoerg /// gc_relocate, and gc_result). See StatepointLowering.hpp/cpp for details. 1347330f729Sjoerg StatepointLoweringState StatepointLowering; 1357330f729Sjoerg 1367330f729Sjoerg private: 1377330f729Sjoerg /// CopyToReg nodes that copy values to virtual registers for export to other 1387330f729Sjoerg /// blocks need to be emitted before any terminator instruction, but they have 1397330f729Sjoerg /// no other ordering requirements. We bunch them up and the emit a single 1407330f729Sjoerg /// tokenfactor for them just before terminator instructions. 1417330f729Sjoerg SmallVector<SDValue, 8> PendingExports; 1427330f729Sjoerg 143*82d56013Sjoerg /// Similar to loads, nodes corresponding to constrained FP intrinsics are 144*82d56013Sjoerg /// bunched up and emitted when necessary. These can be moved across each 145*82d56013Sjoerg /// other and any (normal) memory operation (load or store), but not across 146*82d56013Sjoerg /// calls or instructions having unspecified side effects. As a special 147*82d56013Sjoerg /// case, constrained FP intrinsics using fpexcept.strict may not be deleted 148*82d56013Sjoerg /// even if otherwise unused, so they need to be chained before any 149*82d56013Sjoerg /// terminator instruction (like PendingExports). We track the latter 150*82d56013Sjoerg /// set of nodes in a separate list. 151*82d56013Sjoerg SmallVector<SDValue, 8> PendingConstrainedFP; 152*82d56013Sjoerg SmallVector<SDValue, 8> PendingConstrainedFPStrict; 153*82d56013Sjoerg 154*82d56013Sjoerg /// Update root to include all chains from the Pending list. 155*82d56013Sjoerg SDValue updateRoot(SmallVectorImpl<SDValue> &Pending); 156*82d56013Sjoerg 1577330f729Sjoerg /// A unique monotonically increasing number used to order the SDNodes we 1587330f729Sjoerg /// create. 1597330f729Sjoerg unsigned SDNodeOrder; 1607330f729Sjoerg 1617330f729Sjoerg /// Determine the rank by weight of CC in [First,Last]. If CC has more weight 1627330f729Sjoerg /// than each cluster in the range, its rank is 0. 1637330f729Sjoerg unsigned caseClusterRank(const SwitchCG::CaseCluster &CC, 1647330f729Sjoerg SwitchCG::CaseClusterIt First, 1657330f729Sjoerg SwitchCG::CaseClusterIt Last); 1667330f729Sjoerg 1677330f729Sjoerg /// Emit comparison and split W into two subtrees. 1687330f729Sjoerg void splitWorkItem(SwitchCG::SwitchWorkList &WorkList, 1697330f729Sjoerg const SwitchCG::SwitchWorkListItem &W, Value *Cond, 1707330f729Sjoerg MachineBasicBlock *SwitchMBB); 1717330f729Sjoerg 1727330f729Sjoerg /// Lower W. 1737330f729Sjoerg void lowerWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond, 1747330f729Sjoerg MachineBasicBlock *SwitchMBB, 1757330f729Sjoerg MachineBasicBlock *DefaultMBB); 1767330f729Sjoerg 1777330f729Sjoerg /// Peel the top probability case if it exceeds the threshold 1787330f729Sjoerg MachineBasicBlock * 1797330f729Sjoerg peelDominantCaseCluster(const SwitchInst &SI, 1807330f729Sjoerg SwitchCG::CaseClusterVector &Clusters, 1817330f729Sjoerg BranchProbability &PeeledCaseProb); 1827330f729Sjoerg 1837330f729Sjoerg /// A class which encapsulates all of the information needed to generate a 1847330f729Sjoerg /// stack protector check and signals to isel via its state being initialized 1857330f729Sjoerg /// that a stack protector needs to be generated. 1867330f729Sjoerg /// 1877330f729Sjoerg /// *NOTE* The following is a high level documentation of SelectionDAG Stack 1887330f729Sjoerg /// Protector Generation. The reason that it is placed here is for a lack of 1897330f729Sjoerg /// other good places to stick it. 1907330f729Sjoerg /// 1917330f729Sjoerg /// High Level Overview of SelectionDAG Stack Protector Generation: 1927330f729Sjoerg /// 1937330f729Sjoerg /// Previously, generation of stack protectors was done exclusively in the 1947330f729Sjoerg /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated 1957330f729Sjoerg /// splitting basic blocks at the IR level to create the success/failure basic 1967330f729Sjoerg /// blocks in the tail of the basic block in question. As a result of this, 1977330f729Sjoerg /// calls that would have qualified for the sibling call optimization were no 1987330f729Sjoerg /// longer eligible for optimization since said calls were no longer right in 1997330f729Sjoerg /// the "tail position" (i.e. the immediate predecessor of a ReturnInst 2007330f729Sjoerg /// instruction). 2017330f729Sjoerg /// 2027330f729Sjoerg /// Then it was noticed that since the sibling call optimization causes the 2037330f729Sjoerg /// callee to reuse the caller's stack, if we could delay the generation of 2047330f729Sjoerg /// the stack protector check until later in CodeGen after the sibling call 2057330f729Sjoerg /// decision was made, we get both the tail call optimization and the stack 2067330f729Sjoerg /// protector check! 2077330f729Sjoerg /// 2087330f729Sjoerg /// A few goals in solving this problem were: 2097330f729Sjoerg /// 2107330f729Sjoerg /// 1. Preserve the architecture independence of stack protector generation. 2117330f729Sjoerg /// 2127330f729Sjoerg /// 2. Preserve the normal IR level stack protector check for platforms like 2137330f729Sjoerg /// OpenBSD for which we support platform-specific stack protector 2147330f729Sjoerg /// generation. 2157330f729Sjoerg /// 2167330f729Sjoerg /// The main problem that guided the present solution is that one can not 2177330f729Sjoerg /// solve this problem in an architecture independent manner at the IR level 2187330f729Sjoerg /// only. This is because: 2197330f729Sjoerg /// 2207330f729Sjoerg /// 1. The decision on whether or not to perform a sibling call on certain 2217330f729Sjoerg /// platforms (for instance i386) requires lower level information 2227330f729Sjoerg /// related to available registers that can not be known at the IR level. 2237330f729Sjoerg /// 2247330f729Sjoerg /// 2. Even if the previous point were not true, the decision on whether to 2257330f729Sjoerg /// perform a tail call is done in LowerCallTo in SelectionDAG which 2267330f729Sjoerg /// occurs after the Stack Protector Pass. As a result, one would need to 2277330f729Sjoerg /// put the relevant callinst into the stack protector check success 2287330f729Sjoerg /// basic block (where the return inst is placed) and then move it back 2297330f729Sjoerg /// later at SelectionDAG/MI time before the stack protector check if the 2307330f729Sjoerg /// tail call optimization failed. The MI level option was nixed 2317330f729Sjoerg /// immediately since it would require platform-specific pattern 2327330f729Sjoerg /// matching. The SelectionDAG level option was nixed because 2337330f729Sjoerg /// SelectionDAG only processes one IR level basic block at a time 2347330f729Sjoerg /// implying one could not create a DAG Combine to move the callinst. 2357330f729Sjoerg /// 2367330f729Sjoerg /// To get around this problem a few things were realized: 2377330f729Sjoerg /// 2387330f729Sjoerg /// 1. While one can not handle multiple IR level basic blocks at the 2397330f729Sjoerg /// SelectionDAG Level, one can generate multiple machine basic blocks 2407330f729Sjoerg /// for one IR level basic block. This is how we handle bit tests and 2417330f729Sjoerg /// switches. 2427330f729Sjoerg /// 2437330f729Sjoerg /// 2. At the MI level, tail calls are represented via a special return 2447330f729Sjoerg /// MIInst called "tcreturn". Thus if we know the basic block in which we 2457330f729Sjoerg /// wish to insert the stack protector check, we get the correct behavior 2467330f729Sjoerg /// by always inserting the stack protector check right before the return 2477330f729Sjoerg /// statement. This is a "magical transformation" since no matter where 2487330f729Sjoerg /// the stack protector check intrinsic is, we always insert the stack 2497330f729Sjoerg /// protector check code at the end of the BB. 2507330f729Sjoerg /// 2517330f729Sjoerg /// Given the aforementioned constraints, the following solution was devised: 2527330f729Sjoerg /// 2537330f729Sjoerg /// 1. On platforms that do not support SelectionDAG stack protector check 2547330f729Sjoerg /// generation, allow for the normal IR level stack protector check 2557330f729Sjoerg /// generation to continue. 2567330f729Sjoerg /// 2577330f729Sjoerg /// 2. On platforms that do support SelectionDAG stack protector check 2587330f729Sjoerg /// generation: 2597330f729Sjoerg /// 2607330f729Sjoerg /// a. Use the IR level stack protector pass to decide if a stack 2617330f729Sjoerg /// protector is required/which BB we insert the stack protector check 2627330f729Sjoerg /// in by reusing the logic already therein. If we wish to generate a 2637330f729Sjoerg /// stack protector check in a basic block, we place a special IR 2647330f729Sjoerg /// intrinsic called llvm.stackprotectorcheck right before the BB's 2657330f729Sjoerg /// returninst or if there is a callinst that could potentially be 2667330f729Sjoerg /// sibling call optimized, before the call inst. 2677330f729Sjoerg /// 2687330f729Sjoerg /// b. Then when a BB with said intrinsic is processed, we codegen the BB 2697330f729Sjoerg /// normally via SelectBasicBlock. In said process, when we visit the 2707330f729Sjoerg /// stack protector check, we do not actually emit anything into the 2717330f729Sjoerg /// BB. Instead, we just initialize the stack protector descriptor 2727330f729Sjoerg /// class (which involves stashing information/creating the success 2737330f729Sjoerg /// mbbb and the failure mbb if we have not created one for this 2747330f729Sjoerg /// function yet) and export the guard variable that we are going to 2757330f729Sjoerg /// compare. 2767330f729Sjoerg /// 2777330f729Sjoerg /// c. After we finish selecting the basic block, in FinishBasicBlock if 2787330f729Sjoerg /// the StackProtectorDescriptor attached to the SelectionDAGBuilder is 2797330f729Sjoerg /// initialized, we produce the validation code with one of these 2807330f729Sjoerg /// techniques: 2817330f729Sjoerg /// 1) with a call to a guard check function 2827330f729Sjoerg /// 2) with inlined instrumentation 2837330f729Sjoerg /// 2847330f729Sjoerg /// 1) We insert a call to the check function before the terminator. 2857330f729Sjoerg /// 2867330f729Sjoerg /// 2) We first find a splice point in the parent basic block 2877330f729Sjoerg /// before the terminator and then splice the terminator of said basic 2887330f729Sjoerg /// block into the success basic block. Then we code-gen a new tail for 2897330f729Sjoerg /// the parent basic block consisting of the two loads, the comparison, 2907330f729Sjoerg /// and finally two branches to the success/failure basic blocks. We 2917330f729Sjoerg /// conclude by code-gening the failure basic block if we have not 2927330f729Sjoerg /// code-gened it already (all stack protector checks we generate in 2937330f729Sjoerg /// the same function, use the same failure basic block). 2947330f729Sjoerg class StackProtectorDescriptor { 2957330f729Sjoerg public: 2967330f729Sjoerg StackProtectorDescriptor() = default; 2977330f729Sjoerg 2987330f729Sjoerg /// Returns true if all fields of the stack protector descriptor are 2997330f729Sjoerg /// initialized implying that we should/are ready to emit a stack protector. shouldEmitStackProtector()3007330f729Sjoerg bool shouldEmitStackProtector() const { 3017330f729Sjoerg return ParentMBB && SuccessMBB && FailureMBB; 3027330f729Sjoerg } 3037330f729Sjoerg shouldEmitFunctionBasedCheckStackProtector()3047330f729Sjoerg bool shouldEmitFunctionBasedCheckStackProtector() const { 3057330f729Sjoerg return ParentMBB && !SuccessMBB && !FailureMBB; 3067330f729Sjoerg } 3077330f729Sjoerg 3087330f729Sjoerg /// Initialize the stack protector descriptor structure for a new basic 3097330f729Sjoerg /// block. initialize(const BasicBlock * BB,MachineBasicBlock * MBB,bool FunctionBasedInstrumentation)3107330f729Sjoerg void initialize(const BasicBlock *BB, MachineBasicBlock *MBB, 3117330f729Sjoerg bool FunctionBasedInstrumentation) { 3127330f729Sjoerg // Make sure we are not initialized yet. 3137330f729Sjoerg assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " 3147330f729Sjoerg "already initialized!"); 3157330f729Sjoerg ParentMBB = MBB; 3167330f729Sjoerg if (!FunctionBasedInstrumentation) { 3177330f729Sjoerg SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true); 3187330f729Sjoerg FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); 3197330f729Sjoerg } 3207330f729Sjoerg } 3217330f729Sjoerg 3227330f729Sjoerg /// Reset state that changes when we handle different basic blocks. 3237330f729Sjoerg /// 3247330f729Sjoerg /// This currently includes: 3257330f729Sjoerg /// 3267330f729Sjoerg /// 1. The specific basic block we are generating a 3277330f729Sjoerg /// stack protector for (ParentMBB). 3287330f729Sjoerg /// 3297330f729Sjoerg /// 2. The successor machine basic block that will contain the tail of 3307330f729Sjoerg /// parent mbb after we create the stack protector check (SuccessMBB). This 3317330f729Sjoerg /// BB is visited only on stack protector check success. resetPerBBState()3327330f729Sjoerg void resetPerBBState() { 3337330f729Sjoerg ParentMBB = nullptr; 3347330f729Sjoerg SuccessMBB = nullptr; 3357330f729Sjoerg } 3367330f729Sjoerg 3377330f729Sjoerg /// Reset state that only changes when we switch functions. 3387330f729Sjoerg /// 3397330f729Sjoerg /// This currently includes: 3407330f729Sjoerg /// 3417330f729Sjoerg /// 1. FailureMBB since we reuse the failure code path for all stack 3427330f729Sjoerg /// protector checks created in an individual function. 3437330f729Sjoerg /// 3447330f729Sjoerg /// 2.The guard variable since the guard variable we are checking against is 3457330f729Sjoerg /// always the same. resetPerFunctionState()3467330f729Sjoerg void resetPerFunctionState() { 3477330f729Sjoerg FailureMBB = nullptr; 3487330f729Sjoerg } 3497330f729Sjoerg getParentMBB()3507330f729Sjoerg MachineBasicBlock *getParentMBB() { return ParentMBB; } getSuccessMBB()3517330f729Sjoerg MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } getFailureMBB()3527330f729Sjoerg MachineBasicBlock *getFailureMBB() { return FailureMBB; } 3537330f729Sjoerg 3547330f729Sjoerg private: 3557330f729Sjoerg /// The basic block for which we are generating the stack protector. 3567330f729Sjoerg /// 3577330f729Sjoerg /// As a result of stack protector generation, we will splice the 3587330f729Sjoerg /// terminators of this basic block into the successor mbb SuccessMBB and 3597330f729Sjoerg /// replace it with a compare/branch to the successor mbbs 3607330f729Sjoerg /// SuccessMBB/FailureMBB depending on whether or not the stack protector 3617330f729Sjoerg /// was violated. 3627330f729Sjoerg MachineBasicBlock *ParentMBB = nullptr; 3637330f729Sjoerg 3647330f729Sjoerg /// A basic block visited on stack protector check success that contains the 3657330f729Sjoerg /// terminators of ParentMBB. 3667330f729Sjoerg MachineBasicBlock *SuccessMBB = nullptr; 3677330f729Sjoerg 3687330f729Sjoerg /// This basic block visited on stack protector check failure that will 3697330f729Sjoerg /// contain a call to __stack_chk_fail(). 3707330f729Sjoerg MachineBasicBlock *FailureMBB = nullptr; 3717330f729Sjoerg 3727330f729Sjoerg /// Add a successor machine basic block to ParentMBB. If the successor mbb 3737330f729Sjoerg /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic 3747330f729Sjoerg /// block will be created. Assign a large weight if IsLikely is true. 3757330f729Sjoerg MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, 3767330f729Sjoerg MachineBasicBlock *ParentMBB, 3777330f729Sjoerg bool IsLikely, 3787330f729Sjoerg MachineBasicBlock *SuccMBB = nullptr); 3797330f729Sjoerg }; 3807330f729Sjoerg 3817330f729Sjoerg private: 3827330f729Sjoerg const TargetMachine &TM; 3837330f729Sjoerg 3847330f729Sjoerg public: 3857330f729Sjoerg /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling 3867330f729Sjoerg /// nodes without a corresponding SDNode. 3877330f729Sjoerg static const unsigned LowestSDNodeOrder = 1; 3887330f729Sjoerg 3897330f729Sjoerg SelectionDAG &DAG; 3907330f729Sjoerg const DataLayout *DL = nullptr; 391*82d56013Sjoerg AAResults *AA = nullptr; 3927330f729Sjoerg const TargetLibraryInfo *LibInfo; 3937330f729Sjoerg 3947330f729Sjoerg class SDAGSwitchLowering : public SwitchCG::SwitchLowering { 3957330f729Sjoerg public: SDAGSwitchLowering(SelectionDAGBuilder * sdb,FunctionLoweringInfo & funcinfo)3967330f729Sjoerg SDAGSwitchLowering(SelectionDAGBuilder *sdb, FunctionLoweringInfo &funcinfo) 3977330f729Sjoerg : SwitchCG::SwitchLowering(funcinfo), SDB(sdb) {} 3987330f729Sjoerg 3997330f729Sjoerg virtual void addSuccessorWithProb( 4007330f729Sjoerg MachineBasicBlock *Src, MachineBasicBlock *Dst, 4017330f729Sjoerg BranchProbability Prob = BranchProbability::getUnknown()) override { 4027330f729Sjoerg SDB->addSuccessorWithProb(Src, Dst, Prob); 4037330f729Sjoerg } 4047330f729Sjoerg 4057330f729Sjoerg private: 4067330f729Sjoerg SelectionDAGBuilder *SDB; 4077330f729Sjoerg }; 4087330f729Sjoerg 409*82d56013Sjoerg // Data related to deferred switch lowerings. Used to construct additional 410*82d56013Sjoerg // Basic Blocks in SelectionDAGISel::FinishBasicBlock. 4117330f729Sjoerg std::unique_ptr<SDAGSwitchLowering> SL; 4127330f729Sjoerg 4137330f729Sjoerg /// A StackProtectorDescriptor structure used to communicate stack protector 4147330f729Sjoerg /// information in between SelectBasicBlock and FinishBasicBlock. 4157330f729Sjoerg StackProtectorDescriptor SPDescriptor; 4167330f729Sjoerg 4177330f729Sjoerg // Emit PHI-node-operand constants only once even if used by multiple 4187330f729Sjoerg // PHI nodes. 4197330f729Sjoerg DenseMap<const Constant *, unsigned> ConstantsOut; 4207330f729Sjoerg 4217330f729Sjoerg /// Information about the function as a whole. 4227330f729Sjoerg FunctionLoweringInfo &FuncInfo; 4237330f729Sjoerg 4247330f729Sjoerg /// Information about the swifterror values used throughout the function. 4257330f729Sjoerg SwiftErrorValueTracking &SwiftError; 4267330f729Sjoerg 4277330f729Sjoerg /// Garbage collection metadata for the function. 4287330f729Sjoerg GCFunctionInfo *GFI; 4297330f729Sjoerg 4307330f729Sjoerg /// Map a landing pad to the call site indexes. 4317330f729Sjoerg DenseMap<MachineBasicBlock *, SmallVector<unsigned, 4>> LPadToCallSiteMap; 4327330f729Sjoerg 4337330f729Sjoerg /// This is set to true if a call in the current block has been translated as 4347330f729Sjoerg /// a tail call. In this case, no subsequent DAG nodes should be created. 4357330f729Sjoerg bool HasTailCall = false; 4367330f729Sjoerg 4377330f729Sjoerg LLVMContext *Context; 4387330f729Sjoerg SelectionDAGBuilder(SelectionDAG & dag,FunctionLoweringInfo & funcinfo,SwiftErrorValueTracking & swifterror,CodeGenOpt::Level ol)4397330f729Sjoerg SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, 4407330f729Sjoerg SwiftErrorValueTracking &swifterror, CodeGenOpt::Level ol) 4417330f729Sjoerg : SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), DAG(dag), 4427330f729Sjoerg SL(std::make_unique<SDAGSwitchLowering>(this, funcinfo)), FuncInfo(funcinfo), 4437330f729Sjoerg SwiftError(swifterror) {} 4447330f729Sjoerg 445*82d56013Sjoerg void init(GCFunctionInfo *gfi, AAResults *AA, 4467330f729Sjoerg const TargetLibraryInfo *li); 4477330f729Sjoerg 4487330f729Sjoerg /// Clear out the current SelectionDAG and the associated state and prepare 4497330f729Sjoerg /// this SelectionDAGBuilder object to be used for a new block. This doesn't 4507330f729Sjoerg /// clear out information about additional blocks that are needed to complete 4517330f729Sjoerg /// switch lowering or PHI node updating; that information is cleared out as 4527330f729Sjoerg /// it is consumed. 4537330f729Sjoerg void clear(); 4547330f729Sjoerg 4557330f729Sjoerg /// Clear the dangling debug information map. This function is separated from 4567330f729Sjoerg /// the clear so that debug information that is dangling in a basic block can 4577330f729Sjoerg /// be properly resolved in a different basic block. This allows the 4587330f729Sjoerg /// SelectionDAG to resolve dangling debug information attached to PHI nodes. 4597330f729Sjoerg void clearDanglingDebugInfo(); 4607330f729Sjoerg 4617330f729Sjoerg /// Return the current virtual root of the Selection DAG, flushing any 4627330f729Sjoerg /// PendingLoad items. This must be done before emitting a store or any other 463*82d56013Sjoerg /// memory node that may need to be ordered after any prior load instructions. 464*82d56013Sjoerg SDValue getMemoryRoot(); 465*82d56013Sjoerg 466*82d56013Sjoerg /// Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) 467*82d56013Sjoerg /// items. This must be done before emitting any call other any other node 468*82d56013Sjoerg /// that may need to be ordered after FP instructions due to other side 469*82d56013Sjoerg /// effects. 4707330f729Sjoerg SDValue getRoot(); 4717330f729Sjoerg 4727330f729Sjoerg /// Similar to getRoot, but instead of flushing all the PendingLoad items, 473*82d56013Sjoerg /// flush all the PendingExports (and PendingConstrainedFPStrict) items. 474*82d56013Sjoerg /// It is necessary to do this before emitting a terminator instruction. 4757330f729Sjoerg SDValue getControlRoot(); 4767330f729Sjoerg getCurSDLoc()4777330f729Sjoerg SDLoc getCurSDLoc() const { 4787330f729Sjoerg return SDLoc(CurInst, SDNodeOrder); 4797330f729Sjoerg } 4807330f729Sjoerg getCurDebugLoc()4817330f729Sjoerg DebugLoc getCurDebugLoc() const { 4827330f729Sjoerg return CurInst ? CurInst->getDebugLoc() : DebugLoc(); 4837330f729Sjoerg } 4847330f729Sjoerg 4857330f729Sjoerg void CopyValueToVirtualRegister(const Value *V, unsigned Reg); 4867330f729Sjoerg 4877330f729Sjoerg void visit(const Instruction &I); 4887330f729Sjoerg 4897330f729Sjoerg void visit(unsigned Opcode, const User &I); 4907330f729Sjoerg 4917330f729Sjoerg /// If there was virtual register allocated for the value V emit CopyFromReg 4927330f729Sjoerg /// of the specified type Ty. Return empty SDValue() otherwise. 4937330f729Sjoerg SDValue getCopyFromRegs(const Value *V, Type *Ty); 4947330f729Sjoerg 495*82d56013Sjoerg /// Register a dbg_value which relies on a Value which we have not yet seen. 496*82d56013Sjoerg void addDanglingDebugInfo(const DbgValueInst *DI, DebugLoc DL, 497*82d56013Sjoerg unsigned Order); 498*82d56013Sjoerg 4997330f729Sjoerg /// If we have dangling debug info that describes \p Variable, or an 5007330f729Sjoerg /// overlapping part of variable considering the \p Expr, then this method 5017330f729Sjoerg /// will drop that debug info as it isn't valid any longer. 5027330f729Sjoerg void dropDanglingDebugInfo(const DILocalVariable *Variable, 5037330f729Sjoerg const DIExpression *Expr); 5047330f729Sjoerg 5057330f729Sjoerg /// If we saw an earlier dbg_value referring to V, generate the debug data 5067330f729Sjoerg /// structures now that we've seen its definition. 5077330f729Sjoerg void resolveDanglingDebugInfo(const Value *V, SDValue Val); 5087330f729Sjoerg 5097330f729Sjoerg /// For the given dangling debuginfo record, perform last-ditch efforts to 5107330f729Sjoerg /// resolve the debuginfo to something that is represented in this DAG. If 5117330f729Sjoerg /// this cannot be done, produce an Undef debug value record. 5127330f729Sjoerg void salvageUnresolvedDbgValue(DanglingDebugInfo &DDI); 5137330f729Sjoerg 514*82d56013Sjoerg /// For a given list of Values, attempt to create and record a SDDbgValue in 515*82d56013Sjoerg /// the SelectionDAG. 516*82d56013Sjoerg bool handleDebugValue(ArrayRef<const Value *> Values, DILocalVariable *Var, 517*82d56013Sjoerg DIExpression *Expr, DebugLoc CurDL, DebugLoc InstDL, 518*82d56013Sjoerg unsigned Order, bool IsVariadic); 5197330f729Sjoerg 5207330f729Sjoerg /// Evict any dangling debug information, attempting to salvage it first. 5217330f729Sjoerg void resolveOrClearDbgInfo(); 5227330f729Sjoerg 5237330f729Sjoerg SDValue getValue(const Value *V); 5247330f729Sjoerg 5257330f729Sjoerg SDValue getNonRegisterValue(const Value *V); 5267330f729Sjoerg SDValue getValueImpl(const Value *V); 5277330f729Sjoerg setValue(const Value * V,SDValue NewN)5287330f729Sjoerg void setValue(const Value *V, SDValue NewN) { 5297330f729Sjoerg SDValue &N = NodeMap[V]; 5307330f729Sjoerg assert(!N.getNode() && "Already set a value for this node!"); 5317330f729Sjoerg N = NewN; 5327330f729Sjoerg } 5337330f729Sjoerg setUnusedArgValue(const Value * V,SDValue NewN)5347330f729Sjoerg void setUnusedArgValue(const Value *V, SDValue NewN) { 5357330f729Sjoerg SDValue &N = UnusedArgNodeMap[V]; 5367330f729Sjoerg assert(!N.getNode() && "Already set a value for this node!"); 5377330f729Sjoerg N = NewN; 5387330f729Sjoerg } 5397330f729Sjoerg 5407330f729Sjoerg void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, 5417330f729Sjoerg MachineBasicBlock *FBB, MachineBasicBlock *CurBB, 5427330f729Sjoerg MachineBasicBlock *SwitchBB, 5437330f729Sjoerg Instruction::BinaryOps Opc, BranchProbability TProb, 5447330f729Sjoerg BranchProbability FProb, bool InvertCond); 5457330f729Sjoerg void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, 5467330f729Sjoerg MachineBasicBlock *FBB, 5477330f729Sjoerg MachineBasicBlock *CurBB, 5487330f729Sjoerg MachineBasicBlock *SwitchBB, 5497330f729Sjoerg BranchProbability TProb, BranchProbability FProb, 5507330f729Sjoerg bool InvertCond); 5517330f729Sjoerg bool ShouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases); 5527330f729Sjoerg bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); 5537330f729Sjoerg void CopyToExportRegsIfNeeded(const Value *V); 5547330f729Sjoerg void ExportFromCurrentBlock(const Value *V); 555*82d56013Sjoerg void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, 5567330f729Sjoerg const BasicBlock *EHPadBB = nullptr); 5577330f729Sjoerg 5587330f729Sjoerg // Lower range metadata from 0 to N to assert zext to an integer of nearest 5597330f729Sjoerg // floor power of two. 5607330f729Sjoerg SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, 5617330f729Sjoerg SDValue Op); 5627330f729Sjoerg 5637330f729Sjoerg void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, 5647330f729Sjoerg const CallBase *Call, unsigned ArgIdx, 5657330f729Sjoerg unsigned NumArgs, SDValue Callee, 5667330f729Sjoerg Type *ReturnTy, bool IsPatchPoint); 5677330f729Sjoerg 5687330f729Sjoerg std::pair<SDValue, SDValue> 5697330f729Sjoerg lowerInvokable(TargetLowering::CallLoweringInfo &CLI, 5707330f729Sjoerg const BasicBlock *EHPadBB = nullptr); 5717330f729Sjoerg 5727330f729Sjoerg /// When an MBB was split during scheduling, update the 5737330f729Sjoerg /// references that need to refer to the last resulting block. 5747330f729Sjoerg void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); 5757330f729Sjoerg 5767330f729Sjoerg /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes 5777330f729Sjoerg /// of lowering into a STATEPOINT node. 5787330f729Sjoerg struct StatepointLoweringInfo { 5797330f729Sjoerg /// Bases[i] is the base pointer for Ptrs[i]. Together they denote the set 5807330f729Sjoerg /// of gc pointers this STATEPOINT has to relocate. 5817330f729Sjoerg SmallVector<const Value *, 16> Bases; 5827330f729Sjoerg SmallVector<const Value *, 16> Ptrs; 5837330f729Sjoerg 5847330f729Sjoerg /// The set of gc.relocate calls associated with this gc.statepoint. 5857330f729Sjoerg SmallVector<const GCRelocateInst *, 16> GCRelocates; 5867330f729Sjoerg 5877330f729Sjoerg /// The full list of gc arguments to the gc.statepoint being lowered. 5887330f729Sjoerg ArrayRef<const Use> GCArgs; 5897330f729Sjoerg 5907330f729Sjoerg /// The gc.statepoint instruction. 5917330f729Sjoerg const Instruction *StatepointInstr = nullptr; 5927330f729Sjoerg 5937330f729Sjoerg /// The list of gc transition arguments present in the gc.statepoint being 5947330f729Sjoerg /// lowered. 5957330f729Sjoerg ArrayRef<const Use> GCTransitionArgs; 5967330f729Sjoerg 5977330f729Sjoerg /// The ID that the resulting STATEPOINT instruction has to report. 5987330f729Sjoerg unsigned ID = -1; 5997330f729Sjoerg 6007330f729Sjoerg /// Information regarding the underlying call instruction. 6017330f729Sjoerg TargetLowering::CallLoweringInfo CLI; 6027330f729Sjoerg 6037330f729Sjoerg /// The deoptimization state associated with this gc.statepoint call, if 6047330f729Sjoerg /// any. 6057330f729Sjoerg ArrayRef<const Use> DeoptState; 6067330f729Sjoerg 6077330f729Sjoerg /// Flags associated with the meta arguments being lowered. 6087330f729Sjoerg uint64_t StatepointFlags = -1; 6097330f729Sjoerg 6107330f729Sjoerg /// The number of patchable bytes the call needs to get lowered into. 6117330f729Sjoerg unsigned NumPatchBytes = -1; 6127330f729Sjoerg 6137330f729Sjoerg /// The exception handling unwind destination, in case this represents an 6147330f729Sjoerg /// invoke of gc.statepoint. 6157330f729Sjoerg const BasicBlock *EHPadBB = nullptr; 6167330f729Sjoerg StatepointLoweringInfoStatepointLoweringInfo6177330f729Sjoerg explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {} 6187330f729Sjoerg }; 6197330f729Sjoerg 6207330f729Sjoerg /// Lower \p SLI into a STATEPOINT instruction. 6217330f729Sjoerg SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SI); 6227330f729Sjoerg 6237330f729Sjoerg // This function is responsible for the whole statepoint lowering process. 6247330f729Sjoerg // It uniformly handles invoke and call statepoints. 625*82d56013Sjoerg void LowerStatepoint(const GCStatepointInst &I, 6267330f729Sjoerg const BasicBlock *EHPadBB = nullptr); 6277330f729Sjoerg 6287330f729Sjoerg void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, 6297330f729Sjoerg const BasicBlock *EHPadBB); 6307330f729Sjoerg 6317330f729Sjoerg void LowerDeoptimizeCall(const CallInst *CI); 6327330f729Sjoerg void LowerDeoptimizingReturn(); 6337330f729Sjoerg 6347330f729Sjoerg void LowerCallSiteWithDeoptBundleImpl(const CallBase *Call, SDValue Callee, 6357330f729Sjoerg const BasicBlock *EHPadBB, 6367330f729Sjoerg bool VarArgDisallowed, 6377330f729Sjoerg bool ForceVoidReturnTy); 6387330f729Sjoerg 6397330f729Sjoerg /// Returns the type of FrameIndex and TargetFrameIndex nodes. getFrameIndexTy()6407330f729Sjoerg MVT getFrameIndexTy() { 6417330f729Sjoerg return DAG.getTargetLoweringInfo().getFrameIndexTy(DAG.getDataLayout()); 6427330f729Sjoerg } 6437330f729Sjoerg 6447330f729Sjoerg private: 6457330f729Sjoerg // Terminator instructions. 6467330f729Sjoerg void visitRet(const ReturnInst &I); 6477330f729Sjoerg void visitBr(const BranchInst &I); 6487330f729Sjoerg void visitSwitch(const SwitchInst &I); 6497330f729Sjoerg void visitIndirectBr(const IndirectBrInst &I); 6507330f729Sjoerg void visitUnreachable(const UnreachableInst &I); 6517330f729Sjoerg void visitCleanupRet(const CleanupReturnInst &I); 6527330f729Sjoerg void visitCatchSwitch(const CatchSwitchInst &I); 6537330f729Sjoerg void visitCatchRet(const CatchReturnInst &I); 6547330f729Sjoerg void visitCatchPad(const CatchPadInst &I); 6557330f729Sjoerg void visitCleanupPad(const CleanupPadInst &CPI); 6567330f729Sjoerg 6577330f729Sjoerg BranchProbability getEdgeProbability(const MachineBasicBlock *Src, 6587330f729Sjoerg const MachineBasicBlock *Dst) const; 6597330f729Sjoerg void addSuccessorWithProb( 6607330f729Sjoerg MachineBasicBlock *Src, MachineBasicBlock *Dst, 6617330f729Sjoerg BranchProbability Prob = BranchProbability::getUnknown()); 6627330f729Sjoerg 6637330f729Sjoerg public: 6647330f729Sjoerg void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB); 6657330f729Sjoerg void visitSPDescriptorParent(StackProtectorDescriptor &SPD, 6667330f729Sjoerg MachineBasicBlock *ParentBB); 6677330f729Sjoerg void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); 6687330f729Sjoerg void visitBitTestHeader(SwitchCG::BitTestBlock &B, 6697330f729Sjoerg MachineBasicBlock *SwitchBB); 6707330f729Sjoerg void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, 6717330f729Sjoerg BranchProbability BranchProbToNext, unsigned Reg, 6727330f729Sjoerg SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB); 6737330f729Sjoerg void visitJumpTable(SwitchCG::JumpTable &JT); 6747330f729Sjoerg void visitJumpTableHeader(SwitchCG::JumpTable &JT, 6757330f729Sjoerg SwitchCG::JumpTableHeader &JTH, 6767330f729Sjoerg MachineBasicBlock *SwitchBB); 6777330f729Sjoerg 6787330f729Sjoerg private: 6797330f729Sjoerg // These all get lowered before this pass. 6807330f729Sjoerg void visitInvoke(const InvokeInst &I); 6817330f729Sjoerg void visitCallBr(const CallBrInst &I); 6827330f729Sjoerg void visitResume(const ResumeInst &I); 6837330f729Sjoerg 6847330f729Sjoerg void visitUnary(const User &I, unsigned Opcode); visitFNeg(const User & I)6857330f729Sjoerg void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); } 6867330f729Sjoerg 6877330f729Sjoerg void visitBinary(const User &I, unsigned Opcode); 6887330f729Sjoerg void visitShift(const User &I, unsigned Opcode); visitAdd(const User & I)6897330f729Sjoerg void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } visitFAdd(const User & I)6907330f729Sjoerg void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } visitSub(const User & I)6917330f729Sjoerg void visitSub(const User &I) { visitBinary(I, ISD::SUB); } visitFSub(const User & I)692*82d56013Sjoerg void visitFSub(const User &I) { visitBinary(I, ISD::FSUB); } visitMul(const User & I)6937330f729Sjoerg void visitMul(const User &I) { visitBinary(I, ISD::MUL); } visitFMul(const User & I)6947330f729Sjoerg void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } visitURem(const User & I)6957330f729Sjoerg void visitURem(const User &I) { visitBinary(I, ISD::UREM); } visitSRem(const User & I)6967330f729Sjoerg void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } visitFRem(const User & I)6977330f729Sjoerg void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } visitUDiv(const User & I)6987330f729Sjoerg void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } 6997330f729Sjoerg void visitSDiv(const User &I); visitFDiv(const User & I)7007330f729Sjoerg void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } visitAnd(const User & I)7017330f729Sjoerg void visitAnd (const User &I) { visitBinary(I, ISD::AND); } visitOr(const User & I)7027330f729Sjoerg void visitOr (const User &I) { visitBinary(I, ISD::OR); } visitXor(const User & I)7037330f729Sjoerg void visitXor (const User &I) { visitBinary(I, ISD::XOR); } visitShl(const User & I)7047330f729Sjoerg void visitShl (const User &I) { visitShift(I, ISD::SHL); } visitLShr(const User & I)7057330f729Sjoerg void visitLShr(const User &I) { visitShift(I, ISD::SRL); } visitAShr(const User & I)7067330f729Sjoerg void visitAShr(const User &I) { visitShift(I, ISD::SRA); } 7077330f729Sjoerg void visitICmp(const User &I); 7087330f729Sjoerg void visitFCmp(const User &I); 7097330f729Sjoerg // Visit the conversion instructions 7107330f729Sjoerg void visitTrunc(const User &I); 7117330f729Sjoerg void visitZExt(const User &I); 7127330f729Sjoerg void visitSExt(const User &I); 7137330f729Sjoerg void visitFPTrunc(const User &I); 7147330f729Sjoerg void visitFPExt(const User &I); 7157330f729Sjoerg void visitFPToUI(const User &I); 7167330f729Sjoerg void visitFPToSI(const User &I); 7177330f729Sjoerg void visitUIToFP(const User &I); 7187330f729Sjoerg void visitSIToFP(const User &I); 7197330f729Sjoerg void visitPtrToInt(const User &I); 7207330f729Sjoerg void visitIntToPtr(const User &I); 7217330f729Sjoerg void visitBitCast(const User &I); 7227330f729Sjoerg void visitAddrSpaceCast(const User &I); 7237330f729Sjoerg 7247330f729Sjoerg void visitExtractElement(const User &I); 7257330f729Sjoerg void visitInsertElement(const User &I); 7267330f729Sjoerg void visitShuffleVector(const User &I); 7277330f729Sjoerg 7287330f729Sjoerg void visitExtractValue(const User &I); 7297330f729Sjoerg void visitInsertValue(const User &I); 7307330f729Sjoerg void visitLandingPad(const LandingPadInst &LP); 7317330f729Sjoerg 7327330f729Sjoerg void visitGetElementPtr(const User &I); 7337330f729Sjoerg void visitSelect(const User &I); 7347330f729Sjoerg 7357330f729Sjoerg void visitAlloca(const AllocaInst &I); 7367330f729Sjoerg void visitLoad(const LoadInst &I); 7377330f729Sjoerg void visitStore(const StoreInst &I); 7387330f729Sjoerg void visitMaskedLoad(const CallInst &I, bool IsExpanding = false); 7397330f729Sjoerg void visitMaskedStore(const CallInst &I, bool IsCompressing = false); 7407330f729Sjoerg void visitMaskedGather(const CallInst &I); 7417330f729Sjoerg void visitMaskedScatter(const CallInst &I); 7427330f729Sjoerg void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); 7437330f729Sjoerg void visitAtomicRMW(const AtomicRMWInst &I); 7447330f729Sjoerg void visitFence(const FenceInst &I); 7457330f729Sjoerg void visitPHI(const PHINode &I); 7467330f729Sjoerg void visitCall(const CallInst &I); 747*82d56013Sjoerg bool visitMemCmpBCmpCall(const CallInst &I); 7487330f729Sjoerg bool visitMemPCpyCall(const CallInst &I); 7497330f729Sjoerg bool visitMemChrCall(const CallInst &I); 7507330f729Sjoerg bool visitStrCpyCall(const CallInst &I, bool isStpcpy); 7517330f729Sjoerg bool visitStrCmpCall(const CallInst &I); 7527330f729Sjoerg bool visitStrLenCall(const CallInst &I); 7537330f729Sjoerg bool visitStrNLenCall(const CallInst &I); 7547330f729Sjoerg bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); 7557330f729Sjoerg bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode); 7567330f729Sjoerg void visitAtomicLoad(const LoadInst &I); 7577330f729Sjoerg void visitAtomicStore(const StoreInst &I); 7587330f729Sjoerg void visitLoadFromSwiftError(const LoadInst &I); 7597330f729Sjoerg void visitStoreToSwiftError(const StoreInst &I); 760*82d56013Sjoerg void visitFreeze(const FreezeInst &I); 7617330f729Sjoerg 762*82d56013Sjoerg void visitInlineAsm(const CallBase &Call, 763*82d56013Sjoerg const BasicBlock *EHPadBB = nullptr); 7647330f729Sjoerg void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); 7657330f729Sjoerg void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); 7667330f729Sjoerg void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI); 767*82d56013Sjoerg void visitVectorPredicationIntrinsic(const VPIntrinsic &VPIntrin); 7687330f729Sjoerg 7697330f729Sjoerg void visitVAStart(const CallInst &I); 7707330f729Sjoerg void visitVAArg(const VAArgInst &I); 7717330f729Sjoerg void visitVAEnd(const CallInst &I); 7727330f729Sjoerg void visitVACopy(const CallInst &I); 7737330f729Sjoerg void visitStackmap(const CallInst &I); 774*82d56013Sjoerg void visitPatchpoint(const CallBase &CB, const BasicBlock *EHPadBB = nullptr); 7757330f729Sjoerg 7767330f729Sjoerg // These two are implemented in StatepointLowering.cpp 7777330f729Sjoerg void visitGCRelocate(const GCRelocateInst &Relocate); 7787330f729Sjoerg void visitGCResult(const GCResultInst &I); 7797330f729Sjoerg 7807330f729Sjoerg void visitVectorReduce(const CallInst &I, unsigned Intrinsic); 781*82d56013Sjoerg void visitVectorReverse(const CallInst &I); 782*82d56013Sjoerg void visitVectorSplice(const CallInst &I); 783*82d56013Sjoerg void visitStepVector(const CallInst &I); 7847330f729Sjoerg visitUserOp1(const Instruction & I)7857330f729Sjoerg void visitUserOp1(const Instruction &I) { 7867330f729Sjoerg llvm_unreachable("UserOp1 should not exist at instruction selection time!"); 7877330f729Sjoerg } visitUserOp2(const Instruction & I)7887330f729Sjoerg void visitUserOp2(const Instruction &I) { 7897330f729Sjoerg llvm_unreachable("UserOp2 should not exist at instruction selection time!"); 7907330f729Sjoerg } 7917330f729Sjoerg 7927330f729Sjoerg void processIntegerCallValue(const Instruction &I, 7937330f729Sjoerg SDValue Value, bool IsSigned); 7947330f729Sjoerg 7957330f729Sjoerg void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); 7967330f729Sjoerg 797*82d56013Sjoerg void emitInlineAsmError(const CallBase &Call, const Twine &Message); 7987330f729Sjoerg 7997330f729Sjoerg /// If V is an function argument then create corresponding DBG_VALUE machine 8007330f729Sjoerg /// instruction for it now. At the end of instruction selection, they will be 8017330f729Sjoerg /// inserted to the entry BB. 8027330f729Sjoerg bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable, 8037330f729Sjoerg DIExpression *Expr, DILocation *DL, 8047330f729Sjoerg bool IsDbgDeclare, const SDValue &N); 8057330f729Sjoerg 8067330f729Sjoerg /// Return the next block after MBB, or nullptr if there is none. 8077330f729Sjoerg MachineBasicBlock *NextBlock(MachineBasicBlock *MBB); 8087330f729Sjoerg 8097330f729Sjoerg /// Update the DAG and DAG builder with the relevant information after 8107330f729Sjoerg /// a new root node has been created which could be a tail call. 8117330f729Sjoerg void updateDAGForMaybeTailCall(SDValue MaybeTC); 8127330f729Sjoerg 8137330f729Sjoerg /// Return the appropriate SDDbgValue based on N. 8147330f729Sjoerg SDDbgValue *getDbgValue(SDValue N, DILocalVariable *Variable, 8157330f729Sjoerg DIExpression *Expr, const DebugLoc &dl, 8167330f729Sjoerg unsigned DbgSDNodeOrder); 8177330f729Sjoerg 8187330f729Sjoerg /// Lowers CallInst to an external symbol. 8197330f729Sjoerg void lowerCallToExternalSymbol(const CallInst &I, const char *FunctionName); 820*82d56013Sjoerg 821*82d56013Sjoerg SDValue lowerStartEH(SDValue Chain, const BasicBlock *EHPadBB, 822*82d56013Sjoerg MCSymbol *&BeginLabel); 823*82d56013Sjoerg SDValue lowerEndEH(SDValue Chain, const InvokeInst *II, 824*82d56013Sjoerg const BasicBlock *EHPadBB, MCSymbol *BeginLabel); 8257330f729Sjoerg }; 8267330f729Sjoerg 8277330f729Sjoerg /// This struct represents the registers (physical or virtual) 8287330f729Sjoerg /// that a particular set of values is assigned, and the type information about 8297330f729Sjoerg /// the value. The most common situation is to represent one value at a time, 8307330f729Sjoerg /// but struct or array values are handled element-wise as multiple values. The 8317330f729Sjoerg /// splitting of aggregates is performed recursively, so that we never have 8327330f729Sjoerg /// aggregate-typed registers. The values at this point do not necessarily have 8337330f729Sjoerg /// legal types, so each value may require one or more registers of some legal 8347330f729Sjoerg /// type. 8357330f729Sjoerg /// 8367330f729Sjoerg struct RegsForValue { 8377330f729Sjoerg /// The value types of the values, which may not be legal, and 8387330f729Sjoerg /// may need be promoted or synthesized from one or more registers. 8397330f729Sjoerg SmallVector<EVT, 4> ValueVTs; 8407330f729Sjoerg 8417330f729Sjoerg /// The value types of the registers. This is the same size as ValueVTs and it 8427330f729Sjoerg /// records, for each value, what the type of the assigned register or 8437330f729Sjoerg /// registers are. (Individual values are never synthesized from more than one 8447330f729Sjoerg /// type of register.) 8457330f729Sjoerg /// 8467330f729Sjoerg /// With virtual registers, the contents of RegVTs is redundant with TLI's 8477330f729Sjoerg /// getRegisterType member function, however when with physical registers 8487330f729Sjoerg /// it is necessary to have a separate record of the types. 8497330f729Sjoerg SmallVector<MVT, 4> RegVTs; 8507330f729Sjoerg 8517330f729Sjoerg /// This list holds the registers assigned to the values. 8527330f729Sjoerg /// Each legal or promoted value requires one register, and each 8537330f729Sjoerg /// expanded value requires multiple registers. 8547330f729Sjoerg SmallVector<unsigned, 4> Regs; 8557330f729Sjoerg 8567330f729Sjoerg /// This list holds the number of registers for each value. 8577330f729Sjoerg SmallVector<unsigned, 4> RegCount; 8587330f729Sjoerg 8597330f729Sjoerg /// Records if this value needs to be treated in an ABI dependant manner, 8607330f729Sjoerg /// different to normal type legalization. 8617330f729Sjoerg Optional<CallingConv::ID> CallConv; 8627330f729Sjoerg 8637330f729Sjoerg RegsForValue() = default; 8647330f729Sjoerg RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt, EVT valuevt, 8657330f729Sjoerg Optional<CallingConv::ID> CC = None); 8667330f729Sjoerg RegsForValue(LLVMContext &Context, const TargetLowering &TLI, 8677330f729Sjoerg const DataLayout &DL, unsigned Reg, Type *Ty, 8687330f729Sjoerg Optional<CallingConv::ID> CC); 8697330f729Sjoerg isABIMangledRegsForValue8707330f729Sjoerg bool isABIMangled() const { 8717330f729Sjoerg return CallConv.hasValue(); 8727330f729Sjoerg } 8737330f729Sjoerg 8747330f729Sjoerg /// Add the specified values to this one. appendRegsForValue8757330f729Sjoerg void append(const RegsForValue &RHS) { 8767330f729Sjoerg ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); 8777330f729Sjoerg RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); 8787330f729Sjoerg Regs.append(RHS.Regs.begin(), RHS.Regs.end()); 8797330f729Sjoerg RegCount.push_back(RHS.Regs.size()); 8807330f729Sjoerg } 8817330f729Sjoerg 8827330f729Sjoerg /// Emit a series of CopyFromReg nodes that copies from this value and returns 8837330f729Sjoerg /// the result as a ValueVTs value. This uses Chain/Flag as the input and 8847330f729Sjoerg /// updates them for the output Chain/Flag. If the Flag pointer is NULL, no 8857330f729Sjoerg /// flag is used. 8867330f729Sjoerg SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, 8877330f729Sjoerg const SDLoc &dl, SDValue &Chain, SDValue *Flag, 8887330f729Sjoerg const Value *V = nullptr) const; 8897330f729Sjoerg 8907330f729Sjoerg /// Emit a series of CopyToReg nodes that copies the specified value into the 8917330f729Sjoerg /// registers specified by this object. This uses Chain/Flag as the input and 8927330f729Sjoerg /// updates them for the output Chain/Flag. If the Flag pointer is nullptr, no 8937330f729Sjoerg /// flag is used. If V is not nullptr, then it is used in printing better 8947330f729Sjoerg /// diagnostic messages on error. 8957330f729Sjoerg void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, 8967330f729Sjoerg SDValue &Chain, SDValue *Flag, const Value *V = nullptr, 8977330f729Sjoerg ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const; 8987330f729Sjoerg 8997330f729Sjoerg /// Add this value to the specified inlineasm node operand list. This adds the 9007330f729Sjoerg /// code marker, matching input operand index (if applicable), and includes 9017330f729Sjoerg /// the number of values added into it. 9027330f729Sjoerg void AddInlineAsmOperands(unsigned Code, bool HasMatching, 9037330f729Sjoerg unsigned MatchingIdx, const SDLoc &dl, 9047330f729Sjoerg SelectionDAG &DAG, std::vector<SDValue> &Ops) const; 9057330f729Sjoerg 9067330f729Sjoerg /// Check if the total RegCount is greater than one. occupiesMultipleRegsRegsForValue9077330f729Sjoerg bool occupiesMultipleRegs() const { 9087330f729Sjoerg return std::accumulate(RegCount.begin(), RegCount.end(), 0) > 1; 9097330f729Sjoerg } 9107330f729Sjoerg 9117330f729Sjoerg /// Return a list of registers and their sizes. 912*82d56013Sjoerg SmallVector<std::pair<unsigned, TypeSize>, 4> getRegsAndSizes() const; 9137330f729Sjoerg }; 9147330f729Sjoerg 9157330f729Sjoerg } // end namespace llvm 9167330f729Sjoerg 9177330f729Sjoerg #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 918