Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.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" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 15 | #include "SDNodeDbgValue.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 16 | #include "SelectionDAGBuilder.h" |
| 17 | #include "FunctionLoweringInfo.h" |
| 18 | #include "llvm/ADT/BitVector.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| 21 | #include "llvm/Analysis/ConstantFolding.h" |
| 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/GlobalVariable.h" |
| 27 | #include "llvm/InlineAsm.h" |
| 28 | #include "llvm/Instructions.h" |
| 29 | #include "llvm/Intrinsics.h" |
| 30 | #include "llvm/IntrinsicInst.h" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 31 | #include "llvm/LLVMContext.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 33 | #include "llvm/CodeGen/Analysis.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 34 | #include "llvm/CodeGen/FastISel.h" |
| 35 | #include "llvm/CodeGen/GCStrategy.h" |
| 36 | #include "llvm/CodeGen/GCMetadata.h" |
| 37 | #include "llvm/CodeGen/MachineFunction.h" |
| 38 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 39 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 40 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 41 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 42 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 43 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 44 | #include "llvm/CodeGen/SelectionDAG.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 45 | #include "llvm/Analysis/DebugInfo.h" |
| 46 | #include "llvm/Target/TargetRegisterInfo.h" |
| 47 | #include "llvm/Target/TargetData.h" |
| 48 | #include "llvm/Target/TargetFrameInfo.h" |
| 49 | #include "llvm/Target/TargetInstrInfo.h" |
| 50 | #include "llvm/Target/TargetIntrinsicInfo.h" |
| 51 | #include "llvm/Target/TargetLowering.h" |
| 52 | #include "llvm/Target/TargetOptions.h" |
| 53 | #include "llvm/Support/Compiler.h" |
| 54 | #include "llvm/Support/CommandLine.h" |
| 55 | #include "llvm/Support/Debug.h" |
| 56 | #include "llvm/Support/ErrorHandling.h" |
| 57 | #include "llvm/Support/MathExtras.h" |
| 58 | #include "llvm/Support/raw_ostream.h" |
| 59 | #include <algorithm> |
| 60 | using namespace llvm; |
| 61 | |
| 62 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 63 | /// some float libcalls (6, 8 or 12 bits). |
| 64 | static unsigned LimitFloatPrecision; |
| 65 | |
| 66 | static cl::opt<unsigned, true> |
| 67 | LimitFPPrecision("limit-float-precision", |
| 68 | cl::desc("Generate low-precision inline sequences " |
| 69 | "for some float libcalls"), |
| 70 | cl::location(LimitFloatPrecision), |
| 71 | cl::init(0)); |
| 72 | |
| 73 | namespace { |
| 74 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 75 | /// that a particular set of values is assigned, and the type information |
| 76 | /// about the value. The most common situation is to represent one value at a |
| 77 | /// time, but struct or array values are handled element-wise as multiple |
| 78 | /// values. The splitting of aggregates is performed recursively, so that we |
| 79 | /// never have aggregate-typed registers. The values at this point do not |
| 80 | /// necessarily have legal types, so each value may require one or more |
| 81 | /// registers of some legal type. |
| 82 | /// |
| 83 | struct RegsForValue { |
| 84 | /// TLI - The TargetLowering object. |
| 85 | /// |
| 86 | const TargetLowering *TLI; |
| 87 | |
| 88 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 89 | /// may need be promoted or synthesized from one or more registers. |
| 90 | /// |
| 91 | SmallVector<EVT, 4> ValueVTs; |
| 92 | |
| 93 | /// RegVTs - The value types of the registers. This is the same size as |
| 94 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 95 | /// register or registers are. (Individual values are never synthesized |
| 96 | /// from more than one type of register.) |
| 97 | /// |
| 98 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 99 | /// getRegisterType member function, however when with physical registers |
| 100 | /// it is necessary to have a separate record of the types. |
| 101 | /// |
| 102 | SmallVector<EVT, 4> RegVTs; |
| 103 | |
| 104 | /// Regs - This list holds the registers assigned to the values. |
| 105 | /// Each legal or promoted value requires one register, and each |
| 106 | /// expanded value requires multiple registers. |
| 107 | /// |
| 108 | SmallVector<unsigned, 4> Regs; |
| 109 | |
| 110 | RegsForValue() : TLI(0) {} |
| 111 | |
| 112 | RegsForValue(const TargetLowering &tli, |
| 113 | const SmallVector<unsigned, 4> ®s, |
| 114 | EVT regvt, EVT valuevt) |
| 115 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 116 | RegsForValue(const TargetLowering &tli, |
| 117 | const SmallVector<unsigned, 4> ®s, |
| 118 | const SmallVector<EVT, 4> ®vts, |
| 119 | const SmallVector<EVT, 4> &valuevts) |
| 120 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
| 121 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
| 122 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 123 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 124 | |
| 125 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 126 | EVT ValueVT = ValueVTs[Value]; |
| 127 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 128 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
| 129 | for (unsigned i = 0; i != NumRegs; ++i) |
| 130 | Regs.push_back(Reg + i); |
| 131 | RegVTs.push_back(RegisterVT); |
| 132 | Reg += NumRegs; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 137 | bool areValueTypesLegal() { |
| 138 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 139 | EVT RegisterVT = RegVTs[Value]; |
| 140 | if (!TLI->isTypeLegal(RegisterVT)) |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /// append - Add the specified values to this one. |
| 148 | void append(const RegsForValue &RHS) { |
| 149 | TLI = RHS.TLI; |
| 150 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 151 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 152 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 157 | /// this value and returns the result as a ValueVTs value. This uses |
| 158 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 159 | /// If the Flag pointer is NULL, no flag is used. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 160 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 161 | SDValue &Chain, SDValue *Flag) const; |
| 162 | |
| 163 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 164 | /// specified value into the registers specified by this object. This uses |
| 165 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 166 | /// If the Flag pointer is NULL, no flag is used. |
| 167 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 168 | SDValue &Chain, SDValue *Flag) const; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 169 | |
| 170 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 171 | /// operand list. This adds the code marker, matching input operand index |
| 172 | /// (if applicable), and includes the number of values added into it. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 173 | void AddInlineAsmOperands(unsigned Kind, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 174 | bool HasMatching, unsigned MatchingIdx, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 175 | SelectionDAG &DAG, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 176 | std::vector<SDValue> &Ops) const; |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 181 | /// combined into the value they represent. If the parts combine to a type |
| 182 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 183 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 184 | /// (ISD::AssertSext). |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 185 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 186 | const SDValue *Parts, |
| 187 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
| 188 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
| 189 | assert(NumParts > 0 && "No parts to assemble!"); |
| 190 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 191 | SDValue Val = Parts[0]; |
| 192 | |
| 193 | if (NumParts > 1) { |
| 194 | // Assemble the value from multiple parts. |
| 195 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
| 196 | unsigned PartBits = PartVT.getSizeInBits(); |
| 197 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 198 | |
| 199 | // Assemble the power of 2 part. |
| 200 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 201 | 1 << Log2_32(NumParts) : NumParts; |
| 202 | unsigned RoundBits = PartBits * RoundParts; |
| 203 | EVT RoundVT = RoundBits == ValueBits ? |
| 204 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
| 205 | SDValue Lo, Hi; |
| 206 | |
| 207 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
| 208 | |
| 209 | if (RoundParts > 2) { |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 210 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 211 | PartVT, HalfVT); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 212 | Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 213 | RoundParts / 2, PartVT, HalfVT); |
| 214 | } else { |
| 215 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 216 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
| 217 | } |
| 218 | |
| 219 | if (TLI.isBigEndian()) |
| 220 | std::swap(Lo, Hi); |
| 221 | |
| 222 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
| 223 | |
| 224 | if (RoundParts < NumParts) { |
| 225 | // Assemble the trailing non-power-of-2 part. |
| 226 | unsigned OddParts = NumParts - RoundParts; |
| 227 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 228 | Hi = getCopyFromParts(DAG, dl, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 229 | Parts + RoundParts, OddParts, PartVT, OddVT); |
| 230 | |
| 231 | // Combine the round and odd parts. |
| 232 | Lo = Val; |
| 233 | if (TLI.isBigEndian()) |
| 234 | std::swap(Lo, Hi); |
| 235 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 236 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 237 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
| 238 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
| 239 | TLI.getPointerTy())); |
| 240 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 241 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
| 242 | } |
| 243 | } else if (ValueVT.isVector()) { |
| 244 | // Handle a multi-element vector. |
| 245 | EVT IntermediateVT, RegisterVT; |
| 246 | unsigned NumIntermediates; |
| 247 | unsigned NumRegs = |
| 248 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 249 | NumIntermediates, RegisterVT); |
| 250 | assert(NumRegs == NumParts |
| 251 | && "Part count doesn't match vector breakdown!"); |
| 252 | NumParts = NumRegs; // Silence a compiler warning. |
| 253 | assert(RegisterVT == PartVT |
| 254 | && "Part type doesn't match vector breakdown!"); |
| 255 | assert(RegisterVT == Parts[0].getValueType() && |
| 256 | "Part type doesn't match part!"); |
| 257 | |
| 258 | // Assemble the parts into intermediate operands. |
| 259 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 260 | if (NumIntermediates == NumParts) { |
| 261 | // If the register was not expanded, truncate or copy the value, |
| 262 | // as appropriate. |
| 263 | for (unsigned i = 0; i != NumParts; ++i) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 264 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 265 | PartVT, IntermediateVT); |
| 266 | } else if (NumParts > 0) { |
| 267 | // If the intermediate type was expanded, build the intermediate |
| 268 | // operands from the parts. |
| 269 | assert(NumParts % NumIntermediates == 0 && |
| 270 | "Must expand into a divisible number of parts!"); |
| 271 | unsigned Factor = NumParts / NumIntermediates; |
| 272 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 273 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 274 | PartVT, IntermediateVT); |
| 275 | } |
| 276 | |
| 277 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 278 | // intermediate operands. |
| 279 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 280 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
| 281 | ValueVT, &Ops[0], NumIntermediates); |
| 282 | } else if (PartVT.isFloatingPoint()) { |
| 283 | // FP split into multiple FP parts (for ppcf128) |
| 284 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
| 285 | "Unexpected split"); |
| 286 | SDValue Lo, Hi; |
| 287 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 288 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
| 289 | if (TLI.isBigEndian()) |
| 290 | std::swap(Lo, Hi); |
| 291 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 292 | } else { |
| 293 | // FP split into integer parts (soft fp) |
| 294 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 295 | !PartVT.isVector() && "Unexpected split"); |
| 296 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 297 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 302 | PartVT = Val.getValueType(); |
| 303 | |
| 304 | if (PartVT == ValueVT) |
| 305 | return Val; |
| 306 | |
| 307 | if (PartVT.isVector()) { |
| 308 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
| 309 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
| 310 | } |
| 311 | |
| 312 | if (ValueVT.isVector()) { |
| 313 | assert(ValueVT.getVectorElementType() == PartVT && |
| 314 | ValueVT.getVectorNumElements() == 1 && |
| 315 | "Only trivial scalar-to-vector conversions should get here!"); |
| 316 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
| 317 | } |
| 318 | |
| 319 | if (PartVT.isInteger() && |
| 320 | ValueVT.isInteger()) { |
| 321 | if (ValueVT.bitsLT(PartVT)) { |
| 322 | // For a truncate, see if we have any information to |
| 323 | // indicate whether the truncated bits will always be |
| 324 | // zero or sign-extension. |
| 325 | if (AssertOp != ISD::DELETED_NODE) |
| 326 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
| 327 | DAG.getValueType(ValueVT)); |
| 328 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
| 329 | } else { |
| 330 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 335 | if (ValueVT.bitsLT(Val.getValueType())) { |
| 336 | // FP_ROUND's are always exact here. |
| 337 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 338 | DAG.getIntPtrConstant(1)); |
| 339 | } |
| 340 | |
| 341 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
| 342 | } |
| 343 | |
| 344 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 345 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
| 346 | |
| 347 | llvm_unreachable("Unknown mismatch!"); |
| 348 | return SDValue(); |
| 349 | } |
| 350 | |
| 351 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 352 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 353 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 354 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 355 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 356 | EVT PartVT, |
| 357 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
| 358 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 359 | EVT PtrVT = TLI.getPointerTy(); |
| 360 | EVT ValueVT = Val.getValueType(); |
| 361 | unsigned PartBits = PartVT.getSizeInBits(); |
| 362 | unsigned OrigNumParts = NumParts; |
| 363 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 364 | |
| 365 | if (!NumParts) |
| 366 | return; |
| 367 | |
| 368 | if (!ValueVT.isVector()) { |
| 369 | if (PartVT == ValueVT) { |
| 370 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 371 | Parts[0] = Val; |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 376 | // If the parts cover more bits than the value has, promote the value. |
| 377 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 378 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 379 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
| 380 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 381 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 382 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
| 383 | } else { |
| 384 | llvm_unreachable("Unknown mismatch!"); |
| 385 | } |
| 386 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 387 | // Different types of the same size. |
| 388 | assert(NumParts == 1 && PartVT != ValueVT); |
| 389 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
| 390 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 391 | // If the parts cover less bits than value has, truncate the value. |
| 392 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 393 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 394 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
| 395 | } else { |
| 396 | llvm_unreachable("Unknown mismatch!"); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // The value may have changed - recompute ValueVT. |
| 401 | ValueVT = Val.getValueType(); |
| 402 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 403 | "Failed to tile the value with PartVT!"); |
| 404 | |
| 405 | if (NumParts == 1) { |
| 406 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 407 | Parts[0] = Val; |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // Expand the value into multiple parts. |
| 412 | if (NumParts & (NumParts - 1)) { |
| 413 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 414 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 415 | "Do not know what to expand to!"); |
| 416 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 417 | unsigned RoundBits = RoundParts * PartBits; |
| 418 | unsigned OddParts = NumParts - RoundParts; |
| 419 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
| 420 | DAG.getConstant(RoundBits, |
| 421 | TLI.getPointerTy())); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 422 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 423 | OddParts, PartVT); |
| 424 | |
| 425 | if (TLI.isBigEndian()) |
| 426 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 427 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 428 | |
| 429 | NumParts = RoundParts; |
| 430 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 431 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
| 432 | } |
| 433 | |
| 434 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 435 | // EXTRACT_ELEMENT. |
| 436 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 437 | EVT::getIntegerVT(*DAG.getContext(), |
| 438 | ValueVT.getSizeInBits()), |
| 439 | Val); |
| 440 | |
| 441 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 442 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 443 | unsigned ThisBits = StepSize * PartBits / 2; |
| 444 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
| 445 | SDValue &Part0 = Parts[i]; |
| 446 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 447 | |
| 448 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
| 449 | ThisVT, Part0, |
| 450 | DAG.getConstant(1, PtrVT)); |
| 451 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
| 452 | ThisVT, Part0, |
| 453 | DAG.getConstant(0, PtrVT)); |
| 454 | |
| 455 | if (ThisBits == PartBits && ThisVT != PartVT) { |
| 456 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 457 | PartVT, Part0); |
| 458 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 459 | PartVT, Part1); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (TLI.isBigEndian()) |
| 465 | std::reverse(Parts, Parts + OrigNumParts); |
| 466 | |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | // Vector ValueVT. |
| 471 | if (NumParts == 1) { |
| 472 | if (PartVT != ValueVT) { |
| 473 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
| 474 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
| 475 | } else { |
| 476 | assert(ValueVT.getVectorElementType() == PartVT && |
| 477 | ValueVT.getVectorNumElements() == 1 && |
| 478 | "Only trivial vector-to-scalar conversions should get here!"); |
| 479 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 480 | PartVT, Val, |
| 481 | DAG.getConstant(0, PtrVT)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | Parts[0] = Val; |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | // Handle a multi-element vector. |
| 490 | EVT IntermediateVT, RegisterVT; |
| 491 | unsigned NumIntermediates; |
| 492 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 493 | IntermediateVT, NumIntermediates, RegisterVT); |
| 494 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 495 | |
| 496 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 497 | NumParts = NumRegs; // Silence a compiler warning. |
| 498 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 499 | |
| 500 | // Split the vector into intermediate operands. |
| 501 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 502 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
| 503 | if (IntermediateVT.isVector()) |
| 504 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
| 505 | IntermediateVT, Val, |
| 506 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 507 | PtrVT)); |
| 508 | else |
| 509 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 510 | IntermediateVT, Val, |
| 511 | DAG.getConstant(i, PtrVT)); |
| 512 | } |
| 513 | |
| 514 | // Split the intermediate operands into legal parts. |
| 515 | if (NumParts == NumIntermediates) { |
| 516 | // If the register was not expanded, promote or copy the value, |
| 517 | // as appropriate. |
| 518 | for (unsigned i = 0; i != NumParts; ++i) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 519 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 520 | } else if (NumParts > 0) { |
| 521 | // If the intermediate type was expanded, split each the value into |
| 522 | // legal parts. |
| 523 | assert(NumParts % NumIntermediates == 0 && |
| 524 | "Must expand into a divisible number of parts!"); |
| 525 | unsigned Factor = NumParts / NumIntermediates; |
| 526 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 527 | getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | |
| 532 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 533 | AA = &aa; |
| 534 | GFI = gfi; |
| 535 | TD = DAG.getTarget().getTargetData(); |
| 536 | } |
| 537 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 538 | /// clear - Clear out the current SelectionDAG and the associated |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 539 | /// state and prepare this SelectionDAGBuilder object to be used |
| 540 | /// for a new block. This doesn't clear out information about |
| 541 | /// additional blocks that are needed to complete switch lowering |
| 542 | /// or PHI node updating; that information is cleared out as it is |
| 543 | /// consumed. |
| 544 | void SelectionDAGBuilder::clear() { |
| 545 | NodeMap.clear(); |
| 546 | PendingLoads.clear(); |
| 547 | PendingExports.clear(); |
| 548 | EdgeMapping.clear(); |
| 549 | DAG.clear(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 550 | CurDebugLoc = DebugLoc(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 551 | HasTailCall = false; |
| 552 | } |
| 553 | |
| 554 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 555 | /// flushing any PendingLoad items. This must be done before emitting |
| 556 | /// a store or any other node that may need to be ordered after any |
| 557 | /// prior load instructions. |
| 558 | /// |
| 559 | SDValue SelectionDAGBuilder::getRoot() { |
| 560 | if (PendingLoads.empty()) |
| 561 | return DAG.getRoot(); |
| 562 | |
| 563 | if (PendingLoads.size() == 1) { |
| 564 | SDValue Root = PendingLoads[0]; |
| 565 | DAG.setRoot(Root); |
| 566 | PendingLoads.clear(); |
| 567 | return Root; |
| 568 | } |
| 569 | |
| 570 | // Otherwise, we have to make a token factor node. |
| 571 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
| 572 | &PendingLoads[0], PendingLoads.size()); |
| 573 | PendingLoads.clear(); |
| 574 | DAG.setRoot(Root); |
| 575 | return Root; |
| 576 | } |
| 577 | |
| 578 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 579 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 580 | /// to do this before emitting a terminator instruction. |
| 581 | /// |
| 582 | SDValue SelectionDAGBuilder::getControlRoot() { |
| 583 | SDValue Root = DAG.getRoot(); |
| 584 | |
| 585 | if (PendingExports.empty()) |
| 586 | return Root; |
| 587 | |
| 588 | // Turn all of the CopyToReg chains into one factored node. |
| 589 | if (Root.getOpcode() != ISD::EntryToken) { |
| 590 | unsigned i = 0, e = PendingExports.size(); |
| 591 | for (; i != e; ++i) { |
| 592 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 593 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 594 | break; // Don't add the root if we already indirectly depend on it. |
| 595 | } |
| 596 | |
| 597 | if (i == e) |
| 598 | PendingExports.push_back(Root); |
| 599 | } |
| 600 | |
| 601 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
| 602 | &PendingExports[0], |
| 603 | PendingExports.size()); |
| 604 | PendingExports.clear(); |
| 605 | DAG.setRoot(Root); |
| 606 | return Root; |
| 607 | } |
| 608 | |
| 609 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 610 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 611 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 612 | |
| 613 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 614 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 615 | } |
| 616 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 617 | void SelectionDAGBuilder::visit(const Instruction &I) { |
| 618 | // Set up outgoing PHI node register values before emitting the terminator. |
| 619 | if (isa<TerminatorInst>(&I)) |
| 620 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 621 | |
| 622 | CurDebugLoc = I.getDebugLoc(); |
| 623 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 624 | visit(I.getOpcode(), I); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 625 | |
| 626 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 627 | CopyToExportRegsIfNeeded(&I); |
| 628 | |
| 629 | CurDebugLoc = DebugLoc(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 632 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 633 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 634 | } |
| 635 | |
| 636 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 637 | // Note: this doesn't use InstVisitor, because it has to work with |
| 638 | // ConstantExpr's in addition to instructions. |
| 639 | switch (Opcode) { |
| 640 | default: llvm_unreachable("Unknown instruction type encountered!"); |
| 641 | // Build the switch statement using the Instruction.def file. |
| 642 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 643 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
| 644 | #include "llvm/Instruction.def" |
| 645 | } |
| 646 | |
| 647 | // Assign the ordering to the freshly created DAG nodes. |
| 648 | if (NodeMap.count(&I)) { |
| 649 | ++SDNodeOrder; |
| 650 | AssignOrderingToNode(getValue(&I).getNode()); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
| 655 | SDValue &N = NodeMap[V]; |
| 656 | if (N.getNode()) return N; |
| 657 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 658 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 659 | EVT VT = TLI.getValueType(V->getType(), true); |
| 660 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 661 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 662 | return N = DAG.getConstant(*CI, VT); |
| 663 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 664 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 665 | return N = DAG.getGlobalAddress(GV, VT); |
| 666 | |
| 667 | if (isa<ConstantPointerNull>(C)) |
| 668 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 669 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 670 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 671 | return N = DAG.getConstantFP(*CFP, VT); |
| 672 | |
| 673 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
| 674 | return N = DAG.getUNDEF(VT); |
| 675 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 676 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 677 | visit(CE->getOpcode(), *CE); |
| 678 | SDValue N1 = NodeMap[V]; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 679 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 680 | return N1; |
| 681 | } |
| 682 | |
| 683 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 684 | SmallVector<SDValue, 4> Constants; |
| 685 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 686 | OI != OE; ++OI) { |
| 687 | SDNode *Val = getValue(*OI).getNode(); |
| 688 | // If the operand is an empty aggregate, there are no values. |
| 689 | if (!Val) continue; |
| 690 | // Add each leaf value from the operand to the Constants list |
| 691 | // to form a flattened list of all the values. |
| 692 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 693 | Constants.push_back(SDValue(Val, i)); |
| 694 | } |
| 695 | |
| 696 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 697 | getCurDebugLoc()); |
| 698 | } |
| 699 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 700 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 701 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 702 | "Unknown struct or array constant!"); |
| 703 | |
| 704 | SmallVector<EVT, 4> ValueVTs; |
| 705 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 706 | unsigned NumElts = ValueVTs.size(); |
| 707 | if (NumElts == 0) |
| 708 | return SDValue(); // empty struct |
| 709 | SmallVector<SDValue, 4> Constants(NumElts); |
| 710 | for (unsigned i = 0; i != NumElts; ++i) { |
| 711 | EVT EltVT = ValueVTs[i]; |
| 712 | if (isa<UndefValue>(C)) |
| 713 | Constants[i] = DAG.getUNDEF(EltVT); |
| 714 | else if (EltVT.isFloatingPoint()) |
| 715 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 716 | else |
| 717 | Constants[i] = DAG.getConstant(0, EltVT); |
| 718 | } |
| 719 | |
| 720 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 721 | getCurDebugLoc()); |
| 722 | } |
| 723 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 724 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 725 | return DAG.getBlockAddress(BA, VT); |
| 726 | |
| 727 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 728 | unsigned NumElements = VecTy->getNumElements(); |
| 729 | |
| 730 | // Now that we know the number and type of the elements, get that number of |
| 731 | // elements into the Ops array based on what kind of constant it is. |
| 732 | SmallVector<SDValue, 16> Ops; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 733 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 734 | for (unsigned i = 0; i != NumElements; ++i) |
| 735 | Ops.push_back(getValue(CP->getOperand(i))); |
| 736 | } else { |
| 737 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
| 738 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
| 739 | |
| 740 | SDValue Op; |
| 741 | if (EltVT.isFloatingPoint()) |
| 742 | Op = DAG.getConstantFP(0, EltVT); |
| 743 | else |
| 744 | Op = DAG.getConstant(0, EltVT); |
| 745 | Ops.assign(NumElements, Op); |
| 746 | } |
| 747 | |
| 748 | // Create a BUILD_VECTOR node. |
| 749 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 750 | VT, &Ops[0], Ops.size()); |
| 751 | } |
| 752 | |
| 753 | // If this is a static alloca, generate it as the frameindex instead of |
| 754 | // computation. |
| 755 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 756 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 757 | FuncInfo.StaticAllocaMap.find(AI); |
| 758 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 759 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 760 | } |
| 761 | |
| 762 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 763 | assert(InReg && "Value not in map!"); |
| 764 | |
| 765 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
| 766 | SDValue Chain = DAG.getEntryNode(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 767 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 771 | /// type of the given function. This does not require a DAG or a return value, |
| 772 | /// and is suitable for use before any DAGs for the function are constructed. |
| 773 | static void getReturnInfo(const Type* ReturnType, |
| 774 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
| 775 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 776 | const TargetLowering &TLI, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 777 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
| 778 | SmallVector<EVT, 4> ValueVTs; |
| 779 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
| 780 | unsigned NumValues = ValueVTs.size(); |
| 781 | if (NumValues == 0) return; |
| 782 | unsigned Offset = 0; |
| 783 | |
| 784 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 785 | EVT VT = ValueVTs[j]; |
| 786 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 787 | |
| 788 | if (attr & Attribute::SExt) |
| 789 | ExtendKind = ISD::SIGN_EXTEND; |
| 790 | else if (attr & Attribute::ZExt) |
| 791 | ExtendKind = ISD::ZERO_EXTEND; |
| 792 | |
| 793 | // FIXME: C calling convention requires the return type to be promoted to |
| 794 | // at least 32-bit. But this is not necessary for non-C calling |
| 795 | // conventions. The frontend should mark functions whose return values |
| 796 | // require promoting with signext or zeroext attributes. |
| 797 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 798 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
| 799 | if (VT.bitsLT(MinVT)) |
| 800 | VT = MinVT; |
| 801 | } |
| 802 | |
| 803 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 804 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
| 805 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 806 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 807 | |
| 808 | // 'inreg' on function refers to return value |
| 809 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 810 | if (attr & Attribute::InReg) |
| 811 | Flags.setInReg(); |
| 812 | |
| 813 | // Propagate extension type if any |
| 814 | if (attr & Attribute::SExt) |
| 815 | Flags.setSExt(); |
| 816 | else if (attr & Attribute::ZExt) |
| 817 | Flags.setZExt(); |
| 818 | |
| 819 | for (unsigned i = 0; i < NumParts; ++i) { |
| 820 | OutVTs.push_back(PartVT); |
| 821 | OutFlags.push_back(Flags); |
| 822 | if (Offsets) |
| 823 | { |
| 824 | Offsets->push_back(Offset); |
| 825 | Offset += PartSize; |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 831 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 832 | SDValue Chain = getControlRoot(); |
| 833 | SmallVector<ISD::OutputArg, 8> Outs; |
| 834 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 835 | |
| 836 | if (!FLI.CanLowerReturn) { |
| 837 | unsigned DemoteReg = FLI.DemoteRegister; |
| 838 | const Function *F = I.getParent()->getParent(); |
| 839 | |
| 840 | // Emit a store of the return value through the virtual register. |
| 841 | // Leave Outs empty so that LowerReturn won't try to load return |
| 842 | // registers the usual way. |
| 843 | SmallVector<EVT, 1> PtrValueVTs; |
| 844 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
| 845 | PtrValueVTs); |
| 846 | |
| 847 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 848 | SDValue RetOp = getValue(I.getOperand(0)); |
| 849 | |
| 850 | SmallVector<EVT, 4> ValueVTs; |
| 851 | SmallVector<uint64_t, 4> Offsets; |
| 852 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
| 853 | unsigned NumValues = ValueVTs.size(); |
| 854 | |
| 855 | SmallVector<SDValue, 4> Chains(NumValues); |
| 856 | EVT PtrVT = PtrValueVTs[0]; |
| 857 | for (unsigned i = 0; i != NumValues; ++i) { |
| 858 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 859 | DAG.getConstant(Offsets[i], PtrVT)); |
| 860 | Chains[i] = |
| 861 | DAG.getStore(Chain, getCurDebugLoc(), |
| 862 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 863 | Add, NULL, Offsets[i], false, false, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 867 | MVT::Other, &Chains[0], NumValues); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 868 | } else if (I.getNumOperands() != 0) { |
| 869 | SmallVector<EVT, 4> ValueVTs; |
| 870 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 871 | unsigned NumValues = ValueVTs.size(); |
| 872 | if (NumValues) { |
| 873 | SDValue RetOp = getValue(I.getOperand(0)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 874 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 875 | EVT VT = ValueVTs[j]; |
| 876 | |
| 877 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 878 | |
| 879 | const Function *F = I.getParent()->getParent(); |
| 880 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 881 | ExtendKind = ISD::SIGN_EXTEND; |
| 882 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 883 | ExtendKind = ISD::ZERO_EXTEND; |
| 884 | |
| 885 | // FIXME: C calling convention requires the return type to be promoted |
| 886 | // to at least 32-bit. But this is not necessary for non-C calling |
| 887 | // conventions. The frontend should mark functions whose return values |
| 888 | // require promoting with signext or zeroext attributes. |
| 889 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 890 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 891 | if (VT.bitsLT(MinVT)) |
| 892 | VT = MinVT; |
| 893 | } |
| 894 | |
| 895 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 896 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 897 | SmallVector<SDValue, 4> Parts(NumParts); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 898 | getCopyToParts(DAG, getCurDebugLoc(), |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 899 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 900 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 901 | |
| 902 | // 'inreg' on function refers to return value |
| 903 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 904 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 905 | Flags.setInReg(); |
| 906 | |
| 907 | // Propagate extension type if any |
| 908 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 909 | Flags.setSExt(); |
| 910 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 911 | Flags.setZExt(); |
| 912 | |
| 913 | for (unsigned i = 0; i < NumParts; ++i) |
| 914 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
| 920 | CallingConv::ID CallConv = |
| 921 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
| 922 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 923 | Outs, getCurDebugLoc(), DAG); |
| 924 | |
| 925 | // Verify that the target's LowerReturn behaved as expected. |
| 926 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
| 927 | "LowerReturn didn't return a valid chain!"); |
| 928 | |
| 929 | // Update the DAG with the new chain value resulting from return lowering. |
| 930 | DAG.setRoot(Chain); |
| 931 | } |
| 932 | |
| 933 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 934 | /// created for it, emit nodes to copy the value into the virtual |
| 935 | /// registers. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 936 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
| 937 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 938 | if (VMI != FuncInfo.ValueMap.end()) { |
| 939 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 940 | CopyValueToVirtualRegister(V, VMI->second); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
| 944 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 945 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 946 | /// CopyTo/FromReg. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 947 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 948 | // No need to export constants. |
| 949 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
| 950 | |
| 951 | // Already exported? |
| 952 | if (FuncInfo.isExportedInst(V)) return; |
| 953 | |
| 954 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 955 | CopyValueToVirtualRegister(V, Reg); |
| 956 | } |
| 957 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 958 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 959 | const BasicBlock *FromBB) { |
| 960 | // The operands of the setcc have to be in this block. We don't know |
| 961 | // how to export them from some other block. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 962 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 963 | // Can export from current BB. |
| 964 | if (VI->getParent() == FromBB) |
| 965 | return true; |
| 966 | |
| 967 | // Is already exported, noop. |
| 968 | return FuncInfo.isExportedInst(V); |
| 969 | } |
| 970 | |
| 971 | // If this is an argument, we can export it if the BB is the entry block or |
| 972 | // if it is already exported. |
| 973 | if (isa<Argument>(V)) { |
| 974 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 975 | return true; |
| 976 | |
| 977 | // Otherwise, can only export this if it is already exported. |
| 978 | return FuncInfo.isExportedInst(V); |
| 979 | } |
| 980 | |
| 981 | // Otherwise, constants can always be exported. |
| 982 | return true; |
| 983 | } |
| 984 | |
| 985 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 986 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 987 | return I->getParent() == BB; |
| 988 | return true; |
| 989 | } |
| 990 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 991 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 992 | /// This function emits a branch and is used at the leaves of an OR or an |
| 993 | /// AND operator tree. |
| 994 | /// |
| 995 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 996 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 997 | MachineBasicBlock *TBB, |
| 998 | MachineBasicBlock *FBB, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 999 | MachineBasicBlock *CurBB, |
| 1000 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1001 | const BasicBlock *BB = CurBB->getBasicBlock(); |
| 1002 | |
| 1003 | // If the leaf of the tree is a comparison, merge the condition into |
| 1004 | // the caseblock. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1005 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1006 | // The operands of the cmp have to be in this block. We don't know |
| 1007 | // how to export them from some other block. If this is the first block |
| 1008 | // of the sequence, no exporting is needed. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1009 | if (CurBB == SwitchBB || |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1010 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1011 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
| 1012 | ISD::CondCode Condition; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1013 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1014 | Condition = getICmpCondCode(IC->getPredicate()); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1015 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1016 | Condition = getFCmpCondCode(FC->getPredicate()); |
| 1017 | } else { |
| 1018 | Condition = ISD::SETEQ; // silence warning. |
| 1019 | llvm_unreachable("Unknown compare instruction"); |
| 1020 | } |
| 1021 | |
| 1022 | CaseBlock CB(Condition, BOp->getOperand(0), |
| 1023 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1024 | SwitchCases.push_back(CB); |
| 1025 | return; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Create a CaseBlock record representing this branch. |
| 1030 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
| 1031 | NULL, TBB, FBB, CurBB); |
| 1032 | SwitchCases.push_back(CB); |
| 1033 | } |
| 1034 | |
| 1035 | /// FindMergedConditions - If Cond is an expression like |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1036 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1037 | MachineBasicBlock *TBB, |
| 1038 | MachineBasicBlock *FBB, |
| 1039 | MachineBasicBlock *CurBB, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1040 | MachineBasicBlock *SwitchBB, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1041 | unsigned Opc) { |
| 1042 | // If this node is not part of the or/and tree, emit it as a branch. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1043 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1044 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
| 1045 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1046 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1047 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1048 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1049 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | // Create TmpBB after CurBB. |
| 1054 | MachineFunction::iterator BBI = CurBB; |
| 1055 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1056 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1057 | CurBB->getParent()->insert(++BBI, TmpBB); |
| 1058 | |
| 1059 | if (Opc == Instruction::Or) { |
| 1060 | // Codegen X | Y as: |
| 1061 | // jmp_if_X TBB |
| 1062 | // jmp TmpBB |
| 1063 | // TmpBB: |
| 1064 | // jmp_if_Y TBB |
| 1065 | // jmp FBB |
| 1066 | // |
| 1067 | |
| 1068 | // Emit the LHS condition. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1069 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1070 | |
| 1071 | // Emit the RHS condition into TmpBB. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1072 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1073 | } else { |
| 1074 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1075 | // Codegen X & Y as: |
| 1076 | // jmp_if_X TmpBB |
| 1077 | // jmp FBB |
| 1078 | // TmpBB: |
| 1079 | // jmp_if_Y TBB |
| 1080 | // jmp FBB |
| 1081 | // |
| 1082 | // This requires creation of TmpBB after CurBB. |
| 1083 | |
| 1084 | // Emit the LHS condition. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1085 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1086 | |
| 1087 | // Emit the RHS condition into TmpBB. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1088 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1093 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1094 | /// false. |
| 1095 | bool |
| 1096 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1097 | if (Cases.size() != 2) return true; |
| 1098 | |
| 1099 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1100 | // will get folded into a single comparison, so don't emit two blocks. |
| 1101 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1102 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1103 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1104 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1109 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1110 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1111 | Cases[0].CC == Cases[1].CC && |
| 1112 | isa<Constant>(Cases[0].CmpRHS) && |
| 1113 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1114 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1115 | return false; |
| 1116 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1123 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
| 1124 | MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1125 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1126 | // Update machine-CFG edges. |
| 1127 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1128 | |
| 1129 | // Figure out which block is immediately after the current one. |
| 1130 | MachineBasicBlock *NextBlock = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1131 | MachineFunction::iterator BBI = BrMBB; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1132 | if (++BBI != FuncInfo.MF->end()) |
| 1133 | NextBlock = BBI; |
| 1134 | |
| 1135 | if (I.isUnconditional()) { |
| 1136 | // Update machine-CFG edges. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1137 | BrMBB->addSuccessor(Succ0MBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1138 | |
| 1139 | // If this is not a fall-through branch, emit the branch. |
| 1140 | if (Succ0MBB != NextBlock) |
| 1141 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1142 | MVT::Other, getControlRoot(), |
| 1143 | DAG.getBasicBlock(Succ0MBB))); |
| 1144 | |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | // If this condition is one of the special cases we handle, do special stuff |
| 1149 | // now. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1150 | const Value *CondVal = I.getCondition(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1151 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1152 | |
| 1153 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1154 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1155 | // For example, instead of something like: |
| 1156 | // cmp A, B |
| 1157 | // C = seteq |
| 1158 | // cmp D, E |
| 1159 | // F = setle |
| 1160 | // or C, F |
| 1161 | // jnz foo |
| 1162 | // Emit: |
| 1163 | // cmp A, B |
| 1164 | // je foo |
| 1165 | // cmp D, E |
| 1166 | // jle foo |
| 1167 | // |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1168 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1169 | if (BOp->hasOneUse() && |
| 1170 | (BOp->getOpcode() == Instruction::And || |
| 1171 | BOp->getOpcode() == Instruction::Or)) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1172 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1173 | BOp->getOpcode()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1174 | // If the compares in later blocks need to use values not currently |
| 1175 | // exported from this block, export them now. This block should always |
| 1176 | // be the first entry. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1177 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1178 | |
| 1179 | // Allow some cases to be rejected. |
| 1180 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1181 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1182 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1183 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1184 | } |
| 1185 | |
| 1186 | // Emit the branch for this block. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1187 | visitSwitchCase(SwitchCases[0], BrMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1188 | SwitchCases.erase(SwitchCases.begin()); |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1193 | // SwitchCases. |
| 1194 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1195 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
| 1196 | |
| 1197 | SwitchCases.clear(); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | // Create a CaseBlock record representing this branch. |
| 1202 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1203 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1204 | |
| 1205 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1206 | // cond branch. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1207 | visitSwitchCase(CB, BrMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1211 | /// the binary search tree resulting from lowering a switch instruction. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1212 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1213 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1214 | SDValue Cond; |
| 1215 | SDValue CondLHS = getValue(CB.CmpLHS); |
| 1216 | DebugLoc dl = getCurDebugLoc(); |
| 1217 | |
| 1218 | // Build the setcc now. |
| 1219 | if (CB.CmpMHS == NULL) { |
| 1220 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1221 | // handle common cases produced by branch lowering. |
| 1222 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
| 1223 | CB.CC == ISD::SETEQ) |
| 1224 | Cond = CondLHS; |
| 1225 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
| 1226 | CB.CC == ISD::SETEQ) { |
| 1227 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
| 1228 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
| 1229 | } else |
| 1230 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
| 1231 | } else { |
| 1232 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1233 | |
| 1234 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1235 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
| 1236 | |
| 1237 | SDValue CmpOp = getValue(CB.CmpMHS); |
| 1238 | EVT VT = CmpOp.getValueType(); |
| 1239 | |
| 1240 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
| 1241 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
| 1242 | ISD::SETLE); |
| 1243 | } else { |
| 1244 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
| 1245 | VT, CmpOp, DAG.getConstant(Low, VT)); |
| 1246 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
| 1247 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | // Update successor info |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1252 | SwitchBB->addSuccessor(CB.TrueBB); |
| 1253 | SwitchBB->addSuccessor(CB.FalseBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1254 | |
| 1255 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1256 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1257 | MachineBasicBlock *NextBlock = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1258 | MachineFunction::iterator BBI = SwitchBB; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1259 | if (++BBI != FuncInfo.MF->end()) |
| 1260 | NextBlock = BBI; |
| 1261 | |
| 1262 | // If the lhs block is the next block, invert the condition so that we can |
| 1263 | // fall through to the lhs instead of the rhs block. |
| 1264 | if (CB.TrueBB == NextBlock) { |
| 1265 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1266 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
| 1267 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
| 1268 | } |
| 1269 | |
| 1270 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
| 1271 | MVT::Other, getControlRoot(), Cond, |
| 1272 | DAG.getBasicBlock(CB.TrueBB)); |
| 1273 | |
| 1274 | // If the branch was constant folded, fix up the CFG. |
| 1275 | if (BrCond.getOpcode() == ISD::BR) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1276 | SwitchBB->removeSuccessor(CB.FalseBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1277 | } else { |
| 1278 | // Otherwise, go ahead and insert the false branch. |
| 1279 | if (BrCond == getControlRoot()) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1280 | SwitchBB->removeSuccessor(CB.TrueBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (CB.FalseBB != NextBlock) |
| 1283 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1284 | DAG.getBasicBlock(CB.FalseBB)); |
| 1285 | } |
| 1286 | |
| 1287 | DAG.setRoot(BrCond); |
| 1288 | } |
| 1289 | |
| 1290 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1291 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
| 1292 | // Emit the code for the jump table |
| 1293 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
| 1294 | EVT PTy = TLI.getPointerTy(); |
| 1295 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1296 | JT.Reg, PTy); |
| 1297 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
| 1298 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1299 | MVT::Other, Index.getValue(1), |
| 1300 | Table, Index); |
| 1301 | DAG.setRoot(BrJumpTable); |
| 1302 | } |
| 1303 | |
| 1304 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1305 | /// in the JumpTable from switch case. |
| 1306 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1307 | JumpTableHeader &JTH, |
| 1308 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1309 | // Subtract the lowest switch case value from the value being switched on and |
| 1310 | // conditional branch to default mbb if the result is greater than the |
| 1311 | // difference between smallest and largest cases. |
| 1312 | SDValue SwitchOp = getValue(JTH.SValue); |
| 1313 | EVT VT = SwitchOp.getValueType(); |
| 1314 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
| 1315 | DAG.getConstant(JTH.First, VT)); |
| 1316 | |
| 1317 | // The SDNode we just created, which holds the value being switched on minus |
| 1318 | // the smallest case value, needs to be copied to a virtual register so it |
| 1319 | // can be used as an index into the jump table in a subsequent basic block. |
| 1320 | // This value may be smaller or larger than the target's pointer type, and |
| 1321 | // therefore require extension or truncating. |
| 1322 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
| 1323 | |
| 1324 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1325 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1326 | JumpTableReg, SwitchOp); |
| 1327 | JT.Reg = JumpTableReg; |
| 1328 | |
| 1329 | // Emit the range check for the jump table, and branch to the default block |
| 1330 | // for the switch statement if the value being switched on exceeds the largest |
| 1331 | // case in the switch. |
| 1332 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
| 1333 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
| 1334 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1335 | ISD::SETUGT); |
| 1336 | |
| 1337 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1338 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1339 | MachineBasicBlock *NextBlock = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1340 | MachineFunction::iterator BBI = SwitchBB; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1341 | |
| 1342 | if (++BBI != FuncInfo.MF->end()) |
| 1343 | NextBlock = BBI; |
| 1344 | |
| 1345 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
| 1346 | MVT::Other, CopyTo, CMP, |
| 1347 | DAG.getBasicBlock(JT.Default)); |
| 1348 | |
| 1349 | if (JT.MBB != NextBlock) |
| 1350 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1351 | DAG.getBasicBlock(JT.MBB)); |
| 1352 | |
| 1353 | DAG.setRoot(BrCond); |
| 1354 | } |
| 1355 | |
| 1356 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1357 | /// suitable for "bit tests" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1358 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1359 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1360 | // Subtract the minimum value |
| 1361 | SDValue SwitchOp = getValue(B.SValue); |
| 1362 | EVT VT = SwitchOp.getValueType(); |
| 1363 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
| 1364 | DAG.getConstant(B.First, VT)); |
| 1365 | |
| 1366 | // Check range |
| 1367 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1368 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1369 | Sub, DAG.getConstant(B.Range, VT), |
| 1370 | ISD::SETUGT); |
| 1371 | |
| 1372 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1373 | TLI.getPointerTy()); |
| 1374 | |
| 1375 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1376 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1377 | B.Reg, ShiftOp); |
| 1378 | |
| 1379 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1380 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1381 | MachineBasicBlock *NextBlock = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1382 | MachineFunction::iterator BBI = SwitchBB; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1383 | if (++BBI != FuncInfo.MF->end()) |
| 1384 | NextBlock = BBI; |
| 1385 | |
| 1386 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1387 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1388 | SwitchBB->addSuccessor(B.Default); |
| 1389 | SwitchBB->addSuccessor(MBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1390 | |
| 1391 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
| 1392 | MVT::Other, CopyTo, RangeCmp, |
| 1393 | DAG.getBasicBlock(B.Default)); |
| 1394 | |
| 1395 | if (MBB != NextBlock) |
| 1396 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1397 | DAG.getBasicBlock(MBB)); |
| 1398 | |
| 1399 | DAG.setRoot(BrRange); |
| 1400 | } |
| 1401 | |
| 1402 | /// visitBitTestCase - this function produces one "bit test" |
| 1403 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1404 | unsigned Reg, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1405 | BitTestCase &B, |
| 1406 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1407 | // Make desired shift |
| 1408 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
| 1409 | TLI.getPointerTy()); |
| 1410 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
| 1411 | TLI.getPointerTy(), |
| 1412 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1413 | ShiftOp); |
| 1414 | |
| 1415 | // Emit bit tests and jumps |
| 1416 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
| 1417 | TLI.getPointerTy(), SwitchVal, |
| 1418 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
| 1419 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1420 | TLI.getSetCCResultType(AndOp.getValueType()), |
| 1421 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
| 1422 | ISD::SETNE); |
| 1423 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1424 | SwitchBB->addSuccessor(B.TargetBB); |
| 1425 | SwitchBB->addSuccessor(NextMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1426 | |
| 1427 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
| 1428 | MVT::Other, getControlRoot(), |
| 1429 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
| 1430 | |
| 1431 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1432 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1433 | MachineBasicBlock *NextBlock = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1434 | MachineFunction::iterator BBI = SwitchBB; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1435 | if (++BBI != FuncInfo.MF->end()) |
| 1436 | NextBlock = BBI; |
| 1437 | |
| 1438 | if (NextMBB != NextBlock) |
| 1439 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1440 | DAG.getBasicBlock(NextMBB)); |
| 1441 | |
| 1442 | DAG.setRoot(BrAnd); |
| 1443 | } |
| 1444 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1445 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
| 1446 | MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1447 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1448 | // Retrieve successors. |
| 1449 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1450 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1451 | |
| 1452 | const Value *Callee(I.getCalledValue()); |
| 1453 | if (isa<InlineAsm>(Callee)) |
| 1454 | visitInlineAsm(&I); |
| 1455 | else |
| 1456 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
| 1457 | |
| 1458 | // If the value of the invoke is used outside of its defining block, make it |
| 1459 | // available as a virtual register. |
| 1460 | CopyToExportRegsIfNeeded(&I); |
| 1461 | |
| 1462 | // Update successor info |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1463 | InvokeMBB->addSuccessor(Return); |
| 1464 | InvokeMBB->addSuccessor(LandingPad); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1465 | |
| 1466 | // Drop into normal successor. |
| 1467 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1468 | MVT::Other, getControlRoot(), |
| 1469 | DAG.getBasicBlock(Return))); |
| 1470 | } |
| 1471 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1472 | void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1476 | /// small case ranges). |
| 1477 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1478 | CaseRecVector& WorkList, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1479 | const Value* SV, |
| 1480 | MachineBasicBlock *Default, |
| 1481 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1482 | Case& BackCase = *(CR.Range.second-1); |
| 1483 | |
| 1484 | // Size is the number of Cases represented by this range. |
| 1485 | size_t Size = CR.Range.second - CR.Range.first; |
| 1486 | if (Size > 3) |
| 1487 | return false; |
| 1488 | |
| 1489 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1490 | // inserting any additional MBBs necessary to represent the switch. |
| 1491 | MachineFunction *CurMF = FuncInfo.MF; |
| 1492 | |
| 1493 | // Figure out which block is immediately after the current one. |
| 1494 | MachineBasicBlock *NextBlock = 0; |
| 1495 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1496 | |
| 1497 | if (++BBI != FuncInfo.MF->end()) |
| 1498 | NextBlock = BBI; |
| 1499 | |
| 1500 | // TODO: If any two of the cases has the same destination, and if one value |
| 1501 | // is the same as the other, but has one bit unset that the other has set, |
| 1502 | // use bit manipulation to do two compares at once. For example: |
| 1503 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
| 1504 | |
| 1505 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1506 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1507 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1508 | // branches in this order. See if rearranging a case value would help. |
| 1509 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1510 | if (I->BB == NextBlock) { |
| 1511 | std::swap(*I, BackCase); |
| 1512 | break; |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | // Create a CaseBlock record representing a conditional branch to |
| 1518 | // the Case's target mbb if the value being switched on SV is equal |
| 1519 | // to C. |
| 1520 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1521 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1522 | MachineBasicBlock *FallThrough; |
| 1523 | if (I != E-1) { |
| 1524 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1525 | CurMF->insert(BBI, FallThrough); |
| 1526 | |
| 1527 | // Put SV in a virtual register to make it available from the new blocks. |
| 1528 | ExportFromCurrentBlock(SV); |
| 1529 | } else { |
| 1530 | // If the last case doesn't match, go to the default block. |
| 1531 | FallThrough = Default; |
| 1532 | } |
| 1533 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1534 | const Value *RHS, *LHS, *MHS; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1535 | ISD::CondCode CC; |
| 1536 | if (I->High == I->Low) { |
| 1537 | // This is just small small case range :) containing exactly 1 case |
| 1538 | CC = ISD::SETEQ; |
| 1539 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1540 | } else { |
| 1541 | CC = ISD::SETLE; |
| 1542 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1543 | } |
| 1544 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
| 1545 | |
| 1546 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1547 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1548 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1549 | // before the next MBB. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1550 | if (CurBlock == SwitchBB) |
| 1551 | visitSwitchCase(CB, SwitchBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1552 | else |
| 1553 | SwitchCases.push_back(CB); |
| 1554 | |
| 1555 | CurBlock = FallThrough; |
| 1556 | } |
| 1557 | |
| 1558 | return true; |
| 1559 | } |
| 1560 | |
| 1561 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1562 | return !DisableJumpTables && |
| 1563 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1564 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
| 1565 | } |
| 1566 | |
| 1567 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1568 | APInt LastExt(Last), FirstExt(First); |
| 1569 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1570 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1571 | return (LastExt - FirstExt + 1ULL); |
| 1572 | } |
| 1573 | |
| 1574 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1575 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1576 | CaseRecVector& WorkList, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1577 | const Value* SV, |
| 1578 | MachineBasicBlock* Default, |
| 1579 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1580 | Case& FrontCase = *CR.Range.first; |
| 1581 | Case& BackCase = *(CR.Range.second-1); |
| 1582 | |
| 1583 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1584 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
| 1585 | |
| 1586 | APInt TSize(First.getBitWidth(), 0); |
| 1587 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1588 | I!=E; ++I) |
| 1589 | TSize += I->size(); |
| 1590 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1591 | if (!areJTsAllowed(TLI) || TSize.ult(4)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1592 | return false; |
| 1593 | |
| 1594 | APInt Range = ComputeRange(First, Last); |
| 1595 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
| 1596 | if (Density < 0.4) |
| 1597 | return false; |
| 1598 | |
| 1599 | DEBUG(dbgs() << "Lowering jump table\n" |
| 1600 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1601 | << "Range: " << Range |
| 1602 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
| 1603 | |
| 1604 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1605 | // inserting any additional MBBs necessary to represent the switch. |
| 1606 | MachineFunction *CurMF = FuncInfo.MF; |
| 1607 | |
| 1608 | // Figure out which block is immediately after the current one. |
| 1609 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1610 | ++BBI; |
| 1611 | |
| 1612 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1613 | |
| 1614 | // Create a new basic block to hold the code for loading the address |
| 1615 | // of the jump table, and jumping to it. Update successor information; |
| 1616 | // we will either branch to the default case for the switch, or the jump |
| 1617 | // table. |
| 1618 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1619 | CurMF->insert(BBI, JumpTableBB); |
| 1620 | CR.CaseBB->addSuccessor(Default); |
| 1621 | CR.CaseBB->addSuccessor(JumpTableBB); |
| 1622 | |
| 1623 | // Build a vector of destination BBs, corresponding to each target |
| 1624 | // of the jump table. If the value of the jump table slot corresponds to |
| 1625 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1626 | // the default BB. |
| 1627 | std::vector<MachineBasicBlock*> DestBBs; |
| 1628 | APInt TEI = First; |
| 1629 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) { |
| 1630 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1631 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
| 1632 | |
| 1633 | if (Low.sle(TEI) && TEI.sle(High)) { |
| 1634 | DestBBs.push_back(I->BB); |
| 1635 | if (TEI==High) |
| 1636 | ++I; |
| 1637 | } else { |
| 1638 | DestBBs.push_back(Default); |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // Update successor info. Add one edge to each unique successor. |
| 1643 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1644 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
| 1645 | E = DestBBs.end(); I != E; ++I) { |
| 1646 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1647 | SuccsHandled[(*I)->getNumber()] = true; |
| 1648 | JumpTableBB->addSuccessor(*I); |
| 1649 | } |
| 1650 | } |
| 1651 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1652 | // Create a jump table index for this jump table. |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1653 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1654 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1655 | ->createJumpTableIndex(DestBBs); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1656 | |
| 1657 | // Set the jump table information so that we can codegen it as a second |
| 1658 | // MachineBasicBlock |
| 1659 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1660 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 1661 | if (CR.CaseBB == SwitchBB) |
| 1662 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1663 | |
| 1664 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1665 | |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
| 1669 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1670 | /// 2 subtrees. |
| 1671 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1672 | CaseRecVector& WorkList, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1673 | const Value* SV, |
| 1674 | MachineBasicBlock *Default, |
| 1675 | MachineBasicBlock *SwitchBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1676 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1677 | // inserting any additional MBBs necessary to represent the switch. |
| 1678 | MachineFunction *CurMF = FuncInfo.MF; |
| 1679 | |
| 1680 | // Figure out which block is immediately after the current one. |
| 1681 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1682 | ++BBI; |
| 1683 | |
| 1684 | Case& FrontCase = *CR.Range.first; |
| 1685 | Case& BackCase = *(CR.Range.second-1); |
| 1686 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1687 | |
| 1688 | // Size is the number of Cases represented by this range. |
| 1689 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1690 | |
| 1691 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1692 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
| 1693 | double FMetric = 0; |
| 1694 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1695 | |
| 1696 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1697 | // (heuristically) allow us to emit JumpTable's later. |
| 1698 | APInt TSize(First.getBitWidth(), 0); |
| 1699 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1700 | I!=E; ++I) |
| 1701 | TSize += I->size(); |
| 1702 | |
| 1703 | APInt LSize = FrontCase.size(); |
| 1704 | APInt RSize = TSize-LSize; |
| 1705 | DEBUG(dbgs() << "Selecting best pivot: \n" |
| 1706 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1707 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
| 1708 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1709 | J!=E; ++I, ++J) { |
| 1710 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1711 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
| 1712 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1713 | assert((Range - 2ULL).isNonNegative() && |
| 1714 | "Invalid case distance"); |
| 1715 | double LDensity = (double)LSize.roundToDouble() / |
| 1716 | (LEnd - First + 1ULL).roundToDouble(); |
| 1717 | double RDensity = (double)RSize.roundToDouble() / |
| 1718 | (Last - RBegin + 1ULL).roundToDouble(); |
| 1719 | double Metric = Range.logBase2()*(LDensity+RDensity); |
| 1720 | // Should always split in some non-trivial place |
| 1721 | DEBUG(dbgs() <<"=>Step\n" |
| 1722 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1723 | << "LDensity: " << LDensity |
| 1724 | << ", RDensity: " << RDensity << '\n' |
| 1725 | << "Metric: " << Metric << '\n'); |
| 1726 | if (FMetric < Metric) { |
| 1727 | Pivot = J; |
| 1728 | FMetric = Metric; |
| 1729 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
| 1730 | } |
| 1731 | |
| 1732 | LSize += J->size(); |
| 1733 | RSize -= J->size(); |
| 1734 | } |
| 1735 | if (areJTsAllowed(TLI)) { |
| 1736 | // If our case is dense we *really* should handle it earlier! |
| 1737 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1738 | } else { |
| 1739 | Pivot = CR.Range.first + Size/2; |
| 1740 | } |
| 1741 | |
| 1742 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1743 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1744 | Constant *C = Pivot->Low; |
| 1745 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
| 1746 | |
| 1747 | // We know that we branch to the LHS if the Value being switched on is |
| 1748 | // less than the Pivot value, C. We use this to optimize our binary |
| 1749 | // tree a bit, by recognizing that if SV is greater than or equal to the |
| 1750 | // LHS's Case Value, and that Case Value is exactly one less than the |
| 1751 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1752 | // rather than creating a leaf node for it. |
| 1753 | if ((LHSR.second - LHSR.first) == 1 && |
| 1754 | LHSR.first->High == CR.GE && |
| 1755 | cast<ConstantInt>(C)->getValue() == |
| 1756 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
| 1757 | TrueBB = LHSR.first->BB; |
| 1758 | } else { |
| 1759 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1760 | CurMF->insert(BBI, TrueBB); |
| 1761 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
| 1762 | |
| 1763 | // Put SV in a virtual register to make it available from the new blocks. |
| 1764 | ExportFromCurrentBlock(SV); |
| 1765 | } |
| 1766 | |
| 1767 | // Similar to the optimization above, if the Value being switched on is |
| 1768 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1769 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1770 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1771 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
| 1772 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1773 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
| 1774 | FalseBB = RHSR.first->BB; |
| 1775 | } else { |
| 1776 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1777 | CurMF->insert(BBI, FalseBB); |
| 1778 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
| 1779 | |
| 1780 | // Put SV in a virtual register to make it available from the new blocks. |
| 1781 | ExportFromCurrentBlock(SV); |
| 1782 | } |
| 1783 | |
| 1784 | // Create a CaseBlock record representing a conditional branch to |
| 1785 | // the LHS node if the value being switched on SV is less than C. |
| 1786 | // Otherwise, branch to LHS. |
| 1787 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1788 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1789 | if (CR.CaseBB == SwitchBB) |
| 1790 | visitSwitchCase(CB, SwitchBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1791 | else |
| 1792 | SwitchCases.push_back(CB); |
| 1793 | |
| 1794 | return true; |
| 1795 | } |
| 1796 | |
| 1797 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1798 | /// range span less, than machine word bitwidth, encode case range into series |
| 1799 | /// of masks and emit bit tests with these masks. |
| 1800 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1801 | CaseRecVector& WorkList, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1802 | const Value* SV, |
| 1803 | MachineBasicBlock* Default, |
| 1804 | MachineBasicBlock *SwitchBB){ |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1805 | EVT PTy = TLI.getPointerTy(); |
| 1806 | unsigned IntPtrBits = PTy.getSizeInBits(); |
| 1807 | |
| 1808 | Case& FrontCase = *CR.Range.first; |
| 1809 | Case& BackCase = *(CR.Range.second-1); |
| 1810 | |
| 1811 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1812 | // inserting any additional MBBs necessary to represent the switch. |
| 1813 | MachineFunction *CurMF = FuncInfo.MF; |
| 1814 | |
| 1815 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1816 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1817 | return false; |
| 1818 | |
| 1819 | size_t numCmps = 0; |
| 1820 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1821 | I!=E; ++I) { |
| 1822 | // Single case counts one, case range - two. |
| 1823 | numCmps += (I->Low == I->High ? 1 : 2); |
| 1824 | } |
| 1825 | |
| 1826 | // Count unique destinations |
| 1827 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1828 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1829 | Dests.insert(I->BB); |
| 1830 | if (Dests.size() > 3) |
| 1831 | // Don't bother the code below, if there are too much unique destinations |
| 1832 | return false; |
| 1833 | } |
| 1834 | DEBUG(dbgs() << "Total number of unique destinations: " |
| 1835 | << Dests.size() << '\n' |
| 1836 | << "Total number of comparisons: " << numCmps << '\n'); |
| 1837 | |
| 1838 | // Compute span of values. |
| 1839 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1840 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
| 1841 | APInt cmpRange = maxValue - minValue; |
| 1842 | |
| 1843 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
| 1844 | << "Low bound: " << minValue << '\n' |
| 1845 | << "High bound: " << maxValue << '\n'); |
| 1846 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1847 | if (cmpRange.uge(IntPtrBits) || |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1848 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1849 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1850 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1851 | return false; |
| 1852 | |
| 1853 | DEBUG(dbgs() << "Emitting bit tests\n"); |
| 1854 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1855 | |
| 1856 | // Optimize the case where all the case values fit in a |
| 1857 | // word without having to subtract minValue. In this case, |
| 1858 | // we can optimize away the subtraction. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1859 | if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1860 | cmpRange = maxValue; |
| 1861 | } else { |
| 1862 | lowBound = minValue; |
| 1863 | } |
| 1864 | |
| 1865 | CaseBitsVector CasesBits; |
| 1866 | unsigned i, count = 0; |
| 1867 | |
| 1868 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1869 | MachineBasicBlock* Dest = I->BB; |
| 1870 | for (i = 0; i < count; ++i) |
| 1871 | if (Dest == CasesBits[i].BB) |
| 1872 | break; |
| 1873 | |
| 1874 | if (i == count) { |
| 1875 | assert((count < 3) && "Too much destinations to test!"); |
| 1876 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1877 | count++; |
| 1878 | } |
| 1879 | |
| 1880 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1881 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1882 | |
| 1883 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1884 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1885 | |
| 1886 | for (uint64_t j = lo; j <= hi; j++) { |
| 1887 | CasesBits[i].Mask |= 1ULL << j; |
| 1888 | CasesBits[i].Bits++; |
| 1889 | } |
| 1890 | |
| 1891 | } |
| 1892 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
| 1893 | |
| 1894 | BitTestInfo BTC; |
| 1895 | |
| 1896 | // Figure out which block is immediately after the current one. |
| 1897 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1898 | ++BBI; |
| 1899 | |
| 1900 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1901 | |
| 1902 | DEBUG(dbgs() << "Cases:\n"); |
| 1903 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
| 1904 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
| 1905 | << ", Bits: " << CasesBits[i].Bits |
| 1906 | << ", BB: " << CasesBits[i].BB << '\n'); |
| 1907 | |
| 1908 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1909 | CurMF->insert(BBI, CaseBB); |
| 1910 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1911 | CaseBB, |
| 1912 | CasesBits[i].BB)); |
| 1913 | |
| 1914 | // Put SV in a virtual register to make it available from the new blocks. |
| 1915 | ExportFromCurrentBlock(SV); |
| 1916 | } |
| 1917 | |
| 1918 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1919 | -1U, (CR.CaseBB == SwitchBB), |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1920 | CR.CaseBB, Default, BTC); |
| 1921 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1922 | if (CR.CaseBB == SwitchBB) |
| 1923 | visitBitTestHeader(BTB, SwitchBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1924 | |
| 1925 | BitTestCases.push_back(BTB); |
| 1926 | |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
| 1930 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
| 1931 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 1932 | const SwitchInst& SI) { |
| 1933 | size_t numCmps = 0; |
| 1934 | |
| 1935 | // Start with "simple" cases |
| 1936 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
| 1937 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1938 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1939 | SI.getSuccessorValue(i), |
| 1940 | SMBB)); |
| 1941 | } |
| 1942 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1943 | |
| 1944 | // Merge case into clusters |
| 1945 | if (Cases.size() >= 2) |
| 1946 | // Must recompute end() each iteration because it may be |
| 1947 | // invalidated by erase if we hold on to it |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1948 | for (CaseItr TmpBegin = Cases.begin(), I = TmpBegin, J = ++TmpBegin; J != Cases.end(); ) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1949 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 1950 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
| 1951 | MachineBasicBlock* nextBB = J->BB; |
| 1952 | MachineBasicBlock* currentBB = I->BB; |
| 1953 | |
| 1954 | // If the two neighboring cases go to the same destination, merge them |
| 1955 | // into a single case. |
| 1956 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
| 1957 | I->High = J->High; |
| 1958 | J = Cases.erase(J); |
| 1959 | } else { |
| 1960 | I = J++; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1965 | if (I->Low != I->High) |
| 1966 | // A range counts double, since it requires two compares. |
| 1967 | ++numCmps; |
| 1968 | } |
| 1969 | |
| 1970 | return numCmps; |
| 1971 | } |
| 1972 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1973 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
| 1974 | MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()]; |
| 1975 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1976 | // Figure out which block is immediately after the current one. |
| 1977 | MachineBasicBlock *NextBlock = 0; |
| 1978 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 1979 | |
| 1980 | // If there is only the default destination, branch to it if it is not the |
| 1981 | // next basic block. Otherwise, just fall through. |
| 1982 | if (SI.getNumOperands() == 2) { |
| 1983 | // Update machine-CFG edges. |
| 1984 | |
| 1985 | // If this is not a fall-through branch, emit the branch. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1986 | SwitchMBB->addSuccessor(Default); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1987 | if (Default != NextBlock) |
| 1988 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1989 | MVT::Other, getControlRoot(), |
| 1990 | DAG.getBasicBlock(Default))); |
| 1991 | |
| 1992 | return; |
| 1993 | } |
| 1994 | |
| 1995 | // If there are any non-default case statements, create a vector of Cases |
| 1996 | // representing each one, and sort the vector so that we can efficiently |
| 1997 | // create a binary search tree from them. |
| 1998 | CaseVector Cases; |
| 1999 | size_t numCmps = Clusterify(Cases, SI); |
| 2000 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
| 2001 | << ". Total compares: " << numCmps << '\n'); |
| 2002 | numCmps = 0; |
| 2003 | |
| 2004 | // Get the Value to be switched on and default basic blocks, which will be |
| 2005 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2006 | // search tree. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2007 | const Value *SV = SI.getOperand(0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2008 | |
| 2009 | // Push the initial CaseRec onto the worklist |
| 2010 | CaseRecVector WorkList; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2011 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2012 | CaseRange(Cases.begin(),Cases.end()))); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2013 | |
| 2014 | while (!WorkList.empty()) { |
| 2015 | // Grab a record representing a case range to process off the worklist |
| 2016 | CaseRec CR = WorkList.back(); |
| 2017 | WorkList.pop_back(); |
| 2018 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2019 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2020 | continue; |
| 2021 | |
| 2022 | // If the range has few cases (two or less) emit a series of specific |
| 2023 | // tests. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2024 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2025 | continue; |
| 2026 | |
| 2027 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2028 | // target supports indirect branches, then emit a jump table rather than |
| 2029 | // lowering the switch to a binary tree of conditional branches. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2030 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2031 | continue; |
| 2032 | |
| 2033 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2034 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2035 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2039 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
| 2040 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 2041 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2042 | // Update machine-CFG edges with unique successors. |
| 2043 | SmallVector<BasicBlock*, 32> succs; |
| 2044 | succs.reserve(I.getNumSuccessors()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2045 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2046 | succs.push_back(I.getSuccessor(i)); |
| 2047 | array_pod_sort(succs.begin(), succs.end()); |
| 2048 | succs.erase(std::unique(succs.begin(), succs.end()), succs.end()); |
| 2049 | for (unsigned i = 0, e = succs.size(); i != e; ++i) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2050 | IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2051 | |
| 2052 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2053 | MVT::Other, getControlRoot(), |
| 2054 | getValue(I.getAddress()))); |
| 2055 | } |
| 2056 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2057 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2058 | // -0.0 - X --> fneg |
| 2059 | const Type *Ty = I.getType(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2060 | if (Ty->isVectorTy()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2061 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2062 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2063 | const Type *ElTy = DestTy->getElementType(); |
| 2064 | unsigned VL = DestTy->getNumElements(); |
| 2065 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
| 2066 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
| 2067 | if (CV == CNZ) { |
| 2068 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2069 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2070 | Op2.getValueType(), Op2)); |
| 2071 | return; |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 2077 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
| 2078 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2079 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2080 | Op2.getValueType(), Op2)); |
| 2081 | return; |
| 2082 | } |
| 2083 | |
| 2084 | visitBinary(I, ISD::FSUB); |
| 2085 | } |
| 2086 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2087 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2088 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2089 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2090 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2091 | Op1.getValueType(), Op1, Op2)); |
| 2092 | } |
| 2093 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2094 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2095 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2096 | SDValue Op2 = getValue(I.getOperand(1)); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2097 | if (!I.getType()->isVectorTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2098 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2099 | // If the operand is smaller than the shift count type, promote it. |
| 2100 | EVT PTy = TLI.getPointerTy(); |
| 2101 | EVT STy = TLI.getShiftAmountTy(); |
| 2102 | if (STy.bitsGT(Op2.getValueType())) |
| 2103 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2104 | TLI.getShiftAmountTy(), Op2); |
| 2105 | // If the operand is larger than the shift count type but the shift |
| 2106 | // count type has enough bits to represent any shift value, truncate |
| 2107 | // it now. This is a common case and it exposes the truncate to |
| 2108 | // optimization early. |
| 2109 | else if (STy.getSizeInBits() >= |
| 2110 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2111 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2112 | TLI.getShiftAmountTy(), Op2); |
| 2113 | // Otherwise we'll need to temporarily settle for some other |
| 2114 | // convenient type; type legalization will make adjustments as |
| 2115 | // needed. |
| 2116 | else if (PTy.bitsLT(Op2.getValueType())) |
| 2117 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2118 | TLI.getPointerTy(), Op2); |
| 2119 | else if (PTy.bitsGT(Op2.getValueType())) |
| 2120 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2121 | TLI.getPointerTy(), Op2); |
| 2122 | } |
| 2123 | |
| 2124 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2125 | Op1.getValueType(), Op1, Op2)); |
| 2126 | } |
| 2127 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2128 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2129 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2130 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2131 | predicate = IC->getPredicate(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2132 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2133 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2134 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2135 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2136 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
| 2137 | |
| 2138 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2139 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
| 2140 | } |
| 2141 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2142 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2143 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2144 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2145 | predicate = FC->getPredicate(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2146 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2147 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2148 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2149 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2150 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
| 2151 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2152 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
| 2153 | } |
| 2154 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2155 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2156 | SmallVector<EVT, 4> ValueVTs; |
| 2157 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2158 | unsigned NumValues = ValueVTs.size(); |
| 2159 | if (NumValues == 0) return; |
| 2160 | |
| 2161 | SmallVector<SDValue, 4> Values(NumValues); |
| 2162 | SDValue Cond = getValue(I.getOperand(0)); |
| 2163 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2164 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2165 | |
| 2166 | for (unsigned i = 0; i != NumValues; ++i) |
| 2167 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2168 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
| 2169 | Cond, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2170 | SDValue(TrueVal.getNode(), |
| 2171 | TrueVal.getResNo() + i), |
| 2172 | SDValue(FalseVal.getNode(), |
| 2173 | FalseVal.getResNo() + i)); |
| 2174 | |
| 2175 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2176 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2177 | &Values[0], NumValues)); |
| 2178 | } |
| 2179 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2180 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2181 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2182 | SDValue N = getValue(I.getOperand(0)); |
| 2183 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2184 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
| 2185 | } |
| 2186 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2187 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2188 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2189 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2190 | SDValue N = getValue(I.getOperand(0)); |
| 2191 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2192 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
| 2193 | } |
| 2194 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2195 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2196 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2197 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2198 | SDValue N = getValue(I.getOperand(0)); |
| 2199 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2200 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
| 2201 | } |
| 2202 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2203 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2204 | // FPTrunc is never a no-op cast, no need to check |
| 2205 | SDValue N = getValue(I.getOperand(0)); |
| 2206 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2207 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
| 2208 | DestVT, N, DAG.getIntPtrConstant(0))); |
| 2209 | } |
| 2210 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2211 | void SelectionDAGBuilder::visitFPExt(const User &I){ |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2212 | // FPTrunc is never a no-op cast, no need to check |
| 2213 | SDValue N = getValue(I.getOperand(0)); |
| 2214 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2215 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
| 2216 | } |
| 2217 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2218 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2219 | // FPToUI is never a no-op cast, no need to check |
| 2220 | SDValue N = getValue(I.getOperand(0)); |
| 2221 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2222 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
| 2223 | } |
| 2224 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2225 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2226 | // FPToSI is never a no-op cast, no need to check |
| 2227 | SDValue N = getValue(I.getOperand(0)); |
| 2228 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2229 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
| 2230 | } |
| 2231 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2232 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2233 | // UIToFP is never a no-op cast, no need to check |
| 2234 | SDValue N = getValue(I.getOperand(0)); |
| 2235 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2236 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
| 2237 | } |
| 2238 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2239 | void SelectionDAGBuilder::visitSIToFP(const User &I){ |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2240 | // SIToFP is never a no-op cast, no need to check |
| 2241 | SDValue N = getValue(I.getOperand(0)); |
| 2242 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2243 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
| 2244 | } |
| 2245 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2246 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2247 | // What to do depends on the size of the integer and the size of the pointer. |
| 2248 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2249 | SDValue N = getValue(I.getOperand(0)); |
| 2250 | EVT SrcVT = N.getValueType(); |
| 2251 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2252 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
| 2253 | } |
| 2254 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2255 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2256 | // What to do depends on the size of the integer and the size of the pointer. |
| 2257 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2258 | SDValue N = getValue(I.getOperand(0)); |
| 2259 | EVT SrcVT = N.getValueType(); |
| 2260 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2261 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
| 2262 | } |
| 2263 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2264 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2265 | SDValue N = getValue(I.getOperand(0)); |
| 2266 | EVT DestVT = TLI.getValueType(I.getType()); |
| 2267 | |
| 2268 | // BitCast assures us that source and destination are the same size so this is |
| 2269 | // either a BIT_CONVERT or a no-op. |
| 2270 | if (DestVT != N.getValueType()) |
| 2271 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2272 | DestVT, N)); // convert types. |
| 2273 | else |
| 2274 | setValue(&I, N); // noop cast. |
| 2275 | } |
| 2276 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2277 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2278 | SDValue InVec = getValue(I.getOperand(0)); |
| 2279 | SDValue InVal = getValue(I.getOperand(1)); |
| 2280 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
| 2281 | TLI.getPointerTy(), |
| 2282 | getValue(I.getOperand(2))); |
| 2283 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2284 | TLI.getValueType(I.getType()), |
| 2285 | InVec, InVal, InIdx)); |
| 2286 | } |
| 2287 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2288 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2289 | SDValue InVec = getValue(I.getOperand(0)); |
| 2290 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
| 2291 | TLI.getPointerTy(), |
| 2292 | getValue(I.getOperand(1))); |
| 2293 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2294 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2295 | } |
| 2296 | |
| 2297 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2298 | // from SIndx and increasing to the element length (undefs are allowed). |
| 2299 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2300 | unsigned MaskNumElts = Mask.size(); |
| 2301 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2302 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
| 2303 | return false; |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2307 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2308 | SmallVector<int, 8> Mask; |
| 2309 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2310 | SDValue Src2 = getValue(I.getOperand(1)); |
| 2311 | |
| 2312 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2313 | // representing undef values. |
| 2314 | SmallVector<Constant*, 8> MaskElts; |
| 2315 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
| 2316 | unsigned MaskNumElts = MaskElts.size(); |
| 2317 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 2318 | if (isa<UndefValue>(MaskElts[i])) |
| 2319 | Mask.push_back(-1); |
| 2320 | else |
| 2321 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2322 | } |
| 2323 | |
| 2324 | EVT VT = TLI.getValueType(I.getType()); |
| 2325 | EVT SrcVT = Src1.getValueType(); |
| 2326 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
| 2327 | |
| 2328 | if (SrcNumElts == MaskNumElts) { |
| 2329 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2330 | &Mask[0])); |
| 2331 | return; |
| 2332 | } |
| 2333 | |
| 2334 | // Normalize the shuffle vector since mask and vector length don't match. |
| 2335 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2336 | // Mask is longer than the source vectors and is a multiple of the source |
| 2337 | // vectors. We can use concatenate vector to make the mask and vectors |
| 2338 | // lengths match. |
| 2339 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2340 | // The shuffle is concatenating two vectors together. |
| 2341 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2342 | VT, Src1, Src2)); |
| 2343 | return; |
| 2344 | } |
| 2345 | |
| 2346 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2347 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
| 2348 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2349 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
| 2350 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
| 2351 | |
| 2352 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2353 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
| 2354 | MOps1[0] = Src1; |
| 2355 | MOps2[0] = Src2; |
| 2356 | |
| 2357 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2358 | getCurDebugLoc(), VT, |
| 2359 | &MOps1[0], NumConcat); |
| 2360 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2361 | getCurDebugLoc(), VT, |
| 2362 | &MOps2[0], NumConcat); |
| 2363 | |
| 2364 | // Readjust mask for new input vector length. |
| 2365 | SmallVector<int, 8> MappedOps; |
| 2366 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 2367 | int Idx = Mask[i]; |
| 2368 | if (Idx < (int)SrcNumElts) |
| 2369 | MappedOps.push_back(Idx); |
| 2370 | else |
| 2371 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
| 2372 | } |
| 2373 | |
| 2374 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2375 | &MappedOps[0])); |
| 2376 | return; |
| 2377 | } |
| 2378 | |
| 2379 | if (SrcNumElts > MaskNumElts) { |
| 2380 | // Analyze the access pattern of the vector to see if we can extract |
| 2381 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2382 | // the range of elements the mask access on both vectors. |
| 2383 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2384 | int MaxRange[2] = {-1, -1}; |
| 2385 | |
| 2386 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 2387 | int Idx = Mask[i]; |
| 2388 | int Input = 0; |
| 2389 | if (Idx < 0) |
| 2390 | continue; |
| 2391 | |
| 2392 | if (Idx >= (int)SrcNumElts) { |
| 2393 | Input = 1; |
| 2394 | Idx -= SrcNumElts; |
| 2395 | } |
| 2396 | if (Idx > MaxRange[Input]) |
| 2397 | MaxRange[Input] = Idx; |
| 2398 | if (Idx < MinRange[Input]) |
| 2399 | MinRange[Input] = Idx; |
| 2400 | } |
| 2401 | |
| 2402 | // Check if the access is smaller than the vector size and can we find |
| 2403 | // a reasonable extract index. |
| 2404 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2405 | // Extract. |
| 2406 | int StartIdx[2]; // StartIdx to extract from |
| 2407 | for (int Input=0; Input < 2; ++Input) { |
| 2408 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
| 2409 | RangeUse[Input] = 0; // Unused |
| 2410 | StartIdx[Input] = 0; |
| 2411 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
| 2412 | // Fits within range but we should see if we can find a good |
| 2413 | // start index that is a multiple of the mask length. |
| 2414 | if (MaxRange[Input] < (int)MaskNumElts) { |
| 2415 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2416 | StartIdx[Input] = 0; |
| 2417 | } else { |
| 2418 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
| 2419 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
| 2420 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
| 2421 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
| 2427 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
| 2428 | return; |
| 2429 | } |
| 2430 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2431 | // Extract appropriate subvector and generate a vector shuffle |
| 2432 | for (int Input=0; Input < 2; ++Input) { |
| 2433 | SDValue &Src = Input == 0 ? Src1 : Src2; |
| 2434 | if (RangeUse[Input] == 0) |
| 2435 | Src = DAG.getUNDEF(VT); |
| 2436 | else |
| 2437 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
| 2438 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
| 2439 | } |
| 2440 | |
| 2441 | // Calculate new mask. |
| 2442 | SmallVector<int, 8> MappedOps; |
| 2443 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 2444 | int Idx = Mask[i]; |
| 2445 | if (Idx < 0) |
| 2446 | MappedOps.push_back(Idx); |
| 2447 | else if (Idx < (int)SrcNumElts) |
| 2448 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2449 | else |
| 2450 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
| 2451 | } |
| 2452 | |
| 2453 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2454 | &MappedOps[0])); |
| 2455 | return; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2460 | // replacing the shuffle with extract and build vector. |
| 2461 | // to insert and build vector. |
| 2462 | EVT EltVT = VT.getVectorElementType(); |
| 2463 | EVT PtrVT = TLI.getPointerTy(); |
| 2464 | SmallVector<SDValue,8> Ops; |
| 2465 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 2466 | if (Mask[i] < 0) { |
| 2467 | Ops.push_back(DAG.getUNDEF(EltVT)); |
| 2468 | } else { |
| 2469 | int Idx = Mask[i]; |
| 2470 | SDValue Res; |
| 2471 | |
| 2472 | if (Idx < (int)SrcNumElts) |
| 2473 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2474 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
| 2475 | else |
| 2476 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2477 | EltVT, Src2, |
| 2478 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2479 | |
| 2480 | Ops.push_back(Res); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2485 | VT, &Ops[0], Ops.size())); |
| 2486 | } |
| 2487 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2488 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2489 | const Value *Op0 = I.getOperand(0); |
| 2490 | const Value *Op1 = I.getOperand(1); |
| 2491 | const Type *AggTy = I.getType(); |
| 2492 | const Type *ValTy = Op1->getType(); |
| 2493 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2494 | bool FromUndef = isa<UndefValue>(Op1); |
| 2495 | |
| 2496 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2497 | I.idx_begin(), I.idx_end()); |
| 2498 | |
| 2499 | SmallVector<EVT, 4> AggValueVTs; |
| 2500 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 2501 | SmallVector<EVT, 4> ValValueVTs; |
| 2502 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2503 | |
| 2504 | unsigned NumAggValues = AggValueVTs.size(); |
| 2505 | unsigned NumValValues = ValValueVTs.size(); |
| 2506 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2507 | |
| 2508 | SDValue Agg = getValue(Op0); |
| 2509 | SDValue Val = getValue(Op1); |
| 2510 | unsigned i = 0; |
| 2511 | // Copy the beginning value(s) from the original aggregate. |
| 2512 | for (; i != LinearIndex; ++i) |
| 2513 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 2514 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2515 | // Copy values from the inserted value(s). |
| 2516 | for (; i != LinearIndex + NumValValues; ++i) |
| 2517 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 2518 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2519 | // Copy remaining value(s) from the original aggregate. |
| 2520 | for (; i != NumAggValues; ++i) |
| 2521 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 2522 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2523 | |
| 2524 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2525 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2526 | &Values[0], NumAggValues)); |
| 2527 | } |
| 2528 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2529 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2530 | const Value *Op0 = I.getOperand(0); |
| 2531 | const Type *AggTy = Op0->getType(); |
| 2532 | const Type *ValTy = I.getType(); |
| 2533 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2534 | |
| 2535 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2536 | I.idx_begin(), I.idx_end()); |
| 2537 | |
| 2538 | SmallVector<EVT, 4> ValValueVTs; |
| 2539 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2540 | |
| 2541 | unsigned NumValValues = ValValueVTs.size(); |
| 2542 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2543 | |
| 2544 | SDValue Agg = getValue(Op0); |
| 2545 | // Copy out the selected value(s). |
| 2546 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2547 | Values[i - LinearIndex] = |
| 2548 | OutOfUndef ? |
| 2549 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
| 2550 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2551 | |
| 2552 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2553 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2554 | &Values[0], NumValValues)); |
| 2555 | } |
| 2556 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2557 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2558 | SDValue N = getValue(I.getOperand(0)); |
| 2559 | const Type *Ty = I.getOperand(0)->getType(); |
| 2560 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2561 | for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2562 | OI != E; ++OI) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2563 | const Value *Idx = *OI; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2564 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2565 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2566 | if (Field) { |
| 2567 | // N = N + Offset |
| 2568 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
| 2569 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
| 2570 | DAG.getIntPtrConstant(Offset)); |
| 2571 | } |
| 2572 | |
| 2573 | Ty = StTy->getElementType(Field); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2574 | } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) { |
| 2575 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2576 | |
| 2577 | // Offset canonically 0 for unions, but type changes |
| 2578 | Ty = UnTy->getElementType(Field); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2579 | } else { |
| 2580 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2581 | |
| 2582 | // If this is a constant subscript, handle it quickly. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2583 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2584 | if (CI->getZExtValue() == 0) continue; |
| 2585 | uint64_t Offs = |
| 2586 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
| 2587 | SDValue OffsVal; |
| 2588 | EVT PTy = TLI.getPointerTy(); |
| 2589 | unsigned PtrBits = PTy.getSizeInBits(); |
| 2590 | if (PtrBits < 64) |
| 2591 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2592 | TLI.getPointerTy(), |
| 2593 | DAG.getConstant(Offs, MVT::i64)); |
| 2594 | else |
| 2595 | OffsVal = DAG.getIntPtrConstant(Offs); |
| 2596 | |
| 2597 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
| 2598 | OffsVal); |
| 2599 | continue; |
| 2600 | } |
| 2601 | |
| 2602 | // N = N + Idx * ElementSize; |
| 2603 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2604 | TD->getTypeAllocSize(Ty)); |
| 2605 | SDValue IdxN = getValue(Idx); |
| 2606 | |
| 2607 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2608 | // it. |
| 2609 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
| 2610 | |
| 2611 | // If this is a multiply by a power of two, turn it into a shl |
| 2612 | // immediately. This is a very common case. |
| 2613 | if (ElementSize != 1) { |
| 2614 | if (ElementSize.isPowerOf2()) { |
| 2615 | unsigned Amt = ElementSize.logBase2(); |
| 2616 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
| 2617 | N.getValueType(), IdxN, |
| 2618 | DAG.getConstant(Amt, TLI.getPointerTy())); |
| 2619 | } else { |
| 2620 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
| 2621 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
| 2622 | N.getValueType(), IdxN, Scale); |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2627 | N.getValueType(), N, IdxN); |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | setValue(&I, N); |
| 2632 | } |
| 2633 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2634 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2635 | // If this is a fixed sized alloca in the entry block of the function, |
| 2636 | // allocate it statically on the stack. |
| 2637 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2638 | return; // getValue will auto-populate this. |
| 2639 | |
| 2640 | const Type *Ty = I.getAllocatedType(); |
| 2641 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
| 2642 | unsigned Align = |
| 2643 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2644 | I.getAlignment()); |
| 2645 | |
| 2646 | SDValue AllocSize = getValue(I.getArraySize()); |
| 2647 | |
| 2648 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2649 | AllocSize, |
| 2650 | DAG.getConstant(TySize, AllocSize.getValueType())); |
| 2651 | |
| 2652 | EVT IntPtr = TLI.getPointerTy(); |
| 2653 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
| 2654 | |
| 2655 | // Handle alignment. If the requested alignment is less than or equal to |
| 2656 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2657 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2658 | unsigned StackAlign = TM.getFrameInfo()->getStackAlignment(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2659 | if (Align <= StackAlign) |
| 2660 | Align = 0; |
| 2661 | |
| 2662 | // Round the size of the allocation up to the stack alignment size |
| 2663 | // by add SA-1 to the size. |
| 2664 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2665 | AllocSize.getValueType(), AllocSize, |
| 2666 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2667 | |
| 2668 | // Mask out the low bits for alignment purposes. |
| 2669 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
| 2670 | AllocSize.getValueType(), AllocSize, |
| 2671 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2672 | |
| 2673 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
| 2674 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
| 2675 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
| 2676 | VTs, Ops, 3); |
| 2677 | setValue(&I, DSA); |
| 2678 | DAG.setRoot(DSA.getValue(1)); |
| 2679 | |
| 2680 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2681 | // object. |
| 2682 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
| 2683 | } |
| 2684 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2685 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2686 | const Value *SV = I.getOperand(0); |
| 2687 | SDValue Ptr = getValue(SV); |
| 2688 | |
| 2689 | const Type *Ty = I.getType(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2690 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2691 | bool isVolatile = I.isVolatile(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2692 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2693 | unsigned Alignment = I.getAlignment(); |
| 2694 | |
| 2695 | SmallVector<EVT, 4> ValueVTs; |
| 2696 | SmallVector<uint64_t, 4> Offsets; |
| 2697 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2698 | unsigned NumValues = ValueVTs.size(); |
| 2699 | if (NumValues == 0) |
| 2700 | return; |
| 2701 | |
| 2702 | SDValue Root; |
| 2703 | bool ConstantMemory = false; |
| 2704 | if (I.isVolatile()) |
| 2705 | // Serialize volatile loads with other side effects. |
| 2706 | Root = getRoot(); |
| 2707 | else if (AA->pointsToConstantMemory(SV)) { |
| 2708 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2709 | Root = DAG.getEntryNode(); |
| 2710 | ConstantMemory = true; |
| 2711 | } else { |
| 2712 | // Do not serialize non-volatile loads against each other. |
| 2713 | Root = DAG.getRoot(); |
| 2714 | } |
| 2715 | |
| 2716 | SmallVector<SDValue, 4> Values(NumValues); |
| 2717 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2718 | EVT PtrVT = Ptr.getValueType(); |
| 2719 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2720 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2721 | PtrVT, Ptr, |
| 2722 | DAG.getConstant(Offsets[i], PtrVT)); |
| 2723 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2724 | A, SV, Offsets[i], isVolatile, |
| 2725 | isNonTemporal, Alignment); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2726 | |
| 2727 | Values[i] = L; |
| 2728 | Chains[i] = L.getValue(1); |
| 2729 | } |
| 2730 | |
| 2731 | if (!ConstantMemory) { |
| 2732 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2733 | MVT::Other, &Chains[0], NumValues); |
| 2734 | if (isVolatile) |
| 2735 | DAG.setRoot(Chain); |
| 2736 | else |
| 2737 | PendingLoads.push_back(Chain); |
| 2738 | } |
| 2739 | |
| 2740 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2741 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2742 | &Values[0], NumValues)); |
| 2743 | } |
| 2744 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2745 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
| 2746 | const Value *SrcV = I.getOperand(0); |
| 2747 | const Value *PtrV = I.getOperand(1); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2748 | |
| 2749 | SmallVector<EVT, 4> ValueVTs; |
| 2750 | SmallVector<uint64_t, 4> Offsets; |
| 2751 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2752 | unsigned NumValues = ValueVTs.size(); |
| 2753 | if (NumValues == 0) |
| 2754 | return; |
| 2755 | |
| 2756 | // Get the lowered operands. Note that we do this after |
| 2757 | // checking if NumResults is zero, because with zero results |
| 2758 | // the operands won't have values in the map. |
| 2759 | SDValue Src = getValue(SrcV); |
| 2760 | SDValue Ptr = getValue(PtrV); |
| 2761 | |
| 2762 | SDValue Root = getRoot(); |
| 2763 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2764 | EVT PtrVT = Ptr.getValueType(); |
| 2765 | bool isVolatile = I.isVolatile(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2766 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2767 | unsigned Alignment = I.getAlignment(); |
| 2768 | |
| 2769 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2770 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2771 | DAG.getConstant(Offsets[i], PtrVT)); |
| 2772 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
| 2773 | SDValue(Src.getNode(), Src.getResNo() + i), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2774 | Add, PtrV, Offsets[i], isVolatile, |
| 2775 | isNonTemporal, Alignment); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2779 | MVT::Other, &Chains[0], NumValues)); |
| 2780 | } |
| 2781 | |
| 2782 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2783 | /// node. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2784 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2785 | unsigned Intrinsic) { |
| 2786 | bool HasChain = !I.doesNotAccessMemory(); |
| 2787 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2788 | |
| 2789 | // Build the operand list. |
| 2790 | SmallVector<SDValue, 8> Ops; |
| 2791 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2792 | if (OnlyLoad) { |
| 2793 | // We don't need to serialize loads against other loads. |
| 2794 | Ops.push_back(DAG.getRoot()); |
| 2795 | } else { |
| 2796 | Ops.push_back(getRoot()); |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | // Info is set by getTgtMemInstrinsic |
| 2801 | TargetLowering::IntrinsicInfo Info; |
| 2802 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2803 | |
| 2804 | // Add the intrinsic ID as an integer operand if it's not a target intrinsic. |
| 2805 | if (!IsTgtIntrinsic) |
| 2806 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 2807 | |
| 2808 | // Add all operands of the call to the operand list. |
| 2809 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2810 | SDValue Op = getValue(I.getOperand(i)); |
| 2811 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2812 | "Intrinsic uses a non-legal type?"); |
| 2813 | Ops.push_back(Op); |
| 2814 | } |
| 2815 | |
| 2816 | SmallVector<EVT, 4> ValueVTs; |
| 2817 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2818 | #ifndef NDEBUG |
| 2819 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2820 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2821 | "Intrinsic uses a non-legal type?"); |
| 2822 | } |
| 2823 | #endif // NDEBUG |
| 2824 | |
| 2825 | if (HasChain) |
| 2826 | ValueVTs.push_back(MVT::Other); |
| 2827 | |
| 2828 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
| 2829 | |
| 2830 | // Create the node. |
| 2831 | SDValue Result; |
| 2832 | if (IsTgtIntrinsic) { |
| 2833 | // This is target intrinsic that touches memory |
| 2834 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
| 2835 | VTs, &Ops[0], Ops.size(), |
| 2836 | Info.memVT, Info.ptrVal, Info.offset, |
| 2837 | Info.align, Info.vol, |
| 2838 | Info.readMem, Info.writeMem); |
| 2839 | } else if (!HasChain) { |
| 2840 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
| 2841 | VTs, &Ops[0], Ops.size()); |
| 2842 | } else if (!I.getType()->isVoidTy()) { |
| 2843 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
| 2844 | VTs, &Ops[0], Ops.size()); |
| 2845 | } else { |
| 2846 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
| 2847 | VTs, &Ops[0], Ops.size()); |
| 2848 | } |
| 2849 | |
| 2850 | if (HasChain) { |
| 2851 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2852 | if (OnlyLoad) |
| 2853 | PendingLoads.push_back(Chain); |
| 2854 | else |
| 2855 | DAG.setRoot(Chain); |
| 2856 | } |
| 2857 | |
| 2858 | if (!I.getType()->isVoidTy()) { |
| 2859 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
| 2860 | EVT VT = TLI.getValueType(PTy); |
| 2861 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
| 2862 | } |
| 2863 | |
| 2864 | setValue(&I, Result); |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2869 | /// number with exponent of 1: |
| 2870 | /// |
| 2871 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2872 | /// |
| 2873 | /// where Op is the hexidecimal representation of floating point value. |
| 2874 | static SDValue |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2875 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2876 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2877 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2878 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 2879 | DAG.getConstant(0x3f800000, MVT::i32)); |
| 2880 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
| 2881 | } |
| 2882 | |
| 2883 | /// GetExponent - Get the exponent: |
| 2884 | /// |
| 2885 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
| 2886 | /// |
| 2887 | /// where Op is the hexidecimal representation of floating point value. |
| 2888 | static SDValue |
| 2889 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2890 | DebugLoc dl) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2891 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2892 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 2893 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
| 2894 | DAG.getConstant(23, TLI.getPointerTy())); |
| 2895 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 2896 | DAG.getConstant(127, MVT::i32)); |
| 2897 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
| 2898 | } |
| 2899 | |
| 2900 | /// getF32Constant - Get 32-bit floating point constant. |
| 2901 | static SDValue |
| 2902 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
| 2903 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
| 2904 | } |
| 2905 | |
| 2906 | /// Inlined utility function to implement binary input atomic intrinsics for |
| 2907 | /// visitIntrinsicCall: I is a call instruction |
| 2908 | /// Op is the associated NodeType for I |
| 2909 | const char * |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2910 | SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I, |
| 2911 | ISD::NodeType Op) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2912 | SDValue Root = getRoot(); |
| 2913 | SDValue L = |
| 2914 | DAG.getAtomic(Op, getCurDebugLoc(), |
| 2915 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 2916 | Root, |
| 2917 | getValue(I.getOperand(1)), |
| 2918 | getValue(I.getOperand(2)), |
| 2919 | I.getOperand(1)); |
| 2920 | setValue(&I, L); |
| 2921 | DAG.setRoot(L.getValue(1)); |
| 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
| 2926 | const char * |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2927 | SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2928 | SDValue Op1 = getValue(I.getOperand(1)); |
| 2929 | SDValue Op2 = getValue(I.getOperand(2)); |
| 2930 | |
| 2931 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
| 2932 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2937 | /// limited-precision mode. |
| 2938 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 2939 | SelectionDAGBuilder::visitExp(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 2940 | SDValue result; |
| 2941 | DebugLoc dl = getCurDebugLoc(); |
| 2942 | |
| 2943 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 2944 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2945 | SDValue Op = getValue(I.getOperand(1)); |
| 2946 | |
| 2947 | // Put the exponent in the right bit position for later addition to the |
| 2948 | // final result: |
| 2949 | // |
| 2950 | // #define LOG2OFe 1.4426950f |
| 2951 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
| 2952 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
| 2953 | getF32Constant(DAG, 0x3fb8aa3b)); |
| 2954 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
| 2955 | |
| 2956 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
| 2957 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 2958 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
| 2959 | |
| 2960 | // IntegerPartOfX <<= 23; |
| 2961 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
| 2962 | DAG.getConstant(23, TLI.getPointerTy())); |
| 2963 | |
| 2964 | if (LimitFloatPrecision <= 6) { |
| 2965 | // For floating-point precision of 6: |
| 2966 | // |
| 2967 | // TwoToFractionalPartOfX = |
| 2968 | // 0.997535578f + |
| 2969 | // (0.735607626f + 0.252464424f * x) * x; |
| 2970 | // |
| 2971 | // error 0.0144103317, which is 6 bits |
| 2972 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 2973 | getF32Constant(DAG, 0x3e814304)); |
| 2974 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 2975 | getF32Constant(DAG, 0x3f3c50c8)); |
| 2976 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2977 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 2978 | getF32Constant(DAG, 0x3f7f5e7e)); |
| 2979 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
| 2980 | |
| 2981 | // Add the exponent into the result in integer domain. |
| 2982 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 2983 | TwoToFracPartOfX, IntegerPartOfX); |
| 2984 | |
| 2985 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
| 2986 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 2987 | // For floating-point precision of 12: |
| 2988 | // |
| 2989 | // TwoToFractionalPartOfX = |
| 2990 | // 0.999892986f + |
| 2991 | // (0.696457318f + |
| 2992 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 2993 | // |
| 2994 | // 0.000107046256 error, which is 13 to 14 bits |
| 2995 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 2996 | getF32Constant(DAG, 0x3da235e3)); |
| 2997 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 2998 | getF32Constant(DAG, 0x3e65b8f3)); |
| 2999 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3000 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3001 | getF32Constant(DAG, 0x3f324b07)); |
| 3002 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3003 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3004 | getF32Constant(DAG, 0x3f7ff8fd)); |
| 3005 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
| 3006 | |
| 3007 | // Add the exponent into the result in integer domain. |
| 3008 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3009 | TwoToFracPartOfX, IntegerPartOfX); |
| 3010 | |
| 3011 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
| 3012 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3013 | // For floating-point precision of 18: |
| 3014 | // |
| 3015 | // TwoToFractionalPartOfX = |
| 3016 | // 0.999999982f + |
| 3017 | // (0.693148872f + |
| 3018 | // (0.240227044f + |
| 3019 | // (0.554906021e-1f + |
| 3020 | // (0.961591928e-2f + |
| 3021 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3022 | // |
| 3023 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 3024 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3025 | getF32Constant(DAG, 0x3924b03e)); |
| 3026 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3027 | getF32Constant(DAG, 0x3ab24b87)); |
| 3028 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3029 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3030 | getF32Constant(DAG, 0x3c1d8c17)); |
| 3031 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3032 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3033 | getF32Constant(DAG, 0x3d634a1d)); |
| 3034 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3035 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
| 3036 | getF32Constant(DAG, 0x3e75fe14)); |
| 3037 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3038 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
| 3039 | getF32Constant(DAG, 0x3f317234)); |
| 3040 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3041 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3042 | getF32Constant(DAG, 0x3f800000)); |
| 3043 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3044 | MVT::i32, t13); |
| 3045 | |
| 3046 | // Add the exponent into the result in integer domain. |
| 3047 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3048 | TwoToFracPartOfX, IntegerPartOfX); |
| 3049 | |
| 3050 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
| 3051 | } |
| 3052 | } else { |
| 3053 | // No special expansion. |
| 3054 | result = DAG.getNode(ISD::FEXP, dl, |
| 3055 | getValue(I.getOperand(1)).getValueType(), |
| 3056 | getValue(I.getOperand(1))); |
| 3057 | } |
| 3058 | |
| 3059 | setValue(&I, result); |
| 3060 | } |
| 3061 | |
| 3062 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3063 | /// limited-precision mode. |
| 3064 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3065 | SelectionDAGBuilder::visitLog(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3066 | SDValue result; |
| 3067 | DebugLoc dl = getCurDebugLoc(); |
| 3068 | |
| 3069 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3070 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3071 | SDValue Op = getValue(I.getOperand(1)); |
| 3072 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
| 3073 | |
| 3074 | // Scale the exponent by log(2) [0.69314718f]. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3075 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3076 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
| 3077 | getF32Constant(DAG, 0x3f317218)); |
| 3078 | |
| 3079 | // Get the significand and build it into a floating-point number with |
| 3080 | // exponent of 1. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3081 | SDValue X = GetSignificand(DAG, Op1, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3082 | |
| 3083 | if (LimitFloatPrecision <= 6) { |
| 3084 | // For floating-point precision of 6: |
| 3085 | // |
| 3086 | // LogofMantissa = |
| 3087 | // -1.1609546f + |
| 3088 | // (1.4034025f - 0.23903021f * x) * x; |
| 3089 | // |
| 3090 | // error 0.0034276066, which is better than 8 bits |
| 3091 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3092 | getF32Constant(DAG, 0xbe74c456)); |
| 3093 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3094 | getF32Constant(DAG, 0x3fb3a2b1)); |
| 3095 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3096 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3097 | getF32Constant(DAG, 0x3f949a29)); |
| 3098 | |
| 3099 | result = DAG.getNode(ISD::FADD, dl, |
| 3100 | MVT::f32, LogOfExponent, LogOfMantissa); |
| 3101 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3102 | // For floating-point precision of 12: |
| 3103 | // |
| 3104 | // LogOfMantissa = |
| 3105 | // -1.7417939f + |
| 3106 | // (2.8212026f + |
| 3107 | // (-1.4699568f + |
| 3108 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3109 | // |
| 3110 | // error 0.000061011436, which is 14 bits |
| 3111 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3112 | getF32Constant(DAG, 0xbd67b6d6)); |
| 3113 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3114 | getF32Constant(DAG, 0x3ee4f4b8)); |
| 3115 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3116 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3117 | getF32Constant(DAG, 0x3fbc278b)); |
| 3118 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3119 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3120 | getF32Constant(DAG, 0x40348e95)); |
| 3121 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3122 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3123 | getF32Constant(DAG, 0x3fdef31a)); |
| 3124 | |
| 3125 | result = DAG.getNode(ISD::FADD, dl, |
| 3126 | MVT::f32, LogOfExponent, LogOfMantissa); |
| 3127 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3128 | // For floating-point precision of 18: |
| 3129 | // |
| 3130 | // LogOfMantissa = |
| 3131 | // -2.1072184f + |
| 3132 | // (4.2372794f + |
| 3133 | // (-3.7029485f + |
| 3134 | // (2.2781945f + |
| 3135 | // (-0.87823314f + |
| 3136 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3137 | // |
| 3138 | // error 0.0000023660568, which is better than 18 bits |
| 3139 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3140 | getF32Constant(DAG, 0xbc91e5ac)); |
| 3141 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3142 | getF32Constant(DAG, 0x3e4350aa)); |
| 3143 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3144 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3145 | getF32Constant(DAG, 0x3f60d3e3)); |
| 3146 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3147 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3148 | getF32Constant(DAG, 0x4011cdf0)); |
| 3149 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3150 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3151 | getF32Constant(DAG, 0x406cfd1c)); |
| 3152 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3153 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
| 3154 | getF32Constant(DAG, 0x408797cb)); |
| 3155 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3156 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3157 | getF32Constant(DAG, 0x4006dcab)); |
| 3158 | |
| 3159 | result = DAG.getNode(ISD::FADD, dl, |
| 3160 | MVT::f32, LogOfExponent, LogOfMantissa); |
| 3161 | } |
| 3162 | } else { |
| 3163 | // No special expansion. |
| 3164 | result = DAG.getNode(ISD::FLOG, dl, |
| 3165 | getValue(I.getOperand(1)).getValueType(), |
| 3166 | getValue(I.getOperand(1))); |
| 3167 | } |
| 3168 | |
| 3169 | setValue(&I, result); |
| 3170 | } |
| 3171 | |
| 3172 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3173 | /// limited-precision mode. |
| 3174 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3175 | SelectionDAGBuilder::visitLog2(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3176 | SDValue result; |
| 3177 | DebugLoc dl = getCurDebugLoc(); |
| 3178 | |
| 3179 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3180 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3181 | SDValue Op = getValue(I.getOperand(1)); |
| 3182 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
| 3183 | |
| 3184 | // Get the exponent. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3185 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3186 | |
| 3187 | // Get the significand and build it into a floating-point number with |
| 3188 | // exponent of 1. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3189 | SDValue X = GetSignificand(DAG, Op1, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3190 | |
| 3191 | // Different possible minimax approximations of significand in |
| 3192 | // floating-point for various degrees of accuracy over [1,2]. |
| 3193 | if (LimitFloatPrecision <= 6) { |
| 3194 | // For floating-point precision of 6: |
| 3195 | // |
| 3196 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3197 | // |
| 3198 | // error 0.0049451742, which is more than 7 bits |
| 3199 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3200 | getF32Constant(DAG, 0xbeb08fe0)); |
| 3201 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3202 | getF32Constant(DAG, 0x40019463)); |
| 3203 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3204 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3205 | getF32Constant(DAG, 0x3fd6633d)); |
| 3206 | |
| 3207 | result = DAG.getNode(ISD::FADD, dl, |
| 3208 | MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3209 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3210 | // For floating-point precision of 12: |
| 3211 | // |
| 3212 | // Log2ofMantissa = |
| 3213 | // -2.51285454f + |
| 3214 | // (4.07009056f + |
| 3215 | // (-2.12067489f + |
| 3216 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
| 3217 | // |
| 3218 | // error 0.0000876136000, which is better than 13 bits |
| 3219 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3220 | getF32Constant(DAG, 0xbda7262e)); |
| 3221 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3222 | getF32Constant(DAG, 0x3f25280b)); |
| 3223 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3224 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3225 | getF32Constant(DAG, 0x4007b923)); |
| 3226 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3227 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3228 | getF32Constant(DAG, 0x40823e2f)); |
| 3229 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3230 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3231 | getF32Constant(DAG, 0x4020d29c)); |
| 3232 | |
| 3233 | result = DAG.getNode(ISD::FADD, dl, |
| 3234 | MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3235 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3236 | // For floating-point precision of 18: |
| 3237 | // |
| 3238 | // Log2ofMantissa = |
| 3239 | // -3.0400495f + |
| 3240 | // (6.1129976f + |
| 3241 | // (-5.3420409f + |
| 3242 | // (3.2865683f + |
| 3243 | // (-1.2669343f + |
| 3244 | // (0.27515199f - |
| 3245 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3246 | // |
| 3247 | // error 0.0000018516, which is better than 18 bits |
| 3248 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3249 | getF32Constant(DAG, 0xbcd2769e)); |
| 3250 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3251 | getF32Constant(DAG, 0x3e8ce0b9)); |
| 3252 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3253 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3254 | getF32Constant(DAG, 0x3fa22ae7)); |
| 3255 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3256 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3257 | getF32Constant(DAG, 0x40525723)); |
| 3258 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3259 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3260 | getF32Constant(DAG, 0x40aaf200)); |
| 3261 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3262 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
| 3263 | getF32Constant(DAG, 0x40c39dad)); |
| 3264 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3265 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3266 | getF32Constant(DAG, 0x4042902c)); |
| 3267 | |
| 3268 | result = DAG.getNode(ISD::FADD, dl, |
| 3269 | MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3270 | } |
| 3271 | } else { |
| 3272 | // No special expansion. |
| 3273 | result = DAG.getNode(ISD::FLOG2, dl, |
| 3274 | getValue(I.getOperand(1)).getValueType(), |
| 3275 | getValue(I.getOperand(1))); |
| 3276 | } |
| 3277 | |
| 3278 | setValue(&I, result); |
| 3279 | } |
| 3280 | |
| 3281 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3282 | /// limited-precision mode. |
| 3283 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3284 | SelectionDAGBuilder::visitLog10(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3285 | SDValue result; |
| 3286 | DebugLoc dl = getCurDebugLoc(); |
| 3287 | |
| 3288 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3289 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3290 | SDValue Op = getValue(I.getOperand(1)); |
| 3291 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
| 3292 | |
| 3293 | // Scale the exponent by log10(2) [0.30102999f]. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3294 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3295 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
| 3296 | getF32Constant(DAG, 0x3e9a209a)); |
| 3297 | |
| 3298 | // Get the significand and build it into a floating-point number with |
| 3299 | // exponent of 1. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3300 | SDValue X = GetSignificand(DAG, Op1, dl); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3301 | |
| 3302 | if (LimitFloatPrecision <= 6) { |
| 3303 | // For floating-point precision of 6: |
| 3304 | // |
| 3305 | // Log10ofMantissa = |
| 3306 | // -0.50419619f + |
| 3307 | // (0.60948995f - 0.10380950f * x) * x; |
| 3308 | // |
| 3309 | // error 0.0014886165, which is 6 bits |
| 3310 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3311 | getF32Constant(DAG, 0xbdd49a13)); |
| 3312 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
| 3313 | getF32Constant(DAG, 0x3f1c0789)); |
| 3314 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3315 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3316 | getF32Constant(DAG, 0x3f011300)); |
| 3317 | |
| 3318 | result = DAG.getNode(ISD::FADD, dl, |
| 3319 | MVT::f32, LogOfExponent, Log10ofMantissa); |
| 3320 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3321 | // For floating-point precision of 12: |
| 3322 | // |
| 3323 | // Log10ofMantissa = |
| 3324 | // -0.64831180f + |
| 3325 | // (0.91751397f + |
| 3326 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3327 | // |
| 3328 | // error 0.00019228036, which is better than 12 bits |
| 3329 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3330 | getF32Constant(DAG, 0x3d431f31)); |
| 3331 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
| 3332 | getF32Constant(DAG, 0x3ea21fb2)); |
| 3333 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3334 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3335 | getF32Constant(DAG, 0x3f6ae232)); |
| 3336 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3337 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 3338 | getF32Constant(DAG, 0x3f25f7c3)); |
| 3339 | |
| 3340 | result = DAG.getNode(ISD::FADD, dl, |
| 3341 | MVT::f32, LogOfExponent, Log10ofMantissa); |
| 3342 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3343 | // For floating-point precision of 18: |
| 3344 | // |
| 3345 | // Log10ofMantissa = |
| 3346 | // -0.84299375f + |
| 3347 | // (1.5327582f + |
| 3348 | // (-1.0688956f + |
| 3349 | // (0.49102474f + |
| 3350 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3351 | // |
| 3352 | // error 0.0000037995730, which is better than 18 bits |
| 3353 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3354 | getF32Constant(DAG, 0x3c5d51ce)); |
| 3355 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
| 3356 | getF32Constant(DAG, 0x3e00685a)); |
| 3357 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3358 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3359 | getF32Constant(DAG, 0x3efb6798)); |
| 3360 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3361 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 3362 | getF32Constant(DAG, 0x3f88d192)); |
| 3363 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3364 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3365 | getF32Constant(DAG, 0x3fc4316c)); |
| 3366 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3367 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
| 3368 | getF32Constant(DAG, 0x3f57ce70)); |
| 3369 | |
| 3370 | result = DAG.getNode(ISD::FADD, dl, |
| 3371 | MVT::f32, LogOfExponent, Log10ofMantissa); |
| 3372 | } |
| 3373 | } else { |
| 3374 | // No special expansion. |
| 3375 | result = DAG.getNode(ISD::FLOG10, dl, |
| 3376 | getValue(I.getOperand(1)).getValueType(), |
| 3377 | getValue(I.getOperand(1))); |
| 3378 | } |
| 3379 | |
| 3380 | setValue(&I, result); |
| 3381 | } |
| 3382 | |
| 3383 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3384 | /// limited-precision mode. |
| 3385 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3386 | SelectionDAGBuilder::visitExp2(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3387 | SDValue result; |
| 3388 | DebugLoc dl = getCurDebugLoc(); |
| 3389 | |
| 3390 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 3391 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3392 | SDValue Op = getValue(I.getOperand(1)); |
| 3393 | |
| 3394 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
| 3395 | |
| 3396 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
| 3397 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3398 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
| 3399 | |
| 3400 | // IntegerPartOfX <<= 23; |
| 3401 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
| 3402 | DAG.getConstant(23, TLI.getPointerTy())); |
| 3403 | |
| 3404 | if (LimitFloatPrecision <= 6) { |
| 3405 | // For floating-point precision of 6: |
| 3406 | // |
| 3407 | // TwoToFractionalPartOfX = |
| 3408 | // 0.997535578f + |
| 3409 | // (0.735607626f + 0.252464424f * x) * x; |
| 3410 | // |
| 3411 | // error 0.0144103317, which is 6 bits |
| 3412 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3413 | getF32Constant(DAG, 0x3e814304)); |
| 3414 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3415 | getF32Constant(DAG, 0x3f3c50c8)); |
| 3416 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3417 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3418 | getF32Constant(DAG, 0x3f7f5e7e)); |
| 3419 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
| 3420 | SDValue TwoToFractionalPartOfX = |
| 3421 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
| 3422 | |
| 3423 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3424 | MVT::f32, TwoToFractionalPartOfX); |
| 3425 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3426 | // For floating-point precision of 12: |
| 3427 | // |
| 3428 | // TwoToFractionalPartOfX = |
| 3429 | // 0.999892986f + |
| 3430 | // (0.696457318f + |
| 3431 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3432 | // |
| 3433 | // error 0.000107046256, which is 13 to 14 bits |
| 3434 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3435 | getF32Constant(DAG, 0x3da235e3)); |
| 3436 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3437 | getF32Constant(DAG, 0x3e65b8f3)); |
| 3438 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3439 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3440 | getF32Constant(DAG, 0x3f324b07)); |
| 3441 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3442 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3443 | getF32Constant(DAG, 0x3f7ff8fd)); |
| 3444 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
| 3445 | SDValue TwoToFractionalPartOfX = |
| 3446 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
| 3447 | |
| 3448 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3449 | MVT::f32, TwoToFractionalPartOfX); |
| 3450 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3451 | // For floating-point precision of 18: |
| 3452 | // |
| 3453 | // TwoToFractionalPartOfX = |
| 3454 | // 0.999999982f + |
| 3455 | // (0.693148872f + |
| 3456 | // (0.240227044f + |
| 3457 | // (0.554906021e-1f + |
| 3458 | // (0.961591928e-2f + |
| 3459 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3460 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 3461 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3462 | getF32Constant(DAG, 0x3924b03e)); |
| 3463 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3464 | getF32Constant(DAG, 0x3ab24b87)); |
| 3465 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3466 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3467 | getF32Constant(DAG, 0x3c1d8c17)); |
| 3468 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3469 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3470 | getF32Constant(DAG, 0x3d634a1d)); |
| 3471 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3472 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
| 3473 | getF32Constant(DAG, 0x3e75fe14)); |
| 3474 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3475 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
| 3476 | getF32Constant(DAG, 0x3f317234)); |
| 3477 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3478 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3479 | getF32Constant(DAG, 0x3f800000)); |
| 3480 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
| 3481 | SDValue TwoToFractionalPartOfX = |
| 3482 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
| 3483 | |
| 3484 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3485 | MVT::f32, TwoToFractionalPartOfX); |
| 3486 | } |
| 3487 | } else { |
| 3488 | // No special expansion. |
| 3489 | result = DAG.getNode(ISD::FEXP2, dl, |
| 3490 | getValue(I.getOperand(1)).getValueType(), |
| 3491 | getValue(I.getOperand(1))); |
| 3492 | } |
| 3493 | |
| 3494 | setValue(&I, result); |
| 3495 | } |
| 3496 | |
| 3497 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3498 | /// limited-precision mode with x == 10.0f. |
| 3499 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3500 | SelectionDAGBuilder::visitPow(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3501 | SDValue result; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3502 | const Value *Val = I.getOperand(1); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3503 | DebugLoc dl = getCurDebugLoc(); |
| 3504 | bool IsExp10 = false; |
| 3505 | |
| 3506 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3507 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
| 3508 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3509 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3510 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3511 | APFloat Ten(10.0f); |
| 3512 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3518 | SDValue Op = getValue(I.getOperand(2)); |
| 3519 | |
| 3520 | // Put the exponent in the right bit position for later addition to the |
| 3521 | // final result: |
| 3522 | // |
| 3523 | // #define LOG2OF10 3.3219281f |
| 3524 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
| 3525 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
| 3526 | getF32Constant(DAG, 0x40549a78)); |
| 3527 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
| 3528 | |
| 3529 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
| 3530 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3531 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
| 3532 | |
| 3533 | // IntegerPartOfX <<= 23; |
| 3534 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
| 3535 | DAG.getConstant(23, TLI.getPointerTy())); |
| 3536 | |
| 3537 | if (LimitFloatPrecision <= 6) { |
| 3538 | // For floating-point precision of 6: |
| 3539 | // |
| 3540 | // twoToFractionalPartOfX = |
| 3541 | // 0.997535578f + |
| 3542 | // (0.735607626f + 0.252464424f * x) * x; |
| 3543 | // |
| 3544 | // error 0.0144103317, which is 6 bits |
| 3545 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3546 | getF32Constant(DAG, 0x3e814304)); |
| 3547 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3548 | getF32Constant(DAG, 0x3f3c50c8)); |
| 3549 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3550 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3551 | getF32Constant(DAG, 0x3f7f5e7e)); |
| 3552 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
| 3553 | SDValue TwoToFractionalPartOfX = |
| 3554 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
| 3555 | |
| 3556 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3557 | MVT::f32, TwoToFractionalPartOfX); |
| 3558 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3559 | // For floating-point precision of 12: |
| 3560 | // |
| 3561 | // TwoToFractionalPartOfX = |
| 3562 | // 0.999892986f + |
| 3563 | // (0.696457318f + |
| 3564 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3565 | // |
| 3566 | // error 0.000107046256, which is 13 to 14 bits |
| 3567 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3568 | getF32Constant(DAG, 0x3da235e3)); |
| 3569 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3570 | getF32Constant(DAG, 0x3e65b8f3)); |
| 3571 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3572 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3573 | getF32Constant(DAG, 0x3f324b07)); |
| 3574 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3575 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3576 | getF32Constant(DAG, 0x3f7ff8fd)); |
| 3577 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
| 3578 | SDValue TwoToFractionalPartOfX = |
| 3579 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
| 3580 | |
| 3581 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3582 | MVT::f32, TwoToFractionalPartOfX); |
| 3583 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3584 | // For floating-point precision of 18: |
| 3585 | // |
| 3586 | // TwoToFractionalPartOfX = |
| 3587 | // 0.999999982f + |
| 3588 | // (0.693148872f + |
| 3589 | // (0.240227044f + |
| 3590 | // (0.554906021e-1f + |
| 3591 | // (0.961591928e-2f + |
| 3592 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3593 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 3594 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
| 3595 | getF32Constant(DAG, 0x3924b03e)); |
| 3596 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
| 3597 | getF32Constant(DAG, 0x3ab24b87)); |
| 3598 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3599 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3600 | getF32Constant(DAG, 0x3c1d8c17)); |
| 3601 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3602 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3603 | getF32Constant(DAG, 0x3d634a1d)); |
| 3604 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3605 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
| 3606 | getF32Constant(DAG, 0x3e75fe14)); |
| 3607 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3608 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
| 3609 | getF32Constant(DAG, 0x3f317234)); |
| 3610 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3611 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3612 | getF32Constant(DAG, 0x3f800000)); |
| 3613 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
| 3614 | SDValue TwoToFractionalPartOfX = |
| 3615 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
| 3616 | |
| 3617 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
| 3618 | MVT::f32, TwoToFractionalPartOfX); |
| 3619 | } |
| 3620 | } else { |
| 3621 | // No special expansion. |
| 3622 | result = DAG.getNode(ISD::FPOW, dl, |
| 3623 | getValue(I.getOperand(1)).getValueType(), |
| 3624 | getValue(I.getOperand(1)), |
| 3625 | getValue(I.getOperand(2))); |
| 3626 | } |
| 3627 | |
| 3628 | setValue(&I, result); |
| 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3633 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3634 | SelectionDAG &DAG) { |
| 3635 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3636 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3637 | // optimizing for size, we only want to do this if the expansion would produce |
| 3638 | // a small number of multiplies, otherwise we do the full expansion. |
| 3639 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3640 | // Get the exponent as a positive value. |
| 3641 | unsigned Val = RHSC->getSExtValue(); |
| 3642 | if ((int)Val < 0) Val = -Val; |
| 3643 | |
| 3644 | // powi(x, 0) -> 1.0 |
| 3645 | if (Val == 0) |
| 3646 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3647 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3648 | const Function *F = DAG.getMachineFunction().getFunction(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3649 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3650 | // If optimizing for size, don't insert too many multiplies. This |
| 3651 | // inserts up to 5 multiplies. |
| 3652 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3653 | // We use the simple binary decomposition method to generate the multiply |
| 3654 | // sequence. There are more optimal ways to do this (for example, |
| 3655 | // powi(x,15) generates one more multiply than it should), but this has |
| 3656 | // the benefit of being both really simple and much better than a libcall. |
| 3657 | SDValue Res; // Logically starts equal to 1.0 |
| 3658 | SDValue CurSquare = LHS; |
| 3659 | while (Val) { |
| 3660 | if (Val & 1) { |
| 3661 | if (Res.getNode()) |
| 3662 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3663 | else |
| 3664 | Res = CurSquare; // 1.0*CurSquare. |
| 3665 | } |
| 3666 | |
| 3667 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3668 | CurSquare, CurSquare); |
| 3669 | Val >>= 1; |
| 3670 | } |
| 3671 | |
| 3672 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3673 | if (RHSC->getSExtValue() < 0) |
| 3674 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3675 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3676 | return Res; |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | // Otherwise, expand to a libcall. |
| 3681 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3682 | } |
| 3683 | |
| 3684 | |
| 3685 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3686 | /// we want to emit this as a call to a named external function, return the name |
| 3687 | /// otherwise lower it and return null. |
| 3688 | const char * |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3689 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3690 | DebugLoc dl = getCurDebugLoc(); |
| 3691 | SDValue Res; |
| 3692 | |
| 3693 | switch (Intrinsic) { |
| 3694 | default: |
| 3695 | // By default, turn this into a target intrinsic node. |
| 3696 | visitTargetIntrinsic(I, Intrinsic); |
| 3697 | return 0; |
| 3698 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3699 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3700 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3701 | case Intrinsic::returnaddress: |
| 3702 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
| 3703 | getValue(I.getOperand(1)))); |
| 3704 | return 0; |
| 3705 | case Intrinsic::frameaddress: |
| 3706 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
| 3707 | getValue(I.getOperand(1)))); |
| 3708 | return 0; |
| 3709 | case Intrinsic::setjmp: |
| 3710 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3711 | case Intrinsic::longjmp: |
| 3712 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3713 | case Intrinsic::memcpy: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3714 | // Assert for address < 256 since we support only user defined address |
| 3715 | // spaces. |
| 3716 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3717 | < 256 && |
| 3718 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
| 3719 | < 256 && |
| 3720 | "Unknown address space"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3721 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3722 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3723 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3724 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3725 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
| 3726 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3727 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3728 | return 0; |
| 3729 | } |
| 3730 | case Intrinsic::memset: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3731 | // Assert for address < 256 since we support only user defined address |
| 3732 | // spaces. |
| 3733 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3734 | < 256 && |
| 3735 | "Unknown address space"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3736 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3737 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3738 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3739 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3740 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
| 3741 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3742 | I.getOperand(1), 0)); |
| 3743 | return 0; |
| 3744 | } |
| 3745 | case Intrinsic::memmove: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3746 | // Assert for address < 256 since we support only user defined address |
| 3747 | // spaces. |
| 3748 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3749 | < 256 && |
| 3750 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
| 3751 | < 256 && |
| 3752 | "Unknown address space"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3753 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3754 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3755 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3756 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3757 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3758 | |
| 3759 | // If the source and destination are known to not be aliases, we can |
| 3760 | // lower memmove as memcpy. |
| 3761 | uint64_t Size = -1ULL; |
| 3762 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
| 3763 | Size = C->getZExtValue(); |
| 3764 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3765 | AliasAnalysis::NoAlias) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3766 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
| 3767 | false, I.getOperand(1), 0, I.getOperand(2), 0)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3768 | return 0; |
| 3769 | } |
| 3770 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3771 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3772 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3773 | return 0; |
| 3774 | } |
| 3775 | case Intrinsic::dbg_declare: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3776 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3777 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3778 | return 0; |
| 3779 | |
| 3780 | MDNode *Variable = DI.getVariable(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3781 | // Parameters are handled specially. |
| 3782 | bool isParameter = false; |
| 3783 | ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Variable->getOperand(0)); |
| 3784 | if (CI) { |
| 3785 | unsigned Val = CI->getZExtValue(); |
| 3786 | unsigned Tag = Val & ~LLVMDebugVersionMask; |
| 3787 | if (Tag == dwarf::DW_TAG_arg_variable) |
| 3788 | isParameter = true; |
| 3789 | } |
| 3790 | const Value *Address = DI.getAddress(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3791 | if (!Address) |
| 3792 | return 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3793 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3794 | Address = BCI->getOperand(0); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3795 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 3796 | if (AI) { |
| 3797 | // Don't handle byval arguments or VLAs, for example. |
| 3798 | // Non-byval arguments are handled here (they refer to the stack temporary |
| 3799 | // alloca at this point). |
| 3800 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3801 | FuncInfo.StaticAllocaMap.find(AI); |
| 3802 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3803 | return 0; // VLAs. |
| 3804 | int FI = SI->second; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3805 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3806 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3807 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3808 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
| 3809 | } |
| 3810 | |
| 3811 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 3812 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 3813 | // absolute, but not relative, values are different depending on whether |
| 3814 | // debug info exists. |
| 3815 | ++SDNodeOrder; |
| 3816 | SDValue &N = NodeMap[Address]; |
| 3817 | SDDbgValue *SDV; |
| 3818 | if (N.getNode()) { |
| 3819 | if (isParameter && !AI) { |
| 3820 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 3821 | if (FINode) |
| 3822 | // Byval parameter. We have a frame index at this point. |
| 3823 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 3824 | 0, dl, SDNodeOrder); |
| 3825 | else |
| 3826 | // Can't do anything with other non-AI cases yet. This might be a |
| 3827 | // parameter of a callee function that got inlined, for example. |
| 3828 | return 0; |
| 3829 | } else if (AI) |
| 3830 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 3831 | 0, dl, SDNodeOrder); |
| 3832 | else |
| 3833 | // Can't do anything with other non-AI cases yet. |
| 3834 | return 0; |
| 3835 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 3836 | } else { |
| 3837 | // Generating Undefs here seems to be actively harmful because it |
| 3838 | // affects the line numbers. |
| 3839 | return 0; |
| 3840 | #if 0 |
| 3841 | // This isn't useful, but it shows what we're missing. |
| 3842 | SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()), |
| 3843 | 0, dl, SDNodeOrder); |
| 3844 | DAG.AddDbgValue(SDV, 0, isParameter); |
| 3845 | #endif |
| 3846 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3847 | return 0; |
| 3848 | } |
| 3849 | case Intrinsic::dbg_value: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3850 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3851 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3852 | return 0; |
| 3853 | |
| 3854 | MDNode *Variable = DI.getVariable(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3855 | uint64_t Offset = DI.getOffset(); |
| 3856 | const Value *V = DI.getValue(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3857 | if (!V) |
| 3858 | return 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3859 | |
| 3860 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 3861 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 3862 | // absolute, but not relative, values are different depending on whether |
| 3863 | // debug info exists. |
| 3864 | ++SDNodeOrder; |
| 3865 | SDDbgValue *SDV; |
| 3866 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) { |
| 3867 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 3868 | DAG.AddDbgValue(SDV, 0, false); |
| 3869 | } else { |
| 3870 | SDValue &N = NodeMap[V]; |
| 3871 | if (N.getNode()) { |
| 3872 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 3873 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 3874 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 3875 | } else { |
| 3876 | // We may expand this to cover more cases. One case where we have no |
| 3877 | // data available is an unreferenced parameter; we need this fallback. |
| 3878 | SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()), |
| 3879 | Offset, dl, SDNodeOrder); |
| 3880 | DAG.AddDbgValue(SDV, 0, false); |
| 3881 | } |
| 3882 | } |
| 3883 | |
| 3884 | // Build a debug info table entry. |
| 3885 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3886 | V = BCI->getOperand(0); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3887 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3888 | // Don't handle byval struct arguments or VLAs, for example. |
| 3889 | if (!AI) |
| 3890 | return 0; |
| 3891 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3892 | FuncInfo.StaticAllocaMap.find(AI); |
| 3893 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3894 | return 0; // VLAs. |
| 3895 | int FI = SI->second; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3896 | |
| 3897 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3898 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3899 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3900 | return 0; |
| 3901 | } |
| 3902 | case Intrinsic::eh_exception: { |
| 3903 | // Insert the EXCEPTIONADDR instruction. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3904 | assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() && |
| 3905 | "Call to eh.exception not in landing pad!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3906 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3907 | SDValue Ops[1]; |
| 3908 | Ops[0] = DAG.getRoot(); |
| 3909 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
| 3910 | setValue(&I, Op); |
| 3911 | DAG.setRoot(Op.getValue(1)); |
| 3912 | return 0; |
| 3913 | } |
| 3914 | |
| 3915 | case Intrinsic::eh_selector: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3916 | MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()]; |
| 3917 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3918 | if (CallMBB->isLandingPad()) |
| 3919 | AddCatchInfo(I, &MMI, CallMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3920 | else { |
| 3921 | #ifndef NDEBUG |
| 3922 | FuncInfo.CatchInfoLost.insert(&I); |
| 3923 | #endif |
| 3924 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3925 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3926 | if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | // Insert the EHSELECTION instruction. |
| 3930 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3931 | SDValue Ops[2]; |
| 3932 | Ops[0] = getValue(I.getOperand(1)); |
| 3933 | Ops[1] = getRoot(); |
| 3934 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
| 3935 | DAG.setRoot(Op.getValue(1)); |
| 3936 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
| 3937 | return 0; |
| 3938 | } |
| 3939 | |
| 3940 | case Intrinsic::eh_typeid_for: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3941 | // Find the type id for the given typeinfo. |
| 3942 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 3943 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 3944 | Res = DAG.getConstant(TypeID, MVT::i32); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3945 | setValue(&I, Res); |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | case Intrinsic::eh_return_i32: |
| 3950 | case Intrinsic::eh_return_i64: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3951 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
| 3952 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 3953 | MVT::Other, |
| 3954 | getControlRoot(), |
| 3955 | getValue(I.getOperand(1)), |
| 3956 | getValue(I.getOperand(2)))); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3957 | return 0; |
| 3958 | case Intrinsic::eh_unwind_init: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3959 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3960 | return 0; |
| 3961 | case Intrinsic::eh_dwarf_cfa: { |
| 3962 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
| 3963 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 3964 | TLI.getPointerTy()); |
| 3965 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
| 3966 | TLI.getPointerTy(), |
| 3967 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
| 3968 | TLI.getPointerTy()), |
| 3969 | CfaArg); |
| 3970 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
| 3971 | TLI.getPointerTy(), |
| 3972 | DAG.getConstant(0, TLI.getPointerTy())); |
| 3973 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 3974 | FA, Offset)); |
| 3975 | return 0; |
| 3976 | } |
| 3977 | case Intrinsic::eh_sjlj_callsite: { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3978 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3979 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 3980 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3981 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3982 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 3983 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 3984 | return 0; |
| 3985 | } |
| 3986 | |
| 3987 | case Intrinsic::convertff: |
| 3988 | case Intrinsic::convertfsi: |
| 3989 | case Intrinsic::convertfui: |
| 3990 | case Intrinsic::convertsif: |
| 3991 | case Intrinsic::convertuif: |
| 3992 | case Intrinsic::convertss: |
| 3993 | case Intrinsic::convertsu: |
| 3994 | case Intrinsic::convertus: |
| 3995 | case Intrinsic::convertuu: { |
| 3996 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 3997 | switch (Intrinsic) { |
| 3998 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 3999 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4000 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4001 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4002 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4003 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4004 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4005 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4006 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4007 | } |
| 4008 | EVT DestVT = TLI.getValueType(I.getType()); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4009 | const Value *Op1 = I.getOperand(1); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4010 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 4011 | DAG.getValueType(DestVT), |
| 4012 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4013 | getValue(I.getOperand(2)), |
| 4014 | getValue(I.getOperand(3)), |
| 4015 | Code); |
| 4016 | setValue(&I, Res); |
| 4017 | return 0; |
| 4018 | } |
| 4019 | case Intrinsic::sqrt: |
| 4020 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
| 4021 | getValue(I.getOperand(1)).getValueType(), |
| 4022 | getValue(I.getOperand(1)))); |
| 4023 | return 0; |
| 4024 | case Intrinsic::powi: |
| 4025 | setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)), |
| 4026 | getValue(I.getOperand(2)), DAG)); |
| 4027 | return 0; |
| 4028 | case Intrinsic::sin: |
| 4029 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
| 4030 | getValue(I.getOperand(1)).getValueType(), |
| 4031 | getValue(I.getOperand(1)))); |
| 4032 | return 0; |
| 4033 | case Intrinsic::cos: |
| 4034 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
| 4035 | getValue(I.getOperand(1)).getValueType(), |
| 4036 | getValue(I.getOperand(1)))); |
| 4037 | return 0; |
| 4038 | case Intrinsic::log: |
| 4039 | visitLog(I); |
| 4040 | return 0; |
| 4041 | case Intrinsic::log2: |
| 4042 | visitLog2(I); |
| 4043 | return 0; |
| 4044 | case Intrinsic::log10: |
| 4045 | visitLog10(I); |
| 4046 | return 0; |
| 4047 | case Intrinsic::exp: |
| 4048 | visitExp(I); |
| 4049 | return 0; |
| 4050 | case Intrinsic::exp2: |
| 4051 | visitExp2(I); |
| 4052 | return 0; |
| 4053 | case Intrinsic::pow: |
| 4054 | visitPow(I); |
| 4055 | return 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4056 | case Intrinsic::convert_to_fp16: |
| 4057 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl, |
| 4058 | MVT::i16, getValue(I.getOperand(1)))); |
| 4059 | return 0; |
| 4060 | case Intrinsic::convert_from_fp16: |
| 4061 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl, |
| 4062 | MVT::f32, getValue(I.getOperand(1)))); |
| 4063 | return 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4064 | case Intrinsic::pcmarker: { |
| 4065 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4066 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
| 4067 | return 0; |
| 4068 | } |
| 4069 | case Intrinsic::readcyclecounter: { |
| 4070 | SDValue Op = getRoot(); |
| 4071 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4072 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4073 | &Op, 1); |
| 4074 | setValue(&I, Res); |
| 4075 | DAG.setRoot(Res.getValue(1)); |
| 4076 | return 0; |
| 4077 | } |
| 4078 | case Intrinsic::bswap: |
| 4079 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
| 4080 | getValue(I.getOperand(1)).getValueType(), |
| 4081 | getValue(I.getOperand(1)))); |
| 4082 | return 0; |
| 4083 | case Intrinsic::cttz: { |
| 4084 | SDValue Arg = getValue(I.getOperand(1)); |
| 4085 | EVT Ty = Arg.getValueType(); |
| 4086 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
| 4087 | return 0; |
| 4088 | } |
| 4089 | case Intrinsic::ctlz: { |
| 4090 | SDValue Arg = getValue(I.getOperand(1)); |
| 4091 | EVT Ty = Arg.getValueType(); |
| 4092 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
| 4093 | return 0; |
| 4094 | } |
| 4095 | case Intrinsic::ctpop: { |
| 4096 | SDValue Arg = getValue(I.getOperand(1)); |
| 4097 | EVT Ty = Arg.getValueType(); |
| 4098 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
| 4099 | return 0; |
| 4100 | } |
| 4101 | case Intrinsic::stacksave: { |
| 4102 | SDValue Op = getRoot(); |
| 4103 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4104 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4105 | setValue(&I, Res); |
| 4106 | DAG.setRoot(Res.getValue(1)); |
| 4107 | return 0; |
| 4108 | } |
| 4109 | case Intrinsic::stackrestore: { |
| 4110 | Res = getValue(I.getOperand(1)); |
| 4111 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
| 4112 | return 0; |
| 4113 | } |
| 4114 | case Intrinsic::stackprotector: { |
| 4115 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4116 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4117 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 4118 | EVT PtrTy = TLI.getPointerTy(); |
| 4119 | |
| 4120 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4121 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
| 4122 | |
| 4123 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
| 4124 | MFI->setStackProtectorIndex(FI); |
| 4125 | |
| 4126 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4127 | |
| 4128 | // Store the stack protector onto the stack. |
| 4129 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4130 | PseudoSourceValue::getFixedStack(FI), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4131 | 0, true, false, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4132 | setValue(&I, Res); |
| 4133 | DAG.setRoot(Res); |
| 4134 | return 0; |
| 4135 | } |
| 4136 | case Intrinsic::objectsize: { |
| 4137 | // If we don't know by now, we're never going to know. |
| 4138 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4139 | |
| 4140 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4141 | |
| 4142 | SDValue Arg = getValue(I.getOperand(0)); |
| 4143 | EVT Ty = Arg.getValueType(); |
| 4144 | |
| 4145 | if (CI->getZExtValue() == 0) |
| 4146 | Res = DAG.getConstant(-1ULL, Ty); |
| 4147 | else |
| 4148 | Res = DAG.getConstant(0, Ty); |
| 4149 | |
| 4150 | setValue(&I, Res); |
| 4151 | return 0; |
| 4152 | } |
| 4153 | case Intrinsic::var_annotation: |
| 4154 | // Discard annotate attributes |
| 4155 | return 0; |
| 4156 | |
| 4157 | case Intrinsic::init_trampoline: { |
| 4158 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4159 | |
| 4160 | SDValue Ops[6]; |
| 4161 | Ops[0] = getRoot(); |
| 4162 | Ops[1] = getValue(I.getOperand(1)); |
| 4163 | Ops[2] = getValue(I.getOperand(2)); |
| 4164 | Ops[3] = getValue(I.getOperand(3)); |
| 4165 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4166 | Ops[5] = DAG.getSrcValue(F); |
| 4167 | |
| 4168 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4169 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4170 | Ops, 6); |
| 4171 | |
| 4172 | setValue(&I, Res); |
| 4173 | DAG.setRoot(Res.getValue(1)); |
| 4174 | return 0; |
| 4175 | } |
| 4176 | case Intrinsic::gcroot: |
| 4177 | if (GFI) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4178 | const Value *Alloca = I.getOperand(1); |
| 4179 | const Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4180 | |
| 4181 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4182 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4183 | } |
| 4184 | return 0; |
| 4185 | case Intrinsic::gcread: |
| 4186 | case Intrinsic::gcwrite: |
| 4187 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
| 4188 | return 0; |
| 4189 | case Intrinsic::flt_rounds: |
| 4190 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
| 4191 | return 0; |
| 4192 | case Intrinsic::trap: |
| 4193 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
| 4194 | return 0; |
| 4195 | case Intrinsic::uadd_with_overflow: |
| 4196 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4197 | case Intrinsic::sadd_with_overflow: |
| 4198 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4199 | case Intrinsic::usub_with_overflow: |
| 4200 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4201 | case Intrinsic::ssub_with_overflow: |
| 4202 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4203 | case Intrinsic::umul_with_overflow: |
| 4204 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4205 | case Intrinsic::smul_with_overflow: |
| 4206 | return implVisitAluOverflow(I, ISD::SMULO); |
| 4207 | |
| 4208 | case Intrinsic::prefetch: { |
| 4209 | SDValue Ops[4]; |
| 4210 | Ops[0] = getRoot(); |
| 4211 | Ops[1] = getValue(I.getOperand(1)); |
| 4212 | Ops[2] = getValue(I.getOperand(2)); |
| 4213 | Ops[3] = getValue(I.getOperand(3)); |
| 4214 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
| 4218 | case Intrinsic::memory_barrier: { |
| 4219 | SDValue Ops[6]; |
| 4220 | Ops[0] = getRoot(); |
| 4221 | for (int x = 1; x < 6; ++x) |
| 4222 | Ops[x] = getValue(I.getOperand(x)); |
| 4223 | |
| 4224 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
| 4225 | return 0; |
| 4226 | } |
| 4227 | case Intrinsic::atomic_cmp_swap: { |
| 4228 | SDValue Root = getRoot(); |
| 4229 | SDValue L = |
| 4230 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
| 4231 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4232 | Root, |
| 4233 | getValue(I.getOperand(1)), |
| 4234 | getValue(I.getOperand(2)), |
| 4235 | getValue(I.getOperand(3)), |
| 4236 | I.getOperand(1)); |
| 4237 | setValue(&I, L); |
| 4238 | DAG.setRoot(L.getValue(1)); |
| 4239 | return 0; |
| 4240 | } |
| 4241 | case Intrinsic::atomic_load_add: |
| 4242 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
| 4243 | case Intrinsic::atomic_load_sub: |
| 4244 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
| 4245 | case Intrinsic::atomic_load_or: |
| 4246 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
| 4247 | case Intrinsic::atomic_load_xor: |
| 4248 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
| 4249 | case Intrinsic::atomic_load_and: |
| 4250 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
| 4251 | case Intrinsic::atomic_load_nand: |
| 4252 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
| 4253 | case Intrinsic::atomic_load_max: |
| 4254 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
| 4255 | case Intrinsic::atomic_load_min: |
| 4256 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
| 4257 | case Intrinsic::atomic_load_umin: |
| 4258 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
| 4259 | case Intrinsic::atomic_load_umax: |
| 4260 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
| 4261 | case Intrinsic::atomic_swap: |
| 4262 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
| 4263 | |
| 4264 | case Intrinsic::invariant_start: |
| 4265 | case Intrinsic::lifetime_start: |
| 4266 | // Discard region information. |
| 4267 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
| 4268 | return 0; |
| 4269 | case Intrinsic::invariant_end: |
| 4270 | case Intrinsic::lifetime_end: |
| 4271 | // Discard region information. |
| 4272 | return 0; |
| 4273 | } |
| 4274 | } |
| 4275 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4276 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4277 | bool isTailCall, |
| 4278 | MachineBasicBlock *LandingPad) { |
| 4279 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4280 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4281 | const Type *RetTy = FTy->getReturnType(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4282 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4283 | MCSymbol *BeginLabel = 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4284 | |
| 4285 | TargetLowering::ArgListTy Args; |
| 4286 | TargetLowering::ArgListEntry Entry; |
| 4287 | Args.reserve(CS.arg_size()); |
| 4288 | |
| 4289 | // Check whether the function can return without sret-demotion. |
| 4290 | SmallVector<EVT, 4> OutVTs; |
| 4291 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4292 | SmallVector<uint64_t, 4> Offsets; |
| 4293 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
| 4294 | OutVTs, OutsFlags, TLI, &Offsets); |
| 4295 | |
| 4296 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
| 4297 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4298 | |
| 4299 | SDValue DemoteStackSlot; |
| 4300 | |
| 4301 | if (!CanLowerReturn) { |
| 4302 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4303 | FTy->getReturnType()); |
| 4304 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4305 | FTy->getReturnType()); |
| 4306 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4307 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
| 4308 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4309 | |
| 4310 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4311 | Entry.Node = DemoteStackSlot; |
| 4312 | Entry.Ty = StackSlotPtrType; |
| 4313 | Entry.isSExt = false; |
| 4314 | Entry.isZExt = false; |
| 4315 | Entry.isInReg = false; |
| 4316 | Entry.isSRet = true; |
| 4317 | Entry.isNest = false; |
| 4318 | Entry.isByVal = false; |
| 4319 | Entry.Alignment = Align; |
| 4320 | Args.push_back(Entry); |
| 4321 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4322 | } |
| 4323 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4324 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4325 | i != e; ++i) { |
| 4326 | SDValue ArgNode = getValue(*i); |
| 4327 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4328 | |
| 4329 | unsigned attrInd = i - CS.arg_begin() + 1; |
| 4330 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4331 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4332 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4333 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4334 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4335 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
| 4336 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4337 | Args.push_back(Entry); |
| 4338 | } |
| 4339 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4340 | if (LandingPad) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4341 | // Insert a label before the invoke call to mark the try range. This can be |
| 4342 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4343 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4344 | |
| 4345 | // For SjLj, keep track of which landing pads go with which invokes |
| 4346 | // so as to maintain the ordering of pads in the LSDA. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4347 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4348 | if (CallSiteIndex) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4349 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4350 | // Now that the call site is handled, stop tracking it. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4351 | MMI.setCurrentCallSite(0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | // Both PendingLoads and PendingExports must be flushed here; |
| 4355 | // this call might not return. |
| 4356 | (void)getRoot(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4357 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | // Check if target-independent constraints permit a tail call here. |
| 4361 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4362 | if (isTailCall && |
| 4363 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
| 4364 | isTailCall = false; |
| 4365 | |
| 4366 | std::pair<SDValue,SDValue> Result = |
| 4367 | TLI.LowerCallTo(getRoot(), RetTy, |
| 4368 | CS.paramHasAttr(0, Attribute::SExt), |
| 4369 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
| 4370 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
| 4371 | CS.getCallingConv(), |
| 4372 | isTailCall, |
| 4373 | !CS.getInstruction()->use_empty(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4374 | Callee, Args, DAG, getCurDebugLoc()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4375 | assert((isTailCall || Result.second.getNode()) && |
| 4376 | "Non-null chain expected with non-tail call!"); |
| 4377 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4378 | "Null value expected with tail call!"); |
| 4379 | if (Result.first.getNode()) { |
| 4380 | setValue(CS.getInstruction(), Result.first); |
| 4381 | } else if (!CanLowerReturn && Result.second.getNode()) { |
| 4382 | // The instruction result is the result of loading from the |
| 4383 | // hidden sret parameter. |
| 4384 | SmallVector<EVT, 1> PVTs; |
| 4385 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4386 | |
| 4387 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4388 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4389 | EVT PtrVT = PVTs[0]; |
| 4390 | unsigned NumValues = OutVTs.size(); |
| 4391 | SmallVector<SDValue, 4> Values(NumValues); |
| 4392 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4393 | |
| 4394 | for (unsigned i = 0; i < NumValues; ++i) { |
| 4395 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4396 | DemoteStackSlot, |
| 4397 | DAG.getConstant(Offsets[i], PtrVT)); |
| 4398 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4399 | Add, NULL, Offsets[i], false, false, 1); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4400 | Values[i] = L; |
| 4401 | Chains[i] = L.getValue(1); |
| 4402 | } |
| 4403 | |
| 4404 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4405 | MVT::Other, &Chains[0], NumValues); |
| 4406 | PendingLoads.push_back(Chain); |
| 4407 | |
| 4408 | // Collect the legal value parts into potentially illegal values |
| 4409 | // that correspond to the original function's return values. |
| 4410 | SmallVector<EVT, 4> RetTys; |
| 4411 | RetTy = FTy->getReturnType(); |
| 4412 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4413 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4414 | SmallVector<SDValue, 4> ReturnValues; |
| 4415 | unsigned CurReg = 0; |
| 4416 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4417 | EVT VT = RetTys[I]; |
| 4418 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4419 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4420 | |
| 4421 | SDValue ReturnValue = |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4422 | getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4423 | RegisterVT, VT, AssertOp); |
| 4424 | ReturnValues.push_back(ReturnValue); |
| 4425 | CurReg += NumRegs; |
| 4426 | } |
| 4427 | |
| 4428 | setValue(CS.getInstruction(), |
| 4429 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4430 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4431 | &ReturnValues[0], ReturnValues.size())); |
| 4432 | |
| 4433 | } |
| 4434 | |
| 4435 | // As a special case, a null chain means that a tail call has been emitted and |
| 4436 | // the DAG root is already updated. |
| 4437 | if (Result.second.getNode()) |
| 4438 | DAG.setRoot(Result.second); |
| 4439 | else |
| 4440 | HasTailCall = true; |
| 4441 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4442 | if (LandingPad) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4443 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4444 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4445 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
| 4446 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4447 | |
| 4448 | // Inform MachineModuleInfo of range. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4449 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4454 | /// value is equal or not-equal to zero. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4455 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 4456 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4457 | UI != E; ++UI) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4458 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4459 | if (IC->isEquality()) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4460 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4461 | if (C->isNullValue()) |
| 4462 | continue; |
| 4463 | // Unknown instruction. |
| 4464 | return false; |
| 4465 | } |
| 4466 | return true; |
| 4467 | } |
| 4468 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4469 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
| 4470 | const Type *LoadTy, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4471 | SelectionDAGBuilder &Builder) { |
| 4472 | |
| 4473 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4474 | // input is from a string literal. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4475 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4476 | // Cast pointer to the type we really want to load. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4477 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4478 | PointerType::getUnqual(LoadTy)); |
| 4479 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4480 | if (const Constant *LoadCst = |
| 4481 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 4482 | Builder.TD)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4483 | return Builder.getValue(LoadCst); |
| 4484 | } |
| 4485 | |
| 4486 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4487 | // still constant memory, the input chain can be the entry node. |
| 4488 | SDValue Root; |
| 4489 | bool ConstantMemory = false; |
| 4490 | |
| 4491 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4492 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4493 | Root = Builder.DAG.getEntryNode(); |
| 4494 | ConstantMemory = true; |
| 4495 | } else { |
| 4496 | // Do not serialize non-volatile loads against each other. |
| 4497 | Root = Builder.DAG.getRoot(); |
| 4498 | } |
| 4499 | |
| 4500 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4501 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4502 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4503 | false /*volatile*/, |
| 4504 | false /*nontemporal*/, 1 /* align=1 */); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4505 | |
| 4506 | if (!ConstantMemory) |
| 4507 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4508 | return LoadVal; |
| 4509 | } |
| 4510 | |
| 4511 | |
| 4512 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4513 | /// If so, return true and lower it, otherwise return false and it will be |
| 4514 | /// lowered like a normal call. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4515 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4516 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
| 4517 | if (I.getNumOperands() != 4) |
| 4518 | return false; |
| 4519 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4520 | const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4521 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
| 4522 | !I.getOperand(3)->getType()->isIntegerTy() || |
| 4523 | !I.getType()->isIntegerTy()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4524 | return false; |
| 4525 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4526 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4527 | |
| 4528 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4529 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
| 4530 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4531 | bool ActuallyDoIt = true; |
| 4532 | MVT LoadVT; |
| 4533 | const Type *LoadTy; |
| 4534 | switch (Size->getZExtValue()) { |
| 4535 | default: |
| 4536 | LoadVT = MVT::Other; |
| 4537 | LoadTy = 0; |
| 4538 | ActuallyDoIt = false; |
| 4539 | break; |
| 4540 | case 2: |
| 4541 | LoadVT = MVT::i16; |
| 4542 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4543 | break; |
| 4544 | case 4: |
| 4545 | LoadVT = MVT::i32; |
| 4546 | LoadTy = Type::getInt32Ty(Size->getContext()); |
| 4547 | break; |
| 4548 | case 8: |
| 4549 | LoadVT = MVT::i64; |
| 4550 | LoadTy = Type::getInt64Ty(Size->getContext()); |
| 4551 | break; |
| 4552 | /* |
| 4553 | case 16: |
| 4554 | LoadVT = MVT::v4i32; |
| 4555 | LoadTy = Type::getInt32Ty(Size->getContext()); |
| 4556 | LoadTy = VectorType::get(LoadTy, 4); |
| 4557 | break; |
| 4558 | */ |
| 4559 | } |
| 4560 | |
| 4561 | // This turns into unaligned loads. We only do this if the target natively |
| 4562 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4563 | // we'll only produce a small number of byte loads. |
| 4564 | |
| 4565 | // Require that we can find a legal MVT, and only do this if the target |
| 4566 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4567 | // bloat the code. |
| 4568 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4569 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4570 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4571 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4572 | ActuallyDoIt = false; |
| 4573 | } |
| 4574 | |
| 4575 | if (ActuallyDoIt) { |
| 4576 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4577 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
| 4578 | |
| 4579 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4580 | ISD::SETNE); |
| 4581 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4582 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4583 | return true; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | |
| 4588 | return false; |
| 4589 | } |
| 4590 | |
| 4591 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4592 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4593 | const char *RenameFn = 0; |
| 4594 | if (Function *F = I.getCalledFunction()) { |
| 4595 | if (F->isDeclaration()) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4596 | const TargetIntrinsicInfo *II = TM.getIntrinsicInfo(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4597 | if (II) { |
| 4598 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4599 | RenameFn = visitIntrinsicCall(I, IID); |
| 4600 | if (!RenameFn) |
| 4601 | return; |
| 4602 | } |
| 4603 | } |
| 4604 | if (unsigned IID = F->getIntrinsicID()) { |
| 4605 | RenameFn = visitIntrinsicCall(I, IID); |
| 4606 | if (!RenameFn) |
| 4607 | return; |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4612 | // can't be a library call. |
| 4613 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4614 | StringRef Name = F->getName(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4615 | if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4616 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4617 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4618 | I.getType() == I.getOperand(1)->getType() && |
| 4619 | I.getType() == I.getOperand(2)->getType()) { |
| 4620 | SDValue LHS = getValue(I.getOperand(1)); |
| 4621 | SDValue RHS = getValue(I.getOperand(2)); |
| 4622 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4623 | LHS.getValueType(), LHS, RHS)); |
| 4624 | return; |
| 4625 | } |
| 4626 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
| 4627 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4628 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4629 | I.getType() == I.getOperand(1)->getType()) { |
| 4630 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4631 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4632 | Tmp.getValueType(), Tmp)); |
| 4633 | return; |
| 4634 | } |
| 4635 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
| 4636 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4637 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4638 | I.getType() == I.getOperand(1)->getType() && |
| 4639 | I.onlyReadsMemory()) { |
| 4640 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4641 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4642 | Tmp.getValueType(), Tmp)); |
| 4643 | return; |
| 4644 | } |
| 4645 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
| 4646 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4647 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4648 | I.getType() == I.getOperand(1)->getType() && |
| 4649 | I.onlyReadsMemory()) { |
| 4650 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4651 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4652 | Tmp.getValueType(), Tmp)); |
| 4653 | return; |
| 4654 | } |
| 4655 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4656 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4657 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4658 | I.getType() == I.getOperand(1)->getType() && |
| 4659 | I.onlyReadsMemory()) { |
| 4660 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4661 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4662 | Tmp.getValueType(), Tmp)); |
| 4663 | return; |
| 4664 | } |
| 4665 | } else if (Name == "memcmp") { |
| 4666 | if (visitMemCmpCall(I)) |
| 4667 | return; |
| 4668 | } |
| 4669 | } |
| 4670 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4671 | visitInlineAsm(&I); |
| 4672 | return; |
| 4673 | } |
| 4674 | |
| 4675 | SDValue Callee; |
| 4676 | if (!RenameFn) |
| 4677 | Callee = getValue(I.getOperand(0)); |
| 4678 | else |
| 4679 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
| 4680 | |
| 4681 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4682 | // be done within LowerCallTo, after more information about the call is known. |
| 4683 | LowerCallTo(&I, Callee, I.isTailCall()); |
| 4684 | } |
| 4685 | |
| 4686 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 4687 | /// this value and returns the result as a ValueVT value. This uses |
| 4688 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4689 | /// If the Flag pointer is NULL, no flag is used. |
| 4690 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4691 | SDValue &Chain, SDValue *Flag) const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4692 | // Assemble the legal parts into the final values. |
| 4693 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4694 | SmallVector<SDValue, 8> Parts; |
| 4695 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4696 | // Copy the legal parts from the registers. |
| 4697 | EVT ValueVT = ValueVTs[Value]; |
| 4698 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
| 4699 | EVT RegisterVT = RegVTs[Value]; |
| 4700 | |
| 4701 | Parts.resize(NumRegs); |
| 4702 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4703 | SDValue P; |
| 4704 | if (Flag == 0) { |
| 4705 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 4706 | } else { |
| 4707 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 4708 | *Flag = P.getValue(2); |
| 4709 | } |
| 4710 | |
| 4711 | Chain = P.getValue(1); |
| 4712 | |
| 4713 | // If the source register was virtual and if we know something about it, |
| 4714 | // add an assert node. |
| 4715 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4716 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4717 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4718 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4719 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4720 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
| 4721 | |
| 4722 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4723 | unsigned NumSignBits = LOI.NumSignBits; |
| 4724 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
| 4725 | |
| 4726 | // FIXME: We capture more information than the dag can represent. For |
| 4727 | // now, just use the tightest assertzext/assertsext possible. |
| 4728 | bool isSExt = true; |
| 4729 | EVT FromVT(MVT::Other); |
| 4730 | if (NumSignBits == RegSize) |
| 4731 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 4732 | else if (NumZeroBits >= RegSize-1) |
| 4733 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 4734 | else if (NumSignBits > RegSize-8) |
| 4735 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 4736 | else if (NumZeroBits >= RegSize-8) |
| 4737 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 4738 | else if (NumSignBits > RegSize-16) |
| 4739 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 4740 | else if (NumZeroBits >= RegSize-16) |
| 4741 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 4742 | else if (NumSignBits > RegSize-32) |
| 4743 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 4744 | else if (NumZeroBits >= RegSize-32) |
| 4745 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 4746 | |
| 4747 | if (FromVT != MVT::Other) |
| 4748 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 4749 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | Parts[i] = P; |
| 4754 | } |
| 4755 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4756 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4757 | NumRegs, RegisterVT, ValueVT); |
| 4758 | Part += NumRegs; |
| 4759 | Parts.clear(); |
| 4760 | } |
| 4761 | |
| 4762 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 4763 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4764 | &Values[0], ValueVTs.size()); |
| 4765 | } |
| 4766 | |
| 4767 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 4768 | /// specified value into the registers specified by this object. This uses |
| 4769 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4770 | /// If the Flag pointer is NULL, no flag is used. |
| 4771 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4772 | SDValue &Chain, SDValue *Flag) const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4773 | // Get the list of the values's legal parts. |
| 4774 | unsigned NumRegs = Regs.size(); |
| 4775 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4776 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4777 | EVT ValueVT = ValueVTs[Value]; |
| 4778 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
| 4779 | EVT RegisterVT = RegVTs[Value]; |
| 4780 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4781 | getCopyToParts(DAG, dl, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4782 | Val.getValue(Val.getResNo() + Value), |
| 4783 | &Parts[Part], NumParts, RegisterVT); |
| 4784 | Part += NumParts; |
| 4785 | } |
| 4786 | |
| 4787 | // Copy the parts into the registers. |
| 4788 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4789 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4790 | SDValue Part; |
| 4791 | if (Flag == 0) { |
| 4792 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 4793 | } else { |
| 4794 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 4795 | *Flag = Part.getValue(1); |
| 4796 | } |
| 4797 | |
| 4798 | Chains[i] = Part.getValue(0); |
| 4799 | } |
| 4800 | |
| 4801 | if (NumRegs == 1 || Flag) |
| 4802 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 4803 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4804 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4805 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4806 | // user as well as a successor (the TF operands are flagged to the user). |
| 4807 | // c1, f1 = CopyToReg |
| 4808 | // c2, f2 = CopyToReg |
| 4809 | // c3 = TokenFactor c1, c2 |
| 4810 | // ... |
| 4811 | // = op c3, ..., f2 |
| 4812 | Chain = Chains[NumRegs-1]; |
| 4813 | else |
| 4814 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 4815 | } |
| 4816 | |
| 4817 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 4818 | /// operand list. This adds the code marker and includes the number of |
| 4819 | /// values added into it. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4820 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 4821 | unsigned MatchingIdx, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4822 | SelectionDAG &DAG, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4823 | std::vector<SDValue> &Ops) const { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4824 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4825 | if (HasMatching) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4826 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4827 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 4828 | Ops.push_back(Res); |
| 4829 | |
| 4830 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4831 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
| 4832 | EVT RegisterVT = RegVTs[Value]; |
| 4833 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4834 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 4835 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 4836 | } |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 4841 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4842 | /// register class for the register. Otherwise, return null. |
| 4843 | static const TargetRegisterClass * |
| 4844 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4845 | const TargetLowering &TLI, |
| 4846 | const TargetRegisterInfo *TRI) { |
| 4847 | EVT FoundVT = MVT::Other; |
| 4848 | const TargetRegisterClass *FoundRC = 0; |
| 4849 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4850 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
| 4851 | EVT ThisVT = MVT::Other; |
| 4852 | |
| 4853 | const TargetRegisterClass *RC = *RCI; |
| 4854 | // If none of the value types for this register class are valid, we |
| 4855 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4856 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4857 | I != E; ++I) { |
| 4858 | if (TLI.isTypeLegal(*I)) { |
| 4859 | // If we have already found this register in a different register class, |
| 4860 | // choose the one with the largest VT specified. For example, on |
| 4861 | // PowerPC, we favor f64 register classes over f32. |
| 4862 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
| 4863 | ThisVT = *I; |
| 4864 | break; |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | if (ThisVT == MVT::Other) continue; |
| 4870 | |
| 4871 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4872 | // frame pointer in functions that need it (due to them not being taken |
| 4873 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4874 | // yet). This is a slight code pessimization, but should still work. |
| 4875 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4876 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4877 | if (*I == Reg) { |
| 4878 | // We found a matching register class. Keep looking at others in case |
| 4879 | // we find one with larger registers that this physreg is also in. |
| 4880 | FoundRC = RC; |
| 4881 | FoundVT = ThisVT; |
| 4882 | break; |
| 4883 | } |
| 4884 | } |
| 4885 | return FoundRC; |
| 4886 | } |
| 4887 | |
| 4888 | |
| 4889 | namespace llvm { |
| 4890 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4891 | /// lowering. |
| 4892 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
| 4893 | public TargetLowering::AsmOperandInfo { |
| 4894 | public: |
| 4895 | /// CallOperand - If this is the result output operand or a clobber |
| 4896 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4897 | /// This gets modified as the asm is processed. |
| 4898 | SDValue CallOperand; |
| 4899 | |
| 4900 | /// AssignedRegs - If this is a register or register class operand, this |
| 4901 | /// contains the set of register corresponding to the operand. |
| 4902 | RegsForValue AssignedRegs; |
| 4903 | |
| 4904 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4905 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4906 | } |
| 4907 | |
| 4908 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4909 | /// busy in OutputRegs/InputRegs. |
| 4910 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
| 4911 | std::set<unsigned> &OutputRegs, |
| 4912 | std::set<unsigned> &InputRegs, |
| 4913 | const TargetRegisterInfo &TRI) const { |
| 4914 | if (isOutReg) { |
| 4915 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4916 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4917 | } |
| 4918 | if (isInReg) { |
| 4919 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4920 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
| 4925 | /// corresponds to. If there is no Value* for this operand, it returns |
| 4926 | /// MVT::Other. |
| 4927 | EVT getCallOperandValEVT(LLVMContext &Context, |
| 4928 | const TargetLowering &TLI, |
| 4929 | const TargetData *TD) const { |
| 4930 | if (CallOperandVal == 0) return MVT::Other; |
| 4931 | |
| 4932 | if (isa<BasicBlock>(CallOperandVal)) |
| 4933 | return TLI.getPointerTy(); |
| 4934 | |
| 4935 | const llvm::Type *OpTy = CallOperandVal->getType(); |
| 4936 | |
| 4937 | // If this is an indirect operand, the operand is a pointer to the |
| 4938 | // accessed type. |
| 4939 | if (isIndirect) { |
| 4940 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4941 | if (!PtrTy) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 4942 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 4943 | OpTy = PtrTy->getElementType(); |
| 4944 | } |
| 4945 | |
| 4946 | // If OpTy is not a single value, it may be a struct/union that we |
| 4947 | // can tile with integers. |
| 4948 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4949 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4950 | switch (BitSize) { |
| 4951 | default: break; |
| 4952 | case 1: |
| 4953 | case 8: |
| 4954 | case 16: |
| 4955 | case 32: |
| 4956 | case 64: |
| 4957 | case 128: |
| 4958 | OpTy = IntegerType::get(Context, BitSize); |
| 4959 | break; |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | return TLI.getValueType(OpTy, true); |
| 4964 | } |
| 4965 | |
| 4966 | private: |
| 4967 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4968 | /// specified set. |
| 4969 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
| 4970 | const TargetRegisterInfo &TRI) { |
| 4971 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4972 | Regs.insert(Reg); |
| 4973 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4974 | for (; *Aliases; ++Aliases) |
| 4975 | Regs.insert(*Aliases); |
| 4976 | } |
| 4977 | }; |
| 4978 | } // end llvm namespace. |
| 4979 | |
| 4980 | |
| 4981 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4982 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4983 | /// register allocator to handle the assignment process. However, if the asm |
| 4984 | /// uses features that we can't model on machineinstrs, we have SDISel do the |
| 4985 | /// allocation. This produces generally horrible, but correct, code. |
| 4986 | /// |
| 4987 | /// OpInfo describes the operand. |
| 4988 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4989 | /// |
| 4990 | void SelectionDAGBuilder:: |
| 4991 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
| 4992 | std::set<unsigned> &OutputRegs, |
| 4993 | std::set<unsigned> &InputRegs) { |
| 4994 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
| 4995 | |
| 4996 | // Compute whether this value requires an input register, an output register, |
| 4997 | // or both. |
| 4998 | bool isOutReg = false; |
| 4999 | bool isInReg = false; |
| 5000 | switch (OpInfo.Type) { |
| 5001 | case InlineAsm::isOutput: |
| 5002 | isOutReg = true; |
| 5003 | |
| 5004 | // If there is an input constraint that matches this, we need to reserve |
| 5005 | // the input register so no other inputs allocate to it. |
| 5006 | isInReg = OpInfo.hasMatchingInput(); |
| 5007 | break; |
| 5008 | case InlineAsm::isInput: |
| 5009 | isInReg = true; |
| 5010 | isOutReg = false; |
| 5011 | break; |
| 5012 | case InlineAsm::isClobber: |
| 5013 | isOutReg = true; |
| 5014 | isInReg = true; |
| 5015 | break; |
| 5016 | } |
| 5017 | |
| 5018 | |
| 5019 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5020 | SmallVector<unsigned, 4> Regs; |
| 5021 | |
| 5022 | // If this is a constraint for a single physreg, or a constraint for a |
| 5023 | // register class, find it. |
| 5024 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 5025 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5026 | OpInfo.ConstraintVT); |
| 5027 | |
| 5028 | unsigned NumRegs = 1; |
| 5029 | if (OpInfo.ConstraintVT != MVT::Other) { |
| 5030 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5031 | // cast of the input value. More generally, handle any case where the input |
| 5032 | // value disagrees with the register class we plan to stick this in. |
| 5033 | if (OpInfo.Type == InlineAsm::isInput && |
| 5034 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
| 5035 | // Try to convert to the first EVT that the reg class contains. If the |
| 5036 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5037 | // vector types). |
| 5038 | EVT RegVT = *PhysReg.second->vt_begin(); |
| 5039 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
| 5040 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 5041 | RegVT, OpInfo.CallOperand); |
| 5042 | OpInfo.ConstraintVT = RegVT; |
| 5043 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5044 | // If the input is a FP value and we want it in FP registers, do a |
| 5045 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5046 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5047 | // machine. |
| 5048 | RegVT = EVT::getIntegerVT(Context, |
| 5049 | OpInfo.ConstraintVT.getSizeInBits()); |
| 5050 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 5051 | RegVT, OpInfo.CallOperand); |
| 5052 | OpInfo.ConstraintVT = RegVT; |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
| 5057 | } |
| 5058 | |
| 5059 | EVT RegVT; |
| 5060 | EVT ValueVT = OpInfo.ConstraintVT; |
| 5061 | |
| 5062 | // If this is a constraint for a specific physical register, like {r17}, |
| 5063 | // assign it now. |
| 5064 | if (unsigned AssignedReg = PhysReg.first) { |
| 5065 | const TargetRegisterClass *RC = PhysReg.second; |
| 5066 | if (OpInfo.ConstraintVT == MVT::Other) |
| 5067 | ValueVT = *RC->vt_begin(); |
| 5068 | |
| 5069 | // Get the actual register value type. This is important, because the user |
| 5070 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5071 | // remember that AX is actually i16 to get the right extension. |
| 5072 | RegVT = *RC->vt_begin(); |
| 5073 | |
| 5074 | // This is a explicit reference to a physical register. |
| 5075 | Regs.push_back(AssignedReg); |
| 5076 | |
| 5077 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5078 | if (NumRegs != 1) { |
| 5079 | TargetRegisterClass::iterator I = RC->begin(); |
| 5080 | for (; *I != AssignedReg; ++I) |
| 5081 | assert(I != RC->end() && "Didn't find reg!"); |
| 5082 | |
| 5083 | // Already added the first reg. |
| 5084 | --NumRegs; ++I; |
| 5085 | for (; NumRegs; --NumRegs, ++I) { |
| 5086 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
| 5087 | Regs.push_back(*I); |
| 5088 | } |
| 5089 | } |
| 5090 | |
| 5091 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5092 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5093 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5094 | return; |
| 5095 | } |
| 5096 | |
| 5097 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5098 | // for this reference. |
| 5099 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5100 | RegVT = *RC->vt_begin(); |
| 5101 | if (OpInfo.ConstraintVT == MVT::Other) |
| 5102 | ValueVT = RegVT; |
| 5103 | |
| 5104 | // Create the appropriate number of virtual registers. |
| 5105 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5106 | for (; NumRegs; --NumRegs) |
| 5107 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
| 5108 | |
| 5109 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5110 | return; |
| 5111 | } |
| 5112 | |
| 5113 | // This is a reference to a register class that doesn't directly correspond |
| 5114 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5115 | // registers from the class. |
| 5116 | std::vector<unsigned> RegClassRegs |
| 5117 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5118 | OpInfo.ConstraintVT); |
| 5119 | |
| 5120 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5121 | unsigned NumAllocated = 0; |
| 5122 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5123 | unsigned Reg = RegClassRegs[i]; |
| 5124 | // See if this register is available. |
| 5125 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5126 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5127 | // Make sure we find consecutive registers. |
| 5128 | NumAllocated = 0; |
| 5129 | continue; |
| 5130 | } |
| 5131 | |
| 5132 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5133 | // stack pointer). |
| 5134 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5135 | if (!RC) { // Couldn't allocate this register. |
| 5136 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5137 | NumAllocated = 0; |
| 5138 | continue; |
| 5139 | } |
| 5140 | |
| 5141 | // Okay, this register is good, we can use it. |
| 5142 | ++NumAllocated; |
| 5143 | |
| 5144 | // If we allocated enough consecutive registers, succeed. |
| 5145 | if (NumAllocated == NumRegs) { |
| 5146 | unsigned RegStart = (i-NumAllocated)+1; |
| 5147 | unsigned RegEnd = i+1; |
| 5148 | // Mark all of the allocated registers used. |
| 5149 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5150 | Regs.push_back(RegClassRegs[i]); |
| 5151 | |
| 5152 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
| 5153 | OpInfo.ConstraintVT); |
| 5154 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5155 | return; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | // Otherwise, we couldn't allocate enough registers for this. |
| 5160 | } |
| 5161 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5162 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5163 | /// |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5164 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5165 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5166 | |
| 5167 | /// ConstraintOperands - Information about all of the constraints. |
| 5168 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
| 5169 | |
| 5170 | std::set<unsigned> OutputRegs, InputRegs; |
| 5171 | |
| 5172 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5173 | // ConstraintOperands list. |
| 5174 | std::vector<InlineAsm::ConstraintInfo> |
| 5175 | ConstraintInfos = IA->ParseConstraints(); |
| 5176 | |
| 5177 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
| 5178 | |
| 5179 | SDValue Chain, Flag; |
| 5180 | |
| 5181 | // We won't need to flush pending loads if this asm doesn't touch |
| 5182 | // memory and is nonvolatile. |
| 5183 | if (hasMemory || IA->hasSideEffects()) |
| 5184 | Chain = getRoot(); |
| 5185 | else |
| 5186 | Chain = DAG.getRoot(); |
| 5187 | |
| 5188 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5189 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5190 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5191 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5192 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 5193 | |
| 5194 | EVT OpVT = MVT::Other; |
| 5195 | |
| 5196 | // Compute the value type for each operand. |
| 5197 | switch (OpInfo.Type) { |
| 5198 | case InlineAsm::isOutput: |
| 5199 | // Indirect outputs just consume an argument. |
| 5200 | if (OpInfo.isIndirect) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5201 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5202 | break; |
| 5203 | } |
| 5204 | |
| 5205 | // The return value of the call is this value. As such, there is no |
| 5206 | // corresponding argument. |
| 5207 | assert(!CS.getType()->isVoidTy() && |
| 5208 | "Bad inline asm!"); |
| 5209 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5210 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5211 | } else { |
| 5212 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5213 | OpVT = TLI.getValueType(CS.getType()); |
| 5214 | } |
| 5215 | ++ResNo; |
| 5216 | break; |
| 5217 | case InlineAsm::isInput: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5218 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5219 | break; |
| 5220 | case InlineAsm::isClobber: |
| 5221 | // Nothing to do. |
| 5222 | break; |
| 5223 | } |
| 5224 | |
| 5225 | // If this is an input or an indirect output, process the call argument. |
| 5226 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5227 | if (OpInfo.CallOperandVal) { |
| 5228 | // Strip bitcasts, if any. This mostly comes up for functions. |
| 5229 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5230 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5231 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5232 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
| 5233 | } else { |
| 5234 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
| 5235 | } |
| 5236 | |
| 5237 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
| 5238 | } |
| 5239 | |
| 5240 | OpInfo.ConstraintVT = OpVT; |
| 5241 | } |
| 5242 | |
| 5243 | // Second pass over the constraints: compute which constraint option to use |
| 5244 | // and assign registers to constraints that want a specific physreg. |
| 5245 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5246 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5247 | |
| 5248 | // If this is an output operand with a matching input operand, look up the |
| 5249 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5250 | // other is floating point, or their sizes are different, flag it as an |
| 5251 | // error. |
| 5252 | if (OpInfo.hasMatchingInput()) { |
| 5253 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5254 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5255 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
| 5256 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5257 | Input.ConstraintVT.isInteger()) || |
| 5258 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5259 | Input.ConstraintVT.getSizeInBits())) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5260 | report_fatal_error("Unsupported asm: input constraint" |
| 5261 | " with a matching output constraint of" |
| 5262 | " incompatible type!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5263 | } |
| 5264 | Input.ConstraintVT = OpInfo.ConstraintVT; |
| 5265 | } |
| 5266 | } |
| 5267 | |
| 5268 | // Compute the constraint code and ConstraintType to use. |
| 5269 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
| 5270 | |
| 5271 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5272 | // need to to provide an address for the memory input. |
| 5273 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5274 | !OpInfo.isIndirect) { |
| 5275 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5276 | "Can only indirectify direct input operands!"); |
| 5277 | |
| 5278 | // Memory operands really want the address of the value. If we don't have |
| 5279 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5280 | // it to a stack slot. |
| 5281 | |
| 5282 | // If the operand is a float, integer, or vector constant, spill to a |
| 5283 | // constant pool entry to get its address. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5284 | const Value *OpVal = OpInfo.CallOperandVal; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5285 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5286 | isa<ConstantVector>(OpVal)) { |
| 5287 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5288 | TLI.getPointerTy()); |
| 5289 | } else { |
| 5290 | // Otherwise, create a stack slot and emit a store to it before the |
| 5291 | // asm. |
| 5292 | const Type *Ty = OpVal->getType(); |
| 5293 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
| 5294 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5295 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5296 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
| 5297 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 5298 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5299 | OpInfo.CallOperand, StackSlot, NULL, 0, |
| 5300 | false, false, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5301 | OpInfo.CallOperand = StackSlot; |
| 5302 | } |
| 5303 | |
| 5304 | // There is no longer a Value* corresponding to this operand. |
| 5305 | OpInfo.CallOperandVal = 0; |
| 5306 | |
| 5307 | // It is now an indirect operand. |
| 5308 | OpInfo.isIndirect = true; |
| 5309 | } |
| 5310 | |
| 5311 | // If this constraint is for a specific register, allocate it before |
| 5312 | // anything else. |
| 5313 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
| 5314 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
| 5315 | } |
| 5316 | |
| 5317 | ConstraintInfos.clear(); |
| 5318 | |
| 5319 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
| 5320 | // to register class operands. |
| 5321 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5322 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5323 | |
| 5324 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5325 | // to be. |
| 5326 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
| 5327 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
| 5328 | } |
| 5329 | |
| 5330 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5331 | std::vector<SDValue> AsmNodeOperands; |
| 5332 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5333 | AsmNodeOperands.push_back( |
| 5334 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5335 | TLI.getPointerTy())); |
| 5336 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5337 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 5338 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 5339 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 5340 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 5341 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5342 | |
| 5343 | // Loop over all of the inputs, copying the operand values into the |
| 5344 | // appropriate registers and processing the output regs. |
| 5345 | RegsForValue RetValRegs; |
| 5346 | |
| 5347 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5348 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
| 5349 | |
| 5350 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5351 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5352 | |
| 5353 | switch (OpInfo.Type) { |
| 5354 | case InlineAsm::isOutput: { |
| 5355 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5356 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5357 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5358 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5359 | |
| 5360 | // Add information to the INLINEASM node to know about this output. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5361 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 5362 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5363 | TLI.getPointerTy())); |
| 5364 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5365 | break; |
| 5366 | } |
| 5367 | |
| 5368 | // Otherwise, this is a register or register class output. |
| 5369 | |
| 5370 | // Copy the output from the appropriate register. Find a register that |
| 5371 | // we can use. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5372 | if (OpInfo.AssignedRegs.Regs.empty()) |
| 5373 | report_fatal_error("Couldn't allocate output reg for constraint '" + |
| 5374 | Twine(OpInfo.ConstraintCode) + "'!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5375 | |
| 5376 | // If this is an indirect operand, store through the pointer after the |
| 5377 | // asm. |
| 5378 | if (OpInfo.isIndirect) { |
| 5379 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5380 | OpInfo.CallOperandVal)); |
| 5381 | } else { |
| 5382 | // This is the result value of the call. |
| 5383 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
| 5384 | // Concatenate this output onto the outputs list. |
| 5385 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5386 | } |
| 5387 | |
| 5388 | // Add information to the INLINEASM node to know that this register is |
| 5389 | // set. |
| 5390 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5391 | InlineAsm::Kind_RegDefEarlyClobber : |
| 5392 | InlineAsm::Kind_RegDef, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5393 | false, |
| 5394 | 0, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5395 | DAG, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5396 | AsmNodeOperands); |
| 5397 | break; |
| 5398 | } |
| 5399 | case InlineAsm::isInput: { |
| 5400 | SDValue InOperandVal = OpInfo.CallOperand; |
| 5401 | |
| 5402 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
| 5403 | // If this is required to match an output register we have already set, |
| 5404 | // just use its register. |
| 5405 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
| 5406 | |
| 5407 | // Scan until we find the definition we already emitted of this operand. |
| 5408 | // When we find it, create a RegsForValue operand. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5409 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5410 | for (; OperandNo; --OperandNo) { |
| 5411 | // Advance to the next operand. |
| 5412 | unsigned OpFlag = |
| 5413 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5414 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 5415 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 5416 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5417 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
| 5418 | } |
| 5419 | |
| 5420 | unsigned OpFlag = |
| 5421 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5422 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 5423 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5424 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
| 5425 | if (OpInfo.isIndirect) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5426 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
| 5427 | LLVMContext &Ctx = *DAG.getContext(); |
| 5428 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 5429 | " don't know how to handle tied " |
| 5430 | "indirect register inputs"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5431 | } |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5432 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5433 | RegsForValue MatchedRegs; |
| 5434 | MatchedRegs.TLI = &TLI; |
| 5435 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
| 5436 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 5437 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5438 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
| 5439 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
| 5440 | i != e; ++i) |
| 5441 | MatchedRegs.Regs.push_back |
| 5442 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
| 5443 | |
| 5444 | // Use the produced MatchedRegs object to |
| 5445 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5446 | Chain, &Flag); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5447 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5448 | true, OpInfo.getMatchedOperand(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5449 | DAG, AsmNodeOperands); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5450 | break; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5451 | } |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5452 | |
| 5453 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 5454 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 5455 | "Unexpected number of operands"); |
| 5456 | // Add information to the INLINEASM node to know about this input. |
| 5457 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5458 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 5459 | OpInfo.getMatchedOperand()); |
| 5460 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 5461 | TLI.getPointerTy())); |
| 5462 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5463 | break; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 5467 | assert(!OpInfo.isIndirect && |
| 5468 | "Don't know how to handle indirect other inputs yet!"); |
| 5469 | |
| 5470 | std::vector<SDValue> Ops; |
| 5471 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
| 5472 | hasMemory, Ops, DAG); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5473 | if (Ops.empty()) |
| 5474 | report_fatal_error("Invalid operand for inline asm constraint '" + |
| 5475 | Twine(OpInfo.ConstraintCode) + "'!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5476 | |
| 5477 | // Add information to the INLINEASM node to know about this input. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5478 | unsigned ResOpType = |
| 5479 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5480 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 5481 | TLI.getPointerTy())); |
| 5482 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5483 | break; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5484 | } |
| 5485 | |
| 5486 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5487 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5488 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5489 | "Memory operands expect pointer values"); |
| 5490 | |
| 5491 | // Add information to the INLINEASM node to know about this input. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5492 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5493 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 5494 | TLI.getPointerTy())); |
| 5495 | AsmNodeOperands.push_back(InOperandVal); |
| 5496 | break; |
| 5497 | } |
| 5498 | |
| 5499 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5500 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5501 | "Unknown constraint type!"); |
| 5502 | assert(!OpInfo.isIndirect && |
| 5503 | "Don't know how to handle indirect register inputs yet!"); |
| 5504 | |
| 5505 | // Copy the input into the appropriate registers. |
| 5506 | if (OpInfo.AssignedRegs.Regs.empty() || |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5507 | !OpInfo.AssignedRegs.areValueTypesLegal()) |
| 5508 | report_fatal_error("Couldn't allocate input reg for constraint '" + |
| 5509 | Twine(OpInfo.ConstraintCode) + "'!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5510 | |
| 5511 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5512 | Chain, &Flag); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5513 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5514 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5515 | DAG, AsmNodeOperands); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5516 | break; |
| 5517 | } |
| 5518 | case InlineAsm::isClobber: { |
| 5519 | // Add the clobbered value to the operand list, so that the register |
| 5520 | // allocator is aware that the physreg got clobbered. |
| 5521 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5522 | OpInfo.AssignedRegs.AddInlineAsmOperands( |
| 5523 | InlineAsm::Kind_RegDefEarlyClobber, |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5524 | false, 0, DAG, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5525 | AsmNodeOperands); |
| 5526 | break; |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5531 | // Finish up input operands. Set the input chain and add the flag last. |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5532 | AsmNodeOperands[0] = Chain; |
| 5533 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
| 5534 | |
| 5535 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
| 5536 | DAG.getVTList(MVT::Other, MVT::Flag), |
| 5537 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5538 | Flag = Chain.getValue(1); |
| 5539 | |
| 5540 | // If this asm returns a register value, copy the result from that register |
| 5541 | // and set it as the value of the call. |
| 5542 | if (!RetValRegs.Regs.empty()) { |
| 5543 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5544 | Chain, &Flag); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5545 | |
| 5546 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5547 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
| 5548 | EVT ResultType = TLI.getValueType(CS.getType()); |
| 5549 | |
| 5550 | // If any of the results of the inline asm is a vector, it may have the |
| 5551 | // wrong width/num elts. This can happen for register classes that can |
| 5552 | // contain multiple different value types. The preg or vreg allocated may |
| 5553 | // not have the same VT as was expected. Convert it to the right type |
| 5554 | // with bit_convert. |
| 5555 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
| 5556 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 5557 | ResultType, Val); |
| 5558 | |
| 5559 | } else if (ResultType != Val.getValueType() && |
| 5560 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5561 | // If a result value was tied to an input value, the computed result may |
| 5562 | // have a wider width than the expected result. Extract the relevant |
| 5563 | // portion. |
| 5564 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
| 5565 | } |
| 5566 | |
| 5567 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
| 5568 | } |
| 5569 | |
| 5570 | setValue(CS.getInstruction(), Val); |
| 5571 | // Don't need to use this as a chain in this case. |
| 5572 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5573 | return; |
| 5574 | } |
| 5575 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5576 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5577 | |
| 5578 | // Process indirect outputs, first output all of the flagged copies out of |
| 5579 | // physregs. |
| 5580 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5581 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5582 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5583 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5584 | Chain, &Flag); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5585 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | // Emit the non-flagged stores from the physregs. |
| 5589 | SmallVector<SDValue, 8> OutChains; |
| 5590 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5591 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5592 | StoresToEmit[i].first, |
| 5593 | getValue(StoresToEmit[i].second), |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5594 | StoresToEmit[i].second, 0, |
| 5595 | false, false, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5596 | OutChains.push_back(Val); |
| 5597 | } |
| 5598 | |
| 5599 | if (!OutChains.empty()) |
| 5600 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
| 5601 | &OutChains[0], OutChains.size()); |
| 5602 | |
| 5603 | DAG.setRoot(Chain); |
| 5604 | } |
| 5605 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5606 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5607 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5608 | MVT::Other, getRoot(), |
| 5609 | getValue(I.getOperand(1)), |
| 5610 | DAG.getSrcValue(I.getOperand(1)))); |
| 5611 | } |
| 5612 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5613 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5614 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5615 | getRoot(), getValue(I.getOperand(0)), |
| 5616 | DAG.getSrcValue(I.getOperand(0))); |
| 5617 | setValue(&I, V); |
| 5618 | DAG.setRoot(V.getValue(1)); |
| 5619 | } |
| 5620 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5621 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5622 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5623 | MVT::Other, getRoot(), |
| 5624 | getValue(I.getOperand(1)), |
| 5625 | DAG.getSrcValue(I.getOperand(1)))); |
| 5626 | } |
| 5627 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5628 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5629 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5630 | MVT::Other, getRoot(), |
| 5631 | getValue(I.getOperand(1)), |
| 5632 | getValue(I.getOperand(2)), |
| 5633 | DAG.getSrcValue(I.getOperand(1)), |
| 5634 | DAG.getSrcValue(I.getOperand(2)))); |
| 5635 | } |
| 5636 | |
| 5637 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 5638 | /// implementation, which just calls LowerCall. |
| 5639 | /// FIXME: When all targets are |
| 5640 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
| 5641 | std::pair<SDValue, SDValue> |
| 5642 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5643 | bool RetSExt, bool RetZExt, bool isVarArg, |
| 5644 | bool isInreg, unsigned NumFixedArgs, |
| 5645 | CallingConv::ID CallConv, bool isTailCall, |
| 5646 | bool isReturnValueUsed, |
| 5647 | SDValue Callee, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5648 | ArgListTy &Args, SelectionDAG &DAG, |
| 5649 | DebugLoc dl) const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5650 | // Handle all of the outgoing arguments. |
| 5651 | SmallVector<ISD::OutputArg, 32> Outs; |
| 5652 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5653 | SmallVector<EVT, 4> ValueVTs; |
| 5654 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5655 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5656 | Value != NumValues; ++Value) { |
| 5657 | EVT VT = ValueVTs[Value]; |
| 5658 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
| 5659 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5660 | Args[i].Node.getResNo() + Value); |
| 5661 | ISD::ArgFlagsTy Flags; |
| 5662 | unsigned OriginalAlignment = |
| 5663 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5664 | |
| 5665 | if (Args[i].isZExt) |
| 5666 | Flags.setZExt(); |
| 5667 | if (Args[i].isSExt) |
| 5668 | Flags.setSExt(); |
| 5669 | if (Args[i].isInReg) |
| 5670 | Flags.setInReg(); |
| 5671 | if (Args[i].isSRet) |
| 5672 | Flags.setSRet(); |
| 5673 | if (Args[i].isByVal) { |
| 5674 | Flags.setByVal(); |
| 5675 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5676 | const Type *ElementTy = Ty->getElementType(); |
| 5677 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
| 5678 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
| 5679 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5680 | // info is not there but there are cases it cannot get right. |
| 5681 | if (Args[i].Alignment) |
| 5682 | FrameAlign = Args[i].Alignment; |
| 5683 | Flags.setByValAlign(FrameAlign); |
| 5684 | Flags.setByValSize(FrameSize); |
| 5685 | } |
| 5686 | if (Args[i].isNest) |
| 5687 | Flags.setNest(); |
| 5688 | Flags.setOrigAlign(OriginalAlignment); |
| 5689 | |
| 5690 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5691 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
| 5692 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5693 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5694 | |
| 5695 | if (Args[i].isSExt) |
| 5696 | ExtendKind = ISD::SIGN_EXTEND; |
| 5697 | else if (Args[i].isZExt) |
| 5698 | ExtendKind = ISD::ZERO_EXTEND; |
| 5699 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5700 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5701 | PartVT, ExtendKind); |
| 5702 | |
| 5703 | for (unsigned j = 0; j != NumParts; ++j) { |
| 5704 | // if it isn't first piece, alignment must be 1 |
| 5705 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5706 | if (NumParts > 1 && j == 0) |
| 5707 | MyFlags.Flags.setSplit(); |
| 5708 | else if (j != 0) |
| 5709 | MyFlags.Flags.setOrigAlign(1); |
| 5710 | |
| 5711 | Outs.push_back(MyFlags); |
| 5712 | } |
| 5713 | } |
| 5714 | } |
| 5715 | |
| 5716 | // Handle the incoming return values from the call. |
| 5717 | SmallVector<ISD::InputArg, 32> Ins; |
| 5718 | SmallVector<EVT, 4> RetTys; |
| 5719 | ComputeValueVTs(*this, RetTy, RetTys); |
| 5720 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5721 | EVT VT = RetTys[I]; |
| 5722 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5723 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
| 5724 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5725 | ISD::InputArg MyFlags; |
| 5726 | MyFlags.VT = RegisterVT; |
| 5727 | MyFlags.Used = isReturnValueUsed; |
| 5728 | if (RetSExt) |
| 5729 | MyFlags.Flags.setSExt(); |
| 5730 | if (RetZExt) |
| 5731 | MyFlags.Flags.setZExt(); |
| 5732 | if (isInreg) |
| 5733 | MyFlags.Flags.setInReg(); |
| 5734 | Ins.push_back(MyFlags); |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | SmallVector<SDValue, 4> InVals; |
| 5739 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
| 5740 | Outs, Ins, dl, DAG, InVals); |
| 5741 | |
| 5742 | // Verify that the target's LowerCall behaved as expected. |
| 5743 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
| 5744 | "LowerCall didn't return a valid chain!"); |
| 5745 | assert((!isTailCall || InVals.empty()) && |
| 5746 | "LowerCall emitted a return value for a tail call!"); |
| 5747 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5748 | "LowerCall didn't emit the correct number of values!"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5749 | |
| 5750 | // For a tail call, the return value is merely live-out and there aren't |
| 5751 | // any nodes in the DAG representing it. Return a special value to |
| 5752 | // indicate that a tail call has been emitted and no more Instructions |
| 5753 | // should be processed in the current block. |
| 5754 | if (isTailCall) { |
| 5755 | DAG.setRoot(Chain); |
| 5756 | return std::make_pair(SDValue(), SDValue()); |
| 5757 | } |
| 5758 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5759 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5760 | assert(InVals[i].getNode() && |
| 5761 | "LowerCall emitted a null value!"); |
| 5762 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5763 | "LowerCall emitted a value with the wrong type!"); |
| 5764 | }); |
| 5765 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5766 | // Collect the legal value parts into potentially illegal values |
| 5767 | // that correspond to the original function's return values. |
| 5768 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5769 | if (RetSExt) |
| 5770 | AssertOp = ISD::AssertSext; |
| 5771 | else if (RetZExt) |
| 5772 | AssertOp = ISD::AssertZext; |
| 5773 | SmallVector<SDValue, 4> ReturnValues; |
| 5774 | unsigned CurReg = 0; |
| 5775 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5776 | EVT VT = RetTys[I]; |
| 5777 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5778 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
| 5779 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5780 | ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg], |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5781 | NumRegs, RegisterVT, VT, |
| 5782 | AssertOp)); |
| 5783 | CurReg += NumRegs; |
| 5784 | } |
| 5785 | |
| 5786 | // For a function returning void, there is no return value. We can't create |
| 5787 | // such a node, so we just return a null return value in that case. In |
| 5788 | // that case, nothing will actualy look at the value. |
| 5789 | if (ReturnValues.empty()) |
| 5790 | return std::make_pair(SDValue(), Chain); |
| 5791 | |
| 5792 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5793 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5794 | &ReturnValues[0], ReturnValues.size()); |
| 5795 | return std::make_pair(Res, Chain); |
| 5796 | } |
| 5797 | |
| 5798 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5799 | SmallVectorImpl<SDValue> &Results, |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5800 | SelectionDAG &DAG) const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5801 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
| 5802 | if (Res.getNode()) |
| 5803 | Results.push_back(Res); |
| 5804 | } |
| 5805 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5806 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5807 | llvm_unreachable("LowerOperation not implemented for this target!"); |
| 5808 | return SDValue(); |
| 5809 | } |
| 5810 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5811 | void |
| 5812 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5813 | SDValue Op = getValue(V); |
| 5814 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5815 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5816 | "Copy from a reg to the same reg!"); |
| 5817 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5818 | |
| 5819 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
| 5820 | SDValue Chain = DAG.getEntryNode(); |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5821 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5822 | PendingExports.push_back(Chain); |
| 5823 | } |
| 5824 | |
| 5825 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5826 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5827 | void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5828 | // If this is the entry block, emit arguments. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5829 | const Function &F = *LLVMBB->getParent(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5830 | SelectionDAG &DAG = SDB->DAG; |
| 5831 | SDValue OldRoot = DAG.getRoot(); |
| 5832 | DebugLoc dl = SDB->getCurDebugLoc(); |
| 5833 | const TargetData *TD = TLI.getTargetData(); |
| 5834 | SmallVector<ISD::InputArg, 16> Ins; |
| 5835 | |
| 5836 | // Check whether the function can return without sret-demotion. |
| 5837 | SmallVector<EVT, 4> OutVTs; |
| 5838 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 5839 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
| 5840 | OutVTs, OutsFlags, TLI); |
| 5841 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 5842 | |
| 5843 | FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), |
| 5844 | OutVTs, OutsFlags, DAG); |
| 5845 | if (!FLI.CanLowerReturn) { |
| 5846 | // Put in an sret pointer parameter before all the other parameters. |
| 5847 | SmallVector<EVT, 1> ValueVTs; |
| 5848 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5849 | |
| 5850 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5851 | // or one register. |
| 5852 | ISD::ArgFlagsTy Flags; |
| 5853 | Flags.setSRet(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5854 | EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5855 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5856 | Ins.push_back(RetArg); |
| 5857 | } |
| 5858 | |
| 5859 | // Set up the incoming argument description vector. |
| 5860 | unsigned Idx = 1; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5861 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5862 | I != E; ++I, ++Idx) { |
| 5863 | SmallVector<EVT, 4> ValueVTs; |
| 5864 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5865 | bool isArgValueUsed = !I->use_empty(); |
| 5866 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5867 | Value != NumValues; ++Value) { |
| 5868 | EVT VT = ValueVTs[Value]; |
| 5869 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
| 5870 | ISD::ArgFlagsTy Flags; |
| 5871 | unsigned OriginalAlignment = |
| 5872 | TD->getABITypeAlignment(ArgTy); |
| 5873 | |
| 5874 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5875 | Flags.setZExt(); |
| 5876 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5877 | Flags.setSExt(); |
| 5878 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5879 | Flags.setInReg(); |
| 5880 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5881 | Flags.setSRet(); |
| 5882 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5883 | Flags.setByVal(); |
| 5884 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5885 | const Type *ElementTy = Ty->getElementType(); |
| 5886 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5887 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5888 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5889 | // this info is not there but there are cases it cannot get right. |
| 5890 | if (F.getParamAlignment(Idx)) |
| 5891 | FrameAlign = F.getParamAlignment(Idx); |
| 5892 | Flags.setByValAlign(FrameAlign); |
| 5893 | Flags.setByValSize(FrameSize); |
| 5894 | } |
| 5895 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5896 | Flags.setNest(); |
| 5897 | Flags.setOrigAlign(OriginalAlignment); |
| 5898 | |
| 5899 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5900 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
| 5901 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5902 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5903 | if (NumRegs > 1 && i == 0) |
| 5904 | MyFlags.Flags.setSplit(); |
| 5905 | // if it isn't first piece, alignment must be 1 |
| 5906 | else if (i > 0) |
| 5907 | MyFlags.Flags.setOrigAlign(1); |
| 5908 | Ins.push_back(MyFlags); |
| 5909 | } |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | // Call the target to set up the argument values. |
| 5914 | SmallVector<SDValue, 8> InVals; |
| 5915 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5916 | F.isVarArg(), Ins, |
| 5917 | dl, DAG, InVals); |
| 5918 | |
| 5919 | // Verify that the target's LowerFormalArguments behaved as expected. |
| 5920 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
| 5921 | "LowerFormalArguments didn't return a valid chain!"); |
| 5922 | assert(InVals.size() == Ins.size() && |
| 5923 | "LowerFormalArguments didn't emit the correct number of values!"); |
| 5924 | DEBUG({ |
| 5925 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5926 | assert(InVals[i].getNode() && |
| 5927 | "LowerFormalArguments emitted a null value!"); |
| 5928 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5929 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5930 | } |
| 5931 | }); |
| 5932 | |
| 5933 | // Update the DAG with the new chain value resulting from argument lowering. |
| 5934 | DAG.setRoot(NewRoot); |
| 5935 | |
| 5936 | // Set up the argument values. |
| 5937 | unsigned i = 0; |
| 5938 | Idx = 1; |
| 5939 | if (!FLI.CanLowerReturn) { |
| 5940 | // Create a virtual register for the sret pointer, and put in a copy |
| 5941 | // from the sret argument into it. |
| 5942 | SmallVector<EVT, 1> ValueVTs; |
| 5943 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5944 | EVT VT = ValueVTs[0]; |
| 5945 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5946 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5947 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5948 | RegVT, VT, AssertOp); |
| 5949 | |
| 5950 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
| 5951 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 5952 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
| 5953 | FLI.DemoteRegister = SRetReg; |
| 5954 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 5955 | SRetReg, ArgValue); |
| 5956 | DAG.setRoot(NewRoot); |
| 5957 | |
| 5958 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 5959 | // Idx indexes LLVM arguments. Don't touch it. |
| 5960 | ++i; |
| 5961 | } |
| 5962 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5963 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5964 | ++I, ++Idx) { |
| 5965 | SmallVector<SDValue, 4> ArgValues; |
| 5966 | SmallVector<EVT, 4> ValueVTs; |
| 5967 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5968 | unsigned NumValues = ValueVTs.size(); |
| 5969 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
| 5970 | EVT VT = ValueVTs[Value]; |
| 5971 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5972 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
| 5973 | |
| 5974 | if (!I->use_empty()) { |
| 5975 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5976 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5977 | AssertOp = ISD::AssertSext; |
| 5978 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5979 | AssertOp = ISD::AssertZext; |
| 5980 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 5981 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5982 | NumParts, PartVT, VT, |
| 5983 | AssertOp)); |
| 5984 | } |
| 5985 | |
| 5986 | i += NumParts; |
| 5987 | } |
| 5988 | |
| 5989 | if (!I->use_empty()) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 5990 | SDValue Res; |
| 5991 | if (!ArgValues.empty()) |
| 5992 | Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 5993 | SDB->getCurDebugLoc()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 5994 | SDB->setValue(I, Res); |
| 5995 | |
| 5996 | // If this argument is live outside of the entry block, insert a copy from |
| 5997 | // whereever we got it to the vreg that other BB's will reference it as. |
| 5998 | SDB->CopyToExportRegsIfNeeded(I); |
| 5999 | } |
| 6000 | } |
| 6001 | |
| 6002 | assert(i == InVals.size() && "Argument register count mismatch!"); |
| 6003 | |
| 6004 | // Finally, if the target has anything special to do, allow it to do so. |
| 6005 | // FIXME: this should insert code into the DAG! |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6006 | EmitFunctionEntryCode(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6010 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6011 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6012 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6013 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6014 | /// the end. |
| 6015 | /// |
| 6016 | void |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6017 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
| 6018 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6019 | |
| 6020 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6021 | |
| 6022 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6023 | // from this block. |
| 6024 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6025 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6026 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6027 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6028 | |
| 6029 | // If this terminator has multiple identical successors (common for |
| 6030 | // switches), only handle each succ once. |
| 6031 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 6032 | |
| 6033 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6034 | |
| 6035 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6036 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6037 | // emitted yet. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6038 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6039 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6040 | // Ignore dead phi's. |
| 6041 | if (PN->use_empty()) continue; |
| 6042 | |
| 6043 | unsigned Reg; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6044 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6045 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6046 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 6047 | unsigned &RegOut = ConstantsOut[C]; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6048 | if (RegOut == 0) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6049 | RegOut = FuncInfo.CreateRegForValue(C); |
| 6050 | CopyValueToVirtualRegister(C, RegOut); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6051 | } |
| 6052 | Reg = RegOut; |
| 6053 | } else { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6054 | Reg = FuncInfo.ValueMap[PHIOp]; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6055 | if (Reg == 0) { |
| 6056 | assert(isa<AllocaInst>(PHIOp) && |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6057 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6058 | "Didn't codegen value into a register!??"); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6059 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
| 6060 | CopyValueToVirtualRegister(PHIOp, Reg); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | // Remember that this register needs to added to the machine PHI node as |
| 6065 | // the input for this MBB. |
| 6066 | SmallVector<EVT, 4> ValueVTs; |
| 6067 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6068 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 6069 | EVT VT = ValueVTs[vti]; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6070 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6071 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6072 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6073 | Reg += NumRegisters; |
| 6074 | } |
| 6075 | } |
| 6076 | } |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 6077 | ConstantsOut.clear(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 6078 | } |