Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "SelectionDAGBuild.h" |
| 16 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
| 19 | #include "llvm/Constants.h" |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/GlobalVariable.h" |
| 25 | #include "llvm/InlineAsm.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/Intrinsics.h" |
| 28 | #include "llvm/IntrinsicInst.h" |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame^] | 29 | #include "llvm/LLVMContext.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 30 | #include "llvm/Module.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/FastISel.h" |
| 32 | #include "llvm/CodeGen/GCStrategy.h" |
| 33 | #include "llvm/CodeGen/GCMetadata.h" |
| 34 | #include "llvm/CodeGen/MachineFunction.h" |
| 35 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 36 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 37 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 38 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 39 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/DwarfWriter.h" |
| 43 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetRegisterInfo.h" |
| 45 | #include "llvm/Target/TargetData.h" |
| 46 | #include "llvm/Target/TargetFrameInfo.h" |
| 47 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetOptions.h" |
| 51 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 54 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 57 | #include <algorithm> |
| 58 | using namespace llvm; |
| 59 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 60 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 61 | /// some float libcalls (6, 8 or 12 bits). |
| 62 | static unsigned LimitFloatPrecision; |
| 63 | |
| 64 | static cl::opt<unsigned, true> |
| 65 | LimitFPPrecision("limit-float-precision", |
| 66 | cl::desc("Generate low-precision inline sequences " |
| 67 | "for some float libcalls"), |
| 68 | cl::location(LimitFloatPrecision), |
| 69 | cl::init(0)); |
| 70 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 71 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 72 | /// of insertvalue or extractvalue indices that identify a member, return |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 73 | /// the linearized index of the start of the member. |
| 74 | /// |
| 75 | static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, |
| 76 | const unsigned *Indices, |
| 77 | const unsigned *IndicesEnd, |
| 78 | unsigned CurIndex = 0) { |
| 79 | // Base case: We're done. |
| 80 | if (Indices && Indices == IndicesEnd) |
| 81 | return CurIndex; |
| 82 | |
| 83 | // Given a struct type, recursively traverse the elements. |
| 84 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 85 | for (StructType::element_iterator EB = STy->element_begin(), |
| 86 | EI = EB, |
| 87 | EE = STy->element_end(); |
| 88 | EI != EE; ++EI) { |
| 89 | if (Indices && *Indices == unsigned(EI - EB)) |
| 90 | return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); |
| 91 | CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); |
| 92 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 93 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 94 | } |
| 95 | // Given an array type, recursively traverse the elements. |
| 96 | else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 97 | const Type *EltTy = ATy->getElementType(); |
| 98 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 99 | if (Indices && *Indices == i) |
| 100 | return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); |
| 101 | CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); |
| 102 | } |
Dan Gohman | 2c91d10 | 2009-01-06 22:53:52 +0000 | [diff] [blame] | 103 | return CurIndex; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 104 | } |
| 105 | // We haven't found the type we're looking for, so keep searching. |
| 106 | return CurIndex + 1; |
| 107 | } |
| 108 | |
| 109 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 110 | /// EVTs that represent all the individual underlying |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 111 | /// non-aggregate types that comprise it. |
| 112 | /// |
| 113 | /// If Offsets is non-null, it points to a vector to be filled in |
| 114 | /// with the in-memory offsets of each of the individual values. |
| 115 | /// |
| 116 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 117 | SmallVectorImpl<EVT> &ValueVTs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 118 | SmallVectorImpl<uint64_t> *Offsets = 0, |
| 119 | uint64_t StartingOffset = 0) { |
| 120 | // Given a struct type, recursively traverse the elements. |
| 121 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 122 | const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); |
| 123 | for (StructType::element_iterator EB = STy->element_begin(), |
| 124 | EI = EB, |
| 125 | EE = STy->element_end(); |
| 126 | EI != EE; ++EI) |
| 127 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 128 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 129 | return; |
| 130 | } |
| 131 | // Given an array type, recursively traverse the elements. |
| 132 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 133 | const Type *EltTy = ATy->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 134 | uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 135 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 136 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 137 | StartingOffset + i * EltSize); |
| 138 | return; |
| 139 | } |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 140 | // Interpret void as zero return values. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 141 | if (Ty == Type::getVoidTy(Ty->getContext())) |
Dan Gohman | 5e5558b | 2009-04-23 22:50:03 +0000 | [diff] [blame] | 142 | return; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 143 | // Base case: we can get an EVT for this LLVM IR type. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 144 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 145 | if (Offsets) |
| 146 | Offsets->push_back(StartingOffset); |
| 147 | } |
| 148 | |
Dan Gohman | 2a7c671 | 2008-09-03 23:18:39 +0000 | [diff] [blame] | 149 | namespace llvm { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 150 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 151 | /// that a particular set of values is assigned, and the type information about |
| 152 | /// the value. The most common situation is to represent one value at a time, |
| 153 | /// but struct or array values are handled element-wise as multiple values. |
| 154 | /// The splitting of aggregates is performed recursively, so that we never |
| 155 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 156 | /// have legal types, so each value may require one or more registers of some |
| 157 | /// legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 158 | /// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 159 | struct VISIBILITY_HIDDEN RegsForValue { |
| 160 | /// TLI - The TargetLowering object. |
| 161 | /// |
| 162 | const TargetLowering *TLI; |
| 163 | |
| 164 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 165 | /// may need be promoted or synthesized from one or more registers. |
| 166 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 167 | SmallVector<EVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 168 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 169 | /// RegVTs - The value types of the registers. This is the same size as |
| 170 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 171 | /// register or registers are. (Individual values are never synthesized |
| 172 | /// from more than one type of register.) |
| 173 | /// |
| 174 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 175 | /// getRegisterType member function, however when with physical registers |
| 176 | /// it is necessary to have a separate record of the types. |
| 177 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 178 | SmallVector<EVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 180 | /// Regs - This list holds the registers assigned to the values. |
| 181 | /// Each legal or promoted value requires one register, and each |
| 182 | /// expanded value requires multiple registers. |
| 183 | /// |
| 184 | SmallVector<unsigned, 4> Regs; |
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() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 187 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 188 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 189 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 190 | EVT regvt, EVT valuevt) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 192 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 193 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 194 | const SmallVector<EVT, 4> ®vts, |
| 195 | const SmallVector<EVT, 4> &valuevts) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 197 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 199 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 200 | |
| 201 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 202 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 203 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 204 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 205 | for (unsigned i = 0; i != NumRegs; ++i) |
| 206 | Regs.push_back(Reg + i); |
| 207 | RegVTs.push_back(RegisterVT); |
| 208 | Reg += NumRegs; |
| 209 | } |
| 210 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 211 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 212 | /// append - Add the specified values to this one. |
| 213 | void append(const RegsForValue &RHS) { |
| 214 | TLI = RHS.TLI; |
| 215 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 216 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 217 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 218 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 219 | |
| 220 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 221 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 222 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 223 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 224 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 225 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 226 | SDValue &Chain, SDValue *Flag) const; |
| 227 | |
| 228 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 229 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 230 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 231 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 232 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 233 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 234 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 235 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 236 | /// operand list. This adds the code marker, matching input operand index |
| 237 | /// (if applicable), and includes the number of values added into it. |
| 238 | void AddInlineAsmOperands(unsigned Code, |
| 239 | bool HasMatching, unsigned MatchingIdx, |
| 240 | SelectionDAG &DAG, std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 241 | }; |
| 242 | } |
| 243 | |
| 244 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 245 | /// 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] | 246 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
| 247 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 248 | if (isa<PHINode>(I)) return true; |
| 249 | BasicBlock *BB = I->getParent(); |
| 250 | 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] | 251 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 252 | return true; |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 257 | /// entry block, return true. This includes arguments used by switches, since |
| 258 | /// the switch may expand into multiple basic blocks. |
| 259 | static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { |
| 260 | // With FastISel active, we may be splitting blocks, so force creation |
| 261 | // of virtual registers for all non-dead arguments. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 262 | // Don't force virtual registers for byval arguments though, because |
| 263 | // fast-isel can't handle those in all cases. |
| 264 | if (EnableFastISel && !A->hasByValAttr()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 265 | return A->use_empty(); |
| 266 | |
| 267 | BasicBlock *Entry = A->getParent()->begin(); |
| 268 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 269 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 270 | return false; // Use not in entry block. |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) |
| 275 | : TLI(tli) { |
| 276 | } |
| 277 | |
| 278 | void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 279 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 280 | bool EnableFastISel) { |
| 281 | Fn = &fn; |
| 282 | MF = &mf; |
| 283 | RegInfo = &MF->getRegInfo(); |
| 284 | |
| 285 | // Create a vreg for each argument register that is not dead and is used |
| 286 | // outside of the entry block for the function. |
| 287 | for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
| 288 | AI != E; ++AI) |
| 289 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 290 | InitializeRegForValue(AI); |
| 291 | |
| 292 | // Initialize the mapping of values to registers. This is only set up for |
| 293 | // instruction values that are used outside of the block that defines |
| 294 | // them. |
| 295 | Function::iterator BB = Fn->begin(), EB = Fn->end(); |
| 296 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 297 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 298 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 299 | const Type *Ty = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 300 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 301 | unsigned Align = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 302 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 303 | AI->getAlignment()); |
| 304 | |
| 305 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 306 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 307 | StaticAllocaMap[AI] = |
| 308 | MF->getFrameInfo()->CreateStackObject(TySize, Align); |
| 309 | } |
| 310 | |
| 311 | for (; BB != EB; ++BB) |
| 312 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 313 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 314 | if (!isa<AllocaInst>(I) || |
| 315 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 316 | InitializeRegForValue(I); |
| 317 | |
| 318 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 319 | // also creates the initial PHI MachineInstrs, though none of the input |
| 320 | // operands are populated. |
| 321 | for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { |
| 322 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 323 | MBBMap[BB] = MBB; |
| 324 | MF->push_back(MBB); |
| 325 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 326 | // Transfer the address-taken flag. This is necessary because there could |
| 327 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 328 | // the first one should be marked. |
| 329 | if (BB->hasAddressTaken()) |
| 330 | MBB->setHasAddressTaken(); |
| 331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 332 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 333 | // appropriate. |
| 334 | PHINode *PN; |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 335 | DebugLoc DL; |
| 336 | for (BasicBlock::iterator |
| 337 | I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 338 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 339 | if (Function *F = CI->getCalledFunction()) { |
| 340 | switch (F->getIntrinsicID()) { |
| 341 | default: break; |
| 342 | case Intrinsic::dbg_stoppoint: { |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 343 | DbgStopPointInst *SPI = cast<DbgStopPointInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 344 | if (isValidDebugInfoIntrinsic(*SPI, CodeGenOpt::Default)) |
| 345 | DL = ExtractDebugLocation(*SPI, MF->getDebugLocInfo()); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | case Intrinsic::dbg_func_start: { |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 349 | DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 350 | if (isValidDebugInfoIntrinsic(*FSI, CodeGenOpt::Default)) |
| 351 | DL = ExtractDebugLocation(*FSI, MF->getDebugLocInfo()); |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | PN = dyn_cast<PHINode>(I); |
| 359 | if (!PN || PN->use_empty()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 360 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 361 | unsigned PHIReg = ValueMap[PN]; |
| 362 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 363 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 364 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 365 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 366 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 367 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 368 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 369 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 370 | for (unsigned i = 0; i != NumRegisters; ++i) |
Bill Wendling | 6a8a0d7 | 2009-02-03 02:20:52 +0000 | [diff] [blame] | 371 | BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 372 | PHIReg += NumRegisters; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 378 | unsigned FunctionLoweringInfo::MakeReg(EVT VT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 379 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 380 | } |
| 381 | |
| 382 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 383 | /// the correctly promoted or expanded types. Assign these registers |
| 384 | /// consecutive vreg numbers and return the first assigned number. |
| 385 | /// |
| 386 | /// In the case that the given value has struct or array type, this function |
| 387 | /// will assign registers for each member or element. |
| 388 | /// |
| 389 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 390 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 391 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
| 392 | |
| 393 | unsigned FirstReg = 0; |
| 394 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 395 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 396 | EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 397 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 398 | unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 399 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 400 | unsigned R = MakeReg(RegisterVT); |
| 401 | if (!FirstReg) FirstReg = R; |
| 402 | } |
| 403 | } |
| 404 | return FirstReg; |
| 405 | } |
| 406 | |
| 407 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 408 | /// combined into the value they represent. If the parts combine to a type |
| 409 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 410 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 411 | /// (ISD::AssertSext). |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 412 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
| 413 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 414 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 415 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 416 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 417 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 418 | SDValue Val = Parts[0]; |
| 419 | |
| 420 | if (NumParts > 1) { |
| 421 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 422 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 423 | unsigned PartBits = PartVT.getSizeInBits(); |
| 424 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 425 | |
| 426 | // Assemble the power of 2 part. |
| 427 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 428 | 1 << Log2_32(NumParts) : NumParts; |
| 429 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 430 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 431 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 432 | SDValue Lo, Hi; |
| 433 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 434 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 435 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 436 | if (RoundParts > 2) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 437 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT); |
| 438 | Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 439 | PartVT, HalfVT); |
| 440 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 441 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 442 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 443 | } |
| 444 | if (TLI.isBigEndian()) |
| 445 | std::swap(Lo, Hi); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 446 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 447 | |
| 448 | if (RoundParts < NumParts) { |
| 449 | // Assemble the trailing non-power-of-2 part. |
| 450 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 451 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 452 | Hi = getCopyFromParts(DAG, dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 453 | Parts+RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 454 | |
| 455 | // Combine the round and odd parts. |
| 456 | Lo = Val; |
| 457 | if (TLI.isBigEndian()) |
| 458 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 459 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 460 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 461 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 462 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 463 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 464 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 465 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 466 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 467 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 468 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 469 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 470 | unsigned NumIntermediates; |
| 471 | unsigned NumRegs = |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 472 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 473 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 474 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 475 | NumParts = NumRegs; // Silence a compiler warning. |
| 476 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 477 | assert(RegisterVT == Parts[0].getValueType() && |
| 478 | "Part type doesn't match part!"); |
| 479 | |
| 480 | // Assemble the parts into intermediate operands. |
| 481 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 482 | if (NumIntermediates == NumParts) { |
| 483 | // If the register was not expanded, truncate or copy the value, |
| 484 | // as appropriate. |
| 485 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 486 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 487 | PartVT, IntermediateVT); |
| 488 | } else if (NumParts > 0) { |
| 489 | // If the intermediate type was expanded, build the intermediate operands |
| 490 | // from the parts. |
| 491 | assert(NumParts % NumIntermediates == 0 && |
| 492 | "Must expand into a divisible number of parts!"); |
| 493 | unsigned Factor = NumParts / NumIntermediates; |
| 494 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 495 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 496 | PartVT, IntermediateVT); |
| 497 | } |
| 498 | |
| 499 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 500 | // operands. |
| 501 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 502 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 503 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 504 | } else if (PartVT.isFloatingPoint()) { |
| 505 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 506 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 507 | "Unexpected split"); |
| 508 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 509 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 510 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 511 | if (TLI.isBigEndian()) |
| 512 | std::swap(Lo, Hi); |
| 513 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 514 | } else { |
| 515 | // FP split into integer parts (soft fp) |
| 516 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 517 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 518 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 519 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 524 | PartVT = Val.getValueType(); |
| 525 | |
| 526 | if (PartVT == ValueVT) |
| 527 | return Val; |
| 528 | |
| 529 | if (PartVT.isVector()) { |
| 530 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 531 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (ValueVT.isVector()) { |
| 535 | assert(ValueVT.getVectorElementType() == PartVT && |
| 536 | ValueVT.getVectorNumElements() == 1 && |
| 537 | "Only trivial scalar-to-vector conversions should get here!"); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 538 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (PartVT.isInteger() && |
| 542 | ValueVT.isInteger()) { |
| 543 | if (ValueVT.bitsLT(PartVT)) { |
| 544 | // For a truncate, see if we have any information to |
| 545 | // indicate whether the truncated bits will always be |
| 546 | // zero or sign-extension. |
| 547 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 548 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 549 | DAG.getValueType(ValueVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 550 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 551 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 552 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 557 | if (ValueVT.bitsLT(Val.getValueType())) |
| 558 | // FP_ROUND's are always exact here. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 559 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 560 | DAG.getIntPtrConstant(1)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 561 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 565 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 566 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 567 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 568 | return SDValue(); |
| 569 | } |
| 570 | |
| 571 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 572 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 573 | /// 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] | 574 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 575 | SDValue *Parts, unsigned NumParts, EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 576 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 577 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 578 | EVT PtrVT = TLI.getPointerTy(); |
| 579 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 580 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 581 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 582 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 583 | |
| 584 | if (!NumParts) |
| 585 | return; |
| 586 | |
| 587 | if (!ValueVT.isVector()) { |
| 588 | if (PartVT == ValueVT) { |
| 589 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 590 | Parts[0] = Val; |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 595 | // If the parts cover more bits than the value has, promote the value. |
| 596 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 597 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 598 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 599 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 600 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 601 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 602 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 603 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 604 | } |
| 605 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 606 | // Different types of the same size. |
| 607 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 608 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 609 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 610 | // If the parts cover less bits than value has, truncate the value. |
| 611 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 612 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 613 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 614 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 615 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | // The value may have changed - recompute ValueVT. |
| 620 | ValueVT = Val.getValueType(); |
| 621 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 622 | "Failed to tile the value with PartVT!"); |
| 623 | |
| 624 | if (NumParts == 1) { |
| 625 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 626 | Parts[0] = Val; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | // Expand the value into multiple parts. |
| 631 | if (NumParts & (NumParts - 1)) { |
| 632 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 633 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 634 | "Do not know what to expand to!"); |
| 635 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 636 | unsigned RoundBits = RoundParts * PartBits; |
| 637 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 638 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 639 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 640 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 641 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 642 | if (TLI.isBigEndian()) |
| 643 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 644 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 645 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 646 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 647 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 651 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 652 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 653 | EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 654 | Val); |
| 655 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 656 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 657 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 658 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 659 | SDValue &Part0 = Parts[i]; |
| 660 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 661 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 662 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 663 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 664 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 665 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 666 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 667 | DAG.getConstant(0, PtrVT)); |
| 668 | |
| 669 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 670 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 671 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 672 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 673 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 679 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 680 | |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | // Vector ValueVT. |
| 685 | if (NumParts == 1) { |
| 686 | if (PartVT != ValueVT) { |
| 687 | if (PartVT.isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 688 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 689 | } else { |
| 690 | assert(ValueVT.getVectorElementType() == PartVT && |
| 691 | ValueVT.getVectorNumElements() == 1 && |
| 692 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 693 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 694 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 695 | DAG.getConstant(0, PtrVT)); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | Parts[0] = Val; |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 704 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 705 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 706 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 707 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 708 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 709 | |
| 710 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 711 | NumParts = NumRegs; // Silence a compiler warning. |
| 712 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 713 | |
| 714 | // Split the vector into intermediate operands. |
| 715 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 716 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 717 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 718 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 719 | IntermediateVT, Val, |
| 720 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 721 | PtrVT)); |
| 722 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 723 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 724 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 725 | DAG.getConstant(i, PtrVT)); |
| 726 | |
| 727 | // Split the intermediate operands into legal parts. |
| 728 | if (NumParts == NumIntermediates) { |
| 729 | // If the register was not expanded, promote or copy the value, |
| 730 | // as appropriate. |
| 731 | for (unsigned i = 0; i != NumParts; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 732 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 733 | } else if (NumParts > 0) { |
| 734 | // If the intermediate type was expanded, split each the value into |
| 735 | // legal parts. |
| 736 | assert(NumParts % NumIntermediates == 0 && |
| 737 | "Must expand into a divisible number of parts!"); |
| 738 | unsigned Factor = NumParts / NumIntermediates; |
| 739 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 740 | getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | |
| 745 | void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 746 | AA = &aa; |
| 747 | GFI = gfi; |
| 748 | TD = DAG.getTarget().getTargetData(); |
| 749 | } |
| 750 | |
| 751 | /// clear - Clear out the curret SelectionDAG and the associated |
| 752 | /// state and prepare this SelectionDAGLowering object to be used |
| 753 | /// for a new block. This doesn't clear out information about |
| 754 | /// additional blocks that are needed to complete switch lowering |
| 755 | /// or PHI node updating; that information is cleared out as it is |
| 756 | /// consumed. |
| 757 | void SelectionDAGLowering::clear() { |
| 758 | NodeMap.clear(); |
| 759 | PendingLoads.clear(); |
| 760 | PendingExports.clear(); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 761 | EdgeMapping.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 762 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 763 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 764 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 768 | /// flushing any PendingLoad items. This must be done before emitting |
| 769 | /// a store or any other node that may need to be ordered after any |
| 770 | /// prior load instructions. |
| 771 | /// |
| 772 | SDValue SelectionDAGLowering::getRoot() { |
| 773 | if (PendingLoads.empty()) |
| 774 | return DAG.getRoot(); |
| 775 | |
| 776 | if (PendingLoads.size() == 1) { |
| 777 | SDValue Root = PendingLoads[0]; |
| 778 | DAG.setRoot(Root); |
| 779 | PendingLoads.clear(); |
| 780 | return Root; |
| 781 | } |
| 782 | |
| 783 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 784 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 785 | &PendingLoads[0], PendingLoads.size()); |
| 786 | PendingLoads.clear(); |
| 787 | DAG.setRoot(Root); |
| 788 | return Root; |
| 789 | } |
| 790 | |
| 791 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 792 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 793 | /// to do this before emitting a terminator instruction. |
| 794 | /// |
| 795 | SDValue SelectionDAGLowering::getControlRoot() { |
| 796 | SDValue Root = DAG.getRoot(); |
| 797 | |
| 798 | if (PendingExports.empty()) |
| 799 | return Root; |
| 800 | |
| 801 | // Turn all of the CopyToReg chains into one factored node. |
| 802 | if (Root.getOpcode() != ISD::EntryToken) { |
| 803 | unsigned i = 0, e = PendingExports.size(); |
| 804 | for (; i != e; ++i) { |
| 805 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 806 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 807 | break; // Don't add the root if we already indirectly depend on it. |
| 808 | } |
| 809 | |
| 810 | if (i == e) |
| 811 | PendingExports.push_back(Root); |
| 812 | } |
| 813 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 814 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 815 | &PendingExports[0], |
| 816 | PendingExports.size()); |
| 817 | PendingExports.clear(); |
| 818 | DAG.setRoot(Root); |
| 819 | return Root; |
| 820 | } |
| 821 | |
| 822 | void SelectionDAGLowering::visit(Instruction &I) { |
| 823 | visit(I.getOpcode(), I); |
| 824 | } |
| 825 | |
| 826 | void SelectionDAGLowering::visit(unsigned Opcode, User &I) { |
| 827 | // Note: this doesn't use InstVisitor, because it has to work with |
| 828 | // ConstantExpr's in addition to instructions. |
| 829 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 830 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 831 | // Build the switch statement using the Instruction.def file. |
| 832 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 833 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 834 | #include "llvm/Instruction.def" |
| 835 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 836 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 837 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 838 | SDValue SelectionDAGLowering::getValue(const Value *V) { |
| 839 | SDValue &N = NodeMap[V]; |
| 840 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 841 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 842 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 843 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 844 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 845 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 846 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 847 | |
| 848 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 849 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 850 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 851 | if (isa<ConstantPointerNull>(C)) |
| 852 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 854 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 855 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 856 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 857 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 858 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | |
| 860 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 861 | visit(CE->getOpcode(), *CE); |
| 862 | SDValue N1 = NodeMap[V]; |
| 863 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 864 | return N1; |
| 865 | } |
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<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 868 | SmallVector<SDValue, 4> Constants; |
| 869 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 870 | OI != OE; ++OI) { |
| 871 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 872 | // If the operand is an empty aggregate, there are no values. |
| 873 | if (!Val) continue; |
| 874 | // Add each leaf value from the operand to the Constants list |
| 875 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 876 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 877 | Constants.push_back(SDValue(Val, i)); |
| 878 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 879 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 880 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 884 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 885 | "Unknown struct or array constant!"); |
| 886 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 887 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 888 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 889 | unsigned NumElts = ValueVTs.size(); |
| 890 | if (NumElts == 0) |
| 891 | return SDValue(); // empty struct |
| 892 | SmallVector<SDValue, 4> Constants(NumElts); |
| 893 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 894 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 895 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 896 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 897 | else if (EltVT.isFloatingPoint()) |
| 898 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 899 | else |
| 900 | Constants[i] = DAG.getConstant(0, EltVT); |
| 901 | } |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 902 | return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 905 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 906 | return DAG.getBlockAddress(BA, getCurDebugLoc()); |
| 907 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 908 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 909 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 910 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 911 | // Now that we know the number and type of the elements, get that number of |
| 912 | // elements into the Ops array based on what kind of constant it is. |
| 913 | SmallVector<SDValue, 16> Ops; |
| 914 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 915 | for (unsigned i = 0; i != NumElements; ++i) |
| 916 | Ops.push_back(getValue(CP->getOperand(i))); |
| 917 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 918 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 919 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 920 | |
| 921 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 922 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 923 | Op = DAG.getConstantFP(0, EltVT); |
| 924 | else |
| 925 | Op = DAG.getConstant(0, EltVT); |
| 926 | Ops.assign(NumElements, Op); |
| 927 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 928 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 929 | // Create a BUILD_VECTOR node. |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 930 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 931 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 932 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 933 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 934 | // If this is a static alloca, generate it as the frameindex instead of |
| 935 | // computation. |
| 936 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 937 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 938 | FuncInfo.StaticAllocaMap.find(AI); |
| 939 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 940 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 941 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 942 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 943 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 944 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 945 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 946 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 947 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 948 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 951 | /// Get the EVTs and ArgFlags collections that represent the return type |
| 952 | /// of the given function. This does not require a DAG or a return value, and |
| 953 | /// is suitable for use before any DAGs for the function are constructed. |
| 954 | static void getReturnInfo(const Function* F, SmallVectorImpl<EVT> &OutVTs, |
| 955 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
| 956 | TargetLowering &TLI) { |
| 957 | const Type* ReturnType = F->getReturnType(); |
| 958 | |
| 959 | SmallVector<EVT, 4> ValueVTs; |
| 960 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
| 961 | unsigned NumValues = ValueVTs.size(); |
| 962 | if ( NumValues == 0 ) return; |
| 963 | |
| 964 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 965 | EVT VT = ValueVTs[j]; |
| 966 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 967 | |
| 968 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 969 | ExtendKind = ISD::SIGN_EXTEND; |
| 970 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 971 | ExtendKind = ISD::ZERO_EXTEND; |
| 972 | |
| 973 | // FIXME: C calling convention requires the return type to be promoted to |
| 974 | // at least 32-bit. But this is not necessary for non-C calling |
| 975 | // conventions. The frontend should mark functions whose return values |
| 976 | // require promoting with signext or zeroext attributes. |
| 977 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 978 | EVT MinVT = TLI.getRegisterType(F->getContext(), MVT::i32); |
| 979 | if (VT.bitsLT(MinVT)) |
| 980 | VT = MinVT; |
| 981 | } |
| 982 | |
| 983 | unsigned NumParts = TLI.getNumRegisters(F->getContext(), VT); |
| 984 | EVT PartVT = TLI.getRegisterType(F->getContext(), VT); |
| 985 | // 'inreg' on function refers to return value |
| 986 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 987 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 988 | Flags.setInReg(); |
| 989 | |
| 990 | // Propagate extension type if any |
| 991 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 992 | Flags.setSExt(); |
| 993 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 994 | Flags.setZExt(); |
| 995 | |
| 996 | for (unsigned i = 0; i < NumParts; ++i) |
| 997 | { |
| 998 | OutVTs.push_back(PartVT); |
| 999 | OutFlags.push_back(Flags); |
| 1000 | } |
| 1001 | } |
| 1002 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1003 | |
| 1004 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1005 | SDValue Chain = getControlRoot(); |
| 1006 | SmallVector<ISD::OutputArg, 8> Outs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1007 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1008 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1009 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1010 | unsigned NumValues = ValueVTs.size(); |
| 1011 | if (NumValues == 0) continue; |
| 1012 | |
| 1013 | SDValue RetOp = getValue(I.getOperand(i)); |
| 1014 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1015 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1016 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1017 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1018 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1019 | const Function *F = I.getParent()->getParent(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1020 | if (F->paramHasAttr(0, Attribute::SExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1021 | ExtendKind = ISD::SIGN_EXTEND; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1022 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1023 | ExtendKind = ISD::ZERO_EXTEND; |
| 1024 | |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1025 | // FIXME: C calling convention requires the return type to be promoted to |
| 1026 | // at least 32-bit. But this is not necessary for non-C calling |
| 1027 | // conventions. The frontend should mark functions whose return values |
| 1028 | // require promoting with signext or zeroext attributes. |
| 1029 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1030 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1031 | if (VT.bitsLT(MinVT)) |
| 1032 | VT = MinVT; |
| 1033 | } |
| 1034 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1035 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 1036 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1037 | SmallVector<SDValue, 4> Parts(NumParts); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1038 | getCopyToParts(DAG, getCurDebugLoc(), |
| 1039 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1040 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 1041 | |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1042 | // 'inreg' on function refers to return value |
| 1043 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1044 | if (F->paramHasAttr(0, Attribute::InReg)) |
Dale Johannesen | c9c6da6 | 2008-09-25 20:47:45 +0000 | [diff] [blame] | 1045 | Flags.setInReg(); |
Anton Korobeynikov | 0692fab | 2009-07-16 13:35:48 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Propagate extension type if any |
| 1048 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 1049 | Flags.setSExt(); |
| 1050 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 1051 | Flags.setZExt(); |
| 1052 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1053 | for (unsigned i = 0; i < NumParts; ++i) |
| 1054 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1057 | |
| 1058 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1059 | CallingConv::ID CallConv = |
| 1060 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1061 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 1062 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1063 | |
| 1064 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1065 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1066 | "LowerReturn didn't return a valid chain!"); |
| 1067 | |
| 1068 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1069 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1072 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1073 | /// created for it, emit nodes to copy the value into the virtual |
| 1074 | /// registers. |
| 1075 | void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) { |
| 1076 | if (!V->use_empty()) { |
| 1077 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1078 | if (VMI != FuncInfo.ValueMap.end()) |
| 1079 | CopyValueToVirtualRegister(V, VMI->second); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1083 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1084 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1085 | /// CopyTo/FromReg. |
| 1086 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 1087 | // No need to export constants. |
| 1088 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1089 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1090 | // Already exported? |
| 1091 | if (FuncInfo.isExportedInst(V)) return; |
| 1092 | |
| 1093 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1094 | CopyValueToVirtualRegister(V, Reg); |
| 1095 | } |
| 1096 | |
| 1097 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 1098 | const BasicBlock *FromBB) { |
| 1099 | // The operands of the setcc have to be in this block. We don't know |
| 1100 | // how to export them from some other block. |
| 1101 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 1102 | // Can export from current BB. |
| 1103 | if (VI->getParent() == FromBB) |
| 1104 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1105 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1106 | // Is already exported, noop. |
| 1107 | return FuncInfo.isExportedInst(V); |
| 1108 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1109 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1110 | // If this is an argument, we can export it if the BB is the entry block or |
| 1111 | // if it is already exported. |
| 1112 | if (isa<Argument>(V)) { |
| 1113 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1114 | return true; |
| 1115 | |
| 1116 | // Otherwise, can only export this if it is already exported. |
| 1117 | return FuncInfo.isExportedInst(V); |
| 1118 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1119 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1120 | // Otherwise, constants can always be exported. |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
| 1124 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1125 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1126 | return I->getParent() == BB; |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1130 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 1131 | /// the given LLVM IR floating-point condition code. This includes |
| 1132 | /// consideration of global floating-point math flags. |
| 1133 | /// |
| 1134 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 1135 | ISD::CondCode FPC, FOC; |
| 1136 | switch (Pred) { |
| 1137 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1138 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1139 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1140 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1141 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1142 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1143 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1144 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1145 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 1146 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1147 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1148 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1149 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1150 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1151 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1152 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1153 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1154 | llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1155 | FOC = FPC = ISD::SETFALSE; |
| 1156 | break; |
| 1157 | } |
| 1158 | if (FiniteOnlyFPMath()) |
| 1159 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1160 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1161 | return FPC; |
| 1162 | } |
| 1163 | |
| 1164 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1165 | /// the given LLVM IR integer condition code. |
| 1166 | /// |
| 1167 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1168 | switch (Pred) { |
| 1169 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1170 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1171 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1172 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1173 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1174 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1175 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1176 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1177 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1178 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1179 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1180 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1181 | return ISD::SETNE; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1185 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1186 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1187 | /// AND operator tree. |
| 1188 | /// |
| 1189 | void |
| 1190 | SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond, |
| 1191 | MachineBasicBlock *TBB, |
| 1192 | MachineBasicBlock *FBB, |
| 1193 | MachineBasicBlock *CurBB) { |
| 1194 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1195 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1196 | // If the leaf of the tree is a comparison, merge the condition into |
| 1197 | // the caseblock. |
| 1198 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1199 | // The operands of the cmp have to be in this block. We don't know |
| 1200 | // how to export them from some other block. If this is the first block |
| 1201 | // of the sequence, no exporting is needed. |
| 1202 | if (CurBB == CurMBB || |
| 1203 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1204 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1205 | ISD::CondCode Condition; |
| 1206 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1207 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1208 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1209 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1210 | } else { |
| 1211 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1212 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1213 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1214 | |
| 1215 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1216 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1217 | SwitchCases.push_back(CB); |
| 1218 | return; |
| 1219 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1223 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1224 | NULL, TBB, FBB, CurBB); |
| 1225 | SwitchCases.push_back(CB); |
| 1226 | } |
| 1227 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1228 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1229 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 1230 | MachineBasicBlock *TBB, |
| 1231 | MachineBasicBlock *FBB, |
| 1232 | MachineBasicBlock *CurBB, |
| 1233 | unsigned Opc) { |
| 1234 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1235 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1236 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1237 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1238 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1239 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1240 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1241 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1242 | return; |
| 1243 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1244 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1245 | // Create TmpBB after CurBB. |
| 1246 | MachineFunction::iterator BBI = CurBB; |
| 1247 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1248 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1249 | CurBB->getParent()->insert(++BBI, TmpBB); |
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 | if (Opc == Instruction::Or) { |
| 1252 | // Codegen X | Y as: |
| 1253 | // jmp_if_X TBB |
| 1254 | // jmp TmpBB |
| 1255 | // TmpBB: |
| 1256 | // jmp_if_Y TBB |
| 1257 | // jmp FBB |
| 1258 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1259 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1260 | // Emit the LHS condition. |
| 1261 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1262 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1263 | // Emit the RHS condition into TmpBB. |
| 1264 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1265 | } else { |
| 1266 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1267 | // Codegen X & Y as: |
| 1268 | // jmp_if_X TmpBB |
| 1269 | // jmp FBB |
| 1270 | // TmpBB: |
| 1271 | // jmp_if_Y TBB |
| 1272 | // jmp FBB |
| 1273 | // |
| 1274 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1275 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1276 | // Emit the LHS condition. |
| 1277 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1278 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1279 | // Emit the RHS condition into TmpBB. |
| 1280 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1285 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1286 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1287 | bool |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1288 | SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1289 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1290 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1291 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1292 | // will get folded into a single comparison, so don't emit two blocks. |
| 1293 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1294 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1295 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1296 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1297 | return false; |
| 1298 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1299 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1300 | return true; |
| 1301 | } |
| 1302 | |
| 1303 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1304 | // Update machine-CFG edges. |
| 1305 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1306 | |
| 1307 | // Figure out which block is immediately after the current one. |
| 1308 | MachineBasicBlock *NextBlock = 0; |
| 1309 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1310 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1311 | NextBlock = BBI; |
| 1312 | |
| 1313 | if (I.isUnconditional()) { |
| 1314 | // Update machine-CFG edges. |
| 1315 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1316 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1317 | // If this is not a fall-through branch, emit the branch. |
| 1318 | if (Succ0MBB != NextBlock) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1319 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1320 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1321 | DAG.getBasicBlock(Succ0MBB))); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
| 1325 | // If this condition is one of the special cases we handle, do special stuff |
| 1326 | // now. |
| 1327 | Value *CondVal = I.getCondition(); |
| 1328 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1329 | |
| 1330 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1331 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1332 | // For example, instead of something like: |
| 1333 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1334 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1335 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1336 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1337 | // or C, F |
| 1338 | // jnz foo |
| 1339 | // Emit: |
| 1340 | // cmp A, B |
| 1341 | // je foo |
| 1342 | // cmp D, E |
| 1343 | // jle foo |
| 1344 | // |
| 1345 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1346 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1347 | (BOp->getOpcode() == Instruction::And || |
| 1348 | BOp->getOpcode() == Instruction::Or)) { |
| 1349 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1350 | // If the compares in later blocks need to use values not currently |
| 1351 | // exported from this block, export them now. This block should always |
| 1352 | // be the first entry. |
| 1353 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1354 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1355 | // Allow some cases to be rejected. |
| 1356 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1357 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1358 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1359 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1360 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1361 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1362 | // Emit the branch for this block. |
| 1363 | visitSwitchCase(SwitchCases[0]); |
| 1364 | SwitchCases.erase(SwitchCases.begin()); |
| 1365 | return; |
| 1366 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1367 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1368 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1369 | // SwitchCases. |
| 1370 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1371 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1372 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1373 | SwitchCases.clear(); |
| 1374 | } |
| 1375 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1376 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1377 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1378 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1379 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
| 1380 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1381 | // cond branch. |
| 1382 | visitSwitchCase(CB); |
| 1383 | } |
| 1384 | |
| 1385 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1386 | /// the binary search tree resulting from lowering a switch instruction. |
| 1387 | void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) { |
| 1388 | SDValue Cond; |
| 1389 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1390 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1393 | if (CB.CmpMHS == NULL) { |
| 1394 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1395 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1396 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1397 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1398 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1399 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1400 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1401 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1402 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1403 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1404 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1405 | } else { |
| 1406 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1407 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1408 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1409 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1410 | |
| 1411 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1412 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1415 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1416 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1417 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1418 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1419 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1420 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1421 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1422 | } |
| 1423 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1424 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1425 | // Update successor info |
| 1426 | CurMBB->addSuccessor(CB.TrueBB); |
| 1427 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1428 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1429 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1430 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1431 | MachineBasicBlock *NextBlock = 0; |
| 1432 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1433 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1434 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1435 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1436 | // If the lhs block is the next block, invert the condition so that we can |
| 1437 | // fall through to the lhs instead of the rhs block. |
| 1438 | if (CB.TrueBB == NextBlock) { |
| 1439 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1440 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1441 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1442 | } |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1443 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1444 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1445 | DAG.getBasicBlock(CB.TrueBB)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1446 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1447 | // If the branch was constant folded, fix up the CFG. |
| 1448 | if (BrCond.getOpcode() == ISD::BR) { |
| 1449 | CurMBB->removeSuccessor(CB.FalseBB); |
| 1450 | DAG.setRoot(BrCond); |
| 1451 | } else { |
| 1452 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1453 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1454 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1455 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1456 | if (CB.FalseBB == NextBlock) |
| 1457 | DAG.setRoot(BrCond); |
| 1458 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1459 | DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1460 | DAG.getBasicBlock(CB.FalseBB))); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1465 | void SelectionDAGLowering::visitJumpTable(JumpTable &JT) { |
| 1466 | // Emit the code for the jump table |
| 1467 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1468 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1469 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1470 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1471 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1472 | DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1473 | MVT::Other, Index.getValue(1), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1474 | Table, Index)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1478 | /// in the JumpTable from switch case. |
| 1479 | void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT, |
| 1480 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1481 | // Subtract the lowest switch case value from the value being switched on and |
| 1482 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1483 | // difference between smallest and largest cases. |
| 1484 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1485 | EVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1486 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1487 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1488 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1489 | // The SDNode we just created, which holds the value being switched on minus |
| 1490 | // the the smallest case value, needs to be copied to a virtual register so it |
| 1491 | // can be used as an index into the jump table in a subsequent basic block. |
| 1492 | // This value may be smaller or larger than the target's pointer type, and |
| 1493 | // therefore require extension or truncating. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 1494 | SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1495 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1496 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1497 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1498 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1499 | JT.Reg = JumpTableReg; |
| 1500 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1501 | // Emit the range check for the jump table, and branch to the default block |
| 1502 | // for the switch statement if the value being switched on exceeds the largest |
| 1503 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1504 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
| 1505 | TLI.getSetCCResultType(SUB.getValueType()), SUB, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1506 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1507 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1510 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1511 | MachineBasicBlock *NextBlock = 0; |
| 1512 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1513 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1514 | NextBlock = BBI; |
| 1515 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1516 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1517 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1518 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (JT.MBB == NextBlock) |
| 1521 | DAG.setRoot(BrCond); |
| 1522 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1523 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1524 | DAG.getBasicBlock(JT.MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1528 | /// suitable for "bit tests" |
| 1529 | void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) { |
| 1530 | // Subtract the minimum value |
| 1531 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1532 | EVT VT = SwitchOp.getValueType(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1533 | SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1534 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1537 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1538 | TLI.getSetCCResultType(SUB.getValueType()), |
| 1539 | SUB, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1540 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1541 | |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 1542 | SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1543 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1544 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1545 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1546 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1549 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1550 | MachineBasicBlock *NextBlock = 0; |
| 1551 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1552 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1553 | NextBlock = BBI; |
| 1554 | |
| 1555 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1556 | |
| 1557 | CurMBB->addSuccessor(B.Default); |
| 1558 | CurMBB->addSuccessor(MBB); |
| 1559 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1560 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1561 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1562 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1563 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1564 | if (MBB == NextBlock) |
| 1565 | DAG.setRoot(BrRange); |
| 1566 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1567 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1568 | DAG.getBasicBlock(MBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /// visitBitTestCase - this function produces one "bit test" |
| 1572 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1573 | unsigned Reg, |
| 1574 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1575 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1576 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1577 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1578 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1579 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1580 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1581 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1582 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1583 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1584 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1585 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1586 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1587 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1588 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1589 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1590 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1591 | |
| 1592 | CurMBB->addSuccessor(B.TargetBB); |
| 1593 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1594 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1595 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1596 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1597 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1598 | |
| 1599 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1600 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1601 | MachineBasicBlock *NextBlock = 0; |
| 1602 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1603 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1604 | NextBlock = BBI; |
| 1605 | |
| 1606 | if (NextMBB == NextBlock) |
| 1607 | DAG.setRoot(BrAnd); |
| 1608 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1609 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1610 | DAG.getBasicBlock(NextMBB))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1614 | // Retrieve successors. |
| 1615 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1616 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1617 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1618 | const Value *Callee(I.getCalledValue()); |
| 1619 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1620 | visitInlineAsm(&I); |
| 1621 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1622 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1623 | |
| 1624 | // If the value of the invoke is used outside of its defining block, make it |
| 1625 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1626 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1627 | |
| 1628 | // Update successor info |
| 1629 | CurMBB->addSuccessor(Return); |
| 1630 | CurMBB->addSuccessor(LandingPad); |
| 1631 | |
| 1632 | // Drop into normal successor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1633 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1634 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1635 | DAG.getBasicBlock(Return))); |
| 1636 | } |
| 1637 | |
| 1638 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1639 | } |
| 1640 | |
| 1641 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1642 | /// small case ranges). |
| 1643 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
| 1644 | CaseRecVector& WorkList, |
| 1645 | Value* SV, |
| 1646 | MachineBasicBlock* Default) { |
| 1647 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1649 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1650 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1651 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1652 | return false; |
| 1653 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1654 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1655 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1656 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1657 | |
| 1658 | // Figure out which block is immediately after the current one. |
| 1659 | MachineBasicBlock *NextBlock = 0; |
| 1660 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1661 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1662 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1663 | NextBlock = BBI; |
| 1664 | |
| 1665 | // TODO: If any two of the cases has the same destination, and if one value |
| 1666 | // is the same as the other, but has one bit unset that the other has set, |
| 1667 | // use bit manipulation to do two compares at once. For example: |
| 1668 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1669 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1670 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1671 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1672 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1673 | // branches in this order. See if rearranging a case value would help. |
| 1674 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1675 | if (I->BB == NextBlock) { |
| 1676 | std::swap(*I, BackCase); |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1681 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1682 | // Create a CaseBlock record representing a conditional branch to |
| 1683 | // the Case's target mbb if the value being switched on SV is equal |
| 1684 | // to C. |
| 1685 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1686 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1687 | MachineBasicBlock *FallThrough; |
| 1688 | if (I != E-1) { |
| 1689 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1690 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Put SV in a virtual register to make it available from the new blocks. |
| 1693 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1694 | } else { |
| 1695 | // If the last case doesn't match, go to the default block. |
| 1696 | FallThrough = Default; |
| 1697 | } |
| 1698 | |
| 1699 | Value *RHS, *LHS, *MHS; |
| 1700 | ISD::CondCode CC; |
| 1701 | if (I->High == I->Low) { |
| 1702 | // This is just small small case range :) containing exactly 1 case |
| 1703 | CC = ISD::SETEQ; |
| 1704 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1705 | } else { |
| 1706 | CC = ISD::SETLE; |
| 1707 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1708 | } |
| 1709 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1710 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1711 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1712 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1713 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1714 | // before the next MBB. |
| 1715 | if (CurBlock == CurMBB) |
| 1716 | visitSwitchCase(CB); |
| 1717 | else |
| 1718 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1719 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1720 | CurBlock = FallThrough; |
| 1721 | } |
| 1722 | |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1727 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1728 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1729 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1730 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1731 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1732 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1733 | APInt LastExt(Last), FirstExt(First); |
| 1734 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1735 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1736 | return (LastExt - FirstExt + 1ULL); |
| 1737 | } |
| 1738 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1739 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1740 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
| 1741 | CaseRecVector& WorkList, |
| 1742 | Value* SV, |
| 1743 | MachineBasicBlock* Default) { |
| 1744 | Case& FrontCase = *CR.Range.first; |
| 1745 | Case& BackCase = *(CR.Range.second-1); |
| 1746 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1747 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1748 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1750 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1751 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1752 | I!=E; ++I) |
| 1753 | TSize += I->size(); |
| 1754 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1755 | if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4))) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1756 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1757 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1758 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1759 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1760 | if (Density < 0.4) |
| 1761 | return false; |
| 1762 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1763 | DEBUG(errs() << "Lowering jump table\n" |
| 1764 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1765 | << "Range: " << Range |
| 1766 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1769 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1770 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1773 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1774 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1775 | |
| 1776 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1777 | |
| 1778 | // Create a new basic block to hold the code for loading the address |
| 1779 | // of the jump table, and jumping to it. Update successor information; |
| 1780 | // we will either branch to the default case for the switch, or the jump |
| 1781 | // table. |
| 1782 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1783 | CurMF->insert(BBI, JumpTableBB); |
| 1784 | CR.CaseBB->addSuccessor(Default); |
| 1785 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1786 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1787 | // Build a vector of destination BBs, corresponding to each target |
| 1788 | // of the jump table. If the value of the jump table slot corresponds to |
| 1789 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1790 | // the default BB. |
| 1791 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1792 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1793 | 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] | 1794 | const APInt& Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1795 | const APInt& High = cast<ConstantInt>(I->High)->getValue(); |
| 1796 | |
| 1797 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1798 | DestBBs.push_back(I->BB); |
| 1799 | if (TEI==High) |
| 1800 | ++I; |
| 1801 | } else { |
| 1802 | DestBBs.push_back(Default); |
| 1803 | } |
| 1804 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1805 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1806 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1807 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1808 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1809 | E = DestBBs.end(); I != E; ++I) { |
| 1810 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1811 | SuccsHandled[(*I)->getNumber()] = true; |
| 1812 | JumpTableBB->addSuccessor(*I); |
| 1813 | } |
| 1814 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1815 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1816 | // Create a jump table index for this jump table, or return an existing |
| 1817 | // one. |
| 1818 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1819 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1820 | // Set the jump table information so that we can codegen it as a second |
| 1821 | // MachineBasicBlock |
| 1822 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1823 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1824 | if (CR.CaseBB == CurMBB) |
| 1825 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1826 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1827 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1828 | |
| 1829 | return true; |
| 1830 | } |
| 1831 | |
| 1832 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1833 | /// 2 subtrees. |
| 1834 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
| 1835 | CaseRecVector& WorkList, |
| 1836 | Value* SV, |
| 1837 | MachineBasicBlock* Default) { |
| 1838 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1839 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1840 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1843 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1844 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1845 | |
| 1846 | Case& FrontCase = *CR.Range.first; |
| 1847 | Case& BackCase = *(CR.Range.second-1); |
| 1848 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1849 | |
| 1850 | // Size is the number of Cases represented by this range. |
| 1851 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1852 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1853 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1854 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1855 | double FMetric = 0; |
| 1856 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1857 | |
| 1858 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1859 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1860 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1861 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1862 | I!=E; ++I) |
| 1863 | TSize += I->size(); |
| 1864 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1865 | APInt LSize = FrontCase.size(); |
| 1866 | APInt RSize = TSize-LSize; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1867 | DEBUG(errs() << "Selecting best pivot: \n" |
| 1868 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1869 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1870 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1871 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1872 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1873 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1874 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1875 | assert((Range - 2ULL).isNonNegative() && |
| 1876 | "Invalid case distance"); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1877 | double LDensity = (double)LSize.roundToDouble() / |
| 1878 | (LEnd - First + 1ULL).roundToDouble(); |
| 1879 | double RDensity = (double)RSize.roundToDouble() / |
| 1880 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1881 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1882 | // Should always split in some non-trivial place |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1883 | DEBUG(errs() <<"=>Step\n" |
| 1884 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1885 | << "LDensity: " << LDensity |
| 1886 | << ", RDensity: " << RDensity << '\n' |
| 1887 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1888 | if (FMetric < Metric) { |
| 1889 | Pivot = J; |
| 1890 | FMetric = Metric; |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1891 | DEBUG(errs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | LSize += J->size(); |
| 1895 | RSize -= J->size(); |
| 1896 | } |
| 1897 | if (areJTsAllowed(TLI)) { |
| 1898 | // If our case is dense we *really* should handle it earlier! |
| 1899 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1900 | } else { |
| 1901 | Pivot = CR.Range.first + Size/2; |
| 1902 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1903 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1904 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1905 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1906 | Constant *C = Pivot->Low; |
| 1907 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1908 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1909 | // 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] | 1910 | // 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] | 1911 | // 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] | 1912 | // 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] | 1913 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1914 | // rather than creating a leaf node for it. |
| 1915 | if ((LHSR.second - LHSR.first) == 1 && |
| 1916 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1917 | cast<ConstantInt>(C)->getValue() == |
| 1918 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1919 | TrueBB = LHSR.first->BB; |
| 1920 | } else { |
| 1921 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1922 | CurMF->insert(BBI, TrueBB); |
| 1923 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1924 | |
| 1925 | // Put SV in a virtual register to make it available from the new blocks. |
| 1926 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1927 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1928 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1929 | // Similar to the optimization above, if the Value being switched on is |
| 1930 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1931 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1932 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1933 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1934 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1935 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1936 | FalseBB = RHSR.first->BB; |
| 1937 | } else { |
| 1938 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1939 | CurMF->insert(BBI, FalseBB); |
| 1940 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Put SV in a virtual register to make it available from the new blocks. |
| 1943 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1947 | // 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] | 1948 | // Otherwise, branch to LHS. |
| 1949 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1950 | |
| 1951 | if (CR.CaseBB == CurMBB) |
| 1952 | visitSwitchCase(CB); |
| 1953 | else |
| 1954 | SwitchCases.push_back(CB); |
| 1955 | |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
| 1959 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1960 | /// range span less, than machine word bitwidth, encode case range into series |
| 1961 | /// of masks and emit bit tests with these masks. |
| 1962 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1963 | CaseRecVector& WorkList, |
| 1964 | Value* SV, |
| 1965 | MachineBasicBlock* Default){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1966 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1967 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1968 | |
| 1969 | Case& FrontCase = *CR.Range.first; |
| 1970 | Case& BackCase = *(CR.Range.second-1); |
| 1971 | |
| 1972 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1973 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1974 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1975 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1976 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1977 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1978 | return false; |
| 1979 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1980 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1981 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1982 | I!=E; ++I) { |
| 1983 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1984 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1985 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1986 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1987 | // Count unique destinations |
| 1988 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1989 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1990 | Dests.insert(I->BB); |
| 1991 | if (Dests.size() > 3) |
| 1992 | // Don't bother the code below, if there are too much unique destinations |
| 1993 | return false; |
| 1994 | } |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1995 | DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n' |
| 1996 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1997 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1998 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1999 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2000 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2001 | APInt cmpRange = maxValue - minValue; |
| 2002 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2003 | DEBUG(errs() << "Compare range: " << cmpRange << '\n' |
| 2004 | << "Low bound: " << minValue << '\n' |
| 2005 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2006 | |
| 2007 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2008 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2009 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2010 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2011 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2012 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2013 | DEBUG(errs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2014 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2015 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2016 | // Optimize the case where all the case values fit in a |
| 2017 | // word without having to subtract minValue. In this case, |
| 2018 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2019 | if (minValue.isNonNegative() && |
| 2020 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 2021 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2022 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2023 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2024 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2025 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2026 | CaseBitsVector CasesBits; |
| 2027 | unsigned i, count = 0; |
| 2028 | |
| 2029 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2030 | MachineBasicBlock* Dest = I->BB; |
| 2031 | for (i = 0; i < count; ++i) |
| 2032 | if (Dest == CasesBits[i].BB) |
| 2033 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2034 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2035 | if (i == count) { |
| 2036 | assert((count < 3) && "Too much destinations to test!"); |
| 2037 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 2038 | count++; |
| 2039 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2040 | |
| 2041 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2042 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2043 | |
| 2044 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2045 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 2046 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2047 | for (uint64_t j = lo; j <= hi; j++) { |
| 2048 | CasesBits[i].Mask |= 1ULL << j; |
| 2049 | CasesBits[i].Bits++; |
| 2050 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2051 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2052 | } |
| 2053 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2054 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2055 | BitTestInfo BTC; |
| 2056 | |
| 2057 | // Figure out which block is immediately after the current one. |
| 2058 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2059 | ++BBI; |
| 2060 | |
| 2061 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2062 | |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2063 | DEBUG(errs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2064 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2065 | DEBUG(errs() << "Mask: " << CasesBits[i].Mask |
| 2066 | << ", Bits: " << CasesBits[i].Bits |
| 2067 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2068 | |
| 2069 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2070 | CurMF->insert(BBI, CaseBB); |
| 2071 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2072 | CaseBB, |
| 2073 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Put SV in a virtual register to make it available from the new blocks. |
| 2076 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2077 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2078 | |
| 2079 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2080 | -1U, (CR.CaseBB == CurMBB), |
| 2081 | CR.CaseBB, Default, BTC); |
| 2082 | |
| 2083 | if (CR.CaseBB == CurMBB) |
| 2084 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2085 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2086 | BitTestCases.push_back(BTB); |
| 2087 | |
| 2088 | return true; |
| 2089 | } |
| 2090 | |
| 2091 | |
| 2092 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2093 | size_t SelectionDAGLowering::Clusterify(CaseVector& Cases, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2095 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2098 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2099 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2100 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2101 | SI.getSuccessorValue(i), |
| 2102 | SMBB)); |
| 2103 | } |
| 2104 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 2105 | |
| 2106 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2107 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | // Must recompute end() each iteration because it may be |
| 2109 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2110 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 2111 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2112 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2113 | MachineBasicBlock* nextBB = J->BB; |
| 2114 | MachineBasicBlock* currentBB = I->BB; |
| 2115 | |
| 2116 | // If the two neighboring cases go to the same destination, merge them |
| 2117 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2118 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2119 | I->High = J->High; |
| 2120 | J = Cases.erase(J); |
| 2121 | } else { |
| 2122 | I = J++; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2127 | if (I->Low != I->High) |
| 2128 | // A range counts double, since it requires two compares. |
| 2129 | ++numCmps; |
| 2130 | } |
| 2131 | |
| 2132 | return numCmps; |
| 2133 | } |
| 2134 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2135 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2136 | // Figure out which block is immediately after the current one. |
| 2137 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2138 | |
| 2139 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2140 | |
| 2141 | // If there is only the default destination, branch to it if it is not the |
| 2142 | // next basic block. Otherwise, just fall through. |
| 2143 | if (SI.getNumOperands() == 2) { |
| 2144 | // Update machine-CFG edges. |
| 2145 | |
| 2146 | // If this is not a fall-through branch, emit the branch. |
| 2147 | CurMBB->addSuccessor(Default); |
| 2148 | if (Default != NextBlock) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2149 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2150 | MVT::Other, getControlRoot(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2151 | DAG.getBasicBlock(Default))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2152 | return; |
| 2153 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2154 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2155 | // If there are any non-default case statements, create a vector of Cases |
| 2156 | // representing each one, and sort the vector so that we can efficiently |
| 2157 | // create a binary search tree from them. |
| 2158 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2159 | size_t numCmps = Clusterify(Cases, SI); |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2160 | DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size() |
| 2161 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2162 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Get the Value to be switched on and default basic blocks, which will be |
| 2165 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2166 | // search tree. |
| 2167 | Value *SV = SI.getOperand(0); |
| 2168 | |
| 2169 | // Push the initial CaseRec onto the worklist |
| 2170 | CaseRecVector WorkList; |
| 2171 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2172 | |
| 2173 | while (!WorkList.empty()) { |
| 2174 | // Grab a record representing a case range to process off the worklist |
| 2175 | CaseRec CR = WorkList.back(); |
| 2176 | WorkList.pop_back(); |
| 2177 | |
| 2178 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2179 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2180 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2181 | // If the range has few cases (two or less) emit a series of specific |
| 2182 | // tests. |
| 2183 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2184 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2185 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2186 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2187 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2188 | // lowering the switch to a binary tree of conditional branches. |
| 2189 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2190 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2191 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2192 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2193 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2194 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2195 | } |
| 2196 | } |
| 2197 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 2198 | void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) { |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2199 | // Update machine-CFG edges. |
| 2200 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
| 2201 | CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]); |
| 2202 | |
Dan Gohman | 6482515 | 2009-10-27 21:56:26 +0000 | [diff] [blame] | 2203 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2204 | MVT::Other, getControlRoot(), |
| 2205 | getValue(I.getAddress()))); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2208 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2209 | void SelectionDAGLowering::visitFSub(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2210 | // -0.0 - X --> fneg |
| 2211 | const Type *Ty = I.getType(); |
| 2212 | if (isa<VectorType>(Ty)) { |
| 2213 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2214 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2215 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2216 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2217 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2218 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2219 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2220 | SDValue Op2 = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2221 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2222 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2223 | return; |
| 2224 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2225 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2226 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2227 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2228 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2229 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2230 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2231 | Op2.getValueType(), Op2)); |
| 2232 | return; |
| 2233 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2234 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2235 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
| 2239 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2240 | SDValue Op2 = getValue(I.getOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2241 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2242 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2243 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2247 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2248 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2249 | if (!isa<VectorType>(I.getType()) && |
| 2250 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2251 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2252 | EVT PTy = TLI.getPointerTy(); |
| 2253 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2254 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2255 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2256 | TLI.getShiftAmountTy(), Op2); |
| 2257 | // If the operand is larger than the shift count type but the shift |
| 2258 | // count type has enough bits to represent any shift value, truncate |
| 2259 | // it now. This is a common case and it exposes the truncate to |
| 2260 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2261 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2262 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2263 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2264 | TLI.getShiftAmountTy(), Op2); |
| 2265 | // Otherwise we'll need to temporarily settle for some other |
| 2266 | // convenient type; type legalization will make adjustments as |
| 2267 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2268 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2269 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2270 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2271 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2272 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2273 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2274 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2275 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2276 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2277 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | void SelectionDAGLowering::visitICmp(User &I) { |
| 2281 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2282 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2283 | predicate = IC->getPredicate(); |
| 2284 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2285 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2286 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2287 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2288 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2289 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2290 | EVT DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2291 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | void SelectionDAGLowering::visitFCmp(User &I) { |
| 2295 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2296 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2297 | predicate = FC->getPredicate(); |
| 2298 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2299 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2300 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2301 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2302 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2303 | EVT DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 9800e84 | 2009-07-07 22:41:32 +0000 | [diff] [blame] | 2304 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2307 | void SelectionDAGLowering::visitSelect(User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2308 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2309 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2310 | unsigned NumValues = ValueVTs.size(); |
| 2311 | if (NumValues != 0) { |
| 2312 | SmallVector<SDValue, 4> Values(NumValues); |
| 2313 | SDValue Cond = getValue(I.getOperand(0)); |
| 2314 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2315 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2316 | |
| 2317 | for (unsigned i = 0; i != NumValues; ++i) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2318 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2319 | TrueVal.getValueType(), Cond, |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2320 | SDValue(TrueVal.getNode(), TrueVal.getResNo() + i), |
| 2321 | SDValue(FalseVal.getNode(), FalseVal.getResNo() + i)); |
| 2322 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2323 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2324 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2325 | &Values[0], NumValues)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2326 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | |
| 2330 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2331 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2332 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2333 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2334 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2338 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2339 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2340 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2341 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2342 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2346 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2347 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2348 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2349 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2350 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2354 | // FPTrunc is never a no-op cast, no need to check |
| 2355 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2356 | EVT DestVT = TLI.getValueType(I.getType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2357 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2358 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2361 | void SelectionDAGLowering::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2362 | // FPTrunc is never a no-op cast, no need to check |
| 2363 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2364 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2365 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2368 | void SelectionDAGLowering::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2369 | // FPToUI is never a no-op cast, no need to check |
| 2370 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2371 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2372 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2376 | // FPToSI is never a no-op cast, no need to check |
| 2377 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2378 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2379 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2382 | void SelectionDAGLowering::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2383 | // UIToFP is never a no-op cast, no need to check |
| 2384 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2385 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2386 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2389 | void SelectionDAGLowering::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2390 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2391 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2392 | EVT DestVT = TLI.getValueType(I.getType()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2393 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2397 | // What to do depends on the size of the integer and the size of the pointer. |
| 2398 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2399 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2400 | EVT SrcVT = N.getValueType(); |
| 2401 | EVT DestVT = TLI.getValueType(I.getType()); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2402 | SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2403 | setValue(&I, Result); |
| 2404 | } |
| 2405 | |
| 2406 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2407 | // What to do depends on the size of the integer and the size of the pointer. |
| 2408 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2409 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2410 | EVT SrcVT = N.getValueType(); |
| 2411 | EVT DestVT = TLI.getValueType(I.getType()); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2412 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2415 | void SelectionDAGLowering::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2416 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2417 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2418 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2419 | // 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] | 2420 | // is either a BIT_CONVERT or a no-op. |
| 2421 | if (DestVT != N.getValueType()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2422 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2423 | DestVT, N)); // convert types |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2424 | else |
| 2425 | setValue(&I, N); // noop cast. |
| 2426 | } |
| 2427 | |
| 2428 | void SelectionDAGLowering::visitInsertElement(User &I) { |
| 2429 | SDValue InVec = getValue(I.getOperand(0)); |
| 2430 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2431 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2432 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2433 | getValue(I.getOperand(2))); |
| 2434 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2435 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2436 | TLI.getValueType(I.getType()), |
| 2437 | InVec, InVal, InIdx)); |
| 2438 | } |
| 2439 | |
| 2440 | void SelectionDAGLowering::visitExtractElement(User &I) { |
| 2441 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2442 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2443 | TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2444 | getValue(I.getOperand(1))); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2445 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2446 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2447 | } |
| 2448 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2449 | |
| 2450 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2451 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2452 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2453 | unsigned MaskNumElts = Mask.size(); |
| 2454 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2455 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2456 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2457 | return true; |
| 2458 | } |
| 2459 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2460 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2461 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2462 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2463 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2464 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2465 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2466 | // representing undef values. |
| 2467 | SmallVector<Constant*, 8> MaskElts; |
Owen Anderson | 001dbfe | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 2468 | cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(), |
| 2469 | MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2470 | unsigned MaskNumElts = MaskElts.size(); |
| 2471 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2472 | if (isa<UndefValue>(MaskElts[i])) |
| 2473 | Mask.push_back(-1); |
| 2474 | else |
| 2475 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2476 | } |
| 2477 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2478 | EVT VT = TLI.getValueType(I.getType()); |
| 2479 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2480 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2481 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2482 | if (SrcNumElts == MaskNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2483 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2484 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2485 | return; |
| 2486 | } |
| 2487 | |
| 2488 | // 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] | 2489 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2490 | // Mask is longer than the source vectors and is a multiple of the source |
| 2491 | // 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] | 2492 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2493 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2494 | // The shuffle is concatenating two vectors together. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2495 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2496 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | } |
| 2499 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2500 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2501 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2502 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2503 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2504 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2505 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2506 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2507 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2508 | MOps1[0] = Src1; |
| 2509 | MOps2[0] = Src2; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2510 | |
| 2511 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2512 | getCurDebugLoc(), VT, |
| 2513 | &MOps1[0], NumConcat); |
| 2514 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2515 | getCurDebugLoc(), VT, |
| 2516 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2517 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2518 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2519 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2520 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2521 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2522 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2523 | MappedOps.push_back(Idx); |
| 2524 | else |
| 2525 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2526 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2527 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2528 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2529 | return; |
| 2530 | } |
| 2531 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2532 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2533 | // Analyze the access pattern of the vector to see if we can extract |
| 2534 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2535 | // the range of elements the mask access on both vectors. |
| 2536 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2537 | int MaxRange[2] = {-1, -1}; |
| 2538 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2539 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2540 | int Idx = Mask[i]; |
| 2541 | int Input = 0; |
| 2542 | if (Idx < 0) |
| 2543 | continue; |
| 2544 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2545 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2546 | Input = 1; |
| 2547 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2548 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2549 | if (Idx > MaxRange[Input]) |
| 2550 | MaxRange[Input] = Idx; |
| 2551 | if (Idx < MinRange[Input]) |
| 2552 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2553 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2554 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2555 | // Check if the access is smaller than the vector size and can we find |
| 2556 | // a reasonable extract index. |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2557 | 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] | 2558 | int StartIdx[2]; // StartIdx to extract from |
| 2559 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2560 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2561 | RangeUse[Input] = 0; // Unused |
| 2562 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2563 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2564 | // 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] | 2565 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2566 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2567 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2568 | StartIdx[Input] = 0; |
| 2569 | } else { |
| 2570 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2571 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2572 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2573 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2574 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2575 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2578 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2579 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2580 | return; |
| 2581 | } |
| 2582 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2583 | // Extract appropriate subvector and generate a vector shuffle |
| 2584 | for (int Input=0; Input < 2; ++Input) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2585 | SDValue& Src = Input == 0 ? Src1 : Src2; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2586 | if (RangeUse[Input] == 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2587 | Src = DAG.getUNDEF(VT); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2588 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2589 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2590 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2591 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2592 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2593 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2594 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2595 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2596 | int Idx = Mask[i]; |
| 2597 | if (Idx < 0) |
| 2598 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2599 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2600 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2601 | else |
| 2602 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2603 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2604 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2605 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2606 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2607 | } |
| 2608 | } |
| 2609 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2610 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2611 | // replacing the shuffle with extract and build vector. |
| 2612 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2613 | EVT EltVT = VT.getVectorElementType(); |
| 2614 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2615 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2616 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2617 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2618 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2619 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2620 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2621 | if (Idx < (int)SrcNumElts) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2622 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2623 | EltVT, Src1, DAG.getConstant(Idx, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2624 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2625 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2626 | EltVT, Src2, |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2627 | DAG.getConstant(Idx - SrcNumElts, PtrVT))); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2628 | } |
| 2629 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2630 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2631 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) { |
| 2635 | const Value *Op0 = I.getOperand(0); |
| 2636 | const Value *Op1 = I.getOperand(1); |
| 2637 | const Type *AggTy = I.getType(); |
| 2638 | const Type *ValTy = Op1->getType(); |
| 2639 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2640 | bool FromUndef = isa<UndefValue>(Op1); |
| 2641 | |
| 2642 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2643 | I.idx_begin(), I.idx_end()); |
| 2644 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2645 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2646 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2647 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2648 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2649 | |
| 2650 | unsigned NumAggValues = AggValueVTs.size(); |
| 2651 | unsigned NumValValues = ValValueVTs.size(); |
| 2652 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2653 | |
| 2654 | SDValue Agg = getValue(Op0); |
| 2655 | SDValue Val = getValue(Op1); |
| 2656 | unsigned i = 0; |
| 2657 | // Copy the beginning value(s) from the original aggregate. |
| 2658 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2659 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2660 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2661 | // Copy values from the inserted value(s). |
| 2662 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2663 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2664 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2665 | // Copy remaining value(s) from the original aggregate. |
| 2666 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2667 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2668 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2669 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2670 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2671 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2672 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) { |
| 2676 | const Value *Op0 = I.getOperand(0); |
| 2677 | const Type *AggTy = Op0->getType(); |
| 2678 | const Type *ValTy = I.getType(); |
| 2679 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2680 | |
| 2681 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2682 | I.idx_begin(), I.idx_end()); |
| 2683 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2684 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2685 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2686 | |
| 2687 | unsigned NumValValues = ValValueVTs.size(); |
| 2688 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2689 | |
| 2690 | SDValue Agg = getValue(Op0); |
| 2691 | // Copy out the selected value(s). |
| 2692 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2693 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2694 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2695 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2696 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2697 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2698 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2699 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2700 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | |
| 2704 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2705 | SDValue N = getValue(I.getOperand(0)); |
| 2706 | const Type *Ty = I.getOperand(0)->getType(); |
| 2707 | |
| 2708 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2709 | OI != E; ++OI) { |
| 2710 | Value *Idx = *OI; |
| 2711 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2712 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2713 | if (Field) { |
| 2714 | // N = N + Offset |
| 2715 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2716 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2717 | DAG.getIntPtrConstant(Offset)); |
| 2718 | } |
| 2719 | Ty = StTy->getElementType(Field); |
| 2720 | } else { |
| 2721 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2722 | |
| 2723 | // If this is a constant subscript, handle it quickly. |
| 2724 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2725 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2726 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2727 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2728 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2729 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2730 | unsigned PtrBits = PTy.getSizeInBits(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2731 | if (PtrBits < 64) { |
| 2732 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2733 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2734 | DAG.getConstant(Offs, MVT::i64)); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2735 | } else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2736 | OffsVal = DAG.getIntPtrConstant(Offs); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2737 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2738 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2739 | continue; |
| 2740 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2741 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2742 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2743 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2744 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2745 | SDValue IdxN = getValue(Idx); |
| 2746 | |
| 2747 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2748 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2749 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2750 | |
| 2751 | // If this is a multiply by a power of two, turn it into a shl |
| 2752 | // immediately. This is a very common case. |
| 2753 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2754 | if (ElementSize.isPowerOf2()) { |
| 2755 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2756 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2757 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2758 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2759 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2760 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2761 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2762 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2766 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2767 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2768 | } |
| 2769 | } |
| 2770 | setValue(&I, N); |
| 2771 | } |
| 2772 | |
| 2773 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2774 | // If this is a fixed sized alloca in the entry block of the function, |
| 2775 | // allocate it statically on the stack. |
| 2776 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2777 | return; // getValue will auto-populate this. |
| 2778 | |
| 2779 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2780 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2781 | unsigned Align = |
| 2782 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2783 | I.getAlignment()); |
| 2784 | |
| 2785 | SDValue AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2786 | |
| 2787 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2788 | AllocSize, |
| 2789 | DAG.getConstant(TySize, AllocSize.getValueType())); |
| 2790 | |
| 2791 | |
| 2792 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2793 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2794 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2795 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2796 | // Handle alignment. If the requested alignment is less than or equal to |
| 2797 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2798 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2799 | unsigned StackAlign = |
| 2800 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2801 | if (Align <= StackAlign) |
| 2802 | Align = 0; |
| 2803 | |
| 2804 | // Round the size of the allocation up to the stack alignment size |
| 2805 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2806 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2807 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2808 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2809 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2810 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2811 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2812 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2813 | |
| 2814 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2815 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2816 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2817 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2818 | setValue(&I, DSA); |
| 2819 | DAG.setRoot(DSA.getValue(1)); |
| 2820 | |
| 2821 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2822 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2823 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2827 | const Value *SV = I.getOperand(0); |
| 2828 | SDValue Ptr = getValue(SV); |
| 2829 | |
| 2830 | const Type *Ty = I.getType(); |
| 2831 | bool isVolatile = I.isVolatile(); |
| 2832 | unsigned Alignment = I.getAlignment(); |
| 2833 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2834 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2835 | SmallVector<uint64_t, 4> Offsets; |
| 2836 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2837 | unsigned NumValues = ValueVTs.size(); |
| 2838 | if (NumValues == 0) |
| 2839 | return; |
| 2840 | |
| 2841 | SDValue Root; |
| 2842 | bool ConstantMemory = false; |
| 2843 | if (I.isVolatile()) |
| 2844 | // Serialize volatile loads with other side effects. |
| 2845 | Root = getRoot(); |
| 2846 | else if (AA->pointsToConstantMemory(SV)) { |
| 2847 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2848 | Root = DAG.getEntryNode(); |
| 2849 | ConstantMemory = true; |
| 2850 | } else { |
| 2851 | // Do not serialize non-volatile loads against each other. |
| 2852 | Root = DAG.getRoot(); |
| 2853 | } |
| 2854 | |
| 2855 | SmallVector<SDValue, 4> Values(NumValues); |
| 2856 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2857 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2858 | for (unsigned i = 0; i != NumValues; ++i) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2859 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Nate Begeman | e679837 | 2009-09-15 00:13:12 +0000 | [diff] [blame] | 2860 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2861 | PtrVT, Ptr, |
| 2862 | DAG.getConstant(Offsets[i], PtrVT)), |
Nate Begeman | 101b25c | 2009-09-15 19:05:41 +0000 | [diff] [blame] | 2863 | SV, Offsets[i], isVolatile, Alignment); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2864 | Values[i] = L; |
| 2865 | Chains[i] = L.getValue(1); |
| 2866 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2867 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2868 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2869 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2870 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2871 | &Chains[0], NumValues); |
| 2872 | if (isVolatile) |
| 2873 | DAG.setRoot(Chain); |
| 2874 | else |
| 2875 | PendingLoads.push_back(Chain); |
| 2876 | } |
| 2877 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2878 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2879 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2880 | &Values[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | |
| 2884 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2885 | Value *SrcV = I.getOperand(0); |
| 2886 | Value *PtrV = I.getOperand(1); |
| 2887 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2888 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2889 | SmallVector<uint64_t, 4> Offsets; |
| 2890 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2891 | unsigned NumValues = ValueVTs.size(); |
| 2892 | if (NumValues == 0) |
| 2893 | return; |
| 2894 | |
| 2895 | // Get the lowered operands. Note that we do this after |
| 2896 | // checking if NumResults is zero, because with zero results |
| 2897 | // the operands won't have values in the map. |
| 2898 | SDValue Src = getValue(SrcV); |
| 2899 | SDValue Ptr = getValue(PtrV); |
| 2900 | |
| 2901 | SDValue Root = getRoot(); |
| 2902 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2903 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2904 | bool isVolatile = I.isVolatile(); |
| 2905 | unsigned Alignment = I.getAlignment(); |
| 2906 | for (unsigned i = 0; i != NumValues; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2907 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2908 | SDValue(Src.getNode(), Src.getResNo() + i), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2909 | DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2910 | PtrVT, Ptr, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2911 | DAG.getConstant(Offsets[i], PtrVT)), |
Nate Begeman | 101b25c | 2009-09-15 19:05:41 +0000 | [diff] [blame] | 2912 | PtrV, Offsets[i], isVolatile, Alignment); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2913 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2914 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2915 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2919 | /// node. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2920 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2921 | unsigned Intrinsic) { |
| 2922 | bool HasChain = !I.doesNotAccessMemory(); |
| 2923 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2924 | |
| 2925 | // Build the operand list. |
| 2926 | SmallVector<SDValue, 8> Ops; |
| 2927 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2928 | if (OnlyLoad) { |
| 2929 | // We don't need to serialize loads against other loads. |
| 2930 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2931 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2932 | Ops.push_back(getRoot()); |
| 2933 | } |
| 2934 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2935 | |
| 2936 | // Info is set by getTgtMemInstrinsic |
| 2937 | TargetLowering::IntrinsicInfo Info; |
| 2938 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2939 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2940 | // 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] | 2941 | if (!IsTgtIntrinsic) |
| 2942 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2943 | |
| 2944 | // Add all operands of the call to the operand list. |
| 2945 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2946 | SDValue Op = getValue(I.getOperand(i)); |
| 2947 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2948 | "Intrinsic uses a non-legal type?"); |
| 2949 | Ops.push_back(Op); |
| 2950 | } |
| 2951 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2952 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2953 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2954 | #ifndef NDEBUG |
| 2955 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2956 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2957 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2958 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2959 | #endif // NDEBUG |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2960 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2961 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2962 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2963 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2964 | |
| 2965 | // Create the node. |
| 2966 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2967 | if (IsTgtIntrinsic) { |
| 2968 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2969 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2970 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2971 | Info.memVT, Info.ptrVal, Info.offset, |
| 2972 | Info.align, Info.vol, |
| 2973 | Info.readMem, Info.writeMem); |
| 2974 | } |
| 2975 | else if (!HasChain) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2976 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2977 | VTs, &Ops[0], Ops.size()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2978 | else if (I.getType() != Type::getVoidTy(*DAG.getContext())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2979 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2980 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2981 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2982 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2983 | VTs, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2984 | |
| 2985 | if (HasChain) { |
| 2986 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2987 | if (OnlyLoad) |
| 2988 | PendingLoads.push_back(Chain); |
| 2989 | else |
| 2990 | DAG.setRoot(Chain); |
| 2991 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2992 | if (I.getType() != Type::getVoidTy(*DAG.getContext())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2993 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2994 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2995 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2996 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2997 | setValue(&I, Result); |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 3002 | static GlobalVariable *ExtractTypeInfo(Value *V) { |
| 3003 | V = V->stripPointerCasts(); |
| 3004 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 3005 | assert ((GV || isa<ConstantPointerNull>(V)) && |
| 3006 | "TypeInfo must be a global variable or NULL"); |
| 3007 | return GV; |
| 3008 | } |
| 3009 | |
| 3010 | namespace llvm { |
| 3011 | |
| 3012 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 3013 | /// call, and add them to the specified machine basic block. |
| 3014 | void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 3015 | MachineBasicBlock *MBB) { |
| 3016 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 3017 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 3018 | assert(CE->getOpcode() == Instruction::BitCast && |
| 3019 | isa<Function>(CE->getOperand(0)) && |
| 3020 | "Personality should be a function"); |
| 3021 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 3022 | |
| 3023 | // Gather all the type infos for this landing pad and pass them along to |
| 3024 | // MachineModuleInfo. |
| 3025 | std::vector<GlobalVariable *> TyInfo; |
| 3026 | unsigned N = I.getNumOperands(); |
| 3027 | |
| 3028 | for (unsigned i = N - 1; i > 2; --i) { |
| 3029 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 3030 | unsigned FilterLength = CI->getZExtValue(); |
| 3031 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
| 3032 | assert (FirstCatch <= N && "Invalid filter length"); |
| 3033 | |
| 3034 | if (FirstCatch < N) { |
| 3035 | TyInfo.reserve(N - FirstCatch); |
| 3036 | for (unsigned j = FirstCatch; j < N; ++j) |
| 3037 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3038 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3039 | TyInfo.clear(); |
| 3040 | } |
| 3041 | |
| 3042 | if (!FilterLength) { |
| 3043 | // Cleanup. |
| 3044 | MMI->addCleanup(MBB); |
| 3045 | } else { |
| 3046 | // Filter. |
| 3047 | TyInfo.reserve(FilterLength - 1); |
| 3048 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 3049 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3050 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 3051 | TyInfo.clear(); |
| 3052 | } |
| 3053 | |
| 3054 | N = i; |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | if (N > 3) { |
| 3059 | TyInfo.reserve(N - 3); |
| 3060 | for (unsigned j = 3; j < N; ++j) |
| 3061 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 3062 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | } |
| 3067 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3068 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3069 | /// number with exponent of 1: |
| 3070 | /// |
| 3071 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3072 | /// |
| 3073 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3074 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3075 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3076 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3077 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3078 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3079 | DAG.getConstant(0x3f800000, MVT::i32)); |
| 3080 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3083 | /// GetExponent - Get the exponent: |
| 3084 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3085 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3086 | /// |
| 3087 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3088 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3089 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
| 3090 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3091 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3092 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3093 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3094 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3095 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3096 | DAG.getConstant(127, MVT::i32)); |
| 3097 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3100 | /// getF32Constant - Get 32-bit floating point constant. |
| 3101 | static SDValue |
| 3102 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3103 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3106 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3107 | /// visitIntrinsicCall: I is a call instruction |
| 3108 | /// Op is the associated NodeType for I |
| 3109 | const char * |
| 3110 | SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3111 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3112 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3113 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3114 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3115 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3116 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3117 | getValue(I.getOperand(2)), |
| 3118 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3119 | setValue(&I, L); |
| 3120 | DAG.setRoot(L.getValue(1)); |
| 3121 | return 0; |
| 3122 | } |
| 3123 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3124 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3125 | const char * |
| 3126 | SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3127 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3128 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3129 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3130 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3131 | SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3132 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3133 | setValue(&I, Result); |
| 3134 | return 0; |
| 3135 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3136 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3137 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 3138 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3139 | void |
| 3140 | SelectionDAGLowering::visitExp(CallInst &I) { |
| 3141 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3142 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3143 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3144 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3145 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3146 | SDValue Op = getValue(I.getOperand(1)); |
| 3147 | |
| 3148 | // Put the exponent in the right bit position for later addition to the |
| 3149 | // final result: |
| 3150 | // |
| 3151 | // #define LOG2OFe 1.4426950f |
| 3152 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3153 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3154 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3155 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3156 | |
| 3157 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3158 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3159 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3160 | |
| 3161 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3162 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3163 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3164 | |
| 3165 | if (LimitFloatPrecision <= 6) { |
| 3166 | // For floating-point precision of 6: |
| 3167 | // |
| 3168 | // TwoToFractionalPartOfX = |
| 3169 | // 0.997535578f + |
| 3170 | // (0.735607626f + 0.252464424f * x) * x; |
| 3171 | // |
| 3172 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3173 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3174 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3175 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3176 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3177 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3178 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3179 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3180 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3181 | |
| 3182 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3183 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3184 | TwoToFracPartOfX, IntegerPartOfX); |
| 3185 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3186 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3187 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3188 | // For floating-point precision of 12: |
| 3189 | // |
| 3190 | // TwoToFractionalPartOfX = |
| 3191 | // 0.999892986f + |
| 3192 | // (0.696457318f + |
| 3193 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3194 | // |
| 3195 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3196 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3197 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3198 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3199 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3200 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3201 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3202 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3203 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3204 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3205 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3206 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3207 | |
| 3208 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3209 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3210 | TwoToFracPartOfX, IntegerPartOfX); |
| 3211 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3212 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3213 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3214 | // For floating-point precision of 18: |
| 3215 | // |
| 3216 | // TwoToFractionalPartOfX = |
| 3217 | // 0.999999982f + |
| 3218 | // (0.693148872f + |
| 3219 | // (0.240227044f + |
| 3220 | // (0.554906021e-1f + |
| 3221 | // (0.961591928e-2f + |
| 3222 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3223 | // |
| 3224 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3225 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3226 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3227 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3228 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3229 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3230 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3231 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3232 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3233 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3234 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3235 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3236 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3237 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3238 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3239 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3240 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3241 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3242 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3243 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3244 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3245 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3246 | |
| 3247 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3248 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3249 | TwoToFracPartOfX, IntegerPartOfX); |
| 3250 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3251 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } else { |
| 3254 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3255 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3256 | getValue(I.getOperand(1)).getValueType(), |
| 3257 | getValue(I.getOperand(1))); |
| 3258 | } |
| 3259 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3260 | setValue(&I, result); |
| 3261 | } |
| 3262 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3263 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3264 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3265 | void |
| 3266 | SelectionDAGLowering::visitLog(CallInst &I) { |
| 3267 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3268 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3269 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3270 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3271 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3272 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3273 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3274 | |
| 3275 | // Scale the exponent by log(2) [0.69314718f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3276 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3277 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3278 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3279 | |
| 3280 | // Get the significand and build it into a floating-point number with |
| 3281 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3282 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3283 | |
| 3284 | if (LimitFloatPrecision <= 6) { |
| 3285 | // For floating-point precision of 6: |
| 3286 | // |
| 3287 | // LogofMantissa = |
| 3288 | // -1.1609546f + |
| 3289 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3290 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3291 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3292 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3293 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3294 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3295 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3296 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3297 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3298 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3299 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3300 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3301 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3302 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3303 | // For floating-point precision of 12: |
| 3304 | // |
| 3305 | // LogOfMantissa = |
| 3306 | // -1.7417939f + |
| 3307 | // (2.8212026f + |
| 3308 | // (-1.4699568f + |
| 3309 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3310 | // |
| 3311 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3312 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3313 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3314 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3315 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3316 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3317 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3318 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3319 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3320 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3321 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3322 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3323 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3324 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3325 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3326 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3327 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3328 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3329 | // For floating-point precision of 18: |
| 3330 | // |
| 3331 | // LogOfMantissa = |
| 3332 | // -2.1072184f + |
| 3333 | // (4.2372794f + |
| 3334 | // (-3.7029485f + |
| 3335 | // (2.2781945f + |
| 3336 | // (-0.87823314f + |
| 3337 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3338 | // |
| 3339 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3340 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3341 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3342 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3343 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3344 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3345 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3346 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3347 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3348 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3349 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3350 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3351 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3352 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3353 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3354 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3355 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3356 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3357 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3358 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3359 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3360 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3361 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3362 | } |
| 3363 | } else { |
| 3364 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3365 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3366 | getValue(I.getOperand(1)).getValueType(), |
| 3367 | getValue(I.getOperand(1))); |
| 3368 | } |
| 3369 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3370 | setValue(&I, result); |
| 3371 | } |
| 3372 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3373 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3374 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3375 | void |
| 3376 | SelectionDAGLowering::visitLog2(CallInst &I) { |
| 3377 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3378 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3379 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3380 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3381 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3382 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3383 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3384 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3385 | // Get the exponent. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3386 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3387 | |
| 3388 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3389 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3390 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3391 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3392 | // Different possible minimax approximations of significand in |
| 3393 | // floating-point for various degrees of accuracy over [1,2]. |
| 3394 | if (LimitFloatPrecision <= 6) { |
| 3395 | // For floating-point precision of 6: |
| 3396 | // |
| 3397 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3398 | // |
| 3399 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3400 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3401 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3402 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3403 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3404 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3405 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3406 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3407 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3408 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3409 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3410 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3411 | // For floating-point precision of 12: |
| 3412 | // |
| 3413 | // Log2ofMantissa = |
| 3414 | // -2.51285454f + |
| 3415 | // (4.07009056f + |
| 3416 | // (-2.12067489f + |
| 3417 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3418 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3419 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3420 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3421 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3422 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3423 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3424 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3425 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3426 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3427 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3428 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3429 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3430 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3431 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3432 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3433 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3434 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3435 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3436 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3437 | // For floating-point precision of 18: |
| 3438 | // |
| 3439 | // Log2ofMantissa = |
| 3440 | // -3.0400495f + |
| 3441 | // (6.1129976f + |
| 3442 | // (-5.3420409f + |
| 3443 | // (3.2865683f + |
| 3444 | // (-1.2669343f + |
| 3445 | // (0.27515199f - |
| 3446 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3447 | // |
| 3448 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3449 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3450 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3451 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3452 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3453 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3454 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3455 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3456 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3457 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3458 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3459 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3460 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3461 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3462 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3463 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3464 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3465 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3466 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3467 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3468 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3469 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3470 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3471 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3472 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3473 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3474 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3475 | getValue(I.getOperand(1)).getValueType(), |
| 3476 | getValue(I.getOperand(1))); |
| 3477 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3478 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3479 | setValue(&I, result); |
| 3480 | } |
| 3481 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3482 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3483 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3484 | void |
| 3485 | SelectionDAGLowering::visitLog10(CallInst &I) { |
| 3486 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3487 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3488 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3489 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3490 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3491 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3492 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3493 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3494 | // Scale the exponent by log10(2) [0.30102999f]. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3495 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3496 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3497 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3498 | |
| 3499 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3500 | // exponent of 1. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3501 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3502 | |
| 3503 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3504 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3505 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3506 | // Log10ofMantissa = |
| 3507 | // -0.50419619f + |
| 3508 | // (0.60948995f - 0.10380950f * x) * x; |
| 3509 | // |
| 3510 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3511 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3512 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3513 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3514 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3515 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3516 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3517 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3518 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3519 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3520 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3521 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3522 | // For floating-point precision of 12: |
| 3523 | // |
| 3524 | // Log10ofMantissa = |
| 3525 | // -0.64831180f + |
| 3526 | // (0.91751397f + |
| 3527 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3528 | // |
| 3529 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3530 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3531 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3532 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3533 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3534 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3535 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3536 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3537 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3538 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3539 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3540 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3541 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3542 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3543 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3544 | // For floating-point precision of 18: |
| 3545 | // |
| 3546 | // Log10ofMantissa = |
| 3547 | // -0.84299375f + |
| 3548 | // (1.5327582f + |
| 3549 | // (-1.0688956f + |
| 3550 | // (0.49102474f + |
| 3551 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3552 | // |
| 3553 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3554 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3555 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3556 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3557 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3558 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3559 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3560 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3561 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3562 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3563 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3564 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3565 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3566 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3567 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3568 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3569 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3570 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3571 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3572 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3573 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3574 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3575 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3576 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3577 | getValue(I.getOperand(1)).getValueType(), |
| 3578 | getValue(I.getOperand(1))); |
| 3579 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3580 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3581 | setValue(&I, result); |
| 3582 | } |
| 3583 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3584 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3585 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3586 | void |
| 3587 | SelectionDAGLowering::visitExp2(CallInst &I) { |
| 3588 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3589 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3590 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3591 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3592 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3593 | SDValue Op = getValue(I.getOperand(1)); |
| 3594 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3595 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3596 | |
| 3597 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3598 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3599 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3600 | |
| 3601 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3602 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3603 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3604 | |
| 3605 | if (LimitFloatPrecision <= 6) { |
| 3606 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3607 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3608 | // TwoToFractionalPartOfX = |
| 3609 | // 0.997535578f + |
| 3610 | // (0.735607626f + 0.252464424f * x) * x; |
| 3611 | // |
| 3612 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3613 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3614 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3615 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3616 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3617 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3618 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3619 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3620 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3621 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3622 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3623 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3624 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3625 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3626 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3627 | // For floating-point precision of 12: |
| 3628 | // |
| 3629 | // TwoToFractionalPartOfX = |
| 3630 | // 0.999892986f + |
| 3631 | // (0.696457318f + |
| 3632 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3633 | // |
| 3634 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3635 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3636 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3637 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3638 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3639 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3640 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3641 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3642 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3643 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3644 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3645 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3646 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3647 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3648 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3649 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3650 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3651 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3652 | // For floating-point precision of 18: |
| 3653 | // |
| 3654 | // TwoToFractionalPartOfX = |
| 3655 | // 0.999999982f + |
| 3656 | // (0.693148872f + |
| 3657 | // (0.240227044f + |
| 3658 | // (0.554906021e-1f + |
| 3659 | // (0.961591928e-2f + |
| 3660 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3661 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3662 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3663 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3664 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3665 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3666 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3667 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3668 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3669 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3670 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3671 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3672 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3673 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3674 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3675 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3676 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3677 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3678 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3679 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3680 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3681 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3682 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3683 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3684 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3685 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3686 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3687 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3688 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3689 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3690 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3691 | getValue(I.getOperand(1)).getValueType(), |
| 3692 | getValue(I.getOperand(1))); |
| 3693 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3694 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3695 | setValue(&I, result); |
| 3696 | } |
| 3697 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3698 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3699 | /// limited-precision mode with x == 10.0f. |
| 3700 | void |
| 3701 | SelectionDAGLowering::visitPow(CallInst &I) { |
| 3702 | SDValue result; |
| 3703 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3704 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3705 | bool IsExp10 = false; |
| 3706 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3707 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3708 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3709 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3710 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3711 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3712 | APFloat Ten(10.0f); |
| 3713 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3714 | } |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3719 | SDValue Op = getValue(I.getOperand(2)); |
| 3720 | |
| 3721 | // Put the exponent in the right bit position for later addition to the |
| 3722 | // final result: |
| 3723 | // |
| 3724 | // #define LOG2OF10 3.3219281f |
| 3725 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3726 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3727 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3728 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3729 | |
| 3730 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3731 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3732 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3733 | |
| 3734 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3735 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3736 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3737 | |
| 3738 | if (LimitFloatPrecision <= 6) { |
| 3739 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3740 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3741 | // twoToFractionalPartOfX = |
| 3742 | // 0.997535578f + |
| 3743 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3744 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3745 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3746 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3747 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3748 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3749 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3750 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3751 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3752 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3753 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3754 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3755 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3756 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3757 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3758 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3759 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3760 | // For floating-point precision of 12: |
| 3761 | // |
| 3762 | // TwoToFractionalPartOfX = |
| 3763 | // 0.999892986f + |
| 3764 | // (0.696457318f + |
| 3765 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3766 | // |
| 3767 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3768 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3769 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3770 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3771 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3772 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3773 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3774 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3775 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3776 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3777 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3778 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3779 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3780 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3781 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3782 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3783 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3784 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3785 | // For floating-point precision of 18: |
| 3786 | // |
| 3787 | // TwoToFractionalPartOfX = |
| 3788 | // 0.999999982f + |
| 3789 | // (0.693148872f + |
| 3790 | // (0.240227044f + |
| 3791 | // (0.554906021e-1f + |
| 3792 | // (0.961591928e-2f + |
| 3793 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3794 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3795 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3796 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3797 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3798 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3799 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3800 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3801 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3802 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3803 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3804 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3805 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3806 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3807 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3808 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3809 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3810 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3811 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3812 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3813 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3814 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3815 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3816 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3817 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3818 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3819 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3820 | } |
| 3821 | } else { |
| 3822 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3823 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3824 | getValue(I.getOperand(1)).getValueType(), |
| 3825 | getValue(I.getOperand(1)), |
| 3826 | getValue(I.getOperand(2))); |
| 3827 | } |
| 3828 | |
| 3829 | setValue(&I, result); |
| 3830 | } |
| 3831 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3832 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3833 | /// we want to emit this as a call to a named external function, return the name |
| 3834 | /// otherwise lower it and return null. |
| 3835 | const char * |
| 3836 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3837 | DebugLoc dl = getCurDebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3838 | switch (Intrinsic) { |
| 3839 | default: |
| 3840 | // By default, turn this into a target intrinsic node. |
| 3841 | visitTargetIntrinsic(I, Intrinsic); |
| 3842 | return 0; |
| 3843 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3844 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3845 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3846 | case Intrinsic::returnaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3847 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3848 | getValue(I.getOperand(1)))); |
| 3849 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3850 | case Intrinsic::frameaddress: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3851 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3852 | getValue(I.getOperand(1)))); |
| 3853 | return 0; |
| 3854 | case Intrinsic::setjmp: |
| 3855 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3856 | break; |
| 3857 | case Intrinsic::longjmp: |
| 3858 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3859 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3860 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3861 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3862 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3863 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3864 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3865 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3866 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3867 | return 0; |
| 3868 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3869 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3870 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3871 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3872 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3873 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3874 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3875 | I.getOperand(1), 0)); |
| 3876 | return 0; |
| 3877 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3878 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3879 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3880 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3881 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3882 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3883 | |
| 3884 | // If the source and destination are known to not be aliases, we can |
| 3885 | // lower memmove as memcpy. |
| 3886 | uint64_t Size = -1ULL; |
| 3887 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3888 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3889 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3890 | AliasAnalysis::NoAlias) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3891 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3892 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 3896 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
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 | } |
| 3900 | case Intrinsic::dbg_stoppoint: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3901 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3902 | if (isValidDebugInfoIntrinsic(SPI, CodeGenOpt::Default)) { |
Evan Cheng | e3d4232 | 2009-02-25 07:04:34 +0000 | [diff] [blame] | 3903 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3904 | DebugLoc Loc = ExtractDebugLocation(SPI, MF.getDebugLocInfo()); |
Chris Lattner | af29a52 | 2009-05-04 22:10:05 +0000 | [diff] [blame] | 3905 | setCurDebugLoc(Loc); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3906 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3907 | if (OptLevel == CodeGenOpt::None) |
Chris Lattner | af29a52 | 2009-05-04 22:10:05 +0000 | [diff] [blame] | 3908 | DAG.setRoot(DAG.getDbgStopPoint(Loc, getRoot(), |
Dale Johannesen | beaec4c | 2009-03-25 17:36:08 +0000 | [diff] [blame] | 3909 | SPI.getLine(), |
| 3910 | SPI.getColumn(), |
| 3911 | SPI.getContext())); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3912 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3913 | return 0; |
| 3914 | } |
| 3915 | case Intrinsic::dbg_region_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3916 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3917 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3918 | if (isValidDebugInfoIntrinsic(RSI, OptLevel) && DW |
| 3919 | && DW->ShouldEmitDwarfDebug()) { |
Bill Wendling | df7d5d3 | 2009-05-21 00:04:55 +0000 | [diff] [blame] | 3920 | unsigned LabelID = |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3921 | DW->RecordRegionStart(RSI.getContext()); |
Devang Patel | 48c7fa2 | 2009-04-13 18:13:16 +0000 | [diff] [blame] | 3922 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3923 | getRoot(), LabelID)); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3924 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3925 | return 0; |
| 3926 | } |
| 3927 | case Intrinsic::dbg_region_end: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3928 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3929 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 3930 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3931 | if (!isValidDebugInfoIntrinsic(REI, OptLevel) || !DW |
| 3932 | || !DW->ShouldEmitDwarfDebug()) |
| 3933 | return 0; |
Bill Wendling | 6c4311d | 2009-05-08 21:14:49 +0000 | [diff] [blame] | 3934 | |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3935 | DISubprogram Subprogram(REI.getContext()); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3936 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3937 | unsigned LabelID = |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3938 | DW->RecordRegionEnd(REI.getContext()); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3939 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(), |
| 3940 | getRoot(), LabelID)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3941 | return 0; |
| 3942 | } |
| 3943 | case Intrinsic::dbg_func_start: { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 3944 | DwarfWriter *DW = DAG.getDwarfWriter(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3945 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 3946 | if (!isValidDebugInfoIntrinsic(FSI, CodeGenOpt::None)) |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 3947 | return 0; |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 3948 | |
Argyrios Kyrtzidis | 77eaa68 | 2009-05-03 08:50:41 +0000 | [diff] [blame] | 3949 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3950 | MF.setDefaultDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo())); |
Jeffrey Yasskin | 32360a7 | 2009-07-16 21:07:26 +0000 | [diff] [blame] | 3951 | |
| 3952 | if (!DW || !DW->ShouldEmitDwarfDebug()) |
| 3953 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3954 | // llvm.dbg.func_start also defines beginning of function scope. |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3955 | DW->RecordRegionStart(FSI.getSubprogram()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3956 | return 0; |
| 3957 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3958 | case Intrinsic::dbg_declare: { |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3959 | if (OptLevel != CodeGenOpt::None) |
| 3960 | // FIXME: Variable debug info is not supported here. |
| 3961 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3962 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3963 | if (!DW) |
| 3964 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3965 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 3966 | if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None)) |
| 3967 | return 0; |
| 3968 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3969 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3970 | Value *Address = DI.getAddress(); |
| 3971 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 3972 | Address = BCI->getOperand(0); |
| 3973 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 3974 | // Don't handle byval struct arguments or VLAs, for example. |
| 3975 | if (!AI) |
| 3976 | return 0; |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3977 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3978 | FuncInfo.StaticAllocaMap.find(AI); |
| 3979 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3980 | return 0; // VLAs. |
| 3981 | int FI = SI->second; |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3982 | #ifdef ATTACH_DEBUG_INFO_TO_AN_INSN |
| 3983 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame^] | 3984 | if (MMI) { |
| 3985 | MetadataContext &TheMetadata = |
| 3986 | DI.getParent()->getContext().getMetadata(); |
| 3987 | unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); |
| 3988 | MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI); |
| 3989 | MMI->setVariableDbgInfo(Variable, FI, Dbg); |
| 3990 | } |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3991 | #else |
| 3992 | DW->RecordVariable(Variable, FI); |
| 3993 | #endif |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3994 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3995 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3996 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3997 | // Insert the EXCEPTIONADDR instruction. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 3998 | assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3999 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4000 | SDValue Ops[1]; |
| 4001 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4002 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4003 | setValue(&I, Op); |
| 4004 | DAG.setRoot(Op.getValue(1)); |
| 4005 | return 0; |
| 4006 | } |
| 4007 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4008 | case Intrinsic::eh_selector: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4009 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4010 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4011 | if (CurMBB->isLandingPad()) |
| 4012 | AddCatchInfo(I, MMI, CurMBB); |
| 4013 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4014 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4015 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4016 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4017 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 4018 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 4019 | if (Reg) CurMBB->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4020 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4021 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4022 | // Insert the EHSELECTION instruction. |
| 4023 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 4024 | SDValue Ops[2]; |
| 4025 | Ops[0] = getValue(I.getOperand(1)); |
| 4026 | Ops[1] = getRoot(); |
| 4027 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
| 4028 | |
| 4029 | DAG.setRoot(Op.getValue(1)); |
| 4030 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4031 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4032 | return 0; |
| 4033 | } |
| 4034 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4035 | case Intrinsic::eh_typeid_for: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4036 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4037 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4038 | if (MMI) { |
| 4039 | // Find the type id for the given typeinfo. |
| 4040 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 4041 | |
| 4042 | unsigned TypeID = MMI->getTypeIDFor(GV); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4043 | setValue(&I, DAG.getConstant(TypeID, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4044 | } else { |
| 4045 | // Return something different to eh_selector. |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4046 | setValue(&I, DAG.getConstant(1, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4052 | case Intrinsic::eh_return_i32: |
| 4053 | case Intrinsic::eh_return_i64: |
| 4054 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4055 | MMI->setCallsEHReturn(true); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4056 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4057 | MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4058 | getControlRoot(), |
| 4059 | getValue(I.getOperand(1)), |
| 4060 | getValue(I.getOperand(2)))); |
| 4061 | } else { |
| 4062 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 4063 | } |
| 4064 | |
| 4065 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4066 | case Intrinsic::eh_unwind_init: |
| 4067 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 4068 | MMI->setCallsUnwindInit(true); |
| 4069 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4070 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4071 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4072 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4073 | case Intrinsic::eh_dwarf_cfa: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4074 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4075 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 4076 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4077 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4078 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4079 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4080 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4081 | TLI.getPointerTy()), |
| 4082 | CfaArg); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4083 | setValue(&I, DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4084 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4085 | DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4086 | TLI.getPointerTy(), |
| 4087 | DAG.getConstant(0, |
| 4088 | TLI.getPointerTy())), |
| 4089 | Offset)); |
| 4090 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4091 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4092 | case Intrinsic::convertff: |
| 4093 | case Intrinsic::convertfsi: |
| 4094 | case Intrinsic::convertfui: |
| 4095 | case Intrinsic::convertsif: |
| 4096 | case Intrinsic::convertuif: |
| 4097 | case Intrinsic::convertss: |
| 4098 | case Intrinsic::convertsu: |
| 4099 | case Intrinsic::convertus: |
| 4100 | case Intrinsic::convertuu: { |
| 4101 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4102 | switch (Intrinsic) { |
| 4103 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4104 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4105 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4106 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4107 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4108 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4109 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4110 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4111 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4112 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4113 | EVT DestVT = TLI.getValueType(I.getType()); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4114 | Value* Op1 = I.getOperand(1); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4115 | setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4116 | DAG.getValueType(DestVT), |
| 4117 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4118 | getValue(I.getOperand(2)), |
| 4119 | getValue(I.getOperand(3)), |
| 4120 | Code)); |
| 4121 | return 0; |
| 4122 | } |
| 4123 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4124 | case Intrinsic::sqrt: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4125 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4126 | getValue(I.getOperand(1)).getValueType(), |
| 4127 | getValue(I.getOperand(1)))); |
| 4128 | return 0; |
| 4129 | case Intrinsic::powi: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4130 | setValue(&I, DAG.getNode(ISD::FPOWI, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4131 | getValue(I.getOperand(1)).getValueType(), |
| 4132 | getValue(I.getOperand(1)), |
| 4133 | getValue(I.getOperand(2)))); |
| 4134 | return 0; |
| 4135 | case Intrinsic::sin: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4136 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4137 | getValue(I.getOperand(1)).getValueType(), |
| 4138 | getValue(I.getOperand(1)))); |
| 4139 | return 0; |
| 4140 | case Intrinsic::cos: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4141 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4142 | getValue(I.getOperand(1)).getValueType(), |
| 4143 | getValue(I.getOperand(1)))); |
| 4144 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4145 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4146 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4147 | return 0; |
| 4148 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4149 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4150 | return 0; |
| 4151 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4152 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4153 | return 0; |
| 4154 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4155 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4156 | return 0; |
| 4157 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4158 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4159 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4160 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4161 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4162 | return 0; |
| 4163 | case Intrinsic::pcmarker: { |
| 4164 | SDValue Tmp = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4165 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4166 | return 0; |
| 4167 | } |
| 4168 | case Intrinsic::readcyclecounter: { |
| 4169 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4170 | SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4171 | DAG.getVTList(MVT::i64, MVT::Other), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4172 | &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4173 | setValue(&I, Tmp); |
| 4174 | DAG.setRoot(Tmp.getValue(1)); |
| 4175 | return 0; |
| 4176 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4177 | case Intrinsic::bswap: |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4178 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4179 | getValue(I.getOperand(1)).getValueType(), |
| 4180 | getValue(I.getOperand(1)))); |
| 4181 | return 0; |
| 4182 | case Intrinsic::cttz: { |
| 4183 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4184 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4185 | SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4186 | setValue(&I, result); |
| 4187 | return 0; |
| 4188 | } |
| 4189 | case Intrinsic::ctlz: { |
| 4190 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4191 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4192 | SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4193 | setValue(&I, result); |
| 4194 | return 0; |
| 4195 | } |
| 4196 | case Intrinsic::ctpop: { |
| 4197 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4198 | EVT Ty = Arg.getValueType(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4199 | SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4200 | setValue(&I, result); |
| 4201 | return 0; |
| 4202 | } |
| 4203 | case Intrinsic::stacksave: { |
| 4204 | SDValue Op = getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4205 | SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4206 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4207 | setValue(&I, Tmp); |
| 4208 | DAG.setRoot(Tmp.getValue(1)); |
| 4209 | return 0; |
| 4210 | } |
| 4211 | case Intrinsic::stackrestore: { |
| 4212 | SDValue Tmp = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4213 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4214 | return 0; |
| 4215 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4216 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4217 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4218 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4219 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4220 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4221 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4222 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4223 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4224 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4225 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4226 | MFI->setStackProtectorIndex(FI); |
| 4227 | |
| 4228 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4229 | |
| 4230 | // Store the stack protector onto the stack. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4231 | SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 4232 | PseudoSourceValue::getFixedStack(FI), |
| 4233 | 0, true); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4234 | setValue(&I, Result); |
| 4235 | DAG.setRoot(Result); |
| 4236 | return 0; |
| 4237 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4238 | case Intrinsic::objectsize: { |
| 4239 | // If we don't know by now, we're never going to know. |
| 4240 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4241 | |
| 4242 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4243 | |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4244 | SDValue Arg = getValue(I.getOperand(0)); |
| 4245 | EVT Ty = Arg.getValueType(); |
| 4246 | |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4247 | if (CI->getZExtValue() < 2) |
Mike Stump | 70e5e68 | 2009-11-09 22:28:21 +0000 | [diff] [blame] | 4248 | setValue(&I, DAG.getConstant(-1ULL, Ty)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4249 | else |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4250 | setValue(&I, DAG.getConstant(0, Ty)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4251 | return 0; |
| 4252 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4253 | case Intrinsic::var_annotation: |
| 4254 | // Discard annotate attributes |
| 4255 | return 0; |
| 4256 | |
| 4257 | case Intrinsic::init_trampoline: { |
| 4258 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4259 | |
| 4260 | SDValue Ops[6]; |
| 4261 | Ops[0] = getRoot(); |
| 4262 | Ops[1] = getValue(I.getOperand(1)); |
| 4263 | Ops[2] = getValue(I.getOperand(2)); |
| 4264 | Ops[3] = getValue(I.getOperand(3)); |
| 4265 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4266 | Ops[5] = DAG.getSrcValue(F); |
| 4267 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4268 | SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4269 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4270 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4271 | |
| 4272 | setValue(&I, Tmp); |
| 4273 | DAG.setRoot(Tmp.getValue(1)); |
| 4274 | return 0; |
| 4275 | } |
| 4276 | |
| 4277 | case Intrinsic::gcroot: |
| 4278 | if (GFI) { |
| 4279 | Value *Alloca = I.getOperand(1); |
| 4280 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4281 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4282 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4283 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4284 | } |
| 4285 | return 0; |
| 4286 | |
| 4287 | case Intrinsic::gcread: |
| 4288 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4289 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4290 | return 0; |
| 4291 | |
| 4292 | case Intrinsic::flt_rounds: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4293 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4294 | return 0; |
| 4295 | } |
| 4296 | |
| 4297 | case Intrinsic::trap: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4298 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4299 | return 0; |
| 4300 | } |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4301 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4302 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4303 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4304 | case Intrinsic::sadd_with_overflow: |
| 4305 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4306 | case Intrinsic::usub_with_overflow: |
| 4307 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4308 | case Intrinsic::ssub_with_overflow: |
| 4309 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4310 | case Intrinsic::umul_with_overflow: |
| 4311 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4312 | case Intrinsic::smul_with_overflow: |
| 4313 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4314 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4315 | case Intrinsic::prefetch: { |
| 4316 | SDValue Ops[4]; |
| 4317 | Ops[0] = getRoot(); |
| 4318 | Ops[1] = getValue(I.getOperand(1)); |
| 4319 | Ops[2] = getValue(I.getOperand(2)); |
| 4320 | Ops[3] = getValue(I.getOperand(3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4321 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4322 | return 0; |
| 4323 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4324 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4325 | case Intrinsic::memory_barrier: { |
| 4326 | SDValue Ops[6]; |
| 4327 | Ops[0] = getRoot(); |
| 4328 | for (int x = 1; x < 6; ++x) |
| 4329 | Ops[x] = getValue(I.getOperand(x)); |
| 4330 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4331 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4332 | return 0; |
| 4333 | } |
| 4334 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4335 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4336 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4337 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4338 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4339 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4340 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4341 | getValue(I.getOperand(2)), |
| 4342 | getValue(I.getOperand(3)), |
| 4343 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4344 | setValue(&I, L); |
| 4345 | DAG.setRoot(L.getValue(1)); |
| 4346 | return 0; |
| 4347 | } |
| 4348 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4349 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4350 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4351 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4352 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4353 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4354 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4355 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4356 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4357 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4358 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4359 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4360 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4361 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4362 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4363 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4364 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4365 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4366 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4367 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4368 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4369 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4370 | |
| 4371 | case Intrinsic::invariant_start: |
| 4372 | case Intrinsic::lifetime_start: |
| 4373 | // Discard region information. |
| 4374 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
| 4375 | return 0; |
| 4376 | case Intrinsic::invariant_end: |
| 4377 | case Intrinsic::lifetime_end: |
| 4378 | // Discard region information. |
| 4379 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4383 | /// Test if the given instruction is in a position to be optimized |
| 4384 | /// with a tail-call. This roughly means that it's in a block with |
| 4385 | /// a return and there's nothing that needs to be scheduled |
| 4386 | /// between it and the return. |
| 4387 | /// |
| 4388 | /// This function only tests target-independent requirements. |
| 4389 | /// For target-dependent requirements, a target should override |
| 4390 | /// TargetLowering::IsEligibleForTailCallOptimization. |
| 4391 | /// |
| 4392 | static bool |
| 4393 | isInTailCallPosition(const Instruction *I, Attributes RetAttr, |
| 4394 | const TargetLowering &TLI) { |
| 4395 | const BasicBlock *ExitBB = I->getParent(); |
| 4396 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 4397 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
| 4398 | const Function *F = ExitBB->getParent(); |
| 4399 | |
| 4400 | // The block must end in a return statement or an unreachable. |
| 4401 | if (!Ret && !isa<UnreachableInst>(Term)) return false; |
| 4402 | |
| 4403 | // If I will have a chain, make sure no other instruction that will have a |
| 4404 | // chain interposes between I and the return. |
| 4405 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
| 4406 | !I->isSafeToSpeculativelyExecute()) |
| 4407 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 4408 | --BBI) { |
| 4409 | if (&*BBI == I) |
| 4410 | break; |
| 4411 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
| 4412 | !BBI->isSafeToSpeculativelyExecute()) |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
| 4416 | // If the block ends with a void return or unreachable, it doesn't matter |
| 4417 | // what the call's return type is. |
| 4418 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 4419 | |
| 4420 | // Conservatively require the attributes of the call to match those of |
| 4421 | // the return. |
| 4422 | if (F->getAttributes().getRetAttributes() != RetAttr) |
| 4423 | return false; |
| 4424 | |
| 4425 | // Otherwise, make sure the unmodified return value of I is the return value. |
| 4426 | for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; |
| 4427 | U = dyn_cast<Instruction>(U->getOperand(0))) { |
| 4428 | if (!U) |
| 4429 | return false; |
| 4430 | if (!U->hasOneUse()) |
| 4431 | return false; |
| 4432 | if (U == I) |
| 4433 | break; |
| 4434 | // Check for a truly no-op truncate. |
| 4435 | if (isa<TruncInst>(U) && |
| 4436 | TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) |
| 4437 | continue; |
| 4438 | // Check for a truly no-op bitcast. |
| 4439 | if (isa<BitCastInst>(U) && |
| 4440 | (U->getOperand(0)->getType() == U->getType() || |
| 4441 | (isa<PointerType>(U->getOperand(0)->getType()) && |
| 4442 | isa<PointerType>(U->getType())))) |
| 4443 | continue; |
| 4444 | // Otherwise it's not a true no-op. |
| 4445 | return false; |
| 4446 | } |
| 4447 | |
| 4448 | return true; |
| 4449 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4450 | |
| 4451 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4452 | bool isTailCall, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4453 | MachineBasicBlock *LandingPad) { |
| 4454 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4455 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4456 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4457 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4458 | |
| 4459 | TargetLowering::ArgListTy Args; |
| 4460 | TargetLowering::ArgListEntry Entry; |
| 4461 | Args.reserve(CS.arg_size()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4462 | unsigned j = 1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4463 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4464 | i != e; ++i, ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4465 | SDValue ArgNode = getValue(*i); |
| 4466 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4467 | |
| 4468 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4469 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4470 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4471 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4472 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4473 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4474 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4475 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4476 | Args.push_back(Entry); |
| 4477 | } |
| 4478 | |
| 4479 | if (LandingPad && MMI) { |
| 4480 | // Insert a label before the invoke call to mark the try range. This can be |
| 4481 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4482 | BeginLabel = MMI->NextLabelID(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4483 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4484 | // Both PendingLoads and PendingExports must be flushed here; |
| 4485 | // this call might not return. |
| 4486 | (void)getRoot(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4487 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4488 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4491 | // Check if target-independent constraints permit a tail call here. |
| 4492 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4493 | if (isTailCall && |
| 4494 | !isInTailCallPosition(CS.getInstruction(), |
| 4495 | CS.getAttributes().getRetAttributes(), |
| 4496 | TLI)) |
| 4497 | isTailCall = false; |
| 4498 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4499 | std::pair<SDValue,SDValue> Result = |
| 4500 | TLI.LowerCallTo(getRoot(), CS.getType(), |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4501 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4502 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4503 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4504 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4505 | isTailCall, |
| 4506 | !CS.getInstruction()->use_empty(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4507 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4508 | assert((isTailCall || Result.second.getNode()) && |
| 4509 | "Non-null chain expected with non-tail call!"); |
| 4510 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4511 | "Null value expected with tail call!"); |
| 4512 | if (Result.first.getNode()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4513 | setValue(CS.getInstruction(), Result.first); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4514 | // As a special case, a null chain means that a tail call has |
| 4515 | // been emitted and the DAG root is already updated. |
| 4516 | if (Result.second.getNode()) |
| 4517 | DAG.setRoot(Result.second); |
| 4518 | else |
| 4519 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (LandingPad && MMI) { |
| 4522 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4523 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4524 | EndLabel = MMI->NextLabelID(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4525 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4526 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4527 | |
| 4528 | // Inform MachineModuleInfo of range. |
| 4529 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | |
| 4534 | void SelectionDAGLowering::visitCall(CallInst &I) { |
| 4535 | const char *RenameFn = 0; |
| 4536 | if (Function *F = I.getCalledFunction()) { |
| 4537 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4538 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4539 | if (II) { |
| 4540 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4541 | RenameFn = visitIntrinsicCall(I, IID); |
| 4542 | if (!RenameFn) |
| 4543 | return; |
| 4544 | } |
| 4545 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4546 | if (unsigned IID = F->getIntrinsicID()) { |
| 4547 | RenameFn = visitIntrinsicCall(I, IID); |
| 4548 | if (!RenameFn) |
| 4549 | return; |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4554 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4555 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4556 | StringRef Name = F->getName(); |
| 4557 | if (Name == "copysign" || Name == "copysignf") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4558 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4559 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4560 | I.getType() == I.getOperand(1)->getType() && |
| 4561 | I.getType() == I.getOperand(2)->getType()) { |
| 4562 | SDValue LHS = getValue(I.getOperand(1)); |
| 4563 | SDValue RHS = getValue(I.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4564 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4565 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4566 | return; |
| 4567 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4568 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4569 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4570 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4571 | I.getType() == I.getOperand(1)->getType()) { |
| 4572 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4573 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4574 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4575 | return; |
| 4576 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4577 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4578 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4579 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4580 | I.getType() == I.getOperand(1)->getType() && |
| 4581 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4582 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4583 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4584 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4585 | return; |
| 4586 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4587 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4588 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4589 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4590 | I.getType() == I.getOperand(1)->getType() && |
| 4591 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4592 | SDValue Tmp = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4593 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4594 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4597 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4598 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4599 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4600 | I.getType() == I.getOperand(1)->getType() && |
| 4601 | I.onlyReadsMemory()) { |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4602 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4603 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4604 | Tmp.getValueType(), Tmp)); |
| 4605 | return; |
| 4606 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4607 | } |
| 4608 | } |
| 4609 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4610 | visitInlineAsm(&I); |
| 4611 | return; |
| 4612 | } |
| 4613 | |
| 4614 | SDValue Callee; |
| 4615 | if (!RenameFn) |
| 4616 | Callee = getValue(I.getOperand(0)); |
| 4617 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4618 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4619 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4620 | // Check if we can potentially perform a tail call. More detailed |
| 4621 | // checking is be done within LowerCallTo, after more information |
| 4622 | // about the call is known. |
| 4623 | bool isTailCall = PerformTailCallOpt && I.isTailCall(); |
| 4624 | |
| 4625 | LowerCallTo(&I, Callee, isTailCall); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | |
| 4629 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4630 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4631 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4632 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4633 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4634 | SDValue &Chain, |
| 4635 | SDValue *Flag) const { |
| 4636 | // Assemble the legal parts into the final values. |
| 4637 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4638 | SmallVector<SDValue, 8> Parts; |
| 4639 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4640 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4641 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4642 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4643 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4644 | |
| 4645 | Parts.resize(NumRegs); |
| 4646 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4647 | SDValue P; |
| 4648 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4649 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4650 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4651 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4652 | *Flag = P.getValue(2); |
| 4653 | } |
| 4654 | Chain = P.getValue(1); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4655 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4656 | // If the source register was virtual and if we know something about it, |
| 4657 | // add an assert node. |
| 4658 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4659 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4660 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4661 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4662 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4663 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4664 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4665 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4666 | unsigned NumSignBits = LOI.NumSignBits; |
| 4667 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4669 | // FIXME: We capture more information than the dag can represent. For |
| 4670 | // now, just use the tightest assertzext/assertsext possible. |
| 4671 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4672 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4674 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4675 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4676 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4677 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4678 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4679 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4680 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4681 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4682 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4683 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4684 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4685 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4686 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4687 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4688 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4689 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4690 | if (FromVT != MVT::Other) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4691 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4692 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4693 | |
| 4694 | } |
| 4695 | } |
| 4696 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4697 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4698 | Parts[i] = P; |
| 4699 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4700 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4701 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4702 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4703 | Part += NumRegs; |
| 4704 | Parts.clear(); |
| 4705 | } |
| 4706 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4707 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 4708 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4709 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4713 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4714 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4715 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4716 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4717 | SDValue &Chain, SDValue *Flag) const { |
| 4718 | // Get the list of the values's legal parts. |
| 4719 | unsigned NumRegs = Regs.size(); |
| 4720 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4721 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4722 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4723 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4724 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4725 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4726 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4727 | &Parts[Part], NumParts, RegisterVT); |
| 4728 | Part += NumParts; |
| 4729 | } |
| 4730 | |
| 4731 | // Copy the parts into the registers. |
| 4732 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4733 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4734 | SDValue Part; |
| 4735 | if (Flag == 0) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4736 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4737 | else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4738 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4739 | *Flag = Part.getValue(1); |
| 4740 | } |
| 4741 | Chains[i] = Part.getValue(0); |
| 4742 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4743 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4744 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4745 | // 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] | 4746 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4747 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4748 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4749 | // user as well as a successor (the TF operands are flagged to the user). |
| 4750 | // c1, f1 = CopyToReg |
| 4751 | // c2, f2 = CopyToReg |
| 4752 | // c3 = TokenFactor c1, c2 |
| 4753 | // ... |
| 4754 | // = op c3, ..., f2 |
| 4755 | Chain = Chains[NumRegs-1]; |
| 4756 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4757 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4761 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4762 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4763 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4764 | bool HasMatching,unsigned MatchingIdx, |
| 4765 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4766 | std::vector<SDValue> &Ops) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4767 | EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4768 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4769 | unsigned Flag = Code | (Regs.size() << 3); |
| 4770 | if (HasMatching) |
| 4771 | Flag |= 0x80000000 | (MatchingIdx << 16); |
| 4772 | Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4773 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4774 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4775 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4776 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4777 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4778 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4779 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4780 | } |
| 4781 | } |
| 4782 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4783 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4784 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4785 | /// register class for the register. Otherwise, return null. |
| 4786 | static const TargetRegisterClass * |
| 4787 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4788 | const TargetLowering &TLI, |
| 4789 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4790 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4791 | const TargetRegisterClass *FoundRC = 0; |
| 4792 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4793 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4794 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4795 | |
| 4796 | const TargetRegisterClass *RC = *RCI; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4797 | // 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] | 4798 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4799 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4800 | I != E; ++I) { |
| 4801 | if (TLI.isTypeLegal(*I)) { |
| 4802 | // If we have already found this register in a different register class, |
| 4803 | // choose the one with the largest VT specified. For example, on |
| 4804 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4805 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4806 | ThisVT = *I; |
| 4807 | break; |
| 4808 | } |
| 4809 | } |
| 4810 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4811 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4812 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4813 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4814 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4815 | // frame pointer in functions that need it (due to them not being taken |
| 4816 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4817 | // yet). This is a slight code pessimization, but should still work. |
| 4818 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4819 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4820 | if (*I == Reg) { |
| 4821 | // We found a matching register class. Keep looking at others in case |
| 4822 | // we find one with larger registers that this physreg is also in. |
| 4823 | FoundRC = RC; |
| 4824 | FoundVT = ThisVT; |
| 4825 | break; |
| 4826 | } |
| 4827 | } |
| 4828 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4829 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4830 | |
| 4831 | |
| 4832 | namespace llvm { |
| 4833 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4834 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4835 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4836 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4837 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4838 | /// CallOperand - If this is the result output operand or a clobber |
| 4839 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4840 | /// This gets modified as the asm is processed. |
| 4841 | SDValue CallOperand; |
| 4842 | |
| 4843 | /// AssignedRegs - If this is a register or register class operand, this |
| 4844 | /// contains the set of register corresponding to the operand. |
| 4845 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4846 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4847 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4848 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4849 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4850 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4851 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4852 | /// busy in OutputRegs/InputRegs. |
| 4853 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4854 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4855 | std::set<unsigned> &InputRegs, |
| 4856 | const TargetRegisterInfo &TRI) const { |
| 4857 | if (isOutReg) { |
| 4858 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4859 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4860 | } |
| 4861 | if (isInReg) { |
| 4862 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4863 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4864 | } |
| 4865 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4866 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4867 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4868 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4869 | /// MVT::Other. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4870 | EVT getCallOperandValEVT(LLVMContext &Context, |
| 4871 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4872 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4873 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4874 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4875 | if (isa<BasicBlock>(CallOperandVal)) |
| 4876 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4877 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4878 | const llvm::Type *OpTy = CallOperandVal->getType(); |
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 | // If this is an indirect operand, the operand is a pointer to the |
| 4881 | // accessed type. |
| 4882 | if (isIndirect) |
| 4883 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4884 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4885 | // If OpTy is not a single value, it may be a struct/union that we |
| 4886 | // can tile with integers. |
| 4887 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4888 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4889 | switch (BitSize) { |
| 4890 | default: break; |
| 4891 | case 1: |
| 4892 | case 8: |
| 4893 | case 16: |
| 4894 | case 32: |
| 4895 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4896 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4897 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4898 | break; |
| 4899 | } |
| 4900 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4901 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4902 | return TLI.getValueType(OpTy, true); |
| 4903 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4904 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4905 | private: |
| 4906 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4907 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4908 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4909 | const TargetRegisterInfo &TRI) { |
| 4910 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4911 | Regs.insert(Reg); |
| 4912 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4913 | for (; *Aliases; ++Aliases) |
| 4914 | Regs.insert(*Aliases); |
| 4915 | } |
| 4916 | }; |
| 4917 | } // end llvm namespace. |
| 4918 | |
| 4919 | |
| 4920 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4921 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4922 | /// register allocator handle the assignment process. However, if the asm uses |
| 4923 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 4924 | /// allocation. This produces generally horrible, but correct, code. |
| 4925 | /// |
| 4926 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4927 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4928 | /// |
| 4929 | void SelectionDAGLowering:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4930 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4931 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4932 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 4933 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4934 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4968 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 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)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4974 | // Try to convert to the first EVT that the reg class contains. If the |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4975 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 4976 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4977 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 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. |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4987 | RegVT = EVT::getIntegerVT(Context, |
| 4988 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4989 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4990 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4991 | OpInfo.ConstraintVT = RegVT; |
| 4992 | } |
| 4993 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4994 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4995 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 4996 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4997 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4998 | EVT RegVT; |
| 4999 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5000 | |
| 5001 | // If this is a constraint for a specific physical register, like {r17}, |
| 5002 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5003 | if (unsigned AssignedReg = PhysReg.first) { |
| 5004 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5005 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5006 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5007 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5008 | // Get the actual register value type. This is important, because the user |
| 5009 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5010 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5011 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5012 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5013 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5014 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5015 | |
| 5016 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5017 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5018 | TargetRegisterClass::iterator I = RC->begin(); |
| 5019 | for (; *I != AssignedReg; ++I) |
| 5020 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5021 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5022 | // Already added the first reg. |
| 5023 | --NumRegs; ++I; |
| 5024 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5025 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5026 | Regs.push_back(*I); |
| 5027 | } |
| 5028 | } |
| 5029 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5030 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5031 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5032 | return; |
| 5033 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5034 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5035 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5036 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5037 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5038 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5039 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5040 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5041 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5042 | // Create the appropriate number of virtual registers. |
| 5043 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5044 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5045 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5046 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5047 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5048 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5049 | } |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5050 | |
| 5051 | // This is a reference to a register class that doesn't directly correspond |
| 5052 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5053 | // registers from the class. |
| 5054 | std::vector<unsigned> RegClassRegs |
| 5055 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5056 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5057 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5058 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5059 | unsigned NumAllocated = 0; |
| 5060 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5061 | unsigned Reg = RegClassRegs[i]; |
| 5062 | // See if this register is available. |
| 5063 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5064 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5065 | // Make sure we find consecutive registers. |
| 5066 | NumAllocated = 0; |
| 5067 | continue; |
| 5068 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5069 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5070 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5071 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5072 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5073 | if (!RC) { // Couldn't allocate this register. |
| 5074 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5075 | NumAllocated = 0; |
| 5076 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5077 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5078 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5079 | // Okay, this register is good, we can use it. |
| 5080 | ++NumAllocated; |
| 5081 | |
| 5082 | // If we allocated enough consecutive registers, succeed. |
| 5083 | if (NumAllocated == NumRegs) { |
| 5084 | unsigned RegStart = (i-NumAllocated)+1; |
| 5085 | unsigned RegEnd = i+1; |
| 5086 | // Mark all of the allocated registers used. |
| 5087 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5088 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5089 | |
| 5090 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5091 | OpInfo.ConstraintVT); |
| 5092 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5093 | return; |
| 5094 | } |
| 5095 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5096 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5097 | // Otherwise, we couldn't allocate enough registers for this. |
| 5098 | } |
| 5099 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5100 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5101 | /// processed uses a memory 'm' constraint. |
| 5102 | static bool |
| 5103 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5104 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5105 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5106 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5107 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5108 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5109 | if (CType == TargetLowering::C_Memory) |
| 5110 | return true; |
| 5111 | } |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5112 | |
| 5113 | // Indirect operand accesses access memory. |
| 5114 | if (CI.isIndirect) |
| 5115 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | return false; |
| 5119 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5120 | |
| 5121 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5122 | /// |
| 5123 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 5124 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5125 | |
| 5126 | /// ConstraintOperands - Information about all of the constraints. |
| 5127 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
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); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5137 | |
| 5138 | SDValue Chain, Flag; |
| 5139 | |
| 5140 | // We won't need to flush pending loads if this asm doesn't touch |
| 5141 | // memory and is nonvolatile. |
| 5142 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5143 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5144 | else |
| 5145 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5146 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5147 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5148 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5149 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5150 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5151 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5152 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5153 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5154 | |
| 5155 | // Compute the value type for each operand. |
| 5156 | switch (OpInfo.Type) { |
| 5157 | case InlineAsm::isOutput: |
| 5158 | // Indirect outputs just consume an argument. |
| 5159 | if (OpInfo.isIndirect) { |
| 5160 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5161 | break; |
| 5162 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5163 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5164 | // The return value of the call is this value. As such, there is no |
| 5165 | // corresponding argument. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5166 | assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) && |
| 5167 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5168 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5169 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5170 | } else { |
| 5171 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5172 | OpVT = TLI.getValueType(CS.getType()); |
| 5173 | } |
| 5174 | ++ResNo; |
| 5175 | break; |
| 5176 | case InlineAsm::isInput: |
| 5177 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5178 | break; |
| 5179 | case InlineAsm::isClobber: |
| 5180 | // Nothing to do. |
| 5181 | break; |
| 5182 | } |
| 5183 | |
| 5184 | // If this is an input or an indirect output, process the call argument. |
| 5185 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5186 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5187 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5188 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5189 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5190 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5191 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5192 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5193 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5194 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5195 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5196 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5197 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5198 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5199 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5200 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5201 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5202 | // Second pass over the constraints: compute which constraint option to use |
| 5203 | // and assign registers to constraints that want a specific physreg. |
| 5204 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5205 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5206 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5207 | // 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] | 5208 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5209 | // other is floating point, or their sizes are different, flag it as an |
| 5210 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5211 | if (OpInfo.hasMatchingInput()) { |
| 5212 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5213 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5214 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5215 | Input.ConstraintVT.isInteger()) || |
| 5216 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5217 | Input.ConstraintVT.getSizeInBits())) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5218 | llvm_report_error("Unsupported asm: input constraint" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5219 | " with a matching output constraint of incompatible" |
| 5220 | " type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5221 | } |
| 5222 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5223 | } |
| 5224 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5226 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5227 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5228 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5229 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5230 | // need to to provide an address for the memory input. |
| 5231 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5232 | !OpInfo.isIndirect) { |
| 5233 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5234 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5235 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5236 | // Memory operands really want the address of the value. If we don't have |
| 5237 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5238 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5239 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5240 | // If the operand is a float, integer, or vector constant, spill to a |
| 5241 | // constant pool entry to get its address. |
| 5242 | Value *OpVal = OpInfo.CallOperandVal; |
| 5243 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5244 | isa<ConstantVector>(OpVal)) { |
| 5245 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5246 | TLI.getPointerTy()); |
| 5247 | } else { |
| 5248 | // Otherwise, create a stack slot and emit a store to it before the |
| 5249 | // asm. |
| 5250 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5251 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5252 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5253 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5254 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 5255 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5256 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5257 | OpInfo.CallOperand, StackSlot, NULL, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | OpInfo.CallOperand = StackSlot; |
| 5259 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5260 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5261 | // There is no longer a Value* corresponding to this operand. |
| 5262 | OpInfo.CallOperandVal = 0; |
| 5263 | // It is now an indirect operand. |
| 5264 | OpInfo.isIndirect = true; |
| 5265 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5266 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5267 | // If this constraint is for a specific register, allocate it before |
| 5268 | // anything else. |
| 5269 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5270 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5271 | } |
| 5272 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5273 | |
| 5274 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5275 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5276 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5277 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5278 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5279 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5280 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5281 | // to be. |
| 5282 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5283 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5286 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5287 | std::vector<SDValue> AsmNodeOperands; |
| 5288 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5289 | AsmNodeOperands.push_back( |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5290 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5291 | |
| 5292 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5293 | // Loop over all of the inputs, copying the operand values into the |
| 5294 | // appropriate registers and processing the output regs. |
| 5295 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5296 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5297 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5298 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5299 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5300 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5301 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5302 | |
| 5303 | switch (OpInfo.Type) { |
| 5304 | case InlineAsm::isOutput: { |
| 5305 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5306 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5307 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5308 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5309 | |
| 5310 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5311 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5312 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5313 | TLI.getPointerTy())); |
| 5314 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5315 | break; |
| 5316 | } |
| 5317 | |
| 5318 | // Otherwise, this is a register or register class output. |
| 5319 | |
| 5320 | // Copy the output from the appropriate register. Find a register that |
| 5321 | // we can use. |
| 5322 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5323 | llvm_report_error("Couldn't allocate output reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5324 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | // If this is an indirect operand, store through the pointer after the |
| 5328 | // asm. |
| 5329 | if (OpInfo.isIndirect) { |
| 5330 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5331 | OpInfo.CallOperandVal)); |
| 5332 | } else { |
| 5333 | // This is the result value of the call. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5334 | assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) && |
| 5335 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5336 | // Concatenate this output onto the outputs list. |
| 5337 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5338 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5339 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5340 | // Add information to the INLINEASM node to know that this register is |
| 5341 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5342 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5343 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5344 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5345 | false, |
| 5346 | 0, |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5347 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5348 | break; |
| 5349 | } |
| 5350 | case InlineAsm::isInput: { |
| 5351 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5352 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5353 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5354 | // If this is required to match an output register we have already set, |
| 5355 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5356 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5357 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5358 | // Scan until we find the definition we already emitted of this operand. |
| 5359 | // When we find it, create a RegsForValue operand. |
| 5360 | unsigned CurOp = 2; // The first operand. |
| 5361 | for (; OperandNo; --OperandNo) { |
| 5362 | // Advance to the next operand. |
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 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5366 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5367 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5368 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5369 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5372 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5373 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5374 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5375 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5376 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5377 | if (OpInfo.isIndirect) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5378 | llvm_report_error("Don't know how to handle tied indirect " |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5379 | "register inputs yet!"); |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5380 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5381 | RegsForValue MatchedRegs; |
| 5382 | MatchedRegs.TLI = &TLI; |
| 5383 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5384 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5385 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5386 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5387 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5388 | i != e; ++i) |
| 5389 | MatchedRegs.Regs. |
| 5390 | push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5391 | |
| 5392 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5393 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5394 | Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5395 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5396 | true, OpInfo.getMatchedOperand(), |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5397 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5398 | break; |
| 5399 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5400 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5401 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5402 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5403 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5404 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5405 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5406 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5407 | TLI.getPointerTy())); |
| 5408 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5409 | break; |
| 5410 | } |
| 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 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5414 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5415 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5416 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5417 | std::vector<SDValue> Ops; |
| 5418 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5419 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5420 | if (Ops.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5421 | llvm_report_error("Invalid operand for inline asm" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5422 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5423 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5424 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5425 | // Add information to the INLINEASM node to know about this input. |
| 5426 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5427 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5428 | TLI.getPointerTy())); |
| 5429 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5430 | break; |
| 5431 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5432 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5433 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5434 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5435 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5436 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5437 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5438 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5439 | TLI.getPointerTy())); |
| 5440 | AsmNodeOperands.push_back(InOperandVal); |
| 5441 | break; |
| 5442 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5443 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5444 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5445 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5446 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5447 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5448 | "Don't know how to handle indirect register inputs yet!"); |
| 5449 | |
| 5450 | // Copy the input into the appropriate registers. |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5451 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5452 | llvm_report_error("Couldn't allocate input reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5453 | " constraint '"+ OpInfo.ConstraintCode +"'!"); |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5454 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5455 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5456 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
| 5457 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5458 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5459 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5460 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5461 | break; |
| 5462 | } |
| 5463 | case InlineAsm::isClobber: { |
| 5464 | // Add the clobbered value to the operand list, so that the register |
| 5465 | // allocator is aware that the physreg got clobbered. |
| 5466 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5467 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5468 | false, 0, DAG,AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5469 | break; |
| 5470 | } |
| 5471 | } |
| 5472 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5473 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5474 | // Finish up input operands. |
| 5475 | AsmNodeOperands[0] = Chain; |
| 5476 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5477 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5478 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5479 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5480 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5481 | Flag = Chain.getValue(1); |
| 5482 | |
| 5483 | // If this asm returns a register value, copy the result from that register |
| 5484 | // and set it as the value of the call. |
| 5485 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5486 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5487 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5488 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5489 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5490 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5491 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5492 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5493 | // If any of the results of the inline asm is a vector, it may have the |
| 5494 | // wrong width/num elts. This can happen for register classes that can |
| 5495 | // contain multiple different value types. The preg or vreg allocated may |
| 5496 | // not have the same VT as was expected. Convert it to the right type |
| 5497 | // with bit_convert. |
| 5498 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5499 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5500 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5501 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5502 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5503 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5504 | // If a result value was tied to an input value, the computed result may |
| 5505 | // have a wider width than the expected result. Extract the relevant |
| 5506 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5507 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5508 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5509 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5510 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5511 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5512 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5513 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5514 | // Don't need to use this as a chain in this case. |
| 5515 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5516 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5517 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5518 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5519 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5520 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5521 | // Process indirect outputs, first output all of the flagged copies out of |
| 5522 | // physregs. |
| 5523 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5524 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5525 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5526 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
| 5527 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5528 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5529 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5530 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5531 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5532 | // Emit the non-flagged stores from the physregs. |
| 5533 | SmallVector<SDValue, 8> OutChains; |
| 5534 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5535 | OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5536 | StoresToEmit[i].first, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5537 | getValue(StoresToEmit[i].second), |
| 5538 | StoresToEmit[i].second, 0)); |
| 5539 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5540 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5541 | &OutChains[0], OutChains.size()); |
| 5542 | DAG.setRoot(Chain); |
| 5543 | } |
| 5544 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5545 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5546 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5547 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5548 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5549 | DAG.getSrcValue(I.getOperand(1)))); |
| 5550 | } |
| 5551 | |
| 5552 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5553 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5554 | getRoot(), getValue(I.getOperand(0)), |
| 5555 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5556 | setValue(&I, V); |
| 5557 | DAG.setRoot(V.getValue(1)); |
| 5558 | } |
| 5559 | |
| 5560 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5561 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5562 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5563 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5564 | DAG.getSrcValue(I.getOperand(1)))); |
| 5565 | } |
| 5566 | |
| 5567 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5568 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5569 | MVT::Other, getRoot(), |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5570 | getValue(I.getOperand(1)), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5571 | getValue(I.getOperand(2)), |
| 5572 | DAG.getSrcValue(I.getOperand(1)), |
| 5573 | DAG.getSrcValue(I.getOperand(2)))); |
| 5574 | } |
| 5575 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5576 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5577 | /// implementation, which just calls LowerCall. |
| 5578 | /// FIXME: When all targets are |
| 5579 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5580 | std::pair<SDValue, SDValue> |
| 5581 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5582 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5583 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5584 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5585 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5586 | SDValue Callee, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 5587 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5588 | |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5589 | assert((!isTailCall || PerformTailCallOpt) && |
| 5590 | "isTailCall set when tail-call optimizations are disabled!"); |
| 5591 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5592 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5593 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5594 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5595 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5596 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5597 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5598 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5599 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5600 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5601 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5602 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5603 | ISD::ArgFlagsTy Flags; |
| 5604 | unsigned OriginalAlignment = |
| 5605 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5606 | |
| 5607 | if (Args[i].isZExt) |
| 5608 | Flags.setZExt(); |
| 5609 | if (Args[i].isSExt) |
| 5610 | Flags.setSExt(); |
| 5611 | if (Args[i].isInReg) |
| 5612 | Flags.setInReg(); |
| 5613 | if (Args[i].isSRet) |
| 5614 | Flags.setSRet(); |
| 5615 | if (Args[i].isByVal) { |
| 5616 | Flags.setByVal(); |
| 5617 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5618 | const Type *ElementTy = Ty->getElementType(); |
| 5619 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5620 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5621 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5622 | // info is not there but there are cases it cannot get right. |
| 5623 | if (Args[i].Alignment) |
| 5624 | FrameAlign = Args[i].Alignment; |
| 5625 | Flags.setByValAlign(FrameAlign); |
| 5626 | Flags.setByValSize(FrameSize); |
| 5627 | } |
| 5628 | if (Args[i].isNest) |
| 5629 | Flags.setNest(); |
| 5630 | Flags.setOrigAlign(OriginalAlignment); |
| 5631 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5632 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5633 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5634 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5635 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5636 | |
| 5637 | if (Args[i].isSExt) |
| 5638 | ExtendKind = ISD::SIGN_EXTEND; |
| 5639 | else if (Args[i].isZExt) |
| 5640 | ExtendKind = ISD::ZERO_EXTEND; |
| 5641 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5642 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5643 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5644 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5645 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5646 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5647 | if (NumParts > 1 && j == 0) |
| 5648 | MyFlags.Flags.setSplit(); |
| 5649 | else if (j != 0) |
| 5650 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5651 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5652 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5653 | } |
| 5654 | } |
| 5655 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5656 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5657 | // Handle the incoming return values from the call. |
| 5658 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5659 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5660 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5661 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5662 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5663 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5664 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5665 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5666 | ISD::InputArg MyFlags; |
| 5667 | MyFlags.VT = RegisterVT; |
| 5668 | MyFlags.Used = isReturnValueUsed; |
| 5669 | if (RetSExt) |
| 5670 | MyFlags.Flags.setSExt(); |
| 5671 | if (RetZExt) |
| 5672 | MyFlags.Flags.setZExt(); |
| 5673 | if (isInreg) |
| 5674 | MyFlags.Flags.setInReg(); |
| 5675 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5676 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5679 | // Check if target-dependent constraints permit a tail call here. |
| 5680 | // Target-independent constraints should be checked by the caller. |
| 5681 | if (isTailCall && |
| 5682 | !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG)) |
| 5683 | isTailCall = false; |
| 5684 | |
| 5685 | SmallVector<SDValue, 4> InVals; |
| 5686 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
| 5687 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5688 | |
| 5689 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5690 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5691 | "LowerCall didn't return a valid chain!"); |
| 5692 | assert((!isTailCall || InVals.empty()) && |
| 5693 | "LowerCall emitted a return value for a tail call!"); |
| 5694 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5695 | "LowerCall didn't emit the correct number of values!"); |
| 5696 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5697 | assert(InVals[i].getNode() && |
| 5698 | "LowerCall emitted a null value!"); |
| 5699 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5700 | "LowerCall emitted a value with the wrong type!"); |
| 5701 | }); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5702 | |
| 5703 | // For a tail call, the return value is merely live-out and there aren't |
| 5704 | // any nodes in the DAG representing it. Return a special value to |
| 5705 | // indicate that a tail call has been emitted and no more Instructions |
| 5706 | // should be processed in the current block. |
| 5707 | if (isTailCall) { |
| 5708 | DAG.setRoot(Chain); |
| 5709 | return std::make_pair(SDValue(), SDValue()); |
| 5710 | } |
| 5711 | |
| 5712 | // Collect the legal value parts into potentially illegal values |
| 5713 | // that correspond to the original function's return values. |
| 5714 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5715 | if (RetSExt) |
| 5716 | AssertOp = ISD::AssertSext; |
| 5717 | else if (RetZExt) |
| 5718 | AssertOp = ISD::AssertZext; |
| 5719 | SmallVector<SDValue, 4> ReturnValues; |
| 5720 | unsigned CurReg = 0; |
| 5721 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5722 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5723 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5724 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5725 | |
| 5726 | SDValue ReturnValue = |
| 5727 | getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT, |
| 5728 | AssertOp); |
| 5729 | ReturnValues.push_back(ReturnValue); |
| 5730 | CurReg += NumRegs; |
| 5731 | } |
| 5732 | |
| 5733 | // For a function returning void, there is no return value. We can't create |
| 5734 | // such a node, so we just return a null return value in that case. In |
| 5735 | // that case, nothing will actualy look at the value. |
| 5736 | if (ReturnValues.empty()) |
| 5737 | return std::make_pair(SDValue(), Chain); |
| 5738 | |
| 5739 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5740 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5741 | &ReturnValues[0], ReturnValues.size()); |
| 5742 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5743 | return std::make_pair(Res, Chain); |
| 5744 | } |
| 5745 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5746 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5747 | SmallVectorImpl<SDValue> &Results, |
| 5748 | SelectionDAG &DAG) { |
| 5749 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5750 | if (Res.getNode()) |
| 5751 | Results.push_back(Res); |
| 5752 | } |
| 5753 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5754 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5755 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5756 | return SDValue(); |
| 5757 | } |
| 5758 | |
| 5759 | |
| 5760 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
| 5761 | SDValue Op = getValue(V); |
| 5762 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5763 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5764 | "Copy from a reg to the same reg!"); |
| 5765 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5766 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5767 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5768 | SDValue Chain = DAG.getEntryNode(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5769 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5770 | PendingExports.push_back(Chain); |
| 5771 | } |
| 5772 | |
| 5773 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5774 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 5775 | void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5776 | // If this is the entry block, emit arguments. |
| 5777 | Function &F = *LLVMBB->getParent(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5778 | SelectionDAG &DAG = SDL->DAG; |
| 5779 | SDValue OldRoot = DAG.getRoot(); |
| 5780 | DebugLoc dl = SDL->getCurDebugLoc(); |
| 5781 | const TargetData *TD = TLI.getTargetData(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5782 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5783 | // Check whether the function can return without sret-demotion. |
| 5784 | SmallVector<EVT, 4> OutVTs; |
| 5785 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 5786 | getReturnInfo(&F, OutVTs, OutsFlags, TLI); |
| 5787 | // For now, assert and bail out if it can't. |
| 5788 | assert(TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), OutVTs, OutsFlags, |
| 5789 | DAG) && "Cannot fit return value in registers!"); |
| 5790 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5791 | // Set up the incoming argument description vector. |
| 5792 | SmallVector<ISD::InputArg, 16> Ins; |
| 5793 | unsigned Idx = 1; |
| 5794 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5795 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5796 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5797 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5798 | bool isArgValueUsed = !I->use_empty(); |
| 5799 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5800 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5801 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5802 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5803 | ISD::ArgFlagsTy Flags; |
| 5804 | unsigned OriginalAlignment = |
| 5805 | TD->getABITypeAlignment(ArgTy); |
| 5806 | |
| 5807 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5808 | Flags.setZExt(); |
| 5809 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5810 | Flags.setSExt(); |
| 5811 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5812 | Flags.setInReg(); |
| 5813 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5814 | Flags.setSRet(); |
| 5815 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5816 | Flags.setByVal(); |
| 5817 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5818 | const Type *ElementTy = Ty->getElementType(); |
| 5819 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5820 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5821 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5822 | // this info is not there but there are cases it cannot get right. |
| 5823 | if (F.getParamAlignment(Idx)) |
| 5824 | FrameAlign = F.getParamAlignment(Idx); |
| 5825 | Flags.setByValAlign(FrameAlign); |
| 5826 | Flags.setByValSize(FrameSize); |
| 5827 | } |
| 5828 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5829 | Flags.setNest(); |
| 5830 | Flags.setOrigAlign(OriginalAlignment); |
| 5831 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5832 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5833 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5834 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5835 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5836 | if (NumRegs > 1 && i == 0) |
| 5837 | MyFlags.Flags.setSplit(); |
| 5838 | // if it isn't first piece, alignment must be 1 |
| 5839 | else if (i > 0) |
| 5840 | MyFlags.Flags.setOrigAlign(1); |
| 5841 | Ins.push_back(MyFlags); |
| 5842 | } |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | // Call the target to set up the argument values. |
| 5847 | SmallVector<SDValue, 8> InVals; |
| 5848 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5849 | F.isVarArg(), Ins, |
| 5850 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5851 | |
| 5852 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5853 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5854 | "LowerFormalArguments didn't return a valid chain!"); |
| 5855 | assert(InVals.size() == Ins.size() && |
| 5856 | "LowerFormalArguments didn't emit the correct number of values!"); |
| 5857 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5858 | assert(InVals[i].getNode() && |
| 5859 | "LowerFormalArguments emitted a null value!"); |
| 5860 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5861 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5862 | }); |
| 5863 | |
| 5864 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5865 | DAG.setRoot(NewRoot); |
| 5866 | |
| 5867 | // Set up the argument values. |
| 5868 | unsigned i = 0; |
| 5869 | Idx = 1; |
| 5870 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 5871 | ++I, ++Idx) { |
| 5872 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5873 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5874 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5875 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5876 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5877 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5878 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5879 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5880 | |
| 5881 | if (!I->use_empty()) { |
| 5882 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5883 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5884 | AssertOp = ISD::AssertSext; |
| 5885 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5886 | AssertOp = ISD::AssertZext; |
| 5887 | |
| 5888 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts, |
| 5889 | PartVT, VT, AssertOp)); |
| 5890 | } |
| 5891 | i += NumParts; |
| 5892 | } |
| 5893 | if (!I->use_empty()) { |
| 5894 | SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues, |
| 5895 | SDL->getCurDebugLoc())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5896 | // If this argument is live outside of the entry block, insert a copy from |
| 5897 | // whereever we got it to the vreg that other BB's will reference it as. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5898 | SDL->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5899 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5900 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5901 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5902 | |
| 5903 | // Finally, if the target has anything special to do, allow it to do so. |
| 5904 | // FIXME: this should insert code into the DAG! |
| 5905 | EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction()); |
| 5906 | } |
| 5907 | |
| 5908 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 5909 | /// ensure constants are generated when needed. Remember the virtual registers |
| 5910 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 5911 | /// directly add them, because expansion might result in multiple MBB's for one |
| 5912 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 5913 | /// the end. |
| 5914 | /// |
| 5915 | void |
| 5916 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 5917 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 5918 | |
| 5919 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5920 | |
| 5921 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5922 | // from this block. |
| 5923 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5924 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5925 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5926 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5928 | // If this terminator has multiple identical successors (common for |
| 5929 | // switches), only handle each succ once. |
| 5930 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5931 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5932 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5933 | PHINode *PN; |
| 5934 | |
| 5935 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5936 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5937 | // emitted yet. |
| 5938 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5939 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5940 | // Ignore dead phi's. |
| 5941 | if (PN->use_empty()) continue; |
| 5942 | |
| 5943 | unsigned Reg; |
| 5944 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5945 | |
| 5946 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 5947 | unsigned &RegOut = SDL->ConstantsOut[C]; |
| 5948 | if (RegOut == 0) { |
| 5949 | RegOut = FuncInfo->CreateRegForValue(C); |
| 5950 | SDL->CopyValueToVirtualRegister(C, RegOut); |
| 5951 | } |
| 5952 | Reg = RegOut; |
| 5953 | } else { |
| 5954 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 5955 | if (Reg == 0) { |
| 5956 | assert(isa<AllocaInst>(PHIOp) && |
| 5957 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 5958 | "Didn't codegen value into a register!??"); |
| 5959 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
| 5960 | SDL->CopyValueToVirtualRegister(PHIOp, Reg); |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | // Remember that this register needs to added to the machine PHI node as |
| 5965 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5966 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5967 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 5968 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5969 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5970 | unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5971 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
| 5972 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 5973 | Reg += NumRegisters; |
| 5974 | } |
| 5975 | } |
| 5976 | } |
| 5977 | SDL->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5980 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 5981 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 5982 | /// creating SelectionDAG nodes. |
| 5983 | /// |
| 5984 | bool |
| 5985 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 5986 | FastISel *F) { |
| 5987 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5988 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5989 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5990 | unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size(); |
| 5991 | |
| 5992 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5993 | // from this block. |
| 5994 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5995 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5996 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5997 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5998 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5999 | // If this terminator has multiple identical successors (common for |
| 6000 | // switches), only handle each succ once. |
| 6001 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6002 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6003 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6004 | PHINode *PN; |
| 6005 | |
| 6006 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6007 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6008 | // emitted yet. |
| 6009 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6010 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6011 | // Ignore dead phi's. |
| 6012 | if (PN->use_empty()) continue; |
| 6013 | |
| 6014 | // Only handle legal types. Two interesting things to note here. First, |
| 6015 | // by bailing out early, we may leave behind some dead instructions, |
| 6016 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6017 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6018 | // use CreateRegForValue to create registers, so it always creates |
| 6019 | // exactly one register for each non-void instruction. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6020 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6021 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 6022 | // Promote MVT::i1. |
| 6023 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6024 | VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6025 | else { |
| 6026 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6027 | return false; |
| 6028 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6032 | |
| 6033 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6034 | if (Reg == 0) { |
| 6035 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 6036 | return false; |
| 6037 | } |
| 6038 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | return true; |
| 6043 | } |