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