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" |
| 48 | #include "llvm/Target/TargetMachine.h" |
| 49 | #include "llvm/Target/TargetOptions.h" |
| 50 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
| 53 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 54 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include <algorithm> |
| 56 | using namespace llvm; |
| 57 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 58 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 59 | /// some float libcalls (6, 8 or 12 bits). |
| 60 | static unsigned LimitFloatPrecision; |
| 61 | |
| 62 | static cl::opt<unsigned, true> |
| 63 | LimitFPPrecision("limit-float-precision", |
| 64 | cl::desc("Generate low-precision inline sequences " |
| 65 | "for some float libcalls"), |
| 66 | cl::location(LimitFloatPrecision), |
| 67 | cl::init(0)); |
| 68 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 69 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 70 | /// of insertvalue or extractvalue indices that identify a member, return |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 71 | /// the linearized index of the start of the member. |
| 72 | /// |
| 73 | static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, |
| 74 | const unsigned *Indices, |
| 75 | const unsigned *IndicesEnd, |
| 76 | unsigned CurIndex = 0) { |
| 77 | // Base case: We're done. |
| 78 | if (Indices && Indices == IndicesEnd) |
| 79 | return CurIndex; |
| 80 | |
| 81 | // Given a struct type, recursively traverse the elements. |
| 82 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 83 | for (StructType::element_iterator EB = STy->element_begin(), |
| 84 | EI = EB, |
| 85 | EE = STy->element_end(); |
| 86 | EI != EE; ++EI) { |
| 87 | if (Indices && *Indices == unsigned(EI - EB)) |
| 88 | return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); |
| 89 | CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); |
| 90 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 91 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 92 | } |
| 93 | // Given an array type, recursively traverse the elements. |
| 94 | else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 95 | const Type *EltTy = ATy->getElementType(); |
| 96 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 97 | if (Indices && *Indices == i) |
| 98 | return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); |
| 99 | CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); |
| 100 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 101 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 102 | } |
| 103 | // We haven't found the type we're looking for, so keep searching. |
| 104 | return CurIndex + 1; |
| 105 | } |
| 106 | |
| 107 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 108 | /// MVTs that represent all the individual underlying |
| 109 | /// non-aggregate types that comprise it. |
| 110 | /// |
| 111 | /// If Offsets is non-null, it points to a vector to be filled in |
| 112 | /// with the in-memory offsets of each of the individual values. |
| 113 | /// |
| 114 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
| 115 | SmallVectorImpl<MVT> &ValueVTs, |
| 116 | SmallVectorImpl<uint64_t> *Offsets = 0, |
| 117 | uint64_t StartingOffset = 0) { |
| 118 | // Given a struct type, recursively traverse the elements. |
| 119 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 120 | const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); |
| 121 | for (StructType::element_iterator EB = STy->element_begin(), |
| 122 | EI = EB, |
| 123 | EE = STy->element_end(); |
| 124 | EI != EE; ++EI) |
| 125 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 126 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 127 | return; |
| 128 | } |
| 129 | // Given an array type, recursively traverse the elements. |
| 130 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 131 | const Type *EltTy = ATy->getElementType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 132 | uint64_t EltSize = TLI.getTargetData()->getTypePaddedSize(EltTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 133 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 134 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 135 | StartingOffset + i * EltSize); |
| 136 | return; |
| 137 | } |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 138 | // Interpret void as zero return values. |
| 139 | if (Ty == Type::VoidTy) |
| 140 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 141 | // Base case: we can get an MVT for this LLVM IR type. |
| 142 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 143 | if (Offsets) |
| 144 | Offsets->push_back(StartingOffset); |
| 145 | } |
| 146 | |
Dan Gohman | 2a7c671 | 2008-09-03 23:18:39 +0000 | [diff] [blame] | 147 | namespace llvm { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 148 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 149 | /// that a particular set of values is assigned, and the type information about |
| 150 | /// the value. The most common situation is to represent one value at a time, |
| 151 | /// but struct or array values are handled element-wise as multiple values. |
| 152 | /// The splitting of aggregates is performed recursively, so that we never |
| 153 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 154 | /// have legal types, so each value may require one or more registers of some |
| 155 | /// legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | /// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 157 | struct VISIBILITY_HIDDEN RegsForValue { |
| 158 | /// TLI - The TargetLowering object. |
| 159 | /// |
| 160 | const TargetLowering *TLI; |
| 161 | |
| 162 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 163 | /// may need be promoted or synthesized from one or more registers. |
| 164 | /// |
| 165 | SmallVector<MVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 166 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 167 | /// RegVTs - The value types of the registers. This is the same size as |
| 168 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 169 | /// register or registers are. (Individual values are never synthesized |
| 170 | /// from more than one type of register.) |
| 171 | /// |
| 172 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 173 | /// getRegisterType member function, however when with physical registers |
| 174 | /// it is necessary to have a separate record of the types. |
| 175 | /// |
| 176 | SmallVector<MVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 177 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | /// Regs - This list holds the registers assigned to the values. |
| 179 | /// Each legal or promoted value requires one register, and each |
| 180 | /// expanded value requires multiple registers. |
| 181 | /// |
| 182 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 183 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 184 | RegsForValue() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 185 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 186 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 187 | const SmallVector<unsigned, 4> ®s, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 188 | MVT regvt, MVT valuevt) |
| 189 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 190 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 191 | const SmallVector<unsigned, 4> ®s, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 192 | const SmallVector<MVT, 4> ®vts, |
| 193 | const SmallVector<MVT, 4> &valuevts) |
| 194 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
| 195 | RegsForValue(const TargetLowering &tli, |
| 196 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 197 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 198 | |
| 199 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 200 | MVT ValueVT = ValueVTs[Value]; |
| 201 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 202 | MVT RegisterVT = TLI->getRegisterType(ValueVT); |
| 203 | for (unsigned i = 0; i != NumRegs; ++i) |
| 204 | Regs.push_back(Reg + i); |
| 205 | RegVTs.push_back(RegisterVT); |
| 206 | Reg += NumRegs; |
| 207 | } |
| 208 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 209 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 210 | /// append - Add the specified values to this one. |
| 211 | void append(const RegsForValue &RHS) { |
| 212 | TLI = RHS.TLI; |
| 213 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 214 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 215 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 216 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 217 | |
| 218 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 219 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 220 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 221 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 222 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 223 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 224 | SDValue &Chain, SDValue *Flag) const; |
| 225 | |
| 226 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 227 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 228 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 229 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 230 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 231 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 232 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 233 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 234 | /// operand list. This adds the code marker, matching input operand index |
| 235 | /// (if applicable), and includes the number of values added into it. |
| 236 | void AddInlineAsmOperands(unsigned Code, |
| 237 | bool HasMatching, unsigned MatchingIdx, |
| 238 | SelectionDAG &DAG, std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 239 | }; |
| 240 | } |
| 241 | |
| 242 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 243 | /// 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] | 244 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
| 245 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 246 | if (isa<PHINode>(I)) return true; |
| 247 | BasicBlock *BB = I->getParent(); |
| 248 | 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] | 249 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 250 | return true; |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 255 | /// entry block, return true. This includes arguments used by switches, since |
| 256 | /// the switch may expand into multiple basic blocks. |
| 257 | static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { |
| 258 | // With FastISel active, we may be splitting blocks, so force creation |
| 259 | // of virtual registers for all non-dead arguments. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 260 | // Don't force virtual registers for byval arguments though, because |
| 261 | // fast-isel can't handle those in all cases. |
| 262 | if (EnableFastISel && !A->hasByValAttr()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 263 | return A->use_empty(); |
| 264 | |
| 265 | BasicBlock *Entry = A->getParent()->begin(); |
| 266 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 267 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 268 | return false; // Use not in entry block. |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) |
| 273 | : TLI(tli) { |
| 274 | } |
| 275 | |
| 276 | void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 277 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 278 | bool EnableFastISel) { |
| 279 | Fn = &fn; |
| 280 | MF = &mf; |
| 281 | RegInfo = &MF->getRegInfo(); |
| 282 | |
| 283 | // Create a vreg for each argument register that is not dead and is used |
| 284 | // outside of the entry block for the function. |
| 285 | for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
| 286 | AI != E; ++AI) |
| 287 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 288 | InitializeRegForValue(AI); |
| 289 | |
| 290 | // Initialize the mapping of values to registers. This is only set up for |
| 291 | // instruction values that are used outside of the block that defines |
| 292 | // them. |
| 293 | Function::iterator BB = Fn->begin(), EB = Fn->end(); |
| 294 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 295 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 296 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 297 | const Type *Ty = AI->getAllocatedType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 298 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 299 | unsigned Align = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 300 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 301 | AI->getAlignment()); |
| 302 | |
| 303 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 304 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 305 | StaticAllocaMap[AI] = |
| 306 | MF->getFrameInfo()->CreateStackObject(TySize, Align); |
| 307 | } |
| 308 | |
| 309 | for (; BB != EB; ++BB) |
| 310 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 311 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 312 | if (!isa<AllocaInst>(I) || |
| 313 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 314 | InitializeRegForValue(I); |
| 315 | |
| 316 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 317 | // also creates the initial PHI MachineInstrs, though none of the input |
| 318 | // operands are populated. |
| 319 | for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { |
| 320 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 321 | MBBMap[BB] = MBB; |
| 322 | MF->push_back(MBB); |
| 323 | |
| 324 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 325 | // appropriate. |
| 326 | PHINode *PN; |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 327 | DebugLoc DL; |
| 328 | for (BasicBlock::iterator |
| 329 | I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 330 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 331 | if (Function *F = CI->getCalledFunction()) { |
| 332 | switch (F->getIntrinsicID()) { |
| 333 | default: break; |
| 334 | case Intrinsic::dbg_stoppoint: { |
| 335 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 336 | DbgStopPointInst *SPI = cast<DbgStopPointInst>(I); |
| 337 | |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 338 | if (DW && DW->ValidDebugInfo(SPI->getContext(), false)) { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 339 | DICompileUnit CU(cast<GlobalVariable>(SPI->getContext())); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 340 | std::string Dir, FN; |
| 341 | unsigned SrcFile = DW->getOrCreateSourceID(CU.getDirectory(Dir), |
| 342 | CU.getFilename(FN)); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 343 | unsigned idx = MF->getOrCreateDebugLocID(SrcFile, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 344 | SPI->getLine(), |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 345 | SPI->getColumn()); |
| 346 | DL = DebugLoc::get(idx); |
| 347 | } |
| 348 | |
| 349 | break; |
| 350 | } |
| 351 | case Intrinsic::dbg_func_start: { |
| 352 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 353 | if (DW) { |
| 354 | DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I); |
| 355 | Value *SP = FSI->getSubprogram(); |
| 356 | |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 357 | if (DW->ValidDebugInfo(SP, false)) { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 358 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 359 | DICompileUnit CU(Subprogram.getCompileUnit()); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 360 | std::string Dir, FN; |
| 361 | unsigned SrcFile = DW->getOrCreateSourceID(CU.getDirectory(Dir), |
| 362 | CU.getFilename(FN)); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 363 | unsigned Line = Subprogram.getLineNumber(); |
| 364 | DL = DebugLoc::get(MF->getOrCreateDebugLocID(SrcFile, Line, 0)); |
| 365 | } |
| 366 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 367 | |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | PN = dyn_cast<PHINode>(I); |
| 375 | if (!PN || PN->use_empty()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 376 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 377 | unsigned PHIReg = ValueMap[PN]; |
| 378 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 379 | |
| 380 | SmallVector<MVT, 4> ValueVTs; |
| 381 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 382 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 383 | MVT VT = ValueVTs[vti]; |
| 384 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 385 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 386 | for (unsigned i = 0; i != NumRegisters; ++i) |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 387 | BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 388 | PHIReg += NumRegisters; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | unsigned FunctionLoweringInfo::MakeReg(MVT VT) { |
| 395 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 396 | } |
| 397 | |
| 398 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 399 | /// the correctly promoted or expanded types. Assign these registers |
| 400 | /// consecutive vreg numbers and return the first assigned number. |
| 401 | /// |
| 402 | /// In the case that the given value has struct or array type, this function |
| 403 | /// will assign registers for each member or element. |
| 404 | /// |
| 405 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 406 | SmallVector<MVT, 4> ValueVTs; |
| 407 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
| 408 | |
| 409 | unsigned FirstReg = 0; |
| 410 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 411 | MVT ValueVT = ValueVTs[Value]; |
| 412 | MVT RegisterVT = TLI.getRegisterType(ValueVT); |
| 413 | |
| 414 | unsigned NumRegs = TLI.getNumRegisters(ValueVT); |
| 415 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 416 | unsigned R = MakeReg(RegisterVT); |
| 417 | if (!FirstReg) FirstReg = R; |
| 418 | } |
| 419 | } |
| 420 | return FirstReg; |
| 421 | } |
| 422 | |
| 423 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 424 | /// combined into the value they represent. If the parts combine to a type |
| 425 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 426 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 427 | /// (ISD::AssertSext). |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 428 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
| 429 | const SDValue *Parts, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 430 | unsigned NumParts, MVT PartVT, MVT ValueVT, |
| 431 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 432 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 433 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 434 | SDValue Val = Parts[0]; |
| 435 | |
| 436 | if (NumParts > 1) { |
| 437 | // Assemble the value from multiple parts. |
| 438 | if (!ValueVT.isVector()) { |
| 439 | unsigned PartBits = PartVT.getSizeInBits(); |
| 440 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 441 | |
| 442 | // Assemble the power of 2 part. |
| 443 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 444 | 1 << Log2_32(NumParts) : NumParts; |
| 445 | unsigned RoundBits = PartBits * RoundParts; |
| 446 | MVT RoundVT = RoundBits == ValueBits ? |
| 447 | ValueVT : MVT::getIntegerVT(RoundBits); |
| 448 | SDValue Lo, Hi; |
| 449 | |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 450 | MVT HalfVT = ValueVT.isInteger() ? |
| 451 | MVT::getIntegerVT(RoundBits/2) : |
| 452 | MVT::getFloatingPointVT(RoundBits/2); |
| 453 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 454 | if (RoundParts > 2) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 455 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT); |
| 456 | Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 457 | PartVT, HalfVT); |
| 458 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 459 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 460 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 461 | } |
| 462 | if (TLI.isBigEndian()) |
| 463 | std::swap(Lo, Hi); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 464 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 465 | |
| 466 | if (RoundParts < NumParts) { |
| 467 | // Assemble the trailing non-power-of-2 part. |
| 468 | unsigned OddParts = NumParts - RoundParts; |
| 469 | MVT OddVT = MVT::getIntegerVT(OddParts * PartBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 470 | Hi = getCopyFromParts(DAG, dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 471 | Parts+RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 472 | |
| 473 | // Combine the round and odd parts. |
| 474 | Lo = Val; |
| 475 | if (TLI.isBigEndian()) |
| 476 | std::swap(Lo, Hi); |
| 477 | MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 478 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 479 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 480 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 481 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 482 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 483 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 484 | } |
| 485 | } else { |
| 486 | // Handle a multi-element vector. |
| 487 | MVT IntermediateVT, RegisterVT; |
| 488 | unsigned NumIntermediates; |
| 489 | unsigned NumRegs = |
| 490 | TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 491 | RegisterVT); |
| 492 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 493 | NumParts = NumRegs; // Silence a compiler warning. |
| 494 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 495 | assert(RegisterVT == Parts[0].getValueType() && |
| 496 | "Part type doesn't match part!"); |
| 497 | |
| 498 | // Assemble the parts into intermediate operands. |
| 499 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 500 | if (NumIntermediates == NumParts) { |
| 501 | // If the register was not expanded, truncate or copy the value, |
| 502 | // as appropriate. |
| 503 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 504 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 505 | PartVT, IntermediateVT); |
| 506 | } else if (NumParts > 0) { |
| 507 | // If the intermediate type was expanded, build the intermediate operands |
| 508 | // from the parts. |
| 509 | assert(NumParts % NumIntermediates == 0 && |
| 510 | "Must expand into a divisible number of parts!"); |
| 511 | unsigned Factor = NumParts / NumIntermediates; |
| 512 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 513 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 514 | PartVT, IntermediateVT); |
| 515 | } |
| 516 | |
| 517 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 518 | // operands. |
| 519 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 520 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 521 | ValueVT, &Ops[0], NumIntermediates); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 526 | PartVT = Val.getValueType(); |
| 527 | |
| 528 | if (PartVT == ValueVT) |
| 529 | return Val; |
| 530 | |
| 531 | if (PartVT.isVector()) { |
| 532 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 533 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | if (ValueVT.isVector()) { |
| 537 | assert(ValueVT.getVectorElementType() == PartVT && |
| 538 | ValueVT.getVectorNumElements() == 1 && |
| 539 | "Only trivial scalar-to-vector conversions should get here!"); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 540 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | if (PartVT.isInteger() && |
| 544 | ValueVT.isInteger()) { |
| 545 | if (ValueVT.bitsLT(PartVT)) { |
| 546 | // For a truncate, see if we have any information to |
| 547 | // indicate whether the truncated bits will always be |
| 548 | // zero or sign-extension. |
| 549 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 550 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 551 | DAG.getValueType(ValueVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 552 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 553 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 554 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 559 | if (ValueVT.bitsLT(Val.getValueType())) |
| 560 | // FP_ROUND's are always exact here. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 561 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 562 | DAG.getIntPtrConstant(1)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 563 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 567 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 568 | |
| 569 | assert(0 && "Unknown mismatch!"); |
| 570 | return SDValue(); |
| 571 | } |
| 572 | |
| 573 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 574 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 575 | /// 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] | 576 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val, |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 577 | SDValue *Parts, unsigned NumParts, MVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 578 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 579 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 580 | MVT PtrVT = TLI.getPointerTy(); |
| 581 | MVT ValueVT = Val.getValueType(); |
| 582 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 583 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 584 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 585 | |
| 586 | if (!NumParts) |
| 587 | return; |
| 588 | |
| 589 | if (!ValueVT.isVector()) { |
| 590 | if (PartVT == ValueVT) { |
| 591 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 592 | Parts[0] = Val; |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 597 | // If the parts cover more bits than the value has, promote the value. |
| 598 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 599 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 600 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 601 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 602 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 603 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 604 | } else { |
| 605 | assert(0 && "Unknown mismatch!"); |
| 606 | } |
| 607 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 608 | // Different types of the same size. |
| 609 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 610 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 611 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 612 | // If the parts cover less bits than value has, truncate the value. |
| 613 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 614 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 615 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 616 | } else { |
| 617 | assert(0 && "Unknown mismatch!"); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // The value may have changed - recompute ValueVT. |
| 622 | ValueVT = Val.getValueType(); |
| 623 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 624 | "Failed to tile the value with PartVT!"); |
| 625 | |
| 626 | if (NumParts == 1) { |
| 627 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 628 | Parts[0] = Val; |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | // Expand the value into multiple parts. |
| 633 | if (NumParts & (NumParts - 1)) { |
| 634 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 635 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 636 | "Do not know what to expand to!"); |
| 637 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 638 | unsigned RoundBits = RoundParts * PartBits; |
| 639 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 640 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 641 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 642 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 643 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 644 | if (TLI.isBigEndian()) |
| 645 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 646 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 647 | NumParts = RoundParts; |
| 648 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 649 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 653 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 654 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 655 | MVT::getIntegerVT(ValueVT.getSizeInBits()), |
| 656 | Val); |
| 657 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 658 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 659 | unsigned ThisBits = StepSize * PartBits / 2; |
| 660 | MVT ThisVT = MVT::getIntegerVT (ThisBits); |
| 661 | SDValue &Part0 = Parts[i]; |
| 662 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 663 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 664 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 665 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 666 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 667 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 668 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 669 | DAG.getConstant(0, PtrVT)); |
| 670 | |
| 671 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 672 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 673 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 674 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 675 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 681 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 682 | |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | // Vector ValueVT. |
| 687 | if (NumParts == 1) { |
| 688 | if (PartVT != ValueVT) { |
| 689 | if (PartVT.isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 690 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 691 | } else { |
| 692 | assert(ValueVT.getVectorElementType() == PartVT && |
| 693 | ValueVT.getVectorNumElements() == 1 && |
| 694 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 695 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 696 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 697 | DAG.getConstant(0, PtrVT)); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | Parts[0] = Val; |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // Handle a multi-element vector. |
| 706 | MVT IntermediateVT, RegisterVT; |
| 707 | unsigned NumIntermediates; |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 708 | unsigned NumRegs = TLI |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 709 | .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 710 | RegisterVT); |
| 711 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 712 | |
| 713 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 714 | NumParts = NumRegs; // Silence a compiler warning. |
| 715 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 716 | |
| 717 | // Split the vector into intermediate operands. |
| 718 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 719 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 720 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 721 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 722 | IntermediateVT, Val, |
| 723 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 724 | PtrVT)); |
| 725 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 726 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 727 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 728 | DAG.getConstant(i, PtrVT)); |
| 729 | |
| 730 | // Split the intermediate operands into legal parts. |
| 731 | if (NumParts == NumIntermediates) { |
| 732 | // If the register was not expanded, promote or copy the value, |
| 733 | // as appropriate. |
| 734 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 735 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 736 | } else if (NumParts > 0) { |
| 737 | // If the intermediate type was expanded, split each the value into |
| 738 | // legal parts. |
| 739 | assert(NumParts % NumIntermediates == 0 && |
| 740 | "Must expand into a divisible number of parts!"); |
| 741 | unsigned Factor = NumParts / NumIntermediates; |
| 742 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 743 | getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | |
| 748 | void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 749 | AA = &aa; |
| 750 | GFI = gfi; |
| 751 | TD = DAG.getTarget().getTargetData(); |
| 752 | } |
| 753 | |
| 754 | /// clear - Clear out the curret SelectionDAG and the associated |
| 755 | /// state and prepare this SelectionDAGLowering object to be used |
| 756 | /// for a new block. This doesn't clear out information about |
| 757 | /// additional blocks that are needed to complete switch lowering |
| 758 | /// or PHI node updating; that information is cleared out as it is |
| 759 | /// consumed. |
| 760 | void SelectionDAGLowering::clear() { |
| 761 | NodeMap.clear(); |
| 762 | PendingLoads.clear(); |
| 763 | PendingExports.clear(); |
| 764 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 765 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 769 | /// flushing any PendingLoad items. This must be done before emitting |
| 770 | /// a store or any other node that may need to be ordered after any |
| 771 | /// prior load instructions. |
| 772 | /// |
| 773 | SDValue SelectionDAGLowering::getRoot() { |
| 774 | if (PendingLoads.empty()) |
| 775 | return DAG.getRoot(); |
| 776 | |
| 777 | if (PendingLoads.size() == 1) { |
| 778 | SDValue Root = PendingLoads[0]; |
| 779 | DAG.setRoot(Root); |
| 780 | PendingLoads.clear(); |
| 781 | return Root; |
| 782 | } |
| 783 | |
| 784 | // Otherwise, we have to make a token factor node. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 785 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 786 | &PendingLoads[0], PendingLoads.size()); |
| 787 | PendingLoads.clear(); |
| 788 | DAG.setRoot(Root); |
| 789 | return Root; |
| 790 | } |
| 791 | |
| 792 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 793 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 794 | /// to do this before emitting a terminator instruction. |
| 795 | /// |
| 796 | SDValue SelectionDAGLowering::getControlRoot() { |
| 797 | SDValue Root = DAG.getRoot(); |
| 798 | |
| 799 | if (PendingExports.empty()) |
| 800 | return Root; |
| 801 | |
| 802 | // Turn all of the CopyToReg chains into one factored node. |
| 803 | if (Root.getOpcode() != ISD::EntryToken) { |
| 804 | unsigned i = 0, e = PendingExports.size(); |
| 805 | for (; i != e; ++i) { |
| 806 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 807 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 808 | break; // Don't add the root if we already indirectly depend on it. |
| 809 | } |
| 810 | |
| 811 | if (i == e) |
| 812 | PendingExports.push_back(Root); |
| 813 | } |
| 814 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 815 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 816 | &PendingExports[0], |
| 817 | PendingExports.size()); |
| 818 | PendingExports.clear(); |
| 819 | DAG.setRoot(Root); |
| 820 | return Root; |
| 821 | } |
| 822 | |
| 823 | void SelectionDAGLowering::visit(Instruction &I) { |
| 824 | visit(I.getOpcode(), I); |
| 825 | } |
| 826 | |
| 827 | void SelectionDAGLowering::visit(unsigned Opcode, User &I) { |
| 828 | // Note: this doesn't use InstVisitor, because it has to work with |
| 829 | // ConstantExpr's in addition to instructions. |
| 830 | switch (Opcode) { |
| 831 | default: assert(0 && "Unknown instruction type encountered!"); |
| 832 | abort(); |
| 833 | // Build the switch statement using the Instruction.def file. |
| 834 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 835 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 836 | #include "llvm/Instruction.def" |
| 837 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 838 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 839 | |
| 840 | void SelectionDAGLowering::visitAdd(User &I) { |
| 841 | if (I.getType()->isFPOrFPVector()) |
| 842 | visitBinary(I, ISD::FADD); |
| 843 | else |
| 844 | visitBinary(I, ISD::ADD); |
| 845 | } |
| 846 | |
| 847 | void SelectionDAGLowering::visitMul(User &I) { |
| 848 | if (I.getType()->isFPOrFPVector()) |
| 849 | visitBinary(I, ISD::FMUL); |
| 850 | else |
| 851 | visitBinary(I, ISD::MUL); |
| 852 | } |
| 853 | |
| 854 | SDValue SelectionDAGLowering::getValue(const Value *V) { |
| 855 | SDValue &N = NodeMap[V]; |
| 856 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 857 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 858 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 859 | MVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 860 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 861 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 862 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 863 | |
| 864 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 865 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 866 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 867 | if (isa<ConstantPointerNull>(C)) |
| 868 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 869 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 870 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 871 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 872 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 873 | if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) && |
| 874 | !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 875 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 876 | |
| 877 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 878 | visit(CE->getOpcode(), *CE); |
| 879 | SDValue N1 = NodeMap[V]; |
| 880 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 881 | return N1; |
| 882 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 883 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 884 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 885 | SmallVector<SDValue, 4> Constants; |
| 886 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 887 | OI != OE; ++OI) { |
| 888 | SDNode *Val = getValue(*OI).getNode(); |
| 889 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 890 | Constants.push_back(SDValue(Val, i)); |
| 891 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 892 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 893 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 897 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 898 | "Unknown struct or array constant!"); |
| 899 | |
| 900 | SmallVector<MVT, 4> ValueVTs; |
| 901 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 902 | unsigned NumElts = ValueVTs.size(); |
| 903 | if (NumElts == 0) |
| 904 | return SDValue(); // empty struct |
| 905 | SmallVector<SDValue, 4> Constants(NumElts); |
| 906 | for (unsigned i = 0; i != NumElts; ++i) { |
| 907 | MVT EltVT = ValueVTs[i]; |
| 908 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 909 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 910 | else if (EltVT.isFloatingPoint()) |
| 911 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 912 | else |
| 913 | Constants[i] = DAG.getConstant(0, EltVT); |
| 914 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 915 | return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 919 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 920 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 921 | // Now that we know the number and type of the elements, get that number of |
| 922 | // elements into the Ops array based on what kind of constant it is. |
| 923 | SmallVector<SDValue, 16> Ops; |
| 924 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 925 | for (unsigned i = 0; i != NumElements; ++i) |
| 926 | Ops.push_back(getValue(CP->getOperand(i))); |
| 927 | } else { |
| 928 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 929 | "Unknown vector constant!"); |
| 930 | MVT EltVT = TLI.getValueType(VecTy->getElementType()); |
| 931 | |
| 932 | SDValue Op; |
| 933 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 934 | Op = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 935 | else if (EltVT.isFloatingPoint()) |
| 936 | Op = DAG.getConstantFP(0, EltVT); |
| 937 | else |
| 938 | Op = DAG.getConstant(0, EltVT); |
| 939 | Ops.assign(NumElements, Op); |
| 940 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 941 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 942 | // Create a BUILD_VECTOR node. |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 943 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 944 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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 | // If this is a static alloca, generate it as the frameindex instead of |
| 948 | // computation. |
| 949 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 950 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 951 | FuncInfo.StaticAllocaMap.find(AI); |
| 952 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 953 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 954 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 955 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 956 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 957 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 958 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 959 | RegsForValue RFV(TLI, InReg, V->getType()); |
| 960 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 961 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | |
| 965 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 966 | if (I.getNumOperands() == 0) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 967 | DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 968 | MVT::Other, getControlRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 969 | return; |
| 970 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 971 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 972 | SmallVector<SDValue, 8> NewValues; |
| 973 | NewValues.push_back(getControlRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 974 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 975 | SmallVector<MVT, 4> ValueVTs; |
| 976 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 977 | unsigned NumValues = ValueVTs.size(); |
| 978 | if (NumValues == 0) continue; |
| 979 | |
| 980 | SDValue RetOp = getValue(I.getOperand(i)); |
| 981 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 982 | MVT VT = ValueVTs[j]; |
| 983 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 984 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 985 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 986 | const Function *F = I.getParent()->getParent(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 987 | if (F->paramHasAttr(0, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 988 | ExtendKind = ISD::SIGN_EXTEND; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 989 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 990 | ExtendKind = ISD::ZERO_EXTEND; |
| 991 | |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 992 | // FIXME: C calling convention requires the return type to be promoted to |
| 993 | // at least 32-bit. But this is not necessary for non-C calling |
| 994 | // conventions. The frontend should mark functions whose return values |
| 995 | // require promoting with signext or zeroext attributes. |
| 996 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 997 | MVT MinVT = TLI.getRegisterType(MVT::i32); |
| 998 | if (VT.bitsLT(MinVT)) |
| 999 | VT = MinVT; |
| 1000 | } |
| 1001 | |
| 1002 | unsigned NumParts = TLI.getNumRegisters(VT); |
| 1003 | MVT PartVT = TLI.getRegisterType(VT); |
| 1004 | SmallVector<SDValue, 4> Parts(NumParts); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1005 | getCopyToParts(DAG, getCurDebugLoc(), |
| 1006 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1007 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 1008 | |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1009 | // 'inreg' on function refers to return value |
| 1010 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1011 | if (F->paramHasAttr(0, Attribute::InReg)) |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1012 | Flags.setInReg(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1013 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1014 | NewValues.push_back(Parts[i]); |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1015 | NewValues.push_back(DAG.getArgFlags(Flags)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | } |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1019 | DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1020 | &NewValues[0], NewValues.size())); |
| 1021 | } |
| 1022 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame^] | 1023 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1024 | /// created for it, emit nodes to copy the value into the virtual |
| 1025 | /// registers. |
| 1026 | void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) { |
| 1027 | if (!V->use_empty()) { |
| 1028 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1029 | if (VMI != FuncInfo.ValueMap.end()) |
| 1030 | CopyValueToVirtualRegister(V, VMI->second); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1034 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1035 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1036 | /// CopyTo/FromReg. |
| 1037 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 1038 | // No need to export constants. |
| 1039 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1040 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1041 | // Already exported? |
| 1042 | if (FuncInfo.isExportedInst(V)) return; |
| 1043 | |
| 1044 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1045 | CopyValueToVirtualRegister(V, Reg); |
| 1046 | } |
| 1047 | |
| 1048 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 1049 | const BasicBlock *FromBB) { |
| 1050 | // The operands of the setcc have to be in this block. We don't know |
| 1051 | // how to export them from some other block. |
| 1052 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 1053 | // Can export from current BB. |
| 1054 | if (VI->getParent() == FromBB) |
| 1055 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1056 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1057 | // Is already exported, noop. |
| 1058 | return FuncInfo.isExportedInst(V); |
| 1059 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1060 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1061 | // If this is an argument, we can export it if the BB is the entry block or |
| 1062 | // if it is already exported. |
| 1063 | if (isa<Argument>(V)) { |
| 1064 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1065 | return true; |
| 1066 | |
| 1067 | // Otherwise, can only export this if it is already exported. |
| 1068 | return FuncInfo.isExportedInst(V); |
| 1069 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1070 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1071 | // Otherwise, constants can always be exported. |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1076 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1077 | return I->getParent() == BB; |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1081 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 1082 | /// the given LLVM IR floating-point condition code. This includes |
| 1083 | /// consideration of global floating-point math flags. |
| 1084 | /// |
| 1085 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 1086 | ISD::CondCode FPC, FOC; |
| 1087 | switch (Pred) { |
| 1088 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1089 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1090 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1091 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1092 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1093 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1094 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1095 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1096 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 1097 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1098 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1099 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1100 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1101 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1102 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1103 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1104 | default: |
| 1105 | assert(0 && "Invalid FCmp predicate opcode!"); |
| 1106 | FOC = FPC = ISD::SETFALSE; |
| 1107 | break; |
| 1108 | } |
| 1109 | if (FiniteOnlyFPMath()) |
| 1110 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1111 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1112 | return FPC; |
| 1113 | } |
| 1114 | |
| 1115 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1116 | /// the given LLVM IR integer condition code. |
| 1117 | /// |
| 1118 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1119 | switch (Pred) { |
| 1120 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1121 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1122 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1123 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1124 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1125 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1126 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1127 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1128 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1129 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1130 | default: |
| 1131 | assert(0 && "Invalid ICmp predicate opcode!"); |
| 1132 | return ISD::SETNE; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1136 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1137 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1138 | /// AND operator tree. |
| 1139 | /// |
| 1140 | void |
| 1141 | SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond, |
| 1142 | MachineBasicBlock *TBB, |
| 1143 | MachineBasicBlock *FBB, |
| 1144 | MachineBasicBlock *CurBB) { |
| 1145 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1146 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1147 | // If the leaf of the tree is a comparison, merge the condition into |
| 1148 | // the caseblock. |
| 1149 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1150 | // The operands of the cmp have to be in this block. We don't know |
| 1151 | // how to export them from some other block. If this is the first block |
| 1152 | // of the sequence, no exporting is needed. |
| 1153 | if (CurBB == CurMBB || |
| 1154 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1155 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1156 | ISD::CondCode Condition; |
| 1157 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1158 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1159 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1160 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1161 | } else { |
| 1162 | Condition = ISD::SETEQ; // silence warning. |
| 1163 | assert(0 && "Unknown compare instruction"); |
| 1164 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1165 | |
| 1166 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1167 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1168 | SwitchCases.push_back(CB); |
| 1169 | return; |
| 1170 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | // Create a CaseBlock record representing this branch. |
| 1174 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(), |
| 1175 | NULL, TBB, FBB, CurBB); |
| 1176 | SwitchCases.push_back(CB); |
| 1177 | } |
| 1178 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1179 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1180 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 1181 | MachineBasicBlock *TBB, |
| 1182 | MachineBasicBlock *FBB, |
| 1183 | MachineBasicBlock *CurBB, |
| 1184 | unsigned Opc) { |
| 1185 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1186 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1187 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1188 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1189 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1190 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1191 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1192 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1193 | return; |
| 1194 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1195 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1196 | // Create TmpBB after CurBB. |
| 1197 | MachineFunction::iterator BBI = CurBB; |
| 1198 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1199 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1200 | CurBB->getParent()->insert(++BBI, TmpBB); |
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 | if (Opc == Instruction::Or) { |
| 1203 | // Codegen X | Y as: |
| 1204 | // jmp_if_X TBB |
| 1205 | // jmp TmpBB |
| 1206 | // TmpBB: |
| 1207 | // jmp_if_Y TBB |
| 1208 | // jmp FBB |
| 1209 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1210 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1211 | // Emit the LHS condition. |
| 1212 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1213 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1214 | // Emit the RHS condition into TmpBB. |
| 1215 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1216 | } else { |
| 1217 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1218 | // Codegen X & Y as: |
| 1219 | // jmp_if_X TmpBB |
| 1220 | // jmp FBB |
| 1221 | // TmpBB: |
| 1222 | // jmp_if_Y TBB |
| 1223 | // jmp FBB |
| 1224 | // |
| 1225 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1226 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1227 | // Emit the LHS condition. |
| 1228 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1230 | // Emit the RHS condition into TmpBB. |
| 1231 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1236 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1237 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1238 | bool |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1239 | SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1240 | if (Cases.size() != 2) return true; |
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 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1243 | // will get folded into a single comparison, so don't emit two blocks. |
| 1244 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1245 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1246 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1247 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1248 | return false; |
| 1249 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1250 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1251 | return true; |
| 1252 | } |
| 1253 | |
| 1254 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1255 | // Update machine-CFG edges. |
| 1256 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1257 | |
| 1258 | // Figure out which block is immediately after the current one. |
| 1259 | MachineBasicBlock *NextBlock = 0; |
| 1260 | MachineFunction::iterator BBI = CurMBB; |
| 1261 | if (++BBI != CurMBB->getParent()->end()) |
| 1262 | NextBlock = BBI; |
| 1263 | |
| 1264 | if (I.isUnconditional()) { |
| 1265 | // Update machine-CFG edges. |
| 1266 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1267 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1268 | // If this is not a fall-through branch, emit the branch. |
| 1269 | if (Succ0MBB != NextBlock) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1270 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1271 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1272 | DAG.getBasicBlock(Succ0MBB))); |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | // If this condition is one of the special cases we handle, do special stuff |
| 1277 | // now. |
| 1278 | Value *CondVal = I.getCondition(); |
| 1279 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1280 | |
| 1281 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1282 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1283 | // For example, instead of something like: |
| 1284 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1285 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1286 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1287 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1288 | // or C, F |
| 1289 | // jnz foo |
| 1290 | // Emit: |
| 1291 | // cmp A, B |
| 1292 | // je foo |
| 1293 | // cmp D, E |
| 1294 | // jle foo |
| 1295 | // |
| 1296 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1297 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1298 | (BOp->getOpcode() == Instruction::And || |
| 1299 | BOp->getOpcode() == Instruction::Or)) { |
| 1300 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1301 | // If the compares in later blocks need to use values not currently |
| 1302 | // exported from this block, export them now. This block should always |
| 1303 | // be the first entry. |
| 1304 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1305 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1306 | // Allow some cases to be rejected. |
| 1307 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1308 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1309 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1310 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1311 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1312 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1313 | // Emit the branch for this block. |
| 1314 | visitSwitchCase(SwitchCases[0]); |
| 1315 | SwitchCases.erase(SwitchCases.begin()); |
| 1316 | return; |
| 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 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1320 | // SwitchCases. |
| 1321 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1322 | CurMBB->getParent()->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1323 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1324 | SwitchCases.clear(); |
| 1325 | } |
| 1326 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1327 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1328 | // Create a CaseBlock record representing this branch. |
| 1329 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(), |
| 1330 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
| 1331 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1332 | // cond branch. |
| 1333 | visitSwitchCase(CB); |
| 1334 | } |
| 1335 | |
| 1336 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1337 | /// the binary search tree resulting from lowering a switch instruction. |
| 1338 | void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) { |
| 1339 | SDValue Cond; |
| 1340 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1341 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1344 | if (CB.CmpMHS == NULL) { |
| 1345 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1346 | // handle common cases produced by branch lowering. |
| 1347 | if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ) |
| 1348 | Cond = CondLHS; |
| 1349 | else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) { |
| 1350 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1351 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1352 | } else |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1353 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1354 | } else { |
| 1355 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1356 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1357 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1358 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1359 | |
| 1360 | SDValue CmpOp = getValue(CB.CmpMHS); |
| 1361 | MVT VT = CmpOp.getValueType(); |
| 1362 | |
| 1363 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1364 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1365 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1366 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1367 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1368 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1369 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1370 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1371 | } |
| 1372 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1373 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1374 | // Update successor info |
| 1375 | CurMBB->addSuccessor(CB.TrueBB); |
| 1376 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1377 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1378 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1379 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1380 | MachineBasicBlock *NextBlock = 0; |
| 1381 | MachineFunction::iterator BBI = CurMBB; |
| 1382 | if (++BBI != CurMBB->getParent()->end()) |
| 1383 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1384 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1385 | // If the lhs block is the next block, invert the condition so that we can |
| 1386 | // fall through to the lhs instead of the rhs block. |
| 1387 | if (CB.TrueBB == NextBlock) { |
| 1388 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1389 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1390 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1391 | } |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1392 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1393 | MVT::Other, getControlRoot(), Cond, |
| 1394 | DAG.getBasicBlock(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 the branch was constant folded, fix up the CFG. |
| 1397 | if (BrCond.getOpcode() == ISD::BR) { |
| 1398 | CurMBB->removeSuccessor(CB.FalseBB); |
| 1399 | DAG.setRoot(BrCond); |
| 1400 | } else { |
| 1401 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1402 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1403 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1404 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1405 | if (CB.FalseBB == NextBlock) |
| 1406 | DAG.setRoot(BrCond); |
| 1407 | else |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1408 | DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1409 | DAG.getBasicBlock(CB.FalseBB))); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1414 | void SelectionDAGLowering::visitJumpTable(JumpTable &JT) { |
| 1415 | // Emit the code for the jump table |
| 1416 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
| 1417 | MVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1418 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1419 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1420 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1421 | DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1422 | MVT::Other, Index.getValue(1), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1423 | Table, Index)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1427 | /// in the JumpTable from switch case. |
| 1428 | void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT, |
| 1429 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1430 | // Subtract the lowest switch case value from the value being switched on and |
| 1431 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1432 | // difference between smallest and largest cases. |
| 1433 | SDValue SwitchOp = getValue(JTH.SValue); |
| 1434 | MVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1435 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1436 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1437 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1438 | // The SDNode we just created, which holds the value being switched on minus |
| 1439 | // the the smallest case value, needs to be copied to a virtual register so it |
| 1440 | // can be used as an index into the jump table in a subsequent basic block. |
| 1441 | // This value may be smaller or larger than the target's pointer type, and |
| 1442 | // therefore require extension or truncating. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1443 | if (VT.bitsGT(TLI.getPointerTy())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1444 | SwitchOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1445 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1446 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1447 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1448 | TLI.getPointerTy(), SUB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1449 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1450 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1451 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1452 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1453 | JT.Reg = JumpTableReg; |
| 1454 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1455 | // Emit the range check for the jump table, and branch to the default block |
| 1456 | // for the switch statement if the value being switched on exceeds the largest |
| 1457 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1458 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
| 1459 | TLI.getSetCCResultType(SUB.getValueType()), SUB, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1460 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1461 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1464 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1465 | MachineBasicBlock *NextBlock = 0; |
| 1466 | MachineFunction::iterator BBI = CurMBB; |
| 1467 | if (++BBI != CurMBB->getParent()->end()) |
| 1468 | NextBlock = BBI; |
| 1469 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1470 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1471 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1472 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (JT.MBB == NextBlock) |
| 1475 | DAG.setRoot(BrCond); |
| 1476 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1477 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1478 | DAG.getBasicBlock(JT.MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1482 | /// suitable for "bit tests" |
| 1483 | void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) { |
| 1484 | // Subtract the minimum value |
| 1485 | SDValue SwitchOp = getValue(B.SValue); |
| 1486 | MVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1487 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1488 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1489 | |
| 1490 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1491 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1492 | TLI.getSetCCResultType(SUB.getValueType()), |
| 1493 | SUB, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1494 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1495 | |
| 1496 | SDValue ShiftOp; |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1497 | if (VT.bitsGT(TLI.getPointerTy())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1498 | ShiftOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1499 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1500 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1501 | ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1502 | TLI.getPointerTy(), SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1503 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1504 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1505 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1506 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1509 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1510 | MachineBasicBlock *NextBlock = 0; |
| 1511 | MachineFunction::iterator BBI = CurMBB; |
| 1512 | if (++BBI != CurMBB->getParent()->end()) |
| 1513 | NextBlock = BBI; |
| 1514 | |
| 1515 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1516 | |
| 1517 | CurMBB->addSuccessor(B.Default); |
| 1518 | CurMBB->addSuccessor(MBB); |
| 1519 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1520 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1521 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1522 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1523 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1524 | if (MBB == NextBlock) |
| 1525 | DAG.setRoot(BrRange); |
| 1526 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1527 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1528 | DAG.getBasicBlock(MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /// visitBitTestCase - this function produces one "bit test" |
| 1532 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1533 | unsigned Reg, |
| 1534 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1535 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1536 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1537 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1538 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1539 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1540 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1541 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1542 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1543 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1544 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1545 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1546 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1547 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1548 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1549 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1550 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1551 | |
| 1552 | CurMBB->addSuccessor(B.TargetBB); |
| 1553 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1554 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1555 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1556 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1557 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1560 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1561 | MachineBasicBlock *NextBlock = 0; |
| 1562 | MachineFunction::iterator BBI = CurMBB; |
| 1563 | if (++BBI != CurMBB->getParent()->end()) |
| 1564 | NextBlock = BBI; |
| 1565 | |
| 1566 | if (NextMBB == NextBlock) |
| 1567 | DAG.setRoot(BrAnd); |
| 1568 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1569 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1570 | DAG.getBasicBlock(NextMBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1574 | // Retrieve successors. |
| 1575 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1576 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1577 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1578 | const Value *Callee(I.getCalledValue()); |
| 1579 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1580 | visitInlineAsm(&I); |
| 1581 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1582 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1583 | |
| 1584 | // If the value of the invoke is used outside of its defining block, make it |
| 1585 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame^] | 1586 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Update successor info |
| 1589 | CurMBB->addSuccessor(Return); |
| 1590 | CurMBB->addSuccessor(LandingPad); |
| 1591 | |
| 1592 | // Drop into normal successor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1593 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1594 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1595 | DAG.getBasicBlock(Return))); |
| 1596 | } |
| 1597 | |
| 1598 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1599 | } |
| 1600 | |
| 1601 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1602 | /// small case ranges). |
| 1603 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
| 1604 | CaseRecVector& WorkList, |
| 1605 | Value* SV, |
| 1606 | MachineBasicBlock* Default) { |
| 1607 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1608 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1609 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1610 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1611 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1612 | return false; |
| 1613 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1614 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1615 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1616 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Figure out which block is immediately after the current one. |
| 1619 | MachineBasicBlock *NextBlock = 0; |
| 1620 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1621 | |
| 1622 | if (++BBI != CurMBB->getParent()->end()) |
| 1623 | NextBlock = BBI; |
| 1624 | |
| 1625 | // TODO: If any two of the cases has the same destination, and if one value |
| 1626 | // is the same as the other, but has one bit unset that the other has set, |
| 1627 | // use bit manipulation to do two compares at once. For example: |
| 1628 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1629 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1630 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1631 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1632 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1633 | // branches in this order. See if rearranging a case value would help. |
| 1634 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1635 | if (I->BB == NextBlock) { |
| 1636 | std::swap(*I, BackCase); |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1641 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1642 | // Create a CaseBlock record representing a conditional branch to |
| 1643 | // the Case's target mbb if the value being switched on SV is equal |
| 1644 | // to C. |
| 1645 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1646 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1647 | MachineBasicBlock *FallThrough; |
| 1648 | if (I != E-1) { |
| 1649 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1650 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1651 | |
| 1652 | // Put SV in a virtual register to make it available from the new blocks. |
| 1653 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1654 | } else { |
| 1655 | // If the last case doesn't match, go to the default block. |
| 1656 | FallThrough = Default; |
| 1657 | } |
| 1658 | |
| 1659 | Value *RHS, *LHS, *MHS; |
| 1660 | ISD::CondCode CC; |
| 1661 | if (I->High == I->Low) { |
| 1662 | // This is just small small case range :) containing exactly 1 case |
| 1663 | CC = ISD::SETEQ; |
| 1664 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1665 | } else { |
| 1666 | CC = ISD::SETLE; |
| 1667 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1668 | } |
| 1669 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
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 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1672 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1673 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1674 | // before the next MBB. |
| 1675 | if (CurBlock == CurMBB) |
| 1676 | visitSwitchCase(CB); |
| 1677 | else |
| 1678 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1679 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1680 | CurBlock = FallThrough; |
| 1681 | } |
| 1682 | |
| 1683 | return true; |
| 1684 | } |
| 1685 | |
| 1686 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1687 | return !DisableJumpTables && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 1688 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1689 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1690 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1691 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1692 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1693 | APInt LastExt(Last), FirstExt(First); |
| 1694 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1695 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1696 | return (LastExt - FirstExt + 1ULL); |
| 1697 | } |
| 1698 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1699 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1700 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
| 1701 | CaseRecVector& WorkList, |
| 1702 | Value* SV, |
| 1703 | MachineBasicBlock* Default) { |
| 1704 | Case& FrontCase = *CR.Range.first; |
| 1705 | Case& BackCase = *(CR.Range.second-1); |
| 1706 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1707 | const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1708 | const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1709 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1710 | size_t TSize = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1711 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1712 | I!=E; ++I) |
| 1713 | TSize += I->size(); |
| 1714 | |
| 1715 | if (!areJTsAllowed(TLI) || TSize <= 3) |
| 1716 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1717 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1718 | APInt Range = ComputeRange(First, Last); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1719 | double Density = (double)TSize / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1720 | if (Density < 0.4) |
| 1721 | return false; |
| 1722 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1723 | DEBUG(errs() << "Lowering jump table\n" |
| 1724 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1725 | << "Range: " << Range |
| 1726 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1727 | |
| 1728 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1729 | // inserting any additional MBBs necessary to represent the switch. |
| 1730 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1731 | |
| 1732 | // Figure out which block is immediately after the current one. |
| 1733 | MachineBasicBlock *NextBlock = 0; |
| 1734 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1735 | |
| 1736 | if (++BBI != CurMBB->getParent()->end()) |
| 1737 | NextBlock = BBI; |
| 1738 | |
| 1739 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1740 | |
| 1741 | // Create a new basic block to hold the code for loading the address |
| 1742 | // of the jump table, and jumping to it. Update successor information; |
| 1743 | // we will either branch to the default case for the switch, or the jump |
| 1744 | // table. |
| 1745 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1746 | CurMF->insert(BBI, JumpTableBB); |
| 1747 | CR.CaseBB->addSuccessor(Default); |
| 1748 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1749 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1750 | // Build a vector of destination BBs, corresponding to each target |
| 1751 | // of the jump table. If the value of the jump table slot corresponds to |
| 1752 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1753 | // the default BB. |
| 1754 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1755 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1756 | 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] | 1757 | const APInt& Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1758 | const APInt& High = cast<ConstantInt>(I->High)->getValue(); |
| 1759 | |
| 1760 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1761 | DestBBs.push_back(I->BB); |
| 1762 | if (TEI==High) |
| 1763 | ++I; |
| 1764 | } else { |
| 1765 | DestBBs.push_back(Default); |
| 1766 | } |
| 1767 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1768 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1769 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1770 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1771 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1772 | E = DestBBs.end(); I != E; ++I) { |
| 1773 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1774 | SuccsHandled[(*I)->getNumber()] = true; |
| 1775 | JumpTableBB->addSuccessor(*I); |
| 1776 | } |
| 1777 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1778 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1779 | // Create a jump table index for this jump table, or return an existing |
| 1780 | // one. |
| 1781 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1782 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1783 | // Set the jump table information so that we can codegen it as a second |
| 1784 | // MachineBasicBlock |
| 1785 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1786 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1787 | if (CR.CaseBB == CurMBB) |
| 1788 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1789 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1790 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1791 | |
| 1792 | return true; |
| 1793 | } |
| 1794 | |
| 1795 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1796 | /// 2 subtrees. |
| 1797 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
| 1798 | CaseRecVector& WorkList, |
| 1799 | Value* SV, |
| 1800 | MachineBasicBlock* Default) { |
| 1801 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1802 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1803 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Figure out which block is immediately after the current one. |
| 1806 | MachineBasicBlock *NextBlock = 0; |
| 1807 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1808 | |
| 1809 | if (++BBI != CurMBB->getParent()->end()) |
| 1810 | NextBlock = BBI; |
| 1811 | |
| 1812 | Case& FrontCase = *CR.Range.first; |
| 1813 | Case& BackCase = *(CR.Range.second-1); |
| 1814 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1815 | |
| 1816 | // Size is the number of Cases represented by this range. |
| 1817 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1818 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1819 | const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1820 | const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1821 | double FMetric = 0; |
| 1822 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1823 | |
| 1824 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1825 | // (heuristically) allow us to emit JumpTable's later. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1826 | size_t TSize = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1827 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1828 | I!=E; ++I) |
| 1829 | TSize += I->size(); |
| 1830 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1831 | size_t LSize = FrontCase.size(); |
| 1832 | size_t RSize = TSize-LSize; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1833 | DEBUG(errs() << "Selecting best pivot: \n" |
| 1834 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1835 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1836 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1837 | J!=E; ++I, ++J) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1838 | const APInt& LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1839 | const APInt& RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1840 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1841 | assert((Range - 2ULL).isNonNegative() && |
| 1842 | "Invalid case distance"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1843 | double LDensity = (double)LSize / (LEnd - First + 1ULL).roundToDouble(); |
| 1844 | double RDensity = (double)RSize / (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1845 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1846 | // Should always split in some non-trivial place |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1847 | DEBUG(errs() <<"=>Step\n" |
| 1848 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1849 | << "LDensity: " << LDensity |
| 1850 | << ", RDensity: " << RDensity << '\n' |
| 1851 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1852 | if (FMetric < Metric) { |
| 1853 | Pivot = J; |
| 1854 | FMetric = Metric; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1855 | DEBUG(errs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | LSize += J->size(); |
| 1859 | RSize -= J->size(); |
| 1860 | } |
| 1861 | if (areJTsAllowed(TLI)) { |
| 1862 | // If our case is dense we *really* should handle it earlier! |
| 1863 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1864 | } else { |
| 1865 | Pivot = CR.Range.first + Size/2; |
| 1866 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1867 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1868 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1869 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1870 | Constant *C = Pivot->Low; |
| 1871 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1872 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1873 | // 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] | 1874 | // 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] | 1875 | // 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] | 1876 | // 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] | 1877 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1878 | // rather than creating a leaf node for it. |
| 1879 | if ((LHSR.second - LHSR.first) == 1 && |
| 1880 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1881 | cast<ConstantInt>(C)->getValue() == |
| 1882 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1883 | TrueBB = LHSR.first->BB; |
| 1884 | } else { |
| 1885 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1886 | CurMF->insert(BBI, TrueBB); |
| 1887 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1888 | |
| 1889 | // Put SV in a virtual register to make it available from the new blocks. |
| 1890 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1891 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1892 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1893 | // Similar to the optimization above, if the Value being switched on is |
| 1894 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1895 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1896 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1897 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1898 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1899 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1900 | FalseBB = RHSR.first->BB; |
| 1901 | } else { |
| 1902 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1903 | CurMF->insert(BBI, FalseBB); |
| 1904 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1905 | |
| 1906 | // Put SV in a virtual register to make it available from the new blocks. |
| 1907 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1911 | // 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] | 1912 | // Otherwise, branch to LHS. |
| 1913 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1914 | |
| 1915 | if (CR.CaseBB == CurMBB) |
| 1916 | visitSwitchCase(CB); |
| 1917 | else |
| 1918 | SwitchCases.push_back(CB); |
| 1919 | |
| 1920 | return true; |
| 1921 | } |
| 1922 | |
| 1923 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1924 | /// range span less, than machine word bitwidth, encode case range into series |
| 1925 | /// of masks and emit bit tests with these masks. |
| 1926 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1927 | CaseRecVector& WorkList, |
| 1928 | Value* SV, |
| 1929 | MachineBasicBlock* Default){ |
| 1930 | unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits(); |
| 1931 | |
| 1932 | Case& FrontCase = *CR.Range.first; |
| 1933 | Case& BackCase = *(CR.Range.second-1); |
| 1934 | |
| 1935 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1936 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1937 | MachineFunction *CurMF = CurMBB->getParent(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1938 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1939 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1940 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1941 | I!=E; ++I) { |
| 1942 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1943 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1944 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1945 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1946 | // Count unique destinations |
| 1947 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1948 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1949 | Dests.insert(I->BB); |
| 1950 | if (Dests.size() > 3) |
| 1951 | // Don't bother the code below, if there are too much unique destinations |
| 1952 | return false; |
| 1953 | } |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1954 | DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n' |
| 1955 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1956 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1957 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1958 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1959 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1960 | APInt cmpRange = maxValue - minValue; |
| 1961 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1962 | DEBUG(errs() << "Compare range: " << cmpRange << '\n' |
| 1963 | << "Low bound: " << minValue << '\n' |
| 1964 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1965 | |
| 1966 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1967 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1968 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1969 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1970 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1971 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1972 | DEBUG(errs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1973 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1974 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1975 | // Optimize the case where all the case values fit in a |
| 1976 | // word without having to subtract minValue. In this case, |
| 1977 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1978 | if (minValue.isNonNegative() && |
| 1979 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 1980 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1981 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1982 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1983 | } |
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 | CaseBitsVector CasesBits; |
| 1986 | unsigned i, count = 0; |
| 1987 | |
| 1988 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1989 | MachineBasicBlock* Dest = I->BB; |
| 1990 | for (i = 0; i < count; ++i) |
| 1991 | if (Dest == CasesBits[i].BB) |
| 1992 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1993 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1994 | if (i == count) { |
| 1995 | assert((count < 3) && "Too much destinations to test!"); |
| 1996 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1997 | count++; |
| 1998 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1999 | |
| 2000 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2001 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2002 | |
| 2003 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2004 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 2005 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2006 | for (uint64_t j = lo; j <= hi; j++) { |
| 2007 | CasesBits[i].Mask |= 1ULL << j; |
| 2008 | CasesBits[i].Bits++; |
| 2009 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2010 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2011 | } |
| 2012 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2013 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2014 | BitTestInfo BTC; |
| 2015 | |
| 2016 | // Figure out which block is immediately after the current one. |
| 2017 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2018 | ++BBI; |
| 2019 | |
| 2020 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2021 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2022 | DEBUG(errs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2023 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2024 | DEBUG(errs() << "Mask: " << CasesBits[i].Mask |
| 2025 | << ", Bits: " << CasesBits[i].Bits |
| 2026 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2027 | |
| 2028 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2029 | CurMF->insert(BBI, CaseBB); |
| 2030 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2031 | CaseBB, |
| 2032 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2033 | |
| 2034 | // Put SV in a virtual register to make it available from the new blocks. |
| 2035 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2036 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2037 | |
| 2038 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2039 | -1U, (CR.CaseBB == CurMBB), |
| 2040 | CR.CaseBB, Default, BTC); |
| 2041 | |
| 2042 | if (CR.CaseBB == CurMBB) |
| 2043 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2044 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2045 | BitTestCases.push_back(BTB); |
| 2046 | |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2052 | size_t SelectionDAGLowering::Clusterify(CaseVector& Cases, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2053 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2054 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2057 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2058 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2059 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2060 | SI.getSuccessorValue(i), |
| 2061 | SMBB)); |
| 2062 | } |
| 2063 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 2064 | |
| 2065 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2066 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2067 | // Must recompute end() each iteration because it may be |
| 2068 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2069 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 2070 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2071 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2072 | MachineBasicBlock* nextBB = J->BB; |
| 2073 | MachineBasicBlock* currentBB = I->BB; |
| 2074 | |
| 2075 | // If the two neighboring cases go to the same destination, merge them |
| 2076 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2077 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2078 | I->High = J->High; |
| 2079 | J = Cases.erase(J); |
| 2080 | } else { |
| 2081 | I = J++; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2086 | if (I->Low != I->High) |
| 2087 | // A range counts double, since it requires two compares. |
| 2088 | ++numCmps; |
| 2089 | } |
| 2090 | |
| 2091 | return numCmps; |
| 2092 | } |
| 2093 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2094 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2095 | // Figure out which block is immediately after the current one. |
| 2096 | MachineBasicBlock *NextBlock = 0; |
| 2097 | MachineFunction::iterator BBI = CurMBB; |
| 2098 | |
| 2099 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2100 | |
| 2101 | // If there is only the default destination, branch to it if it is not the |
| 2102 | // next basic block. Otherwise, just fall through. |
| 2103 | if (SI.getNumOperands() == 2) { |
| 2104 | // Update machine-CFG edges. |
| 2105 | |
| 2106 | // If this is not a fall-through branch, emit the branch. |
| 2107 | CurMBB->addSuccessor(Default); |
| 2108 | if (Default != NextBlock) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2109 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2110 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2111 | DAG.getBasicBlock(Default))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2112 | return; |
| 2113 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2114 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2115 | // If there are any non-default case statements, create a vector of Cases |
| 2116 | // representing each one, and sort the vector so that we can efficiently |
| 2117 | // create a binary search tree from them. |
| 2118 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2119 | size_t numCmps = Clusterify(Cases, SI); |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2120 | DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size() |
| 2121 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2122 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2123 | |
| 2124 | // Get the Value to be switched on and default basic blocks, which will be |
| 2125 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2126 | // search tree. |
| 2127 | Value *SV = SI.getOperand(0); |
| 2128 | |
| 2129 | // Push the initial CaseRec onto the worklist |
| 2130 | CaseRecVector WorkList; |
| 2131 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2132 | |
| 2133 | while (!WorkList.empty()) { |
| 2134 | // Grab a record representing a case range to process off the worklist |
| 2135 | CaseRec CR = WorkList.back(); |
| 2136 | WorkList.pop_back(); |
| 2137 | |
| 2138 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2139 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2140 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2141 | // If the range has few cases (two or less) emit a series of specific |
| 2142 | // tests. |
| 2143 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2144 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2145 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2146 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2147 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2148 | // lowering the switch to a binary tree of conditional branches. |
| 2149 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2150 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2151 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2152 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2153 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2154 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | |
| 2159 | void SelectionDAGLowering::visitSub(User &I) { |
| 2160 | // -0.0 - X --> fneg |
| 2161 | const Type *Ty = I.getType(); |
| 2162 | if (isa<VectorType>(Ty)) { |
| 2163 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2164 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2165 | const Type *ElTy = DestTy->getElementType(); |
| 2166 | if (ElTy->isFloatingPoint()) { |
| 2167 | unsigned VL = DestTy->getNumElements(); |
| 2168 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
| 2169 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
| 2170 | if (CV == CNZ) { |
| 2171 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2172 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2173 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2174 | return; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | if (Ty->isFloatingPoint()) { |
| 2180 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 2181 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
| 2182 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2183 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2184 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2185 | return; |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB); |
| 2190 | } |
| 2191 | |
| 2192 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
| 2193 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2194 | SDValue Op2 = getValue(I.getOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2195 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2196 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2197 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2201 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2202 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2203 | if (!isa<VectorType>(I.getType()) && |
| 2204 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2205 | // If the operand is smaller than the shift count type, promote it. |
| 2206 | if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType())) |
| 2207 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2208 | TLI.getShiftAmountTy(), Op2); |
| 2209 | // If the operand is larger than the shift count type but the shift |
| 2210 | // count type has enough bits to represent any shift value, truncate |
| 2211 | // it now. This is a common case and it exposes the truncate to |
| 2212 | // optimization early. |
| 2213 | else if (TLI.getShiftAmountTy().getSizeInBits() >= |
| 2214 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2215 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2216 | TLI.getShiftAmountTy(), Op2); |
| 2217 | // Otherwise we'll need to temporarily settle for some other |
| 2218 | // convenient type; type legalization will make adjustments as |
| 2219 | // needed. |
| 2220 | else if (TLI.getPointerTy().bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2221 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2222 | TLI.getPointerTy(), Op2); |
| 2223 | else if (TLI.getPointerTy().bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2224 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2225 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2226 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2227 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2228 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2229 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | void SelectionDAGLowering::visitICmp(User &I) { |
| 2233 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2234 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2235 | predicate = IC->getPredicate(); |
| 2236 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2237 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2238 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2239 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2240 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2241 | setValue(&I, DAG.getSetCC(getCurDebugLoc(),MVT::i1, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | void SelectionDAGLowering::visitFCmp(User &I) { |
| 2245 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2246 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2247 | predicate = FC->getPredicate(); |
| 2248 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2249 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2250 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2251 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2252 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2253 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), MVT::i1, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | void SelectionDAGLowering::visitVICmp(User &I) { |
| 2257 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2258 | if (VICmpInst *IC = dyn_cast<VICmpInst>(&I)) |
| 2259 | predicate = IC->getPredicate(); |
| 2260 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2261 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2262 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2263 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2264 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2265 | setValue(&I, DAG.getVSetCC(getCurDebugLoc(), Op1.getValueType(), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2266 | Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | void SelectionDAGLowering::visitVFCmp(User &I) { |
| 2270 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2271 | if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I)) |
| 2272 | predicate = FC->getPredicate(); |
| 2273 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2274 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2275 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2276 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2277 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2278 | MVT DestVT = TLI.getValueType(I.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2279 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 2280 | setValue(&I, DAG.getVSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | void SelectionDAGLowering::visitSelect(User &I) { |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2284 | SmallVector<MVT, 4> ValueVTs; |
| 2285 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2286 | unsigned NumValues = ValueVTs.size(); |
| 2287 | if (NumValues != 0) { |
| 2288 | SmallVector<SDValue, 4> Values(NumValues); |
| 2289 | SDValue Cond = getValue(I.getOperand(0)); |
| 2290 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2291 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2292 | |
| 2293 | for (unsigned i = 0; i != NumValues; ++i) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2294 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2295 | TrueVal.getValueType(), Cond, |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2296 | SDValue(TrueVal.getNode(), TrueVal.getResNo() + i), |
| 2297 | SDValue(FalseVal.getNode(), FalseVal.getResNo() + i)); |
| 2298 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2299 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2300 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2301 | &Values[0], NumValues)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2302 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | |
| 2306 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2307 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2308 | SDValue N = getValue(I.getOperand(0)); |
| 2309 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2310 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2314 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2315 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2316 | SDValue N = getValue(I.getOperand(0)); |
| 2317 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2318 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2322 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2323 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2324 | SDValue N = getValue(I.getOperand(0)); |
| 2325 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2326 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2330 | // FPTrunc is never a no-op cast, no need to check |
| 2331 | SDValue N = getValue(I.getOperand(0)); |
| 2332 | MVT DestVT = TLI.getValueType(I.getType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2333 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2334 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2337 | void SelectionDAGLowering::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2338 | // FPTrunc is never a no-op cast, no need to check |
| 2339 | SDValue N = getValue(I.getOperand(0)); |
| 2340 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2341 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2344 | void SelectionDAGLowering::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2345 | // FPToUI is never a no-op cast, no need to check |
| 2346 | SDValue N = getValue(I.getOperand(0)); |
| 2347 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2348 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2352 | // FPToSI is never a no-op cast, no need to check |
| 2353 | SDValue N = getValue(I.getOperand(0)); |
| 2354 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2355 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2358 | void SelectionDAGLowering::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2359 | // UIToFP is never a no-op cast, no need to check |
| 2360 | SDValue N = getValue(I.getOperand(0)); |
| 2361 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2362 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2365 | void SelectionDAGLowering::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2366 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2367 | SDValue N = getValue(I.getOperand(0)); |
| 2368 | MVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2369 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2373 | // What to do depends on the size of the integer and the size of the pointer. |
| 2374 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2375 | SDValue N = getValue(I.getOperand(0)); |
| 2376 | MVT SrcVT = N.getValueType(); |
| 2377 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2378 | SDValue Result; |
| 2379 | if (DestVT.bitsLT(SrcVT)) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2380 | Result = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2381 | else |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2382 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2383 | Result = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2384 | setValue(&I, Result); |
| 2385 | } |
| 2386 | |
| 2387 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2388 | // What to do depends on the size of the integer and the size of the pointer. |
| 2389 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2390 | SDValue N = getValue(I.getOperand(0)); |
| 2391 | MVT SrcVT = N.getValueType(); |
| 2392 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2393 | if (DestVT.bitsLT(SrcVT)) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2394 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2395 | else |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2396 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2397 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2398 | DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2401 | void SelectionDAGLowering::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2402 | SDValue N = getValue(I.getOperand(0)); |
| 2403 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2404 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2405 | // 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] | 2406 | // is either a BIT_CONVERT or a no-op. |
| 2407 | if (DestVT != N.getValueType()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2408 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2409 | DestVT, N)); // convert types |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2410 | else |
| 2411 | setValue(&I, N); // noop cast. |
| 2412 | } |
| 2413 | |
| 2414 | void SelectionDAGLowering::visitInsertElement(User &I) { |
| 2415 | SDValue InVec = getValue(I.getOperand(0)); |
| 2416 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2417 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2418 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2419 | getValue(I.getOperand(2))); |
| 2420 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2421 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2422 | TLI.getValueType(I.getType()), |
| 2423 | InVec, InVal, InIdx)); |
| 2424 | } |
| 2425 | |
| 2426 | void SelectionDAGLowering::visitExtractElement(User &I) { |
| 2427 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2428 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2429 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2430 | getValue(I.getOperand(1))); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2431 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2432 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2433 | } |
| 2434 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2435 | |
| 2436 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2437 | // from SIndx and increasing to the element length (undefs are allowed). |
| 2438 | static bool SequentialMask(SDValue Mask, unsigned SIndx) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2439 | unsigned MaskNumElts = Mask.getNumOperands(); |
| 2440 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2441 | if (Mask.getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2442 | unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue(); |
| 2443 | if (Idx != i + SIndx) |
| 2444 | return false; |
| 2445 | } |
| 2446 | } |
| 2447 | return true; |
| 2448 | } |
| 2449 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2450 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2451 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2452 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2453 | SDValue Mask = getValue(I.getOperand(2)); |
| 2454 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2455 | MVT VT = TLI.getValueType(I.getType()); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2456 | MVT SrcVT = Src1.getValueType(); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2457 | int MaskNumElts = Mask.getNumOperands(); |
| 2458 | int SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2459 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2460 | if (SrcNumElts == MaskNumElts) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2461 | setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2462 | VT, Src1, Src2, Mask)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2463 | return; |
| 2464 | } |
| 2465 | |
| 2466 | // 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] | 2467 | MVT MaskEltVT = Mask.getValueType().getVectorElementType(); |
| 2468 | |
| 2469 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2470 | // Mask is longer than the source vectors and is a multiple of the source |
| 2471 | // 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] | 2472 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2473 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2474 | // The shuffle is concatenating two vectors together. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2475 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2476 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2477 | return; |
| 2478 | } |
| 2479 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2480 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2481 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2482 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2483 | |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2484 | SDValue* MOps1 = new SDValue[NumConcat]; |
| 2485 | SDValue* MOps2 = new SDValue[NumConcat]; |
| 2486 | MOps1[0] = Src1; |
| 2487 | MOps2[0] = Src2; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2488 | for (unsigned i = 1; i != NumConcat; ++i) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2489 | MOps1[i] = UndefVal; |
| 2490 | MOps2[i] = UndefVal; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2491 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2492 | Src1 = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2493 | VT, MOps1, NumConcat); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2494 | Src2 = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2495 | VT, MOps2, NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2496 | |
| 2497 | delete [] MOps1; |
| 2498 | delete [] MOps2; |
| 2499 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2500 | // Readjust mask for new input vector length. |
| 2501 | SmallVector<SDValue, 8> MappedOps; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2502 | for (int i = 0; i != MaskNumElts; ++i) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2503 | if (Mask.getOperand(i).getOpcode() == ISD::UNDEF) { |
| 2504 | MappedOps.push_back(Mask.getOperand(i)); |
| 2505 | } else { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2506 | int Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue(); |
| 2507 | if (Idx < SrcNumElts) |
| 2508 | MappedOps.push_back(DAG.getConstant(Idx, MaskEltVT)); |
| 2509 | else |
| 2510 | MappedOps.push_back(DAG.getConstant(Idx + MaskNumElts - SrcNumElts, |
| 2511 | MaskEltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2514 | Mask = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2515 | Mask.getValueType(), |
| 2516 | &MappedOps[0], MappedOps.size()); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2517 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2518 | setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2519 | VT, Src1, Src2, Mask)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
| 2522 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2523 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2524 | // Resulting vector is shorter than the incoming vector. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2525 | if (SrcNumElts == MaskNumElts && SequentialMask(Mask,0)) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2526 | // Shuffle extracts 1st vector. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2527 | setValue(&I, Src1); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2528 | return; |
| 2529 | } |
| 2530 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2531 | if (SrcNumElts == MaskNumElts && SequentialMask(Mask,MaskNumElts)) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2532 | // Shuffle extracts 2nd vector. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2533 | setValue(&I, Src2); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2534 | return; |
| 2535 | } |
| 2536 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2537 | // Analyze the access pattern of the vector to see if we can extract |
| 2538 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2539 | // the range of elements the mask access on both vectors. |
| 2540 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2541 | int MaxRange[2] = {-1, -1}; |
| 2542 | |
| 2543 | for (int i = 0; i != MaskNumElts; ++i) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2544 | SDValue Arg = Mask.getOperand(i); |
| 2545 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2546 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2547 | int Idx = cast<ConstantSDNode>(Arg)->getZExtValue(); |
| 2548 | int Input = 0; |
| 2549 | if (Idx >= SrcNumElts) { |
| 2550 | Input = 1; |
| 2551 | Idx -= SrcNumElts; |
| 2552 | } |
| 2553 | if (Idx > MaxRange[Input]) |
| 2554 | MaxRange[Input] = Idx; |
| 2555 | if (Idx < MinRange[Input]) |
| 2556 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2557 | } |
| 2558 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2559 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2560 | // Check if the access is smaller than the vector size and can we find |
| 2561 | // a reasonable extract index. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2562 | 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] | 2563 | int StartIdx[2]; // StartIdx to extract from |
| 2564 | for (int Input=0; Input < 2; ++Input) { |
| 2565 | if (MinRange[Input] == SrcNumElts+1 && MaxRange[Input] == -1) { |
| 2566 | RangeUse[Input] = 0; // Unused |
| 2567 | StartIdx[Input] = 0; |
| 2568 | } else if (MaxRange[Input] - MinRange[Input] < MaskNumElts) { |
| 2569 | // 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] | 2570 | // start index that is a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2571 | if (MaxRange[Input] < MaskNumElts) { |
| 2572 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2573 | StartIdx[Input] = 0; |
| 2574 | } else { |
| 2575 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Mon P Wang | 6cce3da | 2008-11-23 04:35:05 +0000 | [diff] [blame] | 2576 | if (MaxRange[Input] - StartIdx[Input] < MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2577 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2578 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2579 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2580 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | if (RangeUse[0] == 0 && RangeUse[0] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2584 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
| 2587 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2588 | // Extract appropriate subvector and generate a vector shuffle |
| 2589 | for (int Input=0; Input < 2; ++Input) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2590 | SDValue& Src = Input == 0 ? Src1 : Src2; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2591 | if (RangeUse[Input] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2592 | Src = DAG.getUNDEF(VT); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2593 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2594 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2595 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2596 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2597 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2598 | // Calculate new mask. |
| 2599 | SmallVector<SDValue, 8> MappedOps; |
| 2600 | for (int i = 0; i != MaskNumElts; ++i) { |
| 2601 | SDValue Arg = Mask.getOperand(i); |
| 2602 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2603 | MappedOps.push_back(Arg); |
| 2604 | } else { |
| 2605 | int Idx = cast<ConstantSDNode>(Arg)->getZExtValue(); |
| 2606 | if (Idx < SrcNumElts) |
| 2607 | MappedOps.push_back(DAG.getConstant(Idx - StartIdx[0], MaskEltVT)); |
| 2608 | else { |
| 2609 | Idx = Idx - SrcNumElts - StartIdx[1] + MaskNumElts; |
| 2610 | MappedOps.push_back(DAG.getConstant(Idx, MaskEltVT)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2611 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2612 | } |
| 2613 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2614 | Mask = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2615 | Mask.getValueType(), |
| 2616 | &MappedOps[0], MappedOps.size()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2617 | setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2618 | VT, Src1, Src2, Mask)); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2619 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2623 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2624 | // replacing the shuffle with extract and build vector. |
| 2625 | // to insert and build vector. |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2626 | MVT EltVT = VT.getVectorElementType(); |
| 2627 | MVT PtrVT = TLI.getPointerTy(); |
| 2628 | SmallVector<SDValue,8> Ops; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2629 | for (int i = 0; i != MaskNumElts; ++i) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2630 | SDValue Arg = Mask.getOperand(i); |
| 2631 | if (Arg.getOpcode() == ISD::UNDEF) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2632 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2633 | } else { |
| 2634 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2635 | int Idx = cast<ConstantSDNode>(Arg)->getZExtValue(); |
| 2636 | if (Idx < SrcNumElts) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2637 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2638 | EltVT, Src1, DAG.getConstant(Idx, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2639 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2640 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2641 | EltVT, Src2, |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2642 | DAG.getConstant(Idx - SrcNumElts, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2643 | } |
| 2644 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2645 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2646 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) { |
| 2650 | const Value *Op0 = I.getOperand(0); |
| 2651 | const Value *Op1 = I.getOperand(1); |
| 2652 | const Type *AggTy = I.getType(); |
| 2653 | const Type *ValTy = Op1->getType(); |
| 2654 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2655 | bool FromUndef = isa<UndefValue>(Op1); |
| 2656 | |
| 2657 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2658 | I.idx_begin(), I.idx_end()); |
| 2659 | |
| 2660 | SmallVector<MVT, 4> AggValueVTs; |
| 2661 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 2662 | SmallVector<MVT, 4> ValValueVTs; |
| 2663 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2664 | |
| 2665 | unsigned NumAggValues = AggValueVTs.size(); |
| 2666 | unsigned NumValValues = ValValueVTs.size(); |
| 2667 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2668 | |
| 2669 | SDValue Agg = getValue(Op0); |
| 2670 | SDValue Val = getValue(Op1); |
| 2671 | unsigned i = 0; |
| 2672 | // Copy the beginning value(s) from the original aggregate. |
| 2673 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2674 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2675 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2676 | // Copy values from the inserted value(s). |
| 2677 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2678 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2679 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2680 | // Copy remaining value(s) from the original aggregate. |
| 2681 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2682 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2683 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2684 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2685 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2686 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2687 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) { |
| 2691 | const Value *Op0 = I.getOperand(0); |
| 2692 | const Type *AggTy = Op0->getType(); |
| 2693 | const Type *ValTy = I.getType(); |
| 2694 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2695 | |
| 2696 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2697 | I.idx_begin(), I.idx_end()); |
| 2698 | |
| 2699 | SmallVector<MVT, 4> ValValueVTs; |
| 2700 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2701 | |
| 2702 | unsigned NumValValues = ValValueVTs.size(); |
| 2703 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2704 | |
| 2705 | SDValue Agg = getValue(Op0); |
| 2706 | // Copy out the selected value(s). |
| 2707 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2708 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2709 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2710 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2711 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2712 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2713 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2714 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2715 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | |
| 2719 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2720 | SDValue N = getValue(I.getOperand(0)); |
| 2721 | const Type *Ty = I.getOperand(0)->getType(); |
| 2722 | |
| 2723 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2724 | OI != E; ++OI) { |
| 2725 | Value *Idx = *OI; |
| 2726 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2727 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2728 | if (Field) { |
| 2729 | // N = N + Offset |
| 2730 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2731 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2732 | DAG.getIntPtrConstant(Offset)); |
| 2733 | } |
| 2734 | Ty = StTy->getElementType(Field); |
| 2735 | } else { |
| 2736 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2737 | |
| 2738 | // If this is a constant subscript, handle it quickly. |
| 2739 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2740 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2741 | uint64_t Offs = |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2742 | TD->getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2743 | SDValue OffsVal; |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2744 | unsigned PtrBits = TLI.getPointerTy().getSizeInBits(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2745 | if (PtrBits < 64) { |
| 2746 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2747 | TLI.getPointerTy(), |
| 2748 | DAG.getConstant(Offs, MVT::i64)); |
| 2749 | } else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2750 | OffsVal = DAG.getIntPtrConstant(Offs); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2751 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2752 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2753 | continue; |
| 2754 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2755 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2756 | // N = N + Idx * ElementSize; |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2757 | uint64_t ElementSize = TD->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2758 | SDValue IdxN = getValue(Idx); |
| 2759 | |
| 2760 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2761 | // it. |
| 2762 | if (IdxN.getValueType().bitsLT(N.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2763 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2764 | N.getValueType(), IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | else if (IdxN.getValueType().bitsGT(N.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2766 | IdxN = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2767 | N.getValueType(), IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2768 | |
| 2769 | // If this is a multiply by a power of two, turn it into a shl |
| 2770 | // immediately. This is a very common case. |
| 2771 | if (ElementSize != 1) { |
| 2772 | if (isPowerOf2_64(ElementSize)) { |
| 2773 | unsigned Amt = Log2_64(ElementSize); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2774 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2775 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2776 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2777 | } else { |
| 2778 | SDValue Scale = DAG.getIntPtrConstant(ElementSize); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2779 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2780 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2781 | } |
| 2782 | } |
| 2783 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2784 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2785 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | setValue(&I, N); |
| 2789 | } |
| 2790 | |
| 2791 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2792 | // If this is a fixed sized alloca in the entry block of the function, |
| 2793 | // allocate it statically on the stack. |
| 2794 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2795 | return; // getValue will auto-populate this. |
| 2796 | |
| 2797 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2798 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2799 | unsigned Align = |
| 2800 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2801 | I.getAlignment()); |
| 2802 | |
| 2803 | SDValue AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2804 | |
| 2805 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2806 | AllocSize, |
| 2807 | DAG.getConstant(TySize, AllocSize.getValueType())); |
| 2808 | |
| 2809 | |
| 2810 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | MVT IntPtr = TLI.getPointerTy(); |
| 2812 | if (IntPtr.bitsLT(AllocSize.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2813 | AllocSize = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2814 | IntPtr, AllocSize); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2815 | else if (IntPtr.bitsGT(AllocSize.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2816 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2817 | IntPtr, AllocSize); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2818 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2819 | // Handle alignment. If the requested alignment is less than or equal to |
| 2820 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2821 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2822 | unsigned StackAlign = |
| 2823 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2824 | if (Align <= StackAlign) |
| 2825 | Align = 0; |
| 2826 | |
| 2827 | // Round the size of the allocation up to the stack alignment size |
| 2828 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2829 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2830 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2831 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2832 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2833 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2834 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2835 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2836 | |
| 2837 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2838 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2839 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2840 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2841 | setValue(&I, DSA); |
| 2842 | DAG.setRoot(DSA.getValue(1)); |
| 2843 | |
| 2844 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2845 | // object. |
| 2846 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 2847 | } |
| 2848 | |
| 2849 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2850 | const Value *SV = I.getOperand(0); |
| 2851 | SDValue Ptr = getValue(SV); |
| 2852 | |
| 2853 | const Type *Ty = I.getType(); |
| 2854 | bool isVolatile = I.isVolatile(); |
| 2855 | unsigned Alignment = I.getAlignment(); |
| 2856 | |
| 2857 | SmallVector<MVT, 4> ValueVTs; |
| 2858 | SmallVector<uint64_t, 4> Offsets; |
| 2859 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2860 | unsigned NumValues = ValueVTs.size(); |
| 2861 | if (NumValues == 0) |
| 2862 | return; |
| 2863 | |
| 2864 | SDValue Root; |
| 2865 | bool ConstantMemory = false; |
| 2866 | if (I.isVolatile()) |
| 2867 | // Serialize volatile loads with other side effects. |
| 2868 | Root = getRoot(); |
| 2869 | else if (AA->pointsToConstantMemory(SV)) { |
| 2870 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2871 | Root = DAG.getEntryNode(); |
| 2872 | ConstantMemory = true; |
| 2873 | } else { |
| 2874 | // Do not serialize non-volatile loads against each other. |
| 2875 | Root = DAG.getRoot(); |
| 2876 | } |
| 2877 | |
| 2878 | SmallVector<SDValue, 4> Values(NumValues); |
| 2879 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2880 | MVT PtrVT = Ptr.getValueType(); |
| 2881 | for (unsigned i = 0; i != NumValues; ++i) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2882 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2883 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2884 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2885 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2886 | SV, Offsets[i], |
| 2887 | isVolatile, Alignment); |
| 2888 | Values[i] = L; |
| 2889 | Chains[i] = L.getValue(1); |
| 2890 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2891 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2892 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2893 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2894 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2895 | &Chains[0], NumValues); |
| 2896 | if (isVolatile) |
| 2897 | DAG.setRoot(Chain); |
| 2898 | else |
| 2899 | PendingLoads.push_back(Chain); |
| 2900 | } |
| 2901 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2902 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2903 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2904 | &Values[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | |
| 2908 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2909 | Value *SrcV = I.getOperand(0); |
| 2910 | Value *PtrV = I.getOperand(1); |
| 2911 | |
| 2912 | SmallVector<MVT, 4> ValueVTs; |
| 2913 | SmallVector<uint64_t, 4> Offsets; |
| 2914 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2915 | unsigned NumValues = ValueVTs.size(); |
| 2916 | if (NumValues == 0) |
| 2917 | return; |
| 2918 | |
| 2919 | // Get the lowered operands. Note that we do this after |
| 2920 | // checking if NumResults is zero, because with zero results |
| 2921 | // the operands won't have values in the map. |
| 2922 | SDValue Src = getValue(SrcV); |
| 2923 | SDValue Ptr = getValue(PtrV); |
| 2924 | |
| 2925 | SDValue Root = getRoot(); |
| 2926 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2927 | MVT PtrVT = Ptr.getValueType(); |
| 2928 | bool isVolatile = I.isVolatile(); |
| 2929 | unsigned Alignment = I.getAlignment(); |
| 2930 | for (unsigned i = 0; i != NumValues; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2931 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2932 | SDValue(Src.getNode(), Src.getResNo() + i), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2933 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2934 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2935 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2936 | PtrV, Offsets[i], |
| 2937 | isVolatile, Alignment); |
| 2938 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2939 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2940 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2944 | /// node. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2945 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2946 | unsigned Intrinsic) { |
| 2947 | bool HasChain = !I.doesNotAccessMemory(); |
| 2948 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2949 | |
| 2950 | // Build the operand list. |
| 2951 | SmallVector<SDValue, 8> Ops; |
| 2952 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2953 | if (OnlyLoad) { |
| 2954 | // We don't need to serialize loads against other loads. |
| 2955 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2956 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2957 | Ops.push_back(getRoot()); |
| 2958 | } |
| 2959 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2960 | |
| 2961 | // Info is set by getTgtMemInstrinsic |
| 2962 | TargetLowering::IntrinsicInfo Info; |
| 2963 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2964 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2965 | // 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] | 2966 | if (!IsTgtIntrinsic) |
| 2967 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2968 | |
| 2969 | // Add all operands of the call to the operand list. |
| 2970 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2971 | SDValue Op = getValue(I.getOperand(i)); |
| 2972 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2973 | "Intrinsic uses a non-legal type?"); |
| 2974 | Ops.push_back(Op); |
| 2975 | } |
| 2976 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2977 | std::vector<MVT> VTArray; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2978 | if (I.getType() != Type::VoidTy) { |
| 2979 | MVT VT = TLI.getValueType(I.getType()); |
| 2980 | if (VT.isVector()) { |
| 2981 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2982 | MVT EltVT = TLI.getValueType(DestTy->getElementType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2983 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2984 | VT = MVT::getVectorVT(EltVT, DestTy->getNumElements()); |
| 2985 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2986 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2987 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2988 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2989 | VTArray.push_back(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2990 | } |
| 2991 | if (HasChain) |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2992 | VTArray.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2993 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2994 | SDVTList VTs = DAG.getVTList(&VTArray[0], VTArray.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2995 | |
| 2996 | // Create the node. |
| 2997 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2998 | if (IsTgtIntrinsic) { |
| 2999 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3000 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3001 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3002 | Info.memVT, Info.ptrVal, Info.offset, |
| 3003 | Info.align, Info.vol, |
| 3004 | Info.readMem, Info.writeMem); |
| 3005 | } |
| 3006 | else if (!HasChain) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3007 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3008 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3009 | else if (I.getType() != Type::VoidTy) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3010 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3011 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3012 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3013 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3014 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3015 | |
| 3016 | if (HasChain) { |
| 3017 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3018 | if (OnlyLoad) |
| 3019 | PendingLoads.push_back(Chain); |
| 3020 | else |
| 3021 | DAG.setRoot(Chain); |
| 3022 | } |
| 3023 | if (I.getType() != Type::VoidTy) { |
| 3024 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
| 3025 | MVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3026 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3027 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3028 | setValue(&I, Result); |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 3033 | static GlobalVariable *ExtractTypeInfo(Value *V) { |
| 3034 | V = V->stripPointerCasts(); |
| 3035 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 3036 | assert ((GV || isa<ConstantPointerNull>(V)) && |
| 3037 | "TypeInfo must be a global variable or NULL"); |
| 3038 | return GV; |
| 3039 | } |
| 3040 | |
| 3041 | namespace llvm { |
| 3042 | |
| 3043 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 3044 | /// call, and add them to the specified machine basic block. |
| 3045 | void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 3046 | MachineBasicBlock *MBB) { |
| 3047 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 3048 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 3049 | assert(CE->getOpcode() == Instruction::BitCast && |
| 3050 | isa<Function>(CE->getOperand(0)) && |
| 3051 | "Personality should be a function"); |
| 3052 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 3053 | |
| 3054 | // Gather all the type infos for this landing pad and pass them along to |
| 3055 | // MachineModuleInfo. |
| 3056 | std::vector<GlobalVariable *> TyInfo; |
| 3057 | unsigned N = I.getNumOperands(); |
| 3058 | |
| 3059 | for (unsigned i = N - 1; i > 2; --i) { |
| 3060 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 3061 | unsigned FilterLength = CI->getZExtValue(); |
| 3062 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
| 3063 | assert (FirstCatch <= N && "Invalid filter length"); |
| 3064 | |
| 3065 | if (FirstCatch < N) { |
| 3066 | TyInfo.reserve(N - FirstCatch); |
| 3067 | for (unsigned j = FirstCatch; j < N; ++j) |
| 3068 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3069 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3070 | TyInfo.clear(); |
| 3071 | } |
| 3072 | |
| 3073 | if (!FilterLength) { |
| 3074 | // Cleanup. |
| 3075 | MMI->addCleanup(MBB); |
| 3076 | } else { |
| 3077 | // Filter. |
| 3078 | TyInfo.reserve(FilterLength - 1); |
| 3079 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 3080 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3081 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 3082 | TyInfo.clear(); |
| 3083 | } |
| 3084 | |
| 3085 | N = i; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | if (N > 3) { |
| 3090 | TyInfo.reserve(N - 3); |
| 3091 | for (unsigned j = 3; j < N; ++j) |
| 3092 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3093 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | } |
| 3098 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3099 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3100 | /// number with exponent of 1: |
| 3101 | /// |
| 3102 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3103 | /// |
| 3104 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3105 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3106 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
| 3107 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3108 | DAG.getConstant(0x007fffff, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3109 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3110 | DAG.getConstant(0x3f800000, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3111 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3114 | /// GetExponent - Get the exponent: |
| 3115 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3116 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3117 | /// |
| 3118 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3119 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3120 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
| 3121 | DebugLoc dl) { |
| 3122 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3123 | DAG.getConstant(0x7f800000, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3124 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3125 | DAG.getConstant(23, TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3126 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3127 | DAG.getConstant(127, MVT::i32)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3128 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3131 | /// getF32Constant - Get 32-bit floating point constant. |
| 3132 | static SDValue |
| 3133 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
| 3134 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
| 3135 | } |
| 3136 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3137 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3138 | /// visitIntrinsicCall: I is a call instruction |
| 3139 | /// Op is the associated NodeType for I |
| 3140 | const char * |
| 3141 | SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3142 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3143 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3144 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3145 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3146 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3147 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3148 | getValue(I.getOperand(2)), |
| 3149 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3150 | setValue(&I, L); |
| 3151 | DAG.setRoot(L.getValue(1)); |
| 3152 | return 0; |
| 3153 | } |
| 3154 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3155 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3156 | const char * |
| 3157 | SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3158 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3159 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3160 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3161 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
| 3162 | SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3163 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3164 | setValue(&I, Result); |
| 3165 | return 0; |
| 3166 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3167 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3168 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 3169 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3170 | void |
| 3171 | SelectionDAGLowering::visitExp(CallInst &I) { |
| 3172 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3173 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3174 | |
| 3175 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3176 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3177 | SDValue Op = getValue(I.getOperand(1)); |
| 3178 | |
| 3179 | // Put the exponent in the right bit position for later addition to the |
| 3180 | // final result: |
| 3181 | // |
| 3182 | // #define LOG2OFe 1.4426950f |
| 3183 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3184 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3185 | getF32Constant(DAG, 0x3fb8aa3b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3186 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3187 | |
| 3188 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3189 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3190 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3191 | |
| 3192 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3193 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3194 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3195 | |
| 3196 | if (LimitFloatPrecision <= 6) { |
| 3197 | // For floating-point precision of 6: |
| 3198 | // |
| 3199 | // TwoToFractionalPartOfX = |
| 3200 | // 0.997535578f + |
| 3201 | // (0.735607626f + 0.252464424f * x) * x; |
| 3202 | // |
| 3203 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3204 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3205 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3206 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3207 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3208 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3209 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3210 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3211 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3212 | |
| 3213 | // Add the exponent into the result in integer domain. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3214 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3215 | TwoToFracPartOfX, IntegerPartOfX); |
| 3216 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3217 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3218 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3219 | // For floating-point precision of 12: |
| 3220 | // |
| 3221 | // TwoToFractionalPartOfX = |
| 3222 | // 0.999892986f + |
| 3223 | // (0.696457318f + |
| 3224 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3225 | // |
| 3226 | // 0.000107046256 error, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3227 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3228 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3229 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3230 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3231 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3232 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3233 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3234 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3235 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3236 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3237 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
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 t8 = 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, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3244 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3245 | // For floating-point precision of 18: |
| 3246 | // |
| 3247 | // TwoToFractionalPartOfX = |
| 3248 | // 0.999999982f + |
| 3249 | // (0.693148872f + |
| 3250 | // (0.240227044f + |
| 3251 | // (0.554906021e-1f + |
| 3252 | // (0.961591928e-2f + |
| 3253 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3254 | // |
| 3255 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3256 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3257 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3258 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3259 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3260 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3261 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3262 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3263 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3264 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3265 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3266 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3267 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3268 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3269 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3270 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3271 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3272 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3273 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3274 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3275 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3276 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3277 | |
| 3278 | // Add the exponent into the result in integer domain. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3279 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3280 | TwoToFracPartOfX, IntegerPartOfX); |
| 3281 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3282 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3283 | } |
| 3284 | } else { |
| 3285 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3286 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3287 | getValue(I.getOperand(1)).getValueType(), |
| 3288 | getValue(I.getOperand(1))); |
| 3289 | } |
| 3290 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3291 | setValue(&I, result); |
| 3292 | } |
| 3293 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3294 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3295 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3296 | void |
| 3297 | SelectionDAGLowering::visitLog(CallInst &I) { |
| 3298 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3299 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3300 | |
| 3301 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3302 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3303 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3304 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3305 | |
| 3306 | // Scale the exponent by log(2) [0.69314718f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3307 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3308 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3309 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3310 | |
| 3311 | // Get the significand and build it into a floating-point number with |
| 3312 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3313 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3314 | |
| 3315 | if (LimitFloatPrecision <= 6) { |
| 3316 | // For floating-point precision of 6: |
| 3317 | // |
| 3318 | // LogofMantissa = |
| 3319 | // -1.1609546f + |
| 3320 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3321 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3322 | // error 0.0034276066, which is better than 8 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3323 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3324 | getF32Constant(DAG, 0xbe74c456)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3325 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3326 | getF32Constant(DAG, 0x3fb3a2b1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3327 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3328 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3329 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3330 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3331 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3332 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3333 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3334 | // For floating-point precision of 12: |
| 3335 | // |
| 3336 | // LogOfMantissa = |
| 3337 | // -1.7417939f + |
| 3338 | // (2.8212026f + |
| 3339 | // (-1.4699568f + |
| 3340 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3341 | // |
| 3342 | // error 0.000061011436, which is 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3343 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3344 | getF32Constant(DAG, 0xbd67b6d6)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3345 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3346 | getF32Constant(DAG, 0x3ee4f4b8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3347 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3348 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3349 | getF32Constant(DAG, 0x3fbc278b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3350 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3351 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3352 | getF32Constant(DAG, 0x40348e95)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3353 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3354 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3355 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3356 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3357 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3358 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3359 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3360 | // For floating-point precision of 18: |
| 3361 | // |
| 3362 | // LogOfMantissa = |
| 3363 | // -2.1072184f + |
| 3364 | // (4.2372794f + |
| 3365 | // (-3.7029485f + |
| 3366 | // (2.2781945f + |
| 3367 | // (-0.87823314f + |
| 3368 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3369 | // |
| 3370 | // error 0.0000023660568, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3371 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3372 | getF32Constant(DAG, 0xbc91e5ac)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3373 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3374 | getF32Constant(DAG, 0x3e4350aa)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3375 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3376 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3377 | getF32Constant(DAG, 0x3f60d3e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3378 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3379 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3380 | getF32Constant(DAG, 0x4011cdf0)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3381 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3382 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3383 | getF32Constant(DAG, 0x406cfd1c)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3384 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3385 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3386 | getF32Constant(DAG, 0x408797cb)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3387 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3388 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3389 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3390 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3391 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3392 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } else { |
| 3395 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3396 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3397 | getValue(I.getOperand(1)).getValueType(), |
| 3398 | getValue(I.getOperand(1))); |
| 3399 | } |
| 3400 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3401 | setValue(&I, result); |
| 3402 | } |
| 3403 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3404 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3405 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3406 | void |
| 3407 | SelectionDAGLowering::visitLog2(CallInst &I) { |
| 3408 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3409 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3410 | |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3411 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3412 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3413 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3414 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3415 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3416 | // Get the exponent. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3417 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3420 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3421 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3422 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3423 | // Different possible minimax approximations of significand in |
| 3424 | // floating-point for various degrees of accuracy over [1,2]. |
| 3425 | if (LimitFloatPrecision <= 6) { |
| 3426 | // For floating-point precision of 6: |
| 3427 | // |
| 3428 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3429 | // |
| 3430 | // error 0.0049451742, which is more than 7 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3431 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3432 | getF32Constant(DAG, 0xbeb08fe0)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3433 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3434 | getF32Constant(DAG, 0x40019463)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3435 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3436 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3437 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3438 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3439 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3440 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3441 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3442 | // For floating-point precision of 12: |
| 3443 | // |
| 3444 | // Log2ofMantissa = |
| 3445 | // -2.51285454f + |
| 3446 | // (4.07009056f + |
| 3447 | // (-2.12067489f + |
| 3448 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3449 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3450 | // error 0.0000876136000, which is better than 13 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3451 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3452 | getF32Constant(DAG, 0xbda7262e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3453 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3454 | getF32Constant(DAG, 0x3f25280b)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3455 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3456 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3457 | getF32Constant(DAG, 0x4007b923)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3458 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3459 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3460 | getF32Constant(DAG, 0x40823e2f)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3461 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3462 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3463 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3464 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3465 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3466 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3467 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3468 | // For floating-point precision of 18: |
| 3469 | // |
| 3470 | // Log2ofMantissa = |
| 3471 | // -3.0400495f + |
| 3472 | // (6.1129976f + |
| 3473 | // (-5.3420409f + |
| 3474 | // (3.2865683f + |
| 3475 | // (-1.2669343f + |
| 3476 | // (0.27515199f - |
| 3477 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3478 | // |
| 3479 | // error 0.0000018516, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3480 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3481 | getF32Constant(DAG, 0xbcd2769e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3482 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3483 | getF32Constant(DAG, 0x3e8ce0b9)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3484 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3485 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3486 | getF32Constant(DAG, 0x3fa22ae7)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3487 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3488 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3489 | getF32Constant(DAG, 0x40525723)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3490 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3491 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3492 | getF32Constant(DAG, 0x40aaf200)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3493 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3494 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3495 | getF32Constant(DAG, 0x40c39dad)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3496 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3497 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3498 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3499 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3500 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3501 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3502 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3503 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3504 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3505 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3506 | getValue(I.getOperand(1)).getValueType(), |
| 3507 | getValue(I.getOperand(1))); |
| 3508 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3509 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3510 | setValue(&I, result); |
| 3511 | } |
| 3512 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3513 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3514 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3515 | void |
| 3516 | SelectionDAGLowering::visitLog10(CallInst &I) { |
| 3517 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3518 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3519 | |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3520 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3521 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3522 | SDValue Op = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3523 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3524 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3525 | // Scale the exponent by log10(2) [0.30102999f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3526 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3527 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3528 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3529 | |
| 3530 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3531 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3532 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3533 | |
| 3534 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3535 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3536 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3537 | // Log10ofMantissa = |
| 3538 | // -0.50419619f + |
| 3539 | // (0.60948995f - 0.10380950f * x) * x; |
| 3540 | // |
| 3541 | // error 0.0014886165, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3542 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3543 | getF32Constant(DAG, 0xbdd49a13)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3544 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3545 | getF32Constant(DAG, 0x3f1c0789)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3546 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3547 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3548 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3549 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3550 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3551 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3552 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3553 | // For floating-point precision of 12: |
| 3554 | // |
| 3555 | // Log10ofMantissa = |
| 3556 | // -0.64831180f + |
| 3557 | // (0.91751397f + |
| 3558 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3559 | // |
| 3560 | // error 0.00019228036, which is better than 12 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3561 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3562 | getF32Constant(DAG, 0x3d431f31)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3563 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3564 | getF32Constant(DAG, 0x3ea21fb2)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3565 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3566 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3567 | getF32Constant(DAG, 0x3f6ae232)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3568 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3569 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3570 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3571 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3572 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3573 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3574 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3575 | // For floating-point precision of 18: |
| 3576 | // |
| 3577 | // Log10ofMantissa = |
| 3578 | // -0.84299375f + |
| 3579 | // (1.5327582f + |
| 3580 | // (-1.0688956f + |
| 3581 | // (0.49102474f + |
| 3582 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3583 | // |
| 3584 | // error 0.0000037995730, which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3585 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3586 | getF32Constant(DAG, 0x3c5d51ce)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3587 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3588 | getF32Constant(DAG, 0x3e00685a)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3589 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3590 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3591 | getF32Constant(DAG, 0x3efb6798)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3592 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3593 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3594 | getF32Constant(DAG, 0x3f88d192)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3595 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3596 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3597 | getF32Constant(DAG, 0x3fc4316c)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3598 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3599 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3600 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3601 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3602 | result = DAG.getNode(ISD::FADD, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3603 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3604 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3605 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3606 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3607 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3608 | getValue(I.getOperand(1)).getValueType(), |
| 3609 | getValue(I.getOperand(1))); |
| 3610 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3611 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3612 | setValue(&I, result); |
| 3613 | } |
| 3614 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3615 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3616 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3617 | void |
| 3618 | SelectionDAGLowering::visitExp2(CallInst &I) { |
| 3619 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3620 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3621 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3622 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3623 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3624 | SDValue Op = getValue(I.getOperand(1)); |
| 3625 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3626 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3627 | |
| 3628 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3629 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3630 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3631 | |
| 3632 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3633 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3634 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3635 | |
| 3636 | if (LimitFloatPrecision <= 6) { |
| 3637 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3638 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3639 | // TwoToFractionalPartOfX = |
| 3640 | // 0.997535578f + |
| 3641 | // (0.735607626f + 0.252464424f * x) * x; |
| 3642 | // |
| 3643 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3644 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3645 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3646 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3647 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3648 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3649 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3650 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3651 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3652 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3653 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3654 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3655 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3656 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3657 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3658 | // For floating-point precision of 12: |
| 3659 | // |
| 3660 | // TwoToFractionalPartOfX = |
| 3661 | // 0.999892986f + |
| 3662 | // (0.696457318f + |
| 3663 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3664 | // |
| 3665 | // error 0.000107046256, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3666 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3667 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3668 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3669 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3670 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3671 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3672 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3673 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3674 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3675 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3676 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3677 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3678 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3679 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3680 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3681 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3682 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3683 | // For floating-point precision of 18: |
| 3684 | // |
| 3685 | // TwoToFractionalPartOfX = |
| 3686 | // 0.999999982f + |
| 3687 | // (0.693148872f + |
| 3688 | // (0.240227044f + |
| 3689 | // (0.554906021e-1f + |
| 3690 | // (0.961591928e-2f + |
| 3691 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3692 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3693 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3694 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3695 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3696 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3697 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3698 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3699 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3700 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3701 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3702 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3703 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3704 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3705 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3706 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3707 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3708 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3709 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3710 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3711 | getF32Constant(DAG, 0x3f800000)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3712 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3713 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3714 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3715 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3716 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3717 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3718 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3719 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3720 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3721 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3722 | getValue(I.getOperand(1)).getValueType(), |
| 3723 | getValue(I.getOperand(1))); |
| 3724 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3725 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3726 | setValue(&I, result); |
| 3727 | } |
| 3728 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3729 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3730 | /// limited-precision mode with x == 10.0f. |
| 3731 | void |
| 3732 | SelectionDAGLowering::visitPow(CallInst &I) { |
| 3733 | SDValue result; |
| 3734 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3735 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3736 | bool IsExp10 = false; |
| 3737 | |
| 3738 | if (getValue(Val).getValueType() == MVT::f32 && |
Bill Wendling | 277fc24 | 2008-09-10 00:24:59 +0000 | [diff] [blame] | 3739 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3740 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3741 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3742 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3743 | APFloat Ten(10.0f); |
| 3744 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3745 | } |
| 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3750 | SDValue Op = getValue(I.getOperand(2)); |
| 3751 | |
| 3752 | // Put the exponent in the right bit position for later addition to the |
| 3753 | // final result: |
| 3754 | // |
| 3755 | // #define LOG2OF10 3.3219281f |
| 3756 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3757 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3758 | getF32Constant(DAG, 0x40549a78)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3759 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3760 | |
| 3761 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3762 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3763 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3764 | |
| 3765 | // IntegerPartOfX <<= 23; |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3766 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3767 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3768 | |
| 3769 | if (LimitFloatPrecision <= 6) { |
| 3770 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3771 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3772 | // twoToFractionalPartOfX = |
| 3773 | // 0.997535578f + |
| 3774 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3775 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3776 | // error 0.0144103317, which is 6 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3777 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3778 | getF32Constant(DAG, 0x3e814304)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3779 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3780 | getF32Constant(DAG, 0x3f3c50c8)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3781 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3782 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3783 | getF32Constant(DAG, 0x3f7f5e7e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3784 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3785 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3786 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3787 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3788 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3789 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3790 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3791 | // For floating-point precision of 12: |
| 3792 | // |
| 3793 | // TwoToFractionalPartOfX = |
| 3794 | // 0.999892986f + |
| 3795 | // (0.696457318f + |
| 3796 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3797 | // |
| 3798 | // error 0.000107046256, which is 13 to 14 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3799 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3800 | getF32Constant(DAG, 0x3da235e3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3801 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3802 | getF32Constant(DAG, 0x3e65b8f3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3803 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3804 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3805 | getF32Constant(DAG, 0x3f324b07)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3806 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3807 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3808 | getF32Constant(DAG, 0x3f7ff8fd)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3809 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3810 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3811 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3812 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3813 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3814 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3815 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3816 | // For floating-point precision of 18: |
| 3817 | // |
| 3818 | // TwoToFractionalPartOfX = |
| 3819 | // 0.999999982f + |
| 3820 | // (0.693148872f + |
| 3821 | // (0.240227044f + |
| 3822 | // (0.554906021e-1f + |
| 3823 | // (0.961591928e-2f + |
| 3824 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3825 | // error 2.47208000*10^(-7), which is better than 18 bits |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3826 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3827 | getF32Constant(DAG, 0x3924b03e)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3828 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3829 | getF32Constant(DAG, 0x3ab24b87)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3830 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3831 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3832 | getF32Constant(DAG, 0x3c1d8c17)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3833 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3834 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3835 | getF32Constant(DAG, 0x3d634a1d)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3836 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3837 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3838 | getF32Constant(DAG, 0x3e75fe14)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3839 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3840 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3841 | getF32Constant(DAG, 0x3f317234)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3842 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3843 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3844 | getF32Constant(DAG, 0x3f800000)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3845 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3846 | SDValue TwoToFractionalPartOfX = |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3847 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3848 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3849 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3850 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3851 | } |
| 3852 | } else { |
| 3853 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3854 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3855 | getValue(I.getOperand(1)).getValueType(), |
| 3856 | getValue(I.getOperand(1)), |
| 3857 | getValue(I.getOperand(2))); |
| 3858 | } |
| 3859 | |
| 3860 | setValue(&I, result); |
| 3861 | } |
| 3862 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3863 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3864 | /// we want to emit this as a call to a named external function, return the name |
| 3865 | /// otherwise lower it and return null. |
| 3866 | const char * |
| 3867 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3868 | DebugLoc dl = getCurDebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3869 | switch (Intrinsic) { |
| 3870 | default: |
| 3871 | // By default, turn this into a target intrinsic node. |
| 3872 | visitTargetIntrinsic(I, Intrinsic); |
| 3873 | return 0; |
| 3874 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3875 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3876 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3877 | case Intrinsic::returnaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3878 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3879 | getValue(I.getOperand(1)))); |
| 3880 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3881 | case Intrinsic::frameaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3882 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3883 | getValue(I.getOperand(1)))); |
| 3884 | return 0; |
| 3885 | case Intrinsic::setjmp: |
| 3886 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3887 | break; |
| 3888 | case Intrinsic::longjmp: |
| 3889 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3890 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3891 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3892 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3893 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3894 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3895 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3896 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3897 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3898 | return 0; |
| 3899 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3900 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3901 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3902 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3903 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3904 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3905 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3906 | I.getOperand(1), 0)); |
| 3907 | return 0; |
| 3908 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3909 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3910 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3911 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3912 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3913 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3914 | |
| 3915 | // If the source and destination are known to not be aliases, we can |
| 3916 | // lower memmove as memcpy. |
| 3917 | uint64_t Size = -1ULL; |
| 3918 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3919 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3920 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3921 | AliasAnalysis::NoAlias) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3922 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3923 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3924 | return 0; |
| 3925 | } |
| 3926 | |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3927 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3928 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3929 | return 0; |
| 3930 | } |
| 3931 | case Intrinsic::dbg_stoppoint: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3932 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3933 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3934 | if (DW && DW->ValidDebugInfo(SPI.getContext(), Fast)) { |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3935 | MachineFunction &MF = DAG.getMachineFunction(); |
Dale Johannesen | beaec4c | 2009-03-25 17:36:08 +0000 | [diff] [blame] | 3936 | if (Fast) |
| 3937 | DAG.setRoot(DAG.getDbgStopPoint(getRoot(), |
| 3938 | SPI.getLine(), |
| 3939 | SPI.getColumn(), |
| 3940 | SPI.getContext())); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3941 | DICompileUnit CU(cast<GlobalVariable>(SPI.getContext())); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3942 | std::string Dir, FN; |
| 3943 | unsigned SrcFile = DW->getOrCreateSourceID(CU.getDirectory(Dir), |
| 3944 | CU.getFilename(FN)); |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3945 | unsigned idx = MF.getOrCreateDebugLocID(SrcFile, |
| 3946 | SPI.getLine(), SPI.getColumn()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3947 | setCurDebugLoc(DebugLoc::get(idx)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3948 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3949 | return 0; |
| 3950 | } |
| 3951 | case Intrinsic::dbg_region_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3952 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3953 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3954 | if (DW && DW->ValidDebugInfo(RSI.getContext(), Fast)) { |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3955 | unsigned LabelID = |
| 3956 | DW->RecordRegionStart(cast<GlobalVariable>(RSI.getContext())); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3957 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3958 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3959 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3960 | |
| 3961 | return 0; |
| 3962 | } |
| 3963 | case Intrinsic::dbg_region_end: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3964 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3965 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3966 | if (DW && DW->ValidDebugInfo(REI.getContext(), Fast)) { |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3967 | |
| 3968 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3969 | DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext())); |
| 3970 | std::string SPName; |
| 3971 | Subprogram.getLinkageName(SPName); |
| 3972 | if (!SPName.empty() |
| 3973 | && strcmp(SPName.c_str(), MF.getFunction()->getNameStart())) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3974 | // This is end of inlined function. Debugging information for |
| 3975 | // inlined function is not handled yet (only supported by FastISel). |
| 3976 | if (Fast) { |
| 3977 | unsigned ID = DW->RecordInlinedFnEnd(Subprogram); |
| 3978 | if (ID != 0) |
Devang Patel | 02f8c41 | 2009-04-16 17:55:30 +0000 | [diff] [blame] | 3979 | // Returned ID is 0 if this is unbalanced "end of inlined |
| 3980 | // scope". This could happen if optimizer eats dbg intrinsics |
| 3981 | // or "beginning of inlined scope" is not recoginized due to |
| 3982 | // missing location info. In such cases, do ignore this region.end. |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3983 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3984 | getRoot(), ID)); |
| 3985 | } |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3989 | unsigned LabelID = |
| 3990 | DW->RecordRegionEnd(cast<GlobalVariable>(REI.getContext())); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3991 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3992 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3993 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3994 | |
| 3995 | return 0; |
| 3996 | } |
| 3997 | case Intrinsic::dbg_func_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3998 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3999 | if (!DW) return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4000 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
| 4001 | Value *SP = FSI.getSubprogram(); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 4002 | if (SP && DW->ValidDebugInfo(SP, Fast)) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4003 | MachineFunction &MF = DAG.getMachineFunction(); |
Bill Wendling | 5aa4977 | 2009-02-24 02:35:30 +0000 | [diff] [blame] | 4004 | if (Fast) { |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4005 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is what |
| 4006 | // (most?) gdb expects. |
| 4007 | DebugLoc PrevLoc = CurDebugLoc; |
| 4008 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 4009 | DICompileUnit CompileUnit = Subprogram.getCompileUnit(); |
| 4010 | std::string Dir, FN; |
| 4011 | unsigned SrcFile = DW->getOrCreateSourceID(CompileUnit.getDirectory(Dir), |
| 4012 | CompileUnit.getFilename(FN)); |
| 4013 | |
| 4014 | if (!Subprogram.describes(MF.getFunction())) { |
| 4015 | // This is a beginning of an inlined function. |
| 4016 | |
Devang Patel | 02f8c41 | 2009-04-16 17:55:30 +0000 | [diff] [blame] | 4017 | // If llvm.dbg.func.start is seen in a new block before any |
| 4018 | // llvm.dbg.stoppoint intrinsic then the location info is unknown. |
| 4019 | // FIXME : Why DebugLoc is reset at the beginning of each block ? |
| 4020 | if (PrevLoc.isUnknown()) |
| 4021 | return 0; |
| 4022 | |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4023 | // Record the source line. |
| 4024 | unsigned Line = Subprogram.getLineNumber(); |
| 4025 | unsigned LabelID = DW->RecordSourceLine(Line, 0, SrcFile); |
| 4026 | setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(SrcFile, Line, 0))); |
| 4027 | |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 4028 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 4029 | getRoot(), LabelID)); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4030 | DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc); |
| 4031 | DW->RecordInlinedFnStart(&FSI, Subprogram, LabelID, |
| 4032 | PrevLocTpl.Src, |
| 4033 | PrevLocTpl.Line, |
| 4034 | PrevLocTpl.Col); |
| 4035 | } else { |
| 4036 | // Record the source line. |
| 4037 | unsigned Line = Subprogram.getLineNumber(); |
| 4038 | setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(SrcFile, Line, 0))); |
Devang Patel | 906caf2 | 2009-04-16 15:07:09 +0000 | [diff] [blame] | 4039 | DW->RecordSourceLine(Line, 0, SrcFile); |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 4040 | // llvm.dbg.func_start also defines beginning of function scope. |
| 4041 | DW->RecordRegionStart(cast<GlobalVariable>(FSI.getSubprogram())); |
| 4042 | } |
| 4043 | } else { |
| 4044 | DISubprogram Subprogram(cast<GlobalVariable>(SP)); |
| 4045 | |
| 4046 | std::string SPName; |
| 4047 | Subprogram.getLinkageName(SPName); |
| 4048 | if (!SPName.empty() |
| 4049 | && strcmp(SPName.c_str(), MF.getFunction()->getNameStart())) { |
| 4050 | // This is beginning of inlined function. Debugging information for |
| 4051 | // inlined function is not handled yet (only supported by FastISel). |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
| 4055 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is |
| 4056 | // what (most?) gdb expects. |
| 4057 | DICompileUnit CompileUnit = Subprogram.getCompileUnit(); |
| 4058 | std::string Dir, FN; |
| 4059 | unsigned SrcFile = DW->getOrCreateSourceID(CompileUnit.getDirectory(Dir), |
| 4060 | CompileUnit.getFilename(FN)); |
| 4061 | |
| 4062 | // Record the source line but does not create a label for the normal |
| 4063 | // function start. It will be emitted at asm emission time. However, |
| 4064 | // create a label if this is a beginning of inlined function. |
| 4065 | unsigned Line = Subprogram.getLineNumber(); |
| 4066 | setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(SrcFile, Line, 0))); |
| 4067 | // FIXME - Start new region because llvm.dbg.func_start also defines |
| 4068 | // beginning of function scope. |
| 4069 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | return 0; |
| 4073 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4074 | case Intrinsic::dbg_declare: { |
Bill Wendling | 5aa4977 | 2009-02-24 02:35:30 +0000 | [diff] [blame] | 4075 | if (Fast) { |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 4076 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 4077 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 4078 | Value *Variable = DI.getVariable(); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 4079 | if (DW && DW->ValidDebugInfo(Variable, Fast)) |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 4080 | DAG.setRoot(DAG.getNode(ISD::DECLARE, dl, MVT::Other, getRoot(), |
| 4081 | getValue(DI.getAddress()), getValue(Variable))); |
| 4082 | } else { |
| 4083 | // FIXME: Do something sensible here when we support debug declare. |
| 4084 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4085 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4086 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4087 | case Intrinsic::eh_exception: { |
| 4088 | if (!CurMBB->isLandingPad()) { |
| 4089 | // FIXME: Mark exception register as live in. Hack for PR1508. |
| 4090 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 4091 | if (Reg) CurMBB->addLiveIn(Reg); |
| 4092 | } |
| 4093 | // Insert the EXCEPTIONADDR instruction. |
| 4094 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 4095 | SDValue Ops[1]; |
| 4096 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4097 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4098 | setValue(&I, Op); |
| 4099 | DAG.setRoot(Op.getValue(1)); |
| 4100 | return 0; |
| 4101 | } |
| 4102 | |
| 4103 | case Intrinsic::eh_selector_i32: |
| 4104 | case Intrinsic::eh_selector_i64: { |
| 4105 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4106 | MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ? |
| 4107 | MVT::i32 : MVT::i64); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4109 | if (MMI) { |
| 4110 | if (CurMBB->isLandingPad()) |
| 4111 | AddCatchInfo(I, MMI, CurMBB); |
| 4112 | else { |
| 4113 | #ifndef NDEBUG |
| 4114 | FuncInfo.CatchInfoLost.insert(&I); |
| 4115 | #endif |
| 4116 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 4117 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 4118 | if (Reg) CurMBB->addLiveIn(Reg); |
| 4119 | } |
| 4120 | |
| 4121 | // Insert the EHSELECTION instruction. |
| 4122 | SDVTList VTs = DAG.getVTList(VT, MVT::Other); |
| 4123 | SDValue Ops[2]; |
| 4124 | Ops[0] = getValue(I.getOperand(1)); |
| 4125 | Ops[1] = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4126 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4127 | setValue(&I, Op); |
| 4128 | DAG.setRoot(Op.getValue(1)); |
| 4129 | } else { |
| 4130 | setValue(&I, DAG.getConstant(0, VT)); |
| 4131 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4132 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | case Intrinsic::eh_typeid_for_i32: |
| 4137 | case Intrinsic::eh_typeid_for_i64: { |
| 4138 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4139 | MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ? |
| 4140 | MVT::i32 : MVT::i64); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4141 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4142 | if (MMI) { |
| 4143 | // Find the type id for the given typeinfo. |
| 4144 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 4145 | |
| 4146 | unsigned TypeID = MMI->getTypeIDFor(GV); |
| 4147 | setValue(&I, DAG.getConstant(TypeID, VT)); |
| 4148 | } else { |
| 4149 | // Return something different to eh_selector. |
| 4150 | setValue(&I, DAG.getConstant(1, VT)); |
| 4151 | } |
| 4152 | |
| 4153 | return 0; |
| 4154 | } |
| 4155 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4156 | case Intrinsic::eh_return_i32: |
| 4157 | case Intrinsic::eh_return_i64: |
| 4158 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4159 | MMI->setCallsEHReturn(true); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4160 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4161 | MVT::Other, |
| 4162 | getControlRoot(), |
| 4163 | getValue(I.getOperand(1)), |
| 4164 | getValue(I.getOperand(2)))); |
| 4165 | } else { |
| 4166 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 4167 | } |
| 4168 | |
| 4169 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4170 | case Intrinsic::eh_unwind_init: |
| 4171 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 4172 | MMI->setCallsUnwindInit(true); |
| 4173 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4174 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4175 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4176 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4177 | case Intrinsic::eh_dwarf_cfa: { |
| 4178 | MVT VT = getValue(I.getOperand(1)).getValueType(); |
| 4179 | SDValue CfaArg; |
| 4180 | if (VT.bitsGT(TLI.getPointerTy())) |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4181 | CfaArg = DAG.getNode(ISD::TRUNCATE, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4182 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
| 4183 | else |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4184 | CfaArg = DAG.getNode(ISD::SIGN_EXTEND, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4185 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4186 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4187 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4188 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4189 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4190 | TLI.getPointerTy()), |
| 4191 | CfaArg); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4192 | setValue(&I, DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4193 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4194 | DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4195 | TLI.getPointerTy(), |
| 4196 | DAG.getConstant(0, |
| 4197 | TLI.getPointerTy())), |
| 4198 | Offset)); |
| 4199 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4202 | case Intrinsic::convertff: |
| 4203 | case Intrinsic::convertfsi: |
| 4204 | case Intrinsic::convertfui: |
| 4205 | case Intrinsic::convertsif: |
| 4206 | case Intrinsic::convertuif: |
| 4207 | case Intrinsic::convertss: |
| 4208 | case Intrinsic::convertsu: |
| 4209 | case Intrinsic::convertus: |
| 4210 | case Intrinsic::convertuu: { |
| 4211 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4212 | switch (Intrinsic) { |
| 4213 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4214 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4215 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4216 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4217 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4218 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4219 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4220 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4221 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4222 | } |
| 4223 | MVT DestVT = TLI.getValueType(I.getType()); |
| 4224 | Value* Op1 = I.getOperand(1); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4225 | setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4226 | DAG.getValueType(DestVT), |
| 4227 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4228 | getValue(I.getOperand(2)), |
| 4229 | getValue(I.getOperand(3)), |
| 4230 | Code)); |
| 4231 | return 0; |
| 4232 | } |
| 4233 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4234 | case Intrinsic::sqrt: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4235 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4236 | getValue(I.getOperand(1)).getValueType(), |
| 4237 | getValue(I.getOperand(1)))); |
| 4238 | return 0; |
| 4239 | case Intrinsic::powi: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4240 | setValue(&I, DAG.getNode(ISD::FPOWI, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4241 | getValue(I.getOperand(1)).getValueType(), |
| 4242 | getValue(I.getOperand(1)), |
| 4243 | getValue(I.getOperand(2)))); |
| 4244 | return 0; |
| 4245 | case Intrinsic::sin: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4246 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4247 | getValue(I.getOperand(1)).getValueType(), |
| 4248 | getValue(I.getOperand(1)))); |
| 4249 | return 0; |
| 4250 | case Intrinsic::cos: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4251 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4252 | getValue(I.getOperand(1)).getValueType(), |
| 4253 | getValue(I.getOperand(1)))); |
| 4254 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4255 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4256 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4257 | return 0; |
| 4258 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4259 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4260 | return 0; |
| 4261 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4262 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4263 | return 0; |
| 4264 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4265 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4266 | return 0; |
| 4267 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4268 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4269 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4270 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4271 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4272 | return 0; |
| 4273 | case Intrinsic::pcmarker: { |
| 4274 | SDValue Tmp = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4275 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4276 | return 0; |
| 4277 | } |
| 4278 | case Intrinsic::readcyclecounter: { |
| 4279 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4280 | SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4281 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4282 | &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::part_select: { |
| 4288 | // Currently not implemented: just abort |
| 4289 | assert(0 && "part_select intrinsic not implemented"); |
| 4290 | abort(); |
| 4291 | } |
| 4292 | case Intrinsic::part_set: { |
| 4293 | // Currently not implemented: just abort |
| 4294 | assert(0 && "part_set intrinsic not implemented"); |
| 4295 | abort(); |
| 4296 | } |
| 4297 | case Intrinsic::bswap: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4298 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4299 | getValue(I.getOperand(1)).getValueType(), |
| 4300 | getValue(I.getOperand(1)))); |
| 4301 | return 0; |
| 4302 | case Intrinsic::cttz: { |
| 4303 | SDValue Arg = getValue(I.getOperand(1)); |
| 4304 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4305 | SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4306 | setValue(&I, result); |
| 4307 | return 0; |
| 4308 | } |
| 4309 | case Intrinsic::ctlz: { |
| 4310 | SDValue Arg = getValue(I.getOperand(1)); |
| 4311 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4312 | SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4313 | setValue(&I, result); |
| 4314 | return 0; |
| 4315 | } |
| 4316 | case Intrinsic::ctpop: { |
| 4317 | SDValue Arg = getValue(I.getOperand(1)); |
| 4318 | MVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4319 | SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4320 | setValue(&I, result); |
| 4321 | return 0; |
| 4322 | } |
| 4323 | case Intrinsic::stacksave: { |
| 4324 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4325 | SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4326 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4327 | setValue(&I, Tmp); |
| 4328 | DAG.setRoot(Tmp.getValue(1)); |
| 4329 | return 0; |
| 4330 | } |
| 4331 | case Intrinsic::stackrestore: { |
| 4332 | SDValue Tmp = getValue(I.getOperand(1)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4333 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4334 | return 0; |
| 4335 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4336 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4337 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4338 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4339 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 4340 | MVT PtrTy = TLI.getPointerTy(); |
| 4341 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4342 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4343 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4344 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4345 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4346 | MFI->setStackProtectorIndex(FI); |
| 4347 | |
| 4348 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4349 | |
| 4350 | // Store the stack protector onto the stack. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4351 | SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4352 | PseudoSourceValue::getFixedStack(FI), |
| 4353 | 0, true); |
| 4354 | setValue(&I, Result); |
| 4355 | DAG.setRoot(Result); |
| 4356 | return 0; |
| 4357 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4358 | case Intrinsic::var_annotation: |
| 4359 | // Discard annotate attributes |
| 4360 | return 0; |
| 4361 | |
| 4362 | case Intrinsic::init_trampoline: { |
| 4363 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4364 | |
| 4365 | SDValue Ops[6]; |
| 4366 | Ops[0] = getRoot(); |
| 4367 | Ops[1] = getValue(I.getOperand(1)); |
| 4368 | Ops[2] = getValue(I.getOperand(2)); |
| 4369 | Ops[3] = getValue(I.getOperand(3)); |
| 4370 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4371 | Ops[5] = DAG.getSrcValue(F); |
| 4372 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4373 | SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl, |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4374 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4375 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4376 | |
| 4377 | setValue(&I, Tmp); |
| 4378 | DAG.setRoot(Tmp.getValue(1)); |
| 4379 | return 0; |
| 4380 | } |
| 4381 | |
| 4382 | case Intrinsic::gcroot: |
| 4383 | if (GFI) { |
| 4384 | Value *Alloca = I.getOperand(1); |
| 4385 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4386 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4387 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4388 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4389 | } |
| 4390 | return 0; |
| 4391 | |
| 4392 | case Intrinsic::gcread: |
| 4393 | case Intrinsic::gcwrite: |
| 4394 | assert(0 && "GC failed to lower gcread/gcwrite intrinsics!"); |
| 4395 | return 0; |
| 4396 | |
| 4397 | case Intrinsic::flt_rounds: { |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4398 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | case Intrinsic::trap: { |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4403 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4404 | return 0; |
| 4405 | } |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4406 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4407 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4408 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4409 | case Intrinsic::sadd_with_overflow: |
| 4410 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4411 | case Intrinsic::usub_with_overflow: |
| 4412 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4413 | case Intrinsic::ssub_with_overflow: |
| 4414 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4415 | case Intrinsic::umul_with_overflow: |
| 4416 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4417 | case Intrinsic::smul_with_overflow: |
| 4418 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4419 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4420 | case Intrinsic::prefetch: { |
| 4421 | SDValue Ops[4]; |
| 4422 | Ops[0] = getRoot(); |
| 4423 | Ops[1] = getValue(I.getOperand(1)); |
| 4424 | Ops[2] = getValue(I.getOperand(2)); |
| 4425 | Ops[3] = getValue(I.getOperand(3)); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4426 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4427 | return 0; |
| 4428 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4429 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4430 | case Intrinsic::memory_barrier: { |
| 4431 | SDValue Ops[6]; |
| 4432 | Ops[0] = getRoot(); |
| 4433 | for (int x = 1; x < 6; ++x) |
| 4434 | Ops[x] = getValue(I.getOperand(x)); |
| 4435 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4436 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4440 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4441 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4442 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4443 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4444 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4445 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4446 | getValue(I.getOperand(2)), |
| 4447 | getValue(I.getOperand(3)), |
| 4448 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4449 | setValue(&I, L); |
| 4450 | DAG.setRoot(L.getValue(1)); |
| 4451 | return 0; |
| 4452 | } |
| 4453 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4454 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4455 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4456 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4457 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4458 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4459 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4460 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4461 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4462 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4463 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4464 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4465 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4466 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4467 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4468 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4469 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4470 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4471 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4472 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4473 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4474 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | |
| 4479 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee, |
| 4480 | bool IsTailCall, |
| 4481 | MachineBasicBlock *LandingPad) { |
| 4482 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4483 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4484 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4485 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4486 | |
| 4487 | TargetLowering::ArgListTy Args; |
| 4488 | TargetLowering::ArgListEntry Entry; |
| 4489 | Args.reserve(CS.arg_size()); |
| 4490 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 4491 | i != e; ++i) { |
| 4492 | SDValue ArgNode = getValue(*i); |
| 4493 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4494 | |
| 4495 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4496 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4497 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4498 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4499 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4500 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4501 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4502 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4503 | Args.push_back(Entry); |
| 4504 | } |
| 4505 | |
| 4506 | if (LandingPad && MMI) { |
| 4507 | // Insert a label before the invoke call to mark the try range. This can be |
| 4508 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4509 | BeginLabel = MMI->NextLabelID(); |
| 4510 | // Both PendingLoads and PendingExports must be flushed here; |
| 4511 | // this call might not return. |
| 4512 | (void)getRoot(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4513 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4514 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | std::pair<SDValue,SDValue> Result = |
| 4518 | TLI.LowerCallTo(getRoot(), CS.getType(), |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4519 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4520 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
| 4521 | CS.paramHasAttr(0, Attribute::InReg), |
| 4522 | CS.getCallingConv(), |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 4523 | IsTailCall && PerformTailCallOpt, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4524 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4525 | if (CS.getType() != Type::VoidTy) |
| 4526 | setValue(CS.getInstruction(), Result.first); |
| 4527 | DAG.setRoot(Result.second); |
| 4528 | |
| 4529 | if (LandingPad && MMI) { |
| 4530 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4531 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4532 | EndLabel = MMI->NextLabelID(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4533 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4534 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4535 | |
| 4536 | // Inform MachineModuleInfo of range. |
| 4537 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | |
| 4542 | void SelectionDAGLowering::visitCall(CallInst &I) { |
| 4543 | const char *RenameFn = 0; |
| 4544 | if (Function *F = I.getCalledFunction()) { |
| 4545 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4546 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4547 | if (II) { |
| 4548 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4549 | RenameFn = visitIntrinsicCall(I, IID); |
| 4550 | if (!RenameFn) |
| 4551 | return; |
| 4552 | } |
| 4553 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4554 | if (unsigned IID = F->getIntrinsicID()) { |
| 4555 | RenameFn = visitIntrinsicCall(I, IID); |
| 4556 | if (!RenameFn) |
| 4557 | return; |
| 4558 | } |
| 4559 | } |
| 4560 | |
| 4561 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4562 | // can't be a library call. |
| 4563 | unsigned NameLen = F->getNameLen(); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 4564 | if (!F->hasLocalLinkage() && NameLen) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4565 | const char *NameStr = F->getNameStart(); |
| 4566 | if (NameStr[0] == 'c' && |
| 4567 | ((NameLen == 8 && !strcmp(NameStr, "copysign")) || |
| 4568 | (NameLen == 9 && !strcmp(NameStr, "copysignf")))) { |
| 4569 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4570 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4571 | I.getType() == I.getOperand(1)->getType() && |
| 4572 | I.getType() == I.getOperand(2)->getType()) { |
| 4573 | SDValue LHS = getValue(I.getOperand(1)); |
| 4574 | SDValue RHS = getValue(I.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4575 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4576 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4577 | return; |
| 4578 | } |
| 4579 | } else if (NameStr[0] == 'f' && |
| 4580 | ((NameLen == 4 && !strcmp(NameStr, "fabs")) || |
| 4581 | (NameLen == 5 && !strcmp(NameStr, "fabsf")) || |
| 4582 | (NameLen == 5 && !strcmp(NameStr, "fabsl")))) { |
| 4583 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4584 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4585 | I.getType() == I.getOperand(1)->getType()) { |
| 4586 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4587 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4588 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4589 | return; |
| 4590 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4591 | } else if (NameStr[0] == 's' && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4592 | ((NameLen == 3 && !strcmp(NameStr, "sin")) || |
| 4593 | (NameLen == 4 && !strcmp(NameStr, "sinf")) || |
| 4594 | (NameLen == 4 && !strcmp(NameStr, "sinl")))) { |
| 4595 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4596 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4597 | I.getType() == I.getOperand(1)->getType()) { |
| 4598 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4599 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4600 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4601 | return; |
| 4602 | } |
| 4603 | } else if (NameStr[0] == 'c' && |
| 4604 | ((NameLen == 3 && !strcmp(NameStr, "cos")) || |
| 4605 | (NameLen == 4 && !strcmp(NameStr, "cosf")) || |
| 4606 | (NameLen == 4 && !strcmp(NameStr, "cosl")))) { |
| 4607 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4608 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4609 | I.getType() == I.getOperand(1)->getType()) { |
| 4610 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4611 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4612 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4613 | return; |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4618 | visitInlineAsm(&I); |
| 4619 | return; |
| 4620 | } |
| 4621 | |
| 4622 | SDValue Callee; |
| 4623 | if (!RenameFn) |
| 4624 | Callee = getValue(I.getOperand(0)); |
| 4625 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4626 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4627 | |
| 4628 | LowerCallTo(&I, Callee, I.isTailCall()); |
| 4629 | } |
| 4630 | |
| 4631 | |
| 4632 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4633 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4634 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4635 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4636 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4637 | SDValue &Chain, |
| 4638 | SDValue *Flag) const { |
| 4639 | // Assemble the legal parts into the final values. |
| 4640 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4641 | SmallVector<SDValue, 8> Parts; |
| 4642 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4643 | // Copy the legal parts from the registers. |
| 4644 | MVT ValueVT = ValueVTs[Value]; |
| 4645 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 4646 | MVT RegisterVT = RegVTs[Value]; |
| 4647 | |
| 4648 | Parts.resize(NumRegs); |
| 4649 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4650 | SDValue P; |
| 4651 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4652 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4653 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4654 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4655 | *Flag = P.getValue(2); |
| 4656 | } |
| 4657 | Chain = P.getValue(1); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4658 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4659 | // If the source register was virtual and if we know something about it, |
| 4660 | // add an assert node. |
| 4661 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4662 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4663 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4664 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4665 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4666 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4667 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4668 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4669 | unsigned NumSignBits = LOI.NumSignBits; |
| 4670 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4671 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4672 | // FIXME: We capture more information than the dag can represent. For |
| 4673 | // now, just use the tightest assertzext/assertsext possible. |
| 4674 | bool isSExt = true; |
| 4675 | MVT FromVT(MVT::Other); |
| 4676 | if (NumSignBits == RegSize) |
| 4677 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 4678 | else if (NumZeroBits >= RegSize-1) |
| 4679 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 4680 | else if (NumSignBits > RegSize-8) |
| 4681 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4682 | else if (NumZeroBits >= RegSize-8) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4683 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 4684 | else if (NumSignBits > RegSize-16) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4685 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4686 | else if (NumZeroBits >= RegSize-16) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4687 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4688 | else if (NumSignBits > RegSize-32) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4689 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4690 | else if (NumZeroBits >= RegSize-32) |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 4691 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4692 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4693 | if (FromVT != MVT::Other) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4694 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4695 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4696 | |
| 4697 | } |
| 4698 | } |
| 4699 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4700 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4701 | Parts[i] = P; |
| 4702 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4703 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4704 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4705 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4706 | Part += NumRegs; |
| 4707 | Parts.clear(); |
| 4708 | } |
| 4709 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4710 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 4711 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4712 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4716 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4717 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4718 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4719 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4720 | SDValue &Chain, SDValue *Flag) const { |
| 4721 | // Get the list of the values's legal parts. |
| 4722 | unsigned NumRegs = Regs.size(); |
| 4723 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4724 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4725 | MVT ValueVT = ValueVTs[Value]; |
| 4726 | unsigned NumParts = TLI->getNumRegisters(ValueVT); |
| 4727 | MVT RegisterVT = RegVTs[Value]; |
| 4728 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4729 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4730 | &Parts[Part], NumParts, RegisterVT); |
| 4731 | Part += NumParts; |
| 4732 | } |
| 4733 | |
| 4734 | // Copy the parts into the registers. |
| 4735 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4736 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4737 | SDValue Part; |
| 4738 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4739 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4740 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4741 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4742 | *Flag = Part.getValue(1); |
| 4743 | } |
| 4744 | Chains[i] = Part.getValue(0); |
| 4745 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4746 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4747 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4748 | // 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] | 4749 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4750 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4751 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4752 | // user as well as a successor (the TF operands are flagged to the user). |
| 4753 | // c1, f1 = CopyToReg |
| 4754 | // c2, f2 = CopyToReg |
| 4755 | // c3 = TokenFactor c1, c2 |
| 4756 | // ... |
| 4757 | // = op c3, ..., f2 |
| 4758 | Chain = Chains[NumRegs-1]; |
| 4759 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4760 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4764 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4765 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4766 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4767 | bool HasMatching,unsigned MatchingIdx, |
| 4768 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4769 | std::vector<SDValue> &Ops) const { |
| 4770 | MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4771 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4772 | unsigned Flag = Code | (Regs.size() << 3); |
| 4773 | if (HasMatching) |
| 4774 | Flag |= 0x80000000 | (MatchingIdx << 16); |
| 4775 | Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4776 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4777 | unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]); |
| 4778 | MVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4779 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4780 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4781 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4782 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4783 | } |
| 4784 | } |
| 4785 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4786 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4787 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4788 | /// register class for the register. Otherwise, return null. |
| 4789 | static const TargetRegisterClass * |
| 4790 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4791 | const TargetLowering &TLI, |
| 4792 | const TargetRegisterInfo *TRI) { |
| 4793 | MVT FoundVT = MVT::Other; |
| 4794 | const TargetRegisterClass *FoundRC = 0; |
| 4795 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4796 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
| 4797 | MVT ThisVT = MVT::Other; |
| 4798 | |
| 4799 | const TargetRegisterClass *RC = *RCI; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4800 | // 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] | 4801 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4802 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4803 | I != E; ++I) { |
| 4804 | if (TLI.isTypeLegal(*I)) { |
| 4805 | // If we have already found this register in a different register class, |
| 4806 | // choose the one with the largest VT specified. For example, on |
| 4807 | // PowerPC, we favor f64 register classes over f32. |
| 4808 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
| 4809 | ThisVT = *I; |
| 4810 | break; |
| 4811 | } |
| 4812 | } |
| 4813 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4814 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4815 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4816 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4817 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4818 | // frame pointer in functions that need it (due to them not being taken |
| 4819 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4820 | // yet). This is a slight code pessimization, but should still work. |
| 4821 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4822 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4823 | if (*I == Reg) { |
| 4824 | // We found a matching register class. Keep looking at others in case |
| 4825 | // we find one with larger registers that this physreg is also in. |
| 4826 | FoundRC = RC; |
| 4827 | FoundVT = ThisVT; |
| 4828 | break; |
| 4829 | } |
| 4830 | } |
| 4831 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4832 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4833 | |
| 4834 | |
| 4835 | namespace llvm { |
| 4836 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4837 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4838 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4839 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4840 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4841 | /// CallOperand - If this is the result output operand or a clobber |
| 4842 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4843 | /// This gets modified as the asm is processed. |
| 4844 | SDValue CallOperand; |
| 4845 | |
| 4846 | /// AssignedRegs - If this is a register or register class operand, this |
| 4847 | /// contains the set of register corresponding to the operand. |
| 4848 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4849 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4851 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4852 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4854 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4855 | /// busy in OutputRegs/InputRegs. |
| 4856 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4857 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4858 | std::set<unsigned> &InputRegs, |
| 4859 | const TargetRegisterInfo &TRI) const { |
| 4860 | if (isOutReg) { |
| 4861 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4862 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4863 | } |
| 4864 | if (isInReg) { |
| 4865 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4866 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4867 | } |
| 4868 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4869 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4870 | /// getCallOperandValMVT - Return the MVT of the Value* that this operand |
| 4871 | /// corresponds to. If there is no Value* for this operand, it returns |
| 4872 | /// MVT::Other. |
| 4873 | MVT getCallOperandValMVT(const TargetLowering &TLI, |
| 4874 | const TargetData *TD) const { |
| 4875 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4876 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4877 | if (isa<BasicBlock>(CallOperandVal)) |
| 4878 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4879 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4880 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4881 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4882 | // If this is an indirect operand, the operand is a pointer to the |
| 4883 | // accessed type. |
| 4884 | if (isIndirect) |
| 4885 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4886 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4887 | // If OpTy is not a single value, it may be a struct/union that we |
| 4888 | // can tile with integers. |
| 4889 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4890 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4891 | switch (BitSize) { |
| 4892 | default: break; |
| 4893 | case 1: |
| 4894 | case 8: |
| 4895 | case 16: |
| 4896 | case 32: |
| 4897 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4898 | case 128: |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4899 | OpTy = IntegerType::get(BitSize); |
| 4900 | break; |
| 4901 | } |
| 4902 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4903 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4904 | return TLI.getValueType(OpTy, true); |
| 4905 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4906 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4907 | private: |
| 4908 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4909 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4910 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4911 | const TargetRegisterInfo &TRI) { |
| 4912 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4913 | Regs.insert(Reg); |
| 4914 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4915 | for (; *Aliases; ++Aliases) |
| 4916 | Regs.insert(*Aliases); |
| 4917 | } |
| 4918 | }; |
| 4919 | } // end llvm namespace. |
| 4920 | |
| 4921 | |
| 4922 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4923 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4924 | /// register allocator handle the assignment process. However, if the asm uses |
| 4925 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 4926 | /// allocation. This produces generally horrible, but correct, code. |
| 4927 | /// |
| 4928 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4929 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4930 | /// |
| 4931 | void SelectionDAGLowering:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4932 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4933 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4934 | std::set<unsigned> &InputRegs) { |
| 4935 | // Compute whether this value requires an input register, an output register, |
| 4936 | // or both. |
| 4937 | bool isOutReg = false; |
| 4938 | bool isInReg = false; |
| 4939 | switch (OpInfo.Type) { |
| 4940 | case InlineAsm::isOutput: |
| 4941 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4942 | |
| 4943 | // 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] | 4944 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 4945 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4946 | break; |
| 4947 | case InlineAsm::isInput: |
| 4948 | isInReg = true; |
| 4949 | isOutReg = false; |
| 4950 | break; |
| 4951 | case InlineAsm::isClobber: |
| 4952 | isOutReg = true; |
| 4953 | isInReg = true; |
| 4954 | break; |
| 4955 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4956 | |
| 4957 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4958 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4959 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4960 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4961 | // If this is a constraint for a single physreg, or a constraint for a |
| 4962 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4963 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4964 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 4965 | OpInfo.ConstraintVT); |
| 4966 | |
| 4967 | unsigned NumRegs = 1; |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4968 | if (OpInfo.ConstraintVT != MVT::Other) { |
| 4969 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 4970 | // cast of the input value. More generally, handle any case where the input |
| 4971 | // value disagrees with the register class we plan to stick this in. |
| 4972 | if (OpInfo.Type == InlineAsm::isInput && |
| 4973 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
| 4974 | // Try to convert to the first MVT that the reg class contains. If the |
| 4975 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 4976 | // vector types). |
| 4977 | MVT RegVT = *PhysReg.second->vt_begin(); |
| 4978 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4979 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4980 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4981 | OpInfo.ConstraintVT = RegVT; |
| 4982 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 4983 | // If the input is a FP value and we want it in FP registers, do a |
| 4984 | // bitcast to the corresponding integer type. This turns an f64 value |
| 4985 | // into i64, which can be passed with two i32 values on a 32-bit |
| 4986 | // machine. |
| 4987 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4988 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4989 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4990 | OpInfo.ConstraintVT = RegVT; |
| 4991 | } |
| 4992 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4993 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4994 | NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4995 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4996 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4997 | MVT RegVT; |
| 4998 | MVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4999 | |
| 5000 | // If this is a constraint for a specific physical register, like {r17}, |
| 5001 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5002 | if (unsigned AssignedReg = PhysReg.first) { |
| 5003 | const TargetRegisterClass *RC = PhysReg.second; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5004 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5005 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5006 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5007 | // Get the actual register value type. This is important, because the user |
| 5008 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5009 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5010 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5011 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5012 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5013 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5014 | |
| 5015 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5016 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5017 | TargetRegisterClass::iterator I = RC->begin(); |
| 5018 | for (; *I != AssignedReg; ++I) |
| 5019 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5020 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5021 | // Already added the first reg. |
| 5022 | --NumRegs; ++I; |
| 5023 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5024 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5025 | Regs.push_back(*I); |
| 5026 | } |
| 5027 | } |
| 5028 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5029 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5030 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5031 | return; |
| 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 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5035 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5036 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5037 | RegVT = *RC->vt_begin(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5038 | if (OpInfo.ConstraintVT == MVT::Other) |
| 5039 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5040 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5041 | // Create the appropriate number of virtual registers. |
| 5042 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5043 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5044 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5045 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5046 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5047 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5048 | } |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5049 | |
| 5050 | // This is a reference to a register class that doesn't directly correspond |
| 5051 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5052 | // registers from the class. |
| 5053 | std::vector<unsigned> RegClassRegs |
| 5054 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5055 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5056 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5057 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5058 | unsigned NumAllocated = 0; |
| 5059 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5060 | unsigned Reg = RegClassRegs[i]; |
| 5061 | // See if this register is available. |
| 5062 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5063 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5064 | // Make sure we find consecutive registers. |
| 5065 | NumAllocated = 0; |
| 5066 | continue; |
| 5067 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5068 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5069 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5070 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5071 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5072 | if (!RC) { // Couldn't allocate this register. |
| 5073 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5074 | NumAllocated = 0; |
| 5075 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5076 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5077 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5078 | // Okay, this register is good, we can use it. |
| 5079 | ++NumAllocated; |
| 5080 | |
| 5081 | // If we allocated enough consecutive registers, succeed. |
| 5082 | if (NumAllocated == NumRegs) { |
| 5083 | unsigned RegStart = (i-NumAllocated)+1; |
| 5084 | unsigned RegEnd = i+1; |
| 5085 | // Mark all of the allocated registers used. |
| 5086 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5087 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5088 | |
| 5089 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5090 | OpInfo.ConstraintVT); |
| 5091 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5092 | return; |
| 5093 | } |
| 5094 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5095 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5096 | // Otherwise, we couldn't allocate enough registers for this. |
| 5097 | } |
| 5098 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5099 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5100 | /// processed uses a memory 'm' constraint. |
| 5101 | static bool |
| 5102 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5103 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5104 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5105 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5106 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5107 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5108 | if (CType == TargetLowering::C_Memory) |
| 5109 | return true; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | return false; |
| 5114 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5115 | |
| 5116 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5117 | /// |
| 5118 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 5119 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5120 | |
| 5121 | /// ConstraintOperands - Information about all of the constraints. |
| 5122 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5123 | |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5124 | // We won't need to flush pending loads if this asm doesn't touch |
| 5125 | // memory and is nonvolatile. |
| 5126 | SDValue Chain = IA->hasSideEffects() ? getRoot() : DAG.getRoot(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5127 | SDValue Flag; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5128 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5129 | std::set<unsigned> OutputRegs, InputRegs; |
| 5130 | |
| 5131 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5132 | // ConstraintOperands list. |
| 5133 | std::vector<InlineAsm::ConstraintInfo> |
| 5134 | ConstraintInfos = IA->ParseConstraints(); |
| 5135 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5136 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5137 | // Flush pending loads if this touches memory (includes clobbering it). |
| 5138 | // It's possible this is overly conservative. |
| 5139 | if (hasMemory) |
| 5140 | Chain = getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5141 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5142 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5143 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5144 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5145 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5146 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5147 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5148 | MVT OpVT = MVT::Other; |
| 5149 | |
| 5150 | // Compute the value type for each operand. |
| 5151 | switch (OpInfo.Type) { |
| 5152 | case InlineAsm::isOutput: |
| 5153 | // Indirect outputs just consume an argument. |
| 5154 | if (OpInfo.isIndirect) { |
| 5155 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5156 | break; |
| 5157 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5158 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5159 | // The return value of the call is this value. As such, there is no |
| 5160 | // corresponding argument. |
| 5161 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 5162 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5163 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5164 | } else { |
| 5165 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5166 | OpVT = TLI.getValueType(CS.getType()); |
| 5167 | } |
| 5168 | ++ResNo; |
| 5169 | break; |
| 5170 | case InlineAsm::isInput: |
| 5171 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5172 | break; |
| 5173 | case InlineAsm::isClobber: |
| 5174 | // Nothing to do. |
| 5175 | break; |
| 5176 | } |
| 5177 | |
| 5178 | // If this is an input or an indirect output, process the call argument. |
| 5179 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5180 | if (OpInfo.CallOperandVal) { |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5181 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5182 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5183 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5184 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5185 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5186 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5187 | OpVT = OpInfo.getCallOperandValMVT(TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5188 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5190 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5191 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5192 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5193 | // Second pass over the constraints: compute which constraint option to use |
| 5194 | // and assign registers to constraints that want a specific physreg. |
| 5195 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5196 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5197 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5198 | // 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] | 5199 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5200 | // other is floating point, or their sizes are different, flag it as an |
| 5201 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5202 | if (OpInfo.hasMatchingInput()) { |
| 5203 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5204 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5205 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5206 | Input.ConstraintVT.isInteger()) || |
| 5207 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5208 | Input.ConstraintVT.getSizeInBits())) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5209 | cerr << "llvm: error: Unsupported asm: input constraint with a " |
| 5210 | << "matching output constraint of incompatible type!\n"; |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5211 | exit(1); |
| 5212 | } |
| 5213 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5214 | } |
| 5215 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5216 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5217 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5218 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5219 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5220 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5221 | // need to to provide an address for the memory input. |
| 5222 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5223 | !OpInfo.isIndirect) { |
| 5224 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5225 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5226 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5227 | // Memory operands really want the address of the value. If we don't have |
| 5228 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5229 | // it to a stack slot. |
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 | // If the operand is a float, integer, or vector constant, spill to a |
| 5232 | // constant pool entry to get its address. |
| 5233 | Value *OpVal = OpInfo.CallOperandVal; |
| 5234 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5235 | isa<ConstantVector>(OpVal)) { |
| 5236 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5237 | TLI.getPointerTy()); |
| 5238 | } else { |
| 5239 | // Otherwise, create a stack slot and emit a store to it before the |
| 5240 | // asm. |
| 5241 | const Type *Ty = OpVal->getType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5242 | uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5243 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5244 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5245 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 5246 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5247 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5248 | OpInfo.CallOperand, StackSlot, NULL, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5249 | OpInfo.CallOperand = StackSlot; |
| 5250 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5251 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5252 | // There is no longer a Value* corresponding to this operand. |
| 5253 | OpInfo.CallOperandVal = 0; |
| 5254 | // It is now an indirect operand. |
| 5255 | OpInfo.isIndirect = true; |
| 5256 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5257 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | // If this constraint is for a specific register, allocate it before |
| 5259 | // anything else. |
| 5260 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5261 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5262 | } |
| 5263 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5264 | |
| 5265 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5266 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5267 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5268 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5269 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5270 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5271 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5272 | // to be. |
| 5273 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5274 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5277 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5278 | std::vector<SDValue> AsmNodeOperands; |
| 5279 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5280 | AsmNodeOperands.push_back( |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5281 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5282 | |
| 5283 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5284 | // Loop over all of the inputs, copying the operand values into the |
| 5285 | // appropriate registers and processing the output regs. |
| 5286 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5287 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5288 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5289 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
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 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5292 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5293 | |
| 5294 | switch (OpInfo.Type) { |
| 5295 | case InlineAsm::isOutput: { |
| 5296 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5297 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5298 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5299 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5300 | |
| 5301 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5302 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5303 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5304 | TLI.getPointerTy())); |
| 5305 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5306 | break; |
| 5307 | } |
| 5308 | |
| 5309 | // Otherwise, this is a register or register class output. |
| 5310 | |
| 5311 | // Copy the output from the appropriate register. Find a register that |
| 5312 | // we can use. |
| 5313 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5314 | cerr << "llvm: error: Couldn't allocate output reg for constraint '" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5315 | << OpInfo.ConstraintCode << "'!\n"; |
| 5316 | exit(1); |
| 5317 | } |
| 5318 | |
| 5319 | // If this is an indirect operand, store through the pointer after the |
| 5320 | // asm. |
| 5321 | if (OpInfo.isIndirect) { |
| 5322 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5323 | OpInfo.CallOperandVal)); |
| 5324 | } else { |
| 5325 | // This is the result value of the call. |
| 5326 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 5327 | // Concatenate this output onto the outputs list. |
| 5328 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5329 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5330 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5331 | // Add information to the INLINEASM node to know that this register is |
| 5332 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5333 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5334 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5335 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5336 | false, |
| 5337 | 0, |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5338 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5339 | break; |
| 5340 | } |
| 5341 | case InlineAsm::isInput: { |
| 5342 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5343 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5344 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5345 | // If this is required to match an output register we have already set, |
| 5346 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5347 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5348 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5349 | // Scan until we find the definition we already emitted of this operand. |
| 5350 | // When we find it, create a RegsForValue operand. |
| 5351 | unsigned CurOp = 2; // The first operand. |
| 5352 | for (; OperandNo; --OperandNo) { |
| 5353 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5354 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5355 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5356 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5357 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5358 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5359 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5360 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5363 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5364 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5365 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5366 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5367 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5368 | RegsForValue MatchedRegs; |
| 5369 | MatchedRegs.TLI = &TLI; |
| 5370 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5371 | MVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 5372 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5373 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5374 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5375 | i != e; ++i) |
| 5376 | MatchedRegs.Regs. |
| 5377 | push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5378 | |
| 5379 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5380 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5381 | Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5382 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5383 | true, OpInfo.getMatchedOperand(), |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5384 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5385 | break; |
| 5386 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5387 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5388 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5389 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5390 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5391 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5392 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5393 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5394 | TLI.getPointerTy())); |
| 5395 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5396 | break; |
| 5397 | } |
| 5398 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5399 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5400 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5401 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5402 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5403 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5404 | std::vector<SDValue> Ops; |
| 5405 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5406 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5407 | if (Ops.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5408 | cerr << "llvm: error: Invalid operand for inline asm constraint '" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5409 | << OpInfo.ConstraintCode << "'!\n"; |
| 5410 | exit(1); |
| 5411 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5412 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5413 | // Add information to the INLINEASM node to know about this input. |
| 5414 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5415 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5416 | TLI.getPointerTy())); |
| 5417 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5418 | break; |
| 5419 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5420 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5421 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5422 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5423 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5424 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5425 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5426 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5427 | TLI.getPointerTy())); |
| 5428 | AsmNodeOperands.push_back(InOperandVal); |
| 5429 | break; |
| 5430 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5431 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5432 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5433 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5434 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5435 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5436 | "Don't know how to handle indirect register inputs yet!"); |
| 5437 | |
| 5438 | // Copy the input into the appropriate registers. |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5439 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Daniel Dunbar | 4cbb173 | 2009-04-13 22:26:09 +0000 | [diff] [blame] | 5440 | cerr << "llvm: error: Couldn't allocate output reg for constraint '" |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5441 | << OpInfo.ConstraintCode << "'!\n"; |
| 5442 | exit(1); |
| 5443 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5444 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5445 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5446 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5447 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5448 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5449 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5450 | break; |
| 5451 | } |
| 5452 | case InlineAsm::isClobber: { |
| 5453 | // Add the clobbered value to the operand list, so that the register |
| 5454 | // allocator is aware that the physreg got clobbered. |
| 5455 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5456 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5457 | false, 0, DAG,AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5458 | break; |
| 5459 | } |
| 5460 | } |
| 5461 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5462 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5463 | // Finish up input operands. |
| 5464 | AsmNodeOperands[0] = Chain; |
| 5465 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5466 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5467 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5468 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5469 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5470 | Flag = Chain.getValue(1); |
| 5471 | |
| 5472 | // If this asm returns a register value, copy the result from that register |
| 5473 | // and set it as the value of the call. |
| 5474 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5475 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5476 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5477 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5478 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5479 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
| 5480 | MVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5481 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5482 | // If any of the results of the inline asm is a vector, it may have the |
| 5483 | // wrong width/num elts. This can happen for register classes that can |
| 5484 | // contain multiple different value types. The preg or vreg allocated may |
| 5485 | // not have the same VT as was expected. Convert it to the right type |
| 5486 | // with bit_convert. |
| 5487 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5488 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5489 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5490 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5491 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5492 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5493 | // If a result value was tied to an input value, the computed result may |
| 5494 | // have a wider width than the expected result. Extract the relevant |
| 5495 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5496 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5497 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5499 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5500 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5501 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5502 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5503 | // Don't need to use this as a chain in this case. |
| 5504 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5505 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5506 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5507 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5508 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5509 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5510 | // Process indirect outputs, first output all of the flagged copies out of |
| 5511 | // physregs. |
| 5512 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5513 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5514 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5515 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
| 5516 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5517 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 5518 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5519 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5520 | // Emit the non-flagged stores from the physregs. |
| 5521 | SmallVector<SDValue, 8> OutChains; |
| 5522 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5523 | OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5524 | StoresToEmit[i].first, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5525 | getValue(StoresToEmit[i].second), |
| 5526 | StoresToEmit[i].second, 0)); |
| 5527 | if (!OutChains.empty()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5528 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5529 | &OutChains[0], OutChains.size()); |
| 5530 | DAG.setRoot(Chain); |
| 5531 | } |
| 5532 | |
| 5533 | |
| 5534 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 5535 | SDValue Src = getValue(I.getOperand(0)); |
| 5536 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 5537 | // Scale up by the type size in the original i32 type width. Various |
| 5538 | // mid-level optimizers may make assumptions about demanded bits etc from the |
| 5539 | // i32-ness of the optimizer: we do not want to promote to i64 and then |
| 5540 | // multiply on 64-bit targets. |
| 5541 | // FIXME: Malloc inst should go away: PR715. |
| 5542 | uint64_t ElementSize = TD->getTypePaddedSize(I.getType()->getElementType()); |
| 5543 | if (ElementSize != 1) |
| 5544 | Src = DAG.getNode(ISD::MUL, getCurDebugLoc(), Src.getValueType(), |
| 5545 | Src, DAG.getConstant(ElementSize, Src.getValueType())); |
| 5546 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5547 | MVT IntPtr = TLI.getPointerTy(); |
| 5548 | |
| 5549 | if (IntPtr.bitsLT(Src.getValueType())) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5550 | Src = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), IntPtr, Src); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5551 | else if (IntPtr.bitsGT(Src.getValueType())) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5552 | Src = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), IntPtr, Src); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5553 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5554 | TargetLowering::ArgListTy Args; |
| 5555 | TargetLowering::ArgListEntry Entry; |
| 5556 | Entry.Node = Src; |
| 5557 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5558 | Args.push_back(Entry); |
| 5559 | |
| 5560 | std::pair<SDValue,SDValue> Result = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5561 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, false, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5562 | CallingConv::C, PerformTailCallOpt, |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5563 | DAG.getExternalSymbol("malloc", IntPtr), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5564 | Args, DAG, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5565 | setValue(&I, Result.first); // Pointers always fit in registers |
| 5566 | DAG.setRoot(Result.second); |
| 5567 | } |
| 5568 | |
| 5569 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 5570 | TargetLowering::ArgListTy Args; |
| 5571 | TargetLowering::ArgListEntry Entry; |
| 5572 | Entry.Node = getValue(I.getOperand(0)); |
| 5573 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5574 | Args.push_back(Entry); |
| 5575 | MVT IntPtr = TLI.getPointerTy(); |
| 5576 | std::pair<SDValue,SDValue> Result = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5577 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, false, |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5578 | CallingConv::C, PerformTailCallOpt, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5579 | DAG.getExternalSymbol("free", IntPtr), Args, DAG, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5580 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5581 | DAG.setRoot(Result.second); |
| 5582 | } |
| 5583 | |
| 5584 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5585 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5586 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5587 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5588 | DAG.getSrcValue(I.getOperand(1)))); |
| 5589 | } |
| 5590 | |
| 5591 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5592 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5593 | getRoot(), getValue(I.getOperand(0)), |
| 5594 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5595 | setValue(&I, V); |
| 5596 | DAG.setRoot(V.getValue(1)); |
| 5597 | } |
| 5598 | |
| 5599 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5600 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5601 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5602 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5603 | DAG.getSrcValue(I.getOperand(1)))); |
| 5604 | } |
| 5605 | |
| 5606 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5607 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5608 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5609 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5610 | getValue(I.getOperand(2)), |
| 5611 | DAG.getSrcValue(I.getOperand(1)), |
| 5612 | DAG.getSrcValue(I.getOperand(2)))); |
| 5613 | } |
| 5614 | |
| 5615 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 5616 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5617 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5618 | /// integrated into SDISel. |
| 5619 | void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5620 | SmallVectorImpl<SDValue> &ArgValues, |
| 5621 | DebugLoc dl) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5622 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 5623 | SmallVector<SDValue, 3+16> Ops; |
| 5624 | Ops.push_back(DAG.getRoot()); |
| 5625 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 5626 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 5627 | |
| 5628 | // Add one result value for each formal argument. |
| 5629 | SmallVector<MVT, 16> RetVals; |
| 5630 | unsigned j = 1; |
| 5631 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5632 | I != E; ++I, ++j) { |
| 5633 | SmallVector<MVT, 4> ValueVTs; |
| 5634 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5635 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5636 | Value != NumValues; ++Value) { |
| 5637 | MVT VT = ValueVTs[Value]; |
| 5638 | const Type *ArgTy = VT.getTypeForMVT(); |
| 5639 | ISD::ArgFlagsTy Flags; |
| 5640 | unsigned OriginalAlignment = |
| 5641 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5642 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5643 | if (F.paramHasAttr(j, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5644 | Flags.setZExt(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5645 | if (F.paramHasAttr(j, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5646 | Flags.setSExt(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5647 | if (F.paramHasAttr(j, Attribute::InReg)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | Flags.setInReg(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5649 | if (F.paramHasAttr(j, Attribute::StructRet)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5650 | Flags.setSRet(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5651 | if (F.paramHasAttr(j, Attribute::ByVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5652 | Flags.setByVal(); |
| 5653 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5654 | const Type *ElementTy = Ty->getElementType(); |
| 5655 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5656 | unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5657 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5658 | // this info is not there but there are cases it cannot get right. |
| 5659 | if (F.getParamAlignment(j)) |
| 5660 | FrameAlign = F.getParamAlignment(j); |
| 5661 | Flags.setByValAlign(FrameAlign); |
| 5662 | Flags.setByValSize(FrameSize); |
| 5663 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5664 | if (F.paramHasAttr(j, Attribute::Nest)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5665 | Flags.setNest(); |
| 5666 | Flags.setOrigAlign(OriginalAlignment); |
| 5667 | |
| 5668 | MVT RegisterVT = getRegisterType(VT); |
| 5669 | unsigned NumRegs = getNumRegisters(VT); |
| 5670 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5671 | RetVals.push_back(RegisterVT); |
| 5672 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5673 | if (NumRegs > 1 && i == 0) |
| 5674 | MyFlags.setSplit(); |
| 5675 | // if it isn't first piece, alignment must be 1 |
| 5676 | else if (i > 0) |
| 5677 | MyFlags.setOrigAlign(1); |
| 5678 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5679 | } |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | RetVals.push_back(MVT::Other); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5684 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5685 | // Create the node. |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5686 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5687 | DAG.getVTList(&RetVals[0], RetVals.size()), |
| 5688 | &Ops[0], Ops.size()).getNode(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5689 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5690 | // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but |
| 5691 | // allows exposing the loads that may be part of the argument access to the |
| 5692 | // first DAGCombiner pass. |
| 5693 | SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5694 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5695 | // The number of results should match up, except that the lowered one may have |
| 5696 | // an extra flag result. |
| 5697 | assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() || |
| 5698 | (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() && |
| 5699 | TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag)) |
| 5700 | && "Lowering produced unexpected number of results!"); |
| 5701 | |
| 5702 | // The FORMAL_ARGUMENTS node itself is likely no longer needed. |
| 5703 | if (Result != TmpRes.getNode() && Result->use_empty()) { |
| 5704 | HandleSDNode Dummy(DAG.getRoot()); |
| 5705 | DAG.RemoveDeadNode(Result); |
| 5706 | } |
| 5707 | |
| 5708 | Result = TmpRes.getNode(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5709 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5710 | unsigned NumArgRegs = Result->getNumValues() - 1; |
| 5711 | DAG.setRoot(SDValue(Result, NumArgRegs)); |
| 5712 | |
| 5713 | // Set up the return result vector. |
| 5714 | unsigned i = 0; |
| 5715 | unsigned Idx = 1; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5716 | 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] | 5717 | ++I, ++Idx) { |
| 5718 | SmallVector<MVT, 4> ValueVTs; |
| 5719 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5720 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5721 | Value != NumValues; ++Value) { |
| 5722 | MVT VT = ValueVTs[Value]; |
| 5723 | MVT PartVT = getRegisterType(VT); |
| 5724 | |
| 5725 | unsigned NumParts = getNumRegisters(VT); |
| 5726 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5727 | for (unsigned j = 0; j != NumParts; ++j) |
| 5728 | Parts[j] = SDValue(Result, i++); |
| 5729 | |
| 5730 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5731 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5732 | AssertOp = ISD::AssertSext; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 5733 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5734 | AssertOp = ISD::AssertZext; |
| 5735 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5736 | ArgValues.push_back(getCopyFromParts(DAG, dl, &Parts[0], NumParts, |
| 5737 | PartVT, VT, AssertOp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5738 | } |
| 5739 | } |
| 5740 | assert(i == NumArgRegs && "Argument register count mismatch!"); |
| 5741 | } |
| 5742 | |
| 5743 | |
| 5744 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 5745 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 5746 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 5747 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 5748 | std::pair<SDValue, SDValue> |
| 5749 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5750 | bool RetSExt, bool RetZExt, bool isVarArg, |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5751 | bool isInreg, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5752 | unsigned CallingConv, bool isTailCall, |
| 5753 | SDValue Callee, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5754 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5755 | assert((!isTailCall || PerformTailCallOpt) && |
| 5756 | "isTailCall set when tail-call optimizations are disabled!"); |
| 5757 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5758 | SmallVector<SDValue, 32> Ops; |
| 5759 | Ops.push_back(Chain); // Op#0 - Chain |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5760 | Ops.push_back(Callee); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5761 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5762 | // Handle all of the outgoing arguments. |
| 5763 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5764 | SmallVector<MVT, 4> ValueVTs; |
| 5765 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5766 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5767 | Value != NumValues; ++Value) { |
| 5768 | MVT VT = ValueVTs[Value]; |
| 5769 | const Type *ArgTy = VT.getTypeForMVT(); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5770 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5771 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5772 | ISD::ArgFlagsTy Flags; |
| 5773 | unsigned OriginalAlignment = |
| 5774 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5775 | |
| 5776 | if (Args[i].isZExt) |
| 5777 | Flags.setZExt(); |
| 5778 | if (Args[i].isSExt) |
| 5779 | Flags.setSExt(); |
| 5780 | if (Args[i].isInReg) |
| 5781 | Flags.setInReg(); |
| 5782 | if (Args[i].isSRet) |
| 5783 | Flags.setSRet(); |
| 5784 | if (Args[i].isByVal) { |
| 5785 | Flags.setByVal(); |
| 5786 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5787 | const Type *ElementTy = Ty->getElementType(); |
| 5788 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5789 | unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5790 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5791 | // info is not there but there are cases it cannot get right. |
| 5792 | if (Args[i].Alignment) |
| 5793 | FrameAlign = Args[i].Alignment; |
| 5794 | Flags.setByValAlign(FrameAlign); |
| 5795 | Flags.setByValSize(FrameSize); |
| 5796 | } |
| 5797 | if (Args[i].isNest) |
| 5798 | Flags.setNest(); |
| 5799 | Flags.setOrigAlign(OriginalAlignment); |
| 5800 | |
| 5801 | MVT PartVT = getRegisterType(VT); |
| 5802 | unsigned NumParts = getNumRegisters(VT); |
| 5803 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5804 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5805 | |
| 5806 | if (Args[i].isSExt) |
| 5807 | ExtendKind = ISD::SIGN_EXTEND; |
| 5808 | else if (Args[i].isZExt) |
| 5809 | ExtendKind = ISD::ZERO_EXTEND; |
| 5810 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5811 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | |
| 5813 | for (unsigned i = 0; i != NumParts; ++i) { |
| 5814 | // if it isn't first piece, alignment must be 1 |
| 5815 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5816 | if (NumParts > 1 && i == 0) |
| 5817 | MyFlags.setSplit(); |
| 5818 | else if (i != 0) |
| 5819 | MyFlags.setOrigAlign(1); |
| 5820 | |
| 5821 | Ops.push_back(Parts[i]); |
| 5822 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5823 | } |
| 5824 | } |
| 5825 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5826 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5827 | // Figure out the result value types. We start by making a list of |
| 5828 | // the potentially illegal return value types. |
| 5829 | SmallVector<MVT, 4> LoweredRetTys; |
| 5830 | SmallVector<MVT, 4> RetTys; |
| 5831 | ComputeValueVTs(*this, RetTy, RetTys); |
| 5832 | |
| 5833 | // Then we translate that to a list of legal types. |
| 5834 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5835 | MVT VT = RetTys[I]; |
| 5836 | MVT RegisterVT = getRegisterType(VT); |
| 5837 | unsigned NumRegs = getNumRegisters(VT); |
| 5838 | for (unsigned i = 0; i != NumRegs; ++i) |
| 5839 | LoweredRetTys.push_back(RegisterVT); |
| 5840 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5841 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5842 | LoweredRetTys.push_back(MVT::Other); // Always has a chain. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5843 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5844 | // Create the CALL node. |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5845 | SDValue Res = DAG.getCall(CallingConv, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5846 | isVarArg, isTailCall, isInreg, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5847 | DAG.getVTList(&LoweredRetTys[0], |
| 5848 | LoweredRetTys.size()), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 5849 | &Ops[0], Ops.size() |
| 5850 | ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5851 | Chain = Res.getValue(LoweredRetTys.size() - 1); |
| 5852 | |
| 5853 | // Gather up the call result into a single value. |
Dan Gohman | b5cc34d | 2008-10-07 00:12:37 +0000 | [diff] [blame] | 5854 | if (RetTy != Type::VoidTy && !RetTys.empty()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5855 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5856 | |
| 5857 | if (RetSExt) |
| 5858 | AssertOp = ISD::AssertSext; |
| 5859 | else if (RetZExt) |
| 5860 | AssertOp = ISD::AssertZext; |
| 5861 | |
| 5862 | SmallVector<SDValue, 4> ReturnValues; |
| 5863 | unsigned RegNo = 0; |
| 5864 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5865 | MVT VT = RetTys[I]; |
| 5866 | MVT RegisterVT = getRegisterType(VT); |
| 5867 | unsigned NumRegs = getNumRegisters(VT); |
| 5868 | unsigned RegNoEnd = NumRegs + RegNo; |
| 5869 | SmallVector<SDValue, 4> Results; |
| 5870 | for (; RegNo != RegNoEnd; ++RegNo) |
| 5871 | Results.push_back(Res.getValue(RegNo)); |
| 5872 | SDValue ReturnValue = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5873 | getCopyFromParts(DAG, dl, &Results[0], NumRegs, RegisterVT, VT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5874 | AssertOp); |
| 5875 | ReturnValues.push_back(ReturnValue); |
| 5876 | } |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5877 | Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 5878 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5879 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | return std::make_pair(Res, Chain); |
| 5883 | } |
| 5884 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5885 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5886 | SmallVectorImpl<SDValue> &Results, |
| 5887 | SelectionDAG &DAG) { |
| 5888 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5889 | if (Res.getNode()) |
| 5890 | Results.push_back(Res); |
| 5891 | } |
| 5892 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5893 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
| 5894 | assert(0 && "LowerOperation not implemented for this target!"); |
| 5895 | abort(); |
| 5896 | return SDValue(); |
| 5897 | } |
| 5898 | |
| 5899 | |
| 5900 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
| 5901 | SDValue Op = getValue(V); |
| 5902 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5903 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5904 | "Copy from a reg to the same reg!"); |
| 5905 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5906 | |
| 5907 | RegsForValue RFV(TLI, Reg, V->getType()); |
| 5908 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5909 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5910 | PendingExports.push_back(Chain); |
| 5911 | } |
| 5912 | |
| 5913 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5914 | |
| 5915 | void SelectionDAGISel:: |
| 5916 | LowerArguments(BasicBlock *LLVMBB) { |
| 5917 | // If this is the entry block, emit arguments. |
| 5918 | Function &F = *LLVMBB->getParent(); |
| 5919 | SDValue OldRoot = SDL->DAG.getRoot(); |
| 5920 | SmallVector<SDValue, 16> Args; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5921 | TLI.LowerArguments(F, SDL->DAG, Args, SDL->getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5922 | |
| 5923 | unsigned a = 0; |
| 5924 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 5925 | AI != E; ++AI) { |
| 5926 | SmallVector<MVT, 4> ValueVTs; |
| 5927 | ComputeValueVTs(TLI, AI->getType(), ValueVTs); |
| 5928 | unsigned NumValues = ValueVTs.size(); |
| 5929 | if (!AI->use_empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5930 | SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues, |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5931 | SDL->getCurDebugLoc())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5932 | // If this argument is live outside of the entry block, insert a copy from |
| 5933 | // 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^] | 5934 | SDL->CopyToExportRegsIfNeeded(AI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5935 | } |
| 5936 | a += NumValues; |
| 5937 | } |
| 5938 | |
| 5939 | // Finally, if the target has anything special to do, allow it to do so. |
| 5940 | // FIXME: this should insert code into the DAG! |
| 5941 | EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction()); |
| 5942 | } |
| 5943 | |
| 5944 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 5945 | /// ensure constants are generated when needed. Remember the virtual registers |
| 5946 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 5947 | /// directly add them, because expansion might result in multiple MBB's for one |
| 5948 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 5949 | /// the end. |
| 5950 | /// |
| 5951 | void |
| 5952 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 5953 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 5954 | |
| 5955 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5956 | |
| 5957 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5958 | // from this block. |
| 5959 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5960 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5961 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5962 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5963 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5964 | // If this terminator has multiple identical successors (common for |
| 5965 | // switches), only handle each succ once. |
| 5966 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5967 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5968 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5969 | PHINode *PN; |
| 5970 | |
| 5971 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5972 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5973 | // emitted yet. |
| 5974 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5975 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5976 | // Ignore dead phi's. |
| 5977 | if (PN->use_empty()) continue; |
| 5978 | |
| 5979 | unsigned Reg; |
| 5980 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5981 | |
| 5982 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 5983 | unsigned &RegOut = SDL->ConstantsOut[C]; |
| 5984 | if (RegOut == 0) { |
| 5985 | RegOut = FuncInfo->CreateRegForValue(C); |
| 5986 | SDL->CopyValueToVirtualRegister(C, RegOut); |
| 5987 | } |
| 5988 | Reg = RegOut; |
| 5989 | } else { |
| 5990 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 5991 | if (Reg == 0) { |
| 5992 | assert(isa<AllocaInst>(PHIOp) && |
| 5993 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 5994 | "Didn't codegen value into a register!??"); |
| 5995 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
| 5996 | SDL->CopyValueToVirtualRegister(PHIOp, Reg); |
| 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | // Remember that this register needs to added to the machine PHI node as |
| 6001 | // the input for this MBB. |
| 6002 | SmallVector<MVT, 4> ValueVTs; |
| 6003 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6004 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 6005 | MVT VT = ValueVTs[vti]; |
| 6006 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
| 6007 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
| 6008 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 6009 | Reg += NumRegisters; |
| 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | SDL->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6016 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 6017 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 6018 | /// creating SelectionDAG nodes. |
| 6019 | /// |
| 6020 | bool |
| 6021 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 6022 | FastISel *F) { |
| 6023 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6024 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6025 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6026 | unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size(); |
| 6027 | |
| 6028 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6029 | // from this block. |
| 6030 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6031 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6032 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6033 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6034 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6035 | // If this terminator has multiple identical successors (common for |
| 6036 | // switches), only handle each succ once. |
| 6037 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6038 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6039 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6040 | PHINode *PN; |
| 6041 | |
| 6042 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6043 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6044 | // emitted yet. |
| 6045 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6046 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6047 | // Ignore dead phi's. |
| 6048 | if (PN->use_empty()) continue; |
| 6049 | |
| 6050 | // Only handle legal types. Two interesting things to note here. First, |
| 6051 | // by bailing out early, we may leave behind some dead instructions, |
| 6052 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6053 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6054 | // use CreateRegForValue to create registers, so it always creates |
| 6055 | // exactly one register for each non-void instruction. |
| 6056 | MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 6057 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6058 | // Promote MVT::i1. |
| 6059 | if (VT == MVT::i1) |
| 6060 | VT = TLI.getTypeToTransformTo(VT); |
| 6061 | else { |
| 6062 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6063 | return false; |
| 6064 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6065 | } |
| 6066 | |
| 6067 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6068 | |
| 6069 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6070 | if (Reg == 0) { |
| 6071 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6072 | return false; |
| 6073 | } |
| 6074 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | return true; |
| 6079 | } |