Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 15 | #include "SelectionDAGBuilder.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 16 | #include "FunctionLoweringInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/CallingConv.h" |
| 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Function.h" |
| 25 | #include "llvm/GlobalVariable.h" |
| 26 | #include "llvm/InlineAsm.h" |
| 27 | #include "llvm/Instructions.h" |
| 28 | #include "llvm/Intrinsics.h" |
| 29 | #include "llvm/IntrinsicInst.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 30 | #include "llvm/Module.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/FastISel.h" |
| 32 | #include "llvm/CodeGen/GCStrategy.h" |
| 33 | #include "llvm/CodeGen/GCMetadata.h" |
| 34 | #include "llvm/CodeGen/MachineFunction.h" |
| 35 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 36 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 37 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 38 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 39 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/DwarfWriter.h" |
| 43 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetRegisterInfo.h" |
| 45 | #include "llvm/Target/TargetData.h" |
| 46 | #include "llvm/Target/TargetFrameInfo.h" |
| 47 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetOptions.h" |
| 51 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 54 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 57 | #include <algorithm> |
| 58 | using namespace llvm; |
| 59 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 60 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 61 | /// some float libcalls (6, 8 or 12 bits). |
| 62 | static unsigned LimitFloatPrecision; |
| 63 | |
| 64 | static cl::opt<unsigned, true> |
| 65 | LimitFPPrecision("limit-float-precision", |
| 66 | cl::desc("Generate low-precision inline sequences " |
| 67 | "for some float libcalls"), |
| 68 | cl::location(LimitFloatPrecision), |
| 69 | cl::init(0)); |
| 70 | |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 71 | namespace { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 72 | /// RegsForValue - This struct represents the registers (physical or virtual) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 73 | /// that a particular set of values is assigned, and the type information |
| 74 | /// about the value. The most common situation is to represent one value at a |
| 75 | /// time, but struct or array values are handled element-wise as multiple |
| 76 | /// values. The splitting of aggregates is performed recursively, so that we |
| 77 | /// never have aggregate-typed registers. The values at this point do not |
| 78 | /// necessarily have legal types, so each value may require one or more |
| 79 | /// registers of some legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 80 | /// |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 81 | struct RegsForValue { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 82 | /// TLI - The TargetLowering object. |
| 83 | /// |
| 84 | const TargetLowering *TLI; |
| 85 | |
| 86 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 87 | /// may need be promoted or synthesized from one or more registers. |
| 88 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 89 | SmallVector<EVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 90 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 91 | /// RegVTs - The value types of the registers. This is the same size as |
| 92 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 93 | /// register or registers are. (Individual values are never synthesized |
| 94 | /// from more than one type of register.) |
| 95 | /// |
| 96 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 97 | /// getRegisterType member function, however when with physical registers |
| 98 | /// it is necessary to have a separate record of the types. |
| 99 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 100 | SmallVector<EVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 101 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 102 | /// Regs - This list holds the registers assigned to the values. |
| 103 | /// Each legal or promoted value requires one register, and each |
| 104 | /// expanded value requires multiple registers. |
| 105 | /// |
| 106 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 108 | RegsForValue() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 109 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 111 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 112 | EVT regvt, EVT valuevt) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 113 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 114 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 115 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 116 | const SmallVector<EVT, 4> ®vts, |
| 117 | const SmallVector<EVT, 4> &valuevts) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 118 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 119 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 120 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 121 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 122 | |
| 123 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 124 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 125 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 126 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 127 | for (unsigned i = 0; i != NumRegs; ++i) |
| 128 | Regs.push_back(Reg + i); |
| 129 | RegVTs.push_back(RegisterVT); |
| 130 | Reg += NumRegs; |
| 131 | } |
| 132 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 133 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 134 | /// append - Add the specified values to this one. |
| 135 | void append(const RegsForValue &RHS) { |
| 136 | TLI = RHS.TLI; |
| 137 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 138 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 139 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 140 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 141 | |
| 142 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 143 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 144 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 145 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 146 | /// If the Flag pointer is NULL, no flag is used. |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 147 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, unsigned Order, |
| 148 | SDValue &Chain, SDValue *Flag) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 149 | |
| 150 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 151 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 152 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 153 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 154 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 155 | unsigned Order, SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 157 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 158 | /// operand list. This adds the code marker, matching input operand index |
| 159 | /// (if applicable), and includes the number of values added into it. |
| 160 | void AddInlineAsmOperands(unsigned Code, |
| 161 | bool HasMatching, unsigned MatchingIdx, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 162 | SelectionDAG &DAG, unsigned Order, |
| 163 | std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 164 | }; |
| 165 | } |
| 166 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 167 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 168 | /// combined into the value they represent. If the parts combine to a type |
| 169 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 170 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 171 | /// (ISD::AssertSext). |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 172 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 173 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 174 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 175 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 176 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 177 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | SDValue Val = Parts[0]; |
| 179 | |
| 180 | if (NumParts > 1) { |
| 181 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 182 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 183 | unsigned PartBits = PartVT.getSizeInBits(); |
| 184 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 185 | |
| 186 | // Assemble the power of 2 part. |
| 187 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 188 | 1 << Log2_32(NumParts) : NumParts; |
| 189 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 190 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 191 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 192 | SDValue Lo, Hi; |
| 193 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 194 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 195 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | if (RoundParts > 2) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 197 | Lo = getCopyFromParts(DAG, dl, Order, Parts, RoundParts / 2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | PartVT, HalfVT); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 199 | Hi = getCopyFromParts(DAG, dl, Order, Parts + RoundParts / 2, |
| 200 | RoundParts / 2, PartVT, HalfVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 201 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 202 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 203 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 204 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 205 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 206 | if (TLI.isBigEndian()) |
| 207 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 208 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 209 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 210 | |
| 211 | if (RoundParts < NumParts) { |
| 212 | // Assemble the trailing non-power-of-2 part. |
| 213 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 214 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 215 | Hi = getCopyFromParts(DAG, dl, Order, |
| 216 | Parts + RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 217 | |
| 218 | // Combine the round and odd parts. |
| 219 | Lo = Val; |
| 220 | if (TLI.isBigEndian()) |
| 221 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 222 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 223 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 224 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 225 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 226 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 227 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 228 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 229 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 230 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 231 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 232 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 233 | unsigned NumIntermediates; |
| 234 | unsigned NumRegs = |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 235 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 236 | NumIntermediates, RegisterVT); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 237 | assert(NumRegs == NumParts |
| 238 | && "Part count doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 239 | NumParts = NumRegs; // Silence a compiler warning. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 240 | assert(RegisterVT == PartVT |
| 241 | && "Part type doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 242 | assert(RegisterVT == Parts[0].getValueType() && |
| 243 | "Part type doesn't match part!"); |
| 244 | |
| 245 | // Assemble the parts into intermediate operands. |
| 246 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 247 | if (NumIntermediates == NumParts) { |
| 248 | // If the register was not expanded, truncate or copy the value, |
| 249 | // as appropriate. |
| 250 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 251 | Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 252 | PartVT, IntermediateVT); |
| 253 | } else if (NumParts > 0) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 254 | // If the intermediate type was expanded, build the intermediate |
| 255 | // operands from the parts. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 256 | assert(NumParts % NumIntermediates == 0 && |
| 257 | "Must expand into a divisible number of parts!"); |
| 258 | unsigned Factor = NumParts / NumIntermediates; |
| 259 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 260 | Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 261 | PartVT, IntermediateVT); |
| 262 | } |
| 263 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 264 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 265 | // intermediate operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 266 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 267 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 268 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 269 | } else if (PartVT.isFloatingPoint()) { |
| 270 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 271 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 272 | "Unexpected split"); |
| 273 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 274 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 275 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 276 | if (TLI.isBigEndian()) |
| 277 | std::swap(Lo, Hi); |
| 278 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 279 | } else { |
| 280 | // FP split into integer parts (soft fp) |
| 281 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 282 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 283 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 284 | Val = getCopyFromParts(DAG, dl, Order, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 289 | PartVT = Val.getValueType(); |
| 290 | |
| 291 | if (PartVT == ValueVT) |
| 292 | return Val; |
| 293 | |
| 294 | if (PartVT.isVector()) { |
| 295 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 296 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (ValueVT.isVector()) { |
| 300 | assert(ValueVT.getVectorElementType() == PartVT && |
| 301 | ValueVT.getVectorNumElements() == 1 && |
| 302 | "Only trivial scalar-to-vector conversions should get here!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 303 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (PartVT.isInteger() && |
| 307 | ValueVT.isInteger()) { |
| 308 | if (ValueVT.bitsLT(PartVT)) { |
| 309 | // For a truncate, see if we have any information to |
| 310 | // indicate whether the truncated bits will always be |
| 311 | // zero or sign-extension. |
| 312 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 313 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 314 | DAG.getValueType(ValueVT)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 315 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 316 | } else { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 317 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 322 | if (ValueVT.bitsLT(Val.getValueType())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 323 | // FP_ROUND's are always exact here. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 324 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 325 | DAG.getIntPtrConstant(1)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 328 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 331 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 332 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 333 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 334 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 335 | return SDValue(); |
| 336 | } |
| 337 | |
| 338 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 339 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 340 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 341 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order, |
| 342 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 343 | EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 344 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 345 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 346 | EVT PtrVT = TLI.getPointerTy(); |
| 347 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 348 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 349 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 350 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 351 | |
| 352 | if (!NumParts) |
| 353 | return; |
| 354 | |
| 355 | if (!ValueVT.isVector()) { |
| 356 | if (PartVT == ValueVT) { |
| 357 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 358 | Parts[0] = Val; |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 363 | // If the parts cover more bits than the value has, promote the value. |
| 364 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 365 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 366 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 367 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 368 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 369 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 370 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 371 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 372 | } |
| 373 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 374 | // Different types of the same size. |
| 375 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 376 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 377 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 378 | // If the parts cover less bits than value has, truncate the value. |
| 379 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 380 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 381 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 382 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 383 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | // The value may have changed - recompute ValueVT. |
| 388 | ValueVT = Val.getValueType(); |
| 389 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 390 | "Failed to tile the value with PartVT!"); |
| 391 | |
| 392 | if (NumParts == 1) { |
| 393 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 394 | Parts[0] = Val; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | // Expand the value into multiple parts. |
| 399 | if (NumParts & (NumParts - 1)) { |
| 400 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 401 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 402 | "Do not know what to expand to!"); |
| 403 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 404 | unsigned RoundBits = RoundParts * PartBits; |
| 405 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 406 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 407 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 408 | TLI.getPointerTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 409 | getCopyToParts(DAG, dl, Order, OddVal, Parts + RoundParts, |
| 410 | OddParts, PartVT); |
| 411 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 412 | if (TLI.isBigEndian()) |
| 413 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 414 | std::reverse(Parts + RoundParts, Parts + NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 415 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 416 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 417 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 418 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 422 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 423 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 424 | EVT::getIntegerVT(*DAG.getContext(), |
| 425 | ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 426 | Val); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 427 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 428 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 429 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 430 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 431 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 432 | SDValue &Part0 = Parts[i]; |
| 433 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 434 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 435 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 436 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 437 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 438 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 439 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 440 | DAG.getConstant(0, PtrVT)); |
| 441 | |
| 442 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 443 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 444 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 445 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 446 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 452 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 453 | |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | // Vector ValueVT. |
| 458 | if (NumParts == 1) { |
| 459 | if (PartVT != ValueVT) { |
Bob Wilson | 5afffae | 2009-12-18 01:03:29 +0000 | [diff] [blame] | 460 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 461 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 462 | } else { |
| 463 | assert(ValueVT.getVectorElementType() == PartVT && |
| 464 | ValueVT.getVectorNumElements() == 1 && |
| 465 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 466 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 467 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 468 | DAG.getConstant(0, PtrVT)); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | Parts[0] = Val; |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 477 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 478 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 479 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 480 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 481 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 482 | |
| 483 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 484 | NumParts = NumRegs; // Silence a compiler warning. |
| 485 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 486 | |
| 487 | // Split the vector into intermediate operands. |
| 488 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 489 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 490 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 491 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 492 | IntermediateVT, Val, |
| 493 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 494 | PtrVT)); |
| 495 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 496 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 497 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 498 | DAG.getConstant(i, PtrVT)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 501 | // Split the intermediate operands into legal parts. |
| 502 | if (NumParts == NumIntermediates) { |
| 503 | // If the register was not expanded, promote or copy the value, |
| 504 | // as appropriate. |
| 505 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 506 | getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 507 | } else if (NumParts > 0) { |
| 508 | // If the intermediate type was expanded, split each the value into |
| 509 | // legal parts. |
| 510 | assert(NumParts % NumIntermediates == 0 && |
| 511 | "Must expand into a divisible number of parts!"); |
| 512 | unsigned Factor = NumParts / NumIntermediates; |
| 513 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 514 | getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 519 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 520 | AA = &aa; |
| 521 | GFI = gfi; |
| 522 | TD = DAG.getTarget().getTargetData(); |
| 523 | } |
| 524 | |
| 525 | /// clear - Clear out the curret SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 526 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 527 | /// for a new block. This doesn't clear out information about |
| 528 | /// additional blocks that are needed to complete switch lowering |
| 529 | /// or PHI node updating; that information is cleared out as it is |
| 530 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 531 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | NodeMap.clear(); |
| 533 | PendingLoads.clear(); |
| 534 | PendingExports.clear(); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 535 | EdgeMapping.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 536 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 537 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 538 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 542 | /// flushing any PendingLoad items. This must be done before emitting |
| 543 | /// a store or any other node that may need to be ordered after any |
| 544 | /// prior load instructions. |
| 545 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 546 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 547 | if (PendingLoads.empty()) |
| 548 | return DAG.getRoot(); |
| 549 | |
| 550 | if (PendingLoads.size() == 1) { |
| 551 | SDValue Root = PendingLoads[0]; |
| 552 | DAG.setRoot(Root); |
| 553 | PendingLoads.clear(); |
| 554 | return Root; |
| 555 | } |
| 556 | |
| 557 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 558 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 559 | &PendingLoads[0], PendingLoads.size()); |
| 560 | PendingLoads.clear(); |
| 561 | DAG.setRoot(Root); |
| 562 | return Root; |
| 563 | } |
| 564 | |
| 565 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 566 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 567 | /// to do this before emitting a terminator instruction. |
| 568 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 569 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 570 | SDValue Root = DAG.getRoot(); |
| 571 | |
| 572 | if (PendingExports.empty()) |
| 573 | return Root; |
| 574 | |
| 575 | // Turn all of the CopyToReg chains into one factored node. |
| 576 | if (Root.getOpcode() != ISD::EntryToken) { |
| 577 | unsigned i = 0, e = PendingExports.size(); |
| 578 | for (; i != e; ++i) { |
| 579 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 580 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 581 | break; // Don't add the root if we already indirectly depend on it. |
| 582 | } |
| 583 | |
| 584 | if (i == e) |
| 585 | PendingExports.push_back(Root); |
| 586 | } |
| 587 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 588 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 589 | &PendingExports[0], |
| 590 | PendingExports.size()); |
| 591 | PendingExports.clear(); |
| 592 | DAG.setRoot(Root); |
| 593 | return Root; |
| 594 | } |
| 595 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 596 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 597 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 598 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 599 | |
| 600 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 601 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 602 | } |
| 603 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 604 | void SelectionDAGBuilder::visit(Instruction &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 605 | visit(I.getOpcode(), I); |
| 606 | } |
| 607 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 608 | void SelectionDAGBuilder::visit(unsigned Opcode, User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 609 | // Note: this doesn't use InstVisitor, because it has to work with |
| 610 | // ConstantExpr's in addition to instructions. |
| 611 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 612 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 613 | // Build the switch statement using the Instruction.def file. |
| 614 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 615 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 616 | #include "llvm/Instruction.def" |
| 617 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 618 | |
| 619 | // Assign the ordering to the freshly created DAG nodes. |
| 620 | if (NodeMap.count(&I)) { |
| 621 | ++SDNodeOrder; |
| 622 | AssignOrderingToNode(getValue(&I).getNode()); |
| 623 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 624 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 625 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 626 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 627 | SDValue &N = NodeMap[V]; |
| 628 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 629 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 630 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 631 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 632 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 633 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 634 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 635 | |
| 636 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 637 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 638 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 639 | if (isa<ConstantPointerNull>(C)) |
| 640 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 641 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 642 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 643 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 644 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 645 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 646 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 647 | |
| 648 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 649 | visit(CE->getOpcode(), *CE); |
| 650 | SDValue N1 = NodeMap[V]; |
| 651 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 652 | return N1; |
| 653 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 654 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 655 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 656 | SmallVector<SDValue, 4> Constants; |
| 657 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 658 | OI != OE; ++OI) { |
| 659 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 660 | // If the operand is an empty aggregate, there are no values. |
| 661 | if (!Val) continue; |
| 662 | // Add each leaf value from the operand to the Constants list |
| 663 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 665 | Constants.push_back(SDValue(Val, i)); |
| 666 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 667 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 668 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 669 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 673 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 674 | "Unknown struct or array constant!"); |
| 675 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 676 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 677 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 678 | unsigned NumElts = ValueVTs.size(); |
| 679 | if (NumElts == 0) |
| 680 | return SDValue(); // empty struct |
| 681 | SmallVector<SDValue, 4> Constants(NumElts); |
| 682 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 683 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 684 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 685 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 686 | else if (EltVT.isFloatingPoint()) |
| 687 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 688 | else |
| 689 | Constants[i] = DAG.getConstant(0, EltVT); |
| 690 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 691 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 692 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 693 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 696 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 697 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 698 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 699 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 700 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 701 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 702 | // Now that we know the number and type of the elements, get that number of |
| 703 | // elements into the Ops array based on what kind of constant it is. |
| 704 | SmallVector<SDValue, 16> Ops; |
| 705 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 706 | for (unsigned i = 0; i != NumElements; ++i) |
| 707 | Ops.push_back(getValue(CP->getOperand(i))); |
| 708 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 709 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 710 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 711 | |
| 712 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 713 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 714 | Op = DAG.getConstantFP(0, EltVT); |
| 715 | else |
| 716 | Op = DAG.getConstant(0, EltVT); |
| 717 | Ops.assign(NumElements, Op); |
| 718 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 719 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 720 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 721 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 722 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 723 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 724 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 725 | // If this is a static alloca, generate it as the frameindex instead of |
| 726 | // computation. |
| 727 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 728 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 729 | FuncInfo.StaticAllocaMap.find(AI); |
| 730 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 731 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 732 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 733 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 734 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 735 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 736 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 737 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 738 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 739 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), |
| 740 | SDNodeOrder, Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 743 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 744 | /// type of the given function. This does not require a DAG or a return value, |
| 745 | /// and is suitable for use before any DAGs for the function are constructed. |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 746 | static void getReturnInfo(const Type* ReturnType, |
| 747 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 748 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 749 | TargetLowering &TLI, |
| 750 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 751 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 752 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 753 | unsigned NumValues = ValueVTs.size(); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 754 | if (NumValues == 0) return; |
| 755 | unsigned Offset = 0; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 756 | |
| 757 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 758 | EVT VT = ValueVTs[j]; |
| 759 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 760 | |
| 761 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 762 | ExtendKind = ISD::SIGN_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 763 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 764 | ExtendKind = ISD::ZERO_EXTEND; |
| 765 | |
| 766 | // FIXME: C calling convention requires the return type to be promoted to |
| 767 | // at least 32-bit. But this is not necessary for non-C calling |
| 768 | // conventions. The frontend should mark functions whose return values |
| 769 | // require promoting with signext or zeroext attributes. |
| 770 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 771 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 772 | if (VT.bitsLT(MinVT)) |
| 773 | VT = MinVT; |
| 774 | } |
| 775 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 776 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 777 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 778 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 779 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 780 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 781 | // 'inreg' on function refers to return value |
| 782 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 783 | if (attr & Attribute::InReg) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 784 | Flags.setInReg(); |
| 785 | |
| 786 | // Propagate extension type if any |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 787 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 788 | Flags.setSExt(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 789 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 790 | Flags.setZExt(); |
| 791 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 792 | for (unsigned i = 0; i < NumParts; ++i) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 793 | OutVTs.push_back(PartVT); |
| 794 | OutFlags.push_back(Flags); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 795 | if (Offsets) |
| 796 | { |
| 797 | Offsets->push_back(Offset); |
| 798 | Offset += PartSize; |
| 799 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 803 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 804 | void SelectionDAGBuilder::visitRet(ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 805 | SDValue Chain = getControlRoot(); |
| 806 | SmallVector<ISD::OutputArg, 8> Outs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 807 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 808 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 809 | if (!FLI.CanLowerReturn) { |
| 810 | unsigned DemoteReg = FLI.DemoteRegister; |
| 811 | const Function *F = I.getParent()->getParent(); |
| 812 | |
| 813 | // Emit a store of the return value through the virtual register. |
| 814 | // Leave Outs empty so that LowerReturn won't try to load return |
| 815 | // registers the usual way. |
| 816 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 817 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 818 | PtrValueVTs); |
| 819 | |
| 820 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 821 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 822 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 823 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 824 | SmallVector<uint64_t, 4> Offsets; |
| 825 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 826 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 827 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 828 | SmallVector<SDValue, 4> Chains(NumValues); |
| 829 | EVT PtrVT = PtrValueVTs[0]; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 830 | for (unsigned i = 0; i != NumValues; ++i) { |
| 831 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 832 | DAG.getConstant(Offsets[i], PtrVT)); |
| 833 | Chains[i] = |
| 834 | DAG.getStore(Chain, getCurDebugLoc(), |
| 835 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
| 836 | Add, NULL, Offsets[i], false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 839 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 840 | MVT::Other, &Chains[0], NumValues); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 841 | } else { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 842 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 843 | SmallVector<EVT, 4> ValueVTs; |
| 844 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
| 845 | unsigned NumValues = ValueVTs.size(); |
| 846 | if (NumValues == 0) continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 847 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 848 | SDValue RetOp = getValue(I.getOperand(i)); |
| 849 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 850 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 851 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 852 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 853 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 854 | const Function *F = I.getParent()->getParent(); |
| 855 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 856 | ExtendKind = ISD::SIGN_EXTEND; |
| 857 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 858 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 860 | // FIXME: C calling convention requires the return type to be promoted |
| 861 | // to at least 32-bit. But this is not necessary for non-C calling |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 862 | // conventions. The frontend should mark functions whose return values |
| 863 | // require promoting with signext or zeroext attributes. |
| 864 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 865 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 866 | if (VT.bitsLT(MinVT)) |
| 867 | VT = MinVT; |
| 868 | } |
| 869 | |
| 870 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 871 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 872 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 873 | getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 874 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 875 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 876 | |
| 877 | // 'inreg' on function refers to return value |
| 878 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 879 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 880 | Flags.setInReg(); |
| 881 | |
| 882 | // Propagate extension type if any |
| 883 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 884 | Flags.setSExt(); |
| 885 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 886 | Flags.setZExt(); |
| 887 | |
| 888 | for (unsigned i = 0; i < NumParts; ++i) |
| 889 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 890 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 893 | |
| 894 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 895 | CallingConv::ID CallConv = |
| 896 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 897 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 898 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 899 | |
| 900 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 901 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 902 | "LowerReturn didn't return a valid chain!"); |
| 903 | |
| 904 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 905 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 908 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 909 | /// created for it, emit nodes to copy the value into the virtual |
| 910 | /// registers. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 911 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) { |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 912 | if (!V->use_empty()) { |
| 913 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 914 | if (VMI != FuncInfo.ValueMap.end()) |
| 915 | CopyValueToVirtualRegister(V, VMI->second); |
| 916 | } |
| 917 | } |
| 918 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 919 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 920 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 921 | /// CopyTo/FromReg. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 922 | void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 923 | // No need to export constants. |
| 924 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 925 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 926 | // Already exported? |
| 927 | if (FuncInfo.isExportedInst(V)) return; |
| 928 | |
| 929 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 930 | CopyValueToVirtualRegister(V, Reg); |
| 931 | } |
| 932 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 933 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V, |
| 934 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 935 | // The operands of the setcc have to be in this block. We don't know |
| 936 | // how to export them from some other block. |
| 937 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 938 | // Can export from current BB. |
| 939 | if (VI->getParent() == FromBB) |
| 940 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 941 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 942 | // Is already exported, noop. |
| 943 | return FuncInfo.isExportedInst(V); |
| 944 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 945 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 946 | // If this is an argument, we can export it if the BB is the entry block or |
| 947 | // if it is already exported. |
| 948 | if (isa<Argument>(V)) { |
| 949 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 950 | return true; |
| 951 | |
| 952 | // Otherwise, can only export this if it is already exported. |
| 953 | return FuncInfo.isExportedInst(V); |
| 954 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 955 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 956 | // Otherwise, constants can always be exported. |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 961 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 962 | return I->getParent() == BB; |
| 963 | return true; |
| 964 | } |
| 965 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 966 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 967 | /// the given LLVM IR floating-point condition code. This includes |
| 968 | /// consideration of global floating-point math flags. |
| 969 | /// |
| 970 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 971 | ISD::CondCode FPC, FOC; |
| 972 | switch (Pred) { |
| 973 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 974 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 975 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 976 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 977 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 978 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 979 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 980 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 981 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 982 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 983 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 984 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 985 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 986 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 987 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 988 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 989 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 990 | llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 991 | FOC = FPC = ISD::SETFALSE; |
| 992 | break; |
| 993 | } |
| 994 | if (FiniteOnlyFPMath()) |
| 995 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 996 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 997 | return FPC; |
| 998 | } |
| 999 | |
| 1000 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1001 | /// the given LLVM IR integer condition code. |
| 1002 | /// |
| 1003 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1004 | switch (Pred) { |
| 1005 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1006 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1007 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1008 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1009 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1010 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1011 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1012 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1013 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1014 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1015 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1016 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1017 | return ISD::SETNE; |
| 1018 | } |
| 1019 | } |
| 1020 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1021 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1022 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1023 | /// AND operator tree. |
| 1024 | /// |
| 1025 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1026 | SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond, |
| 1027 | MachineBasicBlock *TBB, |
| 1028 | MachineBasicBlock *FBB, |
| 1029 | MachineBasicBlock *CurBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1030 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1031 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1032 | // If the leaf of the tree is a comparison, merge the condition into |
| 1033 | // the caseblock. |
| 1034 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1035 | // The operands of the cmp have to be in this block. We don't know |
| 1036 | // how to export them from some other block. If this is the first block |
| 1037 | // of the sequence, no exporting is needed. |
| 1038 | if (CurBB == CurMBB || |
| 1039 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1040 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1041 | ISD::CondCode Condition; |
| 1042 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1043 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1044 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1045 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1046 | } else { |
| 1047 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1048 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1049 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1050 | |
| 1051 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1052 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1053 | SwitchCases.push_back(CB); |
| 1054 | return; |
| 1055 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1059 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1060 | NULL, TBB, FBB, CurBB); |
| 1061 | SwitchCases.push_back(CB); |
| 1062 | } |
| 1063 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1064 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1065 | void SelectionDAGBuilder::FindMergedConditions(Value *Cond, |
| 1066 | MachineBasicBlock *TBB, |
| 1067 | MachineBasicBlock *FBB, |
| 1068 | MachineBasicBlock *CurBB, |
| 1069 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1070 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1071 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1072 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1073 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1074 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1075 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1076 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1077 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1078 | return; |
| 1079 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1080 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1081 | // Create TmpBB after CurBB. |
| 1082 | MachineFunction::iterator BBI = CurBB; |
| 1083 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1084 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1085 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1086 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1087 | if (Opc == Instruction::Or) { |
| 1088 | // Codegen X | Y as: |
| 1089 | // jmp_if_X TBB |
| 1090 | // jmp TmpBB |
| 1091 | // TmpBB: |
| 1092 | // jmp_if_Y TBB |
| 1093 | // jmp FBB |
| 1094 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1095 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1096 | // Emit the LHS condition. |
| 1097 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1098 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1099 | // Emit the RHS condition into TmpBB. |
| 1100 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1101 | } else { |
| 1102 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1103 | // Codegen X & Y as: |
| 1104 | // jmp_if_X TmpBB |
| 1105 | // jmp FBB |
| 1106 | // TmpBB: |
| 1107 | // jmp_if_Y TBB |
| 1108 | // jmp FBB |
| 1109 | // |
| 1110 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1111 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1112 | // Emit the LHS condition. |
| 1113 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1114 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1115 | // Emit the RHS condition into TmpBB. |
| 1116 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1121 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1122 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1123 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1124 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1125 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1126 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1127 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1128 | // will get folded into a single comparison, so don't emit two blocks. |
| 1129 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1130 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1131 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1132 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1133 | return false; |
| 1134 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1136 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1137 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1138 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1139 | Cases[0].CC == Cases[1].CC && |
| 1140 | isa<Constant>(Cases[0].CmpRHS) && |
| 1141 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1142 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1143 | return false; |
| 1144 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1148 | return true; |
| 1149 | } |
| 1150 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1151 | void SelectionDAGBuilder::visitBr(BranchInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1152 | // Update machine-CFG edges. |
| 1153 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1154 | |
| 1155 | // Figure out which block is immediately after the current one. |
| 1156 | MachineBasicBlock *NextBlock = 0; |
| 1157 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1158 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1159 | NextBlock = BBI; |
| 1160 | |
| 1161 | if (I.isUnconditional()) { |
| 1162 | // Update machine-CFG edges. |
| 1163 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1164 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1165 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1166 | if (Succ0MBB != NextBlock) |
| 1167 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1168 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1169 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1170 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1171 | return; |
| 1172 | } |
| 1173 | |
| 1174 | // If this condition is one of the special cases we handle, do special stuff |
| 1175 | // now. |
| 1176 | Value *CondVal = I.getCondition(); |
| 1177 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1178 | |
| 1179 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1180 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1181 | // For example, instead of something like: |
| 1182 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1183 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1184 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1185 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1186 | // or C, F |
| 1187 | // jnz foo |
| 1188 | // Emit: |
| 1189 | // cmp A, B |
| 1190 | // je foo |
| 1191 | // cmp D, E |
| 1192 | // jle foo |
| 1193 | // |
| 1194 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1195 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1196 | (BOp->getOpcode() == Instruction::And || |
| 1197 | BOp->getOpcode() == Instruction::Or)) { |
| 1198 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1199 | // If the compares in later blocks need to use values not currently |
| 1200 | // exported from this block, export them now. This block should always |
| 1201 | // be the first entry. |
| 1202 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1203 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1204 | // Allow some cases to be rejected. |
| 1205 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1206 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1207 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1208 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1209 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1210 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1211 | // Emit the branch for this block. |
| 1212 | visitSwitchCase(SwitchCases[0]); |
| 1213 | SwitchCases.erase(SwitchCases.begin()); |
| 1214 | return; |
| 1215 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1216 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1217 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1218 | // SwitchCases. |
| 1219 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1220 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1221 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1222 | SwitchCases.clear(); |
| 1223 | } |
| 1224 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1226 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1227 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1228 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1230 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1231 | // cond branch. |
| 1232 | visitSwitchCase(CB); |
| 1233 | } |
| 1234 | |
| 1235 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1236 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1237 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1238 | SDValue Cond; |
| 1239 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1240 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1243 | if (CB.CmpMHS == NULL) { |
| 1244 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1245 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1246 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1247 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1248 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1249 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1250 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1251 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1252 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1253 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1254 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1255 | } else { |
| 1256 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1257 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1258 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1259 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1260 | |
| 1261 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1262 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1263 | |
| 1264 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1265 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1266 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1267 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1268 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1269 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1270 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1271 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1272 | } |
| 1273 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1274 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1275 | // Update successor info |
| 1276 | CurMBB->addSuccessor(CB.TrueBB); |
| 1277 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1278 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1279 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1280 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1281 | MachineBasicBlock *NextBlock = 0; |
| 1282 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1283 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1284 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1286 | // If the lhs block is the next block, invert the condition so that we can |
| 1287 | // fall through to the lhs instead of the rhs block. |
| 1288 | if (CB.TrueBB == NextBlock) { |
| 1289 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1290 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1291 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1292 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1293 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1294 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1295 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1296 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1297 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1298 | // If the branch was constant folded, fix up the CFG. |
| 1299 | if (BrCond.getOpcode() == ISD::BR) { |
| 1300 | CurMBB->removeSuccessor(CB.FalseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1301 | } else { |
| 1302 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1303 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1304 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1305 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1306 | if (CB.FalseBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1307 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1308 | DAG.getBasicBlock(CB.FalseBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1309 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1310 | |
| 1311 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1315 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1316 | // Emit the code for the jump table |
| 1317 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1318 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1319 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1320 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1321 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1322 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1323 | MVT::Other, Index.getValue(1), |
| 1324 | Table, Index); |
| 1325 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1329 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1330 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
| 1331 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1332 | // Subtract the lowest switch case value from the value being switched on and |
| 1333 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1334 | // difference between smallest and largest cases. |
| 1335 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1336 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1337 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1338 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1339 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1340 | // The SDNode we just created, which holds the value being switched on minus |
| 1341 | // the the smallest case value, needs to be copied to a virtual register so it |
| 1342 | // can be used as an index into the jump table in a subsequent basic block. |
| 1343 | // This value may be smaller or larger than the target's pointer type, and |
| 1344 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1345 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1346 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1347 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1348 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1349 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1350 | JT.Reg = JumpTableReg; |
| 1351 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1352 | // Emit the range check for the jump table, and branch to the default block |
| 1353 | // for the switch statement if the value being switched on exceeds the largest |
| 1354 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1355 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1356 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1357 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1358 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1361 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1362 | MachineBasicBlock *NextBlock = 0; |
| 1363 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1364 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1365 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1366 | NextBlock = BBI; |
| 1367 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1368 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1369 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1370 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1371 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1372 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1373 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1374 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1375 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1376 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1380 | /// suitable for "bit tests" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1381 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1382 | // Subtract the minimum value |
| 1383 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1384 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1385 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1386 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1389 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1390 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1391 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1392 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1393 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1394 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1395 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1396 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1397 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1398 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1399 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1402 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1403 | MachineBasicBlock *NextBlock = 0; |
| 1404 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1405 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1406 | NextBlock = BBI; |
| 1407 | |
| 1408 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1409 | |
| 1410 | CurMBB->addSuccessor(B.Default); |
| 1411 | CurMBB->addSuccessor(MBB); |
| 1412 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1413 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1414 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1415 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1416 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1417 | if (MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1418 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1419 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1420 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1421 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /// visitBitTestCase - this function produces one "bit test" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1425 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1426 | unsigned Reg, |
| 1427 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1428 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1429 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1430 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1431 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1432 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1433 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1434 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1435 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1436 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1437 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1438 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1439 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1440 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1441 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1442 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1443 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1444 | |
| 1445 | CurMBB->addSuccessor(B.TargetBB); |
| 1446 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1447 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1448 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1449 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1450 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1451 | |
| 1452 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1453 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1454 | MachineBasicBlock *NextBlock = 0; |
| 1455 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1456 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1457 | NextBlock = BBI; |
| 1458 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1459 | if (NextMBB != NextBlock) |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1460 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1461 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1462 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1463 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1466 | void SelectionDAGBuilder::visitInvoke(InvokeInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1467 | // Retrieve successors. |
| 1468 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1469 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1470 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1471 | const Value *Callee(I.getCalledValue()); |
| 1472 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1473 | visitInlineAsm(&I); |
| 1474 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1475 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1476 | |
| 1477 | // If the value of the invoke is used outside of its defining block, make it |
| 1478 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1479 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Update successor info |
| 1482 | CurMBB->addSuccessor(Return); |
| 1483 | CurMBB->addSuccessor(LandingPad); |
| 1484 | |
| 1485 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1486 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1487 | MVT::Other, getControlRoot(), |
| 1488 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1491 | void SelectionDAGBuilder::visitUnwind(UnwindInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1495 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1496 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1497 | CaseRecVector& WorkList, |
| 1498 | Value* SV, |
| 1499 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1500 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1501 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1502 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1503 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1504 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1505 | return false; |
| 1506 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1507 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1508 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1509 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Figure out which block is immediately after the current one. |
| 1512 | MachineBasicBlock *NextBlock = 0; |
| 1513 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1514 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1515 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1516 | NextBlock = BBI; |
| 1517 | |
| 1518 | // TODO: If any two of the cases has the same destination, and if one value |
| 1519 | // is the same as the other, but has one bit unset that the other has set, |
| 1520 | // use bit manipulation to do two compares at once. For example: |
| 1521 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1522 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1523 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1524 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1525 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1526 | // branches in this order. See if rearranging a case value would help. |
| 1527 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1528 | if (I->BB == NextBlock) { |
| 1529 | std::swap(*I, BackCase); |
| 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1534 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1535 | // Create a CaseBlock record representing a conditional branch to |
| 1536 | // the Case's target mbb if the value being switched on SV is equal |
| 1537 | // to C. |
| 1538 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1539 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1540 | MachineBasicBlock *FallThrough; |
| 1541 | if (I != E-1) { |
| 1542 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1543 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Put SV in a virtual register to make it available from the new blocks. |
| 1546 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | // If the last case doesn't match, go to the default block. |
| 1549 | FallThrough = Default; |
| 1550 | } |
| 1551 | |
| 1552 | Value *RHS, *LHS, *MHS; |
| 1553 | ISD::CondCode CC; |
| 1554 | if (I->High == I->Low) { |
| 1555 | // This is just small small case range :) containing exactly 1 case |
| 1556 | CC = ISD::SETEQ; |
| 1557 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1558 | } else { |
| 1559 | CC = ISD::SETLE; |
| 1560 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1561 | } |
| 1562 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1563 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1564 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1565 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1566 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1567 | // before the next MBB. |
| 1568 | if (CurBlock == CurMBB) |
| 1569 | visitSwitchCase(CB); |
| 1570 | else |
| 1571 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1572 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1573 | CurBlock = FallThrough; |
| 1574 | } |
| 1575 | |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1580 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1581 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1582 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1583 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1584 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1585 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1586 | APInt LastExt(Last), FirstExt(First); |
| 1587 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1588 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1589 | return (LastExt - FirstExt + 1ULL); |
| 1590 | } |
| 1591 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1592 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1593 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1594 | CaseRecVector& WorkList, |
| 1595 | Value* SV, |
| 1596 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1597 | Case& FrontCase = *CR.Range.first; |
| 1598 | Case& BackCase = *(CR.Range.second-1); |
| 1599 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1600 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1601 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1603 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1604 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1605 | I!=E; ++I) |
| 1606 | TSize += I->size(); |
| 1607 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1608 | if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4))) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1609 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1610 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1611 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1612 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1613 | if (Density < 0.4) |
| 1614 | return false; |
| 1615 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1616 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1617 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1618 | << "Range: " << Range |
| 1619 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1620 | |
| 1621 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1622 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1623 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1626 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1627 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1628 | |
| 1629 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1630 | |
| 1631 | // Create a new basic block to hold the code for loading the address |
| 1632 | // of the jump table, and jumping to it. Update successor information; |
| 1633 | // we will either branch to the default case for the switch, or the jump |
| 1634 | // table. |
| 1635 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1636 | CurMF->insert(BBI, JumpTableBB); |
| 1637 | CR.CaseBB->addSuccessor(Default); |
| 1638 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1639 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1640 | // Build a vector of destination BBs, corresponding to each target |
| 1641 | // of the jump table. If the value of the jump table slot corresponds to |
| 1642 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1643 | // the default BB. |
| 1644 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1645 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1646 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) { |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1647 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1648 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1649 | |
| 1650 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1651 | DestBBs.push_back(I->BB); |
| 1652 | if (TEI==High) |
| 1653 | ++I; |
| 1654 | } else { |
| 1655 | DestBBs.push_back(Default); |
| 1656 | } |
| 1657 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1658 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1659 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1660 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1661 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1662 | E = DestBBs.end(); I != E; ++I) { |
| 1663 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1664 | SuccsHandled[(*I)->getNumber()] = true; |
| 1665 | JumpTableBB->addSuccessor(*I); |
| 1666 | } |
| 1667 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1669 | // Create a jump table index for this jump table, or return an existing |
| 1670 | // one. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1671 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1672 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
| 1673 | ->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1674 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1675 | // Set the jump table information so that we can codegen it as a second |
| 1676 | // MachineBasicBlock |
| 1677 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1678 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1679 | if (CR.CaseBB == CurMBB) |
| 1680 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1681 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1682 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1683 | |
| 1684 | return true; |
| 1685 | } |
| 1686 | |
| 1687 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1688 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1689 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1690 | CaseRecVector& WorkList, |
| 1691 | Value* SV, |
| 1692 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1693 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1694 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1695 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1698 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1699 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1700 | |
| 1701 | Case& FrontCase = *CR.Range.first; |
| 1702 | Case& BackCase = *(CR.Range.second-1); |
| 1703 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1704 | |
| 1705 | // Size is the number of Cases represented by this range. |
| 1706 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1707 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1708 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1709 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1710 | double FMetric = 0; |
| 1711 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1712 | |
| 1713 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1714 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1715 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1716 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1717 | I!=E; ++I) |
| 1718 | TSize += I->size(); |
| 1719 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1720 | APInt LSize = FrontCase.size(); |
| 1721 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1722 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1723 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1724 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1725 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1726 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1727 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1728 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1729 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1730 | assert((Range - 2ULL).isNonNegative() && |
| 1731 | "Invalid case distance"); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1732 | double LDensity = (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1733 | (LEnd - First + 1ULL).roundToDouble(); |
| 1734 | double RDensity = (double)RSize.roundToDouble() / |
| 1735 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1736 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1737 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1738 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1739 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1740 | << "LDensity: " << LDensity |
| 1741 | << ", RDensity: " << RDensity << '\n' |
| 1742 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1743 | if (FMetric < Metric) { |
| 1744 | Pivot = J; |
| 1745 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1746 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | LSize += J->size(); |
| 1750 | RSize -= J->size(); |
| 1751 | } |
| 1752 | if (areJTsAllowed(TLI)) { |
| 1753 | // If our case is dense we *really* should handle it earlier! |
| 1754 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1755 | } else { |
| 1756 | Pivot = CR.Range.first + Size/2; |
| 1757 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1758 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1759 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1760 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1761 | Constant *C = Pivot->Low; |
| 1762 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1763 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1764 | // We know that we branch to the LHS if the Value being switched on is |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1765 | // less than the Pivot value, C. We use this to optimize our binary |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1766 | // tree a bit, by recognizing that if SV is greater than or equal to the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1767 | // LHS's Case Value, and that Case Value is exactly one less than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1768 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1769 | // rather than creating a leaf node for it. |
| 1770 | if ((LHSR.second - LHSR.first) == 1 && |
| 1771 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1772 | cast<ConstantInt>(C)->getValue() == |
| 1773 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1774 | TrueBB = LHSR.first->BB; |
| 1775 | } else { |
| 1776 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1777 | CurMF->insert(BBI, TrueBB); |
| 1778 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Put SV in a virtual register to make it available from the new blocks. |
| 1781 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1782 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1783 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1784 | // Similar to the optimization above, if the Value being switched on is |
| 1785 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1786 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1787 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1788 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1789 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1790 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1791 | FalseBB = RHSR.first->BB; |
| 1792 | } else { |
| 1793 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1794 | CurMF->insert(BBI, FalseBB); |
| 1795 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Put SV in a virtual register to make it available from the new blocks. |
| 1798 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1802 | // the LHS node if the value being switched on SV is less than C. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1803 | // Otherwise, branch to LHS. |
| 1804 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1805 | |
| 1806 | if (CR.CaseBB == CurMBB) |
| 1807 | visitSwitchCase(CB); |
| 1808 | else |
| 1809 | SwitchCases.push_back(CB); |
| 1810 | |
| 1811 | return true; |
| 1812 | } |
| 1813 | |
| 1814 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1815 | /// range span less, than machine word bitwidth, encode case range into series |
| 1816 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1817 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1818 | CaseRecVector& WorkList, |
| 1819 | Value* SV, |
| 1820 | MachineBasicBlock* Default){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1821 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1822 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1823 | |
| 1824 | Case& FrontCase = *CR.Range.first; |
| 1825 | Case& BackCase = *(CR.Range.second-1); |
| 1826 | |
| 1827 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1828 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1829 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1830 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1831 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1832 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1833 | return false; |
| 1834 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1835 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1836 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1837 | I!=E; ++I) { |
| 1838 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1839 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1840 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1841 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1842 | // Count unique destinations |
| 1843 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1844 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1845 | Dests.insert(I->BB); |
| 1846 | if (Dests.size() > 3) |
| 1847 | // Don't bother the code below, if there are too much unique destinations |
| 1848 | return false; |
| 1849 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1850 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1851 | << Dests.size() << '\n' |
| 1852 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1854 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1855 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1856 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1857 | APInt cmpRange = maxValue - minValue; |
| 1858 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1859 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1860 | << "Low bound: " << minValue << '\n' |
| 1861 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1862 | |
| 1863 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1864 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1865 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1866 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1867 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1868 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1869 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1870 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1871 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | // Optimize the case where all the case values fit in a |
| 1873 | // word without having to subtract minValue. In this case, |
| 1874 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1875 | if (minValue.isNonNegative() && |
| 1876 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 1877 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1878 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1879 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1880 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1881 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1882 | CaseBitsVector CasesBits; |
| 1883 | unsigned i, count = 0; |
| 1884 | |
| 1885 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1886 | MachineBasicBlock* Dest = I->BB; |
| 1887 | for (i = 0; i < count; ++i) |
| 1888 | if (Dest == CasesBits[i].BB) |
| 1889 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1890 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1891 | if (i == count) { |
| 1892 | assert((count < 3) && "Too much destinations to test!"); |
| 1893 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1894 | count++; |
| 1895 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1896 | |
| 1897 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1898 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1899 | |
| 1900 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1901 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1902 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1903 | for (uint64_t j = lo; j <= hi; j++) { |
| 1904 | CasesBits[i].Mask |= 1ULL << j; |
| 1905 | CasesBits[i].Bits++; |
| 1906 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1907 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1908 | } |
| 1909 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1910 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1911 | BitTestInfo BTC; |
| 1912 | |
| 1913 | // Figure out which block is immediately after the current one. |
| 1914 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1915 | ++BBI; |
| 1916 | |
| 1917 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1918 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1919 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1920 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1921 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1922 | << ", Bits: " << CasesBits[i].Bits |
| 1923 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1924 | |
| 1925 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1926 | CurMF->insert(BBI, CaseBB); |
| 1927 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1928 | CaseBB, |
| 1929 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Put SV in a virtual register to make it available from the new blocks. |
| 1932 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1933 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1934 | |
| 1935 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1936 | -1U, (CR.CaseBB == CurMBB), |
| 1937 | CR.CaseBB, Default, BTC); |
| 1938 | |
| 1939 | if (CR.CaseBB == CurMBB) |
| 1940 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1941 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1942 | BitTestCases.push_back(BTB); |
| 1943 | |
| 1944 | return true; |
| 1945 | } |
| 1946 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1947 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1948 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 1949 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1950 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1951 | |
| 1952 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1953 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1954 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1955 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1956 | SI.getSuccessorValue(i), |
| 1957 | SMBB)); |
| 1958 | } |
| 1959 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1960 | |
| 1961 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1962 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1963 | // Must recompute end() each iteration because it may be |
| 1964 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1965 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 1966 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 1967 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1968 | MachineBasicBlock* nextBB = J->BB; |
| 1969 | MachineBasicBlock* currentBB = I->BB; |
| 1970 | |
| 1971 | // If the two neighboring cases go to the same destination, merge them |
| 1972 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1973 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1974 | I->High = J->High; |
| 1975 | J = Cases.erase(J); |
| 1976 | } else { |
| 1977 | I = J++; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1982 | if (I->Low != I->High) |
| 1983 | // A range counts double, since it requires two compares. |
| 1984 | ++numCmps; |
| 1985 | } |
| 1986 | |
| 1987 | return numCmps; |
| 1988 | } |
| 1989 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1990 | void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1991 | // Figure out which block is immediately after the current one. |
| 1992 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1993 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 1994 | |
| 1995 | // If there is only the default destination, branch to it if it is not the |
| 1996 | // next basic block. Otherwise, just fall through. |
| 1997 | if (SI.getNumOperands() == 2) { |
| 1998 | // Update machine-CFG edges. |
| 1999 | |
| 2000 | // If this is not a fall-through branch, emit the branch. |
| 2001 | CurMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2002 | if (Default != NextBlock) |
| 2003 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 2004 | MVT::Other, getControlRoot(), |
| 2005 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2006 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2007 | return; |
| 2008 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2009 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2010 | // If there are any non-default case statements, create a vector of Cases |
| 2011 | // representing each one, and sort the vector so that we can efficiently |
| 2012 | // create a binary search tree from them. |
| 2013 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2014 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2015 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2016 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2017 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | |
| 2019 | // Get the Value to be switched on and default basic blocks, which will be |
| 2020 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2021 | // search tree. |
| 2022 | Value *SV = SI.getOperand(0); |
| 2023 | |
| 2024 | // Push the initial CaseRec onto the worklist |
| 2025 | CaseRecVector WorkList; |
| 2026 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2027 | |
| 2028 | while (!WorkList.empty()) { |
| 2029 | // Grab a record representing a case range to process off the worklist |
| 2030 | CaseRec CR = WorkList.back(); |
| 2031 | WorkList.pop_back(); |
| 2032 | |
| 2033 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2034 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2035 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2036 | // If the range has few cases (two or less) emit a series of specific |
| 2037 | // tests. |
| 2038 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2039 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2040 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2041 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2042 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2043 | // lowering the switch to a binary tree of conditional branches. |
| 2044 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2045 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2046 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2047 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2048 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2049 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2050 | } |
| 2051 | } |
| 2052 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2053 | void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) { |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2054 | // Update machine-CFG edges. |
| 2055 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
| 2056 | CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]); |
| 2057 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2058 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2059 | MVT::Other, getControlRoot(), |
| 2060 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2061 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2062 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2063 | void SelectionDAGBuilder::visitFSub(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2064 | // -0.0 - X --> fneg |
| 2065 | const Type *Ty = I.getType(); |
| 2066 | if (isa<VectorType>(Ty)) { |
| 2067 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2068 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2069 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2070 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2071 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2072 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2073 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2074 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2075 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2076 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2079 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2080 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2081 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2082 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2083 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2084 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2085 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2086 | Op2.getValueType(), Op2)); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2087 | return; |
| 2088 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2089 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2090 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2093 | void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2095 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2096 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2097 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2100 | void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2101 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2102 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2103 | if (!isa<VectorType>(I.getType()) && |
| 2104 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2105 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2106 | EVT PTy = TLI.getPointerTy(); |
| 2107 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2108 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2109 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2110 | TLI.getShiftAmountTy(), Op2); |
| 2111 | // If the operand is larger than the shift count type but the shift |
| 2112 | // count type has enough bits to represent any shift value, truncate |
| 2113 | // it now. This is a common case and it exposes the truncate to |
| 2114 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2115 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2116 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2117 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2118 | TLI.getShiftAmountTy(), Op2); |
| 2119 | // Otherwise we'll need to temporarily settle for some other |
| 2120 | // convenient type; type legalization will make adjustments as |
| 2121 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2122 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2123 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2124 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2125 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2126 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2127 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2128 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2129 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2130 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2131 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2134 | void SelectionDAGBuilder::visitICmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2135 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2136 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2137 | predicate = IC->getPredicate(); |
| 2138 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2139 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2140 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2141 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2142 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2143 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2144 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2145 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2148 | void SelectionDAGBuilder::visitFCmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2149 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2150 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2151 | predicate = FC->getPredicate(); |
| 2152 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2153 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2154 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2155 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2156 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2157 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2158 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2161 | void SelectionDAGBuilder::visitSelect(User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2162 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2163 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2164 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2165 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2166 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2167 | SmallVector<SDValue, 4> Values(NumValues); |
| 2168 | SDValue Cond = getValue(I.getOperand(0)); |
| 2169 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2170 | SDValue FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2171 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2172 | for (unsigned i = 0; i != NumValues; ++i) |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2173 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
| 2174 | TrueVal.getNode()->getValueType(i), Cond, |
| 2175 | SDValue(TrueVal.getNode(), |
| 2176 | TrueVal.getResNo() + i), |
| 2177 | SDValue(FalseVal.getNode(), |
| 2178 | FalseVal.getResNo() + i)); |
| 2179 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2180 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2181 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2182 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2183 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2184 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2185 | void SelectionDAGBuilder::visitTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2186 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2187 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2188 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2189 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2192 | void SelectionDAGBuilder::visitZExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2193 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2194 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2195 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2196 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2197 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2200 | void SelectionDAGBuilder::visitSExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2201 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2202 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2203 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2204 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2205 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2208 | void SelectionDAGBuilder::visitFPTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2209 | // FPTrunc is never a no-op cast, no need to check |
| 2210 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2211 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2212 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
| 2213 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2216 | void SelectionDAGBuilder::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2217 | // FPTrunc is never a no-op cast, no need to check |
| 2218 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2219 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2220 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2223 | void SelectionDAGBuilder::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | // FPToUI is never a no-op cast, no need to check |
| 2225 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2226 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2227 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2230 | void SelectionDAGBuilder::visitFPToSI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2231 | // FPToSI is never a no-op cast, no need to check |
| 2232 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2233 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2234 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2237 | void SelectionDAGBuilder::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2238 | // UIToFP is never a no-op cast, no need to check |
| 2239 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2240 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2241 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2244 | void SelectionDAGBuilder::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2245 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2246 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2247 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2248 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2251 | void SelectionDAGBuilder::visitPtrToInt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2252 | // What to do depends on the size of the integer and the size of the pointer. |
| 2253 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2254 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2255 | EVT SrcVT = N.getValueType(); |
| 2256 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2257 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2260 | void SelectionDAGBuilder::visitIntToPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2261 | // What to do depends on the size of the integer and the size of the pointer. |
| 2262 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2263 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2264 | EVT SrcVT = N.getValueType(); |
| 2265 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2266 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2269 | void SelectionDAGBuilder::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2270 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2271 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2272 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2273 | // BitCast assures us that source and destination are the same size so this is |
| 2274 | // either a BIT_CONVERT or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2275 | if (DestVT != N.getValueType()) |
| 2276 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2277 | DestVT, N)); // convert types. |
| 2278 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2279 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2282 | void SelectionDAGBuilder::visitInsertElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2283 | SDValue InVec = getValue(I.getOperand(0)); |
| 2284 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2285 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2286 | TLI.getPointerTy(), |
| 2287 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2288 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2289 | TLI.getValueType(I.getType()), |
| 2290 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2293 | void SelectionDAGBuilder::visitExtractElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2294 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2295 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2296 | TLI.getPointerTy(), |
| 2297 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2298 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2299 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2302 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2303 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2304 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2305 | unsigned MaskNumElts = Mask.size(); |
| 2306 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2307 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2308 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2309 | return true; |
| 2310 | } |
| 2311 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2312 | void SelectionDAGBuilder::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2313 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2314 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2315 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2316 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2317 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2318 | // representing undef values. |
| 2319 | SmallVector<Constant*, 8> MaskElts; |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 2320 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2321 | unsigned MaskNumElts = MaskElts.size(); |
| 2322 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2323 | if (isa<UndefValue>(MaskElts[i])) |
| 2324 | Mask.push_back(-1); |
| 2325 | else |
| 2326 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2327 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2328 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2329 | EVT VT = TLI.getValueType(I.getType()); |
| 2330 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2331 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2332 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2333 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2334 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2335 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2336 | return; |
| 2337 | } |
| 2338 | |
| 2339 | // Normalize the shuffle vector since mask and vector length don't match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2340 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2341 | // Mask is longer than the source vectors and is a multiple of the source |
| 2342 | // vectors. We can use concatenate vector to make the mask and vectors |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2343 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2344 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2345 | // The shuffle is concatenating two vectors together. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2346 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2347 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2348 | return; |
| 2349 | } |
| 2350 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2351 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2352 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2353 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2354 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2355 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2356 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2357 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2358 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2359 | MOps1[0] = Src1; |
| 2360 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2361 | |
| 2362 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2363 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2364 | &MOps1[0], NumConcat); |
| 2365 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2366 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2367 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2368 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2369 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2370 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2371 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2372 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2373 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2374 | MappedOps.push_back(Idx); |
| 2375 | else |
| 2376 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2377 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2378 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2379 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2380 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2381 | return; |
| 2382 | } |
| 2383 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2384 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2385 | // Analyze the access pattern of the vector to see if we can extract |
| 2386 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2387 | // the range of elements the mask access on both vectors. |
| 2388 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2389 | int MaxRange[2] = {-1, -1}; |
| 2390 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2391 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2392 | int Idx = Mask[i]; |
| 2393 | int Input = 0; |
| 2394 | if (Idx < 0) |
| 2395 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2396 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2397 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2398 | Input = 1; |
| 2399 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2400 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2401 | if (Idx > MaxRange[Input]) |
| 2402 | MaxRange[Input] = Idx; |
| 2403 | if (Idx < MinRange[Input]) |
| 2404 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2405 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2406 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2407 | // Check if the access is smaller than the vector size and can we find |
| 2408 | // a reasonable extract index. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2409 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2410 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2411 | int StartIdx[2]; // StartIdx to extract from |
| 2412 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2413 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2414 | RangeUse[Input] = 0; // Unused |
| 2415 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2416 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2417 | // Fits within range but we should see if we can find a good |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2418 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2419 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2420 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2421 | StartIdx[Input] = 0; |
| 2422 | } else { |
| 2423 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2424 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2425 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2426 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2427 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2428 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2431 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2432 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
| 2435 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2436 | // Extract appropriate subvector and generate a vector shuffle |
| 2437 | for (int Input=0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2438 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2439 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2440 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2441 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2442 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2443 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2444 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2445 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2446 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2447 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2448 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2449 | int Idx = Mask[i]; |
| 2450 | if (Idx < 0) |
| 2451 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2452 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2453 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2454 | else |
| 2455 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2456 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2457 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2458 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2459 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2460 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2464 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2465 | // replacing the shuffle with extract and build vector. |
| 2466 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2467 | EVT EltVT = VT.getVectorElementType(); |
| 2468 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2469 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2470 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2471 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2472 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2473 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2474 | int Idx = Mask[i]; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2475 | SDValue Res; |
| 2476 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2477 | if (Idx < (int)SrcNumElts) |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2478 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2479 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2480 | else |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2481 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2482 | EltVT, Src2, |
| 2483 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2484 | |
| 2485 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2486 | } |
| 2487 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2488 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2489 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2490 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2493 | void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2494 | const Value *Op0 = I.getOperand(0); |
| 2495 | const Value *Op1 = I.getOperand(1); |
| 2496 | const Type *AggTy = I.getType(); |
| 2497 | const Type *ValTy = Op1->getType(); |
| 2498 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2499 | bool FromUndef = isa<UndefValue>(Op1); |
| 2500 | |
| 2501 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2502 | I.idx_begin(), I.idx_end()); |
| 2503 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2504 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2505 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2506 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2507 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2508 | |
| 2509 | unsigned NumAggValues = AggValueVTs.size(); |
| 2510 | unsigned NumValValues = ValValueVTs.size(); |
| 2511 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2512 | |
| 2513 | SDValue Agg = getValue(Op0); |
| 2514 | SDValue Val = getValue(Op1); |
| 2515 | unsigned i = 0; |
| 2516 | // Copy the beginning value(s) from the original aggregate. |
| 2517 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2518 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2519 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2520 | // Copy values from the inserted value(s). |
| 2521 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2522 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2523 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2524 | // Copy remaining value(s) from the original aggregate. |
| 2525 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2526 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2527 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2528 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2529 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2530 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2531 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2534 | void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2535 | const Value *Op0 = I.getOperand(0); |
| 2536 | const Type *AggTy = Op0->getType(); |
| 2537 | const Type *ValTy = I.getType(); |
| 2538 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2539 | |
| 2540 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2541 | I.idx_begin(), I.idx_end()); |
| 2542 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2543 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2544 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2545 | |
| 2546 | unsigned NumValValues = ValValueVTs.size(); |
| 2547 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2548 | |
| 2549 | SDValue Agg = getValue(Op0); |
| 2550 | // Copy out the selected value(s). |
| 2551 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2552 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2553 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2554 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2555 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2556 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2557 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2558 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2559 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2562 | void SelectionDAGBuilder::visitGetElementPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2563 | SDValue N = getValue(I.getOperand(0)); |
| 2564 | const Type *Ty = I.getOperand(0)->getType(); |
| 2565 | |
| 2566 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2567 | OI != E; ++OI) { |
| 2568 | Value *Idx = *OI; |
| 2569 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2570 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2571 | if (Field) { |
| 2572 | // N = N + Offset |
| 2573 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2574 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2575 | DAG.getIntPtrConstant(Offset)); |
| 2576 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2577 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2578 | Ty = StTy->getElementType(Field); |
| 2579 | } else { |
| 2580 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2581 | |
| 2582 | // If this is a constant subscript, handle it quickly. |
| 2583 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2584 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2585 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2586 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2587 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2588 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2589 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2590 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2591 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2592 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2593 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2594 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2595 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2596 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2597 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2598 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2599 | continue; |
| 2600 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2601 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2602 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2603 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2604 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2605 | SDValue IdxN = getValue(Idx); |
| 2606 | |
| 2607 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2608 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2609 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2614 | if (ElementSize.isPowerOf2()) { |
| 2615 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2616 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2617 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2618 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2619 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2620 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2621 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2622 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2626 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2627 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2628 | } |
| 2629 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2630 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2631 | setValue(&I, N); |
| 2632 | } |
| 2633 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2634 | void SelectionDAGBuilder::visitAlloca(AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [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(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2641 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2642 | unsigned Align = |
| 2643 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2644 | I.getAlignment()); |
| 2645 | |
| 2646 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2648 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2649 | AllocSize, |
| 2650 | DAG.getConstant(TySize, AllocSize.getValueType())); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2651 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2652 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2653 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2654 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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. |
| 2658 | unsigned StackAlign = |
| 2659 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2660 | if (Align <= StackAlign) |
| 2661 | Align = 0; |
| 2662 | |
| 2663 | // Round the size of the allocation up to the stack alignment size |
| 2664 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2665 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2666 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2667 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2669 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2670 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2671 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2672 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2673 | |
| 2674 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2675 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2676 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2677 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2678 | setValue(&I, DSA); |
| 2679 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2680 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2681 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2682 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2683 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2686 | void SelectionDAGBuilder::visitLoad(LoadInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2687 | const Value *SV = I.getOperand(0); |
| 2688 | SDValue Ptr = getValue(SV); |
| 2689 | |
| 2690 | const Type *Ty = I.getType(); |
| 2691 | bool isVolatile = I.isVolatile(); |
| 2692 | unsigned Alignment = I.getAlignment(); |
| 2693 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2694 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2695 | SmallVector<uint64_t, 4> Offsets; |
| 2696 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2697 | unsigned NumValues = ValueVTs.size(); |
| 2698 | if (NumValues == 0) |
| 2699 | return; |
| 2700 | |
| 2701 | SDValue Root; |
| 2702 | bool ConstantMemory = false; |
| 2703 | if (I.isVolatile()) |
| 2704 | // Serialize volatile loads with other side effects. |
| 2705 | Root = getRoot(); |
| 2706 | else if (AA->pointsToConstantMemory(SV)) { |
| 2707 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2708 | Root = DAG.getEntryNode(); |
| 2709 | ConstantMemory = true; |
| 2710 | } else { |
| 2711 | // Do not serialize non-volatile loads against each other. |
| 2712 | Root = DAG.getRoot(); |
| 2713 | } |
| 2714 | |
| 2715 | SmallVector<SDValue, 4> Values(NumValues); |
| 2716 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2717 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2718 | for (unsigned i = 0; i != NumValues; ++i) { |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2719 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2720 | PtrVT, Ptr, |
| 2721 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2722 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2723 | A, SV, Offsets[i], isVolatile, Alignment); |
| 2724 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2725 | Values[i] = L; |
| 2726 | Chains[i] = L.getValue(1); |
| 2727 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2728 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2729 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2730 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2731 | MVT::Other, &Chains[0], NumValues); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2732 | if (isVolatile) |
| 2733 | DAG.setRoot(Chain); |
| 2734 | else |
| 2735 | PendingLoads.push_back(Chain); |
| 2736 | } |
| 2737 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2738 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2739 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2740 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2741 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2742 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2743 | void SelectionDAGBuilder::visitStore(StoreInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2744 | Value *SrcV = I.getOperand(0); |
| 2745 | Value *PtrV = I.getOperand(1); |
| 2746 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2747 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2748 | SmallVector<uint64_t, 4> Offsets; |
| 2749 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2750 | unsigned NumValues = ValueVTs.size(); |
| 2751 | if (NumValues == 0) |
| 2752 | return; |
| 2753 | |
| 2754 | // Get the lowered operands. Note that we do this after |
| 2755 | // checking if NumResults is zero, because with zero results |
| 2756 | // the operands won't have values in the map. |
| 2757 | SDValue Src = getValue(SrcV); |
| 2758 | SDValue Ptr = getValue(PtrV); |
| 2759 | |
| 2760 | SDValue Root = getRoot(); |
| 2761 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2762 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2763 | bool isVolatile = I.isVolatile(); |
| 2764 | unsigned Alignment = I.getAlignment(); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2765 | |
| 2766 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2767 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2768 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2769 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2770 | SDValue(Src.getNode(), Src.getResNo() + i), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2771 | Add, PtrV, Offsets[i], isVolatile, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2774 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2775 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2779 | /// node. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2780 | void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I, |
| 2781 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2782 | bool HasChain = !I.doesNotAccessMemory(); |
| 2783 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2784 | |
| 2785 | // Build the operand list. |
| 2786 | SmallVector<SDValue, 8> Ops; |
| 2787 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2788 | if (OnlyLoad) { |
| 2789 | // We don't need to serialize loads against other loads. |
| 2790 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2791 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2792 | Ops.push_back(getRoot()); |
| 2793 | } |
| 2794 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2795 | |
| 2796 | // Info is set by getTgtMemInstrinsic |
| 2797 | TargetLowering::IntrinsicInfo Info; |
| 2798 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2799 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2800 | // Add the intrinsic ID as an integer operand if it's not a target intrinsic. |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2801 | if (!IsTgtIntrinsic) |
| 2802 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2803 | |
| 2804 | // Add all operands of the call to the operand list. |
| 2805 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2806 | SDValue Op = getValue(I.getOperand(i)); |
| 2807 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2808 | "Intrinsic uses a non-legal type?"); |
| 2809 | Ops.push_back(Op); |
| 2810 | } |
| 2811 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2812 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2813 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2814 | #ifndef NDEBUG |
| 2815 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2816 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2817 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2818 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2819 | #endif // NDEBUG |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2820 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2821 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2822 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2823 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2824 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Create the node. |
| 2827 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2828 | if (IsTgtIntrinsic) { |
| 2829 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2830 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2831 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2832 | Info.memVT, Info.ptrVal, Info.offset, |
| 2833 | Info.align, Info.vol, |
| 2834 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2835 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2836 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2837 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2838 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2839 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2840 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2841 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2842 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2843 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2846 | if (HasChain) { |
| 2847 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2848 | if (OnlyLoad) |
| 2849 | PendingLoads.push_back(Chain); |
| 2850 | else |
| 2851 | DAG.setRoot(Chain); |
| 2852 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2853 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2854 | if (!I.getType()->isVoidTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2855 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2856 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2857 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2858 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2859 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2860 | setValue(&I, Result); |
| 2861 | } |
| 2862 | } |
| 2863 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2864 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2865 | /// number with exponent of 1: |
| 2866 | /// |
| 2867 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2868 | /// |
| 2869 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2870 | static SDValue |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2871 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2872 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2873 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2874 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 2875 | DAG.getConstant(0x3f800000, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2876 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2879 | /// GetExponent - Get the exponent: |
| 2880 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 2881 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2882 | /// |
| 2883 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2884 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2885 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2886 | DebugLoc dl, unsigned Order) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2887 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2888 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 2889 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2890 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2891 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 2892 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2893 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2896 | /// getF32Constant - Get 32-bit floating point constant. |
| 2897 | static SDValue |
| 2898 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2899 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2902 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2903 | /// visitIntrinsicCall: I is a call instruction |
| 2904 | /// Op is the associated NodeType for I |
| 2905 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2906 | SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2907 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2908 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2909 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2910 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2911 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2912 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2913 | getValue(I.getOperand(2)), |
| 2914 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2915 | setValue(&I, L); |
| 2916 | DAG.setRoot(L.getValue(1)); |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2920 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2921 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2922 | SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2923 | SDValue Op1 = getValue(I.getOperand(1)); |
| 2924 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2925 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2926 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2927 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2928 | return 0; |
| 2929 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2930 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2931 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2932 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2933 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2934 | SelectionDAGBuilder::visitExp(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2935 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2936 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2937 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2938 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2939 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2940 | SDValue Op = getValue(I.getOperand(1)); |
| 2941 | |
| 2942 | // Put the exponent in the right bit position for later addition to the |
| 2943 | // final result: |
| 2944 | // |
| 2945 | // #define LOG2OFe 1.4426950f |
| 2946 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2947 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2948 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2949 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2950 | |
| 2951 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2952 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 2953 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2954 | |
| 2955 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2956 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2957 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2958 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2959 | if (LimitFloatPrecision <= 6) { |
| 2960 | // For floating-point precision of 6: |
| 2961 | // |
| 2962 | // TwoToFractionalPartOfX = |
| 2963 | // 0.997535578f + |
| 2964 | // (0.735607626f + 0.252464424f * x) * x; |
| 2965 | // |
| 2966 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2967 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2968 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2969 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2970 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2971 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2972 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2973 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2974 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2975 | |
| 2976 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2977 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2978 | TwoToFracPartOfX, IntegerPartOfX); |
| 2979 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2980 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2981 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 2982 | // For floating-point precision of 12: |
| 2983 | // |
| 2984 | // TwoToFractionalPartOfX = |
| 2985 | // 0.999892986f + |
| 2986 | // (0.696457318f + |
| 2987 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 2988 | // |
| 2989 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2990 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2991 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2992 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2993 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2994 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2995 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2996 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2997 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 2998 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2999 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3000 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3001 | |
| 3002 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3003 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3004 | TwoToFracPartOfX, IntegerPartOfX); |
| 3005 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3006 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3007 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3008 | // For floating-point precision of 18: |
| 3009 | // |
| 3010 | // TwoToFractionalPartOfX = |
| 3011 | // 0.999999982f + |
| 3012 | // (0.693148872f + |
| 3013 | // (0.240227044f + |
| 3014 | // (0.554906021e-1f + |
| 3015 | // (0.961591928e-2f + |
| 3016 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3017 | // |
| 3018 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3019 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3020 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3021 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3022 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3023 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3024 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3025 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3026 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3027 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3028 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3029 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3030 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3031 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3032 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3033 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3034 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3035 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3036 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3037 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3038 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3039 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3042 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3043 | TwoToFracPartOfX, IntegerPartOfX); |
| 3044 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3045 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3046 | } |
| 3047 | } else { |
| 3048 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3049 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3050 | getValue(I.getOperand(1)).getValueType(), |
| 3051 | getValue(I.getOperand(1))); |
| 3052 | } |
| 3053 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3054 | setValue(&I, result); |
| 3055 | } |
| 3056 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3057 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3058 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3059 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3060 | SelectionDAGBuilder::visitLog(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3061 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3062 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3063 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3064 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3065 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3066 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3067 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3068 | |
| 3069 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3070 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3071 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3072 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3073 | |
| 3074 | // Get the significand and build it into a floating-point number with |
| 3075 | // exponent of 1. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3076 | SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3077 | |
| 3078 | if (LimitFloatPrecision <= 6) { |
| 3079 | // For floating-point precision of 6: |
| 3080 | // |
| 3081 | // LogofMantissa = |
| 3082 | // -1.1609546f + |
| 3083 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3084 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3085 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3086 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3087 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3088 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3089 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3090 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3091 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3092 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3093 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3094 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3095 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3096 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3097 | // For floating-point precision of 12: |
| 3098 | // |
| 3099 | // LogOfMantissa = |
| 3100 | // -1.7417939f + |
| 3101 | // (2.8212026f + |
| 3102 | // (-1.4699568f + |
| 3103 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3104 | // |
| 3105 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3106 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3107 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3108 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3109 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3110 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3111 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3112 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3113 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3114 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3115 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3116 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3117 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3118 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3119 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3120 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3121 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3122 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3123 | // For floating-point precision of 18: |
| 3124 | // |
| 3125 | // LogOfMantissa = |
| 3126 | // -2.1072184f + |
| 3127 | // (4.2372794f + |
| 3128 | // (-3.7029485f + |
| 3129 | // (2.2781945f + |
| 3130 | // (-0.87823314f + |
| 3131 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3132 | // |
| 3133 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3134 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3135 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3136 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3137 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3138 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3139 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3140 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3141 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3142 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3143 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3144 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3145 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3146 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3147 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3148 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3149 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3150 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3151 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3152 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3153 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3154 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3155 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3156 | } |
| 3157 | } else { |
| 3158 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3159 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3160 | getValue(I.getOperand(1)).getValueType(), |
| 3161 | getValue(I.getOperand(1))); |
| 3162 | } |
| 3163 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3164 | setValue(&I, result); |
| 3165 | } |
| 3166 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3167 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3168 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3169 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3170 | SelectionDAGBuilder::visitLog2(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3171 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3172 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3173 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3174 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3175 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3176 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3177 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3178 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3179 | // Get the exponent. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3180 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder); |
| 3181 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3182 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3183 | // exponent of 1. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3184 | SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3185 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3186 | // Different possible minimax approximations of significand in |
| 3187 | // floating-point for various degrees of accuracy over [1,2]. |
| 3188 | if (LimitFloatPrecision <= 6) { |
| 3189 | // For floating-point precision of 6: |
| 3190 | // |
| 3191 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3192 | // |
| 3193 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3194 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3195 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3196 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3197 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3198 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3199 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3200 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3201 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3202 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3203 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3204 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3205 | // For floating-point precision of 12: |
| 3206 | // |
| 3207 | // Log2ofMantissa = |
| 3208 | // -2.51285454f + |
| 3209 | // (4.07009056f + |
| 3210 | // (-2.12067489f + |
| 3211 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3212 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3213 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3214 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3215 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3216 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3217 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3218 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3219 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3220 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3221 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3222 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3223 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3224 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3225 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3226 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3227 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3228 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3229 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3230 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3231 | // For floating-point precision of 18: |
| 3232 | // |
| 3233 | // Log2ofMantissa = |
| 3234 | // -3.0400495f + |
| 3235 | // (6.1129976f + |
| 3236 | // (-5.3420409f + |
| 3237 | // (3.2865683f + |
| 3238 | // (-1.2669343f + |
| 3239 | // (0.27515199f - |
| 3240 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3241 | // |
| 3242 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3243 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3244 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3245 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3246 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3247 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3248 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3249 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3250 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3251 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3252 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3253 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3254 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3255 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3256 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3257 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3258 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3259 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3260 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3261 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3262 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3263 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3264 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3265 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3266 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3267 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3268 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3269 | getValue(I.getOperand(1)).getValueType(), |
| 3270 | getValue(I.getOperand(1))); |
| 3271 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3272 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3273 | setValue(&I, result); |
| 3274 | } |
| 3275 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3276 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3277 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3278 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3279 | SelectionDAGBuilder::visitLog10(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3280 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3281 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3282 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3283 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3284 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3285 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3286 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3287 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3288 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3289 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3290 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3291 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3292 | |
| 3293 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3294 | // exponent of 1. |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3295 | SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3296 | |
| 3297 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3298 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3299 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3300 | // Log10ofMantissa = |
| 3301 | // -0.50419619f + |
| 3302 | // (0.60948995f - 0.10380950f * x) * x; |
| 3303 | // |
| 3304 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3305 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3306 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3307 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3308 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3309 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3310 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3311 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3312 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3313 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3314 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3315 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3316 | // For floating-point precision of 12: |
| 3317 | // |
| 3318 | // Log10ofMantissa = |
| 3319 | // -0.64831180f + |
| 3320 | // (0.91751397f + |
| 3321 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3322 | // |
| 3323 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3324 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3325 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3326 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3327 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3328 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3329 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3330 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3331 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3332 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3333 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3334 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3335 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3336 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3337 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3338 | // For floating-point precision of 18: |
| 3339 | // |
| 3340 | // Log10ofMantissa = |
| 3341 | // -0.84299375f + |
| 3342 | // (1.5327582f + |
| 3343 | // (-1.0688956f + |
| 3344 | // (0.49102474f + |
| 3345 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3346 | // |
| 3347 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3348 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3349 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3350 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3351 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3352 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3353 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3354 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3355 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3356 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3357 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3358 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3359 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3360 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3361 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3362 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3363 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3364 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3365 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3366 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3367 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3368 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3369 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3370 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3371 | getValue(I.getOperand(1)).getValueType(), |
| 3372 | getValue(I.getOperand(1))); |
| 3373 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3374 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3375 | setValue(&I, result); |
| 3376 | } |
| 3377 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3378 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3379 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3380 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3381 | SelectionDAGBuilder::visitExp2(CallInst &I) { |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3382 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3383 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3384 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3385 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3386 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3387 | SDValue Op = getValue(I.getOperand(1)); |
| 3388 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3389 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3390 | |
| 3391 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3392 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3393 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3394 | |
| 3395 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3396 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3397 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3398 | |
| 3399 | if (LimitFloatPrecision <= 6) { |
| 3400 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3401 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3402 | // TwoToFractionalPartOfX = |
| 3403 | // 0.997535578f + |
| 3404 | // (0.735607626f + 0.252464424f * x) * x; |
| 3405 | // |
| 3406 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3407 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3408 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3409 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3410 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3411 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3412 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3413 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3414 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3415 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3416 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3417 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3418 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3419 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3420 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3421 | // For floating-point precision of 12: |
| 3422 | // |
| 3423 | // TwoToFractionalPartOfX = |
| 3424 | // 0.999892986f + |
| 3425 | // (0.696457318f + |
| 3426 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3427 | // |
| 3428 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3429 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3430 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3431 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3432 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3433 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3434 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3435 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3436 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3437 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3438 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3439 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3440 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3441 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3442 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3443 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3444 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3445 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3446 | // For floating-point precision of 18: |
| 3447 | // |
| 3448 | // TwoToFractionalPartOfX = |
| 3449 | // 0.999999982f + |
| 3450 | // (0.693148872f + |
| 3451 | // (0.240227044f + |
| 3452 | // (0.554906021e-1f + |
| 3453 | // (0.961591928e-2f + |
| 3454 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3455 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3456 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3457 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3458 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3459 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3460 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3461 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3462 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3463 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3464 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3465 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3466 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3467 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3468 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3469 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3470 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3471 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3472 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3473 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3474 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3475 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3476 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3477 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3478 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3479 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3480 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3481 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3482 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3483 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3484 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3485 | getValue(I.getOperand(1)).getValueType(), |
| 3486 | getValue(I.getOperand(1))); |
| 3487 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3488 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3489 | setValue(&I, result); |
| 3490 | } |
| 3491 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3492 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3493 | /// limited-precision mode with x == 10.0f. |
| 3494 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3495 | SelectionDAGBuilder::visitPow(CallInst &I) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3496 | SDValue result; |
| 3497 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3498 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3499 | bool IsExp10 = false; |
| 3500 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3501 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3502 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3503 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3504 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3505 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3506 | APFloat Ten(10.0f); |
| 3507 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3508 | } |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3513 | SDValue Op = getValue(I.getOperand(2)); |
| 3514 | |
| 3515 | // Put the exponent in the right bit position for later addition to the |
| 3516 | // final result: |
| 3517 | // |
| 3518 | // #define LOG2OF10 3.3219281f |
| 3519 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3520 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3521 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3522 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3523 | |
| 3524 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3525 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3526 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3527 | |
| 3528 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3529 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3530 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3531 | |
| 3532 | if (LimitFloatPrecision <= 6) { |
| 3533 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3534 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3535 | // twoToFractionalPartOfX = |
| 3536 | // 0.997535578f + |
| 3537 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3538 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3539 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3540 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3541 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3542 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3543 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3544 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3545 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3546 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3547 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3548 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3549 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3550 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3551 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3552 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3553 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3554 | // For floating-point precision of 12: |
| 3555 | // |
| 3556 | // TwoToFractionalPartOfX = |
| 3557 | // 0.999892986f + |
| 3558 | // (0.696457318f + |
| 3559 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3560 | // |
| 3561 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3562 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3563 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3564 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3565 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3566 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3567 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3568 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3569 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3570 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3571 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3572 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3573 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3574 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3575 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3576 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3577 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3578 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3579 | // For floating-point precision of 18: |
| 3580 | // |
| 3581 | // TwoToFractionalPartOfX = |
| 3582 | // 0.999999982f + |
| 3583 | // (0.693148872f + |
| 3584 | // (0.240227044f + |
| 3585 | // (0.554906021e-1f + |
| 3586 | // (0.961591928e-2f + |
| 3587 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3588 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3589 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3590 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3591 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3592 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3593 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3594 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3595 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3596 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3597 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3598 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3599 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3600 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3601 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3602 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3603 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3604 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3605 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3606 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3607 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3608 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3609 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3610 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3611 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3612 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3613 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } else { |
| 3616 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3617 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3618 | getValue(I.getOperand(1)).getValueType(), |
| 3619 | getValue(I.getOperand(1)), |
| 3620 | getValue(I.getOperand(2))); |
| 3621 | } |
| 3622 | |
| 3623 | setValue(&I, result); |
| 3624 | } |
| 3625 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3626 | |
| 3627 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3628 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3629 | SelectionDAG &DAG) { |
| 3630 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3631 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3632 | // optimizing for size, we only want to do this if the expansion would produce |
| 3633 | // a small number of multiplies, otherwise we do the full expansion. |
| 3634 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3635 | // Get the exponent as a positive value. |
| 3636 | unsigned Val = RHSC->getSExtValue(); |
| 3637 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3638 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3639 | // powi(x, 0) -> 1.0 |
| 3640 | if (Val == 0) |
| 3641 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3642 | |
| 3643 | Function *F = DAG.getMachineFunction().getFunction(); |
| 3644 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3645 | // If optimizing for size, don't insert too many multiplies. This |
| 3646 | // inserts up to 5 multiplies. |
| 3647 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3648 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3649 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3650 | // powi(x,15) generates one more multiply than it should), but this has |
| 3651 | // the benefit of being both really simple and much better than a libcall. |
| 3652 | SDValue Res; // Logically starts equal to 1.0 |
| 3653 | SDValue CurSquare = LHS; |
| 3654 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3655 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3656 | if (Res.getNode()) |
| 3657 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3658 | else |
| 3659 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3660 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3661 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3662 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3663 | CurSquare, CurSquare); |
| 3664 | Val >>= 1; |
| 3665 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3666 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3667 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3668 | if (RHSC->getSExtValue() < 0) |
| 3669 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3670 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3671 | return Res; |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | // Otherwise, expand to a libcall. |
| 3676 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3677 | } |
| 3678 | |
| 3679 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3680 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3681 | /// we want to emit this as a call to a named external function, return the name |
| 3682 | /// otherwise lower it and return null. |
| 3683 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3684 | SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3685 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3686 | SDValue Res; |
| 3687 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3688 | switch (Intrinsic) { |
| 3689 | default: |
| 3690 | // By default, turn this into a target intrinsic node. |
| 3691 | visitTargetIntrinsic(I, Intrinsic); |
| 3692 | return 0; |
| 3693 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3694 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3695 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3696 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3697 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
| 3698 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3699 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3700 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3701 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
| 3702 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3703 | return 0; |
| 3704 | case Intrinsic::setjmp: |
| 3705 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3706 | case Intrinsic::longjmp: |
| 3707 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3708 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3709 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3710 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3711 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3712 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3713 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
| 3714 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3715 | return 0; |
| 3716 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3717 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3718 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3719 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3720 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3721 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3722 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
| 3723 | I.getOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3724 | return 0; |
| 3725 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3726 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3727 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3728 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3729 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3730 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3731 | |
| 3732 | // If the source and destination are known to not be aliases, we can |
| 3733 | // lower memmove as memcpy. |
| 3734 | uint64_t Size = -1ULL; |
| 3735 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3736 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3737 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3738 | AliasAnalysis::NoAlias) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3739 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
| 3740 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3741 | return 0; |
| 3742 | } |
| 3743 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3744 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
| 3745 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3746 | return 0; |
| 3747 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3748 | case Intrinsic::dbg_declare: { |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3749 | // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None. |
| 3750 | // The real handling of this intrinsic is in FastISel. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3751 | if (OptLevel != CodeGenOpt::None) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3752 | // FIXME: Variable debug info is not supported here. |
| 3753 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3754 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3755 | if (!DW) |
| 3756 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3757 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Chris Lattner | bf0ca2b | 2009-12-29 09:32:19 +0000 | [diff] [blame] | 3758 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3759 | return 0; |
| 3760 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3761 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3762 | Value *Address = DI.getAddress(); |
| 3763 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 3764 | Address = BCI->getOperand(0); |
| 3765 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 3766 | // Don't handle byval struct arguments or VLAs, for example. |
| 3767 | if (!AI) |
| 3768 | return 0; |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3769 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3770 | FuncInfo.StaticAllocaMap.find(AI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3771 | if (SI == FuncInfo.StaticAllocaMap.end()) |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3772 | return 0; // VLAs. |
| 3773 | int FI = SI->second; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 3774 | |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 3775 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) |
| 3776 | if (MDNode *Dbg = DI.getMetadata("dbg")) |
Chris Lattner | 0eb4198 | 2009-12-28 20:45:51 +0000 | [diff] [blame] | 3777 | MMI->setVariableDbgInfo(Variable, FI, Dbg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3778 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3779 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3780 | case Intrinsic::dbg_value: { |
| 3781 | // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None. |
| 3782 | // The real handling of this intrinsic is in FastISel. |
| 3783 | if (OptLevel != CodeGenOpt::None) |
| 3784 | // FIXME: Variable debug info is not supported here. |
| 3785 | return 0; |
| 3786 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3787 | if (!DW) |
| 3788 | return 0; |
| 3789 | DbgValueInst &DI = cast<DbgValueInst>(I); |
| 3790 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3791 | return 0; |
| 3792 | |
| 3793 | MDNode *Variable = DI.getVariable(); |
| 3794 | Value *V = DI.getValue(); |
| 3795 | if (!V) |
| 3796 | return 0; |
| 3797 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
| 3798 | V = BCI->getOperand(0); |
| 3799 | AllocaInst *AI = dyn_cast<AllocaInst>(V); |
| 3800 | // Don't handle byval struct arguments or VLAs, for example. |
| 3801 | if (!AI) |
| 3802 | return 0; |
| 3803 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3804 | FuncInfo.StaticAllocaMap.find(AI); |
| 3805 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3806 | return 0; // VLAs. |
| 3807 | int FI = SI->second; |
| 3808 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) |
| 3809 | if (MDNode *Dbg = DI.getMetadata("dbg")) |
| 3810 | MMI->setVariableDbgInfo(Variable, FI, Dbg); |
| 3811 | return 0; |
| 3812 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3813 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3814 | // Insert the EXCEPTIONADDR instruction. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 3815 | assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3816 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3817 | SDValue Ops[1]; |
| 3818 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3819 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3820 | setValue(&I, Op); |
| 3821 | DAG.setRoot(Op.getValue(1)); |
| 3822 | return 0; |
| 3823 | } |
| 3824 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3825 | case Intrinsic::eh_selector: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3826 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3827 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3828 | if (CurMBB->isLandingPad()) |
| 3829 | AddCatchInfo(I, MMI, CurMBB); |
| 3830 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3831 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3832 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3833 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3834 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3835 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 3836 | if (Reg) CurMBB->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3837 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3838 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3839 | // Insert the EHSELECTION instruction. |
| 3840 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3841 | SDValue Ops[2]; |
| 3842 | Ops[0] = getValue(I.getOperand(1)); |
| 3843 | Ops[1] = getRoot(); |
| 3844 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3845 | DAG.setRoot(Op.getValue(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3846 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3847 | return 0; |
| 3848 | } |
| 3849 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3850 | case Intrinsic::eh_typeid_for: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3851 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3852 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3853 | if (MMI) { |
| 3854 | // Find the type id for the given typeinfo. |
| 3855 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3856 | unsigned TypeID = MMI->getTypeIDFor(GV); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3857 | Res = DAG.getConstant(TypeID, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3858 | } else { |
| 3859 | // Return something different to eh_selector. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3860 | Res = DAG.getConstant(1, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3863 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3864 | return 0; |
| 3865 | } |
| 3866 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3867 | case Intrinsic::eh_return_i32: |
| 3868 | case Intrinsic::eh_return_i64: |
| 3869 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3870 | MMI->setCallsEHReturn(true); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3871 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 3872 | MVT::Other, |
| 3873 | getControlRoot(), |
| 3874 | getValue(I.getOperand(1)), |
| 3875 | getValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3876 | } else { |
| 3877 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 3878 | } |
| 3879 | |
| 3880 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3881 | case Intrinsic::eh_unwind_init: |
| 3882 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 3883 | MMI->setCallsUnwindInit(true); |
| 3884 | } |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3885 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3886 | case Intrinsic::eh_dwarf_cfa: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3887 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 3888 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 3889 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3890 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3891 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3892 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3893 | TLI.getPointerTy()), |
| 3894 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3895 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3896 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3897 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3898 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 3899 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3900 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3901 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3902 | case Intrinsic::eh_sjlj_callsite: { |
| 3903 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3904 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 3905 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
| 3906 | assert(MMI->getCurrentCallSite() == 0 && "Overlapping call sites!"); |
| 3907 | |
| 3908 | MMI->setCurrentCallSite(CI->getZExtValue()); |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3912 | case Intrinsic::convertff: |
| 3913 | case Intrinsic::convertfsi: |
| 3914 | case Intrinsic::convertfui: |
| 3915 | case Intrinsic::convertsif: |
| 3916 | case Intrinsic::convertuif: |
| 3917 | case Intrinsic::convertss: |
| 3918 | case Intrinsic::convertsu: |
| 3919 | case Intrinsic::convertus: |
| 3920 | case Intrinsic::convertuu: { |
| 3921 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 3922 | switch (Intrinsic) { |
| 3923 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 3924 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 3925 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 3926 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 3927 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 3928 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 3929 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 3930 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 3931 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 3932 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3933 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3934 | Value *Op1 = I.getOperand(1); |
| 3935 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 3936 | DAG.getValueType(DestVT), |
| 3937 | DAG.getValueType(getValue(Op1).getValueType()), |
| 3938 | getValue(I.getOperand(2)), |
| 3939 | getValue(I.getOperand(3)), |
| 3940 | Code); |
| 3941 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3942 | return 0; |
| 3943 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3944 | case Intrinsic::sqrt: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3945 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
| 3946 | getValue(I.getOperand(1)).getValueType(), |
| 3947 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3948 | return 0; |
| 3949 | case Intrinsic::powi: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3950 | setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)), |
| 3951 | getValue(I.getOperand(2)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3952 | return 0; |
| 3953 | case Intrinsic::sin: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3954 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
| 3955 | getValue(I.getOperand(1)).getValueType(), |
| 3956 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3957 | return 0; |
| 3958 | case Intrinsic::cos: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3959 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
| 3960 | getValue(I.getOperand(1)).getValueType(), |
| 3961 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3962 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3963 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3964 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3965 | return 0; |
| 3966 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3967 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3968 | return 0; |
| 3969 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3970 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3971 | return 0; |
| 3972 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3973 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3974 | return 0; |
| 3975 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3976 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3977 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3978 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3979 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3980 | return 0; |
| 3981 | case Intrinsic::pcmarker: { |
| 3982 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3983 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3984 | return 0; |
| 3985 | } |
| 3986 | case Intrinsic::readcyclecounter: { |
| 3987 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3988 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 3989 | DAG.getVTList(MVT::i64, MVT::Other), |
| 3990 | &Op, 1); |
| 3991 | setValue(&I, Res); |
| 3992 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3993 | return 0; |
| 3994 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3995 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3996 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
| 3997 | getValue(I.getOperand(1)).getValueType(), |
| 3998 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3999 | return 0; |
| 4000 | case Intrinsic::cttz: { |
| 4001 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4002 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4003 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4004 | return 0; |
| 4005 | } |
| 4006 | case Intrinsic::ctlz: { |
| 4007 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4008 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4009 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4010 | return 0; |
| 4011 | } |
| 4012 | case Intrinsic::ctpop: { |
| 4013 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4014 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4015 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4016 | return 0; |
| 4017 | } |
| 4018 | case Intrinsic::stacksave: { |
| 4019 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4020 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4021 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4022 | setValue(&I, Res); |
| 4023 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4024 | return 0; |
| 4025 | } |
| 4026 | case Intrinsic::stackrestore: { |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4027 | Res = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4028 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4029 | return 0; |
| 4030 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4031 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4032 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4033 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4034 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4035 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4036 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4037 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4038 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4039 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4040 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4041 | MFI->setStackProtectorIndex(FI); |
| 4042 | |
| 4043 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4044 | |
| 4045 | // Store the stack protector onto the stack. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4046 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4047 | PseudoSourceValue::getFixedStack(FI), |
| 4048 | 0, true); |
| 4049 | setValue(&I, Res); |
| 4050 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4051 | return 0; |
| 4052 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4053 | case Intrinsic::objectsize: { |
| 4054 | // If we don't know by now, we're never going to know. |
| 4055 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4056 | |
| 4057 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4058 | |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4059 | SDValue Arg = getValue(I.getOperand(0)); |
| 4060 | EVT Ty = Arg.getValueType(); |
| 4061 | |
Eric Christopher | d060b25 | 2009-12-23 02:51:48 +0000 | [diff] [blame] | 4062 | if (CI->getZExtValue() == 0) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4063 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4064 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4065 | Res = DAG.getConstant(0, Ty); |
| 4066 | |
| 4067 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4068 | return 0; |
| 4069 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4070 | case Intrinsic::var_annotation: |
| 4071 | // Discard annotate attributes |
| 4072 | return 0; |
| 4073 | |
| 4074 | case Intrinsic::init_trampoline: { |
| 4075 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4076 | |
| 4077 | SDValue Ops[6]; |
| 4078 | Ops[0] = getRoot(); |
| 4079 | Ops[1] = getValue(I.getOperand(1)); |
| 4080 | Ops[2] = getValue(I.getOperand(2)); |
| 4081 | Ops[3] = getValue(I.getOperand(3)); |
| 4082 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4083 | Ops[5] = DAG.getSrcValue(F); |
| 4084 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4085 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4086 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4087 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4088 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4089 | setValue(&I, Res); |
| 4090 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4091 | return 0; |
| 4092 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4093 | case Intrinsic::gcroot: |
| 4094 | if (GFI) { |
| 4095 | Value *Alloca = I.getOperand(1); |
| 4096 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4097 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4098 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4099 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4100 | } |
| 4101 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4102 | case Intrinsic::gcread: |
| 4103 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4104 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4105 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4106 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4107 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4108 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4109 | case Intrinsic::trap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4110 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4111 | return 0; |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4112 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4113 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4114 | case Intrinsic::sadd_with_overflow: |
| 4115 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4116 | case Intrinsic::usub_with_overflow: |
| 4117 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4118 | case Intrinsic::ssub_with_overflow: |
| 4119 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4120 | case Intrinsic::umul_with_overflow: |
| 4121 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4122 | case Intrinsic::smul_with_overflow: |
| 4123 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4124 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4125 | case Intrinsic::prefetch: { |
| 4126 | SDValue Ops[4]; |
| 4127 | Ops[0] = getRoot(); |
| 4128 | Ops[1] = getValue(I.getOperand(1)); |
| 4129 | Ops[2] = getValue(I.getOperand(2)); |
| 4130 | Ops[3] = getValue(I.getOperand(3)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4131 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4132 | return 0; |
| 4133 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4134 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4135 | case Intrinsic::memory_barrier: { |
| 4136 | SDValue Ops[6]; |
| 4137 | Ops[0] = getRoot(); |
| 4138 | for (int x = 1; x < 6; ++x) |
| 4139 | Ops[x] = getValue(I.getOperand(x)); |
| 4140 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4141 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4145 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4146 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4147 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4148 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4149 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4150 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4151 | getValue(I.getOperand(2)), |
| 4152 | getValue(I.getOperand(3)), |
| 4153 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4154 | setValue(&I, L); |
| 4155 | DAG.setRoot(L.getValue(1)); |
| 4156 | return 0; |
| 4157 | } |
| 4158 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4159 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4160 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4161 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4162 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4163 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4164 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4165 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4166 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4167 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4168 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4169 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4170 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4171 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4172 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4173 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4174 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4175 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4176 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4177 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4178 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4179 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4180 | |
| 4181 | case Intrinsic::invariant_start: |
| 4182 | case Intrinsic::lifetime_start: |
| 4183 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4184 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4185 | return 0; |
| 4186 | case Intrinsic::invariant_end: |
| 4187 | case Intrinsic::lifetime_end: |
| 4188 | // Discard region information. |
| 4189 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4190 | } |
| 4191 | } |
| 4192 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4193 | /// Test if the given instruction is in a position to be optimized |
| 4194 | /// with a tail-call. This roughly means that it's in a block with |
| 4195 | /// a return and there's nothing that needs to be scheduled |
| 4196 | /// between it and the return. |
| 4197 | /// |
| 4198 | /// This function only tests target-independent requirements. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4199 | static bool |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4200 | isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4201 | const TargetLowering &TLI) { |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4202 | const Instruction *I = CS.getInstruction(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4203 | const BasicBlock *ExitBB = I->getParent(); |
| 4204 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 4205 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
| 4206 | const Function *F = ExitBB->getParent(); |
| 4207 | |
Evan Cheng | febc816 | 2010-02-03 03:55:59 +0000 | [diff] [blame] | 4208 | // The block must end in a return statement. |
| 4209 | // FIXME: Disallow tailcall if the block ends in an unreachable for now. |
| 4210 | // The way tailcall optimization is currently implemented means it will |
| 4211 | // add an epilogue followed by a jump. That is not profitable. Also, if |
| 4212 | // the callee is a special function (e.g. longjmp on x86), it can end up |
| 4213 | // causing miscompilation that has not been fully understood. |
| 4214 | if (!Ret) return false; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4215 | |
| 4216 | // If I will have a chain, make sure no other instruction that will have a |
| 4217 | // chain interposes between I and the return. |
| 4218 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
| 4219 | !I->isSafeToSpeculativelyExecute()) |
| 4220 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 4221 | --BBI) { |
| 4222 | if (&*BBI == I) |
| 4223 | break; |
| 4224 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
| 4225 | !BBI->isSafeToSpeculativelyExecute()) |
| 4226 | return false; |
| 4227 | } |
| 4228 | |
| 4229 | // If the block ends with a void return or unreachable, it doesn't matter |
| 4230 | // what the call's return type is. |
| 4231 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 4232 | |
Dan Gohman | ed9bab3 | 2009-11-14 02:06:30 +0000 | [diff] [blame] | 4233 | // If the return value is undef, it doesn't matter what the call's |
| 4234 | // return type is. |
| 4235 | if (isa<UndefValue>(Ret->getOperand(0))) return true; |
| 4236 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4237 | // Conservatively require the attributes of the call to match those of |
Dan Gohman | 01205a8 | 2009-11-13 18:49:38 +0000 | [diff] [blame] | 4238 | // the return. Ignore noalias because it doesn't affect the call sequence. |
| 4239 | unsigned CallerRetAttr = F->getAttributes().getRetAttributes(); |
| 4240 | if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4241 | return false; |
| 4242 | |
Evan Cheng | 6fdce65 | 2010-02-04 19:07:06 +0000 | [diff] [blame] | 4243 | // It's not safe to eliminate the sign / zero extension of the return value. |
Evan Cheng | 446bc10 | 2010-02-04 02:45:02 +0000 | [diff] [blame] | 4244 | if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) |
| 4245 | return false; |
| 4246 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4247 | // Otherwise, make sure the unmodified return value of I is the return value. |
| 4248 | for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; |
| 4249 | U = dyn_cast<Instruction>(U->getOperand(0))) { |
| 4250 | if (!U) |
| 4251 | return false; |
| 4252 | if (!U->hasOneUse()) |
| 4253 | return false; |
| 4254 | if (U == I) |
| 4255 | break; |
| 4256 | // Check for a truly no-op truncate. |
| 4257 | if (isa<TruncInst>(U) && |
| 4258 | TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) |
| 4259 | continue; |
| 4260 | // Check for a truly no-op bitcast. |
| 4261 | if (isa<BitCastInst>(U) && |
| 4262 | (U->getOperand(0)->getType() == U->getType() || |
| 4263 | (isa<PointerType>(U->getOperand(0)->getType()) && |
| 4264 | isa<PointerType>(U->getType())))) |
| 4265 | continue; |
| 4266 | // Otherwise it's not a true no-op. |
| 4267 | return false; |
| 4268 | } |
| 4269 | |
| 4270 | return true; |
| 4271 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4272 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4273 | void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee, |
| 4274 | bool isTailCall, |
| 4275 | MachineBasicBlock *LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4276 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4277 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4278 | const Type *RetTy = FTy->getReturnType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4279 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4280 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4281 | |
| 4282 | TargetLowering::ArgListTy Args; |
| 4283 | TargetLowering::ArgListEntry Entry; |
| 4284 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4285 | |
| 4286 | // Check whether the function can return without sret-demotion. |
| 4287 | SmallVector<EVT, 4> OutVTs; |
| 4288 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4289 | SmallVector<uint64_t, 4> Offsets; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4290 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4291 | OutVTs, OutsFlags, TLI, &Offsets); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4292 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4293 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4294 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4295 | |
| 4296 | SDValue DemoteStackSlot; |
| 4297 | |
| 4298 | if (!CanLowerReturn) { |
| 4299 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4300 | FTy->getReturnType()); |
| 4301 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4302 | FTy->getReturnType()); |
| 4303 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4304 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4305 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4306 | |
| 4307 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4308 | Entry.Node = DemoteStackSlot; |
| 4309 | Entry.Ty = StackSlotPtrType; |
| 4310 | Entry.isSExt = false; |
| 4311 | Entry.isZExt = false; |
| 4312 | Entry.isInReg = false; |
| 4313 | Entry.isSRet = true; |
| 4314 | Entry.isNest = false; |
| 4315 | Entry.isByVal = false; |
| 4316 | Entry.Alignment = Align; |
| 4317 | Args.push_back(Entry); |
| 4318 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4319 | } |
| 4320 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4321 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4322 | i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4323 | SDValue ArgNode = getValue(*i); |
| 4324 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4325 | |
| 4326 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4327 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4328 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4329 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4330 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4331 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4332 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4333 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4334 | Args.push_back(Entry); |
| 4335 | } |
| 4336 | |
| 4337 | if (LandingPad && MMI) { |
| 4338 | // Insert a label before the invoke call to mark the try range. This can be |
| 4339 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4340 | BeginLabel = MMI->NextLabelID(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4341 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4342 | // For SjLj, keep track of which landing pads go with which invokes |
| 4343 | // so as to maintain the ordering of pads in the LSDA. |
| 4344 | unsigned CallSiteIndex = MMI->getCurrentCallSite(); |
| 4345 | if (CallSiteIndex) { |
| 4346 | MMI->setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
| 4347 | // Now that the call site is handled, stop tracking it. |
| 4348 | MMI->setCurrentCallSite(0); |
| 4349 | } |
| 4350 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4351 | // Both PendingLoads and PendingExports must be flushed here; |
| 4352 | // this call might not return. |
| 4353 | (void)getRoot(); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4354 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4355 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4358 | // Check if target-independent constraints permit a tail call here. |
| 4359 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4360 | if (isTailCall && |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4361 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4362 | isTailCall = false; |
| 4363 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4364 | std::pair<SDValue,SDValue> Result = |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4365 | TLI.LowerCallTo(getRoot(), RetTy, |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4366 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4367 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4368 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4369 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4370 | isTailCall, |
| 4371 | !CS.getInstruction()->use_empty(), |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4372 | Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4373 | assert((isTailCall || Result.second.getNode()) && |
| 4374 | "Non-null chain expected with non-tail call!"); |
| 4375 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4376 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4377 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4378 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4379 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4380 | // The instruction result is the result of loading from the |
| 4381 | // hidden sret parameter. |
| 4382 | SmallVector<EVT, 1> PVTs; |
| 4383 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4384 | |
| 4385 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4386 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4387 | EVT PtrVT = PVTs[0]; |
| 4388 | unsigned NumValues = OutVTs.size(); |
| 4389 | SmallVector<SDValue, 4> Values(NumValues); |
| 4390 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4391 | |
| 4392 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4393 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4394 | DemoteStackSlot, |
| 4395 | DAG.getConstant(Offsets[i], PtrVT)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4396 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4397 | Add, NULL, Offsets[i], false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4398 | Values[i] = L; |
| 4399 | Chains[i] = L.getValue(1); |
| 4400 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4401 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4402 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4403 | MVT::Other, &Chains[0], NumValues); |
| 4404 | PendingLoads.push_back(Chain); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4405 | |
| 4406 | // Collect the legal value parts into potentially illegal values |
| 4407 | // that correspond to the original function's return values. |
| 4408 | SmallVector<EVT, 4> RetTys; |
| 4409 | RetTy = FTy->getReturnType(); |
| 4410 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4411 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4412 | SmallVector<SDValue, 4> ReturnValues; |
| 4413 | unsigned CurReg = 0; |
| 4414 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4415 | EVT VT = RetTys[I]; |
| 4416 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4417 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4418 | |
| 4419 | SDValue ReturnValue = |
| 4420 | getCopyFromParts(DAG, getCurDebugLoc(), SDNodeOrder, &Values[CurReg], NumRegs, |
| 4421 | RegisterVT, VT, AssertOp); |
| 4422 | ReturnValues.push_back(ReturnValue); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4423 | CurReg += NumRegs; |
| 4424 | } |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4425 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4426 | setValue(CS.getInstruction(), |
| 4427 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4428 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4429 | &ReturnValues[0], ReturnValues.size())); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4430 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4431 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4432 | |
| 4433 | // As a special case, a null chain means that a tail call has been emitted and |
| 4434 | // the DAG root is already updated. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4435 | if (Result.second.getNode()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4436 | DAG.setRoot(Result.second); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4437 | else |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4438 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4439 | |
| 4440 | if (LandingPad && MMI) { |
| 4441 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4442 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4443 | EndLabel = MMI->NextLabelID(); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4444 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4445 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4446 | |
| 4447 | // Inform MachineModuleInfo of range. |
| 4448 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4449 | } |
| 4450 | } |
| 4451 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4452 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4453 | /// value is equal or not-equal to zero. |
| 4454 | static bool IsOnlyUsedInZeroEqualityComparison(Value *V) { |
| 4455 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 4456 | UI != E; ++UI) { |
| 4457 | if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
| 4458 | if (IC->isEquality()) |
| 4459 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 4460 | if (C->isNullValue()) |
| 4461 | continue; |
| 4462 | // Unknown instruction. |
| 4463 | return false; |
| 4464 | } |
| 4465 | return true; |
| 4466 | } |
| 4467 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4468 | static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4469 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4470 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4471 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4472 | // input is from a string literal. |
| 4473 | if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
| 4474 | // Cast pointer to the type we really want to load. |
| 4475 | LoadInput = ConstantExpr::getBitCast(LoadInput, |
| 4476 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4477 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4478 | if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD)) |
| 4479 | return Builder.getValue(LoadCst); |
| 4480 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4481 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4482 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4483 | // still constant memory, the input chain can be the entry node. |
| 4484 | SDValue Root; |
| 4485 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4486 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4487 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4488 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4489 | Root = Builder.DAG.getEntryNode(); |
| 4490 | ConstantMemory = true; |
| 4491 | } else { |
| 4492 | // Do not serialize non-volatile loads against each other. |
| 4493 | Root = Builder.DAG.getRoot(); |
| 4494 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4495 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4496 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4497 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4498 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
| 4499 | false /*volatile*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4500 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4501 | if (!ConstantMemory) |
| 4502 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4503 | return LoadVal; |
| 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4508 | /// If so, return true and lower it, otherwise return false and it will be |
| 4509 | /// lowered like a normal call. |
| 4510 | bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) { |
| 4511 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
| 4512 | if (I.getNumOperands() != 4) |
| 4513 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4514 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4515 | Value *LHS = I.getOperand(1), *RHS = I.getOperand(2); |
| 4516 | if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) || |
| 4517 | !isa<IntegerType>(I.getOperand(3)->getType()) || |
| 4518 | !isa<IntegerType>(I.getType())) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4519 | return false; |
| 4520 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4521 | ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4522 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4523 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4524 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4525 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4526 | bool ActuallyDoIt = true; |
| 4527 | MVT LoadVT; |
| 4528 | const Type *LoadTy; |
| 4529 | switch (Size->getZExtValue()) { |
| 4530 | default: |
| 4531 | LoadVT = MVT::Other; |
| 4532 | LoadTy = 0; |
| 4533 | ActuallyDoIt = false; |
| 4534 | break; |
| 4535 | case 2: |
| 4536 | LoadVT = MVT::i16; |
| 4537 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4538 | break; |
| 4539 | case 4: |
| 4540 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4541 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4542 | break; |
| 4543 | case 8: |
| 4544 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4545 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4546 | break; |
| 4547 | /* |
| 4548 | case 16: |
| 4549 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4550 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4551 | LoadTy = VectorType::get(LoadTy, 4); |
| 4552 | break; |
| 4553 | */ |
| 4554 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4555 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4556 | // This turns into unaligned loads. We only do this if the target natively |
| 4557 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4558 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4559 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4560 | // Require that we can find a legal MVT, and only do this if the target |
| 4561 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4562 | // bloat the code. |
| 4563 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4564 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4565 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4566 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4567 | ActuallyDoIt = false; |
| 4568 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4569 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4570 | if (ActuallyDoIt) { |
| 4571 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4572 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4573 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4574 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4575 | ISD::SETNE); |
| 4576 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4577 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4578 | return true; |
| 4579 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4580 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4581 | |
| 4582 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4583 | return false; |
| 4584 | } |
| 4585 | |
| 4586 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4587 | void SelectionDAGBuilder::visitCall(CallInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4588 | const char *RenameFn = 0; |
| 4589 | if (Function *F = I.getCalledFunction()) { |
| 4590 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4591 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4592 | if (II) { |
| 4593 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4594 | RenameFn = visitIntrinsicCall(I, IID); |
| 4595 | if (!RenameFn) |
| 4596 | return; |
| 4597 | } |
| 4598 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4599 | if (unsigned IID = F->getIntrinsicID()) { |
| 4600 | RenameFn = visitIntrinsicCall(I, IID); |
| 4601 | if (!RenameFn) |
| 4602 | return; |
| 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4607 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4608 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4609 | StringRef Name = F->getName(); |
| 4610 | if (Name == "copysign" || Name == "copysignf") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4611 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4612 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4613 | I.getType() == I.getOperand(1)->getType() && |
| 4614 | I.getType() == I.getOperand(2)->getType()) { |
| 4615 | SDValue LHS = getValue(I.getOperand(1)); |
| 4616 | SDValue RHS = getValue(I.getOperand(2)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4617 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4618 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4619 | return; |
| 4620 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4621 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4622 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4623 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4624 | I.getType() == I.getOperand(1)->getType()) { |
| 4625 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4626 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4627 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4628 | return; |
| 4629 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4630 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4631 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4632 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4633 | I.getType() == I.getOperand(1)->getType() && |
| 4634 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4635 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4636 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4637 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4638 | return; |
| 4639 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4640 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4641 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4642 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4643 | I.getType() == I.getOperand(1)->getType() && |
| 4644 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4645 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4646 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4647 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4648 | return; |
| 4649 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4650 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4651 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4652 | I.getOperand(1)->getType()->isFloatingPoint() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4653 | I.getType() == I.getOperand(1)->getType() && |
| 4654 | I.onlyReadsMemory()) { |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4655 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4656 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4657 | Tmp.getValueType(), Tmp)); |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4658 | return; |
| 4659 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4660 | } else if (Name == "memcmp") { |
| 4661 | if (visitMemCmpCall(I)) |
| 4662 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4663 | } |
| 4664 | } |
| 4665 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4666 | visitInlineAsm(&I); |
| 4667 | return; |
| 4668 | } |
| 4669 | |
| 4670 | SDValue Callee; |
| 4671 | if (!RenameFn) |
| 4672 | Callee = getValue(I.getOperand(0)); |
| 4673 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4674 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4675 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4676 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4677 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 4678 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4681 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4682 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4683 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4684 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4685 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4686 | unsigned Order, SDValue &Chain, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4687 | SDValue *Flag) const { |
| 4688 | // Assemble the legal parts into the final values. |
| 4689 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4690 | SmallVector<SDValue, 8> Parts; |
| 4691 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4692 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4693 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4694 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4695 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4696 | |
| 4697 | Parts.resize(NumRegs); |
| 4698 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4699 | SDValue P; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4700 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4701 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4702 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4703 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4704 | *Flag = P.getValue(2); |
| 4705 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4706 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4707 | Chain = P.getValue(1); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4708 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4709 | // If the source register was virtual and if we know something about it, |
| 4710 | // add an assert node. |
| 4711 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4712 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4713 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4714 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4715 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4716 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4717 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4718 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4719 | unsigned NumSignBits = LOI.NumSignBits; |
| 4720 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4721 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4722 | // FIXME: We capture more information than the dag can represent. For |
| 4723 | // now, just use the tightest assertzext/assertsext possible. |
| 4724 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4725 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4726 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4727 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4728 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4729 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4730 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4731 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4732 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4733 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4734 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4735 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4736 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4737 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4738 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4739 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4740 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4741 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4742 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4743 | if (FromVT != MVT::Other) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4744 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4745 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4746 | } |
| 4747 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4748 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4749 | Parts[i] = P; |
| 4750 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4751 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4752 | Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4753 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4754 | Part += NumRegs; |
| 4755 | Parts.clear(); |
| 4756 | } |
| 4757 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4758 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 4759 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4760 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4764 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4765 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4766 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4767 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4768 | unsigned Order, SDValue &Chain, |
| 4769 | SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4770 | // Get the list of the values's legal parts. |
| 4771 | unsigned NumRegs = Regs.size(); |
| 4772 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4773 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4774 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4775 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4776 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4777 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4778 | getCopyToParts(DAG, dl, Order, |
| 4779 | Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4780 | &Parts[Part], NumParts, RegisterVT); |
| 4781 | Part += NumParts; |
| 4782 | } |
| 4783 | |
| 4784 | // Copy the parts into the registers. |
| 4785 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4786 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4787 | SDValue Part; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4788 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4789 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4790 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4791 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4792 | *Flag = Part.getValue(1); |
| 4793 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4794 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4795 | Chains[i] = Part.getValue(0); |
| 4796 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4797 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4798 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4799 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4800 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4801 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4802 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4803 | // user as well as a successor (the TF operands are flagged to the user). |
| 4804 | // c1, f1 = CopyToReg |
| 4805 | // c2, f2 = CopyToReg |
| 4806 | // c3 = TokenFactor c1, c2 |
| 4807 | // ... |
| 4808 | // = op c3, ..., f2 |
| 4809 | Chain = Chains[NumRegs-1]; |
| 4810 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4811 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4815 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4816 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4817 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4818 | bool HasMatching,unsigned MatchingIdx, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 4819 | SelectionDAG &DAG, unsigned Order, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4820 | std::vector<SDValue> &Ops) const { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4821 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4822 | unsigned Flag = Code | (Regs.size() << 3); |
| 4823 | if (HasMatching) |
| 4824 | Flag |= 0x80000000 | (MatchingIdx << 16); |
Dale Johannesen | 9949933 | 2009-12-23 07:32:51 +0000 | [diff] [blame] | 4825 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 4826 | Ops.push_back(Res); |
| 4827 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4828 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4829 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4830 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4831 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4832 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4833 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4834 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4838 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4839 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4840 | /// register class for the register. Otherwise, return null. |
| 4841 | static const TargetRegisterClass * |
| 4842 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4843 | const TargetLowering &TLI, |
| 4844 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4845 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4846 | const TargetRegisterClass *FoundRC = 0; |
| 4847 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4848 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4849 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | |
| 4851 | const TargetRegisterClass *RC = *RCI; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4852 | // If none of the the value types for this register class are valid, we |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4853 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4854 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4855 | I != E; ++I) { |
| 4856 | if (TLI.isTypeLegal(*I)) { |
| 4857 | // If we have already found this register in a different register class, |
| 4858 | // choose the one with the largest VT specified. For example, on |
| 4859 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4860 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4861 | ThisVT = *I; |
| 4862 | break; |
| 4863 | } |
| 4864 | } |
| 4865 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4866 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4867 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4868 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4869 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4870 | // frame pointer in functions that need it (due to them not being taken |
| 4871 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4872 | // yet). This is a slight code pessimization, but should still work. |
| 4873 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4874 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4875 | if (*I == Reg) { |
| 4876 | // We found a matching register class. Keep looking at others in case |
| 4877 | // we find one with larger registers that this physreg is also in. |
| 4878 | FoundRC = RC; |
| 4879 | FoundVT = ThisVT; |
| 4880 | break; |
| 4881 | } |
| 4882 | } |
| 4883 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4884 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4885 | |
| 4886 | |
| 4887 | namespace llvm { |
| 4888 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4889 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4890 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4891 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4892 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4893 | /// CallOperand - If this is the result output operand or a clobber |
| 4894 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4895 | /// This gets modified as the asm is processed. |
| 4896 | SDValue CallOperand; |
| 4897 | |
| 4898 | /// AssignedRegs - If this is a register or register class operand, this |
| 4899 | /// contains the set of register corresponding to the operand. |
| 4900 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4901 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4902 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4903 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4904 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4905 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4906 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4907 | /// busy in OutputRegs/InputRegs. |
| 4908 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4909 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4910 | std::set<unsigned> &InputRegs, |
| 4911 | const TargetRegisterInfo &TRI) const { |
| 4912 | if (isOutReg) { |
| 4913 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4914 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4915 | } |
| 4916 | if (isInReg) { |
| 4917 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4918 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4919 | } |
| 4920 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4921 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4922 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4923 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4924 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4925 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4926 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4927 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4928 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4929 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4930 | if (isa<BasicBlock>(CallOperandVal)) |
| 4931 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4932 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4933 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4934 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4935 | // If this is an indirect operand, the operand is a pointer to the |
| 4936 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4937 | if (isIndirect) { |
| 4938 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4939 | if (!PtrTy) |
| 4940 | llvm_report_error("Indirect operand for inline asm not a pointer!"); |
| 4941 | OpTy = PtrTy->getElementType(); |
| 4942 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4943 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4944 | // If OpTy is not a single value, it may be a struct/union that we |
| 4945 | // can tile with integers. |
| 4946 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4947 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4948 | switch (BitSize) { |
| 4949 | default: break; |
| 4950 | case 1: |
| 4951 | case 8: |
| 4952 | case 16: |
| 4953 | case 32: |
| 4954 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4955 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4956 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4957 | break; |
| 4958 | } |
| 4959 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4960 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4961 | return TLI.getValueType(OpTy, true); |
| 4962 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4963 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4964 | private: |
| 4965 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4966 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4967 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4968 | const TargetRegisterInfo &TRI) { |
| 4969 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4970 | Regs.insert(Reg); |
| 4971 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4972 | for (; *Aliases; ++Aliases) |
| 4973 | Regs.insert(*Aliases); |
| 4974 | } |
| 4975 | }; |
| 4976 | } // end llvm namespace. |
| 4977 | |
| 4978 | |
| 4979 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4980 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 4981 | /// register allocator to handle the assignment process. However, if the asm |
| 4982 | /// uses features that we can't model on machineinstrs, we have SDISel do the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4983 | /// allocation. This produces generally horrible, but correct, code. |
| 4984 | /// |
| 4985 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4986 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4987 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4988 | void SelectionDAGBuilder:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 4989 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4990 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4991 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 4992 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4993 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4994 | // Compute whether this value requires an input register, an output register, |
| 4995 | // or both. |
| 4996 | bool isOutReg = false; |
| 4997 | bool isInReg = false; |
| 4998 | switch (OpInfo.Type) { |
| 4999 | case InlineAsm::isOutput: |
| 5000 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5001 | |
| 5002 | // If there is an input constraint that matches this, we need to reserve |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5003 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5004 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5005 | break; |
| 5006 | case InlineAsm::isInput: |
| 5007 | isInReg = true; |
| 5008 | isOutReg = false; |
| 5009 | break; |
| 5010 | case InlineAsm::isClobber: |
| 5011 | isOutReg = true; |
| 5012 | isInReg = true; |
| 5013 | break; |
| 5014 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5015 | |
| 5016 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5017 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5018 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5019 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5020 | // If this is a constraint for a single physreg, or a constraint for a |
| 5021 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5022 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5023 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5024 | OpInfo.ConstraintVT); |
| 5025 | |
| 5026 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5027 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5028 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5029 | // cast of the input value. More generally, handle any case where the input |
| 5030 | // value disagrees with the register class we plan to stick this in. |
| 5031 | if (OpInfo.Type == InlineAsm::isInput && |
| 5032 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5033 | // Try to convert to the first EVT that the reg class contains. If the |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5034 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5035 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5036 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5037 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5038 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5039 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5040 | OpInfo.ConstraintVT = RegVT; |
| 5041 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5042 | // If the input is a FP value and we want it in FP registers, do a |
| 5043 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5044 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5045 | // machine. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5046 | RegVT = EVT::getIntegerVT(Context, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5047 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5048 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5049 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5050 | OpInfo.ConstraintVT = RegVT; |
| 5051 | } |
| 5052 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5053 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5054 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5055 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5056 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5057 | EVT RegVT; |
| 5058 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5059 | |
| 5060 | // If this is a constraint for a specific physical register, like {r17}, |
| 5061 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5062 | if (unsigned AssignedReg = PhysReg.first) { |
| 5063 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5064 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5065 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5066 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5067 | // Get the actual register value type. This is important, because the user |
| 5068 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5069 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5070 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5071 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5072 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5073 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5074 | |
| 5075 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5076 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5077 | TargetRegisterClass::iterator I = RC->begin(); |
| 5078 | for (; *I != AssignedReg; ++I) |
| 5079 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5080 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5081 | // Already added the first reg. |
| 5082 | --NumRegs; ++I; |
| 5083 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5084 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5085 | Regs.push_back(*I); |
| 5086 | } |
| 5087 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5088 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5089 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5090 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5091 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5092 | return; |
| 5093 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5094 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5095 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5096 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5097 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5098 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5099 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5100 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5101 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5102 | // Create the appropriate number of virtual registers. |
| 5103 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5104 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5105 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5106 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5107 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5108 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5109 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5111 | // This is a reference to a register class that doesn't directly correspond |
| 5112 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5113 | // registers from the class. |
| 5114 | std::vector<unsigned> RegClassRegs |
| 5115 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5116 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5117 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5118 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5119 | unsigned NumAllocated = 0; |
| 5120 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5121 | unsigned Reg = RegClassRegs[i]; |
| 5122 | // See if this register is available. |
| 5123 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5124 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5125 | // Make sure we find consecutive registers. |
| 5126 | NumAllocated = 0; |
| 5127 | continue; |
| 5128 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5129 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5130 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5131 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5132 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5133 | if (!RC) { // Couldn't allocate this register. |
| 5134 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5135 | NumAllocated = 0; |
| 5136 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5137 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5138 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5139 | // Okay, this register is good, we can use it. |
| 5140 | ++NumAllocated; |
| 5141 | |
| 5142 | // If we allocated enough consecutive registers, succeed. |
| 5143 | if (NumAllocated == NumRegs) { |
| 5144 | unsigned RegStart = (i-NumAllocated)+1; |
| 5145 | unsigned RegEnd = i+1; |
| 5146 | // Mark all of the allocated registers used. |
| 5147 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5148 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5149 | |
| 5150 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5151 | OpInfo.ConstraintVT); |
| 5152 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5153 | return; |
| 5154 | } |
| 5155 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5156 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5157 | // Otherwise, we couldn't allocate enough registers for this. |
| 5158 | } |
| 5159 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5160 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5161 | /// processed uses a memory 'm' constraint. |
| 5162 | static bool |
| 5163 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5164 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5165 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5166 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5167 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5168 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5169 | if (CType == TargetLowering::C_Memory) |
| 5170 | return true; |
| 5171 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5172 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5173 | // Indirect operand accesses access memory. |
| 5174 | if (CI.isIndirect) |
| 5175 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | return false; |
| 5179 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5180 | |
| 5181 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5182 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5183 | void SelectionDAGBuilder::visitInlineAsm(CallSite CS) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5184 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5185 | |
| 5186 | /// ConstraintOperands - Information about all of the constraints. |
| 5187 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5188 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5189 | std::set<unsigned> OutputRegs, InputRegs; |
| 5190 | |
| 5191 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5192 | // ConstraintOperands list. |
| 5193 | std::vector<InlineAsm::ConstraintInfo> |
| 5194 | ConstraintInfos = IA->ParseConstraints(); |
| 5195 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5196 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5197 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5198 | SDValue Chain, Flag; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5199 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5200 | // We won't need to flush pending loads if this asm doesn't touch |
| 5201 | // memory and is nonvolatile. |
| 5202 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5203 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5204 | else |
| 5205 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5206 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5207 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5208 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5209 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5210 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5211 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5212 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5213 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5214 | |
| 5215 | // Compute the value type for each operand. |
| 5216 | switch (OpInfo.Type) { |
| 5217 | case InlineAsm::isOutput: |
| 5218 | // Indirect outputs just consume an argument. |
| 5219 | if (OpInfo.isIndirect) { |
| 5220 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5221 | break; |
| 5222 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5223 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5224 | // The return value of the call is this value. As such, there is no |
| 5225 | // corresponding argument. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5226 | assert(!CS.getType()->isVoidTy() && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5227 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5228 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5229 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5230 | } else { |
| 5231 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5232 | OpVT = TLI.getValueType(CS.getType()); |
| 5233 | } |
| 5234 | ++ResNo; |
| 5235 | break; |
| 5236 | case InlineAsm::isInput: |
| 5237 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5238 | break; |
| 5239 | case InlineAsm::isClobber: |
| 5240 | // Nothing to do. |
| 5241 | break; |
| 5242 | } |
| 5243 | |
| 5244 | // If this is an input or an indirect output, process the call argument. |
| 5245 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5246 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5247 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5248 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5249 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5250 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5251 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5252 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5253 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5254 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5255 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5256 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5257 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5258 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5259 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5260 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5261 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5262 | // Second pass over the constraints: compute which constraint option to use |
| 5263 | // and assign registers to constraints that want a specific physreg. |
| 5264 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5265 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5266 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5267 | // If this is an output operand with a matching input operand, look up the |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5268 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5269 | // other is floating point, or their sizes are different, flag it as an |
| 5270 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5271 | if (OpInfo.hasMatchingInput()) { |
| 5272 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5273 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5274 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5275 | Input.ConstraintVT.isInteger()) || |
| 5276 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5277 | Input.ConstraintVT.getSizeInBits())) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5278 | llvm_report_error("Unsupported asm: input constraint" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5279 | " with a matching output constraint of incompatible" |
| 5280 | " type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5281 | } |
| 5282 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5286 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5287 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5288 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5289 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5290 | // need to to provide an address for the memory input. |
| 5291 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5292 | !OpInfo.isIndirect) { |
| 5293 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5294 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5295 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5296 | // Memory operands really want the address of the value. If we don't have |
| 5297 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5298 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5299 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5300 | // If the operand is a float, integer, or vector constant, spill to a |
| 5301 | // constant pool entry to get its address. |
| 5302 | Value *OpVal = OpInfo.CallOperandVal; |
| 5303 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5304 | isa<ConstantVector>(OpVal)) { |
| 5305 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5306 | TLI.getPointerTy()); |
| 5307 | } else { |
| 5308 | // Otherwise, create a stack slot and emit a store to it before the |
| 5309 | // asm. |
| 5310 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5311 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5312 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5313 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5314 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5315 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5316 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5317 | OpInfo.CallOperand, StackSlot, NULL, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5318 | OpInfo.CallOperand = StackSlot; |
| 5319 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5320 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5321 | // There is no longer a Value* corresponding to this operand. |
| 5322 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5323 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5324 | // It is now an indirect operand. |
| 5325 | OpInfo.isIndirect = true; |
| 5326 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5327 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5328 | // If this constraint is for a specific register, allocate it before |
| 5329 | // anything else. |
| 5330 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5331 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5332 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5333 | |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5334 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5335 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5336 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5337 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5338 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5339 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5340 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5341 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5342 | // to be. |
| 5343 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5344 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5347 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5348 | std::vector<SDValue> AsmNodeOperands; |
| 5349 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5350 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 5351 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5352 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5353 | |
| 5354 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5355 | // Loop over all of the inputs, copying the operand values into the |
| 5356 | // appropriate registers and processing the output regs. |
| 5357 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5358 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5359 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5360 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5361 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5362 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5363 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5364 | |
| 5365 | switch (OpInfo.Type) { |
| 5366 | case InlineAsm::isOutput: { |
| 5367 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5368 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5369 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5370 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5371 | |
| 5372 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5373 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5374 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5375 | TLI.getPointerTy())); |
| 5376 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5377 | break; |
| 5378 | } |
| 5379 | |
| 5380 | // Otherwise, this is a register or register class output. |
| 5381 | |
| 5382 | // Copy the output from the appropriate register. Find a register that |
| 5383 | // we can use. |
| 5384 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5385 | llvm_report_error("Couldn't allocate output reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5386 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | // If this is an indirect operand, store through the pointer after the |
| 5390 | // asm. |
| 5391 | if (OpInfo.isIndirect) { |
| 5392 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5393 | OpInfo.CallOperandVal)); |
| 5394 | } else { |
| 5395 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5396 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5397 | // Concatenate this output onto the outputs list. |
| 5398 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5399 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5400 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5401 | // Add information to the INLINEASM node to know that this register is |
| 5402 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5403 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5404 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5405 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5406 | false, |
| 5407 | 0, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5408 | DAG, SDNodeOrder, |
| 5409 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5410 | break; |
| 5411 | } |
| 5412 | case InlineAsm::isInput: { |
| 5413 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5414 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5415 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5416 | // If this is required to match an output register we have already set, |
| 5417 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5418 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5419 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5420 | // Scan until we find the definition we already emitted of this operand. |
| 5421 | // When we find it, create a RegsForValue operand. |
| 5422 | unsigned CurOp = 2; // The first operand. |
| 5423 | for (; OperandNo; --OperandNo) { |
| 5424 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5425 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5426 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5427 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5428 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5429 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5430 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5431 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5434 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5435 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5436 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5437 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5438 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5439 | if (OpInfo.isIndirect) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5440 | llvm_report_error("Don't know how to handle tied indirect " |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5441 | "register inputs yet!"); |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5442 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5443 | RegsForValue MatchedRegs; |
| 5444 | MatchedRegs.TLI = &TLI; |
| 5445 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5446 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5447 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5448 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5449 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5450 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5451 | MatchedRegs.Regs.push_back |
| 5452 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5453 | |
| 5454 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5455 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 5456 | SDNodeOrder, Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5457 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5458 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5459 | DAG, SDNodeOrder, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5460 | break; |
| 5461 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5462 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5463 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5464 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5465 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5466 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5467 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5468 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5469 | TLI.getPointerTy())); |
| 5470 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5471 | break; |
| 5472 | } |
| 5473 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5474 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5475 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5476 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5477 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5478 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5479 | std::vector<SDValue> Ops; |
| 5480 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5481 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5482 | if (Ops.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5483 | llvm_report_error("Invalid operand for inline asm" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5484 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5485 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5486 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5487 | // Add information to the INLINEASM node to know about this input. |
| 5488 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5489 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5490 | TLI.getPointerTy())); |
| 5491 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5492 | break; |
| 5493 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5494 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5495 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5496 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5497 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5498 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5499 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5500 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5501 | TLI.getPointerTy())); |
| 5502 | AsmNodeOperands.push_back(InOperandVal); |
| 5503 | break; |
| 5504 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5505 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5506 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5507 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5508 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5509 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5510 | "Don't know how to handle indirect register inputs yet!"); |
| 5511 | |
| 5512 | // Copy the input into the appropriate registers. |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5513 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5514 | llvm_report_error("Couldn't allocate input reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5515 | " constraint '"+ OpInfo.ConstraintCode +"'!"); |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5516 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5517 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5518 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 5519 | SDNodeOrder, Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5520 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5521 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5522 | DAG, SDNodeOrder, |
| 5523 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5524 | break; |
| 5525 | } |
| 5526 | case InlineAsm::isClobber: { |
| 5527 | // Add the clobbered value to the operand list, so that the register |
| 5528 | // allocator is aware that the physreg got clobbered. |
| 5529 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5530 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5531 | false, 0, DAG, SDNodeOrder, |
| 5532 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5533 | break; |
| 5534 | } |
| 5535 | } |
| 5536 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5537 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5538 | // Finish up input operands. |
| 5539 | AsmNodeOperands[0] = Chain; |
| 5540 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5541 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5542 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5543 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5544 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5545 | Flag = Chain.getValue(1); |
| 5546 | |
| 5547 | // If this asm returns a register value, copy the result from that register |
| 5548 | // and set it as the value of the call. |
| 5549 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5550 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 5551 | SDNodeOrder, Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5552 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5553 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5554 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5555 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5556 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5557 | // If any of the results of the inline asm is a vector, it may have the |
| 5558 | // wrong width/num elts. This can happen for register classes that can |
| 5559 | // contain multiple different value types. The preg or vreg allocated may |
| 5560 | // not have the same VT as was expected. Convert it to the right type |
| 5561 | // with bit_convert. |
| 5562 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5563 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5564 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5565 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5566 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5567 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5568 | // If a result value was tied to an input value, the computed result may |
| 5569 | // have a wider width than the expected result. Extract the relevant |
| 5570 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5571 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5572 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5573 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5574 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5575 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5576 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5577 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5578 | // Don't need to use this as a chain in this case. |
| 5579 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5580 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5581 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5582 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5583 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5584 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5585 | // Process indirect outputs, first output all of the flagged copies out of |
| 5586 | // physregs. |
| 5587 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5588 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5589 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5590 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 5591 | SDNodeOrder, Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5592 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5593 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5594 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5595 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5596 | // Emit the non-flagged stores from the physregs. |
| 5597 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5598 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5599 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5600 | StoresToEmit[i].first, |
| 5601 | getValue(StoresToEmit[i].second), |
| 5602 | StoresToEmit[i].second, 0); |
| 5603 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5606 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5607 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5608 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5609 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5610 | DAG.setRoot(Chain); |
| 5611 | } |
| 5612 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5613 | void SelectionDAGBuilder::visitVAStart(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5614 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5615 | MVT::Other, getRoot(), |
| 5616 | getValue(I.getOperand(1)), |
| 5617 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5620 | void SelectionDAGBuilder::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5621 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5622 | getRoot(), getValue(I.getOperand(0)), |
| 5623 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5624 | setValue(&I, V); |
| 5625 | DAG.setRoot(V.getValue(1)); |
| 5626 | } |
| 5627 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5628 | void SelectionDAGBuilder::visitVAEnd(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5629 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5630 | MVT::Other, getRoot(), |
| 5631 | getValue(I.getOperand(1)), |
| 5632 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5635 | void SelectionDAGBuilder::visitVACopy(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5636 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5637 | MVT::Other, getRoot(), |
| 5638 | getValue(I.getOperand(1)), |
| 5639 | getValue(I.getOperand(2)), |
| 5640 | DAG.getSrcValue(I.getOperand(1)), |
| 5641 | DAG.getSrcValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5644 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5645 | /// implementation, which just calls LowerCall. |
| 5646 | /// FIXME: When all targets are |
| 5647 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | std::pair<SDValue, SDValue> |
| 5649 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5650 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5651 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5652 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5653 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5654 | SDValue Callee, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5655 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl, |
| 5656 | unsigned Order) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5657 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5658 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5659 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5660 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5661 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5662 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5663 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5664 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5665 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5666 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5667 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5668 | ISD::ArgFlagsTy Flags; |
| 5669 | unsigned OriginalAlignment = |
| 5670 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5671 | |
| 5672 | if (Args[i].isZExt) |
| 5673 | Flags.setZExt(); |
| 5674 | if (Args[i].isSExt) |
| 5675 | Flags.setSExt(); |
| 5676 | if (Args[i].isInReg) |
| 5677 | Flags.setInReg(); |
| 5678 | if (Args[i].isSRet) |
| 5679 | Flags.setSRet(); |
| 5680 | if (Args[i].isByVal) { |
| 5681 | Flags.setByVal(); |
| 5682 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5683 | const Type *ElementTy = Ty->getElementType(); |
| 5684 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5685 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5686 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5687 | // info is not there but there are cases it cannot get right. |
| 5688 | if (Args[i].Alignment) |
| 5689 | FrameAlign = Args[i].Alignment; |
| 5690 | Flags.setByValAlign(FrameAlign); |
| 5691 | Flags.setByValSize(FrameSize); |
| 5692 | } |
| 5693 | if (Args[i].isNest) |
| 5694 | Flags.setNest(); |
| 5695 | Flags.setOrigAlign(OriginalAlignment); |
| 5696 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5697 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5698 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5699 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5700 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5701 | |
| 5702 | if (Args[i].isSExt) |
| 5703 | ExtendKind = ISD::SIGN_EXTEND; |
| 5704 | else if (Args[i].isZExt) |
| 5705 | ExtendKind = ISD::ZERO_EXTEND; |
| 5706 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5707 | getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts, |
| 5708 | PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5709 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5710 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5711 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5712 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5713 | if (NumParts > 1 && j == 0) |
| 5714 | MyFlags.Flags.setSplit(); |
| 5715 | else if (j != 0) |
| 5716 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5717 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5718 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5719 | } |
| 5720 | } |
| 5721 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5722 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5723 | // Handle the incoming return values from the call. |
| 5724 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5725 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5726 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5727 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5728 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5729 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5730 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5731 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5732 | ISD::InputArg MyFlags; |
| 5733 | MyFlags.VT = RegisterVT; |
| 5734 | MyFlags.Used = isReturnValueUsed; |
| 5735 | if (RetSExt) |
| 5736 | MyFlags.Flags.setSExt(); |
| 5737 | if (RetZExt) |
| 5738 | MyFlags.Flags.setZExt(); |
| 5739 | if (isInreg) |
| 5740 | MyFlags.Flags.setInReg(); |
| 5741 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5742 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5745 | SmallVector<SDValue, 4> InVals; |
Evan Cheng | 022d9e1 | 2010-02-02 23:55:14 +0000 | [diff] [blame] | 5746 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5747 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5748 | |
| 5749 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5750 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5751 | "LowerCall didn't return a valid chain!"); |
| 5752 | assert((!isTailCall || InVals.empty()) && |
| 5753 | "LowerCall emitted a return value for a tail call!"); |
| 5754 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5755 | "LowerCall didn't emit the correct number of values!"); |
| 5756 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5757 | assert(InVals[i].getNode() && |
| 5758 | "LowerCall emitted a null value!"); |
| 5759 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5760 | "LowerCall emitted a value with the wrong type!"); |
| 5761 | }); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5762 | |
| 5763 | // For a tail call, the return value is merely live-out and there aren't |
| 5764 | // any nodes in the DAG representing it. Return a special value to |
| 5765 | // indicate that a tail call has been emitted and no more Instructions |
| 5766 | // should be processed in the current block. |
| 5767 | if (isTailCall) { |
| 5768 | DAG.setRoot(Chain); |
| 5769 | return std::make_pair(SDValue(), SDValue()); |
| 5770 | } |
| 5771 | |
| 5772 | // Collect the legal value parts into potentially illegal values |
| 5773 | // that correspond to the original function's return values. |
| 5774 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5775 | if (RetSExt) |
| 5776 | AssertOp = ISD::AssertSext; |
| 5777 | else if (RetZExt) |
| 5778 | AssertOp = ISD::AssertZext; |
| 5779 | SmallVector<SDValue, 4> ReturnValues; |
| 5780 | unsigned CurReg = 0; |
| 5781 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5782 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5783 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5784 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5785 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5786 | ReturnValues.push_back(getCopyFromParts(DAG, dl, Order, &InVals[CurReg], |
| 5787 | NumRegs, RegisterVT, VT, |
| 5788 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5789 | CurReg += NumRegs; |
| 5790 | } |
| 5791 | |
| 5792 | // For a function returning void, there is no return value. We can't create |
| 5793 | // such a node, so we just return a null return value in that case. In |
| 5794 | // that case, nothing will actualy look at the value. |
| 5795 | if (ReturnValues.empty()) |
| 5796 | return std::make_pair(SDValue(), Chain); |
| 5797 | |
| 5798 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5799 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5800 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5801 | return std::make_pair(Res, Chain); |
| 5802 | } |
| 5803 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5804 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5805 | SmallVectorImpl<SDValue> &Results, |
| 5806 | SelectionDAG &DAG) { |
| 5807 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5808 | if (Res.getNode()) |
| 5809 | Results.push_back(Res); |
| 5810 | } |
| 5811 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5813 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5814 | return SDValue(); |
| 5815 | } |
| 5816 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5817 | void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5818 | SDValue Op = getValue(V); |
| 5819 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5820 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5821 | "Copy from a reg to the same reg!"); |
| 5822 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5823 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5824 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5825 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 5826 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5827 | PendingExports.push_back(Chain); |
| 5828 | } |
| 5829 | |
| 5830 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5831 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 5832 | void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5833 | // If this is the entry block, emit arguments. |
| 5834 | Function &F = *LLVMBB->getParent(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5835 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5836 | SDValue OldRoot = DAG.getRoot(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5837 | DebugLoc dl = SDB->getCurDebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5838 | const TargetData *TD = TLI.getTargetData(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5839 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5840 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5841 | // Check whether the function can return without sret-demotion. |
| 5842 | SmallVector<EVT, 4> OutVTs; |
| 5843 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5844 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5845 | OutVTs, OutsFlags, TLI); |
| 5846 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 5847 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5848 | FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5849 | OutVTs, OutsFlags, DAG); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5850 | if (!FLI.CanLowerReturn) { |
| 5851 | // Put in an sret pointer parameter before all the other parameters. |
| 5852 | SmallVector<EVT, 1> ValueVTs; |
| 5853 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5854 | |
| 5855 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5856 | // or one register. |
| 5857 | ISD::ArgFlagsTy Flags; |
| 5858 | Flags.setSRet(); |
| 5859 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]); |
| 5860 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5861 | Ins.push_back(RetArg); |
| 5862 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5863 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5864 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5865 | unsigned Idx = 1; |
| 5866 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5867 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5868 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5869 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5870 | bool isArgValueUsed = !I->use_empty(); |
| 5871 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5872 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5873 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5874 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5875 | ISD::ArgFlagsTy Flags; |
| 5876 | unsigned OriginalAlignment = |
| 5877 | TD->getABITypeAlignment(ArgTy); |
| 5878 | |
| 5879 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5880 | Flags.setZExt(); |
| 5881 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5882 | Flags.setSExt(); |
| 5883 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5884 | Flags.setInReg(); |
| 5885 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5886 | Flags.setSRet(); |
| 5887 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5888 | Flags.setByVal(); |
| 5889 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5890 | const Type *ElementTy = Ty->getElementType(); |
| 5891 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5892 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5893 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5894 | // this info is not there but there are cases it cannot get right. |
| 5895 | if (F.getParamAlignment(Idx)) |
| 5896 | FrameAlign = F.getParamAlignment(Idx); |
| 5897 | Flags.setByValAlign(FrameAlign); |
| 5898 | Flags.setByValSize(FrameSize); |
| 5899 | } |
| 5900 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5901 | Flags.setNest(); |
| 5902 | Flags.setOrigAlign(OriginalAlignment); |
| 5903 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5904 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5905 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5906 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5907 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5908 | if (NumRegs > 1 && i == 0) |
| 5909 | MyFlags.Flags.setSplit(); |
| 5910 | // if it isn't first piece, alignment must be 1 |
| 5911 | else if (i > 0) |
| 5912 | MyFlags.Flags.setOrigAlign(1); |
| 5913 | Ins.push_back(MyFlags); |
| 5914 | } |
| 5915 | } |
| 5916 | } |
| 5917 | |
| 5918 | // Call the target to set up the argument values. |
| 5919 | SmallVector<SDValue, 8> InVals; |
| 5920 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5921 | F.isVarArg(), Ins, |
| 5922 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5923 | |
| 5924 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5925 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5926 | "LowerFormalArguments didn't return a valid chain!"); |
| 5927 | assert(InVals.size() == Ins.size() && |
| 5928 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5929 | DEBUG({ |
| 5930 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5931 | assert(InVals[i].getNode() && |
| 5932 | "LowerFormalArguments emitted a null value!"); |
| 5933 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5934 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5935 | } |
| 5936 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5937 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5938 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5939 | DAG.setRoot(NewRoot); |
| 5940 | |
| 5941 | // Set up the argument values. |
| 5942 | unsigned i = 0; |
| 5943 | Idx = 1; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5944 | if (!FLI.CanLowerReturn) { |
| 5945 | // Create a virtual register for the sret pointer, and put in a copy |
| 5946 | // from the sret argument into it. |
| 5947 | SmallVector<EVT, 1> ValueVTs; |
| 5948 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5949 | EVT VT = ValueVTs[0]; |
| 5950 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5951 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5952 | SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5953 | RegVT, VT, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5954 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5955 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5956 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 5957 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
| 5958 | FLI.DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5959 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 5960 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5961 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5962 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5963 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 5964 | // Idx indexes LLVM arguments. Don't touch it. |
| 5965 | ++i; |
| 5966 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5967 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5968 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 5969 | ++I, ++Idx) { |
| 5970 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5971 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5972 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5973 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5974 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5975 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5976 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5977 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5978 | |
| 5979 | if (!I->use_empty()) { |
| 5980 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5981 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5982 | AssertOp = ISD::AssertSext; |
| 5983 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5984 | AssertOp = ISD::AssertZext; |
| 5985 | |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5986 | ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5987 | NumParts, PartVT, VT, |
| 5988 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5989 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5990 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5991 | i += NumParts; |
| 5992 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5993 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5994 | if (!I->use_empty()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5995 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 5996 | SDB->getCurDebugLoc()); |
| 5997 | SDB->setValue(I, Res); |
| 5998 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5999 | // If this argument is live outside of the entry block, insert a copy from |
| 6000 | // whereever we got it to the vreg that other BB's will reference it as. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6001 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6002 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6003 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6004 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6005 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6006 | |
| 6007 | // Finally, if the target has anything special to do, allow it to do so. |
| 6008 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6009 | EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6013 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6014 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6015 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6016 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6017 | /// the end. |
| 6018 | /// |
| 6019 | void |
| 6020 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 6021 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 6022 | |
| 6023 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6024 | |
| 6025 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6026 | // from this block. |
| 6027 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6028 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6029 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6030 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6031 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6032 | // If this terminator has multiple identical successors (common for |
| 6033 | // switches), only handle each succ once. |
| 6034 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6035 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6036 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6037 | PHINode *PN; |
| 6038 | |
| 6039 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6040 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6041 | // emitted yet. |
| 6042 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6043 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6044 | // Ignore dead phi's. |
| 6045 | if (PN->use_empty()) continue; |
| 6046 | |
| 6047 | unsigned Reg; |
| 6048 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6049 | |
| 6050 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6051 | unsigned &RegOut = SDB->ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6052 | if (RegOut == 0) { |
| 6053 | RegOut = FuncInfo->CreateRegForValue(C); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6054 | SDB->CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6055 | } |
| 6056 | Reg = RegOut; |
| 6057 | } else { |
| 6058 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 6059 | if (Reg == 0) { |
| 6060 | assert(isa<AllocaInst>(PHIOp) && |
| 6061 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 6062 | "Didn't codegen value into a register!??"); |
| 6063 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6064 | SDB->CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6065 | } |
| 6066 | } |
| 6067 | |
| 6068 | // Remember that this register needs to added to the machine PHI node as |
| 6069 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6070 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6071 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6072 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6073 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6074 | unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6075 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6076 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6077 | Reg += NumRegisters; |
| 6078 | } |
| 6079 | } |
| 6080 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6081 | SDB->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6084 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 6085 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 6086 | /// creating SelectionDAG nodes. |
| 6087 | /// |
| 6088 | bool |
| 6089 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 6090 | FastISel *F) { |
| 6091 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6092 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6093 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6094 | unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6095 | |
| 6096 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6097 | // from this block. |
| 6098 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6099 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6100 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6101 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6102 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6103 | // If this terminator has multiple identical successors (common for |
| 6104 | // switches), only handle each succ once. |
| 6105 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6106 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6107 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6108 | PHINode *PN; |
| 6109 | |
| 6110 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6111 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6112 | // emitted yet. |
| 6113 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6114 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6115 | // Ignore dead phi's. |
| 6116 | if (PN->use_empty()) continue; |
| 6117 | |
| 6118 | // Only handle legal types. Two interesting things to note here. First, |
| 6119 | // by bailing out early, we may leave behind some dead instructions, |
| 6120 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6121 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6122 | // use CreateRegForValue to create registers, so it always creates |
| 6123 | // exactly one register for each non-void instruction. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6124 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6125 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 6126 | // Promote MVT::i1. |
| 6127 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6128 | VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6129 | else { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6130 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6131 | return false; |
| 6132 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6136 | |
| 6137 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6138 | if (Reg == 0) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6139 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6140 | return false; |
| 6141 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6142 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | return true; |
| 6147 | } |