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 | |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 134 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 135 | bool areValueTypesLegal() { |
| 136 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 137 | EVT RegisterVT = RegVTs[Value]; |
| 138 | if (!TLI->isTypeLegal(RegisterVT)) |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 145 | /// append - Add the specified values to this one. |
| 146 | void append(const RegsForValue &RHS) { |
| 147 | TLI = RHS.TLI; |
| 148 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 149 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 150 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 151 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 152 | |
| 153 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 154 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 155 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 156 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 157 | /// If the Flag pointer is NULL, no flag is used. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 158 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 159 | SDValue &Chain, SDValue *Flag) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 160 | |
| 161 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 162 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 163 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 164 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 165 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 166 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 167 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 168 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 169 | /// operand list. This adds the code marker, matching input operand index |
| 170 | /// (if applicable), and includes the number of values added into it. |
| 171 | void AddInlineAsmOperands(unsigned Code, |
| 172 | bool HasMatching, unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 173 | SelectionDAG &DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 174 | std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 175 | }; |
| 176 | } |
| 177 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 179 | /// combined into the value they represent. If the parts combine to a type |
| 180 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 181 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 182 | /// (ISD::AssertSext). |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 183 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 184 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 185 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 186 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 187 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 188 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 189 | SDValue Val = Parts[0]; |
| 190 | |
| 191 | if (NumParts > 1) { |
| 192 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 193 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 194 | unsigned PartBits = PartVT.getSizeInBits(); |
| 195 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 196 | |
| 197 | // Assemble the power of 2 part. |
| 198 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 199 | 1 << Log2_32(NumParts) : NumParts; |
| 200 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 201 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 202 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 203 | SDValue Lo, Hi; |
| 204 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 205 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 206 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 207 | if (RoundParts > 2) { |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 208 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 209 | PartVT, HalfVT); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 210 | Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 211 | RoundParts / 2, PartVT, HalfVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 212 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 213 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 214 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 215 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 216 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 217 | if (TLI.isBigEndian()) |
| 218 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 219 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 220 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 221 | |
| 222 | if (RoundParts < NumParts) { |
| 223 | // Assemble the trailing non-power-of-2 part. |
| 224 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 225 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 226 | Hi = getCopyFromParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 227 | Parts + RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 228 | |
| 229 | // Combine the round and odd parts. |
| 230 | Lo = Val; |
| 231 | if (TLI.isBigEndian()) |
| 232 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 233 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 234 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 235 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 236 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 237 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 238 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 239 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 240 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 241 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 242 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 243 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 244 | unsigned NumIntermediates; |
| 245 | unsigned NumRegs = |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 246 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 247 | NumIntermediates, RegisterVT); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 248 | assert(NumRegs == NumParts |
| 249 | && "Part count doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 250 | NumParts = NumRegs; // Silence a compiler warning. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 251 | assert(RegisterVT == PartVT |
| 252 | && "Part type doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 253 | assert(RegisterVT == Parts[0].getValueType() && |
| 254 | "Part type doesn't match part!"); |
| 255 | |
| 256 | // Assemble the parts into intermediate operands. |
| 257 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 258 | if (NumIntermediates == NumParts) { |
| 259 | // If the register was not expanded, truncate or copy the value, |
| 260 | // as appropriate. |
| 261 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 262 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 263 | PartVT, IntermediateVT); |
| 264 | } else if (NumParts > 0) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 265 | // If the intermediate type was expanded, build the intermediate |
| 266 | // operands from the parts. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 267 | assert(NumParts % NumIntermediates == 0 && |
| 268 | "Must expand into a divisible number of parts!"); |
| 269 | unsigned Factor = NumParts / NumIntermediates; |
| 270 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 271 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 272 | PartVT, IntermediateVT); |
| 273 | } |
| 274 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 275 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 276 | // intermediate operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 277 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 278 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 279 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 280 | } else if (PartVT.isFloatingPoint()) { |
| 281 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 282 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 283 | "Unexpected split"); |
| 284 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 285 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 286 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 287 | if (TLI.isBigEndian()) |
| 288 | std::swap(Lo, Hi); |
| 289 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 290 | } else { |
| 291 | // FP split into integer parts (soft fp) |
| 292 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 293 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 294 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 295 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 300 | PartVT = Val.getValueType(); |
| 301 | |
| 302 | if (PartVT == ValueVT) |
| 303 | return Val; |
| 304 | |
| 305 | if (PartVT.isVector()) { |
| 306 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 307 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (ValueVT.isVector()) { |
| 311 | assert(ValueVT.getVectorElementType() == PartVT && |
| 312 | ValueVT.getVectorNumElements() == 1 && |
| 313 | "Only trivial scalar-to-vector conversions should get here!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 314 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (PartVT.isInteger() && |
| 318 | ValueVT.isInteger()) { |
| 319 | if (ValueVT.bitsLT(PartVT)) { |
| 320 | // For a truncate, see if we have any information to |
| 321 | // indicate whether the truncated bits will always be |
| 322 | // zero or sign-extension. |
| 323 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 324 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 325 | DAG.getValueType(ValueVT)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 326 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 327 | } else { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 328 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 333 | if (ValueVT.bitsLT(Val.getValueType())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 334 | // FP_ROUND's are always exact here. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 335 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 336 | DAG.getIntPtrConstant(1)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 339 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 342 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 343 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 344 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 345 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 346 | return SDValue(); |
| 347 | } |
| 348 | |
| 349 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 350 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 351 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 352 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 353 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 354 | EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 355 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 356 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 357 | EVT PtrVT = TLI.getPointerTy(); |
| 358 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 360 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 361 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 362 | |
| 363 | if (!NumParts) |
| 364 | return; |
| 365 | |
| 366 | if (!ValueVT.isVector()) { |
| 367 | if (PartVT == ValueVT) { |
| 368 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 369 | Parts[0] = Val; |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 374 | // If the parts cover more bits than the value has, promote the value. |
| 375 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 376 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 377 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 378 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 379 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 380 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 381 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 382 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 383 | } |
| 384 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 385 | // Different types of the same size. |
| 386 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 387 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 388 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 389 | // If the parts cover less bits than value has, truncate the value. |
| 390 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 391 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 392 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 393 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 394 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | // The value may have changed - recompute ValueVT. |
| 399 | ValueVT = Val.getValueType(); |
| 400 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 401 | "Failed to tile the value with PartVT!"); |
| 402 | |
| 403 | if (NumParts == 1) { |
| 404 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 405 | Parts[0] = Val; |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // Expand the value into multiple parts. |
| 410 | if (NumParts & (NumParts - 1)) { |
| 411 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 412 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 413 | "Do not know what to expand to!"); |
| 414 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 415 | unsigned RoundBits = RoundParts * PartBits; |
| 416 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 417 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 418 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 419 | TLI.getPointerTy())); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 420 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 421 | OddParts, PartVT); |
| 422 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 423 | if (TLI.isBigEndian()) |
| 424 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 425 | std::reverse(Parts + RoundParts, Parts + NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 426 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 427 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 428 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 429 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 433 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 434 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 435 | EVT::getIntegerVT(*DAG.getContext(), |
| 436 | ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 437 | Val); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 438 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 439 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 440 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 441 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 442 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 443 | SDValue &Part0 = Parts[i]; |
| 444 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 445 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 446 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 447 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 448 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 449 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 450 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 451 | DAG.getConstant(0, PtrVT)); |
| 452 | |
| 453 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 454 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 455 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 456 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 457 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 463 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 464 | |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | // Vector ValueVT. |
| 469 | if (NumParts == 1) { |
| 470 | if (PartVT != ValueVT) { |
Bob Wilson | 5afffae | 2009-12-18 01:03:29 +0000 | [diff] [blame] | 471 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 472 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 473 | } else { |
| 474 | assert(ValueVT.getVectorElementType() == PartVT && |
| 475 | ValueVT.getVectorNumElements() == 1 && |
| 476 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 477 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 478 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 479 | DAG.getConstant(0, PtrVT)); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | Parts[0] = Val; |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 488 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 489 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 490 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 491 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 492 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 493 | |
| 494 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 495 | NumParts = NumRegs; // Silence a compiler warning. |
| 496 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 497 | |
| 498 | // Split the vector into intermediate operands. |
| 499 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 500 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 501 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 502 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 503 | IntermediateVT, Val, |
| 504 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 505 | PtrVT)); |
| 506 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 507 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 508 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 509 | DAG.getConstant(i, PtrVT)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 512 | // Split the intermediate operands into legal parts. |
| 513 | if (NumParts == NumIntermediates) { |
| 514 | // If the register was not expanded, promote or copy the value, |
| 515 | // as appropriate. |
| 516 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 517 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 518 | } else if (NumParts > 0) { |
| 519 | // If the intermediate type was expanded, split each the value into |
| 520 | // legal parts. |
| 521 | assert(NumParts % NumIntermediates == 0 && |
| 522 | "Must expand into a divisible number of parts!"); |
| 523 | unsigned Factor = NumParts / NumIntermediates; |
| 524 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 525 | getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 530 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 531 | AA = &aa; |
| 532 | GFI = gfi; |
| 533 | TD = DAG.getTarget().getTargetData(); |
| 534 | } |
| 535 | |
| 536 | /// clear - Clear out the curret SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 537 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | /// for a new block. This doesn't clear out information about |
| 539 | /// additional blocks that are needed to complete switch lowering |
| 540 | /// or PHI node updating; that information is cleared out as it is |
| 541 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 542 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 543 | NodeMap.clear(); |
| 544 | PendingLoads.clear(); |
| 545 | PendingExports.clear(); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 546 | EdgeMapping.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 547 | DAG.clear(); |
Bill Wendling | 8fcf170 | 2009-02-06 21:36:23 +0000 | [diff] [blame] | 548 | CurDebugLoc = DebugLoc::getUnknownLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 549 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 553 | /// flushing any PendingLoad items. This must be done before emitting |
| 554 | /// a store or any other node that may need to be ordered after any |
| 555 | /// prior load instructions. |
| 556 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 557 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 558 | if (PendingLoads.empty()) |
| 559 | return DAG.getRoot(); |
| 560 | |
| 561 | if (PendingLoads.size() == 1) { |
| 562 | SDValue Root = PendingLoads[0]; |
| 563 | DAG.setRoot(Root); |
| 564 | PendingLoads.clear(); |
| 565 | return Root; |
| 566 | } |
| 567 | |
| 568 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 569 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 570 | &PendingLoads[0], PendingLoads.size()); |
| 571 | PendingLoads.clear(); |
| 572 | DAG.setRoot(Root); |
| 573 | return Root; |
| 574 | } |
| 575 | |
| 576 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 577 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 578 | /// to do this before emitting a terminator instruction. |
| 579 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 580 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 581 | SDValue Root = DAG.getRoot(); |
| 582 | |
| 583 | if (PendingExports.empty()) |
| 584 | return Root; |
| 585 | |
| 586 | // Turn all of the CopyToReg chains into one factored node. |
| 587 | if (Root.getOpcode() != ISD::EntryToken) { |
| 588 | unsigned i = 0, e = PendingExports.size(); |
| 589 | for (; i != e; ++i) { |
| 590 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 591 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 592 | break; // Don't add the root if we already indirectly depend on it. |
| 593 | } |
| 594 | |
| 595 | if (i == e) |
| 596 | PendingExports.push_back(Root); |
| 597 | } |
| 598 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 599 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 600 | &PendingExports[0], |
| 601 | PendingExports.size()); |
| 602 | PendingExports.clear(); |
| 603 | DAG.setRoot(Root); |
| 604 | return Root; |
| 605 | } |
| 606 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 607 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 608 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 609 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 610 | |
| 611 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 612 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 613 | } |
| 614 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 615 | void SelectionDAGBuilder::visit(Instruction &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 616 | visit(I.getOpcode(), I); |
| 617 | } |
| 618 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 619 | void SelectionDAGBuilder::visit(unsigned Opcode, User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 620 | // Note: this doesn't use InstVisitor, because it has to work with |
| 621 | // ConstantExpr's in addition to instructions. |
| 622 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 623 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 624 | // Build the switch statement using the Instruction.def file. |
| 625 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 626 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 627 | #include "llvm/Instruction.def" |
| 628 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 629 | |
| 630 | // Assign the ordering to the freshly created DAG nodes. |
| 631 | if (NodeMap.count(&I)) { |
| 632 | ++SDNodeOrder; |
| 633 | AssignOrderingToNode(getValue(&I).getNode()); |
| 634 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 635 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 636 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 637 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 638 | SDValue &N = NodeMap[V]; |
| 639 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 640 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 641 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 642 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 643 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 644 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 645 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 646 | |
| 647 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 648 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 649 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 650 | if (isa<ConstantPointerNull>(C)) |
| 651 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 652 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 653 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 654 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 655 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 656 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 657 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 658 | |
| 659 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 660 | visit(CE->getOpcode(), *CE); |
| 661 | SDValue N1 = NodeMap[V]; |
| 662 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 663 | return N1; |
| 664 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 665 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 666 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 667 | SmallVector<SDValue, 4> Constants; |
| 668 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 669 | OI != OE; ++OI) { |
| 670 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 671 | // If the operand is an empty aggregate, there are no values. |
| 672 | if (!Val) continue; |
| 673 | // Add each leaf value from the operand to the Constants list |
| 674 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 675 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 676 | Constants.push_back(SDValue(Val, i)); |
| 677 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 678 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 679 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 680 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 683 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 684 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 685 | "Unknown struct or array constant!"); |
| 686 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 687 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 688 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 689 | unsigned NumElts = ValueVTs.size(); |
| 690 | if (NumElts == 0) |
| 691 | return SDValue(); // empty struct |
| 692 | SmallVector<SDValue, 4> Constants(NumElts); |
| 693 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 694 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 695 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 696 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 697 | else if (EltVT.isFloatingPoint()) |
| 698 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 699 | else |
| 700 | Constants[i] = DAG.getConstant(0, EltVT); |
| 701 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 702 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 703 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 704 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 707 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 708 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 709 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 710 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 711 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 712 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 713 | // Now that we know the number and type of the elements, get that number of |
| 714 | // elements into the Ops array based on what kind of constant it is. |
| 715 | SmallVector<SDValue, 16> Ops; |
| 716 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 717 | for (unsigned i = 0; i != NumElements; ++i) |
| 718 | Ops.push_back(getValue(CP->getOperand(i))); |
| 719 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 720 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 721 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 722 | |
| 723 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 724 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 725 | Op = DAG.getConstantFP(0, EltVT); |
| 726 | else |
| 727 | Op = DAG.getConstant(0, EltVT); |
| 728 | Ops.assign(NumElements, Op); |
| 729 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 730 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 731 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 732 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 733 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 734 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 735 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 736 | // If this is a static alloca, generate it as the frameindex instead of |
| 737 | // computation. |
| 738 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 739 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 740 | FuncInfo.StaticAllocaMap.find(AI); |
| 741 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 742 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 743 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 744 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 745 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 746 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 747 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 748 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 749 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 750 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 753 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 754 | /// type of the given function. This does not require a DAG or a return value, |
| 755 | /// 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] | 756 | static void getReturnInfo(const Type* ReturnType, |
| 757 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 758 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 759 | TargetLowering &TLI, |
| 760 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 761 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 762 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 763 | unsigned NumValues = ValueVTs.size(); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 764 | if (NumValues == 0) return; |
| 765 | unsigned Offset = 0; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 766 | |
| 767 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 768 | EVT VT = ValueVTs[j]; |
| 769 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 770 | |
| 771 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 772 | ExtendKind = ISD::SIGN_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 773 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 774 | ExtendKind = ISD::ZERO_EXTEND; |
| 775 | |
| 776 | // FIXME: C calling convention requires the return type to be promoted to |
| 777 | // at least 32-bit. But this is not necessary for non-C calling |
| 778 | // conventions. The frontend should mark functions whose return values |
| 779 | // require promoting with signext or zeroext attributes. |
| 780 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 781 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 782 | if (VT.bitsLT(MinVT)) |
| 783 | VT = MinVT; |
| 784 | } |
| 785 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 786 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 787 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 788 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 789 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 790 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 791 | // 'inreg' on function refers to return value |
| 792 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 793 | if (attr & Attribute::InReg) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 794 | Flags.setInReg(); |
| 795 | |
| 796 | // Propagate extension type if any |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 797 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 798 | Flags.setSExt(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 799 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 800 | Flags.setZExt(); |
| 801 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 802 | for (unsigned i = 0; i < NumParts; ++i) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 803 | OutVTs.push_back(PartVT); |
| 804 | OutFlags.push_back(Flags); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 805 | if (Offsets) |
| 806 | { |
| 807 | Offsets->push_back(Offset); |
| 808 | Offset += PartSize; |
| 809 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 813 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 814 | void SelectionDAGBuilder::visitRet(ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 815 | SDValue Chain = getControlRoot(); |
| 816 | SmallVector<ISD::OutputArg, 8> Outs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 817 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 818 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 819 | if (!FLI.CanLowerReturn) { |
| 820 | unsigned DemoteReg = FLI.DemoteRegister; |
| 821 | const Function *F = I.getParent()->getParent(); |
| 822 | |
| 823 | // Emit a store of the return value through the virtual register. |
| 824 | // Leave Outs empty so that LowerReturn won't try to load return |
| 825 | // registers the usual way. |
| 826 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 827 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 828 | PtrValueVTs); |
| 829 | |
| 830 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 831 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 832 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 833 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 834 | SmallVector<uint64_t, 4> Offsets; |
| 835 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 836 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 837 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 838 | SmallVector<SDValue, 4> Chains(NumValues); |
| 839 | EVT PtrVT = PtrValueVTs[0]; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 840 | for (unsigned i = 0; i != NumValues; ++i) { |
| 841 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 842 | DAG.getConstant(Offsets[i], PtrVT)); |
| 843 | Chains[i] = |
| 844 | DAG.getStore(Chain, getCurDebugLoc(), |
| 845 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 846 | Add, NULL, Offsets[i], false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 849 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 850 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 851 | } else if (I.getNumOperands() != 0) { |
| 852 | SmallVector<EVT, 4> ValueVTs; |
| 853 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 854 | unsigned NumValues = ValueVTs.size(); |
| 855 | if (NumValues) { |
| 856 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 857 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 858 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 860 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 861 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 862 | const Function *F = I.getParent()->getParent(); |
| 863 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 864 | ExtendKind = ISD::SIGN_EXTEND; |
| 865 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 866 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 867 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 868 | // FIXME: C calling convention requires the return type to be promoted |
| 869 | // 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] | 870 | // conventions. The frontend should mark functions whose return values |
| 871 | // require promoting with signext or zeroext attributes. |
| 872 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 873 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 874 | if (VT.bitsLT(MinVT)) |
| 875 | VT = MinVT; |
| 876 | } |
| 877 | |
| 878 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 879 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 880 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 881 | getCopyToParts(DAG, getCurDebugLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 882 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 883 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 884 | |
| 885 | // 'inreg' on function refers to return value |
| 886 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 887 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 888 | Flags.setInReg(); |
| 889 | |
| 890 | // Propagate extension type if any |
| 891 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 892 | Flags.setSExt(); |
| 893 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 894 | Flags.setZExt(); |
| 895 | |
| 896 | for (unsigned i = 0; i < NumParts; ++i) |
| 897 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 898 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 901 | |
| 902 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 903 | CallingConv::ID CallConv = |
| 904 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 905 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 906 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 907 | |
| 908 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 909 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 910 | "LowerReturn didn't return a valid chain!"); |
| 911 | |
| 912 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 913 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 916 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 917 | /// created for it, emit nodes to copy the value into the virtual |
| 918 | /// registers. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 919 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) { |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 920 | if (!V->use_empty()) { |
| 921 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 922 | if (VMI != FuncInfo.ValueMap.end()) |
| 923 | CopyValueToVirtualRegister(V, VMI->second); |
| 924 | } |
| 925 | } |
| 926 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 927 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 928 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 929 | /// CopyTo/FromReg. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 930 | void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 931 | // No need to export constants. |
| 932 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 933 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 934 | // Already exported? |
| 935 | if (FuncInfo.isExportedInst(V)) return; |
| 936 | |
| 937 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 938 | CopyValueToVirtualRegister(V, Reg); |
| 939 | } |
| 940 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 941 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V, |
| 942 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 943 | // The operands of the setcc have to be in this block. We don't know |
| 944 | // how to export them from some other block. |
| 945 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 946 | // Can export from current BB. |
| 947 | if (VI->getParent() == FromBB) |
| 948 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 950 | // Is already exported, noop. |
| 951 | return FuncInfo.isExportedInst(V); |
| 952 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 953 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 954 | // If this is an argument, we can export it if the BB is the entry block or |
| 955 | // if it is already exported. |
| 956 | if (isa<Argument>(V)) { |
| 957 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 958 | return true; |
| 959 | |
| 960 | // Otherwise, can only export this if it is already exported. |
| 961 | return FuncInfo.isExportedInst(V); |
| 962 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 963 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 964 | // Otherwise, constants can always be exported. |
| 965 | return true; |
| 966 | } |
| 967 | |
| 968 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 969 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 970 | return I->getParent() == BB; |
| 971 | return true; |
| 972 | } |
| 973 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 974 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 975 | /// the given LLVM IR floating-point condition code. This includes |
| 976 | /// consideration of global floating-point math flags. |
| 977 | /// |
| 978 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 979 | ISD::CondCode FPC, FOC; |
| 980 | switch (Pred) { |
| 981 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 982 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 983 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 984 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 985 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 986 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 987 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 988 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 989 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 990 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 991 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 992 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 993 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 994 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 995 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 996 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 997 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 998 | llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 999 | FOC = FPC = ISD::SETFALSE; |
| 1000 | break; |
| 1001 | } |
| 1002 | if (FiniteOnlyFPMath()) |
| 1003 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1004 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1005 | return FPC; |
| 1006 | } |
| 1007 | |
| 1008 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1009 | /// the given LLVM IR integer condition code. |
| 1010 | /// |
| 1011 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1012 | switch (Pred) { |
| 1013 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1014 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1015 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1016 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1017 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1018 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1019 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1020 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1021 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1022 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1023 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1024 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1025 | return ISD::SETNE; |
| 1026 | } |
| 1027 | } |
| 1028 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1029 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1030 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1031 | /// AND operator tree. |
| 1032 | /// |
| 1033 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1034 | SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond, |
| 1035 | MachineBasicBlock *TBB, |
| 1036 | MachineBasicBlock *FBB, |
| 1037 | MachineBasicBlock *CurBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1038 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1039 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1040 | // If the leaf of the tree is a comparison, merge the condition into |
| 1041 | // the caseblock. |
| 1042 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1043 | // The operands of the cmp have to be in this block. We don't know |
| 1044 | // how to export them from some other block. If this is the first block |
| 1045 | // of the sequence, no exporting is needed. |
| 1046 | if (CurBB == CurMBB || |
| 1047 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1048 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1049 | ISD::CondCode Condition; |
| 1050 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1051 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1052 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1053 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1056 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1057 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1058 | |
| 1059 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1060 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1061 | SwitchCases.push_back(CB); |
| 1062 | return; |
| 1063 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1067 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1068 | NULL, TBB, FBB, CurBB); |
| 1069 | SwitchCases.push_back(CB); |
| 1070 | } |
| 1071 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1072 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1073 | void SelectionDAGBuilder::FindMergedConditions(Value *Cond, |
| 1074 | MachineBasicBlock *TBB, |
| 1075 | MachineBasicBlock *FBB, |
| 1076 | MachineBasicBlock *CurBB, |
| 1077 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1078 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1079 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1080 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1081 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1082 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1083 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1084 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1085 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1088 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1089 | // Create TmpBB after CurBB. |
| 1090 | MachineFunction::iterator BBI = CurBB; |
| 1091 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1092 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1093 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1094 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1095 | if (Opc == Instruction::Or) { |
| 1096 | // Codegen X | Y as: |
| 1097 | // jmp_if_X TBB |
| 1098 | // jmp TmpBB |
| 1099 | // TmpBB: |
| 1100 | // jmp_if_Y TBB |
| 1101 | // jmp FBB |
| 1102 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1103 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1104 | // Emit the LHS condition. |
| 1105 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1106 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1107 | // Emit the RHS condition into TmpBB. |
| 1108 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1109 | } else { |
| 1110 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1111 | // Codegen X & Y as: |
| 1112 | // jmp_if_X TmpBB |
| 1113 | // jmp FBB |
| 1114 | // TmpBB: |
| 1115 | // jmp_if_Y TBB |
| 1116 | // jmp FBB |
| 1117 | // |
| 1118 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1119 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1120 | // Emit the LHS condition. |
| 1121 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1122 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1123 | // Emit the RHS condition into TmpBB. |
| 1124 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1129 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1130 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1131 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1132 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1133 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1134 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1135 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1136 | // will get folded into a single comparison, so don't emit two blocks. |
| 1137 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1138 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1139 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1140 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1141 | return false; |
| 1142 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1144 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1145 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1146 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1147 | Cases[0].CC == Cases[1].CC && |
| 1148 | isa<Constant>(Cases[0].CmpRHS) && |
| 1149 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1150 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1151 | return false; |
| 1152 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1156 | return true; |
| 1157 | } |
| 1158 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1159 | void SelectionDAGBuilder::visitBr(BranchInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1160 | // Update machine-CFG edges. |
| 1161 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1162 | |
| 1163 | // Figure out which block is immediately after the current one. |
| 1164 | MachineBasicBlock *NextBlock = 0; |
| 1165 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1166 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1167 | NextBlock = BBI; |
| 1168 | |
| 1169 | if (I.isUnconditional()) { |
| 1170 | // Update machine-CFG edges. |
| 1171 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1172 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1173 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1174 | if (Succ0MBB != NextBlock) |
| 1175 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1176 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1177 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1178 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | // If this condition is one of the special cases we handle, do special stuff |
| 1183 | // now. |
| 1184 | Value *CondVal = I.getCondition(); |
| 1185 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1186 | |
| 1187 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1188 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1189 | // For example, instead of something like: |
| 1190 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1191 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1192 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1193 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1194 | // or C, F |
| 1195 | // jnz foo |
| 1196 | // Emit: |
| 1197 | // cmp A, B |
| 1198 | // je foo |
| 1199 | // cmp D, E |
| 1200 | // jle foo |
| 1201 | // |
| 1202 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1203 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1204 | (BOp->getOpcode() == Instruction::And || |
| 1205 | BOp->getOpcode() == Instruction::Or)) { |
| 1206 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1207 | // If the compares in later blocks need to use values not currently |
| 1208 | // exported from this block, export them now. This block should always |
| 1209 | // be the first entry. |
| 1210 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1211 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1212 | // Allow some cases to be rejected. |
| 1213 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1214 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1215 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1216 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1217 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1218 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1219 | // Emit the branch for this block. |
| 1220 | visitSwitchCase(SwitchCases[0]); |
| 1221 | SwitchCases.erase(SwitchCases.begin()); |
| 1222 | return; |
| 1223 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1224 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1225 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1226 | // SwitchCases. |
| 1227 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1228 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1230 | SwitchCases.clear(); |
| 1231 | } |
| 1232 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1233 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1234 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1235 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1236 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1237 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1238 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1239 | // cond branch. |
| 1240 | visitSwitchCase(CB); |
| 1241 | } |
| 1242 | |
| 1243 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1244 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1245 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1246 | SDValue Cond; |
| 1247 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1248 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1251 | if (CB.CmpMHS == NULL) { |
| 1252 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1253 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1254 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1255 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1256 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1257 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1258 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1259 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1260 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1261 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1262 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1263 | } else { |
| 1264 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1265 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1266 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1267 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1268 | |
| 1269 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1270 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1273 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1274 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1275 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1276 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1277 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1278 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1279 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1280 | } |
| 1281 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1282 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1283 | // Update successor info |
| 1284 | CurMBB->addSuccessor(CB.TrueBB); |
| 1285 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1286 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1287 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1288 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1289 | MachineBasicBlock *NextBlock = 0; |
| 1290 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1291 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1292 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1293 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1294 | // If the lhs block is the next block, invert the condition so that we can |
| 1295 | // fall through to the lhs instead of the rhs block. |
| 1296 | if (CB.TrueBB == NextBlock) { |
| 1297 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1298 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1299 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1300 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1301 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1302 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1303 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1304 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1305 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1306 | // If the branch was constant folded, fix up the CFG. |
| 1307 | if (BrCond.getOpcode() == ISD::BR) { |
| 1308 | CurMBB->removeSuccessor(CB.FalseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1311 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1312 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1313 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1314 | if (CB.FalseBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1315 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1316 | DAG.getBasicBlock(CB.FalseBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1317 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1318 | |
| 1319 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1323 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1324 | // Emit the code for the jump table |
| 1325 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1326 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1327 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1328 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1329 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1330 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1331 | MVT::Other, Index.getValue(1), |
| 1332 | Table, Index); |
| 1333 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1337 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1338 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
| 1339 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1340 | // Subtract the lowest switch case value from the value being switched on and |
| 1341 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1342 | // difference between smallest and largest cases. |
| 1343 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1344 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1345 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1346 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1347 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1348 | // The SDNode we just created, which holds the value being switched on minus |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1349 | // the smallest case value, needs to be copied to a virtual register so it |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1350 | // can be used as an index into the jump table in a subsequent basic block. |
| 1351 | // This value may be smaller or larger than the target's pointer type, and |
| 1352 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1353 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1354 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1355 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1356 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1357 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1358 | JT.Reg = JumpTableReg; |
| 1359 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1360 | // Emit the range check for the jump table, and branch to the default block |
| 1361 | // for the switch statement if the value being switched on exceeds the largest |
| 1362 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1363 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1364 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1365 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1366 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1369 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1370 | MachineBasicBlock *NextBlock = 0; |
| 1371 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1372 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1373 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1374 | NextBlock = BBI; |
| 1375 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1376 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1377 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1378 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1379 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1380 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1381 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1382 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1383 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1384 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1388 | /// suitable for "bit tests" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1389 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1390 | // Subtract the minimum value |
| 1391 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1392 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1393 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1394 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1397 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1398 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1399 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1400 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1401 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1402 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1403 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1404 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1405 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1406 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1407 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1408 | |
| 1409 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1410 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1411 | MachineBasicBlock *NextBlock = 0; |
| 1412 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1413 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1414 | NextBlock = BBI; |
| 1415 | |
| 1416 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1417 | |
| 1418 | CurMBB->addSuccessor(B.Default); |
| 1419 | CurMBB->addSuccessor(MBB); |
| 1420 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1421 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1422 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1423 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1424 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1425 | if (MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1426 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1427 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1428 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1429 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /// visitBitTestCase - this function produces one "bit test" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1433 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1434 | unsigned Reg, |
| 1435 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1436 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1437 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1438 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1439 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1440 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1441 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1442 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1443 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1444 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1445 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1446 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1447 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1448 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1449 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1450 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1451 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1452 | |
| 1453 | CurMBB->addSuccessor(B.TargetBB); |
| 1454 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1455 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1456 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1457 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1458 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1461 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1462 | MachineBasicBlock *NextBlock = 0; |
| 1463 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1464 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1465 | NextBlock = BBI; |
| 1466 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1467 | if (NextMBB != NextBlock) |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1468 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1469 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1470 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1471 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1474 | void SelectionDAGBuilder::visitInvoke(InvokeInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1475 | // Retrieve successors. |
| 1476 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1477 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1478 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1479 | const Value *Callee(I.getCalledValue()); |
| 1480 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1481 | visitInlineAsm(&I); |
| 1482 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1483 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1484 | |
| 1485 | // If the value of the invoke is used outside of its defining block, make it |
| 1486 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1487 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Update successor info |
| 1490 | CurMBB->addSuccessor(Return); |
| 1491 | CurMBB->addSuccessor(LandingPad); |
| 1492 | |
| 1493 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1494 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1495 | MVT::Other, getControlRoot(), |
| 1496 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1499 | void SelectionDAGBuilder::visitUnwind(UnwindInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1503 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1504 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1505 | CaseRecVector& WorkList, |
| 1506 | Value* SV, |
| 1507 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1508 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1509 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1510 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1511 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1512 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1513 | return false; |
| 1514 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1515 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1516 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1517 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Figure out which block is immediately after the current one. |
| 1520 | MachineBasicBlock *NextBlock = 0; |
| 1521 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1522 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1523 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1524 | NextBlock = BBI; |
| 1525 | |
| 1526 | // TODO: If any two of the cases has the same destination, and if one value |
| 1527 | // is the same as the other, but has one bit unset that the other has set, |
| 1528 | // use bit manipulation to do two compares at once. For example: |
| 1529 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1530 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1531 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1532 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1533 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1534 | // branches in this order. See if rearranging a case value would help. |
| 1535 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1536 | if (I->BB == NextBlock) { |
| 1537 | std::swap(*I, BackCase); |
| 1538 | break; |
| 1539 | } |
| 1540 | } |
| 1541 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1542 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1543 | // Create a CaseBlock record representing a conditional branch to |
| 1544 | // the Case's target mbb if the value being switched on SV is equal |
| 1545 | // to C. |
| 1546 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1547 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1548 | MachineBasicBlock *FallThrough; |
| 1549 | if (I != E-1) { |
| 1550 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1551 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1552 | |
| 1553 | // Put SV in a virtual register to make it available from the new blocks. |
| 1554 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1555 | } else { |
| 1556 | // If the last case doesn't match, go to the default block. |
| 1557 | FallThrough = Default; |
| 1558 | } |
| 1559 | |
| 1560 | Value *RHS, *LHS, *MHS; |
| 1561 | ISD::CondCode CC; |
| 1562 | if (I->High == I->Low) { |
| 1563 | // This is just small small case range :) containing exactly 1 case |
| 1564 | CC = ISD::SETEQ; |
| 1565 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1566 | } else { |
| 1567 | CC = ISD::SETLE; |
| 1568 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1569 | } |
| 1570 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1571 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1572 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1573 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1574 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1575 | // before the next MBB. |
| 1576 | if (CurBlock == CurMBB) |
| 1577 | visitSwitchCase(CB); |
| 1578 | else |
| 1579 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1580 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1581 | CurBlock = FallThrough; |
| 1582 | } |
| 1583 | |
| 1584 | return true; |
| 1585 | } |
| 1586 | |
| 1587 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1588 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1589 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1590 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1591 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1592 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1593 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1594 | APInt LastExt(Last), FirstExt(First); |
| 1595 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1596 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1597 | return (LastExt - FirstExt + 1ULL); |
| 1598 | } |
| 1599 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1600 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1601 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1602 | CaseRecVector& WorkList, |
| 1603 | Value* SV, |
| 1604 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1605 | Case& FrontCase = *CR.Range.first; |
| 1606 | Case& BackCase = *(CR.Range.second-1); |
| 1607 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1608 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1609 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1610 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1611 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1612 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1613 | I!=E; ++I) |
| 1614 | TSize += I->size(); |
| 1615 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1616 | if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4))) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1617 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1618 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1619 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1620 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1621 | if (Density < 0.4) |
| 1622 | return false; |
| 1623 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1624 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1625 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1626 | << "Range: " << Range |
| 1627 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1630 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1631 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1634 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1635 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1636 | |
| 1637 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1638 | |
| 1639 | // Create a new basic block to hold the code for loading the address |
| 1640 | // of the jump table, and jumping to it. Update successor information; |
| 1641 | // we will either branch to the default case for the switch, or the jump |
| 1642 | // table. |
| 1643 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1644 | CurMF->insert(BBI, JumpTableBB); |
| 1645 | CR.CaseBB->addSuccessor(Default); |
| 1646 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1647 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1648 | // Build a vector of destination BBs, corresponding to each target |
| 1649 | // of the jump table. If the value of the jump table slot corresponds to |
| 1650 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1651 | // the default BB. |
| 1652 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1653 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1654 | 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] | 1655 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1656 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1659 | DestBBs.push_back(I->BB); |
| 1660 | if (TEI==High) |
| 1661 | ++I; |
| 1662 | } else { |
| 1663 | DestBBs.push_back(Default); |
| 1664 | } |
| 1665 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1666 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1667 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1668 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1669 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1670 | E = DestBBs.end(); I != E; ++I) { |
| 1671 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1672 | SuccsHandled[(*I)->getNumber()] = true; |
| 1673 | JumpTableBB->addSuccessor(*I); |
| 1674 | } |
| 1675 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1676 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1677 | // Create a jump table index for this jump table, or return an existing |
| 1678 | // one. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1679 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1680 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
| 1681 | ->getJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1682 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1683 | // Set the jump table information so that we can codegen it as a second |
| 1684 | // MachineBasicBlock |
| 1685 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1686 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1687 | if (CR.CaseBB == CurMBB) |
| 1688 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1689 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1690 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1691 | |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1696 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1697 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1698 | CaseRecVector& WorkList, |
| 1699 | Value* SV, |
| 1700 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1701 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1702 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1703 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1706 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1707 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1708 | |
| 1709 | Case& FrontCase = *CR.Range.first; |
| 1710 | Case& BackCase = *(CR.Range.second-1); |
| 1711 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1712 | |
| 1713 | // Size is the number of Cases represented by this range. |
| 1714 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1715 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1716 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1717 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1718 | double FMetric = 0; |
| 1719 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1720 | |
| 1721 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1722 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1723 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1724 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1725 | I!=E; ++I) |
| 1726 | TSize += I->size(); |
| 1727 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1728 | APInt LSize = FrontCase.size(); |
| 1729 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1730 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1731 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1732 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1733 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1734 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1735 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1736 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1737 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1738 | assert((Range - 2ULL).isNonNegative() && |
| 1739 | "Invalid case distance"); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1740 | double LDensity = (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1741 | (LEnd - First + 1ULL).roundToDouble(); |
| 1742 | double RDensity = (double)RSize.roundToDouble() / |
| 1743 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1744 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1745 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1746 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1747 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1748 | << "LDensity: " << LDensity |
| 1749 | << ", RDensity: " << RDensity << '\n' |
| 1750 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1751 | if (FMetric < Metric) { |
| 1752 | Pivot = J; |
| 1753 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1754 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | LSize += J->size(); |
| 1758 | RSize -= J->size(); |
| 1759 | } |
| 1760 | if (areJTsAllowed(TLI)) { |
| 1761 | // If our case is dense we *really* should handle it earlier! |
| 1762 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1763 | } else { |
| 1764 | Pivot = CR.Range.first + Size/2; |
| 1765 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1766 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1767 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1768 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1769 | Constant *C = Pivot->Low; |
| 1770 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1771 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1772 | // 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] | 1773 | // 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] | 1774 | // 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] | 1775 | // 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] | 1776 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1777 | // rather than creating a leaf node for it. |
| 1778 | if ((LHSR.second - LHSR.first) == 1 && |
| 1779 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1780 | cast<ConstantInt>(C)->getValue() == |
| 1781 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1782 | TrueBB = LHSR.first->BB; |
| 1783 | } else { |
| 1784 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1785 | CurMF->insert(BBI, TrueBB); |
| 1786 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Put SV in a virtual register to make it available from the new blocks. |
| 1789 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1790 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1791 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1792 | // Similar to the optimization above, if the Value being switched on is |
| 1793 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1794 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1795 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1796 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1797 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1798 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1799 | FalseBB = RHSR.first->BB; |
| 1800 | } else { |
| 1801 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1802 | CurMF->insert(BBI, FalseBB); |
| 1803 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Put SV in a virtual register to make it available from the new blocks. |
| 1806 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1810 | // 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] | 1811 | // Otherwise, branch to LHS. |
| 1812 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1813 | |
| 1814 | if (CR.CaseBB == CurMBB) |
| 1815 | visitSwitchCase(CB); |
| 1816 | else |
| 1817 | SwitchCases.push_back(CB); |
| 1818 | |
| 1819 | return true; |
| 1820 | } |
| 1821 | |
| 1822 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1823 | /// range span less, than machine word bitwidth, encode case range into series |
| 1824 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1825 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1826 | CaseRecVector& WorkList, |
| 1827 | Value* SV, |
| 1828 | MachineBasicBlock* Default){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1829 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1830 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1831 | |
| 1832 | Case& FrontCase = *CR.Range.first; |
| 1833 | Case& BackCase = *(CR.Range.second-1); |
| 1834 | |
| 1835 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1836 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1837 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1838 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1839 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1840 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1841 | return false; |
| 1842 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1843 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1844 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1845 | I!=E; ++I) { |
| 1846 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1847 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1848 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1849 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1850 | // Count unique destinations |
| 1851 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1852 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1853 | Dests.insert(I->BB); |
| 1854 | if (Dests.size() > 3) |
| 1855 | // Don't bother the code below, if there are too much unique destinations |
| 1856 | return false; |
| 1857 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1858 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1859 | << Dests.size() << '\n' |
| 1860 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1861 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1862 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1863 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1864 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1865 | APInt cmpRange = maxValue - minValue; |
| 1866 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1867 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1868 | << "Low bound: " << minValue << '\n' |
| 1869 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1873 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1874 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1875 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1876 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1877 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1878 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1879 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1880 | // Optimize the case where all the case values fit in a |
| 1881 | // word without having to subtract minValue. In this case, |
| 1882 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1883 | if (minValue.isNonNegative() && |
| 1884 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 1885 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1886 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1887 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1888 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1889 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1890 | CaseBitsVector CasesBits; |
| 1891 | unsigned i, count = 0; |
| 1892 | |
| 1893 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1894 | MachineBasicBlock* Dest = I->BB; |
| 1895 | for (i = 0; i < count; ++i) |
| 1896 | if (Dest == CasesBits[i].BB) |
| 1897 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1898 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1899 | if (i == count) { |
| 1900 | assert((count < 3) && "Too much destinations to test!"); |
| 1901 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1902 | count++; |
| 1903 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1904 | |
| 1905 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1906 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1907 | |
| 1908 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1909 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1910 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1911 | for (uint64_t j = lo; j <= hi; j++) { |
| 1912 | CasesBits[i].Mask |= 1ULL << j; |
| 1913 | CasesBits[i].Bits++; |
| 1914 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1915 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1916 | } |
| 1917 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1918 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1919 | BitTestInfo BTC; |
| 1920 | |
| 1921 | // Figure out which block is immediately after the current one. |
| 1922 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1923 | ++BBI; |
| 1924 | |
| 1925 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1926 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1927 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1928 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1929 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1930 | << ", Bits: " << CasesBits[i].Bits |
| 1931 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1932 | |
| 1933 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1934 | CurMF->insert(BBI, CaseBB); |
| 1935 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1936 | CaseBB, |
| 1937 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Put SV in a virtual register to make it available from the new blocks. |
| 1940 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1941 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1942 | |
| 1943 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1944 | -1U, (CR.CaseBB == CurMBB), |
| 1945 | CR.CaseBB, Default, BTC); |
| 1946 | |
| 1947 | if (CR.CaseBB == CurMBB) |
| 1948 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1950 | BitTestCases.push_back(BTB); |
| 1951 | |
| 1952 | return true; |
| 1953 | } |
| 1954 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1955 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1956 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 1957 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1958 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1961 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1962 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1963 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1964 | SI.getSuccessorValue(i), |
| 1965 | SMBB)); |
| 1966 | } |
| 1967 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1968 | |
| 1969 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1970 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1971 | // Must recompute end() each iteration because it may be |
| 1972 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1973 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 1974 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 1975 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1976 | MachineBasicBlock* nextBB = J->BB; |
| 1977 | MachineBasicBlock* currentBB = I->BB; |
| 1978 | |
| 1979 | // If the two neighboring cases go to the same destination, merge them |
| 1980 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1981 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1982 | I->High = J->High; |
| 1983 | J = Cases.erase(J); |
| 1984 | } else { |
| 1985 | I = J++; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1990 | if (I->Low != I->High) |
| 1991 | // A range counts double, since it requires two compares. |
| 1992 | ++numCmps; |
| 1993 | } |
| 1994 | |
| 1995 | return numCmps; |
| 1996 | } |
| 1997 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1998 | void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1999 | // Figure out which block is immediately after the current one. |
| 2000 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2001 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2002 | |
| 2003 | // If there is only the default destination, branch to it if it is not the |
| 2004 | // next basic block. Otherwise, just fall through. |
| 2005 | if (SI.getNumOperands() == 2) { |
| 2006 | // Update machine-CFG edges. |
| 2007 | |
| 2008 | // If this is not a fall-through branch, emit the branch. |
| 2009 | CurMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2010 | if (Default != NextBlock) |
| 2011 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 2012 | MVT::Other, getControlRoot(), |
| 2013 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2014 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2017 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | // If there are any non-default case statements, create a vector of Cases |
| 2019 | // representing each one, and sort the vector so that we can efficiently |
| 2020 | // create a binary search tree from them. |
| 2021 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2022 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2023 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2024 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2025 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2026 | |
| 2027 | // Get the Value to be switched on and default basic blocks, which will be |
| 2028 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2029 | // search tree. |
| 2030 | Value *SV = SI.getOperand(0); |
| 2031 | |
| 2032 | // Push the initial CaseRec onto the worklist |
| 2033 | CaseRecVector WorkList; |
| 2034 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2035 | |
| 2036 | while (!WorkList.empty()) { |
| 2037 | // Grab a record representing a case range to process off the worklist |
| 2038 | CaseRec CR = WorkList.back(); |
| 2039 | WorkList.pop_back(); |
| 2040 | |
| 2041 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2042 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2043 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2044 | // If the range has few cases (two or less) emit a series of specific |
| 2045 | // tests. |
| 2046 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2047 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2048 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2049 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2050 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2051 | // lowering the switch to a binary tree of conditional branches. |
| 2052 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2053 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2054 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2055 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2056 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2057 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2058 | } |
| 2059 | } |
| 2060 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2061 | void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) { |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2062 | // Update machine-CFG edges with unique successors. |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2063 | SmallVector<BasicBlock*, 32> succs; |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2064 | succs.reserve(I.getNumSuccessors()); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2065 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2066 | succs.push_back(I.getSuccessor(i)); |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2067 | array_pod_sort(succs.begin(), succs.end()); |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2068 | succs.erase(std::unique(succs.begin(), succs.end()), succs.end()); |
| 2069 | for (unsigned i = 0, e = succs.size(); i != e; ++i) |
| 2070 | CurMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2071 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2072 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2073 | MVT::Other, getControlRoot(), |
| 2074 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2075 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2076 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2077 | void SelectionDAGBuilder::visitFSub(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2078 | // -0.0 - X --> fneg |
| 2079 | const Type *Ty = I.getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2080 | if (Ty->isVectorTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2081 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2082 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2083 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2084 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2085 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2086 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2087 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2088 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2089 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2090 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2093 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2095 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2096 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2097 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2098 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2099 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2100 | Op2.getValueType(), Op2)); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2103 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2104 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2107 | void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2109 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2110 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2111 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2114 | void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2115 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2116 | SDValue Op2 = getValue(I.getOperand(1)); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2117 | if (!I.getType()->isVectorTy() && |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2118 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2119 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2120 | EVT PTy = TLI.getPointerTy(); |
| 2121 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2122 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2123 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2124 | TLI.getShiftAmountTy(), Op2); |
| 2125 | // If the operand is larger than the shift count type but the shift |
| 2126 | // count type has enough bits to represent any shift value, truncate |
| 2127 | // it now. This is a common case and it exposes the truncate to |
| 2128 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2129 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2130 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2131 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2132 | TLI.getShiftAmountTy(), Op2); |
| 2133 | // Otherwise we'll need to temporarily settle for some other |
| 2134 | // convenient type; type legalization will make adjustments as |
| 2135 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2136 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2137 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2138 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2139 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2140 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2141 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2142 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2143 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2144 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2145 | Op1.getValueType(), Op1, Op2)); |
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::visitICmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2149 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2150 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2151 | predicate = IC->getPredicate(); |
| 2152 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2153 | predicate = ICmpInst::Predicate(IC->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 Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2157 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2158 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2159 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2162 | void SelectionDAGBuilder::visitFCmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2163 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2164 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2165 | predicate = FC->getPredicate(); |
| 2166 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2167 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2168 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2169 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2170 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2171 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2172 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2175 | void SelectionDAGBuilder::visitSelect(User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2176 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2177 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2178 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2179 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2180 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2181 | SmallVector<SDValue, 4> Values(NumValues); |
| 2182 | SDValue Cond = getValue(I.getOperand(0)); |
| 2183 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2184 | SDValue FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2185 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2186 | for (unsigned i = 0; i != NumValues; ++i) |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2187 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
| 2188 | TrueVal.getNode()->getValueType(i), Cond, |
| 2189 | SDValue(TrueVal.getNode(), |
| 2190 | TrueVal.getResNo() + i), |
| 2191 | SDValue(FalseVal.getNode(), |
| 2192 | FalseVal.getResNo() + i)); |
| 2193 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2194 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2195 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2196 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2197 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2198 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2199 | void SelectionDAGBuilder::visitTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2200 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2201 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2202 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2203 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2206 | void SelectionDAGBuilder::visitZExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2207 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2208 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2209 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2210 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2211 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2214 | void SelectionDAGBuilder::visitSExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2215 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2216 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2217 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2218 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2219 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2222 | void SelectionDAGBuilder::visitFPTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2223 | // FPTrunc is never a no-op cast, no need to check |
| 2224 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2225 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2226 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
| 2227 | DestVT, N, DAG.getIntPtrConstant(0))); |
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::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2231 | // FPTrunc 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_EXTEND, 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::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2238 | // FPToUI 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::FP_TO_UINT, 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::visitFPToSI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2245 | // FPToSI is never a no-op cast, no need to check |
| 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::FP_TO_SINT, 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::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2252 | // UIToFP is never a no-op cast, no need to check |
| 2253 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2254 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2255 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2258 | void SelectionDAGBuilder::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2259 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2260 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2261 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2262 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2265 | void SelectionDAGBuilder::visitPtrToInt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2266 | // What to do depends on the size of the integer and the size of the pointer. |
| 2267 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2268 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2269 | EVT SrcVT = N.getValueType(); |
| 2270 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2271 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2274 | void SelectionDAGBuilder::visitIntToPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2275 | // What to do depends on the size of the integer and the size of the pointer. |
| 2276 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2277 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2278 | EVT SrcVT = N.getValueType(); |
| 2279 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2280 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2283 | void SelectionDAGBuilder::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2284 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2285 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2286 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2287 | // BitCast assures us that source and destination are the same size so this is |
| 2288 | // either a BIT_CONVERT or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2289 | if (DestVT != N.getValueType()) |
| 2290 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2291 | DestVT, N)); // convert types. |
| 2292 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2293 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2296 | void SelectionDAGBuilder::visitInsertElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2297 | SDValue InVec = getValue(I.getOperand(0)); |
| 2298 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2299 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2300 | TLI.getPointerTy(), |
| 2301 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2302 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2303 | TLI.getValueType(I.getType()), |
| 2304 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2307 | void SelectionDAGBuilder::visitExtractElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2308 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2309 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2310 | TLI.getPointerTy(), |
| 2311 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2312 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2313 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2316 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2317 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2318 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2319 | unsigned MaskNumElts = Mask.size(); |
| 2320 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2321 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2322 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2323 | return true; |
| 2324 | } |
| 2325 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2326 | void SelectionDAGBuilder::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2327 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2328 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2329 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2330 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2331 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2332 | // representing undef values. |
| 2333 | SmallVector<Constant*, 8> MaskElts; |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 2334 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2335 | unsigned MaskNumElts = MaskElts.size(); |
| 2336 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2337 | if (isa<UndefValue>(MaskElts[i])) |
| 2338 | Mask.push_back(-1); |
| 2339 | else |
| 2340 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2341 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2342 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2343 | EVT VT = TLI.getValueType(I.getType()); |
| 2344 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2345 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2346 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2347 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2348 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2349 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
| 2352 | |
| 2353 | // 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] | 2354 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2355 | // Mask is longer than the source vectors and is a multiple of the source |
| 2356 | // 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] | 2357 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2358 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2359 | // The shuffle is concatenating two vectors together. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2360 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2361 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
| 2364 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2365 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2366 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2367 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2368 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2369 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2370 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2371 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2372 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2373 | MOps1[0] = Src1; |
| 2374 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2375 | |
| 2376 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2377 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2378 | &MOps1[0], NumConcat); |
| 2379 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2380 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2381 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2382 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2383 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2384 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2385 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2386 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2387 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2388 | MappedOps.push_back(Idx); |
| 2389 | else |
| 2390 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2391 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2392 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2393 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2394 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
| 2397 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2398 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2399 | // Analyze the access pattern of the vector to see if we can extract |
| 2400 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2401 | // the range of elements the mask access on both vectors. |
| 2402 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2403 | int MaxRange[2] = {-1, -1}; |
| 2404 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2405 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2406 | int Idx = Mask[i]; |
| 2407 | int Input = 0; |
| 2408 | if (Idx < 0) |
| 2409 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2410 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2411 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2412 | Input = 1; |
| 2413 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2414 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2415 | if (Idx > MaxRange[Input]) |
| 2416 | MaxRange[Input] = Idx; |
| 2417 | if (Idx < MinRange[Input]) |
| 2418 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2419 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2420 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2421 | // Check if the access is smaller than the vector size and can we find |
| 2422 | // a reasonable extract index. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2423 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2424 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2425 | int StartIdx[2]; // StartIdx to extract from |
| 2426 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2427 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2428 | RangeUse[Input] = 0; // Unused |
| 2429 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2430 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2431 | // 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] | 2432 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2433 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2434 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2435 | StartIdx[Input] = 0; |
| 2436 | } else { |
| 2437 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2438 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2439 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2440 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2441 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2442 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2445 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2446 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2447 | return; |
| 2448 | } |
| 2449 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2450 | // Extract appropriate subvector and generate a vector shuffle |
| 2451 | for (int Input=0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2452 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2453 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2454 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2455 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2456 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2457 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2458 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2459 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2460 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2461 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2462 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2463 | int Idx = Mask[i]; |
| 2464 | if (Idx < 0) |
| 2465 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2466 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2467 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2468 | else |
| 2469 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2470 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2471 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2472 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2473 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2474 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2478 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2479 | // replacing the shuffle with extract and build vector. |
| 2480 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2481 | EVT EltVT = VT.getVectorElementType(); |
| 2482 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2483 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2484 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2485 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2486 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2487 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2488 | int Idx = Mask[i]; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2489 | SDValue Res; |
| 2490 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2491 | if (Idx < (int)SrcNumElts) |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2492 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2493 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2494 | else |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2495 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2496 | EltVT, Src2, |
| 2497 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2498 | |
| 2499 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2500 | } |
| 2501 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2502 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2503 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2504 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2507 | void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2508 | const Value *Op0 = I.getOperand(0); |
| 2509 | const Value *Op1 = I.getOperand(1); |
| 2510 | const Type *AggTy = I.getType(); |
| 2511 | const Type *ValTy = Op1->getType(); |
| 2512 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2513 | bool FromUndef = isa<UndefValue>(Op1); |
| 2514 | |
| 2515 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2516 | I.idx_begin(), I.idx_end()); |
| 2517 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2518 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2519 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2520 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2521 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2522 | |
| 2523 | unsigned NumAggValues = AggValueVTs.size(); |
| 2524 | unsigned NumValValues = ValValueVTs.size(); |
| 2525 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2526 | |
| 2527 | SDValue Agg = getValue(Op0); |
| 2528 | SDValue Val = getValue(Op1); |
| 2529 | unsigned i = 0; |
| 2530 | // Copy the beginning value(s) from the original aggregate. |
| 2531 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2532 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2533 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2534 | // Copy values from the inserted value(s). |
| 2535 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2536 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2537 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2538 | // Copy remaining value(s) from the original aggregate. |
| 2539 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2540 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2541 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2542 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2543 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2544 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2545 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2548 | void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2549 | const Value *Op0 = I.getOperand(0); |
| 2550 | const Type *AggTy = Op0->getType(); |
| 2551 | const Type *ValTy = I.getType(); |
| 2552 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2553 | |
| 2554 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2555 | I.idx_begin(), I.idx_end()); |
| 2556 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2557 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2558 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2559 | |
| 2560 | unsigned NumValValues = ValValueVTs.size(); |
| 2561 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2562 | |
| 2563 | SDValue Agg = getValue(Op0); |
| 2564 | // Copy out the selected value(s). |
| 2565 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2566 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2567 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2568 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2569 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2570 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2571 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2572 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2573 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2576 | void SelectionDAGBuilder::visitGetElementPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2577 | SDValue N = getValue(I.getOperand(0)); |
| 2578 | const Type *Ty = I.getOperand(0)->getType(); |
| 2579 | |
| 2580 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2581 | OI != E; ++OI) { |
| 2582 | Value *Idx = *OI; |
| 2583 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2584 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2585 | if (Field) { |
| 2586 | // N = N + Offset |
| 2587 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2588 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2589 | DAG.getIntPtrConstant(Offset)); |
| 2590 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2591 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2592 | Ty = StTy->getElementType(Field); |
| 2593 | } else { |
| 2594 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2595 | |
| 2596 | // If this is a constant subscript, handle it quickly. |
| 2597 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2598 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2599 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2600 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2601 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2602 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2603 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2604 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2605 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2606 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2607 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2608 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2609 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2610 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2611 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2612 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2613 | continue; |
| 2614 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2615 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2616 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2617 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2618 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2619 | SDValue IdxN = getValue(Idx); |
| 2620 | |
| 2621 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2622 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2623 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2624 | |
| 2625 | // If this is a multiply by a power of two, turn it into a shl |
| 2626 | // immediately. This is a very common case. |
| 2627 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2628 | if (ElementSize.isPowerOf2()) { |
| 2629 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2630 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2631 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2632 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2633 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2634 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2635 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2636 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2640 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2641 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2642 | } |
| 2643 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2644 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2645 | setValue(&I, N); |
| 2646 | } |
| 2647 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2648 | void SelectionDAGBuilder::visitAlloca(AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2649 | // If this is a fixed sized alloca in the entry block of the function, |
| 2650 | // allocate it statically on the stack. |
| 2651 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2652 | return; // getValue will auto-populate this. |
| 2653 | |
| 2654 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2655 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2656 | unsigned Align = |
| 2657 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2658 | I.getAlignment()); |
| 2659 | |
| 2660 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2662 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2663 | AllocSize, |
| 2664 | DAG.getConstant(TySize, AllocSize.getValueType())); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2665 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2666 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2667 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2669 | // Handle alignment. If the requested alignment is less than or equal to |
| 2670 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2671 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2672 | unsigned StackAlign = |
| 2673 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2674 | if (Align <= StackAlign) |
| 2675 | Align = 0; |
| 2676 | |
| 2677 | // Round the size of the allocation up to the stack alignment size |
| 2678 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2679 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2680 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2681 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2682 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2683 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2684 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2685 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2686 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2687 | |
| 2688 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2689 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2690 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2691 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2692 | setValue(&I, DSA); |
| 2693 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2694 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2695 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2696 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2697 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2700 | void SelectionDAGBuilder::visitLoad(LoadInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2701 | const Value *SV = I.getOperand(0); |
| 2702 | SDValue Ptr = getValue(SV); |
| 2703 | |
| 2704 | const Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2705 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2706 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2707 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2708 | unsigned Alignment = I.getAlignment(); |
| 2709 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2710 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2711 | SmallVector<uint64_t, 4> Offsets; |
| 2712 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2713 | unsigned NumValues = ValueVTs.size(); |
| 2714 | if (NumValues == 0) |
| 2715 | return; |
| 2716 | |
| 2717 | SDValue Root; |
| 2718 | bool ConstantMemory = false; |
| 2719 | if (I.isVolatile()) |
| 2720 | // Serialize volatile loads with other side effects. |
| 2721 | Root = getRoot(); |
| 2722 | else if (AA->pointsToConstantMemory(SV)) { |
| 2723 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2724 | Root = DAG.getEntryNode(); |
| 2725 | ConstantMemory = true; |
| 2726 | } else { |
| 2727 | // Do not serialize non-volatile loads against each other. |
| 2728 | Root = DAG.getRoot(); |
| 2729 | } |
| 2730 | |
| 2731 | SmallVector<SDValue, 4> Values(NumValues); |
| 2732 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2733 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2734 | for (unsigned i = 0; i != NumValues; ++i) { |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2735 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2736 | PtrVT, Ptr, |
| 2737 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2738 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2739 | A, SV, Offsets[i], isVolatile, |
| 2740 | isNonTemporal, Alignment); |
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 | Values[i] = L; |
| 2743 | Chains[i] = L.getValue(1); |
| 2744 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2745 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2746 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2747 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2748 | MVT::Other, &Chains[0], NumValues); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2749 | if (isVolatile) |
| 2750 | DAG.setRoot(Chain); |
| 2751 | else |
| 2752 | PendingLoads.push_back(Chain); |
| 2753 | } |
| 2754 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2755 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2756 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2757 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2758 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2759 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2760 | void SelectionDAGBuilder::visitStore(StoreInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2761 | Value *SrcV = I.getOperand(0); |
| 2762 | Value *PtrV = I.getOperand(1); |
| 2763 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2764 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | SmallVector<uint64_t, 4> Offsets; |
| 2766 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2767 | unsigned NumValues = ValueVTs.size(); |
| 2768 | if (NumValues == 0) |
| 2769 | return; |
| 2770 | |
| 2771 | // Get the lowered operands. Note that we do this after |
| 2772 | // checking if NumResults is zero, because with zero results |
| 2773 | // the operands won't have values in the map. |
| 2774 | SDValue Src = getValue(SrcV); |
| 2775 | SDValue Ptr = getValue(PtrV); |
| 2776 | |
| 2777 | SDValue Root = getRoot(); |
| 2778 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2779 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2780 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2781 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2782 | unsigned Alignment = I.getAlignment(); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2783 | |
| 2784 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2785 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2786 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2787 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2788 | SDValue(Src.getNode(), Src.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2789 | Add, PtrV, Offsets[i], isVolatile, |
| 2790 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2793 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2794 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2798 | /// node. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2799 | void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I, |
| 2800 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2801 | bool HasChain = !I.doesNotAccessMemory(); |
| 2802 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2803 | |
| 2804 | // Build the operand list. |
| 2805 | SmallVector<SDValue, 8> Ops; |
| 2806 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2807 | if (OnlyLoad) { |
| 2808 | // We don't need to serialize loads against other loads. |
| 2809 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2810 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | Ops.push_back(getRoot()); |
| 2812 | } |
| 2813 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2814 | |
| 2815 | // Info is set by getTgtMemInstrinsic |
| 2816 | TargetLowering::IntrinsicInfo Info; |
| 2817 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2818 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2819 | // 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] | 2820 | if (!IsTgtIntrinsic) |
| 2821 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2822 | |
| 2823 | // Add all operands of the call to the operand list. |
| 2824 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2825 | SDValue Op = getValue(I.getOperand(i)); |
| 2826 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2827 | "Intrinsic uses a non-legal type?"); |
| 2828 | Ops.push_back(Op); |
| 2829 | } |
| 2830 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2831 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2832 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2833 | #ifndef NDEBUG |
| 2834 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2835 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2836 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2837 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2838 | #endif // NDEBUG |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2839 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2840 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2841 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2842 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2843 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2844 | |
| 2845 | // Create the node. |
| 2846 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2847 | if (IsTgtIntrinsic) { |
| 2848 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2849 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2850 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2851 | Info.memVT, Info.ptrVal, Info.offset, |
| 2852 | Info.align, Info.vol, |
| 2853 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2854 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2855 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2856 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2857 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2858 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2859 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2860 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2861 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2862 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2865 | if (HasChain) { |
| 2866 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2867 | if (OnlyLoad) |
| 2868 | PendingLoads.push_back(Chain); |
| 2869 | else |
| 2870 | DAG.setRoot(Chain); |
| 2871 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2872 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2873 | if (!I.getType()->isVoidTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2874 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2875 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2876 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2877 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2878 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2879 | setValue(&I, Result); |
| 2880 | } |
| 2881 | } |
| 2882 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2883 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2884 | /// number with exponent of 1: |
| 2885 | /// |
| 2886 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2887 | /// |
| 2888 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2889 | static SDValue |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 2890 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2891 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2892 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2893 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 2894 | DAG.getConstant(0x3f800000, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2895 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2898 | /// GetExponent - Get the exponent: |
| 2899 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 2900 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2901 | /// |
| 2902 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2903 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2904 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 2905 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2906 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2907 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 2908 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2909 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2910 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 2911 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2912 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2915 | /// getF32Constant - Get 32-bit floating point constant. |
| 2916 | static SDValue |
| 2917 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2918 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2921 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2922 | /// visitIntrinsicCall: I is a call instruction |
| 2923 | /// Op is the associated NodeType for I |
| 2924 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2925 | SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2926 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2927 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2928 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2929 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2930 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2931 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2932 | getValue(I.getOperand(2)), |
| 2933 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2934 | setValue(&I, L); |
| 2935 | DAG.setRoot(L.getValue(1)); |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2939 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2940 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2941 | SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2942 | SDValue Op1 = getValue(I.getOperand(1)); |
| 2943 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2944 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2945 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2946 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2947 | return 0; |
| 2948 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2949 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2950 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2951 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2952 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2953 | SelectionDAGBuilder::visitExp(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2954 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2955 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2956 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2957 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2958 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2959 | SDValue Op = getValue(I.getOperand(1)); |
| 2960 | |
| 2961 | // Put the exponent in the right bit position for later addition to the |
| 2962 | // final result: |
| 2963 | // |
| 2964 | // #define LOG2OFe 1.4426950f |
| 2965 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2966 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2967 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2968 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2969 | |
| 2970 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2971 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 2972 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2973 | |
| 2974 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2975 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2976 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2977 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2978 | if (LimitFloatPrecision <= 6) { |
| 2979 | // For floating-point precision of 6: |
| 2980 | // |
| 2981 | // TwoToFractionalPartOfX = |
| 2982 | // 0.997535578f + |
| 2983 | // (0.735607626f + 0.252464424f * x) * x; |
| 2984 | // |
| 2985 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2986 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2987 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2988 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2989 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2990 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2991 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2992 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2993 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2994 | |
| 2995 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2996 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2997 | TwoToFracPartOfX, IntegerPartOfX); |
| 2998 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2999 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3000 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3001 | // For floating-point precision of 12: |
| 3002 | // |
| 3003 | // TwoToFractionalPartOfX = |
| 3004 | // 0.999892986f + |
| 3005 | // (0.696457318f + |
| 3006 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3007 | // |
| 3008 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3009 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3010 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3011 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3012 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3013 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3014 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3015 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3016 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3017 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3018 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3019 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3022 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3023 | TwoToFracPartOfX, IntegerPartOfX); |
| 3024 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3025 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3026 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3027 | // For floating-point precision of 18: |
| 3028 | // |
| 3029 | // TwoToFractionalPartOfX = |
| 3030 | // 0.999999982f + |
| 3031 | // (0.693148872f + |
| 3032 | // (0.240227044f + |
| 3033 | // (0.554906021e-1f + |
| 3034 | // (0.961591928e-2f + |
| 3035 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3036 | // |
| 3037 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3038 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3039 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3040 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3041 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3042 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3043 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3044 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3045 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3046 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3047 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3048 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3049 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3050 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3051 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3052 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3053 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3054 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3055 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3056 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3057 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3058 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3061 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3062 | TwoToFracPartOfX, IntegerPartOfX); |
| 3063 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3064 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } else { |
| 3067 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3068 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3069 | getValue(I.getOperand(1)).getValueType(), |
| 3070 | getValue(I.getOperand(1))); |
| 3071 | } |
| 3072 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3073 | setValue(&I, result); |
| 3074 | } |
| 3075 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3076 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3077 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3078 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3079 | SelectionDAGBuilder::visitLog(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3080 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3081 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3082 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3083 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3084 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3085 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3086 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3087 | |
| 3088 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3089 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3090 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3091 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Get the significand and build it into a floating-point number with |
| 3094 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3095 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3096 | |
| 3097 | if (LimitFloatPrecision <= 6) { |
| 3098 | // For floating-point precision of 6: |
| 3099 | // |
| 3100 | // LogofMantissa = |
| 3101 | // -1.1609546f + |
| 3102 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3103 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3104 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3105 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3106 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3107 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3108 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3109 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3110 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3111 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3112 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3113 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3114 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3115 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3116 | // For floating-point precision of 12: |
| 3117 | // |
| 3118 | // LogOfMantissa = |
| 3119 | // -1.7417939f + |
| 3120 | // (2.8212026f + |
| 3121 | // (-1.4699568f + |
| 3122 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3123 | // |
| 3124 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3125 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3126 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3127 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3128 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3129 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3130 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3131 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3132 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3133 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3134 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3135 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3136 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3137 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3138 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3139 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3140 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3141 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3142 | // For floating-point precision of 18: |
| 3143 | // |
| 3144 | // LogOfMantissa = |
| 3145 | // -2.1072184f + |
| 3146 | // (4.2372794f + |
| 3147 | // (-3.7029485f + |
| 3148 | // (2.2781945f + |
| 3149 | // (-0.87823314f + |
| 3150 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3151 | // |
| 3152 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3153 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3154 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3155 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3156 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3157 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3158 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3159 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3160 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3161 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3162 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3163 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3164 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3165 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3166 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3167 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3168 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3169 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3170 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3171 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3172 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3173 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3174 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } else { |
| 3177 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3178 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3179 | getValue(I.getOperand(1)).getValueType(), |
| 3180 | getValue(I.getOperand(1))); |
| 3181 | } |
| 3182 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3183 | setValue(&I, result); |
| 3184 | } |
| 3185 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3186 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3187 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3188 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3189 | SelectionDAGBuilder::visitLog2(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3190 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3191 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3192 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3193 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3194 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3195 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3196 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3197 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3198 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3199 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3200 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3201 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3202 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3203 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3204 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3205 | // Different possible minimax approximations of significand in |
| 3206 | // floating-point for various degrees of accuracy over [1,2]. |
| 3207 | if (LimitFloatPrecision <= 6) { |
| 3208 | // For floating-point precision of 6: |
| 3209 | // |
| 3210 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3211 | // |
| 3212 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3213 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3214 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3215 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3216 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3217 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3218 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3219 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3220 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3221 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3222 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3223 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3224 | // For floating-point precision of 12: |
| 3225 | // |
| 3226 | // Log2ofMantissa = |
| 3227 | // -2.51285454f + |
| 3228 | // (4.07009056f + |
| 3229 | // (-2.12067489f + |
| 3230 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3231 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3232 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3233 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3234 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3235 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3236 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3237 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3238 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3239 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3240 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3241 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3242 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3243 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3244 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3245 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3246 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3247 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3248 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3249 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3250 | // For floating-point precision of 18: |
| 3251 | // |
| 3252 | // Log2ofMantissa = |
| 3253 | // -3.0400495f + |
| 3254 | // (6.1129976f + |
| 3255 | // (-5.3420409f + |
| 3256 | // (3.2865683f + |
| 3257 | // (-1.2669343f + |
| 3258 | // (0.27515199f - |
| 3259 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3260 | // |
| 3261 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3262 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3263 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3264 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3265 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3266 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3267 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3268 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3269 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3270 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3271 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3272 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3273 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3274 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3275 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3276 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3277 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3278 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3279 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3280 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3281 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3282 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3283 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3284 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3285 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3286 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3287 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3288 | getValue(I.getOperand(1)).getValueType(), |
| 3289 | getValue(I.getOperand(1))); |
| 3290 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3291 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3292 | setValue(&I, result); |
| 3293 | } |
| 3294 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3295 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3296 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3297 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3298 | SelectionDAGBuilder::visitLog10(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3299 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3300 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3301 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3302 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3303 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3304 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3305 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3306 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3307 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3308 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3309 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3310 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3311 | |
| 3312 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3313 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 3314 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3315 | |
| 3316 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3317 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3318 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3319 | // Log10ofMantissa = |
| 3320 | // -0.50419619f + |
| 3321 | // (0.60948995f - 0.10380950f * x) * x; |
| 3322 | // |
| 3323 | // error 0.0014886165, which is 6 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, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3326 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3327 | getF32Constant(DAG, 0x3f1c0789)); |
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 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3330 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3331 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3332 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3333 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3334 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3335 | // For floating-point precision of 12: |
| 3336 | // |
| 3337 | // Log10ofMantissa = |
| 3338 | // -0.64831180f + |
| 3339 | // (0.91751397f + |
| 3340 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3341 | // |
| 3342 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3343 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3344 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3345 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3346 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3347 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3348 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3349 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3350 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3351 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3352 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3353 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3354 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3355 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3356 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3357 | // For floating-point precision of 18: |
| 3358 | // |
| 3359 | // Log10ofMantissa = |
| 3360 | // -0.84299375f + |
| 3361 | // (1.5327582f + |
| 3362 | // (-1.0688956f + |
| 3363 | // (0.49102474f + |
| 3364 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3365 | // |
| 3366 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3367 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3368 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3369 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3370 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3371 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3372 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3373 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3374 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3375 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3376 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3377 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3378 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3379 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3380 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3381 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3382 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3383 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3384 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3385 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3386 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3387 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3388 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3389 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3390 | getValue(I.getOperand(1)).getValueType(), |
| 3391 | getValue(I.getOperand(1))); |
| 3392 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3393 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3394 | setValue(&I, result); |
| 3395 | } |
| 3396 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3397 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3398 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3399 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3400 | SelectionDAGBuilder::visitExp2(CallInst &I) { |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3401 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3402 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3403 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3404 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3405 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3406 | SDValue Op = getValue(I.getOperand(1)); |
| 3407 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3408 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3409 | |
| 3410 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3411 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3412 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3413 | |
| 3414 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3415 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3416 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3417 | |
| 3418 | if (LimitFloatPrecision <= 6) { |
| 3419 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3420 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3421 | // TwoToFractionalPartOfX = |
| 3422 | // 0.997535578f + |
| 3423 | // (0.735607626f + 0.252464424f * x) * x; |
| 3424 | // |
| 3425 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3426 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3427 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3428 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3429 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3430 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3431 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3432 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3433 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3434 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3435 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3436 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3437 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3438 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3439 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3440 | // For floating-point precision of 12: |
| 3441 | // |
| 3442 | // TwoToFractionalPartOfX = |
| 3443 | // 0.999892986f + |
| 3444 | // (0.696457318f + |
| 3445 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3446 | // |
| 3447 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3448 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3449 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3450 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3451 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3452 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3453 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3454 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3455 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3456 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3457 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3458 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3459 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3460 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3461 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3462 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3463 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3464 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3465 | // For floating-point precision of 18: |
| 3466 | // |
| 3467 | // TwoToFractionalPartOfX = |
| 3468 | // 0.999999982f + |
| 3469 | // (0.693148872f + |
| 3470 | // (0.240227044f + |
| 3471 | // (0.554906021e-1f + |
| 3472 | // (0.961591928e-2f + |
| 3473 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3474 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3475 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3476 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3477 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3478 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3479 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3480 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3481 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3482 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3483 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3484 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3485 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3486 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3487 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3488 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3489 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3490 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3491 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3492 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3493 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3494 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3495 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3496 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3497 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3498 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3499 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3500 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3501 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3502 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3503 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3504 | getValue(I.getOperand(1)).getValueType(), |
| 3505 | getValue(I.getOperand(1))); |
| 3506 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3507 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3508 | setValue(&I, result); |
| 3509 | } |
| 3510 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3511 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3512 | /// limited-precision mode with x == 10.0f. |
| 3513 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3514 | SelectionDAGBuilder::visitPow(CallInst &I) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3515 | SDValue result; |
| 3516 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3517 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3518 | bool IsExp10 = false; |
| 3519 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3520 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3521 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3522 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3523 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3524 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3525 | APFloat Ten(10.0f); |
| 3526 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3532 | SDValue Op = getValue(I.getOperand(2)); |
| 3533 | |
| 3534 | // Put the exponent in the right bit position for later addition to the |
| 3535 | // final result: |
| 3536 | // |
| 3537 | // #define LOG2OF10 3.3219281f |
| 3538 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3539 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3540 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3541 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3542 | |
| 3543 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3544 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3545 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3546 | |
| 3547 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3548 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3549 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3550 | |
| 3551 | if (LimitFloatPrecision <= 6) { |
| 3552 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3553 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3554 | // twoToFractionalPartOfX = |
| 3555 | // 0.997535578f + |
| 3556 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3557 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3558 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3559 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3560 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3561 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3562 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3563 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3564 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3565 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3566 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3567 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3568 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3569 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3570 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3571 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3572 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3573 | // For floating-point precision of 12: |
| 3574 | // |
| 3575 | // TwoToFractionalPartOfX = |
| 3576 | // 0.999892986f + |
| 3577 | // (0.696457318f + |
| 3578 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3579 | // |
| 3580 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3581 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3582 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3583 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3584 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3585 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3586 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3587 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3588 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3589 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3590 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3591 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3592 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3593 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3594 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3595 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3596 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3597 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3598 | // For floating-point precision of 18: |
| 3599 | // |
| 3600 | // TwoToFractionalPartOfX = |
| 3601 | // 0.999999982f + |
| 3602 | // (0.693148872f + |
| 3603 | // (0.240227044f + |
| 3604 | // (0.554906021e-1f + |
| 3605 | // (0.961591928e-2f + |
| 3606 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3607 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3608 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3609 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3610 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3611 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3612 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3613 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3614 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3615 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3616 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3617 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3618 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3619 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3620 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3621 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3622 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3623 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3624 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3625 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3626 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3627 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3628 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3629 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3630 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3631 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3632 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3633 | } |
| 3634 | } else { |
| 3635 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3636 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3637 | getValue(I.getOperand(1)).getValueType(), |
| 3638 | getValue(I.getOperand(1)), |
| 3639 | getValue(I.getOperand(2))); |
| 3640 | } |
| 3641 | |
| 3642 | setValue(&I, result); |
| 3643 | } |
| 3644 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3645 | |
| 3646 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3647 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3648 | SelectionDAG &DAG) { |
| 3649 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3650 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3651 | // optimizing for size, we only want to do this if the expansion would produce |
| 3652 | // a small number of multiplies, otherwise we do the full expansion. |
| 3653 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3654 | // Get the exponent as a positive value. |
| 3655 | unsigned Val = RHSC->getSExtValue(); |
| 3656 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3657 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3658 | // powi(x, 0) -> 1.0 |
| 3659 | if (Val == 0) |
| 3660 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3661 | |
| 3662 | Function *F = DAG.getMachineFunction().getFunction(); |
| 3663 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3664 | // If optimizing for size, don't insert too many multiplies. This |
| 3665 | // inserts up to 5 multiplies. |
| 3666 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3667 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3668 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3669 | // powi(x,15) generates one more multiply than it should), but this has |
| 3670 | // the benefit of being both really simple and much better than a libcall. |
| 3671 | SDValue Res; // Logically starts equal to 1.0 |
| 3672 | SDValue CurSquare = LHS; |
| 3673 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3674 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3675 | if (Res.getNode()) |
| 3676 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3677 | else |
| 3678 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3679 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3680 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3681 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3682 | CurSquare, CurSquare); |
| 3683 | Val >>= 1; |
| 3684 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3685 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3686 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3687 | if (RHSC->getSExtValue() < 0) |
| 3688 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3689 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3690 | return Res; |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | // Otherwise, expand to a libcall. |
| 3695 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3696 | } |
| 3697 | |
| 3698 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3699 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3700 | /// we want to emit this as a call to a named external function, return the name |
| 3701 | /// otherwise lower it and return null. |
| 3702 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3703 | SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3704 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3705 | SDValue Res; |
| 3706 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3707 | switch (Intrinsic) { |
| 3708 | default: |
| 3709 | // By default, turn this into a target intrinsic node. |
| 3710 | visitTargetIntrinsic(I, Intrinsic); |
| 3711 | return 0; |
| 3712 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3713 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3714 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3715 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3716 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
| 3717 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3718 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3719 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3720 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
| 3721 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3722 | return 0; |
| 3723 | case Intrinsic::setjmp: |
| 3724 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3725 | case Intrinsic::longjmp: |
| 3726 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3727 | case Intrinsic::memcpy: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3728 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3729 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3730 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3731 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3732 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
| 3733 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3734 | return 0; |
| 3735 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3736 | case Intrinsic::memset: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3737 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3738 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3739 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3740 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3741 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, |
| 3742 | I.getOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3743 | return 0; |
| 3744 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3745 | case Intrinsic::memmove: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3746 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3747 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3748 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3749 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3750 | |
| 3751 | // If the source and destination are known to not be aliases, we can |
| 3752 | // lower memmove as memcpy. |
| 3753 | uint64_t Size = -1ULL; |
| 3754 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3755 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3756 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3757 | AliasAnalysis::NoAlias) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3758 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false, |
| 3759 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3760 | return 0; |
| 3761 | } |
| 3762 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3763 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, |
| 3764 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3765 | return 0; |
| 3766 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3767 | case Intrinsic::dbg_declare: { |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3768 | // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None. |
| 3769 | // The real handling of this intrinsic is in FastISel. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3770 | if (OptLevel != CodeGenOpt::None) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3771 | // FIXME: Variable debug info is not supported here. |
| 3772 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3773 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3774 | if (!DW) |
| 3775 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3776 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Chris Lattner | bf0ca2b | 2009-12-29 09:32:19 +0000 | [diff] [blame] | 3777 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3778 | return 0; |
| 3779 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3780 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3781 | Value *Address = DI.getAddress(); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 3782 | if (!Address) |
| 3783 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3784 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 3785 | Address = BCI->getOperand(0); |
| 3786 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 3787 | // Don't handle byval struct arguments or VLAs, for example. |
| 3788 | if (!AI) |
| 3789 | return 0; |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3790 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3791 | FuncInfo.StaticAllocaMap.find(AI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3792 | if (SI == FuncInfo.StaticAllocaMap.end()) |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3793 | return 0; // VLAs. |
| 3794 | int FI = SI->second; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 3795 | |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 3796 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) |
| 3797 | if (MDNode *Dbg = DI.getMetadata("dbg")) |
Chris Lattner | 0eb4198 | 2009-12-28 20:45:51 +0000 | [diff] [blame] | 3798 | MMI->setVariableDbgInfo(Variable, FI, Dbg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3799 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3800 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3801 | case Intrinsic::dbg_value: { |
| 3802 | // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None. |
| 3803 | // The real handling of this intrinsic is in FastISel. |
| 3804 | if (OptLevel != CodeGenOpt::None) |
| 3805 | // FIXME: Variable debug info is not supported here. |
| 3806 | return 0; |
| 3807 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 3808 | if (!DW) |
| 3809 | return 0; |
| 3810 | DbgValueInst &DI = cast<DbgValueInst>(I); |
| 3811 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3812 | return 0; |
| 3813 | |
| 3814 | MDNode *Variable = DI.getVariable(); |
| 3815 | Value *V = DI.getValue(); |
| 3816 | if (!V) |
| 3817 | return 0; |
| 3818 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
| 3819 | V = BCI->getOperand(0); |
| 3820 | AllocaInst *AI = dyn_cast<AllocaInst>(V); |
| 3821 | // Don't handle byval struct arguments or VLAs, for example. |
| 3822 | if (!AI) |
| 3823 | return 0; |
| 3824 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3825 | FuncInfo.StaticAllocaMap.find(AI); |
| 3826 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3827 | return 0; // VLAs. |
| 3828 | int FI = SI->second; |
| 3829 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) |
| 3830 | if (MDNode *Dbg = DI.getMetadata("dbg")) |
| 3831 | MMI->setVariableDbgInfo(Variable, FI, Dbg); |
| 3832 | return 0; |
| 3833 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3834 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3835 | // Insert the EXCEPTIONADDR instruction. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 3836 | assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3837 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3838 | SDValue Ops[1]; |
| 3839 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3840 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3841 | setValue(&I, Op); |
| 3842 | DAG.setRoot(Op.getValue(1)); |
| 3843 | return 0; |
| 3844 | } |
| 3845 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3846 | case Intrinsic::eh_selector: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3847 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3848 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3849 | if (CurMBB->isLandingPad()) |
| 3850 | AddCatchInfo(I, MMI, CurMBB); |
| 3851 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3852 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3853 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3854 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3855 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3856 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 3857 | if (Reg) CurMBB->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3858 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3859 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3860 | // Insert the EHSELECTION instruction. |
| 3861 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3862 | SDValue Ops[2]; |
| 3863 | Ops[0] = getValue(I.getOperand(1)); |
| 3864 | Ops[1] = getRoot(); |
| 3865 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3866 | DAG.setRoot(Op.getValue(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3867 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3871 | case Intrinsic::eh_typeid_for: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3872 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3873 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3874 | if (MMI) { |
| 3875 | // Find the type id for the given typeinfo. |
| 3876 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3877 | unsigned TypeID = MMI->getTypeIDFor(GV); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3878 | Res = DAG.getConstant(TypeID, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3879 | } else { |
| 3880 | // Return something different to eh_selector. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3881 | Res = DAG.getConstant(1, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3884 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3885 | return 0; |
| 3886 | } |
| 3887 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3888 | case Intrinsic::eh_return_i32: |
| 3889 | case Intrinsic::eh_return_i64: |
| 3890 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3891 | MMI->setCallsEHReturn(true); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3892 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 3893 | MVT::Other, |
| 3894 | getControlRoot(), |
| 3895 | getValue(I.getOperand(1)), |
| 3896 | getValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3897 | } else { |
| 3898 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 3899 | } |
| 3900 | |
| 3901 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3902 | case Intrinsic::eh_unwind_init: |
| 3903 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 3904 | MMI->setCallsUnwindInit(true); |
| 3905 | } |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3906 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3907 | case Intrinsic::eh_dwarf_cfa: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3908 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 3909 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 3910 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3911 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3912 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3913 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3914 | TLI.getPointerTy()), |
| 3915 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3916 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3917 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3918 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3919 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 3920 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3921 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3922 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3923 | case Intrinsic::eh_sjlj_callsite: { |
| 3924 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3925 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 3926 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
| 3927 | assert(MMI->getCurrentCallSite() == 0 && "Overlapping call sites!"); |
| 3928 | |
| 3929 | MMI->setCurrentCallSite(CI->getZExtValue()); |
| 3930 | return 0; |
| 3931 | } |
| 3932 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3933 | case Intrinsic::convertff: |
| 3934 | case Intrinsic::convertfsi: |
| 3935 | case Intrinsic::convertfui: |
| 3936 | case Intrinsic::convertsif: |
| 3937 | case Intrinsic::convertuif: |
| 3938 | case Intrinsic::convertss: |
| 3939 | case Intrinsic::convertsu: |
| 3940 | case Intrinsic::convertus: |
| 3941 | case Intrinsic::convertuu: { |
| 3942 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 3943 | switch (Intrinsic) { |
| 3944 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 3945 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 3946 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 3947 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 3948 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 3949 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 3950 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 3951 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 3952 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 3953 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3954 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3955 | Value *Op1 = I.getOperand(1); |
| 3956 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 3957 | DAG.getValueType(DestVT), |
| 3958 | DAG.getValueType(getValue(Op1).getValueType()), |
| 3959 | getValue(I.getOperand(2)), |
| 3960 | getValue(I.getOperand(3)), |
| 3961 | Code); |
| 3962 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3963 | return 0; |
| 3964 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3965 | case Intrinsic::sqrt: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3966 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
| 3967 | getValue(I.getOperand(1)).getValueType(), |
| 3968 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3969 | return 0; |
| 3970 | case Intrinsic::powi: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3971 | setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)), |
| 3972 | getValue(I.getOperand(2)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3973 | return 0; |
| 3974 | case Intrinsic::sin: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3975 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
| 3976 | getValue(I.getOperand(1)).getValueType(), |
| 3977 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3978 | return 0; |
| 3979 | case Intrinsic::cos: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3980 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
| 3981 | getValue(I.getOperand(1)).getValueType(), |
| 3982 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3983 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3984 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3985 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3986 | return 0; |
| 3987 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3988 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3989 | return 0; |
| 3990 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3991 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3992 | return 0; |
| 3993 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3994 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3995 | return 0; |
| 3996 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3997 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3998 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3999 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4000 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4001 | return 0; |
| 4002 | case Intrinsic::pcmarker: { |
| 4003 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4004 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4005 | return 0; |
| 4006 | } |
| 4007 | case Intrinsic::readcyclecounter: { |
| 4008 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4009 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4010 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4011 | &Op, 1); |
| 4012 | setValue(&I, Res); |
| 4013 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4014 | return 0; |
| 4015 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4016 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4017 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
| 4018 | getValue(I.getOperand(1)).getValueType(), |
| 4019 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4020 | return 0; |
| 4021 | case Intrinsic::cttz: { |
| 4022 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4023 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4024 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4025 | return 0; |
| 4026 | } |
| 4027 | case Intrinsic::ctlz: { |
| 4028 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4029 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4030 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4031 | return 0; |
| 4032 | } |
| 4033 | case Intrinsic::ctpop: { |
| 4034 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4035 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4036 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4037 | return 0; |
| 4038 | } |
| 4039 | case Intrinsic::stacksave: { |
| 4040 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4041 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4042 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4043 | setValue(&I, Res); |
| 4044 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4045 | return 0; |
| 4046 | } |
| 4047 | case Intrinsic::stackrestore: { |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4048 | Res = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4049 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4050 | return 0; |
| 4051 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4052 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4053 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4054 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4055 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4056 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4057 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4058 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4059 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4060 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4061 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4062 | MFI->setStackProtectorIndex(FI); |
| 4063 | |
| 4064 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4065 | |
| 4066 | // Store the stack protector onto the stack. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4067 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4068 | PseudoSourceValue::getFixedStack(FI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4069 | 0, true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4070 | setValue(&I, Res); |
| 4071 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4072 | return 0; |
| 4073 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4074 | case Intrinsic::objectsize: { |
| 4075 | // If we don't know by now, we're never going to know. |
| 4076 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4077 | |
| 4078 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4079 | |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4080 | SDValue Arg = getValue(I.getOperand(0)); |
| 4081 | EVT Ty = Arg.getValueType(); |
| 4082 | |
Eric Christopher | d060b25 | 2009-12-23 02:51:48 +0000 | [diff] [blame] | 4083 | if (CI->getZExtValue() == 0) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4084 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4085 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4086 | Res = DAG.getConstant(0, Ty); |
| 4087 | |
| 4088 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4089 | return 0; |
| 4090 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4091 | case Intrinsic::var_annotation: |
| 4092 | // Discard annotate attributes |
| 4093 | return 0; |
| 4094 | |
| 4095 | case Intrinsic::init_trampoline: { |
| 4096 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4097 | |
| 4098 | SDValue Ops[6]; |
| 4099 | Ops[0] = getRoot(); |
| 4100 | Ops[1] = getValue(I.getOperand(1)); |
| 4101 | Ops[2] = getValue(I.getOperand(2)); |
| 4102 | Ops[3] = getValue(I.getOperand(3)); |
| 4103 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4104 | Ops[5] = DAG.getSrcValue(F); |
| 4105 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4106 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4107 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4108 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4109 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4110 | setValue(&I, Res); |
| 4111 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4112 | return 0; |
| 4113 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4114 | case Intrinsic::gcroot: |
| 4115 | if (GFI) { |
| 4116 | Value *Alloca = I.getOperand(1); |
| 4117 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4118 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4119 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4120 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4121 | } |
| 4122 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4123 | case Intrinsic::gcread: |
| 4124 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4125 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4126 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4127 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4128 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4129 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4130 | case Intrinsic::trap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4131 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4132 | return 0; |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4133 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4134 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4135 | case Intrinsic::sadd_with_overflow: |
| 4136 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4137 | case Intrinsic::usub_with_overflow: |
| 4138 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4139 | case Intrinsic::ssub_with_overflow: |
| 4140 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4141 | case Intrinsic::umul_with_overflow: |
| 4142 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4143 | case Intrinsic::smul_with_overflow: |
| 4144 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4146 | case Intrinsic::prefetch: { |
| 4147 | SDValue Ops[4]; |
| 4148 | Ops[0] = getRoot(); |
| 4149 | Ops[1] = getValue(I.getOperand(1)); |
| 4150 | Ops[2] = getValue(I.getOperand(2)); |
| 4151 | Ops[3] = getValue(I.getOperand(3)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4152 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4153 | return 0; |
| 4154 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4155 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4156 | case Intrinsic::memory_barrier: { |
| 4157 | SDValue Ops[6]; |
| 4158 | Ops[0] = getRoot(); |
| 4159 | for (int x = 1; x < 6; ++x) |
| 4160 | Ops[x] = getValue(I.getOperand(x)); |
| 4161 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4162 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4163 | return 0; |
| 4164 | } |
| 4165 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4166 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4167 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4168 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4169 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4170 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4171 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4172 | getValue(I.getOperand(2)), |
| 4173 | getValue(I.getOperand(3)), |
| 4174 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4175 | setValue(&I, L); |
| 4176 | DAG.setRoot(L.getValue(1)); |
| 4177 | return 0; |
| 4178 | } |
| 4179 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4180 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4181 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4182 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4183 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4184 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4185 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4186 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4187 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4188 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4189 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4190 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4191 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4192 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4193 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4194 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4195 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4196 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4197 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4198 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4199 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4200 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4201 | |
| 4202 | case Intrinsic::invariant_start: |
| 4203 | case Intrinsic::lifetime_start: |
| 4204 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4205 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4206 | return 0; |
| 4207 | case Intrinsic::invariant_end: |
| 4208 | case Intrinsic::lifetime_end: |
| 4209 | // Discard region information. |
| 4210 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4214 | /// Test if the given instruction is in a position to be optimized |
| 4215 | /// with a tail-call. This roughly means that it's in a block with |
| 4216 | /// a return and there's nothing that needs to be scheduled |
| 4217 | /// between it and the return. |
| 4218 | /// |
| 4219 | /// This function only tests target-independent requirements. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4220 | static bool |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4221 | isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4222 | const TargetLowering &TLI) { |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4223 | const Instruction *I = CS.getInstruction(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4224 | const BasicBlock *ExitBB = I->getParent(); |
| 4225 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 4226 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
| 4227 | const Function *F = ExitBB->getParent(); |
| 4228 | |
Dan Gohman | c2e93b2 | 2010-02-08 20:34:14 +0000 | [diff] [blame] | 4229 | // The block must end in a return statement or unreachable. |
| 4230 | // |
| 4231 | // FIXME: Decline tailcall if it's not guaranteed and if the block ends in |
| 4232 | // an unreachable, for now. The way tailcall optimization is currently |
| 4233 | // implemented means it will add an epilogue followed by a jump. That is |
| 4234 | // not profitable. Also, if the callee is a special function (e.g. |
| 4235 | // longjmp on x86), it can end up causing miscompilation that has not |
| 4236 | // been fully understood. |
| 4237 | if (!Ret && |
| 4238 | (!GuaranteedTailCallOpt || !isa<UnreachableInst>(Term))) return false; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4239 | |
| 4240 | // If I will have a chain, make sure no other instruction that will have a |
| 4241 | // chain interposes between I and the return. |
| 4242 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
| 4243 | !I->isSafeToSpeculativelyExecute()) |
| 4244 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 4245 | --BBI) { |
| 4246 | if (&*BBI == I) |
| 4247 | break; |
| 4248 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
| 4249 | !BBI->isSafeToSpeculativelyExecute()) |
| 4250 | return false; |
| 4251 | } |
| 4252 | |
| 4253 | // If the block ends with a void return or unreachable, it doesn't matter |
| 4254 | // what the call's return type is. |
| 4255 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 4256 | |
Dan Gohman | ed9bab3 | 2009-11-14 02:06:30 +0000 | [diff] [blame] | 4257 | // If the return value is undef, it doesn't matter what the call's |
| 4258 | // return type is. |
| 4259 | if (isa<UndefValue>(Ret->getOperand(0))) return true; |
| 4260 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4261 | // Conservatively require the attributes of the call to match those of |
Dan Gohman | 01205a8 | 2009-11-13 18:49:38 +0000 | [diff] [blame] | 4262 | // the return. Ignore noalias because it doesn't affect the call sequence. |
| 4263 | unsigned CallerRetAttr = F->getAttributes().getRetAttributes(); |
| 4264 | if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4265 | return false; |
| 4266 | |
Evan Cheng | 6fdce65 | 2010-02-04 19:07:06 +0000 | [diff] [blame] | 4267 | // 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] | 4268 | if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) |
| 4269 | return false; |
| 4270 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4271 | // Otherwise, make sure the unmodified return value of I is the return value. |
| 4272 | for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; |
| 4273 | U = dyn_cast<Instruction>(U->getOperand(0))) { |
| 4274 | if (!U) |
| 4275 | return false; |
| 4276 | if (!U->hasOneUse()) |
| 4277 | return false; |
| 4278 | if (U == I) |
| 4279 | break; |
| 4280 | // Check for a truly no-op truncate. |
| 4281 | if (isa<TruncInst>(U) && |
| 4282 | TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) |
| 4283 | continue; |
| 4284 | // Check for a truly no-op bitcast. |
| 4285 | if (isa<BitCastInst>(U) && |
| 4286 | (U->getOperand(0)->getType() == U->getType() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4287 | (U->getOperand(0)->getType()->isPointerTy() && |
| 4288 | U->getType()->isPointerTy()))) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4289 | continue; |
| 4290 | // Otherwise it's not a true no-op. |
| 4291 | return false; |
| 4292 | } |
| 4293 | |
| 4294 | return true; |
| 4295 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4296 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4297 | void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee, |
| 4298 | bool isTailCall, |
| 4299 | MachineBasicBlock *LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4300 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4301 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4302 | const Type *RetTy = FTy->getReturnType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4303 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4304 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4305 | |
| 4306 | TargetLowering::ArgListTy Args; |
| 4307 | TargetLowering::ArgListEntry Entry; |
| 4308 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4309 | |
| 4310 | // Check whether the function can return without sret-demotion. |
| 4311 | SmallVector<EVT, 4> OutVTs; |
| 4312 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4313 | SmallVector<uint64_t, 4> Offsets; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4314 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4315 | OutVTs, OutsFlags, TLI, &Offsets); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4316 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4317 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4318 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4319 | |
| 4320 | SDValue DemoteStackSlot; |
| 4321 | |
| 4322 | if (!CanLowerReturn) { |
| 4323 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4324 | FTy->getReturnType()); |
| 4325 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4326 | FTy->getReturnType()); |
| 4327 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4328 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4329 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4330 | |
| 4331 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4332 | Entry.Node = DemoteStackSlot; |
| 4333 | Entry.Ty = StackSlotPtrType; |
| 4334 | Entry.isSExt = false; |
| 4335 | Entry.isZExt = false; |
| 4336 | Entry.isInReg = false; |
| 4337 | Entry.isSRet = true; |
| 4338 | Entry.isNest = false; |
| 4339 | Entry.isByVal = false; |
| 4340 | Entry.Alignment = Align; |
| 4341 | Args.push_back(Entry); |
| 4342 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4343 | } |
| 4344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4345 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4346 | i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4347 | SDValue ArgNode = getValue(*i); |
| 4348 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4349 | |
| 4350 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4351 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4352 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4353 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4354 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4355 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4356 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4357 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4358 | Args.push_back(Entry); |
| 4359 | } |
| 4360 | |
| 4361 | if (LandingPad && MMI) { |
| 4362 | // Insert a label before the invoke call to mark the try range. This can be |
| 4363 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4364 | BeginLabel = MMI->NextLabelID(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4365 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4366 | // For SjLj, keep track of which landing pads go with which invokes |
| 4367 | // so as to maintain the ordering of pads in the LSDA. |
| 4368 | unsigned CallSiteIndex = MMI->getCurrentCallSite(); |
| 4369 | if (CallSiteIndex) { |
| 4370 | MMI->setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
| 4371 | // Now that the call site is handled, stop tracking it. |
| 4372 | MMI->setCurrentCallSite(0); |
| 4373 | } |
| 4374 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4375 | // Both PendingLoads and PendingExports must be flushed here; |
| 4376 | // this call might not return. |
| 4377 | (void)getRoot(); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4378 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4379 | getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4382 | // Check if target-independent constraints permit a tail call here. |
| 4383 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4384 | if (isTailCall && |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4385 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4386 | isTailCall = false; |
| 4387 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4388 | std::pair<SDValue,SDValue> Result = |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4389 | TLI.LowerCallTo(getRoot(), RetTy, |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4390 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4391 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4392 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4393 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4394 | isTailCall, |
| 4395 | !CS.getInstruction()->use_empty(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4396 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4397 | assert((isTailCall || Result.second.getNode()) && |
| 4398 | "Non-null chain expected with non-tail call!"); |
| 4399 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4400 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4401 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4402 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4403 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4404 | // The instruction result is the result of loading from the |
| 4405 | // hidden sret parameter. |
| 4406 | SmallVector<EVT, 1> PVTs; |
| 4407 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4408 | |
| 4409 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4410 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4411 | EVT PtrVT = PVTs[0]; |
| 4412 | unsigned NumValues = OutVTs.size(); |
| 4413 | SmallVector<SDValue, 4> Values(NumValues); |
| 4414 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4415 | |
| 4416 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4417 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4418 | DemoteStackSlot, |
| 4419 | DAG.getConstant(Offsets[i], PtrVT)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4420 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4421 | Add, NULL, Offsets[i], false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4422 | Values[i] = L; |
| 4423 | Chains[i] = L.getValue(1); |
| 4424 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4425 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4426 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4427 | MVT::Other, &Chains[0], NumValues); |
| 4428 | PendingLoads.push_back(Chain); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4429 | |
| 4430 | // Collect the legal value parts into potentially illegal values |
| 4431 | // that correspond to the original function's return values. |
| 4432 | SmallVector<EVT, 4> RetTys; |
| 4433 | RetTy = FTy->getReturnType(); |
| 4434 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4435 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4436 | SmallVector<SDValue, 4> ReturnValues; |
| 4437 | unsigned CurReg = 0; |
| 4438 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4439 | EVT VT = RetTys[I]; |
| 4440 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4441 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4442 | |
| 4443 | SDValue ReturnValue = |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4444 | getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs, |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4445 | RegisterVT, VT, AssertOp); |
| 4446 | ReturnValues.push_back(ReturnValue); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4447 | CurReg += NumRegs; |
| 4448 | } |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4449 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4450 | setValue(CS.getInstruction(), |
| 4451 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4452 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4453 | &ReturnValues[0], ReturnValues.size())); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4454 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4455 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4456 | |
| 4457 | // As a special case, a null chain means that a tail call has been emitted and |
| 4458 | // the DAG root is already updated. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4459 | if (Result.second.getNode()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4460 | DAG.setRoot(Result.second); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4461 | else |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4462 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4463 | |
| 4464 | if (LandingPad && MMI) { |
| 4465 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4466 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4467 | EndLabel = MMI->NextLabelID(); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4468 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(), |
| 4469 | getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4470 | |
| 4471 | // Inform MachineModuleInfo of range. |
| 4472 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4473 | } |
| 4474 | } |
| 4475 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4476 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4477 | /// value is equal or not-equal to zero. |
| 4478 | static bool IsOnlyUsedInZeroEqualityComparison(Value *V) { |
| 4479 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 4480 | UI != E; ++UI) { |
| 4481 | if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
| 4482 | if (IC->isEquality()) |
| 4483 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 4484 | if (C->isNullValue()) |
| 4485 | continue; |
| 4486 | // Unknown instruction. |
| 4487 | return false; |
| 4488 | } |
| 4489 | return true; |
| 4490 | } |
| 4491 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4492 | static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4493 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4494 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4495 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4496 | // input is from a string literal. |
| 4497 | if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
| 4498 | // Cast pointer to the type we really want to load. |
| 4499 | LoadInput = ConstantExpr::getBitCast(LoadInput, |
| 4500 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4501 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4502 | if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD)) |
| 4503 | return Builder.getValue(LoadCst); |
| 4504 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4505 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4506 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4507 | // still constant memory, the input chain can be the entry node. |
| 4508 | SDValue Root; |
| 4509 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4510 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4511 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4512 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4513 | Root = Builder.DAG.getEntryNode(); |
| 4514 | ConstantMemory = true; |
| 4515 | } else { |
| 4516 | // Do not serialize non-volatile loads against each other. |
| 4517 | Root = Builder.DAG.getRoot(); |
| 4518 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4519 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4520 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4521 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4522 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4523 | false /*volatile*/, |
| 4524 | false /*nontemporal*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4525 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4526 | if (!ConstantMemory) |
| 4527 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4528 | return LoadVal; |
| 4529 | } |
| 4530 | |
| 4531 | |
| 4532 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4533 | /// If so, return true and lower it, otherwise return false and it will be |
| 4534 | /// lowered like a normal call. |
| 4535 | bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) { |
| 4536 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
| 4537 | if (I.getNumOperands() != 4) |
| 4538 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4539 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4540 | Value *LHS = I.getOperand(1), *RHS = I.getOperand(2); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4541 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
| 4542 | !I.getOperand(3)->getType()->isIntegerTy() || |
| 4543 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4544 | return false; |
| 4545 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4546 | ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4547 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4548 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4549 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4550 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4551 | bool ActuallyDoIt = true; |
| 4552 | MVT LoadVT; |
| 4553 | const Type *LoadTy; |
| 4554 | switch (Size->getZExtValue()) { |
| 4555 | default: |
| 4556 | LoadVT = MVT::Other; |
| 4557 | LoadTy = 0; |
| 4558 | ActuallyDoIt = false; |
| 4559 | break; |
| 4560 | case 2: |
| 4561 | LoadVT = MVT::i16; |
| 4562 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4563 | break; |
| 4564 | case 4: |
| 4565 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4566 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4567 | break; |
| 4568 | case 8: |
| 4569 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4570 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4571 | break; |
| 4572 | /* |
| 4573 | case 16: |
| 4574 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4575 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4576 | LoadTy = VectorType::get(LoadTy, 4); |
| 4577 | break; |
| 4578 | */ |
| 4579 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4580 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4581 | // This turns into unaligned loads. We only do this if the target natively |
| 4582 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4583 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4584 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4585 | // Require that we can find a legal MVT, and only do this if the target |
| 4586 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4587 | // bloat the code. |
| 4588 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4589 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4590 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4591 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4592 | ActuallyDoIt = false; |
| 4593 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4594 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4595 | if (ActuallyDoIt) { |
| 4596 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4597 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4598 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4599 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4600 | ISD::SETNE); |
| 4601 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4602 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4603 | return true; |
| 4604 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4605 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4606 | |
| 4607 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4608 | return false; |
| 4609 | } |
| 4610 | |
| 4611 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4612 | void SelectionDAGBuilder::visitCall(CallInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4613 | const char *RenameFn = 0; |
| 4614 | if (Function *F = I.getCalledFunction()) { |
| 4615 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4616 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4617 | if (II) { |
| 4618 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4619 | RenameFn = visitIntrinsicCall(I, IID); |
| 4620 | if (!RenameFn) |
| 4621 | return; |
| 4622 | } |
| 4623 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4624 | if (unsigned IID = F->getIntrinsicID()) { |
| 4625 | RenameFn = visitIntrinsicCall(I, IID); |
| 4626 | if (!RenameFn) |
| 4627 | return; |
| 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4632 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4633 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4634 | StringRef Name = F->getName(); |
| 4635 | if (Name == "copysign" || Name == "copysignf") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4636 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4637 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4638 | I.getType() == I.getOperand(1)->getType() && |
| 4639 | I.getType() == I.getOperand(2)->getType()) { |
| 4640 | SDValue LHS = getValue(I.getOperand(1)); |
| 4641 | SDValue RHS = getValue(I.getOperand(2)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4642 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4643 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4644 | return; |
| 4645 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4646 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4647 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4648 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4649 | I.getType() == I.getOperand(1)->getType()) { |
| 4650 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4651 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4652 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4653 | return; |
| 4654 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4655 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4656 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4657 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4658 | I.getType() == I.getOperand(1)->getType() && |
| 4659 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4660 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4661 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4662 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4663 | return; |
| 4664 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4665 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4666 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4667 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4668 | I.getType() == I.getOperand(1)->getType() && |
| 4669 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4670 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4671 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4672 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | return; |
| 4674 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4675 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4676 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4677 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4678 | I.getType() == I.getOperand(1)->getType() && |
| 4679 | I.onlyReadsMemory()) { |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4680 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4681 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4682 | Tmp.getValueType(), Tmp)); |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4683 | return; |
| 4684 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4685 | } else if (Name == "memcmp") { |
| 4686 | if (visitMemCmpCall(I)) |
| 4687 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4688 | } |
| 4689 | } |
| 4690 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4691 | visitInlineAsm(&I); |
| 4692 | return; |
| 4693 | } |
| 4694 | |
| 4695 | SDValue Callee; |
| 4696 | if (!RenameFn) |
| 4697 | Callee = getValue(I.getOperand(0)); |
| 4698 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4699 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4700 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4701 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4702 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 4703 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4706 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4707 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4708 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4709 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4710 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4711 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4712 | // Assemble the legal parts into the final values. |
| 4713 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4714 | SmallVector<SDValue, 8> Parts; |
| 4715 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4716 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4717 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4718 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4719 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4720 | |
| 4721 | Parts.resize(NumRegs); |
| 4722 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4723 | SDValue P; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4724 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4725 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4726 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4727 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4728 | *Flag = P.getValue(2); |
| 4729 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4730 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4731 | Chain = P.getValue(1); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4732 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4733 | // If the source register was virtual and if we know something about it, |
| 4734 | // add an assert node. |
| 4735 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4736 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4737 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4738 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4739 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4740 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4741 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4742 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4743 | unsigned NumSignBits = LOI.NumSignBits; |
| 4744 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4745 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4746 | // FIXME: We capture more information than the dag can represent. For |
| 4747 | // now, just use the tightest assertzext/assertsext possible. |
| 4748 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4749 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4750 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4751 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4752 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4753 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4754 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4755 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4756 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4757 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4758 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4759 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4760 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4761 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4762 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4763 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4764 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4765 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4766 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4767 | if (FromVT != MVT::Other) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4768 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4769 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4770 | } |
| 4771 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4772 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4773 | Parts[i] = P; |
| 4774 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4775 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4776 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4777 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4778 | Part += NumRegs; |
| 4779 | Parts.clear(); |
| 4780 | } |
| 4781 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4782 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 4783 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4784 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4788 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4789 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4790 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4791 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4792 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4793 | // Get the list of the values's legal parts. |
| 4794 | unsigned NumRegs = Regs.size(); |
| 4795 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4796 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4797 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4798 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4799 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4800 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4801 | getCopyToParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4802 | Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4803 | &Parts[Part], NumParts, RegisterVT); |
| 4804 | Part += NumParts; |
| 4805 | } |
| 4806 | |
| 4807 | // Copy the parts into the registers. |
| 4808 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4809 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4810 | SDValue Part; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4811 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4812 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4813 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4814 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4815 | *Flag = Part.getValue(1); |
| 4816 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4817 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4818 | Chains[i] = Part.getValue(0); |
| 4819 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4820 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4821 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4822 | // 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] | 4823 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4824 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4825 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4826 | // user as well as a successor (the TF operands are flagged to the user). |
| 4827 | // c1, f1 = CopyToReg |
| 4828 | // c2, f2 = CopyToReg |
| 4829 | // c3 = TokenFactor c1, c2 |
| 4830 | // ... |
| 4831 | // = op c3, ..., f2 |
| 4832 | Chain = Chains[NumRegs-1]; |
| 4833 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4834 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4838 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4839 | /// values added into it. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4840 | void RegsForValue::AddInlineAsmOperands(unsigned Code, |
| 4841 | bool HasMatching,unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 4842 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4843 | std::vector<SDValue> &Ops) const { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4844 | assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!"); |
| 4845 | unsigned Flag = Code | (Regs.size() << 3); |
| 4846 | if (HasMatching) |
| 4847 | Flag |= 0x80000000 | (MatchingIdx << 16); |
Dale Johannesen | 9949933 | 2009-12-23 07:32:51 +0000 | [diff] [blame] | 4848 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 4849 | Ops.push_back(Res); |
| 4850 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4851 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4852 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4853 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4854 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4855 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4856 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4857 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4861 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4862 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4863 | /// register class for the register. Otherwise, return null. |
| 4864 | static const TargetRegisterClass * |
| 4865 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4866 | const TargetLowering &TLI, |
| 4867 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4868 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4869 | const TargetRegisterClass *FoundRC = 0; |
| 4870 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4871 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4872 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4873 | |
| 4874 | const TargetRegisterClass *RC = *RCI; |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 4875 | // If none of the value types for this register class are valid, we |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4876 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4877 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4878 | I != E; ++I) { |
| 4879 | if (TLI.isTypeLegal(*I)) { |
| 4880 | // If we have already found this register in a different register class, |
| 4881 | // choose the one with the largest VT specified. For example, on |
| 4882 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4883 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4884 | ThisVT = *I; |
| 4885 | break; |
| 4886 | } |
| 4887 | } |
| 4888 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4889 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4890 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4891 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4892 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4893 | // frame pointer in functions that need it (due to them not being taken |
| 4894 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4895 | // yet). This is a slight code pessimization, but should still work. |
| 4896 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4897 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4898 | if (*I == Reg) { |
| 4899 | // We found a matching register class. Keep looking at others in case |
| 4900 | // we find one with larger registers that this physreg is also in. |
| 4901 | FoundRC = RC; |
| 4902 | FoundVT = ThisVT; |
| 4903 | break; |
| 4904 | } |
| 4905 | } |
| 4906 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4907 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4908 | |
| 4909 | |
| 4910 | namespace llvm { |
| 4911 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4912 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4913 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4914 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4915 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4916 | /// CallOperand - If this is the result output operand or a clobber |
| 4917 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4918 | /// This gets modified as the asm is processed. |
| 4919 | SDValue CallOperand; |
| 4920 | |
| 4921 | /// AssignedRegs - If this is a register or register class operand, this |
| 4922 | /// contains the set of register corresponding to the operand. |
| 4923 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4924 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4925 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4926 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4927 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4928 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4929 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4930 | /// busy in OutputRegs/InputRegs. |
| 4931 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4932 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4933 | std::set<unsigned> &InputRegs, |
| 4934 | const TargetRegisterInfo &TRI) const { |
| 4935 | if (isOutReg) { |
| 4936 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4937 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4938 | } |
| 4939 | if (isInReg) { |
| 4940 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4941 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4942 | } |
| 4943 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4944 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4945 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4946 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4947 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4948 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4949 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4950 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4951 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4952 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4953 | if (isa<BasicBlock>(CallOperandVal)) |
| 4954 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4955 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4956 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4957 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4958 | // If this is an indirect operand, the operand is a pointer to the |
| 4959 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4960 | if (isIndirect) { |
| 4961 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4962 | if (!PtrTy) |
| 4963 | llvm_report_error("Indirect operand for inline asm not a pointer!"); |
| 4964 | OpTy = PtrTy->getElementType(); |
| 4965 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4966 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4967 | // If OpTy is not a single value, it may be a struct/union that we |
| 4968 | // can tile with integers. |
| 4969 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4970 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4971 | switch (BitSize) { |
| 4972 | default: break; |
| 4973 | case 1: |
| 4974 | case 8: |
| 4975 | case 16: |
| 4976 | case 32: |
| 4977 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4978 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4979 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4980 | break; |
| 4981 | } |
| 4982 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4983 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4984 | return TLI.getValueType(OpTy, true); |
| 4985 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4986 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4987 | private: |
| 4988 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4989 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4990 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4991 | const TargetRegisterInfo &TRI) { |
| 4992 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4993 | Regs.insert(Reg); |
| 4994 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4995 | for (; *Aliases; ++Aliases) |
| 4996 | Regs.insert(*Aliases); |
| 4997 | } |
| 4998 | }; |
| 4999 | } // end llvm namespace. |
| 5000 | |
| 5001 | |
| 5002 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5003 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5004 | /// register allocator to handle the assignment process. However, if the asm |
| 5005 | /// 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] | 5006 | /// allocation. This produces generally horrible, but correct, code. |
| 5007 | /// |
| 5008 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5009 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 5010 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5011 | void SelectionDAGBuilder:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5012 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5013 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5014 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 5015 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5016 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5017 | // Compute whether this value requires an input register, an output register, |
| 5018 | // or both. |
| 5019 | bool isOutReg = false; |
| 5020 | bool isInReg = false; |
| 5021 | switch (OpInfo.Type) { |
| 5022 | case InlineAsm::isOutput: |
| 5023 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5024 | |
| 5025 | // 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] | 5026 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5027 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5028 | break; |
| 5029 | case InlineAsm::isInput: |
| 5030 | isInReg = true; |
| 5031 | isOutReg = false; |
| 5032 | break; |
| 5033 | case InlineAsm::isClobber: |
| 5034 | isOutReg = true; |
| 5035 | isInReg = true; |
| 5036 | break; |
| 5037 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5038 | |
| 5039 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5040 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5041 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5042 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5043 | // If this is a constraint for a single physreg, or a constraint for a |
| 5044 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5045 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5046 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5047 | OpInfo.ConstraintVT); |
| 5048 | |
| 5049 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5050 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5051 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5052 | // cast of the input value. More generally, handle any case where the input |
| 5053 | // value disagrees with the register class we plan to stick this in. |
| 5054 | if (OpInfo.Type == InlineAsm::isInput && |
| 5055 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5056 | // 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] | 5057 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5058 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5059 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5060 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5061 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5062 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5063 | OpInfo.ConstraintVT = RegVT; |
| 5064 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5065 | // If the input is a FP value and we want it in FP registers, do a |
| 5066 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5067 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5068 | // machine. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5069 | RegVT = EVT::getIntegerVT(Context, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5070 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5071 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5072 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5073 | OpInfo.ConstraintVT = RegVT; |
| 5074 | } |
| 5075 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5076 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5077 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5078 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5079 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5080 | EVT RegVT; |
| 5081 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5082 | |
| 5083 | // If this is a constraint for a specific physical register, like {r17}, |
| 5084 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5085 | if (unsigned AssignedReg = PhysReg.first) { |
| 5086 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5087 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5088 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5089 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5090 | // Get the actual register value type. This is important, because the user |
| 5091 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5092 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5093 | RegVT = *RC->vt_begin(); |
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 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5096 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5097 | |
| 5098 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5099 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5100 | TargetRegisterClass::iterator I = RC->begin(); |
| 5101 | for (; *I != AssignedReg; ++I) |
| 5102 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5103 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5104 | // Already added the first reg. |
| 5105 | --NumRegs; ++I; |
| 5106 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5107 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5108 | Regs.push_back(*I); |
| 5109 | } |
| 5110 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5111 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5112 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5113 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5114 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5115 | return; |
| 5116 | } |
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 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5119 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5120 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5121 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5122 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5123 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5124 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5125 | // Create the appropriate number of virtual registers. |
| 5126 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5127 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5128 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5129 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5130 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5131 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5132 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5133 | |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5134 | // This is a reference to a register class that doesn't directly correspond |
| 5135 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5136 | // registers from the class. |
| 5137 | std::vector<unsigned> RegClassRegs |
| 5138 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5139 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5140 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5141 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5142 | unsigned NumAllocated = 0; |
| 5143 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5144 | unsigned Reg = RegClassRegs[i]; |
| 5145 | // See if this register is available. |
| 5146 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5147 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5148 | // Make sure we find consecutive registers. |
| 5149 | NumAllocated = 0; |
| 5150 | continue; |
| 5151 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5152 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5153 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5154 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5155 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5156 | if (!RC) { // Couldn't allocate this register. |
| 5157 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5158 | NumAllocated = 0; |
| 5159 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5160 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5161 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5162 | // Okay, this register is good, we can use it. |
| 5163 | ++NumAllocated; |
| 5164 | |
| 5165 | // If we allocated enough consecutive registers, succeed. |
| 5166 | if (NumAllocated == NumRegs) { |
| 5167 | unsigned RegStart = (i-NumAllocated)+1; |
| 5168 | unsigned RegEnd = i+1; |
| 5169 | // Mark all of the allocated registers used. |
| 5170 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5171 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5172 | |
| 5173 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5174 | OpInfo.ConstraintVT); |
| 5175 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5176 | return; |
| 5177 | } |
| 5178 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5180 | // Otherwise, we couldn't allocate enough registers for this. |
| 5181 | } |
| 5182 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5183 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5184 | /// processed uses a memory 'm' constraint. |
| 5185 | static bool |
| 5186 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5187 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5188 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5189 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5190 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5191 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5192 | if (CType == TargetLowering::C_Memory) |
| 5193 | return true; |
| 5194 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5195 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5196 | // Indirect operand accesses access memory. |
| 5197 | if (CI.isIndirect) |
| 5198 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | return false; |
| 5202 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5203 | |
| 5204 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5205 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5206 | void SelectionDAGBuilder::visitInlineAsm(CallSite CS) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5207 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5208 | |
| 5209 | /// ConstraintOperands - Information about all of the constraints. |
| 5210 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5211 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5212 | std::set<unsigned> OutputRegs, InputRegs; |
| 5213 | |
| 5214 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5215 | // ConstraintOperands list. |
| 5216 | std::vector<InlineAsm::ConstraintInfo> |
| 5217 | ConstraintInfos = IA->ParseConstraints(); |
| 5218 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5219 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5220 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5221 | SDValue Chain, Flag; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5222 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5223 | // We won't need to flush pending loads if this asm doesn't touch |
| 5224 | // memory and is nonvolatile. |
| 5225 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5226 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5227 | else |
| 5228 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5230 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5231 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5232 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5233 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5234 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5235 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5236 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5237 | |
| 5238 | // Compute the value type for each operand. |
| 5239 | switch (OpInfo.Type) { |
| 5240 | case InlineAsm::isOutput: |
| 5241 | // Indirect outputs just consume an argument. |
| 5242 | if (OpInfo.isIndirect) { |
| 5243 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5244 | break; |
| 5245 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5246 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5247 | // The return value of the call is this value. As such, there is no |
| 5248 | // corresponding argument. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5249 | assert(!CS.getType()->isVoidTy() && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5250 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5251 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5252 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5253 | } else { |
| 5254 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5255 | OpVT = TLI.getValueType(CS.getType()); |
| 5256 | } |
| 5257 | ++ResNo; |
| 5258 | break; |
| 5259 | case InlineAsm::isInput: |
| 5260 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5261 | break; |
| 5262 | case InlineAsm::isClobber: |
| 5263 | // Nothing to do. |
| 5264 | break; |
| 5265 | } |
| 5266 | |
| 5267 | // If this is an input or an indirect output, process the call argument. |
| 5268 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5269 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5270 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5271 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5272 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5273 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5274 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5275 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5276 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5277 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5278 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5279 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5280 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5281 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5282 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5283 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5285 | // Second pass over the constraints: compute which constraint option to use |
| 5286 | // and assign registers to constraints that want a specific physreg. |
| 5287 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5288 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5289 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5290 | // 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] | 5291 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5292 | // other is floating point, or their sizes are different, flag it as an |
| 5293 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5294 | if (OpInfo.hasMatchingInput()) { |
| 5295 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
| 5296 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5297 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5298 | Input.ConstraintVT.isInteger()) || |
| 5299 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5300 | Input.ConstraintVT.getSizeInBits())) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5301 | llvm_report_error("Unsupported asm: input constraint" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5302 | " with a matching output constraint of incompatible" |
| 5303 | " type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5304 | } |
| 5305 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5306 | } |
| 5307 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5308 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5309 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5310 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5311 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5312 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5313 | // need to to provide an address for the memory input. |
| 5314 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5315 | !OpInfo.isIndirect) { |
| 5316 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5317 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5318 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5319 | // Memory operands really want the address of the value. If we don't have |
| 5320 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5321 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5322 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5323 | // If the operand is a float, integer, or vector constant, spill to a |
| 5324 | // constant pool entry to get its address. |
| 5325 | Value *OpVal = OpInfo.CallOperandVal; |
| 5326 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5327 | isa<ConstantVector>(OpVal)) { |
| 5328 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5329 | TLI.getPointerTy()); |
| 5330 | } else { |
| 5331 | // Otherwise, create a stack slot and emit a store to it before the |
| 5332 | // asm. |
| 5333 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5334 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5335 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5336 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5337 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5338 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5339 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5340 | OpInfo.CallOperand, StackSlot, NULL, 0, |
| 5341 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5342 | OpInfo.CallOperand = StackSlot; |
| 5343 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5345 | // There is no longer a Value* corresponding to this operand. |
| 5346 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5347 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5348 | // It is now an indirect operand. |
| 5349 | OpInfo.isIndirect = true; |
| 5350 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5351 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5352 | // If this constraint is for a specific register, allocate it before |
| 5353 | // anything else. |
| 5354 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5355 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5356 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5357 | |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5358 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5359 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5360 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5361 | // to register class operands. |
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]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5364 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5365 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5366 | // to be. |
| 5367 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5368 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5371 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5372 | std::vector<SDValue> AsmNodeOperands; |
| 5373 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5374 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 5375 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5376 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5377 | |
| 5378 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5379 | // Loop over all of the inputs, copying the operand values into the |
| 5380 | // appropriate registers and processing the output regs. |
| 5381 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5382 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5383 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5384 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5385 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5386 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5387 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5388 | |
| 5389 | switch (OpInfo.Type) { |
| 5390 | case InlineAsm::isOutput: { |
| 5391 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5392 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5393 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5394 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5395 | |
| 5396 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5397 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5398 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5399 | TLI.getPointerTy())); |
| 5400 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5401 | break; |
| 5402 | } |
| 5403 | |
| 5404 | // Otherwise, this is a register or register class output. |
| 5405 | |
| 5406 | // Copy the output from the appropriate register. Find a register that |
| 5407 | // we can use. |
| 5408 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5409 | llvm_report_error("Couldn't allocate output reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5410 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | // If this is an indirect operand, store through the pointer after the |
| 5414 | // asm. |
| 5415 | if (OpInfo.isIndirect) { |
| 5416 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5417 | OpInfo.CallOperandVal)); |
| 5418 | } else { |
| 5419 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5420 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5421 | // Concatenate this output onto the outputs list. |
| 5422 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5423 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5424 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5425 | // Add information to the INLINEASM node to know that this register is |
| 5426 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5427 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 5428 | 6 /* EARLYCLOBBER REGDEF */ : |
| 5429 | 2 /* REGDEF */ , |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5430 | false, |
| 5431 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5432 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5433 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5434 | break; |
| 5435 | } |
| 5436 | case InlineAsm::isInput: { |
| 5437 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5438 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5439 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5440 | // If this is required to match an output register we have already set, |
| 5441 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5442 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5443 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5444 | // Scan until we find the definition we already emitted of this operand. |
| 5445 | // When we find it, create a RegsForValue operand. |
| 5446 | unsigned CurOp = 2; // The first operand. |
| 5447 | for (; OperandNo; --OperandNo) { |
| 5448 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5449 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5450 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5451 | assert(((OpFlag & 7) == 2 /*REGDEF*/ || |
| 5452 | (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
| 5453 | (OpFlag & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5454 | "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5455 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5458 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5459 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5460 | if ((OpFlag & 7) == 2 /*REGDEF*/ |
| 5461 | || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
| 5462 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5463 | if (OpInfo.isIndirect) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5464 | llvm_report_error("Don't know how to handle tied indirect " |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5465 | "register inputs yet!"); |
Dan Gohman | 15480bd | 2009-06-15 22:32:41 +0000 | [diff] [blame] | 5466 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5467 | RegsForValue MatchedRegs; |
| 5468 | MatchedRegs.TLI = &TLI; |
| 5469 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5470 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5471 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5472 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5473 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5474 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5475 | MatchedRegs.Regs.push_back |
| 5476 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5477 | |
| 5478 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5479 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5480 | Chain, &Flag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5481 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, |
| 5482 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5483 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5484 | break; |
| 5485 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5486 | assert(((OpFlag & 7) == 4) && "Unknown matching constraint!"); |
| 5487 | assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 && |
| 5488 | "Unexpected number of operands"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5489 | // Add information to the INLINEASM node to know about this input. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5490 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5491 | OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5492 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5493 | TLI.getPointerTy())); |
| 5494 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5495 | break; |
| 5496 | } |
| 5497 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5498 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5499 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5500 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5501 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5502 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5503 | std::vector<SDValue> Ops; |
| 5504 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5505 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5506 | if (Ops.empty()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5507 | llvm_report_error("Invalid operand for inline asm" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5508 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5509 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5510 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5511 | // Add information to the INLINEASM node to know about this input. |
| 5512 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5513 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5514 | TLI.getPointerTy())); |
| 5515 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5516 | break; |
| 5517 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 5518 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5519 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5520 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5521 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5522 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5523 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 5524 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5525 | TLI.getPointerTy())); |
| 5526 | AsmNodeOperands.push_back(InOperandVal); |
| 5527 | break; |
| 5528 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5529 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5530 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5531 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5532 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5533 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5534 | "Don't know how to handle indirect register inputs yet!"); |
| 5535 | |
| 5536 | // Copy the input into the appropriate registers. |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 5537 | if (OpInfo.AssignedRegs.Regs.empty() || |
| 5538 | !OpInfo.AssignedRegs.areValueTypesLegal()) { |
Benjamin Kramer | d5fe92e | 2009-08-03 13:33:33 +0000 | [diff] [blame] | 5539 | llvm_report_error("Couldn't allocate input reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5540 | " constraint '"+ OpInfo.ConstraintCode +"'!"); |
Evan Cheng | aa765b8 | 2008-09-25 00:14:04 +0000 | [diff] [blame] | 5541 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5542 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5543 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5544 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5545 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5546 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5547 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5548 | break; |
| 5549 | } |
| 5550 | case InlineAsm::isClobber: { |
| 5551 | // Add the clobbered value to the operand list, so that the register |
| 5552 | // allocator is aware that the physreg got clobbered. |
| 5553 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 5554 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5555 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5556 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5557 | break; |
| 5558 | } |
| 5559 | } |
| 5560 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5561 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5562 | // Finish up input operands. |
| 5563 | AsmNodeOperands[0] = Chain; |
| 5564 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5565 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5566 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5567 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5568 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5569 | Flag = Chain.getValue(1); |
| 5570 | |
| 5571 | // If this asm returns a register value, copy the result from that register |
| 5572 | // and set it as the value of the call. |
| 5573 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5574 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5575 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5576 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5577 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5578 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5579 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5580 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5581 | // If any of the results of the inline asm is a vector, it may have the |
| 5582 | // wrong width/num elts. This can happen for register classes that can |
| 5583 | // contain multiple different value types. The preg or vreg allocated may |
| 5584 | // not have the same VT as was expected. Convert it to the right type |
| 5585 | // with bit_convert. |
| 5586 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5587 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5588 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5589 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5590 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5591 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5592 | // If a result value was tied to an input value, the computed result may |
| 5593 | // have a wider width than the expected result. Extract the relevant |
| 5594 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5595 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5596 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5597 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5598 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5599 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5600 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5601 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5602 | // Don't need to use this as a chain in this case. |
| 5603 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5604 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5605 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5606 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5607 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5608 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5609 | // Process indirect outputs, first output all of the flagged copies out of |
| 5610 | // physregs. |
| 5611 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5612 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5613 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5614 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5615 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5616 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5617 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5618 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5619 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5620 | // Emit the non-flagged stores from the physregs. |
| 5621 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5622 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5623 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5624 | StoresToEmit[i].first, |
| 5625 | getValue(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5626 | StoresToEmit[i].second, 0, |
| 5627 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5628 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5631 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5632 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5633 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5634 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5635 | DAG.setRoot(Chain); |
| 5636 | } |
| 5637 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5638 | void SelectionDAGBuilder::visitVAStart(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5639 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5640 | MVT::Other, getRoot(), |
| 5641 | getValue(I.getOperand(1)), |
| 5642 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5645 | void SelectionDAGBuilder::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5646 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5647 | getRoot(), getValue(I.getOperand(0)), |
| 5648 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5649 | setValue(&I, V); |
| 5650 | DAG.setRoot(V.getValue(1)); |
| 5651 | } |
| 5652 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5653 | void SelectionDAGBuilder::visitVAEnd(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5654 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5655 | MVT::Other, getRoot(), |
| 5656 | getValue(I.getOperand(1)), |
| 5657 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5658 | } |
| 5659 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5660 | void SelectionDAGBuilder::visitVACopy(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5661 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5662 | MVT::Other, getRoot(), |
| 5663 | getValue(I.getOperand(1)), |
| 5664 | getValue(I.getOperand(2)), |
| 5665 | DAG.getSrcValue(I.getOperand(1)), |
| 5666 | DAG.getSrcValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5669 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5670 | /// implementation, which just calls LowerCall. |
| 5671 | /// FIXME: When all targets are |
| 5672 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5673 | std::pair<SDValue, SDValue> |
| 5674 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5675 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5676 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5677 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5678 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5679 | SDValue Callee, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5680 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5681 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5682 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5683 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5684 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5685 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5686 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5687 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5688 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5689 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5690 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5691 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5692 | ISD::ArgFlagsTy Flags; |
| 5693 | unsigned OriginalAlignment = |
| 5694 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5695 | |
| 5696 | if (Args[i].isZExt) |
| 5697 | Flags.setZExt(); |
| 5698 | if (Args[i].isSExt) |
| 5699 | Flags.setSExt(); |
| 5700 | if (Args[i].isInReg) |
| 5701 | Flags.setInReg(); |
| 5702 | if (Args[i].isSRet) |
| 5703 | Flags.setSRet(); |
| 5704 | if (Args[i].isByVal) { |
| 5705 | Flags.setByVal(); |
| 5706 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5707 | const Type *ElementTy = Ty->getElementType(); |
| 5708 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5709 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5710 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5711 | // info is not there but there are cases it cannot get right. |
| 5712 | if (Args[i].Alignment) |
| 5713 | FrameAlign = Args[i].Alignment; |
| 5714 | Flags.setByValAlign(FrameAlign); |
| 5715 | Flags.setByValSize(FrameSize); |
| 5716 | } |
| 5717 | if (Args[i].isNest) |
| 5718 | Flags.setNest(); |
| 5719 | Flags.setOrigAlign(OriginalAlignment); |
| 5720 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5721 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5722 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5723 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5724 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5725 | |
| 5726 | if (Args[i].isSExt) |
| 5727 | ExtendKind = ISD::SIGN_EXTEND; |
| 5728 | else if (Args[i].isZExt) |
| 5729 | ExtendKind = ISD::ZERO_EXTEND; |
| 5730 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5731 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5732 | PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5733 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5734 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5735 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5736 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5737 | if (NumParts > 1 && j == 0) |
| 5738 | MyFlags.Flags.setSplit(); |
| 5739 | else if (j != 0) |
| 5740 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5741 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5742 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5743 | } |
| 5744 | } |
| 5745 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5746 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5747 | // Handle the incoming return values from the call. |
| 5748 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5749 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5750 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5751 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5752 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5753 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5754 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5755 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5756 | ISD::InputArg MyFlags; |
| 5757 | MyFlags.VT = RegisterVT; |
| 5758 | MyFlags.Used = isReturnValueUsed; |
| 5759 | if (RetSExt) |
| 5760 | MyFlags.Flags.setSExt(); |
| 5761 | if (RetZExt) |
| 5762 | MyFlags.Flags.setZExt(); |
| 5763 | if (isInreg) |
| 5764 | MyFlags.Flags.setInReg(); |
| 5765 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5766 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5769 | SmallVector<SDValue, 4> InVals; |
Evan Cheng | 022d9e1 | 2010-02-02 23:55:14 +0000 | [diff] [blame] | 5770 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5771 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5772 | |
| 5773 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5774 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5775 | "LowerCall didn't return a valid chain!"); |
| 5776 | assert((!isTailCall || InVals.empty()) && |
| 5777 | "LowerCall emitted a return value for a tail call!"); |
| 5778 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5779 | "LowerCall didn't emit the correct number of values!"); |
| 5780 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5781 | assert(InVals[i].getNode() && |
| 5782 | "LowerCall emitted a null value!"); |
| 5783 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5784 | "LowerCall emitted a value with the wrong type!"); |
| 5785 | }); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5786 | |
| 5787 | // For a tail call, the return value is merely live-out and there aren't |
| 5788 | // any nodes in the DAG representing it. Return a special value to |
| 5789 | // indicate that a tail call has been emitted and no more Instructions |
| 5790 | // should be processed in the current block. |
| 5791 | if (isTailCall) { |
| 5792 | DAG.setRoot(Chain); |
| 5793 | return std::make_pair(SDValue(), SDValue()); |
| 5794 | } |
| 5795 | |
| 5796 | // Collect the legal value parts into potentially illegal values |
| 5797 | // that correspond to the original function's return values. |
| 5798 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5799 | if (RetSExt) |
| 5800 | AssertOp = ISD::AssertSext; |
| 5801 | else if (RetZExt) |
| 5802 | AssertOp = ISD::AssertZext; |
| 5803 | SmallVector<SDValue, 4> ReturnValues; |
| 5804 | unsigned CurReg = 0; |
| 5805 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5806 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5807 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5808 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5809 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5810 | ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg], |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5811 | NumRegs, RegisterVT, VT, |
| 5812 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5813 | CurReg += NumRegs; |
| 5814 | } |
| 5815 | |
| 5816 | // For a function returning void, there is no return value. We can't create |
| 5817 | // such a node, so we just return a null return value in that case. In |
| 5818 | // that case, nothing will actualy look at the value. |
| 5819 | if (ReturnValues.empty()) |
| 5820 | return std::make_pair(SDValue(), Chain); |
| 5821 | |
| 5822 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5823 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5824 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5825 | return std::make_pair(Res, Chain); |
| 5826 | } |
| 5827 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5828 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5829 | SmallVectorImpl<SDValue> &Results, |
| 5830 | SelectionDAG &DAG) { |
| 5831 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5832 | if (Res.getNode()) |
| 5833 | Results.push_back(Res); |
| 5834 | } |
| 5835 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5836 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5837 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5838 | return SDValue(); |
| 5839 | } |
| 5840 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5841 | void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5842 | SDValue Op = getValue(V); |
| 5843 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5844 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5845 | "Copy from a reg to the same reg!"); |
| 5846 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5847 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5848 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5849 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5850 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5851 | PendingExports.push_back(Chain); |
| 5852 | } |
| 5853 | |
| 5854 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5855 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 5856 | void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5857 | // If this is the entry block, emit arguments. |
| 5858 | Function &F = *LLVMBB->getParent(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5859 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5860 | SDValue OldRoot = DAG.getRoot(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5861 | DebugLoc dl = SDB->getCurDebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5862 | const TargetData *TD = TLI.getTargetData(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5863 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5864 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5865 | // Check whether the function can return without sret-demotion. |
| 5866 | SmallVector<EVT, 4> OutVTs; |
| 5867 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5868 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5869 | OutVTs, OutsFlags, TLI); |
| 5870 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 5871 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5872 | FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5873 | OutVTs, OutsFlags, DAG); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5874 | if (!FLI.CanLowerReturn) { |
| 5875 | // Put in an sret pointer parameter before all the other parameters. |
| 5876 | SmallVector<EVT, 1> ValueVTs; |
| 5877 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5878 | |
| 5879 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5880 | // or one register. |
| 5881 | ISD::ArgFlagsTy Flags; |
| 5882 | Flags.setSRet(); |
| 5883 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]); |
| 5884 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5885 | Ins.push_back(RetArg); |
| 5886 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5887 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5888 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5889 | unsigned Idx = 1; |
| 5890 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5891 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5892 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5893 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5894 | bool isArgValueUsed = !I->use_empty(); |
| 5895 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5896 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5897 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5898 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5899 | ISD::ArgFlagsTy Flags; |
| 5900 | unsigned OriginalAlignment = |
| 5901 | TD->getABITypeAlignment(ArgTy); |
| 5902 | |
| 5903 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5904 | Flags.setZExt(); |
| 5905 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5906 | Flags.setSExt(); |
| 5907 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5908 | Flags.setInReg(); |
| 5909 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5910 | Flags.setSRet(); |
| 5911 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5912 | Flags.setByVal(); |
| 5913 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5914 | const Type *ElementTy = Ty->getElementType(); |
| 5915 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5916 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5917 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5918 | // this info is not there but there are cases it cannot get right. |
| 5919 | if (F.getParamAlignment(Idx)) |
| 5920 | FrameAlign = F.getParamAlignment(Idx); |
| 5921 | Flags.setByValAlign(FrameAlign); |
| 5922 | Flags.setByValSize(FrameSize); |
| 5923 | } |
| 5924 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5925 | Flags.setNest(); |
| 5926 | Flags.setOrigAlign(OriginalAlignment); |
| 5927 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5928 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5929 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5930 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5931 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5932 | if (NumRegs > 1 && i == 0) |
| 5933 | MyFlags.Flags.setSplit(); |
| 5934 | // if it isn't first piece, alignment must be 1 |
| 5935 | else if (i > 0) |
| 5936 | MyFlags.Flags.setOrigAlign(1); |
| 5937 | Ins.push_back(MyFlags); |
| 5938 | } |
| 5939 | } |
| 5940 | } |
| 5941 | |
| 5942 | // Call the target to set up the argument values. |
| 5943 | SmallVector<SDValue, 8> InVals; |
| 5944 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5945 | F.isVarArg(), Ins, |
| 5946 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5947 | |
| 5948 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5949 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5950 | "LowerFormalArguments didn't return a valid chain!"); |
| 5951 | assert(InVals.size() == Ins.size() && |
| 5952 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5953 | DEBUG({ |
| 5954 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5955 | assert(InVals[i].getNode() && |
| 5956 | "LowerFormalArguments emitted a null value!"); |
| 5957 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5958 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5959 | } |
| 5960 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5961 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5962 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5963 | DAG.setRoot(NewRoot); |
| 5964 | |
| 5965 | // Set up the argument values. |
| 5966 | unsigned i = 0; |
| 5967 | Idx = 1; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5968 | if (!FLI.CanLowerReturn) { |
| 5969 | // Create a virtual register for the sret pointer, and put in a copy |
| 5970 | // from the sret argument into it. |
| 5971 | SmallVector<EVT, 1> ValueVTs; |
| 5972 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5973 | EVT VT = ValueVTs[0]; |
| 5974 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5975 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 5976 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5977 | RegVT, VT, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5978 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5979 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5980 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 5981 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
| 5982 | FLI.DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5983 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 5984 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5985 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5986 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5987 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 5988 | // Idx indexes LLVM arguments. Don't touch it. |
| 5989 | ++i; |
| 5990 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5991 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5992 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 5993 | ++I, ++Idx) { |
| 5994 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5995 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5996 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5997 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5998 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5999 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6000 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6001 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6002 | |
| 6003 | if (!I->use_empty()) { |
| 6004 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 6005 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 6006 | AssertOp = ISD::AssertSext; |
| 6007 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 6008 | AssertOp = ISD::AssertZext; |
| 6009 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame^] | 6010 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6011 | NumParts, PartVT, VT, |
| 6012 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6013 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6014 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6015 | i += NumParts; |
| 6016 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6017 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6018 | if (!I->use_empty()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6019 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 6020 | SDB->getCurDebugLoc()); |
| 6021 | SDB->setValue(I, Res); |
| 6022 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6023 | // If this argument is live outside of the entry block, insert a copy from |
| 6024 | // 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] | 6025 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6026 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6027 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6028 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6029 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6030 | |
| 6031 | // Finally, if the target has anything special to do, allow it to do so. |
| 6032 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6033 | EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6037 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6038 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6039 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6040 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6041 | /// the end. |
| 6042 | /// |
| 6043 | void |
| 6044 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 6045 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 6046 | |
| 6047 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6048 | |
| 6049 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6050 | // from this block. |
| 6051 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6052 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6053 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6054 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6055 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6056 | // If this terminator has multiple identical successors (common for |
| 6057 | // switches), only handle each succ once. |
| 6058 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6059 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6060 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6061 | PHINode *PN; |
| 6062 | |
| 6063 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6064 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6065 | // emitted yet. |
| 6066 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6067 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6068 | // Ignore dead phi's. |
| 6069 | if (PN->use_empty()) continue; |
| 6070 | |
| 6071 | unsigned Reg; |
| 6072 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6073 | |
| 6074 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6075 | unsigned &RegOut = SDB->ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6076 | if (RegOut == 0) { |
| 6077 | RegOut = FuncInfo->CreateRegForValue(C); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6078 | SDB->CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6079 | } |
| 6080 | Reg = RegOut; |
| 6081 | } else { |
| 6082 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 6083 | if (Reg == 0) { |
| 6084 | assert(isa<AllocaInst>(PHIOp) && |
| 6085 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 6086 | "Didn't codegen value into a register!??"); |
| 6087 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6088 | SDB->CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6089 | } |
| 6090 | } |
| 6091 | |
| 6092 | // Remember that this register needs to added to the machine PHI node as |
| 6093 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6094 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6095 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6096 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6097 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6098 | unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6099 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6100 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6101 | Reg += NumRegisters; |
| 6102 | } |
| 6103 | } |
| 6104 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6105 | SDB->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6108 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 6109 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 6110 | /// creating SelectionDAG nodes. |
| 6111 | /// |
| 6112 | bool |
| 6113 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 6114 | FastISel *F) { |
| 6115 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6116 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6117 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6118 | unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6119 | |
| 6120 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6121 | // from this block. |
| 6122 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6123 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6124 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6125 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6126 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6127 | // If this terminator has multiple identical successors (common for |
| 6128 | // switches), only handle each succ once. |
| 6129 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6130 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6131 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6132 | PHINode *PN; |
| 6133 | |
| 6134 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6135 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6136 | // emitted yet. |
| 6137 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6138 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6139 | // Ignore dead phi's. |
| 6140 | if (PN->use_empty()) continue; |
| 6141 | |
| 6142 | // Only handle legal types. Two interesting things to note here. First, |
| 6143 | // by bailing out early, we may leave behind some dead instructions, |
| 6144 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6145 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6146 | // use CreateRegForValue to create registers, so it always creates |
| 6147 | // exactly one register for each non-void instruction. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6148 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6149 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 6150 | // Promote MVT::i1. |
| 6151 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6152 | VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6153 | else { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6154 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6155 | return false; |
| 6156 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6160 | |
| 6161 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6162 | if (Reg == 0) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6163 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6164 | return false; |
| 6165 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6166 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | return true; |
| 6171 | } |