Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===// |
| 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 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "SelectionDAGBuild.h" |
| 16 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
| 19 | #include "llvm/Constants.h" |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/GlobalVariable.h" |
| 25 | #include "llvm/InlineAsm.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/Intrinsics.h" |
| 28 | #include "llvm/IntrinsicInst.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/FastISel.h" |
| 31 | #include "llvm/CodeGen/GCStrategy.h" |
| 32 | #include "llvm/CodeGen/GCMetadata.h" |
| 33 | #include "llvm/CodeGen/MachineFunction.h" |
| 34 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 35 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 36 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 37 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 38 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/DwarfWriter.h" |
| 42 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetRegisterInfo.h" |
| 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Target/TargetFrameInfo.h" |
| 46 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetOptions.h" |
| 50 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 54 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 55 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 56 | #include <algorithm> |
| 57 | using namespace llvm; |
| 58 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 59 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 60 | /// some float libcalls (6, 8 or 12 bits). |
| 61 | static unsigned LimitFloatPrecision; |
| 62 | |
| 63 | static cl::opt<unsigned, true> |
| 64 | LimitFPPrecision("limit-float-precision", |
| 65 | cl::desc("Generate low-precision inline sequences " |
| 66 | "for some float libcalls"), |
| 67 | cl::location(LimitFloatPrecision), |
| 68 | cl::init(0)); |
| 69 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 70 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 71 | /// of insertvalue or extractvalue indices that identify a member, return |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 72 | /// the linearized index of the start of the member. |
| 73 | /// |
| 74 | static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, |
| 75 | const unsigned *Indices, |
| 76 | const unsigned *IndicesEnd, |
| 77 | unsigned CurIndex = 0) { |
| 78 | // Base case: We're done. |
| 79 | if (Indices && Indices == IndicesEnd) |
| 80 | return CurIndex; |
| 81 | |
| 82 | // Given a struct type, recursively traverse the elements. |
| 83 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 84 | for (StructType::element_iterator EB = STy->element_begin(), |
| 85 | EI = EB, |
| 86 | EE = STy->element_end(); |
| 87 | EI != EE; ++EI) { |
| 88 | if (Indices && *Indices == unsigned(EI - EB)) |
| 89 | return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); |
| 90 | CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); |
| 91 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 92 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 93 | } |
| 94 | // Given an array type, recursively traverse the elements. |
| 95 | else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 96 | const Type *EltTy = ATy->getElementType(); |
| 97 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 98 | if (Indices && *Indices == i) |
| 99 | return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); |
| 100 | CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); |
| 101 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 102 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 103 | } |
| 104 | // We haven't found the type we're looking for, so keep searching. |
| 105 | return CurIndex + 1; |
| 106 | } |
| 107 | |
| 108 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 109 | /// EVTs that represent all the individual underlying |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | /// non-aggregate types that comprise it. |
| 111 | /// |
| 112 | /// If Offsets is non-null, it points to a vector to be filled in |
| 113 | /// with the in-memory offsets of each of the individual values. |
| 114 | /// |
| 115 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 116 | SmallVectorImpl<EVT> &ValueVTs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 117 | SmallVectorImpl<uint64_t> *Offsets = 0, |
| 118 | uint64_t StartingOffset = 0) { |
| 119 | // Given a struct type, recursively traverse the elements. |
| 120 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 121 | const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); |
| 122 | for (StructType::element_iterator EB = STy->element_begin(), |
| 123 | EI = EB, |
| 124 | EE = STy->element_end(); |
| 125 | EI != EE; ++EI) |
| 126 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 127 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 128 | return; |
| 129 | } |
| 130 | // Given an array type, recursively traverse the elements. |
| 131 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 132 | const Type *EltTy = ATy->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 133 | uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 134 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 135 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 136 | StartingOffset + i * EltSize); |
| 137 | return; |
| 138 | } |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 139 | // Interpret void as zero return values. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 140 | if (Ty == Type::getVoidTy(Ty->getContext())) |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 141 | return; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 142 | // Base case: we can get an EVT for this LLVM IR type. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 143 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 144 | if (Offsets) |
| 145 | Offsets->push_back(StartingOffset); |
| 146 | } |
| 147 | |
Dan Gohman | 2a7c671 | 2008-09-03 23:18:39 +0000 | [diff] [blame] | 148 | namespace llvm { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 149 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 150 | /// that a particular set of values is assigned, and the type information about |
| 151 | /// the value. The most common situation is to represent one value at a time, |
| 152 | /// but struct or array values are handled element-wise as multiple values. |
| 153 | /// The splitting of aggregates is performed recursively, so that we never |
| 154 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 155 | /// have legal types, so each value may require one or more registers of some |
| 156 | /// legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 157 | /// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 158 | struct VISIBILITY_HIDDEN RegsForValue { |
| 159 | /// TLI - The TargetLowering object. |
| 160 | /// |
| 161 | const TargetLowering *TLI; |
| 162 | |
| 163 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 164 | /// may need be promoted or synthesized from one or more registers. |
| 165 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 166 | SmallVector<EVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 167 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 168 | /// RegVTs - The value types of the registers. This is the same size as |
| 169 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 170 | /// register or registers are. (Individual values are never synthesized |
| 171 | /// from more than one type of register.) |
| 172 | /// |
| 173 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 174 | /// getRegisterType member function, however when with physical registers |
| 175 | /// it is necessary to have a separate record of the types. |
| 176 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 177 | SmallVector<EVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 178 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 179 | /// Regs - This list holds the registers assigned to the values. |
| 180 | /// Each legal or promoted value requires one register, and each |
| 181 | /// expanded value requires multiple registers. |
| 182 | /// |
| 183 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 184 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 185 | RegsForValue() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 186 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 187 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 188 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 189 | EVT regvt, EVT valuevt) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 190 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 191 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 192 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 193 | const SmallVector<EVT, 4> ®vts, |
| 194 | const SmallVector<EVT, 4> &valuevts) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 195 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 196 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 197 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 198 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 199 | |
| 200 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 201 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 202 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 203 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 204 | for (unsigned i = 0; i != NumRegs; ++i) |
| 205 | Regs.push_back(Reg + i); |
| 206 | RegVTs.push_back(RegisterVT); |
| 207 | Reg += NumRegs; |
| 208 | } |
| 209 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 210 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | /// append - Add the specified values to this one. |
| 212 | void append(const RegsForValue &RHS) { |
| 213 | TLI = RHS.TLI; |
| 214 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 215 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 216 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 217 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 218 | |
| 219 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 220 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 221 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 222 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 223 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 224 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 225 | SDValue &Chain, SDValue *Flag) const; |
| 226 | |
| 227 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 228 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 229 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 230 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 231 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 232 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 233 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 234 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 235 | /// operand list. This adds the code marker, matching input operand index |
| 236 | /// (if applicable), and includes the number of values added into it. |
| 237 | void AddInlineAsmOperands(unsigned Code, |
| 238 | bool HasMatching, unsigned MatchingIdx, |
| 239 | SelectionDAG &DAG, std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 240 | }; |
| 241 | } |
| 242 | |
| 243 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 244 | /// PHI nodes or outside of the basic block that defines it, or used by a |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 245 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
| 246 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 247 | if (isa<PHINode>(I)) return true; |
| 248 | BasicBlock *BB = I->getParent(); |
| 249 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 250 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 251 | return true; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 256 | /// entry block, return true. This includes arguments used by switches, since |
| 257 | /// the switch may expand into multiple basic blocks. |
| 258 | static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { |
| 259 | // With FastISel active, we may be splitting blocks, so force creation |
| 260 | // of virtual registers for all non-dead arguments. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 261 | // Don't force virtual registers for byval arguments though, because |
| 262 | // fast-isel can't handle those in all cases. |
| 263 | if (EnableFastISel && !A->hasByValAttr()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 264 | return A->use_empty(); |
| 265 | |
| 266 | BasicBlock *Entry = A->getParent()->begin(); |
| 267 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 268 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 269 | return false; // Use not in entry block. |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) |
| 274 | : TLI(tli) { |
| 275 | } |
| 276 | |
| 277 | void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 278 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 279 | bool EnableFastISel) { |
| 280 | Fn = &fn; |
| 281 | MF = &mf; |
| 282 | RegInfo = &MF->getRegInfo(); |
| 283 | |
| 284 | // Create a vreg for each argument register that is not dead and is used |
| 285 | // outside of the entry block for the function. |
| 286 | for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
| 287 | AI != E; ++AI) |
| 288 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 289 | InitializeRegForValue(AI); |
| 290 | |
| 291 | // Initialize the mapping of values to registers. This is only set up for |
| 292 | // instruction values that are used outside of the block that defines |
| 293 | // them. |
| 294 | Function::iterator BB = Fn->begin(), EB = Fn->end(); |
| 295 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 296 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 297 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 298 | const Type *Ty = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 299 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 300 | unsigned Align = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 301 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 302 | AI->getAlignment()); |
| 303 | |
| 304 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 305 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 306 | StaticAllocaMap[AI] = |
| 307 | MF->getFrameInfo()->CreateStackObject(TySize, Align); |
| 308 | } |
| 309 | |
| 310 | for (; BB != EB; ++BB) |
| 311 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 312 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 313 | if (!isa<AllocaInst>(I) || |
| 314 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 315 | InitializeRegForValue(I); |
| 316 | |
| 317 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 318 | // also creates the initial PHI MachineInstrs, though none of the input |
| 319 | // operands are populated. |
| 320 | for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { |
| 321 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 322 | MBBMap[BB] = MBB; |
| 323 | MF->push_back(MBB); |
| 324 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 325 | // Transfer the address-taken flag. This is necessary because there could |
| 326 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 327 | // the first one should be marked. |
| 328 | if (BB->hasAddressTaken()) |
| 329 | MBB->setHasAddressTaken(); |
| 330 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 331 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 332 | // appropriate. |
| 333 | PHINode *PN; |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 334 | DebugLoc DL; |
| 335 | for (BasicBlock::iterator |
| 336 | I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 337 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 338 | if (Function *F = CI->getCalledFunction()) { |
| 339 | switch (F->getIntrinsicID()) { |
| 340 | default: break; |
| 341 | case Intrinsic::dbg_stoppoint: { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 342 | DbgStopPointInst *SPI = cast<DbgStopPointInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 343 | if (isValidDebugInfoIntrinsic(*SPI, CodeGenOpt::Default)) |
| 344 | DL = ExtractDebugLocation(*SPI, MF->getDebugLocInfo()); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 345 | break; |
| 346 | } |
| 347 | case Intrinsic::dbg_func_start: { |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 348 | DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 349 | if (isValidDebugInfoIntrinsic(*FSI, CodeGenOpt::Default)) |
| 350 | DL = ExtractDebugLocation(*FSI, MF->getDebugLocInfo()); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | PN = dyn_cast<PHINode>(I); |
| 358 | if (!PN || PN->use_empty()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 359 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 360 | unsigned PHIReg = ValueMap[PN]; |
| 361 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 362 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 363 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 364 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 365 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 366 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 367 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 368 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 369 | for (unsigned i = 0; i != NumRegisters; ++i) |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 370 | BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 371 | PHIReg += NumRegisters; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 377 | unsigned FunctionLoweringInfo::MakeReg(EVT VT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 378 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 379 | } |
| 380 | |
| 381 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 382 | /// the correctly promoted or expanded types. Assign these registers |
| 383 | /// consecutive vreg numbers and return the first assigned number. |
| 384 | /// |
| 385 | /// In the case that the given value has struct or array type, this function |
| 386 | /// will assign registers for each member or element. |
| 387 | /// |
| 388 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 389 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 390 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
| 391 | |
| 392 | unsigned FirstReg = 0; |
| 393 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 394 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 395 | EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 396 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 397 | unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 398 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 399 | unsigned R = MakeReg(RegisterVT); |
| 400 | if (!FirstReg) FirstReg = R; |
| 401 | } |
| 402 | } |
| 403 | return FirstReg; |
| 404 | } |
| 405 | |
| 406 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 407 | /// combined into the value they represent. If the parts combine to a type |
| 408 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 409 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 410 | /// (ISD::AssertSext). |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 411 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
| 412 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 413 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 414 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 415 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 416 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 417 | SDValue Val = Parts[0]; |
| 418 | |
| 419 | if (NumParts > 1) { |
| 420 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 421 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 422 | unsigned PartBits = PartVT.getSizeInBits(); |
| 423 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 424 | |
| 425 | // Assemble the power of 2 part. |
| 426 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 427 | 1 << Log2_32(NumParts) : NumParts; |
| 428 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 429 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 430 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 431 | SDValue Lo, Hi; |
| 432 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 433 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 434 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 435 | if (RoundParts > 2) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 436 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT); |
| 437 | Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 438 | PartVT, HalfVT); |
| 439 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 440 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 441 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 442 | } |
| 443 | if (TLI.isBigEndian()) |
| 444 | std::swap(Lo, Hi); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 445 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 446 | |
| 447 | if (RoundParts < NumParts) { |
| 448 | // Assemble the trailing non-power-of-2 part. |
| 449 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 450 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 451 | Hi = getCopyFromParts(DAG, dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 452 | Parts+RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 453 | |
| 454 | // Combine the round and odd parts. |
| 455 | Lo = Val; |
| 456 | if (TLI.isBigEndian()) |
| 457 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 458 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 459 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 460 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 461 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 462 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 463 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 464 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 465 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 466 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 467 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 468 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 469 | unsigned NumIntermediates; |
| 470 | unsigned NumRegs = |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 471 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 472 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 473 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 474 | NumParts = NumRegs; // Silence a compiler warning. |
| 475 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 476 | assert(RegisterVT == Parts[0].getValueType() && |
| 477 | "Part type doesn't match part!"); |
| 478 | |
| 479 | // Assemble the parts into intermediate operands. |
| 480 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 481 | if (NumIntermediates == NumParts) { |
| 482 | // If the register was not expanded, truncate or copy the value, |
| 483 | // as appropriate. |
| 484 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 485 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 486 | PartVT, IntermediateVT); |
| 487 | } else if (NumParts > 0) { |
| 488 | // If the intermediate type was expanded, build the intermediate operands |
| 489 | // from the parts. |
| 490 | assert(NumParts % NumIntermediates == 0 && |
| 491 | "Must expand into a divisible number of parts!"); |
| 492 | unsigned Factor = NumParts / NumIntermediates; |
| 493 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 494 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 495 | PartVT, IntermediateVT); |
| 496 | } |
| 497 | |
| 498 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 499 | // operands. |
| 500 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 501 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 502 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 503 | } else if (PartVT.isFloatingPoint()) { |
| 504 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 505 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 506 | "Unexpected split"); |
| 507 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 508 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 509 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 510 | if (TLI.isBigEndian()) |
| 511 | std::swap(Lo, Hi); |
| 512 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 513 | } else { |
| 514 | // FP split into integer parts (soft fp) |
| 515 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 516 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 517 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 518 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 523 | PartVT = Val.getValueType(); |
| 524 | |
| 525 | if (PartVT == ValueVT) |
| 526 | return Val; |
| 527 | |
| 528 | if (PartVT.isVector()) { |
| 529 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 530 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (ValueVT.isVector()) { |
| 534 | assert(ValueVT.getVectorElementType() == PartVT && |
| 535 | ValueVT.getVectorNumElements() == 1 && |
| 536 | "Only trivial scalar-to-vector conversions should get here!"); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 537 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | if (PartVT.isInteger() && |
| 541 | ValueVT.isInteger()) { |
| 542 | if (ValueVT.bitsLT(PartVT)) { |
| 543 | // For a truncate, see if we have any information to |
| 544 | // indicate whether the truncated bits will always be |
| 545 | // zero or sign-extension. |
| 546 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 547 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 548 | DAG.getValueType(ValueVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 549 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 550 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 551 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 556 | if (ValueVT.bitsLT(Val.getValueType())) |
| 557 | // FP_ROUND's are always exact here. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 558 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 559 | DAG.getIntPtrConstant(1)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 560 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 564 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 565 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 566 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 567 | return SDValue(); |
| 568 | } |
| 569 | |
| 570 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 571 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 572 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 573 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 574 | SDValue *Parts, unsigned NumParts, EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 575 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 576 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 577 | EVT PtrVT = TLI.getPointerTy(); |
| 578 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 579 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 580 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 581 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 582 | |
| 583 | if (!NumParts) |
| 584 | return; |
| 585 | |
| 586 | if (!ValueVT.isVector()) { |
| 587 | if (PartVT == ValueVT) { |
| 588 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 589 | Parts[0] = Val; |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 594 | // If the parts cover more bits than the value has, promote the value. |
| 595 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 596 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 597 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 598 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 599 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 600 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 601 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 602 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 603 | } |
| 604 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 605 | // Different types of the same size. |
| 606 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 607 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 608 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 609 | // If the parts cover less bits than value has, truncate the value. |
| 610 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 611 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 612 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 613 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 614 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | // The value may have changed - recompute ValueVT. |
| 619 | ValueVT = Val.getValueType(); |
| 620 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 621 | "Failed to tile the value with PartVT!"); |
| 622 | |
| 623 | if (NumParts == 1) { |
| 624 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 625 | Parts[0] = Val; |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | // Expand the value into multiple parts. |
| 630 | if (NumParts & (NumParts - 1)) { |
| 631 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 632 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 633 | "Do not know what to expand to!"); |
| 634 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 635 | unsigned RoundBits = RoundParts * PartBits; |
| 636 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 637 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 638 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 639 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 640 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 641 | if (TLI.isBigEndian()) |
| 642 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 643 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 644 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 645 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 646 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 650 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 651 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 652 | EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 653 | Val); |
| 654 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 655 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 656 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 657 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 658 | SDValue &Part0 = Parts[i]; |
| 659 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 660 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 661 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 662 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 663 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 664 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 665 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 666 | DAG.getConstant(0, PtrVT)); |
| 667 | |
| 668 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 669 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 670 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 671 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 672 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 678 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 679 | |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | // Vector ValueVT. |
| 684 | if (NumParts == 1) { |
| 685 | if (PartVT != ValueVT) { |
| 686 | if (PartVT.isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 687 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 688 | } else { |
| 689 | assert(ValueVT.getVectorElementType() == PartVT && |
| 690 | ValueVT.getVectorNumElements() == 1 && |
| 691 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 692 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 693 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 694 | DAG.getConstant(0, PtrVT)); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | Parts[0] = Val; |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 703 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 704 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 705 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 706 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 707 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 708 | |
| 709 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 710 | NumParts = NumRegs; // Silence a compiler warning. |
| 711 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 712 | |
| 713 | // Split the vector into intermediate operands. |
| 714 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 715 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 716 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 717 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 718 | IntermediateVT, Val, |
| 719 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 720 | PtrVT)); |
| 721 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 722 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 723 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 724 | DAG.getConstant(i, PtrVT)); |
| 725 | |
| 726 | // Split the intermediate operands into legal parts. |
| 727 | if (NumParts == NumIntermediates) { |
| 728 | // If the register was not expanded, promote or copy the value, |
| 729 | // as appropriate. |
| 730 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 731 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 732 | } else if (NumParts > 0) { |
| 733 | // If the intermediate type was expanded, split each the value into |
| 734 | // legal parts. |
| 735 | assert(NumParts % NumIntermediates == 0 && |
| 736 | "Must expand into a divisible number of parts!"); |
| 737 | unsigned Factor = NumParts / NumIntermediates; |
| 738 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 739 | getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | |
| 744 | void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 745 | AA = &aa; |
| 746 | GFI = gfi; |
| 747 | TD = DAG.getTarget().getTargetData(); |
| 748 | } |
| 749 | |
| 750 | /// clear - Clear out the curret SelectionDAG and the associated |
| 751 | /// state and prepare this SelectionDAGLowering object to be used |
| 752 | /// for a new block. This doesn't clear out information about |
| 753 | /// additional blocks that are needed to complete switch lowering |
| 754 | /// or PHI node updating; that information is cleared out as it is |
| 755 | /// consumed. |
| 756 | void SelectionDAGLowering::clear() { |
| 757 | NodeMap.clear(); |
| 758 | PendingLoads.clear(); |
| 759 | PendingExports.clear(); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 760 | EdgeMapping.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 761 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 762 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 763 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 767 | /// flushing any PendingLoad items. This must be done before emitting |
| 768 | /// a store or any other node that may need to be ordered after any |
| 769 | /// prior load instructions. |
| 770 | /// |
| 771 | SDValue SelectionDAGLowering::getRoot() { |
| 772 | if (PendingLoads.empty()) |
| 773 | return DAG.getRoot(); |
| 774 | |
| 775 | if (PendingLoads.size() == 1) { |
| 776 | SDValue Root = PendingLoads[0]; |
| 777 | DAG.setRoot(Root); |
| 778 | PendingLoads.clear(); |
| 779 | return Root; |
| 780 | } |
| 781 | |
| 782 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 783 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 784 | &PendingLoads[0], PendingLoads.size()); |
| 785 | PendingLoads.clear(); |
| 786 | DAG.setRoot(Root); |
| 787 | return Root; |
| 788 | } |
| 789 | |
| 790 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 791 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 792 | /// to do this before emitting a terminator instruction. |
| 793 | /// |
| 794 | SDValue SelectionDAGLowering::getControlRoot() { |
| 795 | SDValue Root = DAG.getRoot(); |
| 796 | |
| 797 | if (PendingExports.empty()) |
| 798 | return Root; |
| 799 | |
| 800 | // Turn all of the CopyToReg chains into one factored node. |
| 801 | if (Root.getOpcode() != ISD::EntryToken) { |
| 802 | unsigned i = 0, e = PendingExports.size(); |
| 803 | for (; i != e; ++i) { |
| 804 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 805 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 806 | break; // Don't add the root if we already indirectly depend on it. |
| 807 | } |
| 808 | |
| 809 | if (i == e) |
| 810 | PendingExports.push_back(Root); |
| 811 | } |
| 812 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 813 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 814 | &PendingExports[0], |
| 815 | PendingExports.size()); |
| 816 | PendingExports.clear(); |
| 817 | DAG.setRoot(Root); |
| 818 | return Root; |
| 819 | } |
| 820 | |
| 821 | void SelectionDAGLowering::visit(Instruction &I) { |
| 822 | visit(I.getOpcode(), I); |
| 823 | } |
| 824 | |
| 825 | void SelectionDAGLowering::visit(unsigned Opcode, User &I) { |
| 826 | // Note: this doesn't use InstVisitor, because it has to work with |
| 827 | // ConstantExpr's in addition to instructions. |
| 828 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 829 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 830 | // Build the switch statement using the Instruction.def file. |
| 831 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 832 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 833 | #include "llvm/Instruction.def" |
| 834 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 835 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 836 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 837 | SDValue SelectionDAGLowering::getValue(const Value *V) { |
| 838 | SDValue &N = NodeMap[V]; |
| 839 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 840 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 841 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 842 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 843 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 844 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 845 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 846 | |
| 847 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 848 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 849 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 850 | if (isa<ConstantPointerNull>(C)) |
| 851 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 852 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 853 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 854 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 855 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 856 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 857 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 858 | |
| 859 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 860 | visit(CE->getOpcode(), *CE); |
| 861 | SDValue N1 = NodeMap[V]; |
| 862 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 863 | return N1; |
| 864 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 865 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 866 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 867 | SmallVector<SDValue, 4> Constants; |
| 868 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 869 | OI != OE; ++OI) { |
| 870 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 871 | // If the operand is an empty aggregate, there are no values. |
| 872 | if (!Val) continue; |
| 873 | // Add each leaf value from the operand to the Constants list |
| 874 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 875 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 876 | Constants.push_back(SDValue(Val, i)); |
| 877 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 878 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 879 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 883 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 884 | "Unknown struct or array constant!"); |
| 885 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 886 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 887 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 888 | unsigned NumElts = ValueVTs.size(); |
| 889 | if (NumElts == 0) |
| 890 | return SDValue(); // empty struct |
| 891 | SmallVector<SDValue, 4> Constants(NumElts); |
| 892 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 893 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 894 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 895 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 896 | else if (EltVT.isFloatingPoint()) |
| 897 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 898 | else |
| 899 | Constants[i] = DAG.getConstant(0, EltVT); |
| 900 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 901 | return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 904 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 905 | return DAG.getBlockAddress(BA, getCurDebugLoc()); |
| 906 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 907 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 908 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 909 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 910 | // Now that we know the number and type of the elements, get that number of |
| 911 | // elements into the Ops array based on what kind of constant it is. |
| 912 | SmallVector<SDValue, 16> Ops; |
| 913 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 914 | for (unsigned i = 0; i != NumElements; ++i) |
| 915 | Ops.push_back(getValue(CP->getOperand(i))); |
| 916 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 917 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 918 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 919 | |
| 920 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 921 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 922 | Op = DAG.getConstantFP(0, EltVT); |
| 923 | else |
| 924 | Op = DAG.getConstant(0, EltVT); |
| 925 | Ops.assign(NumElements, Op); |
| 926 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 928 | // Create a BUILD_VECTOR node. |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 929 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 930 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 931 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 932 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 933 | // If this is a static alloca, generate it as the frameindex instead of |
| 934 | // computation. |
| 935 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 936 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 937 | FuncInfo.StaticAllocaMap.find(AI); |
| 938 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 939 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 940 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 941 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 942 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 943 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 944 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 945 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 946 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 947 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 950 | /// Get the EVTs and ArgFlags collections that represent the return type |
| 951 | /// of the given function. This does not require a DAG or a return value, and |
| 952 | /// is suitable for use before any DAGs for the function are constructed. |
| 953 | static void getReturnInfo(const Function* F, SmallVectorImpl<EVT> &OutVTs, |
| 954 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
| 955 | TargetLowering &TLI) { |
| 956 | const Type* ReturnType = F->getReturnType(); |
| 957 | |
| 958 | SmallVector<EVT, 4> ValueVTs; |
| 959 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
| 960 | unsigned NumValues = ValueVTs.size(); |
| 961 | if ( NumValues == 0 ) return; |
| 962 | |
| 963 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 964 | EVT VT = ValueVTs[j]; |
| 965 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 966 | |
| 967 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 968 | ExtendKind = ISD::SIGN_EXTEND; |
| 969 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 970 | ExtendKind = ISD::ZERO_EXTEND; |
| 971 | |
| 972 | // FIXME: C calling convention requires the return type to be promoted to |
| 973 | // at least 32-bit. But this is not necessary for non-C calling |
| 974 | // conventions. The frontend should mark functions whose return values |
| 975 | // require promoting with signext or zeroext attributes. |
| 976 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 977 | EVT MinVT = TLI.getRegisterType(F->getContext(), MVT::i32); |
| 978 | if (VT.bitsLT(MinVT)) |
| 979 | VT = MinVT; |
| 980 | } |
| 981 | |
| 982 | unsigned NumParts = TLI.getNumRegisters(F->getContext(), VT); |
| 983 | EVT PartVT = TLI.getRegisterType(F->getContext(), VT); |
| 984 | // 'inreg' on function refers to return value |
| 985 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 986 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 987 | Flags.setInReg(); |
| 988 | |
| 989 | // Propagate extension type if any |
| 990 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 991 | Flags.setSExt(); |
| 992 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 993 | Flags.setZExt(); |
| 994 | |
| 995 | for (unsigned i = 0; i < NumParts; ++i) |
| 996 | { |
| 997 | OutVTs.push_back(PartVT); |
| 998 | OutFlags.push_back(Flags); |
| 999 | } |
| 1000 | } |
| 1001 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1002 | |
| 1003 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1004 | SDValue Chain = getControlRoot(); |
| 1005 | SmallVector<ISD::OutputArg, 8> Outs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1006 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1007 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1008 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1009 | unsigned NumValues = ValueVTs.size(); |
| 1010 | if (NumValues == 0) continue; |
| 1011 | |
| 1012 | SDValue RetOp = getValue(I.getOperand(i)); |
| 1013 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1014 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1015 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1016 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1017 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1018 | const Function *F = I.getParent()->getParent(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1019 | if (F->paramHasAttr(0, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1020 | ExtendKind = ISD::SIGN_EXTEND; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1021 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1022 | ExtendKind = ISD::ZERO_EXTEND; |
| 1023 | |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1024 | // FIXME: C calling convention requires the return type to be promoted to |
| 1025 | // at least 32-bit. But this is not necessary for non-C calling |
| 1026 | // conventions. The frontend should mark functions whose return values |
| 1027 | // require promoting with signext or zeroext attributes. |
| 1028 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1029 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1030 | if (VT.bitsLT(MinVT)) |
| 1031 | VT = MinVT; |
| 1032 | } |
| 1033 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1034 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 1035 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1036 | SmallVector<SDValue, 4> Parts(NumParts); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1037 | getCopyToParts(DAG, getCurDebugLoc(), |
| 1038 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1039 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 1040 | |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1041 | // 'inreg' on function refers to return value |
| 1042 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1043 | if (F->paramHasAttr(0, Attribute::InReg)) |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1044 | Flags.setInReg(); |
Anton Korobeynikov | 0692fab | 2009-07-16 13:35:48 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Propagate extension type if any |
| 1047 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 1048 | Flags.setSExt(); |
| 1049 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 1050 | Flags.setZExt(); |
| 1051 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1052 | for (unsigned i = 0; i < NumParts; ++i) |
| 1053 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1056 | |
| 1057 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1058 | CallingConv::ID CallConv = |
| 1059 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1060 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 1061 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1064 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1065 | "LowerReturn didn't return a valid chain!"); |
| 1066 | |
| 1067 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1068 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1071 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1072 | /// created for it, emit nodes to copy the value into the virtual |
| 1073 | /// registers. |
| 1074 | void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) { |
| 1075 | if (!V->use_empty()) { |
| 1076 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1077 | if (VMI != FuncInfo.ValueMap.end()) |
| 1078 | CopyValueToVirtualRegister(V, VMI->second); |
| 1079 | } |
| 1080 | } |
| 1081 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1082 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1083 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1084 | /// CopyTo/FromReg. |
| 1085 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 1086 | // No need to export constants. |
| 1087 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1088 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1089 | // Already exported? |
| 1090 | if (FuncInfo.isExportedInst(V)) return; |
| 1091 | |
| 1092 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1093 | CopyValueToVirtualRegister(V, Reg); |
| 1094 | } |
| 1095 | |
| 1096 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 1097 | const BasicBlock *FromBB) { |
| 1098 | // The operands of the setcc have to be in this block. We don't know |
| 1099 | // how to export them from some other block. |
| 1100 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 1101 | // Can export from current BB. |
| 1102 | if (VI->getParent() == FromBB) |
| 1103 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1104 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1105 | // Is already exported, noop. |
| 1106 | return FuncInfo.isExportedInst(V); |
| 1107 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1109 | // If this is an argument, we can export it if the BB is the entry block or |
| 1110 | // if it is already exported. |
| 1111 | if (isa<Argument>(V)) { |
| 1112 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1113 | return true; |
| 1114 | |
| 1115 | // Otherwise, can only export this if it is already exported. |
| 1116 | return FuncInfo.isExportedInst(V); |
| 1117 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1118 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1119 | // Otherwise, constants can always be exported. |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1124 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1125 | return I->getParent() == BB; |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1129 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 1130 | /// the given LLVM IR floating-point condition code. This includes |
| 1131 | /// consideration of global floating-point math flags. |
| 1132 | /// |
| 1133 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 1134 | ISD::CondCode FPC, FOC; |
| 1135 | switch (Pred) { |
| 1136 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1137 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1138 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1139 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1140 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1141 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1142 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1143 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1144 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 1145 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1146 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1147 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1148 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1149 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1150 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1151 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1152 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1153 | llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1154 | FOC = FPC = ISD::SETFALSE; |
| 1155 | break; |
| 1156 | } |
| 1157 | if (FiniteOnlyFPMath()) |
| 1158 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1159 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1160 | return FPC; |
| 1161 | } |
| 1162 | |
| 1163 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1164 | /// the given LLVM IR integer condition code. |
| 1165 | /// |
| 1166 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1167 | switch (Pred) { |
| 1168 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1169 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1170 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1171 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1172 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1173 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1174 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1175 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1176 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1177 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1178 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1179 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1180 | return ISD::SETNE; |
| 1181 | } |
| 1182 | } |
| 1183 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1184 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1185 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1186 | /// AND operator tree. |
| 1187 | /// |
| 1188 | void |
| 1189 | SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond, |
| 1190 | MachineBasicBlock *TBB, |
| 1191 | MachineBasicBlock *FBB, |
| 1192 | MachineBasicBlock *CurBB) { |
| 1193 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1194 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1195 | // If the leaf of the tree is a comparison, merge the condition into |
| 1196 | // the caseblock. |
| 1197 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1198 | // The operands of the cmp have to be in this block. We don't know |
| 1199 | // how to export them from some other block. If this is the first block |
| 1200 | // of the sequence, no exporting is needed. |
| 1201 | if (CurBB == CurMBB || |
| 1202 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1203 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1204 | ISD::CondCode Condition; |
| 1205 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1206 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1207 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1208 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1209 | } else { |
| 1210 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1211 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1212 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1213 | |
| 1214 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1215 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1216 | SwitchCases.push_back(CB); |
| 1217 | return; |
| 1218 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1222 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1223 | NULL, TBB, FBB, CurBB); |
| 1224 | SwitchCases.push_back(CB); |
| 1225 | } |
| 1226 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1227 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1228 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 1229 | MachineBasicBlock *TBB, |
| 1230 | MachineBasicBlock *FBB, |
| 1231 | MachineBasicBlock *CurBB, |
| 1232 | unsigned Opc) { |
| 1233 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1234 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1235 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1236 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1237 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1238 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1239 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1240 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1241 | return; |
| 1242 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1243 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1244 | // Create TmpBB after CurBB. |
| 1245 | MachineFunction::iterator BBI = CurBB; |
| 1246 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1247 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1248 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1249 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1250 | if (Opc == Instruction::Or) { |
| 1251 | // Codegen X | Y as: |
| 1252 | // jmp_if_X TBB |
| 1253 | // jmp TmpBB |
| 1254 | // TmpBB: |
| 1255 | // jmp_if_Y TBB |
| 1256 | // jmp FBB |
| 1257 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1258 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1259 | // Emit the LHS condition. |
| 1260 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1261 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1262 | // Emit the RHS condition into TmpBB. |
| 1263 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1264 | } else { |
| 1265 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1266 | // Codegen X & Y as: |
| 1267 | // jmp_if_X TmpBB |
| 1268 | // jmp FBB |
| 1269 | // TmpBB: |
| 1270 | // jmp_if_Y TBB |
| 1271 | // jmp FBB |
| 1272 | // |
| 1273 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1274 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1275 | // Emit the LHS condition. |
| 1276 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1277 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1278 | // Emit the RHS condition into TmpBB. |
| 1279 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1284 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1285 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1286 | bool |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1287 | SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1288 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1289 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1290 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1291 | // will get folded into a single comparison, so don't emit two blocks. |
| 1292 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1293 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1294 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1295 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1296 | return false; |
| 1297 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1298 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1299 | return true; |
| 1300 | } |
| 1301 | |
| 1302 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1303 | // Update machine-CFG edges. |
| 1304 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1305 | |
| 1306 | // Figure out which block is immediately after the current one. |
| 1307 | MachineBasicBlock *NextBlock = 0; |
| 1308 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1309 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1310 | NextBlock = BBI; |
| 1311 | |
| 1312 | if (I.isUnconditional()) { |
| 1313 | // Update machine-CFG edges. |
| 1314 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1315 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1316 | // If this is not a fall-through branch, emit the branch. |
| 1317 | if (Succ0MBB != NextBlock) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1318 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1319 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1320 | DAG.getBasicBlock(Succ0MBB))); |
| 1321 | return; |
| 1322 | } |
| 1323 | |
| 1324 | // If this condition is one of the special cases we handle, do special stuff |
| 1325 | // now. |
| 1326 | Value *CondVal = I.getCondition(); |
| 1327 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1328 | |
| 1329 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1330 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1331 | // For example, instead of something like: |
| 1332 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1333 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1334 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1335 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1336 | // or C, F |
| 1337 | // jnz foo |
| 1338 | // Emit: |
| 1339 | // cmp A, B |
| 1340 | // je foo |
| 1341 | // cmp D, E |
| 1342 | // jle foo |
| 1343 | // |
| 1344 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1345 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1346 | (BOp->getOpcode() == Instruction::And || |
| 1347 | BOp->getOpcode() == Instruction::Or)) { |
| 1348 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1349 | // If the compares in later blocks need to use values not currently |
| 1350 | // exported from this block, export them now. This block should always |
| 1351 | // be the first entry. |
| 1352 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1353 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1354 | // Allow some cases to be rejected. |
| 1355 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1356 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1357 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1358 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1359 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1360 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1361 | // Emit the branch for this block. |
| 1362 | visitSwitchCase(SwitchCases[0]); |
| 1363 | SwitchCases.erase(SwitchCases.begin()); |
| 1364 | return; |
| 1365 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1366 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1367 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1368 | // SwitchCases. |
| 1369 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1370 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1371 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1372 | SwitchCases.clear(); |
| 1373 | } |
| 1374 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1375 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1376 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1377 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1378 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
| 1379 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1380 | // cond branch. |
| 1381 | visitSwitchCase(CB); |
| 1382 | } |
| 1383 | |
| 1384 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1385 | /// the binary search tree resulting from lowering a switch instruction. |
| 1386 | void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) { |
| 1387 | SDValue Cond; |
| 1388 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1389 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1392 | if (CB.CmpMHS == NULL) { |
| 1393 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1394 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1395 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1396 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1397 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1398 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1399 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1400 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1401 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1402 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1403 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1404 | } else { |
| 1405 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1406 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1407 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1408 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1409 | |
| 1410 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1411 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1414 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1415 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1416 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1417 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1418 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1419 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1420 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1421 | } |
| 1422 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1423 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1424 | // Update successor info |
| 1425 | CurMBB->addSuccessor(CB.TrueBB); |
| 1426 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1427 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1428 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1429 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1430 | MachineBasicBlock *NextBlock = 0; |
| 1431 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1432 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1433 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1434 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1435 | // If the lhs block is the next block, invert the condition so that we can |
| 1436 | // fall through to the lhs instead of the rhs block. |
| 1437 | if (CB.TrueBB == NextBlock) { |
| 1438 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1439 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1440 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1441 | } |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1442 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1443 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1444 | DAG.getBasicBlock(CB.TrueBB)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1445 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1446 | // If the branch was constant folded, fix up the CFG. |
| 1447 | if (BrCond.getOpcode() == ISD::BR) { |
| 1448 | CurMBB->removeSuccessor(CB.FalseBB); |
| 1449 | DAG.setRoot(BrCond); |
| 1450 | } else { |
| 1451 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1452 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1453 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1454 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1455 | if (CB.FalseBB == NextBlock) |
| 1456 | DAG.setRoot(BrCond); |
| 1457 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1458 | DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1459 | DAG.getBasicBlock(CB.FalseBB))); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1464 | void SelectionDAGLowering::visitJumpTable(JumpTable &JT) { |
| 1465 | // Emit the code for the jump table |
| 1466 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1467 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1468 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1469 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1470 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1471 | DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1472 | MVT::Other, Index.getValue(1), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1473 | Table, Index)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1477 | /// in the JumpTable from switch case. |
| 1478 | void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT, |
| 1479 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1480 | // Subtract the lowest switch case value from the value being switched on and |
| 1481 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1482 | // difference between smallest and largest cases. |
| 1483 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1484 | EVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1485 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1486 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1487 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1488 | // The SDNode we just created, which holds the value being switched on minus |
| 1489 | // the the smallest case value, needs to be copied to a virtual register so it |
| 1490 | // can be used as an index into the jump table in a subsequent basic block. |
| 1491 | // This value may be smaller or larger than the target's pointer type, and |
| 1492 | // therefore require extension or truncating. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 1493 | SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1494 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1495 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1496 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1497 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1498 | JT.Reg = JumpTableReg; |
| 1499 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1500 | // Emit the range check for the jump table, and branch to the default block |
| 1501 | // for the switch statement if the value being switched on exceeds the largest |
| 1502 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1503 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
| 1504 | TLI.getSetCCResultType(SUB.getValueType()), SUB, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1505 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1506 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1509 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1510 | MachineBasicBlock *NextBlock = 0; |
| 1511 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1512 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1513 | NextBlock = BBI; |
| 1514 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1515 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1516 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1517 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (JT.MBB == NextBlock) |
| 1520 | DAG.setRoot(BrCond); |
| 1521 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1522 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1523 | DAG.getBasicBlock(JT.MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1527 | /// suitable for "bit tests" |
| 1528 | void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) { |
| 1529 | // Subtract the minimum value |
| 1530 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1531 | EVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1532 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1533 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1536 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1537 | TLI.getSetCCResultType(SUB.getValueType()), |
| 1538 | SUB, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1539 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1540 | |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 1541 | SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1542 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1543 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1544 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1545 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1548 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1549 | MachineBasicBlock *NextBlock = 0; |
| 1550 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1551 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1552 | NextBlock = BBI; |
| 1553 | |
| 1554 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1555 | |
| 1556 | CurMBB->addSuccessor(B.Default); |
| 1557 | CurMBB->addSuccessor(MBB); |
| 1558 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1559 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1560 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1561 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1562 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1563 | if (MBB == NextBlock) |
| 1564 | DAG.setRoot(BrRange); |
| 1565 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1566 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1567 | DAG.getBasicBlock(MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /// visitBitTestCase - this function produces one "bit test" |
| 1571 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1572 | unsigned Reg, |
| 1573 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1574 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1575 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1576 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1577 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1578 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1579 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1580 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1581 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1582 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1583 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1584 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1585 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1586 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1587 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1588 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1589 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1590 | |
| 1591 | CurMBB->addSuccessor(B.TargetBB); |
| 1592 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1593 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1594 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1595 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1596 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1599 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1600 | MachineBasicBlock *NextBlock = 0; |
| 1601 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1602 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1603 | NextBlock = BBI; |
| 1604 | |
| 1605 | if (NextMBB == NextBlock) |
| 1606 | DAG.setRoot(BrAnd); |
| 1607 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1608 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1609 | DAG.getBasicBlock(NextMBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1613 | // Retrieve successors. |
| 1614 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1615 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1616 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1617 | const Value *Callee(I.getCalledValue()); |
| 1618 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1619 | visitInlineAsm(&I); |
| 1620 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1621 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1622 | |
| 1623 | // If the value of the invoke is used outside of its defining block, make it |
| 1624 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1625 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Update successor info |
| 1628 | CurMBB->addSuccessor(Return); |
| 1629 | CurMBB->addSuccessor(LandingPad); |
| 1630 | |
| 1631 | // Drop into normal successor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1632 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1633 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1634 | DAG.getBasicBlock(Return))); |
| 1635 | } |
| 1636 | |
| 1637 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1638 | } |
| 1639 | |
| 1640 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1641 | /// small case ranges). |
| 1642 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
| 1643 | CaseRecVector& WorkList, |
| 1644 | Value* SV, |
| 1645 | MachineBasicBlock* Default) { |
| 1646 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1647 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1648 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1649 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1650 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1651 | return false; |
| 1652 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1653 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1654 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1655 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1656 | |
| 1657 | // Figure out which block is immediately after the current one. |
| 1658 | MachineBasicBlock *NextBlock = 0; |
| 1659 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1660 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1661 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1662 | NextBlock = BBI; |
| 1663 | |
| 1664 | // TODO: If any two of the cases has the same destination, and if one value |
| 1665 | // is the same as the other, but has one bit unset that the other has set, |
| 1666 | // use bit manipulation to do two compares at once. For example: |
| 1667 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1669 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1670 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1671 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1672 | // branches in this order. See if rearranging a case value would help. |
| 1673 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1674 | if (I->BB == NextBlock) { |
| 1675 | std::swap(*I, BackCase); |
| 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1680 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1681 | // Create a CaseBlock record representing a conditional branch to |
| 1682 | // the Case's target mbb if the value being switched on SV is equal |
| 1683 | // to C. |
| 1684 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1685 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1686 | MachineBasicBlock *FallThrough; |
| 1687 | if (I != E-1) { |
| 1688 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1689 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1690 | |
| 1691 | // Put SV in a virtual register to make it available from the new blocks. |
| 1692 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1693 | } else { |
| 1694 | // If the last case doesn't match, go to the default block. |
| 1695 | FallThrough = Default; |
| 1696 | } |
| 1697 | |
| 1698 | Value *RHS, *LHS, *MHS; |
| 1699 | ISD::CondCode CC; |
| 1700 | if (I->High == I->Low) { |
| 1701 | // This is just small small case range :) containing exactly 1 case |
| 1702 | CC = ISD::SETEQ; |
| 1703 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1704 | } else { |
| 1705 | CC = ISD::SETLE; |
| 1706 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1707 | } |
| 1708 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1709 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1710 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1711 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1712 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1713 | // before the next MBB. |
| 1714 | if (CurBlock == CurMBB) |
| 1715 | visitSwitchCase(CB); |
| 1716 | else |
| 1717 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1718 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1719 | CurBlock = FallThrough; |
| 1720 | } |
| 1721 | |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
| 1725 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1726 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1727 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1728 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1729 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1730 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1731 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1732 | APInt LastExt(Last), FirstExt(First); |
| 1733 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1734 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1735 | return (LastExt - FirstExt + 1ULL); |
| 1736 | } |
| 1737 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1738 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1739 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
| 1740 | CaseRecVector& WorkList, |
| 1741 | Value* SV, |
| 1742 | MachineBasicBlock* Default) { |
| 1743 | Case& FrontCase = *CR.Range.first; |
| 1744 | Case& BackCase = *(CR.Range.second-1); |
| 1745 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1746 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1747 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1748 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1749 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1750 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1751 | I!=E; ++I) |
| 1752 | TSize += I->size(); |
| 1753 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1754 | if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4))) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1755 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1756 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1757 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1758 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1759 | if (Density < 0.4) |
| 1760 | return false; |
| 1761 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1762 | DEBUG(errs() << "Lowering jump table\n" |
| 1763 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1764 | << "Range: " << Range |
| 1765 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1766 | |
| 1767 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1768 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1769 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1772 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1773 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1774 | |
| 1775 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1776 | |
| 1777 | // Create a new basic block to hold the code for loading the address |
| 1778 | // of the jump table, and jumping to it. Update successor information; |
| 1779 | // we will either branch to the default case for the switch, or the jump |
| 1780 | // table. |
| 1781 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1782 | CurMF->insert(BBI, JumpTableBB); |
| 1783 | CR.CaseBB->addSuccessor(Default); |
| 1784 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1785 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1786 | // Build a vector of destination BBs, corresponding to each target |
| 1787 | // of the jump table. If the value of the jump table slot corresponds to |
| 1788 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1789 | // the default BB. |
| 1790 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1791 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1792 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1793 | const APInt& Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1794 | const APInt& High = cast<ConstantInt>(I->High)->getValue(); |
| 1795 | |
| 1796 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1797 | DestBBs.push_back(I->BB); |
| 1798 | if (TEI==High) |
| 1799 | ++I; |
| 1800 | } else { |
| 1801 | DestBBs.push_back(Default); |
| 1802 | } |
| 1803 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1804 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1805 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1806 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1807 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1808 | E = DestBBs.end(); I != E; ++I) { |
| 1809 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1810 | SuccsHandled[(*I)->getNumber()] = true; |
| 1811 | JumpTableBB->addSuccessor(*I); |
| 1812 | } |
| 1813 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1814 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1815 | // Create a jump table index for this jump table, or return an existing |
| 1816 | // one. |
| 1817 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1818 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1819 | // Set the jump table information so that we can codegen it as a second |
| 1820 | // MachineBasicBlock |
| 1821 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1822 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1823 | if (CR.CaseBB == CurMBB) |
| 1824 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1825 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1826 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1827 | |
| 1828 | return true; |
| 1829 | } |
| 1830 | |
| 1831 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1832 | /// 2 subtrees. |
| 1833 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
| 1834 | CaseRecVector& WorkList, |
| 1835 | Value* SV, |
| 1836 | MachineBasicBlock* Default) { |
| 1837 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1838 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1839 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1840 | |
| 1841 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1842 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1843 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1844 | |
| 1845 | Case& FrontCase = *CR.Range.first; |
| 1846 | Case& BackCase = *(CR.Range.second-1); |
| 1847 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1848 | |
| 1849 | // Size is the number of Cases represented by this range. |
| 1850 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1851 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1852 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1853 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1854 | double FMetric = 0; |
| 1855 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1856 | |
| 1857 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1858 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1859 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1860 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1861 | I!=E; ++I) |
| 1862 | TSize += I->size(); |
| 1863 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1864 | APInt LSize = FrontCase.size(); |
| 1865 | APInt RSize = TSize-LSize; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1866 | DEBUG(errs() << "Selecting best pivot: \n" |
| 1867 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1868 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1869 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1870 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1871 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1872 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1873 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1874 | assert((Range - 2ULL).isNonNegative() && |
| 1875 | "Invalid case distance"); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1876 | double LDensity = (double)LSize.roundToDouble() / |
| 1877 | (LEnd - First + 1ULL).roundToDouble(); |
| 1878 | double RDensity = (double)RSize.roundToDouble() / |
| 1879 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1880 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1881 | // Should always split in some non-trivial place |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1882 | DEBUG(errs() <<"=>Step\n" |
| 1883 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1884 | << "LDensity: " << LDensity |
| 1885 | << ", RDensity: " << RDensity << '\n' |
| 1886 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1887 | if (FMetric < Metric) { |
| 1888 | Pivot = J; |
| 1889 | FMetric = Metric; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1890 | DEBUG(errs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | LSize += J->size(); |
| 1894 | RSize -= J->size(); |
| 1895 | } |
| 1896 | if (areJTsAllowed(TLI)) { |
| 1897 | // If our case is dense we *really* should handle it earlier! |
| 1898 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1899 | } else { |
| 1900 | Pivot = CR.Range.first + Size/2; |
| 1901 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1902 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1903 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1904 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1905 | Constant *C = Pivot->Low; |
| 1906 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1907 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1908 | // We know that we branch to the LHS if the Value being switched on is |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1909 | // less than the Pivot value, C. We use this to optimize our binary |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1910 | // tree a bit, by recognizing that if SV is greater than or equal to the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1911 | // LHS's Case Value, and that Case Value is exactly one less than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1912 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1913 | // rather than creating a leaf node for it. |
| 1914 | if ((LHSR.second - LHSR.first) == 1 && |
| 1915 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1916 | cast<ConstantInt>(C)->getValue() == |
| 1917 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1918 | TrueBB = LHSR.first->BB; |
| 1919 | } else { |
| 1920 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1921 | CurMF->insert(BBI, TrueBB); |
| 1922 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Put SV in a virtual register to make it available from the new blocks. |
| 1925 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1926 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1928 | // Similar to the optimization above, if the Value being switched on is |
| 1929 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1930 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1931 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1932 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1933 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1934 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1935 | FalseBB = RHSR.first->BB; |
| 1936 | } else { |
| 1937 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1938 | CurMF->insert(BBI, FalseBB); |
| 1939 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Put SV in a virtual register to make it available from the new blocks. |
| 1942 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1946 | // the LHS node if the value being switched on SV is less than C. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1947 | // Otherwise, branch to LHS. |
| 1948 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1949 | |
| 1950 | if (CR.CaseBB == CurMBB) |
| 1951 | visitSwitchCase(CB); |
| 1952 | else |
| 1953 | SwitchCases.push_back(CB); |
| 1954 | |
| 1955 | return true; |
| 1956 | } |
| 1957 | |
| 1958 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1959 | /// range span less, than machine word bitwidth, encode case range into series |
| 1960 | /// of masks and emit bit tests with these masks. |
| 1961 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1962 | CaseRecVector& WorkList, |
| 1963 | Value* SV, |
| 1964 | MachineBasicBlock* Default){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1965 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1966 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1967 | |
| 1968 | Case& FrontCase = *CR.Range.first; |
| 1969 | Case& BackCase = *(CR.Range.second-1); |
| 1970 | |
| 1971 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1972 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1973 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1974 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1975 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1976 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1977 | return false; |
| 1978 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1979 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1980 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1981 | I!=E; ++I) { |
| 1982 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1983 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1984 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1985 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1986 | // Count unique destinations |
| 1987 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1988 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1989 | Dests.insert(I->BB); |
| 1990 | if (Dests.size() > 3) |
| 1991 | // Don't bother the code below, if there are too much unique destinations |
| 1992 | return false; |
| 1993 | } |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1994 | DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n' |
| 1995 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1996 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1997 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1998 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1999 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2000 | APInt cmpRange = maxValue - minValue; |
| 2001 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2002 | DEBUG(errs() << "Compare range: " << cmpRange << '\n' |
| 2003 | << "Low bound: " << minValue << '\n' |
| 2004 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2005 | |
| 2006 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2007 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2008 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2009 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2010 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2011 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2012 | DEBUG(errs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2013 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2014 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2015 | // Optimize the case where all the case values fit in a |
| 2016 | // word without having to subtract minValue. In this case, |
| 2017 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2018 | if (minValue.isNonNegative() && |
| 2019 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 2020 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2021 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2022 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2023 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2024 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2025 | CaseBitsVector CasesBits; |
| 2026 | unsigned i, count = 0; |
| 2027 | |
| 2028 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2029 | MachineBasicBlock* Dest = I->BB; |
| 2030 | for (i = 0; i < count; ++i) |
| 2031 | if (Dest == CasesBits[i].BB) |
| 2032 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2033 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2034 | if (i == count) { |
| 2035 | assert((count < 3) && "Too much destinations to test!"); |
| 2036 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 2037 | count++; |
| 2038 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2039 | |
| 2040 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2041 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2042 | |
| 2043 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2044 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 2045 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2046 | for (uint64_t j = lo; j <= hi; j++) { |
| 2047 | CasesBits[i].Mask |= 1ULL << j; |
| 2048 | CasesBits[i].Bits++; |
| 2049 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2050 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2051 | } |
| 2052 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2053 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2054 | BitTestInfo BTC; |
| 2055 | |
| 2056 | // Figure out which block is immediately after the current one. |
| 2057 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2058 | ++BBI; |
| 2059 | |
| 2060 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2061 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2062 | DEBUG(errs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2063 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2064 | DEBUG(errs() << "Mask: " << CasesBits[i].Mask |
| 2065 | << ", Bits: " << CasesBits[i].Bits |
| 2066 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2067 | |
| 2068 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2069 | CurMF->insert(BBI, CaseBB); |
| 2070 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2071 | CaseBB, |
| 2072 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2073 | |
| 2074 | // Put SV in a virtual register to make it available from the new blocks. |
| 2075 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2076 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2077 | |
| 2078 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2079 | -1U, (CR.CaseBB == CurMBB), |
| 2080 | CR.CaseBB, Default, BTC); |
| 2081 | |
| 2082 | if (CR.CaseBB == CurMBB) |
| 2083 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2084 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2085 | BitTestCases.push_back(BTB); |
| 2086 | |
| 2087 | return true; |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2092 | size_t SelectionDAGLowering::Clusterify(CaseVector& Cases, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2093 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2094 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2097 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2098 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2099 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2100 | SI.getSuccessorValue(i), |
| 2101 | SMBB)); |
| 2102 | } |
| 2103 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 2104 | |
| 2105 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2106 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2107 | // Must recompute end() each iteration because it may be |
| 2108 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2109 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 2110 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2111 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2112 | MachineBasicBlock* nextBB = J->BB; |
| 2113 | MachineBasicBlock* currentBB = I->BB; |
| 2114 | |
| 2115 | // If the two neighboring cases go to the same destination, merge them |
| 2116 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2117 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2118 | I->High = J->High; |
| 2119 | J = Cases.erase(J); |
| 2120 | } else { |
| 2121 | I = J++; |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2126 | if (I->Low != I->High) |
| 2127 | // A range counts double, since it requires two compares. |
| 2128 | ++numCmps; |
| 2129 | } |
| 2130 | |
| 2131 | return numCmps; |
| 2132 | } |
| 2133 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2134 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2135 | // Figure out which block is immediately after the current one. |
| 2136 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2137 | |
| 2138 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2139 | |
| 2140 | // If there is only the default destination, branch to it if it is not the |
| 2141 | // next basic block. Otherwise, just fall through. |
| 2142 | if (SI.getNumOperands() == 2) { |
| 2143 | // Update machine-CFG edges. |
| 2144 | |
| 2145 | // If this is not a fall-through branch, emit the branch. |
| 2146 | CurMBB->addSuccessor(Default); |
| 2147 | if (Default != NextBlock) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2148 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2149 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2150 | DAG.getBasicBlock(Default))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2151 | return; |
| 2152 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2153 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2154 | // If there are any non-default case statements, create a vector of Cases |
| 2155 | // representing each one, and sort the vector so that we can efficiently |
| 2156 | // create a binary search tree from them. |
| 2157 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2158 | size_t numCmps = Clusterify(Cases, SI); |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2159 | DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size() |
| 2160 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2161 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Get the Value to be switched on and default basic blocks, which will be |
| 2164 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2165 | // search tree. |
| 2166 | Value *SV = SI.getOperand(0); |
| 2167 | |
| 2168 | // Push the initial CaseRec onto the worklist |
| 2169 | CaseRecVector WorkList; |
| 2170 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2171 | |
| 2172 | while (!WorkList.empty()) { |
| 2173 | // Grab a record representing a case range to process off the worklist |
| 2174 | CaseRec CR = WorkList.back(); |
| 2175 | WorkList.pop_back(); |
| 2176 | |
| 2177 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2178 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2180 | // If the range has few cases (two or less) emit a series of specific |
| 2181 | // tests. |
| 2182 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2183 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2184 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2185 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2186 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2187 | // lowering the switch to a binary tree of conditional branches. |
| 2188 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2189 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2190 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2191 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2192 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2193 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2194 | } |
| 2195 | } |
| 2196 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 2197 | void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) { |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2198 | // Update machine-CFG edges. |
| 2199 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
| 2200 | CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]); |
| 2201 | |
Dan Gohman | 6482515 | 2009-10-27 21:56:26 +0000 | [diff] [blame] | 2202 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2203 | MVT::Other, getControlRoot(), |
| 2204 | getValue(I.getAddress()))); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2207 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2208 | void SelectionDAGLowering::visitFSub(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2209 | // -0.0 - X --> fneg |
| 2210 | const Type *Ty = I.getType(); |
| 2211 | if (isa<VectorType>(Ty)) { |
| 2212 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2213 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2214 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2215 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2216 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2217 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2218 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2219 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2220 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2221 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2222 | return; |
| 2223 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2224 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2225 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2226 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2227 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2228 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2229 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2230 | Op2.getValueType(), Op2)); |
| 2231 | return; |
| 2232 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2233 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2234 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
| 2238 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2239 | SDValue Op2 = getValue(I.getOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2240 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2241 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2242 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2246 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2247 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2248 | if (!isa<VectorType>(I.getType()) && |
| 2249 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2250 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2251 | EVT PTy = TLI.getPointerTy(); |
| 2252 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2253 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2254 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2255 | TLI.getShiftAmountTy(), Op2); |
| 2256 | // If the operand is larger than the shift count type but the shift |
| 2257 | // count type has enough bits to represent any shift value, truncate |
| 2258 | // it now. This is a common case and it exposes the truncate to |
| 2259 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2260 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2261 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2262 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2263 | TLI.getShiftAmountTy(), Op2); |
| 2264 | // Otherwise we'll need to temporarily settle for some other |
| 2265 | // convenient type; type legalization will make adjustments as |
| 2266 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2267 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2268 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2269 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2270 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2271 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2272 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2273 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2274 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2275 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2276 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | void SelectionDAGLowering::visitICmp(User &I) { |
| 2280 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2281 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2282 | predicate = IC->getPredicate(); |
| 2283 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2284 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2285 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2286 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2287 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2288 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2289 | EVT DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2290 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | void SelectionDAGLowering::visitFCmp(User &I) { |
| 2294 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2295 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2296 | predicate = FC->getPredicate(); |
| 2297 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2298 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2299 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2300 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2301 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2302 | EVT DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2303 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2306 | void SelectionDAGLowering::visitSelect(User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2307 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2308 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2309 | unsigned NumValues = ValueVTs.size(); |
| 2310 | if (NumValues != 0) { |
| 2311 | SmallVector<SDValue, 4> Values(NumValues); |
| 2312 | SDValue Cond = getValue(I.getOperand(0)); |
| 2313 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2314 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2315 | |
| 2316 | for (unsigned i = 0; i != NumValues; ++i) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2317 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2318 | TrueVal.getValueType(), Cond, |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2319 | SDValue(TrueVal.getNode(), TrueVal.getResNo() + i), |
| 2320 | SDValue(FalseVal.getNode(), FalseVal.getResNo() + i)); |
| 2321 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2322 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2323 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2324 | &Values[0], NumValues)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2325 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | |
| 2329 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2330 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2331 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2332 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2333 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2337 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2338 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2339 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2340 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2341 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2345 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2346 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2347 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2348 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2349 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2353 | // FPTrunc is never a no-op cast, no need to check |
| 2354 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2355 | EVT DestVT = TLI.getValueType(I.getType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2356 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2357 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2360 | void SelectionDAGLowering::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2361 | // FPTrunc is never a no-op cast, no need to check |
| 2362 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2363 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2364 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2367 | void SelectionDAGLowering::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2368 | // FPToUI is never a no-op cast, no need to check |
| 2369 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2370 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2371 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2375 | // FPToSI is never a no-op cast, no need to check |
| 2376 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2377 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2378 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2381 | void SelectionDAGLowering::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2382 | // UIToFP is never a no-op cast, no need to check |
| 2383 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2384 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2385 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2388 | void SelectionDAGLowering::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2389 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2390 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2391 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2392 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2396 | // What to do depends on the size of the integer and the size of the pointer. |
| 2397 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2398 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2399 | EVT SrcVT = N.getValueType(); |
| 2400 | EVT DestVT = TLI.getValueType(I.getType()); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2401 | SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2402 | setValue(&I, Result); |
| 2403 | } |
| 2404 | |
| 2405 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2406 | // What to do depends on the size of the integer and the size of the pointer. |
| 2407 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2408 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2409 | EVT SrcVT = N.getValueType(); |
| 2410 | EVT DestVT = TLI.getValueType(I.getType()); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2411 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2414 | void SelectionDAGLowering::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2415 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2416 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2417 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2418 | // BitCast assures us that source and destination are the same size so this |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2419 | // is either a BIT_CONVERT or a no-op. |
| 2420 | if (DestVT != N.getValueType()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2421 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2422 | DestVT, N)); // convert types |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2423 | else |
| 2424 | setValue(&I, N); // noop cast. |
| 2425 | } |
| 2426 | |
| 2427 | void SelectionDAGLowering::visitInsertElement(User &I) { |
| 2428 | SDValue InVec = getValue(I.getOperand(0)); |
| 2429 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2430 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2431 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2432 | getValue(I.getOperand(2))); |
| 2433 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2434 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2435 | TLI.getValueType(I.getType()), |
| 2436 | InVec, InVal, InIdx)); |
| 2437 | } |
| 2438 | |
| 2439 | void SelectionDAGLowering::visitExtractElement(User &I) { |
| 2440 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2441 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2442 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2443 | getValue(I.getOperand(1))); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2444 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2445 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2446 | } |
| 2447 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2450 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2451 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2452 | unsigned MaskNumElts = Mask.size(); |
| 2453 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2454 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2455 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2456 | return true; |
| 2457 | } |
| 2458 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2459 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2460 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2461 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2462 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2463 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2464 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2465 | // representing undef values. |
| 2466 | SmallVector<Constant*, 8> MaskElts; |
Owen Anderson | 001dbfe | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 2467 | cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(), |
| 2468 | MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2469 | unsigned MaskNumElts = MaskElts.size(); |
| 2470 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2471 | if (isa<UndefValue>(MaskElts[i])) |
| 2472 | Mask.push_back(-1); |
| 2473 | else |
| 2474 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2475 | } |
| 2476 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2477 | EVT VT = TLI.getValueType(I.getType()); |
| 2478 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2479 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2480 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2481 | if (SrcNumElts == MaskNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2482 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2483 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2484 | return; |
| 2485 | } |
| 2486 | |
| 2487 | // Normalize the shuffle vector since mask and vector length don't match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2488 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2489 | // Mask is longer than the source vectors and is a multiple of the source |
| 2490 | // vectors. We can use concatenate vector to make the mask and vectors |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2491 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2492 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2493 | // The shuffle is concatenating two vectors together. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2494 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2495 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2496 | return; |
| 2497 | } |
| 2498 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2499 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2500 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2501 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2502 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2503 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2504 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2505 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2506 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2507 | MOps1[0] = Src1; |
| 2508 | MOps2[0] = Src2; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2509 | |
| 2510 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2511 | getCurDebugLoc(), VT, |
| 2512 | &MOps1[0], NumConcat); |
| 2513 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2514 | getCurDebugLoc(), VT, |
| 2515 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2516 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2517 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2518 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2519 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2520 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2521 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2522 | MappedOps.push_back(Idx); |
| 2523 | else |
| 2524 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2525 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2526 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2527 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2528 | return; |
| 2529 | } |
| 2530 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2531 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2532 | // Analyze the access pattern of the vector to see if we can extract |
| 2533 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2534 | // the range of elements the mask access on both vectors. |
| 2535 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2536 | int MaxRange[2] = {-1, -1}; |
| 2537 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2538 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2539 | int Idx = Mask[i]; |
| 2540 | int Input = 0; |
| 2541 | if (Idx < 0) |
| 2542 | continue; |
| 2543 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2544 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2545 | Input = 1; |
| 2546 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2547 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2548 | if (Idx > MaxRange[Input]) |
| 2549 | MaxRange[Input] = Idx; |
| 2550 | if (Idx < MinRange[Input]) |
| 2551 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2552 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2553 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2554 | // Check if the access is smaller than the vector size and can we find |
| 2555 | // a reasonable extract index. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2556 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2557 | int StartIdx[2]; // StartIdx to extract from |
| 2558 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2559 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2560 | RangeUse[Input] = 0; // Unused |
| 2561 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2562 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2563 | // Fits within range but we should see if we can find a good |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2564 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2565 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2566 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2567 | StartIdx[Input] = 0; |
| 2568 | } else { |
| 2569 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2570 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2571 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2572 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2573 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2574 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2577 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2578 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2582 | // Extract appropriate subvector and generate a vector shuffle |
| 2583 | for (int Input=0; Input < 2; ++Input) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2584 | SDValue& Src = Input == 0 ? Src1 : Src2; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2585 | if (RangeUse[Input] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2586 | Src = DAG.getUNDEF(VT); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2587 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2588 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2589 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2590 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2591 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2592 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2593 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2594 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2595 | int Idx = Mask[i]; |
| 2596 | if (Idx < 0) |
| 2597 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2598 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2599 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2600 | else |
| 2601 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2602 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2603 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2604 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2605 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2606 | } |
| 2607 | } |
| 2608 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2609 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2610 | // replacing the shuffle with extract and build vector. |
| 2611 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2612 | EVT EltVT = VT.getVectorElementType(); |
| 2613 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2614 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2615 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2616 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2617 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2618 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2619 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2620 | if (Idx < (int)SrcNumElts) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2621 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2622 | EltVT, Src1, DAG.getConstant(Idx, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2623 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2624 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2625 | EltVT, Src2, |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2626 | DAG.getConstant(Idx - SrcNumElts, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2629 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2630 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) { |
| 2634 | const Value *Op0 = I.getOperand(0); |
| 2635 | const Value *Op1 = I.getOperand(1); |
| 2636 | const Type *AggTy = I.getType(); |
| 2637 | const Type *ValTy = Op1->getType(); |
| 2638 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2639 | bool FromUndef = isa<UndefValue>(Op1); |
| 2640 | |
| 2641 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2642 | I.idx_begin(), I.idx_end()); |
| 2643 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2644 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2645 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2646 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2647 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2648 | |
| 2649 | unsigned NumAggValues = AggValueVTs.size(); |
| 2650 | unsigned NumValValues = ValValueVTs.size(); |
| 2651 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2652 | |
| 2653 | SDValue Agg = getValue(Op0); |
| 2654 | SDValue Val = getValue(Op1); |
| 2655 | unsigned i = 0; |
| 2656 | // Copy the beginning value(s) from the original aggregate. |
| 2657 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2658 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2659 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2660 | // Copy values from the inserted value(s). |
| 2661 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2662 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2663 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2664 | // Copy remaining value(s) from the original aggregate. |
| 2665 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2666 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2667 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2668 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2669 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2670 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2671 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) { |
| 2675 | const Value *Op0 = I.getOperand(0); |
| 2676 | const Type *AggTy = Op0->getType(); |
| 2677 | const Type *ValTy = I.getType(); |
| 2678 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2679 | |
| 2680 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2681 | I.idx_begin(), I.idx_end()); |
| 2682 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2683 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2684 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2685 | |
| 2686 | unsigned NumValValues = ValValueVTs.size(); |
| 2687 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2688 | |
| 2689 | SDValue Agg = getValue(Op0); |
| 2690 | // Copy out the selected value(s). |
| 2691 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2692 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2693 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2694 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2695 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2696 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2697 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2698 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2699 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | |
| 2703 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2704 | SDValue N = getValue(I.getOperand(0)); |
| 2705 | const Type *Ty = I.getOperand(0)->getType(); |
| 2706 | |
| 2707 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2708 | OI != E; ++OI) { |
| 2709 | Value *Idx = *OI; |
| 2710 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2711 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2712 | if (Field) { |
| 2713 | // N = N + Offset |
| 2714 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2715 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2716 | DAG.getIntPtrConstant(Offset)); |
| 2717 | } |
| 2718 | Ty = StTy->getElementType(Field); |
| 2719 | } else { |
| 2720 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2721 | |
| 2722 | // If this is a constant subscript, handle it quickly. |
| 2723 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2724 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2725 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2726 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2727 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2728 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2729 | unsigned PtrBits = PTy.getSizeInBits(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2730 | if (PtrBits < 64) { |
| 2731 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2732 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2733 | DAG.getConstant(Offs, MVT::i64)); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2734 | } else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2735 | OffsVal = DAG.getIntPtrConstant(Offs); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2736 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2737 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2738 | continue; |
| 2739 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2740 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2741 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2742 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2743 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2744 | SDValue IdxN = getValue(Idx); |
| 2745 | |
| 2746 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2747 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2748 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2749 | |
| 2750 | // If this is a multiply by a power of two, turn it into a shl |
| 2751 | // immediately. This is a very common case. |
| 2752 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2753 | if (ElementSize.isPowerOf2()) { |
| 2754 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2755 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2756 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2757 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2758 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2759 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2760 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2761 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2765 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2766 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | setValue(&I, N); |
| 2770 | } |
| 2771 | |
| 2772 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2773 | // If this is a fixed sized alloca in the entry block of the function, |
| 2774 | // allocate it statically on the stack. |
| 2775 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2776 | return; // getValue will auto-populate this. |
| 2777 | |
| 2778 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2779 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2780 | unsigned Align = |
| 2781 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2782 | I.getAlignment()); |
| 2783 | |
| 2784 | SDValue AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2785 | |
| 2786 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2787 | AllocSize, |
| 2788 | DAG.getConstant(TySize, AllocSize.getValueType())); |
| 2789 | |
| 2790 | |
| 2791 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2792 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2793 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2794 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2795 | // Handle alignment. If the requested alignment is less than or equal to |
| 2796 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2797 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2798 | unsigned StackAlign = |
| 2799 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2800 | if (Align <= StackAlign) |
| 2801 | Align = 0; |
| 2802 | |
| 2803 | // Round the size of the allocation up to the stack alignment size |
| 2804 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2805 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2806 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2807 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2808 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2809 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2810 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2812 | |
| 2813 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2814 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2815 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2816 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2817 | setValue(&I, DSA); |
| 2818 | DAG.setRoot(DSA.getValue(1)); |
| 2819 | |
| 2820 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2821 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2822 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2826 | const Value *SV = I.getOperand(0); |
| 2827 | SDValue Ptr = getValue(SV); |
| 2828 | |
| 2829 | const Type *Ty = I.getType(); |
| 2830 | bool isVolatile = I.isVolatile(); |
| 2831 | unsigned Alignment = I.getAlignment(); |
| 2832 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2833 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2834 | SmallVector<uint64_t, 4> Offsets; |
| 2835 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2836 | unsigned NumValues = ValueVTs.size(); |
| 2837 | if (NumValues == 0) |
| 2838 | return; |
| 2839 | |
| 2840 | SDValue Root; |
| 2841 | bool ConstantMemory = false; |
| 2842 | if (I.isVolatile()) |
| 2843 | // Serialize volatile loads with other side effects. |
| 2844 | Root = getRoot(); |
| 2845 | else if (AA->pointsToConstantMemory(SV)) { |
| 2846 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2847 | Root = DAG.getEntryNode(); |
| 2848 | ConstantMemory = true; |
| 2849 | } else { |
| 2850 | // Do not serialize non-volatile loads against each other. |
| 2851 | Root = DAG.getRoot(); |
| 2852 | } |
| 2853 | |
| 2854 | SmallVector<SDValue, 4> Values(NumValues); |
| 2855 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2856 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2857 | for (unsigned i = 0; i != NumValues; ++i) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2858 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Nate Begeman | e679837 | 2009-09-15 00:13:12 +0000 | [diff] [blame] | 2859 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2860 | PtrVT, Ptr, |
| 2861 | DAG.getConstant(Offsets[i], PtrVT)), |
Nate Begeman | 101b25c | 2009-09-15 19:05:41 +0000 | [diff] [blame] | 2862 | SV, Offsets[i], isVolatile, Alignment); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2863 | Values[i] = L; |
| 2864 | Chains[i] = L.getValue(1); |
| 2865 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2866 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2867 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2868 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2869 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2870 | &Chains[0], NumValues); |
| 2871 | if (isVolatile) |
| 2872 | DAG.setRoot(Chain); |
| 2873 | else |
| 2874 | PendingLoads.push_back(Chain); |
| 2875 | } |
| 2876 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2877 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2878 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2879 | &Values[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | |
| 2883 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2884 | Value *SrcV = I.getOperand(0); |
| 2885 | Value *PtrV = I.getOperand(1); |
| 2886 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2887 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2888 | SmallVector<uint64_t, 4> Offsets; |
| 2889 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2890 | unsigned NumValues = ValueVTs.size(); |
| 2891 | if (NumValues == 0) |
| 2892 | return; |
| 2893 | |
| 2894 | // Get the lowered operands. Note that we do this after |
| 2895 | // checking if NumResults is zero, because with zero results |
| 2896 | // the operands won't have values in the map. |
| 2897 | SDValue Src = getValue(SrcV); |
| 2898 | SDValue Ptr = getValue(PtrV); |
| 2899 | |
| 2900 | SDValue Root = getRoot(); |
| 2901 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2902 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2903 | bool isVolatile = I.isVolatile(); |
| 2904 | unsigned Alignment = I.getAlignment(); |
| 2905 | for (unsigned i = 0; i != NumValues; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2906 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2907 | SDValue(Src.getNode(), Src.getResNo() + i), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2908 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2909 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2910 | DAG.getConstant(Offsets[i], PtrVT)), |
Nate Begeman | 101b25c | 2009-09-15 19:05:41 +0000 | [diff] [blame] | 2911 | PtrV, Offsets[i], isVolatile, Alignment); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2912 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2913 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2914 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2918 | /// node. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2919 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2920 | unsigned Intrinsic) { |
| 2921 | bool HasChain = !I.doesNotAccessMemory(); |
| 2922 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2923 | |
| 2924 | // Build the operand list. |
| 2925 | SmallVector<SDValue, 8> Ops; |
| 2926 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2927 | if (OnlyLoad) { |
| 2928 | // We don't need to serialize loads against other loads. |
| 2929 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2930 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2931 | Ops.push_back(getRoot()); |
| 2932 | } |
| 2933 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2934 | |
| 2935 | // Info is set by getTgtMemInstrinsic |
| 2936 | TargetLowering::IntrinsicInfo Info; |
| 2937 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2938 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2939 | // Add the intrinsic ID as an integer operand if it's not a target intrinsic. |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2940 | if (!IsTgtIntrinsic) |
| 2941 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2942 | |
| 2943 | // Add all operands of the call to the operand list. |
| 2944 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2945 | SDValue Op = getValue(I.getOperand(i)); |
| 2946 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2947 | "Intrinsic uses a non-legal type?"); |
| 2948 | Ops.push_back(Op); |
| 2949 | } |
| 2950 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2951 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2952 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2953 | #ifndef NDEBUG |
| 2954 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2955 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2956 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2957 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2958 | #endif // NDEBUG |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2959 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2960 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2961 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2962 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2963 | |
| 2964 | // Create the node. |
| 2965 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2966 | if (IsTgtIntrinsic) { |
| 2967 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2968 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2969 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2970 | Info.memVT, Info.ptrVal, Info.offset, |
| 2971 | Info.align, Info.vol, |
| 2972 | Info.readMem, Info.writeMem); |
| 2973 | } |
| 2974 | else if (!HasChain) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2975 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2976 | VTs, &Ops[0], Ops.size()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2977 | else if (I.getType() != Type::getVoidTy(*DAG.getContext())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2978 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2979 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2980 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2981 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2982 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2983 | |
| 2984 | if (HasChain) { |
| 2985 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2986 | if (OnlyLoad) |
| 2987 | PendingLoads.push_back(Chain); |
| 2988 | else |
| 2989 | DAG.setRoot(Chain); |
| 2990 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2991 | if (I.getType() != Type::getVoidTy(*DAG.getContext())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2992 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2993 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2994 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2995 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2996 | setValue(&I, Result); |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 3001 | static GlobalVariable *ExtractTypeInfo(Value *V) { |
| 3002 | V = V->stripPointerCasts(); |
| 3003 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 3004 | assert ((GV || isa<ConstantPointerNull>(V)) && |
| 3005 | "TypeInfo must be a global variable or NULL"); |
| 3006 | return GV; |
| 3007 | } |
| 3008 | |
| 3009 | namespace llvm { |
| 3010 | |
| 3011 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 3012 | /// call, and add them to the specified machine basic block. |
| 3013 | void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 3014 | MachineBasicBlock *MBB) { |
| 3015 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 3016 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 3017 | assert(CE->getOpcode() == Instruction::BitCast && |
| 3018 | isa<Function>(CE->getOperand(0)) && |
| 3019 | "Personality should be a function"); |
| 3020 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 3021 | |
| 3022 | // Gather all the type infos for this landing pad and pass them along to |
| 3023 | // MachineModuleInfo. |
| 3024 | std::vector<GlobalVariable *> TyInfo; |
| 3025 | unsigned N = I.getNumOperands(); |
| 3026 | |
| 3027 | for (unsigned i = N - 1; i > 2; --i) { |
| 3028 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 3029 | unsigned FilterLength = CI->getZExtValue(); |
| 3030 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
| 3031 | assert (FirstCatch <= N && "Invalid filter length"); |
| 3032 | |
| 3033 | if (FirstCatch < N) { |
| 3034 | TyInfo.reserve(N - FirstCatch); |
| 3035 | for (unsigned j = FirstCatch; j < N; ++j) |
| 3036 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3037 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3038 | TyInfo.clear(); |
| 3039 | } |
| 3040 | |
| 3041 | if (!FilterLength) { |
| 3042 | // Cleanup. |
| 3043 | MMI->addCleanup(MBB); |
| 3044 | } else { |
| 3045 | // Filter. |
| 3046 | TyInfo.reserve(FilterLength - 1); |
| 3047 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 3048 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3049 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 3050 | TyInfo.clear(); |
| 3051 | } |
| 3052 | |
| 3053 | N = i; |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | if (N > 3) { |
| 3058 | TyInfo.reserve(N - 3); |
| 3059 | for (unsigned j = 3; j < N; ++j) |
| 3060 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3061 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | } |
| 3066 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3067 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3068 | /// number with exponent of 1: |
| 3069 | /// |
| 3070 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3071 | /// |
| 3072 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3073 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3074 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3075 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3076 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3077 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3078 | DAG.getConstant(0x3f800000, MVT::i32)); |
| 3079 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3082 | /// GetExponent - Get the exponent: |
| 3083 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3084 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3085 | /// |
| 3086 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3087 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3088 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
| 3089 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3090 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3091 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3092 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3093 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3094 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3095 | DAG.getConstant(127, MVT::i32)); |
| 3096 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3099 | /// getF32Constant - Get 32-bit floating point constant. |
| 3100 | static SDValue |
| 3101 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3102 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3105 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3106 | /// visitIntrinsicCall: I is a call instruction |
| 3107 | /// Op is the associated NodeType for I |
| 3108 | const char * |
| 3109 | SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3110 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3111 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3112 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3113 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3114 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3115 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3116 | getValue(I.getOperand(2)), |
| 3117 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3118 | setValue(&I, L); |
| 3119 | DAG.setRoot(L.getValue(1)); |
| 3120 | return 0; |
| 3121 | } |
| 3122 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3123 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3124 | const char * |
| 3125 | SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3126 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3127 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3128 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3129 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3130 | SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3131 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3132 | setValue(&I, Result); |
| 3133 | return 0; |
| 3134 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3135 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3136 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 3137 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3138 | void |
| 3139 | SelectionDAGLowering::visitExp(CallInst &I) { |
| 3140 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3141 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3142 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3143 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3144 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3145 | SDValue Op = getValue(I.getOperand(1)); |
| 3146 | |
| 3147 | // Put the exponent in the right bit position for later addition to the |
| 3148 | // final result: |
| 3149 | // |
| 3150 | // #define LOG2OFe 1.4426950f |
| 3151 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3152 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3153 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3154 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3155 | |
| 3156 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3157 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3158 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3159 | |
| 3160 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3161 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3162 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (LimitFloatPrecision <= 6) { |
| 3165 | // For floating-point precision of 6: |
| 3166 | // |
| 3167 | // TwoToFractionalPartOfX = |
| 3168 | // 0.997535578f + |
| 3169 | // (0.735607626f + 0.252464424f * x) * x; |
| 3170 | // |
| 3171 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3172 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3173 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3174 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3175 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3176 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3177 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3178 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3179 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3180 | |
| 3181 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3182 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3183 | TwoToFracPartOfX, IntegerPartOfX); |
| 3184 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3185 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3186 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3187 | // For floating-point precision of 12: |
| 3188 | // |
| 3189 | // TwoToFractionalPartOfX = |
| 3190 | // 0.999892986f + |
| 3191 | // (0.696457318f + |
| 3192 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3193 | // |
| 3194 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3195 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3196 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3197 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3198 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3199 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3200 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3201 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3202 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3203 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3204 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3205 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3206 | |
| 3207 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3208 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3209 | TwoToFracPartOfX, IntegerPartOfX); |
| 3210 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3211 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3212 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3213 | // For floating-point precision of 18: |
| 3214 | // |
| 3215 | // TwoToFractionalPartOfX = |
| 3216 | // 0.999999982f + |
| 3217 | // (0.693148872f + |
| 3218 | // (0.240227044f + |
| 3219 | // (0.554906021e-1f + |
| 3220 | // (0.961591928e-2f + |
| 3221 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3222 | // |
| 3223 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3224 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3225 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3226 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3227 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3228 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3229 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3230 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3231 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3232 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3233 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3234 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3235 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3236 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3237 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3238 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3239 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3240 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3241 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3242 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3243 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3244 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3247 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3248 | TwoToFracPartOfX, IntegerPartOfX); |
| 3249 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3250 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3251 | } |
| 3252 | } else { |
| 3253 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3254 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3255 | getValue(I.getOperand(1)).getValueType(), |
| 3256 | getValue(I.getOperand(1))); |
| 3257 | } |
| 3258 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3259 | setValue(&I, result); |
| 3260 | } |
| 3261 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3262 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3263 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3264 | void |
| 3265 | SelectionDAGLowering::visitLog(CallInst &I) { |
| 3266 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3267 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3268 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3269 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3270 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3271 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3272 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3273 | |
| 3274 | // Scale the exponent by log(2) [0.69314718f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3275 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3276 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3277 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3278 | |
| 3279 | // Get the significand and build it into a floating-point number with |
| 3280 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3281 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3282 | |
| 3283 | if (LimitFloatPrecision <= 6) { |
| 3284 | // For floating-point precision of 6: |
| 3285 | // |
| 3286 | // LogofMantissa = |
| 3287 | // -1.1609546f + |
| 3288 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3289 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3290 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3291 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3292 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3293 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3294 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3295 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3296 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3297 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3298 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3299 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3300 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3301 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3302 | // For floating-point precision of 12: |
| 3303 | // |
| 3304 | // LogOfMantissa = |
| 3305 | // -1.7417939f + |
| 3306 | // (2.8212026f + |
| 3307 | // (-1.4699568f + |
| 3308 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3309 | // |
| 3310 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3311 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3312 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3313 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3314 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3315 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3316 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3317 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3318 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3319 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3320 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3321 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3322 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3323 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3324 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3325 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3326 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3327 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3328 | // For floating-point precision of 18: |
| 3329 | // |
| 3330 | // LogOfMantissa = |
| 3331 | // -2.1072184f + |
| 3332 | // (4.2372794f + |
| 3333 | // (-3.7029485f + |
| 3334 | // (2.2781945f + |
| 3335 | // (-0.87823314f + |
| 3336 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3337 | // |
| 3338 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3339 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3340 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3341 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3342 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3343 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3344 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3345 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3346 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3347 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3348 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3349 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3350 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3351 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3352 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3353 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3354 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3355 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3356 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3357 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3358 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3359 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3360 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } else { |
| 3363 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3364 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3365 | getValue(I.getOperand(1)).getValueType(), |
| 3366 | getValue(I.getOperand(1))); |
| 3367 | } |
| 3368 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3369 | setValue(&I, result); |
| 3370 | } |
| 3371 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3372 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3373 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3374 | void |
| 3375 | SelectionDAGLowering::visitLog2(CallInst &I) { |
| 3376 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3377 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3378 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3379 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3380 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3381 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3382 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3383 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3384 | // Get the exponent. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3385 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3386 | |
| 3387 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3388 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3389 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3390 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3391 | // Different possible minimax approximations of significand in |
| 3392 | // floating-point for various degrees of accuracy over [1,2]. |
| 3393 | if (LimitFloatPrecision <= 6) { |
| 3394 | // For floating-point precision of 6: |
| 3395 | // |
| 3396 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3397 | // |
| 3398 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3399 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3400 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3401 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3402 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3403 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3404 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3405 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3406 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3407 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3408 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3409 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3410 | // For floating-point precision of 12: |
| 3411 | // |
| 3412 | // Log2ofMantissa = |
| 3413 | // -2.51285454f + |
| 3414 | // (4.07009056f + |
| 3415 | // (-2.12067489f + |
| 3416 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3417 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3418 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3419 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3420 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3421 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3422 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3423 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3424 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3425 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3426 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3427 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3428 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3429 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3430 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3431 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3432 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3433 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3434 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3435 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3436 | // For floating-point precision of 18: |
| 3437 | // |
| 3438 | // Log2ofMantissa = |
| 3439 | // -3.0400495f + |
| 3440 | // (6.1129976f + |
| 3441 | // (-5.3420409f + |
| 3442 | // (3.2865683f + |
| 3443 | // (-1.2669343f + |
| 3444 | // (0.27515199f - |
| 3445 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3446 | // |
| 3447 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3448 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3449 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3450 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3451 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3452 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3453 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3454 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3455 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3456 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3457 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3458 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3459 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3460 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3461 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3462 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3463 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3464 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3465 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3466 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3467 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3468 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3469 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3470 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3471 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3472 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3473 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3474 | getValue(I.getOperand(1)).getValueType(), |
| 3475 | getValue(I.getOperand(1))); |
| 3476 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3477 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3478 | setValue(&I, result); |
| 3479 | } |
| 3480 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3481 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3482 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3483 | void |
| 3484 | SelectionDAGLowering::visitLog10(CallInst &I) { |
| 3485 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3486 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3487 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3488 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3489 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3490 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3491 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3492 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3493 | // Scale the exponent by log10(2) [0.30102999f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3494 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3495 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3496 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3497 | |
| 3498 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3499 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3500 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3501 | |
| 3502 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3503 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3504 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3505 | // Log10ofMantissa = |
| 3506 | // -0.50419619f + |
| 3507 | // (0.60948995f - 0.10380950f * x) * x; |
| 3508 | // |
| 3509 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3510 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3511 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3512 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3513 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3514 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3515 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3516 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3517 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3518 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3519 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3520 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3521 | // For floating-point precision of 12: |
| 3522 | // |
| 3523 | // Log10ofMantissa = |
| 3524 | // -0.64831180f + |
| 3525 | // (0.91751397f + |
| 3526 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3527 | // |
| 3528 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3529 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3530 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3531 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3532 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3533 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3534 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3535 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3536 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3537 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3538 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3539 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3540 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3541 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3542 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3543 | // For floating-point precision of 18: |
| 3544 | // |
| 3545 | // Log10ofMantissa = |
| 3546 | // -0.84299375f + |
| 3547 | // (1.5327582f + |
| 3548 | // (-1.0688956f + |
| 3549 | // (0.49102474f + |
| 3550 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3551 | // |
| 3552 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3553 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3554 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3555 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3556 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3557 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3558 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3559 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3560 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3561 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3562 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3563 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3564 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3565 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3566 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3567 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3568 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3569 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3570 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3571 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3572 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3573 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3574 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3575 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3576 | getValue(I.getOperand(1)).getValueType(), |
| 3577 | getValue(I.getOperand(1))); |
| 3578 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3579 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3580 | setValue(&I, result); |
| 3581 | } |
| 3582 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3583 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3584 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3585 | void |
| 3586 | SelectionDAGLowering::visitExp2(CallInst &I) { |
| 3587 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3588 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3589 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3590 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3591 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3592 | SDValue Op = getValue(I.getOperand(1)); |
| 3593 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3594 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3595 | |
| 3596 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3597 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3598 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3599 | |
| 3600 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3601 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3602 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3603 | |
| 3604 | if (LimitFloatPrecision <= 6) { |
| 3605 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3606 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3607 | // TwoToFractionalPartOfX = |
| 3608 | // 0.997535578f + |
| 3609 | // (0.735607626f + 0.252464424f * x) * x; |
| 3610 | // |
| 3611 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3612 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3613 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3614 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3615 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3616 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3617 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3618 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3619 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3620 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3621 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3622 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3623 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3624 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3625 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3626 | // For floating-point precision of 12: |
| 3627 | // |
| 3628 | // TwoToFractionalPartOfX = |
| 3629 | // 0.999892986f + |
| 3630 | // (0.696457318f + |
| 3631 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3632 | // |
| 3633 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3634 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3635 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3636 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3637 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3638 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3639 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3640 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3641 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3642 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3643 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3644 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3645 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3646 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3647 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3648 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3649 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3650 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3651 | // For floating-point precision of 18: |
| 3652 | // |
| 3653 | // TwoToFractionalPartOfX = |
| 3654 | // 0.999999982f + |
| 3655 | // (0.693148872f + |
| 3656 | // (0.240227044f + |
| 3657 | // (0.554906021e-1f + |
| 3658 | // (0.961591928e-2f + |
| 3659 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3660 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3661 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3662 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3663 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3664 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3665 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3666 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3667 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3668 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3669 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3670 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3671 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3672 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3673 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3674 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3675 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3676 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3677 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3678 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3679 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3680 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3681 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3682 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3683 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3684 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3685 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3686 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3687 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3688 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3689 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3690 | getValue(I.getOperand(1)).getValueType(), |
| 3691 | getValue(I.getOperand(1))); |
| 3692 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3693 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3694 | setValue(&I, result); |
| 3695 | } |
| 3696 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3697 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3698 | /// limited-precision mode with x == 10.0f. |
| 3699 | void |
| 3700 | SelectionDAGLowering::visitPow(CallInst &I) { |
| 3701 | SDValue result; |
| 3702 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3703 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3704 | bool IsExp10 = false; |
| 3705 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3706 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3707 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3708 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3709 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3710 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3711 | APFloat Ten(10.0f); |
| 3712 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3713 | } |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3718 | SDValue Op = getValue(I.getOperand(2)); |
| 3719 | |
| 3720 | // Put the exponent in the right bit position for later addition to the |
| 3721 | // final result: |
| 3722 | // |
| 3723 | // #define LOG2OF10 3.3219281f |
| 3724 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3725 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3726 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3727 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3728 | |
| 3729 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3730 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3731 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3732 | |
| 3733 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3734 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3735 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3736 | |
| 3737 | if (LimitFloatPrecision <= 6) { |
| 3738 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3739 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3740 | // twoToFractionalPartOfX = |
| 3741 | // 0.997535578f + |
| 3742 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3743 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3744 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3745 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3746 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3747 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3748 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3749 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3750 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3751 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3752 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3753 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3754 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3755 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3756 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3757 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3758 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3759 | // For floating-point precision of 12: |
| 3760 | // |
| 3761 | // TwoToFractionalPartOfX = |
| 3762 | // 0.999892986f + |
| 3763 | // (0.696457318f + |
| 3764 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3765 | // |
| 3766 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3767 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3768 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3769 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3770 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3771 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3772 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3773 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3774 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3775 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3776 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3777 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3778 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3779 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3780 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3781 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3782 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3783 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3784 | // For floating-point precision of 18: |
| 3785 | // |
| 3786 | // TwoToFractionalPartOfX = |
| 3787 | // 0.999999982f + |
| 3788 | // (0.693148872f + |
| 3789 | // (0.240227044f + |
| 3790 | // (0.554906021e-1f + |
| 3791 | // (0.961591928e-2f + |
| 3792 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3793 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3794 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3795 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3796 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3797 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3798 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3799 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3800 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3801 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3802 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3803 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3804 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3805 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3806 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3807 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3808 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3809 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3810 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3811 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3812 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3813 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3814 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3815 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3816 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3817 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3818 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3819 | } |
| 3820 | } else { |
| 3821 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3822 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3823 | getValue(I.getOperand(1)).getValueType(), |
| 3824 | getValue(I.getOperand(1)), |
| 3825 | getValue(I.getOperand(2))); |
| 3826 | } |
| 3827 | |
| 3828 | setValue(&I, result); |
| 3829 | } |
| 3830 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3831 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3832 | /// we want to emit this as a call to a named external function, return the name |
| 3833 | /// otherwise lower it and return null. |
| 3834 | const char * |
| 3835 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3836 | DebugLoc dl = getCurDebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3837 | switch (Intrinsic) { |
| 3838 | default: |
| 3839 | // By default, turn this into a target intrinsic node. |
| 3840 | visitTargetIntrinsic(I, Intrinsic); |
| 3841 | return 0; |
| 3842 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3843 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3844 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3845 | case Intrinsic::returnaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3846 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3847 | getValue(I.getOperand(1)))); |
| 3848 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3849 | case Intrinsic::frameaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3850 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3851 | getValue(I.getOperand(1)))); |
| 3852 | return 0; |
| 3853 | case Intrinsic::setjmp: |
| 3854 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3855 | break; |
| 3856 | case Intrinsic::longjmp: |
| 3857 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3858 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3859 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3860 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3861 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3862 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3863 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3864 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3865 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3866 | return 0; |
| 3867 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3868 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3869 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3870 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3871 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3872 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3873 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3874 | I.getOperand(1), 0)); |
| 3875 | return 0; |
| 3876 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3877 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3878 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3879 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3880 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3881 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3882 | |
| 3883 | // If the source and destination are known to not be aliases, we can |
| 3884 | // lower memmove as memcpy. |
| 3885 | uint64_t Size = -1ULL; |
| 3886 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3887 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3888 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3889 | AliasAnalysis::NoAlias) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3890 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3891 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3892 | return 0; |
| 3893 | } |
| 3894 | |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3895 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3896 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | case Intrinsic::dbg_stoppoint: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3900 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3901 | if (isValidDebugInfoIntrinsic(SPI, CodeGenOpt::Default)) { |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3902 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3903 | DebugLoc Loc = ExtractDebugLocation(SPI, MF.getDebugLocInfo()); |
Chris Lattner | af29a52 | 2009-05-04 22:10:05 +0000 | [diff] [blame] | 3904 | setCurDebugLoc(Loc); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3905 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3906 | if (OptLevel == CodeGenOpt::None) |
Chris Lattner | af29a52 | 2009-05-04 22:10:05 +0000 | [diff] [blame] | 3907 | DAG.setRoot(DAG.getDbgStopPoint(Loc, getRoot(), |
Dale Johannesen | beaec4c | 2009-03-25 17:36:08 +0000 | [diff] [blame] | 3908 | SPI.getLine(), |
| 3909 | SPI.getColumn(), |
| 3910 | SPI.getContext())); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3911 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3912 | return 0; |
| 3913 | } |
| 3914 | case Intrinsic::dbg_region_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3915 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3916 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3917 | if (isValidDebugInfoIntrinsic(RSI, OptLevel) && DW |
| 3918 | && DW->ShouldEmitDwarfDebug()) { |
Bill Wendling | df7d5d3 | 2009-05-21 00:04:55 +0000 | [diff] [blame] | 3919 | unsigned LabelID = |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3920 | DW->RecordRegionStart(RSI.getContext()); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3921 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3922 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3923 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3924 | return 0; |
| 3925 | } |
| 3926 | case Intrinsic::dbg_region_end: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3927 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3928 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3929 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3930 | if (!isValidDebugInfoIntrinsic(REI, OptLevel) || !DW |
| 3931 | || !DW->ShouldEmitDwarfDebug()) |
| 3932 | return 0; |
Bill Wendling | 6c4311d | 2009-05-08 21:14:49 +0000 | [diff] [blame] | 3933 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3934 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3935 | DISubprogram Subprogram(REI.getContext()); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3936 | |
| 3937 | if (isInlinedFnEnd(REI, MF.getFunction())) { |
| 3938 | // This is end of inlined function. Debugging information for inlined |
| 3939 | // function is not handled yet (only supported by FastISel). |
| 3940 | if (OptLevel == CodeGenOpt::None) { |
| 3941 | unsigned ID = DW->RecordInlinedFnEnd(Subprogram); |
| 3942 | if (ID != 0) |
| 3943 | // Returned ID is 0 if this is unbalanced "end of inlined |
| 3944 | // scope". This could happen if optimizer eats dbg intrinsics or |
| 3945 | // "beginning of inlined scope" is not recoginized due to missing |
| 3946 | // location info. In such cases, do ignore this region.end. |
| 3947 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3948 | getRoot(), ID)); |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3949 | } |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3950 | return 0; |
| 3951 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3952 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3953 | unsigned LabelID = |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3954 | DW->RecordRegionEnd(REI.getContext()); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3955 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3956 | getRoot(), LabelID)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3957 | return 0; |
| 3958 | } |
| 3959 | case Intrinsic::dbg_func_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3960 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3961 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 3962 | if (!isValidDebugInfoIntrinsic(FSI, CodeGenOpt::None)) |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 3963 | return 0; |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3964 | |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 3965 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3966 | // This is a beginning of an inlined function. |
| 3967 | if (isInlinedFnStart(FSI, MF.getFunction())) { |
| 3968 | if (OptLevel != CodeGenOpt::None) |
| 3969 | // FIXME: Debugging informaation for inlined function is only |
| 3970 | // supported at CodeGenOpt::Node. |
| 3971 | return 0; |
| 3972 | |
Bill Wendling | c677fe5 | 2009-05-10 00:10:50 +0000 | [diff] [blame] | 3973 | DebugLoc PrevLoc = CurDebugLoc; |
Devang Patel | 07b0ec0 | 2009-07-02 00:08:09 +0000 | [diff] [blame] | 3974 | // If llvm.dbg.func.start is seen in a new block before any |
| 3975 | // llvm.dbg.stoppoint intrinsic then the location info is unknown. |
| 3976 | // FIXME : Why DebugLoc is reset at the beginning of each block ? |
| 3977 | if (PrevLoc.isUnknown()) |
| 3978 | return 0; |
Devang Patel | 07b0ec0 | 2009-07-02 00:08:09 +0000 | [diff] [blame] | 3979 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3980 | // Record the source line. |
| 3981 | setCurDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo())); |
| 3982 | |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 3983 | if (!DW || !DW->ShouldEmitDwarfDebug()) |
| 3984 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3985 | DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc); |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3986 | DISubprogram SP(FSI.getSubprogram()); |
Devang Patel | 1619dc3 | 2009-10-13 23:28:53 +0000 | [diff] [blame] | 3987 | DICompileUnit CU(PrevLocTpl.Scope); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3988 | unsigned LabelID = DW->RecordInlinedFnStart(SP, CU, |
| 3989 | PrevLocTpl.Line, |
| 3990 | PrevLocTpl.Col); |
| 3991 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3992 | getRoot(), LabelID)); |
Devang Patel | 07b0ec0 | 2009-07-02 00:08:09 +0000 | [diff] [blame] | 3993 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Devang Patel | 07b0ec0 | 2009-07-02 00:08:09 +0000 | [diff] [blame] | 3996 | // This is a beginning of a new function. |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3997 | MF.setDefaultDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo())); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 3998 | |
| 3999 | if (!DW || !DW->ShouldEmitDwarfDebug()) |
| 4000 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 4001 | // llvm.dbg.func_start also defines beginning of function scope. |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 4002 | DW->RecordRegionStart(FSI.getSubprogram()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4003 | return 0; |
| 4004 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4005 | case Intrinsic::dbg_declare: { |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 4006 | if (OptLevel != CodeGenOpt::None) |
| 4007 | // FIXME: Variable debug info is not supported here. |
| 4008 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 4009 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 4010 | if (!DW) |
| 4011 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 4012 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 4013 | if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None)) |
| 4014 | return 0; |
| 4015 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4016 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 4017 | Value *Address = DI.getAddress(); |
| 4018 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 4019 | Address = BCI->getOperand(0); |
| 4020 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 4021 | // Don't handle byval struct arguments or VLAs, for example. |
| 4022 | if (!AI) |
| 4023 | return 0; |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 4024 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4025 | FuncInfo.StaticAllocaMap.find(AI); |
| 4026 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4027 | return 0; // VLAs. |
| 4028 | int FI = SI->second; |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4029 | #ifdef ATTACH_DEBUG_INFO_TO_AN_INSN |
| 4030 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4031 | if (MMI) |
| 4032 | MMI->setVariableDbgInfo(Variable, FI); |
| 4033 | #else |
| 4034 | DW->RecordVariable(Variable, FI); |
| 4035 | #endif |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4036 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4037 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4038 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4039 | // Insert the EXCEPTIONADDR instruction. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 4040 | assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4041 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4042 | SDValue Ops[1]; |
| 4043 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4044 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4045 | setValue(&I, Op); |
| 4046 | DAG.setRoot(Op.getValue(1)); |
| 4047 | return 0; |
| 4048 | } |
| 4049 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4050 | case Intrinsic::eh_selector: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4051 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4052 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4053 | if (CurMBB->isLandingPad()) |
| 4054 | AddCatchInfo(I, MMI, CurMBB); |
| 4055 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4056 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4057 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4058 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4059 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 4060 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 4061 | if (Reg) CurMBB->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4062 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4063 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4064 | // Insert the EHSELECTION instruction. |
| 4065 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 4066 | SDValue Ops[2]; |
| 4067 | Ops[0] = getValue(I.getOperand(1)); |
| 4068 | Ops[1] = getRoot(); |
| 4069 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
| 4070 | |
| 4071 | DAG.setRoot(Op.getValue(1)); |
| 4072 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4073 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4074 | return 0; |
| 4075 | } |
| 4076 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4077 | case Intrinsic::eh_typeid_for: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4078 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4079 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4080 | if (MMI) { |
| 4081 | // Find the type id for the given typeinfo. |
| 4082 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 4083 | |
| 4084 | unsigned TypeID = MMI->getTypeIDFor(GV); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4085 | setValue(&I, DAG.getConstant(TypeID, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4086 | } else { |
| 4087 | // Return something different to eh_selector. |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4088 | setValue(&I, DAG.getConstant(1, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | return 0; |
| 4092 | } |
| 4093 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4094 | case Intrinsic::eh_return_i32: |
| 4095 | case Intrinsic::eh_return_i64: |
| 4096 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4097 | MMI->setCallsEHReturn(true); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4098 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4099 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4100 | getControlRoot(), |
| 4101 | getValue(I.getOperand(1)), |
| 4102 | getValue(I.getOperand(2)))); |
| 4103 | } else { |
| 4104 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 4105 | } |
| 4106 | |
| 4107 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4108 | case Intrinsic::eh_unwind_init: |
| 4109 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 4110 | MMI->setCallsUnwindInit(true); |
| 4111 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4112 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4113 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4114 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4115 | case Intrinsic::eh_dwarf_cfa: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4116 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4117 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 4118 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4119 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4120 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4121 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4122 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4123 | TLI.getPointerTy()), |
| 4124 | CfaArg); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4125 | setValue(&I, DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4126 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4127 | DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4128 | TLI.getPointerTy(), |
| 4129 | DAG.getConstant(0, |
| 4130 | TLI.getPointerTy())), |
| 4131 | Offset)); |
| 4132 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4133 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4134 | case Intrinsic::convertff: |
| 4135 | case Intrinsic::convertfsi: |
| 4136 | case Intrinsic::convertfui: |
| 4137 | case Intrinsic::convertsif: |
| 4138 | case Intrinsic::convertuif: |
| 4139 | case Intrinsic::convertss: |
| 4140 | case Intrinsic::convertsu: |
| 4141 | case Intrinsic::convertus: |
| 4142 | case Intrinsic::convertuu: { |
| 4143 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4144 | switch (Intrinsic) { |
| 4145 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4146 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4147 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4148 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4149 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4150 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4151 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4152 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4153 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4154 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4155 | EVT DestVT = TLI.getValueType(I.getType()); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4156 | Value* Op1 = I.getOperand(1); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4157 | setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4158 | DAG.getValueType(DestVT), |
| 4159 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4160 | getValue(I.getOperand(2)), |
| 4161 | getValue(I.getOperand(3)), |
| 4162 | Code)); |
| 4163 | return 0; |
| 4164 | } |
| 4165 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4166 | case Intrinsic::sqrt: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4167 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4168 | getValue(I.getOperand(1)).getValueType(), |
| 4169 | getValue(I.getOperand(1)))); |
| 4170 | return 0; |
| 4171 | case Intrinsic::powi: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4172 | setValue(&I, DAG.getNode(ISD::FPOWI, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4173 | getValue(I.getOperand(1)).getValueType(), |
| 4174 | getValue(I.getOperand(1)), |
| 4175 | getValue(I.getOperand(2)))); |
| 4176 | return 0; |
| 4177 | case Intrinsic::sin: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4178 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4179 | getValue(I.getOperand(1)).getValueType(), |
| 4180 | getValue(I.getOperand(1)))); |
| 4181 | return 0; |
| 4182 | case Intrinsic::cos: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4183 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4184 | getValue(I.getOperand(1)).getValueType(), |
| 4185 | getValue(I.getOperand(1)))); |
| 4186 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4187 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4188 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4189 | return 0; |
| 4190 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4191 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4192 | return 0; |
| 4193 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4194 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4195 | return 0; |
| 4196 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4197 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4198 | return 0; |
| 4199 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4200 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4201 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4202 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4203 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4204 | return 0; |
| 4205 | case Intrinsic::pcmarker: { |
| 4206 | SDValue Tmp = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4207 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4208 | return 0; |
| 4209 | } |
| 4210 | case Intrinsic::readcyclecounter: { |
| 4211 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4212 | SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4213 | DAG.getVTList(MVT::i64, MVT::Other), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4214 | &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4215 | setValue(&I, Tmp); |
| 4216 | DAG.setRoot(Tmp.getValue(1)); |
| 4217 | return 0; |
| 4218 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4219 | case Intrinsic::bswap: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4220 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4221 | getValue(I.getOperand(1)).getValueType(), |
| 4222 | getValue(I.getOperand(1)))); |
| 4223 | return 0; |
| 4224 | case Intrinsic::cttz: { |
| 4225 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4226 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4227 | SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4228 | setValue(&I, result); |
| 4229 | return 0; |
| 4230 | } |
| 4231 | case Intrinsic::ctlz: { |
| 4232 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4233 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4234 | SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4235 | setValue(&I, result); |
| 4236 | return 0; |
| 4237 | } |
| 4238 | case Intrinsic::ctpop: { |
| 4239 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4240 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4241 | SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4242 | setValue(&I, result); |
| 4243 | return 0; |
| 4244 | } |
| 4245 | case Intrinsic::stacksave: { |
| 4246 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4247 | SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4248 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4249 | setValue(&I, Tmp); |
| 4250 | DAG.setRoot(Tmp.getValue(1)); |
| 4251 | return 0; |
| 4252 | } |
| 4253 | case Intrinsic::stackrestore: { |
| 4254 | SDValue Tmp = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4255 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4256 | return 0; |
| 4257 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4258 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4259 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4260 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4261 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4262 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4263 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4264 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4265 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4266 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4267 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4268 | MFI->setStackProtectorIndex(FI); |
| 4269 | |
| 4270 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4271 | |
| 4272 | // Store the stack protector onto the stack. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4273 | SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 4274 | PseudoSourceValue::getFixedStack(FI), |
| 4275 | 0, true); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4276 | setValue(&I, Result); |
| 4277 | DAG.setRoot(Result); |
| 4278 | return 0; |
| 4279 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4280 | case Intrinsic::objectsize: { |
| 4281 | // If we don't know by now, we're never going to know. |
| 4282 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4283 | |
| 4284 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4285 | |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4286 | SDValue Arg = getValue(I.getOperand(0)); |
| 4287 | EVT Ty = Arg.getValueType(); |
| 4288 | |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4289 | if (CI->getZExtValue() < 2) |
Mike Stump | 70e5e68 | 2009-11-09 22:28:21 +0000 | [diff] [blame] | 4290 | setValue(&I, DAG.getConstant(-1ULL, Ty)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4291 | else |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4292 | setValue(&I, DAG.getConstant(0, Ty)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4293 | return 0; |
| 4294 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4295 | case Intrinsic::var_annotation: |
| 4296 | // Discard annotate attributes |
| 4297 | return 0; |
| 4298 | |
| 4299 | case Intrinsic::init_trampoline: { |
| 4300 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4301 | |
| 4302 | SDValue Ops[6]; |
| 4303 | Ops[0] = getRoot(); |
| 4304 | Ops[1] = getValue(I.getOperand(1)); |
| 4305 | Ops[2] = getValue(I.getOperand(2)); |
| 4306 | Ops[3] = getValue(I.getOperand(3)); |
| 4307 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4308 | Ops[5] = DAG.getSrcValue(F); |
| 4309 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4310 | SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4311 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4312 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4313 | |
| 4314 | setValue(&I, Tmp); |
| 4315 | DAG.setRoot(Tmp.getValue(1)); |
| 4316 | return 0; |
| 4317 | } |
| 4318 | |
| 4319 | case Intrinsic::gcroot: |
| 4320 | if (GFI) { |
| 4321 | Value *Alloca = I.getOperand(1); |
| 4322 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4323 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4324 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4325 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4326 | } |
| 4327 | return 0; |
| 4328 | |
| 4329 | case Intrinsic::gcread: |
| 4330 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4331 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4332 | return 0; |
| 4333 | |
| 4334 | case Intrinsic::flt_rounds: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4335 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4336 | return 0; |
| 4337 | } |
| 4338 | |
| 4339 | case Intrinsic::trap: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4340 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4341 | return 0; |
| 4342 | } |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4343 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4344 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4345 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4346 | case Intrinsic::sadd_with_overflow: |
| 4347 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4348 | case Intrinsic::usub_with_overflow: |
| 4349 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4350 | case Intrinsic::ssub_with_overflow: |
| 4351 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4352 | case Intrinsic::umul_with_overflow: |
| 4353 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4354 | case Intrinsic::smul_with_overflow: |
| 4355 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4356 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4357 | case Intrinsic::prefetch: { |
| 4358 | SDValue Ops[4]; |
| 4359 | Ops[0] = getRoot(); |
| 4360 | Ops[1] = getValue(I.getOperand(1)); |
| 4361 | Ops[2] = getValue(I.getOperand(2)); |
| 4362 | Ops[3] = getValue(I.getOperand(3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4363 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4364 | return 0; |
| 4365 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4366 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4367 | case Intrinsic::memory_barrier: { |
| 4368 | SDValue Ops[6]; |
| 4369 | Ops[0] = getRoot(); |
| 4370 | for (int x = 1; x < 6; ++x) |
| 4371 | Ops[x] = getValue(I.getOperand(x)); |
| 4372 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4373 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4374 | return 0; |
| 4375 | } |
| 4376 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4377 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4378 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4379 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4380 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4381 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4382 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4383 | getValue(I.getOperand(2)), |
| 4384 | getValue(I.getOperand(3)), |
| 4385 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4386 | setValue(&I, L); |
| 4387 | DAG.setRoot(L.getValue(1)); |
| 4388 | return 0; |
| 4389 | } |
| 4390 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4391 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4392 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4393 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4394 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4395 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4396 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4397 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4398 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4399 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4400 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4401 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4402 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4403 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4404 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4405 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4406 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4407 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4408 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4409 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4410 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4411 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4415 | /// Test if the given instruction is in a position to be optimized |
| 4416 | /// with a tail-call. This roughly means that it's in a block with |
| 4417 | /// a return and there's nothing that needs to be scheduled |
| 4418 | /// between it and the return. |
| 4419 | /// |
| 4420 | /// This function only tests target-independent requirements. |
| 4421 | /// For target-dependent requirements, a target should override |
| 4422 | /// TargetLowering::IsEligibleForTailCallOptimization. |
| 4423 | /// |
| 4424 | static bool |
| 4425 | isInTailCallPosition(const Instruction *I, Attributes RetAttr, |
| 4426 | const TargetLowering &TLI) { |
| 4427 | const BasicBlock *ExitBB = I->getParent(); |
| 4428 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 4429 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
| 4430 | const Function *F = ExitBB->getParent(); |
| 4431 | |
| 4432 | // The block must end in a return statement or an unreachable. |
| 4433 | if (!Ret && !isa<UnreachableInst>(Term)) return false; |
| 4434 | |
| 4435 | // If I will have a chain, make sure no other instruction that will have a |
| 4436 | // chain interposes between I and the return. |
| 4437 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
| 4438 | !I->isSafeToSpeculativelyExecute()) |
| 4439 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 4440 | --BBI) { |
| 4441 | if (&*BBI == I) |
| 4442 | break; |
| 4443 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
| 4444 | !BBI->isSafeToSpeculativelyExecute()) |
| 4445 | return false; |
| 4446 | } |
| 4447 | |
| 4448 | // If the block ends with a void return or unreachable, it doesn't matter |
| 4449 | // what the call's return type is. |
| 4450 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 4451 | |
| 4452 | // Conservatively require the attributes of the call to match those of |
| 4453 | // the return. |
| 4454 | if (F->getAttributes().getRetAttributes() != RetAttr) |
| 4455 | return false; |
| 4456 | |
| 4457 | // Otherwise, make sure the unmodified return value of I is the return value. |
| 4458 | for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; |
| 4459 | U = dyn_cast<Instruction>(U->getOperand(0))) { |
| 4460 | if (!U) |
| 4461 | return false; |
| 4462 | if (!U->hasOneUse()) |
| 4463 | return false; |
| 4464 | if (U == I) |
| 4465 | break; |
| 4466 | // Check for a truly no-op truncate. |
| 4467 | if (isa<TruncInst>(U) && |
| 4468 | TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) |
| 4469 | continue; |
| 4470 | // Check for a truly no-op bitcast. |
| 4471 | if (isa<BitCastInst>(U) && |
| 4472 | (U->getOperand(0)->getType() == U->getType() || |
| 4473 | (isa<PointerType>(U->getOperand(0)->getType()) && |
| 4474 | isa<PointerType>(U->getType())))) |
| 4475 | continue; |
| 4476 | // Otherwise it's not a true no-op. |
| 4477 | return false; |
| 4478 | } |
| 4479 | |
| 4480 | return true; |
| 4481 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4482 | |
| 4483 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4484 | bool isTailCall, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4485 | MachineBasicBlock *LandingPad) { |
| 4486 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4487 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4488 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4489 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4490 | |
| 4491 | TargetLowering::ArgListTy Args; |
| 4492 | TargetLowering::ArgListEntry Entry; |
| 4493 | Args.reserve(CS.arg_size()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4494 | unsigned j = 1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4495 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4496 | i != e; ++i, ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4497 | SDValue ArgNode = getValue(*i); |
| 4498 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4499 | |
| 4500 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4501 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4502 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4503 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4504 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4505 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4506 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4507 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4508 | Args.push_back(Entry); |
| 4509 | } |
| 4510 | |
| 4511 | if (LandingPad && MMI) { |
| 4512 | // Insert a label before the invoke call to mark the try range. This can be |
| 4513 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4514 | BeginLabel = MMI->NextLabelID(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4515 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4516 | // Both PendingLoads and PendingExports must be flushed here; |
| 4517 | // this call might not return. |
| 4518 | (void)getRoot(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4519 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4520 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4523 | // Check if target-independent constraints permit a tail call here. |
| 4524 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4525 | if (isTailCall && |
| 4526 | !isInTailCallPosition(CS.getInstruction(), |
| 4527 | CS.getAttributes().getRetAttributes(), |
| 4528 | TLI)) |
| 4529 | isTailCall = false; |
| 4530 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4531 | std::pair<SDValue,SDValue> Result = |
| 4532 | TLI.LowerCallTo(getRoot(), CS.getType(), |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4533 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4534 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4535 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4536 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4537 | isTailCall, |
| 4538 | !CS.getInstruction()->use_empty(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4539 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4540 | assert((isTailCall || Result.second.getNode()) && |
| 4541 | "Non-null chain expected with non-tail call!"); |
| 4542 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4543 | "Null value expected with tail call!"); |
| 4544 | if (Result.first.getNode()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4545 | setValue(CS.getInstruction(), Result.first); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4546 | // As a special case, a null chain means that a tail call has |
| 4547 | // been emitted and the DAG root is already updated. |
| 4548 | if (Result.second.getNode()) |
| 4549 | DAG.setRoot(Result.second); |
| 4550 | else |
| 4551 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4552 | |
| 4553 | if (LandingPad && MMI) { |
| 4554 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4555 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4556 | EndLabel = MMI->NextLabelID(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4557 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4558 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4559 | |
| 4560 | // Inform MachineModuleInfo of range. |
| 4561 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4562 | } |
| 4563 | } |
| 4564 | |
| 4565 | |
| 4566 | void SelectionDAGLowering::visitCall(CallInst &I) { |
| 4567 | const char *RenameFn = 0; |
| 4568 | if (Function *F = I.getCalledFunction()) { |
| 4569 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4570 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4571 | if (II) { |
| 4572 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4573 | RenameFn = visitIntrinsicCall(I, IID); |
| 4574 | if (!RenameFn) |
| 4575 | return; |
| 4576 | } |
| 4577 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4578 | if (unsigned IID = F->getIntrinsicID()) { |
| 4579 | RenameFn = visitIntrinsicCall(I, IID); |
| 4580 | if (!RenameFn) |
| 4581 | return; |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4586 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4587 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4588 | StringRef Name = F->getName(); |
| 4589 | if (Name == "copysign" || Name == "copysignf") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4590 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4591 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4592 | I.getType() == I.getOperand(1)->getType() && |
| 4593 | I.getType() == I.getOperand(2)->getType()) { |
| 4594 | SDValue LHS = getValue(I.getOperand(1)); |
| 4595 | SDValue RHS = getValue(I.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4596 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4597 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4598 | return; |
| 4599 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4600 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4601 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4602 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4603 | I.getType() == I.getOperand(1)->getType()) { |
| 4604 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4605 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4606 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4607 | return; |
| 4608 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4609 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4610 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4611 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4612 | I.getType() == I.getOperand(1)->getType() && |
| 4613 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4614 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4615 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4616 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4617 | return; |
| 4618 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4619 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4620 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4621 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4622 | I.getType() == I.getOperand(1)->getType() && |
| 4623 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4624 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4625 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4626 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4627 | return; |
| 4628 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4629 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4630 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4631 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4632 | I.getType() == I.getOperand(1)->getType() && |
| 4633 | I.onlyReadsMemory()) { |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4634 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4635 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4636 | Tmp.getValueType(), Tmp)); |
| 4637 | return; |
| 4638 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4639 | } |
| 4640 | } |
| 4641 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4642 | visitInlineAsm(&I); |
| 4643 | return; |
| 4644 | } |
| 4645 | |
| 4646 | SDValue Callee; |
| 4647 | if (!RenameFn) |
| 4648 | Callee = getValue(I.getOperand(0)); |
| 4649 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4650 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4651 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4652 | // Check if we can potentially perform a tail call. More detailed |
| 4653 | // checking is be done within LowerCallTo, after more information |
| 4654 | // about the call is known. |
| 4655 | bool isTailCall = PerformTailCallOpt && I.isTailCall(); |
| 4656 | |
| 4657 | LowerCallTo(&I, Callee, isTailCall); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | |
| 4661 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4662 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4663 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4664 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4665 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4666 | SDValue &Chain, |
| 4667 | SDValue *Flag) const { |
| 4668 | // Assemble the legal parts into the final values. |
| 4669 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4670 | SmallVector<SDValue, 8> Parts; |
| 4671 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4672 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4673 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4674 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4675 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4676 | |
| 4677 | Parts.resize(NumRegs); |
| 4678 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4679 | SDValue P; |
| 4680 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4681 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4682 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4683 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4684 | *Flag = P.getValue(2); |
| 4685 | } |
| 4686 | Chain = P.getValue(1); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4687 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4688 | // If the source register was virtual and if we know something about it, |
| 4689 | // add an assert node. |
| 4690 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4691 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4692 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4693 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4694 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4695 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4696 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4697 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4698 | unsigned NumSignBits = LOI.NumSignBits; |
| 4699 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4700 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4701 | // FIXME: We capture more information than the dag can represent. For |
| 4702 | // now, just use the tightest assertzext/assertsext possible. |
| 4703 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4704 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4705 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4706 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4707 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4708 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4709 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4710 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4711 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4712 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4713 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4714 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4715 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4716 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4717 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4718 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4719 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4720 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4721 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4722 | if (FromVT != MVT::Other) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4723 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4724 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4725 | |
| 4726 | } |
| 4727 | } |
| 4728 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4729 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4730 | Parts[i] = P; |
| 4731 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4732 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4733 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4734 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4735 | Part += NumRegs; |
| 4736 | Parts.clear(); |
| 4737 | } |
| 4738 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4739 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 4740 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4741 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4745 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4746 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4747 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4748 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4749 | SDValue &Chain, SDValue *Flag) const { |
| 4750 | // Get the list of the values's legal parts. |
| 4751 | unsigned NumRegs = Regs.size(); |
| 4752 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4753 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4754 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4755 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4756 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4757 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4758 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4759 | &Parts[Part], NumParts, RegisterVT); |
| 4760 | Part += NumParts; |
| 4761 | } |
| 4762 | |
| 4763 | // Copy the parts into the registers. |
| 4764 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4765 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4766 | SDValue Part; |
| 4767 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4768 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4769 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4770 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4771 | *Flag = Part.getValue(1); |
| 4772 | } |
| 4773 | Chains[i] = Part.getValue(0); |
| 4774 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4775 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4776 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4777 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4778 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4779 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4780 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4781 | // user as well as a successor (the TF operands are flagged to the user). |
| 4782 | // c1, f1 = CopyToReg |
| 4783 | // c2, f2 = CopyToReg |
| 4784 | // c3 = TokenFactor c1, c2 |
| 4785 | // ... |
| 4786 | // = op c3, ..., f2 |
| 4787 | Chain = Chains[NumRegs-1]; |
| 4788 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4789 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4793 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4794 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4795 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4796 | bool HasMatching,unsigned MatchingIdx, |
| 4797 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4798 | std::vector<SDValue> &Ops) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4799 | EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4800 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4801 | unsigned Flag = Code | (Regs.size() << 3); |
| 4802 | if (HasMatching) |
| 4803 | Flag |= 0x80000000 | (MatchingIdx << 16); |
| 4804 | Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4805 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4806 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4807 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4808 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4809 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4810 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4811 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4815 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4816 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4817 | /// register class for the register. Otherwise, return null. |
| 4818 | static const TargetRegisterClass * |
| 4819 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4820 | const TargetLowering &TLI, |
| 4821 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4822 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4823 | const TargetRegisterClass *FoundRC = 0; |
| 4824 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4825 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4826 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4827 | |
| 4828 | const TargetRegisterClass *RC = *RCI; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4829 | // If none of the the value types for this register class are valid, we |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4830 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4831 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4832 | I != E; ++I) { |
| 4833 | if (TLI.isTypeLegal(*I)) { |
| 4834 | // If we have already found this register in a different register class, |
| 4835 | // choose the one with the largest VT specified. For example, on |
| 4836 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4837 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4838 | ThisVT = *I; |
| 4839 | break; |
| 4840 | } |
| 4841 | } |
| 4842 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4843 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4844 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4845 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4846 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4847 | // frame pointer in functions that need it (due to them not being taken |
| 4848 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4849 | // yet). This is a slight code pessimization, but should still work. |
| 4850 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4851 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4852 | if (*I == Reg) { |
| 4853 | // We found a matching register class. Keep looking at others in case |
| 4854 | // we find one with larger registers that this physreg is also in. |
| 4855 | FoundRC = RC; |
| 4856 | FoundVT = ThisVT; |
| 4857 | break; |
| 4858 | } |
| 4859 | } |
| 4860 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4861 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4862 | |
| 4863 | |
| 4864 | namespace llvm { |
| 4865 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4866 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4867 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4868 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4869 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4870 | /// CallOperand - If this is the result output operand or a clobber |
| 4871 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4872 | /// This gets modified as the asm is processed. |
| 4873 | SDValue CallOperand; |
| 4874 | |
| 4875 | /// AssignedRegs - If this is a register or register class operand, this |
| 4876 | /// contains the set of register corresponding to the operand. |
| 4877 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4878 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4879 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4880 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4881 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4882 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4883 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4884 | /// busy in OutputRegs/InputRegs. |
| 4885 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4886 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4887 | std::set<unsigned> &InputRegs, |
| 4888 | const TargetRegisterInfo &TRI) const { |
| 4889 | if (isOutReg) { |
| 4890 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4891 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4892 | } |
| 4893 | if (isInReg) { |
| 4894 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4895 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4896 | } |
| 4897 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4898 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4899 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4900 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4901 | /// MVT::Other. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4902 | EVT getCallOperandValEVT(LLVMContext &Context, |
| 4903 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4904 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4905 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4906 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4907 | if (isa<BasicBlock>(CallOperandVal)) |
| 4908 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4909 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4910 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4911 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4912 | // If this is an indirect operand, the operand is a pointer to the |
| 4913 | // accessed type. |
| 4914 | if (isIndirect) |
| 4915 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4916 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4917 | // If OpTy is not a single value, it may be a struct/union that we |
| 4918 | // can tile with integers. |
| 4919 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4920 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4921 | switch (BitSize) { |
| 4922 | default: break; |
| 4923 | case 1: |
| 4924 | case 8: |
| 4925 | case 16: |
| 4926 | case 32: |
| 4927 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4928 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4929 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4930 | break; |
| 4931 | } |
| 4932 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4933 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4934 | return TLI.getValueType(OpTy, true); |
| 4935 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4936 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4937 | private: |
| 4938 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4939 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4940 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4941 | const TargetRegisterInfo &TRI) { |
| 4942 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4943 | Regs.insert(Reg); |
| 4944 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4945 | for (; *Aliases; ++Aliases) |
| 4946 | Regs.insert(*Aliases); |
| 4947 | } |
| 4948 | }; |
| 4949 | } // end llvm namespace. |
| 4950 | |
| 4951 | |
| 4952 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4953 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4954 | /// register allocator handle the assignment process. However, if the asm uses |
| 4955 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 4956 | /// allocation. This produces generally horrible, but correct, code. |
| 4957 | /// |
| 4958 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4959 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4960 | /// |
| 4961 | void SelectionDAGLowering:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4962 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4963 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4964 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 4965 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4966 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4967 | // Compute whether this value requires an input register, an output register, |
| 4968 | // or both. |
| 4969 | bool isOutReg = false; |
| 4970 | bool isInReg = false; |
| 4971 | switch (OpInfo.Type) { |
| 4972 | case InlineAsm::isOutput: |
| 4973 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4974 | |
| 4975 | // If there is an input constraint that matches this, we need to reserve |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4976 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 4977 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4978 | break; |
| 4979 | case InlineAsm::isInput: |
| 4980 | isInReg = true; |
| 4981 | isOutReg = false; |
| 4982 | break; |
| 4983 | case InlineAsm::isClobber: |
| 4984 | isOutReg = true; |
| 4985 | isInReg = true; |
| 4986 | break; |
| 4987 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4988 | |
| 4989 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4990 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4991 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4992 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4993 | // If this is a constraint for a single physreg, or a constraint for a |
| 4994 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4995 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4996 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 4997 | OpInfo.ConstraintVT); |
| 4998 | |
| 4999 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5000 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5001 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5002 | // cast of the input value. More generally, handle any case where the input |
| 5003 | // value disagrees with the register class we plan to stick this in. |
| 5004 | if (OpInfo.Type == InlineAsm::isInput && |
| 5005 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5006 | // Try to convert to the first EVT that the reg class contains. If the |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5007 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5008 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5009 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5010 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5011 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5012 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5013 | OpInfo.ConstraintVT = RegVT; |
| 5014 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5015 | // If the input is a FP value and we want it in FP registers, do a |
| 5016 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5017 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5018 | // machine. |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5019 | RegVT = EVT::getIntegerVT(Context, |
| 5020 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5021 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5022 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5023 | OpInfo.ConstraintVT = RegVT; |
| 5024 | } |
| 5025 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5026 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5027 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5028 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5029 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5030 | EVT RegVT; |
| 5031 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5032 | |
| 5033 | // If this is a constraint for a specific physical register, like {r17}, |
| 5034 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5035 | if (unsigned AssignedReg = PhysReg.first) { |
| 5036 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5037 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5038 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5039 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5040 | // Get the actual register value type. This is important, because the user |
| 5041 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5042 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5043 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5044 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5045 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5046 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5047 | |
| 5048 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5049 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5050 | TargetRegisterClass::iterator I = RC->begin(); |
| 5051 | for (; *I != AssignedReg; ++I) |
| 5052 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5053 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5054 | // Already added the first reg. |
| 5055 | --NumRegs; ++I; |
| 5056 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5057 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5058 | Regs.push_back(*I); |
| 5059 | } |
| 5060 | } |
| 5061 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5062 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5063 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5064 | return; |
| 5065 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5066 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5067 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5068 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5069 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5070 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5071 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5072 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5073 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5074 | // Create the appropriate number of virtual registers. |
| 5075 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5076 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5077 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5078 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5079 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5080 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5081 | } |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5082 | |
| 5083 | // This is a reference to a register class that doesn't directly correspond |
| 5084 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5085 | // registers from the class. |
| 5086 | std::vector<unsigned> RegClassRegs |
| 5087 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5088 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5089 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5090 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5091 | unsigned NumAllocated = 0; |
| 5092 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5093 | unsigned Reg = RegClassRegs[i]; |
| 5094 | // See if this register is available. |
| 5095 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5096 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5097 | // Make sure we find consecutive registers. |
| 5098 | NumAllocated = 0; |
| 5099 | continue; |
| 5100 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5101 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5102 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5103 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5104 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5105 | if (!RC) { // Couldn't allocate this register. |
| 5106 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5107 | NumAllocated = 0; |
| 5108 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5109 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5110 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5111 | // Okay, this register is good, we can use it. |
| 5112 | ++NumAllocated; |
| 5113 | |
| 5114 | // If we allocated enough consecutive registers, succeed. |
| 5115 | if (NumAllocated == NumRegs) { |
| 5116 | unsigned RegStart = (i-NumAllocated)+1; |
| 5117 | unsigned RegEnd = i+1; |
| 5118 | // Mark all of the allocated registers used. |
| 5119 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5120 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5121 | |
| 5122 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5123 | OpInfo.ConstraintVT); |
| 5124 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5125 | return; |
| 5126 | } |
| 5127 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5128 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5129 | // Otherwise, we couldn't allocate enough registers for this. |
| 5130 | } |
| 5131 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5132 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5133 | /// processed uses a memory 'm' constraint. |
| 5134 | static bool |
| 5135 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5136 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5137 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5138 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5139 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5140 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5141 | if (CType == TargetLowering::C_Memory) |
| 5142 | return true; |
| 5143 | } |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5144 | |
| 5145 | // Indirect operand accesses access memory. |
| 5146 | if (CI.isIndirect) |
| 5147 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | return false; |
| 5151 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5152 | |
| 5153 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5154 | /// |
| 5155 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 5156 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5157 | |
| 5158 | /// ConstraintOperands - Information about all of the constraints. |
| 5159 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5160 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5161 | std::set<unsigned> OutputRegs, InputRegs; |
| 5162 | |
| 5163 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5164 | // ConstraintOperands list. |
| 5165 | std::vector<InlineAsm::ConstraintInfo> |
| 5166 | ConstraintInfos = IA->ParseConstraints(); |
| 5167 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5168 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5169 | |
| 5170 | SDValue Chain, Flag; |
| 5171 | |
| 5172 | // We won't need to flush pending loads if this asm doesn't touch |
| 5173 | // memory and is nonvolatile. |
| 5174 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5175 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5176 | else |
| 5177 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5178 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5179 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5180 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5181 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5182 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5183 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5184 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5185 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5186 | |
| 5187 | // Compute the value type for each operand. |
| 5188 | switch (OpInfo.Type) { |
| 5189 | case InlineAsm::isOutput: |
| 5190 | // Indirect outputs just consume an argument. |
| 5191 | if (OpInfo.isIndirect) { |
| 5192 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5193 | break; |
| 5194 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5195 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5196 | // The return value of the call is this value. As such, there is no |
| 5197 | // corresponding argument. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5198 | assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) && |
| 5199 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5200 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5201 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5202 | } else { |
| 5203 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5204 | OpVT = TLI.getValueType(CS.getType()); |
| 5205 | } |
| 5206 | ++ResNo; |
| 5207 | break; |
| 5208 | case InlineAsm::isInput: |
| 5209 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5210 | break; |
| 5211 | case InlineAsm::isClobber: |
| 5212 | // Nothing to do. |
| 5213 | break; |
| 5214 | } |
| 5215 | |
| 5216 | // If this is an input or an indirect output, process the call argument. |
| 5217 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5218 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5219 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5220 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5221 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5222 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5223 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5224 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5225 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5226 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5227 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5228 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5229 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5230 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5231 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5232 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5233 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5234 | // Second pass over the constraints: compute which constraint option to use |
| 5235 | // and assign registers to constraints that want a specific physreg. |
| 5236 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5237 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5238 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5239 | // If this is an output operand with a matching input operand, look up the |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5240 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5241 | // other is floating point, or their sizes are different, flag it as an |
| 5242 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5243 | if (OpInfo.hasMatchingInput()) { |
| 5244 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5245 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5246 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5247 | Input.ConstraintVT.isInteger()) || |
| 5248 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5249 | Input.ConstraintVT.getSizeInBits())) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5250 | llvm_report_error("Unsupported asm: input constraint" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5251 | " with a matching output constraint of incompatible" |
| 5252 | " type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5253 | } |
| 5254 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5255 | } |
| 5256 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5257 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5259 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5260 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5261 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5262 | // need to to provide an address for the memory input. |
| 5263 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5264 | !OpInfo.isIndirect) { |
| 5265 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5266 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5267 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5268 | // Memory operands really want the address of the value. If we don't have |
| 5269 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5270 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5271 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5272 | // If the operand is a float, integer, or vector constant, spill to a |
| 5273 | // constant pool entry to get its address. |
| 5274 | Value *OpVal = OpInfo.CallOperandVal; |
| 5275 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5276 | isa<ConstantVector>(OpVal)) { |
| 5277 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5278 | TLI.getPointerTy()); |
| 5279 | } else { |
| 5280 | // Otherwise, create a stack slot and emit a store to it before the |
| 5281 | // asm. |
| 5282 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5283 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5284 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5285 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5286 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 5287 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5288 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5289 | OpInfo.CallOperand, StackSlot, NULL, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5290 | OpInfo.CallOperand = StackSlot; |
| 5291 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5292 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5293 | // There is no longer a Value* corresponding to this operand. |
| 5294 | OpInfo.CallOperandVal = 0; |
| 5295 | // It is now an indirect operand. |
| 5296 | OpInfo.isIndirect = true; |
| 5297 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5298 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5299 | // If this constraint is for a specific register, allocate it before |
| 5300 | // anything else. |
| 5301 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5302 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5303 | } |
| 5304 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5305 | |
| 5306 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5307 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5308 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5309 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5310 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5311 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5312 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5313 | // to be. |
| 5314 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5315 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5318 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5319 | std::vector<SDValue> AsmNodeOperands; |
| 5320 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5321 | AsmNodeOperands.push_back( |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5322 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5323 | |
| 5324 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5325 | // Loop over all of the inputs, copying the operand values into the |
| 5326 | // appropriate registers and processing the output regs. |
| 5327 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5328 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5329 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5330 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5332 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5333 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5334 | |
| 5335 | switch (OpInfo.Type) { |
| 5336 | case InlineAsm::isOutput: { |
| 5337 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5338 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5339 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5340 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5341 | |
| 5342 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5343 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5344 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5345 | TLI.getPointerTy())); |
| 5346 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5347 | break; |
| 5348 | } |
| 5349 | |
| 5350 | // Otherwise, this is a register or register class output. |
| 5351 | |
| 5352 | // Copy the output from the appropriate register. Find a register that |
| 5353 | // we can use. |
| 5354 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5355 | llvm_report_error("Couldn't allocate output reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5356 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | // If this is an indirect operand, store through the pointer after the |
| 5360 | // asm. |
| 5361 | if (OpInfo.isIndirect) { |
| 5362 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5363 | OpInfo.CallOperandVal)); |
| 5364 | } else { |
| 5365 | // This is the result value of the call. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5366 | assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) && |
| 5367 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5368 | // Concatenate this output onto the outputs list. |
| 5369 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5370 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5371 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5372 | // Add information to the INLINEASM node to know that this register is |
| 5373 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5374 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5375 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5376 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5377 | false, |
| 5378 | 0, |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5379 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5380 | break; |
| 5381 | } |
| 5382 | case InlineAsm::isInput: { |
| 5383 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5384 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5385 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5386 | // If this is required to match an output register we have already set, |
| 5387 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5388 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5389 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5390 | // Scan until we find the definition we already emitted of this operand. |
| 5391 | // When we find it, create a RegsForValue operand. |
| 5392 | unsigned CurOp = 2; // The first operand. |
| 5393 | for (; OperandNo; --OperandNo) { |
| 5394 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5395 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5396 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5397 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5398 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5399 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5400 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5401 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5404 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5405 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5406 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5407 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5408 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5409 | if (OpInfo.isIndirect) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5410 | llvm_report_error("Don't know how to handle tied indirect " |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5411 | "register inputs yet!"); |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5412 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5413 | RegsForValue MatchedRegs; |
| 5414 | MatchedRegs.TLI = &TLI; |
| 5415 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5416 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5417 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5418 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5419 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5420 | i != e; ++i) |
| 5421 | MatchedRegs.Regs. |
| 5422 | push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5423 | |
| 5424 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5425 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5426 | Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5427 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5428 | true, OpInfo.getMatchedOperand(), |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5429 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5430 | break; |
| 5431 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5432 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5433 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5434 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5435 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5436 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5437 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5438 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5439 | TLI.getPointerTy())); |
| 5440 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5441 | break; |
| 5442 | } |
| 5443 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5444 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5445 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5446 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5447 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5448 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5449 | std::vector<SDValue> Ops; |
| 5450 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5451 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5452 | if (Ops.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5453 | llvm_report_error("Invalid operand for inline asm" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5454 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5455 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5456 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5457 | // Add information to the INLINEASM node to know about this input. |
| 5458 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5459 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5460 | TLI.getPointerTy())); |
| 5461 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5462 | break; |
| 5463 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5464 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5465 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5466 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5467 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5468 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5469 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5470 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5471 | TLI.getPointerTy())); |
| 5472 | AsmNodeOperands.push_back(InOperandVal); |
| 5473 | break; |
| 5474 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5475 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5476 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5477 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5478 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5479 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5480 | "Don't know how to handle indirect register inputs yet!"); |
| 5481 | |
| 5482 | // Copy the input into the appropriate registers. |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5483 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5484 | llvm_report_error("Couldn't allocate input reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5485 | " constraint '"+ OpInfo.ConstraintCode +"'!"); |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5486 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5487 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5488 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5489 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5490 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5491 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5492 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5493 | break; |
| 5494 | } |
| 5495 | case InlineAsm::isClobber: { |
| 5496 | // Add the clobbered value to the operand list, so that the register |
| 5497 | // allocator is aware that the physreg got clobbered. |
| 5498 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5499 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5500 | false, 0, DAG,AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5501 | break; |
| 5502 | } |
| 5503 | } |
| 5504 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5505 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5506 | // Finish up input operands. |
| 5507 | AsmNodeOperands[0] = Chain; |
| 5508 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5509 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5510 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5511 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5512 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5513 | Flag = Chain.getValue(1); |
| 5514 | |
| 5515 | // If this asm returns a register value, copy the result from that register |
| 5516 | // and set it as the value of the call. |
| 5517 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5518 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5519 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5520 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5521 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5522 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5523 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5524 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5525 | // If any of the results of the inline asm is a vector, it may have the |
| 5526 | // wrong width/num elts. This can happen for register classes that can |
| 5527 | // contain multiple different value types. The preg or vreg allocated may |
| 5528 | // not have the same VT as was expected. Convert it to the right type |
| 5529 | // with bit_convert. |
| 5530 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5531 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5532 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5533 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5534 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5535 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5536 | // If a result value was tied to an input value, the computed result may |
| 5537 | // have a wider width than the expected result. Extract the relevant |
| 5538 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5539 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5540 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5541 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5542 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5543 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5544 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5545 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5546 | // Don't need to use this as a chain in this case. |
| 5547 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5548 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5549 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5550 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5551 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5552 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5553 | // Process indirect outputs, first output all of the flagged copies out of |
| 5554 | // physregs. |
| 5555 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5556 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5557 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5558 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
| 5559 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5560 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5561 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5562 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5563 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5564 | // Emit the non-flagged stores from the physregs. |
| 5565 | SmallVector<SDValue, 8> OutChains; |
| 5566 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5567 | OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5568 | StoresToEmit[i].first, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5569 | getValue(StoresToEmit[i].second), |
| 5570 | StoresToEmit[i].second, 0)); |
| 5571 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5572 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5573 | &OutChains[0], OutChains.size()); |
| 5574 | DAG.setRoot(Chain); |
| 5575 | } |
| 5576 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5577 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5578 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5579 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5580 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5581 | DAG.getSrcValue(I.getOperand(1)))); |
| 5582 | } |
| 5583 | |
| 5584 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5585 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5586 | getRoot(), getValue(I.getOperand(0)), |
| 5587 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5588 | setValue(&I, V); |
| 5589 | DAG.setRoot(V.getValue(1)); |
| 5590 | } |
| 5591 | |
| 5592 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5593 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5594 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5595 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5596 | DAG.getSrcValue(I.getOperand(1)))); |
| 5597 | } |
| 5598 | |
| 5599 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5600 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5601 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5602 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5603 | getValue(I.getOperand(2)), |
| 5604 | DAG.getSrcValue(I.getOperand(1)), |
| 5605 | DAG.getSrcValue(I.getOperand(2)))); |
| 5606 | } |
| 5607 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5608 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5609 | /// implementation, which just calls LowerCall. |
| 5610 | /// FIXME: When all targets are |
| 5611 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5612 | std::pair<SDValue, SDValue> |
| 5613 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5614 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5615 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5616 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5617 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5618 | SDValue Callee, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5619 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5620 | |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5621 | assert((!isTailCall || PerformTailCallOpt) && |
| 5622 | "isTailCall set when tail-call optimizations are disabled!"); |
| 5623 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5624 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5625 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5626 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5627 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5628 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5629 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5630 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5631 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5632 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5633 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5634 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5635 | ISD::ArgFlagsTy Flags; |
| 5636 | unsigned OriginalAlignment = |
| 5637 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5638 | |
| 5639 | if (Args[i].isZExt) |
| 5640 | Flags.setZExt(); |
| 5641 | if (Args[i].isSExt) |
| 5642 | Flags.setSExt(); |
| 5643 | if (Args[i].isInReg) |
| 5644 | Flags.setInReg(); |
| 5645 | if (Args[i].isSRet) |
| 5646 | Flags.setSRet(); |
| 5647 | if (Args[i].isByVal) { |
| 5648 | Flags.setByVal(); |
| 5649 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5650 | const Type *ElementTy = Ty->getElementType(); |
| 5651 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5652 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5653 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5654 | // info is not there but there are cases it cannot get right. |
| 5655 | if (Args[i].Alignment) |
| 5656 | FrameAlign = Args[i].Alignment; |
| 5657 | Flags.setByValAlign(FrameAlign); |
| 5658 | Flags.setByValSize(FrameSize); |
| 5659 | } |
| 5660 | if (Args[i].isNest) |
| 5661 | Flags.setNest(); |
| 5662 | Flags.setOrigAlign(OriginalAlignment); |
| 5663 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5664 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5665 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5666 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5667 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5668 | |
| 5669 | if (Args[i].isSExt) |
| 5670 | ExtendKind = ISD::SIGN_EXTEND; |
| 5671 | else if (Args[i].isZExt) |
| 5672 | ExtendKind = ISD::ZERO_EXTEND; |
| 5673 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5674 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5675 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5676 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5677 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5678 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5679 | if (NumParts > 1 && j == 0) |
| 5680 | MyFlags.Flags.setSplit(); |
| 5681 | else if (j != 0) |
| 5682 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5683 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5684 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5685 | } |
| 5686 | } |
| 5687 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5688 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5689 | // Handle the incoming return values from the call. |
| 5690 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5691 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5692 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5693 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5694 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5695 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5696 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5697 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5698 | ISD::InputArg MyFlags; |
| 5699 | MyFlags.VT = RegisterVT; |
| 5700 | MyFlags.Used = isReturnValueUsed; |
| 5701 | if (RetSExt) |
| 5702 | MyFlags.Flags.setSExt(); |
| 5703 | if (RetZExt) |
| 5704 | MyFlags.Flags.setZExt(); |
| 5705 | if (isInreg) |
| 5706 | MyFlags.Flags.setInReg(); |
| 5707 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5708 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5711 | // Check if target-dependent constraints permit a tail call here. |
| 5712 | // Target-independent constraints should be checked by the caller. |
| 5713 | if (isTailCall && |
| 5714 | !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG)) |
| 5715 | isTailCall = false; |
| 5716 | |
| 5717 | SmallVector<SDValue, 4> InVals; |
| 5718 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
| 5719 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5720 | |
| 5721 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5722 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5723 | "LowerCall didn't return a valid chain!"); |
| 5724 | assert((!isTailCall || InVals.empty()) && |
| 5725 | "LowerCall emitted a return value for a tail call!"); |
| 5726 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5727 | "LowerCall didn't emit the correct number of values!"); |
| 5728 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5729 | assert(InVals[i].getNode() && |
| 5730 | "LowerCall emitted a null value!"); |
| 5731 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5732 | "LowerCall emitted a value with the wrong type!"); |
| 5733 | }); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5734 | |
| 5735 | // For a tail call, the return value is merely live-out and there aren't |
| 5736 | // any nodes in the DAG representing it. Return a special value to |
| 5737 | // indicate that a tail call has been emitted and no more Instructions |
| 5738 | // should be processed in the current block. |
| 5739 | if (isTailCall) { |
| 5740 | DAG.setRoot(Chain); |
| 5741 | return std::make_pair(SDValue(), SDValue()); |
| 5742 | } |
| 5743 | |
| 5744 | // Collect the legal value parts into potentially illegal values |
| 5745 | // that correspond to the original function's return values. |
| 5746 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5747 | if (RetSExt) |
| 5748 | AssertOp = ISD::AssertSext; |
| 5749 | else if (RetZExt) |
| 5750 | AssertOp = ISD::AssertZext; |
| 5751 | SmallVector<SDValue, 4> ReturnValues; |
| 5752 | unsigned CurReg = 0; |
| 5753 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5754 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5755 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5756 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5757 | |
| 5758 | SDValue ReturnValue = |
| 5759 | getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT, |
| 5760 | AssertOp); |
| 5761 | ReturnValues.push_back(ReturnValue); |
| 5762 | CurReg += NumRegs; |
| 5763 | } |
| 5764 | |
| 5765 | // For a function returning void, there is no return value. We can't create |
| 5766 | // such a node, so we just return a null return value in that case. In |
| 5767 | // that case, nothing will actualy look at the value. |
| 5768 | if (ReturnValues.empty()) |
| 5769 | return std::make_pair(SDValue(), Chain); |
| 5770 | |
| 5771 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5772 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5773 | &ReturnValues[0], ReturnValues.size()); |
| 5774 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5775 | return std::make_pair(Res, Chain); |
| 5776 | } |
| 5777 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5778 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5779 | SmallVectorImpl<SDValue> &Results, |
| 5780 | SelectionDAG &DAG) { |
| 5781 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5782 | if (Res.getNode()) |
| 5783 | Results.push_back(Res); |
| 5784 | } |
| 5785 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5786 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5787 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5788 | return SDValue(); |
| 5789 | } |
| 5790 | |
| 5791 | |
| 5792 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
| 5793 | SDValue Op = getValue(V); |
| 5794 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5795 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5796 | "Copy from a reg to the same reg!"); |
| 5797 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5798 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5799 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5800 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5801 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5802 | PendingExports.push_back(Chain); |
| 5803 | } |
| 5804 | |
| 5805 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5806 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 5807 | void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5808 | // If this is the entry block, emit arguments. |
| 5809 | Function &F = *LLVMBB->getParent(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5810 | SelectionDAG &DAG = SDL->DAG; |
| 5811 | SDValue OldRoot = DAG.getRoot(); |
| 5812 | DebugLoc dl = SDL->getCurDebugLoc(); |
| 5813 | const TargetData *TD = TLI.getTargetData(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5814 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5815 | // Check whether the function can return without sret-demotion. |
| 5816 | SmallVector<EVT, 4> OutVTs; |
| 5817 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 5818 | getReturnInfo(&F, OutVTs, OutsFlags, TLI); |
| 5819 | // For now, assert and bail out if it can't. |
| 5820 | assert(TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), OutVTs, OutsFlags, |
| 5821 | DAG) && "Cannot fit return value in registers!"); |
| 5822 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5823 | // Set up the incoming argument description vector. |
| 5824 | SmallVector<ISD::InputArg, 16> Ins; |
| 5825 | unsigned Idx = 1; |
| 5826 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5827 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5828 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5829 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5830 | bool isArgValueUsed = !I->use_empty(); |
| 5831 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5832 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5833 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5834 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5835 | ISD::ArgFlagsTy Flags; |
| 5836 | unsigned OriginalAlignment = |
| 5837 | TD->getABITypeAlignment(ArgTy); |
| 5838 | |
| 5839 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5840 | Flags.setZExt(); |
| 5841 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5842 | Flags.setSExt(); |
| 5843 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5844 | Flags.setInReg(); |
| 5845 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5846 | Flags.setSRet(); |
| 5847 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5848 | Flags.setByVal(); |
| 5849 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5850 | const Type *ElementTy = Ty->getElementType(); |
| 5851 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5852 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5853 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5854 | // this info is not there but there are cases it cannot get right. |
| 5855 | if (F.getParamAlignment(Idx)) |
| 5856 | FrameAlign = F.getParamAlignment(Idx); |
| 5857 | Flags.setByValAlign(FrameAlign); |
| 5858 | Flags.setByValSize(FrameSize); |
| 5859 | } |
| 5860 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5861 | Flags.setNest(); |
| 5862 | Flags.setOrigAlign(OriginalAlignment); |
| 5863 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5864 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5865 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5866 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5867 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5868 | if (NumRegs > 1 && i == 0) |
| 5869 | MyFlags.Flags.setSplit(); |
| 5870 | // if it isn't first piece, alignment must be 1 |
| 5871 | else if (i > 0) |
| 5872 | MyFlags.Flags.setOrigAlign(1); |
| 5873 | Ins.push_back(MyFlags); |
| 5874 | } |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | // Call the target to set up the argument values. |
| 5879 | SmallVector<SDValue, 8> InVals; |
| 5880 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5881 | F.isVarArg(), Ins, |
| 5882 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5885 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5886 | "LowerFormalArguments didn't return a valid chain!"); |
| 5887 | assert(InVals.size() == Ins.size() && |
| 5888 | "LowerFormalArguments didn't emit the correct number of values!"); |
| 5889 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5890 | assert(InVals[i].getNode() && |
| 5891 | "LowerFormalArguments emitted a null value!"); |
| 5892 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5893 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5894 | }); |
| 5895 | |
| 5896 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5897 | DAG.setRoot(NewRoot); |
| 5898 | |
| 5899 | // Set up the argument values. |
| 5900 | unsigned i = 0; |
| 5901 | Idx = 1; |
| 5902 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 5903 | ++I, ++Idx) { |
| 5904 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5905 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5906 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5907 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5908 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5909 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5910 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5911 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5912 | |
| 5913 | if (!I->use_empty()) { |
| 5914 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5915 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5916 | AssertOp = ISD::AssertSext; |
| 5917 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5918 | AssertOp = ISD::AssertZext; |
| 5919 | |
| 5920 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts, |
| 5921 | PartVT, VT, AssertOp)); |
| 5922 | } |
| 5923 | i += NumParts; |
| 5924 | } |
| 5925 | if (!I->use_empty()) { |
| 5926 | SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues, |
| 5927 | SDL->getCurDebugLoc())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5928 | // If this argument is live outside of the entry block, insert a copy from |
| 5929 | // whereever we got it to the vreg that other BB's will reference it as. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5930 | SDL->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5931 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5932 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5933 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5934 | |
| 5935 | // Finally, if the target has anything special to do, allow it to do so. |
| 5936 | // FIXME: this should insert code into the DAG! |
| 5937 | EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction()); |
| 5938 | } |
| 5939 | |
| 5940 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 5941 | /// ensure constants are generated when needed. Remember the virtual registers |
| 5942 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 5943 | /// directly add them, because expansion might result in multiple MBB's for one |
| 5944 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 5945 | /// the end. |
| 5946 | /// |
| 5947 | void |
| 5948 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 5949 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 5950 | |
| 5951 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5952 | |
| 5953 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5954 | // from this block. |
| 5955 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5956 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5957 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5958 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5959 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5960 | // If this terminator has multiple identical successors (common for |
| 5961 | // switches), only handle each succ once. |
| 5962 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5963 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5964 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5965 | PHINode *PN; |
| 5966 | |
| 5967 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5968 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5969 | // emitted yet. |
| 5970 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5971 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5972 | // Ignore dead phi's. |
| 5973 | if (PN->use_empty()) continue; |
| 5974 | |
| 5975 | unsigned Reg; |
| 5976 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5977 | |
| 5978 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 5979 | unsigned &RegOut = SDL->ConstantsOut[C]; |
| 5980 | if (RegOut == 0) { |
| 5981 | RegOut = FuncInfo->CreateRegForValue(C); |
| 5982 | SDL->CopyValueToVirtualRegister(C, RegOut); |
| 5983 | } |
| 5984 | Reg = RegOut; |
| 5985 | } else { |
| 5986 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 5987 | if (Reg == 0) { |
| 5988 | assert(isa<AllocaInst>(PHIOp) && |
| 5989 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 5990 | "Didn't codegen value into a register!??"); |
| 5991 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
| 5992 | SDL->CopyValueToVirtualRegister(PHIOp, Reg); |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | // Remember that this register needs to added to the machine PHI node as |
| 5997 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5998 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5999 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6000 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6001 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6002 | unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6003 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
| 6004 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 6005 | Reg += NumRegisters; |
| 6006 | } |
| 6007 | } |
| 6008 | } |
| 6009 | SDL->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6012 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 6013 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 6014 | /// creating SelectionDAG nodes. |
| 6015 | /// |
| 6016 | bool |
| 6017 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 6018 | FastISel *F) { |
| 6019 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6020 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6021 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6022 | unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size(); |
| 6023 | |
| 6024 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6025 | // from this block. |
| 6026 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6027 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6028 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6029 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6030 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6031 | // If this terminator has multiple identical successors (common for |
| 6032 | // switches), only handle each succ once. |
| 6033 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6034 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6035 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6036 | PHINode *PN; |
| 6037 | |
| 6038 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6039 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6040 | // emitted yet. |
| 6041 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6042 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6043 | // Ignore dead phi's. |
| 6044 | if (PN->use_empty()) continue; |
| 6045 | |
| 6046 | // Only handle legal types. Two interesting things to note here. First, |
| 6047 | // by bailing out early, we may leave behind some dead instructions, |
| 6048 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6049 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6050 | // use CreateRegForValue to create registers, so it always creates |
| 6051 | // exactly one register for each non-void instruction. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6052 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6053 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 6054 | // Promote MVT::i1. |
| 6055 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6056 | VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6057 | else { |
| 6058 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6059 | return false; |
| 6060 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
| 6063 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6064 | |
| 6065 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6066 | if (Reg == 0) { |
| 6067 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6068 | return false; |
| 6069 | } |
| 6070 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | return true; |
| 6075 | } |