| Michael Gottesman | 3923bec | 2013-08-12 21:02:02 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===// |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H |
| 15 | #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 16 | |
| Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 17 | #include "StatepointLowering.h" |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/APInt.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
| Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CallSite.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Statepoint.h" |
| Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
| Juergen Ributzka | fd4633e | 2014-10-16 21:26:35 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetLowering.h" |
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 28 | #include <utility> |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 29 | #include <vector> |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 30 | |
| 31 | namespace llvm { |
| 32 | |
| Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 33 | class AddrSpaceCastInst; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 34 | class AllocaInst; |
| 35 | class BasicBlock; |
| 36 | class BitCastInst; |
| 37 | class BranchInst; |
| 38 | class CallInst; |
| Devang Patel | b12ff59 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 39 | class DbgValueInst; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 40 | class ExtractElementInst; |
| 41 | class ExtractValueInst; |
| 42 | class FCmpInst; |
| 43 | class FPExtInst; |
| 44 | class FPToSIInst; |
| 45 | class FPToUIInst; |
| 46 | class FPTruncInst; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 47 | class Function; |
| Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 48 | class FunctionLoweringInfo; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 49 | class GetElementPtrInst; |
| 50 | class GCFunctionInfo; |
| 51 | class ICmpInst; |
| 52 | class IntToPtrInst; |
| Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 53 | class IndirectBrInst; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 54 | class InvokeInst; |
| 55 | class InsertElementInst; |
| 56 | class InsertValueInst; |
| 57 | class Instruction; |
| 58 | class LoadInst; |
| 59 | class MachineBasicBlock; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | class MachineInstr; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 61 | class MachineRegisterInfo; |
| Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 62 | class MDNode; |
| Patrik Hagglund | 1da3512 | 2014-03-12 08:00:24 +0000 | [diff] [blame] | 63 | class MVT; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 64 | class PHINode; |
| 65 | class PtrToIntInst; |
| 66 | class ReturnInst; |
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 67 | class SDDbgValue; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 68 | class SExtInst; |
| 69 | class SelectInst; |
| 70 | class ShuffleVectorInst; |
| 71 | class SIToFPInst; |
| 72 | class StoreInst; |
| 73 | class SwitchInst; |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 74 | class DataLayout; |
| Owen Anderson | bb15fec | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 75 | class TargetLibraryInfo; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 76 | class TargetLowering; |
| 77 | class TruncInst; |
| 78 | class UIToFPInst; |
| 79 | class UnreachableInst; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 80 | class VAArgInst; |
| 81 | class ZExtInst; |
| 82 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
| Dan Gohman | 1a6c47f | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 84 | /// SelectionDAGBuilder - This is the common target-independent lowering |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 85 | /// implementation that is parameterized by a TargetLowering object. |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 86 | /// |
| Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 87 | class SelectionDAGBuilder { |
| Andrew Trick | 175143b | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 88 | /// CurInst - The current instruction being visited |
| 89 | const Instruction *CurInst; |
| Dale Johannesen | db7c5f6 | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 90 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 91 | DenseMap<const Value*, SDValue> NodeMap; |
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 92 | |
| Devang Patel | b0c7639 | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 93 | /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used |
| 94 | /// to preserve debug information for incoming arguments. |
| 95 | DenseMap<const Value*, SDValue> UnusedArgNodeMap; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 96 | |
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 97 | /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap. |
| 98 | class DanglingDebugInfo { |
| Devang Patel | b12ff59 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 99 | const DbgValueInst* DI; |
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 100 | DebugLoc dl; |
| 101 | unsigned SDNodeOrder; |
| 102 | public: |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 103 | DanglingDebugInfo() : DI(nullptr), dl(DebugLoc()), SDNodeOrder(0) { } |
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 104 | DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) |
| 105 | : DI(di), dl(std::move(DL)), SDNodeOrder(SDNO) {} |
| Devang Patel | b12ff59 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 106 | const DbgValueInst* getDI() { return DI; } |
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 107 | DebugLoc getdl() { return dl; } |
| 108 | unsigned getSDNodeOrder() { return SDNodeOrder; } |
| 109 | }; |
| 110 | |
| 111 | /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not |
| 112 | /// yet seen the referent. We defer handling these until we do see it. |
| 113 | DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap; |
| 114 | |
| Chris Lattner | 1a32ede | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 115 | public: |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 116 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 117 | /// them up and then emit token factor nodes when possible. This allows us to |
| 118 | /// get simple disambiguation between loads without worrying about alias |
| 119 | /// analysis. |
| 120 | SmallVector<SDValue, 8> PendingLoads; |
| Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 121 | |
| 122 | /// State used while lowering a statepoint sequence (gc_statepoint, |
| 123 | /// gc_relocate, and gc_result). See StatepointLowering.hpp/cpp for details. |
| 124 | StatepointLoweringState StatepointLowering; |
| Chris Lattner | 1a32ede | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 125 | private: |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 126 | |
| 127 | /// PendingExports - CopyToReg nodes that copy values to virtual registers |
| 128 | /// for export to other blocks need to be emitted before any terminator |
| 129 | /// instruction, but they have no other ordering requirements. We bunch them |
| 130 | /// up and the emit a single tokenfactor for them just before terminator |
| 131 | /// instructions. |
| 132 | SmallVector<SDValue, 8> PendingExports; |
| 133 | |
| Bill Wendling | 022d18f | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 134 | /// SDNodeOrder - A unique monotonically increasing number used to order the |
| 135 | /// SDNodes we create. |
| 136 | unsigned SDNodeOrder; |
| 137 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 138 | enum CaseClusterKind { |
| 139 | /// A cluster of adjacent case labels with the same destination, or just one |
| 140 | /// case. |
| 141 | CC_Range, |
| 142 | /// A cluster of cases suitable for jump table lowering. |
| 143 | CC_JumpTable, |
| 144 | /// A cluster of cases suitable for bit test lowering. |
| 145 | CC_BitTests |
| 146 | }; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 147 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 148 | /// A cluster of case labels. |
| 149 | struct CaseCluster { |
| 150 | CaseClusterKind Kind; |
| 151 | const ConstantInt *Low, *High; |
| 152 | union { |
| 153 | MachineBasicBlock *MBB; |
| 154 | unsigned JTCasesIndex; |
| 155 | unsigned BTCasesIndex; |
| 156 | }; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 157 | BranchProbability Prob; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 158 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 159 | static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 160 | MachineBasicBlock *MBB, BranchProbability Prob) { |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 161 | CaseCluster C; |
| 162 | C.Kind = CC_Range; |
| 163 | C.Low = Low; |
| 164 | C.High = High; |
| 165 | C.MBB = MBB; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 166 | C.Prob = Prob; |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 167 | return C; |
| 168 | } |
| 169 | |
| 170 | static CaseCluster jumpTable(const ConstantInt *Low, |
| 171 | const ConstantInt *High, unsigned JTCasesIndex, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 172 | BranchProbability Prob) { |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 173 | CaseCluster C; |
| 174 | C.Kind = CC_JumpTable; |
| 175 | C.Low = Low; |
| 176 | C.High = High; |
| 177 | C.JTCasesIndex = JTCasesIndex; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 178 | C.Prob = Prob; |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 179 | return C; |
| 180 | } |
| 181 | |
| 182 | static CaseCluster bitTests(const ConstantInt *Low, const ConstantInt *High, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 183 | unsigned BTCasesIndex, BranchProbability Prob) { |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 184 | CaseCluster C; |
| 185 | C.Kind = CC_BitTests; |
| 186 | C.Low = Low; |
| 187 | C.High = High; |
| 188 | C.BTCasesIndex = BTCasesIndex; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 189 | C.Prob = Prob; |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 190 | return C; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | } |
| 192 | }; |
| 193 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 194 | typedef std::vector<CaseCluster> CaseClusterVector; |
| 195 | typedef CaseClusterVector::iterator CaseClusterIt; |
| 196 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 197 | struct CaseBits { |
| 198 | uint64_t Mask; |
| 199 | MachineBasicBlock* BB; |
| 200 | unsigned Bits; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 201 | BranchProbability ExtraProb; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 202 | |
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 203 | CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 204 | BranchProbability Prob): |
| 205 | Mask(mask), BB(bb), Bits(bits), ExtraProb(Prob) { } |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 206 | |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 207 | CaseBits() : Mask(0), BB(nullptr), Bits(0) {} |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 210 | typedef std::vector<CaseBits> CaseBitsVector; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 212 | /// Sort Clusters and merge adjacent cases. |
| 213 | void sortAndRangeify(CaseClusterVector &Clusters); |
| Anton Korobeynikov | 6f21913 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 214 | |
| Dan Gohman | 1a6c47f | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 215 | /// CaseBlock - This structure is used to communicate between |
| 216 | /// SelectionDAGBuilder and SDISel for the code generation of additional basic |
| 217 | /// blocks needed by multi-case switch statements. |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 218 | struct CaseBlock { |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 219 | CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 220 | const Value *cmpmiddle, MachineBasicBlock *truebb, |
| 221 | MachineBasicBlock *falsebb, MachineBasicBlock *me, |
| 222 | BranchProbability trueprob = BranchProbability::getUnknown(), |
| 223 | BranchProbability falseprob = BranchProbability::getUnknown()) |
| 224 | : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), |
| 225 | TrueBB(truebb), FalseBB(falsebb), ThisBB(me), TrueProb(trueprob), |
| 226 | FalseProb(falseprob) {} |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 227 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 228 | // CC - the condition code to use for the case block's setcc node |
| 229 | ISD::CondCode CC; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 230 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 231 | // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. |
| 232 | // Emit by default LHS op RHS. MHS is used for range comparisons: |
| 233 | // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 234 | const Value *CmpLHS, *CmpMHS, *CmpRHS; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 235 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 236 | // TrueBB/FalseBB - the block to branch to if the setcc is true/false. |
| 237 | MachineBasicBlock *TrueBB, *FalseBB; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 238 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 239 | // ThisBB - the block into which to emit the code for the setcc and branches |
| 240 | MachineBasicBlock *ThisBB; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 241 | |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 242 | // TrueProb/FalseProb - branch weights. |
| 243 | BranchProbability TrueProb, FalseProb; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 244 | }; |
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 245 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 246 | struct JumpTable { |
| 247 | JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, |
| 248 | MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} |
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 249 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 250 | /// Reg - the virtual register containing the index of the jump table entry |
| 251 | //. to jump to. |
| 252 | unsigned Reg; |
| 253 | /// JTI - the JumpTableIndex for this jump table in the function. |
| 254 | unsigned JTI; |
| 255 | /// MBB - the MBB into which to emit the code for the indirect jump. |
| 256 | MachineBasicBlock *MBB; |
| 257 | /// Default - the MBB of the default bb, which is a successor of the range |
| 258 | /// check MBB. This is when updating PHI nodes in successors. |
| 259 | MachineBasicBlock *Default; |
| 260 | }; |
| 261 | struct JumpTableHeader { |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 262 | JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H, |
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 263 | bool E = false) |
| 264 | : First(std::move(F)), Last(std::move(L)), SValue(SV), HeaderBB(H), |
| 265 | Emitted(E) {} |
| Anton Korobeynikov | 6f21913 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 266 | APInt First; |
| 267 | APInt Last; |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 268 | const Value *SValue; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 269 | MachineBasicBlock *HeaderBB; |
| 270 | bool Emitted; |
| 271 | }; |
| 272 | typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; |
| 273 | |
| 274 | struct BitTestCase { |
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 275 | BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 276 | BranchProbability Prob): |
| 277 | Mask(M), ThisBB(T), TargetBB(Tr), ExtraProb(Prob) { } |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 278 | uint64_t Mask; |
| Chris Lattner | 24576a5 | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 279 | MachineBasicBlock *ThisBB; |
| 280 | MachineBasicBlock *TargetBB; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 281 | BranchProbability ExtraProb; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | typedef SmallVector<BitTestCase, 3> BitTestInfo; |
| 285 | |
| 286 | struct BitTestBlock { |
| Cong Hou | 0312770 | 2015-08-26 23:15:32 +0000 | [diff] [blame] | 287 | BitTestBlock(APInt F, APInt R, const Value *SV, unsigned Rg, MVT RgVT, |
| 288 | bool E, bool CR, MachineBasicBlock *P, MachineBasicBlock *D, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 289 | BitTestInfo C, BranchProbability Pr) |
| Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 290 | : First(std::move(F)), Range(std::move(R)), SValue(SV), Reg(Rg), |
| 291 | RegVT(RgVT), Emitted(E), ContiguousRange(CR), Parent(P), Default(D), |
| 292 | Cases(std::move(C)), Prob(Pr) {} |
| Anton Korobeynikov | 6f21913 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 293 | APInt First; |
| 294 | APInt Range; |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 295 | const Value *SValue; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 296 | unsigned Reg; |
| Patrik Hagglund | 4e0f828 | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 297 | MVT RegVT; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 298 | bool Emitted; |
| Cong Hou | cd59591 | 2015-08-25 21:34:38 +0000 | [diff] [blame] | 299 | bool ContiguousRange; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 300 | MachineBasicBlock *Parent; |
| 301 | MachineBasicBlock *Default; |
| 302 | BitTestInfo Cases; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 303 | BranchProbability Prob; |
| 304 | BranchProbability DefaultProb; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| Jun Bum Lim | 919f9e8 | 2017-04-28 16:04:03 +0000 | [diff] [blame] | 307 | /// Return the range of value in [First..Last]. |
| 308 | uint64_t getJumpTableRange(const CaseClusterVector &Clusters, unsigned First, |
| 309 | unsigned Last) const; |
| 310 | |
| 311 | /// Return the number of cases in [First..Last]. |
| 312 | uint64_t getJumpTableNumCases(const SmallVectorImpl<unsigned> &TotalCases, |
| 313 | unsigned First, unsigned Last) const; |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 314 | |
| 315 | /// Build a jump table cluster from Clusters[First..Last]. Returns false if it |
| 316 | /// decides it's not a good idea. |
| Aditya Kumar | 356f79d | 2016-09-01 23:35:26 +0000 | [diff] [blame] | 317 | bool buildJumpTable(const CaseClusterVector &Clusters, unsigned First, |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 318 | unsigned Last, const SwitchInst *SI, |
| 319 | MachineBasicBlock *DefaultMBB, CaseCluster &JTCluster); |
| 320 | |
| 321 | /// Find clusters of cases suitable for jump table lowering. |
| 322 | void findJumpTables(CaseClusterVector &Clusters, const SwitchInst *SI, |
| 323 | MachineBasicBlock *DefaultMBB); |
| 324 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 325 | /// Build a bit test cluster from Clusters[First..Last]. Returns false if it |
| 326 | /// decides it's not a good idea. |
| 327 | bool buildBitTests(CaseClusterVector &Clusters, unsigned First, unsigned Last, |
| 328 | const SwitchInst *SI, CaseCluster &BTCluster); |
| 329 | |
| 330 | /// Find clusters of cases suitable for bit test lowering. |
| 331 | void findBitTestClusters(CaseClusterVector &Clusters, const SwitchInst *SI); |
| 332 | |
| 333 | struct SwitchWorkListItem { |
| 334 | MachineBasicBlock *MBB; |
| 335 | CaseClusterIt FirstCluster; |
| 336 | CaseClusterIt LastCluster; |
| 337 | const ConstantInt *GE; |
| 338 | const ConstantInt *LT; |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 339 | BranchProbability DefaultProb; |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 340 | }; |
| 341 | typedef SmallVector<SwitchWorkListItem, 4> SwitchWorkList; |
| 342 | |
| Hans Wennborg | 6ed81cb | 2015-06-20 17:14:07 +0000 | [diff] [blame] | 343 | /// Determine the rank by weight of CC in [First,Last]. If CC has more weight |
| 344 | /// than each cluster in the range, its rank is 0. |
| 345 | static unsigned caseClusterRank(const CaseCluster &CC, CaseClusterIt First, |
| 346 | CaseClusterIt Last); |
| 347 | |
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 348 | /// Emit comparison and split W into two subtrees. |
| 349 | void splitWorkItem(SwitchWorkList &WorkList, const SwitchWorkListItem &W, |
| 350 | Value *Cond, MachineBasicBlock *SwitchMBB); |
| 351 | |
| 352 | /// Lower W. |
| 353 | void lowerWorkItem(SwitchWorkListItem W, Value *Cond, |
| 354 | MachineBasicBlock *SwitchMBB, |
| 355 | MachineBasicBlock *DefaultMBB); |
| 356 | |
| 357 | |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 358 | /// A class which encapsulates all of the information needed to generate a |
| 359 | /// stack protector check and signals to isel via its state being initialized |
| 360 | /// that a stack protector needs to be generated. |
| 361 | /// |
| 362 | /// *NOTE* The following is a high level documentation of SelectionDAG Stack |
| 363 | /// Protector Generation. The reason that it is placed here is for a lack of |
| 364 | /// other good places to stick it. |
| 365 | /// |
| 366 | /// High Level Overview of SelectionDAG Stack Protector Generation: |
| 367 | /// |
| 368 | /// Previously, generation of stack protectors was done exclusively in the |
| 369 | /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated |
| 370 | /// splitting basic blocks at the IR level to create the success/failure basic |
| 371 | /// blocks in the tail of the basic block in question. As a result of this, |
| 372 | /// calls that would have qualified for the sibling call optimization were no |
| 373 | /// longer eligible for optimization since said calls were no longer right in |
| 374 | /// the "tail position" (i.e. the immediate predecessor of a ReturnInst |
| 375 | /// instruction). |
| 376 | /// |
| 377 | /// Then it was noticed that since the sibling call optimization causes the |
| 378 | /// callee to reuse the caller's stack, if we could delay the generation of |
| 379 | /// the stack protector check until later in CodeGen after the sibling call |
| 380 | /// decision was made, we get both the tail call optimization and the stack |
| 381 | /// protector check! |
| 382 | /// |
| 383 | /// A few goals in solving this problem were: |
| 384 | /// |
| 385 | /// 1. Preserve the architecture independence of stack protector generation. |
| 386 | /// |
| 387 | /// 2. Preserve the normal IR level stack protector check for platforms like |
| Alp Toker | cf21875 | 2014-06-30 18:57:16 +0000 | [diff] [blame] | 388 | /// OpenBSD for which we support platform-specific stack protector |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 389 | /// generation. |
| 390 | /// |
| 391 | /// The main problem that guided the present solution is that one can not |
| 392 | /// solve this problem in an architecture independent manner at the IR level |
| 393 | /// only. This is because: |
| 394 | /// |
| 395 | /// 1. The decision on whether or not to perform a sibling call on certain |
| 396 | /// platforms (for instance i386) requires lower level information |
| 397 | /// related to available registers that can not be known at the IR level. |
| 398 | /// |
| 399 | /// 2. Even if the previous point were not true, the decision on whether to |
| 400 | /// perform a tail call is done in LowerCallTo in SelectionDAG which |
| 401 | /// occurs after the Stack Protector Pass. As a result, one would need to |
| 402 | /// put the relevant callinst into the stack protector check success |
| 403 | /// basic block (where the return inst is placed) and then move it back |
| 404 | /// later at SelectionDAG/MI time before the stack protector check if the |
| 405 | /// tail call optimization failed. The MI level option was nixed |
| Alp Toker | cf21875 | 2014-06-30 18:57:16 +0000 | [diff] [blame] | 406 | /// immediately since it would require platform-specific pattern |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 407 | /// matching. The SelectionDAG level option was nixed because |
| 408 | /// SelectionDAG only processes one IR level basic block at a time |
| 409 | /// implying one could not create a DAG Combine to move the callinst. |
| 410 | /// |
| 411 | /// To get around this problem a few things were realized: |
| 412 | /// |
| 413 | /// 1. While one can not handle multiple IR level basic blocks at the |
| 414 | /// SelectionDAG Level, one can generate multiple machine basic blocks |
| 415 | /// for one IR level basic block. This is how we handle bit tests and |
| 416 | /// switches. |
| 417 | /// |
| 418 | /// 2. At the MI level, tail calls are represented via a special return |
| 419 | /// MIInst called "tcreturn". Thus if we know the basic block in which we |
| 420 | /// wish to insert the stack protector check, we get the correct behavior |
| 421 | /// by always inserting the stack protector check right before the return |
| 422 | /// statement. This is a "magical transformation" since no matter where |
| 423 | /// the stack protector check intrinsic is, we always insert the stack |
| 424 | /// protector check code at the end of the BB. |
| 425 | /// |
| 426 | /// Given the aforementioned constraints, the following solution was devised: |
| 427 | /// |
| 428 | /// 1. On platforms that do not support SelectionDAG stack protector check |
| 429 | /// generation, allow for the normal IR level stack protector check |
| 430 | /// generation to continue. |
| 431 | /// |
| 432 | /// 2. On platforms that do support SelectionDAG stack protector check |
| 433 | /// generation: |
| 434 | /// |
| 435 | /// a. Use the IR level stack protector pass to decide if a stack |
| 436 | /// protector is required/which BB we insert the stack protector check |
| 437 | /// in by reusing the logic already therein. If we wish to generate a |
| 438 | /// stack protector check in a basic block, we place a special IR |
| 439 | /// intrinsic called llvm.stackprotectorcheck right before the BB's |
| 440 | /// returninst or if there is a callinst that could potentially be |
| 441 | /// sibling call optimized, before the call inst. |
| 442 | /// |
| 443 | /// b. Then when a BB with said intrinsic is processed, we codegen the BB |
| 444 | /// normally via SelectBasicBlock. In said process, when we visit the |
| 445 | /// stack protector check, we do not actually emit anything into the |
| 446 | /// BB. Instead, we just initialize the stack protector descriptor |
| 447 | /// class (which involves stashing information/creating the success |
| 448 | /// mbbb and the failure mbb if we have not created one for this |
| 449 | /// function yet) and export the guard variable that we are going to |
| 450 | /// compare. |
| 451 | /// |
| 452 | /// c. After we finish selecting the basic block, in FinishBasicBlock if |
| 453 | /// the StackProtectorDescriptor attached to the SelectionDAGBuilder is |
| Etienne Bergeron | 22bfa83 | 2016-06-07 20:15:35 +0000 | [diff] [blame] | 454 | /// initialized, we produce the validation code with one of these |
| 455 | /// techniques: |
| 456 | /// 1) with a call to a guard check function |
| 457 | /// 2) with inlined instrumentation |
| 458 | /// |
| 459 | /// 1) We insert a call to the check function before the terminator. |
| 460 | /// |
| 461 | /// 2) We first find a splice point in the parent basic block |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 462 | /// before the terminator and then splice the terminator of said basic |
| 463 | /// block into the success basic block. Then we code-gen a new tail for |
| 464 | /// the parent basic block consisting of the two loads, the comparison, |
| 465 | /// and finally two branches to the success/failure basic blocks. We |
| 466 | /// conclude by code-gening the failure basic block if we have not |
| 467 | /// code-gened it already (all stack protector checks we generate in |
| 468 | /// the same function, use the same failure basic block). |
| 469 | class StackProtectorDescriptor { |
| 470 | public: |
| Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 471 | StackProtectorDescriptor() |
| Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame] | 472 | : ParentMBB(nullptr), SuccessMBB(nullptr), FailureMBB(nullptr) {} |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 473 | |
| 474 | /// Returns true if all fields of the stack protector descriptor are |
| 475 | /// initialized implying that we should/are ready to emit a stack protector. |
| 476 | bool shouldEmitStackProtector() const { |
| Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 477 | return ParentMBB && SuccessMBB && FailureMBB; |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| Etienne Bergeron | 22bfa83 | 2016-06-07 20:15:35 +0000 | [diff] [blame] | 480 | bool shouldEmitFunctionBasedCheckStackProtector() const { |
| 481 | return ParentMBB && !SuccessMBB && !FailureMBB; |
| 482 | } |
| 483 | |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 484 | /// Initialize the stack protector descriptor structure for a new basic |
| 485 | /// block. |
| Etienne Bergeron | 22bfa83 | 2016-06-07 20:15:35 +0000 | [diff] [blame] | 486 | void initialize(const BasicBlock *BB, MachineBasicBlock *MBB, |
| 487 | bool FunctionBasedInstrumentation) { |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 488 | // Make sure we are not initialized yet. |
| 489 | assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " |
| 490 | "already initialized!"); |
| 491 | ParentMBB = MBB; |
| Etienne Bergeron | 22bfa83 | 2016-06-07 20:15:35 +0000 | [diff] [blame] | 492 | if (!FunctionBasedInstrumentation) { |
| 493 | SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true); |
| 494 | FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); |
| 495 | } |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /// Reset state that changes when we handle different basic blocks. |
| 499 | /// |
| 500 | /// This currently includes: |
| 501 | /// |
| 502 | /// 1. The specific basic block we are generating a |
| 503 | /// stack protector for (ParentMBB). |
| 504 | /// |
| 505 | /// 2. The successor machine basic block that will contain the tail of |
| 506 | /// parent mbb after we create the stack protector check (SuccessMBB). This |
| 507 | /// BB is visited only on stack protector check success. |
| 508 | void resetPerBBState() { |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 509 | ParentMBB = nullptr; |
| 510 | SuccessMBB = nullptr; |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /// Reset state that only changes when we switch functions. |
| 514 | /// |
| 515 | /// This currently includes: |
| 516 | /// |
| 517 | /// 1. FailureMBB since we reuse the failure code path for all stack |
| 518 | /// protector checks created in an individual function. |
| 519 | /// |
| 520 | /// 2.The guard variable since the guard variable we are checking against is |
| 521 | /// always the same. |
| 522 | void resetPerFunctionState() { |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 523 | FailureMBB = nullptr; |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | MachineBasicBlock *getParentMBB() { return ParentMBB; } |
| 527 | MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } |
| 528 | MachineBasicBlock *getFailureMBB() { return FailureMBB; } |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 529 | |
| 530 | private: |
| 531 | /// The basic block for which we are generating the stack protector. |
| 532 | /// |
| 533 | /// As a result of stack protector generation, we will splice the |
| 534 | /// terminators of this basic block into the successor mbb SuccessMBB and |
| 535 | /// replace it with a compare/branch to the successor mbbs |
| 536 | /// SuccessMBB/FailureMBB depending on whether or not the stack protector |
| 537 | /// was violated. |
| 538 | MachineBasicBlock *ParentMBB; |
| 539 | |
| 540 | /// A basic block visited on stack protector check success that contains the |
| 541 | /// terminators of ParentMBB. |
| 542 | MachineBasicBlock *SuccessMBB; |
| 543 | |
| 544 | /// This basic block visited on stack protector check failure that will |
| 545 | /// contain a call to __stack_chk_fail(). |
| 546 | MachineBasicBlock *FailureMBB; |
| 547 | |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 548 | /// Add a successor machine basic block to ParentMBB. If the successor mbb |
| 549 | /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic |
| Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 550 | /// block will be created. Assign a large weight if IsLikely is true. |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 551 | MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, |
| 552 | MachineBasicBlock *ParentMBB, |
| Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 553 | bool IsLikely, |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 554 | MachineBasicBlock *SuccMBB = nullptr); |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 555 | }; |
| 556 | |
| Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 557 | private: |
| Dan Gohman | c334960 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 558 | const TargetMachine &TM; |
| Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 559 | public: |
| Nico Rieck | b5262d6 | 2014-01-12 14:09:17 +0000 | [diff] [blame] | 560 | /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling |
| 561 | /// nodes without a corresponding SDNode. |
| 562 | static const unsigned LowestSDNodeOrder = 1; |
| 563 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 564 | SelectionDAG &DAG; |
| Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 565 | const DataLayout *DL; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 566 | AliasAnalysis *AA; |
| Owen Anderson | bb15fec | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 567 | const TargetLibraryInfo *LibInfo; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 568 | |
| 569 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 570 | /// SwitchInst code generation information. |
| 571 | std::vector<CaseBlock> SwitchCases; |
| 572 | /// JTCases - Vector of JumpTable structures used to communicate |
| 573 | /// SwitchInst code generation information. |
| 574 | std::vector<JumpTableBlock> JTCases; |
| 575 | /// BitTestCases - Vector of BitTestBlock structures used to communicate |
| 576 | /// SwitchInst code generation information. |
| 577 | std::vector<BitTestBlock> BitTestCases; |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 578 | /// A StackProtectorDescriptor structure used to communicate stack protector |
| 579 | /// information in between SelectBasicBlock and FinishBasicBlock. |
| 580 | StackProtectorDescriptor SPDescriptor; |
| Evan Cheng | 270d0f9 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 581 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 582 | // Emit PHI-node-operand constants only once even if used by multiple |
| 583 | // PHI nodes. |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 584 | DenseMap<const Constant *, unsigned> ConstantsOut; |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 585 | |
| 586 | /// FuncInfo - Information about the function as a whole. |
| 587 | /// |
| 588 | FunctionLoweringInfo &FuncInfo; |
| Bill Wendling | 19e0a5b | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 589 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 590 | /// GFI - Garbage collection metadata for the function. |
| 591 | GCFunctionInfo *GFI; |
| 592 | |
| Bill Wendling | 267f323 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 593 | /// LPadToCallSiteMap - Map a landing pad to the call site indexes. |
| 594 | DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap; |
| Bill Wendling | 3d11aa7 | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 595 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 596 | /// HasTailCall - This is set to true if a call in the current |
| 597 | /// block has been translated as a tail call. In this case, |
| 598 | /// no subsequent DAG nodes should be created. |
| 599 | /// |
| 600 | bool HasTailCall; |
| 601 | |
| Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 602 | LLVMContext *Context; |
| 603 | |
| Dan Gohman | c334960 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 604 | SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, |
| Dan Gohman | 1a6c47f | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 605 | CodeGenOpt::Level ol) |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 606 | : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), |
| Ahmed Bougacha | 604526f | 2017-05-10 00:39:30 +0000 | [diff] [blame] | 607 | DAG(dag), DL(nullptr), AA(nullptr), FuncInfo(funcinfo), |
| Richard Smith | 3fb2047 | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 608 | HasTailCall(false) { |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| Ahmed Bougacha | 604526f | 2017-05-10 00:39:30 +0000 | [diff] [blame] | 611 | void init(GCFunctionInfo *gfi, AliasAnalysis *AA, |
| Owen Anderson | bb15fec | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 612 | const TargetLibraryInfo *li); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 613 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 614 | /// Clear out the current SelectionDAG and the associated state and prepare |
| 615 | /// this SelectionDAGBuilder object to be used for a new block. This doesn't |
| 616 | /// clear out information about additional blocks that are needed to complete |
| 617 | /// switch lowering or PHI node updating; that information is cleared out as |
| 618 | /// it is consumed. |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 619 | void clear(); |
| 620 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 621 | /// Clear the dangling debug information map. This function is separated from |
| 622 | /// the clear so that debug information that is dangling in a basic block can |
| 623 | /// be properly resolved in a different basic block. This allows the |
| 624 | /// SelectionDAG to resolve dangling debug information attached to PHI nodes. |
| Devang Patel | 79928838 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 625 | void clearDanglingDebugInfo(); |
| 626 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 627 | /// Return the current virtual root of the Selection DAG, flushing any |
| 628 | /// PendingLoad items. This must be done before emitting a store or any other |
| 629 | /// node that may need to be ordered after any prior load instructions. |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 630 | SDValue getRoot(); |
| 631 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 632 | /// Similar to getRoot, but instead of flushing all the PendingLoad items, |
| 633 | /// flush all the PendingExports items. It is necessary to do this before |
| 634 | /// emitting a terminator instruction. |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 635 | SDValue getControlRoot(); |
| 636 | |
| Andrew Trick | 175143b | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 637 | SDLoc getCurSDLoc() const { |
| Andrew Trick | 175143b | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 638 | return SDLoc(CurInst, SDNodeOrder); |
| 639 | } |
| 640 | |
| 641 | DebugLoc getCurDebugLoc() const { |
| 642 | return CurInst ? CurInst->getDebugLoc() : DebugLoc(); |
| 643 | } |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 644 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 645 | void CopyValueToVirtualRegister(const Value *V, unsigned Reg); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 646 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 647 | void visit(const Instruction &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 648 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 649 | void visit(unsigned Opcode, const User &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 650 | |
| Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 651 | /// getCopyFromRegs - If there was virtual register allocated for the value V |
| 652 | /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise. |
| 653 | SDValue getCopyFromRegs(const Value *V, Type *Ty); |
| 654 | |
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 655 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 656 | // generate the debug data structures now that we've seen its definition. |
| 657 | void resolveDanglingDebugInfo(const Value *V, SDValue Val); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 658 | SDValue getValue(const Value *V); |
| Elena Demikhovsky | 584ce37 | 2015-04-28 07:57:37 +0000 | [diff] [blame] | 659 | bool findValue(const Value *V) const; |
| 660 | |
| Dan Gohman | d432223 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 661 | SDValue getNonRegisterValue(const Value *V); |
| 662 | SDValue getValueImpl(const Value *V); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 663 | |
| 664 | void setValue(const Value *V, SDValue NewN) { |
| 665 | SDValue &N = NodeMap[V]; |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 666 | assert(!N.getNode() && "Already set a value for this node!"); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 667 | N = NewN; |
| 668 | } |
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 669 | |
| Devang Patel | b0c7639 | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 670 | void setUnusedArgValue(const Value *V, SDValue NewN) { |
| 671 | SDValue &N = UnusedArgNodeMap[V]; |
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 672 | assert(!N.getNode() && "Already set a value for this node!"); |
| Devang Patel | b0c7639 | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 673 | N = NewN; |
| 674 | } |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 675 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 676 | void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 677 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
| Pete Cooper | 6923461 | 2015-07-15 01:31:26 +0000 | [diff] [blame] | 678 | MachineBasicBlock *SwitchBB, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 679 | Instruction::BinaryOps Opc, BranchProbability TW, |
| Geoff Berry | 92a286a | 2017-01-24 16:36:07 +0000 | [diff] [blame] | 680 | BranchProbability FW, bool InvertCond); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 681 | void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, |
| Dan Gohman | d01ddb5 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 682 | MachineBasicBlock *FBB, |
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 683 | MachineBasicBlock *CurBB, |
| Manman Ren | 4ece745 | 2014-01-31 00:42:44 +0000 | [diff] [blame] | 684 | MachineBasicBlock *SwitchBB, |
| Geoff Berry | 92a286a | 2017-01-24 16:36:07 +0000 | [diff] [blame] | 685 | BranchProbability TW, BranchProbability FW, |
| 686 | bool InvertCond); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 687 | bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 688 | bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); |
| 689 | void CopyToExportRegsIfNeeded(const Value *V); |
| 690 | void ExportFromCurrentBlock(const Value *V); |
| 691 | void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, |
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 692 | const BasicBlock *EHPadBB = nullptr); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 693 | |
| Matt Arsenault | 2bba779 | 2016-02-08 16:28:19 +0000 | [diff] [blame] | 694 | // Lower range metadata from 0 to N to assert zext to an integer of nearest |
| 695 | // floor power of two. |
| 696 | SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, |
| 697 | SDValue Op); |
| 698 | |
| Sanjoy Das | 19c6159 | 2016-03-16 20:49:31 +0000 | [diff] [blame] | 699 | void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, |
| 700 | ImmutableCallSite CS, unsigned ArgIdx, |
| 701 | unsigned NumArgs, SDValue Callee, |
| 702 | Type *ReturnTy, bool IsPatchPoint); |
| 703 | |
| 704 | std::pair<SDValue, SDValue> |
| 705 | lowerInvokable(TargetLowering::CallLoweringInfo &CLI, |
| 706 | const BasicBlock *EHPadBB = nullptr); |
| Andrew Trick | 74f4c74 | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 707 | |
| Jakob Stoklund Olesen | 665aa6e | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 708 | /// UpdateSplitBlock - When an MBB was split during scheduling, update the |
| Alp Toker | 798060e | 2014-01-11 14:01:43 +0000 | [diff] [blame] | 709 | /// references that need to refer to the last resulting block. |
| Jakob Stoklund Olesen | 665aa6e | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 710 | void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); |
| 711 | |
| Sanjoy Das | 70697ff | 2016-03-16 23:08:00 +0000 | [diff] [blame] | 712 | /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes |
| Sanjoy Das | a5b2972 | 2016-03-23 02:28:35 +0000 | [diff] [blame] | 713 | /// of lowering into a STATEPOINT node. |
| Sanjoy Das | 70697ff | 2016-03-16 23:08:00 +0000 | [diff] [blame] | 714 | struct StatepointLoweringInfo { |
| 715 | /// Bases[i] is the base pointer for Ptrs[i]. Together they denote the set |
| 716 | /// of gc pointers this STATEPOINT has to relocate. |
| Sanjoy Das | e58ca59 | 2016-03-23 02:24:07 +0000 | [diff] [blame] | 717 | SmallVector<const Value *, 16> Bases; |
| 718 | SmallVector<const Value *, 16> Ptrs; |
| Sanjoy Das | 70697ff | 2016-03-16 23:08:00 +0000 | [diff] [blame] | 719 | |
| 720 | /// The set of gc.relocate calls associated with this gc.statepoint. |
| Sanjoy Das | e58ca59 | 2016-03-23 02:24:07 +0000 | [diff] [blame] | 721 | SmallVector<const GCRelocateInst *, 16> GCRelocates; |
| Sanjoy Das | 70697ff | 2016-03-16 23:08:00 +0000 | [diff] [blame] | 722 | |
| 723 | /// The full list of gc arguments to the gc.statepoint being lowered. |
| 724 | ArrayRef<const Use> GCArgs; |
| 725 | |
| 726 | /// The gc.statepoint instruction. |
| 727 | const Instruction *StatepointInstr = nullptr; |
| 728 | |
| 729 | /// The list of gc transition arguments present in the gc.statepoint being |
| 730 | /// lowered. |
| 731 | ArrayRef<const Use> GCTransitionArgs; |
| 732 | |
| 733 | /// The ID that the resulting STATEPOINT instruction has to report. |
| 734 | unsigned ID = -1; |
| 735 | |
| 736 | /// Information regarding the underlying call instruction. |
| 737 | TargetLowering::CallLoweringInfo CLI; |
| 738 | |
| 739 | /// The deoptimization state associated with this gc.statepoint call, if |
| 740 | /// any. |
| 741 | ArrayRef<const Use> DeoptState; |
| 742 | |
| 743 | /// Flags associated with the meta arguments being lowered. |
| 744 | uint64_t StatepointFlags = -1; |
| 745 | |
| 746 | /// The number of patchable bytes the call needs to get lowered into. |
| 747 | unsigned NumPatchBytes = -1; |
| 748 | |
| 749 | /// The exception handling unwind destination, in case this represents an |
| 750 | /// invoke of gc.statepoint. |
| 751 | const BasicBlock *EHPadBB = nullptr; |
| 752 | |
| 753 | explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {} |
| 754 | }; |
| 755 | |
| 756 | /// Lower \p SLI into a STATEPOINT instruction. |
| Sanjoy Das | 38bfc22 | 2016-03-22 00:59:13 +0000 | [diff] [blame] | 757 | SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SLI); |
| Sanjoy Das | 70697ff | 2016-03-16 23:08:00 +0000 | [diff] [blame] | 758 | |
| Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 759 | // This function is responsible for the whole statepoint lowering process. |
| Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 760 | // It uniformly handles invoke and call statepoints. |
| 761 | void LowerStatepoint(ImmutableStatepoint Statepoint, |
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 762 | const BasicBlock *EHPadBB = nullptr); |
| Sanjoy Das | 38bfc22 | 2016-03-22 00:59:13 +0000 | [diff] [blame] | 763 | |
| 764 | void LowerCallSiteWithDeoptBundle(ImmutableCallSite CS, SDValue Callee, |
| 765 | const BasicBlock *EHPadBB); |
| 766 | |
| Sanjoy Das | df9ae70 | 2016-03-24 20:23:29 +0000 | [diff] [blame] | 767 | void LowerDeoptimizeCall(const CallInst *CI); |
| Sanjoy Das | 65a6067 | 2016-04-06 01:33:49 +0000 | [diff] [blame] | 768 | void LowerDeoptimizingReturn(); |
| Sanjoy Das | df9ae70 | 2016-03-24 20:23:29 +0000 | [diff] [blame] | 769 | |
| Sanjoy Das | fd3eaa8 | 2016-03-24 22:51:49 +0000 | [diff] [blame] | 770 | void LowerCallSiteWithDeoptBundleImpl(ImmutableCallSite CS, SDValue Callee, |
| 771 | const BasicBlock *EHPadBB, |
| Sanjoy Das | 65a6067 | 2016-04-06 01:33:49 +0000 | [diff] [blame] | 772 | bool VarArgDisallowed, |
| 773 | bool ForceVoidReturnTy); |
| Sanjoy Das | fd3eaa8 | 2016-03-24 22:51:49 +0000 | [diff] [blame] | 774 | |
| Sanjoy Das | 40c32dd | 2017-04-27 17:17:16 +0000 | [diff] [blame] | 775 | /// Returns the type of FrameIndex and TargetFrameIndex nodes. |
| 776 | MVT getFrameIndexTy() { |
| 777 | return DAG.getTargetLoweringInfo().getFrameIndexTy(DAG.getDataLayout()); |
| 778 | } |
| 779 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 780 | private: |
| 781 | // Terminator instructions. |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 782 | void visitRet(const ReturnInst &I); |
| 783 | void visitBr(const BranchInst &I); |
| 784 | void visitSwitch(const SwitchInst &I); |
| 785 | void visitIndirectBr(const IndirectBrInst &I); |
| Yaron Keren | d7ba46b | 2014-04-19 13:47:43 +0000 | [diff] [blame] | 786 | void visitUnreachable(const UnreachableInst &I); |
| David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 787 | void visitCleanupRet(const CleanupReturnInst &I); |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 788 | void visitCatchSwitch(const CatchSwitchInst &I); |
| David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 789 | void visitCatchRet(const CatchReturnInst &I); |
| 790 | void visitCatchPad(const CatchPadInst &I); |
| David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 791 | void visitCleanupPad(const CleanupPadInst &CPI); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 792 | |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 793 | BranchProbability getEdgeProbability(const MachineBasicBlock *Src, |
| 794 | const MachineBasicBlock *Dst) const; |
| 795 | void addSuccessorWithProb( |
| 796 | MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 797 | BranchProbability Prob = BranchProbability::getUnknown()); |
| 798 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 799 | public: |
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 800 | void visitSwitchCase(CaseBlock &CB, |
| 801 | MachineBasicBlock *SwitchBB); |
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 802 | void visitSPDescriptorParent(StackProtectorDescriptor &SPD, |
| 803 | MachineBasicBlock *ParentBB); |
| 804 | void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); |
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 805 | void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); |
| Evan Cheng | ac730dd | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 806 | void visitBitTestCase(BitTestBlock &BB, |
| 807 | MachineBasicBlock* NextMBB, |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 808 | BranchProbability BranchProbToNext, |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 809 | unsigned Reg, |
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 810 | BitTestCase &B, |
| 811 | MachineBasicBlock *SwitchBB); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 812 | void visitJumpTable(JumpTable &JT); |
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 813 | void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, |
| 814 | MachineBasicBlock *SwitchBB); |
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 815 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 816 | private: |
| 817 | // These all get lowered before this pass. |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 818 | void visitInvoke(const InvokeInst &I); |
| Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 819 | void visitResume(const ResumeInst &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 820 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 821 | void visitBinary(const User &I, unsigned OpCode); |
| 822 | void visitShift(const User &I, unsigned Opcode); |
| 823 | void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } |
| 824 | void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } |
| 825 | void visitSub(const User &I) { visitBinary(I, ISD::SUB); } |
| 826 | void visitFSub(const User &I); |
| 827 | void visitMul(const User &I) { visitBinary(I, ISD::MUL); } |
| 828 | void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } |
| 829 | void visitURem(const User &I) { visitBinary(I, ISD::UREM); } |
| 830 | void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } |
| 831 | void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } |
| 832 | void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } |
| Benjamin Kramer | 9960a25 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 833 | void visitSDiv(const User &I); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 834 | void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } |
| 835 | void visitAnd (const User &I) { visitBinary(I, ISD::AND); } |
| 836 | void visitOr (const User &I) { visitBinary(I, ISD::OR); } |
| 837 | void visitXor (const User &I) { visitBinary(I, ISD::XOR); } |
| 838 | void visitShl (const User &I) { visitShift(I, ISD::SHL); } |
| 839 | void visitLShr(const User &I) { visitShift(I, ISD::SRL); } |
| 840 | void visitAShr(const User &I) { visitShift(I, ISD::SRA); } |
| 841 | void visitICmp(const User &I); |
| 842 | void visitFCmp(const User &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 843 | // Visit the conversion instructions |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 844 | void visitTrunc(const User &I); |
| 845 | void visitZExt(const User &I); |
| 846 | void visitSExt(const User &I); |
| 847 | void visitFPTrunc(const User &I); |
| 848 | void visitFPExt(const User &I); |
| 849 | void visitFPToUI(const User &I); |
| 850 | void visitFPToSI(const User &I); |
| 851 | void visitUIToFP(const User &I); |
| 852 | void visitSIToFP(const User &I); |
| 853 | void visitPtrToInt(const User &I); |
| 854 | void visitIntToPtr(const User &I); |
| 855 | void visitBitCast(const User &I); |
| Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 856 | void visitAddrSpaceCast(const User &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 857 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 858 | void visitExtractElement(const User &I); |
| 859 | void visitInsertElement(const User &I); |
| 860 | void visitShuffleVector(const User &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 861 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 862 | void visitExtractValue(const ExtractValueInst &I); |
| 863 | void visitInsertValue(const InsertValueInst &I); |
| Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 864 | void visitLandingPad(const LandingPadInst &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 865 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 866 | void visitGetElementPtr(const User &I); |
| 867 | void visitSelect(const User &I); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 868 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 869 | void visitAlloca(const AllocaInst &I); |
| 870 | void visitLoad(const LoadInst &I); |
| 871 | void visitStore(const StoreInst &I); |
| Elena Demikhovsky | caaceef | 2016-11-03 03:23:55 +0000 | [diff] [blame] | 872 | void visitMaskedLoad(const CallInst &I, bool IsExpanding = false); |
| 873 | void visitMaskedStore(const CallInst &I, bool IsCompressing = false); |
| Elena Demikhovsky | 584ce37 | 2015-04-28 07:57:37 +0000 | [diff] [blame] | 874 | void visitMaskedGather(const CallInst &I); |
| 875 | void visitMaskedScatter(const CallInst &I); |
| Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 876 | void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); |
| 877 | void visitAtomicRMW(const AtomicRMWInst &I); |
| Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 878 | void visitFence(const FenceInst &I); |
| Dan Gohman | f41ad47 | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 879 | void visitPHI(const PHINode &I); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 880 | void visitCall(const CallInst &I); |
| 881 | bool visitMemCmpCall(const CallInst &I); |
| Andrew Kaylor | b99d1cc | 2016-07-29 18:23:18 +0000 | [diff] [blame] | 882 | bool visitMemPCpyCall(const CallInst &I); |
| Richard Sandiford | 6f6d551 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 883 | bool visitMemChrCall(const CallInst &I); |
| Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 884 | bool visitStrCpyCall(const CallInst &I, bool isStpcpy); |
| Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 885 | bool visitStrCmpCall(const CallInst &I); |
| Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 886 | bool visitStrLenCall(const CallInst &I); |
| 887 | bool visitStrNLenCall(const CallInst &I); |
| Bob Wilson | 874886c | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 888 | bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); |
| Matt Arsenault | 7c93690 | 2014-10-21 23:01:01 +0000 | [diff] [blame] | 889 | bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode); |
| Eli Friedman | 342e8df | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 890 | void visitAtomicLoad(const LoadInst &I); |
| 891 | void visitAtomicStore(const StoreInst &I); |
| Manman Ren | e221a87 | 2016-04-05 18:13:16 +0000 | [diff] [blame] | 892 | void visitLoadFromSwiftError(const LoadInst &I); |
| 893 | void visitStoreToSwiftError(const StoreInst &I); |
| Eli Friedman | 342e8df | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 894 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 895 | void visitInlineAsm(ImmutableCallSite CS); |
| 896 | const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); |
| 897 | void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); |
| Andrew Kaylor | f466001 | 2017-05-25 21:31:00 +0000 | [diff] [blame] | 898 | void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 899 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 900 | void visitVAStart(const CallInst &I); |
| 901 | void visitVAArg(const VAArgInst &I); |
| 902 | void visitVAEnd(const CallInst &I); |
| 903 | void visitVACopy(const CallInst &I); |
| Andrew Trick | 74f4c74 | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 904 | void visitStackmap(const CallInst &I); |
| Juergen Ributzka | ad2363f | 2014-10-17 17:39:00 +0000 | [diff] [blame] | 905 | void visitPatchpoint(ImmutableCallSite CS, |
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 906 | const BasicBlock *EHPadBB = nullptr); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 907 | |
| Sanjoy Das | 3a02019 | 2016-03-17 00:47:14 +0000 | [diff] [blame] | 908 | // These two are implemented in StatepointLowering.cpp |
| Manuel Jacob | 83eefa6 | 2016-01-05 04:03:00 +0000 | [diff] [blame] | 909 | void visitGCRelocate(const GCRelocateInst &I); |
| Philip Reames | 92d1f0c | 2016-04-12 18:05:10 +0000 | [diff] [blame] | 910 | void visitGCResult(const GCResultInst &I); |
| Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 911 | |
| Amara Emerson | cf9daa3 | 2017-05-09 10:43:25 +0000 | [diff] [blame] | 912 | void visitVectorReduce(const CallInst &I, unsigned Intrinsic); |
| 913 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 914 | void visitUserOp1(const Instruction &I) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 915 | llvm_unreachable("UserOp1 should not exist at instruction selection time!"); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 916 | } |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 917 | void visitUserOp2(const Instruction &I) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 918 | llvm_unreachable("UserOp2 should not exist at instruction selection time!"); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 919 | } |
| Dan Gohman | 5b43aa0 | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 920 | |
| Richard Sandiford | e382775 | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 921 | void processIntegerCallValue(const Instruction &I, |
| 922 | SDValue Value, bool IsSigned); |
| 923 | |
| Dan Gohman | 5b43aa0 | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 924 | void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); |
| Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 925 | |
| Renato Golin | 38ed802 | 2016-05-17 19:52:01 +0000 | [diff] [blame] | 926 | void emitInlineAsmError(ImmutableCallSite CS, const Twine &Message); |
| 927 | |
| Devang Patel | 32a72ab | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 928 | /// EmitFuncArgumentDbgValue - If V is an function argument then create |
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 929 | /// corresponding DBG_VALUE machine instruction for it now. At the end of |
| Devang Patel | 32a72ab | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 930 | /// instruction selection, they will be inserted to the entry BB. |
| Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 931 | bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable, |
| 932 | DIExpression *Expr, DILocation *DL, |
| Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 933 | int64_t Offset, bool IsDbgDeclare, |
| Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 934 | const SDValue &N); |
| Hans Wennborg | b4db142 | 2015-03-19 20:41:48 +0000 | [diff] [blame] | 935 | |
| 936 | /// Return the next block after MBB, or nullptr if there is none. |
| 937 | MachineBasicBlock *NextBlock(MachineBasicBlock *MBB); |
| Krzysztof Parzyszek | a46c36b | 2015-04-13 17:16:45 +0000 | [diff] [blame] | 938 | |
| 939 | /// Update the DAG and DAG builder with the relevant information after |
| 940 | /// a new root node has been created which could be a tail call. |
| 941 | void updateDAGForMaybeTailCall(SDValue MaybeTC); |
| Wolfgang Pieb | dfad9b2 | 2016-08-15 18:18:26 +0000 | [diff] [blame] | 942 | |
| 943 | /// Return the appropriate SDDbgValue based on N. |
| 944 | SDDbgValue *getDbgValue(SDValue N, DILocalVariable *Variable, |
| Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 945 | DIExpression *Expr, int64_t Offset, |
| 946 | const DebugLoc &dl, unsigned DbgSDNodeOrder); |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 949 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 950 | /// that a particular set of values is assigned, and the type information about |
| 951 | /// the value. The most common situation is to represent one value at a time, |
| 952 | /// but struct or array values are handled element-wise as multiple values. The |
| 953 | /// splitting of aggregates is performed recursively, so that we never have |
| 954 | /// aggregate-typed registers. The values at this point do not necessarily have |
| 955 | /// legal types, so each value may require one or more registers of some legal |
| 956 | /// type. |
| 957 | /// |
| 958 | struct RegsForValue { |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 959 | /// The value types of the values, which may not be legal, and |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 960 | /// may need be promoted or synthesized from one or more registers. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 961 | SmallVector<EVT, 4> ValueVTs; |
| 962 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 963 | /// The value types of the registers. This is the same size as ValueVTs and it |
| 964 | /// records, for each value, what the type of the assigned register or |
| 965 | /// registers are. (Individual values are never synthesized from more than one |
| 966 | /// type of register.) |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 967 | /// |
| 968 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 969 | /// getRegisterType member function, however when with physical registers |
| 970 | /// it is necessary to have a separate record of the types. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 971 | SmallVector<MVT, 4> RegVTs; |
| 972 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 973 | /// This list holds the registers assigned to the values. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 974 | /// Each legal or promoted value requires one register, and each |
| 975 | /// expanded value requires multiple registers. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 976 | SmallVector<unsigned, 4> Regs; |
| 977 | |
| Simon Dardis | 212cccb | 2017-06-09 14:37:08 +0000 | [diff] [blame] | 978 | /// This list holds the number of registers for each value. |
| 979 | SmallVector<unsigned, 4> RegCount; |
| 980 | |
| 981 | /// Records if this value needs to be treated in an ABI dependant manner, |
| 982 | /// different to normal type legalization. |
| 983 | bool IsABIMangled; |
| 984 | |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 985 | RegsForValue(); |
| 986 | |
| Simon Dardis | 212cccb | 2017-06-09 14:37:08 +0000 | [diff] [blame] | 987 | RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt, EVT valuevt, |
| 988 | bool IsABIMangledValue = false); |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 989 | |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 990 | RegsForValue(LLVMContext &Context, const TargetLowering &TLI, |
| Simon Dardis | 212cccb | 2017-06-09 14:37:08 +0000 | [diff] [blame] | 991 | const DataLayout &DL, unsigned Reg, Type *Ty, |
| 992 | bool IsABIMangledValue = false); |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 993 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 994 | /// Add the specified values to this one. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 995 | void append(const RegsForValue &RHS) { |
| 996 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 997 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 998 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| Simon Dardis | 212cccb | 2017-06-09 14:37:08 +0000 | [diff] [blame] | 999 | RegCount.push_back(RHS.Regs.size()); |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 1002 | /// Emit a series of CopyFromReg nodes that copies from this value and returns |
| 1003 | /// the result as a ValueVTs value. This uses Chain/Flag as the input and |
| 1004 | /// updates them for the output Chain/Flag. If the Flag pointer is NULL, no |
| 1005 | /// flag is used. |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 1006 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1007 | const SDLoc &dl, SDValue &Chain, SDValue *Flag, |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 1008 | const Value *V = nullptr) const; |
| 1009 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 1010 | /// Emit a series of CopyToReg nodes that copies the specified value into the |
| 1011 | /// registers specified by this object. This uses Chain/Flag as the input and |
| 1012 | /// updates them for the output Chain/Flag. If the Flag pointer is nullptr, no |
| 1013 | /// flag is used. If V is not nullptr, then it is used in printing better |
| 1014 | /// diagnostic messages on error. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1015 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, |
| 1016 | SDValue &Chain, SDValue *Flag, const Value *V = nullptr, |
| 1017 | ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const; |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 1018 | |
| Sanjay Patel | 209b0f9 | 2017-03-02 20:48:08 +0000 | [diff] [blame] | 1019 | /// Add this value to the specified inlineasm node operand list. This adds the |
| 1020 | /// code marker, matching input operand index (if applicable), and includes |
| 1021 | /// the number of values added into it. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1022 | void AddInlineAsmOperands(unsigned Kind, bool HasMatching, |
| 1023 | unsigned MatchingIdx, const SDLoc &dl, |
| 1024 | SelectionDAG &DAG, std::vector<SDValue> &Ops) const; |
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 1025 | }; |
| 1026 | |
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1027 | } // end namespace llvm |
| 1028 | |
| 1029 | #endif |