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