Michael Gottesman | b0a50ad | 2013-08-12 21:02:02 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===// |
Dan Gohman | f0cbcd4 | 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 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 14 | #ifndef SELECTIONDAGBUILDER_H |
| 15 | #define SELECTIONDAGBUILDER_H |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 16 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APInt.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 24 | #include <vector> |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 28 | class AddrSpaceCastInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 29 | class AliasAnalysis; |
| 30 | class AllocaInst; |
| 31 | class BasicBlock; |
| 32 | class BitCastInst; |
| 33 | class BranchInst; |
| 34 | class CallInst; |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 35 | class DbgValueInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 36 | class ExtractElementInst; |
| 37 | class ExtractValueInst; |
| 38 | class FCmpInst; |
| 39 | class FPExtInst; |
| 40 | class FPToSIInst; |
| 41 | class FPToUIInst; |
| 42 | class FPTruncInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 43 | class Function; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 44 | class FunctionLoweringInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 45 | class GetElementPtrInst; |
| 46 | class GCFunctionInfo; |
| 47 | class ICmpInst; |
| 48 | class IntToPtrInst; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 49 | class IndirectBrInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 50 | class InvokeInst; |
| 51 | class InsertElementInst; |
| 52 | class InsertValueInst; |
| 53 | class Instruction; |
| 54 | class LoadInst; |
| 55 | class MachineBasicBlock; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 56 | class MachineInstr; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 57 | class MachineRegisterInfo; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 58 | class MDNode; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 59 | class MVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | class PHINode; |
| 61 | class PtrToIntInst; |
| 62 | class ReturnInst; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 63 | class SDDbgValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 64 | class SExtInst; |
| 65 | class SelectInst; |
| 66 | class ShuffleVectorInst; |
| 67 | class SIToFPInst; |
| 68 | class StoreInst; |
| 69 | class SwitchInst; |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 70 | class DataLayout; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 71 | class TargetLibraryInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 72 | class TargetLowering; |
| 73 | class TruncInst; |
| 74 | class UIToFPInst; |
| 75 | class UnreachableInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 76 | class VAArgInst; |
| 77 | class ZExtInst; |
| 78 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 80 | /// SelectionDAGBuilder - This is the common target-independent lowering |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 81 | /// implementation that is parameterized by a TargetLowering object. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 82 | /// |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 83 | class SelectionDAGBuilder { |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 84 | /// CurInst - The current instruction being visited |
| 85 | const Instruction *CurInst; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 86 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 87 | DenseMap<const Value*, SDValue> NodeMap; |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 88 | |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 89 | /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used |
| 90 | /// to preserve debug information for incoming arguments. |
| 91 | DenseMap<const Value*, SDValue> UnusedArgNodeMap; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 92 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 93 | /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap. |
| 94 | class DanglingDebugInfo { |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 95 | const DbgValueInst* DI; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 96 | DebugLoc dl; |
| 97 | unsigned SDNodeOrder; |
| 98 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 99 | DanglingDebugInfo() : DI(nullptr), dl(DebugLoc()), SDNodeOrder(0) { } |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 100 | DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) : |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 101 | DI(di), dl(DL), SDNodeOrder(SDNO) { } |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 102 | const DbgValueInst* getDI() { return DI; } |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 103 | DebugLoc getdl() { return dl; } |
| 104 | unsigned getSDNodeOrder() { return SDNodeOrder; } |
| 105 | }; |
| 106 | |
| 107 | /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not |
| 108 | /// yet seen the referent. We defer handling these until we do see it. |
| 109 | DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap; |
| 110 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 111 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 112 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 113 | /// them up and then emit token factor nodes when possible. This allows us to |
| 114 | /// get simple disambiguation between loads without worrying about alias |
| 115 | /// analysis. |
| 116 | SmallVector<SDValue, 8> PendingLoads; |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 117 | private: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 118 | |
| 119 | /// PendingExports - CopyToReg nodes that copy values to virtual registers |
| 120 | /// for export to other blocks need to be emitted before any terminator |
| 121 | /// instruction, but they have no other ordering requirements. We bunch them |
| 122 | /// up and the emit a single tokenfactor for them just before terminator |
| 123 | /// instructions. |
| 124 | SmallVector<SDValue, 8> PendingExports; |
| 125 | |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 126 | /// SDNodeOrder - A unique monotonically increasing number used to order the |
| 127 | /// SDNodes we create. |
| 128 | unsigned SDNodeOrder; |
| 129 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 130 | /// Case - A struct to record the Value for a switch case, and the |
| 131 | /// case's target basic block. |
| 132 | struct Case { |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 133 | const Constant *Low; |
| 134 | const Constant *High; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 135 | MachineBasicBlock* BB; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 136 | uint32_t ExtraWeight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 137 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 138 | Case() : Low(nullptr), High(nullptr), BB(nullptr), ExtraWeight(0) { } |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 139 | Case(const Constant *low, const Constant *high, MachineBasicBlock *bb, |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 140 | uint32_t extraweight) : Low(low), High(high), BB(bb), |
| 141 | ExtraWeight(extraweight) { } |
| 142 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 143 | APInt size() const { |
| 144 | const APInt &rHigh = cast<ConstantInt>(High)->getValue(); |
| 145 | const APInt &rLow = cast<ConstantInt>(Low)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 146 | return (rHigh - rLow + 1ULL); |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | struct CaseBits { |
| 151 | uint64_t Mask; |
| 152 | MachineBasicBlock* BB; |
| 153 | unsigned Bits; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 154 | uint32_t ExtraWeight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 155 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 156 | CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits, |
| 157 | uint32_t Weight): |
| 158 | Mask(mask), BB(bb), Bits(bits), ExtraWeight(Weight) { } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | typedef std::vector<Case> CaseVector; |
| 162 | typedef std::vector<CaseBits> CaseBitsVector; |
| 163 | typedef CaseVector::iterator CaseItr; |
| 164 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
| 165 | |
| 166 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 167 | /// of conditional branches. |
| 168 | struct CaseRec { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 169 | CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge, |
| 170 | CaseRange r) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 171 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 172 | |
| 173 | /// CaseBB - The MBB in which to emit the compare and branch |
| 174 | MachineBasicBlock *CaseBB; |
| 175 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 176 | /// greater-than-or-equal-to these Constants. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 177 | const Constant *LT; |
| 178 | const Constant *GE; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 179 | /// Range - A pair of iterators representing the range of case values to be |
| 180 | /// processed at this point in the binary search tree. |
| 181 | CaseRange Range; |
| 182 | }; |
| 183 | |
| 184 | typedef std::vector<CaseRec> CaseRecVector; |
| 185 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 186 | /// The comparison function for sorting the switch case values in the vector. |
| 187 | /// WARNING: Case ranges should be disjoint! |
| 188 | struct CaseCmp { |
| 189 | bool operator()(const Case &C1, const Case &C2) { |
| 190 | assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); |
| 191 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 192 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 193 | return CI1->getValue().slt(CI2->getValue()); |
| 194 | } |
| 195 | }; |
| 196 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 197 | struct CaseBitsCmp { |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 198 | bool operator()(const CaseBits &C1, const CaseBits &C2) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 199 | return C1.Bits > C2.Bits; |
| 200 | } |
| 201 | }; |
| 202 | |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 203 | size_t Clusterify(CaseVector &Cases, const SwitchInst &SI); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 204 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 205 | /// CaseBlock - This structure is used to communicate between |
| 206 | /// SelectionDAGBuilder and SDISel for the code generation of additional basic |
| 207 | /// blocks needed by multi-case switch statements. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 208 | struct CaseBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 209 | CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs, |
| 210 | const Value *cmpmiddle, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | MachineBasicBlock *truebb, MachineBasicBlock *falsebb, |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 212 | MachineBasicBlock *me, |
| 213 | uint32_t trueweight = 0, uint32_t falseweight = 0) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 214 | : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 215 | TrueBB(truebb), FalseBB(falsebb), ThisBB(me), |
| 216 | TrueWeight(trueweight), FalseWeight(falseweight) { } |
| 217 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 218 | // CC - the condition code to use for the case block's setcc node |
| 219 | ISD::CondCode CC; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 220 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 221 | // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. |
| 222 | // Emit by default LHS op RHS. MHS is used for range comparisons: |
| 223 | // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 224 | const Value *CmpLHS, *CmpMHS, *CmpRHS; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 226 | // TrueBB/FalseBB - the block to branch to if the setcc is true/false. |
| 227 | MachineBasicBlock *TrueBB, *FalseBB; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 228 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 229 | // ThisBB - the block into which to emit the code for the setcc and branches |
| 230 | MachineBasicBlock *ThisBB; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 231 | |
| 232 | // TrueWeight/FalseWeight - branch weights. |
| 233 | uint32_t TrueWeight, FalseWeight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 234 | }; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 235 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 236 | struct JumpTable { |
| 237 | JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, |
| 238 | MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 239 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 240 | /// Reg - the virtual register containing the index of the jump table entry |
| 241 | //. to jump to. |
| 242 | unsigned Reg; |
| 243 | /// JTI - the JumpTableIndex for this jump table in the function. |
| 244 | unsigned JTI; |
| 245 | /// MBB - the MBB into which to emit the code for the indirect jump. |
| 246 | MachineBasicBlock *MBB; |
| 247 | /// Default - the MBB of the default bb, which is a successor of the range |
| 248 | /// check MBB. This is when updating PHI nodes in successors. |
| 249 | MachineBasicBlock *Default; |
| 250 | }; |
| 251 | struct JumpTableHeader { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 252 | JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 253 | bool E = false): |
| 254 | First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 255 | APInt First; |
| 256 | APInt Last; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 257 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 258 | MachineBasicBlock *HeaderBB; |
| 259 | bool Emitted; |
| 260 | }; |
| 261 | typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; |
| 262 | |
| 263 | struct BitTestCase { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 264 | BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr, |
| 265 | uint32_t Weight): |
| 266 | Mask(M), ThisBB(T), TargetBB(Tr), ExtraWeight(Weight) { } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 267 | uint64_t Mask; |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 268 | MachineBasicBlock *ThisBB; |
| 269 | MachineBasicBlock *TargetBB; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 270 | uint32_t ExtraWeight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | typedef SmallVector<BitTestCase, 3> BitTestInfo; |
| 274 | |
| 275 | struct BitTestBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 276 | BitTestBlock(APInt F, APInt R, const Value* SV, |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 277 | unsigned Rg, MVT RgVT, bool E, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 278 | MachineBasicBlock* P, MachineBasicBlock* D, |
| 279 | const BitTestInfo& C): |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 280 | First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 281 | Parent(P), Default(D), Cases(C) { } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 282 | APInt First; |
| 283 | APInt Range; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 284 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 285 | unsigned Reg; |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 286 | MVT RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 287 | bool Emitted; |
| 288 | MachineBasicBlock *Parent; |
| 289 | MachineBasicBlock *Default; |
| 290 | BitTestInfo Cases; |
| 291 | }; |
| 292 | |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 293 | /// A class which encapsulates all of the information needed to generate a |
| 294 | /// stack protector check and signals to isel via its state being initialized |
| 295 | /// that a stack protector needs to be generated. |
| 296 | /// |
| 297 | /// *NOTE* The following is a high level documentation of SelectionDAG Stack |
| 298 | /// Protector Generation. The reason that it is placed here is for a lack of |
| 299 | /// other good places to stick it. |
| 300 | /// |
| 301 | /// High Level Overview of SelectionDAG Stack Protector Generation: |
| 302 | /// |
| 303 | /// Previously, generation of stack protectors was done exclusively in the |
| 304 | /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated |
| 305 | /// splitting basic blocks at the IR level to create the success/failure basic |
| 306 | /// blocks in the tail of the basic block in question. As a result of this, |
| 307 | /// calls that would have qualified for the sibling call optimization were no |
| 308 | /// longer eligible for optimization since said calls were no longer right in |
| 309 | /// the "tail position" (i.e. the immediate predecessor of a ReturnInst |
| 310 | /// instruction). |
| 311 | /// |
| 312 | /// Then it was noticed that since the sibling call optimization causes the |
| 313 | /// callee to reuse the caller's stack, if we could delay the generation of |
| 314 | /// the stack protector check until later in CodeGen after the sibling call |
| 315 | /// decision was made, we get both the tail call optimization and the stack |
| 316 | /// protector check! |
| 317 | /// |
| 318 | /// A few goals in solving this problem were: |
| 319 | /// |
| 320 | /// 1. Preserve the architecture independence of stack protector generation. |
| 321 | /// |
| 322 | /// 2. Preserve the normal IR level stack protector check for platforms like |
| 323 | /// OpenBSD for which we support platform specific stack protector |
| 324 | /// generation. |
| 325 | /// |
| 326 | /// The main problem that guided the present solution is that one can not |
| 327 | /// solve this problem in an architecture independent manner at the IR level |
| 328 | /// only. This is because: |
| 329 | /// |
| 330 | /// 1. The decision on whether or not to perform a sibling call on certain |
| 331 | /// platforms (for instance i386) requires lower level information |
| 332 | /// related to available registers that can not be known at the IR level. |
| 333 | /// |
| 334 | /// 2. Even if the previous point were not true, the decision on whether to |
| 335 | /// perform a tail call is done in LowerCallTo in SelectionDAG which |
| 336 | /// occurs after the Stack Protector Pass. As a result, one would need to |
| 337 | /// put the relevant callinst into the stack protector check success |
| 338 | /// basic block (where the return inst is placed) and then move it back |
| 339 | /// later at SelectionDAG/MI time before the stack protector check if the |
| 340 | /// tail call optimization failed. The MI level option was nixed |
| 341 | /// immediately since it would require platform specific pattern |
| 342 | /// matching. The SelectionDAG level option was nixed because |
| 343 | /// SelectionDAG only processes one IR level basic block at a time |
| 344 | /// implying one could not create a DAG Combine to move the callinst. |
| 345 | /// |
| 346 | /// To get around this problem a few things were realized: |
| 347 | /// |
| 348 | /// 1. While one can not handle multiple IR level basic blocks at the |
| 349 | /// SelectionDAG Level, one can generate multiple machine basic blocks |
| 350 | /// for one IR level basic block. This is how we handle bit tests and |
| 351 | /// switches. |
| 352 | /// |
| 353 | /// 2. At the MI level, tail calls are represented via a special return |
| 354 | /// MIInst called "tcreturn". Thus if we know the basic block in which we |
| 355 | /// wish to insert the stack protector check, we get the correct behavior |
| 356 | /// by always inserting the stack protector check right before the return |
| 357 | /// statement. This is a "magical transformation" since no matter where |
| 358 | /// the stack protector check intrinsic is, we always insert the stack |
| 359 | /// protector check code at the end of the BB. |
| 360 | /// |
| 361 | /// Given the aforementioned constraints, the following solution was devised: |
| 362 | /// |
| 363 | /// 1. On platforms that do not support SelectionDAG stack protector check |
| 364 | /// generation, allow for the normal IR level stack protector check |
| 365 | /// generation to continue. |
| 366 | /// |
| 367 | /// 2. On platforms that do support SelectionDAG stack protector check |
| 368 | /// generation: |
| 369 | /// |
| 370 | /// a. Use the IR level stack protector pass to decide if a stack |
| 371 | /// protector is required/which BB we insert the stack protector check |
| 372 | /// in by reusing the logic already therein. If we wish to generate a |
| 373 | /// stack protector check in a basic block, we place a special IR |
| 374 | /// intrinsic called llvm.stackprotectorcheck right before the BB's |
| 375 | /// returninst or if there is a callinst that could potentially be |
| 376 | /// sibling call optimized, before the call inst. |
| 377 | /// |
| 378 | /// b. Then when a BB with said intrinsic is processed, we codegen the BB |
| 379 | /// normally via SelectBasicBlock. In said process, when we visit the |
| 380 | /// stack protector check, we do not actually emit anything into the |
| 381 | /// BB. Instead, we just initialize the stack protector descriptor |
| 382 | /// class (which involves stashing information/creating the success |
| 383 | /// mbbb and the failure mbb if we have not created one for this |
| 384 | /// function yet) and export the guard variable that we are going to |
| 385 | /// compare. |
| 386 | /// |
| 387 | /// c. After we finish selecting the basic block, in FinishBasicBlock if |
| 388 | /// the StackProtectorDescriptor attached to the SelectionDAGBuilder is |
| 389 | /// initialized, we first find a splice point in the parent basic block |
| 390 | /// before the terminator and then splice the terminator of said basic |
| 391 | /// block into the success basic block. Then we code-gen a new tail for |
| 392 | /// the parent basic block consisting of the two loads, the comparison, |
| 393 | /// and finally two branches to the success/failure basic blocks. We |
| 394 | /// conclude by code-gening the failure basic block if we have not |
| 395 | /// code-gened it already (all stack protector checks we generate in |
| 396 | /// the same function, use the same failure basic block). |
| 397 | class StackProtectorDescriptor { |
| 398 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 399 | StackProtectorDescriptor() : ParentMBB(nullptr), SuccessMBB(nullptr), |
| 400 | FailureMBB(nullptr), Guard(nullptr) { } |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 401 | ~StackProtectorDescriptor() { } |
| 402 | |
| 403 | /// Returns true if all fields of the stack protector descriptor are |
| 404 | /// initialized implying that we should/are ready to emit a stack protector. |
| 405 | bool shouldEmitStackProtector() const { |
| 406 | return ParentMBB && SuccessMBB && FailureMBB && Guard; |
| 407 | } |
| 408 | |
| 409 | /// Initialize the stack protector descriptor structure for a new basic |
| 410 | /// block. |
| 411 | void initialize(const BasicBlock *BB, |
| 412 | MachineBasicBlock *MBB, |
| 413 | const CallInst &StackProtCheckCall) { |
| 414 | // Make sure we are not initialized yet. |
| 415 | assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " |
| 416 | "already initialized!"); |
| 417 | ParentMBB = MBB; |
| 418 | SuccessMBB = AddSuccessorMBB(BB, MBB); |
| 419 | FailureMBB = AddSuccessorMBB(BB, MBB, FailureMBB); |
| 420 | if (!Guard) |
| 421 | Guard = StackProtCheckCall.getArgOperand(0); |
| 422 | } |
| 423 | |
| 424 | /// Reset state that changes when we handle different basic blocks. |
| 425 | /// |
| 426 | /// This currently includes: |
| 427 | /// |
| 428 | /// 1. The specific basic block we are generating a |
| 429 | /// stack protector for (ParentMBB). |
| 430 | /// |
| 431 | /// 2. The successor machine basic block that will contain the tail of |
| 432 | /// parent mbb after we create the stack protector check (SuccessMBB). This |
| 433 | /// BB is visited only on stack protector check success. |
| 434 | void resetPerBBState() { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 435 | ParentMBB = nullptr; |
| 436 | SuccessMBB = nullptr; |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /// Reset state that only changes when we switch functions. |
| 440 | /// |
| 441 | /// This currently includes: |
| 442 | /// |
| 443 | /// 1. FailureMBB since we reuse the failure code path for all stack |
| 444 | /// protector checks created in an individual function. |
| 445 | /// |
| 446 | /// 2.The guard variable since the guard variable we are checking against is |
| 447 | /// always the same. |
| 448 | void resetPerFunctionState() { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 449 | FailureMBB = nullptr; |
| 450 | Guard = nullptr; |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | MachineBasicBlock *getParentMBB() { return ParentMBB; } |
| 454 | MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } |
| 455 | MachineBasicBlock *getFailureMBB() { return FailureMBB; } |
| 456 | const Value *getGuard() { return Guard; } |
| 457 | |
| 458 | private: |
| 459 | /// The basic block for which we are generating the stack protector. |
| 460 | /// |
| 461 | /// As a result of stack protector generation, we will splice the |
| 462 | /// terminators of this basic block into the successor mbb SuccessMBB and |
| 463 | /// replace it with a compare/branch to the successor mbbs |
| 464 | /// SuccessMBB/FailureMBB depending on whether or not the stack protector |
| 465 | /// was violated. |
| 466 | MachineBasicBlock *ParentMBB; |
| 467 | |
| 468 | /// A basic block visited on stack protector check success that contains the |
| 469 | /// terminators of ParentMBB. |
| 470 | MachineBasicBlock *SuccessMBB; |
| 471 | |
| 472 | /// This basic block visited on stack protector check failure that will |
| 473 | /// contain a call to __stack_chk_fail(). |
| 474 | MachineBasicBlock *FailureMBB; |
| 475 | |
| 476 | /// The guard variable which we will compare against the stored value in the |
| 477 | /// stack protector stack slot. |
| 478 | const Value *Guard; |
| 479 | |
| 480 | /// Add a successor machine basic block to ParentMBB. If the successor mbb |
| 481 | /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic |
| 482 | /// block will be created. |
| 483 | MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, |
| 484 | MachineBasicBlock *ParentMBB, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 485 | MachineBasicBlock *SuccMBB = nullptr); |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 488 | private: |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 489 | const TargetMachine &TM; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 490 | public: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 491 | /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling |
| 492 | /// nodes without a corresponding SDNode. |
| 493 | static const unsigned LowestSDNodeOrder = 1; |
| 494 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 495 | SelectionDAG &DAG; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 496 | const DataLayout *DL; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 497 | AliasAnalysis *AA; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 498 | const TargetLibraryInfo *LibInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 499 | |
| 500 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 501 | /// SwitchInst code generation information. |
| 502 | std::vector<CaseBlock> SwitchCases; |
| 503 | /// JTCases - Vector of JumpTable structures used to communicate |
| 504 | /// SwitchInst code generation information. |
| 505 | std::vector<JumpTableBlock> JTCases; |
| 506 | /// BitTestCases - Vector of BitTestBlock structures used to communicate |
| 507 | /// SwitchInst code generation information. |
| 508 | std::vector<BitTestBlock> BitTestCases; |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 509 | /// A StackProtectorDescriptor structure used to communicate stack protector |
| 510 | /// information in between SelectBasicBlock and FinishBasicBlock. |
| 511 | StackProtectorDescriptor SPDescriptor; |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 512 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 513 | // Emit PHI-node-operand constants only once even if used by multiple |
| 514 | // PHI nodes. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 515 | DenseMap<const Constant *, unsigned> ConstantsOut; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 516 | |
| 517 | /// FuncInfo - Information about the function as a whole. |
| 518 | /// |
| 519 | FunctionLoweringInfo &FuncInfo; |
Bill Wendling | dfdacee | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 520 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 521 | /// OptLevel - What optimization level we're generating code for. |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 522 | /// |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 523 | CodeGenOpt::Level OptLevel; |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 524 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 525 | /// GFI - Garbage collection metadata for the function. |
| 526 | GCFunctionInfo *GFI; |
| 527 | |
Bill Wendling | 30e6740 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 528 | /// LPadToCallSiteMap - Map a landing pad to the call site indexes. |
| 529 | DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap; |
Bill Wendling | a8512ed | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 530 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 531 | /// HasTailCall - This is set to true if a call in the current |
| 532 | /// block has been translated as a tail call. In this case, |
| 533 | /// no subsequent DAG nodes should be created. |
| 534 | /// |
| 535 | bool HasTailCall; |
| 536 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 537 | LLVMContext *Context; |
| 538 | |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 539 | SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 540 | CodeGenOpt::Level ol) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 541 | : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 542 | DAG(dag), FuncInfo(funcinfo), OptLevel(ol), |
Richard Smith | cb1f68d | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 543 | HasTailCall(false) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 546 | void init(GCFunctionInfo *gfi, AliasAnalysis &aa, |
| 547 | const TargetLibraryInfo *li); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 548 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 549 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 550 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 551 | /// for a new block. This doesn't clear out information about |
| 552 | /// additional blocks that are needed to complete switch lowering |
| 553 | /// or PHI node updating; that information is cleared out as it is |
| 554 | /// consumed. |
| 555 | void clear(); |
| 556 | |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 557 | /// clearDanglingDebugInfo - Clear the dangling debug information |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 558 | /// map. This function is separated from the clear so that debug |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 559 | /// information that is dangling in a basic block can be properly |
| 560 | /// resolved in a different basic block. This allows the |
| 561 | /// SelectionDAG to resolve dangling debug information attached |
| 562 | /// to PHI nodes. |
| 563 | void clearDanglingDebugInfo(); |
| 564 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 565 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 566 | /// flushing any PendingLoad items. This must be done before emitting |
| 567 | /// a store or any other node that may need to be ordered after any |
| 568 | /// prior load instructions. |
| 569 | /// |
| 570 | SDValue getRoot(); |
| 571 | |
| 572 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 573 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 574 | /// to do this before emitting a terminator instruction. |
| 575 | /// |
| 576 | SDValue getControlRoot(); |
| 577 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 578 | SDLoc getCurSDLoc() const { |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 579 | return SDLoc(CurInst, SDNodeOrder); |
| 580 | } |
| 581 | |
| 582 | DebugLoc getCurDebugLoc() const { |
| 583 | return CurInst ? CurInst->getDebugLoc() : DebugLoc(); |
| 584 | } |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 585 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 586 | unsigned getSDNodeOrder() const { return SDNodeOrder; } |
| 587 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 588 | void CopyValueToVirtualRegister(const Value *V, unsigned Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 589 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 590 | void visit(const Instruction &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 591 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 592 | void visit(unsigned Opcode, const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 593 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 594 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 595 | // generate the debug data structures now that we've seen its definition. |
| 596 | void resolveDanglingDebugInfo(const Value *V, SDValue Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 597 | SDValue getValue(const Value *V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 598 | SDValue getNonRegisterValue(const Value *V); |
| 599 | SDValue getValueImpl(const Value *V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 600 | |
| 601 | void setValue(const Value *V, SDValue NewN) { |
| 602 | SDValue &N = NodeMap[V]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 603 | assert(!N.getNode() && "Already set a value for this node!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 604 | N = NewN; |
| 605 | } |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 606 | |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 607 | void setUnusedArgValue(const Value *V, SDValue NewN) { |
| 608 | SDValue &N = UnusedArgNodeMap[V]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 609 | assert(!N.getNode() && "Already set a value for this node!"); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 610 | N = NewN; |
| 611 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 612 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 613 | void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 614 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 615 | MachineBasicBlock *SwitchBB, unsigned Opc, |
| 616 | uint32_t TW, uint32_t FW); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 617 | void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 618 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 619 | MachineBasicBlock *CurBB, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 620 | MachineBasicBlock *SwitchBB, |
| 621 | uint32_t TW, uint32_t FW); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 622 | bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 623 | bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); |
| 624 | void CopyToExportRegsIfNeeded(const Value *V); |
| 625 | void ExportFromCurrentBlock(const Value *V); |
| 626 | void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 627 | MachineBasicBlock *LandingPad = nullptr); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 628 | |
Andrew Trick | 2343e3b | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 629 | std::pair<SDValue, SDValue> LowerCallOperands(const CallInst &CI, |
| 630 | unsigned ArgIdx, |
| 631 | unsigned NumArgs, |
Juergen Ributzka | 623d2e6 | 2013-11-08 23:28:16 +0000 | [diff] [blame] | 632 | SDValue Callee, |
| 633 | bool useVoidTy = false); |
Andrew Trick | 2343e3b | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 634 | |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 635 | /// UpdateSplitBlock - When an MBB was split during scheduling, update the |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 636 | /// references that need to refer to the last resulting block. |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 637 | void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); |
| 638 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 639 | private: |
| 640 | // Terminator instructions. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 641 | void visitRet(const ReturnInst &I); |
| 642 | void visitBr(const BranchInst &I); |
| 643 | void visitSwitch(const SwitchInst &I); |
| 644 | void visitIndirectBr(const IndirectBrInst &I); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 645 | void visitUnreachable(const UnreachableInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 646 | |
| 647 | // Helpers for visitSwitch |
| 648 | bool handleSmallSwitchRange(CaseRec& CR, |
| 649 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 650 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 651 | MachineBasicBlock* Default, |
| 652 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 653 | bool handleJTSwitchCase(CaseRec& CR, |
| 654 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 655 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 656 | MachineBasicBlock* Default, |
| 657 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 658 | bool handleBTSplitSwitchCase(CaseRec& CR, |
| 659 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 660 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 661 | MachineBasicBlock* Default, |
| 662 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 663 | bool handleBitTestsSwitchCase(CaseRec& CR, |
| 664 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 665 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 666 | MachineBasicBlock* Default, |
| 667 | MachineBasicBlock *SwitchBB); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 668 | |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 669 | uint32_t getEdgeWeight(const MachineBasicBlock *Src, |
| 670 | const MachineBasicBlock *Dst) const; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 671 | void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 672 | uint32_t Weight = 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 673 | public: |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 674 | void visitSwitchCase(CaseBlock &CB, |
| 675 | MachineBasicBlock *SwitchBB); |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 676 | void visitSPDescriptorParent(StackProtectorDescriptor &SPD, |
| 677 | MachineBasicBlock *ParentBB); |
| 678 | void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 679 | void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 680 | void visitBitTestCase(BitTestBlock &BB, |
| 681 | MachineBasicBlock* NextMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 682 | uint32_t BranchWeightToNext, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 683 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 684 | BitTestCase &B, |
| 685 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 686 | void visitJumpTable(JumpTable &JT); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 687 | void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, |
| 688 | MachineBasicBlock *SwitchBB); |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 689 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 690 | private: |
| 691 | // These all get lowered before this pass. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 692 | void visitInvoke(const InvokeInst &I); |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 693 | void visitResume(const ResumeInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 694 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 695 | void visitBinary(const User &I, unsigned OpCode); |
| 696 | void visitShift(const User &I, unsigned Opcode); |
| 697 | void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } |
| 698 | void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } |
| 699 | void visitSub(const User &I) { visitBinary(I, ISD::SUB); } |
| 700 | void visitFSub(const User &I); |
| 701 | void visitMul(const User &I) { visitBinary(I, ISD::MUL); } |
| 702 | void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } |
| 703 | void visitURem(const User &I) { visitBinary(I, ISD::UREM); } |
| 704 | void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } |
| 705 | void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } |
| 706 | void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 707 | void visitSDiv(const User &I); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 708 | void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } |
| 709 | void visitAnd (const User &I) { visitBinary(I, ISD::AND); } |
| 710 | void visitOr (const User &I) { visitBinary(I, ISD::OR); } |
| 711 | void visitXor (const User &I) { visitBinary(I, ISD::XOR); } |
| 712 | void visitShl (const User &I) { visitShift(I, ISD::SHL); } |
| 713 | void visitLShr(const User &I) { visitShift(I, ISD::SRL); } |
| 714 | void visitAShr(const User &I) { visitShift(I, ISD::SRA); } |
| 715 | void visitICmp(const User &I); |
| 716 | void visitFCmp(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 717 | // Visit the conversion instructions |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 718 | void visitTrunc(const User &I); |
| 719 | void visitZExt(const User &I); |
| 720 | void visitSExt(const User &I); |
| 721 | void visitFPTrunc(const User &I); |
| 722 | void visitFPExt(const User &I); |
| 723 | void visitFPToUI(const User &I); |
| 724 | void visitFPToSI(const User &I); |
| 725 | void visitUIToFP(const User &I); |
| 726 | void visitSIToFP(const User &I); |
| 727 | void visitPtrToInt(const User &I); |
| 728 | void visitIntToPtr(const User &I); |
| 729 | void visitBitCast(const User &I); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 730 | void visitAddrSpaceCast(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 731 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 732 | void visitExtractElement(const User &I); |
| 733 | void visitInsertElement(const User &I); |
| 734 | void visitShuffleVector(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 735 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 736 | void visitExtractValue(const ExtractValueInst &I); |
| 737 | void visitInsertValue(const InsertValueInst &I); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 738 | void visitLandingPad(const LandingPadInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 739 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 740 | void visitGetElementPtr(const User &I); |
| 741 | void visitSelect(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 742 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 743 | void visitAlloca(const AllocaInst &I); |
| 744 | void visitLoad(const LoadInst &I); |
| 745 | void visitStore(const StoreInst &I); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 746 | void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); |
| 747 | void visitAtomicRMW(const AtomicRMWInst &I); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 748 | void visitFence(const FenceInst &I); |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 749 | void visitPHI(const PHINode &I); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 750 | void visitCall(const CallInst &I); |
| 751 | bool visitMemCmpCall(const CallInst &I); |
Richard Sandiford | 8c20158 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 752 | bool visitMemChrCall(const CallInst &I); |
Richard Sandiford | 4fc7355 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 753 | bool visitStrCpyCall(const CallInst &I, bool isStpcpy); |
Richard Sandiford | e1b2af7 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 754 | bool visitStrCmpCall(const CallInst &I); |
Richard Sandiford | 19262ee | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 755 | bool visitStrLenCall(const CallInst &I); |
| 756 | bool visitStrNLenCall(const CallInst &I); |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 757 | bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 758 | void visitAtomicLoad(const LoadInst &I); |
| 759 | void visitAtomicStore(const StoreInst &I); |
| 760 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 761 | void visitInlineAsm(ImmutableCallSite CS); |
| 762 | const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); |
| 763 | void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 764 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 765 | void visitVAStart(const CallInst &I); |
| 766 | void visitVAArg(const VAArgInst &I); |
| 767 | void visitVAEnd(const CallInst &I); |
| 768 | void visitVACopy(const CallInst &I); |
Andrew Trick | 2343e3b | 2013-10-31 17:18:24 +0000 | [diff] [blame] | 769 | void visitStackmap(const CallInst &I); |
| 770 | void visitPatchpoint(const CallInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 771 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 772 | void visitUserOp1(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 773 | llvm_unreachable("UserOp1 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 774 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 775 | void visitUserOp2(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 776 | llvm_unreachable("UserOp2 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 777 | } |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 778 | |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 779 | void processIntegerCallValue(const Instruction &I, |
| 780 | SDValue Value, bool IsSigned); |
| 781 | |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 782 | void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 783 | |
Devang Patel | ab43add | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 784 | /// EmitFuncArgumentDbgValue - If V is an function argument then create |
Andrew Trick | cf940ce | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 785 | /// corresponding DBG_VALUE machine instruction for it now. At the end of |
Devang Patel | ab43add | 2010-08-25 20:41:24 +0000 | [diff] [blame] | 786 | /// instruction selection, they will be inserted to the entry BB. |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 787 | bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 788 | int64_t Offset, bool IsIndirect, |
| 789 | const SDValue &N); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 790 | }; |
| 791 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 792 | } // end namespace llvm |
| 793 | |
| 794 | #endif |