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" |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 15 | #include "SDNodeDbgValue.h" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 16 | #include "SelectionDAGBuilder.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 17 | #include "FunctionLoweringInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/GlobalVariable.h" |
| 27 | #include "llvm/InlineAsm.h" |
| 28 | #include "llvm/Instructions.h" |
| 29 | #include "llvm/Intrinsics.h" |
| 30 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 31 | #include "llvm/LLVMContext.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/FastISel.h" |
| 35 | #include "llvm/CodeGen/GCStrategy.h" |
| 36 | #include "llvm/CodeGen/GCMetadata.h" |
| 37 | #include "llvm/CodeGen/MachineFunction.h" |
| 38 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 39 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 40 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 41 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 42 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetRegisterInfo.h" |
| 47 | #include "llvm/Target/TargetData.h" |
| 48 | #include "llvm/Target/TargetFrameInfo.h" |
| 49 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 51 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetOptions.h" |
| 53 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 57 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include <algorithm> |
| 60 | using namespace llvm; |
| 61 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 62 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 63 | /// some float libcalls (6, 8 or 12 bits). |
| 64 | static unsigned LimitFloatPrecision; |
| 65 | |
| 66 | static cl::opt<unsigned, true> |
| 67 | LimitFPPrecision("limit-float-precision", |
| 68 | cl::desc("Generate low-precision inline sequences " |
| 69 | "for some float libcalls"), |
| 70 | cl::location(LimitFloatPrecision), |
| 71 | cl::init(0)); |
| 72 | |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 73 | namespace { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 74 | /// RegsForValue - This struct represents the registers (physical or virtual) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 75 | /// that a particular set of values is assigned, and the type information |
| 76 | /// about the value. The most common situation is to represent one value at a |
| 77 | /// time, but struct or array values are handled element-wise as multiple |
| 78 | /// values. The splitting of aggregates is performed recursively, so that we |
| 79 | /// never have aggregate-typed registers. The values at this point do not |
| 80 | /// necessarily have legal types, so each value may require one or more |
| 81 | /// registers of some legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 82 | /// |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 83 | struct RegsForValue { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 84 | /// TLI - The TargetLowering object. |
| 85 | /// |
| 86 | const TargetLowering *TLI; |
| 87 | |
| 88 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 89 | /// may need be promoted or synthesized from one or more registers. |
| 90 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 91 | SmallVector<EVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 92 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 93 | /// RegVTs - The value types of the registers. This is the same size as |
| 94 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 95 | /// register or registers are. (Individual values are never synthesized |
| 96 | /// from more than one type of register.) |
| 97 | /// |
| 98 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 99 | /// getRegisterType member function, however when with physical registers |
| 100 | /// it is necessary to have a separate record of the types. |
| 101 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 102 | SmallVector<EVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 103 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 104 | /// Regs - This list holds the registers assigned to the values. |
| 105 | /// Each legal or promoted value requires one register, and each |
| 106 | /// expanded value requires multiple registers. |
| 107 | /// |
| 108 | SmallVector<unsigned, 4> Regs; |
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() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 111 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 112 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 113 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 114 | EVT regvt, EVT valuevt) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 115 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 116 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 117 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 118 | const SmallVector<EVT, 4> ®vts, |
| 119 | const SmallVector<EVT, 4> &valuevts) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 120 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 121 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 122 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 123 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 124 | |
| 125 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 126 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 127 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 128 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 129 | for (unsigned i = 0; i != NumRegs; ++i) |
| 130 | Regs.push_back(Reg + i); |
| 131 | RegVTs.push_back(RegisterVT); |
| 132 | Reg += NumRegs; |
| 133 | } |
| 134 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 135 | |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 136 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 137 | bool areValueTypesLegal() { |
| 138 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 139 | EVT RegisterVT = RegVTs[Value]; |
| 140 | if (!TLI->isTypeLegal(RegisterVT)) |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 147 | /// append - Add the specified values to this one. |
| 148 | void append(const RegsForValue &RHS) { |
| 149 | TLI = RHS.TLI; |
| 150 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 151 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 152 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 153 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 154 | |
| 155 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 156 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 157 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 158 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 159 | /// If the Flag pointer is NULL, no flag is used. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 160 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 161 | SDValue &Chain, SDValue *Flag) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 162 | |
| 163 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 164 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 165 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 166 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 167 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 168 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 169 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 170 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 171 | /// operand list. This adds the code marker, matching input operand index |
| 172 | /// (if applicable), and includes the number of values added into it. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 173 | void AddInlineAsmOperands(unsigned Kind, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 174 | bool HasMatching, unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 175 | SelectionDAG &DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 176 | std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 177 | }; |
| 178 | } |
| 179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 180 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 181 | /// combined into the value they represent. If the parts combine to a type |
| 182 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 183 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 184 | /// (ISD::AssertSext). |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 185 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 186 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 187 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 188 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 189 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 190 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | SDValue Val = Parts[0]; |
| 192 | |
| 193 | if (NumParts > 1) { |
| 194 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 195 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | unsigned PartBits = PartVT.getSizeInBits(); |
| 197 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 198 | |
| 199 | // Assemble the power of 2 part. |
| 200 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 201 | 1 << Log2_32(NumParts) : NumParts; |
| 202 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 203 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 204 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 205 | SDValue Lo, Hi; |
| 206 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 207 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 208 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 209 | if (RoundParts > 2) { |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 210 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | PartVT, HalfVT); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 212 | Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 213 | RoundParts / 2, PartVT, HalfVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 214 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 215 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 216 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 217 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 218 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 219 | if (TLI.isBigEndian()) |
| 220 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 221 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 222 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 223 | |
| 224 | if (RoundParts < NumParts) { |
| 225 | // Assemble the trailing non-power-of-2 part. |
| 226 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 227 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 228 | Hi = getCopyFromParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 229 | Parts + RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 230 | |
| 231 | // Combine the round and odd parts. |
| 232 | Lo = Val; |
| 233 | if (TLI.isBigEndian()) |
| 234 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 235 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 236 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 237 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 238 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 239 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 240 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 241 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 242 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 243 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 244 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 245 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 246 | unsigned NumIntermediates; |
| 247 | unsigned NumRegs = |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 248 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 249 | NumIntermediates, RegisterVT); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 250 | assert(NumRegs == NumParts |
| 251 | && "Part count doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 252 | NumParts = NumRegs; // Silence a compiler warning. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 253 | assert(RegisterVT == PartVT |
| 254 | && "Part type doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 255 | assert(RegisterVT == Parts[0].getValueType() && |
| 256 | "Part type doesn't match part!"); |
| 257 | |
| 258 | // Assemble the parts into intermediate operands. |
| 259 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 260 | if (NumIntermediates == NumParts) { |
| 261 | // If the register was not expanded, truncate or copy the value, |
| 262 | // as appropriate. |
| 263 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 264 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 265 | PartVT, IntermediateVT); |
| 266 | } else if (NumParts > 0) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 267 | // If the intermediate type was expanded, build the intermediate |
| 268 | // operands from the parts. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 269 | assert(NumParts % NumIntermediates == 0 && |
| 270 | "Must expand into a divisible number of parts!"); |
| 271 | unsigned Factor = NumParts / NumIntermediates; |
| 272 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 273 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 274 | PartVT, IntermediateVT); |
| 275 | } |
| 276 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 277 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 278 | // intermediate operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 279 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 280 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 281 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 282 | } else if (PartVT.isFloatingPoint()) { |
| 283 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 284 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 285 | "Unexpected split"); |
| 286 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 287 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 288 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 289 | if (TLI.isBigEndian()) |
| 290 | std::swap(Lo, Hi); |
| 291 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 292 | } else { |
| 293 | // FP split into integer parts (soft fp) |
| 294 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 295 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 296 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 297 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 302 | PartVT = Val.getValueType(); |
| 303 | |
| 304 | if (PartVT == ValueVT) |
| 305 | return Val; |
| 306 | |
| 307 | if (PartVT.isVector()) { |
| 308 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 309 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (ValueVT.isVector()) { |
| 313 | assert(ValueVT.getVectorElementType() == PartVT && |
| 314 | ValueVT.getVectorNumElements() == 1 && |
| 315 | "Only trivial scalar-to-vector conversions should get here!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 316 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (PartVT.isInteger() && |
| 320 | ValueVT.isInteger()) { |
| 321 | if (ValueVT.bitsLT(PartVT)) { |
| 322 | // For a truncate, see if we have any information to |
| 323 | // indicate whether the truncated bits will always be |
| 324 | // zero or sign-extension. |
| 325 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 326 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 327 | DAG.getValueType(ValueVT)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 328 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 329 | } else { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 330 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 335 | if (ValueVT.bitsLT(Val.getValueType())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 336 | // FP_ROUND's are always exact here. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 337 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 338 | DAG.getIntPtrConstant(1)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 341 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 344 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 345 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 346 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 347 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 348 | return SDValue(); |
| 349 | } |
| 350 | |
| 351 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 352 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 353 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 354 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 355 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 356 | EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 357 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 358 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 359 | EVT PtrVT = TLI.getPointerTy(); |
| 360 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 361 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 362 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 363 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 364 | |
| 365 | if (!NumParts) |
| 366 | return; |
| 367 | |
| 368 | if (!ValueVT.isVector()) { |
| 369 | if (PartVT == ValueVT) { |
| 370 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 371 | Parts[0] = Val; |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 376 | // If the parts cover more bits than the value has, promote the value. |
| 377 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 378 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 379 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 380 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 381 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 382 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 383 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 384 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 385 | } |
| 386 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 387 | // Different types of the same size. |
| 388 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 389 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 390 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 391 | // If the parts cover less bits than value has, truncate the value. |
| 392 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 393 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 394 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 395 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 396 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | // The value may have changed - recompute ValueVT. |
| 401 | ValueVT = Val.getValueType(); |
| 402 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 403 | "Failed to tile the value with PartVT!"); |
| 404 | |
| 405 | if (NumParts == 1) { |
| 406 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 407 | Parts[0] = Val; |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // Expand the value into multiple parts. |
| 412 | if (NumParts & (NumParts - 1)) { |
| 413 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 414 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 415 | "Do not know what to expand to!"); |
| 416 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 417 | unsigned RoundBits = RoundParts * PartBits; |
| 418 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 419 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 420 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 421 | TLI.getPointerTy())); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 422 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 423 | OddParts, PartVT); |
| 424 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 425 | if (TLI.isBigEndian()) |
| 426 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 427 | std::reverse(Parts + RoundParts, Parts + NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 428 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 429 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 430 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 431 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 435 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 436 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 437 | EVT::getIntegerVT(*DAG.getContext(), |
| 438 | ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 439 | Val); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 440 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 441 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 442 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 443 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 444 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 445 | SDValue &Part0 = Parts[i]; |
| 446 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 447 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 448 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 449 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 450 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 451 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 452 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 453 | DAG.getConstant(0, PtrVT)); |
| 454 | |
| 455 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 456 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 457 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 458 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 459 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 465 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 466 | |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | // Vector ValueVT. |
| 471 | if (NumParts == 1) { |
| 472 | if (PartVT != ValueVT) { |
Bob Wilson | 5afffae | 2009-12-18 01:03:29 +0000 | [diff] [blame] | 473 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 474 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 475 | } else { |
| 476 | assert(ValueVT.getVectorElementType() == PartVT && |
| 477 | ValueVT.getVectorNumElements() == 1 && |
| 478 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 479 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 480 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 481 | DAG.getConstant(0, PtrVT)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | Parts[0] = Val; |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 490 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 491 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 492 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 493 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 494 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 495 | |
| 496 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 497 | NumParts = NumRegs; // Silence a compiler warning. |
| 498 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 499 | |
| 500 | // Split the vector into intermediate operands. |
| 501 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 502 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 503 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 504 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 505 | IntermediateVT, Val, |
| 506 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 507 | PtrVT)); |
| 508 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 509 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 510 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 511 | DAG.getConstant(i, PtrVT)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 514 | // Split the intermediate operands into legal parts. |
| 515 | if (NumParts == NumIntermediates) { |
| 516 | // If the register was not expanded, promote or copy the value, |
| 517 | // as appropriate. |
| 518 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 519 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 520 | } else if (NumParts > 0) { |
| 521 | // If the intermediate type was expanded, split each the value into |
| 522 | // legal parts. |
| 523 | assert(NumParts % NumIntermediates == 0 && |
| 524 | "Must expand into a divisible number of parts!"); |
| 525 | unsigned Factor = NumParts / NumIntermediates; |
| 526 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 527 | getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 532 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 533 | AA = &aa; |
| 534 | GFI = gfi; |
| 535 | TD = DAG.getTarget().getTargetData(); |
| 536 | } |
| 537 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 538 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 539 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 540 | /// for a new block. This doesn't clear out information about |
| 541 | /// additional blocks that are needed to complete switch lowering |
| 542 | /// or PHI node updating; that information is cleared out as it is |
| 543 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 544 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 545 | NodeMap.clear(); |
| 546 | PendingLoads.clear(); |
| 547 | PendingExports.clear(); |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 548 | CurDebugLoc = DebugLoc(); |
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 | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 615 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 616 | // Set up outgoing PHI node register values before emitting the terminator. |
| 617 | if (isa<TerminatorInst>(&I)) |
| 618 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 619 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 620 | CurDebugLoc = I.getDebugLoc(); |
| 621 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 622 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 623 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 624 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 625 | CopyToExportRegsIfNeeded(&I); |
| 626 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 627 | CurDebugLoc = DebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 630 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 631 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 632 | } |
| 633 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 634 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 635 | // Note: this doesn't use InstVisitor, because it has to work with |
| 636 | // ConstantExpr's in addition to instructions. |
| 637 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 638 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 639 | // Build the switch statement using the Instruction.def file. |
| 640 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 641 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 642 | #include "llvm/Instruction.def" |
| 643 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 644 | |
| 645 | // Assign the ordering to the freshly created DAG nodes. |
| 646 | if (NodeMap.count(&I)) { |
| 647 | ++SDNodeOrder; |
| 648 | AssignOrderingToNode(getValue(&I).getNode()); |
| 649 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 650 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 651 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 652 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 653 | SDValue &N = NodeMap[V]; |
| 654 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 655 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 656 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 657 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 658 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 659 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 660 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 661 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 662 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 663 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 664 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 665 | if (isa<ConstantPointerNull>(C)) |
| 666 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 667 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 668 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 669 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 670 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 671 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 672 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 673 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 674 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 675 | visit(CE->getOpcode(), *CE); |
| 676 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 677 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 678 | return N1; |
| 679 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 680 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 681 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 682 | SmallVector<SDValue, 4> Constants; |
| 683 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 684 | OI != OE; ++OI) { |
| 685 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 686 | // If the operand is an empty aggregate, there are no values. |
| 687 | if (!Val) continue; |
| 688 | // Add each leaf value from the operand to the Constants list |
| 689 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 690 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 691 | Constants.push_back(SDValue(Val, i)); |
| 692 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 693 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 694 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 695 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 698 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 699 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 700 | "Unknown struct or array constant!"); |
| 701 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 702 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 703 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 704 | unsigned NumElts = ValueVTs.size(); |
| 705 | if (NumElts == 0) |
| 706 | return SDValue(); // empty struct |
| 707 | SmallVector<SDValue, 4> Constants(NumElts); |
| 708 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 709 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 710 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 711 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 712 | else if (EltVT.isFloatingPoint()) |
| 713 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 714 | else |
| 715 | Constants[i] = DAG.getConstant(0, EltVT); |
| 716 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 717 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 718 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 719 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 722 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 723 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 724 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 725 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 726 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 727 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 728 | // Now that we know the number and type of the elements, get that number of |
| 729 | // elements into the Ops array based on what kind of constant it is. |
| 730 | SmallVector<SDValue, 16> Ops; |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 731 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 732 | for (unsigned i = 0; i != NumElements; ++i) |
| 733 | Ops.push_back(getValue(CP->getOperand(i))); |
| 734 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 735 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 736 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 737 | |
| 738 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 739 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 740 | Op = DAG.getConstantFP(0, EltVT); |
| 741 | else |
| 742 | Op = DAG.getConstant(0, EltVT); |
| 743 | Ops.assign(NumElements, Op); |
| 744 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 745 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 746 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 747 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 748 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 749 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 750 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 751 | // If this is a static alloca, generate it as the frameindex instead of |
| 752 | // computation. |
| 753 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 754 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 755 | FuncInfo.StaticAllocaMap.find(AI); |
| 756 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 757 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 758 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 759 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 760 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 761 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 762 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 763 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 764 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 765 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 768 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 769 | /// type of the given function. This does not require a DAG or a return value, |
| 770 | /// 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] | 771 | static void getReturnInfo(const Type* ReturnType, |
| 772 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 773 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 774 | const TargetLowering &TLI, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 775 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 776 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 777 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 778 | unsigned NumValues = ValueVTs.size(); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 779 | if (NumValues == 0) return; |
| 780 | unsigned Offset = 0; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 781 | |
| 782 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 783 | EVT VT = ValueVTs[j]; |
| 784 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 785 | |
| 786 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 787 | ExtendKind = ISD::SIGN_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 788 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 789 | ExtendKind = ISD::ZERO_EXTEND; |
| 790 | |
| 791 | // FIXME: C calling convention requires the return type to be promoted to |
| 792 | // at least 32-bit. But this is not necessary for non-C calling |
| 793 | // conventions. The frontend should mark functions whose return values |
| 794 | // require promoting with signext or zeroext attributes. |
| 795 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 796 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 797 | if (VT.bitsLT(MinVT)) |
| 798 | VT = MinVT; |
| 799 | } |
| 800 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 801 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 802 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 803 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 804 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 805 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 806 | // 'inreg' on function refers to return value |
| 807 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 808 | if (attr & Attribute::InReg) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 809 | Flags.setInReg(); |
| 810 | |
| 811 | // Propagate extension type if any |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 812 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 813 | Flags.setSExt(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 814 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 815 | Flags.setZExt(); |
| 816 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 817 | for (unsigned i = 0; i < NumParts; ++i) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 818 | OutVTs.push_back(PartVT); |
| 819 | OutFlags.push_back(Flags); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 820 | if (Offsets) |
| 821 | { |
| 822 | Offsets->push_back(Offset); |
| 823 | Offset += PartSize; |
| 824 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 828 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 829 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 830 | SDValue Chain = getControlRoot(); |
| 831 | SmallVector<ISD::OutputArg, 8> Outs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 832 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 833 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 834 | if (!FLI.CanLowerReturn) { |
| 835 | unsigned DemoteReg = FLI.DemoteRegister; |
| 836 | const Function *F = I.getParent()->getParent(); |
| 837 | |
| 838 | // Emit a store of the return value through the virtual register. |
| 839 | // Leave Outs empty so that LowerReturn won't try to load return |
| 840 | // registers the usual way. |
| 841 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 842 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 843 | PtrValueVTs); |
| 844 | |
| 845 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 846 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 847 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 848 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 849 | SmallVector<uint64_t, 4> Offsets; |
| 850 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 851 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 852 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 853 | SmallVector<SDValue, 4> Chains(NumValues); |
| 854 | EVT PtrVT = PtrValueVTs[0]; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 855 | for (unsigned i = 0; i != NumValues; ++i) { |
| 856 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 857 | DAG.getConstant(Offsets[i], PtrVT)); |
| 858 | Chains[i] = |
| 859 | DAG.getStore(Chain, getCurDebugLoc(), |
| 860 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 861 | Add, NULL, Offsets[i], false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 864 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 865 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 866 | } else if (I.getNumOperands() != 0) { |
| 867 | SmallVector<EVT, 4> ValueVTs; |
| 868 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 869 | unsigned NumValues = ValueVTs.size(); |
| 870 | if (NumValues) { |
| 871 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 872 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 873 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 874 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 875 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 876 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 877 | const Function *F = I.getParent()->getParent(); |
| 878 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 879 | ExtendKind = ISD::SIGN_EXTEND; |
| 880 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 881 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 882 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 883 | // FIXME: C calling convention requires the return type to be promoted |
| 884 | // 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] | 885 | // conventions. The frontend should mark functions whose return values |
| 886 | // require promoting with signext or zeroext attributes. |
| 887 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 888 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 889 | if (VT.bitsLT(MinVT)) |
| 890 | VT = MinVT; |
| 891 | } |
| 892 | |
| 893 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 894 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 895 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 896 | getCopyToParts(DAG, getCurDebugLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 897 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 898 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 899 | |
| 900 | // 'inreg' on function refers to return value |
| 901 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 902 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 903 | Flags.setInReg(); |
| 904 | |
| 905 | // Propagate extension type if any |
| 906 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 907 | Flags.setSExt(); |
| 908 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 909 | Flags.setZExt(); |
| 910 | |
| 911 | for (unsigned i = 0; i < NumParts; ++i) |
| 912 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 913 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 916 | |
| 917 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 918 | CallingConv::ID CallConv = |
| 919 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 920 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 921 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 922 | |
| 923 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 924 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 925 | "LowerReturn didn't return a valid chain!"); |
| 926 | |
| 927 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 928 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 931 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 932 | /// created for it, emit nodes to copy the value into the virtual |
| 933 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 934 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 935 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 936 | if (VMI != FuncInfo.ValueMap.end()) { |
| 937 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 938 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 942 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 943 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 944 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 945 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 946 | // No need to export constants. |
| 947 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 948 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 949 | // Already exported? |
| 950 | if (FuncInfo.isExportedInst(V)) return; |
| 951 | |
| 952 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 953 | CopyValueToVirtualRegister(V, Reg); |
| 954 | } |
| 955 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 956 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 957 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 958 | // The operands of the setcc have to be in this block. We don't know |
| 959 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 960 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 961 | // Can export from current BB. |
| 962 | if (VI->getParent() == FromBB) |
| 963 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 964 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 965 | // Is already exported, noop. |
| 966 | return FuncInfo.isExportedInst(V); |
| 967 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 968 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 969 | // If this is an argument, we can export it if the BB is the entry block or |
| 970 | // if it is already exported. |
| 971 | if (isa<Argument>(V)) { |
| 972 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 973 | return true; |
| 974 | |
| 975 | // Otherwise, can only export this if it is already exported. |
| 976 | return FuncInfo.isExportedInst(V); |
| 977 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 978 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 979 | // Otherwise, constants can always be exported. |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 984 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 985 | return I->getParent() == BB; |
| 986 | return true; |
| 987 | } |
| 988 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 989 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 990 | /// This function emits a branch and is used at the leaves of an OR or an |
| 991 | /// AND operator tree. |
| 992 | /// |
| 993 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 994 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 995 | MachineBasicBlock *TBB, |
| 996 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 997 | MachineBasicBlock *CurBB, |
| 998 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 999 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1000 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1001 | // If the leaf of the tree is a comparison, merge the condition into |
| 1002 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1003 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1004 | // The operands of the cmp have to be in this block. We don't know |
| 1005 | // how to export them from some other block. If this is the first block |
| 1006 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1007 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1008 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1009 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1010 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1011 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1012 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1013 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1014 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1015 | } else { |
| 1016 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1017 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1018 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1019 | |
| 1020 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1021 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1022 | SwitchCases.push_back(CB); |
| 1023 | return; |
| 1024 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1028 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1029 | NULL, TBB, FBB, CurBB); |
| 1030 | SwitchCases.push_back(CB); |
| 1031 | } |
| 1032 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1033 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1034 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1035 | MachineBasicBlock *TBB, |
| 1036 | MachineBasicBlock *FBB, |
| 1037 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1038 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1039 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1040 | // If this node is not part of the or/and tree, emit it as a branch. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1041 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1042 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1043 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1044 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1045 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1046 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1047 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1048 | return; |
| 1049 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1050 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1051 | // Create TmpBB after CurBB. |
| 1052 | MachineFunction::iterator BBI = CurBB; |
| 1053 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1054 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1055 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1056 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1057 | if (Opc == Instruction::Or) { |
| 1058 | // Codegen X | Y as: |
| 1059 | // jmp_if_X TBB |
| 1060 | // jmp TmpBB |
| 1061 | // TmpBB: |
| 1062 | // jmp_if_Y TBB |
| 1063 | // jmp FBB |
| 1064 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1065 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1066 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1067 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1068 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1069 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1070 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1073 | // Codegen X & Y as: |
| 1074 | // jmp_if_X TmpBB |
| 1075 | // jmp FBB |
| 1076 | // TmpBB: |
| 1077 | // jmp_if_Y TBB |
| 1078 | // jmp FBB |
| 1079 | // |
| 1080 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1082 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1083 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1084 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1085 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1086 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1091 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1092 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1093 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1094 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1095 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1096 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1097 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1098 | // will get folded into a single comparison, so don't emit two blocks. |
| 1099 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1100 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1101 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1102 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1103 | return false; |
| 1104 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1106 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1107 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1108 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1109 | Cases[0].CC == Cases[1].CC && |
| 1110 | isa<Constant>(Cases[0].CmpRHS) && |
| 1111 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1112 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1113 | return false; |
| 1114 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1118 | return true; |
| 1119 | } |
| 1120 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1121 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1122 | MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1123 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1124 | // Update machine-CFG edges. |
| 1125 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1126 | |
| 1127 | // Figure out which block is immediately after the current one. |
| 1128 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1129 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1130 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1131 | NextBlock = BBI; |
| 1132 | |
| 1133 | if (I.isUnconditional()) { |
| 1134 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1135 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1136 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1137 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1138 | if (Succ0MBB != NextBlock) |
| 1139 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1140 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1141 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1142 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1143 | return; |
| 1144 | } |
| 1145 | |
| 1146 | // If this condition is one of the special cases we handle, do special stuff |
| 1147 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1148 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1149 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1150 | |
| 1151 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1152 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1153 | // For example, instead of something like: |
| 1154 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1155 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1156 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1157 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1158 | // or C, F |
| 1159 | // jnz foo |
| 1160 | // Emit: |
| 1161 | // cmp A, B |
| 1162 | // je foo |
| 1163 | // cmp D, E |
| 1164 | // jle foo |
| 1165 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1166 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1167 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1168 | (BOp->getOpcode() == Instruction::And || |
| 1169 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1170 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1171 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1172 | // If the compares in later blocks need to use values not currently |
| 1173 | // exported from this block, export them now. This block should always |
| 1174 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1175 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1176 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1177 | // Allow some cases to be rejected. |
| 1178 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1179 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1180 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1181 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1182 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1183 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1184 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1185 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1186 | SwitchCases.erase(SwitchCases.begin()); |
| 1187 | return; |
| 1188 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1190 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1191 | // SwitchCases. |
| 1192 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1193 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1194 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1195 | SwitchCases.clear(); |
| 1196 | } |
| 1197 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1198 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1199 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1200 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1201 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1202 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1203 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1204 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1205 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1209 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1210 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1211 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1212 | SDValue Cond; |
| 1213 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1214 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1217 | if (CB.CmpMHS == NULL) { |
| 1218 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1219 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1220 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1221 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1222 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1223 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1224 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1225 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1226 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1227 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1228 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1229 | } else { |
| 1230 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1231 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1232 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1233 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1234 | |
| 1235 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1236 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1239 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1240 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1241 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1242 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1243 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1244 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1245 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1246 | } |
| 1247 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1248 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1249 | // Update successor info |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1250 | SwitchBB->addSuccessor(CB.TrueBB); |
| 1251 | SwitchBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1252 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1253 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1254 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1255 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1256 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1257 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1258 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1259 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1260 | // If the lhs block is the next block, invert the condition so that we can |
| 1261 | // fall through to the lhs instead of the rhs block. |
| 1262 | if (CB.TrueBB == NextBlock) { |
| 1263 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1264 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1265 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1266 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1267 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1268 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1269 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1270 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1271 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1272 | // If the branch was constant folded, fix up the CFG. |
| 1273 | if (BrCond.getOpcode() == ISD::BR) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1274 | SwitchBB->removeSuccessor(CB.FalseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1275 | } else { |
| 1276 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1277 | if (BrCond == getControlRoot()) |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1278 | SwitchBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1279 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1280 | if (CB.FalseBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1281 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1282 | DAG.getBasicBlock(CB.FalseBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1283 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1284 | |
| 1285 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1289 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1290 | // Emit the code for the jump table |
| 1291 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1292 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1293 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1294 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1295 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1296 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1297 | MVT::Other, Index.getValue(1), |
| 1298 | Table, Index); |
| 1299 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1303 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1304 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1305 | JumpTableHeader &JTH, |
| 1306 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1307 | // Subtract the lowest switch case value from the value being switched on and |
| 1308 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1309 | // difference between smallest and largest cases. |
| 1310 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1311 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1312 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1313 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1314 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1315 | // 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] | 1316 | // 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] | 1317 | // can be used as an index into the jump table in a subsequent basic block. |
| 1318 | // This value may be smaller or larger than the target's pointer type, and |
| 1319 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1320 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1321 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1322 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1323 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1324 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1325 | JT.Reg = JumpTableReg; |
| 1326 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1327 | // Emit the range check for the jump table, and branch to the default block |
| 1328 | // for the switch statement if the value being switched on exceeds the largest |
| 1329 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1330 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1331 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1332 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1333 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1336 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1337 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1338 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1339 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1340 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1341 | NextBlock = BBI; |
| 1342 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1343 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1344 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1345 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1346 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1347 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1348 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1349 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1350 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1351 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1355 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1356 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1357 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1358 | // Subtract the minimum value |
| 1359 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1360 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1361 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1362 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1365 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1366 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1367 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1368 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1369 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1370 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1371 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1372 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1373 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1374 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1375 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1376 | |
| 1377 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1378 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1379 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1380 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1381 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1382 | NextBlock = BBI; |
| 1383 | |
| 1384 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1385 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1386 | SwitchBB->addSuccessor(B.Default); |
| 1387 | SwitchBB->addSuccessor(MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1388 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1389 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1390 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1391 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1392 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1393 | if (MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1394 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1395 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1396 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1397 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /// visitBitTestCase - this function produces one "bit test" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1401 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1402 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1403 | BitTestCase &B, |
| 1404 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1405 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1406 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1407 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1408 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1409 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1410 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1411 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1412 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1413 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1414 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1415 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1416 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1417 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1418 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1419 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1420 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1421 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1422 | SwitchBB->addSuccessor(B.TargetBB); |
| 1423 | SwitchBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1424 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1425 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1426 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1427 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1430 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1431 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1432 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1433 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1434 | NextBlock = BBI; |
| 1435 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1436 | if (NextMBB != NextBlock) |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1437 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1438 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1439 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1440 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1443 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1444 | MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1445 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1446 | // Retrieve successors. |
| 1447 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1448 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1449 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1450 | const Value *Callee(I.getCalledValue()); |
| 1451 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1452 | visitInlineAsm(&I); |
| 1453 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1454 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1455 | |
| 1456 | // If the value of the invoke is used outside of its defining block, make it |
| 1457 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1458 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Update successor info |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1461 | InvokeMBB->addSuccessor(Return); |
| 1462 | InvokeMBB->addSuccessor(LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1463 | |
| 1464 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1465 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1466 | MVT::Other, getControlRoot(), |
| 1467 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1470 | void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1474 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1475 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1476 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1477 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1478 | MachineBasicBlock *Default, |
| 1479 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1480 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1481 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1482 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1483 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1484 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1485 | return false; |
| 1486 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1487 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1488 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1489 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Figure out which block is immediately after the current one. |
| 1492 | MachineBasicBlock *NextBlock = 0; |
| 1493 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1494 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1495 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1496 | NextBlock = BBI; |
| 1497 | |
| 1498 | // TODO: If any two of the cases has the same destination, and if one value |
| 1499 | // is the same as the other, but has one bit unset that the other has set, |
| 1500 | // use bit manipulation to do two compares at once. For example: |
| 1501 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1502 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1503 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1504 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1505 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1506 | // branches in this order. See if rearranging a case value would help. |
| 1507 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1508 | if (I->BB == NextBlock) { |
| 1509 | std::swap(*I, BackCase); |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1514 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1515 | // Create a CaseBlock record representing a conditional branch to |
| 1516 | // the Case's target mbb if the value being switched on SV is equal |
| 1517 | // to C. |
| 1518 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1519 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1520 | MachineBasicBlock *FallThrough; |
| 1521 | if (I != E-1) { |
| 1522 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1523 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Put SV in a virtual register to make it available from the new blocks. |
| 1526 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1527 | } else { |
| 1528 | // If the last case doesn't match, go to the default block. |
| 1529 | FallThrough = Default; |
| 1530 | } |
| 1531 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1532 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1533 | ISD::CondCode CC; |
| 1534 | if (I->High == I->Low) { |
| 1535 | // This is just small small case range :) containing exactly 1 case |
| 1536 | CC = ISD::SETEQ; |
| 1537 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1538 | } else { |
| 1539 | CC = ISD::SETLE; |
| 1540 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1541 | } |
| 1542 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1543 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1544 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1545 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1546 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1547 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1548 | if (CurBlock == SwitchBB) |
| 1549 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1550 | else |
| 1551 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1552 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1553 | CurBlock = FallThrough; |
| 1554 | } |
| 1555 | |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
| 1559 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1560 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1561 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1562 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1563 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1564 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1565 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1566 | APInt LastExt(Last), FirstExt(First); |
| 1567 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1568 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1569 | return (LastExt - FirstExt + 1ULL); |
| 1570 | } |
| 1571 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1572 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1573 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1574 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1575 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1576 | MachineBasicBlock* Default, |
| 1577 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1578 | Case& FrontCase = *CR.Range.first; |
| 1579 | Case& BackCase = *(CR.Range.second-1); |
| 1580 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1581 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1582 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1584 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1585 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1586 | I!=E; ++I) |
| 1587 | TSize += I->size(); |
| 1588 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 1589 | if (!areJTsAllowed(TLI) || TSize.ult(4)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1590 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1591 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1592 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1593 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1594 | if (Density < 0.4) |
| 1595 | return false; |
| 1596 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1597 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1598 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1599 | << "Range: " << Range |
| 1600 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1601 | |
| 1602 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1603 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1604 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1607 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1608 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1609 | |
| 1610 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1611 | |
| 1612 | // Create a new basic block to hold the code for loading the address |
| 1613 | // of the jump table, and jumping to it. Update successor information; |
| 1614 | // we will either branch to the default case for the switch, or the jump |
| 1615 | // table. |
| 1616 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1617 | CurMF->insert(BBI, JumpTableBB); |
| 1618 | CR.CaseBB->addSuccessor(Default); |
| 1619 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1620 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1621 | // Build a vector of destination BBs, corresponding to each target |
| 1622 | // of the jump table. If the value of the jump table slot corresponds to |
| 1623 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1624 | // the default BB. |
| 1625 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1626 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1627 | 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] | 1628 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1629 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1632 | DestBBs.push_back(I->BB); |
| 1633 | if (TEI==High) |
| 1634 | ++I; |
| 1635 | } else { |
| 1636 | DestBBs.push_back(Default); |
| 1637 | } |
| 1638 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1639 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1640 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1641 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1642 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1643 | E = DestBBs.end(); I != E; ++I) { |
| 1644 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1645 | SuccsHandled[(*I)->getNumber()] = true; |
| 1646 | JumpTableBB->addSuccessor(*I); |
| 1647 | } |
| 1648 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1649 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1650 | // Create a jump table index for this jump table. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1651 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1652 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1653 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1654 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1655 | // Set the jump table information so that we can codegen it as a second |
| 1656 | // MachineBasicBlock |
| 1657 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1658 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 1659 | if (CR.CaseBB == SwitchBB) |
| 1660 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1661 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1662 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1663 | |
| 1664 | return true; |
| 1665 | } |
| 1666 | |
| 1667 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1668 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1669 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1670 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1671 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1672 | MachineBasicBlock *Default, |
| 1673 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1674 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1675 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1676 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1679 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1680 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1681 | |
| 1682 | Case& FrontCase = *CR.Range.first; |
| 1683 | Case& BackCase = *(CR.Range.second-1); |
| 1684 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1685 | |
| 1686 | // Size is the number of Cases represented by this range. |
| 1687 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1688 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1689 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1690 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1691 | double FMetric = 0; |
| 1692 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1693 | |
| 1694 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1695 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1696 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1697 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1698 | I!=E; ++I) |
| 1699 | TSize += I->size(); |
| 1700 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1701 | APInt LSize = FrontCase.size(); |
| 1702 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1703 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1704 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1705 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1706 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1707 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1708 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1709 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1710 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1711 | assert((Range - 2ULL).isNonNegative() && |
| 1712 | "Invalid case distance"); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1713 | double LDensity = (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1714 | (LEnd - First + 1ULL).roundToDouble(); |
| 1715 | double RDensity = (double)RSize.roundToDouble() / |
| 1716 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1717 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1718 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1719 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1720 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1721 | << "LDensity: " << LDensity |
| 1722 | << ", RDensity: " << RDensity << '\n' |
| 1723 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1724 | if (FMetric < Metric) { |
| 1725 | Pivot = J; |
| 1726 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1727 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | LSize += J->size(); |
| 1731 | RSize -= J->size(); |
| 1732 | } |
| 1733 | if (areJTsAllowed(TLI)) { |
| 1734 | // If our case is dense we *really* should handle it earlier! |
| 1735 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1736 | } else { |
| 1737 | Pivot = CR.Range.first + Size/2; |
| 1738 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1739 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1740 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1741 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1742 | Constant *C = Pivot->Low; |
| 1743 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1744 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1745 | // 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] | 1746 | // 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] | 1747 | // 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] | 1748 | // 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] | 1749 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1750 | // rather than creating a leaf node for it. |
| 1751 | if ((LHSR.second - LHSR.first) == 1 && |
| 1752 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1753 | cast<ConstantInt>(C)->getValue() == |
| 1754 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1755 | TrueBB = LHSR.first->BB; |
| 1756 | } else { |
| 1757 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1758 | CurMF->insert(BBI, TrueBB); |
| 1759 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Put SV in a virtual register to make it available from the new blocks. |
| 1762 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1763 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1764 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1765 | // Similar to the optimization above, if the Value being switched on is |
| 1766 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1767 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1768 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1769 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1770 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1771 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1772 | FalseBB = RHSR.first->BB; |
| 1773 | } else { |
| 1774 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1775 | CurMF->insert(BBI, FalseBB); |
| 1776 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1777 | |
| 1778 | // Put SV in a virtual register to make it available from the new blocks. |
| 1779 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1783 | // 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] | 1784 | // Otherwise, branch to LHS. |
| 1785 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1786 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1787 | if (CR.CaseBB == SwitchBB) |
| 1788 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1789 | else |
| 1790 | SwitchCases.push_back(CB); |
| 1791 | |
| 1792 | return true; |
| 1793 | } |
| 1794 | |
| 1795 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1796 | /// range span less, than machine word bitwidth, encode case range into series |
| 1797 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1798 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1799 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1800 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1801 | MachineBasicBlock* Default, |
| 1802 | MachineBasicBlock *SwitchBB){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1803 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1804 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1805 | |
| 1806 | Case& FrontCase = *CR.Range.first; |
| 1807 | Case& BackCase = *(CR.Range.second-1); |
| 1808 | |
| 1809 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1810 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1811 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1812 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1813 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1814 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1815 | return false; |
| 1816 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1817 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1818 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1819 | I!=E; ++I) { |
| 1820 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1821 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1822 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1823 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1824 | // Count unique destinations |
| 1825 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1826 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1827 | Dests.insert(I->BB); |
| 1828 | if (Dests.size() > 3) |
| 1829 | // Don't bother the code below, if there are too much unique destinations |
| 1830 | return false; |
| 1831 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1832 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1833 | << Dests.size() << '\n' |
| 1834 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1835 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1836 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1837 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1838 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1839 | APInt cmpRange = maxValue - minValue; |
| 1840 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1841 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1842 | << "Low bound: " << minValue << '\n' |
| 1843 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1844 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 1845 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1846 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1847 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1848 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1849 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1850 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1851 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1852 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1854 | // Optimize the case where all the case values fit in a |
| 1855 | // word without having to subtract minValue. In this case, |
| 1856 | // we can optimize away the subtraction. |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 1857 | if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1858 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1859 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1860 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1861 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1862 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1863 | CaseBitsVector CasesBits; |
| 1864 | unsigned i, count = 0; |
| 1865 | |
| 1866 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1867 | MachineBasicBlock* Dest = I->BB; |
| 1868 | for (i = 0; i < count; ++i) |
| 1869 | if (Dest == CasesBits[i].BB) |
| 1870 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1871 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | if (i == count) { |
| 1873 | assert((count < 3) && "Too much destinations to test!"); |
| 1874 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1875 | count++; |
| 1876 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1877 | |
| 1878 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1879 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1880 | |
| 1881 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1882 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1883 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1884 | for (uint64_t j = lo; j <= hi; j++) { |
| 1885 | CasesBits[i].Mask |= 1ULL << j; |
| 1886 | CasesBits[i].Bits++; |
| 1887 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1888 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1889 | } |
| 1890 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1891 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1892 | BitTestInfo BTC; |
| 1893 | |
| 1894 | // Figure out which block is immediately after the current one. |
| 1895 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1896 | ++BBI; |
| 1897 | |
| 1898 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1899 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1900 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1901 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1902 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1903 | << ", Bits: " << CasesBits[i].Bits |
| 1904 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1905 | |
| 1906 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1907 | CurMF->insert(BBI, CaseBB); |
| 1908 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1909 | CaseBB, |
| 1910 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Put SV in a virtual register to make it available from the new blocks. |
| 1913 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1914 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1915 | |
| 1916 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1917 | -1U, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1918 | CR.CaseBB, Default, BTC); |
| 1919 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1920 | if (CR.CaseBB == SwitchBB) |
| 1921 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1922 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1923 | BitTestCases.push_back(BTB); |
| 1924 | |
| 1925 | return true; |
| 1926 | } |
| 1927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1928 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1929 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 1930 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1931 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1932 | |
| 1933 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1934 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1935 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1936 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1937 | SI.getSuccessorValue(i), |
| 1938 | SMBB)); |
| 1939 | } |
| 1940 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1941 | |
| 1942 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1943 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1944 | // Must recompute end() each iteration because it may be |
| 1945 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1946 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 1947 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 1948 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1949 | MachineBasicBlock* nextBB = J->BB; |
| 1950 | MachineBasicBlock* currentBB = I->BB; |
| 1951 | |
| 1952 | // If the two neighboring cases go to the same destination, merge them |
| 1953 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1954 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1955 | I->High = J->High; |
| 1956 | J = Cases.erase(J); |
| 1957 | } else { |
| 1958 | I = J++; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1963 | if (I->Low != I->High) |
| 1964 | // A range counts double, since it requires two compares. |
| 1965 | ++numCmps; |
| 1966 | } |
| 1967 | |
| 1968 | return numCmps; |
| 1969 | } |
| 1970 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1971 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1972 | MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()]; |
| 1973 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1974 | // Figure out which block is immediately after the current one. |
| 1975 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1976 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 1977 | |
| 1978 | // If there is only the default destination, branch to it if it is not the |
| 1979 | // next basic block. Otherwise, just fall through. |
| 1980 | if (SI.getNumOperands() == 2) { |
| 1981 | // Update machine-CFG edges. |
| 1982 | |
| 1983 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1984 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1985 | if (Default != NextBlock) |
| 1986 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1987 | MVT::Other, getControlRoot(), |
| 1988 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 1989 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1992 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1993 | // If there are any non-default case statements, create a vector of Cases |
| 1994 | // representing each one, and sort the vector so that we can efficiently |
| 1995 | // create a binary search tree from them. |
| 1996 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1997 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1998 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1999 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2000 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2001 | |
| 2002 | // Get the Value to be switched on and default basic blocks, which will be |
| 2003 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2004 | // search tree. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2005 | const Value *SV = SI.getOperand(0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Push the initial CaseRec onto the worklist |
| 2008 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2009 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2010 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2011 | |
| 2012 | while (!WorkList.empty()) { |
| 2013 | // Grab a record representing a case range to process off the worklist |
| 2014 | CaseRec CR = WorkList.back(); |
| 2015 | WorkList.pop_back(); |
| 2016 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2017 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2019 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2020 | // If the range has few cases (two or less) emit a series of specific |
| 2021 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2022 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2023 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2024 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2025 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2026 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2027 | // lowering the switch to a binary tree of conditional branches. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2028 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2029 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2030 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2031 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2032 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2033 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2037 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2038 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 2039 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2040 | // Update machine-CFG edges with unique successors. |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2041 | SmallVector<BasicBlock*, 32> succs; |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2042 | succs.reserve(I.getNumSuccessors()); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2043 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2044 | succs.push_back(I.getSuccessor(i)); |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2045 | array_pod_sort(succs.begin(), succs.end()); |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2046 | succs.erase(std::unique(succs.begin(), succs.end()), succs.end()); |
| 2047 | for (unsigned i = 0, e = succs.size(); i != e; ++i) |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2048 | IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2049 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2050 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2051 | MVT::Other, getControlRoot(), |
| 2052 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2053 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2054 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2055 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2056 | // -0.0 - X --> fneg |
| 2057 | const Type *Ty = I.getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2058 | if (Ty->isVectorTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2059 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2060 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2061 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2062 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2063 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2064 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2065 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2066 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2067 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2068 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2069 | return; |
| 2070 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2071 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2072 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2073 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2074 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2075 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2076 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2077 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2078 | Op2.getValueType(), Op2)); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2079 | return; |
| 2080 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2081 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2082 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2085 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2086 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2087 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2088 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2089 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2092 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2093 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2094 | SDValue Op2 = getValue(I.getOperand(1)); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2095 | if (!I.getType()->isVectorTy() && |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2096 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2097 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2098 | EVT PTy = TLI.getPointerTy(); |
| 2099 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2100 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2101 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2102 | TLI.getShiftAmountTy(), Op2); |
| 2103 | // If the operand is larger than the shift count type but the shift |
| 2104 | // count type has enough bits to represent any shift value, truncate |
| 2105 | // it now. This is a common case and it exposes the truncate to |
| 2106 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2107 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2108 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2109 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2110 | TLI.getShiftAmountTy(), Op2); |
| 2111 | // Otherwise we'll need to temporarily settle for some other |
| 2112 | // convenient type; type legalization will make adjustments as |
| 2113 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2114 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2115 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2116 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2117 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2118 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2119 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2120 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2121 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2122 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2123 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2126 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2127 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2128 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2129 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2130 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2131 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2132 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2133 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2134 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2135 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2136 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2137 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2140 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2141 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2142 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2143 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2144 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2145 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2146 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2147 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2148 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2149 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2150 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2153 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2154 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2155 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2156 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2157 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2158 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2159 | SmallVector<SDValue, 4> Values(NumValues); |
| 2160 | SDValue Cond = getValue(I.getOperand(0)); |
| 2161 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2162 | SDValue FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2163 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2164 | for (unsigned i = 0; i != NumValues; ++i) |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2165 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2166 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
| 2167 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2168 | SDValue(TrueVal.getNode(), |
| 2169 | TrueVal.getResNo() + i), |
| 2170 | SDValue(FalseVal.getNode(), |
| 2171 | FalseVal.getResNo() + i)); |
| 2172 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2173 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2174 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2175 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2176 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2177 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2178 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2179 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2180 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2181 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2182 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2185 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2186 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2187 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2188 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2189 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2190 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2193 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2194 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2195 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2196 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2197 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2198 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2201 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2202 | // FPTrunc is never a no-op cast, no need to check |
| 2203 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2204 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2205 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
| 2206 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2209 | void SelectionDAGBuilder::visitFPExt(const User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2210 | // FPTrunc is never a no-op cast, no need to check |
| 2211 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2212 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2213 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2216 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2217 | // FPToUI is never a no-op cast, no need to check |
| 2218 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2219 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2220 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2223 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | // FPToSI is never a no-op cast, no need to check |
| 2225 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2226 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2227 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2230 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2231 | // UIToFP 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::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2237 | void SelectionDAGBuilder::visitSIToFP(const User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2238 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2244 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2245 | // What to do depends on the size of the integer and the size of the pointer. |
| 2246 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2247 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2248 | EVT SrcVT = N.getValueType(); |
| 2249 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2250 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2253 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2254 | // What to do depends on the size of the integer and the size of the pointer. |
| 2255 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2256 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2257 | EVT SrcVT = N.getValueType(); |
| 2258 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2259 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2262 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2263 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2264 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2265 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2266 | // BitCast assures us that source and destination are the same size so this is |
| 2267 | // either a BIT_CONVERT or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2268 | if (DestVT != N.getValueType()) |
| 2269 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2270 | DestVT, N)); // convert types. |
| 2271 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2272 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2275 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2276 | SDValue InVec = getValue(I.getOperand(0)); |
| 2277 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2278 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2279 | TLI.getPointerTy(), |
| 2280 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2281 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2282 | TLI.getValueType(I.getType()), |
| 2283 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2286 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2287 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2288 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2289 | TLI.getPointerTy(), |
| 2290 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2291 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2292 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2295 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2296 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2297 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2298 | unsigned MaskNumElts = Mask.size(); |
| 2299 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2300 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2301 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2302 | return true; |
| 2303 | } |
| 2304 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2305 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2306 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2307 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2308 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2309 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2310 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2311 | // representing undef values. |
| 2312 | SmallVector<Constant*, 8> MaskElts; |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 2313 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2314 | unsigned MaskNumElts = MaskElts.size(); |
| 2315 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2316 | if (isa<UndefValue>(MaskElts[i])) |
| 2317 | Mask.push_back(-1); |
| 2318 | else |
| 2319 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2320 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2321 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2322 | EVT VT = TLI.getValueType(I.getType()); |
| 2323 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2324 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2325 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2326 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2327 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2328 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2329 | return; |
| 2330 | } |
| 2331 | |
| 2332 | // 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] | 2333 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2334 | // Mask is longer than the source vectors and is a multiple of the source |
| 2335 | // 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] | 2336 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2337 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2338 | // The shuffle is concatenating two vectors together. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2339 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2340 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2341 | return; |
| 2342 | } |
| 2343 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2344 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2345 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2346 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2347 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2348 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2349 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2350 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2351 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2352 | MOps1[0] = Src1; |
| 2353 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2354 | |
| 2355 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2356 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2357 | &MOps1[0], NumConcat); |
| 2358 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2359 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2360 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2361 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2362 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2363 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2364 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2365 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2366 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2367 | MappedOps.push_back(Idx); |
| 2368 | else |
| 2369 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2370 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2371 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2372 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2373 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2377 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2378 | // Analyze the access pattern of the vector to see if we can extract |
| 2379 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2380 | // the range of elements the mask access on both vectors. |
| 2381 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2382 | int MaxRange[2] = {-1, -1}; |
| 2383 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2384 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2385 | int Idx = Mask[i]; |
| 2386 | int Input = 0; |
| 2387 | if (Idx < 0) |
| 2388 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2389 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2390 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2391 | Input = 1; |
| 2392 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2393 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2394 | if (Idx > MaxRange[Input]) |
| 2395 | MaxRange[Input] = Idx; |
| 2396 | if (Idx < MinRange[Input]) |
| 2397 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2398 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2399 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2400 | // Check if the access is smaller than the vector size and can we find |
| 2401 | // a reasonable extract index. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2402 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2403 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2404 | int StartIdx[2]; // StartIdx to extract from |
| 2405 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2406 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2407 | RangeUse[Input] = 0; // Unused |
| 2408 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2409 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2410 | // 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] | 2411 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2412 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2413 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2414 | StartIdx[Input] = 0; |
| 2415 | } else { |
| 2416 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2417 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2418 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2419 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2420 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2421 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2424 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2425 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2426 | return; |
| 2427 | } |
| 2428 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2429 | // Extract appropriate subvector and generate a vector shuffle |
| 2430 | for (int Input=0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2431 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2432 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2433 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2434 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2435 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2436 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2437 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2438 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2439 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2440 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2441 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2442 | int Idx = Mask[i]; |
| 2443 | if (Idx < 0) |
| 2444 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2445 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2446 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2447 | else |
| 2448 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2449 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2450 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2451 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2452 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2453 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2457 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2458 | // replacing the shuffle with extract and build vector. |
| 2459 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2460 | EVT EltVT = VT.getVectorElementType(); |
| 2461 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2462 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2463 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2464 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2465 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2466 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2467 | int Idx = Mask[i]; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2468 | SDValue Res; |
| 2469 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2470 | if (Idx < (int)SrcNumElts) |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2471 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2472 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2473 | else |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2474 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2475 | EltVT, Src2, |
| 2476 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2477 | |
| 2478 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2479 | } |
| 2480 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2481 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2482 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2483 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2486 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2487 | const Value *Op0 = I.getOperand(0); |
| 2488 | const Value *Op1 = I.getOperand(1); |
| 2489 | const Type *AggTy = I.getType(); |
| 2490 | const Type *ValTy = Op1->getType(); |
| 2491 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2492 | bool FromUndef = isa<UndefValue>(Op1); |
| 2493 | |
| 2494 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2495 | I.idx_begin(), I.idx_end()); |
| 2496 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2497 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2498 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2499 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2500 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2501 | |
| 2502 | unsigned NumAggValues = AggValueVTs.size(); |
| 2503 | unsigned NumValValues = ValValueVTs.size(); |
| 2504 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2505 | |
| 2506 | SDValue Agg = getValue(Op0); |
| 2507 | SDValue Val = getValue(Op1); |
| 2508 | unsigned i = 0; |
| 2509 | // Copy the beginning value(s) from the original aggregate. |
| 2510 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2511 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2512 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2513 | // Copy values from the inserted value(s). |
| 2514 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2515 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2516 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2517 | // Copy remaining value(s) from the original aggregate. |
| 2518 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2519 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2520 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2521 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2522 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2523 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2524 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2527 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2528 | const Value *Op0 = I.getOperand(0); |
| 2529 | const Type *AggTy = Op0->getType(); |
| 2530 | const Type *ValTy = I.getType(); |
| 2531 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2532 | |
| 2533 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2534 | I.idx_begin(), I.idx_end()); |
| 2535 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2536 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2537 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2538 | |
| 2539 | unsigned NumValValues = ValValueVTs.size(); |
| 2540 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2541 | |
| 2542 | SDValue Agg = getValue(Op0); |
| 2543 | // Copy out the selected value(s). |
| 2544 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2545 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2546 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2547 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2548 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2549 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2550 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2551 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2552 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2555 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2556 | SDValue N = getValue(I.getOperand(0)); |
| 2557 | const Type *Ty = I.getOperand(0)->getType(); |
| 2558 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2559 | for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2560 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2561 | const Value *Idx = *OI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2562 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2563 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2564 | if (Field) { |
| 2565 | // N = N + Offset |
| 2566 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2567 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2568 | DAG.getIntPtrConstant(Offset)); |
| 2569 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2570 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2571 | Ty = StTy->getElementType(Field); |
Chris Lattner | 93b122d | 2010-03-16 21:25:55 +0000 | [diff] [blame] | 2572 | } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) { |
| 2573 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2574 | |
| 2575 | // Offset canonically 0 for unions, but type changes |
| 2576 | Ty = UnTy->getElementType(Field); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2577 | } else { |
| 2578 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2579 | |
| 2580 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2581 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2582 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2583 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2584 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2585 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2586 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2587 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2588 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2589 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2590 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2591 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2592 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2593 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2594 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2595 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2596 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2597 | continue; |
| 2598 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2599 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2600 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2601 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2602 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2603 | SDValue IdxN = getValue(Idx); |
| 2604 | |
| 2605 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2606 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2607 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2608 | |
| 2609 | // If this is a multiply by a power of two, turn it into a shl |
| 2610 | // immediately. This is a very common case. |
| 2611 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2612 | if (ElementSize.isPowerOf2()) { |
| 2613 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2614 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2615 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2616 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2617 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2618 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2619 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2620 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2624 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2625 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2628 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2629 | setValue(&I, N); |
| 2630 | } |
| 2631 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2632 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2633 | // If this is a fixed sized alloca in the entry block of the function, |
| 2634 | // allocate it statically on the stack. |
| 2635 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2636 | return; // getValue will auto-populate this. |
| 2637 | |
| 2638 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2639 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2640 | unsigned Align = |
| 2641 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2642 | I.getAlignment()); |
| 2643 | |
| 2644 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2646 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2647 | AllocSize, |
| 2648 | DAG.getConstant(TySize, AllocSize.getValueType())); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2649 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2650 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2651 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2652 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2653 | // Handle alignment. If the requested alignment is less than or equal to |
| 2654 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2655 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 2656 | unsigned StackAlign = TM.getFrameInfo()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2657 | if (Align <= StackAlign) |
| 2658 | Align = 0; |
| 2659 | |
| 2660 | // Round the size of the allocation up to the stack alignment size |
| 2661 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2662 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2663 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2664 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2665 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2666 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2667 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2668 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2669 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2670 | |
| 2671 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2672 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2673 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2674 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2675 | setValue(&I, DSA); |
| 2676 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2677 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2678 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2679 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2680 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2683 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2684 | const Value *SV = I.getOperand(0); |
| 2685 | SDValue Ptr = getValue(SV); |
| 2686 | |
| 2687 | const Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2688 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2689 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2690 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2691 | unsigned Alignment = I.getAlignment(); |
| 2692 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2693 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2694 | SmallVector<uint64_t, 4> Offsets; |
| 2695 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2696 | unsigned NumValues = ValueVTs.size(); |
| 2697 | if (NumValues == 0) |
| 2698 | return; |
| 2699 | |
| 2700 | SDValue Root; |
| 2701 | bool ConstantMemory = false; |
| 2702 | if (I.isVolatile()) |
| 2703 | // Serialize volatile loads with other side effects. |
| 2704 | Root = getRoot(); |
| 2705 | else if (AA->pointsToConstantMemory(SV)) { |
| 2706 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2707 | Root = DAG.getEntryNode(); |
| 2708 | ConstantMemory = true; |
| 2709 | } else { |
| 2710 | // Do not serialize non-volatile loads against each other. |
| 2711 | Root = DAG.getRoot(); |
| 2712 | } |
| 2713 | |
| 2714 | SmallVector<SDValue, 4> Values(NumValues); |
| 2715 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2716 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2717 | for (unsigned i = 0; i != NumValues; ++i) { |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2718 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2719 | PtrVT, Ptr, |
| 2720 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2721 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2722 | A, SV, Offsets[i], isVolatile, |
| 2723 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2724 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2725 | Values[i] = L; |
| 2726 | Chains[i] = L.getValue(1); |
| 2727 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2728 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2729 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2730 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2731 | MVT::Other, &Chains[0], NumValues); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2732 | if (isVolatile) |
| 2733 | DAG.setRoot(Chain); |
| 2734 | else |
| 2735 | PendingLoads.push_back(Chain); |
| 2736 | } |
| 2737 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2738 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2739 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2740 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2741 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2742 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2743 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
| 2744 | const Value *SrcV = I.getOperand(0); |
| 2745 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2746 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2747 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2748 | SmallVector<uint64_t, 4> Offsets; |
| 2749 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2750 | unsigned NumValues = ValueVTs.size(); |
| 2751 | if (NumValues == 0) |
| 2752 | return; |
| 2753 | |
| 2754 | // Get the lowered operands. Note that we do this after |
| 2755 | // checking if NumResults is zero, because with zero results |
| 2756 | // the operands won't have values in the map. |
| 2757 | SDValue Src = getValue(SrcV); |
| 2758 | SDValue Ptr = getValue(PtrV); |
| 2759 | |
| 2760 | SDValue Root = getRoot(); |
| 2761 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2762 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2763 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2764 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | unsigned Alignment = I.getAlignment(); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2766 | |
| 2767 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2768 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2769 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2770 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2771 | SDValue(Src.getNode(), Src.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2772 | Add, PtrV, Offsets[i], isVolatile, |
| 2773 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2776 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2777 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2781 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2782 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2783 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2784 | bool HasChain = !I.doesNotAccessMemory(); |
| 2785 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2786 | |
| 2787 | // Build the operand list. |
| 2788 | SmallVector<SDValue, 8> Ops; |
| 2789 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2790 | if (OnlyLoad) { |
| 2791 | // We don't need to serialize loads against other loads. |
| 2792 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2793 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2794 | Ops.push_back(getRoot()); |
| 2795 | } |
| 2796 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2797 | |
| 2798 | // Info is set by getTgtMemInstrinsic |
| 2799 | TargetLowering::IntrinsicInfo Info; |
| 2800 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2801 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2802 | // 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] | 2803 | if (!IsTgtIntrinsic) |
| 2804 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2805 | |
| 2806 | // Add all operands of the call to the operand list. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2807 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2808 | SDValue Op = getValue(I.getOperand(i)); |
| 2809 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2810 | "Intrinsic uses a non-legal type?"); |
| 2811 | Ops.push_back(Op); |
| 2812 | } |
| 2813 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2814 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2815 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2816 | #ifndef NDEBUG |
| 2817 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2818 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2819 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2820 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2821 | #endif // NDEBUG |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2822 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2823 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2824 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2825 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2826 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2827 | |
| 2828 | // Create the node. |
| 2829 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2830 | if (IsTgtIntrinsic) { |
| 2831 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2832 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2833 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2834 | Info.memVT, Info.ptrVal, Info.offset, |
| 2835 | Info.align, Info.vol, |
| 2836 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2837 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2838 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2839 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2840 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2841 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2842 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2843 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2844 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2845 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2848 | if (HasChain) { |
| 2849 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2850 | if (OnlyLoad) |
| 2851 | PendingLoads.push_back(Chain); |
| 2852 | else |
| 2853 | DAG.setRoot(Chain); |
| 2854 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2855 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2856 | if (!I.getType()->isVoidTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2857 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2858 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2859 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2860 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2861 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2862 | setValue(&I, Result); |
| 2863 | } |
| 2864 | } |
| 2865 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2866 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2867 | /// number with exponent of 1: |
| 2868 | /// |
| 2869 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2870 | /// |
| 2871 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2872 | static SDValue |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 2873 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2874 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2875 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2876 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 2877 | DAG.getConstant(0x3f800000, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2878 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2881 | /// GetExponent - Get the exponent: |
| 2882 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 2883 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2884 | /// |
| 2885 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2886 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2887 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 2888 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2889 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2890 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 2891 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2892 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2893 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 2894 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2895 | return DAG.getNode(ISD::SINT_TO_FP, 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 | /// getF32Constant - Get 32-bit floating point constant. |
| 2899 | static SDValue |
| 2900 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2901 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2904 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2905 | /// visitIntrinsicCall: I is a call instruction |
| 2906 | /// Op is the associated NodeType for I |
| 2907 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2908 | SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I, |
| 2909 | ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2910 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2911 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2912 | DAG.getAtomic(Op, getCurDebugLoc(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2913 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2914 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2915 | getValue(I.getOperand(1)), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2916 | getValue(I.getOperand(2)), |
| 2917 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2918 | setValue(&I, L); |
| 2919 | DAG.setRoot(L.getValue(1)); |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2923 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2924 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2925 | SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2926 | SDValue Op1 = getValue(I.getOperand(1)); |
| 2927 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2928 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2929 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2930 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2931 | return 0; |
| 2932 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2933 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2934 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2935 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2936 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2937 | SelectionDAGBuilder::visitExp(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2938 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2939 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2940 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2941 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2942 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 2943 | SDValue Op = getValue(I.getOperand(1)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2944 | |
| 2945 | // Put the exponent in the right bit position for later addition to the |
| 2946 | // final result: |
| 2947 | // |
| 2948 | // #define LOG2OFe 1.4426950f |
| 2949 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2950 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2951 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2952 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2953 | |
| 2954 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2955 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 2956 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2957 | |
| 2958 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2959 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2960 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2961 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2962 | if (LimitFloatPrecision <= 6) { |
| 2963 | // For floating-point precision of 6: |
| 2964 | // |
| 2965 | // TwoToFractionalPartOfX = |
| 2966 | // 0.997535578f + |
| 2967 | // (0.735607626f + 0.252464424f * x) * x; |
| 2968 | // |
| 2969 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2970 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2971 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2972 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2973 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2974 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2975 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2976 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2977 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2978 | |
| 2979 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2980 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2981 | TwoToFracPartOfX, IntegerPartOfX); |
| 2982 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2983 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2984 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 2985 | // For floating-point precision of 12: |
| 2986 | // |
| 2987 | // TwoToFractionalPartOfX = |
| 2988 | // 0.999892986f + |
| 2989 | // (0.696457318f + |
| 2990 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 2991 | // |
| 2992 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2993 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2994 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2995 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2996 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2997 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2998 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2999 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3000 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3001 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3002 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3003 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3006 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3007 | TwoToFracPartOfX, IntegerPartOfX); |
| 3008 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3009 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3010 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3011 | // For floating-point precision of 18: |
| 3012 | // |
| 3013 | // TwoToFractionalPartOfX = |
| 3014 | // 0.999999982f + |
| 3015 | // (0.693148872f + |
| 3016 | // (0.240227044f + |
| 3017 | // (0.554906021e-1f + |
| 3018 | // (0.961591928e-2f + |
| 3019 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3020 | // |
| 3021 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3022 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3023 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3024 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3025 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3026 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3027 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3028 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3029 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3030 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3031 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3032 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3033 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3034 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3035 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3036 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3037 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3038 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3039 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3040 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3041 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3042 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3045 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3046 | TwoToFracPartOfX, IntegerPartOfX); |
| 3047 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3048 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } else { |
| 3051 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3052 | result = DAG.getNode(ISD::FEXP, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3053 | getValue(I.getOperand(1)).getValueType(), |
| 3054 | getValue(I.getOperand(1))); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3057 | setValue(&I, result); |
| 3058 | } |
| 3059 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3060 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3061 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3062 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3063 | SelectionDAGBuilder::visitLog(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3064 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3065 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3066 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3067 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3068 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3069 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3070 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3071 | |
| 3072 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3073 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3074 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3075 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3076 | |
| 3077 | // Get the significand and build it into a floating-point number with |
| 3078 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3079 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3080 | |
| 3081 | if (LimitFloatPrecision <= 6) { |
| 3082 | // For floating-point precision of 6: |
| 3083 | // |
| 3084 | // LogofMantissa = |
| 3085 | // -1.1609546f + |
| 3086 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3087 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3088 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3089 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3090 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3091 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3092 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3093 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3094 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3095 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3096 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3097 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3098 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3099 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3100 | // For floating-point precision of 12: |
| 3101 | // |
| 3102 | // LogOfMantissa = |
| 3103 | // -1.7417939f + |
| 3104 | // (2.8212026f + |
| 3105 | // (-1.4699568f + |
| 3106 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3107 | // |
| 3108 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3109 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3110 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3111 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3112 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3113 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3114 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3115 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3116 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3117 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3118 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3119 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3120 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3121 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3122 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3123 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3124 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3125 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3126 | // For floating-point precision of 18: |
| 3127 | // |
| 3128 | // LogOfMantissa = |
| 3129 | // -2.1072184f + |
| 3130 | // (4.2372794f + |
| 3131 | // (-3.7029485f + |
| 3132 | // (2.2781945f + |
| 3133 | // (-0.87823314f + |
| 3134 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3135 | // |
| 3136 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3137 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3138 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3139 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3140 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3141 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3142 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3143 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3144 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3145 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3146 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3147 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3148 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3149 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3150 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3151 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3152 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3153 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3154 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3155 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3156 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3157 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3158 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } else { |
| 3161 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3162 | result = DAG.getNode(ISD::FLOG, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3163 | getValue(I.getOperand(1)).getValueType(), |
| 3164 | getValue(I.getOperand(1))); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3167 | setValue(&I, result); |
| 3168 | } |
| 3169 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3170 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3171 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3172 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3173 | SelectionDAGBuilder::visitLog2(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3174 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3175 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3176 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3177 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3178 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3179 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3180 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3181 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3182 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3183 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3184 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3185 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3186 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3187 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3188 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3189 | // Different possible minimax approximations of significand in |
| 3190 | // floating-point for various degrees of accuracy over [1,2]. |
| 3191 | if (LimitFloatPrecision <= 6) { |
| 3192 | // For floating-point precision of 6: |
| 3193 | // |
| 3194 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3195 | // |
| 3196 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3197 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3198 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3199 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3200 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3201 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3202 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3203 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3204 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3205 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3206 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3207 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3208 | // For floating-point precision of 12: |
| 3209 | // |
| 3210 | // Log2ofMantissa = |
| 3211 | // -2.51285454f + |
| 3212 | // (4.07009056f + |
| 3213 | // (-2.12067489f + |
| 3214 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3215 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3216 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3217 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3218 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3219 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3220 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3221 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3222 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3223 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3224 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3225 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3226 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3227 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3228 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3229 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3230 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3231 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3232 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3233 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3234 | // For floating-point precision of 18: |
| 3235 | // |
| 3236 | // Log2ofMantissa = |
| 3237 | // -3.0400495f + |
| 3238 | // (6.1129976f + |
| 3239 | // (-5.3420409f + |
| 3240 | // (3.2865683f + |
| 3241 | // (-1.2669343f + |
| 3242 | // (0.27515199f - |
| 3243 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3244 | // |
| 3245 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3246 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3247 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3248 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3249 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3250 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3251 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3252 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3253 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3254 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3255 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3256 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3257 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3258 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3259 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3260 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3261 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3262 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3263 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3264 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3265 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3266 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3267 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3268 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3269 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3270 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3271 | result = DAG.getNode(ISD::FLOG2, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3272 | getValue(I.getOperand(1)).getValueType(), |
| 3273 | getValue(I.getOperand(1))); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3274 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3275 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3276 | setValue(&I, result); |
| 3277 | } |
| 3278 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3279 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3280 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3281 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3282 | SelectionDAGBuilder::visitLog10(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3283 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3284 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3285 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3286 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3287 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3288 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3289 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3290 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3291 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3292 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3293 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3294 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3295 | |
| 3296 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3297 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3298 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3299 | |
| 3300 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3301 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3302 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3303 | // Log10ofMantissa = |
| 3304 | // -0.50419619f + |
| 3305 | // (0.60948995f - 0.10380950f * x) * x; |
| 3306 | // |
| 3307 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3308 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3309 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3310 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3311 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3312 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3313 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3314 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3315 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3316 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3317 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3318 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3319 | // For floating-point precision of 12: |
| 3320 | // |
| 3321 | // Log10ofMantissa = |
| 3322 | // -0.64831180f + |
| 3323 | // (0.91751397f + |
| 3324 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3325 | // |
| 3326 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3327 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3328 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3329 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3330 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3331 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3332 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3333 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3334 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3335 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3336 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3337 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3338 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3339 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3340 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3341 | // For floating-point precision of 18: |
| 3342 | // |
| 3343 | // Log10ofMantissa = |
| 3344 | // -0.84299375f + |
| 3345 | // (1.5327582f + |
| 3346 | // (-1.0688956f + |
| 3347 | // (0.49102474f + |
| 3348 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3349 | // |
| 3350 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3351 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3352 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3353 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3354 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3355 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3356 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3357 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3358 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3359 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3360 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3361 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3362 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3363 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3364 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3365 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3366 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3367 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3368 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3369 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3370 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3371 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3372 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3373 | result = DAG.getNode(ISD::FLOG10, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3374 | getValue(I.getOperand(1)).getValueType(), |
| 3375 | getValue(I.getOperand(1))); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3376 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3377 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3378 | setValue(&I, result); |
| 3379 | } |
| 3380 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3381 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3382 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3383 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3384 | SelectionDAGBuilder::visitExp2(const CallInst &I) { |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3385 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3386 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3387 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3388 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3389 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3390 | SDValue Op = getValue(I.getOperand(1)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3391 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3392 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3393 | |
| 3394 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3395 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3396 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3397 | |
| 3398 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3399 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3400 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3401 | |
| 3402 | if (LimitFloatPrecision <= 6) { |
| 3403 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3404 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3405 | // TwoToFractionalPartOfX = |
| 3406 | // 0.997535578f + |
| 3407 | // (0.735607626f + 0.252464424f * x) * x; |
| 3408 | // |
| 3409 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3410 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3411 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3412 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3413 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3414 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3415 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3416 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3417 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3418 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3419 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3420 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3421 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3422 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3423 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3424 | // For floating-point precision of 12: |
| 3425 | // |
| 3426 | // TwoToFractionalPartOfX = |
| 3427 | // 0.999892986f + |
| 3428 | // (0.696457318f + |
| 3429 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3430 | // |
| 3431 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3432 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3433 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3434 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3435 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3436 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3437 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3438 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3439 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3440 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3441 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3442 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3443 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3444 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3445 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3446 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3447 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3448 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3449 | // For floating-point precision of 18: |
| 3450 | // |
| 3451 | // TwoToFractionalPartOfX = |
| 3452 | // 0.999999982f + |
| 3453 | // (0.693148872f + |
| 3454 | // (0.240227044f + |
| 3455 | // (0.554906021e-1f + |
| 3456 | // (0.961591928e-2f + |
| 3457 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3458 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3459 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3460 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3461 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3462 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3463 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3464 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3465 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3466 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3467 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3468 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3469 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3470 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3471 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3472 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3473 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3474 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3475 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3476 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3477 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3478 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3479 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3480 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3481 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3482 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3483 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3484 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3485 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3486 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3487 | result = DAG.getNode(ISD::FEXP2, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3488 | getValue(I.getOperand(1)).getValueType(), |
| 3489 | getValue(I.getOperand(1))); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3490 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3491 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3492 | setValue(&I, result); |
| 3493 | } |
| 3494 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3495 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3496 | /// limited-precision mode with x == 10.0f. |
| 3497 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3498 | SelectionDAGBuilder::visitPow(const CallInst &I) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3499 | SDValue result; |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3500 | const Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3501 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3502 | bool IsExp10 = false; |
| 3503 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3504 | if (getValue(Val).getValueType() == MVT::f32 && |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3505 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3506 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3507 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3508 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3509 | APFloat Ten(10.0f); |
| 3510 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3511 | } |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3516 | SDValue Op = getValue(I.getOperand(2)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3517 | |
| 3518 | // Put the exponent in the right bit position for later addition to the |
| 3519 | // final result: |
| 3520 | // |
| 3521 | // #define LOG2OF10 3.3219281f |
| 3522 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3523 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3524 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3525 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3526 | |
| 3527 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3528 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3529 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3530 | |
| 3531 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3532 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3533 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3534 | |
| 3535 | if (LimitFloatPrecision <= 6) { |
| 3536 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3537 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3538 | // twoToFractionalPartOfX = |
| 3539 | // 0.997535578f + |
| 3540 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3541 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3542 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3543 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3544 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3545 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3546 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3547 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3548 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3549 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3550 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3551 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3552 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3553 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3554 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3555 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3556 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3557 | // For floating-point precision of 12: |
| 3558 | // |
| 3559 | // TwoToFractionalPartOfX = |
| 3560 | // 0.999892986f + |
| 3561 | // (0.696457318f + |
| 3562 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3563 | // |
| 3564 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3565 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3566 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3567 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3568 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3569 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3570 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3571 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3572 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3573 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3574 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3575 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3576 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3577 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3578 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3579 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3580 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3581 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3582 | // For floating-point precision of 18: |
| 3583 | // |
| 3584 | // TwoToFractionalPartOfX = |
| 3585 | // 0.999999982f + |
| 3586 | // (0.693148872f + |
| 3587 | // (0.240227044f + |
| 3588 | // (0.554906021e-1f + |
| 3589 | // (0.961591928e-2f + |
| 3590 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3591 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3592 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3593 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3594 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3595 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3596 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3597 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3598 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3599 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3600 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3601 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3602 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3603 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3604 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3605 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3606 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3607 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3608 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3609 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3610 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3611 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3612 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3613 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3614 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3615 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3616 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3617 | } |
| 3618 | } else { |
| 3619 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3620 | result = DAG.getNode(ISD::FPOW, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3621 | getValue(I.getOperand(1)).getValueType(), |
| 3622 | getValue(I.getOperand(1)), |
| 3623 | getValue(I.getOperand(2))); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | setValue(&I, result); |
| 3627 | } |
| 3628 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3629 | |
| 3630 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3631 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3632 | SelectionDAG &DAG) { |
| 3633 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3634 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3635 | // optimizing for size, we only want to do this if the expansion would produce |
| 3636 | // a small number of multiplies, otherwise we do the full expansion. |
| 3637 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3638 | // Get the exponent as a positive value. |
| 3639 | unsigned Val = RHSC->getSExtValue(); |
| 3640 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3641 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3642 | // powi(x, 0) -> 1.0 |
| 3643 | if (Val == 0) |
| 3644 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3645 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 3646 | const Function *F = DAG.getMachineFunction().getFunction(); |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3647 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3648 | // If optimizing for size, don't insert too many multiplies. This |
| 3649 | // inserts up to 5 multiplies. |
| 3650 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3651 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3652 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3653 | // powi(x,15) generates one more multiply than it should), but this has |
| 3654 | // the benefit of being both really simple and much better than a libcall. |
| 3655 | SDValue Res; // Logically starts equal to 1.0 |
| 3656 | SDValue CurSquare = LHS; |
| 3657 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3658 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3659 | if (Res.getNode()) |
| 3660 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3661 | else |
| 3662 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3663 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3664 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3665 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3666 | CurSquare, CurSquare); |
| 3667 | Val >>= 1; |
| 3668 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3669 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3670 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3671 | if (RHSC->getSExtValue() < 0) |
| 3672 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3673 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3674 | return Res; |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | // Otherwise, expand to a libcall. |
| 3679 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3680 | } |
| 3681 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3682 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 3683 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 3684 | /// At the end of instruction selection, they will be inserted to the entry BB. |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3685 | bool |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3686 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI, |
| 3687 | const Value *V, MDNode *Variable, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 3688 | uint64_t Offset, |
| 3689 | const SDValue &N) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3690 | if (!isa<Argument>(V)) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3691 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3692 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 3693 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 3694 | // Ignore inlined function arguments here. |
| 3695 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 3696 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 3697 | return false; |
| 3698 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3699 | MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()]; |
| 3700 | if (MBB != &MF.front()) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3701 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3702 | |
| 3703 | unsigned Reg = 0; |
| 3704 | if (N.getOpcode() == ISD::CopyFromReg) { |
| 3705 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
Evan Cheng | 1deef27 | 2010-04-29 00:59:34 +0000 | [diff] [blame] | 3706 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3707 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 3708 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 3709 | if (PR) |
| 3710 | Reg = PR; |
| 3711 | } |
| 3712 | } |
| 3713 | |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 3714 | if (!Reg) { |
| 3715 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 3716 | if (VMI == FuncInfo.ValueMap.end()) |
| 3717 | return false; |
| 3718 | Reg = VMI->second; |
| 3719 | } |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3720 | |
| 3721 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
| 3722 | MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(), |
| 3723 | TII->get(TargetOpcode::DBG_VALUE)) |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 3724 | .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3725 | FuncInfo.ArgDbgValues.push_back(&*MIB); |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3726 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3727 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3728 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3729 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3730 | /// we want to emit this as a call to a named external function, return the name |
| 3731 | /// otherwise lower it and return null. |
| 3732 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3733 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3734 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3735 | SDValue Res; |
| 3736 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3737 | switch (Intrinsic) { |
| 3738 | default: |
| 3739 | // By default, turn this into a target intrinsic node. |
| 3740 | visitTargetIntrinsic(I, Intrinsic); |
| 3741 | return 0; |
| 3742 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3743 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3744 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3745 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3746 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3747 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3748 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3749 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3750 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3751 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3752 | return 0; |
| 3753 | case Intrinsic::setjmp: |
| 3754 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3755 | case Intrinsic::longjmp: |
| 3756 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3757 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3758 | // Assert for address < 256 since we support only user defined address |
| 3759 | // spaces. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3760 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3761 | < 256 && |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3762 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3763 | < 256 && |
| 3764 | "Unknown address space"); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3765 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3766 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3767 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3768 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3769 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3770 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3771 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3772 | return 0; |
| 3773 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3774 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3775 | // Assert for address < 256 since we support only user defined address |
| 3776 | // spaces. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3777 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3778 | < 256 && |
| 3779 | "Unknown address space"); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3780 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3781 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3782 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3783 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3784 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3785 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3786 | I.getOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3787 | return 0; |
| 3788 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3789 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3790 | // Assert for address < 256 since we support only user defined address |
| 3791 | // spaces. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3792 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3793 | < 256 && |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3794 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3795 | < 256 && |
| 3796 | "Unknown address space"); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3797 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3798 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3799 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3800 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3801 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3802 | |
| 3803 | // If the source and destination are known to not be aliases, we can |
| 3804 | // lower memmove as memcpy. |
| 3805 | uint64_t Size = -1ULL; |
| 3806 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3807 | Size = C->getZExtValue(); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3808 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3809 | AliasAnalysis::NoAlias) { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3810 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3811 | false, I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3812 | return 0; |
| 3813 | } |
| 3814 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3815 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3816 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3817 | return 0; |
| 3818 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3819 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3820 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Chris Lattner | bf0ca2b | 2009-12-29 09:32:19 +0000 | [diff] [blame] | 3821 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3822 | return 0; |
| 3823 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3824 | MDNode *Variable = DI.getVariable(); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3825 | // Parameters are handled specially. |
Devang Patel | f38c6c8 | 2010-04-28 23:24:13 +0000 | [diff] [blame] | 3826 | bool isParameter = |
| 3827 | DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3828 | const Value *Address = DI.getAddress(); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 3829 | if (!Address) |
| 3830 | return 0; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3831 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3832 | Address = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3833 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3834 | if (AI) { |
| 3835 | // Don't handle byval arguments or VLAs, for example. |
| 3836 | // Non-byval arguments are handled here (they refer to the stack temporary |
| 3837 | // alloca at this point). |
| 3838 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3839 | FuncInfo.StaticAllocaMap.find(AI); |
| 3840 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3841 | return 0; // VLAs. |
| 3842 | int FI = SI->second; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 3843 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3844 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3845 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3846 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
| 3847 | } |
| 3848 | |
| 3849 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 3850 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 3851 | // absolute, but not relative, values are different depending on whether |
| 3852 | // debug info exists. |
| 3853 | ++SDNodeOrder; |
| 3854 | SDValue &N = NodeMap[Address]; |
| 3855 | SDDbgValue *SDV; |
| 3856 | if (N.getNode()) { |
| 3857 | if (isParameter && !AI) { |
| 3858 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 3859 | if (FINode) |
| 3860 | // Byval parameter. We have a frame index at this point. |
| 3861 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 3862 | 0, dl, SDNodeOrder); |
| 3863 | else |
| 3864 | // Can't do anything with other non-AI cases yet. This might be a |
| 3865 | // parameter of a callee function that got inlined, for example. |
| 3866 | return 0; |
| 3867 | } else if (AI) |
| 3868 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 3869 | 0, dl, SDNodeOrder); |
| 3870 | else |
| 3871 | // Can't do anything with other non-AI cases yet. |
| 3872 | return 0; |
| 3873 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 3874 | } else { |
| 3875 | // This isn't useful, but it shows what we're missing. |
| 3876 | SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()), |
| 3877 | 0, dl, SDNodeOrder); |
| 3878 | DAG.AddDbgValue(SDV, 0, isParameter); |
| 3879 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3880 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3881 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3882 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3883 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3884 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3885 | return 0; |
| 3886 | |
| 3887 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3888 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3889 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3890 | if (!V) |
| 3891 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3892 | |
| 3893 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 3894 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 3895 | // absolute, but not relative, values are different depending on whether |
| 3896 | // debug info exists. |
| 3897 | ++SDNodeOrder; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3898 | SDDbgValue *SDV; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3899 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3900 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 3901 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3902 | } else { |
Devang Patel | d47f3c8 | 2010-05-05 22:29:00 +0000 | [diff] [blame^] | 3903 | bool createUndef = false; |
| 3904 | // FIXME : Why not use getValue() directly ? |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3905 | SDValue &N = NodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3906 | if (N.getNode()) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3907 | if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) { |
| 3908 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 3909 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 3910 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 3911 | } |
Devang Patel | d47f3c8 | 2010-05-05 22:29:00 +0000 | [diff] [blame^] | 3912 | } else if (isa<PHINode>(V) && !V->use_empty()) { |
| 3913 | SDValue N = getValue(V); |
| 3914 | if (N.getNode()) { |
| 3915 | if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) { |
| 3916 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 3917 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 3918 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 3919 | } |
| 3920 | } else |
| 3921 | createUndef = true; |
| 3922 | } else |
| 3923 | createUndef = true; |
| 3924 | if (createUndef) { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3925 | // We may expand this to cover more cases. One case where we have no |
| 3926 | // data available is an unreferenced parameter; we need this fallback. |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3927 | SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()), |
| 3928 | Offset, dl, SDNodeOrder); |
| 3929 | DAG.AddDbgValue(SDV, 0, false); |
| 3930 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3934 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3935 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3936 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3937 | // Don't handle byval struct arguments or VLAs, for example. |
| 3938 | if (!AI) |
| 3939 | return 0; |
| 3940 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3941 | FuncInfo.StaticAllocaMap.find(AI); |
| 3942 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3943 | return 0; // VLAs. |
| 3944 | int FI = SI->second; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3946 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3947 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3948 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3949 | return 0; |
| 3950 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3951 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3952 | // Insert the EXCEPTIONADDR instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 3953 | assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() && |
| 3954 | "Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3955 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3956 | SDValue Ops[1]; |
| 3957 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3958 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3959 | setValue(&I, Op); |
| 3960 | DAG.setRoot(Op.getValue(1)); |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3964 | case Intrinsic::eh_selector: { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 3965 | MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()]; |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3966 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 3967 | if (CallMBB->isLandingPad()) |
| 3968 | AddCatchInfo(I, &MMI, CallMBB); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3969 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3970 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3971 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3972 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3973 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3974 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 3975 | if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3976 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3977 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3978 | // Insert the EHSELECTION instruction. |
| 3979 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3980 | SDValue Ops[2]; |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3981 | Ops[0] = getValue(I.getOperand(1)); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3982 | Ops[1] = getRoot(); |
| 3983 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3984 | DAG.setRoot(Op.getValue(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3985 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3989 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3990 | // Find the type id for the given typeinfo. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 3991 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3992 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 3993 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3994 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3995 | return 0; |
| 3996 | } |
| 3997 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3998 | case Intrinsic::eh_return_i32: |
| 3999 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4000 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
| 4001 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 4002 | MVT::Other, |
| 4003 | getControlRoot(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4004 | getValue(I.getOperand(1)), |
| 4005 | getValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4006 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4007 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4008 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4009 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4010 | case Intrinsic::eh_dwarf_cfa: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4011 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
| 4012 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4013 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4014 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4015 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4016 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4017 | TLI.getPointerTy()), |
| 4018 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4019 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4020 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4021 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4022 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 4023 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4024 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4025 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4026 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4027 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4028 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4029 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4030 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4031 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4032 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4033 | return 0; |
| 4034 | } |
| 4035 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4036 | case Intrinsic::convertff: |
| 4037 | case Intrinsic::convertfsi: |
| 4038 | case Intrinsic::convertfui: |
| 4039 | case Intrinsic::convertsif: |
| 4040 | case Intrinsic::convertuif: |
| 4041 | case Intrinsic::convertss: |
| 4042 | case Intrinsic::convertsu: |
| 4043 | case Intrinsic::convertus: |
| 4044 | case Intrinsic::convertuu: { |
| 4045 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4046 | switch (Intrinsic) { |
| 4047 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4048 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4049 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4050 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4051 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4052 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4053 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4054 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4055 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4056 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4057 | EVT DestVT = TLI.getValueType(I.getType()); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4058 | const Value *Op1 = I.getOperand(1); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4059 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 4060 | DAG.getValueType(DestVT), |
| 4061 | DAG.getValueType(getValue(Op1).getValueType()), |
| 4062 | getValue(I.getOperand(2)), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4063 | getValue(I.getOperand(3)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4064 | Code); |
| 4065 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4066 | return 0; |
| 4067 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4068 | case Intrinsic::sqrt: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4069 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4070 | getValue(I.getOperand(1)).getValueType(), |
| 4071 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4072 | return 0; |
| 4073 | case Intrinsic::powi: |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4074 | setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)), |
| 4075 | getValue(I.getOperand(2)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4076 | return 0; |
| 4077 | case Intrinsic::sin: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4078 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4079 | getValue(I.getOperand(1)).getValueType(), |
| 4080 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4081 | return 0; |
| 4082 | case Intrinsic::cos: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4083 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4084 | getValue(I.getOperand(1)).getValueType(), |
| 4085 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4086 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4087 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4088 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4089 | return 0; |
| 4090 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4091 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4092 | return 0; |
| 4093 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4094 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4095 | return 0; |
| 4096 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4097 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4098 | return 0; |
| 4099 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4100 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4101 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4102 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4103 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4104 | return 0; |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4105 | case Intrinsic::convert_to_fp16: |
| 4106 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4107 | MVT::i16, getValue(I.getOperand(1)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4108 | return 0; |
| 4109 | case Intrinsic::convert_from_fp16: |
| 4110 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4111 | MVT::f32, getValue(I.getOperand(1)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4112 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4113 | case Intrinsic::pcmarker: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4114 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4115 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4116 | return 0; |
| 4117 | } |
| 4118 | case Intrinsic::readcyclecounter: { |
| 4119 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4120 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4121 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4122 | &Op, 1); |
| 4123 | setValue(&I, Res); |
| 4124 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4125 | return 0; |
| 4126 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4127 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4128 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4129 | getValue(I.getOperand(1)).getValueType(), |
| 4130 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4131 | return 0; |
| 4132 | case Intrinsic::cttz: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4133 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4134 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4135 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4136 | return 0; |
| 4137 | } |
| 4138 | case Intrinsic::ctlz: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4139 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4140 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4141 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | case Intrinsic::ctpop: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4145 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4146 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4147 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4148 | return 0; |
| 4149 | } |
| 4150 | case Intrinsic::stacksave: { |
| 4151 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4152 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4153 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4154 | setValue(&I, Res); |
| 4155 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4156 | return 0; |
| 4157 | } |
| 4158 | case Intrinsic::stackrestore: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4159 | Res = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4160 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4161 | return 0; |
| 4162 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4163 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4164 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4165 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4166 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4167 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4168 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4169 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4170 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4171 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4172 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4173 | MFI->setStackProtectorIndex(FI); |
| 4174 | |
| 4175 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4176 | |
| 4177 | // Store the stack protector onto the stack. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4178 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4179 | PseudoSourceValue::getFixedStack(FI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4180 | 0, true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4181 | setValue(&I, Res); |
| 4182 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4183 | return 0; |
| 4184 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4185 | case Intrinsic::objectsize: { |
| 4186 | // If we don't know by now, we're never going to know. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4187 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4188 | |
| 4189 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4190 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4191 | SDValue Arg = getValue(I.getOperand(0)); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4192 | EVT Ty = Arg.getValueType(); |
| 4193 | |
Eric Christopher | d060b25 | 2009-12-23 02:51:48 +0000 | [diff] [blame] | 4194 | if (CI->getZExtValue() == 0) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4195 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4196 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4197 | Res = DAG.getConstant(0, Ty); |
| 4198 | |
| 4199 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4200 | return 0; |
| 4201 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4202 | case Intrinsic::var_annotation: |
| 4203 | // Discard annotate attributes |
| 4204 | return 0; |
| 4205 | |
| 4206 | case Intrinsic::init_trampoline: { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4207 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4208 | |
| 4209 | SDValue Ops[6]; |
| 4210 | Ops[0] = getRoot(); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4211 | Ops[1] = getValue(I.getOperand(1)); |
| 4212 | Ops[2] = getValue(I.getOperand(2)); |
| 4213 | Ops[3] = getValue(I.getOperand(3)); |
| 4214 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4215 | Ops[5] = DAG.getSrcValue(F); |
| 4216 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4217 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4218 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4219 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4220 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4221 | setValue(&I, Res); |
| 4222 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4223 | return 0; |
| 4224 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4225 | case Intrinsic::gcroot: |
| 4226 | if (GFI) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4227 | const Value *Alloca = I.getOperand(1); |
| 4228 | const Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4230 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4231 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4232 | } |
| 4233 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4234 | case Intrinsic::gcread: |
| 4235 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4236 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4237 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4238 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4239 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4240 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4241 | case Intrinsic::trap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4242 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4243 | return 0; |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4244 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4245 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4246 | case Intrinsic::sadd_with_overflow: |
| 4247 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4248 | case Intrinsic::usub_with_overflow: |
| 4249 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4250 | case Intrinsic::ssub_with_overflow: |
| 4251 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4252 | case Intrinsic::umul_with_overflow: |
| 4253 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4254 | case Intrinsic::smul_with_overflow: |
| 4255 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4256 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4257 | case Intrinsic::prefetch: { |
| 4258 | SDValue Ops[4]; |
| 4259 | Ops[0] = getRoot(); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4260 | Ops[1] = getValue(I.getOperand(1)); |
| 4261 | Ops[2] = getValue(I.getOperand(2)); |
| 4262 | Ops[3] = getValue(I.getOperand(3)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4263 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4264 | return 0; |
| 4265 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4266 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4267 | case Intrinsic::memory_barrier: { |
| 4268 | SDValue Ops[6]; |
| 4269 | Ops[0] = getRoot(); |
| 4270 | for (int x = 1; x < 6; ++x) |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4271 | Ops[x] = getValue(I.getOperand(x)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4272 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4273 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4274 | return 0; |
| 4275 | } |
| 4276 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4277 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4278 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4279 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4280 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4281 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4282 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4283 | getValue(I.getOperand(2)), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4284 | getValue(I.getOperand(3)), |
| 4285 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4286 | setValue(&I, L); |
| 4287 | DAG.setRoot(L.getValue(1)); |
| 4288 | return 0; |
| 4289 | } |
| 4290 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4291 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4292 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4293 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4294 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4295 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4296 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4297 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4298 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4299 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4300 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4301 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4302 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4303 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4304 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4305 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4306 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4307 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4308 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4309 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4310 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4311 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4312 | |
| 4313 | case Intrinsic::invariant_start: |
| 4314 | case Intrinsic::lifetime_start: |
| 4315 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4316 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4317 | return 0; |
| 4318 | case Intrinsic::invariant_end: |
| 4319 | case Intrinsic::lifetime_end: |
| 4320 | // Discard region information. |
| 4321 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4325 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4326 | bool isTailCall, |
| 4327 | MachineBasicBlock *LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4328 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4329 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4330 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4331 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 4332 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4333 | |
| 4334 | TargetLowering::ArgListTy Args; |
| 4335 | TargetLowering::ArgListEntry Entry; |
| 4336 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4337 | |
| 4338 | // Check whether the function can return without sret-demotion. |
| 4339 | SmallVector<EVT, 4> OutVTs; |
| 4340 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4341 | SmallVector<uint64_t, 4> Offsets; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4342 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4343 | OutVTs, OutsFlags, TLI, &Offsets); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4344 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4345 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4346 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4347 | |
| 4348 | SDValue DemoteStackSlot; |
| 4349 | |
| 4350 | if (!CanLowerReturn) { |
| 4351 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4352 | FTy->getReturnType()); |
| 4353 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4354 | FTy->getReturnType()); |
| 4355 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4356 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4357 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4358 | |
| 4359 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4360 | Entry.Node = DemoteStackSlot; |
| 4361 | Entry.Ty = StackSlotPtrType; |
| 4362 | Entry.isSExt = false; |
| 4363 | Entry.isZExt = false; |
| 4364 | Entry.isInReg = false; |
| 4365 | Entry.isSRet = true; |
| 4366 | Entry.isNest = false; |
| 4367 | Entry.isByVal = false; |
| 4368 | Entry.Alignment = Align; |
| 4369 | Args.push_back(Entry); |
| 4370 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4371 | } |
| 4372 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4373 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4374 | i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4375 | SDValue ArgNode = getValue(*i); |
| 4376 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4377 | |
| 4378 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4379 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4380 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4381 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4382 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4383 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4384 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4385 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4386 | Args.push_back(Entry); |
| 4387 | } |
| 4388 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4389 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4390 | // Insert a label before the invoke call to mark the try range. This can be |
| 4391 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4392 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4393 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4394 | // For SjLj, keep track of which landing pads go with which invokes |
| 4395 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4396 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4397 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4398 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4399 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4400 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4403 | // Both PendingLoads and PendingExports must be flushed here; |
| 4404 | // this call might not return. |
| 4405 | (void)getRoot(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4406 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4409 | // Check if target-independent constraints permit a tail call here. |
| 4410 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4411 | if (isTailCall && |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4412 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4413 | isTailCall = false; |
| 4414 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4415 | std::pair<SDValue,SDValue> Result = |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4416 | TLI.LowerCallTo(getRoot(), RetTy, |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4417 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4418 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4419 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4420 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4421 | isTailCall, |
| 4422 | !CS.getInstruction()->use_empty(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4423 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4424 | assert((isTailCall || Result.second.getNode()) && |
| 4425 | "Non-null chain expected with non-tail call!"); |
| 4426 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4427 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4428 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4429 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4430 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4431 | // The instruction result is the result of loading from the |
| 4432 | // hidden sret parameter. |
| 4433 | SmallVector<EVT, 1> PVTs; |
| 4434 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4435 | |
| 4436 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4437 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4438 | EVT PtrVT = PVTs[0]; |
| 4439 | unsigned NumValues = OutVTs.size(); |
| 4440 | SmallVector<SDValue, 4> Values(NumValues); |
| 4441 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4442 | |
| 4443 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4444 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4445 | DemoteStackSlot, |
| 4446 | DAG.getConstant(Offsets[i], PtrVT)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4447 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4448 | Add, NULL, Offsets[i], false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4449 | Values[i] = L; |
| 4450 | Chains[i] = L.getValue(1); |
| 4451 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4452 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4453 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4454 | MVT::Other, &Chains[0], NumValues); |
| 4455 | PendingLoads.push_back(Chain); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4456 | |
| 4457 | // Collect the legal value parts into potentially illegal values |
| 4458 | // that correspond to the original function's return values. |
| 4459 | SmallVector<EVT, 4> RetTys; |
| 4460 | RetTy = FTy->getReturnType(); |
| 4461 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4462 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4463 | SmallVector<SDValue, 4> ReturnValues; |
| 4464 | unsigned CurReg = 0; |
| 4465 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4466 | EVT VT = RetTys[I]; |
| 4467 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4468 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4469 | |
| 4470 | SDValue ReturnValue = |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4471 | getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs, |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4472 | RegisterVT, VT, AssertOp); |
| 4473 | ReturnValues.push_back(ReturnValue); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4474 | CurReg += NumRegs; |
| 4475 | } |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4476 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4477 | setValue(CS.getInstruction(), |
| 4478 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4479 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4480 | &ReturnValues[0], ReturnValues.size())); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4481 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4482 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4483 | |
| 4484 | // As a special case, a null chain means that a tail call has been emitted and |
| 4485 | // the DAG root is already updated. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4486 | if (Result.second.getNode()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4487 | DAG.setRoot(Result.second); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4488 | else |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4489 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4490 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4491 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4492 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4493 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4494 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4495 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4496 | |
| 4497 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4498 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4502 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4503 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4504 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 4505 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4506 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4507 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4508 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4509 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4510 | if (C->isNullValue()) |
| 4511 | continue; |
| 4512 | // Unknown instruction. |
| 4513 | return false; |
| 4514 | } |
| 4515 | return true; |
| 4516 | } |
| 4517 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4518 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
| 4519 | const Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4520 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4521 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4522 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4523 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4524 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4525 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4526 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4527 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4528 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4529 | if (const Constant *LoadCst = |
| 4530 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 4531 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4532 | return Builder.getValue(LoadCst); |
| 4533 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4534 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4535 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4536 | // still constant memory, the input chain can be the entry node. |
| 4537 | SDValue Root; |
| 4538 | bool ConstantMemory = 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 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4541 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4542 | Root = Builder.DAG.getEntryNode(); |
| 4543 | ConstantMemory = true; |
| 4544 | } else { |
| 4545 | // Do not serialize non-volatile loads against each other. |
| 4546 | Root = Builder.DAG.getRoot(); |
| 4547 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4548 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4549 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4550 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4551 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4552 | false /*volatile*/, |
| 4553 | false /*nontemporal*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4554 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4555 | if (!ConstantMemory) |
| 4556 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4557 | return LoadVal; |
| 4558 | } |
| 4559 | |
| 4560 | |
| 4561 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4562 | /// If so, return true and lower it, otherwise return false and it will be |
| 4563 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4564 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4565 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
| 4566 | if (I.getNumOperands() != 4) |
| 4567 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4568 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4569 | const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4570 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4571 | !I.getOperand(3)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4572 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4573 | return false; |
| 4574 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4575 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4576 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4577 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4578 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4579 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4580 | bool ActuallyDoIt = true; |
| 4581 | MVT LoadVT; |
| 4582 | const Type *LoadTy; |
| 4583 | switch (Size->getZExtValue()) { |
| 4584 | default: |
| 4585 | LoadVT = MVT::Other; |
| 4586 | LoadTy = 0; |
| 4587 | ActuallyDoIt = false; |
| 4588 | break; |
| 4589 | case 2: |
| 4590 | LoadVT = MVT::i16; |
| 4591 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4592 | break; |
| 4593 | case 4: |
| 4594 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4595 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4596 | break; |
| 4597 | case 8: |
| 4598 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4599 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4600 | break; |
| 4601 | /* |
| 4602 | case 16: |
| 4603 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4604 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4605 | LoadTy = VectorType::get(LoadTy, 4); |
| 4606 | break; |
| 4607 | */ |
| 4608 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4609 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4610 | // This turns into unaligned loads. We only do this if the target natively |
| 4611 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4612 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4613 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4614 | // Require that we can find a legal MVT, and only do this if the target |
| 4615 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4616 | // bloat the code. |
| 4617 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4618 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4619 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4620 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4621 | ActuallyDoIt = false; |
| 4622 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4623 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4624 | if (ActuallyDoIt) { |
| 4625 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4626 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4627 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4628 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4629 | ISD::SETNE); |
| 4630 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4631 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4632 | return true; |
| 4633 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4634 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4635 | |
| 4636 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4637 | return false; |
| 4638 | } |
| 4639 | |
| 4640 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4641 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4642 | const char *RenameFn = 0; |
| 4643 | if (Function *F = I.getCalledFunction()) { |
| 4644 | if (F->isDeclaration()) { |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 4645 | const TargetIntrinsicInfo *II = TM.getIntrinsicInfo(); |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4646 | if (II) { |
| 4647 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4648 | RenameFn = visitIntrinsicCall(I, IID); |
| 4649 | if (!RenameFn) |
| 4650 | return; |
| 4651 | } |
| 4652 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4653 | if (unsigned IID = F->getIntrinsicID()) { |
| 4654 | RenameFn = visitIntrinsicCall(I, IID); |
| 4655 | if (!RenameFn) |
| 4656 | return; |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4661 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4662 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4663 | StringRef Name = F->getName(); |
Duncan Sands | d2c817e | 2010-03-14 21:08:40 +0000 | [diff] [blame] | 4664 | if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4665 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4666 | I.getOperand(1)->getType()->isFloatingPointTy() && |
| 4667 | I.getType() == I.getOperand(1)->getType() && |
| 4668 | I.getType() == I.getOperand(2)->getType()) { |
| 4669 | SDValue LHS = getValue(I.getOperand(1)); |
| 4670 | SDValue RHS = getValue(I.getOperand(2)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4671 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4672 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | return; |
| 4674 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4675 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4676 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4677 | I.getOperand(1)->getType()->isFloatingPointTy() && |
| 4678 | I.getType() == I.getOperand(1)->getType()) { |
| 4679 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4680 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4681 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4682 | return; |
| 4683 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4684 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4685 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4686 | I.getOperand(1)->getType()->isFloatingPointTy() && |
| 4687 | I.getType() == I.getOperand(1)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4688 | I.onlyReadsMemory()) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4689 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4690 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4691 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4692 | return; |
| 4693 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4694 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4695 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4696 | I.getOperand(1)->getType()->isFloatingPointTy() && |
| 4697 | I.getType() == I.getOperand(1)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4698 | I.onlyReadsMemory()) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4699 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4700 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4701 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4702 | return; |
| 4703 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4704 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4705 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4706 | I.getOperand(1)->getType()->isFloatingPointTy() && |
| 4707 | I.getType() == I.getOperand(1)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4708 | I.onlyReadsMemory()) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4709 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4710 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4711 | Tmp.getValueType(), Tmp)); |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4712 | return; |
| 4713 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4714 | } else if (Name == "memcmp") { |
| 4715 | if (visitMemCmpCall(I)) |
| 4716 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4717 | } |
| 4718 | } |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4719 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4720 | visitInlineAsm(&I); |
| 4721 | return; |
| 4722 | } |
| 4723 | |
| 4724 | SDValue Callee; |
| 4725 | if (!RenameFn) |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 4726 | Callee = getValue(I.getOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4727 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4728 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4729 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4730 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4731 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 4732 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4735 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4736 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4737 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4738 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4739 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4740 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4741 | // Assemble the legal parts into the final values. |
| 4742 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4743 | SmallVector<SDValue, 8> Parts; |
| 4744 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4745 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4746 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4747 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4748 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4749 | |
| 4750 | Parts.resize(NumRegs); |
| 4751 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4752 | SDValue P; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4753 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4754 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4755 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4756 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4757 | *Flag = P.getValue(2); |
| 4758 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4759 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4760 | Chain = P.getValue(1); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4761 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4762 | // If the source register was virtual and if we know something about it, |
| 4763 | // add an assert node. |
| 4764 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4765 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4766 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4767 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4768 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4769 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4770 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4771 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4772 | unsigned NumSignBits = LOI.NumSignBits; |
| 4773 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4774 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4775 | // FIXME: We capture more information than the dag can represent. For |
| 4776 | // now, just use the tightest assertzext/assertsext possible. |
| 4777 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4778 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4779 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4780 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4781 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4782 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4783 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4784 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4785 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4786 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4787 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4788 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4789 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4790 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4791 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4792 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4793 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4794 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4795 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4796 | if (FromVT != MVT::Other) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4797 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4798 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4799 | } |
| 4800 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4801 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4802 | Parts[i] = P; |
| 4803 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4804 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4805 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4806 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4807 | Part += NumRegs; |
| 4808 | Parts.clear(); |
| 4809 | } |
| 4810 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4811 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 4812 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4813 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4817 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4818 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4819 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4820 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4821 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4822 | // Get the list of the values's legal parts. |
| 4823 | unsigned NumRegs = Regs.size(); |
| 4824 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4825 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4826 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4827 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4828 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4829 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4830 | getCopyToParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4831 | Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4832 | &Parts[Part], NumParts, RegisterVT); |
| 4833 | Part += NumParts; |
| 4834 | } |
| 4835 | |
| 4836 | // Copy the parts into the registers. |
| 4837 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4838 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4839 | SDValue Part; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4840 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4841 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4842 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4843 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4844 | *Flag = Part.getValue(1); |
| 4845 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4846 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4847 | Chains[i] = Part.getValue(0); |
| 4848 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4849 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4851 | // 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] | 4852 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4853 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4854 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4855 | // user as well as a successor (the TF operands are flagged to the user). |
| 4856 | // c1, f1 = CopyToReg |
| 4857 | // c2, f2 = CopyToReg |
| 4858 | // c3 = TokenFactor c1, c2 |
| 4859 | // ... |
| 4860 | // = op c3, ..., f2 |
| 4861 | Chain = Chains[NumRegs-1]; |
| 4862 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4863 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4867 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4868 | /// values added into it. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4869 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 4870 | unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4871 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4872 | std::vector<SDValue> &Ops) const { |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4873 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4874 | if (HasMatching) |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4875 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Dale Johannesen | 9949933 | 2009-12-23 07:32:51 +0000 | [diff] [blame] | 4876 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 4877 | Ops.push_back(Res); |
| 4878 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4879 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4880 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4881 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4882 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4883 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4884 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4885 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4886 | } |
| 4887 | } |
| 4888 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4889 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4890 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4891 | /// register class for the register. Otherwise, return null. |
| 4892 | static const TargetRegisterClass * |
| 4893 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4894 | const TargetLowering &TLI, |
| 4895 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4896 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4897 | const TargetRegisterClass *FoundRC = 0; |
| 4898 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4899 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4900 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4901 | |
| 4902 | const TargetRegisterClass *RC = *RCI; |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 4903 | // 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] | 4904 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4905 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4906 | I != E; ++I) { |
| 4907 | if (TLI.isTypeLegal(*I)) { |
| 4908 | // If we have already found this register in a different register class, |
| 4909 | // choose the one with the largest VT specified. For example, on |
| 4910 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4911 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4912 | ThisVT = *I; |
| 4913 | break; |
| 4914 | } |
| 4915 | } |
| 4916 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4917 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4918 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4919 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4920 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4921 | // frame pointer in functions that need it (due to them not being taken |
| 4922 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4923 | // yet). This is a slight code pessimization, but should still work. |
| 4924 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4925 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4926 | if (*I == Reg) { |
| 4927 | // We found a matching register class. Keep looking at others in case |
| 4928 | // we find one with larger registers that this physreg is also in. |
| 4929 | FoundRC = RC; |
| 4930 | FoundVT = ThisVT; |
| 4931 | break; |
| 4932 | } |
| 4933 | } |
| 4934 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4935 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4936 | |
| 4937 | |
| 4938 | namespace llvm { |
| 4939 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4940 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4941 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4942 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4943 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4944 | /// CallOperand - If this is the result output operand or a clobber |
| 4945 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4946 | /// This gets modified as the asm is processed. |
| 4947 | SDValue CallOperand; |
| 4948 | |
| 4949 | /// AssignedRegs - If this is a register or register class operand, this |
| 4950 | /// contains the set of register corresponding to the operand. |
| 4951 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4952 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4953 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4954 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4955 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4956 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4957 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4958 | /// busy in OutputRegs/InputRegs. |
| 4959 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4960 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4961 | std::set<unsigned> &InputRegs, |
| 4962 | const TargetRegisterInfo &TRI) const { |
| 4963 | if (isOutReg) { |
| 4964 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4965 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4966 | } |
| 4967 | if (isInReg) { |
| 4968 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4969 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4970 | } |
| 4971 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4972 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4973 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4974 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4975 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4976 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4977 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4978 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4979 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4980 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4981 | if (isa<BasicBlock>(CallOperandVal)) |
| 4982 | return TLI.getPointerTy(); |
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 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4985 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4986 | // If this is an indirect operand, the operand is a pointer to the |
| 4987 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4988 | if (isIndirect) { |
| 4989 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4990 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 4991 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4992 | OpTy = PtrTy->getElementType(); |
| 4993 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4994 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4995 | // If OpTy is not a single value, it may be a struct/union that we |
| 4996 | // can tile with integers. |
| 4997 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4998 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4999 | switch (BitSize) { |
| 5000 | default: break; |
| 5001 | case 1: |
| 5002 | case 8: |
| 5003 | case 16: |
| 5004 | case 32: |
| 5005 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 5006 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5007 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5008 | break; |
| 5009 | } |
| 5010 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5011 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5012 | return TLI.getValueType(OpTy, true); |
| 5013 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5014 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5015 | private: |
| 5016 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 5017 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5018 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5019 | const TargetRegisterInfo &TRI) { |
| 5020 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 5021 | Regs.insert(Reg); |
| 5022 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 5023 | for (; *Aliases; ++Aliases) |
| 5024 | Regs.insert(*Aliases); |
| 5025 | } |
| 5026 | }; |
| 5027 | } // end llvm namespace. |
| 5028 | |
| 5029 | |
| 5030 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5031 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5032 | /// register allocator to handle the assignment process. However, if the asm |
| 5033 | /// 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] | 5034 | /// allocation. This produces generally horrible, but correct, code. |
| 5035 | /// |
| 5036 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5037 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 5038 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5039 | void SelectionDAGBuilder:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5040 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5041 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5042 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 5043 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5044 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5045 | // Compute whether this value requires an input register, an output register, |
| 5046 | // or both. |
| 5047 | bool isOutReg = false; |
| 5048 | bool isInReg = false; |
| 5049 | switch (OpInfo.Type) { |
| 5050 | case InlineAsm::isOutput: |
| 5051 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5052 | |
| 5053 | // 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] | 5054 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5055 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5056 | break; |
| 5057 | case InlineAsm::isInput: |
| 5058 | isInReg = true; |
| 5059 | isOutReg = false; |
| 5060 | break; |
| 5061 | case InlineAsm::isClobber: |
| 5062 | isOutReg = true; |
| 5063 | isInReg = true; |
| 5064 | break; |
| 5065 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5066 | |
| 5067 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5068 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5069 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5070 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5071 | // If this is a constraint for a single physreg, or a constraint for a |
| 5072 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5073 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5074 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5075 | OpInfo.ConstraintVT); |
| 5076 | |
| 5077 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5078 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5079 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5080 | // cast of the input value. More generally, handle any case where the input |
| 5081 | // value disagrees with the register class we plan to stick this in. |
| 5082 | if (OpInfo.Type == InlineAsm::isInput && |
| 5083 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5084 | // 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] | 5085 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5086 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5087 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5088 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5089 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5090 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5091 | OpInfo.ConstraintVT = RegVT; |
| 5092 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5093 | // If the input is a FP value and we want it in FP registers, do a |
| 5094 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5095 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5096 | // machine. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5097 | RegVT = EVT::getIntegerVT(Context, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5098 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5099 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5100 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5101 | OpInfo.ConstraintVT = RegVT; |
| 5102 | } |
| 5103 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5104 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5105 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5106 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5107 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5108 | EVT RegVT; |
| 5109 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5110 | |
| 5111 | // If this is a constraint for a specific physical register, like {r17}, |
| 5112 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5113 | if (unsigned AssignedReg = PhysReg.first) { |
| 5114 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5115 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5116 | ValueVT = *RC->vt_begin(); |
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 | // Get the actual register value type. This is important, because the user |
| 5119 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5120 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5121 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5122 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5123 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5124 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5125 | |
| 5126 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5127 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5128 | TargetRegisterClass::iterator I = RC->begin(); |
| 5129 | for (; *I != AssignedReg; ++I) |
| 5130 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5131 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5132 | // Already added the first reg. |
| 5133 | --NumRegs; ++I; |
| 5134 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5135 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5136 | Regs.push_back(*I); |
| 5137 | } |
| 5138 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5139 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5140 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5141 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5142 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5143 | return; |
| 5144 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5146 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5147 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5148 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5149 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5150 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5151 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5152 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5153 | // Create the appropriate number of virtual registers. |
| 5154 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5155 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5156 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5157 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5158 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5159 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5160 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5161 | |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5162 | // This is a reference to a register class that doesn't directly correspond |
| 5163 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5164 | // registers from the class. |
| 5165 | std::vector<unsigned> RegClassRegs |
| 5166 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5167 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5168 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5169 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5170 | unsigned NumAllocated = 0; |
| 5171 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5172 | unsigned Reg = RegClassRegs[i]; |
| 5173 | // See if this register is available. |
| 5174 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5175 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5176 | // Make sure we find consecutive registers. |
| 5177 | NumAllocated = 0; |
| 5178 | continue; |
| 5179 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5180 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5181 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5182 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5183 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5184 | if (!RC) { // Couldn't allocate this register. |
| 5185 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5186 | NumAllocated = 0; |
| 5187 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5188 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5190 | // Okay, this register is good, we can use it. |
| 5191 | ++NumAllocated; |
| 5192 | |
| 5193 | // If we allocated enough consecutive registers, succeed. |
| 5194 | if (NumAllocated == NumRegs) { |
| 5195 | unsigned RegStart = (i-NumAllocated)+1; |
| 5196 | unsigned RegEnd = i+1; |
| 5197 | // Mark all of the allocated registers used. |
| 5198 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5199 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5200 | |
| 5201 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5202 | OpInfo.ConstraintVT); |
| 5203 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5204 | return; |
| 5205 | } |
| 5206 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5207 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5208 | // Otherwise, we couldn't allocate enough registers for this. |
| 5209 | } |
| 5210 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5211 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5212 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5213 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5214 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5215 | |
| 5216 | /// ConstraintOperands - Information about all of the constraints. |
| 5217 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5218 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5219 | std::set<unsigned> OutputRegs, InputRegs; |
| 5220 | |
| 5221 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5222 | // ConstraintOperands list. |
| 5223 | std::vector<InlineAsm::ConstraintInfo> |
| 5224 | ConstraintInfos = IA->ParseConstraints(); |
| 5225 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5226 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5228 | SDValue Chain, Flag; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5229 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5230 | // We won't need to flush pending loads if this asm doesn't touch |
| 5231 | // memory and is nonvolatile. |
| 5232 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5233 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5234 | else |
| 5235 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5236 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5237 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5238 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5239 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5240 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5241 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5242 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5243 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5244 | |
| 5245 | // Compute the value type for each operand. |
| 5246 | switch (OpInfo.Type) { |
| 5247 | case InlineAsm::isOutput: |
| 5248 | // Indirect outputs just consume an argument. |
| 5249 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5250 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5251 | break; |
| 5252 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5253 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5254 | // The return value of the call is this value. As such, there is no |
| 5255 | // corresponding argument. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5256 | assert(!CS.getType()->isVoidTy() && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5257 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5259 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5260 | } else { |
| 5261 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5262 | OpVT = TLI.getValueType(CS.getType()); |
| 5263 | } |
| 5264 | ++ResNo; |
| 5265 | break; |
| 5266 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5267 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5268 | break; |
| 5269 | case InlineAsm::isClobber: |
| 5270 | // Nothing to do. |
| 5271 | break; |
| 5272 | } |
| 5273 | |
| 5274 | // If this is an input or an indirect output, process the call argument. |
| 5275 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5276 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5277 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5278 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5279 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5280 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5281 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5282 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5283 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5284 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5285 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5286 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5287 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5288 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5289 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5290 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5291 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5292 | // Second pass over the constraints: compute which constraint option to use |
| 5293 | // and assign registers to constraints that want a specific physreg. |
| 5294 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5295 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5296 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5297 | // 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] | 5298 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5299 | // other is floating point, or their sizes are different, flag it as an |
| 5300 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5301 | if (OpInfo.hasMatchingInput()) { |
| 5302 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5303 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5304 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5305 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5306 | Input.ConstraintVT.isInteger()) || |
| 5307 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5308 | Input.ConstraintVT.getSizeInBits())) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5309 | report_fatal_error("Unsupported asm: input constraint" |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5310 | " with a matching output constraint of" |
| 5311 | " incompatible type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5312 | } |
| 5313 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5314 | } |
| 5315 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5316 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5317 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5318 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5319 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5320 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5321 | // need to to provide an address for the memory input. |
| 5322 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5323 | !OpInfo.isIndirect) { |
| 5324 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5325 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5326 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5327 | // Memory operands really want the address of the value. If we don't have |
| 5328 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5329 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5330 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5331 | // If the operand is a float, integer, or vector constant, spill to a |
| 5332 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5333 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5334 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5335 | isa<ConstantVector>(OpVal)) { |
| 5336 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5337 | TLI.getPointerTy()); |
| 5338 | } else { |
| 5339 | // Otherwise, create a stack slot and emit a store to it before the |
| 5340 | // asm. |
| 5341 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5342 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5343 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5344 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5345 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5346 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5347 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5348 | OpInfo.CallOperand, StackSlot, NULL, 0, |
| 5349 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5350 | OpInfo.CallOperand = StackSlot; |
| 5351 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5352 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5353 | // There is no longer a Value* corresponding to this operand. |
| 5354 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5355 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5356 | // It is now an indirect operand. |
| 5357 | OpInfo.isIndirect = true; |
| 5358 | } |
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 | // If this constraint is for a specific register, allocate it before |
| 5361 | // anything else. |
| 5362 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5363 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5364 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5365 | |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5366 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5367 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5368 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5369 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5370 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5371 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5372 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5373 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5374 | // to be. |
| 5375 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5376 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
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 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5380 | std::vector<SDValue> AsmNodeOperands; |
| 5381 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5382 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 5383 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5384 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5385 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5386 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 5387 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 5388 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 5389 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 5390 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5391 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5392 | // Loop over all of the inputs, copying the operand values into the |
| 5393 | // appropriate registers and processing the output regs. |
| 5394 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5395 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5396 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5397 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5398 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5399 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5400 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5401 | |
| 5402 | switch (OpInfo.Type) { |
| 5403 | case InlineAsm::isOutput: { |
| 5404 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5405 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5406 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5407 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5408 | |
| 5409 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5410 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 5411 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5412 | TLI.getPointerTy())); |
| 5413 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5414 | break; |
| 5415 | } |
| 5416 | |
| 5417 | // Otherwise, this is a register or register class output. |
| 5418 | |
| 5419 | // Copy the output from the appropriate register. Find a register that |
| 5420 | // we can use. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5421 | if (OpInfo.AssignedRegs.Regs.empty()) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5422 | report_fatal_error("Couldn't allocate output reg for constraint '" + |
| 5423 | Twine(OpInfo.ConstraintCode) + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5424 | |
| 5425 | // If this is an indirect operand, store through the pointer after the |
| 5426 | // asm. |
| 5427 | if (OpInfo.isIndirect) { |
| 5428 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5429 | OpInfo.CallOperandVal)); |
| 5430 | } else { |
| 5431 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5432 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5433 | // Concatenate this output onto the outputs list. |
| 5434 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5435 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5436 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5437 | // Add information to the INLINEASM node to know that this register is |
| 5438 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5439 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5440 | InlineAsm::Kind_RegDefEarlyClobber : |
| 5441 | InlineAsm::Kind_RegDef, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5442 | false, |
| 5443 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5444 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5445 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5446 | break; |
| 5447 | } |
| 5448 | case InlineAsm::isInput: { |
| 5449 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5450 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5451 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5452 | // If this is required to match an output register we have already set, |
| 5453 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5454 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5455 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5456 | // Scan until we find the definition we already emitted of this operand. |
| 5457 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5458 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5459 | for (; OperandNo; --OperandNo) { |
| 5460 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5461 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5462 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5463 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 5464 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 5465 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5466 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5469 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5470 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5471 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 5472 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5473 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 5474 | if (OpInfo.isIndirect) { |
| 5475 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 5476 | LLVMContext &Ctx = *DAG.getContext(); |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 5477 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 5478 | " don't know how to handle tied " |
| 5479 | "indirect register inputs"); |
| 5480 | } |
| 5481 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5482 | RegsForValue MatchedRegs; |
| 5483 | MatchedRegs.TLI = &TLI; |
| 5484 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5485 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5486 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5487 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5488 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5489 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5490 | MatchedRegs.Regs.push_back |
| 5491 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5492 | |
| 5493 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5494 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5495 | Chain, &Flag); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5496 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5497 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5498 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5499 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5500 | } |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5501 | |
| 5502 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 5503 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 5504 | "Unexpected number of operands"); |
| 5505 | // Add information to the INLINEASM node to know about this input. |
| 5506 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5507 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 5508 | OpInfo.getMatchedOperand()); |
| 5509 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 5510 | TLI.getPointerTy())); |
| 5511 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5512 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5513 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5514 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5515 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5516 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5517 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5518 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5519 | std::vector<SDValue> Ops; |
| 5520 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5521 | hasMemory, Ops, DAG); |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5522 | if (Ops.empty()) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5523 | report_fatal_error("Invalid operand for inline asm constraint '" + |
| 5524 | Twine(OpInfo.ConstraintCode) + "'!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5525 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5526 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5527 | unsigned ResOpType = |
| 5528 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5529 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5530 | TLI.getPointerTy())); |
| 5531 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5532 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5536 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5537 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5538 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5539 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5540 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5541 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5542 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5543 | TLI.getPointerTy())); |
| 5544 | AsmNodeOperands.push_back(InOperandVal); |
| 5545 | break; |
| 5546 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5547 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5548 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5549 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5550 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5551 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5552 | "Don't know how to handle indirect register inputs yet!"); |
| 5553 | |
| 5554 | // Copy the input into the appropriate registers. |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 5555 | if (OpInfo.AssignedRegs.Regs.empty() || |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5556 | !OpInfo.AssignedRegs.areValueTypesLegal()) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5557 | report_fatal_error("Couldn't allocate input reg for constraint '" + |
| 5558 | Twine(OpInfo.ConstraintCode) + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5559 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5560 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5561 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5562 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5563 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5564 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5565 | break; |
| 5566 | } |
| 5567 | case InlineAsm::isClobber: { |
| 5568 | // Add the clobbered value to the operand list, so that the register |
| 5569 | // allocator is aware that the physreg got clobbered. |
| 5570 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5571 | OpInfo.AssignedRegs.AddInlineAsmOperands( |
| 5572 | InlineAsm::Kind_RegDefEarlyClobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5573 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5574 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5575 | break; |
| 5576 | } |
| 5577 | } |
| 5578 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5579 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5580 | // Finish up input operands. Set the input chain and add the flag last. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5581 | AsmNodeOperands[0] = Chain; |
| 5582 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5583 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5584 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5585 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5586 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5587 | Flag = Chain.getValue(1); |
| 5588 | |
| 5589 | // If this asm returns a register value, copy the result from that register |
| 5590 | // and set it as the value of the call. |
| 5591 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5592 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5593 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5594 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5595 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5596 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5597 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5598 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5599 | // If any of the results of the inline asm is a vector, it may have the |
| 5600 | // wrong width/num elts. This can happen for register classes that can |
| 5601 | // contain multiple different value types. The preg or vreg allocated may |
| 5602 | // not have the same VT as was expected. Convert it to the right type |
| 5603 | // with bit_convert. |
| 5604 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5605 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5606 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5607 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5608 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5609 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5610 | // If a result value was tied to an input value, the computed result may |
| 5611 | // have a wider width than the expected result. Extract the relevant |
| 5612 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5613 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5614 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5615 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5616 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5617 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5618 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5619 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5620 | // Don't need to use this as a chain in this case. |
| 5621 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5622 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5623 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5624 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5625 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5626 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5627 | // Process indirect outputs, first output all of the flagged copies out of |
| 5628 | // physregs. |
| 5629 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5630 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5631 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5632 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5633 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5634 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 5635 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5636 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5637 | // Emit the non-flagged stores from the physregs. |
| 5638 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5639 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5640 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5641 | StoresToEmit[i].first, |
| 5642 | getValue(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5643 | StoresToEmit[i].second, 0, |
| 5644 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5645 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5649 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5650 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5651 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5652 | DAG.setRoot(Chain); |
| 5653 | } |
| 5654 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5655 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5656 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5657 | MVT::Other, getRoot(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 5658 | getValue(I.getOperand(1)), |
| 5659 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5662 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5663 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5664 | getRoot(), getValue(I.getOperand(0)), |
| 5665 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5666 | setValue(&I, V); |
| 5667 | DAG.setRoot(V.getValue(1)); |
| 5668 | } |
| 5669 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5670 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5671 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5672 | MVT::Other, getRoot(), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 5673 | getValue(I.getOperand(1)), |
| 5674 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5677 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5678 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5679 | MVT::Other, getRoot(), |
| 5680 | getValue(I.getOperand(1)), |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 5681 | getValue(I.getOperand(2)), |
| 5682 | DAG.getSrcValue(I.getOperand(1)), |
| 5683 | DAG.getSrcValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5686 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5687 | /// implementation, which just calls LowerCall. |
| 5688 | /// FIXME: When all targets are |
| 5689 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5690 | std::pair<SDValue, SDValue> |
| 5691 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5692 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5693 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5694 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5695 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5696 | SDValue Callee, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5697 | ArgListTy &Args, SelectionDAG &DAG, |
| 5698 | DebugLoc dl) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5699 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5700 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5701 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5702 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5703 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5704 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5705 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5706 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5707 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5708 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5709 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5710 | ISD::ArgFlagsTy Flags; |
| 5711 | unsigned OriginalAlignment = |
| 5712 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5713 | |
| 5714 | if (Args[i].isZExt) |
| 5715 | Flags.setZExt(); |
| 5716 | if (Args[i].isSExt) |
| 5717 | Flags.setSExt(); |
| 5718 | if (Args[i].isInReg) |
| 5719 | Flags.setInReg(); |
| 5720 | if (Args[i].isSRet) |
| 5721 | Flags.setSRet(); |
| 5722 | if (Args[i].isByVal) { |
| 5723 | Flags.setByVal(); |
| 5724 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5725 | const Type *ElementTy = Ty->getElementType(); |
| 5726 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5727 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5728 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5729 | // info is not there but there are cases it cannot get right. |
| 5730 | if (Args[i].Alignment) |
| 5731 | FrameAlign = Args[i].Alignment; |
| 5732 | Flags.setByValAlign(FrameAlign); |
| 5733 | Flags.setByValSize(FrameSize); |
| 5734 | } |
| 5735 | if (Args[i].isNest) |
| 5736 | Flags.setNest(); |
| 5737 | Flags.setOrigAlign(OriginalAlignment); |
| 5738 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5739 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5740 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5741 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5742 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5743 | |
| 5744 | if (Args[i].isSExt) |
| 5745 | ExtendKind = ISD::SIGN_EXTEND; |
| 5746 | else if (Args[i].isZExt) |
| 5747 | ExtendKind = ISD::ZERO_EXTEND; |
| 5748 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5749 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5750 | PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5751 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5752 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5753 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5754 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5755 | if (NumParts > 1 && j == 0) |
| 5756 | MyFlags.Flags.setSplit(); |
| 5757 | else if (j != 0) |
| 5758 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5759 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5760 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5761 | } |
| 5762 | } |
| 5763 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5764 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5765 | // Handle the incoming return values from the call. |
| 5766 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5767 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5768 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5769 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5770 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5771 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5772 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5773 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5774 | ISD::InputArg MyFlags; |
| 5775 | MyFlags.VT = RegisterVT; |
| 5776 | MyFlags.Used = isReturnValueUsed; |
| 5777 | if (RetSExt) |
| 5778 | MyFlags.Flags.setSExt(); |
| 5779 | if (RetZExt) |
| 5780 | MyFlags.Flags.setZExt(); |
| 5781 | if (isInreg) |
| 5782 | MyFlags.Flags.setInReg(); |
| 5783 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5784 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5787 | SmallVector<SDValue, 4> InVals; |
Evan Cheng | 022d9e1 | 2010-02-02 23:55:14 +0000 | [diff] [blame] | 5788 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5789 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5790 | |
| 5791 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5792 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5793 | "LowerCall didn't return a valid chain!"); |
| 5794 | assert((!isTailCall || InVals.empty()) && |
| 5795 | "LowerCall emitted a return value for a tail call!"); |
| 5796 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5797 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5798 | |
| 5799 | // For a tail call, the return value is merely live-out and there aren't |
| 5800 | // any nodes in the DAG representing it. Return a special value to |
| 5801 | // indicate that a tail call has been emitted and no more Instructions |
| 5802 | // should be processed in the current block. |
| 5803 | if (isTailCall) { |
| 5804 | DAG.setRoot(Chain); |
| 5805 | return std::make_pair(SDValue(), SDValue()); |
| 5806 | } |
| 5807 | |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 5808 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5809 | assert(InVals[i].getNode() && |
| 5810 | "LowerCall emitted a null value!"); |
| 5811 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5812 | "LowerCall emitted a value with the wrong type!"); |
| 5813 | }); |
| 5814 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5815 | // Collect the legal value parts into potentially illegal values |
| 5816 | // that correspond to the original function's return values. |
| 5817 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5818 | if (RetSExt) |
| 5819 | AssertOp = ISD::AssertSext; |
| 5820 | else if (RetZExt) |
| 5821 | AssertOp = ISD::AssertZext; |
| 5822 | SmallVector<SDValue, 4> ReturnValues; |
| 5823 | unsigned CurReg = 0; |
| 5824 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5825 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5826 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5827 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5828 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5829 | ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg], |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5830 | NumRegs, RegisterVT, VT, |
| 5831 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5832 | CurReg += NumRegs; |
| 5833 | } |
| 5834 | |
| 5835 | // For a function returning void, there is no return value. We can't create |
| 5836 | // such a node, so we just return a null return value in that case. In |
| 5837 | // that case, nothing will actualy look at the value. |
| 5838 | if (ReturnValues.empty()) |
| 5839 | return std::make_pair(SDValue(), Chain); |
| 5840 | |
| 5841 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5842 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5843 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5844 | return std::make_pair(Res, Chain); |
| 5845 | } |
| 5846 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5847 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5848 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5849 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5850 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5851 | if (Res.getNode()) |
| 5852 | Results.push_back(Res); |
| 5853 | } |
| 5854 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5855 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5856 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5857 | return SDValue(); |
| 5858 | } |
| 5859 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5860 | void |
| 5861 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5862 | SDValue Op = getValue(V); |
| 5863 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5864 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5865 | "Copy from a reg to the same reg!"); |
| 5866 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5867 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5868 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5869 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5870 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5871 | PendingExports.push_back(Chain); |
| 5872 | } |
| 5873 | |
| 5874 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5875 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5876 | void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5877 | // If this is the entry block, emit arguments. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5878 | const Function &F = *LLVMBB->getParent(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5879 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5880 | SDValue OldRoot = DAG.getRoot(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5881 | DebugLoc dl = SDB->getCurDebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5882 | const TargetData *TD = TLI.getTargetData(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5883 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5884 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5885 | // Check whether the function can return without sret-demotion. |
| 5886 | SmallVector<EVT, 4> OutVTs; |
| 5887 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5888 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5889 | OutVTs, OutsFlags, TLI); |
| 5890 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 5891 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5892 | FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5893 | OutVTs, OutsFlags, DAG); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5894 | if (!FLI.CanLowerReturn) { |
| 5895 | // Put in an sret pointer parameter before all the other parameters. |
| 5896 | SmallVector<EVT, 1> ValueVTs; |
| 5897 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5898 | |
| 5899 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5900 | // or one register. |
| 5901 | ISD::ArgFlagsTy Flags; |
| 5902 | Flags.setSRet(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 5903 | EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5904 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5905 | Ins.push_back(RetArg); |
| 5906 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5907 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5908 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5909 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5910 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5911 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5912 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5913 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5914 | bool isArgValueUsed = !I->use_empty(); |
| 5915 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5916 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5917 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5918 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5919 | ISD::ArgFlagsTy Flags; |
| 5920 | unsigned OriginalAlignment = |
| 5921 | TD->getABITypeAlignment(ArgTy); |
| 5922 | |
| 5923 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5924 | Flags.setZExt(); |
| 5925 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5926 | Flags.setSExt(); |
| 5927 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5928 | Flags.setInReg(); |
| 5929 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5930 | Flags.setSRet(); |
| 5931 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5932 | Flags.setByVal(); |
| 5933 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5934 | const Type *ElementTy = Ty->getElementType(); |
| 5935 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5936 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5937 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5938 | // this info is not there but there are cases it cannot get right. |
| 5939 | if (F.getParamAlignment(Idx)) |
| 5940 | FrameAlign = F.getParamAlignment(Idx); |
| 5941 | Flags.setByValAlign(FrameAlign); |
| 5942 | Flags.setByValSize(FrameSize); |
| 5943 | } |
| 5944 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5945 | Flags.setNest(); |
| 5946 | Flags.setOrigAlign(OriginalAlignment); |
| 5947 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5948 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5949 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5950 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5951 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5952 | if (NumRegs > 1 && i == 0) |
| 5953 | MyFlags.Flags.setSplit(); |
| 5954 | // if it isn't first piece, alignment must be 1 |
| 5955 | else if (i > 0) |
| 5956 | MyFlags.Flags.setOrigAlign(1); |
| 5957 | Ins.push_back(MyFlags); |
| 5958 | } |
| 5959 | } |
| 5960 | } |
| 5961 | |
| 5962 | // Call the target to set up the argument values. |
| 5963 | SmallVector<SDValue, 8> InVals; |
| 5964 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5965 | F.isVarArg(), Ins, |
| 5966 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5967 | |
| 5968 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5969 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5970 | "LowerFormalArguments didn't return a valid chain!"); |
| 5971 | assert(InVals.size() == Ins.size() && |
| 5972 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5973 | DEBUG({ |
| 5974 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5975 | assert(InVals[i].getNode() && |
| 5976 | "LowerFormalArguments emitted a null value!"); |
| 5977 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5978 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 5979 | } |
| 5980 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5981 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5982 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5983 | DAG.setRoot(NewRoot); |
| 5984 | |
| 5985 | // Set up the argument values. |
| 5986 | unsigned i = 0; |
| 5987 | Idx = 1; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5988 | if (!FLI.CanLowerReturn) { |
| 5989 | // Create a virtual register for the sret pointer, and put in a copy |
| 5990 | // from the sret argument into it. |
| 5991 | SmallVector<EVT, 1> ValueVTs; |
| 5992 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5993 | EVT VT = ValueVTs[0]; |
| 5994 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5995 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5996 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5997 | RegVT, VT, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5998 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5999 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6000 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 6001 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
| 6002 | FLI.DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6003 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 6004 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6005 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6006 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6007 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6008 | // Idx indexes LLVM arguments. Don't touch it. |
| 6009 | ++i; |
| 6010 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6011 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6012 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6013 | ++I, ++Idx) { |
| 6014 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6015 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6016 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6017 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6018 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6019 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6020 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6021 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6022 | |
| 6023 | if (!I->use_empty()) { |
| 6024 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 6025 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 6026 | AssertOp = ISD::AssertSext; |
| 6027 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 6028 | AssertOp = ISD::AssertZext; |
| 6029 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6030 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6031 | NumParts, PartVT, VT, |
| 6032 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6033 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6034 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6035 | i += NumParts; |
| 6036 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6037 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6038 | if (!I->use_empty()) { |
Evan Cheng | 8e36a5c | 2010-03-29 21:27:30 +0000 | [diff] [blame] | 6039 | SDValue Res; |
| 6040 | if (!ArgValues.empty()) |
| 6041 | Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 6042 | SDB->getCurDebugLoc()); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6043 | SDB->setValue(I, Res); |
| 6044 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6045 | // If this argument is live outside of the entry block, insert a copy from |
| 6046 | // 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] | 6047 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6048 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6049 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6050 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6051 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6052 | |
| 6053 | // Finally, if the target has anything special to do, allow it to do so. |
| 6054 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 6055 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6059 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6060 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6061 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6062 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6063 | /// the end. |
| 6064 | /// |
| 6065 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6066 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6067 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6068 | |
| 6069 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6070 | |
| 6071 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6072 | // from this block. |
| 6073 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6074 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6075 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6076 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6077 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6078 | // If this terminator has multiple identical successors (common for |
| 6079 | // switches), only handle each succ once. |
| 6080 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6081 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6082 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6083 | |
| 6084 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6085 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6086 | // emitted yet. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6087 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6088 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6089 | // Ignore dead phi's. |
| 6090 | if (PN->use_empty()) continue; |
| 6091 | |
| 6092 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6093 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6094 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6095 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6096 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6097 | if (RegOut == 0) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6098 | RegOut = FuncInfo.CreateRegForValue(C); |
| 6099 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6100 | } |
| 6101 | Reg = RegOut; |
| 6102 | } else { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6103 | Reg = FuncInfo.ValueMap[PHIOp]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6104 | if (Reg == 0) { |
| 6105 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6106 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6107 | "Didn't codegen value into a register!??"); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6108 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
| 6109 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | |
| 6113 | // Remember that this register needs to added to the machine PHI node as |
| 6114 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6115 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6116 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6117 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6118 | EVT VT = ValueVTs[vti]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6119 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6120 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6121 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6122 | Reg += NumRegisters; |
| 6123 | } |
| 6124 | } |
| 6125 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6126 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6127 | } |