Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.h - Selection-DAG building --------------------===// |
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 | |
| 17 | #include "llvm/Constants.h" |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APInt.h" |
| 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #ifndef NDEBUG |
| 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #endif |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Bill Wendling | 0eb96fd | 2009-02-03 01:32:22 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ValueTypes.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CallSite.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include <set> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | class AliasAnalysis; |
| 34 | class AllocaInst; |
| 35 | class BasicBlock; |
| 36 | class BitCastInst; |
| 37 | class BranchInst; |
| 38 | class CallInst; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 39 | class DbgValueInst; |
Dan Gohman | f0cbcd4 | 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 | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 47 | class Function; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 48 | class FunctionLoweringInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 49 | class GetElementPtrInst; |
| 50 | class GCFunctionInfo; |
| 51 | class ICmpInst; |
| 52 | class IntToPtrInst; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 53 | class IndirectBrInst; |
Dan Gohman | f0cbcd4 | 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 | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | class MachineInstr; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 61 | class MachineRegisterInfo; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 62 | class MDNode; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 63 | class PHINode; |
| 64 | class PtrToIntInst; |
| 65 | class ReturnInst; |
| 66 | class SDISelAsmOperandInfo; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame^] | 67 | class SDDbgValue; |
Dan Gohman | f0cbcd4 | 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; |
| 74 | class TargetData; |
| 75 | class TargetLowering; |
| 76 | class TruncInst; |
| 77 | class UIToFPInst; |
| 78 | class UnreachableInst; |
| 79 | class UnwindInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 80 | class VAArgInst; |
| 81 | class ZExtInst; |
| 82 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 84 | /// SelectionDAGBuilder - This is the common target-independent lowering |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 85 | /// implementation that is parameterized by a TargetLowering object. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 86 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 87 | class SelectionDAGBuilder { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 88 | /// CurDebugLoc - current file + line number. Changes as we build the DAG. |
| 89 | DebugLoc CurDebugLoc; |
| 90 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 91 | DenseMap<const Value*, SDValue> NodeMap; |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 92 | |
| 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 | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 96 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame^] | 97 | /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap. |
| 98 | class DanglingDebugInfo { |
| 99 | const DbgValueInst* DI; |
| 100 | DebugLoc dl; |
| 101 | unsigned SDNodeOrder; |
| 102 | public: |
| 103 | DanglingDebugInfo() : DI(0), dl(DebugLoc()), SDNodeOrder(0) { } |
| 104 | DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) : |
| 105 | DI(di), dl(DL), SDNodeOrder(SDNO) { } |
| 106 | const DbgValueInst* getDI() { return DI; } |
| 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 | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 115 | public: |
Dan Gohman | f0cbcd4 | 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; |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 121 | private: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 122 | |
| 123 | /// PendingExports - CopyToReg nodes that copy values to virtual registers |
| 124 | /// for export to other blocks need to be emitted before any terminator |
| 125 | /// instruction, but they have no other ordering requirements. We bunch them |
| 126 | /// up and the emit a single tokenfactor for them just before terminator |
| 127 | /// instructions. |
| 128 | SmallVector<SDValue, 8> PendingExports; |
| 129 | |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 130 | /// SDNodeOrder - A unique monotonically increasing number used to order the |
| 131 | /// SDNodes we create. |
| 132 | unsigned SDNodeOrder; |
| 133 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 134 | /// Case - A struct to record the Value for a switch case, and the |
| 135 | /// case's target basic block. |
| 136 | struct Case { |
| 137 | Constant* Low; |
| 138 | Constant* High; |
| 139 | MachineBasicBlock* BB; |
| 140 | |
| 141 | Case() : Low(0), High(0), BB(0) { } |
| 142 | Case(Constant* low, Constant* high, MachineBasicBlock* bb) : |
| 143 | Low(low), High(high), BB(bb) { } |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 144 | APInt size() const { |
| 145 | const APInt &rHigh = cast<ConstantInt>(High)->getValue(); |
| 146 | const APInt &rLow = cast<ConstantInt>(Low)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 147 | return (rHigh - rLow + 1ULL); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | struct CaseBits { |
| 152 | uint64_t Mask; |
| 153 | MachineBasicBlock* BB; |
| 154 | unsigned Bits; |
| 155 | |
| 156 | CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits): |
| 157 | Mask(mask), BB(bb), Bits(bits) { } |
| 158 | }; |
| 159 | |
| 160 | typedef std::vector<Case> CaseVector; |
| 161 | typedef std::vector<CaseBits> CaseBitsVector; |
| 162 | typedef CaseVector::iterator CaseItr; |
| 163 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
| 164 | |
| 165 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 166 | /// of conditional branches. |
| 167 | struct CaseRec { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 168 | CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge, |
| 169 | CaseRange r) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 170 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 171 | |
| 172 | /// CaseBB - The MBB in which to emit the compare and branch |
| 173 | MachineBasicBlock *CaseBB; |
| 174 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 175 | /// greater-than-or-equal-to these Constants. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 176 | const Constant *LT; |
| 177 | const Constant *GE; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | /// Range - A pair of iterators representing the range of case values to be |
| 179 | /// processed at this point in the binary search tree. |
| 180 | CaseRange Range; |
| 181 | }; |
| 182 | |
| 183 | typedef std::vector<CaseRec> CaseRecVector; |
| 184 | |
| 185 | /// The comparison function for sorting the switch case values in the vector. |
| 186 | /// WARNING: Case ranges should be disjoint! |
| 187 | struct CaseCmp { |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 188 | bool operator()(const Case &C1, const Case &C2) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 189 | assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); |
| 190 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 191 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 192 | return CI1->getValue().slt(CI2->getValue()); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | struct CaseBitsCmp { |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 197 | bool operator()(const CaseBits &C1, const CaseBits &C2) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | return C1.Bits > C2.Bits; |
| 199 | } |
| 200 | }; |
| 201 | |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 202 | size_t Clusterify(CaseVector &Cases, const SwitchInst &SI); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 203 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 204 | /// CaseBlock - This structure is used to communicate between |
| 205 | /// SelectionDAGBuilder and SDISel for the code generation of additional basic |
| 206 | /// blocks needed by multi-case switch statements. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 207 | struct CaseBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 208 | CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs, |
| 209 | const Value *cmpmiddle, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 210 | MachineBasicBlock *truebb, MachineBasicBlock *falsebb, |
| 211 | MachineBasicBlock *me) |
| 212 | : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), |
| 213 | TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {} |
| 214 | // CC - the condition code to use for the case block's setcc node |
| 215 | ISD::CondCode CC; |
| 216 | // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. |
| 217 | // Emit by default LHS op RHS. MHS is used for range comparisons: |
| 218 | // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 219 | const Value *CmpLHS, *CmpMHS, *CmpRHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 220 | // TrueBB/FalseBB - the block to branch to if the setcc is true/false. |
| 221 | MachineBasicBlock *TrueBB, *FalseBB; |
| 222 | // ThisBB - the block into which to emit the code for the setcc and branches |
| 223 | MachineBasicBlock *ThisBB; |
| 224 | }; |
| 225 | struct JumpTable { |
| 226 | JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, |
| 227 | MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} |
| 228 | |
| 229 | /// Reg - the virtual register containing the index of the jump table entry |
| 230 | //. to jump to. |
| 231 | unsigned Reg; |
| 232 | /// JTI - the JumpTableIndex for this jump table in the function. |
| 233 | unsigned JTI; |
| 234 | /// MBB - the MBB into which to emit the code for the indirect jump. |
| 235 | MachineBasicBlock *MBB; |
| 236 | /// Default - the MBB of the default bb, which is a successor of the range |
| 237 | /// check MBB. This is when updating PHI nodes in successors. |
| 238 | MachineBasicBlock *Default; |
| 239 | }; |
| 240 | struct JumpTableHeader { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 241 | JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 242 | bool E = false): |
| 243 | First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 244 | APInt First; |
| 245 | APInt Last; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 246 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 247 | MachineBasicBlock *HeaderBB; |
| 248 | bool Emitted; |
| 249 | }; |
| 250 | typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; |
| 251 | |
| 252 | struct BitTestCase { |
| 253 | BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr): |
| 254 | Mask(M), ThisBB(T), TargetBB(Tr) { } |
| 255 | uint64_t Mask; |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 256 | MachineBasicBlock *ThisBB; |
| 257 | MachineBasicBlock *TargetBB; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | typedef SmallVector<BitTestCase, 3> BitTestInfo; |
| 261 | |
| 262 | struct BitTestBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 263 | BitTestBlock(APInt F, APInt R, const Value* SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 264 | unsigned Rg, bool E, |
| 265 | MachineBasicBlock* P, MachineBasicBlock* D, |
| 266 | const BitTestInfo& C): |
| 267 | First(F), Range(R), SValue(SV), Reg(Rg), Emitted(E), |
| 268 | Parent(P), Default(D), Cases(C) { } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 269 | APInt First; |
| 270 | APInt Range; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 271 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 272 | unsigned Reg; |
| 273 | bool Emitted; |
| 274 | MachineBasicBlock *Parent; |
| 275 | MachineBasicBlock *Default; |
| 276 | BitTestInfo Cases; |
| 277 | }; |
| 278 | |
| 279 | public: |
| 280 | // TLI - This is information that describes the available target features we |
| 281 | // need for lowering. This indicates when operations are unavailable, |
| 282 | // implemented with a libcall, etc. |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 283 | const TargetMachine &TM; |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 284 | const TargetLowering &TLI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 285 | SelectionDAG &DAG; |
| 286 | const TargetData *TD; |
| 287 | AliasAnalysis *AA; |
| 288 | |
| 289 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 290 | /// SwitchInst code generation information. |
| 291 | std::vector<CaseBlock> SwitchCases; |
| 292 | /// JTCases - Vector of JumpTable structures used to communicate |
| 293 | /// SwitchInst code generation information. |
| 294 | std::vector<JumpTableBlock> JTCases; |
| 295 | /// BitTestCases - Vector of BitTestBlock structures used to communicate |
| 296 | /// SwitchInst code generation information. |
| 297 | std::vector<BitTestBlock> BitTestCases; |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 298 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 299 | // Emit PHI-node-operand constants only once even if used by multiple |
| 300 | // PHI nodes. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 301 | DenseMap<const Constant *, unsigned> ConstantsOut; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 302 | |
| 303 | /// FuncInfo - Information about the function as a whole. |
| 304 | /// |
| 305 | FunctionLoweringInfo &FuncInfo; |
Bill Wendling | dfdacee | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 306 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 307 | /// OptLevel - What optimization level we're generating code for. |
Bill Wendling | dfdacee | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 308 | /// |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 309 | CodeGenOpt::Level OptLevel; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 310 | |
| 311 | /// GFI - Garbage collection metadata for the function. |
| 312 | GCFunctionInfo *GFI; |
| 313 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 314 | /// HasTailCall - This is set to true if a call in the current |
| 315 | /// block has been translated as a tail call. In this case, |
| 316 | /// no subsequent DAG nodes should be created. |
| 317 | /// |
| 318 | bool HasTailCall; |
| 319 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 320 | LLVMContext *Context; |
| 321 | |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 322 | SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 323 | CodeGenOpt::Level ol) |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 324 | : SDNodeOrder(0), TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()), |
| 325 | DAG(dag), FuncInfo(funcinfo), OptLevel(ol), |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 326 | HasTailCall(false), Context(dag.getContext()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void init(GCFunctionInfo *gfi, AliasAnalysis &aa); |
| 330 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 331 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 332 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 333 | /// for a new block. This doesn't clear out information about |
| 334 | /// additional blocks that are needed to complete switch lowering |
| 335 | /// or PHI node updating; that information is cleared out as it is |
| 336 | /// consumed. |
| 337 | void clear(); |
| 338 | |
| 339 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 340 | /// flushing any PendingLoad items. This must be done before emitting |
| 341 | /// a store or any other node that may need to be ordered after any |
| 342 | /// prior load instructions. |
| 343 | /// |
| 344 | SDValue getRoot(); |
| 345 | |
| 346 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 347 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 348 | /// to do this before emitting a terminator instruction. |
| 349 | /// |
| 350 | SDValue getControlRoot(); |
| 351 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 352 | DebugLoc getCurDebugLoc() const { return CurDebugLoc; } |
| 353 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 354 | unsigned getSDNodeOrder() const { return SDNodeOrder; } |
| 355 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 356 | void CopyValueToVirtualRegister(const Value *V, unsigned Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 357 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 358 | /// AssignOrderingToNode - Assign an ordering to the node. The order is gotten |
| 359 | /// from how the code appeared in the source. The ordering is used by the |
| 360 | /// scheduler to effectively turn off scheduling. |
| 361 | void AssignOrderingToNode(const SDNode *Node); |
| 362 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 363 | void visit(const Instruction &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 364 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 365 | void visit(unsigned Opcode, const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 366 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame^] | 367 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 368 | // generate the debug data structures now that we've seen its definition. |
| 369 | void resolveDanglingDebugInfo(const Value *V, SDValue Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 370 | SDValue getValue(const Value *V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 371 | SDValue getNonRegisterValue(const Value *V); |
| 372 | SDValue getValueImpl(const Value *V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 373 | |
| 374 | void setValue(const Value *V, SDValue NewN) { |
| 375 | SDValue &N = NodeMap[V]; |
| 376 | assert(N.getNode() == 0 && "Already set a value for this node!"); |
| 377 | N = NewN; |
| 378 | } |
| 379 | |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 380 | void setUnusedArgValue(const Value *V, SDValue NewN) { |
| 381 | SDValue &N = UnusedArgNodeMap[V]; |
| 382 | assert(N.getNode() == 0 && "Already set a value for this node!"); |
| 383 | N = NewN; |
| 384 | } |
| 385 | |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 386 | void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 387 | std::set<unsigned> &OutputRegs, |
| 388 | std::set<unsigned> &InputRegs); |
| 389 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 390 | void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 391 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 392 | MachineBasicBlock *SwitchBB, unsigned Opc); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 393 | void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 394 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 395 | MachineBasicBlock *CurBB, |
| 396 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 397 | bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 398 | bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); |
| 399 | void CopyToExportRegsIfNeeded(const Value *V); |
| 400 | void ExportFromCurrentBlock(const Value *V); |
| 401 | void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 402 | MachineBasicBlock *LandingPad = NULL); |
| 403 | |
| 404 | private: |
| 405 | // Terminator instructions. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 406 | void visitRet(const ReturnInst &I); |
| 407 | void visitBr(const BranchInst &I); |
| 408 | void visitSwitch(const SwitchInst &I); |
| 409 | void visitIndirectBr(const IndirectBrInst &I); |
Bill Wendling | a60f0e7 | 2010-07-15 23:42:21 +0000 | [diff] [blame] | 410 | void visitUnreachable(const UnreachableInst &I) { /* noop */ } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 411 | |
| 412 | // Helpers for visitSwitch |
| 413 | bool handleSmallSwitchRange(CaseRec& CR, |
| 414 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 415 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 416 | MachineBasicBlock* Default, |
| 417 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 418 | bool handleJTSwitchCase(CaseRec& CR, |
| 419 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 420 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 421 | MachineBasicBlock* Default, |
| 422 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 423 | bool handleBTSplitSwitchCase(CaseRec& CR, |
| 424 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 425 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 426 | MachineBasicBlock* Default, |
| 427 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 428 | bool handleBitTestsSwitchCase(CaseRec& CR, |
| 429 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 430 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 431 | MachineBasicBlock* Default, |
| 432 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 433 | public: |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 434 | void visitSwitchCase(CaseBlock &CB, |
| 435 | MachineBasicBlock *SwitchBB); |
| 436 | void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 437 | void visitBitTestCase(MachineBasicBlock* NextMBB, |
| 438 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 439 | BitTestCase &B, |
| 440 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 441 | void visitJumpTable(JumpTable &JT); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 442 | void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, |
| 443 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 444 | |
| 445 | private: |
| 446 | // These all get lowered before this pass. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 447 | void visitInvoke(const InvokeInst &I); |
| 448 | void visitUnwind(const UnwindInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 449 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 450 | void visitBinary(const User &I, unsigned OpCode); |
| 451 | void visitShift(const User &I, unsigned Opcode); |
| 452 | void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } |
| 453 | void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } |
| 454 | void visitSub(const User &I) { visitBinary(I, ISD::SUB); } |
| 455 | void visitFSub(const User &I); |
| 456 | void visitMul(const User &I) { visitBinary(I, ISD::MUL); } |
| 457 | void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } |
| 458 | void visitURem(const User &I) { visitBinary(I, ISD::UREM); } |
| 459 | void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } |
| 460 | void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } |
| 461 | void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } |
| 462 | void visitSDiv(const User &I) { visitBinary(I, ISD::SDIV); } |
| 463 | void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } |
| 464 | void visitAnd (const User &I) { visitBinary(I, ISD::AND); } |
| 465 | void visitOr (const User &I) { visitBinary(I, ISD::OR); } |
| 466 | void visitXor (const User &I) { visitBinary(I, ISD::XOR); } |
| 467 | void visitShl (const User &I) { visitShift(I, ISD::SHL); } |
| 468 | void visitLShr(const User &I) { visitShift(I, ISD::SRL); } |
| 469 | void visitAShr(const User &I) { visitShift(I, ISD::SRA); } |
| 470 | void visitICmp(const User &I); |
| 471 | void visitFCmp(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 472 | // Visit the conversion instructions |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 473 | void visitTrunc(const User &I); |
| 474 | void visitZExt(const User &I); |
| 475 | void visitSExt(const User &I); |
| 476 | void visitFPTrunc(const User &I); |
| 477 | void visitFPExt(const User &I); |
| 478 | void visitFPToUI(const User &I); |
| 479 | void visitFPToSI(const User &I); |
| 480 | void visitUIToFP(const User &I); |
| 481 | void visitSIToFP(const User &I); |
| 482 | void visitPtrToInt(const User &I); |
| 483 | void visitIntToPtr(const User &I); |
| 484 | void visitBitCast(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 485 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 486 | void visitExtractElement(const User &I); |
| 487 | void visitInsertElement(const User &I); |
| 488 | void visitShuffleVector(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 489 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 490 | void visitExtractValue(const ExtractValueInst &I); |
| 491 | void visitInsertValue(const InsertValueInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 492 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 493 | void visitGetElementPtr(const User &I); |
| 494 | void visitSelect(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 495 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 496 | void visitAlloca(const AllocaInst &I); |
| 497 | void visitLoad(const LoadInst &I); |
| 498 | void visitStore(const StoreInst &I); |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 499 | void visitPHI(const PHINode &I); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 500 | void visitCall(const CallInst &I); |
| 501 | bool visitMemCmpCall(const CallInst &I); |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 502 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 503 | void visitInlineAsm(ImmutableCallSite CS); |
| 504 | const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); |
| 505 | void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 506 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 507 | void visitPow(const CallInst &I); |
| 508 | void visitExp2(const CallInst &I); |
| 509 | void visitExp(const CallInst &I); |
| 510 | void visitLog(const CallInst &I); |
| 511 | void visitLog2(const CallInst &I); |
| 512 | void visitLog10(const CallInst &I); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 513 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 514 | void visitVAStart(const CallInst &I); |
| 515 | void visitVAArg(const VAArgInst &I); |
| 516 | void visitVAEnd(const CallInst &I); |
| 517 | void visitVACopy(const CallInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 518 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 519 | void visitUserOp1(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 520 | llvm_unreachable("UserOp1 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 521 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 522 | void visitUserOp2(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 523 | llvm_unreachable("UserOp2 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 526 | const char *implVisitBinaryAtomic(const CallInst& I, ISD::NodeType Op); |
| 527 | const char *implVisitAluOverflow(const CallInst &I, ISD::NodeType Op); |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 528 | |
| 529 | void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 530 | |
| 531 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a |
| 532 | /// function argument, create the corresponding DBG_VALUE machine instruction |
| 533 | /// for it now. At the end of instruction selection, they will be inserted to |
| 534 | /// the entry BB. |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 535 | bool EmitFuncArgumentDbgValue(const DbgValueInst &DI, |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 536 | const Value *V, MDNode *Variable, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 537 | uint64_t Offset, const SDValue &N); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 540 | } // end namespace llvm |
| 541 | |
| 542 | #endif |