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