10b57cec5SDimitry Andric //===- SelectionDAGBuilder.h - Selection-DAG building -----------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This implements routines for translating from LLVM IR into SelectionDAG IR. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 140b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "StatepointLowering.h" 170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 180b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 190b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 200b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/SwitchLoweringUtils.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h" 260b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 270b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 280b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h" 290b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 300b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 310b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h" 320b57cec5SDimitry Andric #include <algorithm> 330b57cec5SDimitry Andric #include <cassert> 340b57cec5SDimitry Andric #include <cstdint> 350b57cec5SDimitry Andric #include <utility> 360b57cec5SDimitry Andric #include <vector> 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric namespace llvm { 390b57cec5SDimitry Andric 40e8d8bef9SDimitry Andric class AAResults; 410b57cec5SDimitry Andric class AllocaInst; 420b57cec5SDimitry Andric class AtomicCmpXchgInst; 430b57cec5SDimitry Andric class AtomicRMWInst; 440b57cec5SDimitry Andric class BasicBlock; 450b57cec5SDimitry Andric class BranchInst; 460b57cec5SDimitry Andric class CallInst; 470b57cec5SDimitry Andric class CallBrInst; 480b57cec5SDimitry Andric class CatchPadInst; 490b57cec5SDimitry Andric class CatchReturnInst; 500b57cec5SDimitry Andric class CatchSwitchInst; 510b57cec5SDimitry Andric class CleanupPadInst; 520b57cec5SDimitry Andric class CleanupReturnInst; 530b57cec5SDimitry Andric class Constant; 540b57cec5SDimitry Andric class ConstrainedFPIntrinsic; 550b57cec5SDimitry Andric class DbgValueInst; 560b57cec5SDimitry Andric class DataLayout; 570b57cec5SDimitry Andric class DIExpression; 580b57cec5SDimitry Andric class DILocalVariable; 590b57cec5SDimitry Andric class DILocation; 600b57cec5SDimitry Andric class FenceInst; 610b57cec5SDimitry Andric class FunctionLoweringInfo; 620b57cec5SDimitry Andric class GCFunctionInfo; 630b57cec5SDimitry Andric class GCRelocateInst; 640b57cec5SDimitry Andric class GCResultInst; 65e8d8bef9SDimitry Andric class GCStatepointInst; 660b57cec5SDimitry Andric class IndirectBrInst; 670b57cec5SDimitry Andric class InvokeInst; 680b57cec5SDimitry Andric class LandingPadInst; 690b57cec5SDimitry Andric class LLVMContext; 700b57cec5SDimitry Andric class LoadInst; 710b57cec5SDimitry Andric class MachineBasicBlock; 720b57cec5SDimitry Andric class PHINode; 730b57cec5SDimitry Andric class ResumeInst; 740b57cec5SDimitry Andric class ReturnInst; 750b57cec5SDimitry Andric class SDDbgValue; 765ffd83dbSDimitry Andric class SelectionDAG; 770b57cec5SDimitry Andric class StoreInst; 780b57cec5SDimitry Andric class SwiftErrorValueTracking; 790b57cec5SDimitry Andric class SwitchInst; 800b57cec5SDimitry Andric class TargetLibraryInfo; 810b57cec5SDimitry Andric class TargetMachine; 820b57cec5SDimitry Andric class Type; 830b57cec5SDimitry Andric class VAArgInst; 840b57cec5SDimitry Andric class UnreachableInst; 850b57cec5SDimitry Andric class Use; 860b57cec5SDimitry Andric class User; 870b57cec5SDimitry Andric class Value; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 900b57cec5SDimitry Andric /// SelectionDAGBuilder - This is the common target-independent lowering 910b57cec5SDimitry Andric /// implementation that is parameterized by a TargetLowering object. 920b57cec5SDimitry Andric /// 930b57cec5SDimitry Andric class SelectionDAGBuilder { 940b57cec5SDimitry Andric /// The current instruction being visited. 950b57cec5SDimitry Andric const Instruction *CurInst = nullptr; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric DenseMap<const Value*, SDValue> NodeMap; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric /// Maps argument value for unused arguments. This is used 1000b57cec5SDimitry Andric /// to preserve debug information for incoming arguments. 1010b57cec5SDimitry Andric DenseMap<const Value*, SDValue> UnusedArgNodeMap; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric /// Helper type for DanglingDebugInfoMap. 1040b57cec5SDimitry Andric class DanglingDebugInfo { 1050b57cec5SDimitry Andric const DbgValueInst* DI = nullptr; 1060b57cec5SDimitry Andric DebugLoc dl; 1070b57cec5SDimitry Andric unsigned SDNodeOrder = 0; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric public: 1100b57cec5SDimitry Andric DanglingDebugInfo() = default; 1110b57cec5SDimitry Andric DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) 1120b57cec5SDimitry Andric : DI(di), dl(std::move(DL)), SDNodeOrder(SDNO) {} 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric const DbgValueInst* getDI() { return DI; } 1150b57cec5SDimitry Andric DebugLoc getdl() { return dl; } 1160b57cec5SDimitry Andric unsigned getSDNodeOrder() { return SDNodeOrder; } 1170b57cec5SDimitry Andric }; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric /// Helper type for DanglingDebugInfoMap. 1200b57cec5SDimitry Andric typedef std::vector<DanglingDebugInfo> DanglingDebugInfoVector; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric /// Keeps track of dbg_values for which we have not yet seen the referent. 1230b57cec5SDimitry Andric /// We defer handling these until we do see it. 1240b57cec5SDimitry Andric MapVector<const Value*, DanglingDebugInfoVector> DanglingDebugInfoMap; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric public: 1270b57cec5SDimitry Andric /// Loads are not emitted to the program immediately. We bunch them up and 1280b57cec5SDimitry Andric /// then emit token factor nodes when possible. This allows us to get simple 1290b57cec5SDimitry Andric /// disambiguation between loads without worrying about alias analysis. 1300b57cec5SDimitry Andric SmallVector<SDValue, 8> PendingLoads; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric /// State used while lowering a statepoint sequence (gc_statepoint, 1330b57cec5SDimitry Andric /// gc_relocate, and gc_result). See StatepointLowering.hpp/cpp for details. 1340b57cec5SDimitry Andric StatepointLoweringState StatepointLowering; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric private: 1370b57cec5SDimitry Andric /// CopyToReg nodes that copy values to virtual registers for export to other 1380b57cec5SDimitry Andric /// blocks need to be emitted before any terminator instruction, but they have 1390b57cec5SDimitry Andric /// no other ordering requirements. We bunch them up and the emit a single 1400b57cec5SDimitry Andric /// tokenfactor for them just before terminator instructions. 1410b57cec5SDimitry Andric SmallVector<SDValue, 8> PendingExports; 1420b57cec5SDimitry Andric 143480093f4SDimitry Andric /// Similar to loads, nodes corresponding to constrained FP intrinsics are 144480093f4SDimitry Andric /// bunched up and emitted when necessary. These can be moved across each 145480093f4SDimitry Andric /// other and any (normal) memory operation (load or store), but not across 146480093f4SDimitry Andric /// calls or instructions having unspecified side effects. As a special 147480093f4SDimitry Andric /// case, constrained FP intrinsics using fpexcept.strict may not be deleted 148480093f4SDimitry Andric /// even if otherwise unused, so they need to be chained before any 149480093f4SDimitry Andric /// terminator instruction (like PendingExports). We track the latter 150480093f4SDimitry Andric /// set of nodes in a separate list. 151480093f4SDimitry Andric SmallVector<SDValue, 8> PendingConstrainedFP; 152480093f4SDimitry Andric SmallVector<SDValue, 8> PendingConstrainedFPStrict; 153480093f4SDimitry Andric 154480093f4SDimitry Andric /// Update root to include all chains from the Pending list. 155480093f4SDimitry Andric SDValue updateRoot(SmallVectorImpl<SDValue> &Pending); 156480093f4SDimitry Andric 1570b57cec5SDimitry Andric /// A unique monotonically increasing number used to order the SDNodes we 1580b57cec5SDimitry Andric /// create. 1590b57cec5SDimitry Andric unsigned SDNodeOrder; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric /// Determine the rank by weight of CC in [First,Last]. If CC has more weight 1620b57cec5SDimitry Andric /// than each cluster in the range, its rank is 0. 1630b57cec5SDimitry Andric unsigned caseClusterRank(const SwitchCG::CaseCluster &CC, 1640b57cec5SDimitry Andric SwitchCG::CaseClusterIt First, 1650b57cec5SDimitry Andric SwitchCG::CaseClusterIt Last); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric /// Emit comparison and split W into two subtrees. 1680b57cec5SDimitry Andric void splitWorkItem(SwitchCG::SwitchWorkList &WorkList, 1690b57cec5SDimitry Andric const SwitchCG::SwitchWorkListItem &W, Value *Cond, 1700b57cec5SDimitry Andric MachineBasicBlock *SwitchMBB); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric /// Lower W. 1730b57cec5SDimitry Andric void lowerWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond, 1740b57cec5SDimitry Andric MachineBasicBlock *SwitchMBB, 1750b57cec5SDimitry Andric MachineBasicBlock *DefaultMBB); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric /// Peel the top probability case if it exceeds the threshold 1780b57cec5SDimitry Andric MachineBasicBlock * 1790b57cec5SDimitry Andric peelDominantCaseCluster(const SwitchInst &SI, 1800b57cec5SDimitry Andric SwitchCG::CaseClusterVector &Clusters, 1810b57cec5SDimitry Andric BranchProbability &PeeledCaseProb); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric /// A class which encapsulates all of the information needed to generate a 1840b57cec5SDimitry Andric /// stack protector check and signals to isel via its state being initialized 1850b57cec5SDimitry Andric /// that a stack protector needs to be generated. 1860b57cec5SDimitry Andric /// 1870b57cec5SDimitry Andric /// *NOTE* The following is a high level documentation of SelectionDAG Stack 1880b57cec5SDimitry Andric /// Protector Generation. The reason that it is placed here is for a lack of 1890b57cec5SDimitry Andric /// other good places to stick it. 1900b57cec5SDimitry Andric /// 1910b57cec5SDimitry Andric /// High Level Overview of SelectionDAG Stack Protector Generation: 1920b57cec5SDimitry Andric /// 1930b57cec5SDimitry Andric /// Previously, generation of stack protectors was done exclusively in the 1940b57cec5SDimitry Andric /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated 1950b57cec5SDimitry Andric /// splitting basic blocks at the IR level to create the success/failure basic 1960b57cec5SDimitry Andric /// blocks in the tail of the basic block in question. As a result of this, 1970b57cec5SDimitry Andric /// calls that would have qualified for the sibling call optimization were no 1980b57cec5SDimitry Andric /// longer eligible for optimization since said calls were no longer right in 1990b57cec5SDimitry Andric /// the "tail position" (i.e. the immediate predecessor of a ReturnInst 2000b57cec5SDimitry Andric /// instruction). 2010b57cec5SDimitry Andric /// 2020b57cec5SDimitry Andric /// Then it was noticed that since the sibling call optimization causes the 2030b57cec5SDimitry Andric /// callee to reuse the caller's stack, if we could delay the generation of 2040b57cec5SDimitry Andric /// the stack protector check until later in CodeGen after the sibling call 2050b57cec5SDimitry Andric /// decision was made, we get both the tail call optimization and the stack 2060b57cec5SDimitry Andric /// protector check! 2070b57cec5SDimitry Andric /// 2080b57cec5SDimitry Andric /// A few goals in solving this problem were: 2090b57cec5SDimitry Andric /// 2100b57cec5SDimitry Andric /// 1. Preserve the architecture independence of stack protector generation. 2110b57cec5SDimitry Andric /// 2120b57cec5SDimitry Andric /// 2. Preserve the normal IR level stack protector check for platforms like 2130b57cec5SDimitry Andric /// OpenBSD for which we support platform-specific stack protector 2140b57cec5SDimitry Andric /// generation. 2150b57cec5SDimitry Andric /// 2160b57cec5SDimitry Andric /// The main problem that guided the present solution is that one can not 2170b57cec5SDimitry Andric /// solve this problem in an architecture independent manner at the IR level 2180b57cec5SDimitry Andric /// only. This is because: 2190b57cec5SDimitry Andric /// 2200b57cec5SDimitry Andric /// 1. The decision on whether or not to perform a sibling call on certain 2210b57cec5SDimitry Andric /// platforms (for instance i386) requires lower level information 2220b57cec5SDimitry Andric /// related to available registers that can not be known at the IR level. 2230b57cec5SDimitry Andric /// 2240b57cec5SDimitry Andric /// 2. Even if the previous point were not true, the decision on whether to 2250b57cec5SDimitry Andric /// perform a tail call is done in LowerCallTo in SelectionDAG which 2260b57cec5SDimitry Andric /// occurs after the Stack Protector Pass. As a result, one would need to 2270b57cec5SDimitry Andric /// put the relevant callinst into the stack protector check success 2280b57cec5SDimitry Andric /// basic block (where the return inst is placed) and then move it back 2290b57cec5SDimitry Andric /// later at SelectionDAG/MI time before the stack protector check if the 2300b57cec5SDimitry Andric /// tail call optimization failed. The MI level option was nixed 2310b57cec5SDimitry Andric /// immediately since it would require platform-specific pattern 2320b57cec5SDimitry Andric /// matching. The SelectionDAG level option was nixed because 2330b57cec5SDimitry Andric /// SelectionDAG only processes one IR level basic block at a time 2340b57cec5SDimitry Andric /// implying one could not create a DAG Combine to move the callinst. 2350b57cec5SDimitry Andric /// 2360b57cec5SDimitry Andric /// To get around this problem a few things were realized: 2370b57cec5SDimitry Andric /// 2380b57cec5SDimitry Andric /// 1. While one can not handle multiple IR level basic blocks at the 2390b57cec5SDimitry Andric /// SelectionDAG Level, one can generate multiple machine basic blocks 2400b57cec5SDimitry Andric /// for one IR level basic block. This is how we handle bit tests and 2410b57cec5SDimitry Andric /// switches. 2420b57cec5SDimitry Andric /// 2430b57cec5SDimitry Andric /// 2. At the MI level, tail calls are represented via a special return 2440b57cec5SDimitry Andric /// MIInst called "tcreturn". Thus if we know the basic block in which we 2450b57cec5SDimitry Andric /// wish to insert the stack protector check, we get the correct behavior 2460b57cec5SDimitry Andric /// by always inserting the stack protector check right before the return 2470b57cec5SDimitry Andric /// statement. This is a "magical transformation" since no matter where 2480b57cec5SDimitry Andric /// the stack protector check intrinsic is, we always insert the stack 2490b57cec5SDimitry Andric /// protector check code at the end of the BB. 2500b57cec5SDimitry Andric /// 2510b57cec5SDimitry Andric /// Given the aforementioned constraints, the following solution was devised: 2520b57cec5SDimitry Andric /// 2530b57cec5SDimitry Andric /// 1. On platforms that do not support SelectionDAG stack protector check 2540b57cec5SDimitry Andric /// generation, allow for the normal IR level stack protector check 2550b57cec5SDimitry Andric /// generation to continue. 2560b57cec5SDimitry Andric /// 2570b57cec5SDimitry Andric /// 2. On platforms that do support SelectionDAG stack protector check 2580b57cec5SDimitry Andric /// generation: 2590b57cec5SDimitry Andric /// 2600b57cec5SDimitry Andric /// a. Use the IR level stack protector pass to decide if a stack 2610b57cec5SDimitry Andric /// protector is required/which BB we insert the stack protector check 2620b57cec5SDimitry Andric /// in by reusing the logic already therein. If we wish to generate a 2630b57cec5SDimitry Andric /// stack protector check in a basic block, we place a special IR 2640b57cec5SDimitry Andric /// intrinsic called llvm.stackprotectorcheck right before the BB's 2650b57cec5SDimitry Andric /// returninst or if there is a callinst that could potentially be 2660b57cec5SDimitry Andric /// sibling call optimized, before the call inst. 2670b57cec5SDimitry Andric /// 2680b57cec5SDimitry Andric /// b. Then when a BB with said intrinsic is processed, we codegen the BB 2690b57cec5SDimitry Andric /// normally via SelectBasicBlock. In said process, when we visit the 2700b57cec5SDimitry Andric /// stack protector check, we do not actually emit anything into the 2710b57cec5SDimitry Andric /// BB. Instead, we just initialize the stack protector descriptor 2720b57cec5SDimitry Andric /// class (which involves stashing information/creating the success 2730b57cec5SDimitry Andric /// mbbb and the failure mbb if we have not created one for this 2740b57cec5SDimitry Andric /// function yet) and export the guard variable that we are going to 2750b57cec5SDimitry Andric /// compare. 2760b57cec5SDimitry Andric /// 2770b57cec5SDimitry Andric /// c. After we finish selecting the basic block, in FinishBasicBlock if 2780b57cec5SDimitry Andric /// the StackProtectorDescriptor attached to the SelectionDAGBuilder is 2790b57cec5SDimitry Andric /// initialized, we produce the validation code with one of these 2800b57cec5SDimitry Andric /// techniques: 2810b57cec5SDimitry Andric /// 1) with a call to a guard check function 2820b57cec5SDimitry Andric /// 2) with inlined instrumentation 2830b57cec5SDimitry Andric /// 2840b57cec5SDimitry Andric /// 1) We insert a call to the check function before the terminator. 2850b57cec5SDimitry Andric /// 2860b57cec5SDimitry Andric /// 2) We first find a splice point in the parent basic block 2870b57cec5SDimitry Andric /// before the terminator and then splice the terminator of said basic 2880b57cec5SDimitry Andric /// block into the success basic block. Then we code-gen a new tail for 2890b57cec5SDimitry Andric /// the parent basic block consisting of the two loads, the comparison, 2900b57cec5SDimitry Andric /// and finally two branches to the success/failure basic blocks. We 2910b57cec5SDimitry Andric /// conclude by code-gening the failure basic block if we have not 2920b57cec5SDimitry Andric /// code-gened it already (all stack protector checks we generate in 2930b57cec5SDimitry Andric /// the same function, use the same failure basic block). 2940b57cec5SDimitry Andric class StackProtectorDescriptor { 2950b57cec5SDimitry Andric public: 2960b57cec5SDimitry Andric StackProtectorDescriptor() = default; 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric /// Returns true if all fields of the stack protector descriptor are 2990b57cec5SDimitry Andric /// initialized implying that we should/are ready to emit a stack protector. 3000b57cec5SDimitry Andric bool shouldEmitStackProtector() const { 3010b57cec5SDimitry Andric return ParentMBB && SuccessMBB && FailureMBB; 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric bool shouldEmitFunctionBasedCheckStackProtector() const { 3050b57cec5SDimitry Andric return ParentMBB && !SuccessMBB && !FailureMBB; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric /// Initialize the stack protector descriptor structure for a new basic 3090b57cec5SDimitry Andric /// block. 3100b57cec5SDimitry Andric void initialize(const BasicBlock *BB, MachineBasicBlock *MBB, 3110b57cec5SDimitry Andric bool FunctionBasedInstrumentation) { 3120b57cec5SDimitry Andric // Make sure we are not initialized yet. 3130b57cec5SDimitry Andric assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " 3140b57cec5SDimitry Andric "already initialized!"); 3150b57cec5SDimitry Andric ParentMBB = MBB; 3160b57cec5SDimitry Andric if (!FunctionBasedInstrumentation) { 3170b57cec5SDimitry Andric SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true); 3180b57cec5SDimitry Andric FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric /// Reset state that changes when we handle different basic blocks. 3230b57cec5SDimitry Andric /// 3240b57cec5SDimitry Andric /// This currently includes: 3250b57cec5SDimitry Andric /// 3260b57cec5SDimitry Andric /// 1. The specific basic block we are generating a 3270b57cec5SDimitry Andric /// stack protector for (ParentMBB). 3280b57cec5SDimitry Andric /// 3290b57cec5SDimitry Andric /// 2. The successor machine basic block that will contain the tail of 3300b57cec5SDimitry Andric /// parent mbb after we create the stack protector check (SuccessMBB). This 3310b57cec5SDimitry Andric /// BB is visited only on stack protector check success. 3320b57cec5SDimitry Andric void resetPerBBState() { 3330b57cec5SDimitry Andric ParentMBB = nullptr; 3340b57cec5SDimitry Andric SuccessMBB = nullptr; 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric /// Reset state that only changes when we switch functions. 3380b57cec5SDimitry Andric /// 3390b57cec5SDimitry Andric /// This currently includes: 3400b57cec5SDimitry Andric /// 3410b57cec5SDimitry Andric /// 1. FailureMBB since we reuse the failure code path for all stack 3420b57cec5SDimitry Andric /// protector checks created in an individual function. 3430b57cec5SDimitry Andric /// 3440b57cec5SDimitry Andric /// 2.The guard variable since the guard variable we are checking against is 3450b57cec5SDimitry Andric /// always the same. 3460b57cec5SDimitry Andric void resetPerFunctionState() { 3470b57cec5SDimitry Andric FailureMBB = nullptr; 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric MachineBasicBlock *getParentMBB() { return ParentMBB; } 3510b57cec5SDimitry Andric MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } 3520b57cec5SDimitry Andric MachineBasicBlock *getFailureMBB() { return FailureMBB; } 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric private: 3550b57cec5SDimitry Andric /// The basic block for which we are generating the stack protector. 3560b57cec5SDimitry Andric /// 3570b57cec5SDimitry Andric /// As a result of stack protector generation, we will splice the 3580b57cec5SDimitry Andric /// terminators of this basic block into the successor mbb SuccessMBB and 3590b57cec5SDimitry Andric /// replace it with a compare/branch to the successor mbbs 3600b57cec5SDimitry Andric /// SuccessMBB/FailureMBB depending on whether or not the stack protector 3610b57cec5SDimitry Andric /// was violated. 3620b57cec5SDimitry Andric MachineBasicBlock *ParentMBB = nullptr; 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric /// A basic block visited on stack protector check success that contains the 3650b57cec5SDimitry Andric /// terminators of ParentMBB. 3660b57cec5SDimitry Andric MachineBasicBlock *SuccessMBB = nullptr; 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric /// This basic block visited on stack protector check failure that will 3690b57cec5SDimitry Andric /// contain a call to __stack_chk_fail(). 3700b57cec5SDimitry Andric MachineBasicBlock *FailureMBB = nullptr; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric /// Add a successor machine basic block to ParentMBB. If the successor mbb 3730b57cec5SDimitry Andric /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic 3740b57cec5SDimitry Andric /// block will be created. Assign a large weight if IsLikely is true. 3750b57cec5SDimitry Andric MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, 3760b57cec5SDimitry Andric MachineBasicBlock *ParentMBB, 3770b57cec5SDimitry Andric bool IsLikely, 3780b57cec5SDimitry Andric MachineBasicBlock *SuccMBB = nullptr); 3790b57cec5SDimitry Andric }; 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric private: 3820b57cec5SDimitry Andric const TargetMachine &TM; 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric public: 3850b57cec5SDimitry Andric /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling 3860b57cec5SDimitry Andric /// nodes without a corresponding SDNode. 3870b57cec5SDimitry Andric static const unsigned LowestSDNodeOrder = 1; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric SelectionDAG &DAG; 3900b57cec5SDimitry Andric const DataLayout *DL = nullptr; 391e8d8bef9SDimitry Andric AAResults *AA = nullptr; 3920b57cec5SDimitry Andric const TargetLibraryInfo *LibInfo; 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric class SDAGSwitchLowering : public SwitchCG::SwitchLowering { 3950b57cec5SDimitry Andric public: 3960b57cec5SDimitry Andric SDAGSwitchLowering(SelectionDAGBuilder *sdb, FunctionLoweringInfo &funcinfo) 3970b57cec5SDimitry Andric : SwitchCG::SwitchLowering(funcinfo), SDB(sdb) {} 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric virtual void addSuccessorWithProb( 4000b57cec5SDimitry Andric MachineBasicBlock *Src, MachineBasicBlock *Dst, 4010b57cec5SDimitry Andric BranchProbability Prob = BranchProbability::getUnknown()) override { 4020b57cec5SDimitry Andric SDB->addSuccessorWithProb(Src, Dst, Prob); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric private: 4060b57cec5SDimitry Andric SelectionDAGBuilder *SDB; 4070b57cec5SDimitry Andric }; 4080b57cec5SDimitry Andric 4095ffd83dbSDimitry Andric // Data related to deferred switch lowerings. Used to construct additional 4105ffd83dbSDimitry Andric // Basic Blocks in SelectionDAGISel::FinishBasicBlock. 4110b57cec5SDimitry Andric std::unique_ptr<SDAGSwitchLowering> SL; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric /// A StackProtectorDescriptor structure used to communicate stack protector 4140b57cec5SDimitry Andric /// information in between SelectBasicBlock and FinishBasicBlock. 4150b57cec5SDimitry Andric StackProtectorDescriptor SPDescriptor; 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric // Emit PHI-node-operand constants only once even if used by multiple 4180b57cec5SDimitry Andric // PHI nodes. 4190b57cec5SDimitry Andric DenseMap<const Constant *, unsigned> ConstantsOut; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric /// Information about the function as a whole. 4220b57cec5SDimitry Andric FunctionLoweringInfo &FuncInfo; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric /// Information about the swifterror values used throughout the function. 4250b57cec5SDimitry Andric SwiftErrorValueTracking &SwiftError; 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric /// Garbage collection metadata for the function. 4280b57cec5SDimitry Andric GCFunctionInfo *GFI; 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric /// Map a landing pad to the call site indexes. 4310b57cec5SDimitry Andric DenseMap<MachineBasicBlock *, SmallVector<unsigned, 4>> LPadToCallSiteMap; 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric /// This is set to true if a call in the current block has been translated as 4340b57cec5SDimitry Andric /// a tail call. In this case, no subsequent DAG nodes should be created. 4350b57cec5SDimitry Andric bool HasTailCall = false; 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric LLVMContext *Context; 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, 4400b57cec5SDimitry Andric SwiftErrorValueTracking &swifterror, CodeGenOpt::Level ol) 4410b57cec5SDimitry Andric : SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), DAG(dag), 4428bcb0991SDimitry Andric SL(std::make_unique<SDAGSwitchLowering>(this, funcinfo)), FuncInfo(funcinfo), 4430b57cec5SDimitry Andric SwiftError(swifterror) {} 4440b57cec5SDimitry Andric 445e8d8bef9SDimitry Andric void init(GCFunctionInfo *gfi, AAResults *AA, 4460b57cec5SDimitry Andric const TargetLibraryInfo *li); 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric /// Clear out the current SelectionDAG and the associated state and prepare 4490b57cec5SDimitry Andric /// this SelectionDAGBuilder object to be used for a new block. This doesn't 4500b57cec5SDimitry Andric /// clear out information about additional blocks that are needed to complete 4510b57cec5SDimitry Andric /// switch lowering or PHI node updating; that information is cleared out as 4520b57cec5SDimitry Andric /// it is consumed. 4530b57cec5SDimitry Andric void clear(); 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric /// Clear the dangling debug information map. This function is separated from 4560b57cec5SDimitry Andric /// the clear so that debug information that is dangling in a basic block can 4570b57cec5SDimitry Andric /// be properly resolved in a different basic block. This allows the 4580b57cec5SDimitry Andric /// SelectionDAG to resolve dangling debug information attached to PHI nodes. 4590b57cec5SDimitry Andric void clearDanglingDebugInfo(); 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric /// Return the current virtual root of the Selection DAG, flushing any 4620b57cec5SDimitry Andric /// PendingLoad items. This must be done before emitting a store or any other 463480093f4SDimitry Andric /// memory node that may need to be ordered after any prior load instructions. 464480093f4SDimitry Andric SDValue getMemoryRoot(); 465480093f4SDimitry Andric 466480093f4SDimitry Andric /// Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) 467480093f4SDimitry Andric /// items. This must be done before emitting any call other any other node 468480093f4SDimitry Andric /// that may need to be ordered after FP instructions due to other side 469480093f4SDimitry Andric /// effects. 4700b57cec5SDimitry Andric SDValue getRoot(); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric /// Similar to getRoot, but instead of flushing all the PendingLoad items, 473480093f4SDimitry Andric /// flush all the PendingExports (and PendingConstrainedFPStrict) items. 474480093f4SDimitry Andric /// It is necessary to do this before emitting a terminator instruction. 4750b57cec5SDimitry Andric SDValue getControlRoot(); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric SDLoc getCurSDLoc() const { 4780b57cec5SDimitry Andric return SDLoc(CurInst, SDNodeOrder); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric DebugLoc getCurDebugLoc() const { 4820b57cec5SDimitry Andric return CurInst ? CurInst->getDebugLoc() : DebugLoc(); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric void CopyValueToVirtualRegister(const Value *V, unsigned Reg); 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric void visit(const Instruction &I); 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric void visit(unsigned Opcode, const User &I); 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric /// If there was virtual register allocated for the value V emit CopyFromReg 4920b57cec5SDimitry Andric /// of the specified type Ty. Return empty SDValue() otherwise. 4930b57cec5SDimitry Andric SDValue getCopyFromRegs(const Value *V, Type *Ty); 4940b57cec5SDimitry Andric 495*fe6060f1SDimitry Andric /// Register a dbg_value which relies on a Value which we have not yet seen. 496*fe6060f1SDimitry Andric void addDanglingDebugInfo(const DbgValueInst *DI, DebugLoc DL, 497*fe6060f1SDimitry Andric unsigned Order); 498*fe6060f1SDimitry Andric 4990b57cec5SDimitry Andric /// If we have dangling debug info that describes \p Variable, or an 5000b57cec5SDimitry Andric /// overlapping part of variable considering the \p Expr, then this method 5010b57cec5SDimitry Andric /// will drop that debug info as it isn't valid any longer. 5020b57cec5SDimitry Andric void dropDanglingDebugInfo(const DILocalVariable *Variable, 5030b57cec5SDimitry Andric const DIExpression *Expr); 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric /// If we saw an earlier dbg_value referring to V, generate the debug data 5060b57cec5SDimitry Andric /// structures now that we've seen its definition. 5070b57cec5SDimitry Andric void resolveDanglingDebugInfo(const Value *V, SDValue Val); 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric /// For the given dangling debuginfo record, perform last-ditch efforts to 5100b57cec5SDimitry Andric /// resolve the debuginfo to something that is represented in this DAG. If 5110b57cec5SDimitry Andric /// this cannot be done, produce an Undef debug value record. 5120b57cec5SDimitry Andric void salvageUnresolvedDbgValue(DanglingDebugInfo &DDI); 5130b57cec5SDimitry Andric 514*fe6060f1SDimitry Andric /// For a given list of Values, attempt to create and record a SDDbgValue in 515*fe6060f1SDimitry Andric /// the SelectionDAG. 516*fe6060f1SDimitry Andric bool handleDebugValue(ArrayRef<const Value *> Values, DILocalVariable *Var, 517*fe6060f1SDimitry Andric DIExpression *Expr, DebugLoc CurDL, DebugLoc InstDL, 518*fe6060f1SDimitry Andric unsigned Order, bool IsVariadic); 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric /// Evict any dangling debug information, attempting to salvage it first. 5210b57cec5SDimitry Andric void resolveOrClearDbgInfo(); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric SDValue getValue(const Value *V); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric SDValue getNonRegisterValue(const Value *V); 5260b57cec5SDimitry Andric SDValue getValueImpl(const Value *V); 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric void setValue(const Value *V, SDValue NewN) { 5290b57cec5SDimitry Andric SDValue &N = NodeMap[V]; 5300b57cec5SDimitry Andric assert(!N.getNode() && "Already set a value for this node!"); 5310b57cec5SDimitry Andric N = NewN; 5320b57cec5SDimitry Andric } 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric void setUnusedArgValue(const Value *V, SDValue NewN) { 5350b57cec5SDimitry Andric SDValue &N = UnusedArgNodeMap[V]; 5360b57cec5SDimitry Andric assert(!N.getNode() && "Already set a value for this node!"); 5370b57cec5SDimitry Andric N = NewN; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, 5410b57cec5SDimitry Andric MachineBasicBlock *FBB, MachineBasicBlock *CurBB, 5420b57cec5SDimitry Andric MachineBasicBlock *SwitchBB, 5430b57cec5SDimitry Andric Instruction::BinaryOps Opc, BranchProbability TProb, 5440b57cec5SDimitry Andric BranchProbability FProb, bool InvertCond); 5450b57cec5SDimitry Andric void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, 5460b57cec5SDimitry Andric MachineBasicBlock *FBB, 5470b57cec5SDimitry Andric MachineBasicBlock *CurBB, 5480b57cec5SDimitry Andric MachineBasicBlock *SwitchBB, 5490b57cec5SDimitry Andric BranchProbability TProb, BranchProbability FProb, 5500b57cec5SDimitry Andric bool InvertCond); 5510b57cec5SDimitry Andric bool ShouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases); 5520b57cec5SDimitry Andric bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); 5530b57cec5SDimitry Andric void CopyToExportRegsIfNeeded(const Value *V); 5540b57cec5SDimitry Andric void ExportFromCurrentBlock(const Value *V); 5555ffd83dbSDimitry Andric void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, 556*fe6060f1SDimitry Andric bool IsMustTailCall, const BasicBlock *EHPadBB = nullptr); 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric // Lower range metadata from 0 to N to assert zext to an integer of nearest 5590b57cec5SDimitry Andric // floor power of two. 5600b57cec5SDimitry Andric SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, 5610b57cec5SDimitry Andric SDValue Op); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, 5640b57cec5SDimitry Andric const CallBase *Call, unsigned ArgIdx, 5650b57cec5SDimitry Andric unsigned NumArgs, SDValue Callee, 5660b57cec5SDimitry Andric Type *ReturnTy, bool IsPatchPoint); 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric std::pair<SDValue, SDValue> 5690b57cec5SDimitry Andric lowerInvokable(TargetLowering::CallLoweringInfo &CLI, 5700b57cec5SDimitry Andric const BasicBlock *EHPadBB = nullptr); 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric /// When an MBB was split during scheduling, update the 5730b57cec5SDimitry Andric /// references that need to refer to the last resulting block. 5740b57cec5SDimitry Andric void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes 5770b57cec5SDimitry Andric /// of lowering into a STATEPOINT node. 5780b57cec5SDimitry Andric struct StatepointLoweringInfo { 5790b57cec5SDimitry Andric /// Bases[i] is the base pointer for Ptrs[i]. Together they denote the set 5800b57cec5SDimitry Andric /// of gc pointers this STATEPOINT has to relocate. 5810b57cec5SDimitry Andric SmallVector<const Value *, 16> Bases; 5820b57cec5SDimitry Andric SmallVector<const Value *, 16> Ptrs; 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric /// The set of gc.relocate calls associated with this gc.statepoint. 5850b57cec5SDimitry Andric SmallVector<const GCRelocateInst *, 16> GCRelocates; 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric /// The full list of gc arguments to the gc.statepoint being lowered. 5880b57cec5SDimitry Andric ArrayRef<const Use> GCArgs; 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric /// The gc.statepoint instruction. 5910b57cec5SDimitry Andric const Instruction *StatepointInstr = nullptr; 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric /// The list of gc transition arguments present in the gc.statepoint being 5940b57cec5SDimitry Andric /// lowered. 5950b57cec5SDimitry Andric ArrayRef<const Use> GCTransitionArgs; 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric /// The ID that the resulting STATEPOINT instruction has to report. 5980b57cec5SDimitry Andric unsigned ID = -1; 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric /// Information regarding the underlying call instruction. 6010b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI; 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric /// The deoptimization state associated with this gc.statepoint call, if 6040b57cec5SDimitry Andric /// any. 6050b57cec5SDimitry Andric ArrayRef<const Use> DeoptState; 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric /// Flags associated with the meta arguments being lowered. 6080b57cec5SDimitry Andric uint64_t StatepointFlags = -1; 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric /// The number of patchable bytes the call needs to get lowered into. 6110b57cec5SDimitry Andric unsigned NumPatchBytes = -1; 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric /// The exception handling unwind destination, in case this represents an 6140b57cec5SDimitry Andric /// invoke of gc.statepoint. 6150b57cec5SDimitry Andric const BasicBlock *EHPadBB = nullptr; 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {} 6180b57cec5SDimitry Andric }; 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric /// Lower \p SLI into a STATEPOINT instruction. 6210b57cec5SDimitry Andric SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SI); 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric // This function is responsible for the whole statepoint lowering process. 6240b57cec5SDimitry Andric // It uniformly handles invoke and call statepoints. 6255ffd83dbSDimitry Andric void LowerStatepoint(const GCStatepointInst &I, 6260b57cec5SDimitry Andric const BasicBlock *EHPadBB = nullptr); 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, 6290b57cec5SDimitry Andric const BasicBlock *EHPadBB); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric void LowerDeoptimizeCall(const CallInst *CI); 6320b57cec5SDimitry Andric void LowerDeoptimizingReturn(); 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric void LowerCallSiteWithDeoptBundleImpl(const CallBase *Call, SDValue Callee, 6350b57cec5SDimitry Andric const BasicBlock *EHPadBB, 6360b57cec5SDimitry Andric bool VarArgDisallowed, 6370b57cec5SDimitry Andric bool ForceVoidReturnTy); 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric /// Returns the type of FrameIndex and TargetFrameIndex nodes. 6400b57cec5SDimitry Andric MVT getFrameIndexTy() { 6410b57cec5SDimitry Andric return DAG.getTargetLoweringInfo().getFrameIndexTy(DAG.getDataLayout()); 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric private: 6450b57cec5SDimitry Andric // Terminator instructions. 6460b57cec5SDimitry Andric void visitRet(const ReturnInst &I); 6470b57cec5SDimitry Andric void visitBr(const BranchInst &I); 6480b57cec5SDimitry Andric void visitSwitch(const SwitchInst &I); 6490b57cec5SDimitry Andric void visitIndirectBr(const IndirectBrInst &I); 6500b57cec5SDimitry Andric void visitUnreachable(const UnreachableInst &I); 6510b57cec5SDimitry Andric void visitCleanupRet(const CleanupReturnInst &I); 6520b57cec5SDimitry Andric void visitCatchSwitch(const CatchSwitchInst &I); 6530b57cec5SDimitry Andric void visitCatchRet(const CatchReturnInst &I); 6540b57cec5SDimitry Andric void visitCatchPad(const CatchPadInst &I); 6550b57cec5SDimitry Andric void visitCleanupPad(const CleanupPadInst &CPI); 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric BranchProbability getEdgeProbability(const MachineBasicBlock *Src, 6580b57cec5SDimitry Andric const MachineBasicBlock *Dst) const; 6590b57cec5SDimitry Andric void addSuccessorWithProb( 6600b57cec5SDimitry Andric MachineBasicBlock *Src, MachineBasicBlock *Dst, 6610b57cec5SDimitry Andric BranchProbability Prob = BranchProbability::getUnknown()); 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric public: 6640b57cec5SDimitry Andric void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB); 6650b57cec5SDimitry Andric void visitSPDescriptorParent(StackProtectorDescriptor &SPD, 6660b57cec5SDimitry Andric MachineBasicBlock *ParentBB); 6670b57cec5SDimitry Andric void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); 6680b57cec5SDimitry Andric void visitBitTestHeader(SwitchCG::BitTestBlock &B, 6690b57cec5SDimitry Andric MachineBasicBlock *SwitchBB); 6700b57cec5SDimitry Andric void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, 6710b57cec5SDimitry Andric BranchProbability BranchProbToNext, unsigned Reg, 6720b57cec5SDimitry Andric SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB); 6730b57cec5SDimitry Andric void visitJumpTable(SwitchCG::JumpTable &JT); 6740b57cec5SDimitry Andric void visitJumpTableHeader(SwitchCG::JumpTable &JT, 6750b57cec5SDimitry Andric SwitchCG::JumpTableHeader &JTH, 6760b57cec5SDimitry Andric MachineBasicBlock *SwitchBB); 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric private: 6790b57cec5SDimitry Andric // These all get lowered before this pass. 6800b57cec5SDimitry Andric void visitInvoke(const InvokeInst &I); 6810b57cec5SDimitry Andric void visitCallBr(const CallBrInst &I); 6820b57cec5SDimitry Andric void visitResume(const ResumeInst &I); 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric void visitUnary(const User &I, unsigned Opcode); 6850b57cec5SDimitry Andric void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); } 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric void visitBinary(const User &I, unsigned Opcode); 6880b57cec5SDimitry Andric void visitShift(const User &I, unsigned Opcode); 6890b57cec5SDimitry Andric void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } 6900b57cec5SDimitry Andric void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } 6910b57cec5SDimitry Andric void visitSub(const User &I) { visitBinary(I, ISD::SUB); } 692e8d8bef9SDimitry Andric void visitFSub(const User &I) { visitBinary(I, ISD::FSUB); } 6930b57cec5SDimitry Andric void visitMul(const User &I) { visitBinary(I, ISD::MUL); } 6940b57cec5SDimitry Andric void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } 6950b57cec5SDimitry Andric void visitURem(const User &I) { visitBinary(I, ISD::UREM); } 6960b57cec5SDimitry Andric void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } 6970b57cec5SDimitry Andric void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } 6980b57cec5SDimitry Andric void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } 6990b57cec5SDimitry Andric void visitSDiv(const User &I); 7000b57cec5SDimitry Andric void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } 7010b57cec5SDimitry Andric void visitAnd (const User &I) { visitBinary(I, ISD::AND); } 7020b57cec5SDimitry Andric void visitOr (const User &I) { visitBinary(I, ISD::OR); } 7030b57cec5SDimitry Andric void visitXor (const User &I) { visitBinary(I, ISD::XOR); } 7040b57cec5SDimitry Andric void visitShl (const User &I) { visitShift(I, ISD::SHL); } 7050b57cec5SDimitry Andric void visitLShr(const User &I) { visitShift(I, ISD::SRL); } 7060b57cec5SDimitry Andric void visitAShr(const User &I) { visitShift(I, ISD::SRA); } 7070b57cec5SDimitry Andric void visitICmp(const User &I); 7080b57cec5SDimitry Andric void visitFCmp(const User &I); 7090b57cec5SDimitry Andric // Visit the conversion instructions 7100b57cec5SDimitry Andric void visitTrunc(const User &I); 7110b57cec5SDimitry Andric void visitZExt(const User &I); 7120b57cec5SDimitry Andric void visitSExt(const User &I); 7130b57cec5SDimitry Andric void visitFPTrunc(const User &I); 7140b57cec5SDimitry Andric void visitFPExt(const User &I); 7150b57cec5SDimitry Andric void visitFPToUI(const User &I); 7160b57cec5SDimitry Andric void visitFPToSI(const User &I); 7170b57cec5SDimitry Andric void visitUIToFP(const User &I); 7180b57cec5SDimitry Andric void visitSIToFP(const User &I); 7190b57cec5SDimitry Andric void visitPtrToInt(const User &I); 7200b57cec5SDimitry Andric void visitIntToPtr(const User &I); 7210b57cec5SDimitry Andric void visitBitCast(const User &I); 7220b57cec5SDimitry Andric void visitAddrSpaceCast(const User &I); 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric void visitExtractElement(const User &I); 7250b57cec5SDimitry Andric void visitInsertElement(const User &I); 7260b57cec5SDimitry Andric void visitShuffleVector(const User &I); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric void visitExtractValue(const User &I); 7290b57cec5SDimitry Andric void visitInsertValue(const User &I); 7300b57cec5SDimitry Andric void visitLandingPad(const LandingPadInst &LP); 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric void visitGetElementPtr(const User &I); 7330b57cec5SDimitry Andric void visitSelect(const User &I); 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric void visitAlloca(const AllocaInst &I); 7360b57cec5SDimitry Andric void visitLoad(const LoadInst &I); 7370b57cec5SDimitry Andric void visitStore(const StoreInst &I); 7380b57cec5SDimitry Andric void visitMaskedLoad(const CallInst &I, bool IsExpanding = false); 7390b57cec5SDimitry Andric void visitMaskedStore(const CallInst &I, bool IsCompressing = false); 7400b57cec5SDimitry Andric void visitMaskedGather(const CallInst &I); 7410b57cec5SDimitry Andric void visitMaskedScatter(const CallInst &I); 7420b57cec5SDimitry Andric void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); 7430b57cec5SDimitry Andric void visitAtomicRMW(const AtomicRMWInst &I); 7440b57cec5SDimitry Andric void visitFence(const FenceInst &I); 7450b57cec5SDimitry Andric void visitPHI(const PHINode &I); 7460b57cec5SDimitry Andric void visitCall(const CallInst &I); 747e8d8bef9SDimitry Andric bool visitMemCmpBCmpCall(const CallInst &I); 7480b57cec5SDimitry Andric bool visitMemPCpyCall(const CallInst &I); 7490b57cec5SDimitry Andric bool visitMemChrCall(const CallInst &I); 7500b57cec5SDimitry Andric bool visitStrCpyCall(const CallInst &I, bool isStpcpy); 7510b57cec5SDimitry Andric bool visitStrCmpCall(const CallInst &I); 7520b57cec5SDimitry Andric bool visitStrLenCall(const CallInst &I); 7530b57cec5SDimitry Andric bool visitStrNLenCall(const CallInst &I); 7540b57cec5SDimitry Andric bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); 7550b57cec5SDimitry Andric bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode); 7560b57cec5SDimitry Andric void visitAtomicLoad(const LoadInst &I); 7570b57cec5SDimitry Andric void visitAtomicStore(const StoreInst &I); 7580b57cec5SDimitry Andric void visitLoadFromSwiftError(const LoadInst &I); 7590b57cec5SDimitry Andric void visitStoreToSwiftError(const StoreInst &I); 760480093f4SDimitry Andric void visitFreeze(const FreezeInst &I); 7610b57cec5SDimitry Andric 762*fe6060f1SDimitry Andric void visitInlineAsm(const CallBase &Call, 763*fe6060f1SDimitry Andric const BasicBlock *EHPadBB = nullptr); 7640b57cec5SDimitry Andric void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); 7650b57cec5SDimitry Andric void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); 7660b57cec5SDimitry Andric void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI); 767e8d8bef9SDimitry Andric void visitVectorPredicationIntrinsic(const VPIntrinsic &VPIntrin); 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric void visitVAStart(const CallInst &I); 7700b57cec5SDimitry Andric void visitVAArg(const VAArgInst &I); 7710b57cec5SDimitry Andric void visitVAEnd(const CallInst &I); 7720b57cec5SDimitry Andric void visitVACopy(const CallInst &I); 7730b57cec5SDimitry Andric void visitStackmap(const CallInst &I); 7745ffd83dbSDimitry Andric void visitPatchpoint(const CallBase &CB, const BasicBlock *EHPadBB = nullptr); 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric // These two are implemented in StatepointLowering.cpp 7770b57cec5SDimitry Andric void visitGCRelocate(const GCRelocateInst &Relocate); 7780b57cec5SDimitry Andric void visitGCResult(const GCResultInst &I); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric void visitVectorReduce(const CallInst &I, unsigned Intrinsic); 781*fe6060f1SDimitry Andric void visitVectorReverse(const CallInst &I); 782*fe6060f1SDimitry Andric void visitVectorSplice(const CallInst &I); 783*fe6060f1SDimitry Andric void visitStepVector(const CallInst &I); 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric void visitUserOp1(const Instruction &I) { 7860b57cec5SDimitry Andric llvm_unreachable("UserOp1 should not exist at instruction selection time!"); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric void visitUserOp2(const Instruction &I) { 7890b57cec5SDimitry Andric llvm_unreachable("UserOp2 should not exist at instruction selection time!"); 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric void processIntegerCallValue(const Instruction &I, 7930b57cec5SDimitry Andric SDValue Value, bool IsSigned); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); 7960b57cec5SDimitry Andric 7975ffd83dbSDimitry Andric void emitInlineAsmError(const CallBase &Call, const Twine &Message); 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric /// If V is an function argument then create corresponding DBG_VALUE machine 8000b57cec5SDimitry Andric /// instruction for it now. At the end of instruction selection, they will be 8010b57cec5SDimitry Andric /// inserted to the entry BB. 8020b57cec5SDimitry Andric bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable, 8030b57cec5SDimitry Andric DIExpression *Expr, DILocation *DL, 8040b57cec5SDimitry Andric bool IsDbgDeclare, const SDValue &N); 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric /// Return the next block after MBB, or nullptr if there is none. 8070b57cec5SDimitry Andric MachineBasicBlock *NextBlock(MachineBasicBlock *MBB); 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric /// Update the DAG and DAG builder with the relevant information after 8100b57cec5SDimitry Andric /// a new root node has been created which could be a tail call. 8110b57cec5SDimitry Andric void updateDAGForMaybeTailCall(SDValue MaybeTC); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric /// Return the appropriate SDDbgValue based on N. 8140b57cec5SDimitry Andric SDDbgValue *getDbgValue(SDValue N, DILocalVariable *Variable, 8150b57cec5SDimitry Andric DIExpression *Expr, const DebugLoc &dl, 8160b57cec5SDimitry Andric unsigned DbgSDNodeOrder); 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric /// Lowers CallInst to an external symbol. 8190b57cec5SDimitry Andric void lowerCallToExternalSymbol(const CallInst &I, const char *FunctionName); 820*fe6060f1SDimitry Andric 821*fe6060f1SDimitry Andric SDValue lowerStartEH(SDValue Chain, const BasicBlock *EHPadBB, 822*fe6060f1SDimitry Andric MCSymbol *&BeginLabel); 823*fe6060f1SDimitry Andric SDValue lowerEndEH(SDValue Chain, const InvokeInst *II, 824*fe6060f1SDimitry Andric const BasicBlock *EHPadBB, MCSymbol *BeginLabel); 8250b57cec5SDimitry Andric }; 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric /// This struct represents the registers (physical or virtual) 8280b57cec5SDimitry Andric /// that a particular set of values is assigned, and the type information about 8290b57cec5SDimitry Andric /// the value. The most common situation is to represent one value at a time, 8300b57cec5SDimitry Andric /// but struct or array values are handled element-wise as multiple values. The 8310b57cec5SDimitry Andric /// splitting of aggregates is performed recursively, so that we never have 8320b57cec5SDimitry Andric /// aggregate-typed registers. The values at this point do not necessarily have 8330b57cec5SDimitry Andric /// legal types, so each value may require one or more registers of some legal 8340b57cec5SDimitry Andric /// type. 8350b57cec5SDimitry Andric /// 8360b57cec5SDimitry Andric struct RegsForValue { 8370b57cec5SDimitry Andric /// The value types of the values, which may not be legal, and 8380b57cec5SDimitry Andric /// may need be promoted or synthesized from one or more registers. 8390b57cec5SDimitry Andric SmallVector<EVT, 4> ValueVTs; 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric /// The value types of the registers. This is the same size as ValueVTs and it 8420b57cec5SDimitry Andric /// records, for each value, what the type of the assigned register or 8430b57cec5SDimitry Andric /// registers are. (Individual values are never synthesized from more than one 8440b57cec5SDimitry Andric /// type of register.) 8450b57cec5SDimitry Andric /// 8460b57cec5SDimitry Andric /// With virtual registers, the contents of RegVTs is redundant with TLI's 8470b57cec5SDimitry Andric /// getRegisterType member function, however when with physical registers 8480b57cec5SDimitry Andric /// it is necessary to have a separate record of the types. 8490b57cec5SDimitry Andric SmallVector<MVT, 4> RegVTs; 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric /// This list holds the registers assigned to the values. 8520b57cec5SDimitry Andric /// Each legal or promoted value requires one register, and each 8530b57cec5SDimitry Andric /// expanded value requires multiple registers. 8540b57cec5SDimitry Andric SmallVector<unsigned, 4> Regs; 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric /// This list holds the number of registers for each value. 8570b57cec5SDimitry Andric SmallVector<unsigned, 4> RegCount; 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric /// Records if this value needs to be treated in an ABI dependant manner, 8600b57cec5SDimitry Andric /// different to normal type legalization. 8610b57cec5SDimitry Andric Optional<CallingConv::ID> CallConv; 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric RegsForValue() = default; 8640b57cec5SDimitry Andric RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt, EVT valuevt, 8650b57cec5SDimitry Andric Optional<CallingConv::ID> CC = None); 8660b57cec5SDimitry Andric RegsForValue(LLVMContext &Context, const TargetLowering &TLI, 8670b57cec5SDimitry Andric const DataLayout &DL, unsigned Reg, Type *Ty, 8680b57cec5SDimitry Andric Optional<CallingConv::ID> CC); 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric bool isABIMangled() const { 8710b57cec5SDimitry Andric return CallConv.hasValue(); 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric /// Add the specified values to this one. 8750b57cec5SDimitry Andric void append(const RegsForValue &RHS) { 8760b57cec5SDimitry Andric ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); 8770b57cec5SDimitry Andric RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); 8780b57cec5SDimitry Andric Regs.append(RHS.Regs.begin(), RHS.Regs.end()); 8790b57cec5SDimitry Andric RegCount.push_back(RHS.Regs.size()); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric /// Emit a series of CopyFromReg nodes that copies from this value and returns 8830b57cec5SDimitry Andric /// the result as a ValueVTs value. This uses Chain/Flag as the input and 8840b57cec5SDimitry Andric /// updates them for the output Chain/Flag. If the Flag pointer is NULL, no 8850b57cec5SDimitry Andric /// flag is used. 8860b57cec5SDimitry Andric SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, 8870b57cec5SDimitry Andric const SDLoc &dl, SDValue &Chain, SDValue *Flag, 8880b57cec5SDimitry Andric const Value *V = nullptr) const; 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric /// Emit a series of CopyToReg nodes that copies the specified value into the 8910b57cec5SDimitry Andric /// registers specified by this object. This uses Chain/Flag as the input and 8920b57cec5SDimitry Andric /// updates them for the output Chain/Flag. If the Flag pointer is nullptr, no 8930b57cec5SDimitry Andric /// flag is used. If V is not nullptr, then it is used in printing better 8940b57cec5SDimitry Andric /// diagnostic messages on error. 8950b57cec5SDimitry Andric void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, 8960b57cec5SDimitry Andric SDValue &Chain, SDValue *Flag, const Value *V = nullptr, 8970b57cec5SDimitry Andric ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const; 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric /// Add this value to the specified inlineasm node operand list. This adds the 9000b57cec5SDimitry Andric /// code marker, matching input operand index (if applicable), and includes 9010b57cec5SDimitry Andric /// the number of values added into it. 9020b57cec5SDimitry Andric void AddInlineAsmOperands(unsigned Code, bool HasMatching, 9030b57cec5SDimitry Andric unsigned MatchingIdx, const SDLoc &dl, 9040b57cec5SDimitry Andric SelectionDAG &DAG, std::vector<SDValue> &Ops) const; 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric /// Check if the total RegCount is greater than one. 9070b57cec5SDimitry Andric bool occupiesMultipleRegs() const { 9080b57cec5SDimitry Andric return std::accumulate(RegCount.begin(), RegCount.end(), 0) > 1; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric /// Return a list of registers and their sizes. 912e8d8bef9SDimitry Andric SmallVector<std::pair<unsigned, TypeSize>, 4> getRegsAndSizes() const; 9130b57cec5SDimitry Andric }; 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric } // end namespace llvm 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 918