| 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" | 
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Analysis.h" | 
| Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SelectionDAG.h" | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/SelectionDAGNodes.h" | 
| Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CallSite.h" | 
| Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Statepoint.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" | 
| Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" | 
| Juergen Ributzka | fd4633e | 2014-10-16 21:26:35 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLowering.h" | 
| 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) { } | 
| Devang Patel | b12ff59 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 104 | DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) : | 
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 105 | DI(di), dl(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 | }; | 
| Hans Wennborg | 7bf4d4e | 2015-04-27 23:52:19 +0000 | [diff] [blame] | 157 | uint32_t Weight; | 
| 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, | 
|  | 160 | MachineBasicBlock *MBB, uint32_t Weight) { | 
|  | 161 | CaseCluster C; | 
|  | 162 | C.Kind = CC_Range; | 
|  | 163 | C.Low = Low; | 
|  | 164 | C.High = High; | 
|  | 165 | C.MBB = MBB; | 
|  | 166 | C.Weight = Weight; | 
|  | 167 | return C; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | static CaseCluster jumpTable(const ConstantInt *Low, | 
|  | 171 | const ConstantInt *High, unsigned JTCasesIndex, | 
|  | 172 | uint32_t Weight) { | 
|  | 173 | CaseCluster C; | 
|  | 174 | C.Kind = CC_JumpTable; | 
|  | 175 | C.Low = Low; | 
|  | 176 | C.High = High; | 
|  | 177 | C.JTCasesIndex = JTCasesIndex; | 
|  | 178 | C.Weight = Weight; | 
|  | 179 | return C; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | static CaseCluster bitTests(const ConstantInt *Low, const ConstantInt *High, | 
|  | 183 | unsigned BTCasesIndex, uint32_t Weight) { | 
|  | 184 | CaseCluster C; | 
|  | 185 | C.Kind = CC_BitTests; | 
|  | 186 | C.Low = Low; | 
|  | 187 | C.High = High; | 
|  | 188 | C.BTCasesIndex = BTCasesIndex; | 
|  | 189 | C.Weight = Weight; | 
|  | 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; | 
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 201 | uint32_t ExtraWeight; | 
| 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, | 
|  | 204 | uint32_t Weight): | 
|  | 205 | Mask(mask), BB(bb), Bits(bits), ExtraWeight(Weight) { } | 
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 206 |  | 
|  | 207 | CaseBits() : Mask(0), BB(nullptr), Bits(0), ExtraWeight(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, | 
|  | 220 | const Value *cmpmiddle, | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 221 | MachineBasicBlock *truebb, MachineBasicBlock *falsebb, | 
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 222 | MachineBasicBlock *me, | 
|  | 223 | uint32_t trueweight = 0, uint32_t falseweight = 0) | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 224 | : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), | 
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 225 | TrueBB(truebb), FalseBB(falsebb), ThisBB(me), | 
|  | 226 | TrueWeight(trueweight), FalseWeight(falseweight) { } | 
|  | 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 |  | 
|  | 242 | // TrueWeight/FalseWeight - branch weights. | 
|  | 243 | uint32_t TrueWeight, FalseWeight; | 
| 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, | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 263 | bool E = false): | 
|  | 264 | First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} | 
| Anton Korobeynikov | 6f21913 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 265 | APInt First; | 
|  | 266 | APInt Last; | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 267 | const Value *SValue; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 268 | MachineBasicBlock *HeaderBB; | 
|  | 269 | bool Emitted; | 
|  | 270 | }; | 
|  | 271 | typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; | 
|  | 272 |  | 
|  | 273 | struct BitTestCase { | 
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 274 | BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr, | 
|  | 275 | uint32_t Weight): | 
|  | 276 | Mask(M), ThisBB(T), TargetBB(Tr), ExtraWeight(Weight) { } | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 277 | uint64_t Mask; | 
| Chris Lattner | 24576a5 | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 278 | MachineBasicBlock *ThisBB; | 
|  | 279 | MachineBasicBlock *TargetBB; | 
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 280 | uint32_t ExtraWeight; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 281 | }; | 
|  | 282 |  | 
|  | 283 | typedef SmallVector<BitTestCase, 3> BitTestInfo; | 
|  | 284 |  | 
|  | 285 | struct BitTestBlock { | 
| Cong Hou | 0312770 | 2015-08-26 23:15:32 +0000 | [diff] [blame] | 286 | BitTestBlock(APInt F, APInt R, const Value *SV, unsigned Rg, MVT RgVT, | 
|  | 287 | bool E, bool CR, MachineBasicBlock *P, MachineBasicBlock *D, | 
|  | 288 | BitTestInfo C, uint32_t W) | 
|  | 289 | : First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E), | 
|  | 290 | ContiguousRange(CR), Parent(P), Default(D), Cases(std::move(C)), | 
| Cong Hou | 511298b | 2015-09-01 01:42:16 +0000 | [diff] [blame] | 291 | Weight(W), DefaultWeight(0) {} | 
| Anton Korobeynikov | 6f21913 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 292 | APInt First; | 
|  | 293 | APInt Range; | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 294 | const Value *SValue; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 295 | unsigned Reg; | 
| Patrik Hagglund | 4e0f828 | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 296 | MVT RegVT; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 297 | bool Emitted; | 
| Cong Hou | cd59591 | 2015-08-25 21:34:38 +0000 | [diff] [blame] | 298 | bool ContiguousRange; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 299 | MachineBasicBlock *Parent; | 
|  | 300 | MachineBasicBlock *Default; | 
|  | 301 | BitTestInfo Cases; | 
| Cong Hou | 0312770 | 2015-08-26 23:15:32 +0000 | [diff] [blame] | 302 | uint32_t Weight; | 
| Cong Hou | 511298b | 2015-09-01 01:42:16 +0000 | [diff] [blame] | 303 | uint32_t DefaultWeight; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 304 | }; | 
|  | 305 |  | 
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 306 | /// Minimum jump table density, in percent. | 
|  | 307 | enum { MinJumpTableDensity = 40 }; | 
|  | 308 |  | 
|  | 309 | /// Check whether a range of clusters is dense enough for a jump table. | 
|  | 310 | bool isDense(const CaseClusterVector &Clusters, unsigned *TotalCases, | 
|  | 311 | unsigned First, unsigned Last); | 
|  | 312 |  | 
|  | 313 | /// Build a jump table cluster from Clusters[First..Last]. Returns false if it | 
|  | 314 | /// decides it's not a good idea. | 
|  | 315 | bool buildJumpTable(CaseClusterVector &Clusters, unsigned First, | 
|  | 316 | unsigned Last, const SwitchInst *SI, | 
|  | 317 | MachineBasicBlock *DefaultMBB, CaseCluster &JTCluster); | 
|  | 318 |  | 
|  | 319 | /// Find clusters of cases suitable for jump table lowering. | 
|  | 320 | void findJumpTables(CaseClusterVector &Clusters, const SwitchInst *SI, | 
|  | 321 | MachineBasicBlock *DefaultMBB); | 
|  | 322 |  | 
|  | 323 | /// Check whether the range [Low,High] fits in a machine word. | 
|  | 324 | bool rangeFitsInWord(const APInt &Low, const APInt &High); | 
|  | 325 |  | 
|  | 326 | /// Check whether these clusters are suitable for lowering with bit tests based | 
|  | 327 | /// on the number of destinations, comparison metric, and range. | 
|  | 328 | bool isSuitableForBitTests(unsigned NumDests, unsigned NumCmps, | 
|  | 329 | const APInt &Low, const APInt &High); | 
|  | 330 |  | 
|  | 331 | /// Build a bit test cluster from Clusters[First..Last]. Returns false if it | 
|  | 332 | /// decides it's not a good idea. | 
|  | 333 | bool buildBitTests(CaseClusterVector &Clusters, unsigned First, unsigned Last, | 
|  | 334 | const SwitchInst *SI, CaseCluster &BTCluster); | 
|  | 335 |  | 
|  | 336 | /// Find clusters of cases suitable for bit test lowering. | 
|  | 337 | void findBitTestClusters(CaseClusterVector &Clusters, const SwitchInst *SI); | 
|  | 338 |  | 
|  | 339 | struct SwitchWorkListItem { | 
|  | 340 | MachineBasicBlock *MBB; | 
|  | 341 | CaseClusterIt FirstCluster; | 
|  | 342 | CaseClusterIt LastCluster; | 
|  | 343 | const ConstantInt *GE; | 
|  | 344 | const ConstantInt *LT; | 
| Cong Hou | 511298b | 2015-09-01 01:42:16 +0000 | [diff] [blame] | 345 | uint32_t DefaultWeight; | 
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 346 | }; | 
|  | 347 | typedef SmallVector<SwitchWorkListItem, 4> SwitchWorkList; | 
|  | 348 |  | 
| Hans Wennborg | 6ed81cb | 2015-06-20 17:14:07 +0000 | [diff] [blame] | 349 | /// Determine the rank by weight of CC in [First,Last]. If CC has more weight | 
|  | 350 | /// than each cluster in the range, its rank is 0. | 
|  | 351 | static unsigned caseClusterRank(const CaseCluster &CC, CaseClusterIt First, | 
|  | 352 | CaseClusterIt Last); | 
|  | 353 |  | 
| Hans Wennborg | 0867b15 | 2015-04-23 16:45:24 +0000 | [diff] [blame] | 354 | /// Emit comparison and split W into two subtrees. | 
|  | 355 | void splitWorkItem(SwitchWorkList &WorkList, const SwitchWorkListItem &W, | 
|  | 356 | Value *Cond, MachineBasicBlock *SwitchMBB); | 
|  | 357 |  | 
|  | 358 | /// Lower W. | 
|  | 359 | void lowerWorkItem(SwitchWorkListItem W, Value *Cond, | 
|  | 360 | MachineBasicBlock *SwitchMBB, | 
|  | 361 | MachineBasicBlock *DefaultMBB); | 
|  | 362 |  | 
|  | 363 |  | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 364 | /// A class which encapsulates all of the information needed to generate a | 
|  | 365 | /// stack protector check and signals to isel via its state being initialized | 
|  | 366 | /// that a stack protector needs to be generated. | 
|  | 367 | /// | 
|  | 368 | /// *NOTE* The following is a high level documentation of SelectionDAG Stack | 
|  | 369 | /// Protector Generation. The reason that it is placed here is for a lack of | 
|  | 370 | /// other good places to stick it. | 
|  | 371 | /// | 
|  | 372 | /// High Level Overview of SelectionDAG Stack Protector Generation: | 
|  | 373 | /// | 
|  | 374 | /// Previously, generation of stack protectors was done exclusively in the | 
|  | 375 | /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated | 
|  | 376 | /// splitting basic blocks at the IR level to create the success/failure basic | 
|  | 377 | /// blocks in the tail of the basic block in question. As a result of this, | 
|  | 378 | /// calls that would have qualified for the sibling call optimization were no | 
|  | 379 | /// longer eligible for optimization since said calls were no longer right in | 
|  | 380 | /// the "tail position" (i.e. the immediate predecessor of a ReturnInst | 
|  | 381 | /// instruction). | 
|  | 382 | /// | 
|  | 383 | /// Then it was noticed that since the sibling call optimization causes the | 
|  | 384 | /// callee to reuse the caller's stack, if we could delay the generation of | 
|  | 385 | /// the stack protector check until later in CodeGen after the sibling call | 
|  | 386 | /// decision was made, we get both the tail call optimization and the stack | 
|  | 387 | /// protector check! | 
|  | 388 | /// | 
|  | 389 | /// A few goals in solving this problem were: | 
|  | 390 | /// | 
|  | 391 | ///   1. Preserve the architecture independence of stack protector generation. | 
|  | 392 | /// | 
|  | 393 | ///   2. Preserve the normal IR level stack protector check for platforms like | 
| Alp Toker | cf21875 | 2014-06-30 18:57:16 +0000 | [diff] [blame] | 394 | ///      OpenBSD for which we support platform-specific stack protector | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 395 | ///      generation. | 
|  | 396 | /// | 
|  | 397 | /// The main problem that guided the present solution is that one can not | 
|  | 398 | /// solve this problem in an architecture independent manner at the IR level | 
|  | 399 | /// only. This is because: | 
|  | 400 | /// | 
|  | 401 | ///   1. The decision on whether or not to perform a sibling call on certain | 
|  | 402 | ///      platforms (for instance i386) requires lower level information | 
|  | 403 | ///      related to available registers that can not be known at the IR level. | 
|  | 404 | /// | 
|  | 405 | ///   2. Even if the previous point were not true, the decision on whether to | 
|  | 406 | ///      perform a tail call is done in LowerCallTo in SelectionDAG which | 
|  | 407 | ///      occurs after the Stack Protector Pass. As a result, one would need to | 
|  | 408 | ///      put the relevant callinst into the stack protector check success | 
|  | 409 | ///      basic block (where the return inst is placed) and then move it back | 
|  | 410 | ///      later at SelectionDAG/MI time before the stack protector check if the | 
|  | 411 | ///      tail call optimization failed. The MI level option was nixed | 
| Alp Toker | cf21875 | 2014-06-30 18:57:16 +0000 | [diff] [blame] | 412 | ///      immediately since it would require platform-specific pattern | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 413 | ///      matching. The SelectionDAG level option was nixed because | 
|  | 414 | ///      SelectionDAG only processes one IR level basic block at a time | 
|  | 415 | ///      implying one could not create a DAG Combine to move the callinst. | 
|  | 416 | /// | 
|  | 417 | /// To get around this problem a few things were realized: | 
|  | 418 | /// | 
|  | 419 | ///   1. While one can not handle multiple IR level basic blocks at the | 
|  | 420 | ///      SelectionDAG Level, one can generate multiple machine basic blocks | 
|  | 421 | ///      for one IR level basic block. This is how we handle bit tests and | 
|  | 422 | ///      switches. | 
|  | 423 | /// | 
|  | 424 | ///   2. At the MI level, tail calls are represented via a special return | 
|  | 425 | ///      MIInst called "tcreturn". Thus if we know the basic block in which we | 
|  | 426 | ///      wish to insert the stack protector check, we get the correct behavior | 
|  | 427 | ///      by always inserting the stack protector check right before the return | 
|  | 428 | ///      statement. This is a "magical transformation" since no matter where | 
|  | 429 | ///      the stack protector check intrinsic is, we always insert the stack | 
|  | 430 | ///      protector check code at the end of the BB. | 
|  | 431 | /// | 
|  | 432 | /// Given the aforementioned constraints, the following solution was devised: | 
|  | 433 | /// | 
|  | 434 | ///   1. On platforms that do not support SelectionDAG stack protector check | 
|  | 435 | ///      generation, allow for the normal IR level stack protector check | 
|  | 436 | ///      generation to continue. | 
|  | 437 | /// | 
|  | 438 | ///   2. On platforms that do support SelectionDAG stack protector check | 
|  | 439 | ///      generation: | 
|  | 440 | /// | 
|  | 441 | ///     a. Use the IR level stack protector pass to decide if a stack | 
|  | 442 | ///        protector is required/which BB we insert the stack protector check | 
|  | 443 | ///        in by reusing the logic already therein. If we wish to generate a | 
|  | 444 | ///        stack protector check in a basic block, we place a special IR | 
|  | 445 | ///        intrinsic called llvm.stackprotectorcheck right before the BB's | 
|  | 446 | ///        returninst or if there is a callinst that could potentially be | 
|  | 447 | ///        sibling call optimized, before the call inst. | 
|  | 448 | /// | 
|  | 449 | ///     b. Then when a BB with said intrinsic is processed, we codegen the BB | 
|  | 450 | ///        normally via SelectBasicBlock. In said process, when we visit the | 
|  | 451 | ///        stack protector check, we do not actually emit anything into the | 
|  | 452 | ///        BB. Instead, we just initialize the stack protector descriptor | 
|  | 453 | ///        class (which involves stashing information/creating the success | 
|  | 454 | ///        mbbb and the failure mbb if we have not created one for this | 
|  | 455 | ///        function yet) and export the guard variable that we are going to | 
|  | 456 | ///        compare. | 
|  | 457 | /// | 
|  | 458 | ///     c. After we finish selecting the basic block, in FinishBasicBlock if | 
|  | 459 | ///        the StackProtectorDescriptor attached to the SelectionDAGBuilder is | 
|  | 460 | ///        initialized, we first find a splice point in the parent basic block | 
|  | 461 | ///        before the terminator and then splice the terminator of said basic | 
|  | 462 | ///        block into the success basic block. Then we code-gen a new tail for | 
|  | 463 | ///        the parent basic block consisting of the two loads, the comparison, | 
|  | 464 | ///        and finally two branches to the success/failure basic blocks. We | 
|  | 465 | ///        conclude by code-gening the failure basic block if we have not | 
|  | 466 | ///        code-gened it already (all stack protector checks we generate in | 
|  | 467 | ///        the same function, use the same failure basic block). | 
|  | 468 | class StackProtectorDescriptor { | 
|  | 469 | public: | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 470 | StackProtectorDescriptor() : ParentMBB(nullptr), SuccessMBB(nullptr), | 
| Akira Hatanaka | e5b6e0d | 2014-07-25 19:31:34 +0000 | [diff] [blame] | 471 | FailureMBB(nullptr), Guard(nullptr), | 
|  | 472 | GuardReg(0) { } | 
| 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 { | 
|  | 477 | return ParentMBB && SuccessMBB && FailureMBB && Guard; | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | /// Initialize the stack protector descriptor structure for a new basic | 
|  | 481 | /// block. | 
|  | 482 | void initialize(const BasicBlock *BB, | 
|  | 483 | MachineBasicBlock *MBB, | 
|  | 484 | const CallInst &StackProtCheckCall) { | 
|  | 485 | // Make sure we are not initialized yet. | 
|  | 486 | assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " | 
|  | 487 | "already initialized!"); | 
|  | 488 | ParentMBB = MBB; | 
| Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 489 | SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true); | 
|  | 490 | FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 491 | if (!Guard) | 
|  | 492 | Guard = StackProtCheckCall.getArgOperand(0); | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | /// Reset state that changes when we handle different basic blocks. | 
|  | 496 | /// | 
|  | 497 | /// This currently includes: | 
|  | 498 | /// | 
|  | 499 | /// 1. The specific basic block we are generating a | 
|  | 500 | /// stack protector for (ParentMBB). | 
|  | 501 | /// | 
|  | 502 | /// 2. The successor machine basic block that will contain the tail of | 
|  | 503 | /// parent mbb after we create the stack protector check (SuccessMBB). This | 
|  | 504 | /// BB is visited only on stack protector check success. | 
|  | 505 | void resetPerBBState() { | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 506 | ParentMBB = nullptr; | 
|  | 507 | SuccessMBB = nullptr; | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 508 | } | 
|  | 509 |  | 
|  | 510 | /// Reset state that only changes when we switch functions. | 
|  | 511 | /// | 
|  | 512 | /// This currently includes: | 
|  | 513 | /// | 
|  | 514 | /// 1. FailureMBB since we reuse the failure code path for all stack | 
|  | 515 | /// protector checks created in an individual function. | 
|  | 516 | /// | 
|  | 517 | /// 2.The guard variable since the guard variable we are checking against is | 
|  | 518 | /// always the same. | 
|  | 519 | void resetPerFunctionState() { | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 520 | FailureMBB = nullptr; | 
|  | 521 | Guard = nullptr; | 
| Matthias Braun | 4e7ded8 | 2015-08-26 20:46:52 +0000 | [diff] [blame] | 522 | GuardReg = 0; | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 523 | } | 
|  | 524 |  | 
|  | 525 | MachineBasicBlock *getParentMBB() { return ParentMBB; } | 
|  | 526 | MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } | 
|  | 527 | MachineBasicBlock *getFailureMBB() { return FailureMBB; } | 
|  | 528 | const Value *getGuard() { return Guard; } | 
|  | 529 |  | 
| Akira Hatanaka | e5b6e0d | 2014-07-25 19:31:34 +0000 | [diff] [blame] | 530 | unsigned getGuardReg() const { return GuardReg; } | 
|  | 531 | void setGuardReg(unsigned R) { GuardReg = R; } | 
|  | 532 |  | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 533 | private: | 
|  | 534 | /// The basic block for which we are generating the stack protector. | 
|  | 535 | /// | 
|  | 536 | /// As a result of stack protector generation, we will splice the | 
|  | 537 | /// terminators of this basic block into the successor mbb SuccessMBB and | 
|  | 538 | /// replace it with a compare/branch to the successor mbbs | 
|  | 539 | /// SuccessMBB/FailureMBB depending on whether or not the stack protector | 
|  | 540 | /// was violated. | 
|  | 541 | MachineBasicBlock *ParentMBB; | 
|  | 542 |  | 
|  | 543 | /// A basic block visited on stack protector check success that contains the | 
|  | 544 | /// terminators of ParentMBB. | 
|  | 545 | MachineBasicBlock *SuccessMBB; | 
|  | 546 |  | 
|  | 547 | /// This basic block visited on stack protector check failure that will | 
|  | 548 | /// contain a call to __stack_chk_fail(). | 
|  | 549 | MachineBasicBlock *FailureMBB; | 
|  | 550 |  | 
|  | 551 | /// The guard variable which we will compare against the stored value in the | 
|  | 552 | /// stack protector stack slot. | 
|  | 553 | const Value *Guard; | 
|  | 554 |  | 
| Akira Hatanaka | e5b6e0d | 2014-07-25 19:31:34 +0000 | [diff] [blame] | 555 | /// The virtual register holding the stack guard value. | 
|  | 556 | unsigned GuardReg; | 
|  | 557 |  | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 558 | /// Add a successor machine basic block to ParentMBB. If the successor mbb | 
|  | 559 | /// 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] | 560 | /// block will be created. Assign a large weight if IsLikely is true. | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 561 | MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, | 
|  | 562 | MachineBasicBlock *ParentMBB, | 
| Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 563 | bool IsLikely, | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 564 | MachineBasicBlock *SuccMBB = nullptr); | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 565 | }; | 
|  | 566 |  | 
| Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 567 | private: | 
| Dan Gohman | c334960 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 568 | const TargetMachine &TM; | 
| Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 569 | public: | 
| Nico Rieck | b5262d6 | 2014-01-12 14:09:17 +0000 | [diff] [blame] | 570 | /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling | 
|  | 571 | /// nodes without a corresponding SDNode. | 
|  | 572 | static const unsigned LowestSDNodeOrder = 1; | 
|  | 573 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 574 | SelectionDAG &DAG; | 
| Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 575 | const DataLayout *DL; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 576 | AliasAnalysis *AA; | 
| Owen Anderson | bb15fec | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 577 | const TargetLibraryInfo *LibInfo; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 578 |  | 
|  | 579 | /// SwitchCases - Vector of CaseBlock structures used to communicate | 
|  | 580 | /// SwitchInst code generation information. | 
|  | 581 | std::vector<CaseBlock> SwitchCases; | 
|  | 582 | /// JTCases - Vector of JumpTable structures used to communicate | 
|  | 583 | /// SwitchInst code generation information. | 
|  | 584 | std::vector<JumpTableBlock> JTCases; | 
|  | 585 | /// BitTestCases - Vector of BitTestBlock structures used to communicate | 
|  | 586 | /// SwitchInst code generation information. | 
|  | 587 | std::vector<BitTestBlock> BitTestCases; | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 588 | /// A StackProtectorDescriptor structure used to communicate stack protector | 
|  | 589 | /// information in between SelectBasicBlock and FinishBasicBlock. | 
|  | 590 | StackProtectorDescriptor SPDescriptor; | 
| Evan Cheng | 270d0f9 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 591 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 592 | // Emit PHI-node-operand constants only once even if used by multiple | 
|  | 593 | // PHI nodes. | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 594 | DenseMap<const Constant *, unsigned> ConstantsOut; | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 595 |  | 
|  | 596 | /// FuncInfo - Information about the function as a whole. | 
|  | 597 | /// | 
|  | 598 | FunctionLoweringInfo &FuncInfo; | 
| Bill Wendling | 19e0a5b | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 599 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 600 | /// GFI - Garbage collection metadata for the function. | 
|  | 601 | GCFunctionInfo *GFI; | 
|  | 602 |  | 
| Bill Wendling | 267f323 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 603 | /// LPadToCallSiteMap - Map a landing pad to the call site indexes. | 
|  | 604 | DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap; | 
| Bill Wendling | 3d11aa7 | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 605 |  | 
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 606 | /// HasTailCall - This is set to true if a call in the current | 
|  | 607 | /// block has been translated as a tail call. In this case, | 
|  | 608 | /// no subsequent DAG nodes should be created. | 
|  | 609 | /// | 
|  | 610 | bool HasTailCall; | 
|  | 611 |  | 
| Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 612 | LLVMContext *Context; | 
|  | 613 |  | 
| Dan Gohman | c334960 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 614 | SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, | 
| Dan Gohman | 1a6c47f | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 615 | CodeGenOpt::Level ol) | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 616 | : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), | 
| Benjamin Kramer | bacc7ba | 2015-10-15 17:54:06 +0000 | [diff] [blame] | 617 | DAG(dag), FuncInfo(funcinfo), | 
| Richard Smith | 3fb2047 | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 618 | HasTailCall(false) { | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
| Owen Anderson | bb15fec | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 621 | void init(GCFunctionInfo *gfi, AliasAnalysis &aa, | 
|  | 622 | const TargetLibraryInfo *li); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 623 |  | 
| Dan Gohman | f5cca35 | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 624 | /// clear - Clear out the current SelectionDAG and the associated | 
| Dan Gohman | 1a6c47f | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 625 | /// state and prepare this SelectionDAGBuilder object to be used | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 626 | /// for a new block. This doesn't clear out information about | 
|  | 627 | /// additional blocks that are needed to complete switch lowering | 
|  | 628 | /// or PHI node updating; that information is cleared out as it is | 
|  | 629 | /// consumed. | 
|  | 630 | void clear(); | 
|  | 631 |  | 
| Devang Patel | 79928838 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 632 | /// clearDanglingDebugInfo - Clear the dangling debug information | 
| Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 633 | /// map. This function is separated from the clear so that debug | 
| Devang Patel | 79928838 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 634 | /// information that is dangling in a basic block can be properly | 
|  | 635 | /// resolved in a different basic block. This allows the | 
|  | 636 | /// SelectionDAG to resolve dangling debug information attached | 
|  | 637 | /// to PHI nodes. | 
|  | 638 | void clearDanglingDebugInfo(); | 
|  | 639 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 640 | /// getRoot - Return the current virtual root of the Selection DAG, | 
|  | 641 | /// flushing any PendingLoad items. This must be done before emitting | 
|  | 642 | /// a store or any other node that may need to be ordered after any | 
|  | 643 | /// prior load instructions. | 
|  | 644 | /// | 
|  | 645 | SDValue getRoot(); | 
|  | 646 |  | 
|  | 647 | /// getControlRoot - Similar to getRoot, but instead of flushing all the | 
|  | 648 | /// PendingLoad items, flush all the PendingExports items. It is necessary | 
|  | 649 | /// to do this before emitting a terminator instruction. | 
|  | 650 | /// | 
|  | 651 | SDValue getControlRoot(); | 
|  | 652 |  | 
| Andrew Trick | 175143b | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 653 | SDLoc getCurSDLoc() const { | 
| Andrew Trick | 175143b | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 654 | return SDLoc(CurInst, SDNodeOrder); | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | DebugLoc getCurDebugLoc() const { | 
|  | 658 | return CurInst ? CurInst->getDebugLoc() : DebugLoc(); | 
|  | 659 | } | 
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 660 |  | 
| Bill Wendling | 919b7aa | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 661 | unsigned getSDNodeOrder() const { return SDNodeOrder; } | 
|  | 662 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 663 | void CopyValueToVirtualRegister(const Value *V, unsigned Reg); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 664 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 665 | void visit(const Instruction &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 666 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 667 | void visit(unsigned Opcode, const User &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 668 |  | 
| Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 669 | /// getCopyFromRegs - If there was virtual register allocated for the value V | 
|  | 670 | /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise. | 
|  | 671 | SDValue getCopyFromRegs(const Value *V, Type *Ty); | 
|  | 672 |  | 
| Dale Johannesen | bfd4fd7 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 673 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, | 
|  | 674 | // generate the debug data structures now that we've seen its definition. | 
|  | 675 | void resolveDanglingDebugInfo(const Value *V, SDValue Val); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 676 | SDValue getValue(const Value *V); | 
| Elena Demikhovsky | 584ce37 | 2015-04-28 07:57:37 +0000 | [diff] [blame] | 677 | bool findValue(const Value *V) const; | 
|  | 678 |  | 
| Dan Gohman | d432223 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 679 | SDValue getNonRegisterValue(const Value *V); | 
|  | 680 | SDValue getValueImpl(const Value *V); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 681 |  | 
|  | 682 | void setValue(const Value *V, SDValue NewN) { | 
|  | 683 | SDValue &N = NodeMap[V]; | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 684 | assert(!N.getNode() && "Already set a value for this node!"); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 685 | N = NewN; | 
|  | 686 | } | 
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 687 |  | 
| Devang Patel | b0c7639 | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 688 | void setUnusedArgValue(const Value *V, SDValue NewN) { | 
|  | 689 | SDValue &N = UnusedArgNodeMap[V]; | 
| Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 690 | assert(!N.getNode() && "Already set a value for this node!"); | 
| Devang Patel | b0c7639 | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 691 | N = NewN; | 
|  | 692 | } | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 693 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 694 | void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 695 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, | 
| Pete Cooper | 6923461 | 2015-07-15 01:31:26 +0000 | [diff] [blame] | 696 | MachineBasicBlock *SwitchBB, | 
|  | 697 | Instruction::BinaryOps Opc, | 
| Manman Ren | 4ece745 | 2014-01-31 00:42:44 +0000 | [diff] [blame] | 698 | uint32_t TW, uint32_t FW); | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 699 | void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, | 
| Dan Gohman | d01ddb5 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 700 | MachineBasicBlock *FBB, | 
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 701 | MachineBasicBlock *CurBB, | 
| Manman Ren | 4ece745 | 2014-01-31 00:42:44 +0000 | [diff] [blame] | 702 | MachineBasicBlock *SwitchBB, | 
|  | 703 | uint32_t TW, uint32_t FW); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 704 | bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 705 | bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); | 
|  | 706 | void CopyToExportRegsIfNeeded(const Value *V); | 
|  | 707 | void ExportFromCurrentBlock(const Value *V); | 
|  | 708 | void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, | 
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 709 | const BasicBlock *EHPadBB = nullptr); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 710 |  | 
| Juergen Ributzka | ad2363f | 2014-10-17 17:39:00 +0000 | [diff] [blame] | 711 | std::pair<SDValue, SDValue> lowerCallOperands( | 
|  | 712 | ImmutableCallSite CS, | 
|  | 713 | unsigned ArgIdx, | 
|  | 714 | unsigned NumArgs, | 
|  | 715 | SDValue Callee, | 
| Sanjoy Das | 84153c4 | 2015-05-05 23:06:52 +0000 | [diff] [blame] | 716 | Type *ReturnTy, | 
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 717 | const BasicBlock *EHPadBB = nullptr, | 
| Hal Finkel | 0ad96c8 | 2015-01-13 17:48:04 +0000 | [diff] [blame] | 718 | bool IsPatchPoint = false); | 
| Andrew Trick | 74f4c74 | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 719 |  | 
| Jakob Stoklund Olesen | 665aa6e | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 720 | /// UpdateSplitBlock - When an MBB was split during scheduling, update the | 
| Alp Toker | 798060e | 2014-01-11 14:01:43 +0000 | [diff] [blame] | 721 | /// references that need to refer to the last resulting block. | 
| Jakob Stoklund Olesen | 665aa6e | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 722 | void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); | 
|  | 723 |  | 
| Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 724 | // This function is responsible for the whole statepoint lowering process. | 
| Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 725 | // It uniformly handles invoke and call statepoints. | 
|  | 726 | void LowerStatepoint(ImmutableStatepoint Statepoint, | 
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 727 | const BasicBlock *EHPadBB = nullptr); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 728 | private: | 
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 729 | std::pair<SDValue, SDValue> | 
|  | 730 | lowerInvokable(TargetLowering::CallLoweringInfo &CLI, | 
|  | 731 | const BasicBlock *EHPadBB = nullptr); | 
| Juergen Ributzka | fd4633e | 2014-10-16 21:26:35 +0000 | [diff] [blame] | 732 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 733 | // Terminator instructions. | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 734 | void visitRet(const ReturnInst &I); | 
|  | 735 | void visitBr(const BranchInst &I); | 
|  | 736 | void visitSwitch(const SwitchInst &I); | 
|  | 737 | void visitIndirectBr(const IndirectBrInst &I); | 
| Yaron Keren | d7ba46b | 2014-04-19 13:47:43 +0000 | [diff] [blame] | 738 | void visitUnreachable(const UnreachableInst &I); | 
| Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 739 | void visitCleanupEndPad(const CleanupEndPadInst &I); | 
| David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 740 | void visitCleanupRet(const CleanupReturnInst &I); | 
|  | 741 | void visitCatchEndPad(const CatchEndPadInst &I); | 
|  | 742 | void visitCatchRet(const CatchReturnInst &I); | 
|  | 743 | void visitCatchPad(const CatchPadInst &I); | 
|  | 744 | void visitTerminatePad(const TerminatePadInst &TPI); | 
|  | 745 | void visitCleanupPad(const CleanupPadInst &CPI); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 746 |  | 
| Jakub Staszak | 96f8c55 | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 747 | uint32_t getEdgeWeight(const MachineBasicBlock *Src, | 
|  | 748 | const MachineBasicBlock *Dst) const; | 
| Jakub Staszak | 0480a8f | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 749 | void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, | 
|  | 750 | uint32_t Weight = 0); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 751 | public: | 
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 752 | void visitSwitchCase(CaseBlock &CB, | 
|  | 753 | MachineBasicBlock *SwitchBB); | 
| Michael Gottesman | b27f0f1 | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 754 | void visitSPDescriptorParent(StackProtectorDescriptor &SPD, | 
|  | 755 | MachineBasicBlock *ParentBB); | 
|  | 756 | void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); | 
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 757 | void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); | 
| Evan Cheng | ac730dd | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 758 | void visitBitTestCase(BitTestBlock &BB, | 
|  | 759 | MachineBasicBlock* NextMBB, | 
| Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 760 | uint32_t BranchWeightToNext, | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 761 | unsigned Reg, | 
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 762 | BitTestCase &B, | 
|  | 763 | MachineBasicBlock *SwitchBB); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 764 | void visitJumpTable(JumpTable &JT); | 
| Dan Gohman | 7c0303a | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 765 | void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, | 
|  | 766 | MachineBasicBlock *SwitchBB); | 
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 767 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 768 | private: | 
|  | 769 | // These all get lowered before this pass. | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 770 | void visitInvoke(const InvokeInst &I); | 
| Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 771 | void visitResume(const ResumeInst &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 772 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 773 | void visitBinary(const User &I, unsigned OpCode); | 
|  | 774 | void visitShift(const User &I, unsigned Opcode); | 
|  | 775 | void visitAdd(const User &I)  { visitBinary(I, ISD::ADD); } | 
|  | 776 | void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } | 
|  | 777 | void visitSub(const User &I)  { visitBinary(I, ISD::SUB); } | 
|  | 778 | void visitFSub(const User &I); | 
|  | 779 | void visitMul(const User &I)  { visitBinary(I, ISD::MUL); } | 
|  | 780 | void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } | 
|  | 781 | void visitURem(const User &I) { visitBinary(I, ISD::UREM); } | 
|  | 782 | void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } | 
|  | 783 | void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } | 
|  | 784 | void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } | 
| Benjamin Kramer | 9960a25 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 785 | void visitSDiv(const User &I); | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 786 | void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } | 
|  | 787 | void visitAnd (const User &I) { visitBinary(I, ISD::AND); } | 
|  | 788 | void visitOr  (const User &I) { visitBinary(I, ISD::OR); } | 
|  | 789 | void visitXor (const User &I) { visitBinary(I, ISD::XOR); } | 
|  | 790 | void visitShl (const User &I) { visitShift(I, ISD::SHL); } | 
|  | 791 | void visitLShr(const User &I) { visitShift(I, ISD::SRL); } | 
|  | 792 | void visitAShr(const User &I) { visitShift(I, ISD::SRA); } | 
|  | 793 | void visitICmp(const User &I); | 
|  | 794 | void visitFCmp(const User &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 795 | // Visit the conversion instructions | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 796 | void visitTrunc(const User &I); | 
|  | 797 | void visitZExt(const User &I); | 
|  | 798 | void visitSExt(const User &I); | 
|  | 799 | void visitFPTrunc(const User &I); | 
|  | 800 | void visitFPExt(const User &I); | 
|  | 801 | void visitFPToUI(const User &I); | 
|  | 802 | void visitFPToSI(const User &I); | 
|  | 803 | void visitUIToFP(const User &I); | 
|  | 804 | void visitSIToFP(const User &I); | 
|  | 805 | void visitPtrToInt(const User &I); | 
|  | 806 | void visitIntToPtr(const User &I); | 
|  | 807 | void visitBitCast(const User &I); | 
| Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 808 | void visitAddrSpaceCast(const User &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 809 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 810 | void visitExtractElement(const User &I); | 
|  | 811 | void visitInsertElement(const User &I); | 
|  | 812 | void visitShuffleVector(const User &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 813 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 814 | void visitExtractValue(const ExtractValueInst &I); | 
|  | 815 | void visitInsertValue(const InsertValueInst &I); | 
| Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 816 | void visitLandingPad(const LandingPadInst &I); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 817 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 818 | void visitGetElementPtr(const User &I); | 
|  | 819 | void visitSelect(const User &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 visitAlloca(const AllocaInst &I); | 
|  | 822 | void visitLoad(const LoadInst &I); | 
|  | 823 | void visitStore(const StoreInst &I); | 
| Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 824 | void visitMaskedLoad(const CallInst &I); | 
|  | 825 | void visitMaskedStore(const CallInst &I); | 
| Elena Demikhovsky | 584ce37 | 2015-04-28 07:57:37 +0000 | [diff] [blame] | 826 | void visitMaskedGather(const CallInst &I); | 
|  | 827 | void visitMaskedScatter(const CallInst &I); | 
| Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 828 | void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); | 
|  | 829 | void visitAtomicRMW(const AtomicRMWInst &I); | 
| Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 830 | void visitFence(const FenceInst &I); | 
| Dan Gohman | f41ad47 | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 831 | void visitPHI(const PHINode &I); | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 832 | void visitCall(const CallInst &I); | 
|  | 833 | bool visitMemCmpCall(const CallInst &I); | 
| Richard Sandiford | 6f6d551 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 834 | bool visitMemChrCall(const CallInst &I); | 
| Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 835 | bool visitStrCpyCall(const CallInst &I, bool isStpcpy); | 
| Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 836 | bool visitStrCmpCall(const CallInst &I); | 
| Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 837 | bool visitStrLenCall(const CallInst &I); | 
|  | 838 | bool visitStrNLenCall(const CallInst &I); | 
| Bob Wilson | 874886c | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 839 | bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); | 
| Matt Arsenault | 7c93690 | 2014-10-21 23:01:01 +0000 | [diff] [blame] | 840 | bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode); | 
| Eli Friedman | 342e8df | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 841 | void visitAtomicLoad(const LoadInst &I); | 
|  | 842 | void visitAtomicStore(const StoreInst &I); | 
|  | 843 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 844 | void visitInlineAsm(ImmutableCallSite CS); | 
|  | 845 | const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); | 
|  | 846 | void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 847 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 848 | void visitVAStart(const CallInst &I); | 
|  | 849 | void visitVAArg(const VAArgInst &I); | 
|  | 850 | void visitVAEnd(const CallInst &I); | 
|  | 851 | void visitVACopy(const CallInst &I); | 
| Andrew Trick | 74f4c74 | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 852 | void visitStackmap(const CallInst &I); | 
| Juergen Ributzka | ad2363f | 2014-10-17 17:39:00 +0000 | [diff] [blame] | 853 | void visitPatchpoint(ImmutableCallSite CS, | 
| Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 854 | const BasicBlock *EHPadBB = nullptr); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 855 |  | 
| Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 856 | // These three are implemented in StatepointLowering.cpp | 
|  | 857 | void visitStatepoint(const CallInst &I); | 
|  | 858 | void visitGCRelocate(const CallInst &I); | 
|  | 859 | void visitGCResult(const CallInst &I); | 
|  | 860 |  | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 861 | void visitUserOp1(const Instruction &I) { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 862 | llvm_unreachable("UserOp1 should not exist at instruction selection time!"); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 863 | } | 
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 864 | void visitUserOp2(const Instruction &I) { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 865 | llvm_unreachable("UserOp2 should not exist at instruction selection time!"); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 866 | } | 
| Dan Gohman | 5b43aa0 | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 867 |  | 
| Richard Sandiford | e382775 | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 868 | void processIntegerCallValue(const Instruction &I, | 
|  | 869 | SDValue Value, bool IsSigned); | 
|  | 870 |  | 
| Dan Gohman | 5b43aa0 | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 871 | void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); | 
| Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 872 |  | 
| Devang Patel | 32a72ab | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 873 | /// EmitFuncArgumentDbgValue - If V is an function argument then create | 
| Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 874 | /// corresponding DBG_VALUE machine instruction for it now. At the end of | 
| Devang Patel | 32a72ab | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 875 | /// 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] | 876 | bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable, | 
|  | 877 | DIExpression *Expr, DILocation *DL, | 
| Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 878 | int64_t Offset, bool IsIndirect, | 
|  | 879 | const SDValue &N); | 
| Hans Wennborg | b4db142 | 2015-03-19 20:41:48 +0000 | [diff] [blame] | 880 |  | 
|  | 881 | /// Return the next block after MBB, or nullptr if there is none. | 
|  | 882 | MachineBasicBlock *NextBlock(MachineBasicBlock *MBB); | 
| Krzysztof Parzyszek | a46c36b | 2015-04-13 17:16:45 +0000 | [diff] [blame] | 883 |  | 
|  | 884 | /// Update the DAG and DAG builder with the relevant information after | 
|  | 885 | /// a new root node has been created which could be a tail call. | 
|  | 886 | void updateDAGForMaybeTailCall(SDValue MaybeTC); | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 887 | }; | 
|  | 888 |  | 
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 889 | /// RegsForValue - This struct represents the registers (physical or virtual) | 
|  | 890 | /// that a particular set of values is assigned, and the type information about | 
|  | 891 | /// the value. The most common situation is to represent one value at a time, | 
|  | 892 | /// but struct or array values are handled element-wise as multiple values.  The | 
|  | 893 | /// splitting of aggregates is performed recursively, so that we never have | 
|  | 894 | /// aggregate-typed registers. The values at this point do not necessarily have | 
|  | 895 | /// legal types, so each value may require one or more registers of some legal | 
|  | 896 | /// type. | 
|  | 897 | /// | 
|  | 898 | struct RegsForValue { | 
|  | 899 | /// ValueVTs - The value types of the values, which may not be legal, and | 
|  | 900 | /// may need be promoted or synthesized from one or more registers. | 
|  | 901 | /// | 
|  | 902 | SmallVector<EVT, 4> ValueVTs; | 
|  | 903 |  | 
|  | 904 | /// RegVTs - The value types of the registers. This is the same size as | 
|  | 905 | /// ValueVTs and it records, for each value, what the type of the assigned | 
|  | 906 | /// register or registers are. (Individual values are never synthesized | 
|  | 907 | /// from more than one type of register.) | 
|  | 908 | /// | 
|  | 909 | /// With virtual registers, the contents of RegVTs is redundant with TLI's | 
|  | 910 | /// getRegisterType member function, however when with physical registers | 
|  | 911 | /// it is necessary to have a separate record of the types. | 
|  | 912 | /// | 
|  | 913 | SmallVector<MVT, 4> RegVTs; | 
|  | 914 |  | 
|  | 915 | /// Regs - This list holds the registers assigned to the values. | 
|  | 916 | /// Each legal or promoted value requires one register, and each | 
|  | 917 | /// expanded value requires multiple registers. | 
|  | 918 | /// | 
|  | 919 | SmallVector<unsigned, 4> Regs; | 
|  | 920 |  | 
|  | 921 | RegsForValue(); | 
|  | 922 |  | 
|  | 923 | RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt, EVT valuevt); | 
|  | 924 |  | 
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 925 | RegsForValue(LLVMContext &Context, const TargetLowering &TLI, | 
|  | 926 | const DataLayout &DL, unsigned Reg, Type *Ty); | 
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 927 |  | 
|  | 928 | /// append - Add the specified values to this one. | 
|  | 929 | void append(const RegsForValue &RHS) { | 
|  | 930 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); | 
|  | 931 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); | 
|  | 932 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from | 
|  | 936 | /// this value and returns the result as a ValueVTs value.  This uses | 
|  | 937 | /// Chain/Flag as the input and updates them for the output Chain/Flag. | 
|  | 938 | /// If the Flag pointer is NULL, no flag is used. | 
|  | 939 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, | 
|  | 940 | SDLoc dl, | 
|  | 941 | SDValue &Chain, SDValue *Flag, | 
|  | 942 | const Value *V = nullptr) const; | 
|  | 943 |  | 
| Sanjoy Das | 1194d1e7 | 2015-05-05 23:06:57 +0000 | [diff] [blame] | 944 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the specified | 
|  | 945 | /// value into the registers specified by this object.  This uses Chain/Flag | 
|  | 946 | /// as the input and updates them for the output Chain/Flag.  If the Flag | 
|  | 947 | /// pointer is nullptr, no flag is used.  If V is not nullptr, then it is used | 
|  | 948 | /// in printing better diagnostic messages on error. | 
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 949 | void | 
|  | 950 | getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, SDValue &Chain, | 
| Sanjoy Das | 1194d1e7 | 2015-05-05 23:06:57 +0000 | [diff] [blame] | 951 | SDValue *Flag, const Value *V = nullptr, | 
| Sanjoy Das | 3936a97 | 2015-05-05 23:06:54 +0000 | [diff] [blame] | 952 | ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const; | 
|  | 953 |  | 
|  | 954 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node | 
|  | 955 | /// operand list.  This adds the code marker, matching input operand index | 
|  | 956 | /// (if applicable), and includes the number of values added into it. | 
|  | 957 | void AddInlineAsmOperands(unsigned Kind, | 
|  | 958 | bool HasMatching, unsigned MatchingIdx, SDLoc dl, | 
|  | 959 | SelectionDAG &DAG, | 
|  | 960 | std::vector<SDValue> &Ops) const; | 
|  | 961 | }; | 
|  | 962 |  | 
| Dan Gohman | 575fad3 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 963 | } // end namespace llvm | 
|  | 964 |  | 
|  | 965 | #endif |