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" |
| 20 | #include "llvm/CallingConv.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 24 | #include "llvm/InlineAsm.h" |
| 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Intrinsics.h" |
| 27 | #include "llvm/IntrinsicInst.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/FastISel.h" |
| 30 | #include "llvm/CodeGen/GCStrategy.h" |
| 31 | #include "llvm/CodeGen/GCMetadata.h" |
| 32 | #include "llvm/CodeGen/MachineFunction.h" |
| 33 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 34 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 35 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 36 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 37 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/DwarfWriter.h" |
| 41 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetRegisterInfo.h" |
| 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Target/TargetFrameInfo.h" |
| 45 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetOptions.h" |
| 49 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 50 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
| 52 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 53 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 54 | #include <algorithm> |
| 55 | using namespace llvm; |
| 56 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 57 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 58 | /// some float libcalls (6, 8 or 12 bits). |
| 59 | static unsigned LimitFloatPrecision; |
| 60 | |
| 61 | static cl::opt<unsigned, true> |
| 62 | LimitFPPrecision("limit-float-precision", |
| 63 | cl::desc("Generate low-precision inline sequences " |
| 64 | "for some float libcalls"), |
| 65 | cl::location(LimitFloatPrecision), |
| 66 | cl::init(0)); |
| 67 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 68 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 69 | /// of insertvalue or extractvalue indices that identify a member, return |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 70 | /// the linearized index of the start of the member. |
| 71 | /// |
| 72 | static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, |
| 73 | const unsigned *Indices, |
| 74 | const unsigned *IndicesEnd, |
| 75 | unsigned CurIndex = 0) { |
| 76 | // Base case: We're done. |
| 77 | if (Indices && Indices == IndicesEnd) |
| 78 | return CurIndex; |
| 79 | |
| 80 | // Given a struct type, recursively traverse the elements. |
| 81 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 82 | for (StructType::element_iterator EB = STy->element_begin(), |
| 83 | EI = EB, |
| 84 | EE = STy->element_end(); |
| 85 | EI != EE; ++EI) { |
| 86 | if (Indices && *Indices == unsigned(EI - EB)) |
| 87 | return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); |
| 88 | CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); |
| 89 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 90 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 91 | } |
| 92 | // Given an array type, recursively traverse the elements. |
| 93 | else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 94 | const Type *EltTy = ATy->getElementType(); |
| 95 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 96 | if (Indices && *Indices == i) |
| 97 | return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); |
| 98 | CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); |
| 99 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 100 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 101 | } |
| 102 | // We haven't found the type we're looking for, so keep searching. |
| 103 | return CurIndex + 1; |
| 104 | } |
| 105 | |
| 106 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 107 | /// MVTs that represent all the individual underlying |
| 108 | /// non-aggregate types that comprise it. |
| 109 | /// |
| 110 | /// If Offsets is non-null, it points to a vector to be filled in |
| 111 | /// with the in-memory offsets of each of the individual values. |
| 112 | /// |
| 113 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
| 114 | SmallVectorImpl<MVT> &ValueVTs, |
| 115 | SmallVectorImpl<uint64_t> *Offsets = 0, |
| 116 | uint64_t StartingOffset = 0) { |
| 117 | // Given a struct type, recursively traverse the elements. |
| 118 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 119 | const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); |
| 120 | for (StructType::element_iterator EB = STy->element_begin(), |
| 121 | EI = EB, |
| 122 | EE = STy->element_end(); |
| 123 | EI != EE; ++EI) |
| 124 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 125 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 126 | return; |
| 127 | } |
| 128 | // Given an array type, recursively traverse the elements. |
| 129 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 130 | const Type *EltTy = ATy->getElementType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 131 | uint64_t EltSize = TLI.getTargetData()->getTypePaddedSize(EltTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 132 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 133 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 134 | StartingOffset + i * EltSize); |
| 135 | return; |
| 136 | } |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 137 | // Interpret void as zero return values. |
| 138 | if (Ty == Type::VoidTy) |
| 139 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 140 | // Base case: we can get an MVT for this LLVM IR type. |
| 141 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 142 | if (Offsets) |
| 143 | Offsets->push_back(StartingOffset); |
| 144 | } |
| 145 | |
Dan Gohman | 2a7c671 | 2008-09-03 23:18:39 +0000 | [diff] [blame] | 146 | namespace llvm { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 147 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 148 | /// that a particular set of values is assigned, and the type information about |
| 149 | /// the value. The most common situation is to represent one value at a time, |
| 150 | /// but struct or array values are handled element-wise as multiple values. |
| 151 | /// The splitting of aggregates is performed recursively, so that we never |
| 152 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 153 | /// have legal types, so each value may require one or more registers of some |
| 154 | /// legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 155 | /// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 156 | struct VISIBILITY_HIDDEN RegsForValue { |
| 157 | /// TLI - The TargetLowering object. |
| 158 | /// |
| 159 | const TargetLowering *TLI; |
| 160 | |
| 161 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 162 | /// may need be promoted or synthesized from one or more registers. |
| 163 | /// |
| 164 | SmallVector<MVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 165 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 166 | /// RegVTs - The value types of the registers. This is the same size as |
| 167 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 168 | /// register or registers are. (Individual values are never synthesized |
| 169 | /// from more than one type of register.) |
| 170 | /// |
| 171 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 172 | /// getRegisterType member function, however when with physical registers |
| 173 | /// it is necessary to have a separate record of the types. |
| 174 | /// |
| 175 | SmallVector<MVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 176 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 177 | /// Regs - This list holds the registers assigned to the values. |
| 178 | /// Each legal or promoted value requires one register, and each |
| 179 | /// expanded value requires multiple registers. |
| 180 | /// |
| 181 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 182 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 183 | RegsForValue() : TLI(0) {} |
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(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 186 | const SmallVector<unsigned, 4> ®s, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 187 | MVT regvt, MVT valuevt) |
| 188 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 189 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 190 | const SmallVector<unsigned, 4> ®s, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | const SmallVector<MVT, 4> ®vts, |
| 192 | const SmallVector<MVT, 4> &valuevts) |
| 193 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
| 194 | RegsForValue(const TargetLowering &tli, |
| 195 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 196 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 197 | |
| 198 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 199 | MVT ValueVT = ValueVTs[Value]; |
| 200 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 201 | MVT RegisterVT = TLI->getRegisterType(ValueVT); |
| 202 | for (unsigned i = 0; i != NumRegs; ++i) |
| 203 | Regs.push_back(Reg + i); |
| 204 | RegVTs.push_back(RegisterVT); |
| 205 | Reg += NumRegs; |
| 206 | } |
| 207 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 208 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 209 | /// append - Add the specified values to this one. |
| 210 | void append(const RegsForValue &RHS) { |
| 211 | TLI = RHS.TLI; |
| 212 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 213 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 214 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 215 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 216 | |
| 217 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 218 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 219 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 220 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 221 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 222 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 223 | SDValue &Chain, SDValue *Flag) const; |
| 224 | |
| 225 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 226 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 227 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 228 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 229 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 230 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 231 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 232 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 233 | /// operand list. This adds the code marker, matching input operand index |
| 234 | /// (if applicable), and includes the number of values added into it. |
| 235 | void AddInlineAsmOperands(unsigned Code, |
| 236 | bool HasMatching, unsigned MatchingIdx, |
| 237 | SelectionDAG &DAG, std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 238 | }; |
| 239 | } |
| 240 | |
| 241 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 242 | /// 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] | 243 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
| 244 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 245 | if (isa<PHINode>(I)) return true; |
| 246 | BasicBlock *BB = I->getParent(); |
| 247 | 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] | 248 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 249 | return true; |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 254 | /// entry block, return true. This includes arguments used by switches, since |
| 255 | /// the switch may expand into multiple basic blocks. |
| 256 | static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { |
| 257 | // With FastISel active, we may be splitting blocks, so force creation |
| 258 | // of virtual registers for all non-dead arguments. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 259 | // Don't force virtual registers for byval arguments though, because |
| 260 | // fast-isel can't handle those in all cases. |
| 261 | if (EnableFastISel && !A->hasByValAttr()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 262 | return A->use_empty(); |
| 263 | |
| 264 | BasicBlock *Entry = A->getParent()->begin(); |
| 265 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 266 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 267 | return false; // Use not in entry block. |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) |
| 272 | : TLI(tli) { |
| 273 | } |
| 274 | |
| 275 | void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 276 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 277 | bool EnableFastISel) { |
| 278 | Fn = &fn; |
| 279 | MF = &mf; |
| 280 | RegInfo = &MF->getRegInfo(); |
| 281 | |
| 282 | // Create a vreg for each argument register that is not dead and is used |
| 283 | // outside of the entry block for the function. |
| 284 | for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
| 285 | AI != E; ++AI) |
| 286 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 287 | InitializeRegForValue(AI); |
| 288 | |
| 289 | // Initialize the mapping of values to registers. This is only set up for |
| 290 | // instruction values that are used outside of the block that defines |
| 291 | // them. |
| 292 | Function::iterator BB = Fn->begin(), EB = Fn->end(); |
| 293 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 294 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 295 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 296 | const Type *Ty = AI->getAllocatedType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 297 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 298 | unsigned Align = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 299 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 300 | AI->getAlignment()); |
| 301 | |
| 302 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 303 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 304 | StaticAllocaMap[AI] = |
| 305 | MF->getFrameInfo()->CreateStackObject(TySize, Align); |
| 306 | } |
| 307 | |
| 308 | for (; BB != EB; ++BB) |
| 309 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 310 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 311 | if (!isa<AllocaInst>(I) || |
| 312 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 313 | InitializeRegForValue(I); |
| 314 | |
| 315 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 316 | // also creates the initial PHI MachineInstrs, though none of the input |
| 317 | // operands are populated. |
| 318 | for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { |
| 319 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 320 | MBBMap[BB] = MBB; |
| 321 | MF->push_back(MBB); |
| 322 | |
| 323 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 324 | // appropriate. |
| 325 | PHINode *PN; |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 326 | DebugLoc DL; |
| 327 | for (BasicBlock::iterator |
| 328 | I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 329 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 330 | if (Function *F = CI->getCalledFunction()) { |
| 331 | switch (F->getIntrinsicID()) { |
| 332 | default: break; |
| 333 | case Intrinsic::dbg_stoppoint: { |
| 334 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 335 | DbgStopPointInst *SPI = cast<DbgStopPointInst>(I); |
| 336 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 337 | if (DW && DW->ValidDebugInfo(SPI->getContext(), |
| 338 | CodeGenOpt::Default)) { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 339 | DICompileUnit CU(cast<GlobalVariable>(SPI->getContext())); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 340 | unsigned idx = MF->getOrCreateDebugLocID(CU.getGV(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 341 | SPI->getLine(), |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 342 | SPI->getColumn()); |
| 343 | DL = DebugLoc::get(idx); |
| 344 | } |
| 345 | |
| 346 | break; |
| 347 | } |
| 348 | case Intrinsic::dbg_func_start: { |
| 349 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 350 | if (DW) { |
| 351 | DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I); |
| 352 | Value *SP = FSI->getSubprogram(); |
| 353 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 354 | if (DW->ValidDebugInfo(SP, CodeGenOpt::Default)) { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 355 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 356 | DICompileUnit CU(Subprogram.getCompileUnit()); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 357 | unsigned Line = Subprogram.getLineNumber(); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 358 | DL = DebugLoc::get(MF->getOrCreateDebugLocID(CU.getGV(), |
| 359 | Line, 0)); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 362 | |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | PN = dyn_cast<PHINode>(I); |
| 370 | if (!PN || PN->use_empty()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 371 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 372 | unsigned PHIReg = ValueMap[PN]; |
| 373 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 374 | |
| 375 | SmallVector<MVT, 4> ValueVTs; |
| 376 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 377 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 378 | MVT VT = ValueVTs[vti]; |
| 379 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 380 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 381 | for (unsigned i = 0; i != NumRegisters; ++i) |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 382 | BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 383 | PHIReg += NumRegisters; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | unsigned FunctionLoweringInfo::MakeReg(MVT VT) { |
| 390 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 391 | } |
| 392 | |
| 393 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 394 | /// the correctly promoted or expanded types. Assign these registers |
| 395 | /// consecutive vreg numbers and return the first assigned number. |
| 396 | /// |
| 397 | /// In the case that the given value has struct or array type, this function |
| 398 | /// will assign registers for each member or element. |
| 399 | /// |
| 400 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 401 | SmallVector<MVT, 4> ValueVTs; |
| 402 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
| 403 | |
| 404 | unsigned FirstReg = 0; |
| 405 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 406 | MVT ValueVT = ValueVTs[Value]; |
| 407 | MVT RegisterVT = TLI.getRegisterType(ValueVT); |
| 408 | |
| 409 | unsigned NumRegs = TLI.getNumRegisters(ValueVT); |
| 410 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 411 | unsigned R = MakeReg(RegisterVT); |
| 412 | if (!FirstReg) FirstReg = R; |
| 413 | } |
| 414 | } |
| 415 | return FirstReg; |
| 416 | } |
| 417 | |
| 418 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 419 | /// combined into the value they represent. If the parts combine to a type |
| 420 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 421 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 422 | /// (ISD::AssertSext). |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 423 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
| 424 | const SDValue *Parts, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 425 | unsigned NumParts, MVT PartVT, MVT ValueVT, |
| 426 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 427 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 428 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 429 | SDValue Val = Parts[0]; |
| 430 | |
| 431 | if (NumParts > 1) { |
| 432 | // Assemble the value from multiple parts. |
| 433 | if (!ValueVT.isVector()) { |
| 434 | unsigned PartBits = PartVT.getSizeInBits(); |
| 435 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 436 | |
| 437 | // Assemble the power of 2 part. |
| 438 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 439 | 1 << Log2_32(NumParts) : NumParts; |
| 440 | unsigned RoundBits = PartBits * RoundParts; |
| 441 | MVT RoundVT = RoundBits == ValueBits ? |
| 442 | ValueVT : MVT::getIntegerVT(RoundBits); |
| 443 | SDValue Lo, Hi; |
| 444 | |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 445 | MVT HalfVT = ValueVT.isInteger() ? |
| 446 | MVT::getIntegerVT(RoundBits/2) : |
| 447 | MVT::getFloatingPointVT(RoundBits/2); |
| 448 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 449 | if (RoundParts > 2) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 450 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT); |
| 451 | Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 452 | PartVT, HalfVT); |
| 453 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 454 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 455 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 456 | } |
| 457 | if (TLI.isBigEndian()) |
| 458 | std::swap(Lo, Hi); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 459 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 460 | |
| 461 | if (RoundParts < NumParts) { |
| 462 | // Assemble the trailing non-power-of-2 part. |
| 463 | unsigned OddParts = NumParts - RoundParts; |
| 464 | MVT OddVT = MVT::getIntegerVT(OddParts * PartBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 465 | Hi = getCopyFromParts(DAG, dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 466 | Parts+RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 467 | |
| 468 | // Combine the round and odd parts. |
| 469 | Lo = Val; |
| 470 | if (TLI.isBigEndian()) |
| 471 | std::swap(Lo, Hi); |
| 472 | MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 473 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 474 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 475 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 476 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 477 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 478 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 479 | } |
| 480 | } else { |
| 481 | // Handle a multi-element vector. |
| 482 | MVT IntermediateVT, RegisterVT; |
| 483 | unsigned NumIntermediates; |
| 484 | unsigned NumRegs = |
| 485 | TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 486 | RegisterVT); |
| 487 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 488 | NumParts = NumRegs; // Silence a compiler warning. |
| 489 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 490 | assert(RegisterVT == Parts[0].getValueType() && |
| 491 | "Part type doesn't match part!"); |
| 492 | |
| 493 | // Assemble the parts into intermediate operands. |
| 494 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 495 | if (NumIntermediates == NumParts) { |
| 496 | // If the register was not expanded, truncate or copy the value, |
| 497 | // as appropriate. |
| 498 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 499 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 500 | PartVT, IntermediateVT); |
| 501 | } else if (NumParts > 0) { |
| 502 | // If the intermediate type was expanded, build the intermediate operands |
| 503 | // from the parts. |
| 504 | assert(NumParts % NumIntermediates == 0 && |
| 505 | "Must expand into a divisible number of parts!"); |
| 506 | unsigned Factor = NumParts / NumIntermediates; |
| 507 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 508 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 509 | PartVT, IntermediateVT); |
| 510 | } |
| 511 | |
| 512 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 513 | // operands. |
| 514 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 515 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 516 | ValueVT, &Ops[0], NumIntermediates); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 521 | PartVT = Val.getValueType(); |
| 522 | |
| 523 | if (PartVT == ValueVT) |
| 524 | return Val; |
| 525 | |
| 526 | if (PartVT.isVector()) { |
| 527 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 528 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (ValueVT.isVector()) { |
| 532 | assert(ValueVT.getVectorElementType() == PartVT && |
| 533 | ValueVT.getVectorNumElements() == 1 && |
| 534 | "Only trivial scalar-to-vector conversions should get here!"); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 535 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (PartVT.isInteger() && |
| 539 | ValueVT.isInteger()) { |
| 540 | if (ValueVT.bitsLT(PartVT)) { |
| 541 | // For a truncate, see if we have any information to |
| 542 | // indicate whether the truncated bits will always be |
| 543 | // zero or sign-extension. |
| 544 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 545 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 546 | DAG.getValueType(ValueVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 547 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 548 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 549 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 554 | if (ValueVT.bitsLT(Val.getValueType())) |
| 555 | // FP_ROUND's are always exact here. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 556 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 557 | DAG.getIntPtrConstant(1)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 558 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 562 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 563 | |
| 564 | assert(0 && "Unknown mismatch!"); |
| 565 | return SDValue(); |
| 566 | } |
| 567 | |
| 568 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 569 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 570 | /// 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] | 571 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val, |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 572 | SDValue *Parts, unsigned NumParts, MVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 573 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 574 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 575 | MVT PtrVT = TLI.getPointerTy(); |
| 576 | MVT ValueVT = Val.getValueType(); |
| 577 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 578 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 579 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 580 | |
| 581 | if (!NumParts) |
| 582 | return; |
| 583 | |
| 584 | if (!ValueVT.isVector()) { |
| 585 | if (PartVT == ValueVT) { |
| 586 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 587 | Parts[0] = Val; |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 592 | // If the parts cover more bits than the value has, promote the value. |
| 593 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 594 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 595 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 596 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 597 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 598 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 599 | } else { |
| 600 | assert(0 && "Unknown mismatch!"); |
| 601 | } |
| 602 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 603 | // Different types of the same size. |
| 604 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 605 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 606 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 607 | // If the parts cover less bits than value has, truncate the value. |
| 608 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 609 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 610 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 611 | } else { |
| 612 | assert(0 && "Unknown mismatch!"); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // The value may have changed - recompute ValueVT. |
| 617 | ValueVT = Val.getValueType(); |
| 618 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 619 | "Failed to tile the value with PartVT!"); |
| 620 | |
| 621 | if (NumParts == 1) { |
| 622 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 623 | Parts[0] = Val; |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | // Expand the value into multiple parts. |
| 628 | if (NumParts & (NumParts - 1)) { |
| 629 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 630 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 631 | "Do not know what to expand to!"); |
| 632 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 633 | unsigned RoundBits = RoundParts * PartBits; |
| 634 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 635 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 636 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 637 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 638 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 639 | if (TLI.isBigEndian()) |
| 640 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 641 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 642 | NumParts = RoundParts; |
| 643 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 644 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 648 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 649 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 650 | MVT::getIntegerVT(ValueVT.getSizeInBits()), |
| 651 | Val); |
| 652 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 653 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 654 | unsigned ThisBits = StepSize * PartBits / 2; |
| 655 | MVT ThisVT = MVT::getIntegerVT (ThisBits); |
| 656 | SDValue &Part0 = Parts[i]; |
| 657 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 658 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 659 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 660 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 661 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 662 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 663 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 664 | DAG.getConstant(0, PtrVT)); |
| 665 | |
| 666 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 667 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 668 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 669 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 670 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 676 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 677 | |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | // Vector ValueVT. |
| 682 | if (NumParts == 1) { |
| 683 | if (PartVT != ValueVT) { |
| 684 | if (PartVT.isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 685 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 686 | } else { |
| 687 | assert(ValueVT.getVectorElementType() == PartVT && |
| 688 | ValueVT.getVectorNumElements() == 1 && |
| 689 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 690 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 691 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 692 | DAG.getConstant(0, PtrVT)); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | Parts[0] = Val; |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | // Handle a multi-element vector. |
| 701 | MVT IntermediateVT, RegisterVT; |
| 702 | unsigned NumIntermediates; |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 703 | unsigned NumRegs = TLI |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 704 | .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 705 | RegisterVT); |
| 706 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 707 | |
| 708 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 709 | NumParts = NumRegs; // Silence a compiler warning. |
| 710 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 711 | |
| 712 | // Split the vector into intermediate operands. |
| 713 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 714 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 715 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 716 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 717 | IntermediateVT, Val, |
| 718 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 719 | PtrVT)); |
| 720 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 721 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 722 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 723 | DAG.getConstant(i, PtrVT)); |
| 724 | |
| 725 | // Split the intermediate operands into legal parts. |
| 726 | if (NumParts == NumIntermediates) { |
| 727 | // If the register was not expanded, promote or copy the value, |
| 728 | // as appropriate. |
| 729 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 730 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 731 | } else if (NumParts > 0) { |
| 732 | // If the intermediate type was expanded, split each the value into |
| 733 | // legal parts. |
| 734 | assert(NumParts % NumIntermediates == 0 && |
| 735 | "Must expand into a divisible number of parts!"); |
| 736 | unsigned Factor = NumParts / NumIntermediates; |
| 737 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 738 | getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | |
| 743 | void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 744 | AA = &aa; |
| 745 | GFI = gfi; |
| 746 | TD = DAG.getTarget().getTargetData(); |
| 747 | } |
| 748 | |
| 749 | /// clear - Clear out the curret SelectionDAG and the associated |
| 750 | /// state and prepare this SelectionDAGLowering object to be used |
| 751 | /// for a new block. This doesn't clear out information about |
| 752 | /// additional blocks that are needed to complete switch lowering |
| 753 | /// or PHI node updating; that information is cleared out as it is |
| 754 | /// consumed. |
| 755 | void SelectionDAGLowering::clear() { |
| 756 | NodeMap.clear(); |
| 757 | PendingLoads.clear(); |
| 758 | PendingExports.clear(); |
| 759 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 760 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 764 | /// flushing any PendingLoad items. This must be done before emitting |
| 765 | /// a store or any other node that may need to be ordered after any |
| 766 | /// prior load instructions. |
| 767 | /// |
| 768 | SDValue SelectionDAGLowering::getRoot() { |
| 769 | if (PendingLoads.empty()) |
| 770 | return DAG.getRoot(); |
| 771 | |
| 772 | if (PendingLoads.size() == 1) { |
| 773 | SDValue Root = PendingLoads[0]; |
| 774 | DAG.setRoot(Root); |
| 775 | PendingLoads.clear(); |
| 776 | return Root; |
| 777 | } |
| 778 | |
| 779 | // Otherwise, we have to make a token factor node. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 780 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 781 | &PendingLoads[0], PendingLoads.size()); |
| 782 | PendingLoads.clear(); |
| 783 | DAG.setRoot(Root); |
| 784 | return Root; |
| 785 | } |
| 786 | |
| 787 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 788 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 789 | /// to do this before emitting a terminator instruction. |
| 790 | /// |
| 791 | SDValue SelectionDAGLowering::getControlRoot() { |
| 792 | SDValue Root = DAG.getRoot(); |
| 793 | |
| 794 | if (PendingExports.empty()) |
| 795 | return Root; |
| 796 | |
| 797 | // Turn all of the CopyToReg chains into one factored node. |
| 798 | if (Root.getOpcode() != ISD::EntryToken) { |
| 799 | unsigned i = 0, e = PendingExports.size(); |
| 800 | for (; i != e; ++i) { |
| 801 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 802 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 803 | break; // Don't add the root if we already indirectly depend on it. |
| 804 | } |
| 805 | |
| 806 | if (i == e) |
| 807 | PendingExports.push_back(Root); |
| 808 | } |
| 809 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 810 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 811 | &PendingExports[0], |
| 812 | PendingExports.size()); |
| 813 | PendingExports.clear(); |
| 814 | DAG.setRoot(Root); |
| 815 | return Root; |
| 816 | } |
| 817 | |
| 818 | void SelectionDAGLowering::visit(Instruction &I) { |
| 819 | visit(I.getOpcode(), I); |
| 820 | } |
| 821 | |
| 822 | void SelectionDAGLowering::visit(unsigned Opcode, User &I) { |
| 823 | // Note: this doesn't use InstVisitor, because it has to work with |
| 824 | // ConstantExpr's in addition to instructions. |
| 825 | switch (Opcode) { |
| 826 | default: assert(0 && "Unknown instruction type encountered!"); |
| 827 | abort(); |
| 828 | // Build the switch statement using the Instruction.def file. |
| 829 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 830 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 831 | #include "llvm/Instruction.def" |
| 832 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 833 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 834 | |
| 835 | void SelectionDAGLowering::visitAdd(User &I) { |
| 836 | if (I.getType()->isFPOrFPVector()) |
| 837 | visitBinary(I, ISD::FADD); |
| 838 | else |
| 839 | visitBinary(I, ISD::ADD); |
| 840 | } |
| 841 | |
| 842 | void SelectionDAGLowering::visitMul(User &I) { |
| 843 | if (I.getType()->isFPOrFPVector()) |
| 844 | visitBinary(I, ISD::FMUL); |
| 845 | else |
| 846 | visitBinary(I, ISD::MUL); |
| 847 | } |
| 848 | |
| 849 | SDValue SelectionDAGLowering::getValue(const Value *V) { |
| 850 | SDValue &N = NodeMap[V]; |
| 851 | if (N.getNode()) return N; |
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 (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 854 | MVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 855 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 856 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 857 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 858 | |
| 859 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 860 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 861 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 862 | if (isa<ConstantPointerNull>(C)) |
| 863 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 864 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 865 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 866 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 867 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 868 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 869 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 870 | |
| 871 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 872 | visit(CE->getOpcode(), *CE); |
| 873 | SDValue N1 = NodeMap[V]; |
| 874 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 875 | return N1; |
| 876 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 877 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 878 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 879 | SmallVector<SDValue, 4> Constants; |
| 880 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 881 | OI != OE; ++OI) { |
| 882 | SDNode *Val = getValue(*OI).getNode(); |
| 883 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 884 | Constants.push_back(SDValue(Val, i)); |
| 885 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 886 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 887 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 891 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 892 | "Unknown struct or array constant!"); |
| 893 | |
| 894 | SmallVector<MVT, 4> ValueVTs; |
| 895 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 896 | unsigned NumElts = ValueVTs.size(); |
| 897 | if (NumElts == 0) |
| 898 | return SDValue(); // empty struct |
| 899 | SmallVector<SDValue, 4> Constants(NumElts); |
| 900 | for (unsigned i = 0; i != NumElts; ++i) { |
| 901 | MVT EltVT = ValueVTs[i]; |
| 902 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 903 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 904 | else if (EltVT.isFloatingPoint()) |
| 905 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 906 | else |
| 907 | Constants[i] = DAG.getConstant(0, EltVT); |
| 908 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 909 | return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 913 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 914 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 915 | // Now that we know the number and type of the elements, get that number of |
| 916 | // elements into the Ops array based on what kind of constant it is. |
| 917 | SmallVector<SDValue, 16> Ops; |
| 918 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 919 | for (unsigned i = 0; i != NumElements; ++i) |
| 920 | Ops.push_back(getValue(CP->getOperand(i))); |
| 921 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 922 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 923 | MVT EltVT = TLI.getValueType(VecTy->getElementType()); |
| 924 | |
| 925 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 926 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 927 | Op = DAG.getConstantFP(0, EltVT); |
| 928 | else |
| 929 | Op = DAG.getConstant(0, EltVT); |
| 930 | Ops.assign(NumElements, Op); |
| 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 | // Create a BUILD_VECTOR node. |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 934 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 935 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 936 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 937 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 938 | // If this is a static alloca, generate it as the frameindex instead of |
| 939 | // computation. |
| 940 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 941 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 942 | FuncInfo.StaticAllocaMap.find(AI); |
| 943 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 944 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 945 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 946 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 947 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 948 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 950 | RegsForValue RFV(TLI, InReg, V->getType()); |
| 951 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 952 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | |
| 956 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 957 | if (I.getNumOperands() == 0) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 958 | DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 959 | MVT::Other, getControlRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 960 | return; |
| 961 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 962 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 963 | SmallVector<SDValue, 8> NewValues; |
| 964 | NewValues.push_back(getControlRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 965 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 966 | SmallVector<MVT, 4> ValueVTs; |
| 967 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 968 | unsigned NumValues = ValueVTs.size(); |
| 969 | if (NumValues == 0) continue; |
| 970 | |
| 971 | SDValue RetOp = getValue(I.getOperand(i)); |
| 972 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 973 | MVT VT = ValueVTs[j]; |
| 974 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 975 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 976 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 977 | const Function *F = I.getParent()->getParent(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 978 | if (F->paramHasAttr(0, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 979 | ExtendKind = ISD::SIGN_EXTEND; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 980 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 981 | ExtendKind = ISD::ZERO_EXTEND; |
| 982 | |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 983 | // FIXME: C calling convention requires the return type to be promoted to |
| 984 | // at least 32-bit. But this is not necessary for non-C calling |
| 985 | // conventions. The frontend should mark functions whose return values |
| 986 | // require promoting with signext or zeroext attributes. |
| 987 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 988 | MVT MinVT = TLI.getRegisterType(MVT::i32); |
| 989 | if (VT.bitsLT(MinVT)) |
| 990 | VT = MinVT; |
| 991 | } |
| 992 | |
| 993 | unsigned NumParts = TLI.getNumRegisters(VT); |
| 994 | MVT PartVT = TLI.getRegisterType(VT); |
| 995 | SmallVector<SDValue, 4> Parts(NumParts); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 996 | getCopyToParts(DAG, getCurDebugLoc(), |
| 997 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 998 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 999 | |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1000 | // 'inreg' on function refers to return value |
| 1001 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1002 | if (F->paramHasAttr(0, Attribute::InReg)) |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1003 | Flags.setInReg(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1004 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1005 | NewValues.push_back(Parts[i]); |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1006 | NewValues.push_back(DAG.getArgFlags(Flags)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1010 | DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1011 | &NewValues[0], NewValues.size())); |
| 1012 | } |
| 1013 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1014 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1015 | /// created for it, emit nodes to copy the value into the virtual |
| 1016 | /// registers. |
| 1017 | void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) { |
| 1018 | if (!V->use_empty()) { |
| 1019 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1020 | if (VMI != FuncInfo.ValueMap.end()) |
| 1021 | CopyValueToVirtualRegister(V, VMI->second); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1025 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1026 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1027 | /// CopyTo/FromReg. |
| 1028 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 1029 | // No need to export constants. |
| 1030 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1031 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1032 | // Already exported? |
| 1033 | if (FuncInfo.isExportedInst(V)) return; |
| 1034 | |
| 1035 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1036 | CopyValueToVirtualRegister(V, Reg); |
| 1037 | } |
| 1038 | |
| 1039 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 1040 | const BasicBlock *FromBB) { |
| 1041 | // The operands of the setcc have to be in this block. We don't know |
| 1042 | // how to export them from some other block. |
| 1043 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 1044 | // Can export from current BB. |
| 1045 | if (VI->getParent() == FromBB) |
| 1046 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1047 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1048 | // Is already exported, noop. |
| 1049 | return FuncInfo.isExportedInst(V); |
| 1050 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1051 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1052 | // If this is an argument, we can export it if the BB is the entry block or |
| 1053 | // if it is already exported. |
| 1054 | if (isa<Argument>(V)) { |
| 1055 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1056 | return true; |
| 1057 | |
| 1058 | // Otherwise, can only export this if it is already exported. |
| 1059 | return FuncInfo.isExportedInst(V); |
| 1060 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1061 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1062 | // Otherwise, constants can always be exported. |
| 1063 | return true; |
| 1064 | } |
| 1065 | |
| 1066 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1067 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1068 | return I->getParent() == BB; |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1072 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 1073 | /// the given LLVM IR floating-point condition code. This includes |
| 1074 | /// consideration of global floating-point math flags. |
| 1075 | /// |
| 1076 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 1077 | ISD::CondCode FPC, FOC; |
| 1078 | switch (Pred) { |
| 1079 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1080 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1081 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1082 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1083 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1084 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1085 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1086 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1087 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 1088 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1089 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1090 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1091 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1092 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1093 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1094 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1095 | default: |
| 1096 | assert(0 && "Invalid FCmp predicate opcode!"); |
| 1097 | FOC = FPC = ISD::SETFALSE; |
| 1098 | break; |
| 1099 | } |
| 1100 | if (FiniteOnlyFPMath()) |
| 1101 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1102 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1103 | return FPC; |
| 1104 | } |
| 1105 | |
| 1106 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1107 | /// the given LLVM IR integer condition code. |
| 1108 | /// |
| 1109 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1110 | switch (Pred) { |
| 1111 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1112 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1113 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1114 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1115 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1116 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1117 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1118 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1119 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1120 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1121 | default: |
| 1122 | assert(0 && "Invalid ICmp predicate opcode!"); |
| 1123 | return ISD::SETNE; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1127 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1128 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1129 | /// AND operator tree. |
| 1130 | /// |
| 1131 | void |
| 1132 | SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond, |
| 1133 | MachineBasicBlock *TBB, |
| 1134 | MachineBasicBlock *FBB, |
| 1135 | MachineBasicBlock *CurBB) { |
| 1136 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1137 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1138 | // If the leaf of the tree is a comparison, merge the condition into |
| 1139 | // the caseblock. |
| 1140 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1141 | // The operands of the cmp have to be in this block. We don't know |
| 1142 | // how to export them from some other block. If this is the first block |
| 1143 | // of the sequence, no exporting is needed. |
| 1144 | if (CurBB == CurMBB || |
| 1145 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1146 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1147 | ISD::CondCode Condition; |
| 1148 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1149 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1150 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1151 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1152 | } else { |
| 1153 | Condition = ISD::SETEQ; // silence warning. |
| 1154 | assert(0 && "Unknown compare instruction"); |
| 1155 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1156 | |
| 1157 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1158 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1159 | SwitchCases.push_back(CB); |
| 1160 | return; |
| 1161 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | // Create a CaseBlock record representing this branch. |
| 1165 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(), |
| 1166 | NULL, TBB, FBB, CurBB); |
| 1167 | SwitchCases.push_back(CB); |
| 1168 | } |
| 1169 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1170 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1171 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 1172 | MachineBasicBlock *TBB, |
| 1173 | MachineBasicBlock *FBB, |
| 1174 | MachineBasicBlock *CurBB, |
| 1175 | unsigned Opc) { |
| 1176 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1177 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1178 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1179 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1180 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1181 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1182 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1183 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1186 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1187 | // Create TmpBB after CurBB. |
| 1188 | MachineFunction::iterator BBI = CurBB; |
| 1189 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1190 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1191 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1192 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1193 | if (Opc == Instruction::Or) { |
| 1194 | // Codegen X | Y as: |
| 1195 | // jmp_if_X TBB |
| 1196 | // jmp TmpBB |
| 1197 | // TmpBB: |
| 1198 | // jmp_if_Y TBB |
| 1199 | // jmp FBB |
| 1200 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1201 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1202 | // Emit the LHS condition. |
| 1203 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1204 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1205 | // Emit the RHS condition into TmpBB. |
| 1206 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1207 | } else { |
| 1208 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1209 | // Codegen X & Y as: |
| 1210 | // jmp_if_X TmpBB |
| 1211 | // jmp FBB |
| 1212 | // TmpBB: |
| 1213 | // jmp_if_Y TBB |
| 1214 | // jmp FBB |
| 1215 | // |
| 1216 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1217 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1218 | // Emit the LHS condition. |
| 1219 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1220 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1221 | // Emit the RHS condition into TmpBB. |
| 1222 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1227 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1228 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1229 | bool |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1230 | SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1231 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1232 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1233 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1234 | // will get folded into a single comparison, so don't emit two blocks. |
| 1235 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1236 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1237 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1238 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1239 | return false; |
| 1240 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1241 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1246 | // Update machine-CFG edges. |
| 1247 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1248 | |
| 1249 | // Figure out which block is immediately after the current one. |
| 1250 | MachineBasicBlock *NextBlock = 0; |
| 1251 | MachineFunction::iterator BBI = CurMBB; |
| 1252 | if (++BBI != CurMBB->getParent()->end()) |
| 1253 | NextBlock = BBI; |
| 1254 | |
| 1255 | if (I.isUnconditional()) { |
| 1256 | // Update machine-CFG edges. |
| 1257 | CurMBB->addSuccessor(Succ0MBB); |
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 | // If this is not a fall-through branch, emit the branch. |
| 1260 | if (Succ0MBB != NextBlock) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1261 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1262 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1263 | DAG.getBasicBlock(Succ0MBB))); |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | // If this condition is one of the special cases we handle, do special stuff |
| 1268 | // now. |
| 1269 | Value *CondVal = I.getCondition(); |
| 1270 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1271 | |
| 1272 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1273 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1274 | // For example, instead of something like: |
| 1275 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1276 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1277 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1278 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1279 | // or C, F |
| 1280 | // jnz foo |
| 1281 | // Emit: |
| 1282 | // cmp A, B |
| 1283 | // je foo |
| 1284 | // cmp D, E |
| 1285 | // jle foo |
| 1286 | // |
| 1287 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1288 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1289 | (BOp->getOpcode() == Instruction::And || |
| 1290 | BOp->getOpcode() == Instruction::Or)) { |
| 1291 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1292 | // If the compares in later blocks need to use values not currently |
| 1293 | // exported from this block, export them now. This block should always |
| 1294 | // be the first entry. |
| 1295 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1296 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1297 | // Allow some cases to be rejected. |
| 1298 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1299 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1300 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1301 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1302 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1303 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1304 | // Emit the branch for this block. |
| 1305 | visitSwitchCase(SwitchCases[0]); |
| 1306 | SwitchCases.erase(SwitchCases.begin()); |
| 1307 | return; |
| 1308 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1309 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1310 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1311 | // SwitchCases. |
| 1312 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1313 | CurMBB->getParent()->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1314 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1315 | SwitchCases.clear(); |
| 1316 | } |
| 1317 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1318 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1319 | // Create a CaseBlock record representing this branch. |
| 1320 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(), |
| 1321 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
| 1322 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1323 | // cond branch. |
| 1324 | visitSwitchCase(CB); |
| 1325 | } |
| 1326 | |
| 1327 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1328 | /// the binary search tree resulting from lowering a switch instruction. |
| 1329 | void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) { |
| 1330 | SDValue Cond; |
| 1331 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1332 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1335 | if (CB.CmpMHS == NULL) { |
| 1336 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1337 | // handle common cases produced by branch lowering. |
| 1338 | if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ) |
| 1339 | Cond = CondLHS; |
| 1340 | else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) { |
| 1341 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1342 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1343 | } else |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1344 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1345 | } else { |
| 1346 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1347 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1348 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1349 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1350 | |
| 1351 | SDValue CmpOp = getValue(CB.CmpMHS); |
| 1352 | MVT VT = CmpOp.getValueType(); |
| 1353 | |
| 1354 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1355 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1356 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1357 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1358 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1359 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1360 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1361 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1362 | } |
| 1363 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1364 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1365 | // Update successor info |
| 1366 | CurMBB->addSuccessor(CB.TrueBB); |
| 1367 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1368 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1369 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1370 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1371 | MachineBasicBlock *NextBlock = 0; |
| 1372 | MachineFunction::iterator BBI = CurMBB; |
| 1373 | if (++BBI != CurMBB->getParent()->end()) |
| 1374 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1375 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1376 | // If the lhs block is the next block, invert the condition so that we can |
| 1377 | // fall through to the lhs instead of the rhs block. |
| 1378 | if (CB.TrueBB == NextBlock) { |
| 1379 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1380 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1381 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1382 | } |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1383 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1384 | MVT::Other, getControlRoot(), Cond, |
| 1385 | DAG.getBasicBlock(CB.TrueBB)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1386 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1387 | // If the branch was constant folded, fix up the CFG. |
| 1388 | if (BrCond.getOpcode() == ISD::BR) { |
| 1389 | CurMBB->removeSuccessor(CB.FalseBB); |
| 1390 | DAG.setRoot(BrCond); |
| 1391 | } else { |
| 1392 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1393 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1394 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1395 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1396 | if (CB.FalseBB == NextBlock) |
| 1397 | DAG.setRoot(BrCond); |
| 1398 | else |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1399 | DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1400 | DAG.getBasicBlock(CB.FalseBB))); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1405 | void SelectionDAGLowering::visitJumpTable(JumpTable &JT) { |
| 1406 | // Emit the code for the jump table |
| 1407 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
| 1408 | MVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1409 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1410 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1411 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1412 | DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1413 | MVT::Other, Index.getValue(1), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1414 | Table, Index)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1418 | /// in the JumpTable from switch case. |
| 1419 | void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT, |
| 1420 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1421 | // Subtract the lowest switch case value from the value being switched on and |
| 1422 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1423 | // difference between smallest and largest cases. |
| 1424 | SDValue SwitchOp = getValue(JTH.SValue); |
| 1425 | MVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1426 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1427 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1428 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1429 | // The SDNode we just created, which holds the value being switched on minus |
| 1430 | // the the smallest case value, needs to be copied to a virtual register so it |
| 1431 | // can be used as an index into the jump table in a subsequent basic block. |
| 1432 | // This value may be smaller or larger than the target's pointer type, and |
| 1433 | // therefore require extension or truncating. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1434 | if (VT.bitsGT(TLI.getPointerTy())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1435 | SwitchOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1436 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1437 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1438 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1439 | TLI.getPointerTy(), SUB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1440 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1441 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1442 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1443 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1444 | JT.Reg = JumpTableReg; |
| 1445 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1446 | // Emit the range check for the jump table, and branch to the default block |
| 1447 | // for the switch statement if the value being switched on exceeds the largest |
| 1448 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1449 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
| 1450 | TLI.getSetCCResultType(SUB.getValueType()), SUB, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1451 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1452 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1455 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1456 | MachineBasicBlock *NextBlock = 0; |
| 1457 | MachineFunction::iterator BBI = CurMBB; |
| 1458 | if (++BBI != CurMBB->getParent()->end()) |
| 1459 | NextBlock = BBI; |
| 1460 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1461 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1462 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1463 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1464 | |
| 1465 | if (JT.MBB == NextBlock) |
| 1466 | DAG.setRoot(BrCond); |
| 1467 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1468 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1469 | DAG.getBasicBlock(JT.MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1473 | /// suitable for "bit tests" |
| 1474 | void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) { |
| 1475 | // Subtract the minimum value |
| 1476 | SDValue SwitchOp = getValue(B.SValue); |
| 1477 | MVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1478 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1479 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1482 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1483 | TLI.getSetCCResultType(SUB.getValueType()), |
| 1484 | SUB, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1485 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1486 | |
| 1487 | SDValue ShiftOp; |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1488 | if (VT.bitsGT(TLI.getPointerTy())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1489 | ShiftOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1490 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1491 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1492 | ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1493 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1494 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1495 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1496 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1497 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1498 | |
| 1499 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1500 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1501 | MachineBasicBlock *NextBlock = 0; |
| 1502 | MachineFunction::iterator BBI = CurMBB; |
| 1503 | if (++BBI != CurMBB->getParent()->end()) |
| 1504 | NextBlock = BBI; |
| 1505 | |
| 1506 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1507 | |
| 1508 | CurMBB->addSuccessor(B.Default); |
| 1509 | CurMBB->addSuccessor(MBB); |
| 1510 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1511 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1512 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1513 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1514 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1515 | if (MBB == NextBlock) |
| 1516 | DAG.setRoot(BrRange); |
| 1517 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1518 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1519 | DAG.getBasicBlock(MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /// visitBitTestCase - this function produces one "bit test" |
| 1523 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1524 | unsigned Reg, |
| 1525 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1526 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1527 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1528 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1529 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1530 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1531 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1532 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1533 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1534 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1535 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1536 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1537 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1538 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1539 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1540 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1541 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1542 | |
| 1543 | CurMBB->addSuccessor(B.TargetBB); |
| 1544 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1545 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1546 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1547 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1548 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1551 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1552 | MachineBasicBlock *NextBlock = 0; |
| 1553 | MachineFunction::iterator BBI = CurMBB; |
| 1554 | if (++BBI != CurMBB->getParent()->end()) |
| 1555 | NextBlock = BBI; |
| 1556 | |
| 1557 | if (NextMBB == NextBlock) |
| 1558 | DAG.setRoot(BrAnd); |
| 1559 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1560 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1561 | DAG.getBasicBlock(NextMBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1565 | // Retrieve successors. |
| 1566 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1567 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1568 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1569 | const Value *Callee(I.getCalledValue()); |
| 1570 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1571 | visitInlineAsm(&I); |
| 1572 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1573 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1574 | |
| 1575 | // If the value of the invoke is used outside of its defining block, make it |
| 1576 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1577 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Update successor info |
| 1580 | CurMBB->addSuccessor(Return); |
| 1581 | CurMBB->addSuccessor(LandingPad); |
| 1582 | |
| 1583 | // Drop into normal successor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1584 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1585 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1586 | DAG.getBasicBlock(Return))); |
| 1587 | } |
| 1588 | |
| 1589 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1590 | } |
| 1591 | |
| 1592 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1593 | /// small case ranges). |
| 1594 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
| 1595 | CaseRecVector& WorkList, |
| 1596 | Value* SV, |
| 1597 | MachineBasicBlock* Default) { |
| 1598 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1599 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1600 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1601 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1602 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1603 | return false; |
| 1604 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1605 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1606 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1607 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Figure out which block is immediately after the current one. |
| 1610 | MachineBasicBlock *NextBlock = 0; |
| 1611 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1612 | |
| 1613 | if (++BBI != CurMBB->getParent()->end()) |
| 1614 | NextBlock = BBI; |
| 1615 | |
| 1616 | // TODO: If any two of the cases has the same destination, and if one value |
| 1617 | // is the same as the other, but has one bit unset that the other has set, |
| 1618 | // use bit manipulation to do two compares at once. For example: |
| 1619 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1620 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1621 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1622 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1623 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1624 | // branches in this order. See if rearranging a case value would help. |
| 1625 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1626 | if (I->BB == NextBlock) { |
| 1627 | std::swap(*I, BackCase); |
| 1628 | break; |
| 1629 | } |
| 1630 | } |
| 1631 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1632 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1633 | // Create a CaseBlock record representing a conditional branch to |
| 1634 | // the Case's target mbb if the value being switched on SV is equal |
| 1635 | // to C. |
| 1636 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1637 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1638 | MachineBasicBlock *FallThrough; |
| 1639 | if (I != E-1) { |
| 1640 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1641 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1642 | |
| 1643 | // Put SV in a virtual register to make it available from the new blocks. |
| 1644 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1645 | } else { |
| 1646 | // If the last case doesn't match, go to the default block. |
| 1647 | FallThrough = Default; |
| 1648 | } |
| 1649 | |
| 1650 | Value *RHS, *LHS, *MHS; |
| 1651 | ISD::CondCode CC; |
| 1652 | if (I->High == I->Low) { |
| 1653 | // This is just small small case range :) containing exactly 1 case |
| 1654 | CC = ISD::SETEQ; |
| 1655 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1656 | } else { |
| 1657 | CC = ISD::SETLE; |
| 1658 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1659 | } |
| 1660 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1661 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1662 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1663 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1664 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1665 | // before the next MBB. |
| 1666 | if (CurBlock == CurMBB) |
| 1667 | visitSwitchCase(CB); |
| 1668 | else |
| 1669 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1670 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1671 | CurBlock = FallThrough; |
| 1672 | } |
| 1673 | |
| 1674 | return true; |
| 1675 | } |
| 1676 | |
| 1677 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1678 | return !DisableJumpTables && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 1679 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1680 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1681 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1682 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1683 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1684 | APInt LastExt(Last), FirstExt(First); |
| 1685 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1686 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1687 | return (LastExt - FirstExt + 1ULL); |
| 1688 | } |
| 1689 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1690 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1691 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
| 1692 | CaseRecVector& WorkList, |
| 1693 | Value* SV, |
| 1694 | MachineBasicBlock* Default) { |
| 1695 | Case& FrontCase = *CR.Range.first; |
| 1696 | Case& BackCase = *(CR.Range.second-1); |
| 1697 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1698 | const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1699 | const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1700 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1701 | size_t TSize = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1702 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1703 | I!=E; ++I) |
| 1704 | TSize += I->size(); |
| 1705 | |
| 1706 | if (!areJTsAllowed(TLI) || TSize <= 3) |
| 1707 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1708 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1709 | APInt Range = ComputeRange(First, Last); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1710 | double Density = (double)TSize / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1711 | if (Density < 0.4) |
| 1712 | return false; |
| 1713 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1714 | DEBUG(errs() << "Lowering jump table\n" |
| 1715 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1716 | << "Range: " << Range |
| 1717 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1720 | // inserting any additional MBBs necessary to represent the switch. |
| 1721 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1722 | |
| 1723 | // Figure out which block is immediately after the current one. |
| 1724 | MachineBasicBlock *NextBlock = 0; |
| 1725 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1726 | |
| 1727 | if (++BBI != CurMBB->getParent()->end()) |
| 1728 | NextBlock = BBI; |
| 1729 | |
| 1730 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1731 | |
| 1732 | // Create a new basic block to hold the code for loading the address |
| 1733 | // of the jump table, and jumping to it. Update successor information; |
| 1734 | // we will either branch to the default case for the switch, or the jump |
| 1735 | // table. |
| 1736 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1737 | CurMF->insert(BBI, JumpTableBB); |
| 1738 | CR.CaseBB->addSuccessor(Default); |
| 1739 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1740 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1741 | // Build a vector of destination BBs, corresponding to each target |
| 1742 | // of the jump table. If the value of the jump table slot corresponds to |
| 1743 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1744 | // the default BB. |
| 1745 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1746 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1747 | 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] | 1748 | const APInt& Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1749 | const APInt& High = cast<ConstantInt>(I->High)->getValue(); |
| 1750 | |
| 1751 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1752 | DestBBs.push_back(I->BB); |
| 1753 | if (TEI==High) |
| 1754 | ++I; |
| 1755 | } else { |
| 1756 | DestBBs.push_back(Default); |
| 1757 | } |
| 1758 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1759 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1760 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1761 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1762 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1763 | E = DestBBs.end(); I != E; ++I) { |
| 1764 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1765 | SuccsHandled[(*I)->getNumber()] = true; |
| 1766 | JumpTableBB->addSuccessor(*I); |
| 1767 | } |
| 1768 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1769 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1770 | // Create a jump table index for this jump table, or return an existing |
| 1771 | // one. |
| 1772 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1773 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1774 | // Set the jump table information so that we can codegen it as a second |
| 1775 | // MachineBasicBlock |
| 1776 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1777 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1778 | if (CR.CaseBB == CurMBB) |
| 1779 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1780 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1781 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1782 | |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
| 1786 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1787 | /// 2 subtrees. |
| 1788 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
| 1789 | CaseRecVector& WorkList, |
| 1790 | Value* SV, |
| 1791 | MachineBasicBlock* Default) { |
| 1792 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1793 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1794 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1795 | |
| 1796 | // Figure out which block is immediately after the current one. |
| 1797 | MachineBasicBlock *NextBlock = 0; |
| 1798 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1799 | |
| 1800 | if (++BBI != CurMBB->getParent()->end()) |
| 1801 | NextBlock = BBI; |
| 1802 | |
| 1803 | Case& FrontCase = *CR.Range.first; |
| 1804 | Case& BackCase = *(CR.Range.second-1); |
| 1805 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1806 | |
| 1807 | // Size is the number of Cases represented by this range. |
| 1808 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1809 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1810 | const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1811 | const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1812 | double FMetric = 0; |
| 1813 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1814 | |
| 1815 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1816 | // (heuristically) allow us to emit JumpTable's later. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1817 | size_t TSize = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1818 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1819 | I!=E; ++I) |
| 1820 | TSize += I->size(); |
| 1821 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1822 | size_t LSize = FrontCase.size(); |
| 1823 | size_t RSize = TSize-LSize; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1824 | DEBUG(errs() << "Selecting best pivot: \n" |
| 1825 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1826 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1827 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1828 | J!=E; ++I, ++J) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1829 | const APInt& LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1830 | const APInt& RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1831 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1832 | assert((Range - 2ULL).isNonNegative() && |
| 1833 | "Invalid case distance"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1834 | double LDensity = (double)LSize / (LEnd - First + 1ULL).roundToDouble(); |
| 1835 | double RDensity = (double)RSize / (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1836 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1837 | // Should always split in some non-trivial place |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1838 | DEBUG(errs() <<"=>Step\n" |
| 1839 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1840 | << "LDensity: " << LDensity |
| 1841 | << ", RDensity: " << RDensity << '\n' |
| 1842 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1843 | if (FMetric < Metric) { |
| 1844 | Pivot = J; |
| 1845 | FMetric = Metric; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1846 | DEBUG(errs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | LSize += J->size(); |
| 1850 | RSize -= J->size(); |
| 1851 | } |
| 1852 | if (areJTsAllowed(TLI)) { |
| 1853 | // If our case is dense we *really* should handle it earlier! |
| 1854 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1855 | } else { |
| 1856 | Pivot = CR.Range.first + Size/2; |
| 1857 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1858 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1859 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1860 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1861 | Constant *C = Pivot->Low; |
| 1862 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1863 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1864 | // 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] | 1865 | // 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] | 1866 | // 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] | 1867 | // 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] | 1868 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1869 | // rather than creating a leaf node for it. |
| 1870 | if ((LHSR.second - LHSR.first) == 1 && |
| 1871 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1872 | cast<ConstantInt>(C)->getValue() == |
| 1873 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1874 | TrueBB = LHSR.first->BB; |
| 1875 | } else { |
| 1876 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1877 | CurMF->insert(BBI, TrueBB); |
| 1878 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Put SV in a virtual register to make it available from the new blocks. |
| 1881 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1882 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1883 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1884 | // Similar to the optimization above, if the Value being switched on is |
| 1885 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1886 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1887 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1888 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1889 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1890 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1891 | FalseBB = RHSR.first->BB; |
| 1892 | } else { |
| 1893 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1894 | CurMF->insert(BBI, FalseBB); |
| 1895 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1896 | |
| 1897 | // Put SV in a virtual register to make it available from the new blocks. |
| 1898 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1902 | // 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] | 1903 | // Otherwise, branch to LHS. |
| 1904 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1905 | |
| 1906 | if (CR.CaseBB == CurMBB) |
| 1907 | visitSwitchCase(CB); |
| 1908 | else |
| 1909 | SwitchCases.push_back(CB); |
| 1910 | |
| 1911 | return true; |
| 1912 | } |
| 1913 | |
| 1914 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1915 | /// range span less, than machine word bitwidth, encode case range into series |
| 1916 | /// of masks and emit bit tests with these masks. |
| 1917 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1918 | CaseRecVector& WorkList, |
| 1919 | Value* SV, |
| 1920 | MachineBasicBlock* Default){ |
| 1921 | unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits(); |
| 1922 | |
| 1923 | Case& FrontCase = *CR.Range.first; |
| 1924 | Case& BackCase = *(CR.Range.second-1); |
| 1925 | |
| 1926 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1927 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1928 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1929 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1930 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1931 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1932 | I!=E; ++I) { |
| 1933 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1934 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1935 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1936 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1937 | // Count unique destinations |
| 1938 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1939 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1940 | Dests.insert(I->BB); |
| 1941 | if (Dests.size() > 3) |
| 1942 | // Don't bother the code below, if there are too much unique destinations |
| 1943 | return false; |
| 1944 | } |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1945 | DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n' |
| 1946 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1947 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1948 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1949 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1950 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1951 | APInt cmpRange = maxValue - minValue; |
| 1952 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1953 | DEBUG(errs() << "Compare range: " << cmpRange << '\n' |
| 1954 | << "Low bound: " << minValue << '\n' |
| 1955 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1956 | |
| 1957 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1958 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1959 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1960 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1961 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1962 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1963 | DEBUG(errs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1964 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1965 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1966 | // Optimize the case where all the case values fit in a |
| 1967 | // word without having to subtract minValue. In this case, |
| 1968 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1969 | if (minValue.isNonNegative() && |
| 1970 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 1971 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1972 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1973 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1974 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1975 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1976 | CaseBitsVector CasesBits; |
| 1977 | unsigned i, count = 0; |
| 1978 | |
| 1979 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1980 | MachineBasicBlock* Dest = I->BB; |
| 1981 | for (i = 0; i < count; ++i) |
| 1982 | if (Dest == CasesBits[i].BB) |
| 1983 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1984 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1985 | if (i == count) { |
| 1986 | assert((count < 3) && "Too much destinations to test!"); |
| 1987 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1988 | count++; |
| 1989 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1990 | |
| 1991 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1992 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1993 | |
| 1994 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1995 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1996 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1997 | for (uint64_t j = lo; j <= hi; j++) { |
| 1998 | CasesBits[i].Mask |= 1ULL << j; |
| 1999 | CasesBits[i].Bits++; |
| 2000 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2001 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2002 | } |
| 2003 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2004 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2005 | BitTestInfo BTC; |
| 2006 | |
| 2007 | // Figure out which block is immediately after the current one. |
| 2008 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2009 | ++BBI; |
| 2010 | |
| 2011 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2012 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2013 | DEBUG(errs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2014 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2015 | DEBUG(errs() << "Mask: " << CasesBits[i].Mask |
| 2016 | << ", Bits: " << CasesBits[i].Bits |
| 2017 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | |
| 2019 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2020 | CurMF->insert(BBI, CaseBB); |
| 2021 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2022 | CaseBB, |
| 2023 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Put SV in a virtual register to make it available from the new blocks. |
| 2026 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2027 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2028 | |
| 2029 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2030 | -1U, (CR.CaseBB == CurMBB), |
| 2031 | CR.CaseBB, Default, BTC); |
| 2032 | |
| 2033 | if (CR.CaseBB == CurMBB) |
| 2034 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2035 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2036 | BitTestCases.push_back(BTB); |
| 2037 | |
| 2038 | return true; |
| 2039 | } |
| 2040 | |
| 2041 | |
| 2042 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2043 | size_t SelectionDAGLowering::Clusterify(CaseVector& Cases, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2044 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2045 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2046 | |
| 2047 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2048 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2049 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2050 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2051 | SI.getSuccessorValue(i), |
| 2052 | SMBB)); |
| 2053 | } |
| 2054 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 2055 | |
| 2056 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2057 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2058 | // Must recompute end() each iteration because it may be |
| 2059 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2060 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 2061 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2062 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2063 | MachineBasicBlock* nextBB = J->BB; |
| 2064 | MachineBasicBlock* currentBB = I->BB; |
| 2065 | |
| 2066 | // If the two neighboring cases go to the same destination, merge them |
| 2067 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2068 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2069 | I->High = J->High; |
| 2070 | J = Cases.erase(J); |
| 2071 | } else { |
| 2072 | I = J++; |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2077 | if (I->Low != I->High) |
| 2078 | // A range counts double, since it requires two compares. |
| 2079 | ++numCmps; |
| 2080 | } |
| 2081 | |
| 2082 | return numCmps; |
| 2083 | } |
| 2084 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2085 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2086 | // Figure out which block is immediately after the current one. |
| 2087 | MachineBasicBlock *NextBlock = 0; |
| 2088 | MachineFunction::iterator BBI = CurMBB; |
| 2089 | |
| 2090 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2091 | |
| 2092 | // If there is only the default destination, branch to it if it is not the |
| 2093 | // next basic block. Otherwise, just fall through. |
| 2094 | if (SI.getNumOperands() == 2) { |
| 2095 | // Update machine-CFG edges. |
| 2096 | |
| 2097 | // If this is not a fall-through branch, emit the branch. |
| 2098 | CurMBB->addSuccessor(Default); |
| 2099 | if (Default != NextBlock) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2100 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2101 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2102 | DAG.getBasicBlock(Default))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2103 | return; |
| 2104 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2105 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2106 | // If there are any non-default case statements, create a vector of Cases |
| 2107 | // representing each one, and sort the vector so that we can efficiently |
| 2108 | // create a binary search tree from them. |
| 2109 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2110 | size_t numCmps = Clusterify(Cases, SI); |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2111 | DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size() |
| 2112 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2113 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2114 | |
| 2115 | // Get the Value to be switched on and default basic blocks, which will be |
| 2116 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2117 | // search tree. |
| 2118 | Value *SV = SI.getOperand(0); |
| 2119 | |
| 2120 | // Push the initial CaseRec onto the worklist |
| 2121 | CaseRecVector WorkList; |
| 2122 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2123 | |
| 2124 | while (!WorkList.empty()) { |
| 2125 | // Grab a record representing a case range to process off the worklist |
| 2126 | CaseRec CR = WorkList.back(); |
| 2127 | WorkList.pop_back(); |
| 2128 | |
| 2129 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2130 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2131 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2132 | // If the range has few cases (two or less) emit a series of specific |
| 2133 | // tests. |
| 2134 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2135 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2136 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2137 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2138 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2139 | // lowering the switch to a binary tree of conditional branches. |
| 2140 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2141 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2142 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2143 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2144 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2145 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | void SelectionDAGLowering::visitSub(User &I) { |
| 2151 | // -0.0 - X --> fneg |
| 2152 | const Type *Ty = I.getType(); |
| 2153 | if (isa<VectorType>(Ty)) { |
| 2154 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2155 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2156 | const Type *ElTy = DestTy->getElementType(); |
| 2157 | if (ElTy->isFloatingPoint()) { |
| 2158 | unsigned VL = DestTy->getNumElements(); |
| 2159 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
| 2160 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
| 2161 | if (CV == CNZ) { |
| 2162 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2163 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2164 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2165 | return; |
| 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | if (Ty->isFloatingPoint()) { |
| 2171 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 2172 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
| 2173 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2174 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2175 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2176 | return; |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB); |
| 2181 | } |
| 2182 | |
| 2183 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
| 2184 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2185 | SDValue Op2 = getValue(I.getOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2186 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2187 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2188 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2192 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2193 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2194 | if (!isa<VectorType>(I.getType()) && |
| 2195 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2196 | // If the operand is smaller than the shift count type, promote it. |
| 2197 | if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType())) |
| 2198 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2199 | TLI.getShiftAmountTy(), Op2); |
| 2200 | // If the operand is larger than the shift count type but the shift |
| 2201 | // count type has enough bits to represent any shift value, truncate |
| 2202 | // it now. This is a common case and it exposes the truncate to |
| 2203 | // optimization early. |
| 2204 | else if (TLI.getShiftAmountTy().getSizeInBits() >= |
| 2205 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2206 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2207 | TLI.getShiftAmountTy(), Op2); |
| 2208 | // Otherwise we'll need to temporarily settle for some other |
| 2209 | // convenient type; type legalization will make adjustments as |
| 2210 | // needed. |
| 2211 | else if (TLI.getPointerTy().bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2212 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2213 | TLI.getPointerTy(), Op2); |
| 2214 | else if (TLI.getPointerTy().bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2215 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2216 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2217 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2218 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2219 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2220 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | void SelectionDAGLowering::visitICmp(User &I) { |
| 2224 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2225 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2226 | predicate = IC->getPredicate(); |
| 2227 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2228 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2229 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2230 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2231 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2232 | setValue(&I, DAG.getSetCC(getCurDebugLoc(),MVT::i1, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | void SelectionDAGLowering::visitFCmp(User &I) { |
| 2236 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2237 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2238 | predicate = FC->getPredicate(); |
| 2239 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2240 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2241 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2242 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2243 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2244 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), MVT::i1, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | void SelectionDAGLowering::visitVICmp(User &I) { |
| 2248 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2249 | if (VICmpInst *IC = dyn_cast<VICmpInst>(&I)) |
| 2250 | predicate = IC->getPredicate(); |
| 2251 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2252 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2253 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2254 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2255 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2256 | setValue(&I, DAG.getVSetCC(getCurDebugLoc(), Op1.getValueType(), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2257 | Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | void SelectionDAGLowering::visitVFCmp(User &I) { |
| 2261 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2262 | if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I)) |
| 2263 | predicate = FC->getPredicate(); |
| 2264 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2265 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2266 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2267 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2268 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2269 | MVT DestVT = TLI.getValueType(I.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2270 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2271 | setValue(&I, DAG.getVSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | void SelectionDAGLowering::visitSelect(User &I) { |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2275 | SmallVector<MVT, 4> ValueVTs; |
| 2276 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2277 | unsigned NumValues = ValueVTs.size(); |
| 2278 | if (NumValues != 0) { |
| 2279 | SmallVector<SDValue, 4> Values(NumValues); |
| 2280 | SDValue Cond = getValue(I.getOperand(0)); |
| 2281 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2282 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2283 | |
| 2284 | for (unsigned i = 0; i != NumValues; ++i) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2285 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2286 | TrueVal.getValueType(), Cond, |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2287 | SDValue(TrueVal.getNode(), TrueVal.getResNo() + i), |
| 2288 | SDValue(FalseVal.getNode(), FalseVal.getResNo() + i)); |
| 2289 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2290 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2291 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2292 | &Values[0], NumValues)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2293 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | |
| 2297 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2298 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2299 | SDValue N = getValue(I.getOperand(0)); |
| 2300 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2301 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2305 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2306 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2307 | SDValue N = getValue(I.getOperand(0)); |
| 2308 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2309 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2313 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2314 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2315 | SDValue N = getValue(I.getOperand(0)); |
| 2316 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2317 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2321 | // FPTrunc is never a no-op cast, no need to check |
| 2322 | SDValue N = getValue(I.getOperand(0)); |
| 2323 | MVT DestVT = TLI.getValueType(I.getType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2324 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2325 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2328 | void SelectionDAGLowering::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2329 | // FPTrunc is never a no-op cast, no need to check |
| 2330 | SDValue N = getValue(I.getOperand(0)); |
| 2331 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2332 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2335 | void SelectionDAGLowering::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2336 | // FPToUI is never a no-op cast, no need to check |
| 2337 | SDValue N = getValue(I.getOperand(0)); |
| 2338 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2339 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2343 | // FPToSI is never a no-op cast, no need to check |
| 2344 | SDValue N = getValue(I.getOperand(0)); |
| 2345 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2346 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2349 | void SelectionDAGLowering::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2350 | // UIToFP is never a no-op cast, no need to check |
| 2351 | SDValue N = getValue(I.getOperand(0)); |
| 2352 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2353 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2356 | void SelectionDAGLowering::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2357 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2358 | SDValue N = getValue(I.getOperand(0)); |
| 2359 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2360 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2364 | // What to do depends on the size of the integer and the size of the pointer. |
| 2365 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2366 | SDValue N = getValue(I.getOperand(0)); |
| 2367 | MVT SrcVT = N.getValueType(); |
| 2368 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2369 | SDValue Result; |
| 2370 | if (DestVT.bitsLT(SrcVT)) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2371 | Result = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2372 | else |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2373 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2374 | Result = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2375 | setValue(&I, Result); |
| 2376 | } |
| 2377 | |
| 2378 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2379 | // What to do depends on the size of the integer and the size of the pointer. |
| 2380 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2381 | SDValue N = getValue(I.getOperand(0)); |
| 2382 | MVT SrcVT = N.getValueType(); |
| 2383 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2384 | if (DestVT.bitsLT(SrcVT)) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2385 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2386 | else |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2387 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2388 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2389 | DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2392 | void SelectionDAGLowering::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2393 | SDValue N = getValue(I.getOperand(0)); |
| 2394 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2395 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2396 | // 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] | 2397 | // is either a BIT_CONVERT or a no-op. |
| 2398 | if (DestVT != N.getValueType()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2399 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2400 | DestVT, N)); // convert types |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2401 | else |
| 2402 | setValue(&I, N); // noop cast. |
| 2403 | } |
| 2404 | |
| 2405 | void SelectionDAGLowering::visitInsertElement(User &I) { |
| 2406 | SDValue InVec = getValue(I.getOperand(0)); |
| 2407 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2408 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2409 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2410 | getValue(I.getOperand(2))); |
| 2411 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2412 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2413 | TLI.getValueType(I.getType()), |
| 2414 | InVec, InVal, InIdx)); |
| 2415 | } |
| 2416 | |
| 2417 | void SelectionDAGLowering::visitExtractElement(User &I) { |
| 2418 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2419 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2420 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2421 | getValue(I.getOperand(1))); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2422 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2423 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2424 | } |
| 2425 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2428 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2429 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2430 | unsigned MaskNumElts = Mask.size(); |
| 2431 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2432 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2433 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2434 | return true; |
| 2435 | } |
| 2436 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2437 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2438 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2439 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2440 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2441 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2442 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2443 | // representing undef values. |
| 2444 | SmallVector<Constant*, 8> MaskElts; |
| 2445 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2446 | unsigned MaskNumElts = MaskElts.size(); |
| 2447 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2448 | if (isa<UndefValue>(MaskElts[i])) |
| 2449 | Mask.push_back(-1); |
| 2450 | else |
| 2451 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2452 | } |
| 2453 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2454 | MVT VT = TLI.getValueType(I.getType()); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2455 | MVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2456 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2457 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2458 | if (SrcNumElts == MaskNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2459 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2460 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2461 | return; |
| 2462 | } |
| 2463 | |
| 2464 | // 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] | 2465 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2466 | // Mask is longer than the source vectors and is a multiple of the source |
| 2467 | // 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] | 2468 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2469 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2470 | // The shuffle is concatenating two vectors together. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2471 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2472 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2473 | return; |
| 2474 | } |
| 2475 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2476 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2477 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2478 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2479 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2480 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2481 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2482 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2483 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2484 | MOps1[0] = Src1; |
| 2485 | MOps2[0] = Src2; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2486 | |
| 2487 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2488 | getCurDebugLoc(), VT, |
| 2489 | &MOps1[0], NumConcat); |
| 2490 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2491 | getCurDebugLoc(), VT, |
| 2492 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2493 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2494 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2495 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2496 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2497 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2498 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2499 | MappedOps.push_back(Idx); |
| 2500 | else |
| 2501 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2502 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2503 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2504 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | } |
| 2507 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2508 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2509 | // Analyze the access pattern of the vector to see if we can extract |
| 2510 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2511 | // the range of elements the mask access on both vectors. |
| 2512 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2513 | int MaxRange[2] = {-1, -1}; |
| 2514 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2515 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2516 | int Idx = Mask[i]; |
| 2517 | int Input = 0; |
| 2518 | if (Idx < 0) |
| 2519 | continue; |
| 2520 | |
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 | Input = 1; |
| 2523 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2524 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2525 | if (Idx > MaxRange[Input]) |
| 2526 | MaxRange[Input] = Idx; |
| 2527 | if (Idx < MinRange[Input]) |
| 2528 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2529 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2530 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2531 | // Check if the access is smaller than the vector size and can we find |
| 2532 | // a reasonable extract index. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2533 | 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] | 2534 | int StartIdx[2]; // StartIdx to extract from |
| 2535 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2536 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2537 | RangeUse[Input] = 0; // Unused |
| 2538 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2539 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2540 | // 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] | 2541 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2542 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2543 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2544 | StartIdx[Input] = 0; |
| 2545 | } else { |
| 2546 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2547 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2548 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2549 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2550 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2551 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | if (RangeUse[0] == 0 && RangeUse[0] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2555 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2556 | return; |
| 2557 | } |
| 2558 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2559 | // Extract appropriate subvector and generate a vector shuffle |
| 2560 | for (int Input=0; Input < 2; ++Input) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2561 | SDValue& Src = Input == 0 ? Src1 : Src2; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2562 | if (RangeUse[Input] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2563 | Src = DAG.getUNDEF(VT); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2564 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2565 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2566 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2567 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2568 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2569 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2570 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2571 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2572 | int Idx = Mask[i]; |
| 2573 | if (Idx < 0) |
| 2574 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2575 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2576 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2577 | else |
| 2578 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2579 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2580 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2581 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2582 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2583 | } |
| 2584 | } |
| 2585 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2586 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2587 | // replacing the shuffle with extract and build vector. |
| 2588 | // to insert and build vector. |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2589 | MVT EltVT = VT.getVectorElementType(); |
| 2590 | MVT PtrVT = TLI.getPointerTy(); |
| 2591 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2592 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2593 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2594 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2595 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2596 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2597 | if (Idx < (int)SrcNumElts) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2598 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2599 | EltVT, Src1, DAG.getConstant(Idx, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2600 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2601 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2602 | EltVT, Src2, |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2603 | DAG.getConstant(Idx - SrcNumElts, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2606 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2607 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) { |
| 2611 | const Value *Op0 = I.getOperand(0); |
| 2612 | const Value *Op1 = I.getOperand(1); |
| 2613 | const Type *AggTy = I.getType(); |
| 2614 | const Type *ValTy = Op1->getType(); |
| 2615 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2616 | bool FromUndef = isa<UndefValue>(Op1); |
| 2617 | |
| 2618 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2619 | I.idx_begin(), I.idx_end()); |
| 2620 | |
| 2621 | SmallVector<MVT, 4> AggValueVTs; |
| 2622 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 2623 | SmallVector<MVT, 4> ValValueVTs; |
| 2624 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2625 | |
| 2626 | unsigned NumAggValues = AggValueVTs.size(); |
| 2627 | unsigned NumValValues = ValValueVTs.size(); |
| 2628 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2629 | |
| 2630 | SDValue Agg = getValue(Op0); |
| 2631 | SDValue Val = getValue(Op1); |
| 2632 | unsigned i = 0; |
| 2633 | // Copy the beginning value(s) from the original aggregate. |
| 2634 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2635 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2636 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2637 | // Copy values from the inserted value(s). |
| 2638 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2639 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2640 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2641 | // Copy remaining value(s) from the original aggregate. |
| 2642 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2643 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2644 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2645 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2646 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2647 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2648 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) { |
| 2652 | const Value *Op0 = I.getOperand(0); |
| 2653 | const Type *AggTy = Op0->getType(); |
| 2654 | const Type *ValTy = I.getType(); |
| 2655 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2656 | |
| 2657 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2658 | I.idx_begin(), I.idx_end()); |
| 2659 | |
| 2660 | SmallVector<MVT, 4> ValValueVTs; |
| 2661 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2662 | |
| 2663 | unsigned NumValValues = ValValueVTs.size(); |
| 2664 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2665 | |
| 2666 | SDValue Agg = getValue(Op0); |
| 2667 | // Copy out the selected value(s). |
| 2668 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2669 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2670 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2671 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2672 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2673 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2674 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2675 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2676 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | |
| 2680 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2681 | SDValue N = getValue(I.getOperand(0)); |
| 2682 | const Type *Ty = I.getOperand(0)->getType(); |
| 2683 | |
| 2684 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2685 | OI != E; ++OI) { |
| 2686 | Value *Idx = *OI; |
| 2687 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2688 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2689 | if (Field) { |
| 2690 | // N = N + Offset |
| 2691 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2692 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2693 | DAG.getIntPtrConstant(Offset)); |
| 2694 | } |
| 2695 | Ty = StTy->getElementType(Field); |
| 2696 | } else { |
| 2697 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2698 | |
| 2699 | // If this is a constant subscript, handle it quickly. |
| 2700 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2701 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2702 | uint64_t Offs = |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2703 | TD->getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2704 | SDValue OffsVal; |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2705 | unsigned PtrBits = TLI.getPointerTy().getSizeInBits(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2706 | if (PtrBits < 64) { |
| 2707 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2708 | TLI.getPointerTy(), |
| 2709 | DAG.getConstant(Offs, MVT::i64)); |
| 2710 | } else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2711 | OffsVal = DAG.getIntPtrConstant(Offs); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2712 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2713 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2714 | continue; |
| 2715 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2716 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2717 | // N = N + Idx * ElementSize; |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2718 | uint64_t ElementSize = TD->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2719 | SDValue IdxN = getValue(Idx); |
| 2720 | |
| 2721 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2722 | // it. |
| 2723 | if (IdxN.getValueType().bitsLT(N.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2724 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2725 | N.getValueType(), IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2726 | else if (IdxN.getValueType().bitsGT(N.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2727 | IdxN = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2728 | N.getValueType(), IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2729 | |
| 2730 | // If this is a multiply by a power of two, turn it into a shl |
| 2731 | // immediately. This is a very common case. |
| 2732 | if (ElementSize != 1) { |
| 2733 | if (isPowerOf2_64(ElementSize)) { |
| 2734 | unsigned Amt = Log2_64(ElementSize); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2735 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2736 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2737 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2738 | } else { |
| 2739 | SDValue Scale = DAG.getIntPtrConstant(ElementSize); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2740 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2741 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2745 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2746 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2747 | } |
| 2748 | } |
| 2749 | setValue(&I, N); |
| 2750 | } |
| 2751 | |
| 2752 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2753 | // If this is a fixed sized alloca in the entry block of the function, |
| 2754 | // allocate it statically on the stack. |
| 2755 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2756 | return; // getValue will auto-populate this. |
| 2757 | |
| 2758 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2759 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2760 | unsigned Align = |
| 2761 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2762 | I.getAlignment()); |
| 2763 | |
| 2764 | SDValue AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2765 | |
| 2766 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2767 | AllocSize, |
| 2768 | DAG.getConstant(TySize, AllocSize.getValueType())); |
| 2769 | |
| 2770 | |
| 2771 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2772 | MVT IntPtr = TLI.getPointerTy(); |
| 2773 | if (IntPtr.bitsLT(AllocSize.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2774 | AllocSize = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2775 | IntPtr, AllocSize); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2776 | else if (IntPtr.bitsGT(AllocSize.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2777 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2778 | IntPtr, AllocSize); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2779 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2780 | // Handle alignment. If the requested alignment is less than or equal to |
| 2781 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2782 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2783 | unsigned StackAlign = |
| 2784 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2785 | if (Align <= StackAlign) |
| 2786 | Align = 0; |
| 2787 | |
| 2788 | // Round the size of the allocation up to the stack alignment size |
| 2789 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2790 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2791 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2792 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2793 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2794 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2795 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2796 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2797 | |
| 2798 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2799 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2800 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2801 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2802 | setValue(&I, DSA); |
| 2803 | DAG.setRoot(DSA.getValue(1)); |
| 2804 | |
| 2805 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2806 | // object. |
| 2807 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 2808 | } |
| 2809 | |
| 2810 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2811 | const Value *SV = I.getOperand(0); |
| 2812 | SDValue Ptr = getValue(SV); |
| 2813 | |
| 2814 | const Type *Ty = I.getType(); |
| 2815 | bool isVolatile = I.isVolatile(); |
| 2816 | unsigned Alignment = I.getAlignment(); |
| 2817 | |
| 2818 | SmallVector<MVT, 4> ValueVTs; |
| 2819 | SmallVector<uint64_t, 4> Offsets; |
| 2820 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2821 | unsigned NumValues = ValueVTs.size(); |
| 2822 | if (NumValues == 0) |
| 2823 | return; |
| 2824 | |
| 2825 | SDValue Root; |
| 2826 | bool ConstantMemory = false; |
| 2827 | if (I.isVolatile()) |
| 2828 | // Serialize volatile loads with other side effects. |
| 2829 | Root = getRoot(); |
| 2830 | else if (AA->pointsToConstantMemory(SV)) { |
| 2831 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2832 | Root = DAG.getEntryNode(); |
| 2833 | ConstantMemory = true; |
| 2834 | } else { |
| 2835 | // Do not serialize non-volatile loads against each other. |
| 2836 | Root = DAG.getRoot(); |
| 2837 | } |
| 2838 | |
| 2839 | SmallVector<SDValue, 4> Values(NumValues); |
| 2840 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2841 | MVT PtrVT = Ptr.getValueType(); |
| 2842 | for (unsigned i = 0; i != NumValues; ++i) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2843 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2844 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2845 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2846 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2847 | SV, Offsets[i], |
| 2848 | isVolatile, Alignment); |
| 2849 | Values[i] = L; |
| 2850 | Chains[i] = L.getValue(1); |
| 2851 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2852 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2853 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2854 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2855 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2856 | &Chains[0], NumValues); |
| 2857 | if (isVolatile) |
| 2858 | DAG.setRoot(Chain); |
| 2859 | else |
| 2860 | PendingLoads.push_back(Chain); |
| 2861 | } |
| 2862 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2863 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2864 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2865 | &Values[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | |
| 2869 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2870 | Value *SrcV = I.getOperand(0); |
| 2871 | Value *PtrV = I.getOperand(1); |
| 2872 | |
| 2873 | SmallVector<MVT, 4> ValueVTs; |
| 2874 | SmallVector<uint64_t, 4> Offsets; |
| 2875 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2876 | unsigned NumValues = ValueVTs.size(); |
| 2877 | if (NumValues == 0) |
| 2878 | return; |
| 2879 | |
| 2880 | // Get the lowered operands. Note that we do this after |
| 2881 | // checking if NumResults is zero, because with zero results |
| 2882 | // the operands won't have values in the map. |
| 2883 | SDValue Src = getValue(SrcV); |
| 2884 | SDValue Ptr = getValue(PtrV); |
| 2885 | |
| 2886 | SDValue Root = getRoot(); |
| 2887 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2888 | MVT PtrVT = Ptr.getValueType(); |
| 2889 | bool isVolatile = I.isVolatile(); |
| 2890 | unsigned Alignment = I.getAlignment(); |
| 2891 | for (unsigned i = 0; i != NumValues; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2892 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2893 | SDValue(Src.getNode(), Src.getResNo() + i), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2894 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2895 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2896 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2897 | PtrV, Offsets[i], |
| 2898 | isVolatile, Alignment); |
| 2899 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2900 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2901 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2905 | /// node. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2906 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2907 | unsigned Intrinsic) { |
| 2908 | bool HasChain = !I.doesNotAccessMemory(); |
| 2909 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2910 | |
| 2911 | // Build the operand list. |
| 2912 | SmallVector<SDValue, 8> Ops; |
| 2913 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2914 | if (OnlyLoad) { |
| 2915 | // We don't need to serialize loads against other loads. |
| 2916 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2917 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2918 | Ops.push_back(getRoot()); |
| 2919 | } |
| 2920 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2921 | |
| 2922 | // Info is set by getTgtMemInstrinsic |
| 2923 | TargetLowering::IntrinsicInfo Info; |
| 2924 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2925 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2926 | // 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] | 2927 | if (!IsTgtIntrinsic) |
| 2928 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Add all operands of the call to the operand list. |
| 2931 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2932 | SDValue Op = getValue(I.getOperand(i)); |
| 2933 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2934 | "Intrinsic uses a non-legal type?"); |
| 2935 | Ops.push_back(Op); |
| 2936 | } |
| 2937 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2938 | std::vector<MVT> VTArray; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2939 | if (I.getType() != Type::VoidTy) { |
| 2940 | MVT VT = TLI.getValueType(I.getType()); |
| 2941 | if (VT.isVector()) { |
| 2942 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2943 | MVT EltVT = TLI.getValueType(DestTy->getElementType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2944 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2945 | VT = MVT::getVectorVT(EltVT, DestTy->getNumElements()); |
| 2946 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2947 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2948 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2949 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2950 | VTArray.push_back(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2951 | } |
| 2952 | if (HasChain) |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2953 | VTArray.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2954 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2955 | SDVTList VTs = DAG.getVTList(&VTArray[0], VTArray.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2956 | |
| 2957 | // Create the node. |
| 2958 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2959 | if (IsTgtIntrinsic) { |
| 2960 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2961 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2962 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2963 | Info.memVT, Info.ptrVal, Info.offset, |
| 2964 | Info.align, Info.vol, |
| 2965 | Info.readMem, Info.writeMem); |
| 2966 | } |
| 2967 | else if (!HasChain) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2968 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2969 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2970 | else if (I.getType() != Type::VoidTy) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2971 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2972 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2973 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2974 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2975 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2976 | |
| 2977 | if (HasChain) { |
| 2978 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2979 | if (OnlyLoad) |
| 2980 | PendingLoads.push_back(Chain); |
| 2981 | else |
| 2982 | DAG.setRoot(Chain); |
| 2983 | } |
| 2984 | if (I.getType() != Type::VoidTy) { |
| 2985 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
| 2986 | MVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2987 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2988 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2989 | setValue(&I, Result); |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 2994 | static GlobalVariable *ExtractTypeInfo(Value *V) { |
| 2995 | V = V->stripPointerCasts(); |
| 2996 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 2997 | assert ((GV || isa<ConstantPointerNull>(V)) && |
| 2998 | "TypeInfo must be a global variable or NULL"); |
| 2999 | return GV; |
| 3000 | } |
| 3001 | |
| 3002 | namespace llvm { |
| 3003 | |
| 3004 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 3005 | /// call, and add them to the specified machine basic block. |
| 3006 | void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 3007 | MachineBasicBlock *MBB) { |
| 3008 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 3009 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 3010 | assert(CE->getOpcode() == Instruction::BitCast && |
| 3011 | isa<Function>(CE->getOperand(0)) && |
| 3012 | "Personality should be a function"); |
| 3013 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 3014 | |
| 3015 | // Gather all the type infos for this landing pad and pass them along to |
| 3016 | // MachineModuleInfo. |
| 3017 | std::vector<GlobalVariable *> TyInfo; |
| 3018 | unsigned N = I.getNumOperands(); |
| 3019 | |
| 3020 | for (unsigned i = N - 1; i > 2; --i) { |
| 3021 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 3022 | unsigned FilterLength = CI->getZExtValue(); |
| 3023 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
| 3024 | assert (FirstCatch <= N && "Invalid filter length"); |
| 3025 | |
| 3026 | if (FirstCatch < N) { |
| 3027 | TyInfo.reserve(N - FirstCatch); |
| 3028 | for (unsigned j = FirstCatch; j < N; ++j) |
| 3029 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3030 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3031 | TyInfo.clear(); |
| 3032 | } |
| 3033 | |
| 3034 | if (!FilterLength) { |
| 3035 | // Cleanup. |
| 3036 | MMI->addCleanup(MBB); |
| 3037 | } else { |
| 3038 | // Filter. |
| 3039 | TyInfo.reserve(FilterLength - 1); |
| 3040 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 3041 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3042 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 3043 | TyInfo.clear(); |
| 3044 | } |
| 3045 | |
| 3046 | N = i; |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | if (N > 3) { |
| 3051 | TyInfo.reserve(N - 3); |
| 3052 | for (unsigned j = 3; j < N; ++j) |
| 3053 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3054 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | } |
| 3059 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3060 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3061 | /// number with exponent of 1: |
| 3062 | /// |
| 3063 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3064 | /// |
| 3065 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3066 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3067 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
| 3068 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3069 | DAG.getConstant(0x007fffff, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3070 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3071 | DAG.getConstant(0x3f800000, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3072 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3075 | /// GetExponent - Get the exponent: |
| 3076 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3077 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3078 | /// |
| 3079 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3080 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3081 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
| 3082 | DebugLoc dl) { |
| 3083 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3084 | DAG.getConstant(0x7f800000, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3085 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3086 | DAG.getConstant(23, TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3087 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3088 | DAG.getConstant(127, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3089 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3092 | /// getF32Constant - Get 32-bit floating point constant. |
| 3093 | static SDValue |
| 3094 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
| 3095 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
| 3096 | } |
| 3097 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3098 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3099 | /// visitIntrinsicCall: I is a call instruction |
| 3100 | /// Op is the associated NodeType for I |
| 3101 | const char * |
| 3102 | SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3103 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3104 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3105 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3106 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3107 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3108 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3109 | getValue(I.getOperand(2)), |
| 3110 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3111 | setValue(&I, L); |
| 3112 | DAG.setRoot(L.getValue(1)); |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3116 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3117 | const char * |
| 3118 | SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3119 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3120 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3121 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3122 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
| 3123 | SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3124 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3125 | setValue(&I, Result); |
| 3126 | return 0; |
| 3127 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3128 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3129 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 3130 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3131 | void |
| 3132 | SelectionDAGLowering::visitExp(CallInst &I) { |
| 3133 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3134 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3135 | |
| 3136 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3137 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3138 | SDValue Op = getValue(I.getOperand(1)); |
| 3139 | |
| 3140 | // Put the exponent in the right bit position for later addition to the |
| 3141 | // final result: |
| 3142 | // |
| 3143 | // #define LOG2OFe 1.4426950f |
| 3144 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3145 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3146 | getF32Constant(DAG, 0x3fb8aa3b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3147 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3148 | |
| 3149 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3150 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3151 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3152 | |
| 3153 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3154 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3155 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3156 | |
| 3157 | if (LimitFloatPrecision <= 6) { |
| 3158 | // For floating-point precision of 6: |
| 3159 | // |
| 3160 | // TwoToFractionalPartOfX = |
| 3161 | // 0.997535578f + |
| 3162 | // (0.735607626f + 0.252464424f * x) * x; |
| 3163 | // |
| 3164 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3165 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3166 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3167 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3168 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3169 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3170 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3171 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3172 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3173 | |
| 3174 | // Add the exponent into the result in integer domain. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3175 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3176 | TwoToFracPartOfX, IntegerPartOfX); |
| 3177 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3178 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3179 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3180 | // For floating-point precision of 12: |
| 3181 | // |
| 3182 | // TwoToFractionalPartOfX = |
| 3183 | // 0.999892986f + |
| 3184 | // (0.696457318f + |
| 3185 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3186 | // |
| 3187 | // 0.000107046256 error, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3188 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3189 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3190 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3191 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3192 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3193 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3194 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3195 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3196 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3197 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3198 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Add the exponent into the result in integer domain. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3201 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3202 | TwoToFracPartOfX, IntegerPartOfX); |
| 3203 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3204 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3205 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3206 | // For floating-point precision of 18: |
| 3207 | // |
| 3208 | // TwoToFractionalPartOfX = |
| 3209 | // 0.999999982f + |
| 3210 | // (0.693148872f + |
| 3211 | // (0.240227044f + |
| 3212 | // (0.554906021e-1f + |
| 3213 | // (0.961591928e-2f + |
| 3214 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3215 | // |
| 3216 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3217 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3218 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3219 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3220 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3221 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3222 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3223 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3224 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3225 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3226 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3227 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3228 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3229 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3230 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3231 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3232 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3233 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3234 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3235 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3236 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3237 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Add the exponent into the result in integer domain. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3240 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3241 | TwoToFracPartOfX, IntegerPartOfX); |
| 3242 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3243 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3244 | } |
| 3245 | } else { |
| 3246 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3247 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3248 | getValue(I.getOperand(1)).getValueType(), |
| 3249 | getValue(I.getOperand(1))); |
| 3250 | } |
| 3251 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3252 | setValue(&I, result); |
| 3253 | } |
| 3254 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3255 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3256 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3257 | void |
| 3258 | SelectionDAGLowering::visitLog(CallInst &I) { |
| 3259 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3260 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3261 | |
| 3262 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3263 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3264 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3265 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3266 | |
| 3267 | // Scale the exponent by log(2) [0.69314718f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3268 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3269 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3270 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3271 | |
| 3272 | // Get the significand and build it into a floating-point number with |
| 3273 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3274 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3275 | |
| 3276 | if (LimitFloatPrecision <= 6) { |
| 3277 | // For floating-point precision of 6: |
| 3278 | // |
| 3279 | // LogofMantissa = |
| 3280 | // -1.1609546f + |
| 3281 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3282 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3283 | // error 0.0034276066, which is better than 8 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3284 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3285 | getF32Constant(DAG, 0xbe74c456)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3286 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3287 | getF32Constant(DAG, 0x3fb3a2b1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3288 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3289 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3290 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3291 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3292 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3293 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3294 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3295 | // For floating-point precision of 12: |
| 3296 | // |
| 3297 | // LogOfMantissa = |
| 3298 | // -1.7417939f + |
| 3299 | // (2.8212026f + |
| 3300 | // (-1.4699568f + |
| 3301 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3302 | // |
| 3303 | // error 0.000061011436, which is 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3304 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3305 | getF32Constant(DAG, 0xbd67b6d6)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3306 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3307 | getF32Constant(DAG, 0x3ee4f4b8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3308 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3309 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3310 | getF32Constant(DAG, 0x3fbc278b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3311 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3312 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3313 | getF32Constant(DAG, 0x40348e95)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3314 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3315 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3316 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3317 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3318 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3319 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3320 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3321 | // For floating-point precision of 18: |
| 3322 | // |
| 3323 | // LogOfMantissa = |
| 3324 | // -2.1072184f + |
| 3325 | // (4.2372794f + |
| 3326 | // (-3.7029485f + |
| 3327 | // (2.2781945f + |
| 3328 | // (-0.87823314f + |
| 3329 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3330 | // |
| 3331 | // error 0.0000023660568, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3332 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3333 | getF32Constant(DAG, 0xbc91e5ac)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3334 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3335 | getF32Constant(DAG, 0x3e4350aa)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3336 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3337 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3338 | getF32Constant(DAG, 0x3f60d3e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3339 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3340 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3341 | getF32Constant(DAG, 0x4011cdf0)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3342 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3343 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3344 | getF32Constant(DAG, 0x406cfd1c)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3345 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3346 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3347 | getF32Constant(DAG, 0x408797cb)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3348 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3349 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3350 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3351 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3352 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3353 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3354 | } |
| 3355 | } else { |
| 3356 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3357 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3358 | getValue(I.getOperand(1)).getValueType(), |
| 3359 | getValue(I.getOperand(1))); |
| 3360 | } |
| 3361 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3362 | setValue(&I, result); |
| 3363 | } |
| 3364 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3365 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3366 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3367 | void |
| 3368 | SelectionDAGLowering::visitLog2(CallInst &I) { |
| 3369 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3370 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3371 | |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3372 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3373 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3374 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3375 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3376 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3377 | // Get the exponent. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3378 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3379 | |
| 3380 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3381 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3382 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3383 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3384 | // Different possible minimax approximations of significand in |
| 3385 | // floating-point for various degrees of accuracy over [1,2]. |
| 3386 | if (LimitFloatPrecision <= 6) { |
| 3387 | // For floating-point precision of 6: |
| 3388 | // |
| 3389 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3390 | // |
| 3391 | // error 0.0049451742, which is more than 7 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3392 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3393 | getF32Constant(DAG, 0xbeb08fe0)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3394 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3395 | getF32Constant(DAG, 0x40019463)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3396 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3397 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3398 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3399 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3400 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3401 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3402 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3403 | // For floating-point precision of 12: |
| 3404 | // |
| 3405 | // Log2ofMantissa = |
| 3406 | // -2.51285454f + |
| 3407 | // (4.07009056f + |
| 3408 | // (-2.12067489f + |
| 3409 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3410 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3411 | // error 0.0000876136000, which is better than 13 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3412 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3413 | getF32Constant(DAG, 0xbda7262e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3414 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3415 | getF32Constant(DAG, 0x3f25280b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3416 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3417 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3418 | getF32Constant(DAG, 0x4007b923)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3419 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3420 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3421 | getF32Constant(DAG, 0x40823e2f)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3422 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3423 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3424 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3425 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3426 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3427 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3428 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3429 | // For floating-point precision of 18: |
| 3430 | // |
| 3431 | // Log2ofMantissa = |
| 3432 | // -3.0400495f + |
| 3433 | // (6.1129976f + |
| 3434 | // (-5.3420409f + |
| 3435 | // (3.2865683f + |
| 3436 | // (-1.2669343f + |
| 3437 | // (0.27515199f - |
| 3438 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3439 | // |
| 3440 | // error 0.0000018516, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3441 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3442 | getF32Constant(DAG, 0xbcd2769e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3443 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3444 | getF32Constant(DAG, 0x3e8ce0b9)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3445 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3446 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3447 | getF32Constant(DAG, 0x3fa22ae7)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3448 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3449 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3450 | getF32Constant(DAG, 0x40525723)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3451 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3452 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3453 | getF32Constant(DAG, 0x40aaf200)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3454 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3455 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3456 | getF32Constant(DAG, 0x40c39dad)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3457 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3458 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3459 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3460 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3461 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3462 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3463 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3464 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3465 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3466 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3467 | getValue(I.getOperand(1)).getValueType(), |
| 3468 | getValue(I.getOperand(1))); |
| 3469 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3470 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3471 | setValue(&I, result); |
| 3472 | } |
| 3473 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3474 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3475 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3476 | void |
| 3477 | SelectionDAGLowering::visitLog10(CallInst &I) { |
| 3478 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3479 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3480 | |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3481 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3482 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3483 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3484 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3485 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3486 | // Scale the exponent by log10(2) [0.30102999f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3487 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3488 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3489 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3490 | |
| 3491 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3492 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3493 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3494 | |
| 3495 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3496 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3497 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3498 | // Log10ofMantissa = |
| 3499 | // -0.50419619f + |
| 3500 | // (0.60948995f - 0.10380950f * x) * x; |
| 3501 | // |
| 3502 | // error 0.0014886165, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3503 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3504 | getF32Constant(DAG, 0xbdd49a13)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3505 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3506 | getF32Constant(DAG, 0x3f1c0789)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3507 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3508 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3509 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3510 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3511 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3512 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3513 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3514 | // For floating-point precision of 12: |
| 3515 | // |
| 3516 | // Log10ofMantissa = |
| 3517 | // -0.64831180f + |
| 3518 | // (0.91751397f + |
| 3519 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3520 | // |
| 3521 | // error 0.00019228036, which is better than 12 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3522 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3523 | getF32Constant(DAG, 0x3d431f31)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3524 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3525 | getF32Constant(DAG, 0x3ea21fb2)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3526 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3527 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3528 | getF32Constant(DAG, 0x3f6ae232)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3529 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3530 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3531 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3532 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3533 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3534 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3535 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3536 | // For floating-point precision of 18: |
| 3537 | // |
| 3538 | // Log10ofMantissa = |
| 3539 | // -0.84299375f + |
| 3540 | // (1.5327582f + |
| 3541 | // (-1.0688956f + |
| 3542 | // (0.49102474f + |
| 3543 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3544 | // |
| 3545 | // error 0.0000037995730, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3546 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3547 | getF32Constant(DAG, 0x3c5d51ce)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3548 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3549 | getF32Constant(DAG, 0x3e00685a)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3550 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3551 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3552 | getF32Constant(DAG, 0x3efb6798)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3553 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3554 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3555 | getF32Constant(DAG, 0x3f88d192)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3556 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3557 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3558 | getF32Constant(DAG, 0x3fc4316c)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3559 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3560 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3561 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3562 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3563 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3564 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3565 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3566 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3567 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3568 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3569 | getValue(I.getOperand(1)).getValueType(), |
| 3570 | getValue(I.getOperand(1))); |
| 3571 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3572 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3573 | setValue(&I, result); |
| 3574 | } |
| 3575 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3576 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3577 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3578 | void |
| 3579 | SelectionDAGLowering::visitExp2(CallInst &I) { |
| 3580 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3581 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3582 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3583 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3584 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3585 | SDValue Op = getValue(I.getOperand(1)); |
| 3586 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3587 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3588 | |
| 3589 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3590 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3591 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3592 | |
| 3593 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3594 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3595 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3596 | |
| 3597 | if (LimitFloatPrecision <= 6) { |
| 3598 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3599 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3600 | // TwoToFractionalPartOfX = |
| 3601 | // 0.997535578f + |
| 3602 | // (0.735607626f + 0.252464424f * x) * x; |
| 3603 | // |
| 3604 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3605 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3606 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3607 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3608 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3609 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3610 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3611 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3612 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3613 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3614 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3615 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3616 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3617 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3618 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3619 | // For floating-point precision of 12: |
| 3620 | // |
| 3621 | // TwoToFractionalPartOfX = |
| 3622 | // 0.999892986f + |
| 3623 | // (0.696457318f + |
| 3624 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3625 | // |
| 3626 | // error 0.000107046256, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3627 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3628 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3629 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3630 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3631 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3632 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3633 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3634 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3635 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3636 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3637 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3638 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3639 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3640 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3641 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3642 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3643 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3644 | // For floating-point precision of 18: |
| 3645 | // |
| 3646 | // TwoToFractionalPartOfX = |
| 3647 | // 0.999999982f + |
| 3648 | // (0.693148872f + |
| 3649 | // (0.240227044f + |
| 3650 | // (0.554906021e-1f + |
| 3651 | // (0.961591928e-2f + |
| 3652 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3653 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3654 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3655 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3656 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3657 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3658 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3659 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3660 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3661 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3662 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3663 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3664 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3665 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3666 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3667 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3668 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3669 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3670 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3671 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3672 | getF32Constant(DAG, 0x3f800000)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3673 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3674 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3675 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3676 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3677 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3678 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3679 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3680 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3681 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3682 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3683 | getValue(I.getOperand(1)).getValueType(), |
| 3684 | getValue(I.getOperand(1))); |
| 3685 | } |
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 | setValue(&I, result); |
| 3688 | } |
| 3689 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3690 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3691 | /// limited-precision mode with x == 10.0f. |
| 3692 | void |
| 3693 | SelectionDAGLowering::visitPow(CallInst &I) { |
| 3694 | SDValue result; |
| 3695 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3696 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3697 | bool IsExp10 = false; |
| 3698 | |
| 3699 | if (getValue(Val).getValueType() == MVT::f32 && |
Bill Wendling | 277fc24 | 2008-09-10 00:24:59 +0000 | [diff] [blame] | 3700 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3701 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3702 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3703 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3704 | APFloat Ten(10.0f); |
| 3705 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3711 | SDValue Op = getValue(I.getOperand(2)); |
| 3712 | |
| 3713 | // Put the exponent in the right bit position for later addition to the |
| 3714 | // final result: |
| 3715 | // |
| 3716 | // #define LOG2OF10 3.3219281f |
| 3717 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3718 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3719 | getF32Constant(DAG, 0x40549a78)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3720 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3721 | |
| 3722 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3723 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3724 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3725 | |
| 3726 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3727 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3728 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3729 | |
| 3730 | if (LimitFloatPrecision <= 6) { |
| 3731 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3732 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3733 | // twoToFractionalPartOfX = |
| 3734 | // 0.997535578f + |
| 3735 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3736 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3737 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3738 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3739 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3740 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3741 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3742 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3743 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3744 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3745 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3746 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3747 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3748 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3749 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3750 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3751 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3752 | // For floating-point precision of 12: |
| 3753 | // |
| 3754 | // TwoToFractionalPartOfX = |
| 3755 | // 0.999892986f + |
| 3756 | // (0.696457318f + |
| 3757 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3758 | // |
| 3759 | // error 0.000107046256, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3760 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3761 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3762 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3763 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3764 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3765 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3766 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3767 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3768 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3769 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3770 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3771 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3772 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3773 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3774 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3775 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3776 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3777 | // For floating-point precision of 18: |
| 3778 | // |
| 3779 | // TwoToFractionalPartOfX = |
| 3780 | // 0.999999982f + |
| 3781 | // (0.693148872f + |
| 3782 | // (0.240227044f + |
| 3783 | // (0.554906021e-1f + |
| 3784 | // (0.961591928e-2f + |
| 3785 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3786 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3787 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3788 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3789 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3790 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3791 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3792 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3793 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3794 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3795 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3796 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3797 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3798 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3799 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3800 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3801 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3802 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3803 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3804 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3805 | getF32Constant(DAG, 0x3f800000)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3806 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3807 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3808 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3809 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3810 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3811 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3812 | } |
| 3813 | } else { |
| 3814 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3815 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3816 | getValue(I.getOperand(1)).getValueType(), |
| 3817 | getValue(I.getOperand(1)), |
| 3818 | getValue(I.getOperand(2))); |
| 3819 | } |
| 3820 | |
| 3821 | setValue(&I, result); |
| 3822 | } |
| 3823 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3824 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3825 | /// we want to emit this as a call to a named external function, return the name |
| 3826 | /// otherwise lower it and return null. |
| 3827 | const char * |
| 3828 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3829 | DebugLoc dl = getCurDebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3830 | switch (Intrinsic) { |
| 3831 | default: |
| 3832 | // By default, turn this into a target intrinsic node. |
| 3833 | visitTargetIntrinsic(I, Intrinsic); |
| 3834 | return 0; |
| 3835 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3836 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3837 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3838 | case Intrinsic::returnaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3839 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3840 | getValue(I.getOperand(1)))); |
| 3841 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3842 | case Intrinsic::frameaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3843 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3844 | getValue(I.getOperand(1)))); |
| 3845 | return 0; |
| 3846 | case Intrinsic::setjmp: |
| 3847 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3848 | break; |
| 3849 | case Intrinsic::longjmp: |
| 3850 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3851 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3852 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3853 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3854 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3855 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3856 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3857 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3858 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3859 | return 0; |
| 3860 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3861 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3862 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3863 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3864 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3865 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3866 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3867 | I.getOperand(1), 0)); |
| 3868 | return 0; |
| 3869 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3870 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3871 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3872 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3873 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3874 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3875 | |
| 3876 | // If the source and destination are known to not be aliases, we can |
| 3877 | // lower memmove as memcpy. |
| 3878 | uint64_t Size = -1ULL; |
| 3879 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3880 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3881 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3882 | AliasAnalysis::NoAlias) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3883 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3884 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3888 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3889 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | case Intrinsic::dbg_stoppoint: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3893 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3894 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 3895 | if (DW && DW->ValidDebugInfo(SPI.getContext(), OptLevel)) { |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3896 | MachineFunction &MF = DAG.getMachineFunction(); |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3897 | if (OptLevel == CodeGenOpt::None) |
Dale Johannesen | beaec4c | 2009-03-25 17:36:08 +0000 | [diff] [blame] | 3898 | DAG.setRoot(DAG.getDbgStopPoint(getRoot(), |
| 3899 | SPI.getLine(), |
| 3900 | SPI.getColumn(), |
| 3901 | SPI.getContext())); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3902 | DICompileUnit CU(cast<GlobalVariable>(SPI.getContext())); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 3903 | unsigned idx = MF.getOrCreateDebugLocID(CU.getGV(), |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3904 | SPI.getLine(), SPI.getColumn()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3905 | setCurDebugLoc(DebugLoc::get(idx)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3906 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3907 | return 0; |
| 3908 | } |
| 3909 | case Intrinsic::dbg_region_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3910 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3911 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 3912 | |
| 3913 | if (DW && DW->ValidDebugInfo(RSI.getContext(), OptLevel)) { |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3914 | unsigned LabelID = |
| 3915 | DW->RecordRegionStart(cast<GlobalVariable>(RSI.getContext())); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3916 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3917 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3918 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3919 | |
| 3920 | return 0; |
| 3921 | } |
| 3922 | case Intrinsic::dbg_region_end: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3923 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3924 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3925 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 3926 | if (DW && DW->ValidDebugInfo(REI.getContext(), OptLevel)) { |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3927 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3928 | DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext())); |
| 3929 | std::string SPName; |
| 3930 | Subprogram.getLinkageName(SPName); |
| 3931 | if (!SPName.empty() |
| 3932 | && strcmp(SPName.c_str(), MF.getFunction()->getNameStart())) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3933 | // This is end of inlined function. Debugging information for |
| 3934 | // inlined function is not handled yet (only supported by FastISel). |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3935 | if (OptLevel == CodeGenOpt::None) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3936 | unsigned ID = DW->RecordInlinedFnEnd(Subprogram); |
| 3937 | if (ID != 0) |
Devang Patel | 02f8c41 | 2009-04-16 17:55:30 +0000 | [diff] [blame] | 3938 | // Returned ID is 0 if this is unbalanced "end of inlined |
| 3939 | // scope". This could happen if optimizer eats dbg intrinsics |
| 3940 | // or "beginning of inlined scope" is not recoginized due to |
| 3941 | // missing location info. In such cases, do ignore this region.end. |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3942 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3943 | getRoot(), ID)); |
| 3944 | } |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3945 | return 0; |
| 3946 | } |
| 3947 | |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3948 | unsigned LabelID = |
| 3949 | DW->RecordRegionEnd(cast<GlobalVariable>(REI.getContext())); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3950 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3951 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3952 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3953 | |
| 3954 | return 0; |
| 3955 | } |
| 3956 | case Intrinsic::dbg_func_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3957 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3958 | if (!DW) return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3959 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
| 3960 | Value *SP = FSI.getSubprogram(); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 3961 | if (SP && DW->ValidDebugInfo(SP, OptLevel)) { |
| 3962 | MachineFunction &MF = DAG.getMachineFunction(); |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3963 | if (OptLevel == CodeGenOpt::None) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3964 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is what |
| 3965 | // (most?) gdb expects. |
| 3966 | DebugLoc PrevLoc = CurDebugLoc; |
| 3967 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 3968 | DICompileUnit CompileUnit = Subprogram.getCompileUnit(); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3969 | |
| 3970 | if (!Subprogram.describes(MF.getFunction())) { |
| 3971 | // This is a beginning of an inlined function. |
| 3972 | |
Devang Patel | 02f8c41 | 2009-04-16 17:55:30 +0000 | [diff] [blame] | 3973 | // If llvm.dbg.func.start is seen in a new block before any |
| 3974 | // llvm.dbg.stoppoint intrinsic then the location info is unknown. |
| 3975 | // FIXME : Why DebugLoc is reset at the beginning of each block ? |
| 3976 | if (PrevLoc.isUnknown()) |
| 3977 | return 0; |
| 3978 | |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3979 | // Record the source line. |
| 3980 | unsigned Line = Subprogram.getLineNumber(); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 3981 | unsigned LabelID = DW->RecordSourceLine(Line, 0, CompileUnit); |
| 3982 | setCurDebugLoc(DebugLoc::get( |
| 3983 | MF.getOrCreateDebugLocID(CompileUnit.getGV(), Line, 0))); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3984 | |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 3985 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3986 | getRoot(), LabelID)); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3987 | DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc); |
| 3988 | DW->RecordInlinedFnStart(&FSI, Subprogram, LabelID, |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 3989 | DICompileUnit(PrevLocTpl.CompileUnit), |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3990 | PrevLocTpl.Line, |
| 3991 | PrevLocTpl.Col); |
| 3992 | } else { |
| 3993 | // Record the source line. |
| 3994 | unsigned Line = Subprogram.getLineNumber(); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 3995 | setCurDebugLoc(DebugLoc::get( |
| 3996 | MF.getOrCreateDebugLocID(CompileUnit.getGV(), Line, 0))); |
| 3997 | DW->RecordSourceLine(Line, 0, CompileUnit); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3998 | // llvm.dbg.func_start also defines beginning of function scope. |
| 3999 | DW->RecordRegionStart(cast<GlobalVariable>(FSI.getSubprogram())); |
| 4000 | } |
| 4001 | } else { |
| 4002 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 4003 | |
| 4004 | std::string SPName; |
| 4005 | Subprogram.getLinkageName(SPName); |
| 4006 | if (!SPName.empty() |
| 4007 | && strcmp(SPName.c_str(), MF.getFunction()->getNameStart())) { |
| 4008 | // This is beginning of inlined function. Debugging information for |
| 4009 | // inlined function is not handled yet (only supported by FastISel). |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
| 4013 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is |
| 4014 | // what (most?) gdb expects. |
| 4015 | DICompileUnit CompileUnit = Subprogram.getCompileUnit(); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4016 | |
| 4017 | // Record the source line but does not create a label for the normal |
| 4018 | // function start. It will be emitted at asm emission time. However, |
| 4019 | // create a label if this is a beginning of inlined function. |
| 4020 | unsigned Line = Subprogram.getLineNumber(); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame^] | 4021 | setCurDebugLoc(DebugLoc::get( |
| 4022 | MF.getOrCreateDebugLocID(CompileUnit.getGV(), Line, 0))); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4023 | // FIXME - Start new region because llvm.dbg.func_start also defines |
| 4024 | // beginning of function scope. |
| 4025 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | return 0; |
| 4029 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4030 | case Intrinsic::dbg_declare: { |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 4031 | if (OptLevel == CodeGenOpt::None) { |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 4032 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 4033 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 4034 | Value *Variable = DI.getVariable(); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 4035 | if (DW && DW->ValidDebugInfo(Variable, OptLevel)) |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 4036 | DAG.setRoot(DAG.getNode(ISD::DECLARE, dl, MVT::Other, getRoot(), |
| 4037 | getValue(DI.getAddress()), getValue(Variable))); |
| 4038 | } else { |
| 4039 | // FIXME: Do something sensible here when we support debug declare. |
| 4040 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4041 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4042 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4043 | case Intrinsic::eh_exception: { |
| 4044 | if (!CurMBB->isLandingPad()) { |
| 4045 | // FIXME: Mark exception register as live in. Hack for PR1508. |
| 4046 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 4047 | if (Reg) CurMBB->addLiveIn(Reg); |
| 4048 | } |
| 4049 | // Insert the EXCEPTIONADDR instruction. |
| 4050 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 4051 | SDValue Ops[1]; |
| 4052 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4053 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4054 | setValue(&I, Op); |
| 4055 | DAG.setRoot(Op.getValue(1)); |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | case Intrinsic::eh_selector_i32: |
| 4060 | case Intrinsic::eh_selector_i64: { |
| 4061 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4062 | MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ? |
| 4063 | MVT::i32 : MVT::i64); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4064 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4065 | if (MMI) { |
| 4066 | if (CurMBB->isLandingPad()) |
| 4067 | AddCatchInfo(I, MMI, CurMBB); |
| 4068 | else { |
| 4069 | #ifndef NDEBUG |
| 4070 | FuncInfo.CatchInfoLost.insert(&I); |
| 4071 | #endif |
| 4072 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 4073 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 4074 | if (Reg) CurMBB->addLiveIn(Reg); |
| 4075 | } |
| 4076 | |
| 4077 | // Insert the EHSELECTION instruction. |
| 4078 | SDVTList VTs = DAG.getVTList(VT, MVT::Other); |
| 4079 | SDValue Ops[2]; |
| 4080 | Ops[0] = getValue(I.getOperand(1)); |
| 4081 | Ops[1] = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4082 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4083 | setValue(&I, Op); |
| 4084 | DAG.setRoot(Op.getValue(1)); |
| 4085 | } else { |
| 4086 | setValue(&I, DAG.getConstant(0, VT)); |
| 4087 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4088 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4089 | return 0; |
| 4090 | } |
| 4091 | |
| 4092 | case Intrinsic::eh_typeid_for_i32: |
| 4093 | case Intrinsic::eh_typeid_for_i64: { |
| 4094 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4095 | MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ? |
| 4096 | MVT::i32 : MVT::i64); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4097 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4098 | if (MMI) { |
| 4099 | // Find the type id for the given typeinfo. |
| 4100 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 4101 | |
| 4102 | unsigned TypeID = MMI->getTypeIDFor(GV); |
| 4103 | setValue(&I, DAG.getConstant(TypeID, VT)); |
| 4104 | } else { |
| 4105 | // Return something different to eh_selector. |
| 4106 | setValue(&I, DAG.getConstant(1, VT)); |
| 4107 | } |
| 4108 | |
| 4109 | return 0; |
| 4110 | } |
| 4111 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4112 | case Intrinsic::eh_return_i32: |
| 4113 | case Intrinsic::eh_return_i64: |
| 4114 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4115 | MMI->setCallsEHReturn(true); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4116 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4117 | MVT::Other, |
| 4118 | getControlRoot(), |
| 4119 | getValue(I.getOperand(1)), |
| 4120 | getValue(I.getOperand(2)))); |
| 4121 | } else { |
| 4122 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 4123 | } |
| 4124 | |
| 4125 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4126 | case Intrinsic::eh_unwind_init: |
| 4127 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 4128 | MMI->setCallsUnwindInit(true); |
| 4129 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4130 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4131 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4132 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4133 | case Intrinsic::eh_dwarf_cfa: { |
| 4134 | MVT VT = getValue(I.getOperand(1)).getValueType(); |
| 4135 | SDValue CfaArg; |
| 4136 | if (VT.bitsGT(TLI.getPointerTy())) |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4137 | CfaArg = DAG.getNode(ISD::TRUNCATE, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4138 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
| 4139 | else |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4140 | CfaArg = DAG.getNode(ISD::SIGN_EXTEND, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4141 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4142 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4143 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4144 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4145 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4146 | TLI.getPointerTy()), |
| 4147 | CfaArg); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4148 | setValue(&I, DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4149 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4150 | DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4151 | TLI.getPointerTy(), |
| 4152 | DAG.getConstant(0, |
| 4153 | TLI.getPointerTy())), |
| 4154 | Offset)); |
| 4155 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4158 | case Intrinsic::convertff: |
| 4159 | case Intrinsic::convertfsi: |
| 4160 | case Intrinsic::convertfui: |
| 4161 | case Intrinsic::convertsif: |
| 4162 | case Intrinsic::convertuif: |
| 4163 | case Intrinsic::convertss: |
| 4164 | case Intrinsic::convertsu: |
| 4165 | case Intrinsic::convertus: |
| 4166 | case Intrinsic::convertuu: { |
| 4167 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4168 | switch (Intrinsic) { |
| 4169 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4170 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4171 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4172 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4173 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4174 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4175 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4176 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4177 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4178 | } |
| 4179 | MVT DestVT = TLI.getValueType(I.getType()); |
| 4180 | Value* Op1 = I.getOperand(1); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4181 | setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4182 | DAG.getValueType(DestVT), |
| 4183 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4184 | getValue(I.getOperand(2)), |
| 4185 | getValue(I.getOperand(3)), |
| 4186 | Code)); |
| 4187 | return 0; |
| 4188 | } |
| 4189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4190 | case Intrinsic::sqrt: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4191 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4192 | getValue(I.getOperand(1)).getValueType(), |
| 4193 | getValue(I.getOperand(1)))); |
| 4194 | return 0; |
| 4195 | case Intrinsic::powi: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4196 | setValue(&I, DAG.getNode(ISD::FPOWI, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4197 | getValue(I.getOperand(1)).getValueType(), |
| 4198 | getValue(I.getOperand(1)), |
| 4199 | getValue(I.getOperand(2)))); |
| 4200 | return 0; |
| 4201 | case Intrinsic::sin: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4202 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4203 | getValue(I.getOperand(1)).getValueType(), |
| 4204 | getValue(I.getOperand(1)))); |
| 4205 | return 0; |
| 4206 | case Intrinsic::cos: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4207 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4208 | getValue(I.getOperand(1)).getValueType(), |
| 4209 | getValue(I.getOperand(1)))); |
| 4210 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4211 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4212 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4213 | return 0; |
| 4214 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4215 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4216 | return 0; |
| 4217 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4218 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4219 | return 0; |
| 4220 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4221 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4222 | return 0; |
| 4223 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4224 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4225 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4226 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4227 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4228 | return 0; |
| 4229 | case Intrinsic::pcmarker: { |
| 4230 | SDValue Tmp = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4231 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4232 | return 0; |
| 4233 | } |
| 4234 | case Intrinsic::readcyclecounter: { |
| 4235 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4236 | SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4237 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4238 | &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4239 | setValue(&I, Tmp); |
| 4240 | DAG.setRoot(Tmp.getValue(1)); |
| 4241 | return 0; |
| 4242 | } |
| 4243 | case Intrinsic::part_select: { |
| 4244 | // Currently not implemented: just abort |
| 4245 | assert(0 && "part_select intrinsic not implemented"); |
| 4246 | abort(); |
| 4247 | } |
| 4248 | case Intrinsic::part_set: { |
| 4249 | // Currently not implemented: just abort |
| 4250 | assert(0 && "part_set intrinsic not implemented"); |
| 4251 | abort(); |
| 4252 | } |
| 4253 | case Intrinsic::bswap: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4254 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4255 | getValue(I.getOperand(1)).getValueType(), |
| 4256 | getValue(I.getOperand(1)))); |
| 4257 | return 0; |
| 4258 | case Intrinsic::cttz: { |
| 4259 | SDValue Arg = getValue(I.getOperand(1)); |
| 4260 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4261 | SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4262 | setValue(&I, result); |
| 4263 | return 0; |
| 4264 | } |
| 4265 | case Intrinsic::ctlz: { |
| 4266 | SDValue Arg = getValue(I.getOperand(1)); |
| 4267 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4268 | SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4269 | setValue(&I, result); |
| 4270 | return 0; |
| 4271 | } |
| 4272 | case Intrinsic::ctpop: { |
| 4273 | SDValue Arg = getValue(I.getOperand(1)); |
| 4274 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4275 | SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4276 | setValue(&I, result); |
| 4277 | return 0; |
| 4278 | } |
| 4279 | case Intrinsic::stacksave: { |
| 4280 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4281 | SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4282 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4283 | setValue(&I, Tmp); |
| 4284 | DAG.setRoot(Tmp.getValue(1)); |
| 4285 | return 0; |
| 4286 | } |
| 4287 | case Intrinsic::stackrestore: { |
| 4288 | SDValue Tmp = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4289 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4290 | return 0; |
| 4291 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4292 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4293 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4294 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4295 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 4296 | MVT PtrTy = TLI.getPointerTy(); |
| 4297 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4298 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4299 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4300 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4301 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4302 | MFI->setStackProtectorIndex(FI); |
| 4303 | |
| 4304 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4305 | |
| 4306 | // Store the stack protector onto the stack. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4307 | SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4308 | PseudoSourceValue::getFixedStack(FI), |
| 4309 | 0, true); |
| 4310 | setValue(&I, Result); |
| 4311 | DAG.setRoot(Result); |
| 4312 | return 0; |
| 4313 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4314 | case Intrinsic::var_annotation: |
| 4315 | // Discard annotate attributes |
| 4316 | return 0; |
| 4317 | |
| 4318 | case Intrinsic::init_trampoline: { |
| 4319 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4320 | |
| 4321 | SDValue Ops[6]; |
| 4322 | Ops[0] = getRoot(); |
| 4323 | Ops[1] = getValue(I.getOperand(1)); |
| 4324 | Ops[2] = getValue(I.getOperand(2)); |
| 4325 | Ops[3] = getValue(I.getOperand(3)); |
| 4326 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4327 | Ops[5] = DAG.getSrcValue(F); |
| 4328 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4329 | SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4330 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4331 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4332 | |
| 4333 | setValue(&I, Tmp); |
| 4334 | DAG.setRoot(Tmp.getValue(1)); |
| 4335 | return 0; |
| 4336 | } |
| 4337 | |
| 4338 | case Intrinsic::gcroot: |
| 4339 | if (GFI) { |
| 4340 | Value *Alloca = I.getOperand(1); |
| 4341 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4342 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4343 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4344 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4345 | } |
| 4346 | return 0; |
| 4347 | |
| 4348 | case Intrinsic::gcread: |
| 4349 | case Intrinsic::gcwrite: |
| 4350 | assert(0 && "GC failed to lower gcread/gcwrite intrinsics!"); |
| 4351 | return 0; |
| 4352 | |
| 4353 | case Intrinsic::flt_rounds: { |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4354 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4355 | return 0; |
| 4356 | } |
| 4357 | |
| 4358 | case Intrinsic::trap: { |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4359 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4360 | return 0; |
| 4361 | } |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4362 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4363 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4364 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4365 | case Intrinsic::sadd_with_overflow: |
| 4366 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4367 | case Intrinsic::usub_with_overflow: |
| 4368 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4369 | case Intrinsic::ssub_with_overflow: |
| 4370 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4371 | case Intrinsic::umul_with_overflow: |
| 4372 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4373 | case Intrinsic::smul_with_overflow: |
| 4374 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4375 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4376 | case Intrinsic::prefetch: { |
| 4377 | SDValue Ops[4]; |
| 4378 | Ops[0] = getRoot(); |
| 4379 | Ops[1] = getValue(I.getOperand(1)); |
| 4380 | Ops[2] = getValue(I.getOperand(2)); |
| 4381 | Ops[3] = getValue(I.getOperand(3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4382 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4383 | return 0; |
| 4384 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4385 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4386 | case Intrinsic::memory_barrier: { |
| 4387 | SDValue Ops[6]; |
| 4388 | Ops[0] = getRoot(); |
| 4389 | for (int x = 1; x < 6; ++x) |
| 4390 | Ops[x] = getValue(I.getOperand(x)); |
| 4391 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4392 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4393 | return 0; |
| 4394 | } |
| 4395 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4396 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4397 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4398 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4399 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4400 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4401 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4402 | getValue(I.getOperand(2)), |
| 4403 | getValue(I.getOperand(3)), |
| 4404 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4405 | setValue(&I, L); |
| 4406 | DAG.setRoot(L.getValue(1)); |
| 4407 | return 0; |
| 4408 | } |
| 4409 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4410 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4411 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4412 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4413 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4414 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4415 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4416 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4417 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4418 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4419 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4420 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4421 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4422 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4423 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4424 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4425 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4426 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4427 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4428 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4429 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4430 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee, |
| 4436 | bool IsTailCall, |
| 4437 | MachineBasicBlock *LandingPad) { |
| 4438 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4439 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4440 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4441 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4442 | |
| 4443 | TargetLowering::ArgListTy Args; |
| 4444 | TargetLowering::ArgListEntry Entry; |
| 4445 | Args.reserve(CS.arg_size()); |
| 4446 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 4447 | i != e; ++i) { |
| 4448 | SDValue ArgNode = getValue(*i); |
| 4449 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4450 | |
| 4451 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4452 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4453 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4454 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4455 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4456 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4457 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4458 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4459 | Args.push_back(Entry); |
| 4460 | } |
| 4461 | |
| 4462 | if (LandingPad && MMI) { |
| 4463 | // Insert a label before the invoke call to mark the try range. This can be |
| 4464 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4465 | BeginLabel = MMI->NextLabelID(); |
| 4466 | // Both PendingLoads and PendingExports must be flushed here; |
| 4467 | // this call might not return. |
| 4468 | (void)getRoot(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4469 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4470 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | std::pair<SDValue,SDValue> Result = |
| 4474 | TLI.LowerCallTo(getRoot(), CS.getType(), |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4475 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4476 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
| 4477 | CS.paramHasAttr(0, Attribute::InReg), |
| 4478 | CS.getCallingConv(), |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 4479 | IsTailCall && PerformTailCallOpt, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4480 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4481 | if (CS.getType() != Type::VoidTy) |
| 4482 | setValue(CS.getInstruction(), Result.first); |
| 4483 | DAG.setRoot(Result.second); |
| 4484 | |
| 4485 | if (LandingPad && MMI) { |
| 4486 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4487 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4488 | EndLabel = MMI->NextLabelID(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4489 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4490 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4491 | |
| 4492 | // Inform MachineModuleInfo of range. |
| 4493 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | |
| 4498 | void SelectionDAGLowering::visitCall(CallInst &I) { |
| 4499 | const char *RenameFn = 0; |
| 4500 | if (Function *F = I.getCalledFunction()) { |
| 4501 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4502 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4503 | if (II) { |
| 4504 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4505 | RenameFn = visitIntrinsicCall(I, IID); |
| 4506 | if (!RenameFn) |
| 4507 | return; |
| 4508 | } |
| 4509 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4510 | if (unsigned IID = F->getIntrinsicID()) { |
| 4511 | RenameFn = visitIntrinsicCall(I, IID); |
| 4512 | if (!RenameFn) |
| 4513 | return; |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4518 | // can't be a library call. |
| 4519 | unsigned NameLen = F->getNameLen(); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 4520 | if (!F->hasLocalLinkage() && NameLen) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4521 | const char *NameStr = F->getNameStart(); |
| 4522 | if (NameStr[0] == 'c' && |
| 4523 | ((NameLen == 8 && !strcmp(NameStr, "copysign")) || |
| 4524 | (NameLen == 9 && !strcmp(NameStr, "copysignf")))) { |
| 4525 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4526 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4527 | I.getType() == I.getOperand(1)->getType() && |
| 4528 | I.getType() == I.getOperand(2)->getType()) { |
| 4529 | SDValue LHS = getValue(I.getOperand(1)); |
| 4530 | SDValue RHS = getValue(I.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4531 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4532 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4533 | return; |
| 4534 | } |
| 4535 | } else if (NameStr[0] == 'f' && |
| 4536 | ((NameLen == 4 && !strcmp(NameStr, "fabs")) || |
| 4537 | (NameLen == 5 && !strcmp(NameStr, "fabsf")) || |
| 4538 | (NameLen == 5 && !strcmp(NameStr, "fabsl")))) { |
| 4539 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4540 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4541 | I.getType() == I.getOperand(1)->getType()) { |
| 4542 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4543 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4544 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4545 | return; |
| 4546 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4547 | } else if (NameStr[0] == 's' && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4548 | ((NameLen == 3 && !strcmp(NameStr, "sin")) || |
| 4549 | (NameLen == 4 && !strcmp(NameStr, "sinf")) || |
| 4550 | (NameLen == 4 && !strcmp(NameStr, "sinl")))) { |
| 4551 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4552 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4553 | I.getType() == I.getOperand(1)->getType()) { |
| 4554 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4555 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4556 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4557 | return; |
| 4558 | } |
| 4559 | } else if (NameStr[0] == 'c' && |
| 4560 | ((NameLen == 3 && !strcmp(NameStr, "cos")) || |
| 4561 | (NameLen == 4 && !strcmp(NameStr, "cosf")) || |
| 4562 | (NameLen == 4 && !strcmp(NameStr, "cosl")))) { |
| 4563 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4564 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4565 | I.getType() == I.getOperand(1)->getType()) { |
| 4566 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4567 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4568 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4569 | return; |
| 4570 | } |
| 4571 | } |
| 4572 | } |
| 4573 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4574 | visitInlineAsm(&I); |
| 4575 | return; |
| 4576 | } |
| 4577 | |
| 4578 | SDValue Callee; |
| 4579 | if (!RenameFn) |
| 4580 | Callee = getValue(I.getOperand(0)); |
| 4581 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4582 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4583 | |
| 4584 | LowerCallTo(&I, Callee, I.isTailCall()); |
| 4585 | } |
| 4586 | |
| 4587 | |
| 4588 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4589 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4590 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4591 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4592 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4593 | SDValue &Chain, |
| 4594 | SDValue *Flag) const { |
| 4595 | // Assemble the legal parts into the final values. |
| 4596 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4597 | SmallVector<SDValue, 8> Parts; |
| 4598 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4599 | // Copy the legal parts from the registers. |
| 4600 | MVT ValueVT = ValueVTs[Value]; |
| 4601 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 4602 | MVT RegisterVT = RegVTs[Value]; |
| 4603 | |
| 4604 | Parts.resize(NumRegs); |
| 4605 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4606 | SDValue P; |
| 4607 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4608 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4609 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4610 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4611 | *Flag = P.getValue(2); |
| 4612 | } |
| 4613 | Chain = P.getValue(1); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4614 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4615 | // If the source register was virtual and if we know something about it, |
| 4616 | // add an assert node. |
| 4617 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4618 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4619 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4620 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4621 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4622 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4623 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4624 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4625 | unsigned NumSignBits = LOI.NumSignBits; |
| 4626 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4627 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4628 | // FIXME: We capture more information than the dag can represent. For |
| 4629 | // now, just use the tightest assertzext/assertsext possible. |
| 4630 | bool isSExt = true; |
| 4631 | MVT FromVT(MVT::Other); |
| 4632 | if (NumSignBits == RegSize) |
| 4633 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 4634 | else if (NumZeroBits >= RegSize-1) |
| 4635 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 4636 | else if (NumSignBits > RegSize-8) |
| 4637 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4638 | else if (NumZeroBits >= RegSize-8) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4639 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 4640 | else if (NumSignBits > RegSize-16) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4641 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4642 | else if (NumZeroBits >= RegSize-16) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4643 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4644 | else if (NumSignBits > RegSize-32) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4645 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4646 | else if (NumZeroBits >= RegSize-32) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4647 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4649 | if (FromVT != MVT::Other) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4650 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4651 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4652 | |
| 4653 | } |
| 4654 | } |
| 4655 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4656 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4657 | Parts[i] = P; |
| 4658 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4659 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4660 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4661 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4662 | Part += NumRegs; |
| 4663 | Parts.clear(); |
| 4664 | } |
| 4665 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4666 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 4667 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4668 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4672 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4674 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4675 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4676 | SDValue &Chain, SDValue *Flag) const { |
| 4677 | // Get the list of the values's legal parts. |
| 4678 | unsigned NumRegs = Regs.size(); |
| 4679 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4680 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4681 | MVT ValueVT = ValueVTs[Value]; |
| 4682 | unsigned NumParts = TLI->getNumRegisters(ValueVT); |
| 4683 | MVT RegisterVT = RegVTs[Value]; |
| 4684 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4685 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4686 | &Parts[Part], NumParts, RegisterVT); |
| 4687 | Part += NumParts; |
| 4688 | } |
| 4689 | |
| 4690 | // Copy the parts into the registers. |
| 4691 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4692 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4693 | SDValue Part; |
| 4694 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4695 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4696 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4697 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4698 | *Flag = Part.getValue(1); |
| 4699 | } |
| 4700 | Chains[i] = Part.getValue(0); |
| 4701 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4702 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4703 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4704 | // 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] | 4705 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4706 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4707 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4708 | // user as well as a successor (the TF operands are flagged to the user). |
| 4709 | // c1, f1 = CopyToReg |
| 4710 | // c2, f2 = CopyToReg |
| 4711 | // c3 = TokenFactor c1, c2 |
| 4712 | // ... |
| 4713 | // = op c3, ..., f2 |
| 4714 | Chain = Chains[NumRegs-1]; |
| 4715 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4716 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4720 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4721 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4722 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4723 | bool HasMatching,unsigned MatchingIdx, |
| 4724 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4725 | std::vector<SDValue> &Ops) const { |
| 4726 | MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4727 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4728 | unsigned Flag = Code | (Regs.size() << 3); |
| 4729 | if (HasMatching) |
| 4730 | Flag |= 0x80000000 | (MatchingIdx << 16); |
| 4731 | Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4732 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4733 | unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]); |
| 4734 | MVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4735 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4736 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4737 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4738 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4742 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4743 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4744 | /// register class for the register. Otherwise, return null. |
| 4745 | static const TargetRegisterClass * |
| 4746 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4747 | const TargetLowering &TLI, |
| 4748 | const TargetRegisterInfo *TRI) { |
| 4749 | MVT FoundVT = MVT::Other; |
| 4750 | const TargetRegisterClass *FoundRC = 0; |
| 4751 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4752 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
| 4753 | MVT ThisVT = MVT::Other; |
| 4754 | |
| 4755 | const TargetRegisterClass *RC = *RCI; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4756 | // 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] | 4757 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4758 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4759 | I != E; ++I) { |
| 4760 | if (TLI.isTypeLegal(*I)) { |
| 4761 | // If we have already found this register in a different register class, |
| 4762 | // choose the one with the largest VT specified. For example, on |
| 4763 | // PowerPC, we favor f64 register classes over f32. |
| 4764 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
| 4765 | ThisVT = *I; |
| 4766 | break; |
| 4767 | } |
| 4768 | } |
| 4769 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4770 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4771 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4772 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4773 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4774 | // frame pointer in functions that need it (due to them not being taken |
| 4775 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4776 | // yet). This is a slight code pessimization, but should still work. |
| 4777 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4778 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4779 | if (*I == Reg) { |
| 4780 | // We found a matching register class. Keep looking at others in case |
| 4781 | // we find one with larger registers that this physreg is also in. |
| 4782 | FoundRC = RC; |
| 4783 | FoundVT = ThisVT; |
| 4784 | break; |
| 4785 | } |
| 4786 | } |
| 4787 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4788 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4789 | |
| 4790 | |
| 4791 | namespace llvm { |
| 4792 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4793 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4794 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4795 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4796 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4797 | /// CallOperand - If this is the result output operand or a clobber |
| 4798 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4799 | /// This gets modified as the asm is processed. |
| 4800 | SDValue CallOperand; |
| 4801 | |
| 4802 | /// AssignedRegs - If this is a register or register class operand, this |
| 4803 | /// contains the set of register corresponding to the operand. |
| 4804 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4805 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4806 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4807 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4808 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4809 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4810 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4811 | /// busy in OutputRegs/InputRegs. |
| 4812 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4813 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4814 | std::set<unsigned> &InputRegs, |
| 4815 | const TargetRegisterInfo &TRI) const { |
| 4816 | if (isOutReg) { |
| 4817 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4818 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4819 | } |
| 4820 | if (isInReg) { |
| 4821 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4822 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4823 | } |
| 4824 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4825 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4826 | /// getCallOperandValMVT - Return the MVT of the Value* that this operand |
| 4827 | /// corresponds to. If there is no Value* for this operand, it returns |
| 4828 | /// MVT::Other. |
| 4829 | MVT getCallOperandValMVT(const TargetLowering &TLI, |
| 4830 | const TargetData *TD) const { |
| 4831 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4832 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4833 | if (isa<BasicBlock>(CallOperandVal)) |
| 4834 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4835 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4836 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4837 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4838 | // If this is an indirect operand, the operand is a pointer to the |
| 4839 | // accessed type. |
| 4840 | if (isIndirect) |
| 4841 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4842 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4843 | // If OpTy is not a single value, it may be a struct/union that we |
| 4844 | // can tile with integers. |
| 4845 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4846 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4847 | switch (BitSize) { |
| 4848 | default: break; |
| 4849 | case 1: |
| 4850 | case 8: |
| 4851 | case 16: |
| 4852 | case 32: |
| 4853 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4854 | case 128: |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4855 | OpTy = IntegerType::get(BitSize); |
| 4856 | break; |
| 4857 | } |
| 4858 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4859 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4860 | return TLI.getValueType(OpTy, true); |
| 4861 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4862 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4863 | private: |
| 4864 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4865 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4866 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4867 | const TargetRegisterInfo &TRI) { |
| 4868 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4869 | Regs.insert(Reg); |
| 4870 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4871 | for (; *Aliases; ++Aliases) |
| 4872 | Regs.insert(*Aliases); |
| 4873 | } |
| 4874 | }; |
| 4875 | } // end llvm namespace. |
| 4876 | |
| 4877 | |
| 4878 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4879 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4880 | /// register allocator handle the assignment process. However, if the asm uses |
| 4881 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 4882 | /// allocation. This produces generally horrible, but correct, code. |
| 4883 | /// |
| 4884 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4885 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4886 | /// |
| 4887 | void SelectionDAGLowering:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4888 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4889 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4890 | std::set<unsigned> &InputRegs) { |
| 4891 | // Compute whether this value requires an input register, an output register, |
| 4892 | // or both. |
| 4893 | bool isOutReg = false; |
| 4894 | bool isInReg = false; |
| 4895 | switch (OpInfo.Type) { |
| 4896 | case InlineAsm::isOutput: |
| 4897 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4898 | |
| 4899 | // 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] | 4900 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 4901 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4902 | break; |
| 4903 | case InlineAsm::isInput: |
| 4904 | isInReg = true; |
| 4905 | isOutReg = false; |
| 4906 | break; |
| 4907 | case InlineAsm::isClobber: |
| 4908 | isOutReg = true; |
| 4909 | isInReg = true; |
| 4910 | break; |
| 4911 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4912 | |
| 4913 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4914 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4915 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4916 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4917 | // If this is a constraint for a single physreg, or a constraint for a |
| 4918 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4919 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4920 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 4921 | OpInfo.ConstraintVT); |
| 4922 | |
| 4923 | unsigned NumRegs = 1; |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4924 | if (OpInfo.ConstraintVT != MVT::Other) { |
| 4925 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 4926 | // cast of the input value. More generally, handle any case where the input |
| 4927 | // value disagrees with the register class we plan to stick this in. |
| 4928 | if (OpInfo.Type == InlineAsm::isInput && |
| 4929 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
| 4930 | // Try to convert to the first MVT that the reg class contains. If the |
| 4931 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 4932 | // vector types). |
| 4933 | MVT RegVT = *PhysReg.second->vt_begin(); |
| 4934 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4935 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4936 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4937 | OpInfo.ConstraintVT = RegVT; |
| 4938 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 4939 | // If the input is a FP value and we want it in FP registers, do a |
| 4940 | // bitcast to the corresponding integer type. This turns an f64 value |
| 4941 | // into i64, which can be passed with two i32 values on a 32-bit |
| 4942 | // machine. |
| 4943 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4944 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4945 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4946 | OpInfo.ConstraintVT = RegVT; |
| 4947 | } |
| 4948 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4950 | NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4951 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4952 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4953 | MVT RegVT; |
| 4954 | MVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4955 | |
| 4956 | // If this is a constraint for a specific physical register, like {r17}, |
| 4957 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4958 | if (unsigned AssignedReg = PhysReg.first) { |
| 4959 | const TargetRegisterClass *RC = PhysReg.second; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4960 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4961 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4962 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4963 | // Get the actual register value type. This is important, because the user |
| 4964 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 4965 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4966 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4967 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4968 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4969 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4970 | |
| 4971 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 4972 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4973 | TargetRegisterClass::iterator I = RC->begin(); |
| 4974 | for (; *I != AssignedReg; ++I) |
| 4975 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4976 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4977 | // Already added the first reg. |
| 4978 | --NumRegs; ++I; |
| 4979 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 4980 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4981 | Regs.push_back(*I); |
| 4982 | } |
| 4983 | } |
| 4984 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 4985 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 4986 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 4987 | return; |
| 4988 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4989 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4990 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 4991 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 4992 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 4993 | RegVT = *RC->vt_begin(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 4994 | if (OpInfo.ConstraintVT == MVT::Other) |
| 4995 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4996 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 4997 | // Create the appropriate number of virtual registers. |
| 4998 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4999 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5000 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5001 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5002 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5003 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5004 | } |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5005 | |
| 5006 | // This is a reference to a register class that doesn't directly correspond |
| 5007 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5008 | // registers from the class. |
| 5009 | std::vector<unsigned> RegClassRegs |
| 5010 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5011 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5012 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5013 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5014 | unsigned NumAllocated = 0; |
| 5015 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5016 | unsigned Reg = RegClassRegs[i]; |
| 5017 | // See if this register is available. |
| 5018 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5019 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5020 | // Make sure we find consecutive registers. |
| 5021 | NumAllocated = 0; |
| 5022 | continue; |
| 5023 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5024 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5025 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5026 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5027 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5028 | if (!RC) { // Couldn't allocate this register. |
| 5029 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5030 | NumAllocated = 0; |
| 5031 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5032 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5033 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5034 | // Okay, this register is good, we can use it. |
| 5035 | ++NumAllocated; |
| 5036 | |
| 5037 | // If we allocated enough consecutive registers, succeed. |
| 5038 | if (NumAllocated == NumRegs) { |
| 5039 | unsigned RegStart = (i-NumAllocated)+1; |
| 5040 | unsigned RegEnd = i+1; |
| 5041 | // Mark all of the allocated registers used. |
| 5042 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5043 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5044 | |
| 5045 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5046 | OpInfo.ConstraintVT); |
| 5047 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5048 | return; |
| 5049 | } |
| 5050 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5051 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5052 | // Otherwise, we couldn't allocate enough registers for this. |
| 5053 | } |
| 5054 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5055 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5056 | /// processed uses a memory 'm' constraint. |
| 5057 | static bool |
| 5058 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5059 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5060 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5061 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5062 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5063 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5064 | if (CType == TargetLowering::C_Memory) |
| 5065 | return true; |
| 5066 | } |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5067 | |
| 5068 | // Indirect operand accesses access memory. |
| 5069 | if (CI.isIndirect) |
| 5070 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | return false; |
| 5074 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5075 | |
| 5076 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5077 | /// |
| 5078 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 5079 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5080 | |
| 5081 | /// ConstraintOperands - Information about all of the constraints. |
| 5082 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5083 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5084 | std::set<unsigned> OutputRegs, InputRegs; |
| 5085 | |
| 5086 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5087 | // ConstraintOperands list. |
| 5088 | std::vector<InlineAsm::ConstraintInfo> |
| 5089 | ConstraintInfos = IA->ParseConstraints(); |
| 5090 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5091 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5092 | |
| 5093 | SDValue Chain, Flag; |
| 5094 | |
| 5095 | // We won't need to flush pending loads if this asm doesn't touch |
| 5096 | // memory and is nonvolatile. |
| 5097 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5098 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5099 | else |
| 5100 | Chain = DAG.getRoot(); |
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 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5103 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5104 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5105 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5106 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5108 | MVT OpVT = MVT::Other; |
| 5109 | |
| 5110 | // Compute the value type for each operand. |
| 5111 | switch (OpInfo.Type) { |
| 5112 | case InlineAsm::isOutput: |
| 5113 | // Indirect outputs just consume an argument. |
| 5114 | if (OpInfo.isIndirect) { |
| 5115 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5116 | break; |
| 5117 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5118 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5119 | // The return value of the call is this value. As such, there is no |
| 5120 | // corresponding argument. |
| 5121 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 5122 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5123 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5124 | } else { |
| 5125 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5126 | OpVT = TLI.getValueType(CS.getType()); |
| 5127 | } |
| 5128 | ++ResNo; |
| 5129 | break; |
| 5130 | case InlineAsm::isInput: |
| 5131 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5132 | break; |
| 5133 | case InlineAsm::isClobber: |
| 5134 | // Nothing to do. |
| 5135 | break; |
| 5136 | } |
| 5137 | |
| 5138 | // If this is an input or an indirect output, process the call argument. |
| 5139 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5140 | if (OpInfo.CallOperandVal) { |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5141 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5142 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5143 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5144 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5145 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5146 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5147 | OpVT = OpInfo.getCallOperandValMVT(TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5148 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5149 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5150 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5151 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5152 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5153 | // Second pass over the constraints: compute which constraint option to use |
| 5154 | // and assign registers to constraints that want a specific physreg. |
| 5155 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5156 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5157 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5158 | // 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] | 5159 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5160 | // other is floating point, or their sizes are different, flag it as an |
| 5161 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5162 | if (OpInfo.hasMatchingInput()) { |
| 5163 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5164 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5165 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5166 | Input.ConstraintVT.isInteger()) || |
| 5167 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5168 | Input.ConstraintVT.getSizeInBits())) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5169 | cerr << "llvm: error: Unsupported asm: input constraint with a " |
| 5170 | << "matching output constraint of incompatible type!\n"; |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5171 | exit(1); |
| 5172 | } |
| 5173 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5174 | } |
| 5175 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5176 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5177 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5178 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5180 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5181 | // need to to provide an address for the memory input. |
| 5182 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5183 | !OpInfo.isIndirect) { |
| 5184 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5185 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5186 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5187 | // Memory operands really want the address of the value. If we don't have |
| 5188 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5189 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5190 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5191 | // If the operand is a float, integer, or vector constant, spill to a |
| 5192 | // constant pool entry to get its address. |
| 5193 | Value *OpVal = OpInfo.CallOperandVal; |
| 5194 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5195 | isa<ConstantVector>(OpVal)) { |
| 5196 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5197 | TLI.getPointerTy()); |
| 5198 | } else { |
| 5199 | // Otherwise, create a stack slot and emit a store to it before the |
| 5200 | // asm. |
| 5201 | const Type *Ty = OpVal->getType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5202 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5203 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5204 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5205 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 5206 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5207 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5208 | OpInfo.CallOperand, StackSlot, NULL, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5209 | OpInfo.CallOperand = StackSlot; |
| 5210 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5211 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5212 | // There is no longer a Value* corresponding to this operand. |
| 5213 | OpInfo.CallOperandVal = 0; |
| 5214 | // It is now an indirect operand. |
| 5215 | OpInfo.isIndirect = true; |
| 5216 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5217 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5218 | // If this constraint is for a specific register, allocate it before |
| 5219 | // anything else. |
| 5220 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5221 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5222 | } |
| 5223 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5224 | |
| 5225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5226 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5227 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5228 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5229 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
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 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5232 | // to be. |
| 5233 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5234 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5237 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5238 | std::vector<SDValue> AsmNodeOperands; |
| 5239 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5240 | AsmNodeOperands.push_back( |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5241 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5242 | |
| 5243 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5244 | // Loop over all of the inputs, copying the operand values into the |
| 5245 | // appropriate registers and processing the output regs. |
| 5246 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5247 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5248 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5249 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5250 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5251 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5252 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5253 | |
| 5254 | switch (OpInfo.Type) { |
| 5255 | case InlineAsm::isOutput: { |
| 5256 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5257 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5258 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5259 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5260 | |
| 5261 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5262 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5263 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5264 | TLI.getPointerTy())); |
| 5265 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5266 | break; |
| 5267 | } |
| 5268 | |
| 5269 | // Otherwise, this is a register or register class output. |
| 5270 | |
| 5271 | // Copy the output from the appropriate register. Find a register that |
| 5272 | // we can use. |
| 5273 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5274 | cerr << "llvm: error: Couldn't allocate output reg for constraint '" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5275 | << OpInfo.ConstraintCode << "'!\n"; |
| 5276 | exit(1); |
| 5277 | } |
| 5278 | |
| 5279 | // If this is an indirect operand, store through the pointer after the |
| 5280 | // asm. |
| 5281 | if (OpInfo.isIndirect) { |
| 5282 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5283 | OpInfo.CallOperandVal)); |
| 5284 | } else { |
| 5285 | // This is the result value of the call. |
| 5286 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 5287 | // Concatenate this output onto the outputs list. |
| 5288 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5289 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5290 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5291 | // Add information to the INLINEASM node to know that this register is |
| 5292 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5293 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5294 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5295 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5296 | false, |
| 5297 | 0, |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5298 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5299 | break; |
| 5300 | } |
| 5301 | case InlineAsm::isInput: { |
| 5302 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5303 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5304 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5305 | // If this is required to match an output register we have already set, |
| 5306 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5307 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5308 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5309 | // Scan until we find the definition we already emitted of this operand. |
| 5310 | // When we find it, create a RegsForValue operand. |
| 5311 | unsigned CurOp = 2; // The first operand. |
| 5312 | for (; OperandNo; --OperandNo) { |
| 5313 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5314 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5315 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5316 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5317 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5318 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5319 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5320 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5323 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5324 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5325 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5326 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5327 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5328 | RegsForValue MatchedRegs; |
| 5329 | MatchedRegs.TLI = &TLI; |
| 5330 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5331 | MVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 5332 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5333 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5334 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5335 | i != e; ++i) |
| 5336 | MatchedRegs.Regs. |
| 5337 | push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5338 | |
| 5339 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5340 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5341 | Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5342 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5343 | true, OpInfo.getMatchedOperand(), |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5344 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5345 | break; |
| 5346 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5347 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5348 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5349 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5350 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5351 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5352 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5353 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5354 | TLI.getPointerTy())); |
| 5355 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5356 | break; |
| 5357 | } |
| 5358 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5359 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5360 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5361 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5362 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5363 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5364 | std::vector<SDValue> Ops; |
| 5365 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5366 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5367 | if (Ops.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5368 | cerr << "llvm: error: Invalid operand for inline asm constraint '" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5369 | << OpInfo.ConstraintCode << "'!\n"; |
| 5370 | exit(1); |
| 5371 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5372 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5373 | // Add information to the INLINEASM node to know about this input. |
| 5374 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5375 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5376 | TLI.getPointerTy())); |
| 5377 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5378 | break; |
| 5379 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5380 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5381 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5382 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5383 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5384 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5385 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5386 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5387 | TLI.getPointerTy())); |
| 5388 | AsmNodeOperands.push_back(InOperandVal); |
| 5389 | break; |
| 5390 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5391 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5392 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5393 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5394 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5395 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5396 | "Don't know how to handle indirect register inputs yet!"); |
| 5397 | |
| 5398 | // Copy the input into the appropriate registers. |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5399 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5400 | cerr << "llvm: error: Couldn't allocate output reg for constraint '" |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5401 | << OpInfo.ConstraintCode << "'!\n"; |
| 5402 | exit(1); |
| 5403 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5404 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5405 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5406 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5407 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5408 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5409 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5410 | break; |
| 5411 | } |
| 5412 | case InlineAsm::isClobber: { |
| 5413 | // Add the clobbered value to the operand list, so that the register |
| 5414 | // allocator is aware that the physreg got clobbered. |
| 5415 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5416 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5417 | false, 0, DAG,AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5418 | break; |
| 5419 | } |
| 5420 | } |
| 5421 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5422 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5423 | // Finish up input operands. |
| 5424 | AsmNodeOperands[0] = Chain; |
| 5425 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5426 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5427 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5428 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5429 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5430 | Flag = Chain.getValue(1); |
| 5431 | |
| 5432 | // If this asm returns a register value, copy the result from that register |
| 5433 | // and set it as the value of the call. |
| 5434 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5435 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5436 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5437 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5438 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5439 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
| 5440 | MVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5441 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5442 | // If any of the results of the inline asm is a vector, it may have the |
| 5443 | // wrong width/num elts. This can happen for register classes that can |
| 5444 | // contain multiple different value types. The preg or vreg allocated may |
| 5445 | // not have the same VT as was expected. Convert it to the right type |
| 5446 | // with bit_convert. |
| 5447 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5448 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5449 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5450 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5451 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5452 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5453 | // If a result value was tied to an input value, the computed result may |
| 5454 | // have a wider width than the expected result. Extract the relevant |
| 5455 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5456 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5457 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5458 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5459 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5460 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5461 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5462 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5463 | // Don't need to use this as a chain in this case. |
| 5464 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5465 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5466 | } |
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 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5469 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5470 | // Process indirect outputs, first output all of the flagged copies out of |
| 5471 | // physregs. |
| 5472 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5473 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5474 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5475 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
| 5476 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5477 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5478 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5479 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5480 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5481 | // Emit the non-flagged stores from the physregs. |
| 5482 | SmallVector<SDValue, 8> OutChains; |
| 5483 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5484 | OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5485 | StoresToEmit[i].first, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5486 | getValue(StoresToEmit[i].second), |
| 5487 | StoresToEmit[i].second, 0)); |
| 5488 | if (!OutChains.empty()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5489 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5490 | &OutChains[0], OutChains.size()); |
| 5491 | DAG.setRoot(Chain); |
| 5492 | } |
| 5493 | |
| 5494 | |
| 5495 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 5496 | SDValue Src = getValue(I.getOperand(0)); |
| 5497 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 5498 | // Scale up by the type size in the original i32 type width. Various |
| 5499 | // mid-level optimizers may make assumptions about demanded bits etc from the |
| 5500 | // i32-ness of the optimizer: we do not want to promote to i64 and then |
| 5501 | // multiply on 64-bit targets. |
| 5502 | // FIXME: Malloc inst should go away: PR715. |
| 5503 | uint64_t ElementSize = TD->getTypePaddedSize(I.getType()->getElementType()); |
| 5504 | if (ElementSize != 1) |
| 5505 | Src = DAG.getNode(ISD::MUL, getCurDebugLoc(), Src.getValueType(), |
| 5506 | Src, DAG.getConstant(ElementSize, Src.getValueType())); |
| 5507 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5508 | MVT IntPtr = TLI.getPointerTy(); |
| 5509 | |
| 5510 | if (IntPtr.bitsLT(Src.getValueType())) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5511 | Src = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), IntPtr, Src); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5512 | else if (IntPtr.bitsGT(Src.getValueType())) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5513 | Src = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), IntPtr, Src); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5514 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5515 | TargetLowering::ArgListTy Args; |
| 5516 | TargetLowering::ArgListEntry Entry; |
| 5517 | Entry.Node = Src; |
| 5518 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5519 | Args.push_back(Entry); |
| 5520 | |
| 5521 | std::pair<SDValue,SDValue> Result = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5522 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, false, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5523 | CallingConv::C, PerformTailCallOpt, |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5524 | DAG.getExternalSymbol("malloc", IntPtr), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5525 | Args, DAG, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5526 | setValue(&I, Result.first); // Pointers always fit in registers |
| 5527 | DAG.setRoot(Result.second); |
| 5528 | } |
| 5529 | |
| 5530 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 5531 | TargetLowering::ArgListTy Args; |
| 5532 | TargetLowering::ArgListEntry Entry; |
| 5533 | Entry.Node = getValue(I.getOperand(0)); |
| 5534 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5535 | Args.push_back(Entry); |
| 5536 | MVT IntPtr = TLI.getPointerTy(); |
| 5537 | std::pair<SDValue,SDValue> Result = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5538 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, false, |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5539 | CallingConv::C, PerformTailCallOpt, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5540 | DAG.getExternalSymbol("free", IntPtr), Args, DAG, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5541 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5542 | DAG.setRoot(Result.second); |
| 5543 | } |
| 5544 | |
| 5545 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5546 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5547 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5548 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5549 | DAG.getSrcValue(I.getOperand(1)))); |
| 5550 | } |
| 5551 | |
| 5552 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5553 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5554 | getRoot(), getValue(I.getOperand(0)), |
| 5555 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5556 | setValue(&I, V); |
| 5557 | DAG.setRoot(V.getValue(1)); |
| 5558 | } |
| 5559 | |
| 5560 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5561 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5562 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5563 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5564 | DAG.getSrcValue(I.getOperand(1)))); |
| 5565 | } |
| 5566 | |
| 5567 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5568 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5569 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5570 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5571 | getValue(I.getOperand(2)), |
| 5572 | DAG.getSrcValue(I.getOperand(1)), |
| 5573 | DAG.getSrcValue(I.getOperand(2)))); |
| 5574 | } |
| 5575 | |
| 5576 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 5577 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5578 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5579 | /// integrated into SDISel. |
| 5580 | void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5581 | SmallVectorImpl<SDValue> &ArgValues, |
| 5582 | DebugLoc dl) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5583 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 5584 | SmallVector<SDValue, 3+16> Ops; |
| 5585 | Ops.push_back(DAG.getRoot()); |
| 5586 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 5587 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 5588 | |
| 5589 | // Add one result value for each formal argument. |
| 5590 | SmallVector<MVT, 16> RetVals; |
| 5591 | unsigned j = 1; |
| 5592 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5593 | I != E; ++I, ++j) { |
| 5594 | SmallVector<MVT, 4> ValueVTs; |
| 5595 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5596 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5597 | Value != NumValues; ++Value) { |
| 5598 | MVT VT = ValueVTs[Value]; |
| 5599 | const Type *ArgTy = VT.getTypeForMVT(); |
| 5600 | ISD::ArgFlagsTy Flags; |
| 5601 | unsigned OriginalAlignment = |
| 5602 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5603 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5604 | if (F.paramHasAttr(j, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5605 | Flags.setZExt(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5606 | if (F.paramHasAttr(j, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5607 | Flags.setSExt(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5608 | if (F.paramHasAttr(j, Attribute::InReg)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5609 | Flags.setInReg(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5610 | if (F.paramHasAttr(j, Attribute::StructRet)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5611 | Flags.setSRet(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5612 | if (F.paramHasAttr(j, Attribute::ByVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5613 | Flags.setByVal(); |
| 5614 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5615 | const Type *ElementTy = Ty->getElementType(); |
| 5616 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5617 | unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5618 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5619 | // this info is not there but there are cases it cannot get right. |
| 5620 | if (F.getParamAlignment(j)) |
| 5621 | FrameAlign = F.getParamAlignment(j); |
| 5622 | Flags.setByValAlign(FrameAlign); |
| 5623 | Flags.setByValSize(FrameSize); |
| 5624 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5625 | if (F.paramHasAttr(j, Attribute::Nest)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5626 | Flags.setNest(); |
| 5627 | Flags.setOrigAlign(OriginalAlignment); |
| 5628 | |
| 5629 | MVT RegisterVT = getRegisterType(VT); |
| 5630 | unsigned NumRegs = getNumRegisters(VT); |
| 5631 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5632 | RetVals.push_back(RegisterVT); |
| 5633 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5634 | if (NumRegs > 1 && i == 0) |
| 5635 | MyFlags.setSplit(); |
| 5636 | // if it isn't first piece, alignment must be 1 |
| 5637 | else if (i > 0) |
| 5638 | MyFlags.setOrigAlign(1); |
| 5639 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5640 | } |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | RetVals.push_back(MVT::Other); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5645 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5646 | // Create the node. |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5647 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | DAG.getVTList(&RetVals[0], RetVals.size()), |
| 5649 | &Ops[0], Ops.size()).getNode(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5650 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5651 | // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but |
| 5652 | // allows exposing the loads that may be part of the argument access to the |
| 5653 | // first DAGCombiner pass. |
| 5654 | SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5655 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5656 | // The number of results should match up, except that the lowered one may have |
| 5657 | // an extra flag result. |
| 5658 | assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() || |
| 5659 | (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() && |
| 5660 | TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag)) |
| 5661 | && "Lowering produced unexpected number of results!"); |
| 5662 | |
| 5663 | // The FORMAL_ARGUMENTS node itself is likely no longer needed. |
| 5664 | if (Result != TmpRes.getNode() && Result->use_empty()) { |
| 5665 | HandleSDNode Dummy(DAG.getRoot()); |
| 5666 | DAG.RemoveDeadNode(Result); |
| 5667 | } |
| 5668 | |
| 5669 | Result = TmpRes.getNode(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5670 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5671 | unsigned NumArgRegs = Result->getNumValues() - 1; |
| 5672 | DAG.setRoot(SDValue(Result, NumArgRegs)); |
| 5673 | |
| 5674 | // Set up the return result vector. |
| 5675 | unsigned i = 0; |
| 5676 | unsigned Idx = 1; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5677 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5678 | ++I, ++Idx) { |
| 5679 | SmallVector<MVT, 4> ValueVTs; |
| 5680 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5681 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5682 | Value != NumValues; ++Value) { |
| 5683 | MVT VT = ValueVTs[Value]; |
| 5684 | MVT PartVT = getRegisterType(VT); |
| 5685 | |
| 5686 | unsigned NumParts = getNumRegisters(VT); |
| 5687 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5688 | for (unsigned j = 0; j != NumParts; ++j) |
| 5689 | Parts[j] = SDValue(Result, i++); |
| 5690 | |
| 5691 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5692 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5693 | AssertOp = ISD::AssertSext; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5694 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5695 | AssertOp = ISD::AssertZext; |
| 5696 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5697 | ArgValues.push_back(getCopyFromParts(DAG, dl, &Parts[0], NumParts, |
| 5698 | PartVT, VT, AssertOp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5699 | } |
| 5700 | } |
| 5701 | assert(i == NumArgRegs && "Argument register count mismatch!"); |
| 5702 | } |
| 5703 | |
| 5704 | |
| 5705 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 5706 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 5707 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 5708 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 5709 | std::pair<SDValue, SDValue> |
| 5710 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5711 | bool RetSExt, bool RetZExt, bool isVarArg, |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5712 | bool isInreg, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5713 | unsigned CallingConv, bool isTailCall, |
| 5714 | SDValue Callee, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5715 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5716 | assert((!isTailCall || PerformTailCallOpt) && |
| 5717 | "isTailCall set when tail-call optimizations are disabled!"); |
| 5718 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5719 | SmallVector<SDValue, 32> Ops; |
| 5720 | Ops.push_back(Chain); // Op#0 - Chain |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5721 | Ops.push_back(Callee); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5722 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5723 | // Handle all of the outgoing arguments. |
| 5724 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5725 | SmallVector<MVT, 4> ValueVTs; |
| 5726 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5727 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5728 | Value != NumValues; ++Value) { |
| 5729 | MVT VT = ValueVTs[Value]; |
| 5730 | const Type *ArgTy = VT.getTypeForMVT(); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5731 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5732 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5733 | ISD::ArgFlagsTy Flags; |
| 5734 | unsigned OriginalAlignment = |
| 5735 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5736 | |
| 5737 | if (Args[i].isZExt) |
| 5738 | Flags.setZExt(); |
| 5739 | if (Args[i].isSExt) |
| 5740 | Flags.setSExt(); |
| 5741 | if (Args[i].isInReg) |
| 5742 | Flags.setInReg(); |
| 5743 | if (Args[i].isSRet) |
| 5744 | Flags.setSRet(); |
| 5745 | if (Args[i].isByVal) { |
| 5746 | Flags.setByVal(); |
| 5747 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5748 | const Type *ElementTy = Ty->getElementType(); |
| 5749 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5750 | unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5751 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5752 | // info is not there but there are cases it cannot get right. |
| 5753 | if (Args[i].Alignment) |
| 5754 | FrameAlign = Args[i].Alignment; |
| 5755 | Flags.setByValAlign(FrameAlign); |
| 5756 | Flags.setByValSize(FrameSize); |
| 5757 | } |
| 5758 | if (Args[i].isNest) |
| 5759 | Flags.setNest(); |
| 5760 | Flags.setOrigAlign(OriginalAlignment); |
| 5761 | |
| 5762 | MVT PartVT = getRegisterType(VT); |
| 5763 | unsigned NumParts = getNumRegisters(VT); |
| 5764 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5765 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5766 | |
| 5767 | if (Args[i].isSExt) |
| 5768 | ExtendKind = ISD::SIGN_EXTEND; |
| 5769 | else if (Args[i].isZExt) |
| 5770 | ExtendKind = ISD::ZERO_EXTEND; |
| 5771 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5772 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5773 | |
| 5774 | for (unsigned i = 0; i != NumParts; ++i) { |
| 5775 | // if it isn't first piece, alignment must be 1 |
| 5776 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5777 | if (NumParts > 1 && i == 0) |
| 5778 | MyFlags.setSplit(); |
| 5779 | else if (i != 0) |
| 5780 | MyFlags.setOrigAlign(1); |
| 5781 | |
| 5782 | Ops.push_back(Parts[i]); |
| 5783 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5784 | } |
| 5785 | } |
| 5786 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5787 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5788 | // Figure out the result value types. We start by making a list of |
| 5789 | // the potentially illegal return value types. |
| 5790 | SmallVector<MVT, 4> LoweredRetTys; |
| 5791 | SmallVector<MVT, 4> RetTys; |
| 5792 | ComputeValueVTs(*this, RetTy, RetTys); |
| 5793 | |
| 5794 | // Then we translate that to a list of legal types. |
| 5795 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5796 | MVT VT = RetTys[I]; |
| 5797 | MVT RegisterVT = getRegisterType(VT); |
| 5798 | unsigned NumRegs = getNumRegisters(VT); |
| 5799 | for (unsigned i = 0; i != NumRegs; ++i) |
| 5800 | LoweredRetTys.push_back(RegisterVT); |
| 5801 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5802 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5803 | LoweredRetTys.push_back(MVT::Other); // Always has a chain. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5804 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5805 | // Create the CALL node. |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5806 | SDValue Res = DAG.getCall(CallingConv, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5807 | isVarArg, isTailCall, isInreg, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5808 | DAG.getVTList(&LoweredRetTys[0], |
| 5809 | LoweredRetTys.size()), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5810 | &Ops[0], Ops.size() |
| 5811 | ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | Chain = Res.getValue(LoweredRetTys.size() - 1); |
| 5813 | |
| 5814 | // Gather up the call result into a single value. |
Dan Gohman | b5cc34d | 2008-10-07 00:12:37 +0000 | [diff] [blame] | 5815 | if (RetTy != Type::VoidTy && !RetTys.empty()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5816 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5817 | |
| 5818 | if (RetSExt) |
| 5819 | AssertOp = ISD::AssertSext; |
| 5820 | else if (RetZExt) |
| 5821 | AssertOp = ISD::AssertZext; |
| 5822 | |
| 5823 | SmallVector<SDValue, 4> ReturnValues; |
| 5824 | unsigned RegNo = 0; |
| 5825 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5826 | MVT VT = RetTys[I]; |
| 5827 | MVT RegisterVT = getRegisterType(VT); |
| 5828 | unsigned NumRegs = getNumRegisters(VT); |
| 5829 | unsigned RegNoEnd = NumRegs + RegNo; |
| 5830 | SmallVector<SDValue, 4> Results; |
| 5831 | for (; RegNo != RegNoEnd; ++RegNo) |
| 5832 | Results.push_back(Res.getValue(RegNo)); |
| 5833 | SDValue ReturnValue = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5834 | getCopyFromParts(DAG, dl, &Results[0], NumRegs, RegisterVT, VT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5835 | AssertOp); |
| 5836 | ReturnValues.push_back(ReturnValue); |
| 5837 | } |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5838 | Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 5839 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5840 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | return std::make_pair(Res, Chain); |
| 5844 | } |
| 5845 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5846 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5847 | SmallVectorImpl<SDValue> &Results, |
| 5848 | SelectionDAG &DAG) { |
| 5849 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5850 | if (Res.getNode()) |
| 5851 | Results.push_back(Res); |
| 5852 | } |
| 5853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5854 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
| 5855 | assert(0 && "LowerOperation not implemented for this target!"); |
| 5856 | abort(); |
| 5857 | return SDValue(); |
| 5858 | } |
| 5859 | |
| 5860 | |
| 5861 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
| 5862 | SDValue Op = getValue(V); |
| 5863 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5864 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5865 | "Copy from a reg to the same reg!"); |
| 5866 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5867 | |
| 5868 | RegsForValue RFV(TLI, Reg, V->getType()); |
| 5869 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5870 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5871 | PendingExports.push_back(Chain); |
| 5872 | } |
| 5873 | |
| 5874 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5875 | |
| 5876 | void SelectionDAGISel:: |
| 5877 | LowerArguments(BasicBlock *LLVMBB) { |
| 5878 | // If this is the entry block, emit arguments. |
| 5879 | Function &F = *LLVMBB->getParent(); |
| 5880 | SDValue OldRoot = SDL->DAG.getRoot(); |
| 5881 | SmallVector<SDValue, 16> Args; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5882 | TLI.LowerArguments(F, SDL->DAG, Args, SDL->getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5883 | |
| 5884 | unsigned a = 0; |
| 5885 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 5886 | AI != E; ++AI) { |
| 5887 | SmallVector<MVT, 4> ValueVTs; |
| 5888 | ComputeValueVTs(TLI, AI->getType(), ValueVTs); |
| 5889 | unsigned NumValues = ValueVTs.size(); |
| 5890 | if (!AI->use_empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5891 | SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues, |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5892 | SDL->getCurDebugLoc())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5893 | // If this argument is live outside of the entry block, insert a copy from |
| 5894 | // whereever we got it to the vreg that other BB's will reference it as. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 5895 | SDL->CopyToExportRegsIfNeeded(AI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5896 | } |
| 5897 | a += NumValues; |
| 5898 | } |
| 5899 | |
| 5900 | // Finally, if the target has anything special to do, allow it to do so. |
| 5901 | // FIXME: this should insert code into the DAG! |
| 5902 | EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction()); |
| 5903 | } |
| 5904 | |
| 5905 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 5906 | /// ensure constants are generated when needed. Remember the virtual registers |
| 5907 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 5908 | /// directly add them, because expansion might result in multiple MBB's for one |
| 5909 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 5910 | /// the end. |
| 5911 | /// |
| 5912 | void |
| 5913 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 5914 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 5915 | |
| 5916 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5917 | |
| 5918 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5919 | // from this block. |
| 5920 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5921 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5922 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5923 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5924 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5925 | // If this terminator has multiple identical successors (common for |
| 5926 | // switches), only handle each succ once. |
| 5927 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5928 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5929 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5930 | PHINode *PN; |
| 5931 | |
| 5932 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5933 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5934 | // emitted yet. |
| 5935 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5936 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5937 | // Ignore dead phi's. |
| 5938 | if (PN->use_empty()) continue; |
| 5939 | |
| 5940 | unsigned Reg; |
| 5941 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5942 | |
| 5943 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 5944 | unsigned &RegOut = SDL->ConstantsOut[C]; |
| 5945 | if (RegOut == 0) { |
| 5946 | RegOut = FuncInfo->CreateRegForValue(C); |
| 5947 | SDL->CopyValueToVirtualRegister(C, RegOut); |
| 5948 | } |
| 5949 | Reg = RegOut; |
| 5950 | } else { |
| 5951 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 5952 | if (Reg == 0) { |
| 5953 | assert(isa<AllocaInst>(PHIOp) && |
| 5954 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 5955 | "Didn't codegen value into a register!??"); |
| 5956 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
| 5957 | SDL->CopyValueToVirtualRegister(PHIOp, Reg); |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | // Remember that this register needs to added to the machine PHI node as |
| 5962 | // the input for this MBB. |
| 5963 | SmallVector<MVT, 4> ValueVTs; |
| 5964 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 5965 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 5966 | MVT VT = ValueVTs[vti]; |
| 5967 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
| 5968 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
| 5969 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 5970 | Reg += NumRegisters; |
| 5971 | } |
| 5972 | } |
| 5973 | } |
| 5974 | SDL->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5977 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 5978 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 5979 | /// creating SelectionDAG nodes. |
| 5980 | /// |
| 5981 | bool |
| 5982 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 5983 | FastISel *F) { |
| 5984 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5985 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5986 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5987 | unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size(); |
| 5988 | |
| 5989 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5990 | // from this block. |
| 5991 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5992 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5993 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5994 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5995 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5996 | // If this terminator has multiple identical successors (common for |
| 5997 | // switches), only handle each succ once. |
| 5998 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5999 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6000 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6001 | PHINode *PN; |
| 6002 | |
| 6003 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6004 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6005 | // emitted yet. |
| 6006 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6007 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6008 | // Ignore dead phi's. |
| 6009 | if (PN->use_empty()) continue; |
| 6010 | |
| 6011 | // Only handle legal types. Two interesting things to note here. First, |
| 6012 | // by bailing out early, we may leave behind some dead instructions, |
| 6013 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6014 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6015 | // use CreateRegForValue to create registers, so it always creates |
| 6016 | // exactly one register for each non-void instruction. |
| 6017 | MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 6018 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6019 | // Promote MVT::i1. |
| 6020 | if (VT == MVT::i1) |
| 6021 | VT = TLI.getTypeToTransformTo(VT); |
| 6022 | else { |
| 6023 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6024 | return false; |
| 6025 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6029 | |
| 6030 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6031 | if (Reg == 0) { |
| 6032 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6033 | return false; |
| 6034 | } |
| 6035 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 6036 | } |
| 6037 | } |
| 6038 | |
| 6039 | return true; |
| 6040 | } |